2 * Mesa 3-D graphics library
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * Meta operations. Some GL operations can be expressed in terms of
27 * other GL operations. For example, glBlitFramebuffer() can be done
28 * with texture mapping and glClear() can be done with polygon rendering.
34 #include "main/glheader.h"
35 #include "main/mtypes.h"
36 #include "main/imports.h"
37 #include "main/arbprogram.h"
38 #include "main/arrayobj.h"
39 #include "main/blend.h"
40 #include "main/bufferobj.h"
41 #include "main/buffers.h"
42 #include "main/colortab.h"
43 #include "main/condrender.h"
44 #include "main/depth.h"
45 #include "main/enable.h"
46 #include "main/fbobject.h"
47 #include "main/feedback.h"
48 #include "main/formats.h"
49 #include "main/glformats.h"
50 #include "main/image.h"
51 #include "main/macros.h"
52 #include "main/matrix.h"
53 #include "main/mipmap.h"
54 #include "main/pixel.h"
56 #include "main/polygon.h"
57 #include "main/readpix.h"
58 #include "main/scissor.h"
59 #include "main/shaderapi.h"
60 #include "main/shaderobj.h"
61 #include "main/state.h"
62 #include "main/stencil.h"
63 #include "main/texobj.h"
64 #include "main/texenv.h"
65 #include "main/texgetimage.h"
66 #include "main/teximage.h"
67 #include "main/texparam.h"
68 #include "main/texstate.h"
69 #include "main/transformfeedback.h"
70 #include "main/uniforms.h"
71 #include "main/varray.h"
72 #include "main/viewport.h"
73 #include "main/samplerobj.h"
74 #include "program/program.h"
75 #include "swrast/swrast.h"
76 #include "drivers/common/meta.h"
77 #include "main/enums.h"
78 #include "main/glformats.h"
79 #include "../glsl/ralloc.h"
82 /** Return offset in bytes of the field within a vertex struct */
83 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
86 * State which we may save/restore across meta ops.
87 * XXX this may be incomplete...
91 GLbitfield SavedState
; /**< bitmask of MESA_META_* flags */
93 /** MESA_META_ALPHA_TEST */
94 GLboolean AlphaEnabled
;
98 /** MESA_META_BLEND */
99 GLbitfield BlendEnabled
;
100 GLboolean ColorLogicOpEnabled
;
102 /** MESA_META_COLOR_MASK */
103 GLubyte ColorMask
[MAX_DRAW_BUFFERS
][4];
105 /** MESA_META_DEPTH_TEST */
106 struct gl_depthbuffer_attrib Depth
;
111 /** MESA_META_PIXEL_STORE */
112 struct gl_pixelstore_attrib Pack
, Unpack
;
114 /** MESA_META_PIXEL_TRANSFER */
115 GLfloat RedBias
, RedScale
;
116 GLfloat GreenBias
, GreenScale
;
117 GLfloat BlueBias
, BlueScale
;
118 GLfloat AlphaBias
, AlphaScale
;
119 GLfloat DepthBias
, DepthScale
;
120 GLboolean MapColorFlag
;
122 /** MESA_META_RASTERIZATION */
123 GLenum FrontPolygonMode
, BackPolygonMode
;
124 GLboolean PolygonOffset
;
125 GLboolean PolygonSmooth
;
126 GLboolean PolygonStipple
;
127 GLboolean PolygonCull
;
129 /** MESA_META_SCISSOR */
130 struct gl_scissor_attrib Scissor
;
132 /** MESA_META_SHADER */
133 GLboolean VertexProgramEnabled
;
134 struct gl_vertex_program
*VertexProgram
;
135 GLboolean FragmentProgramEnabled
;
136 struct gl_fragment_program
*FragmentProgram
;
137 struct gl_shader_program
*VertexShader
;
138 struct gl_shader_program
*GeometryShader
;
139 struct gl_shader_program
*FragmentShader
;
140 struct gl_shader_program
*ActiveShader
;
142 /** MESA_META_STENCIL_TEST */
143 struct gl_stencil_attrib Stencil
;
145 /** MESA_META_TRANSFORM */
147 GLfloat ModelviewMatrix
[16];
148 GLfloat ProjectionMatrix
[16];
149 GLfloat TextureMatrix
[16];
151 /** MESA_META_CLIP */
152 GLbitfield ClipPlanesEnabled
;
154 /** MESA_META_TEXTURE */
156 GLuint ClientActiveUnit
;
157 /** for unit[0] only */
158 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
159 /** mask of TEXTURE_2D_BIT, etc */
160 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
161 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
162 GLuint EnvMode
; /* unit[0] only */
164 /** MESA_META_VERTEX */
165 struct gl_array_object
*ArrayObj
;
166 struct gl_buffer_object
*ArrayBufferObj
;
168 /** MESA_META_VIEWPORT */
169 GLint ViewportX
, ViewportY
, ViewportW
, ViewportH
;
170 GLclampd DepthNear
, DepthFar
;
172 /** MESA_META_CLAMP_FRAGMENT_COLOR */
173 GLenum ClampFragmentColor
;
175 /** MESA_META_CLAMP_VERTEX_COLOR */
176 GLenum ClampVertexColor
;
178 /** MESA_META_CONDITIONAL_RENDER */
179 struct gl_query_object
*CondRenderQuery
;
180 GLenum CondRenderMode
;
182 /** MESA_META_SELECT_FEEDBACK */
184 struct gl_selection Select
;
185 struct gl_feedback Feedback
;
187 /** MESA_META_MULTISAMPLE */
188 GLboolean MultisampleEnabled
;
190 /** MESA_META_FRAMEBUFFER_SRGB */
191 GLboolean sRGBEnabled
;
193 /** Miscellaneous (always disabled) */
195 GLboolean RasterDiscard
;
196 GLboolean TransformFeedbackNeedsResume
;
200 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
201 * This is currently shared by all the meta ops. But we could create a
202 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
207 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
208 GLsizei MinSize
; /**< Min texture size to allocate */
209 GLsizei MaxSize
; /**< Max possible texture size */
210 GLboolean NPOT
; /**< Non-power of two size OK? */
211 GLsizei Width
, Height
; /**< Current texture size */
213 GLfloat Sright
, Ttop
; /**< right, top texcoords */
218 * State for glBlitFramebufer()
229 * State for glClear()
238 GLuint IntegerShaderProg
;
239 GLint IntegerColorLocation
;
244 * State for glCopyPixels()
254 * State for glDrawPixels()
260 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
261 GLuint DepthFP
; /**< Fragment program for drawing depth images */
266 * State for glBitmap()
272 struct temp_texture Tex
; /**< separate texture from other meta ops */
276 * State for GLSL texture sampler which is used to generate fragment
277 * shader in _mesa_meta_generate_mipmap().
279 struct glsl_sampler
{
282 const char *texcoords
;
287 * State for _mesa_meta_generate_mipmap()
289 struct gen_mipmap_state
296 GLuint IntegerShaderProg
;
297 struct glsl_sampler sampler_1d
;
298 struct glsl_sampler sampler_2d
;
299 struct glsl_sampler sampler_3d
;
300 struct glsl_sampler sampler_cubemap
;
301 struct glsl_sampler sampler_1d_array
;
302 struct glsl_sampler sampler_2d_array
;
306 * State for texture decompression
308 struct decompress_state
311 GLuint VBO
, FBO
, RBO
, Sampler
;
316 * State for glDrawTex()
324 #define MAX_META_OPS_DEPTH 8
326 * All per-context meta state.
330 /** Stack of state saved during meta-ops */
331 struct save_state Save
[MAX_META_OPS_DEPTH
];
332 /** Save stack depth */
333 GLuint SaveStackDepth
;
335 struct temp_texture TempTex
;
337 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
338 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
339 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
340 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
341 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
342 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
343 struct decompress_state Decompress
; /**< For texture decompression */
344 struct drawtex_state DrawTex
; /**< For _mesa_meta_DrawTex() */
347 static void meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
);
348 static void cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
);
349 static void meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
);
350 static void meta_glsl_generate_mipmap_cleanup(struct gl_context
*ctx
,
351 struct gen_mipmap_state
*mipmap
);
354 compile_shader_with_debug(struct gl_context
*ctx
, GLenum target
, const GLcharARB
*source
)
360 shader
= _mesa_CreateShaderObjectARB(target
);
361 _mesa_ShaderSourceARB(shader
, 1, &source
, NULL
);
362 _mesa_CompileShaderARB(shader
);
364 _mesa_GetShaderiv(shader
, GL_COMPILE_STATUS
, &ok
);
368 _mesa_GetShaderiv(shader
, GL_INFO_LOG_LENGTH
, &size
);
370 _mesa_DeleteObjectARB(shader
);
376 _mesa_DeleteObjectARB(shader
);
380 _mesa_GetProgramInfoLog(shader
, size
, NULL
, info
);
382 "meta program compile failed:\n%s\n"
387 _mesa_DeleteObjectARB(shader
);
393 link_program_with_debug(struct gl_context
*ctx
, GLuint program
)
398 _mesa_LinkProgramARB(program
);
400 _mesa_GetProgramiv(program
, GL_LINK_STATUS
, &ok
);
404 _mesa_GetProgramiv(program
, GL_INFO_LOG_LENGTH
, &size
);
412 _mesa_GetProgramInfoLog(program
, size
, NULL
, info
);
413 _mesa_problem(ctx
, "meta program link failed:\n%s", info
);
421 * Initialize meta-ops for a context.
422 * To be called once during context creation.
425 _mesa_meta_init(struct gl_context
*ctx
)
429 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
434 * Free context meta-op state.
435 * To be called once during context destruction.
438 _mesa_meta_free(struct gl_context
*ctx
)
440 GET_CURRENT_CONTEXT(old_context
);
441 _mesa_make_current(ctx
, NULL
, NULL
);
442 meta_glsl_blit_cleanup(ctx
, &ctx
->Meta
->Blit
);
443 meta_glsl_clear_cleanup(ctx
, &ctx
->Meta
->Clear
);
444 meta_glsl_generate_mipmap_cleanup(ctx
, &ctx
->Meta
->Mipmap
);
445 cleanup_temp_texture(ctx
, &ctx
->Meta
->TempTex
);
447 _mesa_make_current(old_context
, old_context
->WinSysDrawBuffer
, old_context
->WinSysReadBuffer
);
449 _mesa_make_current(NULL
, NULL
, NULL
);
456 * Enter meta state. This is like a light-weight version of glPushAttrib
457 * but it also resets most GL state back to default values.
459 * \param state bitmask of MESA_META_* flags indicating which attribute groups
460 * to save and reset to their defaults
463 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
465 struct save_state
*save
;
467 /* hope MAX_META_OPS_DEPTH is large enough */
468 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
470 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
471 memset(save
, 0, sizeof(*save
));
472 save
->SavedState
= state
;
474 /* Pausing transform feedback needs to be done early, or else we won't be
475 * able to change other state.
477 save
->TransformFeedbackNeedsResume
=
478 ctx
->TransformFeedback
.CurrentObject
->Active
&&
479 !ctx
->TransformFeedback
.CurrentObject
->Paused
;
480 if (save
->TransformFeedbackNeedsResume
)
481 _mesa_PauseTransformFeedback();
483 if (state
& MESA_META_ALPHA_TEST
) {
484 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
485 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
486 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
487 if (ctx
->Color
.AlphaEnabled
)
488 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
491 if (state
& MESA_META_BLEND
) {
492 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
493 if (ctx
->Color
.BlendEnabled
) {
494 if (ctx
->Extensions
.EXT_draw_buffers2
) {
496 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
497 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
501 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
504 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
505 if (ctx
->Color
.ColorLogicOpEnabled
)
506 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
509 if (state
& MESA_META_COLOR_MASK
) {
510 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
511 sizeof(ctx
->Color
.ColorMask
));
512 if (!ctx
->Color
.ColorMask
[0][0] ||
513 !ctx
->Color
.ColorMask
[0][1] ||
514 !ctx
->Color
.ColorMask
[0][2] ||
515 !ctx
->Color
.ColorMask
[0][3])
516 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
519 if (state
& MESA_META_DEPTH_TEST
) {
520 save
->Depth
= ctx
->Depth
; /* struct copy */
522 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
525 if ((state
& MESA_META_FOG
)
526 && ctx
->API
!= API_OPENGL_CORE
527 && ctx
->API
!= API_OPENGLES2
) {
528 save
->Fog
= ctx
->Fog
.Enabled
;
529 if (ctx
->Fog
.Enabled
)
530 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
533 if (state
& MESA_META_PIXEL_STORE
) {
534 save
->Pack
= ctx
->Pack
;
535 save
->Unpack
= ctx
->Unpack
;
536 ctx
->Pack
= ctx
->DefaultPacking
;
537 ctx
->Unpack
= ctx
->DefaultPacking
;
540 if (state
& MESA_META_PIXEL_TRANSFER
) {
541 save
->RedScale
= ctx
->Pixel
.RedScale
;
542 save
->RedBias
= ctx
->Pixel
.RedBias
;
543 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
544 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
545 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
546 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
547 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
548 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
549 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
550 ctx
->Pixel
.RedScale
= 1.0F
;
551 ctx
->Pixel
.RedBias
= 0.0F
;
552 ctx
->Pixel
.GreenScale
= 1.0F
;
553 ctx
->Pixel
.GreenBias
= 0.0F
;
554 ctx
->Pixel
.BlueScale
= 1.0F
;
555 ctx
->Pixel
.BlueBias
= 0.0F
;
556 ctx
->Pixel
.AlphaScale
= 1.0F
;
557 ctx
->Pixel
.AlphaBias
= 0.0F
;
558 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
560 ctx
->NewState
|=_NEW_PIXEL
;
563 if (state
& MESA_META_RASTERIZATION
) {
564 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
565 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
566 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
567 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
568 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
569 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
570 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
571 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
572 if (ctx
->API
== API_OPENGL
) {
573 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
574 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
576 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
579 if (state
& MESA_META_SCISSOR
) {
580 save
->Scissor
= ctx
->Scissor
; /* struct copy */
581 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
584 if (state
& MESA_META_SHADER
) {
585 if (ctx
->API
== API_OPENGL
&& ctx
->Extensions
.ARB_vertex_program
) {
586 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
587 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
588 ctx
->VertexProgram
.Current
);
589 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
592 if (ctx
->API
== API_OPENGL
&& ctx
->Extensions
.ARB_fragment_program
) {
593 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
594 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
595 ctx
->FragmentProgram
.Current
);
596 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
599 if (ctx
->Extensions
.ARB_shader_objects
) {
600 _mesa_reference_shader_program(ctx
, &save
->VertexShader
,
601 ctx
->Shader
.CurrentVertexProgram
);
602 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
,
603 ctx
->Shader
.CurrentGeometryProgram
);
604 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
,
605 ctx
->Shader
.CurrentFragmentProgram
);
606 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
607 ctx
->Shader
.ActiveProgram
);
609 _mesa_UseProgramObjectARB(0);
613 if (state
& MESA_META_STENCIL_TEST
) {
614 save
->Stencil
= ctx
->Stencil
; /* struct copy */
615 if (ctx
->Stencil
.Enabled
)
616 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
617 /* NOTE: other stencil state not reset */
620 if (state
& MESA_META_TEXTURE
) {
623 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
624 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
625 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
627 /* Disable all texture units */
628 if (ctx
->API
== API_OPENGL
|| ctx
->API
== API_OPENGLES
) {
629 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
630 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
631 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
632 if (ctx
->Texture
.Unit
[u
].Enabled
||
633 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
634 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
635 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
636 if (ctx
->Extensions
.ARB_texture_cube_map
)
637 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
638 if (ctx
->Extensions
.OES_EGL_image_external
)
639 _mesa_set_enable(ctx
, GL_TEXTURE_EXTERNAL_OES
, GL_FALSE
);
641 if (ctx
->API
== API_OPENGL
) {
642 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
643 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
644 if (ctx
->Extensions
.NV_texture_rectangle
)
645 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
646 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
647 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
648 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
649 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
651 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_STR_OES
, GL_FALSE
);
657 /* save current texture objects for unit[0] only */
658 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
659 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
660 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
663 /* set defaults for unit[0] */
664 _mesa_ActiveTextureARB(GL_TEXTURE0
);
665 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
666 if (ctx
->API
== API_OPENGL
|| ctx
->API
== API_OPENGLES
) {
667 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
671 if (state
& MESA_META_TRANSFORM
) {
672 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
673 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
674 16 * sizeof(GLfloat
));
675 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
676 16 * sizeof(GLfloat
));
677 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
678 16 * sizeof(GLfloat
));
679 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
680 /* set 1:1 vertex:pixel coordinate transform */
681 _mesa_ActiveTextureARB(GL_TEXTURE0
);
682 _mesa_MatrixMode(GL_TEXTURE
);
683 _mesa_LoadIdentity();
684 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
685 _mesa_MatrixMode(GL_MODELVIEW
);
686 _mesa_LoadIdentity();
687 _mesa_MatrixMode(GL_PROJECTION
);
688 _mesa_LoadIdentity();
689 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
690 0.0, ctx
->DrawBuffer
->Height
,
694 if (state
& MESA_META_CLIP
) {
695 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
696 if (ctx
->Transform
.ClipPlanesEnabled
) {
698 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
699 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
704 if (state
& MESA_META_VERTEX
) {
705 /* save vertex array object state */
706 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
707 ctx
->Array
.ArrayObj
);
708 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
709 ctx
->Array
.ArrayBufferObj
);
710 /* set some default state? */
713 if (state
& MESA_META_VIEWPORT
) {
714 /* save viewport state */
715 save
->ViewportX
= ctx
->Viewport
.X
;
716 save
->ViewportY
= ctx
->Viewport
.Y
;
717 save
->ViewportW
= ctx
->Viewport
.Width
;
718 save
->ViewportH
= ctx
->Viewport
.Height
;
719 /* set viewport to match window size */
720 if (ctx
->Viewport
.X
!= 0 ||
721 ctx
->Viewport
.Y
!= 0 ||
722 ctx
->Viewport
.Width
!= ctx
->DrawBuffer
->Width
||
723 ctx
->Viewport
.Height
!= ctx
->DrawBuffer
->Height
) {
724 _mesa_set_viewport(ctx
, 0, 0,
725 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
727 /* save depth range state */
728 save
->DepthNear
= ctx
->Viewport
.Near
;
729 save
->DepthFar
= ctx
->Viewport
.Far
;
730 /* set depth range to default */
731 _mesa_DepthRange(0.0, 1.0);
734 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
735 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
737 /* Generally in here we want to do clamping according to whether
738 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
739 * regardless of the internal implementation of the metaops.
741 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
)
742 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
745 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
746 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
748 /* Generally in here we never want vertex color clamping --
749 * result clamping is only dependent on fragment clamping.
751 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
754 if (state
& MESA_META_CONDITIONAL_RENDER
) {
755 save
->CondRenderQuery
= ctx
->Query
.CondRenderQuery
;
756 save
->CondRenderMode
= ctx
->Query
.CondRenderMode
;
758 if (ctx
->Query
.CondRenderQuery
)
759 _mesa_EndConditionalRender();
762 if (state
& MESA_META_SELECT_FEEDBACK
) {
763 save
->RenderMode
= ctx
->RenderMode
;
764 if (ctx
->RenderMode
== GL_SELECT
) {
765 save
->Select
= ctx
->Select
; /* struct copy */
766 _mesa_RenderMode(GL_RENDER
);
767 } else if (ctx
->RenderMode
== GL_FEEDBACK
) {
768 save
->Feedback
= ctx
->Feedback
; /* struct copy */
769 _mesa_RenderMode(GL_RENDER
);
773 if (state
& MESA_META_MULTISAMPLE
) {
774 save
->MultisampleEnabled
= ctx
->Multisample
.Enabled
;
775 if (ctx
->Multisample
.Enabled
)
776 _mesa_set_multisample(ctx
, GL_FALSE
);
779 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
780 save
->sRGBEnabled
= ctx
->Color
.sRGBEnabled
;
781 if (ctx
->Color
.sRGBEnabled
)
782 _mesa_set_framebuffer_srgb(ctx
, GL_FALSE
);
787 save
->Lighting
= ctx
->Light
.Enabled
;
788 if (ctx
->Light
.Enabled
)
789 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
790 save
->RasterDiscard
= ctx
->RasterDiscard
;
791 if (ctx
->RasterDiscard
)
792 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_FALSE
);
798 * Leave meta state. This is like a light-weight version of glPopAttrib().
801 _mesa_meta_end(struct gl_context
*ctx
)
803 struct save_state
*save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
- 1];
804 const GLbitfield state
= save
->SavedState
;
806 if (state
& MESA_META_ALPHA_TEST
) {
807 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
808 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
809 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
812 if (state
& MESA_META_BLEND
) {
813 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
814 if (ctx
->Extensions
.EXT_draw_buffers2
) {
816 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
817 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
821 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
824 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
825 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
828 if (state
& MESA_META_COLOR_MASK
) {
830 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
831 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
833 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
834 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
837 _mesa_ColorMaskIndexed(i
,
838 save
->ColorMask
[i
][0],
839 save
->ColorMask
[i
][1],
840 save
->ColorMask
[i
][2],
841 save
->ColorMask
[i
][3]);
847 if (state
& MESA_META_DEPTH_TEST
) {
848 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
849 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
850 _mesa_DepthFunc(save
->Depth
.Func
);
851 _mesa_DepthMask(save
->Depth
.Mask
);
854 if ((state
& MESA_META_FOG
)
855 && ctx
->API
!= API_OPENGL_CORE
856 && ctx
->API
!= API_OPENGLES2
) {
857 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
860 if (state
& MESA_META_PIXEL_STORE
) {
861 ctx
->Pack
= save
->Pack
;
862 ctx
->Unpack
= save
->Unpack
;
865 if (state
& MESA_META_PIXEL_TRANSFER
) {
866 ctx
->Pixel
.RedScale
= save
->RedScale
;
867 ctx
->Pixel
.RedBias
= save
->RedBias
;
868 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
869 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
870 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
871 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
872 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
873 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
874 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
876 ctx
->NewState
|=_NEW_PIXEL
;
879 if (state
& MESA_META_RASTERIZATION
) {
880 /* Core context requires that front and back mode be the same.
882 if (ctx
->API
== API_OPENGL_CORE
) {
883 _mesa_PolygonMode(GL_FRONT_AND_BACK
, save
->FrontPolygonMode
);
885 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
886 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
888 if (ctx
->API
== API_OPENGL
) {
889 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
890 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
892 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
893 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
896 if (state
& MESA_META_SCISSOR
) {
897 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
898 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
899 save
->Scissor
.Width
, save
->Scissor
.Height
);
902 if (state
& MESA_META_SHADER
) {
903 if (ctx
->API
== API_OPENGL
&& ctx
->Extensions
.ARB_vertex_program
) {
904 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
905 save
->VertexProgramEnabled
);
906 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
907 save
->VertexProgram
);
908 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
911 if (ctx
->API
== API_OPENGL
&& ctx
->Extensions
.ARB_fragment_program
) {
912 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
913 save
->FragmentProgramEnabled
);
914 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
915 save
->FragmentProgram
);
916 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
919 if (ctx
->Extensions
.ARB_vertex_shader
)
920 _mesa_use_shader_program(ctx
, GL_VERTEX_SHADER
, save
->VertexShader
);
922 if (ctx
->Extensions
.ARB_geometry_shader4
)
923 _mesa_use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
,
924 save
->GeometryShader
);
926 if (ctx
->Extensions
.ARB_fragment_shader
)
927 _mesa_use_shader_program(ctx
, GL_FRAGMENT_SHADER
,
928 save
->FragmentShader
);
930 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
933 _mesa_reference_shader_program(ctx
, &save
->VertexShader
, NULL
);
934 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
, NULL
);
935 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
, NULL
);
936 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
, NULL
);
939 if (state
& MESA_META_STENCIL_TEST
) {
940 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
942 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
943 _mesa_ClearStencil(stencil
->Clear
);
944 if (ctx
->API
== API_OPENGL
&& ctx
->Extensions
.EXT_stencil_two_side
) {
945 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
946 stencil
->TestTwoSide
);
947 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
948 ? GL_BACK
: GL_FRONT
);
951 _mesa_StencilFuncSeparate(GL_FRONT
,
952 stencil
->Function
[0],
954 stencil
->ValueMask
[0]);
955 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
956 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
957 stencil
->ZFailFunc
[0],
958 stencil
->ZPassFunc
[0]);
960 _mesa_StencilFuncSeparate(GL_BACK
,
961 stencil
->Function
[1],
963 stencil
->ValueMask
[1]);
964 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
965 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
966 stencil
->ZFailFunc
[1],
967 stencil
->ZPassFunc
[1]);
970 if (state
& MESA_META_TEXTURE
) {
973 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
975 /* restore texenv for unit[0] */
976 if (ctx
->API
== API_OPENGL
|| ctx
->API
== API_OPENGLES
) {
977 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
980 /* restore texture objects for unit[0] only */
981 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
982 if (ctx
->Texture
.Unit
[0].CurrentTex
[tgt
] != save
->CurrentTexture
[tgt
]) {
983 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
984 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
985 save
->CurrentTexture
[tgt
]);
987 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
990 /* Restore fixed function texture enables, texgen */
991 if (ctx
->API
== API_OPENGL
|| ctx
->API
== API_OPENGLES
) {
992 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
993 if (ctx
->Texture
.Unit
[u
].Enabled
!= save
->TexEnabled
[u
]) {
994 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
995 ctx
->Texture
.Unit
[u
].Enabled
= save
->TexEnabled
[u
];
998 if (ctx
->Texture
.Unit
[u
].TexGenEnabled
!= save
->TexGenEnabled
[u
]) {
999 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1000 ctx
->Texture
.Unit
[u
].TexGenEnabled
= save
->TexGenEnabled
[u
];
1005 /* restore current unit state */
1006 _mesa_ActiveTextureARB(GL_TEXTURE0
+ save
->ActiveUnit
);
1007 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ save
->ClientActiveUnit
);
1010 if (state
& MESA_META_TRANSFORM
) {
1011 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
1012 _mesa_ActiveTextureARB(GL_TEXTURE0
);
1013 _mesa_MatrixMode(GL_TEXTURE
);
1014 _mesa_LoadMatrixf(save
->TextureMatrix
);
1015 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
1017 _mesa_MatrixMode(GL_MODELVIEW
);
1018 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
1020 _mesa_MatrixMode(GL_PROJECTION
);
1021 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
1023 _mesa_MatrixMode(save
->MatrixMode
);
1026 if (state
& MESA_META_CLIP
) {
1027 if (save
->ClipPlanesEnabled
) {
1029 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
1030 if (save
->ClipPlanesEnabled
& (1 << i
)) {
1031 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
1037 if (state
& MESA_META_VERTEX
) {
1038 /* restore vertex buffer object */
1039 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
1040 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
1042 /* restore vertex array object */
1043 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
1044 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
1047 if (state
& MESA_META_VIEWPORT
) {
1048 if (save
->ViewportX
!= ctx
->Viewport
.X
||
1049 save
->ViewportY
!= ctx
->Viewport
.Y
||
1050 save
->ViewportW
!= ctx
->Viewport
.Width
||
1051 save
->ViewportH
!= ctx
->Viewport
.Height
) {
1052 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
1053 save
->ViewportW
, save
->ViewportH
);
1055 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
1058 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
1059 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
1062 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
1063 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
1066 if (state
& MESA_META_CONDITIONAL_RENDER
) {
1067 if (save
->CondRenderQuery
)
1068 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
1069 save
->CondRenderMode
);
1072 if (state
& MESA_META_SELECT_FEEDBACK
) {
1073 if (save
->RenderMode
== GL_SELECT
) {
1074 _mesa_RenderMode(GL_SELECT
);
1075 ctx
->Select
= save
->Select
;
1076 } else if (save
->RenderMode
== GL_FEEDBACK
) {
1077 _mesa_RenderMode(GL_FEEDBACK
);
1078 ctx
->Feedback
= save
->Feedback
;
1082 if (state
& MESA_META_MULTISAMPLE
) {
1083 if (ctx
->Multisample
.Enabled
!= save
->MultisampleEnabled
)
1084 _mesa_set_multisample(ctx
, save
->MultisampleEnabled
);
1087 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
1088 if (ctx
->Color
.sRGBEnabled
!= save
->sRGBEnabled
)
1089 _mesa_set_framebuffer_srgb(ctx
, save
->sRGBEnabled
);
1093 if (save
->Lighting
) {
1094 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
1096 if (save
->RasterDiscard
) {
1097 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_TRUE
);
1099 if (save
->TransformFeedbackNeedsResume
)
1100 _mesa_ResumeTransformFeedback();
1102 ctx
->Meta
->SaveStackDepth
--;
1107 * Determine whether Mesa is currently in a meta state.
1110 _mesa_meta_in_progress(struct gl_context
*ctx
)
1112 return ctx
->Meta
->SaveStackDepth
!= 0;
1117 * Convert Z from a normalized value in the range [0, 1] to an object-space
1118 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1119 * default/identity ortho projection results in the original Z value.
1120 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1121 * value comes from the clear value or raster position.
1123 static INLINE GLfloat
1124 invert_z(GLfloat normZ
)
1126 GLfloat objZ
= 1.0f
- 2.0f
* normZ
;
1132 * One-time init for a temp_texture object.
1133 * Choose tex target, compute max tex size, etc.
1136 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1138 /* prefer texture rectangle */
1139 if (ctx
->Extensions
.NV_texture_rectangle
) {
1140 tex
->Target
= GL_TEXTURE_RECTANGLE
;
1141 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
1142 tex
->NPOT
= GL_TRUE
;
1145 /* use 2D texture, NPOT if possible */
1146 tex
->Target
= GL_TEXTURE_2D
;
1147 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1148 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
1150 tex
->MinSize
= 16; /* 16 x 16 at least */
1151 assert(tex
->MaxSize
> 0);
1153 _mesa_GenTextures(1, &tex
->TexObj
);
1157 cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1161 _mesa_DeleteTextures(1, &tex
->TexObj
);
1167 * Return pointer to temp_texture info for non-bitmap ops.
1168 * This does some one-time init if needed.
1170 static struct temp_texture
*
1171 get_temp_texture(struct gl_context
*ctx
)
1173 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
1176 init_temp_texture(ctx
, tex
);
1184 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1185 * We use a separate texture for bitmaps to reduce texture
1186 * allocation/deallocation.
1188 static struct temp_texture
*
1189 get_bitmap_temp_texture(struct gl_context
*ctx
)
1191 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
1194 init_temp_texture(ctx
, tex
);
1202 * Compute the width/height of texture needed to draw an image of the
1203 * given size. Return a flag indicating whether the current texture
1204 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1205 * allocated (glTexImage2D).
1206 * Also, compute s/t texcoords for drawing.
1208 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1211 alloc_texture(struct temp_texture
*tex
,
1212 GLsizei width
, GLsizei height
, GLenum intFormat
)
1214 GLboolean newTex
= GL_FALSE
;
1216 ASSERT(width
<= tex
->MaxSize
);
1217 ASSERT(height
<= tex
->MaxSize
);
1219 if (width
> tex
->Width
||
1220 height
> tex
->Height
||
1221 intFormat
!= tex
->IntFormat
) {
1222 /* alloc new texture (larger or different format) */
1225 /* use non-power of two size */
1226 tex
->Width
= MAX2(tex
->MinSize
, width
);
1227 tex
->Height
= MAX2(tex
->MinSize
, height
);
1230 /* find power of two size */
1232 w
= h
= tex
->MinSize
;
1241 tex
->IntFormat
= intFormat
;
1246 /* compute texcoords */
1247 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1248 tex
->Sright
= (GLfloat
) width
;
1249 tex
->Ttop
= (GLfloat
) height
;
1252 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1253 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1261 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1264 setup_copypix_texture(struct temp_texture
*tex
,
1266 GLint srcX
, GLint srcY
,
1267 GLsizei width
, GLsizei height
, GLenum intFormat
,
1270 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1271 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1272 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1273 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1275 /* copy framebuffer image to texture */
1277 /* create new tex image */
1278 if (tex
->Width
== width
&& tex
->Height
== height
) {
1279 /* create new tex with framebuffer data */
1280 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1281 srcX
, srcY
, width
, height
, 0);
1284 /* create empty texture */
1285 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1286 tex
->Width
, tex
->Height
, 0,
1287 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1289 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1290 0, 0, srcX
, srcY
, width
, height
);
1294 /* replace existing tex image */
1295 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1296 0, 0, srcX
, srcY
, width
, height
);
1302 * Setup/load texture for glDrawPixels.
1305 setup_drawpix_texture(struct gl_context
*ctx
,
1306 struct temp_texture
*tex
,
1308 GLenum texIntFormat
,
1309 GLsizei width
, GLsizei height
,
1310 GLenum format
, GLenum type
,
1311 const GLvoid
*pixels
)
1313 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1314 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1315 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1316 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1318 /* copy pixel data to texture */
1320 /* create new tex image */
1321 if (tex
->Width
== width
&& tex
->Height
== height
) {
1322 /* create new tex and load image data */
1323 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1324 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1327 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1329 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1330 ctx
->Unpack
.BufferObj
);
1331 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1332 /* create empty texture */
1333 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1334 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1335 if (save_unpack_obj
!= NULL
)
1336 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
,
1337 save_unpack_obj
->Name
);
1339 _mesa_TexSubImage2D(tex
->Target
, 0,
1340 0, 0, width
, height
, format
, type
, pixels
);
1344 /* replace existing tex image */
1345 _mesa_TexSubImage2D(tex
->Target
, 0,
1346 0, 0, width
, height
, format
, type
, pixels
);
1353 * One-time init for drawing depth pixels.
1356 init_blit_depth_pixels(struct gl_context
*ctx
)
1358 static const char *program
=
1360 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1363 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1364 struct temp_texture
*tex
= get_temp_texture(ctx
);
1365 const char *texTarget
;
1367 assert(blit
->DepthFP
== 0);
1369 /* replace %s with "RECT" or "2D" */
1370 assert(strlen(program
) + 4 < sizeof(program2
));
1371 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1375 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1377 _mesa_GenPrograms(1, &blit
->DepthFP
);
1378 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1379 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1380 strlen(program2
), (const GLubyte
*) program2
);
1385 * Try to do a glBlitFramebuffer using no-copy texturing.
1386 * We can do this when the src renderbuffer is actually a texture.
1387 * But if the src buffer == dst buffer we cannot do this.
1389 * \return new buffer mask indicating the buffers left to blit using the
1393 blitframebuffer_texture(struct gl_context
*ctx
,
1394 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1395 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1396 GLbitfield mask
, GLenum filter
)
1398 if (mask
& GL_COLOR_BUFFER_BIT
) {
1399 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1400 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1401 const struct gl_renderbuffer_attachment
*drawAtt
=
1402 &drawFb
->Attachment
[drawFb
->_ColorDrawBufferIndexes
[0]];
1403 const struct gl_renderbuffer_attachment
*readAtt
=
1404 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1406 if (readAtt
&& readAtt
->Texture
) {
1407 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1408 const GLuint srcLevel
= readAtt
->TextureLevel
;
1409 const GLint baseLevelSave
= texObj
->BaseLevel
;
1410 const GLint maxLevelSave
= texObj
->MaxLevel
;
1411 const GLenum target
= texObj
->Target
;
1412 GLuint sampler
, samplerSave
=
1413 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
1414 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
1416 if (drawAtt
->Texture
== readAtt
->Texture
) {
1417 /* Can't use same texture as both the source and dest. We need
1418 * to handle overlapping blits and besides, some hw may not
1424 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1425 /* Can't handle other texture types at this time */
1429 _mesa_GenSamplers(1, &sampler
);
1430 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, sampler
);
1433 printf("Blit from texture!\n");
1434 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1435 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1438 /* Prepare src texture state */
1439 _mesa_BindTexture(target
, texObj
->Name
);
1440 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MIN_FILTER
, filter
);
1441 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MAG_FILTER
, filter
);
1442 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1443 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1444 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1446 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1447 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1449 /* Always do our blits with no sRGB decode or encode. Note that
1450 * GL_FRAMEBUFFER_SRGB has already been disabled by
1451 * _mesa_meta_begin().
1453 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1454 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
1455 GL_SKIP_DECODE_EXT
);
1458 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1459 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1461 /* Prepare vertex data (the VBO was previously created and bound) */
1466 struct vertex verts
[4];
1467 GLfloat s0
, t0
, s1
, t1
;
1469 if (target
== GL_TEXTURE_2D
) {
1470 const struct gl_texture_image
*texImage
1471 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1472 s0
= srcX0
/ (float) texImage
->Width
;
1473 s1
= srcX1
/ (float) texImage
->Width
;
1474 t0
= srcY0
/ (float) texImage
->Height
;
1475 t1
= srcY1
/ (float) texImage
->Height
;
1478 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1485 verts
[0].x
= (GLfloat
) dstX0
;
1486 verts
[0].y
= (GLfloat
) dstY0
;
1487 verts
[1].x
= (GLfloat
) dstX1
;
1488 verts
[1].y
= (GLfloat
) dstY0
;
1489 verts
[2].x
= (GLfloat
) dstX1
;
1490 verts
[2].y
= (GLfloat
) dstY1
;
1491 verts
[3].x
= (GLfloat
) dstX0
;
1492 verts
[3].y
= (GLfloat
) dstY1
;
1503 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1506 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1508 /* Restore texture object state, the texture binding will
1509 * be restored by _mesa_meta_end().
1511 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1512 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1513 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1516 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
1517 _mesa_DeleteSamplers(1, &sampler
);
1519 /* Done with color buffer */
1520 mask
&= ~GL_COLOR_BUFFER_BIT
;
1529 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1530 * of texture mapping and polygon rendering.
1533 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1534 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1535 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1536 GLbitfield mask
, GLenum filter
)
1538 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1539 struct temp_texture
*tex
= get_temp_texture(ctx
);
1540 const GLsizei maxTexSize
= tex
->MaxSize
;
1541 const GLint srcX
= MIN2(srcX0
, srcX1
);
1542 const GLint srcY
= MIN2(srcY0
, srcY1
);
1543 const GLint srcW
= abs(srcX1
- srcX0
);
1544 const GLint srcH
= abs(srcY1
- srcY0
);
1545 const GLboolean srcFlipX
= srcX1
< srcX0
;
1546 const GLboolean srcFlipY
= srcY1
< srcY0
;
1550 struct vertex verts
[4];
1553 /* In addition to falling back if the blit size is larger than the maximum
1554 * texture size, fallback if the source is multisampled. This fallback can
1555 * be removed once Mesa gets support ARB_texture_multisample.
1557 if (srcW
> maxTexSize
|| srcH
> maxTexSize
1558 || ctx
->ReadBuffer
->Visual
.samples
> 0) {
1559 /* XXX avoid this fallback */
1560 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1561 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1577 /* only scissor effects blit so save/clear all other relevant state */
1578 _mesa_meta_begin(ctx
, ~MESA_META_SCISSOR
);
1580 if (blit
->ArrayObj
== 0) {
1581 /* one-time setup */
1583 /* create vertex array object */
1584 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1585 _mesa_BindVertexArray(blit
->ArrayObj
);
1587 /* create vertex array buffer */
1588 _mesa_GenBuffersARB(1, &blit
->VBO
);
1589 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1590 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1591 NULL
, GL_DYNAMIC_DRAW_ARB
);
1593 /* setup vertex arrays */
1594 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1595 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1596 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1597 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1600 _mesa_BindVertexArray(blit
->ArrayObj
);
1601 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1604 /* Try faster, direct texture approach first */
1605 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1606 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1608 _mesa_meta_end(ctx
);
1612 /* Continue with "normal" approach which involves copying the src rect
1613 * into a temporary texture and is "blitted" by drawing a textured quad.
1616 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1618 /* vertex positions/texcoords (after texture allocation!) */
1620 verts
[0].x
= (GLfloat
) dstX0
;
1621 verts
[0].y
= (GLfloat
) dstY0
;
1622 verts
[1].x
= (GLfloat
) dstX1
;
1623 verts
[1].y
= (GLfloat
) dstY0
;
1624 verts
[2].x
= (GLfloat
) dstX1
;
1625 verts
[2].y
= (GLfloat
) dstY1
;
1626 verts
[3].x
= (GLfloat
) dstX0
;
1627 verts
[3].y
= (GLfloat
) dstY1
;
1631 verts
[1].s
= tex
->Sright
;
1633 verts
[2].s
= tex
->Sright
;
1634 verts
[2].t
= tex
->Ttop
;
1636 verts
[3].t
= tex
->Ttop
;
1638 /* upload new vertex data */
1639 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1642 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1644 if (mask
& GL_COLOR_BUFFER_BIT
) {
1645 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1647 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1648 mask
&= ~GL_COLOR_BUFFER_BIT
;
1651 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1652 GLuint
*tmp
= malloc(srcW
* srcH
* sizeof(GLuint
));
1655 init_blit_depth_pixels(ctx
);
1657 /* maybe change tex format here */
1658 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1660 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1661 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1663 setup_drawpix_texture(ctx
, tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1664 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1666 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1667 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1668 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1669 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1670 _mesa_DepthFunc(GL_ALWAYS
);
1671 _mesa_DepthMask(GL_TRUE
);
1673 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1674 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1680 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1681 /* XXX can't easily do stencil */
1684 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1686 _mesa_meta_end(ctx
);
1689 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1690 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1695 meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
)
1697 if (blit
->ArrayObj
) {
1698 _mesa_DeleteVertexArraysAPPLE(1, &blit
->ArrayObj
);
1700 _mesa_DeleteBuffersARB(1, &blit
->VBO
);
1703 if (blit
->DepthFP
) {
1704 _mesa_DeletePrograms(1, &blit
->DepthFP
);
1711 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1714 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1716 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1718 GLfloat x
, y
, z
, r
, g
, b
, a
;
1720 struct vertex verts
[4];
1721 /* save all state but scissor, pixel pack/unpack */
1722 GLbitfield metaSave
= (MESA_META_ALL
-
1724 MESA_META_PIXEL_STORE
-
1725 MESA_META_CONDITIONAL_RENDER
-
1726 MESA_META_FRAMEBUFFER_SRGB
);
1727 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1729 if (buffers
& BUFFER_BITS_COLOR
) {
1730 /* if clearing color buffers, don't save/restore colormask */
1731 metaSave
-= MESA_META_COLOR_MASK
;
1734 _mesa_meta_begin(ctx
, metaSave
);
1736 if (clear
->ArrayObj
== 0) {
1737 /* one-time setup */
1739 /* create vertex array object */
1740 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1741 _mesa_BindVertexArray(clear
->ArrayObj
);
1743 /* create vertex array buffer */
1744 _mesa_GenBuffersARB(1, &clear
->VBO
);
1745 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1747 /* setup vertex arrays */
1748 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1749 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1750 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1751 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1754 _mesa_BindVertexArray(clear
->ArrayObj
);
1755 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1758 /* GL_COLOR_BUFFER_BIT */
1759 if (buffers
& BUFFER_BITS_COLOR
) {
1760 /* leave colormask, glDrawBuffer state as-is */
1762 /* Clears never have the color clamped. */
1763 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1766 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1767 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1770 /* GL_DEPTH_BUFFER_BIT */
1771 if (buffers
& BUFFER_BIT_DEPTH
) {
1772 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1773 _mesa_DepthFunc(GL_ALWAYS
);
1774 _mesa_DepthMask(GL_TRUE
);
1777 assert(!ctx
->Depth
.Test
);
1780 /* GL_STENCIL_BUFFER_BIT */
1781 if (buffers
& BUFFER_BIT_STENCIL
) {
1782 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1783 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1784 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1785 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1786 ctx
->Stencil
.Clear
& stencilMax
,
1787 ctx
->Stencil
.WriteMask
[0]);
1790 assert(!ctx
->Stencil
.Enabled
);
1793 /* vertex positions/colors */
1795 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1796 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1797 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1798 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1799 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1816 for (i
= 0; i
< 4; i
++) {
1817 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
1818 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
1819 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
1820 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
1823 /* upload new vertex data */
1824 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1825 GL_DYNAMIC_DRAW_ARB
);
1829 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1831 _mesa_meta_end(ctx
);
1835 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
1837 const char *vs_source
=
1838 "attribute vec4 position;\n"
1841 " gl_Position = position;\n"
1843 const char *fs_source
=
1844 "uniform vec4 color;\n"
1847 " gl_FragColor = color;\n"
1849 const char *vs_int_source
=
1851 "in vec4 position;\n"
1854 " gl_Position = position;\n"
1856 const char *fs_int_source
=
1858 "uniform ivec4 color;\n"
1859 "out ivec4 out_color;\n"
1863 " out_color = color;\n"
1867 if (clear
->ArrayObj
!= 0)
1870 /* create vertex array object */
1871 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1872 _mesa_BindVertexArray(clear
->ArrayObj
);
1874 /* create vertex array buffer */
1875 _mesa_GenBuffersARB(1, &clear
->VBO
);
1876 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1878 /* setup vertex arrays */
1879 _mesa_VertexAttribPointerARB(0, 3, GL_FLOAT
, GL_FALSE
, 0, (void *)0);
1880 _mesa_EnableVertexAttribArrayARB(0);
1882 vs
= _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER
);
1883 _mesa_ShaderSourceARB(vs
, 1, &vs_source
, NULL
);
1884 _mesa_CompileShaderARB(vs
);
1886 fs
= _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER
);
1887 _mesa_ShaderSourceARB(fs
, 1, &fs_source
, NULL
);
1888 _mesa_CompileShaderARB(fs
);
1890 clear
->ShaderProg
= _mesa_CreateProgramObjectARB();
1891 _mesa_AttachShader(clear
->ShaderProg
, fs
);
1892 _mesa_DeleteObjectARB(fs
);
1893 _mesa_AttachShader(clear
->ShaderProg
, vs
);
1894 _mesa_DeleteObjectARB(vs
);
1895 _mesa_BindAttribLocationARB(clear
->ShaderProg
, 0, "position");
1896 _mesa_LinkProgramARB(clear
->ShaderProg
);
1898 clear
->ColorLocation
= _mesa_GetUniformLocationARB(clear
->ShaderProg
,
1901 if (_mesa_is_desktop_gl(ctx
) && ctx
->Const
.GLSLVersion
>= 130) {
1902 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_int_source
);
1903 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_int_source
);
1905 clear
->IntegerShaderProg
= _mesa_CreateProgramObjectARB();
1906 _mesa_AttachShader(clear
->IntegerShaderProg
, fs
);
1907 _mesa_DeleteObjectARB(fs
);
1908 _mesa_AttachShader(clear
->IntegerShaderProg
, vs
);
1909 _mesa_DeleteObjectARB(vs
);
1910 _mesa_BindAttribLocationARB(clear
->IntegerShaderProg
, 0, "position");
1912 /* Note that user-defined out attributes get automatically assigned
1913 * locations starting from 0, so we don't need to explicitly
1914 * BindFragDataLocation to 0.
1917 link_program_with_debug(ctx
, clear
->IntegerShaderProg
);
1919 clear
->IntegerColorLocation
=
1920 _mesa_GetUniformLocationARB(clear
->IntegerShaderProg
, "color");
1925 meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
)
1927 if (clear
->ArrayObj
== 0)
1929 _mesa_DeleteVertexArraysAPPLE(1, &clear
->ArrayObj
);
1930 clear
->ArrayObj
= 0;
1931 _mesa_DeleteBuffersARB(1, &clear
->VBO
);
1933 _mesa_DeleteObjectARB(clear
->ShaderProg
);
1934 clear
->ShaderProg
= 0;
1936 if (clear
->IntegerShaderProg
) {
1937 _mesa_DeleteObjectARB(clear
->IntegerShaderProg
);
1938 clear
->IntegerShaderProg
= 0;
1943 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1946 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1948 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1949 GLbitfield metaSave
;
1950 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1951 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1952 const float x0
= ((float)fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
1953 const float y0
= ((float)fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
1954 const float x1
= ((float)fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
1955 const float y1
= ((float)fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
1956 const float z
= -invert_z(ctx
->Depth
.Clear
);
1961 metaSave
= (MESA_META_ALPHA_TEST
|
1963 MESA_META_DEPTH_TEST
|
1964 MESA_META_RASTERIZATION
|
1966 MESA_META_STENCIL_TEST
|
1968 MESA_META_VIEWPORT
|
1970 MESA_META_CLAMP_FRAGMENT_COLOR
|
1971 MESA_META_MULTISAMPLE
);
1973 if (!(buffers
& BUFFER_BITS_COLOR
)) {
1974 /* We'll use colormask to disable color writes. Otherwise,
1975 * respect color mask
1977 metaSave
|= MESA_META_COLOR_MASK
;
1980 _mesa_meta_begin(ctx
, metaSave
);
1982 meta_glsl_clear_init(ctx
, clear
);
1984 if (fb
->_IntegerColor
) {
1985 _mesa_UseProgramObjectARB(clear
->IntegerShaderProg
);
1986 _mesa_Uniform4ivARB(clear
->IntegerColorLocation
, 1,
1987 ctx
->Color
.ClearColor
.i
);
1989 _mesa_UseProgramObjectARB(clear
->ShaderProg
);
1990 _mesa_Uniform4fvARB(clear
->ColorLocation
, 1,
1991 ctx
->Color
.ClearColor
.f
);
1994 _mesa_BindVertexArray(clear
->ArrayObj
);
1995 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1997 /* GL_COLOR_BUFFER_BIT */
1998 if (buffers
& BUFFER_BITS_COLOR
) {
1999 /* leave colormask, glDrawBuffer state as-is */
2001 /* Clears never have the color clamped. */
2002 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
2005 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
2006 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2009 /* GL_DEPTH_BUFFER_BIT */
2010 if (buffers
& BUFFER_BIT_DEPTH
) {
2011 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
2012 _mesa_DepthFunc(GL_ALWAYS
);
2013 _mesa_DepthMask(GL_TRUE
);
2016 assert(!ctx
->Depth
.Test
);
2019 /* GL_STENCIL_BUFFER_BIT */
2020 if (buffers
& BUFFER_BIT_STENCIL
) {
2021 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2022 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
2023 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2024 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
2025 ctx
->Stencil
.Clear
& stencilMax
,
2026 ctx
->Stencil
.WriteMask
[0]);
2029 assert(!ctx
->Stencil
.Enabled
);
2032 /* vertex positions */
2046 /* upload new vertex data */
2047 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
2048 GL_DYNAMIC_DRAW_ARB
);
2051 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2053 _mesa_meta_end(ctx
);
2057 * Meta implementation of ctx->Driver.CopyPixels() in terms
2058 * of texture mapping and polygon rendering and GLSL shaders.
2061 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
2062 GLsizei width
, GLsizei height
,
2063 GLint dstX
, GLint dstY
, GLenum type
)
2065 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
2066 struct temp_texture
*tex
= get_temp_texture(ctx
);
2068 GLfloat x
, y
, z
, s
, t
;
2070 struct vertex verts
[4];
2072 GLenum intFormat
= GL_RGBA
;
2074 if (type
!= GL_COLOR
||
2075 ctx
->_ImageTransferState
||
2077 width
> tex
->MaxSize
||
2078 height
> tex
->MaxSize
) {
2079 /* XXX avoid this fallback */
2080 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
2084 /* Most GL state applies to glCopyPixels, but a there's a few things
2085 * we need to override:
2087 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2090 MESA_META_TRANSFORM
|
2093 MESA_META_VIEWPORT
));
2095 if (copypix
->ArrayObj
== 0) {
2096 /* one-time setup */
2098 /* create vertex array object */
2099 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
2100 _mesa_BindVertexArray(copypix
->ArrayObj
);
2102 /* create vertex array buffer */
2103 _mesa_GenBuffersARB(1, ©pix
->VBO
);
2104 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2105 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2106 NULL
, GL_DYNAMIC_DRAW_ARB
);
2108 /* setup vertex arrays */
2109 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2110 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2111 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2112 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2115 _mesa_BindVertexArray(copypix
->ArrayObj
);
2116 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2119 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
2121 /* vertex positions, texcoords (after texture allocation!) */
2123 const GLfloat dstX0
= (GLfloat
) dstX
;
2124 const GLfloat dstY0
= (GLfloat
) dstY
;
2125 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
2126 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
2127 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2137 verts
[1].s
= tex
->Sright
;
2142 verts
[2].s
= tex
->Sright
;
2143 verts
[2].t
= tex
->Ttop
;
2148 verts
[3].t
= tex
->Ttop
;
2150 /* upload new vertex data */
2151 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2154 /* Alloc/setup texture */
2155 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
2156 GL_RGBA
, GL_NEAREST
);
2158 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2160 /* draw textured quad */
2161 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2163 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2165 _mesa_meta_end(ctx
);
2171 * When the glDrawPixels() image size is greater than the max rectangle
2172 * texture size we use this function to break the glDrawPixels() image
2173 * into tiles which fit into the max texture size.
2176 tiled_draw_pixels(struct gl_context
*ctx
,
2178 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2179 GLenum format
, GLenum type
,
2180 const struct gl_pixelstore_attrib
*unpack
,
2181 const GLvoid
*pixels
)
2183 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
2186 if (tileUnpack
.RowLength
== 0)
2187 tileUnpack
.RowLength
= width
;
2189 for (i
= 0; i
< width
; i
+= tileSize
) {
2190 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
2191 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
2193 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
2195 for (j
= 0; j
< height
; j
+= tileSize
) {
2196 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
2197 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
2199 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
2201 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
2202 format
, type
, &tileUnpack
, pixels
);
2209 * One-time init for drawing stencil pixels.
2212 init_draw_stencil_pixels(struct gl_context
*ctx
)
2214 /* This program is run eight times, once for each stencil bit.
2215 * The stencil values to draw are found in an 8-bit alpha texture.
2216 * We read the texture/stencil value and test if bit 'b' is set.
2217 * If the bit is not set, use KIL to kill the fragment.
2218 * Finally, we use the stencil test to update the stencil buffer.
2220 * The basic algorithm for checking if a bit is set is:
2221 * if (is_odd(value / (1 << bit)))
2222 * result is one (or non-zero).
2225 * The program parameter contains three values:
2226 * parm.x = 255 / (1 << bit)
2230 static const char *program
=
2232 "PARAM parm = program.local[0]; \n"
2234 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
2235 "# t = t * 255 / bit \n"
2236 "MUL t.x, t.a, parm.x; \n"
2239 "SUB t.x, t.x, t.y; \n"
2241 "MUL t.x, t.x, parm.y; \n"
2242 "# t = fract(t.x) \n"
2243 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2244 "# t.x = (t.x == 0 ? 1 : 0) \n"
2245 "SGE t.x, -t.x, parm.z; \n"
2247 "# for debug only \n"
2248 "#MOV result.color, t.x; \n"
2250 char program2
[1000];
2251 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2252 struct temp_texture
*tex
= get_temp_texture(ctx
);
2253 const char *texTarget
;
2255 assert(drawpix
->StencilFP
== 0);
2257 /* replace %s with "RECT" or "2D" */
2258 assert(strlen(program
) + 4 < sizeof(program2
));
2259 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2263 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2265 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
2266 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2267 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2268 strlen(program2
), (const GLubyte
*) program2
);
2273 * One-time init for drawing depth pixels.
2276 init_draw_depth_pixels(struct gl_context
*ctx
)
2278 static const char *program
=
2280 "PARAM color = program.local[0]; \n"
2281 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2282 "MOV result.color, color; \n"
2285 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2286 struct temp_texture
*tex
= get_temp_texture(ctx
);
2287 const char *texTarget
;
2289 assert(drawpix
->DepthFP
== 0);
2291 /* replace %s with "RECT" or "2D" */
2292 assert(strlen(program
) + 4 < sizeof(program2
));
2293 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2297 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2299 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
2300 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2301 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2302 strlen(program2
), (const GLubyte
*) program2
);
2307 * Meta implementation of ctx->Driver.DrawPixels() in terms
2308 * of texture mapping and polygon rendering.
2311 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2312 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2313 GLenum format
, GLenum type
,
2314 const struct gl_pixelstore_attrib
*unpack
,
2315 const GLvoid
*pixels
)
2317 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2318 struct temp_texture
*tex
= get_temp_texture(ctx
);
2319 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2320 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2322 GLfloat x
, y
, z
, s
, t
;
2324 struct vertex verts
[4];
2325 GLenum texIntFormat
;
2326 GLboolean fallback
, newTex
;
2327 GLbitfield metaExtraSave
= 0x0;
2331 * Determine if we can do the glDrawPixels with texture mapping.
2333 fallback
= GL_FALSE
;
2334 if (ctx
->Fog
.Enabled
) {
2338 if (_mesa_is_color_format(format
)) {
2339 /* use more compact format when possible */
2340 /* XXX disable special case for GL_LUMINANCE for now to work around
2341 * apparent i965 driver bug (see bug #23670).
2343 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2344 texIntFormat
= format
;
2346 texIntFormat
= GL_RGBA
;
2348 /* If we're not supposed to clamp the resulting color, then just
2349 * promote our texture to fully float. We could do better by
2350 * just going for the matching set of channels, in floating
2353 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2354 ctx
->Extensions
.ARB_texture_float
)
2355 texIntFormat
= GL_RGBA32F
;
2357 else if (_mesa_is_stencil_format(format
)) {
2358 if (ctx
->Extensions
.ARB_fragment_program
&&
2359 ctx
->Pixel
.IndexShift
== 0 &&
2360 ctx
->Pixel
.IndexOffset
== 0 &&
2361 type
== GL_UNSIGNED_BYTE
) {
2362 /* We'll store stencil as alpha. This only works for GLubyte
2363 * image data because of how incoming values are mapped to alpha
2366 texIntFormat
= GL_ALPHA
;
2367 metaExtraSave
= (MESA_META_COLOR_MASK
|
2368 MESA_META_DEPTH_TEST
|
2369 MESA_META_PIXEL_TRANSFER
|
2371 MESA_META_STENCIL_TEST
);
2377 else if (_mesa_is_depth_format(format
)) {
2378 if (ctx
->Extensions
.ARB_depth_texture
&&
2379 ctx
->Extensions
.ARB_fragment_program
) {
2380 texIntFormat
= GL_DEPTH_COMPONENT
;
2381 metaExtraSave
= (MESA_META_SHADER
);
2392 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2393 format
, type
, unpack
, pixels
);
2398 * Check image size against max texture size, draw as tiles if needed.
2400 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2401 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2402 format
, type
, unpack
, pixels
);
2406 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2407 * but a there's a few things we need to override:
2409 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2412 MESA_META_TRANSFORM
|
2415 MESA_META_VIEWPORT
|
2418 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2420 /* vertex positions, texcoords (after texture allocation!) */
2422 const GLfloat x0
= (GLfloat
) x
;
2423 const GLfloat y0
= (GLfloat
) y
;
2424 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2425 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2426 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2436 verts
[1].s
= tex
->Sright
;
2441 verts
[2].s
= tex
->Sright
;
2442 verts
[2].t
= tex
->Ttop
;
2447 verts
[3].t
= tex
->Ttop
;
2450 if (drawpix
->ArrayObj
== 0) {
2451 /* one-time setup: create vertex array object */
2452 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
2454 _mesa_BindVertexArray(drawpix
->ArrayObj
);
2456 /* create vertex array buffer */
2457 _mesa_GenBuffersARB(1, &vbo
);
2458 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
2459 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2460 verts
, GL_DYNAMIC_DRAW_ARB
);
2462 /* setup vertex arrays */
2463 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2464 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2465 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2466 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2468 /* set given unpack params */
2469 ctx
->Unpack
= *unpack
;
2471 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2473 if (_mesa_is_stencil_format(format
)) {
2474 /* Drawing stencil */
2477 if (!drawpix
->StencilFP
)
2478 init_draw_stencil_pixels(ctx
);
2480 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2481 GL_ALPHA
, type
, pixels
);
2483 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2485 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2487 /* set all stencil bits to 0 */
2488 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2489 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2490 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2492 /* set stencil bits to 1 where needed */
2493 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2495 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2496 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2498 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2499 const GLuint mask
= 1 << bit
;
2500 if (mask
& origStencilMask
) {
2501 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2502 _mesa_StencilMask(mask
);
2504 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2505 255.0 / mask
, 0.5, 0.0, 0.0);
2507 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2511 else if (_mesa_is_depth_format(format
)) {
2513 if (!drawpix
->DepthFP
)
2514 init_draw_depth_pixels(ctx
);
2516 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2517 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2519 /* polygon color = current raster color */
2520 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2521 ctx
->Current
.RasterColor
);
2523 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2524 format
, type
, pixels
);
2526 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2530 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2531 format
, type
, pixels
);
2532 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2535 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2537 _mesa_DeleteBuffersARB(1, &vbo
);
2539 /* restore unpack params */
2540 ctx
->Unpack
= unpackSave
;
2542 _mesa_meta_end(ctx
);
2546 alpha_test_raster_color(struct gl_context
*ctx
)
2548 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2549 GLfloat ref
= ctx
->Color
.AlphaRef
;
2551 switch (ctx
->Color
.AlphaFunc
) {
2557 return alpha
== ref
;
2559 return alpha
<= ref
;
2563 return alpha
!= ref
;
2565 return alpha
>= ref
;
2575 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2576 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2577 * tracker would improve performance a lot.
2580 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2581 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2582 const struct gl_pixelstore_attrib
*unpack
,
2583 const GLubyte
*bitmap1
)
2585 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2586 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2587 const GLenum texIntFormat
= GL_ALPHA
;
2588 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2591 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2593 struct vertex verts
[4];
2598 * Check if swrast fallback is needed.
2600 if (ctx
->_ImageTransferState
||
2601 ctx
->FragmentProgram
._Enabled
||
2603 ctx
->Texture
._EnabledUnits
||
2604 width
> tex
->MaxSize
||
2605 height
> tex
->MaxSize
) {
2606 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2610 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2613 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2614 * but a there's a few things we need to override:
2616 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
2617 MESA_META_PIXEL_STORE
|
2618 MESA_META_RASTERIZATION
|
2621 MESA_META_TRANSFORM
|
2624 MESA_META_VIEWPORT
));
2626 if (bitmap
->ArrayObj
== 0) {
2627 /* one-time setup */
2629 /* create vertex array object */
2630 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2631 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2633 /* create vertex array buffer */
2634 _mesa_GenBuffersARB(1, &bitmap
->VBO
);
2635 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2636 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2637 NULL
, GL_DYNAMIC_DRAW_ARB
);
2639 /* setup vertex arrays */
2640 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2641 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2642 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2643 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2644 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2645 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2648 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2649 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2652 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2654 /* vertex positions, texcoords, colors (after texture allocation!) */
2656 const GLfloat x0
= (GLfloat
) x
;
2657 const GLfloat y0
= (GLfloat
) y
;
2658 const GLfloat x1
= (GLfloat
) (x
+ width
);
2659 const GLfloat y1
= (GLfloat
) (y
+ height
);
2660 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2671 verts
[1].s
= tex
->Sright
;
2676 verts
[2].s
= tex
->Sright
;
2677 verts
[2].t
= tex
->Ttop
;
2682 verts
[3].t
= tex
->Ttop
;
2684 for (i
= 0; i
< 4; i
++) {
2685 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2686 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2687 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2688 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2691 /* upload new vertex data */
2692 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2695 /* choose different foreground/background alpha values */
2696 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2697 bg
= (fg
> 127 ? 0 : 255);
2699 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2701 _mesa_meta_end(ctx
);
2705 bitmap8
= malloc(width
* height
);
2707 memset(bitmap8
, bg
, width
* height
);
2708 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2709 bitmap8
, width
, fg
);
2711 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2713 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2714 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2716 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2717 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2719 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2721 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2726 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2728 _mesa_meta_end(ctx
);
2733 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2734 * software fallback. The fallback path will require that the texture
2735 * images are mapped.
2736 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2739 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
2740 struct gl_texture_object
*texObj
)
2742 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2743 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2744 struct gl_texture_image
*baseImage
;
2748 /* check for fallbacks */
2749 if (!ctx
->Extensions
.EXT_framebuffer_object
||
2750 target
== GL_TEXTURE_3D
||
2751 target
== GL_TEXTURE_1D_ARRAY
||
2752 target
== GL_TEXTURE_2D_ARRAY
) {
2756 srcLevel
= texObj
->BaseLevel
;
2757 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2758 if (!baseImage
|| _mesa_is_format_compressed(baseImage
->TexFormat
)) {
2762 if (_mesa_get_format_color_encoding(baseImage
->TexFormat
) == GL_SRGB
&&
2763 !ctx
->Extensions
.EXT_texture_sRGB_decode
) {
2764 /* The texture format is sRGB but we can't turn off sRGB->linear
2765 * texture sample conversion. So we won't be able to generate the
2766 * right colors when rendering. Need to use a fallback.
2772 * Test that we can actually render in the texture's format.
2775 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2776 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2778 if (target
== GL_TEXTURE_1D
) {
2779 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2780 GL_COLOR_ATTACHMENT0_EXT
,
2781 target
, texObj
->Name
, srcLevel
);
2784 /* other work is needed to enable 3D mipmap generation */
2785 else if (target
== GL_TEXTURE_3D
) {
2787 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2788 GL_COLOR_ATTACHMENT0_EXT
,
2789 target
, texObj
->Name
, srcLevel
, zoffset
);
2794 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2795 GL_COLOR_ATTACHMENT0_EXT
,
2796 target
, texObj
->Name
, srcLevel
);
2799 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2801 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2803 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2812 * Compute the texture coordinates for the four vertices of a quad for
2813 * drawing a 2D texture image or slice of a cube/3D texture.
2814 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2815 * \param slice slice of a 1D/2D array texture or 3D texture
2816 * \param width width of the texture image
2817 * \param height height of the texture image
2818 * \param coords0/1/2/3 returns the computed texcoords
2821 setup_texture_coords(GLenum faceTarget
,
2830 static const GLfloat st
[4][2] = {
2831 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2836 switch (faceTarget
) {
2840 case GL_TEXTURE_2D_ARRAY
:
2841 if (faceTarget
== GL_TEXTURE_3D
)
2843 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
2847 coords0
[0] = 0.0F
; /* s */
2848 coords0
[1] = 0.0F
; /* t */
2849 coords0
[2] = r
; /* r */
2860 case GL_TEXTURE_RECTANGLE_ARB
:
2861 coords0
[0] = 0.0F
; /* s */
2862 coords0
[1] = 0.0F
; /* t */
2863 coords0
[2] = 0.0F
; /* r */
2868 coords2
[1] = height
;
2871 coords3
[1] = height
;
2874 case GL_TEXTURE_1D_ARRAY
:
2875 coords0
[0] = 0.0F
; /* s */
2876 coords0
[1] = slice
; /* t */
2877 coords0
[2] = 0.0F
; /* r */
2889 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2890 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2891 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2892 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2893 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2894 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2895 /* loop over quad verts */
2896 for (i
= 0; i
< 4; i
++) {
2897 /* Compute sc = +/-scale and tc = +/-scale.
2898 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2899 * though that can still sometimes happen with this scale factor...
2901 const GLfloat scale
= 0.9999f
;
2902 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2903 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2923 switch (faceTarget
) {
2924 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2929 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2934 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2939 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2944 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2949 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2960 assert(0 && "unexpected target in meta setup_texture_coords()");
2966 setup_ff_generate_mipmap(struct gl_context
*ctx
,
2967 struct gen_mipmap_state
*mipmap
)
2970 GLfloat x
, y
, tex
[3];
2973 if (mipmap
->ArrayObj
== 0) {
2974 /* one-time setup */
2975 /* create vertex array object */
2976 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
2977 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
2979 /* create vertex array buffer */
2980 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
2981 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2982 /* setup vertex arrays */
2983 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2984 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
2985 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2986 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2989 /* setup projection matrix */
2990 _mesa_MatrixMode(GL_PROJECTION
);
2991 _mesa_LoadIdentity();
2992 _mesa_Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
2996 static struct glsl_sampler
*
2997 setup_texture_sampler(GLenum target
, struct gen_mipmap_state
*mipmap
)
3001 mipmap
->sampler_1d
.type
= "sampler1D";
3002 mipmap
->sampler_1d
.func
= "texture1D";
3003 mipmap
->sampler_1d
.texcoords
= "texCoords.x";
3004 return &mipmap
->sampler_1d
;
3006 mipmap
->sampler_2d
.type
= "sampler2D";
3007 mipmap
->sampler_2d
.func
= "texture2D";
3008 mipmap
->sampler_2d
.texcoords
= "texCoords.xy";
3009 return &mipmap
->sampler_2d
;
3011 /* Code for mipmap generation with 3D textures is not used yet.
3012 * It's a sw fallback.
3014 mipmap
->sampler_3d
.type
= "sampler3D";
3015 mipmap
->sampler_3d
.func
= "texture3D";
3016 mipmap
->sampler_3d
.texcoords
= "texCoords";
3017 return &mipmap
->sampler_3d
;
3018 case GL_TEXTURE_CUBE_MAP
:
3019 mipmap
->sampler_cubemap
.type
= "samplerCube";
3020 mipmap
->sampler_cubemap
.func
= "textureCube";
3021 mipmap
->sampler_cubemap
.texcoords
= "texCoords";
3022 return &mipmap
->sampler_cubemap
;
3023 case GL_TEXTURE_1D_ARRAY
:
3024 mipmap
->sampler_1d_array
.type
= "sampler1DArray";
3025 mipmap
->sampler_1d_array
.func
= "texture1DArray";
3026 mipmap
->sampler_1d_array
.texcoords
= "texCoords.xy";
3027 return &mipmap
->sampler_1d_array
;
3028 case GL_TEXTURE_2D_ARRAY
:
3029 mipmap
->sampler_2d_array
.type
= "sampler2DArray";
3030 mipmap
->sampler_2d_array
.func
= "texture2DArray";
3031 mipmap
->sampler_2d_array
.texcoords
= "texCoords";
3032 return &mipmap
->sampler_2d_array
;
3034 _mesa_problem(NULL
, "Unexpected texture target 0x%x in"
3035 " setup_texture_sampler()\n", target
);
3042 setup_glsl_generate_mipmap(struct gl_context
*ctx
,
3043 struct gen_mipmap_state
*mipmap
,
3047 GLfloat x
, y
, tex
[3];
3049 struct glsl_sampler
*sampler
;
3050 const char *vs_source
;
3051 const char *fs_template
;
3053 static const char *vs_int_source
=
3055 "in vec2 position;\n"
3056 "in vec3 textureCoords;\n"
3057 "out vec3 texCoords;\n"
3060 " texCoords = textureCoords;\n"
3061 " gl_Position = gl_Vertex;\n"
3063 static const char *fs_int_source
=
3065 "uniform isampler2D tex2d;\n"
3066 "in vec3 texCoords;\n"
3067 "out ivec4 out_color;\n"
3071 " out_color = texture(tex2d, texCoords.xy);\n"
3074 const char *extension_mode
;
3077 if (ctx
->Const
.GLSLVersion
< 130) {
3079 "attribute vec2 position;\n"
3080 "attribute vec3 textureCoords;\n"
3081 "varying vec3 texCoords;\n"
3084 " texCoords = textureCoords;\n"
3085 " gl_Position = vec4(position, 0.0, 1.0);\n"
3088 "#extension GL_EXT_texture_array : %s\n"
3089 "uniform %s texSampler;\n"
3090 "varying vec3 texCoords;\n"
3093 " gl_FragColor = %s(texSampler, %s);\n"
3098 "in vec2 position;\n"
3099 "in vec3 textureCoords;\n"
3100 "out vec3 texCoords;\n"
3103 " texCoords = textureCoords;\n"
3104 " gl_Position = vec4(position, 0.0, 1.0);\n"
3108 "uniform %s texSampler;\n"
3109 "in vec3 texCoords;\n"
3110 "out %s out_color;\n"
3114 " out_color = texture(texSampler, %s);\n"
3118 /* Check if already initialized */
3119 if (mipmap
->ArrayObj
== 0) {
3121 /* create vertex array object */
3122 _mesa_GenVertexArrays(1, &mipmap
->ArrayObj
);
3123 _mesa_BindVertexArray(mipmap
->ArrayObj
);
3125 /* create vertex array buffer */
3126 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
3127 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3129 /* setup vertex arrays */
3130 _mesa_VertexAttribPointerARB(0, 2, GL_FLOAT
, GL_FALSE
,
3131 sizeof(struct vertex
), OFFSET(x
));
3132 _mesa_VertexAttribPointerARB(1, 3, GL_FLOAT
, GL_FALSE
,
3133 sizeof(struct vertex
), OFFSET(tex
));
3136 /* Generate a fragment shader program appropriate for the texture target */
3137 sampler
= setup_texture_sampler(target
, mipmap
);
3138 assert(sampler
!= NULL
);
3139 if (sampler
->shader_prog
!= 0) {
3140 mipmap
->ShaderProg
= sampler
->shader_prog
;
3144 mem_ctx
= ralloc_context(NULL
);
3146 if (ctx
->Const
.GLSLVersion
< 130) {
3147 extension_mode
= ((target
== GL_TEXTURE_1D_ARRAY
) ||
3148 (target
== GL_TEXTURE_2D_ARRAY
)) ?
3149 "require" : "disable";
3151 fs_source
= ralloc_asprintf(mem_ctx
, fs_template
,
3152 extension_mode
, sampler
->type
,
3153 sampler
->func
, sampler
->texcoords
);
3156 fs_source
= ralloc_asprintf(mem_ctx
, fs_template
,
3157 sampler
->type
, "vec4",
3158 sampler
->texcoords
);
3161 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_source
);
3162 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_source
);
3164 mipmap
->ShaderProg
= _mesa_CreateProgramObjectARB();
3165 _mesa_AttachShader(mipmap
->ShaderProg
, fs
);
3166 _mesa_DeleteObjectARB(fs
);
3167 _mesa_AttachShader(mipmap
->ShaderProg
, vs
);
3168 _mesa_DeleteObjectARB(vs
);
3169 _mesa_BindAttribLocationARB(mipmap
->ShaderProg
, 0, "position");
3170 _mesa_BindAttribLocationARB(mipmap
->ShaderProg
, 1, "texcoords");
3171 _mesa_EnableVertexAttribArrayARB(0);
3172 _mesa_EnableVertexAttribArrayARB(1);
3173 link_program_with_debug(ctx
, mipmap
->ShaderProg
);
3174 sampler
->shader_prog
= mipmap
->ShaderProg
;
3175 ralloc_free(mem_ctx
);
3177 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Const
.GLSLVersion
>= 130) ||
3178 _mesa_is_gles3(ctx
)){
3179 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_int_source
);
3180 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_int_source
);
3182 mipmap
->IntegerShaderProg
= _mesa_CreateProgramObjectARB();
3183 _mesa_AttachShader(mipmap
->IntegerShaderProg
, fs
);
3184 _mesa_DeleteObjectARB(fs
);
3185 _mesa_AttachShader(mipmap
->IntegerShaderProg
, vs
);
3186 _mesa_DeleteObjectARB(vs
);
3187 _mesa_BindAttribLocationARB(mipmap
->IntegerShaderProg
, 0, "position");
3188 _mesa_BindAttribLocationARB(mipmap
->IntegerShaderProg
, 1, "texcoords");
3190 /* Note that user-defined out attributes get automatically assigned
3191 * locations starting from 0, so we don't need to explicitly
3192 * BindFragDataLocation to 0.
3194 link_program_with_debug(ctx
, mipmap
->IntegerShaderProg
);
3200 meta_glsl_generate_mipmap_cleanup(struct gl_context
*ctx
,
3201 struct gen_mipmap_state
*mipmap
)
3203 if (mipmap
->ArrayObj
== 0)
3205 _mesa_DeleteVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
3206 mipmap
->ArrayObj
= 0;
3207 _mesa_DeleteBuffersARB(1, &mipmap
->VBO
);
3210 _mesa_DeleteObjectARB(mipmap
->sampler_1d
.shader_prog
);
3211 _mesa_DeleteObjectARB(mipmap
->sampler_2d
.shader_prog
);
3212 _mesa_DeleteObjectARB(mipmap
->sampler_3d
.shader_prog
);
3213 _mesa_DeleteObjectARB(mipmap
->sampler_cubemap
.shader_prog
);
3214 _mesa_DeleteObjectARB(mipmap
->sampler_1d_array
.shader_prog
);
3215 _mesa_DeleteObjectARB(mipmap
->sampler_2d_array
.shader_prog
);
3217 mipmap
->sampler_1d
.shader_prog
= 0;
3218 mipmap
->sampler_2d
.shader_prog
= 0;
3219 mipmap
->sampler_3d
.shader_prog
= 0;
3220 mipmap
->sampler_cubemap
.shader_prog
= 0;
3221 mipmap
->sampler_1d_array
.shader_prog
= 0;
3222 mipmap
->sampler_2d_array
.shader_prog
= 0;
3224 if (mipmap
->IntegerShaderProg
) {
3225 _mesa_DeleteObjectARB(mipmap
->IntegerShaderProg
);
3226 mipmap
->IntegerShaderProg
= 0;
3232 * Called via ctx->Driver.GenerateMipmap()
3233 * Note: We don't yet support 3D textures, 1D/2D array textures or texture
3237 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
3238 struct gl_texture_object
*texObj
)
3240 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
3242 GLfloat x
, y
, tex
[3];
3244 struct vertex verts
[4];
3245 const GLuint baseLevel
= texObj
->BaseLevel
;
3246 const GLuint maxLevel
= texObj
->MaxLevel
;
3247 const GLint maxLevelSave
= texObj
->MaxLevel
;
3248 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
3249 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
3250 const GLuint currentTexUnitSave
= ctx
->Texture
.CurrentUnit
;
3251 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
3252 ctx
->Extensions
.ARB_fragment_shader
&&
3253 (ctx
->API
!= API_OPENGLES
);
3256 const GLint slice
= 0;
3259 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
3260 _mesa_generate_mipmap(ctx
, target
, texObj
);
3264 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
3265 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
3266 faceTarget
= target
;
3267 target
= GL_TEXTURE_CUBE_MAP
;
3270 faceTarget
= target
;
3273 _mesa_meta_begin(ctx
, MESA_META_ALL
);
3275 /* Choose between glsl version and fixed function version of
3276 * GenerateMipmap function.
3278 if (use_glsl_version
) {
3279 setup_glsl_generate_mipmap(ctx
, mipmap
, target
);
3281 if (texObj
->_IsIntegerFormat
)
3282 _mesa_UseProgramObjectARB(mipmap
->IntegerShaderProg
);
3284 _mesa_UseProgramObjectARB(mipmap
->ShaderProg
);
3287 setup_ff_generate_mipmap(ctx
, mipmap
);
3288 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3291 _mesa_BindVertexArray(mipmap
->ArrayObj
);
3292 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3294 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
3295 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
3297 if (currentTexUnitSave
!= 0)
3298 _mesa_BindTexture(target
, texObj
->Name
);
3301 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
3304 if (!mipmap
->Sampler
) {
3305 _mesa_GenSamplers(1, &mipmap
->Sampler
);
3306 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
3308 if (use_glsl_version
&& texObj
->_IsIntegerFormat
)
3309 _mesa_SamplerParameteri(mipmap
->Sampler
,
3310 GL_TEXTURE_MIN_FILTER
,
3311 GL_NEAREST_MIPMAP_NEAREST
);
3313 _mesa_SamplerParameteri(mipmap
->Sampler
,
3314 GL_TEXTURE_MIN_FILTER
,
3315 GL_LINEAR_MIPMAP_LINEAR
);
3317 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
3318 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
3319 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
3320 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
3322 /* We don't want to encode or decode sRGB values; treat them as linear.
3323 * This is not technically correct for GLES3 but we don't get any API
3324 * error at the moment.
3326 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3327 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
3328 GL_SKIP_DECODE_EXT
);
3332 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
3335 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
3337 if (ctx
->API
== API_OPENGL
|| ctx
->API
== API_OPENGLES
)
3338 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
3340 assert(!genMipmapSave
);
3342 /* Setup texture coordinates */
3343 setup_texture_coords(faceTarget
,
3345 0, 0, /* width, height never used here */
3351 /* setup vertex positions */
3361 /* upload vertex data */
3362 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3363 verts
, GL_DYNAMIC_DRAW_ARB
);
3365 /* texture is already locked, unlock now */
3366 _mesa_unlock_texture(ctx
, texObj
);
3368 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
3369 const struct gl_texture_image
*srcImage
;
3370 const GLuint srcLevel
= dstLevel
- 1;
3371 GLsizei srcWidth
, srcHeight
, srcDepth
;
3372 GLsizei dstWidth
, dstHeight
, dstDepth
;
3375 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
3376 assert(srcImage
->Border
== 0);
3379 srcWidth
= srcImage
->Width
;
3380 srcHeight
= srcImage
->Height
;
3381 srcDepth
= srcImage
->Depth
;
3384 dstWidth
= MAX2(1, srcWidth
/ 2);
3385 dstHeight
= MAX2(1, srcHeight
/ 2);
3386 dstDepth
= MAX2(1, srcDepth
/ 2);
3388 if (dstWidth
== srcImage
->Width
&&
3389 dstHeight
== srcImage
->Height
&&
3390 dstDepth
== srcImage
->Depth
) {
3395 /* Allocate storage for the destination mipmap image(s) */
3397 /* Set MaxLevel large enough to hold the new level when we allocate it */
3398 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
3400 if (!_mesa_prepare_mipmap_level(ctx
, texObj
, dstLevel
,
3401 dstWidth
, dstHeight
, dstDepth
,
3403 srcImage
->InternalFormat
,
3404 srcImage
->TexFormat
)) {
3405 /* All done. We either ran out of memory or we would go beyond the
3406 * last valid level of an immutable texture if we continued.
3411 /* limit minification to src level */
3412 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
3414 /* Set to draw into the current dstLevel */
3415 if (target
== GL_TEXTURE_1D
) {
3416 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
3417 GL_COLOR_ATTACHMENT0_EXT
,
3422 else if (target
== GL_TEXTURE_3D
) {
3423 GLint zoffset
= 0; /* XXX unfinished */
3424 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
3425 GL_COLOR_ATTACHMENT0_EXT
,
3432 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
3433 GL_COLOR_ATTACHMENT0_EXT
,
3439 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
3442 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
3443 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
3444 _mesa_problem(ctx
, "Unexpected incomplete framebuffer in "
3445 "_mesa_meta_GenerateMipmap()");
3449 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
3450 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
3452 /* setup viewport */
3453 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
3455 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3458 _mesa_lock_texture(ctx
, texObj
); /* relock */
3460 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
3462 _mesa_meta_end(ctx
);
3464 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3466 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
3468 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
3473 * Determine the GL data type to use for the temporary image read with
3474 * ReadPixels() and passed to Tex[Sub]Image().
3477 get_temp_image_type(struct gl_context
*ctx
, gl_format format
)
3481 baseFormat
= _mesa_get_format_base_format(format
);
3483 switch (baseFormat
) {
3490 case GL_LUMINANCE_ALPHA
:
3492 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8) {
3493 return GL_UNSIGNED_BYTE
;
3494 } else if (ctx
->DrawBuffer
->Visual
.redBits
<= 16) {
3495 return GL_UNSIGNED_SHORT
;
3497 GLenum datatype
= _mesa_get_format_datatype(format
);
3498 if (datatype
== GL_INT
|| datatype
== GL_UNSIGNED_INT
)
3502 case GL_DEPTH_COMPONENT
:
3503 return GL_UNSIGNED_INT
;
3504 case GL_DEPTH_STENCIL
:
3505 return GL_UNSIGNED_INT_24_8
;
3507 _mesa_problem(ctx
, "Unexpected format %d in get_temp_image_type()",
3515 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
3516 * Have to be careful with locking and meta state for pixel transfer.
3519 _mesa_meta_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
3520 struct gl_texture_image
*texImage
,
3521 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3522 struct gl_renderbuffer
*rb
,
3524 GLsizei width
, GLsizei height
)
3526 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3527 GLenum format
, type
;
3531 /* Choose format/type for temporary image buffer */
3532 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
3533 if (format
== GL_LUMINANCE
||
3534 format
== GL_LUMINANCE_ALPHA
||
3535 format
== GL_INTENSITY
) {
3536 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
3537 * temp image buffer because glReadPixels will do L=R+G+B which is
3538 * not what we want (should be L=R).
3543 type
= get_temp_image_type(ctx
, texImage
->TexFormat
);
3544 if (_mesa_is_format_integer_color(texImage
->TexFormat
)) {
3545 format
= _mesa_base_format_to_integer_format(format
);
3547 bpp
= _mesa_bytes_per_pixel(format
, type
);
3549 _mesa_problem(ctx
, "Bad bpp in _mesa_meta_CopyTexSubImage()");
3554 * Alloc image buffer (XXX could use a PBO)
3556 buf
= malloc(width
* height
* bpp
);
3558 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
3562 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
3565 * Read image from framebuffer (disable pixel transfer ops)
3567 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
3568 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
3569 format
, type
, &ctx
->Pack
, buf
);
3570 _mesa_meta_end(ctx
);
3572 _mesa_update_state(ctx
); /* to update pixel transfer state */
3575 * Store texture data (with pixel transfer ops)
3577 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
3579 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3580 xoffset
, yoffset
, zoffset
, width
, height
, 1,
3581 format
, type
, buf
, &ctx
->Unpack
);
3583 _mesa_meta_end(ctx
);
3585 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
3592 * Decompress a texture image by drawing a quad with the compressed
3593 * texture and reading the pixels out of the color buffer.
3594 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
3595 * \param destFormat format, ala glReadPixels
3596 * \param destType type, ala glReadPixels
3597 * \param dest destination buffer
3598 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
3601 decompress_texture_image(struct gl_context
*ctx
,
3602 struct gl_texture_image
*texImage
,
3604 GLenum destFormat
, GLenum destType
,
3607 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
3608 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3609 const GLint width
= texImage
->Width
;
3610 const GLint height
= texImage
->Height
;
3611 const GLenum target
= texObj
->Target
;
3614 GLfloat x
, y
, tex
[3];
3616 struct vertex verts
[4];
3617 GLuint fboDrawSave
, fboReadSave
;
3622 assert(target
== GL_TEXTURE_3D
||
3623 target
== GL_TEXTURE_2D_ARRAY
);
3626 if (target
== GL_TEXTURE_CUBE_MAP
) {
3627 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
3630 faceTarget
= target
;
3633 /* save fbo bindings (not saved by _mesa_meta_begin()) */
3634 fboDrawSave
= ctx
->DrawBuffer
->Name
;
3635 fboReadSave
= ctx
->ReadBuffer
->Name
;
3636 rbSave
= ctx
->CurrentRenderbuffer
? ctx
->CurrentRenderbuffer
->Name
: 0;
3638 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_PIXEL_STORE
);
3640 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
3641 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
3643 /* Create/bind FBO/renderbuffer */
3644 if (decompress
->FBO
== 0) {
3645 _mesa_GenFramebuffersEXT(1, &decompress
->FBO
);
3646 _mesa_GenRenderbuffersEXT(1, &decompress
->RBO
);
3647 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3648 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3649 _mesa_FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT
,
3650 GL_COLOR_ATTACHMENT0_EXT
,
3651 GL_RENDERBUFFER_EXT
,
3655 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3658 /* alloc dest surface */
3659 if (width
> decompress
->Width
|| height
> decompress
->Height
) {
3660 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3661 _mesa_RenderbufferStorageEXT(GL_RENDERBUFFER_EXT
, GL_RGBA
,
3663 decompress
->Width
= width
;
3664 decompress
->Height
= height
;
3667 /* setup VBO data */
3668 if (decompress
->ArrayObj
== 0) {
3669 /* create vertex array object */
3670 _mesa_GenVertexArrays(1, &decompress
->ArrayObj
);
3671 _mesa_BindVertexArray(decompress
->ArrayObj
);
3673 /* create vertex array buffer */
3674 _mesa_GenBuffersARB(1, &decompress
->VBO
);
3675 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3676 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3677 NULL
, GL_DYNAMIC_DRAW_ARB
);
3679 /* setup vertex arrays */
3680 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3681 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
3682 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3683 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3686 _mesa_BindVertexArray(decompress
->ArrayObj
);
3687 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3690 if (!decompress
->Sampler
) {
3691 _mesa_GenSamplers(1, &decompress
->Sampler
);
3692 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
3693 /* nearest filtering */
3694 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
3695 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
3696 /* No sRGB decode or encode.*/
3697 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3698 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
3699 GL_SKIP_DECODE_EXT
);
3703 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
3706 setup_texture_coords(faceTarget
, slice
, width
, height
,
3712 /* setup vertex positions */
3718 verts
[2].y
= height
;
3720 verts
[3].y
= height
;
3722 /* upload new vertex data */
3723 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3725 /* setup texture state */
3726 _mesa_BindTexture(target
, texObj
->Name
);
3727 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3730 /* save texture object state */
3731 const GLint baseLevelSave
= texObj
->BaseLevel
;
3732 const GLint maxLevelSave
= texObj
->MaxLevel
;
3734 /* restrict sampling to the texture level of interest */
3735 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3736 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, texImage
->Level
);
3737 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texImage
->Level
);
3740 /* render quad w/ texture into renderbuffer */
3741 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3743 /* Restore texture object state, the texture binding will
3744 * be restored by _mesa_meta_end().
3746 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3747 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
3748 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3753 /* read pixels from renderbuffer */
3755 GLenum baseTexFormat
= texImage
->_BaseFormat
;
3757 /* The pixel transfer state will be set to default values at this point
3758 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
3759 * turned off (as required by glGetTexImage) but we need to handle some
3760 * special cases. In particular, single-channel texture values are
3761 * returned as red and two-channel texture values are returned as
3764 if (baseTexFormat
== GL_LUMINANCE
||
3765 baseTexFormat
== GL_LUMINANCE_ALPHA
||
3766 baseTexFormat
== GL_INTENSITY
) {
3767 /* Green and blue must be zero */
3768 _mesa_PixelTransferf(GL_GREEN_SCALE
, 0.0f
);
3769 _mesa_PixelTransferf(GL_BLUE_SCALE
, 0.0f
);
3772 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
3775 /* disable texture unit */
3776 _mesa_set_enable(ctx
, target
, GL_FALSE
);
3778 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
3780 _mesa_meta_end(ctx
);
3782 /* restore fbo bindings */
3783 if (fboDrawSave
== fboReadSave
) {
3784 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboDrawSave
);
3787 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT
, fboDrawSave
);
3788 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT
, fboReadSave
);
3790 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, rbSave
);
3795 * This is just a wrapper around _mesa_get_tex_image() and
3796 * decompress_texture_image(). Meta functions should not be directly called
3800 _mesa_meta_GetTexImage(struct gl_context
*ctx
,
3801 GLenum format
, GLenum type
, GLvoid
*pixels
,
3802 struct gl_texture_image
*texImage
)
3804 /* We can only use the decompress-with-blit method here if the texels are
3805 * unsigned, normalized values. We could handle signed and unnormalized
3806 * with floating point renderbuffers...
3808 if (_mesa_is_format_compressed(texImage
->TexFormat
) &&
3809 _mesa_get_format_datatype(texImage
->TexFormat
)
3810 == GL_UNSIGNED_NORMALIZED
) {
3811 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3812 const GLuint slice
= 0; /* only 2D compressed textures for now */
3813 /* Need to unlock the texture here to prevent deadlock... */
3814 _mesa_unlock_texture(ctx
, texObj
);
3815 decompress_texture_image(ctx
, texImage
, slice
, format
, type
, pixels
);
3816 /* ... and relock it */
3817 _mesa_lock_texture(ctx
, texObj
);
3820 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
3826 * Meta implementation of ctx->Driver.DrawTex() in terms
3827 * of polygon rendering.
3830 _mesa_meta_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
3831 GLfloat width
, GLfloat height
)
3833 struct drawtex_state
*drawtex
= &ctx
->Meta
->DrawTex
;
3835 GLfloat x
, y
, z
, st
[MAX_TEXTURE_UNITS
][2];
3837 struct vertex verts
[4];
3840 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
3842 MESA_META_TRANSFORM
|
3844 MESA_META_VIEWPORT
));
3846 if (drawtex
->ArrayObj
== 0) {
3847 /* one-time setup */
3848 GLint active_texture
;
3850 /* create vertex array object */
3851 _mesa_GenVertexArrays(1, &drawtex
->ArrayObj
);
3852 _mesa_BindVertexArray(drawtex
->ArrayObj
);
3854 /* create vertex array buffer */
3855 _mesa_GenBuffersARB(1, &drawtex
->VBO
);
3856 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3857 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3858 NULL
, GL_DYNAMIC_DRAW_ARB
);
3860 /* client active texture is not part of the array object */
3861 active_texture
= ctx
->Array
.ActiveTexture
;
3863 /* setup vertex arrays */
3864 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3865 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3866 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3867 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ i
);
3868 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(st
[i
]));
3869 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3872 /* restore client active texture */
3873 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ active_texture
);
3876 _mesa_BindVertexArray(drawtex
->ArrayObj
);
3877 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3880 /* vertex positions, texcoords */
3882 const GLfloat x1
= x
+ width
;
3883 const GLfloat y1
= y
+ height
;
3885 z
= CLAMP(z
, 0.0, 1.0);
3904 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3905 const struct gl_texture_object
*texObj
;
3906 const struct gl_texture_image
*texImage
;
3907 GLfloat s
, t
, s1
, t1
;
3910 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
3912 for (j
= 0; j
< 4; j
++) {
3913 verts
[j
].st
[i
][0] = 0.0f
;
3914 verts
[j
].st
[i
][1] = 0.0f
;
3919 texObj
= ctx
->Texture
.Unit
[i
]._Current
;
3920 texImage
= texObj
->Image
[0][texObj
->BaseLevel
];
3921 tw
= texImage
->Width2
;
3922 th
= texImage
->Height2
;
3924 s
= (GLfloat
) texObj
->CropRect
[0] / tw
;
3925 t
= (GLfloat
) texObj
->CropRect
[1] / th
;
3926 s1
= (GLfloat
) (texObj
->CropRect
[0] + texObj
->CropRect
[2]) / tw
;
3927 t1
= (GLfloat
) (texObj
->CropRect
[1] + texObj
->CropRect
[3]) / th
;
3929 verts
[0].st
[i
][0] = s
;
3930 verts
[0].st
[i
][1] = t
;
3932 verts
[1].st
[i
][0] = s1
;
3933 verts
[1].st
[i
][1] = t
;
3935 verts
[2].st
[i
][0] = s1
;
3936 verts
[2].st
[i
][1] = t1
;
3938 verts
[3].st
[i
][0] = s
;
3939 verts
[3].st
[i
][1] = t1
;
3942 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3945 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3947 _mesa_meta_end(ctx
);