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
,
744 _mesa_reference_shader_program(ctx
, &save
->VertexShader
, NULL
);
745 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
, NULL
);
746 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
, NULL
);
747 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
, NULL
);
750 if (state
& MESA_META_STENCIL_TEST
) {
751 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
753 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
754 _mesa_ClearStencil(stencil
->Clear
);
755 if (ctx
->Extensions
.EXT_stencil_two_side
) {
756 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
757 stencil
->TestTwoSide
);
758 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
759 ? GL_BACK
: GL_FRONT
);
762 _mesa_StencilFuncSeparate(GL_FRONT
,
763 stencil
->Function
[0],
765 stencil
->ValueMask
[0]);
766 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
767 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
768 stencil
->ZFailFunc
[0],
769 stencil
->ZPassFunc
[0]);
771 _mesa_StencilFuncSeparate(GL_BACK
,
772 stencil
->Function
[1],
774 stencil
->ValueMask
[1]);
775 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
776 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
777 stencil
->ZFailFunc
[1],
778 stencil
->ZPassFunc
[1]);
781 if (state
& MESA_META_TEXTURE
) {
784 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
786 /* restore texenv for unit[0] */
787 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
789 /* restore texture objects for unit[0] only */
790 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
791 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
792 save
->CurrentTexture
[tgt
]);
793 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
796 /* Re-enable textures, texgen */
797 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
798 if (save
->TexEnabled
[u
]) {
799 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
801 if (save
->TexEnabled
[u
] & TEXTURE_1D_BIT
)
802 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_TRUE
);
803 if (save
->TexEnabled
[u
] & TEXTURE_2D_BIT
)
804 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_TRUE
);
805 if (save
->TexEnabled
[u
] & TEXTURE_3D_BIT
)
806 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_TRUE
);
807 if (save
->TexEnabled
[u
] & TEXTURE_CUBE_BIT
)
808 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_TRUE
);
809 if (save
->TexEnabled
[u
] & TEXTURE_RECT_BIT
)
810 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_TRUE
);
813 if (save
->TexGenEnabled
[u
]) {
814 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
816 if (save
->TexGenEnabled
[u
] & S_BIT
)
817 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_TRUE
);
818 if (save
->TexGenEnabled
[u
] & T_BIT
)
819 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_TRUE
);
820 if (save
->TexGenEnabled
[u
] & R_BIT
)
821 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_TRUE
);
822 if (save
->TexGenEnabled
[u
] & Q_BIT
)
823 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_TRUE
);
827 /* restore current unit state */
828 _mesa_ActiveTextureARB(GL_TEXTURE0
+ save
->ActiveUnit
);
829 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ save
->ClientActiveUnit
);
832 if (state
& MESA_META_TRANSFORM
) {
833 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
834 _mesa_ActiveTextureARB(GL_TEXTURE0
);
835 _mesa_MatrixMode(GL_TEXTURE
);
836 _mesa_LoadMatrixf(save
->TextureMatrix
);
837 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
839 _mesa_MatrixMode(GL_MODELVIEW
);
840 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
842 _mesa_MatrixMode(GL_PROJECTION
);
843 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
845 _mesa_MatrixMode(save
->MatrixMode
);
848 if (state
& MESA_META_CLIP
) {
849 if (save
->ClipPlanesEnabled
) {
851 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
852 if (save
->ClipPlanesEnabled
& (1 << i
)) {
853 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
859 if (state
& MESA_META_VERTEX
) {
860 /* restore vertex buffer object */
861 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
862 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
864 /* restore vertex array object */
865 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
866 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
869 if (state
& MESA_META_VIEWPORT
) {
870 if (save
->ViewportX
!= ctx
->Viewport
.X
||
871 save
->ViewportY
!= ctx
->Viewport
.Y
||
872 save
->ViewportW
!= ctx
->Viewport
.Width
||
873 save
->ViewportH
!= ctx
->Viewport
.Height
) {
874 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
875 save
->ViewportW
, save
->ViewportH
);
877 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
880 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
881 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
884 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
885 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
888 if (state
& MESA_META_CONDITIONAL_RENDER
) {
889 if (save
->CondRenderQuery
)
890 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
891 save
->CondRenderMode
);
895 if (save
->Lighting
) {
896 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
902 * Convert Z from a normalized value in the range [0, 1] to an object-space
903 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
904 * default/identity ortho projection results in the original Z value.
905 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
906 * value comes from the clear value or raster position.
908 static INLINE GLfloat
909 invert_z(GLfloat normZ
)
911 GLfloat objZ
= 1.0 - 2.0 * normZ
;
917 * One-time init for a temp_texture object.
918 * Choose tex target, compute max tex size, etc.
921 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
923 /* prefer texture rectangle */
924 if (ctx
->Extensions
.NV_texture_rectangle
) {
925 tex
->Target
= GL_TEXTURE_RECTANGLE
;
926 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
930 /* use 2D texture, NPOT if possible */
931 tex
->Target
= GL_TEXTURE_2D
;
932 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
933 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
935 tex
->MinSize
= 16; /* 16 x 16 at least */
936 assert(tex
->MaxSize
> 0);
938 _mesa_GenTextures(1, &tex
->TexObj
);
943 * Return pointer to temp_texture info for non-bitmap ops.
944 * This does some one-time init if needed.
946 static struct temp_texture
*
947 get_temp_texture(struct gl_context
*ctx
)
949 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
952 init_temp_texture(ctx
, tex
);
960 * Return pointer to temp_texture info for _mesa_meta_bitmap().
961 * We use a separate texture for bitmaps to reduce texture
962 * allocation/deallocation.
964 static struct temp_texture
*
965 get_bitmap_temp_texture(struct gl_context
*ctx
)
967 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
970 init_temp_texture(ctx
, tex
);
978 * Compute the width/height of texture needed to draw an image of the
979 * given size. Return a flag indicating whether the current texture
980 * can be re-used (glTexSubImage2D) or if a new texture needs to be
981 * allocated (glTexImage2D).
982 * Also, compute s/t texcoords for drawing.
984 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
987 alloc_texture(struct temp_texture
*tex
,
988 GLsizei width
, GLsizei height
, GLenum intFormat
)
990 GLboolean newTex
= GL_FALSE
;
992 ASSERT(width
<= tex
->MaxSize
);
993 ASSERT(height
<= tex
->MaxSize
);
995 if (width
> tex
->Width
||
996 height
> tex
->Height
||
997 intFormat
!= tex
->IntFormat
) {
998 /* alloc new texture (larger or different format) */
1001 /* use non-power of two size */
1002 tex
->Width
= MAX2(tex
->MinSize
, width
);
1003 tex
->Height
= MAX2(tex
->MinSize
, height
);
1006 /* find power of two size */
1008 w
= h
= tex
->MinSize
;
1017 tex
->IntFormat
= intFormat
;
1022 /* compute texcoords */
1023 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1024 tex
->Sright
= (GLfloat
) width
;
1025 tex
->Ttop
= (GLfloat
) height
;
1028 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1029 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1037 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1040 setup_copypix_texture(struct temp_texture
*tex
,
1042 GLint srcX
, GLint srcY
,
1043 GLsizei width
, GLsizei height
, GLenum intFormat
,
1046 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1047 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1048 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1049 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1051 /* copy framebuffer image to texture */
1053 /* create new tex image */
1054 if (tex
->Width
== width
&& tex
->Height
== height
) {
1055 /* create new tex with framebuffer data */
1056 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1057 srcX
, srcY
, width
, height
, 0);
1060 /* create empty texture */
1061 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1062 tex
->Width
, tex
->Height
, 0,
1063 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1065 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1066 0, 0, srcX
, srcY
, width
, height
);
1070 /* replace existing tex image */
1071 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1072 0, 0, srcX
, srcY
, width
, height
);
1078 * Setup/load texture for glDrawPixels.
1081 setup_drawpix_texture(struct gl_context
*ctx
,
1082 struct temp_texture
*tex
,
1084 GLenum texIntFormat
,
1085 GLsizei width
, GLsizei height
,
1086 GLenum format
, GLenum type
,
1087 const GLvoid
*pixels
)
1089 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1090 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1091 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1092 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1094 /* copy pixel data to texture */
1096 /* create new tex image */
1097 if (tex
->Width
== width
&& tex
->Height
== height
) {
1098 /* create new tex and load image data */
1099 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1100 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1103 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1105 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1106 ctx
->Unpack
.BufferObj
);
1107 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1108 /* create empty texture */
1109 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1110 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1111 if (save_unpack_obj
!= NULL
)
1112 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
,
1113 save_unpack_obj
->Name
);
1115 _mesa_TexSubImage2D(tex
->Target
, 0,
1116 0, 0, width
, height
, format
, type
, pixels
);
1120 /* replace existing tex image */
1121 _mesa_TexSubImage2D(tex
->Target
, 0,
1122 0, 0, width
, height
, format
, type
, pixels
);
1129 * One-time init for drawing depth pixels.
1132 init_blit_depth_pixels(struct gl_context
*ctx
)
1134 static const char *program
=
1136 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1139 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1140 struct temp_texture
*tex
= get_temp_texture(ctx
);
1141 const char *texTarget
;
1143 assert(blit
->DepthFP
== 0);
1145 /* replace %s with "RECT" or "2D" */
1146 assert(strlen(program
) + 4 < sizeof(program2
));
1147 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1151 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1153 _mesa_GenPrograms(1, &blit
->DepthFP
);
1154 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1155 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1156 strlen(program2
), (const GLubyte
*) program2
);
1161 * Try to do a glBlitFramebuffer using no-copy texturing.
1162 * We can do this when the src renderbuffer is actually a texture.
1163 * But if the src buffer == dst buffer we cannot do this.
1165 * \return new buffer mask indicating the buffers left to blit using the
1169 blitframebuffer_texture(struct gl_context
*ctx
,
1170 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1171 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1172 GLbitfield mask
, GLenum filter
)
1174 if (mask
& GL_COLOR_BUFFER_BIT
) {
1175 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1176 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1177 const struct gl_renderbuffer_attachment
*drawAtt
=
1178 &drawFb
->Attachment
[drawFb
->_ColorDrawBufferIndexes
[0]];
1179 const struct gl_renderbuffer_attachment
*readAtt
=
1180 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1182 if (readAtt
&& readAtt
->Texture
) {
1183 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1184 const GLuint srcLevel
= readAtt
->TextureLevel
;
1185 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
1186 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
1187 const GLint baseLevelSave
= texObj
->BaseLevel
;
1188 const GLint maxLevelSave
= texObj
->MaxLevel
;
1189 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
1190 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
1191 const GLenum srgbSave
= texObj
->Sampler
.sRGBDecode
;
1192 const GLenum fbo_srgb_save
= ctx
->Color
.sRGBEnabled
;
1193 const GLenum target
= texObj
->Target
;
1195 if (drawAtt
->Texture
== readAtt
->Texture
) {
1196 /* Can't use same texture as both the source and dest. We need
1197 * to handle overlapping blits and besides, some hw may not
1203 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1204 /* Can't handle other texture types at this time */
1209 printf("Blit from texture!\n");
1210 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1211 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1214 /* Prepare src texture state */
1215 _mesa_BindTexture(target
, texObj
->Name
);
1216 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
1217 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
1218 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1219 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1220 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1222 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1223 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1225 /* Always do our blits with no sRGB decode or encode.*/
1226 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1227 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
1228 GL_SKIP_DECODE_EXT
);
1230 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
1231 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
1234 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1235 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1237 /* Prepare vertex data (the VBO was previously created and bound) */
1242 struct vertex verts
[4];
1243 GLfloat s0
, t0
, s1
, t1
;
1245 if (target
== GL_TEXTURE_2D
) {
1246 const struct gl_texture_image
*texImage
1247 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1248 s0
= srcX0
/ (float) texImage
->Width
;
1249 s1
= srcX1
/ (float) texImage
->Width
;
1250 t0
= srcY0
/ (float) texImage
->Height
;
1251 t1
= srcY1
/ (float) texImage
->Height
;
1254 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1261 verts
[0].x
= (GLfloat
) dstX0
;
1262 verts
[0].y
= (GLfloat
) dstY0
;
1263 verts
[1].x
= (GLfloat
) dstX1
;
1264 verts
[1].y
= (GLfloat
) dstY0
;
1265 verts
[2].x
= (GLfloat
) dstX1
;
1266 verts
[2].y
= (GLfloat
) dstY1
;
1267 verts
[3].x
= (GLfloat
) dstX0
;
1268 verts
[3].y
= (GLfloat
) dstY1
;
1279 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1282 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1284 /* Restore texture object state, the texture binding will
1285 * be restored by _mesa_meta_end().
1287 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
1288 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
1289 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1290 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1291 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1293 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
1294 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
1295 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1296 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
, srgbSave
);
1298 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&& fbo_srgb_save
) {
1299 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_TRUE
);
1302 /* Done with color buffer */
1303 mask
&= ~GL_COLOR_BUFFER_BIT
;
1312 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1313 * of texture mapping and polygon rendering.
1316 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1317 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1318 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1319 GLbitfield mask
, GLenum filter
)
1321 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1322 struct temp_texture
*tex
= get_temp_texture(ctx
);
1323 const GLsizei maxTexSize
= tex
->MaxSize
;
1324 const GLint srcX
= MIN2(srcX0
, srcX1
);
1325 const GLint srcY
= MIN2(srcY0
, srcY1
);
1326 const GLint srcW
= abs(srcX1
- srcX0
);
1327 const GLint srcH
= abs(srcY1
- srcY0
);
1328 const GLboolean srcFlipX
= srcX1
< srcX0
;
1329 const GLboolean srcFlipY
= srcY1
< srcY0
;
1333 struct vertex verts
[4];
1336 if (srcW
> maxTexSize
|| srcH
> maxTexSize
) {
1337 /* XXX avoid this fallback */
1338 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1339 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1355 /* only scissor effects blit so save/clear all other relevant state */
1356 _mesa_meta_begin(ctx
, ~MESA_META_SCISSOR
);
1358 if (blit
->ArrayObj
== 0) {
1359 /* one-time setup */
1361 /* create vertex array object */
1362 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1363 _mesa_BindVertexArray(blit
->ArrayObj
);
1365 /* create vertex array buffer */
1366 _mesa_GenBuffersARB(1, &blit
->VBO
);
1367 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1368 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1369 NULL
, GL_DYNAMIC_DRAW_ARB
);
1371 /* setup vertex arrays */
1372 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1373 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1374 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1375 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1378 _mesa_BindVertexArray(blit
->ArrayObj
);
1379 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1382 /* Try faster, direct texture approach first */
1383 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1384 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1386 _mesa_meta_end(ctx
);
1390 /* Continue with "normal" approach which involves copying the src rect
1391 * into a temporary texture and is "blitted" by drawing a textured quad.
1394 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1396 /* vertex positions/texcoords (after texture allocation!) */
1398 verts
[0].x
= (GLfloat
) dstX0
;
1399 verts
[0].y
= (GLfloat
) dstY0
;
1400 verts
[1].x
= (GLfloat
) dstX1
;
1401 verts
[1].y
= (GLfloat
) dstY0
;
1402 verts
[2].x
= (GLfloat
) dstX1
;
1403 verts
[2].y
= (GLfloat
) dstY1
;
1404 verts
[3].x
= (GLfloat
) dstX0
;
1405 verts
[3].y
= (GLfloat
) dstY1
;
1409 verts
[1].s
= tex
->Sright
;
1411 verts
[2].s
= tex
->Sright
;
1412 verts
[2].t
= tex
->Ttop
;
1414 verts
[3].t
= tex
->Ttop
;
1416 /* upload new vertex data */
1417 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1420 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1422 if (mask
& GL_COLOR_BUFFER_BIT
) {
1423 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1425 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1426 mask
&= ~GL_COLOR_BUFFER_BIT
;
1429 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1430 GLuint
*tmp
= (GLuint
*) malloc(srcW
* srcH
* sizeof(GLuint
));
1433 init_blit_depth_pixels(ctx
);
1435 /* maybe change tex format here */
1436 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1438 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1439 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1441 setup_drawpix_texture(ctx
, tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1442 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1444 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1445 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1446 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1447 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1448 _mesa_DepthFunc(GL_ALWAYS
);
1449 _mesa_DepthMask(GL_TRUE
);
1451 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1452 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1458 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1459 /* XXX can't easily do stencil */
1462 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1464 _mesa_meta_end(ctx
);
1467 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1468 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1474 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1477 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1479 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1481 GLfloat x
, y
, z
, r
, g
, b
, a
;
1483 struct vertex verts
[4];
1484 /* save all state but scissor, pixel pack/unpack */
1485 GLbitfield metaSave
= (MESA_META_ALL
-
1487 MESA_META_PIXEL_STORE
-
1488 MESA_META_CONDITIONAL_RENDER
);
1489 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1491 if (buffers
& BUFFER_BITS_COLOR
) {
1492 /* if clearing color buffers, don't save/restore colormask */
1493 metaSave
-= MESA_META_COLOR_MASK
;
1496 _mesa_meta_begin(ctx
, metaSave
);
1498 if (clear
->ArrayObj
== 0) {
1499 /* one-time setup */
1501 /* create vertex array object */
1502 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1503 _mesa_BindVertexArray(clear
->ArrayObj
);
1505 /* create vertex array buffer */
1506 _mesa_GenBuffersARB(1, &clear
->VBO
);
1507 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1509 /* setup vertex arrays */
1510 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1511 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1512 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1513 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1516 _mesa_BindVertexArray(clear
->ArrayObj
);
1517 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1520 /* GL_COLOR_BUFFER_BIT */
1521 if (buffers
& BUFFER_BITS_COLOR
) {
1522 /* leave colormask, glDrawBuffer state as-is */
1524 /* Clears never have the color clamped. */
1525 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1528 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1529 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1532 /* GL_DEPTH_BUFFER_BIT */
1533 if (buffers
& BUFFER_BIT_DEPTH
) {
1534 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1535 _mesa_DepthFunc(GL_ALWAYS
);
1536 _mesa_DepthMask(GL_TRUE
);
1539 assert(!ctx
->Depth
.Test
);
1542 /* GL_STENCIL_BUFFER_BIT */
1543 if (buffers
& BUFFER_BIT_STENCIL
) {
1544 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1545 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1546 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1547 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1548 ctx
->Stencil
.Clear
& stencilMax
,
1549 ctx
->Stencil
.WriteMask
[0]);
1552 assert(!ctx
->Stencil
.Enabled
);
1555 /* vertex positions/colors */
1557 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1558 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1559 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1560 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1561 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1578 for (i
= 0; i
< 4; i
++) {
1579 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
1580 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
1581 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
1582 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
1585 /* upload new vertex data */
1586 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1587 GL_DYNAMIC_DRAW_ARB
);
1591 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1593 _mesa_meta_end(ctx
);
1597 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
1599 const char *vs_source
=
1600 "attribute vec4 position;\n"
1603 " gl_Position = position;\n"
1605 const char *fs_source
=
1606 "uniform vec4 color;\n"
1609 " gl_FragColor = color;\n"
1613 if (clear
->ArrayObj
!= 0)
1616 /* create vertex array object */
1617 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1618 _mesa_BindVertexArray(clear
->ArrayObj
);
1620 /* create vertex array buffer */
1621 _mesa_GenBuffersARB(1, &clear
->VBO
);
1622 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1624 /* setup vertex arrays */
1625 _mesa_VertexAttribPointerARB(0, 3, GL_FLOAT
, GL_FALSE
, 0, (void *)0);
1626 _mesa_EnableVertexAttribArrayARB(0);
1628 vs
= _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER
);
1629 _mesa_ShaderSourceARB(vs
, 1, &vs_source
, NULL
);
1630 _mesa_CompileShaderARB(vs
);
1632 fs
= _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER
);
1633 _mesa_ShaderSourceARB(fs
, 1, &fs_source
, NULL
);
1634 _mesa_CompileShaderARB(fs
);
1636 clear
->ShaderProg
= _mesa_CreateProgramObjectARB();
1637 _mesa_AttachShader(clear
->ShaderProg
, fs
);
1638 _mesa_AttachShader(clear
->ShaderProg
, vs
);
1639 _mesa_BindAttribLocationARB(clear
->ShaderProg
, 0, "position");
1640 _mesa_LinkProgramARB(clear
->ShaderProg
);
1642 clear
->ColorLocation
= _mesa_GetUniformLocationARB(clear
->ShaderProg
,
1647 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1650 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1652 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1653 GLbitfield metaSave
;
1654 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1655 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1656 const float x0
= ((float)fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
1657 const float y0
= ((float)fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
1658 const float x1
= ((float)fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
1659 const float y1
= ((float)fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
1660 const float z
= -invert_z(ctx
->Depth
.Clear
);
1665 metaSave
= (MESA_META_ALPHA_TEST
|
1667 MESA_META_DEPTH_TEST
|
1668 MESA_META_RASTERIZATION
|
1670 MESA_META_STENCIL_TEST
|
1672 MESA_META_VIEWPORT
|
1674 MESA_META_CLAMP_FRAGMENT_COLOR
);
1676 if (!(buffers
& BUFFER_BITS_COLOR
)) {
1677 /* We'll use colormask to disable color writes. Otherwise,
1678 * respect color mask
1680 metaSave
|= MESA_META_COLOR_MASK
;
1683 _mesa_meta_begin(ctx
, metaSave
);
1685 meta_glsl_clear_init(ctx
, clear
);
1687 _mesa_UseProgramObjectARB(clear
->ShaderProg
);
1688 _mesa_Uniform4fvARB(clear
->ColorLocation
, 1,
1689 ctx
->Color
.ClearColor
.f
);
1691 _mesa_BindVertexArray(clear
->ArrayObj
);
1692 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1694 /* GL_COLOR_BUFFER_BIT */
1695 if (buffers
& BUFFER_BITS_COLOR
) {
1696 /* leave colormask, glDrawBuffer state as-is */
1698 /* Clears never have the color clamped. */
1699 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1702 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1703 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1706 /* GL_DEPTH_BUFFER_BIT */
1707 if (buffers
& BUFFER_BIT_DEPTH
) {
1708 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1709 _mesa_DepthFunc(GL_ALWAYS
);
1710 _mesa_DepthMask(GL_TRUE
);
1713 assert(!ctx
->Depth
.Test
);
1716 /* GL_STENCIL_BUFFER_BIT */
1717 if (buffers
& BUFFER_BIT_STENCIL
) {
1718 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1719 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1720 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1721 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1722 ctx
->Stencil
.Clear
& stencilMax
,
1723 ctx
->Stencil
.WriteMask
[0]);
1726 assert(!ctx
->Stencil
.Enabled
);
1729 /* vertex positions */
1743 /* upload new vertex data */
1744 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1745 GL_DYNAMIC_DRAW_ARB
);
1748 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1750 _mesa_meta_end(ctx
);
1754 * Meta implementation of ctx->Driver.CopyPixels() in terms
1755 * of texture mapping and polygon rendering and GLSL shaders.
1758 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
1759 GLsizei width
, GLsizei height
,
1760 GLint dstX
, GLint dstY
, GLenum type
)
1762 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1763 struct temp_texture
*tex
= get_temp_texture(ctx
);
1765 GLfloat x
, y
, z
, s
, t
;
1767 struct vertex verts
[4];
1769 GLenum intFormat
= GL_RGBA
;
1771 if (type
!= GL_COLOR
||
1772 ctx
->_ImageTransferState
||
1774 width
> tex
->MaxSize
||
1775 height
> tex
->MaxSize
) {
1776 /* XXX avoid this fallback */
1777 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1781 /* Most GL state applies to glCopyPixels, but a there's a few things
1782 * we need to override:
1784 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
1787 MESA_META_TRANSFORM
|
1790 MESA_META_VIEWPORT
));
1792 if (copypix
->ArrayObj
== 0) {
1793 /* one-time setup */
1795 /* create vertex array object */
1796 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
1797 _mesa_BindVertexArray(copypix
->ArrayObj
);
1799 /* create vertex array buffer */
1800 _mesa_GenBuffersARB(1, ©pix
->VBO
);
1801 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1802 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1803 NULL
, GL_DYNAMIC_DRAW_ARB
);
1805 /* setup vertex arrays */
1806 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1807 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1808 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1809 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1812 _mesa_BindVertexArray(copypix
->ArrayObj
);
1813 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1816 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
1818 /* vertex positions, texcoords (after texture allocation!) */
1820 const GLfloat dstX0
= (GLfloat
) dstX
;
1821 const GLfloat dstY0
= (GLfloat
) dstY
;
1822 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1823 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1824 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1834 verts
[1].s
= tex
->Sright
;
1839 verts
[2].s
= tex
->Sright
;
1840 verts
[2].t
= tex
->Ttop
;
1845 verts
[3].t
= tex
->Ttop
;
1847 /* upload new vertex data */
1848 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1851 /* Alloc/setup texture */
1852 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
1853 GL_RGBA
, GL_NEAREST
);
1855 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1857 /* draw textured quad */
1858 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1860 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1862 _mesa_meta_end(ctx
);
1868 * When the glDrawPixels() image size is greater than the max rectangle
1869 * texture size we use this function to break the glDrawPixels() image
1870 * into tiles which fit into the max texture size.
1873 tiled_draw_pixels(struct gl_context
*ctx
,
1875 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1876 GLenum format
, GLenum type
,
1877 const struct gl_pixelstore_attrib
*unpack
,
1878 const GLvoid
*pixels
)
1880 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
1883 if (tileUnpack
.RowLength
== 0)
1884 tileUnpack
.RowLength
= width
;
1886 for (i
= 0; i
< width
; i
+= tileSize
) {
1887 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
1888 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
1890 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
1892 for (j
= 0; j
< height
; j
+= tileSize
) {
1893 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
1894 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
1896 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
1898 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
1899 format
, type
, &tileUnpack
, pixels
);
1906 * One-time init for drawing stencil pixels.
1909 init_draw_stencil_pixels(struct gl_context
*ctx
)
1911 /* This program is run eight times, once for each stencil bit.
1912 * The stencil values to draw are found in an 8-bit alpha texture.
1913 * We read the texture/stencil value and test if bit 'b' is set.
1914 * If the bit is not set, use KIL to kill the fragment.
1915 * Finally, we use the stencil test to update the stencil buffer.
1917 * The basic algorithm for checking if a bit is set is:
1918 * if (is_odd(value / (1 << bit)))
1919 * result is one (or non-zero).
1922 * The program parameter contains three values:
1923 * parm.x = 255 / (1 << bit)
1927 static const char *program
=
1929 "PARAM parm = program.local[0]; \n"
1931 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1932 "# t = t * 255 / bit \n"
1933 "MUL t.x, t.a, parm.x; \n"
1936 "SUB t.x, t.x, t.y; \n"
1938 "MUL t.x, t.x, parm.y; \n"
1939 "# t = fract(t.x) \n"
1940 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
1941 "# t.x = (t.x == 0 ? 1 : 0) \n"
1942 "SGE t.x, -t.x, parm.z; \n"
1944 "# for debug only \n"
1945 "#MOV result.color, t.x; \n"
1947 char program2
[1000];
1948 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1949 struct temp_texture
*tex
= get_temp_texture(ctx
);
1950 const char *texTarget
;
1952 assert(drawpix
->StencilFP
== 0);
1954 /* replace %s with "RECT" or "2D" */
1955 assert(strlen(program
) + 4 < sizeof(program2
));
1956 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1960 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1962 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
1963 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1964 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1965 strlen(program2
), (const GLubyte
*) program2
);
1970 * One-time init for drawing depth pixels.
1973 init_draw_depth_pixels(struct gl_context
*ctx
)
1975 static const char *program
=
1977 "PARAM color = program.local[0]; \n"
1978 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1979 "MOV result.color, color; \n"
1982 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1983 struct temp_texture
*tex
= get_temp_texture(ctx
);
1984 const char *texTarget
;
1986 assert(drawpix
->DepthFP
== 0);
1988 /* replace %s with "RECT" or "2D" */
1989 assert(strlen(program
) + 4 < sizeof(program2
));
1990 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1994 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1996 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
1997 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1998 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1999 strlen(program2
), (const GLubyte
*) program2
);
2004 * Meta implementation of ctx->Driver.DrawPixels() in terms
2005 * of texture mapping and polygon rendering.
2008 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2009 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2010 GLenum format
, GLenum type
,
2011 const struct gl_pixelstore_attrib
*unpack
,
2012 const GLvoid
*pixels
)
2014 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2015 struct temp_texture
*tex
= get_temp_texture(ctx
);
2016 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2017 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2019 GLfloat x
, y
, z
, s
, t
;
2021 struct vertex verts
[4];
2022 GLenum texIntFormat
;
2023 GLboolean fallback
, newTex
;
2024 GLbitfield metaExtraSave
= 0x0;
2028 * Determine if we can do the glDrawPixels with texture mapping.
2030 fallback
= GL_FALSE
;
2031 if (ctx
->_ImageTransferState
||
2036 if (_mesa_is_color_format(format
)) {
2037 /* use more compact format when possible */
2038 /* XXX disable special case for GL_LUMINANCE for now to work around
2039 * apparent i965 driver bug (see bug #23670).
2041 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2042 texIntFormat
= format
;
2044 texIntFormat
= GL_RGBA
;
2046 /* If we're not supposed to clamp the resulting color, then just
2047 * promote our texture to fully float. We could do better by
2048 * just going for the matching set of channels, in floating
2051 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2052 ctx
->Extensions
.ARB_texture_float
)
2053 texIntFormat
= GL_RGBA32F
;
2055 else if (_mesa_is_stencil_format(format
)) {
2056 if (ctx
->Extensions
.ARB_fragment_program
&&
2057 ctx
->Pixel
.IndexShift
== 0 &&
2058 ctx
->Pixel
.IndexOffset
== 0 &&
2059 type
== GL_UNSIGNED_BYTE
) {
2060 /* We'll store stencil as alpha. This only works for GLubyte
2061 * image data because of how incoming values are mapped to alpha
2064 texIntFormat
= GL_ALPHA
;
2065 metaExtraSave
= (MESA_META_COLOR_MASK
|
2066 MESA_META_DEPTH_TEST
|
2068 MESA_META_STENCIL_TEST
);
2074 else if (_mesa_is_depth_format(format
)) {
2075 if (ctx
->Extensions
.ARB_depth_texture
&&
2076 ctx
->Extensions
.ARB_fragment_program
) {
2077 texIntFormat
= GL_DEPTH_COMPONENT
;
2078 metaExtraSave
= (MESA_META_SHADER
);
2089 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2090 format
, type
, unpack
, pixels
);
2095 * Check image size against max texture size, draw as tiles if needed.
2097 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2098 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2099 format
, type
, unpack
, pixels
);
2103 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2104 * but a there's a few things we need to override:
2106 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2109 MESA_META_TRANSFORM
|
2112 MESA_META_VIEWPORT
|
2113 MESA_META_CLAMP_FRAGMENT_COLOR
|
2116 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2118 /* vertex positions, texcoords (after texture allocation!) */
2120 const GLfloat x0
= (GLfloat
) x
;
2121 const GLfloat y0
= (GLfloat
) y
;
2122 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2123 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2124 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2134 verts
[1].s
= tex
->Sright
;
2139 verts
[2].s
= tex
->Sright
;
2140 verts
[2].t
= tex
->Ttop
;
2145 verts
[3].t
= tex
->Ttop
;
2148 if (drawpix
->ArrayObj
== 0) {
2149 /* one-time setup: create vertex array object */
2150 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
2152 _mesa_BindVertexArray(drawpix
->ArrayObj
);
2154 /* create vertex array buffer */
2155 _mesa_GenBuffersARB(1, &vbo
);
2156 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
2157 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2158 verts
, GL_DYNAMIC_DRAW_ARB
);
2160 /* setup vertex arrays */
2161 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2162 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2163 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2164 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2166 /* set given unpack params */
2167 ctx
->Unpack
= *unpack
;
2169 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2171 if (_mesa_is_stencil_format(format
)) {
2172 /* Drawing stencil */
2175 if (!drawpix
->StencilFP
)
2176 init_draw_stencil_pixels(ctx
);
2178 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2179 GL_ALPHA
, type
, pixels
);
2181 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2183 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2185 /* set all stencil bits to 0 */
2186 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2187 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2188 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2190 /* set stencil bits to 1 where needed */
2191 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2193 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2194 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2196 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2197 const GLuint mask
= 1 << bit
;
2198 if (mask
& origStencilMask
) {
2199 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2200 _mesa_StencilMask(mask
);
2202 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2203 255.0 / mask
, 0.5, 0.0, 0.0);
2205 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2209 else if (_mesa_is_depth_format(format
)) {
2211 if (!drawpix
->DepthFP
)
2212 init_draw_depth_pixels(ctx
);
2214 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2215 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2217 /* polygon color = current raster color */
2218 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2219 ctx
->Current
.RasterColor
);
2221 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2222 format
, type
, pixels
);
2224 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2228 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2229 format
, type
, pixels
);
2230 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2233 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2235 _mesa_DeleteBuffersARB(1, &vbo
);
2237 /* restore unpack params */
2238 ctx
->Unpack
= unpackSave
;
2240 _mesa_meta_end(ctx
);
2244 alpha_test_raster_color(struct gl_context
*ctx
)
2246 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2247 GLfloat ref
= ctx
->Color
.AlphaRef
;
2249 switch (ctx
->Color
.AlphaFunc
) {
2255 return alpha
== ref
;
2257 return alpha
<= ref
;
2261 return alpha
!= ref
;
2263 return alpha
>= ref
;
2273 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2274 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2275 * tracker would improve performance a lot.
2278 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2279 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2280 const struct gl_pixelstore_attrib
*unpack
,
2281 const GLubyte
*bitmap1
)
2283 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2284 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2285 const GLenum texIntFormat
= GL_ALPHA
;
2286 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2289 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2291 struct vertex verts
[4];
2296 * Check if swrast fallback is needed.
2298 if (ctx
->_ImageTransferState
||
2299 ctx
->FragmentProgram
._Enabled
||
2301 ctx
->Texture
._EnabledUnits
||
2302 width
> tex
->MaxSize
||
2303 height
> tex
->MaxSize
) {
2304 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2308 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2311 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2312 * but a there's a few things we need to override:
2314 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
2315 MESA_META_PIXEL_STORE
|
2316 MESA_META_RASTERIZATION
|
2319 MESA_META_TRANSFORM
|
2322 MESA_META_VIEWPORT
));
2324 if (bitmap
->ArrayObj
== 0) {
2325 /* one-time setup */
2327 /* create vertex array object */
2328 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2329 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2331 /* create vertex array buffer */
2332 _mesa_GenBuffersARB(1, &bitmap
->VBO
);
2333 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2334 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2335 NULL
, GL_DYNAMIC_DRAW_ARB
);
2337 /* setup vertex arrays */
2338 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2339 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2340 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2341 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2342 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2343 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2346 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2347 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2350 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2352 /* vertex positions, texcoords, colors (after texture allocation!) */
2354 const GLfloat x0
= (GLfloat
) x
;
2355 const GLfloat y0
= (GLfloat
) y
;
2356 const GLfloat x1
= (GLfloat
) (x
+ width
);
2357 const GLfloat y1
= (GLfloat
) (y
+ height
);
2358 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2369 verts
[1].s
= tex
->Sright
;
2374 verts
[2].s
= tex
->Sright
;
2375 verts
[2].t
= tex
->Ttop
;
2380 verts
[3].t
= tex
->Ttop
;
2382 for (i
= 0; i
< 4; i
++) {
2383 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2384 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2385 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2386 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2389 /* upload new vertex data */
2390 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2393 /* choose different foreground/background alpha values */
2394 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2395 bg
= (fg
> 127 ? 0 : 255);
2397 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2399 _mesa_meta_end(ctx
);
2403 bitmap8
= (GLubyte
*) malloc(width
* height
);
2405 memset(bitmap8
, bg
, width
* height
);
2406 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2407 bitmap8
, width
, fg
);
2409 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2411 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2412 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2414 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2415 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2417 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2419 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2424 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2426 _mesa_meta_end(ctx
);
2431 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2432 * software fallback. The fallback path will require that the texture
2433 * images are mapped.
2434 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2437 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
2438 struct gl_texture_object
*texObj
)
2440 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2441 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2442 struct gl_texture_image
*baseImage
;
2446 /* check for fallbacks */
2447 if (!ctx
->Extensions
.EXT_framebuffer_object
||
2448 target
== GL_TEXTURE_3D
||
2449 target
== GL_TEXTURE_1D_ARRAY
||
2450 target
== GL_TEXTURE_2D_ARRAY
) {
2454 srcLevel
= texObj
->BaseLevel
;
2455 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2456 if (!baseImage
|| _mesa_is_format_compressed(baseImage
->TexFormat
)) {
2460 if (_mesa_get_format_color_encoding(baseImage
->TexFormat
) == GL_SRGB
&&
2461 !ctx
->Extensions
.EXT_texture_sRGB_decode
) {
2462 /* The texture format is sRGB but we can't turn off sRGB->linear
2463 * texture sample conversion. So we won't be able to generate the
2464 * right colors when rendering. Need to use a fallback.
2470 * Test that we can actually render in the texture's format.
2473 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2474 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2476 if (target
== GL_TEXTURE_1D
) {
2477 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2478 GL_COLOR_ATTACHMENT0_EXT
,
2479 target
, texObj
->Name
, srcLevel
);
2482 /* other work is needed to enable 3D mipmap generation */
2483 else if (target
== GL_TEXTURE_3D
) {
2485 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2486 GL_COLOR_ATTACHMENT0_EXT
,
2487 target
, texObj
->Name
, srcLevel
, zoffset
);
2492 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2493 GL_COLOR_ATTACHMENT0_EXT
,
2494 target
, texObj
->Name
, srcLevel
);
2497 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2499 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2501 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2510 * Compute the texture coordinates for the four vertices of a quad for
2511 * drawing a 2D texture image or slice of a cube/3D texture.
2512 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2513 * \param slice slice of a 1D/2D array texture or 3D texture
2514 * \param width width of the texture image
2515 * \param height height of the texture image
2516 * \param coords0/1/2/3 returns the computed texcoords
2519 setup_texture_coords(GLenum faceTarget
,
2528 static const GLfloat st
[4][2] = {
2529 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2534 switch (faceTarget
) {
2538 case GL_TEXTURE_2D_ARRAY
:
2539 if (faceTarget
== GL_TEXTURE_3D
)
2541 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
2545 coords0
[0] = 0.0F
; /* s */
2546 coords0
[1] = 0.0F
; /* t */
2547 coords0
[2] = r
; /* r */
2558 case GL_TEXTURE_RECTANGLE_ARB
:
2559 coords0
[0] = 0.0F
; /* s */
2560 coords0
[1] = 0.0F
; /* t */
2561 coords0
[2] = 0.0F
; /* r */
2566 coords2
[1] = height
;
2569 coords3
[1] = height
;
2572 case GL_TEXTURE_1D_ARRAY
:
2573 coords0
[0] = 0.0F
; /* s */
2574 coords0
[1] = slice
; /* t */
2575 coords0
[2] = 0.0F
; /* r */
2587 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2588 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2589 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2590 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2591 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2592 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2593 /* loop over quad verts */
2594 for (i
= 0; i
< 4; i
++) {
2595 /* Compute sc = +/-scale and tc = +/-scale.
2596 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2597 * though that can still sometimes happen with this scale factor...
2599 const GLfloat scale
= 0.9999f
;
2600 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2601 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2621 switch (faceTarget
) {
2622 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2627 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2632 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2637 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2642 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2647 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2658 assert(0 && "unexpected target in meta setup_texture_coords()");
2664 * Called via ctx->Driver.GenerateMipmap()
2665 * Note: We don't yet support 3D textures, 1D/2D array textures or texture
2669 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
2670 struct gl_texture_object
*texObj
)
2672 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2674 GLfloat x
, y
, tex
[3];
2676 struct vertex verts
[4];
2677 const GLuint baseLevel
= texObj
->BaseLevel
;
2678 const GLuint maxLevel
= texObj
->MaxLevel
;
2679 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
2680 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
2681 const GLint maxLevelSave
= texObj
->MaxLevel
;
2682 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
2683 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
2684 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
2685 const GLenum wrapRSave
= texObj
->Sampler
.WrapR
;
2686 const GLenum srgbDecodeSave
= texObj
->Sampler
.sRGBDecode
;
2687 const GLenum srgbBufferSave
= ctx
->Color
.sRGBEnabled
;
2688 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2689 const GLuint original_active_unit
= ctx
->Texture
.CurrentUnit
;
2692 const GLuint border
= 0;
2693 const GLint slice
= 0;
2695 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
2696 _mesa_generate_mipmap(ctx
, target
, texObj
);
2700 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
2701 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
2702 faceTarget
= target
;
2703 target
= GL_TEXTURE_CUBE_MAP
;
2706 faceTarget
= target
;
2709 _mesa_meta_begin(ctx
, MESA_META_ALL
);
2711 if (original_active_unit
!= 0)
2712 _mesa_BindTexture(target
, texObj
->Name
);
2714 if (mipmap
->ArrayObj
== 0) {
2715 /* one-time setup */
2717 /* create vertex array object */
2718 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
2719 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
2721 /* create vertex array buffer */
2722 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
2723 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2724 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2725 NULL
, GL_DYNAMIC_DRAW_ARB
);
2727 /* setup vertex arrays */
2728 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2729 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
2730 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2731 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2734 _mesa_BindVertexArray(mipmap
->ArrayObj
);
2735 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2739 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2741 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2743 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
2744 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
2745 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
2746 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2747 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2748 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
2750 /* We don't want to encode or decode sRGB values; treat them as linear */
2751 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
2752 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
2753 GL_SKIP_DECODE_EXT
);
2755 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
2756 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
2759 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2761 /* setup texcoords (XXX what about border?) */
2762 setup_texture_coords(faceTarget
,
2763 0.0, 0.0, /* width, height never used here */
2770 /* setup vertex positions */
2780 /* upload new vertex data */
2781 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2783 /* setup projection matrix */
2784 _mesa_MatrixMode(GL_PROJECTION
);
2785 _mesa_LoadIdentity();
2786 _mesa_Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
2788 /* texture is already locked, unlock now */
2789 _mesa_unlock_texture(ctx
, texObj
);
2791 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
2792 const struct gl_texture_image
*srcImage
;
2793 const GLuint srcLevel
= dstLevel
- 1;
2794 GLsizei srcWidth
, srcHeight
, srcDepth
;
2795 GLsizei dstWidth
, dstHeight
, dstDepth
;
2798 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
2799 assert(srcImage
->Border
== 0); /* XXX we can fix this */
2801 /* src size w/out border */
2802 srcWidth
= srcImage
->Width
- 2 * border
;
2803 srcHeight
= srcImage
->Height
- 2 * border
;
2804 srcDepth
= srcImage
->Depth
- 2 * border
;
2806 /* new dst size w/ border */
2807 dstWidth
= MAX2(1, srcWidth
/ 2) + 2 * border
;
2808 dstHeight
= MAX2(1, srcHeight
/ 2) + 2 * border
;
2809 dstDepth
= MAX2(1, srcDepth
/ 2) + 2 * border
;
2811 if (dstWidth
== srcImage
->Width
&&
2812 dstHeight
== srcImage
->Height
&&
2813 dstDepth
== srcImage
->Depth
) {
2818 /* Set MaxLevel large enough to hold the new level when we allocate it */
2819 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
2821 /* Create empty dest image */
2822 if (target
== GL_TEXTURE_1D
) {
2823 _mesa_TexImage1D(target
, dstLevel
, srcImage
->InternalFormat
,
2825 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2827 else if (target
== GL_TEXTURE_3D
) {
2828 _mesa_TexImage3D(target
, dstLevel
, srcImage
->InternalFormat
,
2829 dstWidth
, dstHeight
, dstDepth
, border
,
2830 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2834 _mesa_TexImage2D(faceTarget
, dstLevel
, srcImage
->InternalFormat
,
2835 dstWidth
, dstHeight
, border
,
2836 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2838 if (target
== GL_TEXTURE_CUBE_MAP
) {
2839 /* If texturing from a cube, we need to make sure all src faces
2840 * have been defined (even if we're not sampling from them.)
2841 * Otherwise the texture object will be 'incomplete' and
2842 * texturing from it will not be allowed.
2845 for (face
= 0; face
< 6; face
++) {
2846 if (!texObj
->Image
[face
][srcLevel
] ||
2847 texObj
->Image
[face
][srcLevel
]->Width
!= srcWidth
) {
2848 _mesa_TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ face
,
2849 srcLevel
, srcImage
->InternalFormat
,
2850 srcWidth
, srcHeight
, border
,
2851 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2857 /* limit minification to src level */
2858 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
2860 /* Set to draw into the current dstLevel */
2861 if (target
== GL_TEXTURE_1D
) {
2862 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2863 GL_COLOR_ATTACHMENT0_EXT
,
2868 else if (target
== GL_TEXTURE_3D
) {
2869 GLint zoffset
= 0; /* XXX unfinished */
2870 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2871 GL_COLOR_ATTACHMENT0_EXT
,
2878 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2879 GL_COLOR_ATTACHMENT0_EXT
,
2885 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
2888 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2889 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2894 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
2895 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
2897 /* setup viewport */
2898 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
2900 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2903 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
2904 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
2907 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&& srgbBufferSave
) {
2908 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_TRUE
);
2911 _mesa_lock_texture(ctx
, texObj
); /* relock */
2913 _mesa_meta_end(ctx
);
2915 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
2916 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
2917 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
2918 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
2919 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
2920 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
2921 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, wrapRSave
);
2923 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2928 * Determine the GL data type to use for the temporary image read with
2929 * ReadPixels() and passed to Tex[Sub]Image().
2932 get_temp_image_type(struct gl_context
*ctx
, GLenum baseFormat
)
2934 switch (baseFormat
) {
2939 case GL_LUMINANCE_ALPHA
:
2941 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2942 return GL_UNSIGNED_BYTE
;
2943 else if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2944 return GL_UNSIGNED_SHORT
;
2947 case GL_DEPTH_COMPONENT
:
2948 return GL_UNSIGNED_INT
;
2949 case GL_DEPTH_STENCIL
:
2950 return GL_UNSIGNED_INT_24_8
;
2952 _mesa_problem(ctx
, "Unexpected format in get_temp_image_type()");
2959 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2960 * Have to be careful with locking and meta state for pixel transfer.
2963 copy_tex_sub_image(struct gl_context
*ctx
,
2964 GLuint dims
, GLenum target
, GLint level
,
2965 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2967 GLsizei width
, GLsizei height
)
2969 struct gl_texture_object
*texObj
;
2970 struct gl_texture_image
*texImage
;
2971 GLenum format
, type
;
2975 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2976 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2978 /* Choose format/type for temporary image buffer */
2979 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
2980 if (format
== GL_LUMINANCE
||
2981 format
== GL_LUMINANCE_ALPHA
||
2982 format
== GL_INTENSITY
) {
2983 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
2984 * temp image buffer because glReadPixels will do L=R+G+B which is
2985 * not what we want (should be L=R).
2990 type
= get_temp_image_type(ctx
, format
);
2991 bpp
= _mesa_bytes_per_pixel(format
, type
);
2993 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_sub_image()");
2998 * Alloc image buffer (XXX could use a PBO)
3000 buf
= malloc(width
* height
* bpp
);
3002 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
3006 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
3009 * Read image from framebuffer (disable pixel transfer ops)
3011 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
3012 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
3013 format
, type
, &ctx
->Pack
, buf
);
3014 _mesa_meta_end(ctx
);
3016 _mesa_update_state(ctx
); /* to update pixel transfer state */
3019 * Store texture data (with pixel transfer ops)
3021 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
3022 if (target
== GL_TEXTURE_1D
) {
3023 ctx
->Driver
.TexSubImage1D(ctx
, target
, level
, xoffset
,
3024 width
, format
, type
, buf
,
3025 &ctx
->Unpack
, texObj
, texImage
);
3027 else if (target
== GL_TEXTURE_3D
) {
3028 ctx
->Driver
.TexSubImage3D(ctx
, target
, level
, xoffset
, yoffset
, zoffset
,
3029 width
, height
, 1, format
, type
, buf
,
3030 &ctx
->Unpack
, texObj
, texImage
);
3033 ctx
->Driver
.TexSubImage2D(ctx
, target
, level
, xoffset
, yoffset
,
3034 width
, height
, format
, type
, buf
,
3035 &ctx
->Unpack
, texObj
, texImage
);
3037 _mesa_meta_end(ctx
);
3039 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
3046 _mesa_meta_CopyTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
3048 GLint x
, GLint y
, GLsizei width
)
3050 copy_tex_sub_image(ctx
, 1, target
, level
, xoffset
, 0, 0,
3056 _mesa_meta_CopyTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
3057 GLint xoffset
, GLint yoffset
,
3059 GLsizei width
, GLsizei height
)
3061 copy_tex_sub_image(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
3062 x
, y
, width
, height
);
3067 _mesa_meta_CopyTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
3068 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3070 GLsizei width
, GLsizei height
)
3072 copy_tex_sub_image(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
3073 x
, y
, width
, height
);
3078 * Decompress a texture image by drawing a quad with the compressed
3079 * texture and reading the pixels out of the color buffer.
3080 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
3081 * \param destFormat format, ala glReadPixels
3082 * \param destType type, ala glReadPixels
3083 * \param dest destination buffer
3084 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
3087 decompress_texture_image(struct gl_context
*ctx
,
3088 struct gl_texture_image
*texImage
,
3090 GLenum destFormat
, GLenum destType
,
3091 GLvoid
*dest
, GLint destRowLength
)
3093 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
3094 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3095 const GLint width
= texImage
->Width
;
3096 const GLint height
= texImage
->Height
;
3097 const GLenum target
= texObj
->Target
;
3100 GLfloat x
, y
, tex
[3];
3102 struct vertex verts
[4];
3103 GLuint fboDrawSave
, fboReadSave
;
3106 assert(target
== GL_TEXTURE_3D
||
3107 target
== GL_TEXTURE_2D_ARRAY
);
3110 if (target
== GL_TEXTURE_CUBE_MAP
) {
3111 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
3114 faceTarget
= target
;
3117 /* save fbo bindings (not saved by _mesa_meta_begin()) */
3118 fboDrawSave
= ctx
->DrawBuffer
->Name
;
3119 fboReadSave
= ctx
->ReadBuffer
->Name
;
3121 _mesa_meta_begin(ctx
, MESA_META_ALL
);
3123 /* Create/bind FBO/renderbuffer */
3124 if (decompress
->FBO
== 0) {
3125 _mesa_GenFramebuffersEXT(1, &decompress
->FBO
);
3126 _mesa_GenRenderbuffersEXT(1, &decompress
->RBO
);
3127 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3128 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3129 _mesa_FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT
,
3130 GL_COLOR_ATTACHMENT0_EXT
,
3131 GL_RENDERBUFFER_EXT
,
3135 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3138 /* alloc dest surface */
3139 if (width
!= decompress
->Width
|| height
!= decompress
->Height
) {
3140 _mesa_RenderbufferStorageEXT(GL_RENDERBUFFER_EXT
, GL_RGBA
,
3142 decompress
->Width
= width
;
3143 decompress
->Height
= height
;
3146 /* setup VBO data */
3147 if (decompress
->ArrayObj
== 0) {
3148 /* create vertex array object */
3149 _mesa_GenVertexArrays(1, &decompress
->ArrayObj
);
3150 _mesa_BindVertexArray(decompress
->ArrayObj
);
3152 /* create vertex array buffer */
3153 _mesa_GenBuffersARB(1, &decompress
->VBO
);
3154 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3155 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3156 NULL
, GL_DYNAMIC_DRAW_ARB
);
3158 /* setup vertex arrays */
3159 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3160 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
3161 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3162 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3165 _mesa_BindVertexArray(decompress
->ArrayObj
);
3166 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3169 setup_texture_coords(faceTarget
, slice
, width
, height
,
3175 /* setup vertex positions */
3181 verts
[2].y
= height
;
3183 verts
[3].y
= height
;
3185 /* upload new vertex data */
3186 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3188 /* setup texture state */
3189 _mesa_BindTexture(target
, texObj
->Name
);
3190 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3193 /* save texture object state */
3194 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
3195 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
3196 const GLint baseLevelSave
= texObj
->BaseLevel
;
3197 const GLint maxLevelSave
= texObj
->MaxLevel
;
3198 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
3199 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
3200 const GLenum srgbSave
= texObj
->Sampler
.sRGBDecode
;
3202 /* restrict sampling to the texture level of interest */
3203 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, texImage
->Level
);
3204 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texImage
->Level
);
3205 /* nearest filtering */
3206 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
3207 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
3209 /* No sRGB decode or encode.*/
3210 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3211 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
3212 GL_SKIP_DECODE_EXT
);
3214 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
3215 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
3218 /* render quad w/ texture into renderbuffer */
3219 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3221 /* Restore texture object state, the texture binding will
3222 * be restored by _mesa_meta_end().
3224 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
3225 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
3226 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3227 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
3228 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3230 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
3231 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
3232 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3233 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
, srgbSave
);
3237 /* read pixels from renderbuffer */
3238 ctx
->Pack
.RowLength
= destRowLength
;
3239 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
3241 /* disable texture unit */
3242 _mesa_set_enable(ctx
, target
, GL_FALSE
);
3244 _mesa_meta_end(ctx
);
3246 /* restore fbo bindings */
3247 if (fboDrawSave
== fboReadSave
) {
3248 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboDrawSave
);
3251 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT
, fboDrawSave
);
3252 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT
, fboReadSave
);
3258 * This is just a wrapper around _mesa_get_tex_image() and
3259 * decompress_texture_image(). Meta functions should not be directly called
3263 _mesa_meta_GetTexImage(struct gl_context
*ctx
,
3264 GLenum format
, GLenum type
, GLvoid
*pixels
,
3265 struct gl_texture_image
*texImage
)
3267 /* We can only use the decompress-with-blit method here if the texels are
3268 * unsigned, normalized values. We could handle signed and unnormalized
3269 * with floating point renderbuffers...
3271 if (_mesa_is_format_compressed(texImage
->TexFormat
) &&
3272 _mesa_get_format_datatype(texImage
->TexFormat
)
3273 == GL_UNSIGNED_NORMALIZED
) {
3274 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3275 const GLuint slice
= 0; /* only 2D compressed textures for now */
3276 /* Need to unlock the texture here to prevent deadlock... */
3277 _mesa_unlock_texture(ctx
, texObj
);
3278 decompress_texture_image(ctx
, texImage
, slice
, format
, type
, pixels
,
3279 ctx
->Pack
.RowLength
);
3280 /* ... and relock it */
3281 _mesa_lock_texture(ctx
, texObj
);
3284 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);