2 * Mesa 3-D graphics library
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * Meta operations. Some GL operations can be expressed in terms of
27 * other GL operations. For example, glBlitFramebuffer() can be done
28 * with texture mapping and glClear() can be done with polygon rendering.
34 #include "main/glheader.h"
35 #include "main/mtypes.h"
36 #include "main/imports.h"
37 #include "main/arbprogram.h"
38 #include "main/arrayobj.h"
39 #include "main/blend.h"
40 #include "main/bufferobj.h"
41 #include "main/buffers.h"
42 #include "main/colortab.h"
43 #include "main/condrender.h"
44 #include "main/depth.h"
45 #include "main/enable.h"
46 #include "main/fbobject.h"
47 #include "main/feedback.h"
48 #include "main/formats.h"
49 #include "main/image.h"
50 #include "main/macros.h"
51 #include "main/matrix.h"
52 #include "main/mipmap.h"
53 #include "main/pixel.h"
55 #include "main/polygon.h"
56 #include "main/readpix.h"
57 #include "main/scissor.h"
58 #include "main/shaderapi.h"
59 #include "main/shaderobj.h"
60 #include "main/state.h"
61 #include "main/stencil.h"
62 #include "main/texobj.h"
63 #include "main/texenv.h"
64 #include "main/texgetimage.h"
65 #include "main/teximage.h"
66 #include "main/texparam.h"
67 #include "main/texstate.h"
68 #include "main/uniforms.h"
69 #include "main/varray.h"
70 #include "main/viewport.h"
71 #include "program/program.h"
72 #include "swrast/swrast.h"
73 #include "drivers/common/meta.h"
76 /** Return offset in bytes of the field within a vertex struct */
77 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
80 * State which we may save/restore across meta ops.
81 * XXX this may be incomplete...
85 GLbitfield SavedState
; /**< bitmask of MESA_META_* flags */
87 /** MESA_META_ALPHA_TEST */
88 GLboolean AlphaEnabled
;
92 /** MESA_META_BLEND */
93 GLbitfield BlendEnabled
;
94 GLboolean ColorLogicOpEnabled
;
96 /** MESA_META_COLOR_MASK */
97 GLubyte ColorMask
[MAX_DRAW_BUFFERS
][4];
99 /** MESA_META_DEPTH_TEST */
100 struct gl_depthbuffer_attrib Depth
;
105 /** MESA_META_PIXEL_STORE */
106 struct gl_pixelstore_attrib Pack
, Unpack
;
108 /** MESA_META_PIXEL_TRANSFER */
109 GLfloat RedBias
, RedScale
;
110 GLfloat GreenBias
, GreenScale
;
111 GLfloat BlueBias
, BlueScale
;
112 GLfloat AlphaBias
, AlphaScale
;
113 GLfloat DepthBias
, DepthScale
;
114 GLboolean MapColorFlag
;
116 /** MESA_META_RASTERIZATION */
117 GLenum FrontPolygonMode
, BackPolygonMode
;
118 GLboolean PolygonOffset
;
119 GLboolean PolygonSmooth
;
120 GLboolean PolygonStipple
;
121 GLboolean PolygonCull
;
123 /** MESA_META_SCISSOR */
124 struct gl_scissor_attrib Scissor
;
126 /** MESA_META_SHADER */
127 GLboolean VertexProgramEnabled
;
128 struct gl_vertex_program
*VertexProgram
;
129 GLboolean FragmentProgramEnabled
;
130 struct gl_fragment_program
*FragmentProgram
;
131 struct gl_shader_program
*VertexShader
;
132 struct gl_shader_program
*GeometryShader
;
133 struct gl_shader_program
*FragmentShader
;
134 struct gl_shader_program
*ActiveShader
;
136 /** MESA_META_STENCIL_TEST */
137 struct gl_stencil_attrib Stencil
;
139 /** MESA_META_TRANSFORM */
141 GLfloat ModelviewMatrix
[16];
142 GLfloat ProjectionMatrix
[16];
143 GLfloat TextureMatrix
[16];
145 /** MESA_META_CLIP */
146 GLbitfield ClipPlanesEnabled
;
148 /** MESA_META_TEXTURE */
150 GLuint ClientActiveUnit
;
151 /** for unit[0] only */
152 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
153 /** mask of TEXTURE_2D_BIT, etc */
154 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
155 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
156 GLuint EnvMode
; /* unit[0] only */
158 /** MESA_META_VERTEX */
159 struct gl_array_object
*ArrayObj
;
160 struct gl_buffer_object
*ArrayBufferObj
;
162 /** MESA_META_VIEWPORT */
163 GLint ViewportX
, ViewportY
, ViewportW
, ViewportH
;
164 GLclampd DepthNear
, DepthFar
;
166 /** MESA_META_CLAMP_FRAGMENT_COLOR */
167 GLenum ClampFragmentColor
;
169 /** MESA_META_CLAMP_VERTEX_COLOR */
170 GLenum ClampVertexColor
;
172 /** MESA_META_CONDITIONAL_RENDER */
173 struct gl_query_object
*CondRenderQuery
;
174 GLenum CondRenderMode
;
176 /** MESA_META_SELECT_FEEDBACK */
178 struct gl_selection Select
;
179 struct gl_feedback Feedback
;
181 /** Miscellaneous (always disabled) */
186 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
187 * This is currently shared by all the meta ops. But we could create a
188 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
193 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
194 GLsizei MinSize
; /**< Min texture size to allocate */
195 GLsizei MaxSize
; /**< Max possible texture size */
196 GLboolean NPOT
; /**< Non-power of two size OK? */
197 GLsizei Width
, Height
; /**< Current texture size */
199 GLfloat Sright
, Ttop
; /**< right, top texcoords */
204 * State for glBlitFramebufer()
215 * State for glClear()
224 GLuint IntegerShaderProg
;
225 GLint IntegerColorLocation
;
230 * State for glCopyPixels()
240 * State for glDrawPixels()
246 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
247 GLuint DepthFP
; /**< Fragment program for drawing depth images */
252 * State for glBitmap()
258 struct temp_texture Tex
; /**< separate texture from other meta ops */
263 * State for _mesa_meta_generate_mipmap()
265 struct gen_mipmap_state
274 * State for texture decompression
276 struct decompress_state
279 GLuint VBO
, FBO
, RBO
;
284 * State for glDrawTex()
292 #define MAX_META_OPS_DEPTH 8
294 * All per-context meta state.
298 /** Stack of state saved during meta-ops */
299 struct save_state Save
[MAX_META_OPS_DEPTH
];
300 /** Save stack depth */
301 GLuint SaveStackDepth
;
303 struct temp_texture TempTex
;
305 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
306 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
307 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
308 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
309 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
310 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
311 struct decompress_state Decompress
; /**< For texture decompression */
312 struct drawtex_state DrawTex
; /**< For _mesa_meta_DrawTex() */
316 compile_shader_with_debug(struct gl_context
*ctx
, GLenum target
, const GLcharARB
*source
)
322 shader
= _mesa_CreateShaderObjectARB(target
);
323 _mesa_ShaderSourceARB(shader
, 1, &source
, NULL
);
324 _mesa_CompileShaderARB(shader
);
326 _mesa_GetShaderiv(shader
, GL_COMPILE_STATUS
, &ok
);
330 _mesa_GetShaderiv(shader
, GL_INFO_LOG_LENGTH
, &size
);
338 _mesa_GetProgramInfoLog(shader
, size
, NULL
, info
);
340 "meta program compile failed:\n%s\n"
350 link_program_with_debug(struct gl_context
*ctx
, GLuint program
)
355 _mesa_LinkProgramARB(program
);
357 _mesa_GetProgramiv(program
, GL_LINK_STATUS
, &ok
);
361 _mesa_GetProgramiv(program
, GL_INFO_LOG_LENGTH
, &size
);
369 _mesa_GetProgramInfoLog(program
, size
, NULL
, info
);
370 _mesa_problem(ctx
, "meta program link failed:\n%s", info
);
378 * Initialize meta-ops for a context.
379 * To be called once during context creation.
382 _mesa_meta_init(struct gl_context
*ctx
)
386 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
391 * Free context meta-op state.
392 * To be called once during context destruction.
395 _mesa_meta_free(struct gl_context
*ctx
)
397 /* Note: Any textures, VBOs, etc, that we allocate should get
398 * freed by the normal context destruction code. But this would be
399 * the place to free other meta data someday.
407 * Enter meta state. This is like a light-weight version of glPushAttrib
408 * but it also resets most GL state back to default values.
410 * \param state bitmask of MESA_META_* flags indicating which attribute groups
411 * to save and reset to their defaults
414 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
416 struct save_state
*save
;
418 /* hope MAX_META_OPS_DEPTH is large enough */
419 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
421 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
422 memset(save
, 0, sizeof(*save
));
423 save
->SavedState
= state
;
425 if (state
& MESA_META_ALPHA_TEST
) {
426 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
427 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
428 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
429 if (ctx
->Color
.AlphaEnabled
)
430 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
433 if (state
& MESA_META_BLEND
) {
434 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
435 if (ctx
->Color
.BlendEnabled
) {
436 if (ctx
->Extensions
.EXT_draw_buffers2
) {
438 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
439 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
443 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
446 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
447 if (ctx
->Color
.ColorLogicOpEnabled
)
448 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
451 if (state
& MESA_META_COLOR_MASK
) {
452 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
453 sizeof(ctx
->Color
.ColorMask
));
454 if (!ctx
->Color
.ColorMask
[0][0] ||
455 !ctx
->Color
.ColorMask
[0][1] ||
456 !ctx
->Color
.ColorMask
[0][2] ||
457 !ctx
->Color
.ColorMask
[0][3])
458 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
461 if (state
& MESA_META_DEPTH_TEST
) {
462 save
->Depth
= ctx
->Depth
; /* struct copy */
464 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
467 if (state
& MESA_META_FOG
) {
468 save
->Fog
= ctx
->Fog
.Enabled
;
469 if (ctx
->Fog
.Enabled
)
470 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
473 if (state
& MESA_META_PIXEL_STORE
) {
474 save
->Pack
= ctx
->Pack
;
475 save
->Unpack
= ctx
->Unpack
;
476 ctx
->Pack
= ctx
->DefaultPacking
;
477 ctx
->Unpack
= ctx
->DefaultPacking
;
480 if (state
& MESA_META_PIXEL_TRANSFER
) {
481 save
->RedScale
= ctx
->Pixel
.RedScale
;
482 save
->RedBias
= ctx
->Pixel
.RedBias
;
483 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
484 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
485 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
486 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
487 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
488 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
489 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
490 ctx
->Pixel
.RedScale
= 1.0F
;
491 ctx
->Pixel
.RedBias
= 0.0F
;
492 ctx
->Pixel
.GreenScale
= 1.0F
;
493 ctx
->Pixel
.GreenBias
= 0.0F
;
494 ctx
->Pixel
.BlueScale
= 1.0F
;
495 ctx
->Pixel
.BlueBias
= 0.0F
;
496 ctx
->Pixel
.AlphaScale
= 1.0F
;
497 ctx
->Pixel
.AlphaBias
= 0.0F
;
498 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
500 ctx
->NewState
|=_NEW_PIXEL
;
503 if (state
& MESA_META_RASTERIZATION
) {
504 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
505 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
506 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
507 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
508 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
509 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
510 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
511 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
512 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
513 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
514 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
517 if (state
& MESA_META_SCISSOR
) {
518 save
->Scissor
= ctx
->Scissor
; /* struct copy */
519 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
522 if (state
& MESA_META_SHADER
) {
523 if (ctx
->Extensions
.ARB_vertex_program
) {
524 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
525 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
526 ctx
->VertexProgram
.Current
);
527 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
530 if (ctx
->Extensions
.ARB_fragment_program
) {
531 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
532 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
533 ctx
->FragmentProgram
.Current
);
534 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
537 if (ctx
->Extensions
.ARB_shader_objects
) {
538 _mesa_reference_shader_program(ctx
, &save
->VertexShader
,
539 ctx
->Shader
.CurrentVertexProgram
);
540 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
,
541 ctx
->Shader
.CurrentGeometryProgram
);
542 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
,
543 ctx
->Shader
.CurrentFragmentProgram
);
544 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
545 ctx
->Shader
.ActiveProgram
);
547 _mesa_UseProgramObjectARB(0);
551 if (state
& MESA_META_STENCIL_TEST
) {
552 save
->Stencil
= ctx
->Stencil
; /* struct copy */
553 if (ctx
->Stencil
.Enabled
)
554 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
555 /* NOTE: other stencil state not reset */
558 if (state
& MESA_META_TEXTURE
) {
561 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
562 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
563 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
565 /* Disable all texture units */
566 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
567 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
568 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
569 if (ctx
->Texture
.Unit
[u
].Enabled
||
570 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
571 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
572 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
573 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
574 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
575 if (ctx
->Extensions
.ARB_texture_cube_map
)
576 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
577 if (ctx
->Extensions
.NV_texture_rectangle
)
578 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
579 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
580 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
581 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
582 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
586 /* save current texture objects for unit[0] only */
587 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
588 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
589 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
592 /* set defaults for unit[0] */
593 _mesa_ActiveTextureARB(GL_TEXTURE0
);
594 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
595 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
598 if (state
& MESA_META_TRANSFORM
) {
599 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
600 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
601 16 * sizeof(GLfloat
));
602 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
603 16 * sizeof(GLfloat
));
604 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
605 16 * sizeof(GLfloat
));
606 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
607 /* set 1:1 vertex:pixel coordinate transform */
608 _mesa_ActiveTextureARB(GL_TEXTURE0
);
609 _mesa_MatrixMode(GL_TEXTURE
);
610 _mesa_LoadIdentity();
611 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
612 _mesa_MatrixMode(GL_MODELVIEW
);
613 _mesa_LoadIdentity();
614 _mesa_MatrixMode(GL_PROJECTION
);
615 _mesa_LoadIdentity();
616 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
617 0.0, ctx
->DrawBuffer
->Height
,
621 if (state
& MESA_META_CLIP
) {
622 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
623 if (ctx
->Transform
.ClipPlanesEnabled
) {
625 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
626 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
631 if (state
& MESA_META_VERTEX
) {
632 /* save vertex array object state */
633 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
634 ctx
->Array
.ArrayObj
);
635 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
636 ctx
->Array
.ArrayBufferObj
);
637 /* set some default state? */
640 if (state
& MESA_META_VIEWPORT
) {
641 /* save viewport state */
642 save
->ViewportX
= ctx
->Viewport
.X
;
643 save
->ViewportY
= ctx
->Viewport
.Y
;
644 save
->ViewportW
= ctx
->Viewport
.Width
;
645 save
->ViewportH
= ctx
->Viewport
.Height
;
646 /* set viewport to match window size */
647 if (ctx
->Viewport
.X
!= 0 ||
648 ctx
->Viewport
.Y
!= 0 ||
649 ctx
->Viewport
.Width
!= ctx
->DrawBuffer
->Width
||
650 ctx
->Viewport
.Height
!= ctx
->DrawBuffer
->Height
) {
651 _mesa_set_viewport(ctx
, 0, 0,
652 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
654 /* save depth range state */
655 save
->DepthNear
= ctx
->Viewport
.Near
;
656 save
->DepthFar
= ctx
->Viewport
.Far
;
657 /* set depth range to default */
658 _mesa_DepthRange(0.0, 1.0);
661 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
662 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
664 /* Generally in here we want to do clamping according to whether
665 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
666 * regardless of the internal implementation of the metaops.
668 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
)
669 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
672 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
673 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
675 /* Generally in here we never want vertex color clamping --
676 * result clamping is only dependent on fragment clamping.
678 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
681 if (state
& MESA_META_CONDITIONAL_RENDER
) {
682 save
->CondRenderQuery
= ctx
->Query
.CondRenderQuery
;
683 save
->CondRenderMode
= ctx
->Query
.CondRenderMode
;
685 if (ctx
->Query
.CondRenderQuery
)
686 _mesa_EndConditionalRender();
689 if (state
& MESA_META_SELECT_FEEDBACK
) {
690 save
->RenderMode
= ctx
->RenderMode
;
691 if (ctx
->RenderMode
== GL_SELECT
) {
692 save
->Select
= ctx
->Select
; /* struct copy */
693 _mesa_RenderMode(GL_RENDER
);
694 } else if (ctx
->RenderMode
== GL_FEEDBACK
) {
695 save
->Feedback
= ctx
->Feedback
; /* struct copy */
696 _mesa_RenderMode(GL_RENDER
);
702 save
->Lighting
= ctx
->Light
.Enabled
;
703 if (ctx
->Light
.Enabled
)
704 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
710 * Leave meta state. This is like a light-weight version of glPopAttrib().
713 _mesa_meta_end(struct gl_context
*ctx
)
715 struct save_state
*save
= &ctx
->Meta
->Save
[--ctx
->Meta
->SaveStackDepth
];
716 const GLbitfield state
= save
->SavedState
;
718 if (state
& MESA_META_ALPHA_TEST
) {
719 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
720 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
721 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
724 if (state
& MESA_META_BLEND
) {
725 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
726 if (ctx
->Extensions
.EXT_draw_buffers2
) {
728 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
729 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
733 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
736 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
737 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
740 if (state
& MESA_META_COLOR_MASK
) {
742 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
743 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
745 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
746 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
749 _mesa_ColorMaskIndexed(i
,
750 save
->ColorMask
[i
][0],
751 save
->ColorMask
[i
][1],
752 save
->ColorMask
[i
][2],
753 save
->ColorMask
[i
][3]);
759 if (state
& MESA_META_DEPTH_TEST
) {
760 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
761 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
762 _mesa_DepthFunc(save
->Depth
.Func
);
763 _mesa_DepthMask(save
->Depth
.Mask
);
766 if (state
& MESA_META_FOG
) {
767 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
770 if (state
& MESA_META_PIXEL_STORE
) {
771 ctx
->Pack
= save
->Pack
;
772 ctx
->Unpack
= save
->Unpack
;
775 if (state
& MESA_META_PIXEL_TRANSFER
) {
776 ctx
->Pixel
.RedScale
= save
->RedScale
;
777 ctx
->Pixel
.RedBias
= save
->RedBias
;
778 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
779 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
780 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
781 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
782 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
783 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
784 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
786 ctx
->NewState
|=_NEW_PIXEL
;
789 if (state
& MESA_META_RASTERIZATION
) {
790 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
791 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
792 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
793 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
794 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
795 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
798 if (state
& MESA_META_SCISSOR
) {
799 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
800 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
801 save
->Scissor
.Width
, save
->Scissor
.Height
);
804 if (state
& MESA_META_SHADER
) {
805 if (ctx
->Extensions
.ARB_vertex_program
) {
806 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
807 save
->VertexProgramEnabled
);
808 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
809 save
->VertexProgram
);
810 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
813 if (ctx
->Extensions
.ARB_fragment_program
) {
814 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
815 save
->FragmentProgramEnabled
);
816 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
817 save
->FragmentProgram
);
818 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
821 if (ctx
->Extensions
.ARB_vertex_shader
)
822 _mesa_use_shader_program(ctx
, GL_VERTEX_SHADER
, save
->VertexShader
);
824 if (ctx
->Extensions
.ARB_geometry_shader4
)
825 _mesa_use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
,
826 save
->GeometryShader
);
828 if (ctx
->Extensions
.ARB_fragment_shader
)
829 _mesa_use_shader_program(ctx
, GL_FRAGMENT_SHADER
,
830 save
->FragmentShader
);
832 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
835 _mesa_reference_shader_program(ctx
, &save
->VertexShader
, NULL
);
836 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
, NULL
);
837 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
, NULL
);
838 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
, NULL
);
841 if (state
& MESA_META_STENCIL_TEST
) {
842 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
844 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
845 _mesa_ClearStencil(stencil
->Clear
);
846 if (ctx
->Extensions
.EXT_stencil_two_side
) {
847 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
848 stencil
->TestTwoSide
);
849 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
850 ? GL_BACK
: GL_FRONT
);
853 _mesa_StencilFuncSeparate(GL_FRONT
,
854 stencil
->Function
[0],
856 stencil
->ValueMask
[0]);
857 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
858 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
859 stencil
->ZFailFunc
[0],
860 stencil
->ZPassFunc
[0]);
862 _mesa_StencilFuncSeparate(GL_BACK
,
863 stencil
->Function
[1],
865 stencil
->ValueMask
[1]);
866 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
867 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
868 stencil
->ZFailFunc
[1],
869 stencil
->ZPassFunc
[1]);
872 if (state
& MESA_META_TEXTURE
) {
875 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
877 /* restore texenv for unit[0] */
878 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
880 /* restore texture objects for unit[0] only */
881 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
882 if (ctx
->Texture
.Unit
[0].CurrentTex
[tgt
] != save
->CurrentTexture
[tgt
]) {
883 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
884 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
885 save
->CurrentTexture
[tgt
]);
887 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
890 /* Restore fixed function texture enables, texgen */
891 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
892 if (ctx
->Texture
.Unit
[u
].Enabled
!= save
->TexEnabled
[u
]) {
893 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
894 ctx
->Texture
.Unit
[u
].Enabled
= save
->TexEnabled
[u
];
897 if (ctx
->Texture
.Unit
[u
].TexGenEnabled
!= save
->TexGenEnabled
[u
]) {
898 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
899 ctx
->Texture
.Unit
[u
].TexGenEnabled
= save
->TexGenEnabled
[u
];
903 /* restore current unit state */
904 _mesa_ActiveTextureARB(GL_TEXTURE0
+ save
->ActiveUnit
);
905 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ save
->ClientActiveUnit
);
908 if (state
& MESA_META_TRANSFORM
) {
909 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
910 _mesa_ActiveTextureARB(GL_TEXTURE0
);
911 _mesa_MatrixMode(GL_TEXTURE
);
912 _mesa_LoadMatrixf(save
->TextureMatrix
);
913 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
915 _mesa_MatrixMode(GL_MODELVIEW
);
916 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
918 _mesa_MatrixMode(GL_PROJECTION
);
919 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
921 _mesa_MatrixMode(save
->MatrixMode
);
924 if (state
& MESA_META_CLIP
) {
925 if (save
->ClipPlanesEnabled
) {
927 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
928 if (save
->ClipPlanesEnabled
& (1 << i
)) {
929 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
935 if (state
& MESA_META_VERTEX
) {
936 /* restore vertex buffer object */
937 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
938 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
940 /* restore vertex array object */
941 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
942 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
945 if (state
& MESA_META_VIEWPORT
) {
946 if (save
->ViewportX
!= ctx
->Viewport
.X
||
947 save
->ViewportY
!= ctx
->Viewport
.Y
||
948 save
->ViewportW
!= ctx
->Viewport
.Width
||
949 save
->ViewportH
!= ctx
->Viewport
.Height
) {
950 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
951 save
->ViewportW
, save
->ViewportH
);
953 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
956 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
957 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
960 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
961 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
964 if (state
& MESA_META_CONDITIONAL_RENDER
) {
965 if (save
->CondRenderQuery
)
966 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
967 save
->CondRenderMode
);
970 if (state
& MESA_META_SELECT_FEEDBACK
) {
971 if (save
->RenderMode
== GL_SELECT
) {
972 _mesa_RenderMode(GL_SELECT
);
973 ctx
->Select
= save
->Select
;
974 } else if (save
->RenderMode
== GL_FEEDBACK
) {
975 _mesa_RenderMode(GL_FEEDBACK
);
976 ctx
->Feedback
= save
->Feedback
;
981 if (save
->Lighting
) {
982 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
988 * Determine whether Mesa is currently in a meta state.
991 _mesa_meta_in_progress(struct gl_context
*ctx
)
993 return ctx
->Meta
->SaveStackDepth
!= 0;
998 * Convert Z from a normalized value in the range [0, 1] to an object-space
999 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1000 * default/identity ortho projection results in the original Z value.
1001 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1002 * value comes from the clear value or raster position.
1004 static INLINE GLfloat
1005 invert_z(GLfloat normZ
)
1007 GLfloat objZ
= 1.0 - 2.0 * normZ
;
1013 * One-time init for a temp_texture object.
1014 * Choose tex target, compute max tex size, etc.
1017 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1019 /* prefer texture rectangle */
1020 if (ctx
->Extensions
.NV_texture_rectangle
) {
1021 tex
->Target
= GL_TEXTURE_RECTANGLE
;
1022 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
1023 tex
->NPOT
= GL_TRUE
;
1026 /* use 2D texture, NPOT if possible */
1027 tex
->Target
= GL_TEXTURE_2D
;
1028 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1029 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
1031 tex
->MinSize
= 16; /* 16 x 16 at least */
1032 assert(tex
->MaxSize
> 0);
1034 _mesa_GenTextures(1, &tex
->TexObj
);
1039 * Return pointer to temp_texture info for non-bitmap ops.
1040 * This does some one-time init if needed.
1042 static struct temp_texture
*
1043 get_temp_texture(struct gl_context
*ctx
)
1045 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
1048 init_temp_texture(ctx
, tex
);
1056 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1057 * We use a separate texture for bitmaps to reduce texture
1058 * allocation/deallocation.
1060 static struct temp_texture
*
1061 get_bitmap_temp_texture(struct gl_context
*ctx
)
1063 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
1066 init_temp_texture(ctx
, tex
);
1074 * Compute the width/height of texture needed to draw an image of the
1075 * given size. Return a flag indicating whether the current texture
1076 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1077 * allocated (glTexImage2D).
1078 * Also, compute s/t texcoords for drawing.
1080 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1083 alloc_texture(struct temp_texture
*tex
,
1084 GLsizei width
, GLsizei height
, GLenum intFormat
)
1086 GLboolean newTex
= GL_FALSE
;
1088 ASSERT(width
<= tex
->MaxSize
);
1089 ASSERT(height
<= tex
->MaxSize
);
1091 if (width
> tex
->Width
||
1092 height
> tex
->Height
||
1093 intFormat
!= tex
->IntFormat
) {
1094 /* alloc new texture (larger or different format) */
1097 /* use non-power of two size */
1098 tex
->Width
= MAX2(tex
->MinSize
, width
);
1099 tex
->Height
= MAX2(tex
->MinSize
, height
);
1102 /* find power of two size */
1104 w
= h
= tex
->MinSize
;
1113 tex
->IntFormat
= intFormat
;
1118 /* compute texcoords */
1119 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1120 tex
->Sright
= (GLfloat
) width
;
1121 tex
->Ttop
= (GLfloat
) height
;
1124 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1125 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1133 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1136 setup_copypix_texture(struct temp_texture
*tex
,
1138 GLint srcX
, GLint srcY
,
1139 GLsizei width
, GLsizei height
, GLenum intFormat
,
1142 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1143 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1144 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1145 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1147 /* copy framebuffer image to texture */
1149 /* create new tex image */
1150 if (tex
->Width
== width
&& tex
->Height
== height
) {
1151 /* create new tex with framebuffer data */
1152 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1153 srcX
, srcY
, width
, height
, 0);
1156 /* create empty texture */
1157 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1158 tex
->Width
, tex
->Height
, 0,
1159 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1161 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1162 0, 0, srcX
, srcY
, width
, height
);
1166 /* replace existing tex image */
1167 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1168 0, 0, srcX
, srcY
, width
, height
);
1174 * Setup/load texture for glDrawPixels.
1177 setup_drawpix_texture(struct gl_context
*ctx
,
1178 struct temp_texture
*tex
,
1180 GLenum texIntFormat
,
1181 GLsizei width
, GLsizei height
,
1182 GLenum format
, GLenum type
,
1183 const GLvoid
*pixels
)
1185 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1186 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1187 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1188 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1190 /* copy pixel data to texture */
1192 /* create new tex image */
1193 if (tex
->Width
== width
&& tex
->Height
== height
) {
1194 /* create new tex and load image data */
1195 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1196 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1199 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1201 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1202 ctx
->Unpack
.BufferObj
);
1203 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1204 /* create empty texture */
1205 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1206 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1207 if (save_unpack_obj
!= NULL
)
1208 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
,
1209 save_unpack_obj
->Name
);
1211 _mesa_TexSubImage2D(tex
->Target
, 0,
1212 0, 0, width
, height
, format
, type
, pixels
);
1216 /* replace existing tex image */
1217 _mesa_TexSubImage2D(tex
->Target
, 0,
1218 0, 0, width
, height
, format
, type
, pixels
);
1225 * One-time init for drawing depth pixels.
1228 init_blit_depth_pixels(struct gl_context
*ctx
)
1230 static const char *program
=
1232 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1235 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1236 struct temp_texture
*tex
= get_temp_texture(ctx
);
1237 const char *texTarget
;
1239 assert(blit
->DepthFP
== 0);
1241 /* replace %s with "RECT" or "2D" */
1242 assert(strlen(program
) + 4 < sizeof(program2
));
1243 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1247 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1249 _mesa_GenPrograms(1, &blit
->DepthFP
);
1250 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1251 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1252 strlen(program2
), (const GLubyte
*) program2
);
1257 * Try to do a glBlitFramebuffer using no-copy texturing.
1258 * We can do this when the src renderbuffer is actually a texture.
1259 * But if the src buffer == dst buffer we cannot do this.
1261 * \return new buffer mask indicating the buffers left to blit using the
1265 blitframebuffer_texture(struct gl_context
*ctx
,
1266 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1267 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1268 GLbitfield mask
, GLenum filter
)
1270 if (mask
& GL_COLOR_BUFFER_BIT
) {
1271 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1272 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1273 const struct gl_renderbuffer_attachment
*drawAtt
=
1274 &drawFb
->Attachment
[drawFb
->_ColorDrawBufferIndexes
[0]];
1275 const struct gl_renderbuffer_attachment
*readAtt
=
1276 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1278 if (readAtt
&& readAtt
->Texture
) {
1279 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1280 const GLuint srcLevel
= readAtt
->TextureLevel
;
1281 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
1282 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
1283 const GLint baseLevelSave
= texObj
->BaseLevel
;
1284 const GLint maxLevelSave
= texObj
->MaxLevel
;
1285 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
1286 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
1287 const GLenum srgbSave
= texObj
->Sampler
.sRGBDecode
;
1288 const GLenum fbo_srgb_save
= ctx
->Color
.sRGBEnabled
;
1289 const GLenum target
= texObj
->Target
;
1291 if (drawAtt
->Texture
== readAtt
->Texture
) {
1292 /* Can't use same texture as both the source and dest. We need
1293 * to handle overlapping blits and besides, some hw may not
1299 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1300 /* Can't handle other texture types at this time */
1305 printf("Blit from texture!\n");
1306 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1307 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1310 /* Prepare src texture state */
1311 _mesa_BindTexture(target
, texObj
->Name
);
1312 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
1313 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
1314 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1315 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1316 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1318 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1319 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1321 /* Always do our blits with no sRGB decode or encode.*/
1322 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1323 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
1324 GL_SKIP_DECODE_EXT
);
1326 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
1327 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
1330 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1331 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1333 /* Prepare vertex data (the VBO was previously created and bound) */
1338 struct vertex verts
[4];
1339 GLfloat s0
, t0
, s1
, t1
;
1341 if (target
== GL_TEXTURE_2D
) {
1342 const struct gl_texture_image
*texImage
1343 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1344 s0
= srcX0
/ (float) texImage
->Width
;
1345 s1
= srcX1
/ (float) texImage
->Width
;
1346 t0
= srcY0
/ (float) texImage
->Height
;
1347 t1
= srcY1
/ (float) texImage
->Height
;
1350 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1357 verts
[0].x
= (GLfloat
) dstX0
;
1358 verts
[0].y
= (GLfloat
) dstY0
;
1359 verts
[1].x
= (GLfloat
) dstX1
;
1360 verts
[1].y
= (GLfloat
) dstY0
;
1361 verts
[2].x
= (GLfloat
) dstX1
;
1362 verts
[2].y
= (GLfloat
) dstY1
;
1363 verts
[3].x
= (GLfloat
) dstX0
;
1364 verts
[3].y
= (GLfloat
) dstY1
;
1375 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1378 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1380 /* Restore texture object state, the texture binding will
1381 * be restored by _mesa_meta_end().
1383 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
1384 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
1385 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1386 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1387 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1389 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
1390 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
1391 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1392 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
, srgbSave
);
1394 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&& fbo_srgb_save
) {
1395 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_TRUE
);
1398 /* Done with color buffer */
1399 mask
&= ~GL_COLOR_BUFFER_BIT
;
1408 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1409 * of texture mapping and polygon rendering.
1412 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1413 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1414 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1415 GLbitfield mask
, GLenum filter
)
1417 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1418 struct temp_texture
*tex
= get_temp_texture(ctx
);
1419 const GLsizei maxTexSize
= tex
->MaxSize
;
1420 const GLint srcX
= MIN2(srcX0
, srcX1
);
1421 const GLint srcY
= MIN2(srcY0
, srcY1
);
1422 const GLint srcW
= abs(srcX1
- srcX0
);
1423 const GLint srcH
= abs(srcY1
- srcY0
);
1424 const GLboolean srcFlipX
= srcX1
< srcX0
;
1425 const GLboolean srcFlipY
= srcY1
< srcY0
;
1429 struct vertex verts
[4];
1432 if (srcW
> maxTexSize
|| srcH
> maxTexSize
) {
1433 /* XXX avoid this fallback */
1434 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1435 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1451 /* only scissor effects blit so save/clear all other relevant state */
1452 _mesa_meta_begin(ctx
, ~MESA_META_SCISSOR
);
1454 if (blit
->ArrayObj
== 0) {
1455 /* one-time setup */
1457 /* create vertex array object */
1458 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1459 _mesa_BindVertexArray(blit
->ArrayObj
);
1461 /* create vertex array buffer */
1462 _mesa_GenBuffersARB(1, &blit
->VBO
);
1463 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1464 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1465 NULL
, GL_DYNAMIC_DRAW_ARB
);
1467 /* setup vertex arrays */
1468 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1469 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1470 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1471 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1474 _mesa_BindVertexArray(blit
->ArrayObj
);
1475 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1478 /* Try faster, direct texture approach first */
1479 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1480 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1482 _mesa_meta_end(ctx
);
1486 /* Continue with "normal" approach which involves copying the src rect
1487 * into a temporary texture and is "blitted" by drawing a textured quad.
1490 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1492 /* vertex positions/texcoords (after texture allocation!) */
1494 verts
[0].x
= (GLfloat
) dstX0
;
1495 verts
[0].y
= (GLfloat
) dstY0
;
1496 verts
[1].x
= (GLfloat
) dstX1
;
1497 verts
[1].y
= (GLfloat
) dstY0
;
1498 verts
[2].x
= (GLfloat
) dstX1
;
1499 verts
[2].y
= (GLfloat
) dstY1
;
1500 verts
[3].x
= (GLfloat
) dstX0
;
1501 verts
[3].y
= (GLfloat
) dstY1
;
1505 verts
[1].s
= tex
->Sright
;
1507 verts
[2].s
= tex
->Sright
;
1508 verts
[2].t
= tex
->Ttop
;
1510 verts
[3].t
= tex
->Ttop
;
1512 /* upload new vertex data */
1513 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1516 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1518 if (mask
& GL_COLOR_BUFFER_BIT
) {
1519 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1521 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1522 mask
&= ~GL_COLOR_BUFFER_BIT
;
1525 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1526 GLuint
*tmp
= (GLuint
*) malloc(srcW
* srcH
* sizeof(GLuint
));
1529 init_blit_depth_pixels(ctx
);
1531 /* maybe change tex format here */
1532 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1534 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1535 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1537 setup_drawpix_texture(ctx
, tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1538 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1540 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1541 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1542 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1543 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1544 _mesa_DepthFunc(GL_ALWAYS
);
1545 _mesa_DepthMask(GL_TRUE
);
1547 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1548 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1554 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1555 /* XXX can't easily do stencil */
1558 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1560 _mesa_meta_end(ctx
);
1563 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1564 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1570 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1573 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1575 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1577 GLfloat x
, y
, z
, r
, g
, b
, a
;
1579 struct vertex verts
[4];
1580 /* save all state but scissor, pixel pack/unpack */
1581 GLbitfield metaSave
= (MESA_META_ALL
-
1583 MESA_META_PIXEL_STORE
-
1584 MESA_META_CONDITIONAL_RENDER
);
1585 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1587 if (buffers
& BUFFER_BITS_COLOR
) {
1588 /* if clearing color buffers, don't save/restore colormask */
1589 metaSave
-= MESA_META_COLOR_MASK
;
1592 _mesa_meta_begin(ctx
, metaSave
);
1594 if (clear
->ArrayObj
== 0) {
1595 /* one-time setup */
1597 /* create vertex array object */
1598 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1599 _mesa_BindVertexArray(clear
->ArrayObj
);
1601 /* create vertex array buffer */
1602 _mesa_GenBuffersARB(1, &clear
->VBO
);
1603 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1605 /* setup vertex arrays */
1606 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1607 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1608 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1609 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1612 _mesa_BindVertexArray(clear
->ArrayObj
);
1613 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1616 /* GL_COLOR_BUFFER_BIT */
1617 if (buffers
& BUFFER_BITS_COLOR
) {
1618 /* leave colormask, glDrawBuffer state as-is */
1620 /* Clears never have the color clamped. */
1621 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1624 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1625 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1628 /* GL_DEPTH_BUFFER_BIT */
1629 if (buffers
& BUFFER_BIT_DEPTH
) {
1630 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1631 _mesa_DepthFunc(GL_ALWAYS
);
1632 _mesa_DepthMask(GL_TRUE
);
1635 assert(!ctx
->Depth
.Test
);
1638 /* GL_STENCIL_BUFFER_BIT */
1639 if (buffers
& BUFFER_BIT_STENCIL
) {
1640 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1641 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1642 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1643 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1644 ctx
->Stencil
.Clear
& stencilMax
,
1645 ctx
->Stencil
.WriteMask
[0]);
1648 assert(!ctx
->Stencil
.Enabled
);
1651 /* vertex positions/colors */
1653 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1654 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1655 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1656 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1657 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1674 for (i
= 0; i
< 4; i
++) {
1675 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
1676 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
1677 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
1678 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
1681 /* upload new vertex data */
1682 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1683 GL_DYNAMIC_DRAW_ARB
);
1687 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1689 _mesa_meta_end(ctx
);
1693 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
1695 const char *vs_source
=
1696 "attribute vec4 position;\n"
1699 " gl_Position = position;\n"
1701 const char *fs_source
=
1702 "uniform vec4 color;\n"
1705 " gl_FragColor = color;\n"
1707 const char *vs_int_source
=
1709 "attribute vec4 position;\n"
1712 " gl_Position = position;\n"
1714 const char *fs_int_source
=
1716 "uniform ivec4 color;\n"
1717 "out ivec4 out_color;\n"
1721 " out_color = color;\n"
1725 if (clear
->ArrayObj
!= 0)
1728 /* create vertex array object */
1729 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1730 _mesa_BindVertexArray(clear
->ArrayObj
);
1732 /* create vertex array buffer */
1733 _mesa_GenBuffersARB(1, &clear
->VBO
);
1734 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1736 /* setup vertex arrays */
1737 _mesa_VertexAttribPointerARB(0, 3, GL_FLOAT
, GL_FALSE
, 0, (void *)0);
1738 _mesa_EnableVertexAttribArrayARB(0);
1740 vs
= _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER
);
1741 _mesa_ShaderSourceARB(vs
, 1, &vs_source
, NULL
);
1742 _mesa_CompileShaderARB(vs
);
1744 fs
= _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER
);
1745 _mesa_ShaderSourceARB(fs
, 1, &fs_source
, NULL
);
1746 _mesa_CompileShaderARB(fs
);
1748 clear
->ShaderProg
= _mesa_CreateProgramObjectARB();
1749 _mesa_AttachShader(clear
->ShaderProg
, fs
);
1750 _mesa_AttachShader(clear
->ShaderProg
, vs
);
1751 _mesa_BindAttribLocationARB(clear
->ShaderProg
, 0, "position");
1752 _mesa_LinkProgramARB(clear
->ShaderProg
);
1754 clear
->ColorLocation
= _mesa_GetUniformLocationARB(clear
->ShaderProg
,
1757 if (ctx
->Const
.GLSLVersion
>= 130) {
1758 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_int_source
);
1759 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_int_source
);
1761 clear
->IntegerShaderProg
= _mesa_CreateProgramObjectARB();
1762 _mesa_AttachShader(clear
->IntegerShaderProg
, fs
);
1763 _mesa_AttachShader(clear
->IntegerShaderProg
, vs
);
1764 _mesa_BindAttribLocationARB(clear
->IntegerShaderProg
, 0, "position");
1766 /* Note that user-defined out attributes get automatically assigned
1767 * locations starting from 0, so we don't need to explicitly
1768 * BindFragDataLocation to 0.
1771 link_program_with_debug(ctx
, clear
->IntegerShaderProg
);
1773 clear
->IntegerColorLocation
=
1774 _mesa_GetUniformLocationARB(clear
->IntegerShaderProg
, "color");
1779 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1782 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1784 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1785 GLbitfield metaSave
;
1786 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1787 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1788 const float x0
= ((float)fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
1789 const float y0
= ((float)fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
1790 const float x1
= ((float)fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
1791 const float y1
= ((float)fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
1792 const float z
= -invert_z(ctx
->Depth
.Clear
);
1797 metaSave
= (MESA_META_ALPHA_TEST
|
1799 MESA_META_DEPTH_TEST
|
1800 MESA_META_RASTERIZATION
|
1802 MESA_META_STENCIL_TEST
|
1804 MESA_META_VIEWPORT
|
1806 MESA_META_CLAMP_FRAGMENT_COLOR
);
1808 if (!(buffers
& BUFFER_BITS_COLOR
)) {
1809 /* We'll use colormask to disable color writes. Otherwise,
1810 * respect color mask
1812 metaSave
|= MESA_META_COLOR_MASK
;
1815 _mesa_meta_begin(ctx
, metaSave
);
1817 meta_glsl_clear_init(ctx
, clear
);
1819 if (fb
->_IntegerColor
) {
1820 _mesa_UseProgramObjectARB(clear
->IntegerShaderProg
);
1821 _mesa_Uniform4ivARB(clear
->IntegerColorLocation
, 1,
1822 ctx
->Color
.ClearColor
.i
);
1824 _mesa_UseProgramObjectARB(clear
->ShaderProg
);
1825 _mesa_Uniform4fvARB(clear
->ColorLocation
, 1,
1826 ctx
->Color
.ClearColor
.f
);
1829 _mesa_BindVertexArray(clear
->ArrayObj
);
1830 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1832 /* GL_COLOR_BUFFER_BIT */
1833 if (buffers
& BUFFER_BITS_COLOR
) {
1834 /* leave colormask, glDrawBuffer state as-is */
1836 /* Clears never have the color clamped. */
1837 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1840 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1841 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1844 /* GL_DEPTH_BUFFER_BIT */
1845 if (buffers
& BUFFER_BIT_DEPTH
) {
1846 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1847 _mesa_DepthFunc(GL_ALWAYS
);
1848 _mesa_DepthMask(GL_TRUE
);
1851 assert(!ctx
->Depth
.Test
);
1854 /* GL_STENCIL_BUFFER_BIT */
1855 if (buffers
& BUFFER_BIT_STENCIL
) {
1856 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1857 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1858 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1859 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1860 ctx
->Stencil
.Clear
& stencilMax
,
1861 ctx
->Stencil
.WriteMask
[0]);
1864 assert(!ctx
->Stencil
.Enabled
);
1867 /* vertex positions */
1881 /* upload new vertex data */
1882 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1883 GL_DYNAMIC_DRAW_ARB
);
1886 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1888 _mesa_meta_end(ctx
);
1892 * Meta implementation of ctx->Driver.CopyPixels() in terms
1893 * of texture mapping and polygon rendering and GLSL shaders.
1896 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
1897 GLsizei width
, GLsizei height
,
1898 GLint dstX
, GLint dstY
, GLenum type
)
1900 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1901 struct temp_texture
*tex
= get_temp_texture(ctx
);
1903 GLfloat x
, y
, z
, s
, t
;
1905 struct vertex verts
[4];
1907 GLenum intFormat
= GL_RGBA
;
1909 if (type
!= GL_COLOR
||
1910 ctx
->_ImageTransferState
||
1912 width
> tex
->MaxSize
||
1913 height
> tex
->MaxSize
) {
1914 /* XXX avoid this fallback */
1915 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1919 /* Most GL state applies to glCopyPixels, but a there's a few things
1920 * we need to override:
1922 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
1925 MESA_META_TRANSFORM
|
1928 MESA_META_VIEWPORT
));
1930 if (copypix
->ArrayObj
== 0) {
1931 /* one-time setup */
1933 /* create vertex array object */
1934 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
1935 _mesa_BindVertexArray(copypix
->ArrayObj
);
1937 /* create vertex array buffer */
1938 _mesa_GenBuffersARB(1, ©pix
->VBO
);
1939 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1940 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1941 NULL
, GL_DYNAMIC_DRAW_ARB
);
1943 /* setup vertex arrays */
1944 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1945 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1946 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1947 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1950 _mesa_BindVertexArray(copypix
->ArrayObj
);
1951 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1954 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
1956 /* vertex positions, texcoords (after texture allocation!) */
1958 const GLfloat dstX0
= (GLfloat
) dstX
;
1959 const GLfloat dstY0
= (GLfloat
) dstY
;
1960 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1961 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1962 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1972 verts
[1].s
= tex
->Sright
;
1977 verts
[2].s
= tex
->Sright
;
1978 verts
[2].t
= tex
->Ttop
;
1983 verts
[3].t
= tex
->Ttop
;
1985 /* upload new vertex data */
1986 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1989 /* Alloc/setup texture */
1990 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
1991 GL_RGBA
, GL_NEAREST
);
1993 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1995 /* draw textured quad */
1996 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1998 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2000 _mesa_meta_end(ctx
);
2006 * When the glDrawPixels() image size is greater than the max rectangle
2007 * texture size we use this function to break the glDrawPixels() image
2008 * into tiles which fit into the max texture size.
2011 tiled_draw_pixels(struct gl_context
*ctx
,
2013 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2014 GLenum format
, GLenum type
,
2015 const struct gl_pixelstore_attrib
*unpack
,
2016 const GLvoid
*pixels
)
2018 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
2021 if (tileUnpack
.RowLength
== 0)
2022 tileUnpack
.RowLength
= width
;
2024 for (i
= 0; i
< width
; i
+= tileSize
) {
2025 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
2026 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
2028 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
2030 for (j
= 0; j
< height
; j
+= tileSize
) {
2031 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
2032 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
2034 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
2036 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
2037 format
, type
, &tileUnpack
, pixels
);
2044 * One-time init for drawing stencil pixels.
2047 init_draw_stencil_pixels(struct gl_context
*ctx
)
2049 /* This program is run eight times, once for each stencil bit.
2050 * The stencil values to draw are found in an 8-bit alpha texture.
2051 * We read the texture/stencil value and test if bit 'b' is set.
2052 * If the bit is not set, use KIL to kill the fragment.
2053 * Finally, we use the stencil test to update the stencil buffer.
2055 * The basic algorithm for checking if a bit is set is:
2056 * if (is_odd(value / (1 << bit)))
2057 * result is one (or non-zero).
2060 * The program parameter contains three values:
2061 * parm.x = 255 / (1 << bit)
2065 static const char *program
=
2067 "PARAM parm = program.local[0]; \n"
2069 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
2070 "# t = t * 255 / bit \n"
2071 "MUL t.x, t.a, parm.x; \n"
2074 "SUB t.x, t.x, t.y; \n"
2076 "MUL t.x, t.x, parm.y; \n"
2077 "# t = fract(t.x) \n"
2078 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2079 "# t.x = (t.x == 0 ? 1 : 0) \n"
2080 "SGE t.x, -t.x, parm.z; \n"
2082 "# for debug only \n"
2083 "#MOV result.color, t.x; \n"
2085 char program2
[1000];
2086 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2087 struct temp_texture
*tex
= get_temp_texture(ctx
);
2088 const char *texTarget
;
2090 assert(drawpix
->StencilFP
== 0);
2092 /* replace %s with "RECT" or "2D" */
2093 assert(strlen(program
) + 4 < sizeof(program2
));
2094 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2098 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2100 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
2101 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2102 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2103 strlen(program2
), (const GLubyte
*) program2
);
2108 * One-time init for drawing depth pixels.
2111 init_draw_depth_pixels(struct gl_context
*ctx
)
2113 static const char *program
=
2115 "PARAM color = program.local[0]; \n"
2116 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2117 "MOV result.color, color; \n"
2120 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2121 struct temp_texture
*tex
= get_temp_texture(ctx
);
2122 const char *texTarget
;
2124 assert(drawpix
->DepthFP
== 0);
2126 /* replace %s with "RECT" or "2D" */
2127 assert(strlen(program
) + 4 < sizeof(program2
));
2128 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2132 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2134 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
2135 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2136 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2137 strlen(program2
), (const GLubyte
*) program2
);
2142 * Meta implementation of ctx->Driver.DrawPixels() in terms
2143 * of texture mapping and polygon rendering.
2146 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2147 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2148 GLenum format
, GLenum type
,
2149 const struct gl_pixelstore_attrib
*unpack
,
2150 const GLvoid
*pixels
)
2152 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2153 struct temp_texture
*tex
= get_temp_texture(ctx
);
2154 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2155 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2157 GLfloat x
, y
, z
, s
, t
;
2159 struct vertex verts
[4];
2160 GLenum texIntFormat
;
2161 GLboolean fallback
, newTex
;
2162 GLbitfield metaExtraSave
= 0x0;
2166 * Determine if we can do the glDrawPixels with texture mapping.
2168 fallback
= GL_FALSE
;
2169 if (ctx
->_ImageTransferState
||
2174 if (_mesa_is_color_format(format
)) {
2175 /* use more compact format when possible */
2176 /* XXX disable special case for GL_LUMINANCE for now to work around
2177 * apparent i965 driver bug (see bug #23670).
2179 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2180 texIntFormat
= format
;
2182 texIntFormat
= GL_RGBA
;
2184 /* If we're not supposed to clamp the resulting color, then just
2185 * promote our texture to fully float. We could do better by
2186 * just going for the matching set of channels, in floating
2189 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2190 ctx
->Extensions
.ARB_texture_float
)
2191 texIntFormat
= GL_RGBA32F
;
2193 else if (_mesa_is_stencil_format(format
)) {
2194 if (ctx
->Extensions
.ARB_fragment_program
&&
2195 ctx
->Pixel
.IndexShift
== 0 &&
2196 ctx
->Pixel
.IndexOffset
== 0 &&
2197 type
== GL_UNSIGNED_BYTE
) {
2198 /* We'll store stencil as alpha. This only works for GLubyte
2199 * image data because of how incoming values are mapped to alpha
2202 texIntFormat
= GL_ALPHA
;
2203 metaExtraSave
= (MESA_META_COLOR_MASK
|
2204 MESA_META_DEPTH_TEST
|
2206 MESA_META_STENCIL_TEST
);
2212 else if (_mesa_is_depth_format(format
)) {
2213 if (ctx
->Extensions
.ARB_depth_texture
&&
2214 ctx
->Extensions
.ARB_fragment_program
) {
2215 texIntFormat
= GL_DEPTH_COMPONENT
;
2216 metaExtraSave
= (MESA_META_SHADER
);
2227 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2228 format
, type
, unpack
, pixels
);
2233 * Check image size against max texture size, draw as tiles if needed.
2235 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2236 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2237 format
, type
, unpack
, pixels
);
2241 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2242 * but a there's a few things we need to override:
2244 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2247 MESA_META_TRANSFORM
|
2250 MESA_META_VIEWPORT
|
2251 MESA_META_CLAMP_FRAGMENT_COLOR
|
2254 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2256 /* vertex positions, texcoords (after texture allocation!) */
2258 const GLfloat x0
= (GLfloat
) x
;
2259 const GLfloat y0
= (GLfloat
) y
;
2260 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2261 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2262 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2272 verts
[1].s
= tex
->Sright
;
2277 verts
[2].s
= tex
->Sright
;
2278 verts
[2].t
= tex
->Ttop
;
2283 verts
[3].t
= tex
->Ttop
;
2286 if (drawpix
->ArrayObj
== 0) {
2287 /* one-time setup: create vertex array object */
2288 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
2290 _mesa_BindVertexArray(drawpix
->ArrayObj
);
2292 /* create vertex array buffer */
2293 _mesa_GenBuffersARB(1, &vbo
);
2294 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
2295 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2296 verts
, GL_DYNAMIC_DRAW_ARB
);
2298 /* setup vertex arrays */
2299 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2300 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2301 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2302 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2304 /* set given unpack params */
2305 ctx
->Unpack
= *unpack
;
2307 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2309 if (_mesa_is_stencil_format(format
)) {
2310 /* Drawing stencil */
2313 if (!drawpix
->StencilFP
)
2314 init_draw_stencil_pixels(ctx
);
2316 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2317 GL_ALPHA
, type
, pixels
);
2319 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2321 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2323 /* set all stencil bits to 0 */
2324 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2325 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2326 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2328 /* set stencil bits to 1 where needed */
2329 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2331 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2332 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2334 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2335 const GLuint mask
= 1 << bit
;
2336 if (mask
& origStencilMask
) {
2337 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2338 _mesa_StencilMask(mask
);
2340 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2341 255.0 / mask
, 0.5, 0.0, 0.0);
2343 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2347 else if (_mesa_is_depth_format(format
)) {
2349 if (!drawpix
->DepthFP
)
2350 init_draw_depth_pixels(ctx
);
2352 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2353 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2355 /* polygon color = current raster color */
2356 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2357 ctx
->Current
.RasterColor
);
2359 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2360 format
, type
, pixels
);
2362 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2366 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2367 format
, type
, pixels
);
2368 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2371 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2373 _mesa_DeleteBuffersARB(1, &vbo
);
2375 /* restore unpack params */
2376 ctx
->Unpack
= unpackSave
;
2378 _mesa_meta_end(ctx
);
2382 alpha_test_raster_color(struct gl_context
*ctx
)
2384 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2385 GLfloat ref
= ctx
->Color
.AlphaRef
;
2387 switch (ctx
->Color
.AlphaFunc
) {
2393 return alpha
== ref
;
2395 return alpha
<= ref
;
2399 return alpha
!= ref
;
2401 return alpha
>= ref
;
2411 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2412 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2413 * tracker would improve performance a lot.
2416 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2417 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2418 const struct gl_pixelstore_attrib
*unpack
,
2419 const GLubyte
*bitmap1
)
2421 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2422 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2423 const GLenum texIntFormat
= GL_ALPHA
;
2424 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2427 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2429 struct vertex verts
[4];
2434 * Check if swrast fallback is needed.
2436 if (ctx
->_ImageTransferState
||
2437 ctx
->FragmentProgram
._Enabled
||
2439 ctx
->Texture
._EnabledUnits
||
2440 width
> tex
->MaxSize
||
2441 height
> tex
->MaxSize
) {
2442 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2446 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2449 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2450 * but a there's a few things we need to override:
2452 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
2453 MESA_META_PIXEL_STORE
|
2454 MESA_META_RASTERIZATION
|
2457 MESA_META_TRANSFORM
|
2460 MESA_META_VIEWPORT
));
2462 if (bitmap
->ArrayObj
== 0) {
2463 /* one-time setup */
2465 /* create vertex array object */
2466 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2467 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2469 /* create vertex array buffer */
2470 _mesa_GenBuffersARB(1, &bitmap
->VBO
);
2471 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2472 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2473 NULL
, GL_DYNAMIC_DRAW_ARB
);
2475 /* setup vertex arrays */
2476 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2477 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2478 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2479 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2480 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2481 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2484 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2485 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2488 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2490 /* vertex positions, texcoords, colors (after texture allocation!) */
2492 const GLfloat x0
= (GLfloat
) x
;
2493 const GLfloat y0
= (GLfloat
) y
;
2494 const GLfloat x1
= (GLfloat
) (x
+ width
);
2495 const GLfloat y1
= (GLfloat
) (y
+ height
);
2496 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2507 verts
[1].s
= tex
->Sright
;
2512 verts
[2].s
= tex
->Sright
;
2513 verts
[2].t
= tex
->Ttop
;
2518 verts
[3].t
= tex
->Ttop
;
2520 for (i
= 0; i
< 4; i
++) {
2521 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2522 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2523 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2524 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2527 /* upload new vertex data */
2528 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2531 /* choose different foreground/background alpha values */
2532 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2533 bg
= (fg
> 127 ? 0 : 255);
2535 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2537 _mesa_meta_end(ctx
);
2541 bitmap8
= (GLubyte
*) malloc(width
* height
);
2543 memset(bitmap8
, bg
, width
* height
);
2544 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2545 bitmap8
, width
, fg
);
2547 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2549 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2550 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2552 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2553 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2555 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2557 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2562 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2564 _mesa_meta_end(ctx
);
2569 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2570 * software fallback. The fallback path will require that the texture
2571 * images are mapped.
2572 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2575 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
2576 struct gl_texture_object
*texObj
)
2578 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2579 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2580 struct gl_texture_image
*baseImage
;
2584 /* check for fallbacks */
2585 if (!ctx
->Extensions
.EXT_framebuffer_object
||
2586 target
== GL_TEXTURE_3D
||
2587 target
== GL_TEXTURE_1D_ARRAY
||
2588 target
== GL_TEXTURE_2D_ARRAY
) {
2592 srcLevel
= texObj
->BaseLevel
;
2593 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2594 if (!baseImage
|| _mesa_is_format_compressed(baseImage
->TexFormat
)) {
2598 if (_mesa_get_format_color_encoding(baseImage
->TexFormat
) == GL_SRGB
&&
2599 !ctx
->Extensions
.EXT_texture_sRGB_decode
) {
2600 /* The texture format is sRGB but we can't turn off sRGB->linear
2601 * texture sample conversion. So we won't be able to generate the
2602 * right colors when rendering. Need to use a fallback.
2608 * Test that we can actually render in the texture's format.
2611 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2612 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2614 if (target
== GL_TEXTURE_1D
) {
2615 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2616 GL_COLOR_ATTACHMENT0_EXT
,
2617 target
, texObj
->Name
, srcLevel
);
2620 /* other work is needed to enable 3D mipmap generation */
2621 else if (target
== GL_TEXTURE_3D
) {
2623 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2624 GL_COLOR_ATTACHMENT0_EXT
,
2625 target
, texObj
->Name
, srcLevel
, zoffset
);
2630 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2631 GL_COLOR_ATTACHMENT0_EXT
,
2632 target
, texObj
->Name
, srcLevel
);
2635 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2637 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2639 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2648 * Compute the texture coordinates for the four vertices of a quad for
2649 * drawing a 2D texture image or slice of a cube/3D texture.
2650 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2651 * \param slice slice of a 1D/2D array texture or 3D texture
2652 * \param width width of the texture image
2653 * \param height height of the texture image
2654 * \param coords0/1/2/3 returns the computed texcoords
2657 setup_texture_coords(GLenum faceTarget
,
2666 static const GLfloat st
[4][2] = {
2667 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2672 switch (faceTarget
) {
2676 case GL_TEXTURE_2D_ARRAY
:
2677 if (faceTarget
== GL_TEXTURE_3D
)
2679 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
2683 coords0
[0] = 0.0F
; /* s */
2684 coords0
[1] = 0.0F
; /* t */
2685 coords0
[2] = r
; /* r */
2696 case GL_TEXTURE_RECTANGLE_ARB
:
2697 coords0
[0] = 0.0F
; /* s */
2698 coords0
[1] = 0.0F
; /* t */
2699 coords0
[2] = 0.0F
; /* r */
2704 coords2
[1] = height
;
2707 coords3
[1] = height
;
2710 case GL_TEXTURE_1D_ARRAY
:
2711 coords0
[0] = 0.0F
; /* s */
2712 coords0
[1] = slice
; /* t */
2713 coords0
[2] = 0.0F
; /* r */
2725 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2726 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2727 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2728 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2729 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2730 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2731 /* loop over quad verts */
2732 for (i
= 0; i
< 4; i
++) {
2733 /* Compute sc = +/-scale and tc = +/-scale.
2734 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2735 * though that can still sometimes happen with this scale factor...
2737 const GLfloat scale
= 0.9999f
;
2738 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2739 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2759 switch (faceTarget
) {
2760 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2765 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2770 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2775 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2780 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2785 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2796 assert(0 && "unexpected target in meta setup_texture_coords()");
2802 * Called via ctx->Driver.GenerateMipmap()
2803 * Note: We don't yet support 3D textures, 1D/2D array textures or texture
2807 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
2808 struct gl_texture_object
*texObj
)
2810 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2812 GLfloat x
, y
, tex
[3];
2814 struct vertex verts
[4];
2815 const GLuint baseLevel
= texObj
->BaseLevel
;
2816 const GLuint maxLevel
= texObj
->MaxLevel
;
2817 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
2818 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
2819 const GLint maxLevelSave
= texObj
->MaxLevel
;
2820 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
2821 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
2822 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
2823 const GLenum wrapRSave
= texObj
->Sampler
.WrapR
;
2824 const GLenum srgbDecodeSave
= texObj
->Sampler
.sRGBDecode
;
2825 const GLenum srgbBufferSave
= ctx
->Color
.sRGBEnabled
;
2826 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2827 const GLuint original_active_unit
= ctx
->Texture
.CurrentUnit
;
2830 const GLuint border
= 0;
2831 const GLint slice
= 0;
2833 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
2834 _mesa_generate_mipmap(ctx
, target
, texObj
);
2838 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
2839 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
2840 faceTarget
= target
;
2841 target
= GL_TEXTURE_CUBE_MAP
;
2844 faceTarget
= target
;
2847 _mesa_meta_begin(ctx
, MESA_META_ALL
);
2849 if (original_active_unit
!= 0)
2850 _mesa_BindTexture(target
, texObj
->Name
);
2852 if (mipmap
->ArrayObj
== 0) {
2853 /* one-time setup */
2855 /* create vertex array object */
2856 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
2857 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
2859 /* create vertex array buffer */
2860 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
2861 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2862 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2863 NULL
, GL_DYNAMIC_DRAW_ARB
);
2865 /* setup vertex arrays */
2866 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2867 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
2868 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2869 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2872 _mesa_BindVertexArray(mipmap
->ArrayObj
);
2873 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2877 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2879 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2881 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
2882 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
2883 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
2884 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2885 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2886 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
2888 /* We don't want to encode or decode sRGB values; treat them as linear */
2889 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
2890 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
2891 GL_SKIP_DECODE_EXT
);
2893 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
2894 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
2897 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2899 /* setup texcoords (XXX what about border?) */
2900 setup_texture_coords(faceTarget
,
2901 0.0, 0.0, /* width, height never used here */
2908 /* setup vertex positions */
2918 /* upload new vertex data */
2919 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2921 /* setup projection matrix */
2922 _mesa_MatrixMode(GL_PROJECTION
);
2923 _mesa_LoadIdentity();
2924 _mesa_Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
2926 /* texture is already locked, unlock now */
2927 _mesa_unlock_texture(ctx
, texObj
);
2929 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
2930 const struct gl_texture_image
*srcImage
;
2931 const GLuint srcLevel
= dstLevel
- 1;
2932 GLsizei srcWidth
, srcHeight
, srcDepth
;
2933 GLsizei dstWidth
, dstHeight
, dstDepth
;
2936 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
2937 assert(srcImage
->Border
== 0); /* XXX we can fix this */
2939 /* src size w/out border */
2940 srcWidth
= srcImage
->Width
- 2 * border
;
2941 srcHeight
= srcImage
->Height
- 2 * border
;
2942 srcDepth
= srcImage
->Depth
- 2 * border
;
2944 /* new dst size w/ border */
2945 dstWidth
= MAX2(1, srcWidth
/ 2) + 2 * border
;
2946 dstHeight
= MAX2(1, srcHeight
/ 2) + 2 * border
;
2947 dstDepth
= MAX2(1, srcDepth
/ 2) + 2 * border
;
2949 if (dstWidth
== srcImage
->Width
&&
2950 dstHeight
== srcImage
->Height
&&
2951 dstDepth
== srcImage
->Depth
) {
2956 /* Allocate storage for the destination mipmap image(s) */
2958 /* Set MaxLevel large enough to hold the new level when we allocate it */
2959 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
2961 if (!_mesa_prepare_mipmap_level(ctx
, texObj
, dstLevel
,
2962 dstWidth
, dstHeight
, dstDepth
,
2964 srcImage
->InternalFormat
,
2965 srcImage
->TexFormat
)) {
2966 /* All done. We either ran out of memory or we would go beyond the
2967 * last valid level of an immutable texture if we continued.
2972 /* limit minification to src level */
2973 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
2975 /* Set to draw into the current dstLevel */
2976 if (target
== GL_TEXTURE_1D
) {
2977 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2978 GL_COLOR_ATTACHMENT0_EXT
,
2983 else if (target
== GL_TEXTURE_3D
) {
2984 GLint zoffset
= 0; /* XXX unfinished */
2985 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2986 GL_COLOR_ATTACHMENT0_EXT
,
2993 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2994 GL_COLOR_ATTACHMENT0_EXT
,
3000 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
3003 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
3004 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
3009 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
3010 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
3012 /* setup viewport */
3013 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
3015 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3018 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3019 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
3022 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&& srgbBufferSave
) {
3023 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_TRUE
);
3026 _mesa_lock_texture(ctx
, texObj
); /* relock */
3028 _mesa_meta_end(ctx
);
3030 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
3031 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
3032 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3033 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
3034 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
3035 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
3036 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, wrapRSave
);
3038 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
3043 * Determine the GL data type to use for the temporary image read with
3044 * ReadPixels() and passed to Tex[Sub]Image().
3047 get_temp_image_type(struct gl_context
*ctx
, GLenum baseFormat
)
3049 switch (baseFormat
) {
3054 case GL_LUMINANCE_ALPHA
:
3056 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
3057 return GL_UNSIGNED_BYTE
;
3058 else if (ctx
->DrawBuffer
->Visual
.redBits
<= 16)
3059 return GL_UNSIGNED_SHORT
;
3062 case GL_DEPTH_COMPONENT
:
3063 return GL_UNSIGNED_INT
;
3064 case GL_DEPTH_STENCIL
:
3065 return GL_UNSIGNED_INT_24_8
;
3067 _mesa_problem(ctx
, "Unexpected format in get_temp_image_type()");
3074 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
3075 * Have to be careful with locking and meta state for pixel transfer.
3078 copy_tex_sub_image(struct gl_context
*ctx
,
3079 GLuint dims
, GLenum target
, GLint level
,
3080 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3082 GLsizei width
, GLsizei height
)
3084 struct gl_texture_object
*texObj
;
3085 struct gl_texture_image
*texImage
;
3086 GLenum format
, type
;
3090 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3091 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3093 /* Choose format/type for temporary image buffer */
3094 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
3095 if (format
== GL_LUMINANCE
||
3096 format
== GL_LUMINANCE_ALPHA
||
3097 format
== GL_INTENSITY
) {
3098 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
3099 * temp image buffer because glReadPixels will do L=R+G+B which is
3100 * not what we want (should be L=R).
3105 type
= get_temp_image_type(ctx
, format
);
3106 bpp
= _mesa_bytes_per_pixel(format
, type
);
3108 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_sub_image()");
3113 * Alloc image buffer (XXX could use a PBO)
3115 buf
= malloc(width
* height
* bpp
);
3117 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
3121 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
3124 * Read image from framebuffer (disable pixel transfer ops)
3126 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
3127 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
3128 format
, type
, &ctx
->Pack
, buf
);
3129 _mesa_meta_end(ctx
);
3131 _mesa_update_state(ctx
); /* to update pixel transfer state */
3134 * Store texture data (with pixel transfer ops)
3136 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
3137 if (target
== GL_TEXTURE_1D
) {
3138 ctx
->Driver
.TexSubImage1D(ctx
, target
, level
, xoffset
,
3139 width
, format
, type
, buf
,
3140 &ctx
->Unpack
, texObj
, texImage
);
3142 else if (target
== GL_TEXTURE_3D
) {
3143 ctx
->Driver
.TexSubImage3D(ctx
, target
, level
, xoffset
, yoffset
, zoffset
,
3144 width
, height
, 1, format
, type
, buf
,
3145 &ctx
->Unpack
, texObj
, texImage
);
3148 ctx
->Driver
.TexSubImage2D(ctx
, target
, level
, xoffset
, yoffset
,
3149 width
, height
, format
, type
, buf
,
3150 &ctx
->Unpack
, texObj
, texImage
);
3152 _mesa_meta_end(ctx
);
3154 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
3161 _mesa_meta_CopyTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
3163 GLint x
, GLint y
, GLsizei width
)
3165 copy_tex_sub_image(ctx
, 1, target
, level
, xoffset
, 0, 0,
3171 _mesa_meta_CopyTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
3172 GLint xoffset
, GLint yoffset
,
3174 GLsizei width
, GLsizei height
)
3176 copy_tex_sub_image(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
3177 x
, y
, width
, height
);
3182 _mesa_meta_CopyTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
3183 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3185 GLsizei width
, GLsizei height
)
3187 copy_tex_sub_image(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
3188 x
, y
, width
, height
);
3193 * Decompress a texture image by drawing a quad with the compressed
3194 * texture and reading the pixels out of the color buffer.
3195 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
3196 * \param destFormat format, ala glReadPixels
3197 * \param destType type, ala glReadPixels
3198 * \param dest destination buffer
3199 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
3202 decompress_texture_image(struct gl_context
*ctx
,
3203 struct gl_texture_image
*texImage
,
3205 GLenum destFormat
, GLenum destType
,
3206 GLvoid
*dest
, GLint destRowLength
)
3208 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
3209 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3210 const GLint width
= texImage
->Width
;
3211 const GLint height
= texImage
->Height
;
3212 const GLenum target
= texObj
->Target
;
3215 GLfloat x
, y
, tex
[3];
3217 struct vertex verts
[4];
3218 GLuint fboDrawSave
, fboReadSave
;
3221 assert(target
== GL_TEXTURE_3D
||
3222 target
== GL_TEXTURE_2D_ARRAY
);
3225 if (target
== GL_TEXTURE_CUBE_MAP
) {
3226 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
3229 faceTarget
= target
;
3232 /* save fbo bindings (not saved by _mesa_meta_begin()) */
3233 fboDrawSave
= ctx
->DrawBuffer
->Name
;
3234 fboReadSave
= ctx
->ReadBuffer
->Name
;
3236 _mesa_meta_begin(ctx
, MESA_META_ALL
);
3238 /* Create/bind FBO/renderbuffer */
3239 if (decompress
->FBO
== 0) {
3240 _mesa_GenFramebuffersEXT(1, &decompress
->FBO
);
3241 _mesa_GenRenderbuffersEXT(1, &decompress
->RBO
);
3242 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3243 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3244 _mesa_FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT
,
3245 GL_COLOR_ATTACHMENT0_EXT
,
3246 GL_RENDERBUFFER_EXT
,
3250 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3253 /* alloc dest surface */
3254 if (width
!= decompress
->Width
|| height
!= decompress
->Height
) {
3255 _mesa_RenderbufferStorageEXT(GL_RENDERBUFFER_EXT
, GL_RGBA
,
3257 decompress
->Width
= width
;
3258 decompress
->Height
= height
;
3261 /* setup VBO data */
3262 if (decompress
->ArrayObj
== 0) {
3263 /* create vertex array object */
3264 _mesa_GenVertexArrays(1, &decompress
->ArrayObj
);
3265 _mesa_BindVertexArray(decompress
->ArrayObj
);
3267 /* create vertex array buffer */
3268 _mesa_GenBuffersARB(1, &decompress
->VBO
);
3269 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3270 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3271 NULL
, GL_DYNAMIC_DRAW_ARB
);
3273 /* setup vertex arrays */
3274 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3275 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
3276 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3277 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3280 _mesa_BindVertexArray(decompress
->ArrayObj
);
3281 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3284 setup_texture_coords(faceTarget
, slice
, width
, height
,
3290 /* setup vertex positions */
3296 verts
[2].y
= height
;
3298 verts
[3].y
= height
;
3300 /* upload new vertex data */
3301 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3303 /* setup texture state */
3304 _mesa_BindTexture(target
, texObj
->Name
);
3305 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3308 /* save texture object state */
3309 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
3310 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
3311 const GLint baseLevelSave
= texObj
->BaseLevel
;
3312 const GLint maxLevelSave
= texObj
->MaxLevel
;
3313 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
3314 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
3315 const GLenum srgbSave
= texObj
->Sampler
.sRGBDecode
;
3317 /* restrict sampling to the texture level of interest */
3318 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, texImage
->Level
);
3319 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texImage
->Level
);
3320 /* nearest filtering */
3321 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
3322 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
3324 /* No sRGB decode or encode.*/
3325 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3326 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
3327 GL_SKIP_DECODE_EXT
);
3329 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
3330 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
3333 /* render quad w/ texture into renderbuffer */
3334 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3336 /* Restore texture object state, the texture binding will
3337 * be restored by _mesa_meta_end().
3339 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
3340 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
3341 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3342 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
3343 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3345 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
3346 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
3347 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3348 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
, srgbSave
);
3352 /* read pixels from renderbuffer */
3354 GLenum baseTexFormat
= texImage
->_BaseFormat
;
3356 /* The pixel transfer state will be set to default values at this point
3357 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
3358 * turned off (as required by glGetTexImage) but we need to handle some
3359 * special cases. In particular, single-channel texture values are
3360 * returned as red and two-channel texture values are returned as
3363 if (baseTexFormat
== GL_LUMINANCE
||
3364 baseTexFormat
== GL_LUMINANCE_ALPHA
||
3365 baseTexFormat
== GL_INTENSITY
) {
3366 /* Green and blue must be zero */
3367 _mesa_PixelTransferf(GL_GREEN_SCALE
, 0.0f
);
3368 _mesa_PixelTransferf(GL_BLUE_SCALE
, 0.0f
);
3371 ctx
->Pack
.RowLength
= destRowLength
;
3372 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
3375 /* disable texture unit */
3376 _mesa_set_enable(ctx
, target
, GL_FALSE
);
3378 _mesa_meta_end(ctx
);
3380 /* restore fbo bindings */
3381 if (fboDrawSave
== fboReadSave
) {
3382 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboDrawSave
);
3385 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT
, fboDrawSave
);
3386 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT
, fboReadSave
);
3392 * This is just a wrapper around _mesa_get_tex_image() and
3393 * decompress_texture_image(). Meta functions should not be directly called
3397 _mesa_meta_GetTexImage(struct gl_context
*ctx
,
3398 GLenum format
, GLenum type
, GLvoid
*pixels
,
3399 struct gl_texture_image
*texImage
)
3401 /* We can only use the decompress-with-blit method here if the texels are
3402 * unsigned, normalized values. We could handle signed and unnormalized
3403 * with floating point renderbuffers...
3405 if (_mesa_is_format_compressed(texImage
->TexFormat
) &&
3406 _mesa_get_format_datatype(texImage
->TexFormat
)
3407 == GL_UNSIGNED_NORMALIZED
) {
3408 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3409 const GLuint slice
= 0; /* only 2D compressed textures for now */
3410 /* Need to unlock the texture here to prevent deadlock... */
3411 _mesa_unlock_texture(ctx
, texObj
);
3412 decompress_texture_image(ctx
, texImage
, slice
, format
, type
, pixels
,
3413 ctx
->Pack
.RowLength
);
3414 /* ... and relock it */
3415 _mesa_lock_texture(ctx
, texObj
);
3418 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
3424 * Meta implementation of ctx->Driver.DrawTex() in terms
3425 * of polygon rendering.
3428 _mesa_meta_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
3429 GLfloat width
, GLfloat height
)
3431 #if FEATURE_OES_draw_texture
3432 struct drawtex_state
*drawtex
= &ctx
->Meta
->DrawTex
;
3434 GLfloat x
, y
, z
, st
[MAX_TEXTURE_UNITS
][2];
3436 struct vertex verts
[4];
3439 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
3441 MESA_META_TRANSFORM
|
3443 MESA_META_VIEWPORT
));
3445 if (drawtex
->ArrayObj
== 0) {
3446 /* one-time setup */
3447 GLint active_texture
;
3449 /* create vertex array object */
3450 _mesa_GenVertexArrays(1, &drawtex
->ArrayObj
);
3451 _mesa_BindVertexArray(drawtex
->ArrayObj
);
3453 /* create vertex array buffer */
3454 _mesa_GenBuffersARB(1, &drawtex
->VBO
);
3455 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3456 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3457 NULL
, GL_DYNAMIC_DRAW_ARB
);
3459 /* client active texture is not part of the array object */
3460 active_texture
= ctx
->Array
.ActiveTexture
;
3462 /* setup vertex arrays */
3463 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3464 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3465 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3466 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ i
);
3467 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(st
[i
]));
3468 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3471 /* restore client active texture */
3472 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ active_texture
);
3475 _mesa_BindVertexArray(drawtex
->ArrayObj
);
3476 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3479 /* vertex positions, texcoords */
3481 const GLfloat x1
= x
+ width
;
3482 const GLfloat y1
= y
+ height
;
3484 z
= CLAMP(z
, 0.0, 1.0);
3503 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3504 const struct gl_texture_object
*texObj
;
3505 const struct gl_texture_image
*texImage
;
3506 GLfloat s
, t
, s1
, t1
;
3509 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
3511 for (j
= 0; j
< 4; j
++) {
3512 verts
[j
].st
[i
][0] = 0.0f
;
3513 verts
[j
].st
[i
][1] = 0.0f
;
3518 texObj
= ctx
->Texture
.Unit
[i
]._Current
;
3519 texImage
= texObj
->Image
[0][texObj
->BaseLevel
];
3520 tw
= texImage
->Width2
;
3521 th
= texImage
->Height2
;
3523 s
= (GLfloat
) texObj
->CropRect
[0] / tw
;
3524 t
= (GLfloat
) texObj
->CropRect
[1] / th
;
3525 s1
= (GLfloat
) (texObj
->CropRect
[0] + texObj
->CropRect
[2]) / tw
;
3526 t1
= (GLfloat
) (texObj
->CropRect
[1] + texObj
->CropRect
[3]) / th
;
3528 verts
[0].st
[i
][0] = s
;
3529 verts
[0].st
[i
][1] = t
;
3531 verts
[1].st
[i
][0] = s1
;
3532 verts
[1].st
[i
][1] = t
;
3534 verts
[2].st
[i
][0] = s1
;
3535 verts
[2].st
[i
][1] = t1
;
3537 verts
[3].st
[i
][0] = s
;
3538 verts
[3].st
[i
][1] = t1
;
3541 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3544 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3546 _mesa_meta_end(ctx
);
3547 #endif /* FEATURE_OES_draw_texture */