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/formats.h"
48 #include "main/image.h"
49 #include "main/macros.h"
50 #include "main/matrix.h"
51 #include "main/mipmap.h"
53 #include "main/polygon.h"
54 #include "main/readpix.h"
55 #include "main/scissor.h"
56 #include "main/shaderapi.h"
57 #include "main/shaderobj.h"
58 #include "main/state.h"
59 #include "main/stencil.h"
60 #include "main/texobj.h"
61 #include "main/texenv.h"
62 #include "main/texgetimage.h"
63 #include "main/teximage.h"
64 #include "main/texparam.h"
65 #include "main/texstate.h"
66 #include "main/uniforms.h"
67 #include "main/varray.h"
68 #include "main/viewport.h"
69 #include "program/program.h"
70 #include "swrast/swrast.h"
71 #include "drivers/common/meta.h"
74 /** Return offset in bytes of the field within a vertex struct */
75 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
78 * State which we may save/restore across meta ops.
79 * XXX this may be incomplete...
83 GLbitfield SavedState
; /**< bitmask of MESA_META_* flags */
85 /** MESA_META_ALPHA_TEST */
86 GLboolean AlphaEnabled
;
90 /** MESA_META_BLEND */
91 GLbitfield BlendEnabled
;
92 GLboolean ColorLogicOpEnabled
;
94 /** MESA_META_COLOR_MASK */
95 GLubyte ColorMask
[MAX_DRAW_BUFFERS
][4];
97 /** MESA_META_DEPTH_TEST */
98 struct gl_depthbuffer_attrib Depth
;
103 /** MESA_META_PIXEL_STORE */
104 struct gl_pixelstore_attrib Pack
, Unpack
;
106 /** MESA_META_PIXEL_TRANSFER */
107 GLfloat RedBias
, RedScale
;
108 GLfloat GreenBias
, GreenScale
;
109 GLfloat BlueBias
, BlueScale
;
110 GLfloat AlphaBias
, AlphaScale
;
111 GLfloat DepthBias
, DepthScale
;
112 GLboolean MapColorFlag
;
114 /** MESA_META_RASTERIZATION */
115 GLenum FrontPolygonMode
, BackPolygonMode
;
116 GLboolean PolygonOffset
;
117 GLboolean PolygonSmooth
;
118 GLboolean PolygonStipple
;
119 GLboolean PolygonCull
;
121 /** MESA_META_SCISSOR */
122 struct gl_scissor_attrib Scissor
;
124 /** MESA_META_SHADER */
125 GLboolean VertexProgramEnabled
;
126 struct gl_vertex_program
*VertexProgram
;
127 GLboolean FragmentProgramEnabled
;
128 struct gl_fragment_program
*FragmentProgram
;
129 struct gl_shader_program
*VertexShader
;
130 struct gl_shader_program
*GeometryShader
;
131 struct gl_shader_program
*FragmentShader
;
132 struct gl_shader_program
*ActiveShader
;
134 /** MESA_META_STENCIL_TEST */
135 struct gl_stencil_attrib Stencil
;
137 /** MESA_META_TRANSFORM */
139 GLfloat ModelviewMatrix
[16];
140 GLfloat ProjectionMatrix
[16];
141 GLfloat TextureMatrix
[16];
143 /** MESA_META_CLIP */
144 GLbitfield ClipPlanesEnabled
;
146 /** MESA_META_TEXTURE */
148 GLuint ClientActiveUnit
;
149 /** for unit[0] only */
150 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
151 /** mask of TEXTURE_2D_BIT, etc */
152 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
153 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
154 GLuint EnvMode
; /* unit[0] only */
156 /** MESA_META_VERTEX */
157 struct gl_array_object
*ArrayObj
;
158 struct gl_buffer_object
*ArrayBufferObj
;
160 /** MESA_META_VIEWPORT */
161 GLint ViewportX
, ViewportY
, ViewportW
, ViewportH
;
162 GLclampd DepthNear
, DepthFar
;
164 /** MESA_META_CLAMP_FRAGMENT_COLOR */
165 GLenum ClampFragmentColor
;
167 /** MESA_META_CLAMP_VERTEX_COLOR */
168 GLenum ClampVertexColor
;
170 /** MESA_META_CONDITIONAL_RENDER */
171 struct gl_query_object
*CondRenderQuery
;
172 GLenum CondRenderMode
;
174 /** Miscellaneous (always disabled) */
180 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
181 * This is currently shared by all the meta ops. But we could create a
182 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
187 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
188 GLsizei MinSize
; /**< Min texture size to allocate */
189 GLsizei MaxSize
; /**< Max possible texture size */
190 GLboolean NPOT
; /**< Non-power of two size OK? */
191 GLsizei Width
, Height
; /**< Current texture size */
193 GLfloat Sright
, Ttop
; /**< right, top texcoords */
198 * State for glBlitFramebufer()
209 * State for glClear()
221 * State for glCopyPixels()
231 * State for glDrawPixels()
237 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
238 GLuint DepthFP
; /**< Fragment program for drawing depth images */
243 * State for glBitmap()
249 struct temp_texture Tex
; /**< separate texture from other meta ops */
254 * State for _mesa_meta_generate_mipmap()
256 struct gen_mipmap_state
265 * State for texture decompression
267 struct decompress_state
270 GLuint VBO
, FBO
, RBO
;
275 #define MAX_META_OPS_DEPTH 2
277 * All per-context meta state.
281 /** Stack of state saved during meta-ops */
282 struct save_state Save
[MAX_META_OPS_DEPTH
];
283 /** Save stack depth */
284 GLuint SaveStackDepth
;
286 struct temp_texture TempTex
;
288 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
289 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
290 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
291 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
292 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
293 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
294 struct decompress_state Decompress
; /**< For texture decompression */
299 * Initialize meta-ops for a context.
300 * To be called once during context creation.
303 _mesa_meta_init(struct gl_context
*ctx
)
307 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
312 * Free context meta-op state.
313 * To be called once during context destruction.
316 _mesa_meta_free(struct gl_context
*ctx
)
318 /* Note: Any textures, VBOs, etc, that we allocate should get
319 * freed by the normal context destruction code. But this would be
320 * the place to free other meta data someday.
328 * Enter meta state. This is like a light-weight version of glPushAttrib
329 * but it also resets most GL state back to default values.
331 * \param state bitmask of MESA_META_* flags indicating which attribute groups
332 * to save and reset to their defaults
335 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
337 struct save_state
*save
;
339 /* hope MAX_META_OPS_DEPTH is large enough */
340 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
342 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
343 memset(save
, 0, sizeof(*save
));
344 save
->SavedState
= state
;
346 if (state
& MESA_META_ALPHA_TEST
) {
347 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
348 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
349 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
350 if (ctx
->Color
.AlphaEnabled
)
351 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
354 if (state
& MESA_META_BLEND
) {
355 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
356 if (ctx
->Color
.BlendEnabled
) {
357 if (ctx
->Extensions
.EXT_draw_buffers2
) {
359 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
360 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
364 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
367 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
368 if (ctx
->Color
.ColorLogicOpEnabled
)
369 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
372 if (state
& MESA_META_COLOR_MASK
) {
373 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
374 sizeof(ctx
->Color
.ColorMask
));
375 if (!ctx
->Color
.ColorMask
[0][0] ||
376 !ctx
->Color
.ColorMask
[0][1] ||
377 !ctx
->Color
.ColorMask
[0][2] ||
378 !ctx
->Color
.ColorMask
[0][3])
379 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
382 if (state
& MESA_META_DEPTH_TEST
) {
383 save
->Depth
= ctx
->Depth
; /* struct copy */
385 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
388 if (state
& MESA_META_FOG
) {
389 save
->Fog
= ctx
->Fog
.Enabled
;
390 if (ctx
->Fog
.Enabled
)
391 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
394 if (state
& MESA_META_PIXEL_STORE
) {
395 save
->Pack
= ctx
->Pack
;
396 save
->Unpack
= ctx
->Unpack
;
397 ctx
->Pack
= ctx
->DefaultPacking
;
398 ctx
->Unpack
= ctx
->DefaultPacking
;
401 if (state
& MESA_META_PIXEL_TRANSFER
) {
402 save
->RedScale
= ctx
->Pixel
.RedScale
;
403 save
->RedBias
= ctx
->Pixel
.RedBias
;
404 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
405 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
406 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
407 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
408 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
409 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
410 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
411 ctx
->Pixel
.RedScale
= 1.0F
;
412 ctx
->Pixel
.RedBias
= 0.0F
;
413 ctx
->Pixel
.GreenScale
= 1.0F
;
414 ctx
->Pixel
.GreenBias
= 0.0F
;
415 ctx
->Pixel
.BlueScale
= 1.0F
;
416 ctx
->Pixel
.BlueBias
= 0.0F
;
417 ctx
->Pixel
.AlphaScale
= 1.0F
;
418 ctx
->Pixel
.AlphaBias
= 0.0F
;
419 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
421 ctx
->NewState
|=_NEW_PIXEL
;
424 if (state
& MESA_META_RASTERIZATION
) {
425 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
426 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
427 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
428 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
429 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
430 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
431 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
432 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
433 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
434 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
435 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
438 if (state
& MESA_META_SCISSOR
) {
439 save
->Scissor
= ctx
->Scissor
; /* struct copy */
440 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
443 if (state
& MESA_META_SHADER
) {
444 if (ctx
->Extensions
.ARB_vertex_program
) {
445 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
446 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
447 ctx
->VertexProgram
.Current
);
448 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
451 if (ctx
->Extensions
.ARB_fragment_program
) {
452 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
453 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
454 ctx
->FragmentProgram
.Current
);
455 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
458 if (ctx
->Extensions
.ARB_shader_objects
) {
459 _mesa_reference_shader_program(ctx
, &save
->VertexShader
,
460 ctx
->Shader
.CurrentVertexProgram
);
461 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
,
462 ctx
->Shader
.CurrentGeometryProgram
);
463 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
,
464 ctx
->Shader
.CurrentFragmentProgram
);
465 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
466 ctx
->Shader
.CurrentFragmentProgram
);
468 _mesa_UseProgramObjectARB(0);
472 if (state
& MESA_META_STENCIL_TEST
) {
473 save
->Stencil
= ctx
->Stencil
; /* struct copy */
474 if (ctx
->Stencil
.Enabled
)
475 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
476 /* NOTE: other stencil state not reset */
479 if (state
& MESA_META_TEXTURE
) {
482 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
483 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
484 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
486 /* Disable all texture units */
487 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
488 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
489 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
490 if (ctx
->Texture
.Unit
[u
].Enabled
||
491 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
492 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
493 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
494 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
495 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
496 if (ctx
->Extensions
.ARB_texture_cube_map
)
497 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
498 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
499 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
500 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
501 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
502 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
506 /* save current texture objects for unit[0] only */
507 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
508 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
509 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
512 /* set defaults for unit[0] */
513 _mesa_ActiveTextureARB(GL_TEXTURE0
);
514 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
515 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
518 if (state
& MESA_META_TRANSFORM
) {
519 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
520 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
521 16 * sizeof(GLfloat
));
522 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
523 16 * sizeof(GLfloat
));
524 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
525 16 * sizeof(GLfloat
));
526 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
527 /* set 1:1 vertex:pixel coordinate transform */
528 _mesa_ActiveTextureARB(GL_TEXTURE0
);
529 _mesa_MatrixMode(GL_TEXTURE
);
530 _mesa_LoadIdentity();
531 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
532 _mesa_MatrixMode(GL_MODELVIEW
);
533 _mesa_LoadIdentity();
534 _mesa_MatrixMode(GL_PROJECTION
);
535 _mesa_LoadIdentity();
536 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
537 0.0, ctx
->DrawBuffer
->Height
,
541 if (state
& MESA_META_CLIP
) {
542 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
543 if (ctx
->Transform
.ClipPlanesEnabled
) {
545 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
546 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
551 if (state
& MESA_META_VERTEX
) {
552 /* save vertex array object state */
553 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
554 ctx
->Array
.ArrayObj
);
555 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
556 ctx
->Array
.ArrayBufferObj
);
557 /* set some default state? */
560 if (state
& MESA_META_VIEWPORT
) {
561 /* save viewport state */
562 save
->ViewportX
= ctx
->Viewport
.X
;
563 save
->ViewportY
= ctx
->Viewport
.Y
;
564 save
->ViewportW
= ctx
->Viewport
.Width
;
565 save
->ViewportH
= ctx
->Viewport
.Height
;
566 /* set viewport to match window size */
567 if (ctx
->Viewport
.X
!= 0 ||
568 ctx
->Viewport
.Y
!= 0 ||
569 ctx
->Viewport
.Width
!= ctx
->DrawBuffer
->Width
||
570 ctx
->Viewport
.Height
!= ctx
->DrawBuffer
->Height
) {
571 _mesa_set_viewport(ctx
, 0, 0,
572 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
574 /* save depth range state */
575 save
->DepthNear
= ctx
->Viewport
.Near
;
576 save
->DepthFar
= ctx
->Viewport
.Far
;
577 /* set depth range to default */
578 _mesa_DepthRange(0.0, 1.0);
581 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
582 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
584 /* Generally in here we want to do clamping according to whether
585 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
586 * regardless of the internal implementation of the metaops.
588 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
)
589 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
592 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
593 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
595 /* Generally in here we never want vertex color clamping --
596 * result clamping is only dependent on fragment clamping.
598 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
601 if (state
& MESA_META_CONDITIONAL_RENDER
) {
602 save
->CondRenderQuery
= ctx
->Query
.CondRenderQuery
;
603 save
->CondRenderMode
= ctx
->Query
.CondRenderMode
;
605 if (ctx
->Query
.CondRenderQuery
)
606 _mesa_EndConditionalRender();
611 save
->Lighting
= ctx
->Light
.Enabled
;
612 if (ctx
->Light
.Enabled
)
613 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
619 * Leave meta state. This is like a light-weight version of glPopAttrib().
622 _mesa_meta_end(struct gl_context
*ctx
)
624 struct save_state
*save
= &ctx
->Meta
->Save
[--ctx
->Meta
->SaveStackDepth
];
625 const GLbitfield state
= save
->SavedState
;
627 if (state
& MESA_META_ALPHA_TEST
) {
628 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
629 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
630 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
633 if (state
& MESA_META_BLEND
) {
634 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
635 if (ctx
->Extensions
.EXT_draw_buffers2
) {
637 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
638 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
642 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
645 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
646 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
649 if (state
& MESA_META_COLOR_MASK
) {
651 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
652 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
654 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
655 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
658 _mesa_ColorMaskIndexed(i
,
659 save
->ColorMask
[i
][0],
660 save
->ColorMask
[i
][1],
661 save
->ColorMask
[i
][2],
662 save
->ColorMask
[i
][3]);
668 if (state
& MESA_META_DEPTH_TEST
) {
669 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
670 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
671 _mesa_DepthFunc(save
->Depth
.Func
);
672 _mesa_DepthMask(save
->Depth
.Mask
);
675 if (state
& MESA_META_FOG
) {
676 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
679 if (state
& MESA_META_PIXEL_STORE
) {
680 ctx
->Pack
= save
->Pack
;
681 ctx
->Unpack
= save
->Unpack
;
684 if (state
& MESA_META_PIXEL_TRANSFER
) {
685 ctx
->Pixel
.RedScale
= save
->RedScale
;
686 ctx
->Pixel
.RedBias
= save
->RedBias
;
687 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
688 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
689 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
690 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
691 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
692 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
693 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
695 ctx
->NewState
|=_NEW_PIXEL
;
698 if (state
& MESA_META_RASTERIZATION
) {
699 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
700 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
701 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
702 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
703 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
704 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
707 if (state
& MESA_META_SCISSOR
) {
708 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
709 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
710 save
->Scissor
.Width
, save
->Scissor
.Height
);
713 if (state
& MESA_META_SHADER
) {
714 if (ctx
->Extensions
.ARB_vertex_program
) {
715 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
716 save
->VertexProgramEnabled
);
717 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
718 save
->VertexProgram
);
719 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
722 if (ctx
->Extensions
.ARB_fragment_program
) {
723 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
724 save
->FragmentProgramEnabled
);
725 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
726 save
->FragmentProgram
);
727 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
730 if (ctx
->Extensions
.ARB_vertex_shader
)
731 _mesa_use_shader_program(ctx
, GL_VERTEX_SHADER
, save
->VertexShader
);
733 if (ctx
->Extensions
.ARB_geometry_shader4
)
734 _mesa_use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
,
735 save
->GeometryShader
);
737 if (ctx
->Extensions
.ARB_fragment_shader
)
738 _mesa_use_shader_program(ctx
, GL_FRAGMENT_SHADER
,
739 save
->FragmentShader
);
741 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
745 if (state
& MESA_META_STENCIL_TEST
) {
746 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
748 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
749 _mesa_ClearStencil(stencil
->Clear
);
750 if (ctx
->Extensions
.EXT_stencil_two_side
) {
751 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
752 stencil
->TestTwoSide
);
753 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
754 ? GL_BACK
: GL_FRONT
);
757 _mesa_StencilFuncSeparate(GL_FRONT
,
758 stencil
->Function
[0],
760 stencil
->ValueMask
[0]);
761 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
762 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
763 stencil
->ZFailFunc
[0],
764 stencil
->ZPassFunc
[0]);
766 _mesa_StencilFuncSeparate(GL_BACK
,
767 stencil
->Function
[1],
769 stencil
->ValueMask
[1]);
770 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
771 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
772 stencil
->ZFailFunc
[1],
773 stencil
->ZPassFunc
[1]);
776 if (state
& MESA_META_TEXTURE
) {
779 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
781 /* restore texenv for unit[0] */
782 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
784 /* restore texture objects for unit[0] only */
785 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
786 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
787 save
->CurrentTexture
[tgt
]);
788 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
791 /* Re-enable textures, texgen */
792 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
793 if (save
->TexEnabled
[u
]) {
794 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
796 if (save
->TexEnabled
[u
] & TEXTURE_1D_BIT
)
797 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_TRUE
);
798 if (save
->TexEnabled
[u
] & TEXTURE_2D_BIT
)
799 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_TRUE
);
800 if (save
->TexEnabled
[u
] & TEXTURE_3D_BIT
)
801 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_TRUE
);
802 if (save
->TexEnabled
[u
] & TEXTURE_CUBE_BIT
)
803 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_TRUE
);
804 if (save
->TexEnabled
[u
] & TEXTURE_RECT_BIT
)
805 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_TRUE
);
808 if (save
->TexGenEnabled
[u
]) {
809 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
811 if (save
->TexGenEnabled
[u
] & S_BIT
)
812 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_TRUE
);
813 if (save
->TexGenEnabled
[u
] & T_BIT
)
814 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_TRUE
);
815 if (save
->TexGenEnabled
[u
] & R_BIT
)
816 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_TRUE
);
817 if (save
->TexGenEnabled
[u
] & Q_BIT
)
818 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_TRUE
);
822 /* restore current unit state */
823 _mesa_ActiveTextureARB(GL_TEXTURE0
+ save
->ActiveUnit
);
824 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ save
->ClientActiveUnit
);
827 if (state
& MESA_META_TRANSFORM
) {
828 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
829 _mesa_ActiveTextureARB(GL_TEXTURE0
);
830 _mesa_MatrixMode(GL_TEXTURE
);
831 _mesa_LoadMatrixf(save
->TextureMatrix
);
832 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
834 _mesa_MatrixMode(GL_MODELVIEW
);
835 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
837 _mesa_MatrixMode(GL_PROJECTION
);
838 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
840 _mesa_MatrixMode(save
->MatrixMode
);
843 if (state
& MESA_META_CLIP
) {
844 if (save
->ClipPlanesEnabled
) {
846 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
847 if (save
->ClipPlanesEnabled
& (1 << i
)) {
848 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
854 if (state
& MESA_META_VERTEX
) {
855 /* restore vertex buffer object */
856 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
857 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
859 /* restore vertex array object */
860 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
861 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
864 if (state
& MESA_META_VIEWPORT
) {
865 if (save
->ViewportX
!= ctx
->Viewport
.X
||
866 save
->ViewportY
!= ctx
->Viewport
.Y
||
867 save
->ViewportW
!= ctx
->Viewport
.Width
||
868 save
->ViewportH
!= ctx
->Viewport
.Height
) {
869 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
870 save
->ViewportW
, save
->ViewportH
);
872 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
875 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
876 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
879 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
880 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
883 if (state
& MESA_META_CONDITIONAL_RENDER
) {
884 if (save
->CondRenderQuery
)
885 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
886 save
->CondRenderMode
);
890 if (save
->Lighting
) {
891 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
897 * Convert Z from a normalized value in the range [0, 1] to an object-space
898 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
899 * default/identity ortho projection results in the original Z value.
900 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
901 * value comes from the clear value or raster position.
903 static INLINE GLfloat
904 invert_z(GLfloat normZ
)
906 GLfloat objZ
= 1.0 - 2.0 * normZ
;
912 * One-time init for a temp_texture object.
913 * Choose tex target, compute max tex size, etc.
916 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
918 /* prefer texture rectangle */
919 if (ctx
->Extensions
.NV_texture_rectangle
) {
920 tex
->Target
= GL_TEXTURE_RECTANGLE
;
921 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
925 /* use 2D texture, NPOT if possible */
926 tex
->Target
= GL_TEXTURE_2D
;
927 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
928 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
930 tex
->MinSize
= 16; /* 16 x 16 at least */
931 assert(tex
->MaxSize
> 0);
933 _mesa_GenTextures(1, &tex
->TexObj
);
938 * Return pointer to temp_texture info for non-bitmap ops.
939 * This does some one-time init if needed.
941 static struct temp_texture
*
942 get_temp_texture(struct gl_context
*ctx
)
944 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
947 init_temp_texture(ctx
, tex
);
955 * Return pointer to temp_texture info for _mesa_meta_bitmap().
956 * We use a separate texture for bitmaps to reduce texture
957 * allocation/deallocation.
959 static struct temp_texture
*
960 get_bitmap_temp_texture(struct gl_context
*ctx
)
962 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
965 init_temp_texture(ctx
, tex
);
973 * Compute the width/height of texture needed to draw an image of the
974 * given size. Return a flag indicating whether the current texture
975 * can be re-used (glTexSubImage2D) or if a new texture needs to be
976 * allocated (glTexImage2D).
977 * Also, compute s/t texcoords for drawing.
979 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
982 alloc_texture(struct temp_texture
*tex
,
983 GLsizei width
, GLsizei height
, GLenum intFormat
)
985 GLboolean newTex
= GL_FALSE
;
987 ASSERT(width
<= tex
->MaxSize
);
988 ASSERT(height
<= tex
->MaxSize
);
990 if (width
> tex
->Width
||
991 height
> tex
->Height
||
992 intFormat
!= tex
->IntFormat
) {
993 /* alloc new texture (larger or different format) */
996 /* use non-power of two size */
997 tex
->Width
= MAX2(tex
->MinSize
, width
);
998 tex
->Height
= MAX2(tex
->MinSize
, height
);
1001 /* find power of two size */
1003 w
= h
= tex
->MinSize
;
1012 tex
->IntFormat
= intFormat
;
1017 /* compute texcoords */
1018 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1019 tex
->Sright
= (GLfloat
) width
;
1020 tex
->Ttop
= (GLfloat
) height
;
1023 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1024 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1032 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1035 setup_copypix_texture(struct temp_texture
*tex
,
1037 GLint srcX
, GLint srcY
,
1038 GLsizei width
, GLsizei height
, GLenum intFormat
,
1041 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1042 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1043 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1044 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1046 /* copy framebuffer image to texture */
1048 /* create new tex image */
1049 if (tex
->Width
== width
&& tex
->Height
== height
) {
1050 /* create new tex with framebuffer data */
1051 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1052 srcX
, srcY
, width
, height
, 0);
1055 /* create empty texture */
1056 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1057 tex
->Width
, tex
->Height
, 0,
1058 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1060 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1061 0, 0, srcX
, srcY
, width
, height
);
1065 /* replace existing tex image */
1066 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1067 0, 0, srcX
, srcY
, width
, height
);
1073 * Setup/load texture for glDrawPixels.
1076 setup_drawpix_texture(struct gl_context
*ctx
,
1077 struct temp_texture
*tex
,
1079 GLenum texIntFormat
,
1080 GLsizei width
, GLsizei height
,
1081 GLenum format
, GLenum type
,
1082 const GLvoid
*pixels
)
1084 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1085 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1086 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1087 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1089 /* copy pixel data to texture */
1091 /* create new tex image */
1092 if (tex
->Width
== width
&& tex
->Height
== height
) {
1093 /* create new tex and load image data */
1094 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1095 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1098 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1100 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1101 ctx
->Unpack
.BufferObj
);
1102 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1103 /* create empty texture */
1104 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1105 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1106 if (save_unpack_obj
!= NULL
)
1107 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
,
1108 save_unpack_obj
->Name
);
1110 _mesa_TexSubImage2D(tex
->Target
, 0,
1111 0, 0, width
, height
, format
, type
, pixels
);
1115 /* replace existing tex image */
1116 _mesa_TexSubImage2D(tex
->Target
, 0,
1117 0, 0, width
, height
, format
, type
, pixels
);
1124 * One-time init for drawing depth pixels.
1127 init_blit_depth_pixels(struct gl_context
*ctx
)
1129 static const char *program
=
1131 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1134 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1135 struct temp_texture
*tex
= get_temp_texture(ctx
);
1136 const char *texTarget
;
1138 assert(blit
->DepthFP
== 0);
1140 /* replace %s with "RECT" or "2D" */
1141 assert(strlen(program
) + 4 < sizeof(program2
));
1142 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1146 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1148 _mesa_GenPrograms(1, &blit
->DepthFP
);
1149 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1150 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1151 strlen(program2
), (const GLubyte
*) program2
);
1156 * Try to do a glBlitFramebuffer using no-copy texturing.
1157 * We can do this when the src renderbuffer is actually a texture.
1158 * But if the src buffer == dst buffer we cannot do this.
1160 * \return new buffer mask indicating the buffers left to blit using the
1164 blitframebuffer_texture(struct gl_context
*ctx
,
1165 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1166 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1167 GLbitfield mask
, GLenum filter
)
1169 if (mask
& GL_COLOR_BUFFER_BIT
) {
1170 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1171 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1172 const struct gl_renderbuffer_attachment
*drawAtt
=
1173 &drawFb
->Attachment
[drawFb
->_ColorDrawBufferIndexes
[0]];
1174 const struct gl_renderbuffer_attachment
*readAtt
=
1175 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1177 if (readAtt
&& readAtt
->Texture
) {
1178 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1179 const GLuint srcLevel
= readAtt
->TextureLevel
;
1180 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
1181 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
1182 const GLint baseLevelSave
= texObj
->BaseLevel
;
1183 const GLint maxLevelSave
= texObj
->MaxLevel
;
1184 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
1185 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
1186 const GLenum srgbSave
= texObj
->Sampler
.sRGBDecode
;
1187 const GLenum fbo_srgb_save
= ctx
->Color
.sRGBEnabled
;
1188 const GLenum target
= texObj
->Target
;
1190 if (drawAtt
->Texture
== readAtt
->Texture
) {
1191 /* Can't use same texture as both the source and dest. We need
1192 * to handle overlapping blits and besides, some hw may not
1198 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1199 /* Can't handle other texture types at this time */
1204 printf("Blit from texture!\n");
1205 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1206 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1209 /* Prepare src texture state */
1210 _mesa_BindTexture(target
, texObj
->Name
);
1211 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
1212 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
1213 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1214 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1215 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1217 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1218 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1220 /* Always do our blits with no sRGB decode or encode.*/
1221 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1222 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
1223 GL_SKIP_DECODE_EXT
);
1225 _mesa_Disable(GL_FRAMEBUFFER_SRGB_EXT
);
1227 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1228 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1230 /* Prepare vertex data (the VBO was previously created and bound) */
1235 struct vertex verts
[4];
1236 GLfloat s0
, t0
, s1
, t1
;
1238 if (target
== GL_TEXTURE_2D
) {
1239 const struct gl_texture_image
*texImage
1240 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1241 s0
= srcX0
/ (float) texImage
->Width
;
1242 s1
= srcX1
/ (float) texImage
->Width
;
1243 t0
= srcY0
/ (float) texImage
->Height
;
1244 t1
= srcY1
/ (float) texImage
->Height
;
1247 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1254 verts
[0].x
= (GLfloat
) dstX0
;
1255 verts
[0].y
= (GLfloat
) dstY0
;
1256 verts
[1].x
= (GLfloat
) dstX1
;
1257 verts
[1].y
= (GLfloat
) dstY0
;
1258 verts
[2].x
= (GLfloat
) dstX1
;
1259 verts
[2].y
= (GLfloat
) dstY1
;
1260 verts
[3].x
= (GLfloat
) dstX0
;
1261 verts
[3].y
= (GLfloat
) dstY1
;
1272 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1275 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1277 /* Restore texture object state, the texture binding will
1278 * be restored by _mesa_meta_end().
1280 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
1281 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
1282 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1283 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1284 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1286 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
1287 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
1288 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1289 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
, srgbSave
);
1291 if (ctx
->Extensions
.EXT_texture_sRGB_decode
&& fbo_srgb_save
) {
1292 _mesa_Enable(GL_FRAMEBUFFER_SRGB_EXT
);
1295 /* Done with color buffer */
1296 mask
&= ~GL_COLOR_BUFFER_BIT
;
1305 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1306 * of texture mapping and polygon rendering.
1309 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1310 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1311 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1312 GLbitfield mask
, GLenum filter
)
1314 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1315 struct temp_texture
*tex
= get_temp_texture(ctx
);
1316 const GLsizei maxTexSize
= tex
->MaxSize
;
1317 const GLint srcX
= MIN2(srcX0
, srcX1
);
1318 const GLint srcY
= MIN2(srcY0
, srcY1
);
1319 const GLint srcW
= abs(srcX1
- srcX0
);
1320 const GLint srcH
= abs(srcY1
- srcY0
);
1321 const GLboolean srcFlipX
= srcX1
< srcX0
;
1322 const GLboolean srcFlipY
= srcY1
< srcY0
;
1326 struct vertex verts
[4];
1329 if (srcW
> maxTexSize
|| srcH
> maxTexSize
) {
1330 /* XXX avoid this fallback */
1331 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1332 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1348 /* only scissor effects blit so save/clear all other relevant state */
1349 _mesa_meta_begin(ctx
, ~MESA_META_SCISSOR
);
1351 if (blit
->ArrayObj
== 0) {
1352 /* one-time setup */
1354 /* create vertex array object */
1355 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1356 _mesa_BindVertexArray(blit
->ArrayObj
);
1358 /* create vertex array buffer */
1359 _mesa_GenBuffersARB(1, &blit
->VBO
);
1360 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1361 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1362 NULL
, GL_DYNAMIC_DRAW_ARB
);
1364 /* setup vertex arrays */
1365 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1366 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1367 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1368 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1371 _mesa_BindVertexArray(blit
->ArrayObj
);
1372 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1375 /* Try faster, direct texture approach first */
1376 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1377 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1379 _mesa_meta_end(ctx
);
1383 /* Continue with "normal" approach which involves copying the src rect
1384 * into a temporary texture and is "blitted" by drawing a textured quad.
1387 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1389 /* vertex positions/texcoords (after texture allocation!) */
1391 verts
[0].x
= (GLfloat
) dstX0
;
1392 verts
[0].y
= (GLfloat
) dstY0
;
1393 verts
[1].x
= (GLfloat
) dstX1
;
1394 verts
[1].y
= (GLfloat
) dstY0
;
1395 verts
[2].x
= (GLfloat
) dstX1
;
1396 verts
[2].y
= (GLfloat
) dstY1
;
1397 verts
[3].x
= (GLfloat
) dstX0
;
1398 verts
[3].y
= (GLfloat
) dstY1
;
1402 verts
[1].s
= tex
->Sright
;
1404 verts
[2].s
= tex
->Sright
;
1405 verts
[2].t
= tex
->Ttop
;
1407 verts
[3].t
= tex
->Ttop
;
1409 /* upload new vertex data */
1410 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1413 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1415 if (mask
& GL_COLOR_BUFFER_BIT
) {
1416 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1418 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1419 mask
&= ~GL_COLOR_BUFFER_BIT
;
1422 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1423 GLuint
*tmp
= (GLuint
*) malloc(srcW
* srcH
* sizeof(GLuint
));
1426 init_blit_depth_pixels(ctx
);
1428 /* maybe change tex format here */
1429 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1431 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1432 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1434 setup_drawpix_texture(ctx
, tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1435 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1437 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1438 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1439 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1440 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1441 _mesa_DepthFunc(GL_ALWAYS
);
1442 _mesa_DepthMask(GL_TRUE
);
1444 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1445 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1451 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1452 /* XXX can't easily do stencil */
1455 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1457 _mesa_meta_end(ctx
);
1460 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1461 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1467 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1470 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1472 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1474 GLfloat x
, y
, z
, r
, g
, b
, a
;
1476 struct vertex verts
[4];
1477 /* save all state but scissor, pixel pack/unpack */
1478 GLbitfield metaSave
= (MESA_META_ALL
-
1480 MESA_META_PIXEL_STORE
-
1481 MESA_META_CONDITIONAL_RENDER
);
1482 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1484 if (buffers
& BUFFER_BITS_COLOR
) {
1485 /* if clearing color buffers, don't save/restore colormask */
1486 metaSave
-= MESA_META_COLOR_MASK
;
1489 _mesa_meta_begin(ctx
, metaSave
);
1491 if (clear
->ArrayObj
== 0) {
1492 /* one-time setup */
1494 /* create vertex array object */
1495 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1496 _mesa_BindVertexArray(clear
->ArrayObj
);
1498 /* create vertex array buffer */
1499 _mesa_GenBuffersARB(1, &clear
->VBO
);
1500 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1502 /* setup vertex arrays */
1503 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1504 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1505 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1506 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1509 _mesa_BindVertexArray(clear
->ArrayObj
);
1510 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1513 /* GL_COLOR_BUFFER_BIT */
1514 if (buffers
& BUFFER_BITS_COLOR
) {
1515 /* leave colormask, glDrawBuffer state as-is */
1517 /* Clears never have the color clamped. */
1518 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1521 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1522 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1525 /* GL_DEPTH_BUFFER_BIT */
1526 if (buffers
& BUFFER_BIT_DEPTH
) {
1527 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1528 _mesa_DepthFunc(GL_ALWAYS
);
1529 _mesa_DepthMask(GL_TRUE
);
1532 assert(!ctx
->Depth
.Test
);
1535 /* GL_STENCIL_BUFFER_BIT */
1536 if (buffers
& BUFFER_BIT_STENCIL
) {
1537 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1538 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1539 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1540 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1541 ctx
->Stencil
.Clear
& stencilMax
,
1542 ctx
->Stencil
.WriteMask
[0]);
1545 assert(!ctx
->Stencil
.Enabled
);
1548 /* vertex positions/colors */
1550 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1551 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1552 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1553 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1554 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1571 for (i
= 0; i
< 4; i
++) {
1572 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
1573 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
1574 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
1575 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
1578 /* upload new vertex data */
1579 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1580 GL_DYNAMIC_DRAW_ARB
);
1584 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1586 _mesa_meta_end(ctx
);
1590 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
1592 const char *vs_source
=
1593 "attribute vec4 position;\n"
1596 " gl_Position = position;\n"
1598 const char *fs_source
=
1599 "uniform vec4 color;\n"
1602 " gl_FragColor = color;\n"
1606 if (clear
->ArrayObj
!= 0)
1609 /* create vertex array object */
1610 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1611 _mesa_BindVertexArray(clear
->ArrayObj
);
1613 /* create vertex array buffer */
1614 _mesa_GenBuffersARB(1, &clear
->VBO
);
1615 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1617 /* setup vertex arrays */
1618 _mesa_VertexAttribPointerARB(0, 3, GL_FLOAT
, GL_FALSE
, 0, (void *)0);
1619 _mesa_EnableVertexAttribArrayARB(0);
1621 vs
= _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER
);
1622 _mesa_ShaderSourceARB(vs
, 1, &vs_source
, NULL
);
1623 _mesa_CompileShaderARB(vs
);
1625 fs
= _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER
);
1626 _mesa_ShaderSourceARB(fs
, 1, &fs_source
, NULL
);
1627 _mesa_CompileShaderARB(fs
);
1629 clear
->ShaderProg
= _mesa_CreateProgramObjectARB();
1630 _mesa_AttachShader(clear
->ShaderProg
, fs
);
1631 _mesa_AttachShader(clear
->ShaderProg
, vs
);
1632 _mesa_BindAttribLocationARB(clear
->ShaderProg
, 0, "position");
1633 _mesa_LinkProgramARB(clear
->ShaderProg
);
1635 clear
->ColorLocation
= _mesa_GetUniformLocationARB(clear
->ShaderProg
,
1640 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1643 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1645 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1646 GLbitfield metaSave
;
1647 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1648 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1649 const float x0
= ((float)fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
1650 const float y0
= ((float)fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
1651 const float x1
= ((float)fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
1652 const float y1
= ((float)fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
1653 const float z
= -invert_z(ctx
->Depth
.Clear
);
1658 metaSave
= (MESA_META_ALPHA_TEST
|
1660 MESA_META_DEPTH_TEST
|
1661 MESA_META_RASTERIZATION
|
1663 MESA_META_STENCIL_TEST
|
1665 MESA_META_VIEWPORT
|
1667 MESA_META_CLAMP_FRAGMENT_COLOR
);
1669 if (!(buffers
& BUFFER_BITS_COLOR
)) {
1670 /* We'll use colormask to disable color writes. Otherwise,
1671 * respect color mask
1673 metaSave
|= MESA_META_COLOR_MASK
;
1676 _mesa_meta_begin(ctx
, metaSave
);
1678 meta_glsl_clear_init(ctx
, clear
);
1680 _mesa_UseProgramObjectARB(clear
->ShaderProg
);
1681 _mesa_Uniform4fvARB(clear
->ColorLocation
, 1,
1682 ctx
->Color
.ClearColor
.f
);
1684 _mesa_BindVertexArray(clear
->ArrayObj
);
1685 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1687 /* GL_COLOR_BUFFER_BIT */
1688 if (buffers
& BUFFER_BITS_COLOR
) {
1689 /* leave colormask, glDrawBuffer state as-is */
1691 /* Clears never have the color clamped. */
1692 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1695 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1696 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1699 /* GL_DEPTH_BUFFER_BIT */
1700 if (buffers
& BUFFER_BIT_DEPTH
) {
1701 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1702 _mesa_DepthFunc(GL_ALWAYS
);
1703 _mesa_DepthMask(GL_TRUE
);
1706 assert(!ctx
->Depth
.Test
);
1709 /* GL_STENCIL_BUFFER_BIT */
1710 if (buffers
& BUFFER_BIT_STENCIL
) {
1711 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1712 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1713 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1714 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1715 ctx
->Stencil
.Clear
& stencilMax
,
1716 ctx
->Stencil
.WriteMask
[0]);
1719 assert(!ctx
->Stencil
.Enabled
);
1722 /* vertex positions */
1736 /* upload new vertex data */
1737 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1738 GL_DYNAMIC_DRAW_ARB
);
1741 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1743 _mesa_meta_end(ctx
);
1747 * Meta implementation of ctx->Driver.CopyPixels() in terms
1748 * of texture mapping and polygon rendering and GLSL shaders.
1751 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
1752 GLsizei width
, GLsizei height
,
1753 GLint dstX
, GLint dstY
, GLenum type
)
1755 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1756 struct temp_texture
*tex
= get_temp_texture(ctx
);
1758 GLfloat x
, y
, z
, s
, t
;
1760 struct vertex verts
[4];
1762 GLenum intFormat
= GL_RGBA
;
1764 if (type
!= GL_COLOR
||
1765 ctx
->_ImageTransferState
||
1767 width
> tex
->MaxSize
||
1768 height
> tex
->MaxSize
) {
1769 /* XXX avoid this fallback */
1770 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1774 /* Most GL state applies to glCopyPixels, but a there's a few things
1775 * we need to override:
1777 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
1780 MESA_META_TRANSFORM
|
1783 MESA_META_VIEWPORT
));
1785 if (copypix
->ArrayObj
== 0) {
1786 /* one-time setup */
1788 /* create vertex array object */
1789 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
1790 _mesa_BindVertexArray(copypix
->ArrayObj
);
1792 /* create vertex array buffer */
1793 _mesa_GenBuffersARB(1, ©pix
->VBO
);
1794 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1795 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1796 NULL
, GL_DYNAMIC_DRAW_ARB
);
1798 /* setup vertex arrays */
1799 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1800 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1801 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1802 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1805 _mesa_BindVertexArray(copypix
->ArrayObj
);
1806 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1809 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
1811 /* vertex positions, texcoords (after texture allocation!) */
1813 const GLfloat dstX0
= (GLfloat
) dstX
;
1814 const GLfloat dstY0
= (GLfloat
) dstY
;
1815 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1816 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1817 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1827 verts
[1].s
= tex
->Sright
;
1832 verts
[2].s
= tex
->Sright
;
1833 verts
[2].t
= tex
->Ttop
;
1838 verts
[3].t
= tex
->Ttop
;
1840 /* upload new vertex data */
1841 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1844 /* Alloc/setup texture */
1845 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
1846 GL_RGBA
, GL_NEAREST
);
1848 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1850 /* draw textured quad */
1851 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1853 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1855 _mesa_meta_end(ctx
);
1861 * When the glDrawPixels() image size is greater than the max rectangle
1862 * texture size we use this function to break the glDrawPixels() image
1863 * into tiles which fit into the max texture size.
1866 tiled_draw_pixels(struct gl_context
*ctx
,
1868 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1869 GLenum format
, GLenum type
,
1870 const struct gl_pixelstore_attrib
*unpack
,
1871 const GLvoid
*pixels
)
1873 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
1876 if (tileUnpack
.RowLength
== 0)
1877 tileUnpack
.RowLength
= width
;
1879 for (i
= 0; i
< width
; i
+= tileSize
) {
1880 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
1881 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
1883 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
1885 for (j
= 0; j
< height
; j
+= tileSize
) {
1886 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
1887 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
1889 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
1891 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
1892 format
, type
, &tileUnpack
, pixels
);
1899 * One-time init for drawing stencil pixels.
1902 init_draw_stencil_pixels(struct gl_context
*ctx
)
1904 /* This program is run eight times, once for each stencil bit.
1905 * The stencil values to draw are found in an 8-bit alpha texture.
1906 * We read the texture/stencil value and test if bit 'b' is set.
1907 * If the bit is not set, use KIL to kill the fragment.
1908 * Finally, we use the stencil test to update the stencil buffer.
1910 * The basic algorithm for checking if a bit is set is:
1911 * if (is_odd(value / (1 << bit)))
1912 * result is one (or non-zero).
1915 * The program parameter contains three values:
1916 * parm.x = 255 / (1 << bit)
1920 static const char *program
=
1922 "PARAM parm = program.local[0]; \n"
1924 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1925 "# t = t * 255 / bit \n"
1926 "MUL t.x, t.a, parm.x; \n"
1929 "SUB t.x, t.x, t.y; \n"
1931 "MUL t.x, t.x, parm.y; \n"
1932 "# t = fract(t.x) \n"
1933 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
1934 "# t.x = (t.x == 0 ? 1 : 0) \n"
1935 "SGE t.x, -t.x, parm.z; \n"
1937 "# for debug only \n"
1938 "#MOV result.color, t.x; \n"
1940 char program2
[1000];
1941 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1942 struct temp_texture
*tex
= get_temp_texture(ctx
);
1943 const char *texTarget
;
1945 assert(drawpix
->StencilFP
== 0);
1947 /* replace %s with "RECT" or "2D" */
1948 assert(strlen(program
) + 4 < sizeof(program2
));
1949 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1953 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1955 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
1956 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1957 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1958 strlen(program2
), (const GLubyte
*) program2
);
1963 * One-time init for drawing depth pixels.
1966 init_draw_depth_pixels(struct gl_context
*ctx
)
1968 static const char *program
=
1970 "PARAM color = program.local[0]; \n"
1971 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1972 "MOV result.color, color; \n"
1975 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1976 struct temp_texture
*tex
= get_temp_texture(ctx
);
1977 const char *texTarget
;
1979 assert(drawpix
->DepthFP
== 0);
1981 /* replace %s with "RECT" or "2D" */
1982 assert(strlen(program
) + 4 < sizeof(program2
));
1983 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1987 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1989 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
1990 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1991 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1992 strlen(program2
), (const GLubyte
*) program2
);
1997 * Meta implementation of ctx->Driver.DrawPixels() in terms
1998 * of texture mapping and polygon rendering.
2001 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2002 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2003 GLenum format
, GLenum type
,
2004 const struct gl_pixelstore_attrib
*unpack
,
2005 const GLvoid
*pixels
)
2007 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2008 struct temp_texture
*tex
= get_temp_texture(ctx
);
2009 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2010 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2012 GLfloat x
, y
, z
, s
, t
;
2014 struct vertex verts
[4];
2015 GLenum texIntFormat
;
2016 GLboolean fallback
, newTex
;
2017 GLbitfield metaExtraSave
= 0x0;
2021 * Determine if we can do the glDrawPixels with texture mapping.
2023 fallback
= GL_FALSE
;
2024 if (ctx
->_ImageTransferState
||
2029 if (_mesa_is_color_format(format
)) {
2030 /* use more compact format when possible */
2031 /* XXX disable special case for GL_LUMINANCE for now to work around
2032 * apparent i965 driver bug (see bug #23670).
2034 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2035 texIntFormat
= format
;
2037 texIntFormat
= GL_RGBA
;
2039 /* If we're not supposed to clamp the resulting color, then just
2040 * promote our texture to fully float. We could do better by
2041 * just going for the matching set of channels, in floating
2044 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2045 ctx
->Extensions
.ARB_texture_float
)
2046 texIntFormat
= GL_RGBA32F
;
2048 else if (_mesa_is_stencil_format(format
)) {
2049 if (ctx
->Extensions
.ARB_fragment_program
&&
2050 ctx
->Pixel
.IndexShift
== 0 &&
2051 ctx
->Pixel
.IndexOffset
== 0 &&
2052 type
== GL_UNSIGNED_BYTE
) {
2053 /* We'll store stencil as alpha. This only works for GLubyte
2054 * image data because of how incoming values are mapped to alpha
2057 texIntFormat
= GL_ALPHA
;
2058 metaExtraSave
= (MESA_META_COLOR_MASK
|
2059 MESA_META_DEPTH_TEST
|
2061 MESA_META_STENCIL_TEST
);
2067 else if (_mesa_is_depth_format(format
)) {
2068 if (ctx
->Extensions
.ARB_depth_texture
&&
2069 ctx
->Extensions
.ARB_fragment_program
) {
2070 texIntFormat
= GL_DEPTH_COMPONENT
;
2071 metaExtraSave
= (MESA_META_SHADER
);
2082 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2083 format
, type
, unpack
, pixels
);
2088 * Check image size against max texture size, draw as tiles if needed.
2090 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2091 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2092 format
, type
, unpack
, pixels
);
2096 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2097 * but a there's a few things we need to override:
2099 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2102 MESA_META_TRANSFORM
|
2105 MESA_META_VIEWPORT
|
2106 MESA_META_CLAMP_FRAGMENT_COLOR
|
2109 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2111 /* vertex positions, texcoords (after texture allocation!) */
2113 const GLfloat x0
= (GLfloat
) x
;
2114 const GLfloat y0
= (GLfloat
) y
;
2115 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2116 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2117 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2127 verts
[1].s
= tex
->Sright
;
2132 verts
[2].s
= tex
->Sright
;
2133 verts
[2].t
= tex
->Ttop
;
2138 verts
[3].t
= tex
->Ttop
;
2141 if (drawpix
->ArrayObj
== 0) {
2142 /* one-time setup: create vertex array object */
2143 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
2145 _mesa_BindVertexArray(drawpix
->ArrayObj
);
2147 /* create vertex array buffer */
2148 _mesa_GenBuffersARB(1, &vbo
);
2149 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
2150 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2151 verts
, GL_DYNAMIC_DRAW_ARB
);
2153 /* setup vertex arrays */
2154 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2155 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2156 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2157 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2159 /* set given unpack params */
2160 ctx
->Unpack
= *unpack
;
2162 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2164 if (_mesa_is_stencil_format(format
)) {
2165 /* Drawing stencil */
2168 if (!drawpix
->StencilFP
)
2169 init_draw_stencil_pixels(ctx
);
2171 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2172 GL_ALPHA
, type
, pixels
);
2174 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2176 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2178 /* set all stencil bits to 0 */
2179 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2180 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2181 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2183 /* set stencil bits to 1 where needed */
2184 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2186 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2187 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2189 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2190 const GLuint mask
= 1 << bit
;
2191 if (mask
& origStencilMask
) {
2192 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2193 _mesa_StencilMask(mask
);
2195 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2196 255.0 / mask
, 0.5, 0.0, 0.0);
2198 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2202 else if (_mesa_is_depth_format(format
)) {
2204 if (!drawpix
->DepthFP
)
2205 init_draw_depth_pixels(ctx
);
2207 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2208 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2210 /* polygon color = current raster color */
2211 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2212 ctx
->Current
.RasterColor
);
2214 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2215 format
, type
, pixels
);
2217 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2221 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2222 format
, type
, pixels
);
2223 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2226 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2228 _mesa_DeleteBuffersARB(1, &vbo
);
2230 /* restore unpack params */
2231 ctx
->Unpack
= unpackSave
;
2233 _mesa_meta_end(ctx
);
2237 alpha_test_raster_color(struct gl_context
*ctx
)
2239 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2240 GLfloat ref
= ctx
->Color
.AlphaRef
;
2242 switch (ctx
->Color
.AlphaFunc
) {
2248 return alpha
== ref
;
2250 return alpha
<= ref
;
2254 return alpha
!= ref
;
2256 return alpha
>= ref
;
2266 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2267 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2268 * tracker would improve performance a lot.
2271 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2272 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2273 const struct gl_pixelstore_attrib
*unpack
,
2274 const GLubyte
*bitmap1
)
2276 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2277 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2278 const GLenum texIntFormat
= GL_ALPHA
;
2279 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2282 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2284 struct vertex verts
[4];
2289 * Check if swrast fallback is needed.
2291 if (ctx
->_ImageTransferState
||
2292 ctx
->FragmentProgram
._Enabled
||
2294 ctx
->Texture
._EnabledUnits
||
2295 width
> tex
->MaxSize
||
2296 height
> tex
->MaxSize
) {
2297 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2301 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2304 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2305 * but a there's a few things we need to override:
2307 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
2308 MESA_META_PIXEL_STORE
|
2309 MESA_META_RASTERIZATION
|
2312 MESA_META_TRANSFORM
|
2315 MESA_META_VIEWPORT
));
2317 if (bitmap
->ArrayObj
== 0) {
2318 /* one-time setup */
2320 /* create vertex array object */
2321 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2322 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2324 /* create vertex array buffer */
2325 _mesa_GenBuffersARB(1, &bitmap
->VBO
);
2326 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2327 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2328 NULL
, GL_DYNAMIC_DRAW_ARB
);
2330 /* setup vertex arrays */
2331 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2332 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2333 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2334 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2335 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2336 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2339 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2340 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2343 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2345 /* vertex positions, texcoords, colors (after texture allocation!) */
2347 const GLfloat x0
= (GLfloat
) x
;
2348 const GLfloat y0
= (GLfloat
) y
;
2349 const GLfloat x1
= (GLfloat
) (x
+ width
);
2350 const GLfloat y1
= (GLfloat
) (y
+ height
);
2351 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2362 verts
[1].s
= tex
->Sright
;
2367 verts
[2].s
= tex
->Sright
;
2368 verts
[2].t
= tex
->Ttop
;
2373 verts
[3].t
= tex
->Ttop
;
2375 for (i
= 0; i
< 4; i
++) {
2376 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2377 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2378 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2379 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2382 /* upload new vertex data */
2383 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2386 /* choose different foreground/background alpha values */
2387 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2388 bg
= (fg
> 127 ? 0 : 255);
2390 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2392 _mesa_meta_end(ctx
);
2396 bitmap8
= (GLubyte
*) malloc(width
* height
);
2398 memset(bitmap8
, bg
, width
* height
);
2399 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2400 bitmap8
, width
, fg
);
2402 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2404 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2405 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2407 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2408 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2410 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2412 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2417 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2419 _mesa_meta_end(ctx
);
2424 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2425 * software fallback. The fallback path will require that the texture
2426 * images are mapped.
2427 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2430 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
2431 struct gl_texture_object
*texObj
)
2433 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2434 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2435 struct gl_texture_image
*baseImage
;
2439 /* check for fallbacks */
2440 if (!ctx
->Extensions
.EXT_framebuffer_object
||
2441 target
== GL_TEXTURE_3D
||
2442 target
== GL_TEXTURE_1D_ARRAY
||
2443 target
== GL_TEXTURE_2D_ARRAY
) {
2447 srcLevel
= texObj
->BaseLevel
;
2448 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2449 if (!baseImage
|| _mesa_is_format_compressed(baseImage
->TexFormat
)) {
2454 * Test that we can actually render in the texture's format.
2457 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2458 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2460 if (target
== GL_TEXTURE_1D
) {
2461 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2462 GL_COLOR_ATTACHMENT0_EXT
,
2463 target
, texObj
->Name
, srcLevel
);
2466 /* other work is needed to enable 3D mipmap generation */
2467 else if (target
== GL_TEXTURE_3D
) {
2469 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2470 GL_COLOR_ATTACHMENT0_EXT
,
2471 target
, texObj
->Name
, srcLevel
, zoffset
);
2476 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2477 GL_COLOR_ATTACHMENT0_EXT
,
2478 target
, texObj
->Name
, srcLevel
);
2481 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2483 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2485 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2494 * Compute the texture coordinates for the four vertices of a quad for
2495 * drawing a 2D texture image or slice of a cube/3D texture.
2496 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2497 * \param slice slice of a 1D/2D array texture or 3D texture
2498 * \param width width of the texture image
2499 * \param height height of the texture image
2500 * \param coords0/1/2/3 returns the computed texcoords
2503 setup_texture_coords(GLenum faceTarget
,
2512 static const GLfloat st
[4][2] = {
2513 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2518 switch (faceTarget
) {
2522 case GL_TEXTURE_2D_ARRAY
:
2523 if (faceTarget
== GL_TEXTURE_3D
)
2525 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
2529 coords0
[0] = 0.0F
; /* s */
2530 coords0
[1] = 0.0F
; /* t */
2531 coords0
[2] = r
; /* r */
2542 case GL_TEXTURE_RECTANGLE_ARB
:
2543 coords0
[0] = 0.0F
; /* s */
2544 coords0
[1] = 0.0F
; /* t */
2545 coords0
[2] = 0.0F
; /* r */
2550 coords2
[1] = height
;
2553 coords3
[1] = height
;
2556 case GL_TEXTURE_1D_ARRAY
:
2557 coords0
[0] = 0.0F
; /* s */
2558 coords0
[1] = slice
; /* t */
2559 coords0
[2] = 0.0F
; /* r */
2571 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2572 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2573 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2574 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2575 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2576 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2577 /* loop over quad verts */
2578 for (i
= 0; i
< 4; i
++) {
2579 /* Compute sc = +/-scale and tc = +/-scale.
2580 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2581 * though that can still sometimes happen with this scale factor...
2583 const GLfloat scale
= 0.9999f
;
2584 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2585 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2605 switch (faceTarget
) {
2606 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2611 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2616 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2621 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2626 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2631 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2642 assert(0 && "unexpected target in meta setup_texture_coords()");
2648 * Called via ctx->Driver.GenerateMipmap()
2649 * Note: We don't yet support 3D textures, 1D/2D array textures or texture
2653 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
2654 struct gl_texture_object
*texObj
)
2656 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2658 GLfloat x
, y
, tex
[3];
2660 struct vertex verts
[4];
2661 const GLuint baseLevel
= texObj
->BaseLevel
;
2662 const GLuint maxLevel
= texObj
->MaxLevel
;
2663 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
2664 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
2665 const GLint maxLevelSave
= texObj
->MaxLevel
;
2666 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
2667 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
2668 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
2669 const GLenum wrapRSave
= texObj
->Sampler
.WrapR
;
2670 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2671 const GLuint original_active_unit
= ctx
->Texture
.CurrentUnit
;
2674 const GLuint border
= 0;
2675 const GLint slice
= 0;
2677 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
2678 _mesa_generate_mipmap(ctx
, target
, texObj
);
2682 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
2683 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
2684 faceTarget
= target
;
2685 target
= GL_TEXTURE_CUBE_MAP
;
2688 faceTarget
= target
;
2691 _mesa_meta_begin(ctx
, MESA_META_ALL
);
2693 if (original_active_unit
!= 0)
2694 _mesa_BindTexture(target
, texObj
->Name
);
2696 if (mipmap
->ArrayObj
== 0) {
2697 /* one-time setup */
2699 /* create vertex array object */
2700 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
2701 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
2703 /* create vertex array buffer */
2704 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
2705 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2706 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2707 NULL
, GL_DYNAMIC_DRAW_ARB
);
2709 /* setup vertex arrays */
2710 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2711 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
2712 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2713 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2716 _mesa_BindVertexArray(mipmap
->ArrayObj
);
2717 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2721 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2723 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2725 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
2726 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
2727 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
2728 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2729 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2730 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
2732 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2734 /* setup texcoords (XXX what about border?) */
2735 setup_texture_coords(faceTarget
,
2736 0.0, 0.0, /* width, height never used here */
2743 /* setup vertex positions */
2753 /* upload new vertex data */
2754 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2756 /* setup projection matrix */
2757 _mesa_MatrixMode(GL_PROJECTION
);
2758 _mesa_LoadIdentity();
2759 _mesa_Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
2761 /* texture is already locked, unlock now */
2762 _mesa_unlock_texture(ctx
, texObj
);
2764 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
2765 const struct gl_texture_image
*srcImage
;
2766 const GLuint srcLevel
= dstLevel
- 1;
2767 GLsizei srcWidth
, srcHeight
, srcDepth
;
2768 GLsizei dstWidth
, dstHeight
, dstDepth
;
2771 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
2772 assert(srcImage
->Border
== 0); /* XXX we can fix this */
2774 /* src size w/out border */
2775 srcWidth
= srcImage
->Width
- 2 * border
;
2776 srcHeight
= srcImage
->Height
- 2 * border
;
2777 srcDepth
= srcImage
->Depth
- 2 * border
;
2779 /* new dst size w/ border */
2780 dstWidth
= MAX2(1, srcWidth
/ 2) + 2 * border
;
2781 dstHeight
= MAX2(1, srcHeight
/ 2) + 2 * border
;
2782 dstDepth
= MAX2(1, srcDepth
/ 2) + 2 * border
;
2784 if (dstWidth
== srcImage
->Width
&&
2785 dstHeight
== srcImage
->Height
&&
2786 dstDepth
== srcImage
->Depth
) {
2791 /* Set MaxLevel large enough to hold the new level when we allocate it */
2792 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
2794 /* Create empty dest image */
2795 if (target
== GL_TEXTURE_1D
) {
2796 _mesa_TexImage1D(target
, dstLevel
, srcImage
->InternalFormat
,
2798 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2800 else if (target
== GL_TEXTURE_3D
) {
2801 _mesa_TexImage3D(target
, dstLevel
, srcImage
->InternalFormat
,
2802 dstWidth
, dstHeight
, dstDepth
, border
,
2803 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2807 _mesa_TexImage2D(faceTarget
, dstLevel
, srcImage
->InternalFormat
,
2808 dstWidth
, dstHeight
, border
,
2809 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2811 if (target
== GL_TEXTURE_CUBE_MAP
) {
2812 /* If texturing from a cube, we need to make sure all src faces
2813 * have been defined (even if we're not sampling from them.)
2814 * Otherwise the texture object will be 'incomplete' and
2815 * texturing from it will not be allowed.
2818 for (face
= 0; face
< 6; face
++) {
2819 if (!texObj
->Image
[face
][srcLevel
] ||
2820 texObj
->Image
[face
][srcLevel
]->Width
!= srcWidth
) {
2821 _mesa_TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ face
,
2822 srcLevel
, srcImage
->InternalFormat
,
2823 srcWidth
, srcHeight
, border
,
2824 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2830 /* limit minification to src level */
2831 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
2833 /* Set to draw into the current dstLevel */
2834 if (target
== GL_TEXTURE_1D
) {
2835 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2836 GL_COLOR_ATTACHMENT0_EXT
,
2841 else if (target
== GL_TEXTURE_3D
) {
2842 GLint zoffset
= 0; /* XXX unfinished */
2843 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2844 GL_COLOR_ATTACHMENT0_EXT
,
2851 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2852 GL_COLOR_ATTACHMENT0_EXT
,
2858 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
2861 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2862 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2867 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
2868 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
2870 /* setup viewport */
2871 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
2873 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2876 _mesa_lock_texture(ctx
, texObj
); /* relock */
2878 _mesa_meta_end(ctx
);
2880 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
2881 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
2882 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
2883 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
2884 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
2885 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
2886 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, wrapRSave
);
2888 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2893 * Determine the GL data type to use for the temporary image read with
2894 * ReadPixels() and passed to Tex[Sub]Image().
2897 get_temp_image_type(struct gl_context
*ctx
, GLenum baseFormat
)
2899 switch (baseFormat
) {
2904 case GL_LUMINANCE_ALPHA
:
2906 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2907 return GL_UNSIGNED_BYTE
;
2908 else if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2909 return GL_UNSIGNED_SHORT
;
2912 case GL_DEPTH_COMPONENT
:
2913 return GL_UNSIGNED_INT
;
2914 case GL_DEPTH_STENCIL
:
2915 return GL_UNSIGNED_INT_24_8
;
2917 _mesa_problem(ctx
, "Unexpected format in get_temp_image_type()");
2924 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2925 * Have to be careful with locking and meta state for pixel transfer.
2928 copy_tex_sub_image(struct gl_context
*ctx
,
2929 GLuint dims
, GLenum target
, GLint level
,
2930 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2932 GLsizei width
, GLsizei height
)
2934 struct gl_texture_object
*texObj
;
2935 struct gl_texture_image
*texImage
;
2936 GLenum format
, type
;
2940 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2941 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2943 /* Choose format/type for temporary image buffer */
2944 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
2945 if (format
== GL_LUMINANCE
||
2946 format
== GL_LUMINANCE_ALPHA
||
2947 format
== GL_INTENSITY
) {
2948 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
2949 * temp image buffer because glReadPixels will do L=R+G+B which is
2950 * not what we want (should be L=R).
2955 type
= get_temp_image_type(ctx
, format
);
2956 bpp
= _mesa_bytes_per_pixel(format
, type
);
2958 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_sub_image()");
2963 * Alloc image buffer (XXX could use a PBO)
2965 buf
= malloc(width
* height
* bpp
);
2967 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
2971 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2974 * Read image from framebuffer (disable pixel transfer ops)
2976 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
2977 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2978 format
, type
, &ctx
->Pack
, buf
);
2979 _mesa_meta_end(ctx
);
2981 _mesa_update_state(ctx
); /* to update pixel transfer state */
2984 * Store texture data (with pixel transfer ops)
2986 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
2987 if (target
== GL_TEXTURE_1D
) {
2988 ctx
->Driver
.TexSubImage1D(ctx
, target
, level
, xoffset
,
2989 width
, format
, type
, buf
,
2990 &ctx
->Unpack
, texObj
, texImage
);
2992 else if (target
== GL_TEXTURE_3D
) {
2993 ctx
->Driver
.TexSubImage3D(ctx
, target
, level
, xoffset
, yoffset
, zoffset
,
2994 width
, height
, 1, format
, type
, buf
,
2995 &ctx
->Unpack
, texObj
, texImage
);
2998 ctx
->Driver
.TexSubImage2D(ctx
, target
, level
, xoffset
, yoffset
,
2999 width
, height
, format
, type
, buf
,
3000 &ctx
->Unpack
, texObj
, texImage
);
3002 _mesa_meta_end(ctx
);
3004 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
3011 _mesa_meta_CopyTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
3013 GLint x
, GLint y
, GLsizei width
)
3015 copy_tex_sub_image(ctx
, 1, target
, level
, xoffset
, 0, 0,
3021 _mesa_meta_CopyTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
3022 GLint xoffset
, GLint yoffset
,
3024 GLsizei width
, GLsizei height
)
3026 copy_tex_sub_image(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
3027 x
, y
, width
, height
);
3032 _mesa_meta_CopyTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
3033 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3035 GLsizei width
, GLsizei height
)
3037 copy_tex_sub_image(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
3038 x
, y
, width
, height
);
3043 * Decompress a texture image by drawing a quad with the compressed
3044 * texture and reading the pixels out of the color buffer.
3045 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
3046 * \param destFormat format, ala glReadPixels
3047 * \param destType type, ala glReadPixels
3048 * \param dest destination buffer
3049 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
3052 decompress_texture_image(struct gl_context
*ctx
,
3053 struct gl_texture_image
*texImage
,
3055 GLenum destFormat
, GLenum destType
,
3056 GLvoid
*dest
, GLint destRowLength
)
3058 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
3059 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3060 const GLint width
= texImage
->Width
;
3061 const GLint height
= texImage
->Height
;
3062 const GLenum target
= texObj
->Target
;
3065 GLfloat x
, y
, tex
[3];
3067 struct vertex verts
[4];
3068 GLuint fboDrawSave
, fboReadSave
;
3071 assert(target
== GL_TEXTURE_3D
||
3072 target
== GL_TEXTURE_2D_ARRAY
);
3075 if (target
== GL_TEXTURE_CUBE_MAP
) {
3076 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
3079 faceTarget
= target
;
3082 /* save fbo bindings (not saved by _mesa_meta_begin()) */
3083 fboDrawSave
= ctx
->DrawBuffer
->Name
;
3084 fboReadSave
= ctx
->ReadBuffer
->Name
;
3086 _mesa_meta_begin(ctx
, MESA_META_ALL
);
3088 /* Create/bind FBO/renderbuffer */
3089 if (decompress
->FBO
== 0) {
3090 _mesa_GenFramebuffersEXT(1, &decompress
->FBO
);
3091 _mesa_GenRenderbuffersEXT(1, &decompress
->RBO
);
3092 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3093 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3094 _mesa_FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT
,
3095 GL_COLOR_ATTACHMENT0_EXT
,
3096 GL_RENDERBUFFER_EXT
,
3100 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3103 /* alloc dest surface */
3104 if (width
!= decompress
->Width
|| height
!= decompress
->Height
) {
3105 _mesa_RenderbufferStorageEXT(GL_RENDERBUFFER_EXT
, GL_RGBA
,
3107 decompress
->Width
= width
;
3108 decompress
->Height
= height
;
3111 /* setup VBO data */
3112 if (decompress
->ArrayObj
== 0) {
3113 /* create vertex array object */
3114 _mesa_GenVertexArrays(1, &decompress
->ArrayObj
);
3115 _mesa_BindVertexArray(decompress
->ArrayObj
);
3117 /* create vertex array buffer */
3118 _mesa_GenBuffersARB(1, &decompress
->VBO
);
3119 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3120 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3121 NULL
, GL_DYNAMIC_DRAW_ARB
);
3123 /* setup vertex arrays */
3124 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3125 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
3126 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3127 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3130 _mesa_BindVertexArray(decompress
->ArrayObj
);
3131 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3134 setup_texture_coords(faceTarget
, slice
, width
, height
,
3140 /* setup vertex positions */
3146 verts
[2].y
= height
;
3148 verts
[3].y
= height
;
3150 /* upload new vertex data */
3151 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3153 /* setup texture state */
3154 _mesa_BindTexture(target
, texObj
->Name
);
3155 _mesa_Enable(target
);
3158 /* save texture object state */
3159 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
3160 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
3161 const GLint baseLevelSave
= texObj
->BaseLevel
;
3162 const GLint maxLevelSave
= texObj
->MaxLevel
;
3163 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
3164 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
3165 const GLenum srgbSave
= texObj
->Sampler
.sRGBDecode
;
3167 /* restrict sampling to the texture level of interest */
3168 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, texImage
->Level
);
3169 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texImage
->Level
);
3170 /* nearest filtering */
3171 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
3172 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
3174 /* No sRGB decode or encode.*/
3175 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3176 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
3177 GL_SKIP_DECODE_EXT
);
3179 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
3180 _mesa_Disable(GL_FRAMEBUFFER_SRGB_EXT
);
3183 /* render quad w/ texture into renderbuffer */
3184 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3186 /* Restore texture object state, the texture binding will
3187 * be restored by _mesa_meta_end().
3189 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
3190 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
3191 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3192 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
3193 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3195 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
3196 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
3197 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3198 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
, srgbSave
);
3202 /* read pixels from renderbuffer */
3203 ctx
->Pack
.RowLength
= destRowLength
;
3204 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
3206 _mesa_meta_end(ctx
);
3208 /* restore fbo bindings */
3209 if (fboDrawSave
== fboReadSave
) {
3210 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboDrawSave
);
3213 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT
, fboDrawSave
);
3214 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT
, fboReadSave
);
3220 * This is just a wrapper around _mesa_get_tex_image() and
3221 * decompress_texture_image(). Meta functions should not be directly called
3225 _mesa_meta_GetTexImage(struct gl_context
*ctx
, GLenum target
, GLint level
,
3226 GLenum format
, GLenum type
, GLvoid
*pixels
,
3227 struct gl_texture_object
*texObj
,
3228 struct gl_texture_image
*texImage
)
3230 /* We can only use the decompress-with-blit method here if the texels are
3231 * unsigned, normalized values. We could handle signed and unnormalized
3232 * with floating point renderbuffers...
3234 if (_mesa_is_format_compressed(texImage
->TexFormat
) &&
3235 _mesa_get_format_datatype(texImage
->TexFormat
)
3236 == GL_UNSIGNED_NORMALIZED
) {
3237 const GLuint slice
= 0; /* only 2D compressed textures for now */
3238 /* Need to unlock the texture here to prevent deadlock... */
3239 _mesa_unlock_texture(ctx
, texObj
);
3240 decompress_texture_image(ctx
, texImage
, slice
, format
, type
, pixels
,
3241 ctx
->Pack
.RowLength
);
3242 /* ... and relock it */
3243 _mesa_lock_texture(ctx
, texObj
);
3246 _mesa_get_teximage(ctx
, target
, level
, format
, type
, pixels
,