2 * Mesa 3-D graphics library
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * Meta operations. Some GL operations can be expressed in terms of
27 * other GL operations. For example, glBlitFramebuffer() can be done
28 * with texture mapping and glClear() can be done with polygon rendering.
34 #include "main/glheader.h"
35 #include "main/mtypes.h"
36 #include "main/imports.h"
37 #include "main/arbprogram.h"
38 #include "main/arrayobj.h"
39 #include "main/blend.h"
40 #include "main/bufferobj.h"
41 #include "main/buffers.h"
42 #include "main/colortab.h"
43 #include "main/condrender.h"
44 #include "main/depth.h"
45 #include "main/enable.h"
46 #include "main/fbobject.h"
47 #include "main/feedback.h"
48 #include "main/formats.h"
49 #include "main/glformats.h"
50 #include "main/image.h"
51 #include "main/macros.h"
52 #include "main/matrix.h"
53 #include "main/mipmap.h"
54 #include "main/pixel.h"
56 #include "main/polygon.h"
57 #include "main/readpix.h"
58 #include "main/scissor.h"
59 #include "main/shaderapi.h"
60 #include "main/shaderobj.h"
61 #include "main/state.h"
62 #include "main/stencil.h"
63 #include "main/texobj.h"
64 #include "main/texenv.h"
65 #include "main/texgetimage.h"
66 #include "main/teximage.h"
67 #include "main/texparam.h"
68 #include "main/texstate.h"
69 #include "main/transformfeedback.h"
70 #include "main/uniforms.h"
71 #include "main/varray.h"
72 #include "main/viewport.h"
73 #include "program/program.h"
74 #include "swrast/swrast.h"
75 #include "drivers/common/meta.h"
78 /** Return offset in bytes of the field within a vertex struct */
79 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
82 * State which we may save/restore across meta ops.
83 * XXX this may be incomplete...
87 GLbitfield SavedState
; /**< bitmask of MESA_META_* flags */
89 /** MESA_META_ALPHA_TEST */
90 GLboolean AlphaEnabled
;
94 /** MESA_META_BLEND */
95 GLbitfield BlendEnabled
;
96 GLboolean ColorLogicOpEnabled
;
98 /** MESA_META_COLOR_MASK */
99 GLubyte ColorMask
[MAX_DRAW_BUFFERS
][4];
101 /** MESA_META_DEPTH_TEST */
102 struct gl_depthbuffer_attrib Depth
;
107 /** MESA_META_PIXEL_STORE */
108 struct gl_pixelstore_attrib Pack
, Unpack
;
110 /** MESA_META_PIXEL_TRANSFER */
111 GLfloat RedBias
, RedScale
;
112 GLfloat GreenBias
, GreenScale
;
113 GLfloat BlueBias
, BlueScale
;
114 GLfloat AlphaBias
, AlphaScale
;
115 GLfloat DepthBias
, DepthScale
;
116 GLboolean MapColorFlag
;
118 /** MESA_META_RASTERIZATION */
119 GLenum FrontPolygonMode
, BackPolygonMode
;
120 GLboolean PolygonOffset
;
121 GLboolean PolygonSmooth
;
122 GLboolean PolygonStipple
;
123 GLboolean PolygonCull
;
125 /** MESA_META_SCISSOR */
126 struct gl_scissor_attrib Scissor
;
128 /** MESA_META_SHADER */
129 GLboolean VertexProgramEnabled
;
130 struct gl_vertex_program
*VertexProgram
;
131 GLboolean FragmentProgramEnabled
;
132 struct gl_fragment_program
*FragmentProgram
;
133 struct gl_shader_program
*VertexShader
;
134 struct gl_shader_program
*GeometryShader
;
135 struct gl_shader_program
*FragmentShader
;
136 struct gl_shader_program
*ActiveShader
;
138 /** MESA_META_STENCIL_TEST */
139 struct gl_stencil_attrib Stencil
;
141 /** MESA_META_TRANSFORM */
143 GLfloat ModelviewMatrix
[16];
144 GLfloat ProjectionMatrix
[16];
145 GLfloat TextureMatrix
[16];
147 /** MESA_META_CLIP */
148 GLbitfield ClipPlanesEnabled
;
150 /** MESA_META_TEXTURE */
152 GLuint ClientActiveUnit
;
153 /** for unit[0] only */
154 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
155 /** mask of TEXTURE_2D_BIT, etc */
156 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
157 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
158 GLuint EnvMode
; /* unit[0] only */
160 /** MESA_META_VERTEX */
161 struct gl_array_object
*ArrayObj
;
162 struct gl_buffer_object
*ArrayBufferObj
;
164 /** MESA_META_VIEWPORT */
165 GLint ViewportX
, ViewportY
, ViewportW
, ViewportH
;
166 GLclampd DepthNear
, DepthFar
;
168 /** MESA_META_CLAMP_FRAGMENT_COLOR */
169 GLenum ClampFragmentColor
;
171 /** MESA_META_CLAMP_VERTEX_COLOR */
172 GLenum ClampVertexColor
;
174 /** MESA_META_CONDITIONAL_RENDER */
175 struct gl_query_object
*CondRenderQuery
;
176 GLenum CondRenderMode
;
179 /** MESA_META_SELECT_FEEDBACK */
181 struct gl_selection Select
;
182 struct gl_feedback Feedback
;
185 /** MESA_META_MULTISAMPLE */
186 GLboolean MultisampleEnabled
;
188 /** Miscellaneous (always disabled) */
190 GLboolean RasterDiscard
;
191 #if FEATURE_EXT_transform_feedback
192 GLboolean TransformFeedbackNeedsResume
;
197 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
198 * This is currently shared by all the meta ops. But we could create a
199 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
204 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
205 GLsizei MinSize
; /**< Min texture size to allocate */
206 GLsizei MaxSize
; /**< Max possible texture size */
207 GLboolean NPOT
; /**< Non-power of two size OK? */
208 GLsizei Width
, Height
; /**< Current texture size */
210 GLfloat Sright
, Ttop
; /**< right, top texcoords */
215 * State for glBlitFramebufer()
226 * State for glClear()
235 GLuint IntegerShaderProg
;
236 GLint IntegerColorLocation
;
241 * State for glCopyPixels()
251 * State for glDrawPixels()
257 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
258 GLuint DepthFP
; /**< Fragment program for drawing depth images */
263 * State for glBitmap()
269 struct temp_texture Tex
; /**< separate texture from other meta ops */
274 * State for _mesa_meta_generate_mipmap()
276 struct gen_mipmap_state
285 * State for texture decompression
287 struct decompress_state
290 GLuint VBO
, FBO
, RBO
;
295 * State for glDrawTex()
303 #define MAX_META_OPS_DEPTH 8
305 * All per-context meta state.
309 /** Stack of state saved during meta-ops */
310 struct save_state Save
[MAX_META_OPS_DEPTH
];
311 /** Save stack depth */
312 GLuint SaveStackDepth
;
314 struct temp_texture TempTex
;
316 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
317 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
318 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
319 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
320 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
321 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
322 struct decompress_state Decompress
; /**< For texture decompression */
323 struct drawtex_state DrawTex
; /**< For _mesa_meta_DrawTex() */
326 static void meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
);
327 static void cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
);
328 static void meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
);
331 compile_shader_with_debug(struct gl_context
*ctx
, GLenum target
, const GLcharARB
*source
)
337 shader
= _mesa_CreateShaderObjectARB(target
);
338 _mesa_ShaderSourceARB(shader
, 1, &source
, NULL
);
339 _mesa_CompileShaderARB(shader
);
341 _mesa_GetShaderiv(shader
, GL_COMPILE_STATUS
, &ok
);
345 _mesa_GetShaderiv(shader
, GL_INFO_LOG_LENGTH
, &size
);
347 _mesa_DeleteObjectARB(shader
);
353 _mesa_DeleteObjectARB(shader
);
357 _mesa_GetProgramInfoLog(shader
, size
, NULL
, info
);
359 "meta program compile failed:\n%s\n"
364 _mesa_DeleteObjectARB(shader
);
370 link_program_with_debug(struct gl_context
*ctx
, GLuint program
)
375 _mesa_LinkProgramARB(program
);
377 _mesa_GetProgramiv(program
, GL_LINK_STATUS
, &ok
);
381 _mesa_GetProgramiv(program
, GL_INFO_LOG_LENGTH
, &size
);
389 _mesa_GetProgramInfoLog(program
, size
, NULL
, info
);
390 _mesa_problem(ctx
, "meta program link failed:\n%s", info
);
398 * Initialize meta-ops for a context.
399 * To be called once during context creation.
402 _mesa_meta_init(struct gl_context
*ctx
)
406 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
411 * Free context meta-op state.
412 * To be called once during context destruction.
415 _mesa_meta_free(struct gl_context
*ctx
)
417 GET_CURRENT_CONTEXT(old_context
);
418 _mesa_make_current(ctx
, NULL
, NULL
);
419 meta_glsl_blit_cleanup(ctx
, &ctx
->Meta
->Blit
);
420 meta_glsl_clear_cleanup(ctx
, &ctx
->Meta
->Clear
);
421 cleanup_temp_texture(ctx
, &ctx
->Meta
->TempTex
);
423 _mesa_make_current(old_context
, old_context
->WinSysDrawBuffer
, old_context
->WinSysReadBuffer
);
425 _mesa_make_current(NULL
, NULL
, NULL
);
432 * Enter meta state. This is like a light-weight version of glPushAttrib
433 * but it also resets most GL state back to default values.
435 * \param state bitmask of MESA_META_* flags indicating which attribute groups
436 * to save and reset to their defaults
439 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
441 struct save_state
*save
;
443 /* hope MAX_META_OPS_DEPTH is large enough */
444 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
446 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
447 memset(save
, 0, sizeof(*save
));
448 save
->SavedState
= state
;
450 #if FEATURE_EXT_transform_feedback
451 /* Pausing transform feedback needs to be done early, or else we won't be
452 * able to change other state.
454 save
->TransformFeedbackNeedsResume
=
455 ctx
->TransformFeedback
.CurrentObject
->Active
&&
456 !ctx
->TransformFeedback
.CurrentObject
->Paused
;
457 if (save
->TransformFeedbackNeedsResume
)
458 _mesa_PauseTransformFeedback();
461 if (state
& MESA_META_ALPHA_TEST
) {
462 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
463 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
464 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
465 if (ctx
->Color
.AlphaEnabled
)
466 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
469 if (state
& MESA_META_BLEND
) {
470 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
471 if (ctx
->Color
.BlendEnabled
) {
472 if (ctx
->Extensions
.EXT_draw_buffers2
) {
474 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
475 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
479 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
482 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
483 if (ctx
->Color
.ColorLogicOpEnabled
)
484 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
487 if (state
& MESA_META_COLOR_MASK
) {
488 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
489 sizeof(ctx
->Color
.ColorMask
));
490 if (!ctx
->Color
.ColorMask
[0][0] ||
491 !ctx
->Color
.ColorMask
[0][1] ||
492 !ctx
->Color
.ColorMask
[0][2] ||
493 !ctx
->Color
.ColorMask
[0][3])
494 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
497 if (state
& MESA_META_DEPTH_TEST
) {
498 save
->Depth
= ctx
->Depth
; /* struct copy */
500 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
503 if (state
& MESA_META_FOG
) {
504 save
->Fog
= ctx
->Fog
.Enabled
;
505 if (ctx
->Fog
.Enabled
)
506 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
509 if (state
& MESA_META_PIXEL_STORE
) {
510 save
->Pack
= ctx
->Pack
;
511 save
->Unpack
= ctx
->Unpack
;
512 ctx
->Pack
= ctx
->DefaultPacking
;
513 ctx
->Unpack
= ctx
->DefaultPacking
;
516 if (state
& MESA_META_PIXEL_TRANSFER
) {
517 save
->RedScale
= ctx
->Pixel
.RedScale
;
518 save
->RedBias
= ctx
->Pixel
.RedBias
;
519 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
520 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
521 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
522 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
523 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
524 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
525 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
526 ctx
->Pixel
.RedScale
= 1.0F
;
527 ctx
->Pixel
.RedBias
= 0.0F
;
528 ctx
->Pixel
.GreenScale
= 1.0F
;
529 ctx
->Pixel
.GreenBias
= 0.0F
;
530 ctx
->Pixel
.BlueScale
= 1.0F
;
531 ctx
->Pixel
.BlueBias
= 0.0F
;
532 ctx
->Pixel
.AlphaScale
= 1.0F
;
533 ctx
->Pixel
.AlphaBias
= 0.0F
;
534 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
536 ctx
->NewState
|=_NEW_PIXEL
;
539 if (state
& MESA_META_RASTERIZATION
) {
540 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
541 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
542 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
543 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
544 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
545 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
546 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
547 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
548 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
549 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
550 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
553 if (state
& MESA_META_SCISSOR
) {
554 save
->Scissor
= ctx
->Scissor
; /* struct copy */
555 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
558 if (state
& MESA_META_SHADER
) {
559 if (ctx
->Extensions
.ARB_vertex_program
) {
560 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
561 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
562 ctx
->VertexProgram
.Current
);
563 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
566 if (ctx
->Extensions
.ARB_fragment_program
) {
567 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
568 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
569 ctx
->FragmentProgram
.Current
);
570 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
573 if (ctx
->Extensions
.ARB_shader_objects
) {
574 _mesa_reference_shader_program(ctx
, &save
->VertexShader
,
575 ctx
->Shader
.CurrentVertexProgram
);
576 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
,
577 ctx
->Shader
.CurrentGeometryProgram
);
578 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
,
579 ctx
->Shader
.CurrentFragmentProgram
);
580 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
581 ctx
->Shader
.ActiveProgram
);
583 _mesa_UseProgramObjectARB(0);
587 if (state
& MESA_META_STENCIL_TEST
) {
588 save
->Stencil
= ctx
->Stencil
; /* struct copy */
589 if (ctx
->Stencil
.Enabled
)
590 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
591 /* NOTE: other stencil state not reset */
594 if (state
& MESA_META_TEXTURE
) {
597 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
598 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
599 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
601 /* Disable all texture units */
602 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
603 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
604 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
605 if (ctx
->Texture
.Unit
[u
].Enabled
||
606 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
607 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
608 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
609 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
610 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
611 if (ctx
->Extensions
.ARB_texture_cube_map
)
612 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
613 if (ctx
->Extensions
.NV_texture_rectangle
)
614 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
615 if (ctx
->Extensions
.OES_EGL_image_external
)
616 _mesa_set_enable(ctx
, GL_TEXTURE_EXTERNAL_OES
, GL_FALSE
);
617 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
618 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
619 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
620 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
624 /* save current texture objects for unit[0] only */
625 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
626 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
627 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
630 /* set defaults for unit[0] */
631 _mesa_ActiveTextureARB(GL_TEXTURE0
);
632 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
633 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
636 if (state
& MESA_META_TRANSFORM
) {
637 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
638 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
639 16 * sizeof(GLfloat
));
640 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
641 16 * sizeof(GLfloat
));
642 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
643 16 * sizeof(GLfloat
));
644 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
645 /* set 1:1 vertex:pixel coordinate transform */
646 _mesa_ActiveTextureARB(GL_TEXTURE0
);
647 _mesa_MatrixMode(GL_TEXTURE
);
648 _mesa_LoadIdentity();
649 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
650 _mesa_MatrixMode(GL_MODELVIEW
);
651 _mesa_LoadIdentity();
652 _mesa_MatrixMode(GL_PROJECTION
);
653 _mesa_LoadIdentity();
654 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
655 0.0, ctx
->DrawBuffer
->Height
,
659 if (state
& MESA_META_CLIP
) {
660 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
661 if (ctx
->Transform
.ClipPlanesEnabled
) {
663 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
664 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
669 if (state
& MESA_META_VERTEX
) {
670 /* save vertex array object state */
671 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
672 ctx
->Array
.ArrayObj
);
673 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
674 ctx
->Array
.ArrayBufferObj
);
675 /* set some default state? */
678 if (state
& MESA_META_VIEWPORT
) {
679 /* save viewport state */
680 save
->ViewportX
= ctx
->Viewport
.X
;
681 save
->ViewportY
= ctx
->Viewport
.Y
;
682 save
->ViewportW
= ctx
->Viewport
.Width
;
683 save
->ViewportH
= ctx
->Viewport
.Height
;
684 /* set viewport to match window size */
685 if (ctx
->Viewport
.X
!= 0 ||
686 ctx
->Viewport
.Y
!= 0 ||
687 ctx
->Viewport
.Width
!= ctx
->DrawBuffer
->Width
||
688 ctx
->Viewport
.Height
!= ctx
->DrawBuffer
->Height
) {
689 _mesa_set_viewport(ctx
, 0, 0,
690 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
692 /* save depth range state */
693 save
->DepthNear
= ctx
->Viewport
.Near
;
694 save
->DepthFar
= ctx
->Viewport
.Far
;
695 /* set depth range to default */
696 _mesa_DepthRange(0.0, 1.0);
699 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
700 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
702 /* Generally in here we want to do clamping according to whether
703 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
704 * regardless of the internal implementation of the metaops.
706 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
)
707 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
710 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
711 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
713 /* Generally in here we never want vertex color clamping --
714 * result clamping is only dependent on fragment clamping.
716 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
719 if (state
& MESA_META_CONDITIONAL_RENDER
) {
720 save
->CondRenderQuery
= ctx
->Query
.CondRenderQuery
;
721 save
->CondRenderMode
= ctx
->Query
.CondRenderMode
;
723 if (ctx
->Query
.CondRenderQuery
)
724 _mesa_EndConditionalRender();
728 if (state
& MESA_META_SELECT_FEEDBACK
) {
729 save
->RenderMode
= ctx
->RenderMode
;
730 if (ctx
->RenderMode
== GL_SELECT
) {
731 save
->Select
= ctx
->Select
; /* struct copy */
732 _mesa_RenderMode(GL_RENDER
);
733 } else if (ctx
->RenderMode
== GL_FEEDBACK
) {
734 save
->Feedback
= ctx
->Feedback
; /* struct copy */
735 _mesa_RenderMode(GL_RENDER
);
740 if (state
& MESA_META_MULTISAMPLE
) {
741 save
->MultisampleEnabled
= ctx
->Multisample
.Enabled
;
742 if (ctx
->Multisample
.Enabled
)
743 _mesa_set_enable(ctx
, GL_MULTISAMPLE
, GL_FALSE
);
748 save
->Lighting
= ctx
->Light
.Enabled
;
749 if (ctx
->Light
.Enabled
)
750 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
751 save
->RasterDiscard
= ctx
->RasterDiscard
;
752 if (ctx
->RasterDiscard
)
753 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_FALSE
);
759 * Leave meta state. This is like a light-weight version of glPopAttrib().
762 _mesa_meta_end(struct gl_context
*ctx
)
764 struct save_state
*save
= &ctx
->Meta
->Save
[--ctx
->Meta
->SaveStackDepth
];
765 const GLbitfield state
= save
->SavedState
;
767 if (state
& MESA_META_ALPHA_TEST
) {
768 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
769 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
770 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
773 if (state
& MESA_META_BLEND
) {
774 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
775 if (ctx
->Extensions
.EXT_draw_buffers2
) {
777 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
778 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
782 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
785 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
786 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
789 if (state
& MESA_META_COLOR_MASK
) {
791 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
792 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
794 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
795 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
798 _mesa_ColorMaskIndexed(i
,
799 save
->ColorMask
[i
][0],
800 save
->ColorMask
[i
][1],
801 save
->ColorMask
[i
][2],
802 save
->ColorMask
[i
][3]);
808 if (state
& MESA_META_DEPTH_TEST
) {
809 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
810 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
811 _mesa_DepthFunc(save
->Depth
.Func
);
812 _mesa_DepthMask(save
->Depth
.Mask
);
815 if (state
& MESA_META_FOG
) {
816 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
819 if (state
& MESA_META_PIXEL_STORE
) {
820 ctx
->Pack
= save
->Pack
;
821 ctx
->Unpack
= save
->Unpack
;
824 if (state
& MESA_META_PIXEL_TRANSFER
) {
825 ctx
->Pixel
.RedScale
= save
->RedScale
;
826 ctx
->Pixel
.RedBias
= save
->RedBias
;
827 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
828 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
829 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
830 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
831 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
832 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
833 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
835 ctx
->NewState
|=_NEW_PIXEL
;
838 if (state
& MESA_META_RASTERIZATION
) {
839 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
840 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
841 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
842 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
843 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
844 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
847 if (state
& MESA_META_SCISSOR
) {
848 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
849 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
850 save
->Scissor
.Width
, save
->Scissor
.Height
);
853 if (state
& MESA_META_SHADER
) {
854 if (ctx
->Extensions
.ARB_vertex_program
) {
855 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
856 save
->VertexProgramEnabled
);
857 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
858 save
->VertexProgram
);
859 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
862 if (ctx
->Extensions
.ARB_fragment_program
) {
863 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
864 save
->FragmentProgramEnabled
);
865 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
866 save
->FragmentProgram
);
867 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
870 if (ctx
->Extensions
.ARB_vertex_shader
)
871 _mesa_use_shader_program(ctx
, GL_VERTEX_SHADER
, save
->VertexShader
);
873 if (ctx
->Extensions
.ARB_geometry_shader4
)
874 _mesa_use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
,
875 save
->GeometryShader
);
877 if (ctx
->Extensions
.ARB_fragment_shader
)
878 _mesa_use_shader_program(ctx
, GL_FRAGMENT_SHADER
,
879 save
->FragmentShader
);
881 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
884 _mesa_reference_shader_program(ctx
, &save
->VertexShader
, NULL
);
885 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
, NULL
);
886 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
, NULL
);
887 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
, NULL
);
890 if (state
& MESA_META_STENCIL_TEST
) {
891 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
893 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
894 _mesa_ClearStencil(stencil
->Clear
);
895 if (ctx
->Extensions
.EXT_stencil_two_side
) {
896 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
897 stencil
->TestTwoSide
);
898 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
899 ? GL_BACK
: GL_FRONT
);
902 _mesa_StencilFuncSeparate(GL_FRONT
,
903 stencil
->Function
[0],
905 stencil
->ValueMask
[0]);
906 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
907 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
908 stencil
->ZFailFunc
[0],
909 stencil
->ZPassFunc
[0]);
911 _mesa_StencilFuncSeparate(GL_BACK
,
912 stencil
->Function
[1],
914 stencil
->ValueMask
[1]);
915 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
916 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
917 stencil
->ZFailFunc
[1],
918 stencil
->ZPassFunc
[1]);
921 if (state
& MESA_META_TEXTURE
) {
924 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
926 /* restore texenv for unit[0] */
927 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
929 /* restore texture objects for unit[0] only */
930 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
931 if (ctx
->Texture
.Unit
[0].CurrentTex
[tgt
] != save
->CurrentTexture
[tgt
]) {
932 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
933 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
934 save
->CurrentTexture
[tgt
]);
936 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
939 /* Restore fixed function texture enables, texgen */
940 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
941 if (ctx
->Texture
.Unit
[u
].Enabled
!= save
->TexEnabled
[u
]) {
942 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
943 ctx
->Texture
.Unit
[u
].Enabled
= save
->TexEnabled
[u
];
946 if (ctx
->Texture
.Unit
[u
].TexGenEnabled
!= save
->TexGenEnabled
[u
]) {
947 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
948 ctx
->Texture
.Unit
[u
].TexGenEnabled
= save
->TexGenEnabled
[u
];
952 /* restore current unit state */
953 _mesa_ActiveTextureARB(GL_TEXTURE0
+ save
->ActiveUnit
);
954 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ save
->ClientActiveUnit
);
957 if (state
& MESA_META_TRANSFORM
) {
958 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
959 _mesa_ActiveTextureARB(GL_TEXTURE0
);
960 _mesa_MatrixMode(GL_TEXTURE
);
961 _mesa_LoadMatrixf(save
->TextureMatrix
);
962 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
964 _mesa_MatrixMode(GL_MODELVIEW
);
965 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
967 _mesa_MatrixMode(GL_PROJECTION
);
968 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
970 _mesa_MatrixMode(save
->MatrixMode
);
973 if (state
& MESA_META_CLIP
) {
974 if (save
->ClipPlanesEnabled
) {
976 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
977 if (save
->ClipPlanesEnabled
& (1 << i
)) {
978 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
984 if (state
& MESA_META_VERTEX
) {
985 /* restore vertex buffer object */
986 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
987 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
989 /* restore vertex array object */
990 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
991 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
994 if (state
& MESA_META_VIEWPORT
) {
995 if (save
->ViewportX
!= ctx
->Viewport
.X
||
996 save
->ViewportY
!= ctx
->Viewport
.Y
||
997 save
->ViewportW
!= ctx
->Viewport
.Width
||
998 save
->ViewportH
!= ctx
->Viewport
.Height
) {
999 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
1000 save
->ViewportW
, save
->ViewportH
);
1002 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
1005 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
1006 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
1009 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
1010 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
1013 if (state
& MESA_META_CONDITIONAL_RENDER
) {
1014 if (save
->CondRenderQuery
)
1015 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
1016 save
->CondRenderMode
);
1019 #if FEATURE_feedback
1020 if (state
& MESA_META_SELECT_FEEDBACK
) {
1021 if (save
->RenderMode
== GL_SELECT
) {
1022 _mesa_RenderMode(GL_SELECT
);
1023 ctx
->Select
= save
->Select
;
1024 } else if (save
->RenderMode
== GL_FEEDBACK
) {
1025 _mesa_RenderMode(GL_FEEDBACK
);
1026 ctx
->Feedback
= save
->Feedback
;
1031 if (state
& MESA_META_MULTISAMPLE
) {
1032 if (ctx
->Multisample
.Enabled
!= save
->MultisampleEnabled
)
1033 _mesa_set_enable(ctx
, GL_MULTISAMPLE
, save
->MultisampleEnabled
);
1037 if (save
->Lighting
) {
1038 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
1040 if (save
->RasterDiscard
) {
1041 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_TRUE
);
1043 #if FEATURE_EXT_transform_feedback
1044 if (save
->TransformFeedbackNeedsResume
)
1045 _mesa_ResumeTransformFeedback();
1051 * Determine whether Mesa is currently in a meta state.
1054 _mesa_meta_in_progress(struct gl_context
*ctx
)
1056 return ctx
->Meta
->SaveStackDepth
!= 0;
1061 * Convert Z from a normalized value in the range [0, 1] to an object-space
1062 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1063 * default/identity ortho projection results in the original Z value.
1064 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1065 * value comes from the clear value or raster position.
1067 static INLINE GLfloat
1068 invert_z(GLfloat normZ
)
1070 GLfloat objZ
= 1.0f
- 2.0f
* normZ
;
1076 * One-time init for a temp_texture object.
1077 * Choose tex target, compute max tex size, etc.
1080 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1082 /* prefer texture rectangle */
1083 if (ctx
->Extensions
.NV_texture_rectangle
) {
1084 tex
->Target
= GL_TEXTURE_RECTANGLE
;
1085 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
1086 tex
->NPOT
= GL_TRUE
;
1089 /* use 2D texture, NPOT if possible */
1090 tex
->Target
= GL_TEXTURE_2D
;
1091 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1092 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
1094 tex
->MinSize
= 16; /* 16 x 16 at least */
1095 assert(tex
->MaxSize
> 0);
1097 _mesa_GenTextures(1, &tex
->TexObj
);
1101 cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1105 _mesa_DeleteTextures(1, &tex
->TexObj
);
1111 * Return pointer to temp_texture info for non-bitmap ops.
1112 * This does some one-time init if needed.
1114 static struct temp_texture
*
1115 get_temp_texture(struct gl_context
*ctx
)
1117 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
1120 init_temp_texture(ctx
, tex
);
1128 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1129 * We use a separate texture for bitmaps to reduce texture
1130 * allocation/deallocation.
1132 static struct temp_texture
*
1133 get_bitmap_temp_texture(struct gl_context
*ctx
)
1135 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
1138 init_temp_texture(ctx
, tex
);
1146 * Compute the width/height of texture needed to draw an image of the
1147 * given size. Return a flag indicating whether the current texture
1148 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1149 * allocated (glTexImage2D).
1150 * Also, compute s/t texcoords for drawing.
1152 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1155 alloc_texture(struct temp_texture
*tex
,
1156 GLsizei width
, GLsizei height
, GLenum intFormat
)
1158 GLboolean newTex
= GL_FALSE
;
1160 ASSERT(width
<= tex
->MaxSize
);
1161 ASSERT(height
<= tex
->MaxSize
);
1163 if (width
> tex
->Width
||
1164 height
> tex
->Height
||
1165 intFormat
!= tex
->IntFormat
) {
1166 /* alloc new texture (larger or different format) */
1169 /* use non-power of two size */
1170 tex
->Width
= MAX2(tex
->MinSize
, width
);
1171 tex
->Height
= MAX2(tex
->MinSize
, height
);
1174 /* find power of two size */
1176 w
= h
= tex
->MinSize
;
1185 tex
->IntFormat
= intFormat
;
1190 /* compute texcoords */
1191 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1192 tex
->Sright
= (GLfloat
) width
;
1193 tex
->Ttop
= (GLfloat
) height
;
1196 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1197 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1205 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1208 setup_copypix_texture(struct temp_texture
*tex
,
1210 GLint srcX
, GLint srcY
,
1211 GLsizei width
, GLsizei height
, GLenum intFormat
,
1214 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1215 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1216 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1217 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1219 /* copy framebuffer image to texture */
1221 /* create new tex image */
1222 if (tex
->Width
== width
&& tex
->Height
== height
) {
1223 /* create new tex with framebuffer data */
1224 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1225 srcX
, srcY
, width
, height
, 0);
1228 /* create empty texture */
1229 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1230 tex
->Width
, tex
->Height
, 0,
1231 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1233 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1234 0, 0, srcX
, srcY
, width
, height
);
1238 /* replace existing tex image */
1239 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1240 0, 0, srcX
, srcY
, width
, height
);
1246 * Setup/load texture for glDrawPixels.
1249 setup_drawpix_texture(struct gl_context
*ctx
,
1250 struct temp_texture
*tex
,
1252 GLenum texIntFormat
,
1253 GLsizei width
, GLsizei height
,
1254 GLenum format
, GLenum type
,
1255 const GLvoid
*pixels
)
1257 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1258 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1259 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1260 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1262 /* copy pixel data to texture */
1264 /* create new tex image */
1265 if (tex
->Width
== width
&& tex
->Height
== height
) {
1266 /* create new tex and load image data */
1267 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1268 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1271 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1273 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1274 ctx
->Unpack
.BufferObj
);
1275 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1276 /* create empty texture */
1277 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1278 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1279 if (save_unpack_obj
!= NULL
)
1280 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
,
1281 save_unpack_obj
->Name
);
1283 _mesa_TexSubImage2D(tex
->Target
, 0,
1284 0, 0, width
, height
, format
, type
, pixels
);
1288 /* replace existing tex image */
1289 _mesa_TexSubImage2D(tex
->Target
, 0,
1290 0, 0, width
, height
, format
, type
, pixels
);
1297 * One-time init for drawing depth pixels.
1300 init_blit_depth_pixels(struct gl_context
*ctx
)
1302 static const char *program
=
1304 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1307 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1308 struct temp_texture
*tex
= get_temp_texture(ctx
);
1309 const char *texTarget
;
1311 assert(blit
->DepthFP
== 0);
1313 /* replace %s with "RECT" or "2D" */
1314 assert(strlen(program
) + 4 < sizeof(program2
));
1315 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1319 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1321 _mesa_GenPrograms(1, &blit
->DepthFP
);
1322 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1323 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1324 strlen(program2
), (const GLubyte
*) program2
);
1329 * Try to do a glBlitFramebuffer using no-copy texturing.
1330 * We can do this when the src renderbuffer is actually a texture.
1331 * But if the src buffer == dst buffer we cannot do this.
1333 * \return new buffer mask indicating the buffers left to blit using the
1337 blitframebuffer_texture(struct gl_context
*ctx
,
1338 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1339 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1340 GLbitfield mask
, GLenum filter
)
1342 if (mask
& GL_COLOR_BUFFER_BIT
) {
1343 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1344 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1345 const struct gl_renderbuffer_attachment
*drawAtt
=
1346 &drawFb
->Attachment
[drawFb
->_ColorDrawBufferIndexes
[0]];
1347 const struct gl_renderbuffer_attachment
*readAtt
=
1348 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1350 if (readAtt
&& readAtt
->Texture
) {
1351 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1352 const GLuint srcLevel
= readAtt
->TextureLevel
;
1353 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
1354 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
1355 const GLint baseLevelSave
= texObj
->BaseLevel
;
1356 const GLint maxLevelSave
= texObj
->MaxLevel
;
1357 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
1358 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
1359 const GLenum srgbSave
= texObj
->Sampler
.sRGBDecode
;
1360 const GLenum fbo_srgb_save
= ctx
->Color
.sRGBEnabled
;
1361 const GLenum target
= texObj
->Target
;
1363 if (drawAtt
->Texture
== readAtt
->Texture
) {
1364 /* Can't use same texture as both the source and dest. We need
1365 * to handle overlapping blits and besides, some hw may not
1371 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1372 /* Can't handle other texture types at this time */
1377 printf("Blit from texture!\n");
1378 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1379 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1382 /* Prepare src texture state */
1383 _mesa_BindTexture(target
, texObj
->Name
);
1384 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
1385 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
1386 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1387 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1388 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1390 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1391 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1393 /* Always do our blits with no sRGB decode or encode.*/
1394 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1395 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
1396 GL_SKIP_DECODE_EXT
);
1398 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
1399 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
1402 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1403 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1405 /* Prepare vertex data (the VBO was previously created and bound) */
1410 struct vertex verts
[4];
1411 GLfloat s0
, t0
, s1
, t1
;
1413 if (target
== GL_TEXTURE_2D
) {
1414 const struct gl_texture_image
*texImage
1415 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1416 s0
= srcX0
/ (float) texImage
->Width
;
1417 s1
= srcX1
/ (float) texImage
->Width
;
1418 t0
= srcY0
/ (float) texImage
->Height
;
1419 t1
= srcY1
/ (float) texImage
->Height
;
1422 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1429 verts
[0].x
= (GLfloat
) dstX0
;
1430 verts
[0].y
= (GLfloat
) dstY0
;
1431 verts
[1].x
= (GLfloat
) dstX1
;
1432 verts
[1].y
= (GLfloat
) dstY0
;
1433 verts
[2].x
= (GLfloat
) dstX1
;
1434 verts
[2].y
= (GLfloat
) dstY1
;
1435 verts
[3].x
= (GLfloat
) dstX0
;
1436 verts
[3].y
= (GLfloat
) dstY1
;
1447 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1450 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1452 /* Restore texture object state, the texture binding will
1453 * be restored by _mesa_meta_end().
1455 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
1456 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
1457 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1458 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1459 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1461 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
1462 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
1463 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1464 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
, srgbSave
);
1466 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&& fbo_srgb_save
) {
1467 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_TRUE
);
1470 /* Done with color buffer */
1471 mask
&= ~GL_COLOR_BUFFER_BIT
;
1480 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1481 * of texture mapping and polygon rendering.
1484 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1485 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1486 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1487 GLbitfield mask
, GLenum filter
)
1489 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1490 struct temp_texture
*tex
= get_temp_texture(ctx
);
1491 const GLsizei maxTexSize
= tex
->MaxSize
;
1492 const GLint srcX
= MIN2(srcX0
, srcX1
);
1493 const GLint srcY
= MIN2(srcY0
, srcY1
);
1494 const GLint srcW
= abs(srcX1
- srcX0
);
1495 const GLint srcH
= abs(srcY1
- srcY0
);
1496 const GLboolean srcFlipX
= srcX1
< srcX0
;
1497 const GLboolean srcFlipY
= srcY1
< srcY0
;
1501 struct vertex verts
[4];
1504 /* In addition to falling back if the blit size is larger than the maximum
1505 * texture size, fallback if the source is multisampled. This fallback can
1506 * be removed once Mesa gets support ARB_texture_multisample.
1508 if (srcW
> maxTexSize
|| srcH
> maxTexSize
1509 || ctx
->ReadBuffer
->Visual
.samples
> 0) {
1510 /* XXX avoid this fallback */
1511 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1512 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1528 /* only scissor effects blit so save/clear all other relevant state */
1529 _mesa_meta_begin(ctx
, ~MESA_META_SCISSOR
);
1531 if (blit
->ArrayObj
== 0) {
1532 /* one-time setup */
1534 /* create vertex array object */
1535 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1536 _mesa_BindVertexArray(blit
->ArrayObj
);
1538 /* create vertex array buffer */
1539 _mesa_GenBuffersARB(1, &blit
->VBO
);
1540 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1541 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1542 NULL
, GL_DYNAMIC_DRAW_ARB
);
1544 /* setup vertex arrays */
1545 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1546 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1547 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1548 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1551 _mesa_BindVertexArray(blit
->ArrayObj
);
1552 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1555 /* Try faster, direct texture approach first */
1556 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1557 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1559 _mesa_meta_end(ctx
);
1563 /* Continue with "normal" approach which involves copying the src rect
1564 * into a temporary texture and is "blitted" by drawing a textured quad.
1567 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1569 /* vertex positions/texcoords (after texture allocation!) */
1571 verts
[0].x
= (GLfloat
) dstX0
;
1572 verts
[0].y
= (GLfloat
) dstY0
;
1573 verts
[1].x
= (GLfloat
) dstX1
;
1574 verts
[1].y
= (GLfloat
) dstY0
;
1575 verts
[2].x
= (GLfloat
) dstX1
;
1576 verts
[2].y
= (GLfloat
) dstY1
;
1577 verts
[3].x
= (GLfloat
) dstX0
;
1578 verts
[3].y
= (GLfloat
) dstY1
;
1582 verts
[1].s
= tex
->Sright
;
1584 verts
[2].s
= tex
->Sright
;
1585 verts
[2].t
= tex
->Ttop
;
1587 verts
[3].t
= tex
->Ttop
;
1589 /* upload new vertex data */
1590 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1593 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1595 if (mask
& GL_COLOR_BUFFER_BIT
) {
1596 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1598 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1599 mask
&= ~GL_COLOR_BUFFER_BIT
;
1602 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1603 GLuint
*tmp
= (GLuint
*) malloc(srcW
* srcH
* sizeof(GLuint
));
1606 init_blit_depth_pixels(ctx
);
1608 /* maybe change tex format here */
1609 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1611 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1612 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1614 setup_drawpix_texture(ctx
, tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1615 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1617 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1618 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1619 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1620 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1621 _mesa_DepthFunc(GL_ALWAYS
);
1622 _mesa_DepthMask(GL_TRUE
);
1624 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1625 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1631 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1632 /* XXX can't easily do stencil */
1635 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1637 _mesa_meta_end(ctx
);
1640 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1641 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1646 meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
)
1648 if (blit
->ArrayObj
) {
1649 _mesa_DeleteVertexArraysAPPLE(1, &blit
->ArrayObj
);
1651 _mesa_DeleteBuffersARB(1, &blit
->VBO
);
1654 if (blit
->DepthFP
) {
1655 _mesa_DeletePrograms(1, &blit
->DepthFP
);
1662 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1665 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1667 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1669 GLfloat x
, y
, z
, r
, g
, b
, a
;
1671 struct vertex verts
[4];
1672 /* save all state but scissor, pixel pack/unpack */
1673 GLbitfield metaSave
= (MESA_META_ALL
-
1675 MESA_META_PIXEL_STORE
-
1676 MESA_META_CONDITIONAL_RENDER
);
1677 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1679 if (buffers
& BUFFER_BITS_COLOR
) {
1680 /* if clearing color buffers, don't save/restore colormask */
1681 metaSave
-= MESA_META_COLOR_MASK
;
1684 _mesa_meta_begin(ctx
, metaSave
);
1686 if (clear
->ArrayObj
== 0) {
1687 /* one-time setup */
1689 /* create vertex array object */
1690 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1691 _mesa_BindVertexArray(clear
->ArrayObj
);
1693 /* create vertex array buffer */
1694 _mesa_GenBuffersARB(1, &clear
->VBO
);
1695 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1697 /* setup vertex arrays */
1698 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1699 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1700 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1701 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1704 _mesa_BindVertexArray(clear
->ArrayObj
);
1705 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1708 /* GL_COLOR_BUFFER_BIT */
1709 if (buffers
& BUFFER_BITS_COLOR
) {
1710 /* leave colormask, glDrawBuffer state as-is */
1712 /* Clears never have the color clamped. */
1713 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1716 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1717 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1720 /* GL_DEPTH_BUFFER_BIT */
1721 if (buffers
& BUFFER_BIT_DEPTH
) {
1722 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1723 _mesa_DepthFunc(GL_ALWAYS
);
1724 _mesa_DepthMask(GL_TRUE
);
1727 assert(!ctx
->Depth
.Test
);
1730 /* GL_STENCIL_BUFFER_BIT */
1731 if (buffers
& BUFFER_BIT_STENCIL
) {
1732 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1733 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1734 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1735 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1736 ctx
->Stencil
.Clear
& stencilMax
,
1737 ctx
->Stencil
.WriteMask
[0]);
1740 assert(!ctx
->Stencil
.Enabled
);
1743 /* vertex positions/colors */
1745 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1746 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1747 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1748 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1749 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1766 for (i
= 0; i
< 4; i
++) {
1767 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
1768 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
1769 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
1770 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
1773 /* upload new vertex data */
1774 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1775 GL_DYNAMIC_DRAW_ARB
);
1779 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1781 _mesa_meta_end(ctx
);
1785 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
1787 const char *vs_source
=
1788 "attribute vec4 position;\n"
1791 " gl_Position = position;\n"
1793 const char *fs_source
=
1794 "uniform vec4 color;\n"
1797 " gl_FragColor = color;\n"
1799 const char *vs_int_source
=
1801 "attribute vec4 position;\n"
1804 " gl_Position = position;\n"
1806 const char *fs_int_source
=
1808 "uniform ivec4 color;\n"
1809 "out ivec4 out_color;\n"
1813 " out_color = color;\n"
1817 if (clear
->ArrayObj
!= 0)
1820 /* create vertex array object */
1821 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1822 _mesa_BindVertexArray(clear
->ArrayObj
);
1824 /* create vertex array buffer */
1825 _mesa_GenBuffersARB(1, &clear
->VBO
);
1826 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1828 /* setup vertex arrays */
1829 _mesa_VertexAttribPointerARB(0, 3, GL_FLOAT
, GL_FALSE
, 0, (void *)0);
1830 _mesa_EnableVertexAttribArrayARB(0);
1832 vs
= _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER
);
1833 _mesa_ShaderSourceARB(vs
, 1, &vs_source
, NULL
);
1834 _mesa_CompileShaderARB(vs
);
1836 fs
= _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER
);
1837 _mesa_ShaderSourceARB(fs
, 1, &fs_source
, NULL
);
1838 _mesa_CompileShaderARB(fs
);
1840 clear
->ShaderProg
= _mesa_CreateProgramObjectARB();
1841 _mesa_AttachShader(clear
->ShaderProg
, fs
);
1842 _mesa_DeleteObjectARB(fs
);
1843 _mesa_AttachShader(clear
->ShaderProg
, vs
);
1844 _mesa_DeleteObjectARB(vs
);
1845 _mesa_BindAttribLocationARB(clear
->ShaderProg
, 0, "position");
1846 _mesa_LinkProgramARB(clear
->ShaderProg
);
1848 clear
->ColorLocation
= _mesa_GetUniformLocationARB(clear
->ShaderProg
,
1851 if (_mesa_is_desktop_gl(ctx
) && ctx
->Const
.GLSLVersion
>= 130) {
1852 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_int_source
);
1853 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_int_source
);
1855 clear
->IntegerShaderProg
= _mesa_CreateProgramObjectARB();
1856 _mesa_AttachShader(clear
->IntegerShaderProg
, fs
);
1857 _mesa_DeleteObjectARB(fs
);
1858 _mesa_AttachShader(clear
->IntegerShaderProg
, vs
);
1859 _mesa_DeleteObjectARB(vs
);
1860 _mesa_BindAttribLocationARB(clear
->IntegerShaderProg
, 0, "position");
1862 /* Note that user-defined out attributes get automatically assigned
1863 * locations starting from 0, so we don't need to explicitly
1864 * BindFragDataLocation to 0.
1867 link_program_with_debug(ctx
, clear
->IntegerShaderProg
);
1869 clear
->IntegerColorLocation
=
1870 _mesa_GetUniformLocationARB(clear
->IntegerShaderProg
, "color");
1875 meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
)
1877 if (clear
->ArrayObj
== 0)
1879 _mesa_DeleteVertexArraysAPPLE(1, &clear
->ArrayObj
);
1880 clear
->ArrayObj
= 0;
1881 _mesa_DeleteBuffersARB(1, &clear
->VBO
);
1883 _mesa_DeleteObjectARB(clear
->ShaderProg
);
1884 clear
->ShaderProg
= 0;
1886 if (clear
->IntegerShaderProg
) {
1887 _mesa_DeleteObjectARB(clear
->IntegerShaderProg
);
1888 clear
->IntegerShaderProg
= 0;
1893 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1896 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1898 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1899 GLbitfield metaSave
;
1900 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1901 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1902 const float x0
= ((float)fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
1903 const float y0
= ((float)fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
1904 const float x1
= ((float)fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
1905 const float y1
= ((float)fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
1906 const float z
= -invert_z(ctx
->Depth
.Clear
);
1911 metaSave
= (MESA_META_ALPHA_TEST
|
1913 MESA_META_DEPTH_TEST
|
1914 MESA_META_RASTERIZATION
|
1916 MESA_META_STENCIL_TEST
|
1918 MESA_META_VIEWPORT
|
1920 MESA_META_CLAMP_FRAGMENT_COLOR
|
1921 MESA_META_MULTISAMPLE
);
1923 if (!(buffers
& BUFFER_BITS_COLOR
)) {
1924 /* We'll use colormask to disable color writes. Otherwise,
1925 * respect color mask
1927 metaSave
|= MESA_META_COLOR_MASK
;
1930 _mesa_meta_begin(ctx
, metaSave
);
1932 meta_glsl_clear_init(ctx
, clear
);
1934 if (fb
->_IntegerColor
) {
1935 _mesa_UseProgramObjectARB(clear
->IntegerShaderProg
);
1936 _mesa_Uniform4ivARB(clear
->IntegerColorLocation
, 1,
1937 ctx
->Color
.ClearColor
.i
);
1939 _mesa_UseProgramObjectARB(clear
->ShaderProg
);
1940 _mesa_Uniform4fvARB(clear
->ColorLocation
, 1,
1941 ctx
->Color
.ClearColor
.f
);
1944 _mesa_BindVertexArray(clear
->ArrayObj
);
1945 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1947 /* GL_COLOR_BUFFER_BIT */
1948 if (buffers
& BUFFER_BITS_COLOR
) {
1949 /* leave colormask, glDrawBuffer state as-is */
1951 /* Clears never have the color clamped. */
1952 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1955 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1956 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1959 /* GL_DEPTH_BUFFER_BIT */
1960 if (buffers
& BUFFER_BIT_DEPTH
) {
1961 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1962 _mesa_DepthFunc(GL_ALWAYS
);
1963 _mesa_DepthMask(GL_TRUE
);
1966 assert(!ctx
->Depth
.Test
);
1969 /* GL_STENCIL_BUFFER_BIT */
1970 if (buffers
& BUFFER_BIT_STENCIL
) {
1971 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1972 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1973 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1974 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1975 ctx
->Stencil
.Clear
& stencilMax
,
1976 ctx
->Stencil
.WriteMask
[0]);
1979 assert(!ctx
->Stencil
.Enabled
);
1982 /* vertex positions */
1996 /* upload new vertex data */
1997 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1998 GL_DYNAMIC_DRAW_ARB
);
2001 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2003 _mesa_meta_end(ctx
);
2007 * Meta implementation of ctx->Driver.CopyPixels() in terms
2008 * of texture mapping and polygon rendering and GLSL shaders.
2011 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
2012 GLsizei width
, GLsizei height
,
2013 GLint dstX
, GLint dstY
, GLenum type
)
2015 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
2016 struct temp_texture
*tex
= get_temp_texture(ctx
);
2018 GLfloat x
, y
, z
, s
, t
;
2020 struct vertex verts
[4];
2022 GLenum intFormat
= GL_RGBA
;
2024 if (type
!= GL_COLOR
||
2025 ctx
->_ImageTransferState
||
2027 width
> tex
->MaxSize
||
2028 height
> tex
->MaxSize
) {
2029 /* XXX avoid this fallback */
2030 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
2034 /* Most GL state applies to glCopyPixels, but a there's a few things
2035 * we need to override:
2037 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2040 MESA_META_TRANSFORM
|
2043 MESA_META_VIEWPORT
));
2045 if (copypix
->ArrayObj
== 0) {
2046 /* one-time setup */
2048 /* create vertex array object */
2049 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
2050 _mesa_BindVertexArray(copypix
->ArrayObj
);
2052 /* create vertex array buffer */
2053 _mesa_GenBuffersARB(1, ©pix
->VBO
);
2054 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2055 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2056 NULL
, GL_DYNAMIC_DRAW_ARB
);
2058 /* setup vertex arrays */
2059 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2060 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2061 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2062 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2065 _mesa_BindVertexArray(copypix
->ArrayObj
);
2066 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2069 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
2071 /* vertex positions, texcoords (after texture allocation!) */
2073 const GLfloat dstX0
= (GLfloat
) dstX
;
2074 const GLfloat dstY0
= (GLfloat
) dstY
;
2075 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
2076 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
2077 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2087 verts
[1].s
= tex
->Sright
;
2092 verts
[2].s
= tex
->Sright
;
2093 verts
[2].t
= tex
->Ttop
;
2098 verts
[3].t
= tex
->Ttop
;
2100 /* upload new vertex data */
2101 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2104 /* Alloc/setup texture */
2105 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
2106 GL_RGBA
, GL_NEAREST
);
2108 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2110 /* draw textured quad */
2111 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2113 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2115 _mesa_meta_end(ctx
);
2121 * When the glDrawPixels() image size is greater than the max rectangle
2122 * texture size we use this function to break the glDrawPixels() image
2123 * into tiles which fit into the max texture size.
2126 tiled_draw_pixels(struct gl_context
*ctx
,
2128 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2129 GLenum format
, GLenum type
,
2130 const struct gl_pixelstore_attrib
*unpack
,
2131 const GLvoid
*pixels
)
2133 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
2136 if (tileUnpack
.RowLength
== 0)
2137 tileUnpack
.RowLength
= width
;
2139 for (i
= 0; i
< width
; i
+= tileSize
) {
2140 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
2141 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
2143 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
2145 for (j
= 0; j
< height
; j
+= tileSize
) {
2146 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
2147 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
2149 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
2151 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
2152 format
, type
, &tileUnpack
, pixels
);
2159 * One-time init for drawing stencil pixels.
2162 init_draw_stencil_pixels(struct gl_context
*ctx
)
2164 /* This program is run eight times, once for each stencil bit.
2165 * The stencil values to draw are found in an 8-bit alpha texture.
2166 * We read the texture/stencil value and test if bit 'b' is set.
2167 * If the bit is not set, use KIL to kill the fragment.
2168 * Finally, we use the stencil test to update the stencil buffer.
2170 * The basic algorithm for checking if a bit is set is:
2171 * if (is_odd(value / (1 << bit)))
2172 * result is one (or non-zero).
2175 * The program parameter contains three values:
2176 * parm.x = 255 / (1 << bit)
2180 static const char *program
=
2182 "PARAM parm = program.local[0]; \n"
2184 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
2185 "# t = t * 255 / bit \n"
2186 "MUL t.x, t.a, parm.x; \n"
2189 "SUB t.x, t.x, t.y; \n"
2191 "MUL t.x, t.x, parm.y; \n"
2192 "# t = fract(t.x) \n"
2193 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2194 "# t.x = (t.x == 0 ? 1 : 0) \n"
2195 "SGE t.x, -t.x, parm.z; \n"
2197 "# for debug only \n"
2198 "#MOV result.color, t.x; \n"
2200 char program2
[1000];
2201 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2202 struct temp_texture
*tex
= get_temp_texture(ctx
);
2203 const char *texTarget
;
2205 assert(drawpix
->StencilFP
== 0);
2207 /* replace %s with "RECT" or "2D" */
2208 assert(strlen(program
) + 4 < sizeof(program2
));
2209 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2213 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2215 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
2216 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2217 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2218 strlen(program2
), (const GLubyte
*) program2
);
2223 * One-time init for drawing depth pixels.
2226 init_draw_depth_pixels(struct gl_context
*ctx
)
2228 static const char *program
=
2230 "PARAM color = program.local[0]; \n"
2231 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2232 "MOV result.color, color; \n"
2235 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2236 struct temp_texture
*tex
= get_temp_texture(ctx
);
2237 const char *texTarget
;
2239 assert(drawpix
->DepthFP
== 0);
2241 /* replace %s with "RECT" or "2D" */
2242 assert(strlen(program
) + 4 < sizeof(program2
));
2243 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2247 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2249 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
2250 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2251 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2252 strlen(program2
), (const GLubyte
*) program2
);
2257 * Meta implementation of ctx->Driver.DrawPixels() in terms
2258 * of texture mapping and polygon rendering.
2261 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2262 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2263 GLenum format
, GLenum type
,
2264 const struct gl_pixelstore_attrib
*unpack
,
2265 const GLvoid
*pixels
)
2267 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2268 struct temp_texture
*tex
= get_temp_texture(ctx
);
2269 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2270 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2272 GLfloat x
, y
, z
, s
, t
;
2274 struct vertex verts
[4];
2275 GLenum texIntFormat
;
2276 GLboolean fallback
, newTex
;
2277 GLbitfield metaExtraSave
= 0x0;
2281 * Determine if we can do the glDrawPixels with texture mapping.
2283 fallback
= GL_FALSE
;
2284 if (ctx
->Fog
.Enabled
) {
2288 if (_mesa_is_color_format(format
)) {
2289 /* use more compact format when possible */
2290 /* XXX disable special case for GL_LUMINANCE for now to work around
2291 * apparent i965 driver bug (see bug #23670).
2293 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2294 texIntFormat
= format
;
2296 texIntFormat
= GL_RGBA
;
2298 /* If we're not supposed to clamp the resulting color, then just
2299 * promote our texture to fully float. We could do better by
2300 * just going for the matching set of channels, in floating
2303 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2304 ctx
->Extensions
.ARB_texture_float
)
2305 texIntFormat
= GL_RGBA32F
;
2307 else if (_mesa_is_stencil_format(format
)) {
2308 if (ctx
->Extensions
.ARB_fragment_program
&&
2309 ctx
->Pixel
.IndexShift
== 0 &&
2310 ctx
->Pixel
.IndexOffset
== 0 &&
2311 type
== GL_UNSIGNED_BYTE
) {
2312 /* We'll store stencil as alpha. This only works for GLubyte
2313 * image data because of how incoming values are mapped to alpha
2316 texIntFormat
= GL_ALPHA
;
2317 metaExtraSave
= (MESA_META_COLOR_MASK
|
2318 MESA_META_DEPTH_TEST
|
2319 MESA_META_PIXEL_TRANSFER
|
2321 MESA_META_STENCIL_TEST
);
2327 else if (_mesa_is_depth_format(format
)) {
2328 if (ctx
->Extensions
.ARB_depth_texture
&&
2329 ctx
->Extensions
.ARB_fragment_program
) {
2330 texIntFormat
= GL_DEPTH_COMPONENT
;
2331 metaExtraSave
= (MESA_META_SHADER
);
2342 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2343 format
, type
, unpack
, pixels
);
2348 * Check image size against max texture size, draw as tiles if needed.
2350 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2351 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2352 format
, type
, unpack
, pixels
);
2356 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2357 * but a there's a few things we need to override:
2359 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2362 MESA_META_TRANSFORM
|
2365 MESA_META_VIEWPORT
|
2368 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2370 /* vertex positions, texcoords (after texture allocation!) */
2372 const GLfloat x0
= (GLfloat
) x
;
2373 const GLfloat y0
= (GLfloat
) y
;
2374 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2375 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2376 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2386 verts
[1].s
= tex
->Sright
;
2391 verts
[2].s
= tex
->Sright
;
2392 verts
[2].t
= tex
->Ttop
;
2397 verts
[3].t
= tex
->Ttop
;
2400 if (drawpix
->ArrayObj
== 0) {
2401 /* one-time setup: create vertex array object */
2402 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
2404 _mesa_BindVertexArray(drawpix
->ArrayObj
);
2406 /* create vertex array buffer */
2407 _mesa_GenBuffersARB(1, &vbo
);
2408 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
2409 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2410 verts
, GL_DYNAMIC_DRAW_ARB
);
2412 /* setup vertex arrays */
2413 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2414 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2415 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2416 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2418 /* set given unpack params */
2419 ctx
->Unpack
= *unpack
;
2421 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2423 if (_mesa_is_stencil_format(format
)) {
2424 /* Drawing stencil */
2427 if (!drawpix
->StencilFP
)
2428 init_draw_stencil_pixels(ctx
);
2430 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2431 GL_ALPHA
, type
, pixels
);
2433 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2435 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2437 /* set all stencil bits to 0 */
2438 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2439 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2440 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2442 /* set stencil bits to 1 where needed */
2443 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2445 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2446 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2448 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2449 const GLuint mask
= 1 << bit
;
2450 if (mask
& origStencilMask
) {
2451 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2452 _mesa_StencilMask(mask
);
2454 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2455 255.0 / mask
, 0.5, 0.0, 0.0);
2457 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2461 else if (_mesa_is_depth_format(format
)) {
2463 if (!drawpix
->DepthFP
)
2464 init_draw_depth_pixels(ctx
);
2466 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2467 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2469 /* polygon color = current raster color */
2470 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2471 ctx
->Current
.RasterColor
);
2473 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2474 format
, type
, pixels
);
2476 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2480 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2481 format
, type
, pixels
);
2482 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2485 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2487 _mesa_DeleteBuffersARB(1, &vbo
);
2489 /* restore unpack params */
2490 ctx
->Unpack
= unpackSave
;
2492 _mesa_meta_end(ctx
);
2496 alpha_test_raster_color(struct gl_context
*ctx
)
2498 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2499 GLfloat ref
= ctx
->Color
.AlphaRef
;
2501 switch (ctx
->Color
.AlphaFunc
) {
2507 return alpha
== ref
;
2509 return alpha
<= ref
;
2513 return alpha
!= ref
;
2515 return alpha
>= ref
;
2525 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2526 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2527 * tracker would improve performance a lot.
2530 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2531 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2532 const struct gl_pixelstore_attrib
*unpack
,
2533 const GLubyte
*bitmap1
)
2535 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2536 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2537 const GLenum texIntFormat
= GL_ALPHA
;
2538 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2541 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2543 struct vertex verts
[4];
2548 * Check if swrast fallback is needed.
2550 if (ctx
->_ImageTransferState
||
2551 ctx
->FragmentProgram
._Enabled
||
2553 ctx
->Texture
._EnabledUnits
||
2554 width
> tex
->MaxSize
||
2555 height
> tex
->MaxSize
) {
2556 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2560 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2563 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2564 * but a there's a few things we need to override:
2566 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
2567 MESA_META_PIXEL_STORE
|
2568 MESA_META_RASTERIZATION
|
2571 MESA_META_TRANSFORM
|
2574 MESA_META_VIEWPORT
));
2576 if (bitmap
->ArrayObj
== 0) {
2577 /* one-time setup */
2579 /* create vertex array object */
2580 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2581 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2583 /* create vertex array buffer */
2584 _mesa_GenBuffersARB(1, &bitmap
->VBO
);
2585 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2586 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2587 NULL
, GL_DYNAMIC_DRAW_ARB
);
2589 /* setup vertex arrays */
2590 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2591 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2592 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2593 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2594 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2595 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2598 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2599 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2602 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2604 /* vertex positions, texcoords, colors (after texture allocation!) */
2606 const GLfloat x0
= (GLfloat
) x
;
2607 const GLfloat y0
= (GLfloat
) y
;
2608 const GLfloat x1
= (GLfloat
) (x
+ width
);
2609 const GLfloat y1
= (GLfloat
) (y
+ height
);
2610 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2621 verts
[1].s
= tex
->Sright
;
2626 verts
[2].s
= tex
->Sright
;
2627 verts
[2].t
= tex
->Ttop
;
2632 verts
[3].t
= tex
->Ttop
;
2634 for (i
= 0; i
< 4; i
++) {
2635 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2636 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2637 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2638 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2641 /* upload new vertex data */
2642 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2645 /* choose different foreground/background alpha values */
2646 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2647 bg
= (fg
> 127 ? 0 : 255);
2649 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2651 _mesa_meta_end(ctx
);
2655 bitmap8
= (GLubyte
*) malloc(width
* height
);
2657 memset(bitmap8
, bg
, width
* height
);
2658 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2659 bitmap8
, width
, fg
);
2661 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2663 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2664 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2666 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2667 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2669 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2671 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2676 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2678 _mesa_meta_end(ctx
);
2683 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2684 * software fallback. The fallback path will require that the texture
2685 * images are mapped.
2686 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2689 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
2690 struct gl_texture_object
*texObj
)
2692 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2693 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2694 struct gl_texture_image
*baseImage
;
2698 /* check for fallbacks */
2699 if (!ctx
->Extensions
.EXT_framebuffer_object
||
2700 target
== GL_TEXTURE_3D
||
2701 target
== GL_TEXTURE_1D_ARRAY
||
2702 target
== GL_TEXTURE_2D_ARRAY
) {
2706 srcLevel
= texObj
->BaseLevel
;
2707 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2708 if (!baseImage
|| _mesa_is_format_compressed(baseImage
->TexFormat
)) {
2712 if (_mesa_get_format_color_encoding(baseImage
->TexFormat
) == GL_SRGB
&&
2713 !ctx
->Extensions
.EXT_texture_sRGB_decode
) {
2714 /* The texture format is sRGB but we can't turn off sRGB->linear
2715 * texture sample conversion. So we won't be able to generate the
2716 * right colors when rendering. Need to use a fallback.
2722 * Test that we can actually render in the texture's format.
2725 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2726 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2728 if (target
== GL_TEXTURE_1D
) {
2729 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2730 GL_COLOR_ATTACHMENT0_EXT
,
2731 target
, texObj
->Name
, srcLevel
);
2734 /* other work is needed to enable 3D mipmap generation */
2735 else if (target
== GL_TEXTURE_3D
) {
2737 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2738 GL_COLOR_ATTACHMENT0_EXT
,
2739 target
, texObj
->Name
, srcLevel
, zoffset
);
2744 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2745 GL_COLOR_ATTACHMENT0_EXT
,
2746 target
, texObj
->Name
, srcLevel
);
2749 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2751 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2753 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2762 * Compute the texture coordinates for the four vertices of a quad for
2763 * drawing a 2D texture image or slice of a cube/3D texture.
2764 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2765 * \param slice slice of a 1D/2D array texture or 3D texture
2766 * \param width width of the texture image
2767 * \param height height of the texture image
2768 * \param coords0/1/2/3 returns the computed texcoords
2771 setup_texture_coords(GLenum faceTarget
,
2780 static const GLfloat st
[4][2] = {
2781 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2786 switch (faceTarget
) {
2790 case GL_TEXTURE_2D_ARRAY
:
2791 if (faceTarget
== GL_TEXTURE_3D
)
2793 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
2797 coords0
[0] = 0.0F
; /* s */
2798 coords0
[1] = 0.0F
; /* t */
2799 coords0
[2] = r
; /* r */
2810 case GL_TEXTURE_RECTANGLE_ARB
:
2811 coords0
[0] = 0.0F
; /* s */
2812 coords0
[1] = 0.0F
; /* t */
2813 coords0
[2] = 0.0F
; /* r */
2818 coords2
[1] = height
;
2821 coords3
[1] = height
;
2824 case GL_TEXTURE_1D_ARRAY
:
2825 coords0
[0] = 0.0F
; /* s */
2826 coords0
[1] = slice
; /* t */
2827 coords0
[2] = 0.0F
; /* r */
2839 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2840 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2841 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2842 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2843 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2844 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2845 /* loop over quad verts */
2846 for (i
= 0; i
< 4; i
++) {
2847 /* Compute sc = +/-scale and tc = +/-scale.
2848 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2849 * though that can still sometimes happen with this scale factor...
2851 const GLfloat scale
= 0.9999f
;
2852 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2853 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2873 switch (faceTarget
) {
2874 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2879 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2884 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2889 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2894 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2899 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2910 assert(0 && "unexpected target in meta setup_texture_coords()");
2916 * Called via ctx->Driver.GenerateMipmap()
2917 * Note: We don't yet support 3D textures, 1D/2D array textures or texture
2921 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
2922 struct gl_texture_object
*texObj
)
2924 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2926 GLfloat x
, y
, tex
[3];
2928 struct vertex verts
[4];
2929 const GLuint baseLevel
= texObj
->BaseLevel
;
2930 const GLuint maxLevel
= texObj
->MaxLevel
;
2931 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
2932 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
2933 const GLint maxLevelSave
= texObj
->MaxLevel
;
2934 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
2935 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
2936 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
2937 const GLenum wrapRSave
= texObj
->Sampler
.WrapR
;
2938 const GLenum srgbDecodeSave
= texObj
->Sampler
.sRGBDecode
;
2939 const GLenum srgbBufferSave
= ctx
->Color
.sRGBEnabled
;
2940 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2941 const GLuint original_active_unit
= ctx
->Texture
.CurrentUnit
;
2944 const GLuint border
= 0;
2945 const GLint slice
= 0;
2947 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
2948 _mesa_generate_mipmap(ctx
, target
, texObj
);
2952 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
2953 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
2954 faceTarget
= target
;
2955 target
= GL_TEXTURE_CUBE_MAP
;
2958 faceTarget
= target
;
2961 _mesa_meta_begin(ctx
, MESA_META_ALL
);
2963 if (original_active_unit
!= 0)
2964 _mesa_BindTexture(target
, texObj
->Name
);
2966 if (mipmap
->ArrayObj
== 0) {
2967 /* one-time setup */
2969 /* create vertex array object */
2970 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
2971 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
2973 /* create vertex array buffer */
2974 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
2975 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2976 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2977 NULL
, GL_DYNAMIC_DRAW_ARB
);
2979 /* setup vertex arrays */
2980 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2981 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
2982 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2983 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2986 _mesa_BindVertexArray(mipmap
->ArrayObj
);
2987 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2991 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2993 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2995 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
2996 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
2997 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
2998 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2999 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
3000 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
3002 /* We don't want to encode or decode sRGB values; treat them as linear */
3003 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3004 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
3005 GL_SKIP_DECODE_EXT
);
3007 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
3008 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
3011 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3013 /* setup texcoords (XXX what about border?) */
3014 setup_texture_coords(faceTarget
,
3016 0, 0, /* width, height never used here */
3022 /* setup vertex positions */
3032 /* upload new vertex data */
3033 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3035 /* setup projection matrix */
3036 _mesa_MatrixMode(GL_PROJECTION
);
3037 _mesa_LoadIdentity();
3038 _mesa_Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
3040 /* texture is already locked, unlock now */
3041 _mesa_unlock_texture(ctx
, texObj
);
3043 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
3044 const struct gl_texture_image
*srcImage
;
3045 const GLuint srcLevel
= dstLevel
- 1;
3046 GLsizei srcWidth
, srcHeight
, srcDepth
;
3047 GLsizei dstWidth
, dstHeight
, dstDepth
;
3050 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
3051 assert(srcImage
->Border
== 0); /* XXX we can fix this */
3053 /* src size w/out border */
3054 srcWidth
= srcImage
->Width
- 2 * border
;
3055 srcHeight
= srcImage
->Height
- 2 * border
;
3056 srcDepth
= srcImage
->Depth
- 2 * border
;
3058 /* new dst size w/ border */
3059 dstWidth
= MAX2(1, srcWidth
/ 2) + 2 * border
;
3060 dstHeight
= MAX2(1, srcHeight
/ 2) + 2 * border
;
3061 dstDepth
= MAX2(1, srcDepth
/ 2) + 2 * border
;
3063 if (dstWidth
== srcImage
->Width
&&
3064 dstHeight
== srcImage
->Height
&&
3065 dstDepth
== srcImage
->Depth
) {
3070 /* Allocate storage for the destination mipmap image(s) */
3072 /* Set MaxLevel large enough to hold the new level when we allocate it */
3073 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
3075 if (!_mesa_prepare_mipmap_level(ctx
, texObj
, dstLevel
,
3076 dstWidth
, dstHeight
, dstDepth
,
3078 srcImage
->InternalFormat
,
3079 srcImage
->TexFormat
)) {
3080 /* All done. We either ran out of memory or we would go beyond the
3081 * last valid level of an immutable texture if we continued.
3086 /* limit minification to src level */
3087 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
3089 /* Set to draw into the current dstLevel */
3090 if (target
== GL_TEXTURE_1D
) {
3091 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
3092 GL_COLOR_ATTACHMENT0_EXT
,
3097 else if (target
== GL_TEXTURE_3D
) {
3098 GLint zoffset
= 0; /* XXX unfinished */
3099 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
3100 GL_COLOR_ATTACHMENT0_EXT
,
3107 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
3108 GL_COLOR_ATTACHMENT0_EXT
,
3114 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
3117 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
3118 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
3119 _mesa_problem(ctx
, "Unexpected incomplete framebuffer in "
3120 "_mesa_meta_GenerateMipmap()");
3124 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
3125 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
3127 /* setup viewport */
3128 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
3130 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3133 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3134 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
3137 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&& srgbBufferSave
) {
3138 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_TRUE
);
3141 _mesa_lock_texture(ctx
, texObj
); /* relock */
3143 _mesa_meta_end(ctx
);
3145 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
3146 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
3147 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3148 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
3149 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
3150 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
3151 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, wrapRSave
);
3153 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
3158 * Determine the GL data type to use for the temporary image read with
3159 * ReadPixels() and passed to Tex[Sub]Image().
3162 get_temp_image_type(struct gl_context
*ctx
, GLenum baseFormat
)
3164 switch (baseFormat
) {
3171 case GL_LUMINANCE_ALPHA
:
3173 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
3174 return GL_UNSIGNED_BYTE
;
3175 else if (ctx
->DrawBuffer
->Visual
.redBits
<= 16)
3176 return GL_UNSIGNED_SHORT
;
3179 case GL_DEPTH_COMPONENT
:
3180 return GL_UNSIGNED_INT
;
3181 case GL_DEPTH_STENCIL
:
3182 return GL_UNSIGNED_INT_24_8
;
3184 _mesa_problem(ctx
, "Unexpected format %d in get_temp_image_type()",
3192 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
3193 * Have to be careful with locking and meta state for pixel transfer.
3196 _mesa_meta_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
3197 struct gl_texture_image
*texImage
,
3198 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3199 struct gl_renderbuffer
*rb
,
3201 GLsizei width
, GLsizei height
)
3203 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3204 GLenum format
, type
;
3208 /* Choose format/type for temporary image buffer */
3209 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
3210 if (format
== GL_LUMINANCE
||
3211 format
== GL_LUMINANCE_ALPHA
||
3212 format
== GL_INTENSITY
) {
3213 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
3214 * temp image buffer because glReadPixels will do L=R+G+B which is
3215 * not what we want (should be L=R).
3220 if (_mesa_is_format_integer_color(texImage
->TexFormat
)) {
3221 _mesa_problem(ctx
, "unsupported integer color copyteximage");
3225 type
= get_temp_image_type(ctx
, format
);
3226 bpp
= _mesa_bytes_per_pixel(format
, type
);
3228 _mesa_problem(ctx
, "Bad bpp in _mesa_meta_CopyTexSubImage()");
3233 * Alloc image buffer (XXX could use a PBO)
3235 buf
= malloc(width
* height
* bpp
);
3237 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
3241 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
3244 * Read image from framebuffer (disable pixel transfer ops)
3246 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
3247 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
3248 format
, type
, &ctx
->Pack
, buf
);
3249 _mesa_meta_end(ctx
);
3251 _mesa_update_state(ctx
); /* to update pixel transfer state */
3254 * Store texture data (with pixel transfer ops)
3256 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
3258 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3259 xoffset
, yoffset
, zoffset
, width
, height
, 1,
3260 format
, type
, buf
, &ctx
->Unpack
);
3262 _mesa_meta_end(ctx
);
3264 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
3271 * Decompress a texture image by drawing a quad with the compressed
3272 * texture and reading the pixels out of the color buffer.
3273 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
3274 * \param destFormat format, ala glReadPixels
3275 * \param destType type, ala glReadPixels
3276 * \param dest destination buffer
3277 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
3280 decompress_texture_image(struct gl_context
*ctx
,
3281 struct gl_texture_image
*texImage
,
3283 GLenum destFormat
, GLenum destType
,
3286 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
3287 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3288 const GLint width
= texImage
->Width
;
3289 const GLint height
= texImage
->Height
;
3290 const GLenum target
= texObj
->Target
;
3293 GLfloat x
, y
, tex
[3];
3295 struct vertex verts
[4];
3296 GLuint fboDrawSave
, fboReadSave
;
3300 assert(target
== GL_TEXTURE_3D
||
3301 target
== GL_TEXTURE_2D_ARRAY
);
3304 if (target
== GL_TEXTURE_CUBE_MAP
) {
3305 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
3308 faceTarget
= target
;
3311 /* save fbo bindings (not saved by _mesa_meta_begin()) */
3312 fboDrawSave
= ctx
->DrawBuffer
->Name
;
3313 fboReadSave
= ctx
->ReadBuffer
->Name
;
3314 rbSave
= ctx
->CurrentRenderbuffer
? ctx
->CurrentRenderbuffer
->Name
: 0;
3316 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_PIXEL_STORE
);
3318 /* Create/bind FBO/renderbuffer */
3319 if (decompress
->FBO
== 0) {
3320 _mesa_GenFramebuffersEXT(1, &decompress
->FBO
);
3321 _mesa_GenRenderbuffersEXT(1, &decompress
->RBO
);
3322 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3323 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3324 _mesa_FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT
,
3325 GL_COLOR_ATTACHMENT0_EXT
,
3326 GL_RENDERBUFFER_EXT
,
3330 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3333 /* alloc dest surface */
3334 if (width
> decompress
->Width
|| height
> decompress
->Height
) {
3335 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3336 _mesa_RenderbufferStorageEXT(GL_RENDERBUFFER_EXT
, GL_RGBA
,
3338 decompress
->Width
= width
;
3339 decompress
->Height
= height
;
3342 /* setup VBO data */
3343 if (decompress
->ArrayObj
== 0) {
3344 /* create vertex array object */
3345 _mesa_GenVertexArrays(1, &decompress
->ArrayObj
);
3346 _mesa_BindVertexArray(decompress
->ArrayObj
);
3348 /* create vertex array buffer */
3349 _mesa_GenBuffersARB(1, &decompress
->VBO
);
3350 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3351 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3352 NULL
, GL_DYNAMIC_DRAW_ARB
);
3354 /* setup vertex arrays */
3355 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3356 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
3357 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3358 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3361 _mesa_BindVertexArray(decompress
->ArrayObj
);
3362 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3365 setup_texture_coords(faceTarget
, slice
, width
, height
,
3371 /* setup vertex positions */
3377 verts
[2].y
= height
;
3379 verts
[3].y
= height
;
3381 /* upload new vertex data */
3382 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3384 /* setup texture state */
3385 _mesa_BindTexture(target
, texObj
->Name
);
3386 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3389 /* save texture object state */
3390 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
3391 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
3392 const GLint baseLevelSave
= texObj
->BaseLevel
;
3393 const GLint maxLevelSave
= texObj
->MaxLevel
;
3394 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
3395 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
3396 const GLenum srgbSave
= texObj
->Sampler
.sRGBDecode
;
3398 /* restrict sampling to the texture level of interest */
3399 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, texImage
->Level
);
3400 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texImage
->Level
);
3401 /* nearest filtering */
3402 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
3403 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
3405 /* No sRGB decode or encode.*/
3406 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3407 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
3408 GL_SKIP_DECODE_EXT
);
3410 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
3411 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
3414 /* render quad w/ texture into renderbuffer */
3415 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3417 /* Restore texture object state, the texture binding will
3418 * be restored by _mesa_meta_end().
3420 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
3421 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
3422 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3423 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
3424 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3426 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
3427 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
3428 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3429 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
, srgbSave
);
3433 /* read pixels from renderbuffer */
3435 GLenum baseTexFormat
= texImage
->_BaseFormat
;
3437 /* The pixel transfer state will be set to default values at this point
3438 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
3439 * turned off (as required by glGetTexImage) but we need to handle some
3440 * special cases. In particular, single-channel texture values are
3441 * returned as red and two-channel texture values are returned as
3444 if (baseTexFormat
== GL_LUMINANCE
||
3445 baseTexFormat
== GL_LUMINANCE_ALPHA
||
3446 baseTexFormat
== GL_INTENSITY
) {
3447 /* Green and blue must be zero */
3448 _mesa_PixelTransferf(GL_GREEN_SCALE
, 0.0f
);
3449 _mesa_PixelTransferf(GL_BLUE_SCALE
, 0.0f
);
3452 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
3455 /* disable texture unit */
3456 _mesa_set_enable(ctx
, target
, GL_FALSE
);
3458 _mesa_meta_end(ctx
);
3460 /* restore fbo bindings */
3461 if (fboDrawSave
== fboReadSave
) {
3462 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboDrawSave
);
3465 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT
, fboDrawSave
);
3466 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT
, fboReadSave
);
3468 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, rbSave
);
3473 * This is just a wrapper around _mesa_get_tex_image() and
3474 * decompress_texture_image(). Meta functions should not be directly called
3478 _mesa_meta_GetTexImage(struct gl_context
*ctx
,
3479 GLenum format
, GLenum type
, GLvoid
*pixels
,
3480 struct gl_texture_image
*texImage
)
3482 /* We can only use the decompress-with-blit method here if the texels are
3483 * unsigned, normalized values. We could handle signed and unnormalized
3484 * with floating point renderbuffers...
3486 if (_mesa_is_format_compressed(texImage
->TexFormat
) &&
3487 _mesa_get_format_datatype(texImage
->TexFormat
)
3488 == GL_UNSIGNED_NORMALIZED
) {
3489 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3490 const GLuint slice
= 0; /* only 2D compressed textures for now */
3491 /* Need to unlock the texture here to prevent deadlock... */
3492 _mesa_unlock_texture(ctx
, texObj
);
3493 decompress_texture_image(ctx
, texImage
, slice
, format
, type
, pixels
);
3494 /* ... and relock it */
3495 _mesa_lock_texture(ctx
, texObj
);
3498 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
3504 * Meta implementation of ctx->Driver.DrawTex() in terms
3505 * of polygon rendering.
3508 _mesa_meta_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
3509 GLfloat width
, GLfloat height
)
3511 #if FEATURE_OES_draw_texture
3512 struct drawtex_state
*drawtex
= &ctx
->Meta
->DrawTex
;
3514 GLfloat x
, y
, z
, st
[MAX_TEXTURE_UNITS
][2];
3516 struct vertex verts
[4];
3519 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
3521 MESA_META_TRANSFORM
|
3523 MESA_META_VIEWPORT
));
3525 if (drawtex
->ArrayObj
== 0) {
3526 /* one-time setup */
3527 GLint active_texture
;
3529 /* create vertex array object */
3530 _mesa_GenVertexArrays(1, &drawtex
->ArrayObj
);
3531 _mesa_BindVertexArray(drawtex
->ArrayObj
);
3533 /* create vertex array buffer */
3534 _mesa_GenBuffersARB(1, &drawtex
->VBO
);
3535 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3536 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3537 NULL
, GL_DYNAMIC_DRAW_ARB
);
3539 /* client active texture is not part of the array object */
3540 active_texture
= ctx
->Array
.ActiveTexture
;
3542 /* setup vertex arrays */
3543 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3544 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3545 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3546 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ i
);
3547 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(st
[i
]));
3548 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3551 /* restore client active texture */
3552 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ active_texture
);
3555 _mesa_BindVertexArray(drawtex
->ArrayObj
);
3556 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3559 /* vertex positions, texcoords */
3561 const GLfloat x1
= x
+ width
;
3562 const GLfloat y1
= y
+ height
;
3564 z
= CLAMP(z
, 0.0, 1.0);
3583 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3584 const struct gl_texture_object
*texObj
;
3585 const struct gl_texture_image
*texImage
;
3586 GLfloat s
, t
, s1
, t1
;
3589 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
3591 for (j
= 0; j
< 4; j
++) {
3592 verts
[j
].st
[i
][0] = 0.0f
;
3593 verts
[j
].st
[i
][1] = 0.0f
;
3598 texObj
= ctx
->Texture
.Unit
[i
]._Current
;
3599 texImage
= texObj
->Image
[0][texObj
->BaseLevel
];
3600 tw
= texImage
->Width2
;
3601 th
= texImage
->Height2
;
3603 s
= (GLfloat
) texObj
->CropRect
[0] / tw
;
3604 t
= (GLfloat
) texObj
->CropRect
[1] / th
;
3605 s1
= (GLfloat
) (texObj
->CropRect
[0] + texObj
->CropRect
[2]) / tw
;
3606 t1
= (GLfloat
) (texObj
->CropRect
[1] + texObj
->CropRect
[3]) / th
;
3608 verts
[0].st
[i
][0] = s
;
3609 verts
[0].st
[i
][1] = t
;
3611 verts
[1].st
[i
][0] = s1
;
3612 verts
[1].st
[i
][1] = t
;
3614 verts
[2].st
[i
][0] = s1
;
3615 verts
[2].st
[i
][1] = t1
;
3617 verts
[3].st
[i
][0] = s
;
3618 verts
[3].st
[i
][1] = t1
;
3621 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3624 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3626 _mesa_meta_end(ctx
);
3627 #endif /* FEATURE_OES_draw_texture */