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 * Convert Z from a normalized value in the range [0, 1] to an object-space
989 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
990 * default/identity ortho projection results in the original Z value.
991 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
992 * value comes from the clear value or raster position.
994 static INLINE GLfloat
995 invert_z(GLfloat normZ
)
997 GLfloat objZ
= 1.0 - 2.0 * normZ
;
1003 * One-time init for a temp_texture object.
1004 * Choose tex target, compute max tex size, etc.
1007 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1009 /* prefer texture rectangle */
1010 if (ctx
->Extensions
.NV_texture_rectangle
) {
1011 tex
->Target
= GL_TEXTURE_RECTANGLE
;
1012 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
1013 tex
->NPOT
= GL_TRUE
;
1016 /* use 2D texture, NPOT if possible */
1017 tex
->Target
= GL_TEXTURE_2D
;
1018 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1019 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
1021 tex
->MinSize
= 16; /* 16 x 16 at least */
1022 assert(tex
->MaxSize
> 0);
1024 _mesa_GenTextures(1, &tex
->TexObj
);
1029 * Return pointer to temp_texture info for non-bitmap ops.
1030 * This does some one-time init if needed.
1032 static struct temp_texture
*
1033 get_temp_texture(struct gl_context
*ctx
)
1035 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
1038 init_temp_texture(ctx
, tex
);
1046 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1047 * We use a separate texture for bitmaps to reduce texture
1048 * allocation/deallocation.
1050 static struct temp_texture
*
1051 get_bitmap_temp_texture(struct gl_context
*ctx
)
1053 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
1056 init_temp_texture(ctx
, tex
);
1064 * Compute the width/height of texture needed to draw an image of the
1065 * given size. Return a flag indicating whether the current texture
1066 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1067 * allocated (glTexImage2D).
1068 * Also, compute s/t texcoords for drawing.
1070 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1073 alloc_texture(struct temp_texture
*tex
,
1074 GLsizei width
, GLsizei height
, GLenum intFormat
)
1076 GLboolean newTex
= GL_FALSE
;
1078 ASSERT(width
<= tex
->MaxSize
);
1079 ASSERT(height
<= tex
->MaxSize
);
1081 if (width
> tex
->Width
||
1082 height
> tex
->Height
||
1083 intFormat
!= tex
->IntFormat
) {
1084 /* alloc new texture (larger or different format) */
1087 /* use non-power of two size */
1088 tex
->Width
= MAX2(tex
->MinSize
, width
);
1089 tex
->Height
= MAX2(tex
->MinSize
, height
);
1092 /* find power of two size */
1094 w
= h
= tex
->MinSize
;
1103 tex
->IntFormat
= intFormat
;
1108 /* compute texcoords */
1109 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1110 tex
->Sright
= (GLfloat
) width
;
1111 tex
->Ttop
= (GLfloat
) height
;
1114 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1115 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1123 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1126 setup_copypix_texture(struct temp_texture
*tex
,
1128 GLint srcX
, GLint srcY
,
1129 GLsizei width
, GLsizei height
, GLenum intFormat
,
1132 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1133 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1134 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1135 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1137 /* copy framebuffer image to texture */
1139 /* create new tex image */
1140 if (tex
->Width
== width
&& tex
->Height
== height
) {
1141 /* create new tex with framebuffer data */
1142 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1143 srcX
, srcY
, width
, height
, 0);
1146 /* create empty texture */
1147 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1148 tex
->Width
, tex
->Height
, 0,
1149 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1151 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1152 0, 0, srcX
, srcY
, width
, height
);
1156 /* replace existing tex image */
1157 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1158 0, 0, srcX
, srcY
, width
, height
);
1164 * Setup/load texture for glDrawPixels.
1167 setup_drawpix_texture(struct gl_context
*ctx
,
1168 struct temp_texture
*tex
,
1170 GLenum texIntFormat
,
1171 GLsizei width
, GLsizei height
,
1172 GLenum format
, GLenum type
,
1173 const GLvoid
*pixels
)
1175 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1176 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1177 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1178 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1180 /* copy pixel data to texture */
1182 /* create new tex image */
1183 if (tex
->Width
== width
&& tex
->Height
== height
) {
1184 /* create new tex and load image data */
1185 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1186 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1189 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1191 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1192 ctx
->Unpack
.BufferObj
);
1193 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1194 /* create empty texture */
1195 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1196 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1197 if (save_unpack_obj
!= NULL
)
1198 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
,
1199 save_unpack_obj
->Name
);
1201 _mesa_TexSubImage2D(tex
->Target
, 0,
1202 0, 0, width
, height
, format
, type
, pixels
);
1206 /* replace existing tex image */
1207 _mesa_TexSubImage2D(tex
->Target
, 0,
1208 0, 0, width
, height
, format
, type
, pixels
);
1215 * One-time init for drawing depth pixels.
1218 init_blit_depth_pixels(struct gl_context
*ctx
)
1220 static const char *program
=
1222 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1225 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1226 struct temp_texture
*tex
= get_temp_texture(ctx
);
1227 const char *texTarget
;
1229 assert(blit
->DepthFP
== 0);
1231 /* replace %s with "RECT" or "2D" */
1232 assert(strlen(program
) + 4 < sizeof(program2
));
1233 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1237 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1239 _mesa_GenPrograms(1, &blit
->DepthFP
);
1240 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1241 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1242 strlen(program2
), (const GLubyte
*) program2
);
1247 * Try to do a glBlitFramebuffer using no-copy texturing.
1248 * We can do this when the src renderbuffer is actually a texture.
1249 * But if the src buffer == dst buffer we cannot do this.
1251 * \return new buffer mask indicating the buffers left to blit using the
1255 blitframebuffer_texture(struct gl_context
*ctx
,
1256 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1257 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1258 GLbitfield mask
, GLenum filter
)
1260 if (mask
& GL_COLOR_BUFFER_BIT
) {
1261 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1262 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1263 const struct gl_renderbuffer_attachment
*drawAtt
=
1264 &drawFb
->Attachment
[drawFb
->_ColorDrawBufferIndexes
[0]];
1265 const struct gl_renderbuffer_attachment
*readAtt
=
1266 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1268 if (readAtt
&& readAtt
->Texture
) {
1269 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1270 const GLuint srcLevel
= readAtt
->TextureLevel
;
1271 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
1272 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
1273 const GLint baseLevelSave
= texObj
->BaseLevel
;
1274 const GLint maxLevelSave
= texObj
->MaxLevel
;
1275 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
1276 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
1277 const GLenum srgbSave
= texObj
->Sampler
.sRGBDecode
;
1278 const GLenum fbo_srgb_save
= ctx
->Color
.sRGBEnabled
;
1279 const GLenum target
= texObj
->Target
;
1281 if (drawAtt
->Texture
== readAtt
->Texture
) {
1282 /* Can't use same texture as both the source and dest. We need
1283 * to handle overlapping blits and besides, some hw may not
1289 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1290 /* Can't handle other texture types at this time */
1295 printf("Blit from texture!\n");
1296 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1297 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1300 /* Prepare src texture state */
1301 _mesa_BindTexture(target
, texObj
->Name
);
1302 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
1303 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
1304 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1305 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1306 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1308 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1309 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1311 /* Always do our blits with no sRGB decode or encode.*/
1312 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1313 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
1314 GL_SKIP_DECODE_EXT
);
1316 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
1317 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
1320 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1321 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1323 /* Prepare vertex data (the VBO was previously created and bound) */
1328 struct vertex verts
[4];
1329 GLfloat s0
, t0
, s1
, t1
;
1331 if (target
== GL_TEXTURE_2D
) {
1332 const struct gl_texture_image
*texImage
1333 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1334 s0
= srcX0
/ (float) texImage
->Width
;
1335 s1
= srcX1
/ (float) texImage
->Width
;
1336 t0
= srcY0
/ (float) texImage
->Height
;
1337 t1
= srcY1
/ (float) texImage
->Height
;
1340 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1347 verts
[0].x
= (GLfloat
) dstX0
;
1348 verts
[0].y
= (GLfloat
) dstY0
;
1349 verts
[1].x
= (GLfloat
) dstX1
;
1350 verts
[1].y
= (GLfloat
) dstY0
;
1351 verts
[2].x
= (GLfloat
) dstX1
;
1352 verts
[2].y
= (GLfloat
) dstY1
;
1353 verts
[3].x
= (GLfloat
) dstX0
;
1354 verts
[3].y
= (GLfloat
) dstY1
;
1365 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1368 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1370 /* Restore texture object state, the texture binding will
1371 * be restored by _mesa_meta_end().
1373 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
1374 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
1375 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1376 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1377 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1379 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
1380 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
1381 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1382 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
, srgbSave
);
1384 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&& fbo_srgb_save
) {
1385 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_TRUE
);
1388 /* Done with color buffer */
1389 mask
&= ~GL_COLOR_BUFFER_BIT
;
1398 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1399 * of texture mapping and polygon rendering.
1402 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1403 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1404 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1405 GLbitfield mask
, GLenum filter
)
1407 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1408 struct temp_texture
*tex
= get_temp_texture(ctx
);
1409 const GLsizei maxTexSize
= tex
->MaxSize
;
1410 const GLint srcX
= MIN2(srcX0
, srcX1
);
1411 const GLint srcY
= MIN2(srcY0
, srcY1
);
1412 const GLint srcW
= abs(srcX1
- srcX0
);
1413 const GLint srcH
= abs(srcY1
- srcY0
);
1414 const GLboolean srcFlipX
= srcX1
< srcX0
;
1415 const GLboolean srcFlipY
= srcY1
< srcY0
;
1419 struct vertex verts
[4];
1422 if (srcW
> maxTexSize
|| srcH
> maxTexSize
) {
1423 /* XXX avoid this fallback */
1424 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1425 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1441 /* only scissor effects blit so save/clear all other relevant state */
1442 _mesa_meta_begin(ctx
, ~MESA_META_SCISSOR
);
1444 if (blit
->ArrayObj
== 0) {
1445 /* one-time setup */
1447 /* create vertex array object */
1448 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1449 _mesa_BindVertexArray(blit
->ArrayObj
);
1451 /* create vertex array buffer */
1452 _mesa_GenBuffersARB(1, &blit
->VBO
);
1453 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1454 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1455 NULL
, GL_DYNAMIC_DRAW_ARB
);
1457 /* setup vertex arrays */
1458 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1459 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1460 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1461 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1464 _mesa_BindVertexArray(blit
->ArrayObj
);
1465 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1468 /* Try faster, direct texture approach first */
1469 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1470 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1472 _mesa_meta_end(ctx
);
1476 /* Continue with "normal" approach which involves copying the src rect
1477 * into a temporary texture and is "blitted" by drawing a textured quad.
1480 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1482 /* vertex positions/texcoords (after texture allocation!) */
1484 verts
[0].x
= (GLfloat
) dstX0
;
1485 verts
[0].y
= (GLfloat
) dstY0
;
1486 verts
[1].x
= (GLfloat
) dstX1
;
1487 verts
[1].y
= (GLfloat
) dstY0
;
1488 verts
[2].x
= (GLfloat
) dstX1
;
1489 verts
[2].y
= (GLfloat
) dstY1
;
1490 verts
[3].x
= (GLfloat
) dstX0
;
1491 verts
[3].y
= (GLfloat
) dstY1
;
1495 verts
[1].s
= tex
->Sright
;
1497 verts
[2].s
= tex
->Sright
;
1498 verts
[2].t
= tex
->Ttop
;
1500 verts
[3].t
= tex
->Ttop
;
1502 /* upload new vertex data */
1503 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1506 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1508 if (mask
& GL_COLOR_BUFFER_BIT
) {
1509 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1511 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1512 mask
&= ~GL_COLOR_BUFFER_BIT
;
1515 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1516 GLuint
*tmp
= (GLuint
*) malloc(srcW
* srcH
* sizeof(GLuint
));
1519 init_blit_depth_pixels(ctx
);
1521 /* maybe change tex format here */
1522 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1524 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1525 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1527 setup_drawpix_texture(ctx
, tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1528 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1530 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1531 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1532 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1533 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1534 _mesa_DepthFunc(GL_ALWAYS
);
1535 _mesa_DepthMask(GL_TRUE
);
1537 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1538 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1544 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1545 /* XXX can't easily do stencil */
1548 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1550 _mesa_meta_end(ctx
);
1553 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1554 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1560 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1563 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1565 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1567 GLfloat x
, y
, z
, r
, g
, b
, a
;
1569 struct vertex verts
[4];
1570 /* save all state but scissor, pixel pack/unpack */
1571 GLbitfield metaSave
= (MESA_META_ALL
-
1573 MESA_META_PIXEL_STORE
-
1574 MESA_META_CONDITIONAL_RENDER
);
1575 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1577 if (buffers
& BUFFER_BITS_COLOR
) {
1578 /* if clearing color buffers, don't save/restore colormask */
1579 metaSave
-= MESA_META_COLOR_MASK
;
1582 _mesa_meta_begin(ctx
, metaSave
);
1584 if (clear
->ArrayObj
== 0) {
1585 /* one-time setup */
1587 /* create vertex array object */
1588 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1589 _mesa_BindVertexArray(clear
->ArrayObj
);
1591 /* create vertex array buffer */
1592 _mesa_GenBuffersARB(1, &clear
->VBO
);
1593 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1595 /* setup vertex arrays */
1596 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1597 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1598 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1599 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1602 _mesa_BindVertexArray(clear
->ArrayObj
);
1603 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1606 /* GL_COLOR_BUFFER_BIT */
1607 if (buffers
& BUFFER_BITS_COLOR
) {
1608 /* leave colormask, glDrawBuffer state as-is */
1610 /* Clears never have the color clamped. */
1611 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1614 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1615 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1618 /* GL_DEPTH_BUFFER_BIT */
1619 if (buffers
& BUFFER_BIT_DEPTH
) {
1620 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1621 _mesa_DepthFunc(GL_ALWAYS
);
1622 _mesa_DepthMask(GL_TRUE
);
1625 assert(!ctx
->Depth
.Test
);
1628 /* GL_STENCIL_BUFFER_BIT */
1629 if (buffers
& BUFFER_BIT_STENCIL
) {
1630 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1631 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1632 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1633 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1634 ctx
->Stencil
.Clear
& stencilMax
,
1635 ctx
->Stencil
.WriteMask
[0]);
1638 assert(!ctx
->Stencil
.Enabled
);
1641 /* vertex positions/colors */
1643 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1644 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1645 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1646 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1647 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1664 for (i
= 0; i
< 4; i
++) {
1665 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
1666 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
1667 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
1668 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
1671 /* upload new vertex data */
1672 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1673 GL_DYNAMIC_DRAW_ARB
);
1677 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1679 _mesa_meta_end(ctx
);
1683 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
1685 const char *vs_source
=
1686 "attribute vec4 position;\n"
1689 " gl_Position = position;\n"
1691 const char *fs_source
=
1692 "uniform vec4 color;\n"
1695 " gl_FragColor = color;\n"
1697 const char *vs_int_source
=
1699 "attribute vec4 position;\n"
1702 " gl_Position = position;\n"
1704 const char *fs_int_source
=
1706 "uniform ivec4 color;\n"
1707 "out ivec4 out_color;\n"
1711 " out_color = color;\n"
1715 if (clear
->ArrayObj
!= 0)
1718 /* create vertex array object */
1719 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1720 _mesa_BindVertexArray(clear
->ArrayObj
);
1722 /* create vertex array buffer */
1723 _mesa_GenBuffersARB(1, &clear
->VBO
);
1724 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1726 /* setup vertex arrays */
1727 _mesa_VertexAttribPointerARB(0, 3, GL_FLOAT
, GL_FALSE
, 0, (void *)0);
1728 _mesa_EnableVertexAttribArrayARB(0);
1730 vs
= _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER
);
1731 _mesa_ShaderSourceARB(vs
, 1, &vs_source
, NULL
);
1732 _mesa_CompileShaderARB(vs
);
1734 fs
= _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER
);
1735 _mesa_ShaderSourceARB(fs
, 1, &fs_source
, NULL
);
1736 _mesa_CompileShaderARB(fs
);
1738 clear
->ShaderProg
= _mesa_CreateProgramObjectARB();
1739 _mesa_AttachShader(clear
->ShaderProg
, fs
);
1740 _mesa_AttachShader(clear
->ShaderProg
, vs
);
1741 _mesa_BindAttribLocationARB(clear
->ShaderProg
, 0, "position");
1742 _mesa_LinkProgramARB(clear
->ShaderProg
);
1744 clear
->ColorLocation
= _mesa_GetUniformLocationARB(clear
->ShaderProg
,
1747 if (ctx
->Const
.GLSLVersion
>= 130) {
1748 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_int_source
);
1749 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_int_source
);
1751 clear
->IntegerShaderProg
= _mesa_CreateProgramObjectARB();
1752 _mesa_AttachShader(clear
->IntegerShaderProg
, fs
);
1753 _mesa_AttachShader(clear
->IntegerShaderProg
, vs
);
1754 _mesa_BindAttribLocationARB(clear
->IntegerShaderProg
, 0, "position");
1756 /* Note that user-defined out attributes get automatically assigned
1757 * locations starting from 0, so we don't need to explicitly
1758 * BindFragDataLocation to 0.
1761 link_program_with_debug(ctx
, clear
->IntegerShaderProg
);
1763 clear
->IntegerColorLocation
=
1764 _mesa_GetUniformLocationARB(clear
->IntegerShaderProg
, "color");
1769 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1772 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1774 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1775 GLbitfield metaSave
;
1776 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1777 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1778 const float x0
= ((float)fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
1779 const float y0
= ((float)fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
1780 const float x1
= ((float)fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
1781 const float y1
= ((float)fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
1782 const float z
= -invert_z(ctx
->Depth
.Clear
);
1787 metaSave
= (MESA_META_ALPHA_TEST
|
1789 MESA_META_DEPTH_TEST
|
1790 MESA_META_RASTERIZATION
|
1792 MESA_META_STENCIL_TEST
|
1794 MESA_META_VIEWPORT
|
1796 MESA_META_CLAMP_FRAGMENT_COLOR
);
1798 if (!(buffers
& BUFFER_BITS_COLOR
)) {
1799 /* We'll use colormask to disable color writes. Otherwise,
1800 * respect color mask
1802 metaSave
|= MESA_META_COLOR_MASK
;
1805 _mesa_meta_begin(ctx
, metaSave
);
1807 meta_glsl_clear_init(ctx
, clear
);
1809 if (fb
->_IntegerColor
) {
1810 _mesa_UseProgramObjectARB(clear
->IntegerShaderProg
);
1811 _mesa_Uniform4ivARB(clear
->IntegerColorLocation
, 1,
1812 ctx
->Color
.ClearColor
.i
);
1814 _mesa_UseProgramObjectARB(clear
->ShaderProg
);
1815 _mesa_Uniform4fvARB(clear
->ColorLocation
, 1,
1816 ctx
->Color
.ClearColor
.f
);
1819 _mesa_BindVertexArray(clear
->ArrayObj
);
1820 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1822 /* GL_COLOR_BUFFER_BIT */
1823 if (buffers
& BUFFER_BITS_COLOR
) {
1824 /* leave colormask, glDrawBuffer state as-is */
1826 /* Clears never have the color clamped. */
1827 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1830 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1831 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1834 /* GL_DEPTH_BUFFER_BIT */
1835 if (buffers
& BUFFER_BIT_DEPTH
) {
1836 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1837 _mesa_DepthFunc(GL_ALWAYS
);
1838 _mesa_DepthMask(GL_TRUE
);
1841 assert(!ctx
->Depth
.Test
);
1844 /* GL_STENCIL_BUFFER_BIT */
1845 if (buffers
& BUFFER_BIT_STENCIL
) {
1846 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1847 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1848 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1849 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1850 ctx
->Stencil
.Clear
& stencilMax
,
1851 ctx
->Stencil
.WriteMask
[0]);
1854 assert(!ctx
->Stencil
.Enabled
);
1857 /* vertex positions */
1871 /* upload new vertex data */
1872 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1873 GL_DYNAMIC_DRAW_ARB
);
1876 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1878 _mesa_meta_end(ctx
);
1882 * Meta implementation of ctx->Driver.CopyPixels() in terms
1883 * of texture mapping and polygon rendering and GLSL shaders.
1886 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
1887 GLsizei width
, GLsizei height
,
1888 GLint dstX
, GLint dstY
, GLenum type
)
1890 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1891 struct temp_texture
*tex
= get_temp_texture(ctx
);
1893 GLfloat x
, y
, z
, s
, t
;
1895 struct vertex verts
[4];
1897 GLenum intFormat
= GL_RGBA
;
1899 if (type
!= GL_COLOR
||
1900 ctx
->_ImageTransferState
||
1902 width
> tex
->MaxSize
||
1903 height
> tex
->MaxSize
) {
1904 /* XXX avoid this fallback */
1905 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1909 /* Most GL state applies to glCopyPixels, but a there's a few things
1910 * we need to override:
1912 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
1915 MESA_META_TRANSFORM
|
1918 MESA_META_VIEWPORT
));
1920 if (copypix
->ArrayObj
== 0) {
1921 /* one-time setup */
1923 /* create vertex array object */
1924 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
1925 _mesa_BindVertexArray(copypix
->ArrayObj
);
1927 /* create vertex array buffer */
1928 _mesa_GenBuffersARB(1, ©pix
->VBO
);
1929 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1930 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1931 NULL
, GL_DYNAMIC_DRAW_ARB
);
1933 /* setup vertex arrays */
1934 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1935 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1936 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1937 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1940 _mesa_BindVertexArray(copypix
->ArrayObj
);
1941 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1944 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
1946 /* vertex positions, texcoords (after texture allocation!) */
1948 const GLfloat dstX0
= (GLfloat
) dstX
;
1949 const GLfloat dstY0
= (GLfloat
) dstY
;
1950 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1951 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1952 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1962 verts
[1].s
= tex
->Sright
;
1967 verts
[2].s
= tex
->Sright
;
1968 verts
[2].t
= tex
->Ttop
;
1973 verts
[3].t
= tex
->Ttop
;
1975 /* upload new vertex data */
1976 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1979 /* Alloc/setup texture */
1980 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
1981 GL_RGBA
, GL_NEAREST
);
1983 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1985 /* draw textured quad */
1986 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1988 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1990 _mesa_meta_end(ctx
);
1996 * When the glDrawPixels() image size is greater than the max rectangle
1997 * texture size we use this function to break the glDrawPixels() image
1998 * into tiles which fit into the max texture size.
2001 tiled_draw_pixels(struct gl_context
*ctx
,
2003 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2004 GLenum format
, GLenum type
,
2005 const struct gl_pixelstore_attrib
*unpack
,
2006 const GLvoid
*pixels
)
2008 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
2011 if (tileUnpack
.RowLength
== 0)
2012 tileUnpack
.RowLength
= width
;
2014 for (i
= 0; i
< width
; i
+= tileSize
) {
2015 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
2016 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
2018 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
2020 for (j
= 0; j
< height
; j
+= tileSize
) {
2021 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
2022 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
2024 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
2026 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
2027 format
, type
, &tileUnpack
, pixels
);
2034 * One-time init for drawing stencil pixels.
2037 init_draw_stencil_pixels(struct gl_context
*ctx
)
2039 /* This program is run eight times, once for each stencil bit.
2040 * The stencil values to draw are found in an 8-bit alpha texture.
2041 * We read the texture/stencil value and test if bit 'b' is set.
2042 * If the bit is not set, use KIL to kill the fragment.
2043 * Finally, we use the stencil test to update the stencil buffer.
2045 * The basic algorithm for checking if a bit is set is:
2046 * if (is_odd(value / (1 << bit)))
2047 * result is one (or non-zero).
2050 * The program parameter contains three values:
2051 * parm.x = 255 / (1 << bit)
2055 static const char *program
=
2057 "PARAM parm = program.local[0]; \n"
2059 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
2060 "# t = t * 255 / bit \n"
2061 "MUL t.x, t.a, parm.x; \n"
2064 "SUB t.x, t.x, t.y; \n"
2066 "MUL t.x, t.x, parm.y; \n"
2067 "# t = fract(t.x) \n"
2068 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2069 "# t.x = (t.x == 0 ? 1 : 0) \n"
2070 "SGE t.x, -t.x, parm.z; \n"
2072 "# for debug only \n"
2073 "#MOV result.color, t.x; \n"
2075 char program2
[1000];
2076 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2077 struct temp_texture
*tex
= get_temp_texture(ctx
);
2078 const char *texTarget
;
2080 assert(drawpix
->StencilFP
== 0);
2082 /* replace %s with "RECT" or "2D" */
2083 assert(strlen(program
) + 4 < sizeof(program2
));
2084 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2088 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2090 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
2091 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2092 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2093 strlen(program2
), (const GLubyte
*) program2
);
2098 * One-time init for drawing depth pixels.
2101 init_draw_depth_pixels(struct gl_context
*ctx
)
2103 static const char *program
=
2105 "PARAM color = program.local[0]; \n"
2106 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2107 "MOV result.color, color; \n"
2110 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2111 struct temp_texture
*tex
= get_temp_texture(ctx
);
2112 const char *texTarget
;
2114 assert(drawpix
->DepthFP
== 0);
2116 /* replace %s with "RECT" or "2D" */
2117 assert(strlen(program
) + 4 < sizeof(program2
));
2118 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2122 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2124 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
2125 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2126 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2127 strlen(program2
), (const GLubyte
*) program2
);
2132 * Meta implementation of ctx->Driver.DrawPixels() in terms
2133 * of texture mapping and polygon rendering.
2136 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2137 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2138 GLenum format
, GLenum type
,
2139 const struct gl_pixelstore_attrib
*unpack
,
2140 const GLvoid
*pixels
)
2142 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2143 struct temp_texture
*tex
= get_temp_texture(ctx
);
2144 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2145 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2147 GLfloat x
, y
, z
, s
, t
;
2149 struct vertex verts
[4];
2150 GLenum texIntFormat
;
2151 GLboolean fallback
, newTex
;
2152 GLbitfield metaExtraSave
= 0x0;
2156 * Determine if we can do the glDrawPixels with texture mapping.
2158 fallback
= GL_FALSE
;
2159 if (ctx
->_ImageTransferState
||
2164 if (_mesa_is_color_format(format
)) {
2165 /* use more compact format when possible */
2166 /* XXX disable special case for GL_LUMINANCE for now to work around
2167 * apparent i965 driver bug (see bug #23670).
2169 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2170 texIntFormat
= format
;
2172 texIntFormat
= GL_RGBA
;
2174 /* If we're not supposed to clamp the resulting color, then just
2175 * promote our texture to fully float. We could do better by
2176 * just going for the matching set of channels, in floating
2179 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2180 ctx
->Extensions
.ARB_texture_float
)
2181 texIntFormat
= GL_RGBA32F
;
2183 else if (_mesa_is_stencil_format(format
)) {
2184 if (ctx
->Extensions
.ARB_fragment_program
&&
2185 ctx
->Pixel
.IndexShift
== 0 &&
2186 ctx
->Pixel
.IndexOffset
== 0 &&
2187 type
== GL_UNSIGNED_BYTE
) {
2188 /* We'll store stencil as alpha. This only works for GLubyte
2189 * image data because of how incoming values are mapped to alpha
2192 texIntFormat
= GL_ALPHA
;
2193 metaExtraSave
= (MESA_META_COLOR_MASK
|
2194 MESA_META_DEPTH_TEST
|
2196 MESA_META_STENCIL_TEST
);
2202 else if (_mesa_is_depth_format(format
)) {
2203 if (ctx
->Extensions
.ARB_depth_texture
&&
2204 ctx
->Extensions
.ARB_fragment_program
) {
2205 texIntFormat
= GL_DEPTH_COMPONENT
;
2206 metaExtraSave
= (MESA_META_SHADER
);
2217 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2218 format
, type
, unpack
, pixels
);
2223 * Check image size against max texture size, draw as tiles if needed.
2225 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2226 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2227 format
, type
, unpack
, pixels
);
2231 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2232 * but a there's a few things we need to override:
2234 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2237 MESA_META_TRANSFORM
|
2240 MESA_META_VIEWPORT
|
2241 MESA_META_CLAMP_FRAGMENT_COLOR
|
2244 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2246 /* vertex positions, texcoords (after texture allocation!) */
2248 const GLfloat x0
= (GLfloat
) x
;
2249 const GLfloat y0
= (GLfloat
) y
;
2250 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2251 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2252 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2262 verts
[1].s
= tex
->Sright
;
2267 verts
[2].s
= tex
->Sright
;
2268 verts
[2].t
= tex
->Ttop
;
2273 verts
[3].t
= tex
->Ttop
;
2276 if (drawpix
->ArrayObj
== 0) {
2277 /* one-time setup: create vertex array object */
2278 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
2280 _mesa_BindVertexArray(drawpix
->ArrayObj
);
2282 /* create vertex array buffer */
2283 _mesa_GenBuffersARB(1, &vbo
);
2284 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
2285 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2286 verts
, GL_DYNAMIC_DRAW_ARB
);
2288 /* setup vertex arrays */
2289 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2290 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2291 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2292 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2294 /* set given unpack params */
2295 ctx
->Unpack
= *unpack
;
2297 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2299 if (_mesa_is_stencil_format(format
)) {
2300 /* Drawing stencil */
2303 if (!drawpix
->StencilFP
)
2304 init_draw_stencil_pixels(ctx
);
2306 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2307 GL_ALPHA
, type
, pixels
);
2309 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2311 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2313 /* set all stencil bits to 0 */
2314 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2315 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2316 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2318 /* set stencil bits to 1 where needed */
2319 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2321 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2322 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2324 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2325 const GLuint mask
= 1 << bit
;
2326 if (mask
& origStencilMask
) {
2327 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2328 _mesa_StencilMask(mask
);
2330 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2331 255.0 / mask
, 0.5, 0.0, 0.0);
2333 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2337 else if (_mesa_is_depth_format(format
)) {
2339 if (!drawpix
->DepthFP
)
2340 init_draw_depth_pixels(ctx
);
2342 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2343 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2345 /* polygon color = current raster color */
2346 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2347 ctx
->Current
.RasterColor
);
2349 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2350 format
, type
, pixels
);
2352 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2356 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2357 format
, type
, pixels
);
2358 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2361 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2363 _mesa_DeleteBuffersARB(1, &vbo
);
2365 /* restore unpack params */
2366 ctx
->Unpack
= unpackSave
;
2368 _mesa_meta_end(ctx
);
2372 alpha_test_raster_color(struct gl_context
*ctx
)
2374 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2375 GLfloat ref
= ctx
->Color
.AlphaRef
;
2377 switch (ctx
->Color
.AlphaFunc
) {
2383 return alpha
== ref
;
2385 return alpha
<= ref
;
2389 return alpha
!= ref
;
2391 return alpha
>= ref
;
2401 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2402 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2403 * tracker would improve performance a lot.
2406 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2407 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2408 const struct gl_pixelstore_attrib
*unpack
,
2409 const GLubyte
*bitmap1
)
2411 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2412 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2413 const GLenum texIntFormat
= GL_ALPHA
;
2414 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2417 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2419 struct vertex verts
[4];
2424 * Check if swrast fallback is needed.
2426 if (ctx
->_ImageTransferState
||
2427 ctx
->FragmentProgram
._Enabled
||
2429 ctx
->Texture
._EnabledUnits
||
2430 width
> tex
->MaxSize
||
2431 height
> tex
->MaxSize
) {
2432 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2436 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2439 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2440 * but a there's a few things we need to override:
2442 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
2443 MESA_META_PIXEL_STORE
|
2444 MESA_META_RASTERIZATION
|
2447 MESA_META_TRANSFORM
|
2450 MESA_META_VIEWPORT
));
2452 if (bitmap
->ArrayObj
== 0) {
2453 /* one-time setup */
2455 /* create vertex array object */
2456 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2457 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2459 /* create vertex array buffer */
2460 _mesa_GenBuffersARB(1, &bitmap
->VBO
);
2461 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2462 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2463 NULL
, GL_DYNAMIC_DRAW_ARB
);
2465 /* setup vertex arrays */
2466 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2467 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2468 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2469 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2470 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2471 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2474 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2475 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2478 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2480 /* vertex positions, texcoords, colors (after texture allocation!) */
2482 const GLfloat x0
= (GLfloat
) x
;
2483 const GLfloat y0
= (GLfloat
) y
;
2484 const GLfloat x1
= (GLfloat
) (x
+ width
);
2485 const GLfloat y1
= (GLfloat
) (y
+ height
);
2486 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2497 verts
[1].s
= tex
->Sright
;
2502 verts
[2].s
= tex
->Sright
;
2503 verts
[2].t
= tex
->Ttop
;
2508 verts
[3].t
= tex
->Ttop
;
2510 for (i
= 0; i
< 4; i
++) {
2511 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2512 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2513 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2514 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2517 /* upload new vertex data */
2518 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2521 /* choose different foreground/background alpha values */
2522 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2523 bg
= (fg
> 127 ? 0 : 255);
2525 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2527 _mesa_meta_end(ctx
);
2531 bitmap8
= (GLubyte
*) malloc(width
* height
);
2533 memset(bitmap8
, bg
, width
* height
);
2534 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2535 bitmap8
, width
, fg
);
2537 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2539 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2540 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2542 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2543 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2545 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2547 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2552 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2554 _mesa_meta_end(ctx
);
2559 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2560 * software fallback. The fallback path will require that the texture
2561 * images are mapped.
2562 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2565 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
2566 struct gl_texture_object
*texObj
)
2568 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2569 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2570 struct gl_texture_image
*baseImage
;
2574 /* check for fallbacks */
2575 if (!ctx
->Extensions
.EXT_framebuffer_object
||
2576 target
== GL_TEXTURE_3D
||
2577 target
== GL_TEXTURE_1D_ARRAY
||
2578 target
== GL_TEXTURE_2D_ARRAY
) {
2582 srcLevel
= texObj
->BaseLevel
;
2583 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2584 if (!baseImage
|| _mesa_is_format_compressed(baseImage
->TexFormat
)) {
2588 if (_mesa_get_format_color_encoding(baseImage
->TexFormat
) == GL_SRGB
&&
2589 !ctx
->Extensions
.EXT_texture_sRGB_decode
) {
2590 /* The texture format is sRGB but we can't turn off sRGB->linear
2591 * texture sample conversion. So we won't be able to generate the
2592 * right colors when rendering. Need to use a fallback.
2598 * Test that we can actually render in the texture's format.
2601 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2602 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2604 if (target
== GL_TEXTURE_1D
) {
2605 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2606 GL_COLOR_ATTACHMENT0_EXT
,
2607 target
, texObj
->Name
, srcLevel
);
2610 /* other work is needed to enable 3D mipmap generation */
2611 else if (target
== GL_TEXTURE_3D
) {
2613 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2614 GL_COLOR_ATTACHMENT0_EXT
,
2615 target
, texObj
->Name
, srcLevel
, zoffset
);
2620 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2621 GL_COLOR_ATTACHMENT0_EXT
,
2622 target
, texObj
->Name
, srcLevel
);
2625 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2627 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2629 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2638 * Compute the texture coordinates for the four vertices of a quad for
2639 * drawing a 2D texture image or slice of a cube/3D texture.
2640 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2641 * \param slice slice of a 1D/2D array texture or 3D texture
2642 * \param width width of the texture image
2643 * \param height height of the texture image
2644 * \param coords0/1/2/3 returns the computed texcoords
2647 setup_texture_coords(GLenum faceTarget
,
2656 static const GLfloat st
[4][2] = {
2657 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2662 switch (faceTarget
) {
2666 case GL_TEXTURE_2D_ARRAY
:
2667 if (faceTarget
== GL_TEXTURE_3D
)
2669 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
2673 coords0
[0] = 0.0F
; /* s */
2674 coords0
[1] = 0.0F
; /* t */
2675 coords0
[2] = r
; /* r */
2686 case GL_TEXTURE_RECTANGLE_ARB
:
2687 coords0
[0] = 0.0F
; /* s */
2688 coords0
[1] = 0.0F
; /* t */
2689 coords0
[2] = 0.0F
; /* r */
2694 coords2
[1] = height
;
2697 coords3
[1] = height
;
2700 case GL_TEXTURE_1D_ARRAY
:
2701 coords0
[0] = 0.0F
; /* s */
2702 coords0
[1] = slice
; /* t */
2703 coords0
[2] = 0.0F
; /* r */
2715 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2716 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2717 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2718 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2719 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2720 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2721 /* loop over quad verts */
2722 for (i
= 0; i
< 4; i
++) {
2723 /* Compute sc = +/-scale and tc = +/-scale.
2724 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2725 * though that can still sometimes happen with this scale factor...
2727 const GLfloat scale
= 0.9999f
;
2728 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2729 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2749 switch (faceTarget
) {
2750 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2755 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2760 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2765 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2770 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2775 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2786 assert(0 && "unexpected target in meta setup_texture_coords()");
2792 * Called via ctx->Driver.GenerateMipmap()
2793 * Note: We don't yet support 3D textures, 1D/2D array textures or texture
2797 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
2798 struct gl_texture_object
*texObj
)
2800 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2802 GLfloat x
, y
, tex
[3];
2804 struct vertex verts
[4];
2805 const GLuint baseLevel
= texObj
->BaseLevel
;
2806 const GLuint maxLevel
= texObj
->MaxLevel
;
2807 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
2808 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
2809 const GLint maxLevelSave
= texObj
->MaxLevel
;
2810 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
2811 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
2812 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
2813 const GLenum wrapRSave
= texObj
->Sampler
.WrapR
;
2814 const GLenum srgbDecodeSave
= texObj
->Sampler
.sRGBDecode
;
2815 const GLenum srgbBufferSave
= ctx
->Color
.sRGBEnabled
;
2816 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2817 const GLuint original_active_unit
= ctx
->Texture
.CurrentUnit
;
2820 const GLuint border
= 0;
2821 const GLint slice
= 0;
2823 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
2824 _mesa_generate_mipmap(ctx
, target
, texObj
);
2828 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
2829 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
2830 faceTarget
= target
;
2831 target
= GL_TEXTURE_CUBE_MAP
;
2834 faceTarget
= target
;
2837 _mesa_meta_begin(ctx
, MESA_META_ALL
);
2839 if (original_active_unit
!= 0)
2840 _mesa_BindTexture(target
, texObj
->Name
);
2842 if (mipmap
->ArrayObj
== 0) {
2843 /* one-time setup */
2845 /* create vertex array object */
2846 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
2847 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
2849 /* create vertex array buffer */
2850 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
2851 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2852 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2853 NULL
, GL_DYNAMIC_DRAW_ARB
);
2855 /* setup vertex arrays */
2856 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2857 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
2858 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2859 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2862 _mesa_BindVertexArray(mipmap
->ArrayObj
);
2863 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2867 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2869 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2871 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
2872 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
2873 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
2874 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2875 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2876 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
2878 /* We don't want to encode or decode sRGB values; treat them as linear */
2879 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
2880 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
2881 GL_SKIP_DECODE_EXT
);
2883 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
2884 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
2887 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2889 /* setup texcoords (XXX what about border?) */
2890 setup_texture_coords(faceTarget
,
2891 0.0, 0.0, /* width, height never used here */
2898 /* setup vertex positions */
2908 /* upload new vertex data */
2909 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2911 /* setup projection matrix */
2912 _mesa_MatrixMode(GL_PROJECTION
);
2913 _mesa_LoadIdentity();
2914 _mesa_Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
2916 /* texture is already locked, unlock now */
2917 _mesa_unlock_texture(ctx
, texObj
);
2919 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
2920 const struct gl_texture_image
*srcImage
;
2921 const GLuint srcLevel
= dstLevel
- 1;
2922 GLsizei srcWidth
, srcHeight
, srcDepth
;
2923 GLsizei dstWidth
, dstHeight
, dstDepth
;
2926 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
2927 assert(srcImage
->Border
== 0); /* XXX we can fix this */
2929 /* src size w/out border */
2930 srcWidth
= srcImage
->Width
- 2 * border
;
2931 srcHeight
= srcImage
->Height
- 2 * border
;
2932 srcDepth
= srcImage
->Depth
- 2 * border
;
2934 /* new dst size w/ border */
2935 dstWidth
= MAX2(1, srcWidth
/ 2) + 2 * border
;
2936 dstHeight
= MAX2(1, srcHeight
/ 2) + 2 * border
;
2937 dstDepth
= MAX2(1, srcDepth
/ 2) + 2 * border
;
2939 if (dstWidth
== srcImage
->Width
&&
2940 dstHeight
== srcImage
->Height
&&
2941 dstDepth
== srcImage
->Depth
) {
2946 /* Allocate storage for the destination mipmap image(s) */
2948 /* Set MaxLevel large enough to hold the new level when we allocate it */
2949 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
2951 if (!_mesa_prepare_mipmap_level(ctx
, texObj
, dstLevel
,
2952 dstWidth
, dstHeight
, dstDepth
,
2954 srcImage
->InternalFormat
,
2955 srcImage
->TexFormat
)) {
2956 /* All done. We either ran out of memory or we would go beyond the
2957 * last valid level of an immutable texture if we continued.
2962 /* limit minification to src level */
2963 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
2965 /* Set to draw into the current dstLevel */
2966 if (target
== GL_TEXTURE_1D
) {
2967 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2968 GL_COLOR_ATTACHMENT0_EXT
,
2973 else if (target
== GL_TEXTURE_3D
) {
2974 GLint zoffset
= 0; /* XXX unfinished */
2975 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2976 GL_COLOR_ATTACHMENT0_EXT
,
2983 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2984 GL_COLOR_ATTACHMENT0_EXT
,
2990 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
2993 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2994 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2999 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
3000 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
3002 /* setup viewport */
3003 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
3005 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3008 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3009 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
3012 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&& srgbBufferSave
) {
3013 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_TRUE
);
3016 _mesa_lock_texture(ctx
, texObj
); /* relock */
3018 _mesa_meta_end(ctx
);
3020 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
3021 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
3022 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3023 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
3024 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
3025 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
3026 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, wrapRSave
);
3028 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
3033 * Determine the GL data type to use for the temporary image read with
3034 * ReadPixels() and passed to Tex[Sub]Image().
3037 get_temp_image_type(struct gl_context
*ctx
, GLenum baseFormat
)
3039 switch (baseFormat
) {
3044 case GL_LUMINANCE_ALPHA
:
3046 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
3047 return GL_UNSIGNED_BYTE
;
3048 else if (ctx
->DrawBuffer
->Visual
.redBits
<= 16)
3049 return GL_UNSIGNED_SHORT
;
3052 case GL_DEPTH_COMPONENT
:
3053 return GL_UNSIGNED_INT
;
3054 case GL_DEPTH_STENCIL
:
3055 return GL_UNSIGNED_INT_24_8
;
3057 _mesa_problem(ctx
, "Unexpected format in get_temp_image_type()");
3064 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
3065 * Have to be careful with locking and meta state for pixel transfer.
3068 copy_tex_sub_image(struct gl_context
*ctx
,
3069 GLuint dims
, GLenum target
, GLint level
,
3070 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3072 GLsizei width
, GLsizei height
)
3074 struct gl_texture_object
*texObj
;
3075 struct gl_texture_image
*texImage
;
3076 GLenum format
, type
;
3080 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3081 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3083 /* Choose format/type for temporary image buffer */
3084 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
3085 if (format
== GL_LUMINANCE
||
3086 format
== GL_LUMINANCE_ALPHA
||
3087 format
== GL_INTENSITY
) {
3088 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
3089 * temp image buffer because glReadPixels will do L=R+G+B which is
3090 * not what we want (should be L=R).
3095 type
= get_temp_image_type(ctx
, format
);
3096 bpp
= _mesa_bytes_per_pixel(format
, type
);
3098 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_sub_image()");
3103 * Alloc image buffer (XXX could use a PBO)
3105 buf
= malloc(width
* height
* bpp
);
3107 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
3111 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
3114 * Read image from framebuffer (disable pixel transfer ops)
3116 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
3117 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
3118 format
, type
, &ctx
->Pack
, buf
);
3119 _mesa_meta_end(ctx
);
3121 _mesa_update_state(ctx
); /* to update pixel transfer state */
3124 * Store texture data (with pixel transfer ops)
3126 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
3127 if (target
== GL_TEXTURE_1D
) {
3128 ctx
->Driver
.TexSubImage1D(ctx
, target
, level
, xoffset
,
3129 width
, format
, type
, buf
,
3130 &ctx
->Unpack
, texObj
, texImage
);
3132 else if (target
== GL_TEXTURE_3D
) {
3133 ctx
->Driver
.TexSubImage3D(ctx
, target
, level
, xoffset
, yoffset
, zoffset
,
3134 width
, height
, 1, format
, type
, buf
,
3135 &ctx
->Unpack
, texObj
, texImage
);
3138 ctx
->Driver
.TexSubImage2D(ctx
, target
, level
, xoffset
, yoffset
,
3139 width
, height
, format
, type
, buf
,
3140 &ctx
->Unpack
, texObj
, texImage
);
3142 _mesa_meta_end(ctx
);
3144 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
3151 _mesa_meta_CopyTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
3153 GLint x
, GLint y
, GLsizei width
)
3155 copy_tex_sub_image(ctx
, 1, target
, level
, xoffset
, 0, 0,
3161 _mesa_meta_CopyTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
3162 GLint xoffset
, GLint yoffset
,
3164 GLsizei width
, GLsizei height
)
3166 copy_tex_sub_image(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
3167 x
, y
, width
, height
);
3172 _mesa_meta_CopyTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
3173 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3175 GLsizei width
, GLsizei height
)
3177 copy_tex_sub_image(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
3178 x
, y
, width
, height
);
3183 * Decompress a texture image by drawing a quad with the compressed
3184 * texture and reading the pixels out of the color buffer.
3185 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
3186 * \param destFormat format, ala glReadPixels
3187 * \param destType type, ala glReadPixels
3188 * \param dest destination buffer
3189 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
3192 decompress_texture_image(struct gl_context
*ctx
,
3193 struct gl_texture_image
*texImage
,
3195 GLenum destFormat
, GLenum destType
,
3196 GLvoid
*dest
, GLint destRowLength
)
3198 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
3199 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3200 const GLint width
= texImage
->Width
;
3201 const GLint height
= texImage
->Height
;
3202 const GLenum target
= texObj
->Target
;
3205 GLfloat x
, y
, tex
[3];
3207 struct vertex verts
[4];
3208 GLuint fboDrawSave
, fboReadSave
;
3211 assert(target
== GL_TEXTURE_3D
||
3212 target
== GL_TEXTURE_2D_ARRAY
);
3215 if (target
== GL_TEXTURE_CUBE_MAP
) {
3216 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
3219 faceTarget
= target
;
3222 /* save fbo bindings (not saved by _mesa_meta_begin()) */
3223 fboDrawSave
= ctx
->DrawBuffer
->Name
;
3224 fboReadSave
= ctx
->ReadBuffer
->Name
;
3226 _mesa_meta_begin(ctx
, MESA_META_ALL
);
3228 /* Create/bind FBO/renderbuffer */
3229 if (decompress
->FBO
== 0) {
3230 _mesa_GenFramebuffersEXT(1, &decompress
->FBO
);
3231 _mesa_GenRenderbuffersEXT(1, &decompress
->RBO
);
3232 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3233 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3234 _mesa_FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT
,
3235 GL_COLOR_ATTACHMENT0_EXT
,
3236 GL_RENDERBUFFER_EXT
,
3240 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3243 /* alloc dest surface */
3244 if (width
!= decompress
->Width
|| height
!= decompress
->Height
) {
3245 _mesa_RenderbufferStorageEXT(GL_RENDERBUFFER_EXT
, GL_RGBA
,
3247 decompress
->Width
= width
;
3248 decompress
->Height
= height
;
3251 /* setup VBO data */
3252 if (decompress
->ArrayObj
== 0) {
3253 /* create vertex array object */
3254 _mesa_GenVertexArrays(1, &decompress
->ArrayObj
);
3255 _mesa_BindVertexArray(decompress
->ArrayObj
);
3257 /* create vertex array buffer */
3258 _mesa_GenBuffersARB(1, &decompress
->VBO
);
3259 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3260 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3261 NULL
, GL_DYNAMIC_DRAW_ARB
);
3263 /* setup vertex arrays */
3264 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3265 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
3266 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3267 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3270 _mesa_BindVertexArray(decompress
->ArrayObj
);
3271 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3274 setup_texture_coords(faceTarget
, slice
, width
, height
,
3280 /* setup vertex positions */
3286 verts
[2].y
= height
;
3288 verts
[3].y
= height
;
3290 /* upload new vertex data */
3291 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3293 /* setup texture state */
3294 _mesa_BindTexture(target
, texObj
->Name
);
3295 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3298 /* save texture object state */
3299 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
3300 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
3301 const GLint baseLevelSave
= texObj
->BaseLevel
;
3302 const GLint maxLevelSave
= texObj
->MaxLevel
;
3303 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
3304 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
3305 const GLenum srgbSave
= texObj
->Sampler
.sRGBDecode
;
3307 /* restrict sampling to the texture level of interest */
3308 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, texImage
->Level
);
3309 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texImage
->Level
);
3310 /* nearest filtering */
3311 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
3312 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
3314 /* No sRGB decode or encode.*/
3315 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3316 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
3317 GL_SKIP_DECODE_EXT
);
3319 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
3320 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
3323 /* render quad w/ texture into renderbuffer */
3324 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3326 /* Restore texture object state, the texture binding will
3327 * be restored by _mesa_meta_end().
3329 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
3330 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
3331 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3332 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
3333 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3335 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
3336 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
3337 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3338 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
, srgbSave
);
3342 /* read pixels from renderbuffer */
3344 GLenum baseTexFormat
= texImage
->_BaseFormat
;
3346 /* The pixel transfer state will be set to default values at this point
3347 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
3348 * turned off (as required by glGetTexImage) but we need to handle some
3349 * special cases. In particular, single-channel texture values are
3350 * returned as red and two-channel texture values are returned as
3353 if (baseTexFormat
== GL_LUMINANCE
||
3354 baseTexFormat
== GL_LUMINANCE_ALPHA
||
3355 baseTexFormat
== GL_INTENSITY
) {
3356 /* Green and blue must be zero */
3357 _mesa_PixelTransferf(GL_GREEN_SCALE
, 0.0f
);
3358 _mesa_PixelTransferf(GL_BLUE_SCALE
, 0.0f
);
3361 ctx
->Pack
.RowLength
= destRowLength
;
3362 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
3365 /* disable texture unit */
3366 _mesa_set_enable(ctx
, target
, GL_FALSE
);
3368 _mesa_meta_end(ctx
);
3370 /* restore fbo bindings */
3371 if (fboDrawSave
== fboReadSave
) {
3372 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboDrawSave
);
3375 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT
, fboDrawSave
);
3376 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT
, fboReadSave
);
3382 * This is just a wrapper around _mesa_get_tex_image() and
3383 * decompress_texture_image(). Meta functions should not be directly called
3387 _mesa_meta_GetTexImage(struct gl_context
*ctx
,
3388 GLenum format
, GLenum type
, GLvoid
*pixels
,
3389 struct gl_texture_image
*texImage
)
3391 /* We can only use the decompress-with-blit method here if the texels are
3392 * unsigned, normalized values. We could handle signed and unnormalized
3393 * with floating point renderbuffers...
3395 if (_mesa_is_format_compressed(texImage
->TexFormat
) &&
3396 _mesa_get_format_datatype(texImage
->TexFormat
)
3397 == GL_UNSIGNED_NORMALIZED
) {
3398 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3399 const GLuint slice
= 0; /* only 2D compressed textures for now */
3400 /* Need to unlock the texture here to prevent deadlock... */
3401 _mesa_unlock_texture(ctx
, texObj
);
3402 decompress_texture_image(ctx
, texImage
, slice
, format
, type
, pixels
,
3403 ctx
->Pack
.RowLength
);
3404 /* ... and relock it */
3405 _mesa_lock_texture(ctx
, texObj
);
3408 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
3414 * Meta implementation of ctx->Driver.DrawTex() in terms
3415 * of polygon rendering.
3418 _mesa_meta_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
3419 GLfloat width
, GLfloat height
)
3421 #if FEATURE_OES_draw_texture
3422 struct drawtex_state
*drawtex
= &ctx
->Meta
->DrawTex
;
3424 GLfloat x
, y
, z
, st
[MAX_TEXTURE_UNITS
][2];
3426 struct vertex verts
[4];
3429 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
3431 MESA_META_TRANSFORM
|
3433 MESA_META_VIEWPORT
));
3435 if (drawtex
->ArrayObj
== 0) {
3436 /* one-time setup */
3437 GLint active_texture
;
3439 /* create vertex array object */
3440 _mesa_GenVertexArrays(1, &drawtex
->ArrayObj
);
3441 _mesa_BindVertexArray(drawtex
->ArrayObj
);
3443 /* create vertex array buffer */
3444 _mesa_GenBuffersARB(1, &drawtex
->VBO
);
3445 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3446 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3447 NULL
, GL_DYNAMIC_DRAW_ARB
);
3449 /* client active texture is not part of the array object */
3450 active_texture
= ctx
->Array
.ActiveTexture
;
3452 /* setup vertex arrays */
3453 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3454 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3455 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3456 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ i
);
3457 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(st
[i
]));
3458 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3461 /* restore client active texture */
3462 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ active_texture
);
3465 _mesa_BindVertexArray(drawtex
->ArrayObj
);
3466 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3469 /* vertex positions, texcoords */
3471 const GLfloat x1
= x
+ width
;
3472 const GLfloat y1
= y
+ height
;
3474 z
= CLAMP(z
, 0.0, 1.0);
3493 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3494 const struct gl_texture_object
*texObj
;
3495 const struct gl_texture_image
*texImage
;
3496 GLfloat s
, t
, s1
, t1
;
3499 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
3501 for (j
= 0; j
< 4; j
++) {
3502 verts
[j
].st
[i
][0] = 0.0f
;
3503 verts
[j
].st
[i
][1] = 0.0f
;
3508 texObj
= ctx
->Texture
.Unit
[i
]._Current
;
3509 texImage
= texObj
->Image
[0][texObj
->BaseLevel
];
3510 tw
= texImage
->Width2
;
3511 th
= texImage
->Height2
;
3513 s
= (GLfloat
) texObj
->CropRect
[0] / tw
;
3514 t
= (GLfloat
) texObj
->CropRect
[1] / th
;
3515 s1
= (GLfloat
) (texObj
->CropRect
[0] + texObj
->CropRect
[2]) / tw
;
3516 t1
= (GLfloat
) (texObj
->CropRect
[1] + texObj
->CropRect
[3]) / th
;
3518 verts
[0].st
[i
][0] = s
;
3519 verts
[0].st
[i
][1] = t
;
3521 verts
[1].st
[i
][0] = s1
;
3522 verts
[1].st
[i
][1] = t
;
3524 verts
[2].st
[i
][0] = s1
;
3525 verts
[2].st
[i
][1] = t1
;
3527 verts
[3].st
[i
][0] = s
;
3528 verts
[3].st
[i
][1] = t1
;
3531 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3534 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3536 _mesa_meta_end(ctx
);
3537 #endif /* FEATURE_OES_draw_texture */