2 * Mesa 3-D graphics library
4 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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/multisample.h"
55 #include "main/objectlabel.h"
56 #include "main/pipelineobj.h"
57 #include "main/pixel.h"
59 #include "main/polygon.h"
60 #include "main/queryobj.h"
61 #include "main/readpix.h"
62 #include "main/scissor.h"
63 #include "main/shaderapi.h"
64 #include "main/shaderobj.h"
65 #include "main/state.h"
66 #include "main/stencil.h"
67 #include "main/texobj.h"
68 #include "main/texenv.h"
69 #include "main/texgetimage.h"
70 #include "main/teximage.h"
71 #include "main/texparam.h"
72 #include "main/texstate.h"
73 #include "main/transformfeedback.h"
74 #include "main/uniforms.h"
75 #include "main/varray.h"
76 #include "main/viewport.h"
77 #include "main/samplerobj.h"
78 #include "program/program.h"
79 #include "swrast/swrast.h"
80 #include "drivers/common/meta.h"
81 #include "main/enums.h"
82 #include "main/glformats.h"
83 #include "../glsl/ralloc.h"
85 /** Return offset in bytes of the field within a vertex struct */
86 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
88 static struct blit_shader
*
89 choose_blit_shader(GLenum target
, struct blit_shader_table
*table
);
91 static void cleanup_temp_texture(struct temp_texture
*tex
);
92 static void meta_glsl_clear_cleanup(struct clear_state
*clear
);
93 static void meta_decompress_cleanup(struct decompress_state
*decompress
);
94 static void meta_drawpix_cleanup(struct drawpix_state
*drawpix
);
97 _mesa_meta_bind_fbo_image(struct gl_texture_image
*texImage
, GLuint layer
)
99 struct gl_texture_object
*texObj
= texImage
->TexObject
;
100 int level
= texImage
->Level
;
101 GLenum target
= texObj
->Target
;
105 _mesa_FramebufferTexture1D(GL_FRAMEBUFFER
,
106 GL_COLOR_ATTACHMENT0
,
111 case GL_TEXTURE_1D_ARRAY
:
112 case GL_TEXTURE_2D_ARRAY
:
113 case GL_TEXTURE_CUBE_MAP_ARRAY
:
115 _mesa_FramebufferTextureLayer(GL_FRAMEBUFFER
,
116 GL_COLOR_ATTACHMENT0
,
121 default: /* 2D / cube */
122 if (target
== GL_TEXTURE_CUBE_MAP
)
123 target
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
125 _mesa_FramebufferTexture2D(GL_FRAMEBUFFER
,
126 GL_COLOR_ATTACHMENT0
,
134 _mesa_meta_compile_shader_with_debug(struct gl_context
*ctx
, GLenum target
,
135 const GLcharARB
*source
)
141 shader
= _mesa_CreateShader(target
);
142 _mesa_ShaderSource(shader
, 1, &source
, NULL
);
143 _mesa_CompileShader(shader
);
145 _mesa_GetShaderiv(shader
, GL_COMPILE_STATUS
, &ok
);
149 _mesa_GetShaderiv(shader
, GL_INFO_LOG_LENGTH
, &size
);
151 _mesa_DeleteShader(shader
);
157 _mesa_DeleteShader(shader
);
161 _mesa_GetShaderInfoLog(shader
, size
, NULL
, info
);
163 "meta program compile failed:\n%s\n"
168 _mesa_DeleteShader(shader
);
174 _mesa_meta_link_program_with_debug(struct gl_context
*ctx
, GLuint program
)
179 _mesa_LinkProgram(program
);
181 _mesa_GetProgramiv(program
, GL_LINK_STATUS
, &ok
);
185 _mesa_GetProgramiv(program
, GL_INFO_LOG_LENGTH
, &size
);
193 _mesa_GetProgramInfoLog(program
, size
, NULL
, info
);
194 _mesa_problem(ctx
, "meta program link failed:\n%s", info
);
202 * Generate a generic shader to blit from a texture to a framebuffer
204 * \param ctx Current GL context
205 * \param texTarget Texture target that will be the source of the blit
207 * \returns a handle to a shader program on success or zero on failure.
210 _mesa_meta_setup_blit_shader(struct gl_context
*ctx
,
212 struct blit_shader_table
*table
)
214 const char *vs_source
;
217 void *const mem_ctx
= ralloc_context(NULL
);
218 struct blit_shader
*shader
= choose_blit_shader(target
, table
);
221 assert(shader
!= NULL
);
223 if (shader
->shader_prog
!= 0) {
224 _mesa_UseProgram(shader
->shader_prog
);
228 if (ctx
->Const
.GLSLVersion
< 130) {
230 "attribute vec2 position;\n"
231 "attribute vec4 textureCoords;\n"
232 "varying vec4 texCoords;\n"
235 " texCoords = textureCoords;\n"
236 " gl_Position = vec4(position, 0.0, 1.0);\n"
239 fs_source
= ralloc_asprintf(mem_ctx
,
240 "#extension GL_EXT_texture_array : enable\n"
241 "#extension GL_ARB_texture_cube_map_array: enable\n"
242 "uniform %s texSampler;\n"
243 "varying vec4 texCoords;\n"
246 " gl_FragColor = %s(texSampler, %s);\n"
247 " gl_FragDepth = gl_FragColor.x;\n"
250 shader
->func
, shader
->texcoords
);
253 vs_source
= ralloc_asprintf(mem_ctx
,
255 "in vec2 position;\n"
256 "in vec4 textureCoords;\n"
257 "out vec4 texCoords;\n"
260 " texCoords = textureCoords;\n"
261 " gl_Position = vec4(position, 0.0, 1.0);\n"
263 fs_source
= ralloc_asprintf(mem_ctx
,
265 "#extension GL_ARB_texture_cube_map_array: enable\n"
266 "uniform %s texSampler;\n"
267 "in vec4 texCoords;\n"
268 "out vec4 out_color;\n"
272 " out_color = texture(texSampler, %s);\n"
273 " gl_FragDepth = out_color.x;\n"
279 vs
= _mesa_meta_compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_source
);
280 fs
= _mesa_meta_compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_source
);
282 shader
->shader_prog
= _mesa_CreateProgram();
283 _mesa_AttachShader(shader
->shader_prog
, fs
);
284 _mesa_DeleteShader(fs
);
285 _mesa_AttachShader(shader
->shader_prog
, vs
);
286 _mesa_DeleteShader(vs
);
287 _mesa_BindAttribLocation(shader
->shader_prog
, 0, "position");
288 _mesa_BindAttribLocation(shader
->shader_prog
, 1, "texcoords");
289 _mesa_meta_link_program_with_debug(ctx
, shader
->shader_prog
);
290 name
= ralloc_asprintf(mem_ctx
, "%s blit", shader
->type
);
291 _mesa_ObjectLabel(GL_PROGRAM
, shader
->shader_prog
, -1, name
);
292 ralloc_free(mem_ctx
);
294 _mesa_UseProgram(shader
->shader_prog
);
298 * Configure vertex buffer and vertex array objects for tests
300 * Regardless of whether a new VAO and new VBO are created, the objects
301 * referenced by \c VAO and \c VBO will be bound into the GL state vector
302 * when this function terminates.
304 * \param VAO Storage for vertex array object handle. If 0, a new VAO
306 * \param VBO Storage for vertex buffer object handle. If 0, a new VBO
307 * will be created. The new VBO will have storage for 4
308 * \c vertex structures.
309 * \param use_generic_attributes Should generic attributes 0 and 1 be used,
310 * or should traditional, fixed-function color and texture
311 * coordinate be used?
312 * \param vertex_size Number of components for attribute 0 / vertex.
313 * \param texcoord_size Number of components for attribute 1 / texture
314 * coordinate. If this is 0, attribute 1 will not be set or
316 * \param color_size Number of components for attribute 1 / primary color.
317 * If this is 0, attribute 1 will not be set or enabled.
319 * \note If \c use_generic_attributes is \c true, \c color_size must be zero.
320 * Use \c texcoord_size instead.
323 _mesa_meta_setup_vertex_objects(GLuint
*VAO
, GLuint
*VBO
,
324 bool use_generic_attributes
,
325 unsigned vertex_size
, unsigned texcoord_size
,
331 /* create vertex array object */
332 _mesa_GenVertexArrays(1, VAO
);
333 _mesa_BindVertexArray(*VAO
);
335 /* create vertex array buffer */
336 _mesa_GenBuffers(1, VBO
);
337 _mesa_BindBuffer(GL_ARRAY_BUFFER
, *VBO
);
338 _mesa_BufferData(GL_ARRAY_BUFFER
, 4 * sizeof(struct vertex
), NULL
,
341 /* setup vertex arrays */
342 if (use_generic_attributes
) {
343 assert(color_size
== 0);
345 _mesa_VertexAttribPointer(0, vertex_size
, GL_FLOAT
, GL_FALSE
,
346 sizeof(struct vertex
), OFFSET(x
));
347 _mesa_EnableVertexAttribArray(0);
349 if (texcoord_size
> 0) {
350 _mesa_VertexAttribPointer(1, texcoord_size
, GL_FLOAT
, GL_FALSE
,
351 sizeof(struct vertex
), OFFSET(tex
));
352 _mesa_EnableVertexAttribArray(1);
355 _mesa_VertexPointer(vertex_size
, GL_FLOAT
, sizeof(struct vertex
),
357 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
359 if (texcoord_size
> 0) {
360 _mesa_TexCoordPointer(texcoord_size
, GL_FLOAT
,
361 sizeof(struct vertex
), OFFSET(tex
));
362 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
365 if (color_size
> 0) {
366 _mesa_ColorPointer(color_size
, GL_FLOAT
,
367 sizeof(struct vertex
), OFFSET(r
));
368 _mesa_EnableClientState(GL_COLOR_ARRAY
);
372 _mesa_BindVertexArray(*VAO
);
373 _mesa_BindBuffer(GL_ARRAY_BUFFER
, *VBO
);
378 * Initialize meta-ops for a context.
379 * To be called once during context creation.
382 _mesa_meta_init(struct gl_context
*ctx
)
386 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
391 * Free context meta-op state.
392 * To be called once during context destruction.
395 _mesa_meta_free(struct gl_context
*ctx
)
397 GET_CURRENT_CONTEXT(old_context
);
398 _mesa_make_current(ctx
, NULL
, NULL
);
399 _mesa_meta_glsl_blit_cleanup(&ctx
->Meta
->Blit
);
400 meta_glsl_clear_cleanup(&ctx
->Meta
->Clear
);
401 _mesa_meta_glsl_generate_mipmap_cleanup(&ctx
->Meta
->Mipmap
);
402 cleanup_temp_texture(&ctx
->Meta
->TempTex
);
403 meta_decompress_cleanup(&ctx
->Meta
->Decompress
);
404 meta_drawpix_cleanup(&ctx
->Meta
->DrawPix
);
406 _mesa_make_current(old_context
, old_context
->WinSysDrawBuffer
, old_context
->WinSysReadBuffer
);
408 _mesa_make_current(NULL
, NULL
, NULL
);
415 * Enter meta state. This is like a light-weight version of glPushAttrib
416 * but it also resets most GL state back to default values.
418 * \param state bitmask of MESA_META_* flags indicating which attribute groups
419 * to save and reset to their defaults
422 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
424 struct save_state
*save
;
426 /* hope MAX_META_OPS_DEPTH is large enough */
427 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
429 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
430 memset(save
, 0, sizeof(*save
));
431 save
->SavedState
= state
;
433 /* We always push into desktop GL mode and pop out at the end. No sense in
434 * writing our shaders varying based on the user's context choice, when
435 * Mesa can handle either.
437 save
->API
= ctx
->API
;
438 ctx
->API
= API_OPENGL_COMPAT
;
440 /* Pausing transform feedback needs to be done early, or else we won't be
441 * able to change other state.
443 save
->TransformFeedbackNeedsResume
=
444 _mesa_is_xfb_active_and_unpaused(ctx
);
445 if (save
->TransformFeedbackNeedsResume
)
446 _mesa_PauseTransformFeedback();
448 /* After saving the current occlusion object, call EndQuery so that no
449 * occlusion querying will be active during the meta-operation.
451 if (state
& MESA_META_OCCLUSION_QUERY
) {
452 save
->CurrentOcclusionObject
= ctx
->Query
.CurrentOcclusionObject
;
453 if (save
->CurrentOcclusionObject
)
454 _mesa_EndQuery(save
->CurrentOcclusionObject
->Target
);
457 if (state
& MESA_META_ALPHA_TEST
) {
458 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
459 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
460 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
461 if (ctx
->Color
.AlphaEnabled
)
462 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
465 if (state
& MESA_META_BLEND
) {
466 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
467 if (ctx
->Color
.BlendEnabled
) {
468 if (ctx
->Extensions
.EXT_draw_buffers2
) {
470 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
471 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
475 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
478 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
479 if (ctx
->Color
.ColorLogicOpEnabled
)
480 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
483 if (state
& MESA_META_COLOR_MASK
) {
484 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
485 sizeof(ctx
->Color
.ColorMask
));
486 if (!ctx
->Color
.ColorMask
[0][0] ||
487 !ctx
->Color
.ColorMask
[0][1] ||
488 !ctx
->Color
.ColorMask
[0][2] ||
489 !ctx
->Color
.ColorMask
[0][3])
490 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
493 if (state
& MESA_META_DEPTH_TEST
) {
494 save
->Depth
= ctx
->Depth
; /* struct copy */
496 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
499 if (state
& MESA_META_FOG
) {
500 save
->Fog
= ctx
->Fog
.Enabled
;
501 if (ctx
->Fog
.Enabled
)
502 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
505 if (state
& MESA_META_PIXEL_STORE
) {
506 save
->Pack
= ctx
->Pack
;
507 save
->Unpack
= ctx
->Unpack
;
508 ctx
->Pack
= ctx
->DefaultPacking
;
509 ctx
->Unpack
= ctx
->DefaultPacking
;
512 if (state
& MESA_META_PIXEL_TRANSFER
) {
513 save
->RedScale
= ctx
->Pixel
.RedScale
;
514 save
->RedBias
= ctx
->Pixel
.RedBias
;
515 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
516 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
517 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
518 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
519 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
520 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
521 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
522 ctx
->Pixel
.RedScale
= 1.0F
;
523 ctx
->Pixel
.RedBias
= 0.0F
;
524 ctx
->Pixel
.GreenScale
= 1.0F
;
525 ctx
->Pixel
.GreenBias
= 0.0F
;
526 ctx
->Pixel
.BlueScale
= 1.0F
;
527 ctx
->Pixel
.BlueBias
= 0.0F
;
528 ctx
->Pixel
.AlphaScale
= 1.0F
;
529 ctx
->Pixel
.AlphaBias
= 0.0F
;
530 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
532 ctx
->NewState
|=_NEW_PIXEL
;
535 if (state
& MESA_META_RASTERIZATION
) {
536 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
537 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
538 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
539 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
540 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
541 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
542 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
543 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
544 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
545 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
546 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
549 if (state
& MESA_META_SCISSOR
) {
550 save
->Scissor
= ctx
->Scissor
; /* struct copy */
551 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
554 if (state
& MESA_META_SHADER
) {
557 if (ctx
->Extensions
.ARB_vertex_program
) {
558 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
559 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
560 ctx
->VertexProgram
.Current
);
561 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
564 if (ctx
->Extensions
.ARB_fragment_program
) {
565 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
566 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
567 ctx
->FragmentProgram
.Current
);
568 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
571 if (ctx
->Extensions
.ATI_fragment_shader
) {
572 save
->ATIFragmentShaderEnabled
= ctx
->ATIFragmentShader
.Enabled
;
573 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
, GL_FALSE
);
576 if (ctx
->Extensions
.ARB_separate_shader_objects
) {
577 /* Warning it must be done before _mesa_UseProgram call */
578 _mesa_reference_pipeline_object(ctx
, &save
->_Shader
, ctx
->_Shader
);
579 _mesa_reference_pipeline_object(ctx
, &save
->Pipeline
,
580 ctx
->Pipeline
.Current
);
581 _mesa_BindProgramPipeline(0);
584 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
585 _mesa_reference_shader_program(ctx
, &save
->Shader
[i
],
586 ctx
->_Shader
->CurrentProgram
[i
]);
588 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
589 ctx
->_Shader
->ActiveProgram
);
594 if (state
& MESA_META_STENCIL_TEST
) {
595 save
->Stencil
= ctx
->Stencil
; /* struct copy */
596 if (ctx
->Stencil
.Enabled
)
597 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
598 /* NOTE: other stencil state not reset */
601 if (state
& MESA_META_TEXTURE
) {
604 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
605 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
606 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
608 /* Disable all texture units */
609 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
610 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
611 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
612 if (ctx
->Texture
.Unit
[u
].Enabled
||
613 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
614 _mesa_ActiveTexture(GL_TEXTURE0
+ u
);
615 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
616 if (ctx
->Extensions
.ARB_texture_cube_map
)
617 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
619 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
620 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
621 if (ctx
->Extensions
.NV_texture_rectangle
)
622 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
623 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
624 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
625 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
626 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
630 /* save current texture objects for unit[0] only */
631 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
632 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
633 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
636 /* set defaults for unit[0] */
637 _mesa_ActiveTexture(GL_TEXTURE0
);
638 _mesa_ClientActiveTexture(GL_TEXTURE0
);
639 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
642 if (state
& MESA_META_TRANSFORM
) {
643 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
644 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
645 16 * sizeof(GLfloat
));
646 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
647 16 * sizeof(GLfloat
));
648 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
649 16 * sizeof(GLfloat
));
650 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
651 /* set 1:1 vertex:pixel coordinate transform */
652 _mesa_ActiveTexture(GL_TEXTURE0
);
653 _mesa_MatrixMode(GL_TEXTURE
);
654 _mesa_LoadIdentity();
655 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
656 _mesa_MatrixMode(GL_MODELVIEW
);
657 _mesa_LoadIdentity();
658 _mesa_MatrixMode(GL_PROJECTION
);
659 _mesa_LoadIdentity();
661 /* glOrtho with width = 0 or height = 0 generates GL_INVALID_VALUE.
662 * This can occur when there is no draw buffer.
664 if (ctx
->DrawBuffer
->Width
!= 0 && ctx
->DrawBuffer
->Height
!= 0)
665 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
666 0.0, ctx
->DrawBuffer
->Height
,
670 if (state
& MESA_META_CLIP
) {
671 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
672 if (ctx
->Transform
.ClipPlanesEnabled
) {
674 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
675 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
680 if (state
& MESA_META_VERTEX
) {
681 /* save vertex array object state */
682 _mesa_reference_vao(ctx
, &save
->VAO
,
684 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
685 ctx
->Array
.ArrayBufferObj
);
686 /* set some default state? */
689 if (state
& MESA_META_VIEWPORT
) {
690 /* save viewport state */
691 save
->ViewportX
= ctx
->ViewportArray
[0].X
;
692 save
->ViewportY
= ctx
->ViewportArray
[0].Y
;
693 save
->ViewportW
= ctx
->ViewportArray
[0].Width
;
694 save
->ViewportH
= ctx
->ViewportArray
[0].Height
;
695 /* set viewport to match window size */
696 if (ctx
->ViewportArray
[0].X
!= 0 ||
697 ctx
->ViewportArray
[0].Y
!= 0 ||
698 ctx
->ViewportArray
[0].Width
!= (float) ctx
->DrawBuffer
->Width
||
699 ctx
->ViewportArray
[0].Height
!= (float) ctx
->DrawBuffer
->Height
) {
700 _mesa_set_viewport(ctx
, 0, 0, 0,
701 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
703 /* save depth range state */
704 save
->DepthNear
= ctx
->ViewportArray
[0].Near
;
705 save
->DepthFar
= ctx
->ViewportArray
[0].Far
;
706 /* set depth range to default */
707 _mesa_DepthRange(0.0, 1.0);
710 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
711 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
713 /* Generally in here we want to do clamping according to whether
714 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
715 * regardless of the internal implementation of the metaops.
717 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
718 ctx
->Extensions
.ARB_color_buffer_float
)
719 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
722 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
723 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
725 /* Generally in here we never want vertex color clamping --
726 * result clamping is only dependent on fragment clamping.
728 if (ctx
->Extensions
.ARB_color_buffer_float
)
729 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
732 if (state
& MESA_META_CONDITIONAL_RENDER
) {
733 save
->CondRenderQuery
= ctx
->Query
.CondRenderQuery
;
734 save
->CondRenderMode
= ctx
->Query
.CondRenderMode
;
736 if (ctx
->Query
.CondRenderQuery
)
737 _mesa_EndConditionalRender();
740 if (state
& MESA_META_SELECT_FEEDBACK
) {
741 save
->RenderMode
= ctx
->RenderMode
;
742 if (ctx
->RenderMode
== GL_SELECT
) {
743 save
->Select
= ctx
->Select
; /* struct copy */
744 _mesa_RenderMode(GL_RENDER
);
745 } else if (ctx
->RenderMode
== GL_FEEDBACK
) {
746 save
->Feedback
= ctx
->Feedback
; /* struct copy */
747 _mesa_RenderMode(GL_RENDER
);
751 if (state
& MESA_META_MULTISAMPLE
) {
752 save
->Multisample
= ctx
->Multisample
; /* struct copy */
754 if (ctx
->Multisample
.Enabled
)
755 _mesa_set_multisample(ctx
, GL_FALSE
);
756 if (ctx
->Multisample
.SampleCoverage
)
757 _mesa_set_enable(ctx
, GL_SAMPLE_COVERAGE
, GL_FALSE
);
758 if (ctx
->Multisample
.SampleAlphaToCoverage
)
759 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_COVERAGE
, GL_FALSE
);
760 if (ctx
->Multisample
.SampleAlphaToOne
)
761 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_ONE
, GL_FALSE
);
762 if (ctx
->Multisample
.SampleShading
)
763 _mesa_set_enable(ctx
, GL_SAMPLE_SHADING
, GL_FALSE
);
764 if (ctx
->Multisample
.SampleMask
)
765 _mesa_set_enable(ctx
, GL_SAMPLE_MASK
, GL_FALSE
);
768 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
769 save
->sRGBEnabled
= ctx
->Color
.sRGBEnabled
;
770 if (ctx
->Color
.sRGBEnabled
)
771 _mesa_set_framebuffer_srgb(ctx
, GL_FALSE
);
776 save
->Lighting
= ctx
->Light
.Enabled
;
777 if (ctx
->Light
.Enabled
)
778 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
779 save
->RasterDiscard
= ctx
->RasterDiscard
;
780 if (ctx
->RasterDiscard
)
781 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_FALSE
);
783 save
->DrawBufferName
= ctx
->DrawBuffer
->Name
;
784 save
->ReadBufferName
= ctx
->ReadBuffer
->Name
;
785 save
->RenderbufferName
= (ctx
->CurrentRenderbuffer
?
786 ctx
->CurrentRenderbuffer
->Name
: 0);
792 * Leave meta state. This is like a light-weight version of glPopAttrib().
795 _mesa_meta_end(struct gl_context
*ctx
)
797 struct save_state
*save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
- 1];
798 const GLbitfield state
= save
->SavedState
;
801 /* After starting a new occlusion query, initialize the results to the
802 * values saved previously. The driver will then continue to increment
805 if (state
& MESA_META_OCCLUSION_QUERY
) {
806 if (save
->CurrentOcclusionObject
) {
807 _mesa_BeginQuery(save
->CurrentOcclusionObject
->Target
,
808 save
->CurrentOcclusionObject
->Id
);
809 ctx
->Query
.CurrentOcclusionObject
->Result
= save
->CurrentOcclusionObject
->Result
;
813 if (state
& MESA_META_ALPHA_TEST
) {
814 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
815 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
816 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
819 if (state
& MESA_META_BLEND
) {
820 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
821 if (ctx
->Extensions
.EXT_draw_buffers2
) {
823 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
824 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
828 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
831 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
832 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
835 if (state
& MESA_META_COLOR_MASK
) {
837 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
838 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
840 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
841 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
845 save
->ColorMask
[i
][0],
846 save
->ColorMask
[i
][1],
847 save
->ColorMask
[i
][2],
848 save
->ColorMask
[i
][3]);
854 if (state
& MESA_META_DEPTH_TEST
) {
855 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
856 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
857 _mesa_DepthFunc(save
->Depth
.Func
);
858 _mesa_DepthMask(save
->Depth
.Mask
);
861 if (state
& MESA_META_FOG
) {
862 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
865 if (state
& MESA_META_PIXEL_STORE
) {
866 ctx
->Pack
= save
->Pack
;
867 ctx
->Unpack
= save
->Unpack
;
870 if (state
& MESA_META_PIXEL_TRANSFER
) {
871 ctx
->Pixel
.RedScale
= save
->RedScale
;
872 ctx
->Pixel
.RedBias
= save
->RedBias
;
873 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
874 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
875 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
876 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
877 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
878 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
879 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
881 ctx
->NewState
|=_NEW_PIXEL
;
884 if (state
& MESA_META_RASTERIZATION
) {
885 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
886 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
887 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
888 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
889 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
890 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
893 if (state
& MESA_META_SCISSOR
) {
896 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++) {
897 _mesa_set_scissor(ctx
, i
,
898 save
->Scissor
.ScissorArray
[i
].X
,
899 save
->Scissor
.ScissorArray
[i
].Y
,
900 save
->Scissor
.ScissorArray
[i
].Width
,
901 save
->Scissor
.ScissorArray
[i
].Height
);
902 _mesa_set_enablei(ctx
, GL_SCISSOR_TEST
, i
,
903 (save
->Scissor
.EnableFlags
>> i
) & 1);
907 if (state
& MESA_META_SHADER
) {
908 if (ctx
->Extensions
.ARB_vertex_program
) {
909 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
910 save
->VertexProgramEnabled
);
911 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
912 save
->VertexProgram
);
913 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
916 if (ctx
->Extensions
.ARB_fragment_program
) {
917 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
918 save
->FragmentProgramEnabled
);
919 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
920 save
->FragmentProgram
);
921 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
924 if (ctx
->Extensions
.ATI_fragment_shader
) {
925 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
,
926 save
->ATIFragmentShaderEnabled
);
929 /* Warning it must be done before _mesa_use_shader_program call */
930 if (ctx
->Extensions
.ARB_separate_shader_objects
) {
931 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, save
->_Shader
);
932 _mesa_reference_pipeline_object(ctx
, &ctx
->Pipeline
.Current
,
934 _mesa_reference_pipeline_object(ctx
, &save
->Pipeline
, NULL
);
937 if (ctx
->Extensions
.ARB_vertex_shader
) {
938 _mesa_use_shader_program(ctx
, GL_VERTEX_SHADER
,
939 save
->Shader
[MESA_SHADER_VERTEX
],
943 if (_mesa_has_geometry_shaders(ctx
))
944 _mesa_use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
,
945 save
->Shader
[MESA_SHADER_GEOMETRY
],
948 if (ctx
->Extensions
.ARB_fragment_shader
)
949 _mesa_use_shader_program(ctx
, GL_FRAGMENT_SHADER
,
950 save
->Shader
[MESA_SHADER_FRAGMENT
],
953 _mesa_reference_shader_program(ctx
, &ctx
->_Shader
->ActiveProgram
,
956 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++)
957 _mesa_reference_shader_program(ctx
, &save
->Shader
[i
], NULL
);
958 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
, NULL
);
959 _mesa_reference_pipeline_object(ctx
, &save
->_Shader
, NULL
);
962 if (state
& MESA_META_STENCIL_TEST
) {
963 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
965 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
966 _mesa_ClearStencil(stencil
->Clear
);
967 if (ctx
->Extensions
.EXT_stencil_two_side
) {
968 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
969 stencil
->TestTwoSide
);
970 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
971 ? GL_BACK
: GL_FRONT
);
974 _mesa_StencilFuncSeparate(GL_FRONT
,
975 stencil
->Function
[0],
977 stencil
->ValueMask
[0]);
978 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
979 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
980 stencil
->ZFailFunc
[0],
981 stencil
->ZPassFunc
[0]);
983 _mesa_StencilFuncSeparate(GL_BACK
,
984 stencil
->Function
[1],
986 stencil
->ValueMask
[1]);
987 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
988 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
989 stencil
->ZFailFunc
[1],
990 stencil
->ZPassFunc
[1]);
993 if (state
& MESA_META_TEXTURE
) {
996 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
998 /* restore texenv for unit[0] */
999 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
1001 /* restore texture objects for unit[0] only */
1002 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
1003 if (ctx
->Texture
.Unit
[0].CurrentTex
[tgt
] != save
->CurrentTexture
[tgt
]) {
1004 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1005 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
1006 save
->CurrentTexture
[tgt
]);
1008 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
1011 /* Restore fixed function texture enables, texgen */
1012 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
1013 if (ctx
->Texture
.Unit
[u
].Enabled
!= save
->TexEnabled
[u
]) {
1014 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1015 ctx
->Texture
.Unit
[u
].Enabled
= save
->TexEnabled
[u
];
1018 if (ctx
->Texture
.Unit
[u
].TexGenEnabled
!= save
->TexGenEnabled
[u
]) {
1019 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1020 ctx
->Texture
.Unit
[u
].TexGenEnabled
= save
->TexGenEnabled
[u
];
1024 /* restore current unit state */
1025 _mesa_ActiveTexture(GL_TEXTURE0
+ save
->ActiveUnit
);
1026 _mesa_ClientActiveTexture(GL_TEXTURE0
+ save
->ClientActiveUnit
);
1029 if (state
& MESA_META_TRANSFORM
) {
1030 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
1031 _mesa_ActiveTexture(GL_TEXTURE0
);
1032 _mesa_MatrixMode(GL_TEXTURE
);
1033 _mesa_LoadMatrixf(save
->TextureMatrix
);
1034 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
1036 _mesa_MatrixMode(GL_MODELVIEW
);
1037 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
1039 _mesa_MatrixMode(GL_PROJECTION
);
1040 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
1042 _mesa_MatrixMode(save
->MatrixMode
);
1045 if (state
& MESA_META_CLIP
) {
1046 if (save
->ClipPlanesEnabled
) {
1048 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
1049 if (save
->ClipPlanesEnabled
& (1 << i
)) {
1050 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
1056 if (state
& MESA_META_VERTEX
) {
1057 /* restore vertex buffer object */
1058 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
1059 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
1061 /* restore vertex array object */
1062 _mesa_BindVertexArray(save
->VAO
->Name
);
1063 _mesa_reference_vao(ctx
, &save
->VAO
, NULL
);
1066 if (state
& MESA_META_VIEWPORT
) {
1067 if (save
->ViewportX
!= ctx
->ViewportArray
[0].X
||
1068 save
->ViewportY
!= ctx
->ViewportArray
[0].Y
||
1069 save
->ViewportW
!= ctx
->ViewportArray
[0].Width
||
1070 save
->ViewportH
!= ctx
->ViewportArray
[0].Height
) {
1071 _mesa_set_viewport(ctx
, 0, save
->ViewportX
, save
->ViewportY
,
1072 save
->ViewportW
, save
->ViewportH
);
1074 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
1077 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
&&
1078 ctx
->Extensions
.ARB_color_buffer_float
) {
1079 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
1082 if (state
& MESA_META_CLAMP_VERTEX_COLOR
&&
1083 ctx
->Extensions
.ARB_color_buffer_float
) {
1084 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
1087 if (state
& MESA_META_CONDITIONAL_RENDER
) {
1088 if (save
->CondRenderQuery
)
1089 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
1090 save
->CondRenderMode
);
1093 if (state
& MESA_META_SELECT_FEEDBACK
) {
1094 if (save
->RenderMode
== GL_SELECT
) {
1095 _mesa_RenderMode(GL_SELECT
);
1096 ctx
->Select
= save
->Select
;
1097 } else if (save
->RenderMode
== GL_FEEDBACK
) {
1098 _mesa_RenderMode(GL_FEEDBACK
);
1099 ctx
->Feedback
= save
->Feedback
;
1103 if (state
& MESA_META_MULTISAMPLE
) {
1104 struct gl_multisample_attrib
*ctx_ms
= &ctx
->Multisample
;
1105 struct gl_multisample_attrib
*save_ms
= &save
->Multisample
;
1107 if (ctx_ms
->Enabled
!= save_ms
->Enabled
)
1108 _mesa_set_multisample(ctx
, save_ms
->Enabled
);
1109 if (ctx_ms
->SampleCoverage
!= save_ms
->SampleCoverage
)
1110 _mesa_set_enable(ctx
, GL_SAMPLE_COVERAGE
, save_ms
->SampleCoverage
);
1111 if (ctx_ms
->SampleAlphaToCoverage
!= save_ms
->SampleAlphaToCoverage
)
1112 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_COVERAGE
, save_ms
->SampleAlphaToCoverage
);
1113 if (ctx_ms
->SampleAlphaToOne
!= save_ms
->SampleAlphaToOne
)
1114 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_ONE
, save_ms
->SampleAlphaToOne
);
1115 if (ctx_ms
->SampleCoverageValue
!= save_ms
->SampleCoverageValue
||
1116 ctx_ms
->SampleCoverageInvert
!= save_ms
->SampleCoverageInvert
) {
1117 _mesa_SampleCoverage(save_ms
->SampleCoverageValue
,
1118 save_ms
->SampleCoverageInvert
);
1120 if (ctx_ms
->SampleShading
!= save_ms
->SampleShading
)
1121 _mesa_set_enable(ctx
, GL_SAMPLE_SHADING
, save_ms
->SampleShading
);
1122 if (ctx_ms
->SampleMask
!= save_ms
->SampleMask
)
1123 _mesa_set_enable(ctx
, GL_SAMPLE_MASK
, save_ms
->SampleMask
);
1124 if (ctx_ms
->SampleMaskValue
!= save_ms
->SampleMaskValue
)
1125 _mesa_SampleMaski(0, save_ms
->SampleMaskValue
);
1126 if (ctx_ms
->MinSampleShadingValue
!= save_ms
->MinSampleShadingValue
)
1127 _mesa_MinSampleShading(save_ms
->MinSampleShadingValue
);
1130 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
1131 if (ctx
->Color
.sRGBEnabled
!= save
->sRGBEnabled
)
1132 _mesa_set_framebuffer_srgb(ctx
, save
->sRGBEnabled
);
1136 if (save
->Lighting
) {
1137 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
1139 if (save
->RasterDiscard
) {
1140 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_TRUE
);
1142 if (save
->TransformFeedbackNeedsResume
)
1143 _mesa_ResumeTransformFeedback();
1145 if (ctx
->DrawBuffer
->Name
!= save
->DrawBufferName
)
1146 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, save
->DrawBufferName
);
1148 if (ctx
->ReadBuffer
->Name
!= save
->ReadBufferName
)
1149 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER
, save
->ReadBufferName
);
1151 if (!ctx
->CurrentRenderbuffer
||
1152 ctx
->CurrentRenderbuffer
->Name
!= save
->RenderbufferName
)
1153 _mesa_BindRenderbuffer(GL_RENDERBUFFER
, save
->RenderbufferName
);
1155 ctx
->Meta
->SaveStackDepth
--;
1157 ctx
->API
= save
->API
;
1162 * Determine whether Mesa is currently in a meta state.
1165 _mesa_meta_in_progress(struct gl_context
*ctx
)
1167 return ctx
->Meta
->SaveStackDepth
!= 0;
1172 * Convert Z from a normalized value in the range [0, 1] to an object-space
1173 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1174 * default/identity ortho projection results in the original Z value.
1175 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1176 * value comes from the clear value or raster position.
1178 static INLINE GLfloat
1179 invert_z(GLfloat normZ
)
1181 GLfloat objZ
= 1.0f
- 2.0f
* normZ
;
1187 * One-time init for a temp_texture object.
1188 * Choose tex target, compute max tex size, etc.
1191 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1193 /* prefer texture rectangle */
1194 if (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.NV_texture_rectangle
) {
1195 tex
->Target
= GL_TEXTURE_RECTANGLE
;
1196 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
1197 tex
->NPOT
= GL_TRUE
;
1200 /* use 2D texture, NPOT if possible */
1201 tex
->Target
= GL_TEXTURE_2D
;
1202 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1203 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
1205 tex
->MinSize
= 16; /* 16 x 16 at least */
1206 assert(tex
->MaxSize
> 0);
1208 _mesa_GenTextures(1, &tex
->TexObj
);
1212 cleanup_temp_texture(struct temp_texture
*tex
)
1216 _mesa_DeleteTextures(1, &tex
->TexObj
);
1222 * Return pointer to temp_texture info for non-bitmap ops.
1223 * This does some one-time init if needed.
1225 struct temp_texture
*
1226 _mesa_meta_get_temp_texture(struct gl_context
*ctx
)
1228 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
1231 init_temp_texture(ctx
, tex
);
1239 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1240 * We use a separate texture for bitmaps to reduce texture
1241 * allocation/deallocation.
1243 static struct temp_texture
*
1244 get_bitmap_temp_texture(struct gl_context
*ctx
)
1246 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
1249 init_temp_texture(ctx
, tex
);
1256 * Return pointer to depth temp_texture.
1257 * This does some one-time init if needed.
1259 struct temp_texture
*
1260 _mesa_meta_get_temp_depth_texture(struct gl_context
*ctx
)
1262 struct temp_texture
*tex
= &ctx
->Meta
->Blit
.depthTex
;
1265 init_temp_texture(ctx
, tex
);
1272 * Compute the width/height of texture needed to draw an image of the
1273 * given size. Return a flag indicating whether the current texture
1274 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1275 * allocated (glTexImage2D).
1276 * Also, compute s/t texcoords for drawing.
1278 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1281 _mesa_meta_alloc_texture(struct temp_texture
*tex
,
1282 GLsizei width
, GLsizei height
, GLenum intFormat
)
1284 GLboolean newTex
= GL_FALSE
;
1286 ASSERT(width
<= tex
->MaxSize
);
1287 ASSERT(height
<= tex
->MaxSize
);
1289 if (width
> tex
->Width
||
1290 height
> tex
->Height
||
1291 intFormat
!= tex
->IntFormat
) {
1292 /* alloc new texture (larger or different format) */
1295 /* use non-power of two size */
1296 tex
->Width
= MAX2(tex
->MinSize
, width
);
1297 tex
->Height
= MAX2(tex
->MinSize
, height
);
1300 /* find power of two size */
1302 w
= h
= tex
->MinSize
;
1311 tex
->IntFormat
= intFormat
;
1316 /* compute texcoords */
1317 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1318 tex
->Sright
= (GLfloat
) width
;
1319 tex
->Ttop
= (GLfloat
) height
;
1322 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1323 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1331 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1334 _mesa_meta_setup_copypix_texture(struct gl_context
*ctx
,
1335 struct temp_texture
*tex
,
1336 GLint srcX
, GLint srcY
,
1337 GLsizei width
, GLsizei height
,
1343 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1344 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1345 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1346 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1348 newTex
= _mesa_meta_alloc_texture(tex
, width
, height
, intFormat
);
1350 /* copy framebuffer image to texture */
1352 /* create new tex image */
1353 if (tex
->Width
== width
&& tex
->Height
== height
) {
1354 /* create new tex with framebuffer data */
1355 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1356 srcX
, srcY
, width
, height
, 0);
1359 /* create empty texture */
1360 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1361 tex
->Width
, tex
->Height
, 0,
1362 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1364 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1365 0, 0, srcX
, srcY
, width
, height
);
1369 /* replace existing tex image */
1370 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1371 0, 0, srcX
, srcY
, width
, height
);
1377 * Setup/load texture for glDrawPixels.
1380 _mesa_meta_setup_drawpix_texture(struct gl_context
*ctx
,
1381 struct temp_texture
*tex
,
1383 GLsizei width
, GLsizei height
,
1384 GLenum format
, GLenum type
,
1385 const GLvoid
*pixels
)
1387 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1388 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1389 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1390 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1392 /* copy pixel data to texture */
1394 /* create new tex image */
1395 if (tex
->Width
== width
&& tex
->Height
== height
) {
1396 /* create new tex and load image data */
1397 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1398 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1401 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1403 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1404 ctx
->Unpack
.BufferObj
);
1405 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1406 /* create empty texture */
1407 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1408 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1409 if (save_unpack_obj
!= NULL
)
1410 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
,
1411 save_unpack_obj
->Name
);
1413 _mesa_TexSubImage2D(tex
->Target
, 0,
1414 0, 0, width
, height
, format
, type
, pixels
);
1418 /* replace existing tex image */
1419 _mesa_TexSubImage2D(tex
->Target
, 0,
1420 0, 0, width
, height
, format
, type
, pixels
);
1425 _mesa_meta_setup_ff_tnl_for_blit(GLuint
*VAO
, GLuint
*VBO
,
1426 unsigned texcoord_size
)
1428 _mesa_meta_setup_vertex_objects(VAO
, VBO
, false, 2, texcoord_size
, 0);
1430 /* setup projection matrix */
1431 _mesa_MatrixMode(GL_PROJECTION
);
1432 _mesa_LoadIdentity();
1436 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1439 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1441 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1442 struct vertex verts
[4];
1443 /* save all state but scissor, pixel pack/unpack */
1444 GLbitfield metaSave
= (MESA_META_ALL
-
1446 MESA_META_PIXEL_STORE
-
1447 MESA_META_CONDITIONAL_RENDER
-
1448 MESA_META_FRAMEBUFFER_SRGB
);
1449 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1451 if (buffers
& BUFFER_BITS_COLOR
) {
1452 /* if clearing color buffers, don't save/restore colormask */
1453 metaSave
-= MESA_META_COLOR_MASK
;
1456 _mesa_meta_begin(ctx
, metaSave
);
1458 _mesa_meta_setup_vertex_objects(&clear
->VAO
, &clear
->VBO
, false, 3, 0, 4);
1460 /* GL_COLOR_BUFFER_BIT */
1461 if (buffers
& BUFFER_BITS_COLOR
) {
1462 /* leave colormask, glDrawBuffer state as-is */
1464 /* Clears never have the color clamped. */
1465 if (ctx
->Extensions
.ARB_color_buffer_float
)
1466 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1469 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1470 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1473 /* GL_DEPTH_BUFFER_BIT */
1474 if (buffers
& BUFFER_BIT_DEPTH
) {
1475 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1476 _mesa_DepthFunc(GL_ALWAYS
);
1477 _mesa_DepthMask(GL_TRUE
);
1480 assert(!ctx
->Depth
.Test
);
1483 /* GL_STENCIL_BUFFER_BIT */
1484 if (buffers
& BUFFER_BIT_STENCIL
) {
1485 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1486 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1487 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1488 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1489 ctx
->Stencil
.Clear
& stencilMax
,
1490 ctx
->Stencil
.WriteMask
[0]);
1493 assert(!ctx
->Stencil
.Enabled
);
1496 /* vertex positions/colors */
1498 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1499 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1500 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1501 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1502 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1519 for (i
= 0; i
< 4; i
++) {
1520 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
1521 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
1522 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
1523 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
1526 /* upload new vertex data */
1527 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1528 GL_DYNAMIC_DRAW_ARB
);
1532 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1534 _mesa_meta_end(ctx
);
1538 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
1540 const char *vs_source
=
1541 "attribute vec4 position;\n"
1544 " gl_Position = position;\n"
1546 const char *gs_source
=
1548 "layout(triangles) in;\n"
1549 "layout(triangle_strip, max_vertices = 4) out;\n"
1550 "uniform int layer;\n"
1553 " for (int i = 0; i < 3; i++) {\n"
1554 " gl_Layer = layer;\n"
1555 " gl_Position = gl_in[i].gl_Position;\n"
1559 const char *fs_source
=
1560 "uniform vec4 color;\n"
1563 " gl_FragColor = color;\n"
1565 GLuint vs
, gs
= 0, fs
;
1566 bool has_integer_textures
;
1568 _mesa_meta_setup_vertex_objects(&clear
->VAO
, &clear
->VBO
, true, 3, 0, 0);
1570 if (clear
->ShaderProg
!= 0)
1573 vs
= _mesa_CreateShader(GL_VERTEX_SHADER
);
1574 _mesa_ShaderSource(vs
, 1, &vs_source
, NULL
);
1575 _mesa_CompileShader(vs
);
1577 if (_mesa_has_geometry_shaders(ctx
)) {
1578 gs
= _mesa_CreateShader(GL_GEOMETRY_SHADER
);
1579 _mesa_ShaderSource(gs
, 1, &gs_source
, NULL
);
1580 _mesa_CompileShader(gs
);
1583 fs
= _mesa_CreateShader(GL_FRAGMENT_SHADER
);
1584 _mesa_ShaderSource(fs
, 1, &fs_source
, NULL
);
1585 _mesa_CompileShader(fs
);
1587 clear
->ShaderProg
= _mesa_CreateProgram();
1588 _mesa_AttachShader(clear
->ShaderProg
, fs
);
1589 _mesa_DeleteShader(fs
);
1591 _mesa_AttachShader(clear
->ShaderProg
, gs
);
1592 _mesa_AttachShader(clear
->ShaderProg
, vs
);
1593 _mesa_DeleteShader(vs
);
1594 _mesa_BindAttribLocation(clear
->ShaderProg
, 0, "position");
1595 _mesa_LinkProgram(clear
->ShaderProg
);
1597 clear
->ColorLocation
= _mesa_GetUniformLocation(clear
->ShaderProg
,
1600 clear
->LayerLocation
= _mesa_GetUniformLocation(clear
->ShaderProg
,
1604 has_integer_textures
= _mesa_is_gles3(ctx
) ||
1605 (_mesa_is_desktop_gl(ctx
) && ctx
->Const
.GLSLVersion
>= 130);
1607 if (has_integer_textures
) {
1608 void *shader_source_mem_ctx
= ralloc_context(NULL
);
1609 const char *vs_int_source
=
1610 ralloc_asprintf(shader_source_mem_ctx
,
1612 "in vec4 position;\n"
1615 " gl_Position = position;\n"
1617 const char *fs_int_source
=
1618 ralloc_asprintf(shader_source_mem_ctx
,
1620 "uniform ivec4 color;\n"
1621 "out ivec4 out_color;\n"
1625 " out_color = color;\n"
1628 vs
= _mesa_meta_compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
,
1630 fs
= _mesa_meta_compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
,
1632 ralloc_free(shader_source_mem_ctx
);
1634 clear
->IntegerShaderProg
= _mesa_CreateProgram();
1635 _mesa_AttachShader(clear
->IntegerShaderProg
, fs
);
1636 _mesa_DeleteShader(fs
);
1638 _mesa_AttachShader(clear
->IntegerShaderProg
, gs
);
1639 _mesa_AttachShader(clear
->IntegerShaderProg
, vs
);
1640 _mesa_DeleteShader(vs
);
1641 _mesa_BindAttribLocation(clear
->IntegerShaderProg
, 0, "position");
1643 /* Note that user-defined out attributes get automatically assigned
1644 * locations starting from 0, so we don't need to explicitly
1645 * BindFragDataLocation to 0.
1648 _mesa_ObjectLabel(GL_PROGRAM
, clear
->IntegerShaderProg
, -1,
1650 _mesa_meta_link_program_with_debug(ctx
, clear
->IntegerShaderProg
);
1652 clear
->IntegerColorLocation
=
1653 _mesa_GetUniformLocation(clear
->IntegerShaderProg
, "color");
1655 clear
->IntegerLayerLocation
=
1656 _mesa_GetUniformLocation(clear
->IntegerShaderProg
, "layer");
1660 _mesa_DeleteShader(gs
);
1664 meta_glsl_clear_cleanup(struct clear_state
*clear
)
1666 if (clear
->VAO
== 0)
1668 _mesa_DeleteVertexArrays(1, &clear
->VAO
);
1670 _mesa_DeleteBuffers(1, &clear
->VBO
);
1672 _mesa_DeleteProgram(clear
->ShaderProg
);
1673 clear
->ShaderProg
= 0;
1675 if (clear
->IntegerShaderProg
) {
1676 _mesa_DeleteProgram(clear
->IntegerShaderProg
);
1677 clear
->IntegerShaderProg
= 0;
1682 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1685 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1687 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1688 GLbitfield metaSave
;
1689 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1690 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1691 const float x0
= ((float)fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
1692 const float y0
= ((float)fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
1693 const float x1
= ((float)fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
1694 const float y1
= ((float)fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
1695 const float z
= -invert_z(ctx
->Depth
.Clear
);
1696 struct vertex verts
[4];
1698 metaSave
= (MESA_META_ALPHA_TEST
|
1700 MESA_META_DEPTH_TEST
|
1701 MESA_META_RASTERIZATION
|
1703 MESA_META_STENCIL_TEST
|
1705 MESA_META_VIEWPORT
|
1707 MESA_META_CLAMP_FRAGMENT_COLOR
|
1708 MESA_META_MULTISAMPLE
|
1709 MESA_META_OCCLUSION_QUERY
);
1711 if (!(buffers
& BUFFER_BITS_COLOR
)) {
1712 /* We'll use colormask to disable color writes. Otherwise,
1713 * respect color mask
1715 metaSave
|= MESA_META_COLOR_MASK
;
1718 _mesa_meta_begin(ctx
, metaSave
);
1720 meta_glsl_clear_init(ctx
, clear
);
1722 if (fb
->_IntegerColor
) {
1723 _mesa_UseProgram(clear
->IntegerShaderProg
);
1724 _mesa_Uniform4iv(clear
->IntegerColorLocation
, 1,
1725 ctx
->Color
.ClearColor
.i
);
1727 _mesa_UseProgram(clear
->ShaderProg
);
1728 _mesa_Uniform4fv(clear
->ColorLocation
, 1,
1729 ctx
->Color
.ClearColor
.f
);
1732 /* GL_COLOR_BUFFER_BIT */
1733 if (buffers
& BUFFER_BITS_COLOR
) {
1734 /* leave colormask, glDrawBuffer state as-is */
1736 /* Clears never have the color clamped. */
1737 if (ctx
->Extensions
.ARB_color_buffer_float
)
1738 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1741 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1742 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1745 /* GL_DEPTH_BUFFER_BIT */
1746 if (buffers
& BUFFER_BIT_DEPTH
) {
1747 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1748 _mesa_DepthFunc(GL_ALWAYS
);
1749 _mesa_DepthMask(GL_TRUE
);
1752 assert(!ctx
->Depth
.Test
);
1755 /* GL_STENCIL_BUFFER_BIT */
1756 if (buffers
& BUFFER_BIT_STENCIL
) {
1757 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1758 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1759 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1760 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1761 ctx
->Stencil
.Clear
& stencilMax
,
1762 ctx
->Stencil
.WriteMask
[0]);
1765 assert(!ctx
->Stencil
.Enabled
);
1768 /* vertex positions */
1782 /* upload new vertex data */
1783 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1784 GL_DYNAMIC_DRAW_ARB
);
1787 if (fb
->MaxNumLayers
> 0) {
1789 for (layer
= 0; layer
< fb
->MaxNumLayers
; layer
++) {
1790 if (fb
->_IntegerColor
)
1791 _mesa_Uniform1i(clear
->IntegerLayerLocation
, layer
);
1793 _mesa_Uniform1i(clear
->LayerLocation
, layer
);
1794 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1797 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1800 _mesa_meta_end(ctx
);
1804 * Meta implementation of ctx->Driver.CopyPixels() in terms
1805 * of texture mapping and polygon rendering and GLSL shaders.
1808 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
1809 GLsizei width
, GLsizei height
,
1810 GLint dstX
, GLint dstY
, GLenum type
)
1812 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1813 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
1814 struct vertex verts
[4];
1816 if (type
!= GL_COLOR
||
1817 ctx
->_ImageTransferState
||
1819 width
> tex
->MaxSize
||
1820 height
> tex
->MaxSize
) {
1821 /* XXX avoid this fallback */
1822 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1826 /* Most GL state applies to glCopyPixels, but a there's a few things
1827 * we need to override:
1829 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
1832 MESA_META_TRANSFORM
|
1835 MESA_META_VIEWPORT
));
1837 _mesa_meta_setup_vertex_objects(©pix
->VAO
, ©pix
->VBO
, false,
1840 /* Silence valgrind warnings about reading uninitialized stack. */
1841 memset(verts
, 0, sizeof(verts
));
1843 /* Alloc/setup texture */
1844 _mesa_meta_setup_copypix_texture(ctx
, tex
, srcX
, srcY
, width
, height
,
1845 GL_RGBA
, GL_NEAREST
);
1847 /* vertex positions, texcoords (after texture allocation!) */
1849 const GLfloat dstX0
= (GLfloat
) dstX
;
1850 const GLfloat dstY0
= (GLfloat
) dstY
;
1851 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1852 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1853 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1858 verts
[0].tex
[0] = 0.0F
;
1859 verts
[0].tex
[1] = 0.0F
;
1863 verts
[1].tex
[0] = tex
->Sright
;
1864 verts
[1].tex
[1] = 0.0F
;
1868 verts
[2].tex
[0] = tex
->Sright
;
1869 verts
[2].tex
[1] = tex
->Ttop
;
1873 verts
[3].tex
[0] = 0.0F
;
1874 verts
[3].tex
[1] = tex
->Ttop
;
1876 /* upload new vertex data */
1877 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1880 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1882 /* draw textured quad */
1883 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1885 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1887 _mesa_meta_end(ctx
);
1891 meta_drawpix_cleanup(struct drawpix_state
*drawpix
)
1893 if (drawpix
->VAO
!= 0) {
1894 _mesa_DeleteVertexArrays(1, &drawpix
->VAO
);
1897 _mesa_DeleteBuffers(1, &drawpix
->VBO
);
1901 if (drawpix
->StencilFP
!= 0) {
1902 _mesa_DeleteProgramsARB(1, &drawpix
->StencilFP
);
1903 drawpix
->StencilFP
= 0;
1906 if (drawpix
->DepthFP
!= 0) {
1907 _mesa_DeleteProgramsARB(1, &drawpix
->DepthFP
);
1908 drawpix
->DepthFP
= 0;
1913 * When the glDrawPixels() image size is greater than the max rectangle
1914 * texture size we use this function to break the glDrawPixels() image
1915 * into tiles which fit into the max texture size.
1918 tiled_draw_pixels(struct gl_context
*ctx
,
1920 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1921 GLenum format
, GLenum type
,
1922 const struct gl_pixelstore_attrib
*unpack
,
1923 const GLvoid
*pixels
)
1925 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
1928 if (tileUnpack
.RowLength
== 0)
1929 tileUnpack
.RowLength
= width
;
1931 for (i
= 0; i
< width
; i
+= tileSize
) {
1932 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
1933 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
1935 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
1937 for (j
= 0; j
< height
; j
+= tileSize
) {
1938 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
1939 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
1941 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
1943 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
1944 format
, type
, &tileUnpack
, pixels
);
1951 * One-time init for drawing stencil pixels.
1954 init_draw_stencil_pixels(struct gl_context
*ctx
)
1956 /* This program is run eight times, once for each stencil bit.
1957 * The stencil values to draw are found in an 8-bit alpha texture.
1958 * We read the texture/stencil value and test if bit 'b' is set.
1959 * If the bit is not set, use KIL to kill the fragment.
1960 * Finally, we use the stencil test to update the stencil buffer.
1962 * The basic algorithm for checking if a bit is set is:
1963 * if (is_odd(value / (1 << bit)))
1964 * result is one (or non-zero).
1967 * The program parameter contains three values:
1968 * parm.x = 255 / (1 << bit)
1972 static const char *program
=
1974 "PARAM parm = program.local[0]; \n"
1976 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1977 "# t = t * 255 / bit \n"
1978 "MUL t.x, t.a, parm.x; \n"
1981 "SUB t.x, t.x, t.y; \n"
1983 "MUL t.x, t.x, parm.y; \n"
1984 "# t = fract(t.x) \n"
1985 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
1986 "# t.x = (t.x == 0 ? 1 : 0) \n"
1987 "SGE t.x, -t.x, parm.z; \n"
1989 "# for debug only \n"
1990 "#MOV result.color, t.x; \n"
1992 char program2
[1000];
1993 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1994 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
1995 const char *texTarget
;
1997 assert(drawpix
->StencilFP
== 0);
1999 /* replace %s with "RECT" or "2D" */
2000 assert(strlen(program
) + 4 < sizeof(program2
));
2001 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2005 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2007 _mesa_GenProgramsARB(1, &drawpix
->StencilFP
);
2008 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2009 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2010 strlen(program2
), (const GLubyte
*) program2
);
2015 * One-time init for drawing depth pixels.
2018 init_draw_depth_pixels(struct gl_context
*ctx
)
2020 static const char *program
=
2022 "PARAM color = program.local[0]; \n"
2023 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2024 "MOV result.color, color; \n"
2027 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2028 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
2029 const char *texTarget
;
2031 assert(drawpix
->DepthFP
== 0);
2033 /* replace %s with "RECT" or "2D" */
2034 assert(strlen(program
) + 4 < sizeof(program2
));
2035 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2039 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2041 _mesa_GenProgramsARB(1, &drawpix
->DepthFP
);
2042 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2043 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2044 strlen(program2
), (const GLubyte
*) program2
);
2049 * Meta implementation of ctx->Driver.DrawPixels() in terms
2050 * of texture mapping and polygon rendering.
2053 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2054 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2055 GLenum format
, GLenum type
,
2056 const struct gl_pixelstore_attrib
*unpack
,
2057 const GLvoid
*pixels
)
2059 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2060 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
2061 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2062 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2063 struct vertex verts
[4];
2064 GLenum texIntFormat
;
2065 GLboolean fallback
, newTex
;
2066 GLbitfield metaExtraSave
= 0x0;
2069 * Determine if we can do the glDrawPixels with texture mapping.
2071 fallback
= GL_FALSE
;
2072 if (ctx
->Fog
.Enabled
) {
2076 if (_mesa_is_color_format(format
)) {
2077 /* use more compact format when possible */
2078 /* XXX disable special case for GL_LUMINANCE for now to work around
2079 * apparent i965 driver bug (see bug #23670).
2081 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2082 texIntFormat
= format
;
2084 texIntFormat
= GL_RGBA
;
2086 /* If we're not supposed to clamp the resulting color, then just
2087 * promote our texture to fully float. We could do better by
2088 * just going for the matching set of channels, in floating
2091 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2092 ctx
->Extensions
.ARB_texture_float
)
2093 texIntFormat
= GL_RGBA32F
;
2095 else if (_mesa_is_stencil_format(format
)) {
2096 if (ctx
->Extensions
.ARB_fragment_program
&&
2097 ctx
->Pixel
.IndexShift
== 0 &&
2098 ctx
->Pixel
.IndexOffset
== 0 &&
2099 type
== GL_UNSIGNED_BYTE
) {
2100 /* We'll store stencil as alpha. This only works for GLubyte
2101 * image data because of how incoming values are mapped to alpha
2104 texIntFormat
= GL_ALPHA
;
2105 metaExtraSave
= (MESA_META_COLOR_MASK
|
2106 MESA_META_DEPTH_TEST
|
2107 MESA_META_PIXEL_TRANSFER
|
2109 MESA_META_STENCIL_TEST
);
2115 else if (_mesa_is_depth_format(format
)) {
2116 if (ctx
->Extensions
.ARB_depth_texture
&&
2117 ctx
->Extensions
.ARB_fragment_program
) {
2118 texIntFormat
= GL_DEPTH_COMPONENT
;
2119 metaExtraSave
= (MESA_META_SHADER
);
2130 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2131 format
, type
, unpack
, pixels
);
2136 * Check image size against max texture size, draw as tiles if needed.
2138 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2139 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2140 format
, type
, unpack
, pixels
);
2144 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2145 * but a there's a few things we need to override:
2147 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2150 MESA_META_TRANSFORM
|
2153 MESA_META_VIEWPORT
|
2156 newTex
= _mesa_meta_alloc_texture(tex
, width
, height
, texIntFormat
);
2158 _mesa_meta_setup_vertex_objects(&drawpix
->VAO
, &drawpix
->VBO
, false,
2161 /* Silence valgrind warnings about reading uninitialized stack. */
2162 memset(verts
, 0, sizeof(verts
));
2164 /* vertex positions, texcoords (after texture allocation!) */
2166 const GLfloat x0
= (GLfloat
) x
;
2167 const GLfloat y0
= (GLfloat
) y
;
2168 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2169 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2170 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2175 verts
[0].tex
[0] = 0.0F
;
2176 verts
[0].tex
[1] = 0.0F
;
2180 verts
[1].tex
[0] = tex
->Sright
;
2181 verts
[1].tex
[1] = 0.0F
;
2185 verts
[2].tex
[0] = tex
->Sright
;
2186 verts
[2].tex
[1] = tex
->Ttop
;
2190 verts
[3].tex
[0] = 0.0F
;
2191 verts
[3].tex
[1] = tex
->Ttop
;
2194 /* upload new vertex data */
2195 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2196 verts
, GL_DYNAMIC_DRAW_ARB
);
2198 /* set given unpack params */
2199 ctx
->Unpack
= *unpack
;
2201 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2203 if (_mesa_is_stencil_format(format
)) {
2204 /* Drawing stencil */
2207 if (!drawpix
->StencilFP
)
2208 init_draw_stencil_pixels(ctx
);
2210 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2211 GL_ALPHA
, type
, pixels
);
2213 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2215 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2217 /* set all stencil bits to 0 */
2218 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2219 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2220 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2222 /* set stencil bits to 1 where needed */
2223 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2225 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2226 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2228 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2229 const GLuint mask
= 1 << bit
;
2230 if (mask
& origStencilMask
) {
2231 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2232 _mesa_StencilMask(mask
);
2234 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2235 255.0f
/ mask
, 0.5f
, 0.0f
, 0.0f
);
2237 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2241 else if (_mesa_is_depth_format(format
)) {
2243 if (!drawpix
->DepthFP
)
2244 init_draw_depth_pixels(ctx
);
2246 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2247 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2249 /* polygon color = current raster color */
2250 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2251 ctx
->Current
.RasterColor
);
2253 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2254 format
, type
, pixels
);
2256 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2260 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2261 format
, type
, pixels
);
2262 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2265 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2267 /* restore unpack params */
2268 ctx
->Unpack
= unpackSave
;
2270 _mesa_meta_end(ctx
);
2274 alpha_test_raster_color(struct gl_context
*ctx
)
2276 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2277 GLfloat ref
= ctx
->Color
.AlphaRef
;
2279 switch (ctx
->Color
.AlphaFunc
) {
2285 return alpha
== ref
;
2287 return alpha
<= ref
;
2291 return alpha
!= ref
;
2293 return alpha
>= ref
;
2303 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2304 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2305 * tracker would improve performance a lot.
2308 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2309 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2310 const struct gl_pixelstore_attrib
*unpack
,
2311 const GLubyte
*bitmap1
)
2313 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2314 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2315 const GLenum texIntFormat
= GL_ALPHA
;
2316 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2318 struct vertex verts
[4];
2323 * Check if swrast fallback is needed.
2325 if (ctx
->_ImageTransferState
||
2326 ctx
->FragmentProgram
._Enabled
||
2328 ctx
->Texture
._EnabledUnits
||
2329 width
> tex
->MaxSize
||
2330 height
> tex
->MaxSize
) {
2331 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2335 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2338 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2339 * but a there's a few things we need to override:
2341 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
2342 MESA_META_PIXEL_STORE
|
2343 MESA_META_RASTERIZATION
|
2346 MESA_META_TRANSFORM
|
2349 MESA_META_VIEWPORT
));
2351 _mesa_meta_setup_vertex_objects(&bitmap
->VAO
, &bitmap
->VBO
, false, 3, 2, 4);
2353 newTex
= _mesa_meta_alloc_texture(tex
, width
, height
, texIntFormat
);
2355 /* Silence valgrind warnings about reading uninitialized stack. */
2356 memset(verts
, 0, sizeof(verts
));
2358 /* vertex positions, texcoords, colors (after texture allocation!) */
2360 const GLfloat x0
= (GLfloat
) x
;
2361 const GLfloat y0
= (GLfloat
) y
;
2362 const GLfloat x1
= (GLfloat
) (x
+ width
);
2363 const GLfloat y1
= (GLfloat
) (y
+ height
);
2364 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2370 verts
[0].tex
[0] = 0.0F
;
2371 verts
[0].tex
[1] = 0.0F
;
2375 verts
[1].tex
[0] = tex
->Sright
;
2376 verts
[1].tex
[1] = 0.0F
;
2380 verts
[2].tex
[0] = tex
->Sright
;
2381 verts
[2].tex
[1] = tex
->Ttop
;
2385 verts
[3].tex
[0] = 0.0F
;
2386 verts
[3].tex
[1] = tex
->Ttop
;
2388 for (i
= 0; i
< 4; i
++) {
2389 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2390 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2391 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2392 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2395 /* upload new vertex data */
2396 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2399 /* choose different foreground/background alpha values */
2400 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2401 bg
= (fg
> 127 ? 0 : 255);
2403 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2405 _mesa_meta_end(ctx
);
2409 bitmap8
= malloc(width
* height
);
2411 memset(bitmap8
, bg
, width
* height
);
2412 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2413 bitmap8
, width
, fg
);
2415 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2417 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2418 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2420 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2421 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2423 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2425 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2430 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2432 _mesa_meta_end(ctx
);
2436 * Compute the texture coordinates for the four vertices of a quad for
2437 * drawing a 2D texture image or slice of a cube/3D texture.
2438 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2439 * \param slice slice of a 1D/2D array texture or 3D texture
2440 * \param width width of the texture image
2441 * \param height height of the texture image
2442 * \param coords0/1/2/3 returns the computed texcoords
2445 _mesa_meta_setup_texture_coords(GLenum faceTarget
,
2455 static const GLfloat st
[4][2] = {
2456 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2461 if (faceTarget
== GL_TEXTURE_CUBE_MAP_ARRAY
)
2462 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ slice
% 6;
2464 /* Currently all texture targets want the W component to be 1.0.
2471 switch (faceTarget
) {
2475 case GL_TEXTURE_2D_ARRAY
:
2476 if (faceTarget
== GL_TEXTURE_3D
) {
2477 assert(slice
< depth
);
2479 r
= (slice
+ 0.5f
) / depth
;
2481 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
2485 coords0
[0] = 0.0F
; /* s */
2486 coords0
[1] = 0.0F
; /* t */
2487 coords0
[2] = r
; /* r */
2498 case GL_TEXTURE_RECTANGLE_ARB
:
2499 coords0
[0] = 0.0F
; /* s */
2500 coords0
[1] = 0.0F
; /* t */
2501 coords0
[2] = 0.0F
; /* r */
2502 coords1
[0] = (float) width
;
2505 coords2
[0] = (float) width
;
2506 coords2
[1] = (float) height
;
2509 coords3
[1] = (float) height
;
2512 case GL_TEXTURE_1D_ARRAY
:
2513 coords0
[0] = 0.0F
; /* s */
2514 coords0
[1] = (float) slice
; /* t */
2515 coords0
[2] = 0.0F
; /* r */
2517 coords1
[1] = (float) slice
;
2520 coords2
[1] = (float) slice
;
2523 coords3
[1] = (float) slice
;
2527 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2528 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2529 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2530 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2531 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2532 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2533 /* loop over quad verts */
2534 for (i
= 0; i
< 4; i
++) {
2535 /* Compute sc = +/-scale and tc = +/-scale.
2536 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2537 * though that can still sometimes happen with this scale factor...
2539 const GLfloat scale
= 0.9999f
;
2540 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2541 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2561 coord
[3] = (float) (slice
/ 6);
2563 switch (faceTarget
) {
2564 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2569 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2574 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2579 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2584 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2589 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2600 assert(!"unexpected target in _mesa_meta_setup_texture_coords()");
2604 static struct blit_shader
*
2605 choose_blit_shader(GLenum target
, struct blit_shader_table
*table
)
2609 table
->sampler_1d
.type
= "sampler1D";
2610 table
->sampler_1d
.func
= "texture1D";
2611 table
->sampler_1d
.texcoords
= "texCoords.x";
2612 return &table
->sampler_1d
;
2614 table
->sampler_2d
.type
= "sampler2D";
2615 table
->sampler_2d
.func
= "texture2D";
2616 table
->sampler_2d
.texcoords
= "texCoords.xy";
2617 return &table
->sampler_2d
;
2618 case GL_TEXTURE_RECTANGLE
:
2619 table
->sampler_rect
.type
= "sampler2DRect";
2620 table
->sampler_rect
.func
= "texture2DRect";
2621 table
->sampler_rect
.texcoords
= "texCoords.xy";
2622 return &table
->sampler_rect
;
2624 /* Code for mipmap generation with 3D textures is not used yet.
2625 * It's a sw fallback.
2627 table
->sampler_3d
.type
= "sampler3D";
2628 table
->sampler_3d
.func
= "texture3D";
2629 table
->sampler_3d
.texcoords
= "texCoords.xyz";
2630 return &table
->sampler_3d
;
2631 case GL_TEXTURE_CUBE_MAP
:
2632 table
->sampler_cubemap
.type
= "samplerCube";
2633 table
->sampler_cubemap
.func
= "textureCube";
2634 table
->sampler_cubemap
.texcoords
= "texCoords.xyz";
2635 return &table
->sampler_cubemap
;
2636 case GL_TEXTURE_1D_ARRAY
:
2637 table
->sampler_1d_array
.type
= "sampler1DArray";
2638 table
->sampler_1d_array
.func
= "texture1DArray";
2639 table
->sampler_1d_array
.texcoords
= "texCoords.xy";
2640 return &table
->sampler_1d_array
;
2641 case GL_TEXTURE_2D_ARRAY
:
2642 table
->sampler_2d_array
.type
= "sampler2DArray";
2643 table
->sampler_2d_array
.func
= "texture2DArray";
2644 table
->sampler_2d_array
.texcoords
= "texCoords.xyz";
2645 return &table
->sampler_2d_array
;
2646 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2647 table
->sampler_cubemap_array
.type
= "samplerCubeArray";
2648 table
->sampler_cubemap_array
.func
= "textureCubeArray";
2649 table
->sampler_cubemap_array
.texcoords
= "texCoords.xyzw";
2650 return &table
->sampler_cubemap_array
;
2652 _mesa_problem(NULL
, "Unexpected texture target 0x%x in"
2653 " setup_texture_sampler()\n", target
);
2659 _mesa_meta_blit_shader_table_cleanup(struct blit_shader_table
*table
)
2661 _mesa_DeleteProgram(table
->sampler_1d
.shader_prog
);
2662 _mesa_DeleteProgram(table
->sampler_2d
.shader_prog
);
2663 _mesa_DeleteProgram(table
->sampler_3d
.shader_prog
);
2664 _mesa_DeleteProgram(table
->sampler_rect
.shader_prog
);
2665 _mesa_DeleteProgram(table
->sampler_cubemap
.shader_prog
);
2666 _mesa_DeleteProgram(table
->sampler_1d_array
.shader_prog
);
2667 _mesa_DeleteProgram(table
->sampler_2d_array
.shader_prog
);
2668 _mesa_DeleteProgram(table
->sampler_cubemap_array
.shader_prog
);
2670 table
->sampler_1d
.shader_prog
= 0;
2671 table
->sampler_2d
.shader_prog
= 0;
2672 table
->sampler_3d
.shader_prog
= 0;
2673 table
->sampler_rect
.shader_prog
= 0;
2674 table
->sampler_cubemap
.shader_prog
= 0;
2675 table
->sampler_1d_array
.shader_prog
= 0;
2676 table
->sampler_2d_array
.shader_prog
= 0;
2677 table
->sampler_cubemap_array
.shader_prog
= 0;
2681 * Determine the GL data type to use for the temporary image read with
2682 * ReadPixels() and passed to Tex[Sub]Image().
2685 get_temp_image_type(struct gl_context
*ctx
, mesa_format format
)
2689 baseFormat
= _mesa_get_format_base_format(format
);
2691 switch (baseFormat
) {
2698 case GL_LUMINANCE_ALPHA
:
2700 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8) {
2701 return GL_UNSIGNED_BYTE
;
2702 } else if (ctx
->DrawBuffer
->Visual
.redBits
<= 16) {
2703 return GL_UNSIGNED_SHORT
;
2705 GLenum datatype
= _mesa_get_format_datatype(format
);
2706 if (datatype
== GL_INT
|| datatype
== GL_UNSIGNED_INT
)
2710 case GL_DEPTH_COMPONENT
: {
2711 GLenum datatype
= _mesa_get_format_datatype(format
);
2712 if (datatype
== GL_FLOAT
)
2715 return GL_UNSIGNED_INT
;
2717 case GL_DEPTH_STENCIL
: {
2718 GLenum datatype
= _mesa_get_format_datatype(format
);
2719 if (datatype
== GL_FLOAT
)
2720 return GL_FLOAT_32_UNSIGNED_INT_24_8_REV
;
2722 return GL_UNSIGNED_INT_24_8
;
2725 _mesa_problem(ctx
, "Unexpected format %d in get_temp_image_type()",
2732 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2733 * Have to be careful with locking and meta state for pixel transfer.
2736 _mesa_meta_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2737 struct gl_texture_image
*texImage
,
2738 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2739 struct gl_renderbuffer
*rb
,
2741 GLsizei width
, GLsizei height
)
2743 struct gl_texture_object
*texObj
= texImage
->TexObject
;
2744 GLenum format
, type
;
2748 /* The gl_renderbuffer is part of the interface for
2749 * dd_function_table::CopyTexSubImage, but this implementation does not use
2754 /* Choose format/type for temporary image buffer */
2755 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
2756 if (format
== GL_LUMINANCE
||
2757 format
== GL_LUMINANCE_ALPHA
||
2758 format
== GL_INTENSITY
) {
2759 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
2760 * temp image buffer because glReadPixels will do L=R+G+B which is
2761 * not what we want (should be L=R).
2766 type
= get_temp_image_type(ctx
, texImage
->TexFormat
);
2767 if (_mesa_is_format_integer_color(texImage
->TexFormat
)) {
2768 format
= _mesa_base_format_to_integer_format(format
);
2770 bpp
= _mesa_bytes_per_pixel(format
, type
);
2772 _mesa_problem(ctx
, "Bad bpp in _mesa_meta_CopyTexSubImage()");
2777 * Alloc image buffer (XXX could use a PBO)
2779 buf
= malloc(width
* height
* bpp
);
2781 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
2785 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2788 * Read image from framebuffer (disable pixel transfer ops)
2790 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
2791 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2792 format
, type
, &ctx
->Pack
, buf
);
2793 _mesa_meta_end(ctx
);
2795 _mesa_update_state(ctx
); /* to update pixel transfer state */
2798 * Store texture data (with pixel transfer ops)
2800 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
2802 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
2803 assert(yoffset
== 0);
2804 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
2805 xoffset
, zoffset
, 0, width
, 1, 1,
2806 format
, type
, buf
, &ctx
->Unpack
);
2808 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
2809 xoffset
, yoffset
, zoffset
, width
, height
, 1,
2810 format
, type
, buf
, &ctx
->Unpack
);
2813 _mesa_meta_end(ctx
);
2815 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
2822 meta_decompress_cleanup(struct decompress_state
*decompress
)
2824 if (decompress
->FBO
!= 0) {
2825 _mesa_DeleteFramebuffers(1, &decompress
->FBO
);
2826 _mesa_DeleteRenderbuffers(1, &decompress
->RBO
);
2829 if (decompress
->VAO
!= 0) {
2830 _mesa_DeleteVertexArrays(1, &decompress
->VAO
);
2831 _mesa_DeleteBuffers(1, &decompress
->VBO
);
2834 if (decompress
->Sampler
!= 0)
2835 _mesa_DeleteSamplers(1, &decompress
->Sampler
);
2837 memset(decompress
, 0, sizeof(*decompress
));
2841 * Decompress a texture image by drawing a quad with the compressed
2842 * texture and reading the pixels out of the color buffer.
2843 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
2844 * \param destFormat format, ala glReadPixels
2845 * \param destType type, ala glReadPixels
2846 * \param dest destination buffer
2847 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
2850 decompress_texture_image(struct gl_context
*ctx
,
2851 struct gl_texture_image
*texImage
,
2853 GLenum destFormat
, GLenum destType
,
2856 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
2857 struct gl_texture_object
*texObj
= texImage
->TexObject
;
2858 const GLint width
= texImage
->Width
;
2859 const GLint height
= texImage
->Height
;
2860 const GLint depth
= texImage
->Height
;
2861 const GLenum target
= texObj
->Target
;
2863 struct vertex verts
[4];
2865 const bool use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
2866 ctx
->Extensions
.ARB_fragment_shader
;
2869 assert(target
== GL_TEXTURE_3D
||
2870 target
== GL_TEXTURE_2D_ARRAY
||
2871 target
== GL_TEXTURE_CUBE_MAP_ARRAY
);
2876 case GL_TEXTURE_1D_ARRAY
:
2877 assert(!"No compressed 1D textures.");
2881 assert(!"No compressed 3D textures.");
2884 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2885 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ (slice
% 6);
2888 case GL_TEXTURE_CUBE_MAP
:
2889 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
2893 faceTarget
= target
;
2897 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_PIXEL_STORE
);
2899 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
2900 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
2902 /* Create/bind FBO/renderbuffer */
2903 if (decompress
->FBO
== 0) {
2904 _mesa_GenFramebuffers(1, &decompress
->FBO
);
2905 _mesa_GenRenderbuffers(1, &decompress
->RBO
);
2906 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
2907 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
2908 _mesa_FramebufferRenderbuffer(GL_FRAMEBUFFER_EXT
,
2909 GL_COLOR_ATTACHMENT0_EXT
,
2910 GL_RENDERBUFFER_EXT
,
2914 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
2917 /* alloc dest surface */
2918 if (width
> decompress
->Width
|| height
> decompress
->Height
) {
2919 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
2920 _mesa_RenderbufferStorage(GL_RENDERBUFFER_EXT
, GL_RGBA
,
2922 decompress
->Width
= width
;
2923 decompress
->Height
= height
;
2926 if (use_glsl_version
) {
2927 _mesa_meta_setup_vertex_objects(&decompress
->VAO
, &decompress
->VBO
, true,
2930 _mesa_meta_setup_blit_shader(ctx
, target
, &decompress
->shaders
);
2932 _mesa_meta_setup_ff_tnl_for_blit(&decompress
->VAO
, &decompress
->VBO
, 3);
2935 if (!decompress
->Sampler
) {
2936 _mesa_GenSamplers(1, &decompress
->Sampler
);
2937 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
2938 /* nearest filtering */
2939 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
2940 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
2941 /* No sRGB decode or encode.*/
2942 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
2943 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
2944 GL_SKIP_DECODE_EXT
);
2948 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
2951 /* Silence valgrind warnings about reading uninitialized stack. */
2952 memset(verts
, 0, sizeof(verts
));
2954 _mesa_meta_setup_texture_coords(faceTarget
, slice
, width
, height
, depth
,
2960 /* setup vertex positions */
2970 _mesa_set_viewport(ctx
, 0, 0, 0, width
, height
);
2972 /* upload new vertex data */
2973 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2975 /* setup texture state */
2976 _mesa_BindTexture(target
, texObj
->Name
);
2978 if (!use_glsl_version
)
2979 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2982 /* save texture object state */
2983 const GLint baseLevelSave
= texObj
->BaseLevel
;
2984 const GLint maxLevelSave
= texObj
->MaxLevel
;
2986 /* restrict sampling to the texture level of interest */
2987 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
2988 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, texImage
->Level
);
2989 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texImage
->Level
);
2992 /* render quad w/ texture into renderbuffer */
2993 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2995 /* Restore texture object state, the texture binding will
2996 * be restored by _mesa_meta_end().
2998 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
2999 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
3000 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3005 /* read pixels from renderbuffer */
3007 GLenum baseTexFormat
= texImage
->_BaseFormat
;
3008 GLenum destBaseFormat
= _mesa_base_tex_format(ctx
, destFormat
);
3010 /* The pixel transfer state will be set to default values at this point
3011 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
3012 * turned off (as required by glGetTexImage) but we need to handle some
3013 * special cases. In particular, single-channel texture values are
3014 * returned as red and two-channel texture values are returned as
3017 if ((baseTexFormat
== GL_LUMINANCE
||
3018 baseTexFormat
== GL_LUMINANCE_ALPHA
||
3019 baseTexFormat
== GL_INTENSITY
) ||
3020 /* If we're reading back an RGB(A) texture (using glGetTexImage) as
3021 * luminance then we need to return L=tex(R).
3023 ((baseTexFormat
== GL_RGBA
||
3024 baseTexFormat
== GL_RGB
||
3025 baseTexFormat
== GL_RG
) &&
3026 (destBaseFormat
== GL_LUMINANCE
||
3027 destBaseFormat
== GL_LUMINANCE_ALPHA
||
3028 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
3029 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
))) {
3030 /* Green and blue must be zero */
3031 _mesa_PixelTransferf(GL_GREEN_SCALE
, 0.0f
);
3032 _mesa_PixelTransferf(GL_BLUE_SCALE
, 0.0f
);
3035 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
3038 /* disable texture unit */
3039 if (!use_glsl_version
)
3040 _mesa_set_enable(ctx
, target
, GL_FALSE
);
3042 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
3044 _mesa_meta_end(ctx
);
3049 * This is just a wrapper around _mesa_get_tex_image() and
3050 * decompress_texture_image(). Meta functions should not be directly called
3054 _mesa_meta_GetTexImage(struct gl_context
*ctx
,
3055 GLenum format
, GLenum type
, GLvoid
*pixels
,
3056 struct gl_texture_image
*texImage
)
3058 /* We can only use the decompress-with-blit method here if the texels are
3059 * unsigned, normalized values. We could handle signed and unnormalized
3060 * with floating point renderbuffers...
3062 if (_mesa_is_format_compressed(texImage
->TexFormat
) &&
3063 _mesa_get_format_datatype(texImage
->TexFormat
)
3064 == GL_UNSIGNED_NORMALIZED
) {
3065 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3067 /* Need to unlock the texture here to prevent deadlock... */
3068 _mesa_unlock_texture(ctx
, texObj
);
3069 for (slice
= 0; slice
< texImage
->Depth
; slice
++) {
3071 if (texImage
->TexObject
->Target
== GL_TEXTURE_2D_ARRAY
3072 || texImage
->TexObject
->Target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
3073 /* Setup pixel packing. SkipPixels and SkipRows will be applied
3074 * in the decompress_texture_image() function's call to
3075 * glReadPixels but we need to compute the dest slice's address
3076 * here (according to SkipImages and ImageHeight).
3078 struct gl_pixelstore_attrib packing
= ctx
->Pack
;
3079 packing
.SkipPixels
= 0;
3080 packing
.SkipRows
= 0;
3081 dst
= _mesa_image_address3d(&packing
, pixels
, texImage
->Width
,
3082 texImage
->Height
, format
, type
,
3088 decompress_texture_image(ctx
, texImage
, slice
, format
, type
, dst
);
3090 /* ... and relock it */
3091 _mesa_lock_texture(ctx
, texObj
);
3094 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
3100 * Meta implementation of ctx->Driver.DrawTex() in terms
3101 * of polygon rendering.
3104 _mesa_meta_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
3105 GLfloat width
, GLfloat height
)
3107 struct drawtex_state
*drawtex
= &ctx
->Meta
->DrawTex
;
3109 GLfloat x
, y
, z
, st
[MAX_TEXTURE_UNITS
][2];
3111 struct vertex verts
[4];
3114 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
3116 MESA_META_TRANSFORM
|
3118 MESA_META_VIEWPORT
));
3120 if (drawtex
->VAO
== 0) {
3121 /* one-time setup */
3122 GLint active_texture
;
3124 /* create vertex array object */
3125 _mesa_GenVertexArrays(1, &drawtex
->VAO
);
3126 _mesa_BindVertexArray(drawtex
->VAO
);
3128 /* create vertex array buffer */
3129 _mesa_GenBuffers(1, &drawtex
->VBO
);
3130 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3131 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3132 NULL
, GL_DYNAMIC_DRAW_ARB
);
3134 /* client active texture is not part of the array object */
3135 active_texture
= ctx
->Array
.ActiveTexture
;
3137 /* setup vertex arrays */
3138 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3139 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3140 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3141 _mesa_ClientActiveTexture(GL_TEXTURE0
+ i
);
3142 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(st
[i
]));
3143 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3146 /* restore client active texture */
3147 _mesa_ClientActiveTexture(GL_TEXTURE0
+ active_texture
);
3150 _mesa_BindVertexArray(drawtex
->VAO
);
3151 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3154 /* vertex positions, texcoords */
3156 const GLfloat x1
= x
+ width
;
3157 const GLfloat y1
= y
+ height
;
3159 z
= CLAMP(z
, 0.0f
, 1.0f
);
3178 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3179 const struct gl_texture_object
*texObj
;
3180 const struct gl_texture_image
*texImage
;
3181 GLfloat s
, t
, s1
, t1
;
3184 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
3186 for (j
= 0; j
< 4; j
++) {
3187 verts
[j
].st
[i
][0] = 0.0f
;
3188 verts
[j
].st
[i
][1] = 0.0f
;
3193 texObj
= ctx
->Texture
.Unit
[i
]._Current
;
3194 texImage
= texObj
->Image
[0][texObj
->BaseLevel
];
3195 tw
= texImage
->Width2
;
3196 th
= texImage
->Height2
;
3198 s
= (GLfloat
) texObj
->CropRect
[0] / tw
;
3199 t
= (GLfloat
) texObj
->CropRect
[1] / th
;
3200 s1
= (GLfloat
) (texObj
->CropRect
[0] + texObj
->CropRect
[2]) / tw
;
3201 t1
= (GLfloat
) (texObj
->CropRect
[1] + texObj
->CropRect
[3]) / th
;
3203 verts
[0].st
[i
][0] = s
;
3204 verts
[0].st
[i
][1] = t
;
3206 verts
[1].st
[i
][0] = s1
;
3207 verts
[1].st
[i
][1] = t
;
3209 verts
[2].st
[i
][0] = s1
;
3210 verts
[2].st
[i
][1] = t1
;
3212 verts
[3].st
[i
][0] = s
;
3213 verts
[3].st
[i
][1] = t1
;
3216 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3219 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3221 _mesa_meta_end(ctx
);