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/blit.h"
41 #include "main/bufferobj.h"
42 #include "main/buffers.h"
43 #include "main/colortab.h"
44 #include "main/condrender.h"
45 #include "main/depth.h"
46 #include "main/enable.h"
47 #include "main/fbobject.h"
48 #include "main/feedback.h"
49 #include "main/formats.h"
50 #include "main/glformats.h"
51 #include "main/image.h"
52 #include "main/macros.h"
53 #include "main/matrix.h"
54 #include "main/mipmap.h"
55 #include "main/multisample.h"
56 #include "main/objectlabel.h"
57 #include "main/pipelineobj.h"
58 #include "main/pixel.h"
60 #include "main/polygon.h"
61 #include "main/queryobj.h"
62 #include "main/readpix.h"
63 #include "main/scissor.h"
64 #include "main/shaderapi.h"
65 #include "main/shaderobj.h"
66 #include "main/state.h"
67 #include "main/stencil.h"
68 #include "main/texobj.h"
69 #include "main/texenv.h"
70 #include "main/texgetimage.h"
71 #include "main/teximage.h"
72 #include "main/texparam.h"
73 #include "main/texstate.h"
74 #include "main/transformfeedback.h"
75 #include "main/uniforms.h"
76 #include "main/varray.h"
77 #include "main/viewport.h"
78 #include "main/samplerobj.h"
79 #include "program/program.h"
80 #include "swrast/swrast.h"
81 #include "drivers/common/meta.h"
82 #include "main/enums.h"
83 #include "main/glformats.h"
84 #include "../glsl/ralloc.h"
86 /** Return offset in bytes of the field within a vertex struct */
87 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
89 static struct blit_shader
*
90 choose_blit_shader(GLenum target
, struct blit_shader_table
*table
);
92 static void cleanup_temp_texture(struct temp_texture
*tex
);
93 static void meta_glsl_clear_cleanup(struct clear_state
*clear
);
94 static void meta_decompress_cleanup(struct decompress_state
*decompress
);
95 static void meta_drawpix_cleanup(struct drawpix_state
*drawpix
);
98 _mesa_meta_bind_fbo_image(GLenum attachment
,
99 struct gl_texture_image
*texImage
, GLuint layer
)
101 struct gl_texture_object
*texObj
= texImage
->TexObject
;
102 int level
= texImage
->Level
;
103 GLenum target
= texObj
->Target
;
107 _mesa_FramebufferTexture1D(GL_FRAMEBUFFER
,
113 case GL_TEXTURE_1D_ARRAY
:
114 case GL_TEXTURE_2D_ARRAY
:
115 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
116 case GL_TEXTURE_CUBE_MAP_ARRAY
:
118 _mesa_FramebufferTextureLayer(GL_FRAMEBUFFER
,
124 default: /* 2D / cube */
125 if (target
== GL_TEXTURE_CUBE_MAP
)
126 target
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
128 _mesa_FramebufferTexture2D(GL_FRAMEBUFFER
,
137 _mesa_meta_compile_shader_with_debug(struct gl_context
*ctx
, GLenum target
,
138 const GLcharARB
*source
)
144 shader
= _mesa_CreateShader(target
);
145 _mesa_ShaderSource(shader
, 1, &source
, NULL
);
146 _mesa_CompileShader(shader
);
148 _mesa_GetShaderiv(shader
, GL_COMPILE_STATUS
, &ok
);
152 _mesa_GetShaderiv(shader
, GL_INFO_LOG_LENGTH
, &size
);
154 _mesa_DeleteShader(shader
);
160 _mesa_DeleteShader(shader
);
164 _mesa_GetShaderInfoLog(shader
, size
, NULL
, info
);
166 "meta program compile failed:\n%s\n"
171 _mesa_DeleteShader(shader
);
177 _mesa_meta_link_program_with_debug(struct gl_context
*ctx
, GLuint program
)
182 _mesa_LinkProgram(program
);
184 _mesa_GetProgramiv(program
, GL_LINK_STATUS
, &ok
);
188 _mesa_GetProgramiv(program
, GL_INFO_LOG_LENGTH
, &size
);
196 _mesa_GetProgramInfoLog(program
, size
, NULL
, info
);
197 _mesa_problem(ctx
, "meta program link failed:\n%s", info
);
205 * Generate a generic shader to blit from a texture to a framebuffer
207 * \param ctx Current GL context
208 * \param texTarget Texture target that will be the source of the blit
210 * \returns a handle to a shader program on success or zero on failure.
213 _mesa_meta_setup_blit_shader(struct gl_context
*ctx
,
215 struct blit_shader_table
*table
)
217 const char *vs_source
;
220 void *const mem_ctx
= ralloc_context(NULL
);
221 struct blit_shader
*shader
= choose_blit_shader(target
, table
);
224 assert(shader
!= NULL
);
226 if (shader
->shader_prog
!= 0) {
227 _mesa_UseProgram(shader
->shader_prog
);
231 if (ctx
->Const
.GLSLVersion
< 130) {
233 "attribute vec2 position;\n"
234 "attribute vec4 textureCoords;\n"
235 "varying vec4 texCoords;\n"
238 " texCoords = textureCoords;\n"
239 " gl_Position = vec4(position, 0.0, 1.0);\n"
242 fs_source
= ralloc_asprintf(mem_ctx
,
243 "#extension GL_EXT_texture_array : enable\n"
244 "#extension GL_ARB_texture_cube_map_array: enable\n"
245 "uniform %s texSampler;\n"
246 "varying vec4 texCoords;\n"
249 " gl_FragColor = %s(texSampler, %s);\n"
250 " gl_FragDepth = gl_FragColor.x;\n"
253 shader
->func
, shader
->texcoords
);
256 vs_source
= ralloc_asprintf(mem_ctx
,
258 "in vec2 position;\n"
259 "in vec4 textureCoords;\n"
260 "out vec4 texCoords;\n"
263 " texCoords = textureCoords;\n"
264 " gl_Position = vec4(position, 0.0, 1.0);\n"
266 fs_source
= ralloc_asprintf(mem_ctx
,
268 "#extension GL_ARB_texture_cube_map_array: enable\n"
269 "uniform %s texSampler;\n"
270 "in vec4 texCoords;\n"
271 "out vec4 out_color;\n"
275 " out_color = texture(texSampler, %s);\n"
276 " gl_FragDepth = out_color.x;\n"
282 vs
= _mesa_meta_compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_source
);
283 fs
= _mesa_meta_compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_source
);
285 shader
->shader_prog
= _mesa_CreateProgram();
286 _mesa_AttachShader(shader
->shader_prog
, fs
);
287 _mesa_DeleteShader(fs
);
288 _mesa_AttachShader(shader
->shader_prog
, vs
);
289 _mesa_DeleteShader(vs
);
290 _mesa_BindAttribLocation(shader
->shader_prog
, 0, "position");
291 _mesa_BindAttribLocation(shader
->shader_prog
, 1, "texcoords");
292 _mesa_meta_link_program_with_debug(ctx
, shader
->shader_prog
);
293 name
= ralloc_asprintf(mem_ctx
, "%s blit", shader
->type
);
294 _mesa_ObjectLabel(GL_PROGRAM
, shader
->shader_prog
, -1, name
);
295 ralloc_free(mem_ctx
);
297 _mesa_UseProgram(shader
->shader_prog
);
301 * Configure vertex buffer and vertex array objects for tests
303 * Regardless of whether a new VAO and new VBO are created, the objects
304 * referenced by \c VAO and \c VBO will be bound into the GL state vector
305 * when this function terminates.
307 * \param VAO Storage for vertex array object handle. If 0, a new VAO
309 * \param VBO Storage for vertex buffer object handle. If 0, a new VBO
310 * will be created. The new VBO will have storage for 4
311 * \c vertex structures.
312 * \param use_generic_attributes Should generic attributes 0 and 1 be used,
313 * or should traditional, fixed-function color and texture
314 * coordinate be used?
315 * \param vertex_size Number of components for attribute 0 / vertex.
316 * \param texcoord_size Number of components for attribute 1 / texture
317 * coordinate. If this is 0, attribute 1 will not be set or
319 * \param color_size Number of components for attribute 1 / primary color.
320 * If this is 0, attribute 1 will not be set or enabled.
322 * \note If \c use_generic_attributes is \c true, \c color_size must be zero.
323 * Use \c texcoord_size instead.
326 _mesa_meta_setup_vertex_objects(GLuint
*VAO
, GLuint
*VBO
,
327 bool use_generic_attributes
,
328 unsigned vertex_size
, unsigned texcoord_size
,
334 /* create vertex array object */
335 _mesa_GenVertexArrays(1, VAO
);
336 _mesa_BindVertexArray(*VAO
);
338 /* create vertex array buffer */
339 _mesa_GenBuffers(1, VBO
);
340 _mesa_BindBuffer(GL_ARRAY_BUFFER
, *VBO
);
341 _mesa_BufferData(GL_ARRAY_BUFFER
, 4 * sizeof(struct vertex
), NULL
,
344 /* setup vertex arrays */
345 if (use_generic_attributes
) {
346 assert(color_size
== 0);
348 _mesa_VertexAttribPointer(0, vertex_size
, GL_FLOAT
, GL_FALSE
,
349 sizeof(struct vertex
), OFFSET(x
));
350 _mesa_EnableVertexAttribArray(0);
352 if (texcoord_size
> 0) {
353 _mesa_VertexAttribPointer(1, texcoord_size
, GL_FLOAT
, GL_FALSE
,
354 sizeof(struct vertex
), OFFSET(tex
));
355 _mesa_EnableVertexAttribArray(1);
358 _mesa_VertexPointer(vertex_size
, GL_FLOAT
, sizeof(struct vertex
),
360 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
362 if (texcoord_size
> 0) {
363 _mesa_TexCoordPointer(texcoord_size
, GL_FLOAT
,
364 sizeof(struct vertex
), OFFSET(tex
));
365 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
368 if (color_size
> 0) {
369 _mesa_ColorPointer(color_size
, GL_FLOAT
,
370 sizeof(struct vertex
), OFFSET(r
));
371 _mesa_EnableClientState(GL_COLOR_ARRAY
);
375 _mesa_BindVertexArray(*VAO
);
376 _mesa_BindBuffer(GL_ARRAY_BUFFER
, *VBO
);
381 * Initialize meta-ops for a context.
382 * To be called once during context creation.
385 _mesa_meta_init(struct gl_context
*ctx
)
389 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
394 * Free context meta-op state.
395 * To be called once during context destruction.
398 _mesa_meta_free(struct gl_context
*ctx
)
400 GET_CURRENT_CONTEXT(old_context
);
401 _mesa_make_current(ctx
, NULL
, NULL
);
402 _mesa_meta_glsl_blit_cleanup(&ctx
->Meta
->Blit
);
403 meta_glsl_clear_cleanup(&ctx
->Meta
->Clear
);
404 _mesa_meta_glsl_generate_mipmap_cleanup(&ctx
->Meta
->Mipmap
);
405 cleanup_temp_texture(&ctx
->Meta
->TempTex
);
406 meta_decompress_cleanup(&ctx
->Meta
->Decompress
);
407 meta_drawpix_cleanup(&ctx
->Meta
->DrawPix
);
409 _mesa_make_current(old_context
, old_context
->WinSysDrawBuffer
, old_context
->WinSysReadBuffer
);
411 _mesa_make_current(NULL
, NULL
, NULL
);
418 * Enter meta state. This is like a light-weight version of glPushAttrib
419 * but it also resets most GL state back to default values.
421 * \param state bitmask of MESA_META_* flags indicating which attribute groups
422 * to save and reset to their defaults
425 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
427 struct save_state
*save
;
429 /* hope MAX_META_OPS_DEPTH is large enough */
430 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
432 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
433 memset(save
, 0, sizeof(*save
));
434 save
->SavedState
= state
;
436 /* We always push into desktop GL mode and pop out at the end. No sense in
437 * writing our shaders varying based on the user's context choice, when
438 * Mesa can handle either.
440 save
->API
= ctx
->API
;
441 ctx
->API
= API_OPENGL_COMPAT
;
443 /* Pausing transform feedback needs to be done early, or else we won't be
444 * able to change other state.
446 save
->TransformFeedbackNeedsResume
=
447 _mesa_is_xfb_active_and_unpaused(ctx
);
448 if (save
->TransformFeedbackNeedsResume
)
449 _mesa_PauseTransformFeedback();
451 /* After saving the current occlusion object, call EndQuery so that no
452 * occlusion querying will be active during the meta-operation.
454 if (state
& MESA_META_OCCLUSION_QUERY
) {
455 save
->CurrentOcclusionObject
= ctx
->Query
.CurrentOcclusionObject
;
456 if (save
->CurrentOcclusionObject
)
457 _mesa_EndQuery(save
->CurrentOcclusionObject
->Target
);
460 if (state
& MESA_META_ALPHA_TEST
) {
461 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
462 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
463 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
464 if (ctx
->Color
.AlphaEnabled
)
465 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
468 if (state
& MESA_META_BLEND
) {
469 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
470 if (ctx
->Color
.BlendEnabled
) {
471 if (ctx
->Extensions
.EXT_draw_buffers2
) {
473 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
474 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
478 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
481 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
482 if (ctx
->Color
.ColorLogicOpEnabled
)
483 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
486 if (state
& MESA_META_COLOR_MASK
) {
487 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
488 sizeof(ctx
->Color
.ColorMask
));
489 if (!ctx
->Color
.ColorMask
[0][0] ||
490 !ctx
->Color
.ColorMask
[0][1] ||
491 !ctx
->Color
.ColorMask
[0][2] ||
492 !ctx
->Color
.ColorMask
[0][3])
493 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
496 if (state
& MESA_META_DEPTH_TEST
) {
497 save
->Depth
= ctx
->Depth
; /* struct copy */
499 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
502 if (state
& MESA_META_FOG
) {
503 save
->Fog
= ctx
->Fog
.Enabled
;
504 if (ctx
->Fog
.Enabled
)
505 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
508 if (state
& MESA_META_PIXEL_STORE
) {
509 save
->Pack
= ctx
->Pack
;
510 save
->Unpack
= ctx
->Unpack
;
511 ctx
->Pack
= ctx
->DefaultPacking
;
512 ctx
->Unpack
= ctx
->DefaultPacking
;
515 if (state
& MESA_META_PIXEL_TRANSFER
) {
516 save
->RedScale
= ctx
->Pixel
.RedScale
;
517 save
->RedBias
= ctx
->Pixel
.RedBias
;
518 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
519 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
520 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
521 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
522 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
523 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
524 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
525 ctx
->Pixel
.RedScale
= 1.0F
;
526 ctx
->Pixel
.RedBias
= 0.0F
;
527 ctx
->Pixel
.GreenScale
= 1.0F
;
528 ctx
->Pixel
.GreenBias
= 0.0F
;
529 ctx
->Pixel
.BlueScale
= 1.0F
;
530 ctx
->Pixel
.BlueBias
= 0.0F
;
531 ctx
->Pixel
.AlphaScale
= 1.0F
;
532 ctx
->Pixel
.AlphaBias
= 0.0F
;
533 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
535 ctx
->NewState
|=_NEW_PIXEL
;
538 if (state
& MESA_META_RASTERIZATION
) {
539 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
540 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
541 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
542 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
543 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
544 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
545 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
546 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
547 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
548 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
549 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
552 if (state
& MESA_META_SCISSOR
) {
553 save
->Scissor
= ctx
->Scissor
; /* struct copy */
554 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
557 if (state
& MESA_META_SHADER
) {
560 if (ctx
->Extensions
.ARB_vertex_program
) {
561 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
562 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
563 ctx
->VertexProgram
.Current
);
564 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
567 if (ctx
->Extensions
.ARB_fragment_program
) {
568 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
569 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
570 ctx
->FragmentProgram
.Current
);
571 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
574 if (ctx
->Extensions
.ATI_fragment_shader
) {
575 save
->ATIFragmentShaderEnabled
= ctx
->ATIFragmentShader
.Enabled
;
576 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
, GL_FALSE
);
579 if (ctx
->Pipeline
.Current
) {
580 _mesa_reference_pipeline_object(ctx
, &save
->Pipeline
,
581 ctx
->Pipeline
.Current
);
582 _mesa_BindProgramPipeline(0);
585 /* Save the shader state from ctx->Shader (instead of ctx->_Shader) so
586 * that we don't have to worry about the current pipeline state.
588 for (i
= 0; i
<= MESA_SHADER_FRAGMENT
; i
++) {
589 _mesa_reference_shader_program(ctx
, &save
->Shader
[i
],
590 ctx
->Shader
.CurrentProgram
[i
]);
592 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
593 ctx
->Shader
.ActiveProgram
);
598 if (state
& MESA_META_STENCIL_TEST
) {
599 save
->Stencil
= ctx
->Stencil
; /* struct copy */
600 if (ctx
->Stencil
.Enabled
)
601 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
602 /* NOTE: other stencil state not reset */
605 if (state
& MESA_META_TEXTURE
) {
608 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
609 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
610 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
612 /* Disable all texture units */
613 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
614 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
615 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
616 if (ctx
->Texture
.Unit
[u
].Enabled
||
617 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
618 _mesa_ActiveTexture(GL_TEXTURE0
+ u
);
619 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
620 if (ctx
->Extensions
.ARB_texture_cube_map
)
621 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
623 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
624 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
625 if (ctx
->Extensions
.NV_texture_rectangle
)
626 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
627 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
628 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
629 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
630 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
634 /* save current texture objects for unit[0] only */
635 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
636 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
637 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
640 /* set defaults for unit[0] */
641 _mesa_ActiveTexture(GL_TEXTURE0
);
642 _mesa_ClientActiveTexture(GL_TEXTURE0
);
643 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
646 if (state
& MESA_META_TRANSFORM
) {
647 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
648 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
649 16 * sizeof(GLfloat
));
650 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
651 16 * sizeof(GLfloat
));
652 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
653 16 * sizeof(GLfloat
));
654 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
655 /* set 1:1 vertex:pixel coordinate transform */
656 _mesa_ActiveTexture(GL_TEXTURE0
);
657 _mesa_MatrixMode(GL_TEXTURE
);
658 _mesa_LoadIdentity();
659 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
660 _mesa_MatrixMode(GL_MODELVIEW
);
661 _mesa_LoadIdentity();
662 _mesa_MatrixMode(GL_PROJECTION
);
663 _mesa_LoadIdentity();
665 /* glOrtho with width = 0 or height = 0 generates GL_INVALID_VALUE.
666 * This can occur when there is no draw buffer.
668 if (ctx
->DrawBuffer
->Width
!= 0 && ctx
->DrawBuffer
->Height
!= 0)
669 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
670 0.0, ctx
->DrawBuffer
->Height
,
674 if (state
& MESA_META_CLIP
) {
675 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
676 if (ctx
->Transform
.ClipPlanesEnabled
) {
678 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
679 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
684 if (state
& MESA_META_VERTEX
) {
685 /* save vertex array object state */
686 _mesa_reference_vao(ctx
, &save
->VAO
,
688 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
689 ctx
->Array
.ArrayBufferObj
);
690 /* set some default state? */
693 if (state
& MESA_META_VIEWPORT
) {
694 /* save viewport state */
695 save
->ViewportX
= ctx
->ViewportArray
[0].X
;
696 save
->ViewportY
= ctx
->ViewportArray
[0].Y
;
697 save
->ViewportW
= ctx
->ViewportArray
[0].Width
;
698 save
->ViewportH
= ctx
->ViewportArray
[0].Height
;
699 /* set viewport to match window size */
700 if (ctx
->ViewportArray
[0].X
!= 0 ||
701 ctx
->ViewportArray
[0].Y
!= 0 ||
702 ctx
->ViewportArray
[0].Width
!= (float) ctx
->DrawBuffer
->Width
||
703 ctx
->ViewportArray
[0].Height
!= (float) ctx
->DrawBuffer
->Height
) {
704 _mesa_set_viewport(ctx
, 0, 0, 0,
705 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
707 /* save depth range state */
708 save
->DepthNear
= ctx
->ViewportArray
[0].Near
;
709 save
->DepthFar
= ctx
->ViewportArray
[0].Far
;
710 /* set depth range to default */
711 _mesa_DepthRange(0.0, 1.0);
714 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
715 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
717 /* Generally in here we want to do clamping according to whether
718 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
719 * regardless of the internal implementation of the metaops.
721 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
722 ctx
->Extensions
.ARB_color_buffer_float
)
723 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
726 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
727 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
729 /* Generally in here we never want vertex color clamping --
730 * result clamping is only dependent on fragment clamping.
732 if (ctx
->Extensions
.ARB_color_buffer_float
)
733 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
736 if (state
& MESA_META_CONDITIONAL_RENDER
) {
737 save
->CondRenderQuery
= ctx
->Query
.CondRenderQuery
;
738 save
->CondRenderMode
= ctx
->Query
.CondRenderMode
;
740 if (ctx
->Query
.CondRenderQuery
)
741 _mesa_EndConditionalRender();
744 if (state
& MESA_META_SELECT_FEEDBACK
) {
745 save
->RenderMode
= ctx
->RenderMode
;
746 if (ctx
->RenderMode
== GL_SELECT
) {
747 save
->Select
= ctx
->Select
; /* struct copy */
748 _mesa_RenderMode(GL_RENDER
);
749 } else if (ctx
->RenderMode
== GL_FEEDBACK
) {
750 save
->Feedback
= ctx
->Feedback
; /* struct copy */
751 _mesa_RenderMode(GL_RENDER
);
755 if (state
& MESA_META_MULTISAMPLE
) {
756 save
->Multisample
= ctx
->Multisample
; /* struct copy */
758 if (ctx
->Multisample
.Enabled
)
759 _mesa_set_multisample(ctx
, GL_FALSE
);
760 if (ctx
->Multisample
.SampleCoverage
)
761 _mesa_set_enable(ctx
, GL_SAMPLE_COVERAGE
, GL_FALSE
);
762 if (ctx
->Multisample
.SampleAlphaToCoverage
)
763 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_COVERAGE
, GL_FALSE
);
764 if (ctx
->Multisample
.SampleAlphaToOne
)
765 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_ONE
, GL_FALSE
);
766 if (ctx
->Multisample
.SampleShading
)
767 _mesa_set_enable(ctx
, GL_SAMPLE_SHADING
, GL_FALSE
);
768 if (ctx
->Multisample
.SampleMask
)
769 _mesa_set_enable(ctx
, GL_SAMPLE_MASK
, GL_FALSE
);
772 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
773 save
->sRGBEnabled
= ctx
->Color
.sRGBEnabled
;
774 if (ctx
->Color
.sRGBEnabled
)
775 _mesa_set_framebuffer_srgb(ctx
, GL_FALSE
);
780 save
->Lighting
= ctx
->Light
.Enabled
;
781 if (ctx
->Light
.Enabled
)
782 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
783 save
->RasterDiscard
= ctx
->RasterDiscard
;
784 if (ctx
->RasterDiscard
)
785 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_FALSE
);
787 save
->DrawBufferName
= ctx
->DrawBuffer
->Name
;
788 save
->ReadBufferName
= ctx
->ReadBuffer
->Name
;
789 save
->RenderbufferName
= (ctx
->CurrentRenderbuffer
?
790 ctx
->CurrentRenderbuffer
->Name
: 0);
796 * Leave meta state. This is like a light-weight version of glPopAttrib().
799 _mesa_meta_end(struct gl_context
*ctx
)
801 struct save_state
*save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
- 1];
802 const GLbitfield state
= save
->SavedState
;
805 /* After starting a new occlusion query, initialize the results to the
806 * values saved previously. The driver will then continue to increment
809 if (state
& MESA_META_OCCLUSION_QUERY
) {
810 if (save
->CurrentOcclusionObject
) {
811 _mesa_BeginQuery(save
->CurrentOcclusionObject
->Target
,
812 save
->CurrentOcclusionObject
->Id
);
813 ctx
->Query
.CurrentOcclusionObject
->Result
= save
->CurrentOcclusionObject
->Result
;
817 if (state
& MESA_META_ALPHA_TEST
) {
818 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
819 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
820 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
823 if (state
& MESA_META_BLEND
) {
824 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
825 if (ctx
->Extensions
.EXT_draw_buffers2
) {
827 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
828 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
832 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
835 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
836 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
839 if (state
& MESA_META_COLOR_MASK
) {
841 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
842 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
844 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
845 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
849 save
->ColorMask
[i
][0],
850 save
->ColorMask
[i
][1],
851 save
->ColorMask
[i
][2],
852 save
->ColorMask
[i
][3]);
858 if (state
& MESA_META_DEPTH_TEST
) {
859 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
860 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
861 _mesa_DepthFunc(save
->Depth
.Func
);
862 _mesa_DepthMask(save
->Depth
.Mask
);
865 if (state
& MESA_META_FOG
) {
866 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
869 if (state
& MESA_META_PIXEL_STORE
) {
870 ctx
->Pack
= save
->Pack
;
871 ctx
->Unpack
= save
->Unpack
;
874 if (state
& MESA_META_PIXEL_TRANSFER
) {
875 ctx
->Pixel
.RedScale
= save
->RedScale
;
876 ctx
->Pixel
.RedBias
= save
->RedBias
;
877 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
878 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
879 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
880 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
881 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
882 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
883 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
885 ctx
->NewState
|=_NEW_PIXEL
;
888 if (state
& MESA_META_RASTERIZATION
) {
889 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
890 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
891 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
892 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
893 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
894 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
897 if (state
& MESA_META_SCISSOR
) {
900 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++) {
901 _mesa_set_scissor(ctx
, i
,
902 save
->Scissor
.ScissorArray
[i
].X
,
903 save
->Scissor
.ScissorArray
[i
].Y
,
904 save
->Scissor
.ScissorArray
[i
].Width
,
905 save
->Scissor
.ScissorArray
[i
].Height
);
906 _mesa_set_enablei(ctx
, GL_SCISSOR_TEST
, i
,
907 (save
->Scissor
.EnableFlags
>> i
) & 1);
911 if (state
& MESA_META_SHADER
) {
912 static const GLenum targets
[] = {
920 if (ctx
->Extensions
.ARB_vertex_program
) {
921 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
922 save
->VertexProgramEnabled
);
923 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
924 save
->VertexProgram
);
925 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
928 if (ctx
->Extensions
.ARB_fragment_program
) {
929 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
930 save
->FragmentProgramEnabled
);
931 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
932 save
->FragmentProgram
);
933 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
936 if (ctx
->Extensions
.ATI_fragment_shader
) {
937 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
,
938 save
->ATIFragmentShaderEnabled
);
942 for (i
= 0; i
<= MESA_SHADER_FRAGMENT
; i
++) {
943 /* It is safe to call _mesa_use_shader_program even if the extension
944 * necessary for that program state is not supported. In that case,
945 * the saved program object must be NULL and the currently bound
946 * program object must be NULL. _mesa_use_shader_program is a no-op
949 _mesa_use_shader_program(ctx
, targets
[i
],
953 /* Do this *before* killing the reference. :)
955 if (save
->Shader
[i
] != NULL
)
958 _mesa_reference_shader_program(ctx
, &save
->Shader
[i
], NULL
);
961 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
963 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
, NULL
);
965 /* If there were any stages set with programs, use ctx->Shader as the
966 * current shader state. Otherwise, use Pipeline.Default. The pipeline
967 * hasn't been restored yet, and that may modify ctx->_Shader further.
970 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
973 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
974 ctx
->Pipeline
.Default
);
976 if (save
->Pipeline
) {
977 _mesa_bind_pipeline(ctx
, save
->Pipeline
);
979 _mesa_reference_pipeline_object(ctx
, &save
->Pipeline
, NULL
);
983 if (state
& MESA_META_STENCIL_TEST
) {
984 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
986 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
987 _mesa_ClearStencil(stencil
->Clear
);
988 if (ctx
->Extensions
.EXT_stencil_two_side
) {
989 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
990 stencil
->TestTwoSide
);
991 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
992 ? GL_BACK
: GL_FRONT
);
995 _mesa_StencilFuncSeparate(GL_FRONT
,
996 stencil
->Function
[0],
998 stencil
->ValueMask
[0]);
999 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
1000 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
1001 stencil
->ZFailFunc
[0],
1002 stencil
->ZPassFunc
[0]);
1004 _mesa_StencilFuncSeparate(GL_BACK
,
1005 stencil
->Function
[1],
1007 stencil
->ValueMask
[1]);
1008 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
1009 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
1010 stencil
->ZFailFunc
[1],
1011 stencil
->ZPassFunc
[1]);
1014 if (state
& MESA_META_TEXTURE
) {
1017 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
1019 /* restore texenv for unit[0] */
1020 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
1022 /* restore texture objects for unit[0] only */
1023 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
1024 if (ctx
->Texture
.Unit
[0].CurrentTex
[tgt
] != save
->CurrentTexture
[tgt
]) {
1025 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1026 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
1027 save
->CurrentTexture
[tgt
]);
1029 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
1032 /* Restore fixed function texture enables, texgen */
1033 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
1034 if (ctx
->Texture
.Unit
[u
].Enabled
!= save
->TexEnabled
[u
]) {
1035 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1036 ctx
->Texture
.Unit
[u
].Enabled
= save
->TexEnabled
[u
];
1039 if (ctx
->Texture
.Unit
[u
].TexGenEnabled
!= save
->TexGenEnabled
[u
]) {
1040 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1041 ctx
->Texture
.Unit
[u
].TexGenEnabled
= save
->TexGenEnabled
[u
];
1045 /* restore current unit state */
1046 _mesa_ActiveTexture(GL_TEXTURE0
+ save
->ActiveUnit
);
1047 _mesa_ClientActiveTexture(GL_TEXTURE0
+ save
->ClientActiveUnit
);
1050 if (state
& MESA_META_TRANSFORM
) {
1051 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
1052 _mesa_ActiveTexture(GL_TEXTURE0
);
1053 _mesa_MatrixMode(GL_TEXTURE
);
1054 _mesa_LoadMatrixf(save
->TextureMatrix
);
1055 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
1057 _mesa_MatrixMode(GL_MODELVIEW
);
1058 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
1060 _mesa_MatrixMode(GL_PROJECTION
);
1061 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
1063 _mesa_MatrixMode(save
->MatrixMode
);
1066 if (state
& MESA_META_CLIP
) {
1067 if (save
->ClipPlanesEnabled
) {
1069 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
1070 if (save
->ClipPlanesEnabled
& (1 << i
)) {
1071 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
1077 if (state
& MESA_META_VERTEX
) {
1078 /* restore vertex buffer object */
1079 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
1080 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
1082 /* restore vertex array object */
1083 _mesa_BindVertexArray(save
->VAO
->Name
);
1084 _mesa_reference_vao(ctx
, &save
->VAO
, NULL
);
1087 if (state
& MESA_META_VIEWPORT
) {
1088 if (save
->ViewportX
!= ctx
->ViewportArray
[0].X
||
1089 save
->ViewportY
!= ctx
->ViewportArray
[0].Y
||
1090 save
->ViewportW
!= ctx
->ViewportArray
[0].Width
||
1091 save
->ViewportH
!= ctx
->ViewportArray
[0].Height
) {
1092 _mesa_set_viewport(ctx
, 0, save
->ViewportX
, save
->ViewportY
,
1093 save
->ViewportW
, save
->ViewportH
);
1095 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
1098 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
&&
1099 ctx
->Extensions
.ARB_color_buffer_float
) {
1100 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
1103 if (state
& MESA_META_CLAMP_VERTEX_COLOR
&&
1104 ctx
->Extensions
.ARB_color_buffer_float
) {
1105 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
1108 if (state
& MESA_META_CONDITIONAL_RENDER
) {
1109 if (save
->CondRenderQuery
)
1110 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
1111 save
->CondRenderMode
);
1114 if (state
& MESA_META_SELECT_FEEDBACK
) {
1115 if (save
->RenderMode
== GL_SELECT
) {
1116 _mesa_RenderMode(GL_SELECT
);
1117 ctx
->Select
= save
->Select
;
1118 } else if (save
->RenderMode
== GL_FEEDBACK
) {
1119 _mesa_RenderMode(GL_FEEDBACK
);
1120 ctx
->Feedback
= save
->Feedback
;
1124 if (state
& MESA_META_MULTISAMPLE
) {
1125 struct gl_multisample_attrib
*ctx_ms
= &ctx
->Multisample
;
1126 struct gl_multisample_attrib
*save_ms
= &save
->Multisample
;
1128 if (ctx_ms
->Enabled
!= save_ms
->Enabled
)
1129 _mesa_set_multisample(ctx
, save_ms
->Enabled
);
1130 if (ctx_ms
->SampleCoverage
!= save_ms
->SampleCoverage
)
1131 _mesa_set_enable(ctx
, GL_SAMPLE_COVERAGE
, save_ms
->SampleCoverage
);
1132 if (ctx_ms
->SampleAlphaToCoverage
!= save_ms
->SampleAlphaToCoverage
)
1133 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_COVERAGE
, save_ms
->SampleAlphaToCoverage
);
1134 if (ctx_ms
->SampleAlphaToOne
!= save_ms
->SampleAlphaToOne
)
1135 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_ONE
, save_ms
->SampleAlphaToOne
);
1136 if (ctx_ms
->SampleCoverageValue
!= save_ms
->SampleCoverageValue
||
1137 ctx_ms
->SampleCoverageInvert
!= save_ms
->SampleCoverageInvert
) {
1138 _mesa_SampleCoverage(save_ms
->SampleCoverageValue
,
1139 save_ms
->SampleCoverageInvert
);
1141 if (ctx_ms
->SampleShading
!= save_ms
->SampleShading
)
1142 _mesa_set_enable(ctx
, GL_SAMPLE_SHADING
, save_ms
->SampleShading
);
1143 if (ctx_ms
->SampleMask
!= save_ms
->SampleMask
)
1144 _mesa_set_enable(ctx
, GL_SAMPLE_MASK
, save_ms
->SampleMask
);
1145 if (ctx_ms
->SampleMaskValue
!= save_ms
->SampleMaskValue
)
1146 _mesa_SampleMaski(0, save_ms
->SampleMaskValue
);
1147 if (ctx_ms
->MinSampleShadingValue
!= save_ms
->MinSampleShadingValue
)
1148 _mesa_MinSampleShading(save_ms
->MinSampleShadingValue
);
1151 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
1152 if (ctx
->Color
.sRGBEnabled
!= save
->sRGBEnabled
)
1153 _mesa_set_framebuffer_srgb(ctx
, save
->sRGBEnabled
);
1157 if (save
->Lighting
) {
1158 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
1160 if (save
->RasterDiscard
) {
1161 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_TRUE
);
1163 if (save
->TransformFeedbackNeedsResume
)
1164 _mesa_ResumeTransformFeedback();
1166 if (ctx
->DrawBuffer
->Name
!= save
->DrawBufferName
)
1167 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, save
->DrawBufferName
);
1169 if (ctx
->ReadBuffer
->Name
!= save
->ReadBufferName
)
1170 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER
, save
->ReadBufferName
);
1172 if (!ctx
->CurrentRenderbuffer
||
1173 ctx
->CurrentRenderbuffer
->Name
!= save
->RenderbufferName
)
1174 _mesa_BindRenderbuffer(GL_RENDERBUFFER
, save
->RenderbufferName
);
1176 ctx
->Meta
->SaveStackDepth
--;
1178 ctx
->API
= save
->API
;
1183 * Determine whether Mesa is currently in a meta state.
1186 _mesa_meta_in_progress(struct gl_context
*ctx
)
1188 return ctx
->Meta
->SaveStackDepth
!= 0;
1193 * Convert Z from a normalized value in the range [0, 1] to an object-space
1194 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1195 * default/identity ortho projection results in the original Z value.
1196 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1197 * value comes from the clear value or raster position.
1199 static INLINE GLfloat
1200 invert_z(GLfloat normZ
)
1202 GLfloat objZ
= 1.0f
- 2.0f
* normZ
;
1208 * One-time init for a temp_texture object.
1209 * Choose tex target, compute max tex size, etc.
1212 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1214 /* prefer texture rectangle */
1215 if (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.NV_texture_rectangle
) {
1216 tex
->Target
= GL_TEXTURE_RECTANGLE
;
1217 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
1218 tex
->NPOT
= GL_TRUE
;
1221 /* use 2D texture, NPOT if possible */
1222 tex
->Target
= GL_TEXTURE_2D
;
1223 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1224 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
1226 tex
->MinSize
= 16; /* 16 x 16 at least */
1227 assert(tex
->MaxSize
> 0);
1229 _mesa_GenTextures(1, &tex
->TexObj
);
1233 cleanup_temp_texture(struct temp_texture
*tex
)
1237 _mesa_DeleteTextures(1, &tex
->TexObj
);
1243 * Return pointer to temp_texture info for non-bitmap ops.
1244 * This does some one-time init if needed.
1246 struct temp_texture
*
1247 _mesa_meta_get_temp_texture(struct gl_context
*ctx
)
1249 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
1252 init_temp_texture(ctx
, tex
);
1260 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1261 * We use a separate texture for bitmaps to reduce texture
1262 * allocation/deallocation.
1264 static struct temp_texture
*
1265 get_bitmap_temp_texture(struct gl_context
*ctx
)
1267 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
1270 init_temp_texture(ctx
, tex
);
1277 * Return pointer to depth temp_texture.
1278 * This does some one-time init if needed.
1280 struct temp_texture
*
1281 _mesa_meta_get_temp_depth_texture(struct gl_context
*ctx
)
1283 struct temp_texture
*tex
= &ctx
->Meta
->Blit
.depthTex
;
1286 init_temp_texture(ctx
, tex
);
1293 * Compute the width/height of texture needed to draw an image of the
1294 * given size. Return a flag indicating whether the current texture
1295 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1296 * allocated (glTexImage2D).
1297 * Also, compute s/t texcoords for drawing.
1299 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1302 _mesa_meta_alloc_texture(struct temp_texture
*tex
,
1303 GLsizei width
, GLsizei height
, GLenum intFormat
)
1305 GLboolean newTex
= GL_FALSE
;
1307 ASSERT(width
<= tex
->MaxSize
);
1308 ASSERT(height
<= tex
->MaxSize
);
1310 if (width
> tex
->Width
||
1311 height
> tex
->Height
||
1312 intFormat
!= tex
->IntFormat
) {
1313 /* alloc new texture (larger or different format) */
1316 /* use non-power of two size */
1317 tex
->Width
= MAX2(tex
->MinSize
, width
);
1318 tex
->Height
= MAX2(tex
->MinSize
, height
);
1321 /* find power of two size */
1323 w
= h
= tex
->MinSize
;
1332 tex
->IntFormat
= intFormat
;
1337 /* compute texcoords */
1338 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1339 tex
->Sright
= (GLfloat
) width
;
1340 tex
->Ttop
= (GLfloat
) height
;
1343 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1344 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1352 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1355 _mesa_meta_setup_copypix_texture(struct gl_context
*ctx
,
1356 struct temp_texture
*tex
,
1357 GLint srcX
, GLint srcY
,
1358 GLsizei width
, GLsizei height
,
1364 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1365 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1366 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1367 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1369 newTex
= _mesa_meta_alloc_texture(tex
, width
, height
, intFormat
);
1371 /* copy framebuffer image to texture */
1373 /* create new tex image */
1374 if (tex
->Width
== width
&& tex
->Height
== height
) {
1375 /* create new tex with framebuffer data */
1376 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1377 srcX
, srcY
, width
, height
, 0);
1380 /* create empty texture */
1381 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1382 tex
->Width
, tex
->Height
, 0,
1383 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1385 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1386 0, 0, srcX
, srcY
, width
, height
);
1390 /* replace existing tex image */
1391 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1392 0, 0, srcX
, srcY
, width
, height
);
1398 * Setup/load texture for glDrawPixels.
1401 _mesa_meta_setup_drawpix_texture(struct gl_context
*ctx
,
1402 struct temp_texture
*tex
,
1404 GLsizei width
, GLsizei height
,
1405 GLenum format
, GLenum type
,
1406 const GLvoid
*pixels
)
1408 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1409 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1410 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1411 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1413 /* copy pixel data to texture */
1415 /* create new tex image */
1416 if (tex
->Width
== width
&& tex
->Height
== height
) {
1417 /* create new tex and load image data */
1418 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1419 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1422 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1424 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1425 ctx
->Unpack
.BufferObj
);
1426 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1427 /* create empty texture */
1428 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1429 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1430 if (save_unpack_obj
!= NULL
)
1431 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
,
1432 save_unpack_obj
->Name
);
1434 _mesa_TexSubImage2D(tex
->Target
, 0,
1435 0, 0, width
, height
, format
, type
, pixels
);
1439 /* replace existing tex image */
1440 _mesa_TexSubImage2D(tex
->Target
, 0,
1441 0, 0, width
, height
, format
, type
, pixels
);
1446 _mesa_meta_setup_ff_tnl_for_blit(GLuint
*VAO
, GLuint
*VBO
,
1447 unsigned texcoord_size
)
1449 _mesa_meta_setup_vertex_objects(VAO
, VBO
, false, 2, texcoord_size
, 0);
1451 /* setup projection matrix */
1452 _mesa_MatrixMode(GL_PROJECTION
);
1453 _mesa_LoadIdentity();
1457 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1460 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1462 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1463 struct vertex verts
[4];
1464 /* save all state but scissor, pixel pack/unpack */
1465 GLbitfield metaSave
= (MESA_META_ALL
-
1467 MESA_META_PIXEL_STORE
-
1468 MESA_META_CONDITIONAL_RENDER
-
1469 MESA_META_FRAMEBUFFER_SRGB
);
1470 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1472 if (buffers
& BUFFER_BITS_COLOR
) {
1473 /* if clearing color buffers, don't save/restore colormask */
1474 metaSave
-= MESA_META_COLOR_MASK
;
1477 _mesa_meta_begin(ctx
, metaSave
);
1479 _mesa_meta_setup_vertex_objects(&clear
->VAO
, &clear
->VBO
, false, 3, 0, 4);
1481 /* GL_COLOR_BUFFER_BIT */
1482 if (buffers
& BUFFER_BITS_COLOR
) {
1483 /* leave colormask, glDrawBuffer state as-is */
1485 /* Clears never have the color clamped. */
1486 if (ctx
->Extensions
.ARB_color_buffer_float
)
1487 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1490 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1491 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1494 /* GL_DEPTH_BUFFER_BIT */
1495 if (buffers
& BUFFER_BIT_DEPTH
) {
1496 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1497 _mesa_DepthFunc(GL_ALWAYS
);
1498 _mesa_DepthMask(GL_TRUE
);
1501 assert(!ctx
->Depth
.Test
);
1504 /* GL_STENCIL_BUFFER_BIT */
1505 if (buffers
& BUFFER_BIT_STENCIL
) {
1506 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1507 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1508 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1509 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1510 ctx
->Stencil
.Clear
& stencilMax
,
1511 ctx
->Stencil
.WriteMask
[0]);
1514 assert(!ctx
->Stencil
.Enabled
);
1517 /* vertex positions/colors */
1519 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1520 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1521 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1522 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1523 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1540 for (i
= 0; i
< 4; i
++) {
1541 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
1542 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
1543 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
1544 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
1547 /* upload new vertex data */
1548 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1549 GL_DYNAMIC_DRAW_ARB
);
1553 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1555 _mesa_meta_end(ctx
);
1559 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
1561 const char *vs_source
=
1562 "attribute vec4 position;\n"
1565 " gl_Position = position;\n"
1567 const char *gs_source
=
1569 "layout(triangles) in;\n"
1570 "layout(triangle_strip, max_vertices = 4) out;\n"
1571 "uniform int layer;\n"
1574 " for (int i = 0; i < 3; i++) {\n"
1575 " gl_Layer = layer;\n"
1576 " gl_Position = gl_in[i].gl_Position;\n"
1580 const char *fs_source
=
1581 "uniform vec4 color;\n"
1584 " gl_FragColor = color;\n"
1586 GLuint vs
, gs
= 0, fs
;
1587 bool has_integer_textures
;
1589 _mesa_meta_setup_vertex_objects(&clear
->VAO
, &clear
->VBO
, true, 3, 0, 0);
1591 if (clear
->ShaderProg
!= 0)
1594 vs
= _mesa_CreateShader(GL_VERTEX_SHADER
);
1595 _mesa_ShaderSource(vs
, 1, &vs_source
, NULL
);
1596 _mesa_CompileShader(vs
);
1598 if (_mesa_has_geometry_shaders(ctx
)) {
1599 gs
= _mesa_CreateShader(GL_GEOMETRY_SHADER
);
1600 _mesa_ShaderSource(gs
, 1, &gs_source
, NULL
);
1601 _mesa_CompileShader(gs
);
1604 fs
= _mesa_CreateShader(GL_FRAGMENT_SHADER
);
1605 _mesa_ShaderSource(fs
, 1, &fs_source
, NULL
);
1606 _mesa_CompileShader(fs
);
1608 clear
->ShaderProg
= _mesa_CreateProgram();
1609 _mesa_AttachShader(clear
->ShaderProg
, fs
);
1610 _mesa_DeleteShader(fs
);
1612 _mesa_AttachShader(clear
->ShaderProg
, gs
);
1613 _mesa_AttachShader(clear
->ShaderProg
, vs
);
1614 _mesa_DeleteShader(vs
);
1615 _mesa_BindAttribLocation(clear
->ShaderProg
, 0, "position");
1616 _mesa_LinkProgram(clear
->ShaderProg
);
1618 clear
->ColorLocation
= _mesa_GetUniformLocation(clear
->ShaderProg
,
1621 clear
->LayerLocation
= _mesa_GetUniformLocation(clear
->ShaderProg
,
1625 has_integer_textures
= _mesa_is_gles3(ctx
) ||
1626 (_mesa_is_desktop_gl(ctx
) && ctx
->Const
.GLSLVersion
>= 130);
1628 if (has_integer_textures
) {
1629 void *shader_source_mem_ctx
= ralloc_context(NULL
);
1630 const char *vs_int_source
=
1631 ralloc_asprintf(shader_source_mem_ctx
,
1633 "in vec4 position;\n"
1636 " gl_Position = position;\n"
1638 const char *fs_int_source
=
1639 ralloc_asprintf(shader_source_mem_ctx
,
1641 "uniform ivec4 color;\n"
1642 "out ivec4 out_color;\n"
1646 " out_color = color;\n"
1649 vs
= _mesa_meta_compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
,
1651 fs
= _mesa_meta_compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
,
1653 ralloc_free(shader_source_mem_ctx
);
1655 clear
->IntegerShaderProg
= _mesa_CreateProgram();
1656 _mesa_AttachShader(clear
->IntegerShaderProg
, fs
);
1657 _mesa_DeleteShader(fs
);
1659 _mesa_AttachShader(clear
->IntegerShaderProg
, gs
);
1660 _mesa_AttachShader(clear
->IntegerShaderProg
, vs
);
1661 _mesa_DeleteShader(vs
);
1662 _mesa_BindAttribLocation(clear
->IntegerShaderProg
, 0, "position");
1664 /* Note that user-defined out attributes get automatically assigned
1665 * locations starting from 0, so we don't need to explicitly
1666 * BindFragDataLocation to 0.
1669 _mesa_ObjectLabel(GL_PROGRAM
, clear
->IntegerShaderProg
, -1,
1671 _mesa_meta_link_program_with_debug(ctx
, clear
->IntegerShaderProg
);
1673 clear
->IntegerColorLocation
=
1674 _mesa_GetUniformLocation(clear
->IntegerShaderProg
, "color");
1676 clear
->IntegerLayerLocation
=
1677 _mesa_GetUniformLocation(clear
->IntegerShaderProg
, "layer");
1681 _mesa_DeleteShader(gs
);
1685 meta_glsl_clear_cleanup(struct clear_state
*clear
)
1687 if (clear
->VAO
== 0)
1689 _mesa_DeleteVertexArrays(1, &clear
->VAO
);
1691 _mesa_DeleteBuffers(1, &clear
->VBO
);
1693 _mesa_DeleteProgram(clear
->ShaderProg
);
1694 clear
->ShaderProg
= 0;
1696 if (clear
->IntegerShaderProg
) {
1697 _mesa_DeleteProgram(clear
->IntegerShaderProg
);
1698 clear
->IntegerShaderProg
= 0;
1703 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1706 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1708 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1709 GLbitfield metaSave
;
1710 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1711 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1712 const float x0
= ((float)fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
1713 const float y0
= ((float)fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
1714 const float x1
= ((float)fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
1715 const float y1
= ((float)fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
1716 const float z
= -invert_z(ctx
->Depth
.Clear
);
1717 struct vertex verts
[4];
1719 metaSave
= (MESA_META_ALPHA_TEST
|
1721 MESA_META_DEPTH_TEST
|
1722 MESA_META_RASTERIZATION
|
1724 MESA_META_STENCIL_TEST
|
1726 MESA_META_VIEWPORT
|
1728 MESA_META_CLAMP_FRAGMENT_COLOR
|
1729 MESA_META_MULTISAMPLE
|
1730 MESA_META_OCCLUSION_QUERY
);
1732 if (!(buffers
& BUFFER_BITS_COLOR
)) {
1733 /* We'll use colormask to disable color writes. Otherwise,
1734 * respect color mask
1736 metaSave
|= MESA_META_COLOR_MASK
;
1739 _mesa_meta_begin(ctx
, metaSave
);
1741 meta_glsl_clear_init(ctx
, clear
);
1743 if (fb
->_IntegerColor
) {
1744 _mesa_UseProgram(clear
->IntegerShaderProg
);
1745 _mesa_Uniform4iv(clear
->IntegerColorLocation
, 1,
1746 ctx
->Color
.ClearColor
.i
);
1748 _mesa_UseProgram(clear
->ShaderProg
);
1749 _mesa_Uniform4fv(clear
->ColorLocation
, 1,
1750 ctx
->Color
.ClearColor
.f
);
1753 /* GL_COLOR_BUFFER_BIT */
1754 if (buffers
& BUFFER_BITS_COLOR
) {
1755 /* leave colormask, glDrawBuffer state as-is */
1757 /* Clears never have the color clamped. */
1758 if (ctx
->Extensions
.ARB_color_buffer_float
)
1759 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1762 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1763 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1766 /* GL_DEPTH_BUFFER_BIT */
1767 if (buffers
& BUFFER_BIT_DEPTH
) {
1768 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1769 _mesa_DepthFunc(GL_ALWAYS
);
1770 _mesa_DepthMask(GL_TRUE
);
1773 assert(!ctx
->Depth
.Test
);
1776 /* GL_STENCIL_BUFFER_BIT */
1777 if (buffers
& BUFFER_BIT_STENCIL
) {
1778 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1779 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1780 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1781 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1782 ctx
->Stencil
.Clear
& stencilMax
,
1783 ctx
->Stencil
.WriteMask
[0]);
1786 assert(!ctx
->Stencil
.Enabled
);
1789 /* vertex positions */
1803 /* upload new vertex data */
1804 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1805 GL_DYNAMIC_DRAW_ARB
);
1808 if (fb
->MaxNumLayers
> 0) {
1810 for (layer
= 0; layer
< fb
->MaxNumLayers
; layer
++) {
1811 if (fb
->_IntegerColor
)
1812 _mesa_Uniform1i(clear
->IntegerLayerLocation
, layer
);
1814 _mesa_Uniform1i(clear
->LayerLocation
, layer
);
1815 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1818 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1821 _mesa_meta_end(ctx
);
1825 * Meta implementation of ctx->Driver.CopyPixels() in terms
1826 * of texture mapping and polygon rendering and GLSL shaders.
1829 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
1830 GLsizei width
, GLsizei height
,
1831 GLint dstX
, GLint dstY
, GLenum type
)
1833 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1834 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
1835 struct vertex verts
[4];
1837 if (type
!= GL_COLOR
||
1838 ctx
->_ImageTransferState
||
1840 width
> tex
->MaxSize
||
1841 height
> tex
->MaxSize
) {
1842 /* XXX avoid this fallback */
1843 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1847 /* Most GL state applies to glCopyPixels, but a there's a few things
1848 * we need to override:
1850 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
1853 MESA_META_TRANSFORM
|
1856 MESA_META_VIEWPORT
));
1858 _mesa_meta_setup_vertex_objects(©pix
->VAO
, ©pix
->VBO
, false,
1861 /* Silence valgrind warnings about reading uninitialized stack. */
1862 memset(verts
, 0, sizeof(verts
));
1864 /* Alloc/setup texture */
1865 _mesa_meta_setup_copypix_texture(ctx
, tex
, srcX
, srcY
, width
, height
,
1866 GL_RGBA
, GL_NEAREST
);
1868 /* vertex positions, texcoords (after texture allocation!) */
1870 const GLfloat dstX0
= (GLfloat
) dstX
;
1871 const GLfloat dstY0
= (GLfloat
) dstY
;
1872 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1873 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1874 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1879 verts
[0].tex
[0] = 0.0F
;
1880 verts
[0].tex
[1] = 0.0F
;
1884 verts
[1].tex
[0] = tex
->Sright
;
1885 verts
[1].tex
[1] = 0.0F
;
1889 verts
[2].tex
[0] = tex
->Sright
;
1890 verts
[2].tex
[1] = tex
->Ttop
;
1894 verts
[3].tex
[0] = 0.0F
;
1895 verts
[3].tex
[1] = tex
->Ttop
;
1897 /* upload new vertex data */
1898 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1901 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1903 /* draw textured quad */
1904 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1906 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1908 _mesa_meta_end(ctx
);
1912 meta_drawpix_cleanup(struct drawpix_state
*drawpix
)
1914 if (drawpix
->VAO
!= 0) {
1915 _mesa_DeleteVertexArrays(1, &drawpix
->VAO
);
1918 _mesa_DeleteBuffers(1, &drawpix
->VBO
);
1922 if (drawpix
->StencilFP
!= 0) {
1923 _mesa_DeleteProgramsARB(1, &drawpix
->StencilFP
);
1924 drawpix
->StencilFP
= 0;
1927 if (drawpix
->DepthFP
!= 0) {
1928 _mesa_DeleteProgramsARB(1, &drawpix
->DepthFP
);
1929 drawpix
->DepthFP
= 0;
1934 * When the glDrawPixels() image size is greater than the max rectangle
1935 * texture size we use this function to break the glDrawPixels() image
1936 * into tiles which fit into the max texture size.
1939 tiled_draw_pixels(struct gl_context
*ctx
,
1941 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1942 GLenum format
, GLenum type
,
1943 const struct gl_pixelstore_attrib
*unpack
,
1944 const GLvoid
*pixels
)
1946 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
1949 if (tileUnpack
.RowLength
== 0)
1950 tileUnpack
.RowLength
= width
;
1952 for (i
= 0; i
< width
; i
+= tileSize
) {
1953 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
1954 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
1956 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
1958 for (j
= 0; j
< height
; j
+= tileSize
) {
1959 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
1960 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
1962 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
1964 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
1965 format
, type
, &tileUnpack
, pixels
);
1972 * One-time init for drawing stencil pixels.
1975 init_draw_stencil_pixels(struct gl_context
*ctx
)
1977 /* This program is run eight times, once for each stencil bit.
1978 * The stencil values to draw are found in an 8-bit alpha texture.
1979 * We read the texture/stencil value and test if bit 'b' is set.
1980 * If the bit is not set, use KIL to kill the fragment.
1981 * Finally, we use the stencil test to update the stencil buffer.
1983 * The basic algorithm for checking if a bit is set is:
1984 * if (is_odd(value / (1 << bit)))
1985 * result is one (or non-zero).
1988 * The program parameter contains three values:
1989 * parm.x = 255 / (1 << bit)
1993 static const char *program
=
1995 "PARAM parm = program.local[0]; \n"
1997 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1998 "# t = t * 255 / bit \n"
1999 "MUL t.x, t.a, parm.x; \n"
2002 "SUB t.x, t.x, t.y; \n"
2004 "MUL t.x, t.x, parm.y; \n"
2005 "# t = fract(t.x) \n"
2006 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2007 "# t.x = (t.x == 0 ? 1 : 0) \n"
2008 "SGE t.x, -t.x, parm.z; \n"
2010 "# for debug only \n"
2011 "#MOV result.color, t.x; \n"
2013 char program2
[1000];
2014 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2015 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
2016 const char *texTarget
;
2018 assert(drawpix
->StencilFP
== 0);
2020 /* replace %s with "RECT" or "2D" */
2021 assert(strlen(program
) + 4 < sizeof(program2
));
2022 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2026 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2028 _mesa_GenProgramsARB(1, &drawpix
->StencilFP
);
2029 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2030 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2031 strlen(program2
), (const GLubyte
*) program2
);
2036 * One-time init for drawing depth pixels.
2039 init_draw_depth_pixels(struct gl_context
*ctx
)
2041 static const char *program
=
2043 "PARAM color = program.local[0]; \n"
2044 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2045 "MOV result.color, color; \n"
2048 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2049 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
2050 const char *texTarget
;
2052 assert(drawpix
->DepthFP
== 0);
2054 /* replace %s with "RECT" or "2D" */
2055 assert(strlen(program
) + 4 < sizeof(program2
));
2056 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2060 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2062 _mesa_GenProgramsARB(1, &drawpix
->DepthFP
);
2063 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2064 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2065 strlen(program2
), (const GLubyte
*) program2
);
2070 * Meta implementation of ctx->Driver.DrawPixels() in terms
2071 * of texture mapping and polygon rendering.
2074 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2075 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2076 GLenum format
, GLenum type
,
2077 const struct gl_pixelstore_attrib
*unpack
,
2078 const GLvoid
*pixels
)
2080 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2081 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
2082 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2083 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2084 struct vertex verts
[4];
2085 GLenum texIntFormat
;
2086 GLboolean fallback
, newTex
;
2087 GLbitfield metaExtraSave
= 0x0;
2090 * Determine if we can do the glDrawPixels with texture mapping.
2092 fallback
= GL_FALSE
;
2093 if (ctx
->Fog
.Enabled
) {
2097 if (_mesa_is_color_format(format
)) {
2098 /* use more compact format when possible */
2099 /* XXX disable special case for GL_LUMINANCE for now to work around
2100 * apparent i965 driver bug (see bug #23670).
2102 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2103 texIntFormat
= format
;
2105 texIntFormat
= GL_RGBA
;
2107 /* If we're not supposed to clamp the resulting color, then just
2108 * promote our texture to fully float. We could do better by
2109 * just going for the matching set of channels, in floating
2112 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2113 ctx
->Extensions
.ARB_texture_float
)
2114 texIntFormat
= GL_RGBA32F
;
2116 else if (_mesa_is_stencil_format(format
)) {
2117 if (ctx
->Extensions
.ARB_fragment_program
&&
2118 ctx
->Pixel
.IndexShift
== 0 &&
2119 ctx
->Pixel
.IndexOffset
== 0 &&
2120 type
== GL_UNSIGNED_BYTE
) {
2121 /* We'll store stencil as alpha. This only works for GLubyte
2122 * image data because of how incoming values are mapped to alpha
2125 texIntFormat
= GL_ALPHA
;
2126 metaExtraSave
= (MESA_META_COLOR_MASK
|
2127 MESA_META_DEPTH_TEST
|
2128 MESA_META_PIXEL_TRANSFER
|
2130 MESA_META_STENCIL_TEST
);
2136 else if (_mesa_is_depth_format(format
)) {
2137 if (ctx
->Extensions
.ARB_depth_texture
&&
2138 ctx
->Extensions
.ARB_fragment_program
) {
2139 texIntFormat
= GL_DEPTH_COMPONENT
;
2140 metaExtraSave
= (MESA_META_SHADER
);
2151 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2152 format
, type
, unpack
, pixels
);
2157 * Check image size against max texture size, draw as tiles if needed.
2159 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2160 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2161 format
, type
, unpack
, pixels
);
2165 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2166 * but a there's a few things we need to override:
2168 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2171 MESA_META_TRANSFORM
|
2174 MESA_META_VIEWPORT
|
2177 newTex
= _mesa_meta_alloc_texture(tex
, width
, height
, texIntFormat
);
2179 _mesa_meta_setup_vertex_objects(&drawpix
->VAO
, &drawpix
->VBO
, false,
2182 /* Silence valgrind warnings about reading uninitialized stack. */
2183 memset(verts
, 0, sizeof(verts
));
2185 /* vertex positions, texcoords (after texture allocation!) */
2187 const GLfloat x0
= (GLfloat
) x
;
2188 const GLfloat y0
= (GLfloat
) y
;
2189 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2190 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2191 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2196 verts
[0].tex
[0] = 0.0F
;
2197 verts
[0].tex
[1] = 0.0F
;
2201 verts
[1].tex
[0] = tex
->Sright
;
2202 verts
[1].tex
[1] = 0.0F
;
2206 verts
[2].tex
[0] = tex
->Sright
;
2207 verts
[2].tex
[1] = tex
->Ttop
;
2211 verts
[3].tex
[0] = 0.0F
;
2212 verts
[3].tex
[1] = tex
->Ttop
;
2215 /* upload new vertex data */
2216 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2217 verts
, GL_DYNAMIC_DRAW_ARB
);
2219 /* set given unpack params */
2220 ctx
->Unpack
= *unpack
;
2222 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2224 if (_mesa_is_stencil_format(format
)) {
2225 /* Drawing stencil */
2228 if (!drawpix
->StencilFP
)
2229 init_draw_stencil_pixels(ctx
);
2231 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2232 GL_ALPHA
, type
, pixels
);
2234 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2236 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2238 /* set all stencil bits to 0 */
2239 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2240 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2241 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2243 /* set stencil bits to 1 where needed */
2244 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2246 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2247 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2249 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2250 const GLuint mask
= 1 << bit
;
2251 if (mask
& origStencilMask
) {
2252 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2253 _mesa_StencilMask(mask
);
2255 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2256 255.0f
/ mask
, 0.5f
, 0.0f
, 0.0f
);
2258 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2262 else if (_mesa_is_depth_format(format
)) {
2264 if (!drawpix
->DepthFP
)
2265 init_draw_depth_pixels(ctx
);
2267 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2268 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2270 /* polygon color = current raster color */
2271 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2272 ctx
->Current
.RasterColor
);
2274 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2275 format
, type
, pixels
);
2277 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2281 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2282 format
, type
, pixels
);
2283 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2286 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2288 /* restore unpack params */
2289 ctx
->Unpack
= unpackSave
;
2291 _mesa_meta_end(ctx
);
2295 alpha_test_raster_color(struct gl_context
*ctx
)
2297 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2298 GLfloat ref
= ctx
->Color
.AlphaRef
;
2300 switch (ctx
->Color
.AlphaFunc
) {
2306 return alpha
== ref
;
2308 return alpha
<= ref
;
2312 return alpha
!= ref
;
2314 return alpha
>= ref
;
2324 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2325 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2326 * tracker would improve performance a lot.
2329 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2330 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2331 const struct gl_pixelstore_attrib
*unpack
,
2332 const GLubyte
*bitmap1
)
2334 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2335 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2336 const GLenum texIntFormat
= GL_ALPHA
;
2337 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2339 struct vertex verts
[4];
2344 * Check if swrast fallback is needed.
2346 if (ctx
->_ImageTransferState
||
2347 ctx
->FragmentProgram
._Enabled
||
2349 ctx
->Texture
._MaxEnabledTexImageUnit
!= -1 ||
2350 width
> tex
->MaxSize
||
2351 height
> tex
->MaxSize
) {
2352 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2356 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2359 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2360 * but a there's a few things we need to override:
2362 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
2363 MESA_META_PIXEL_STORE
|
2364 MESA_META_RASTERIZATION
|
2367 MESA_META_TRANSFORM
|
2370 MESA_META_VIEWPORT
));
2372 _mesa_meta_setup_vertex_objects(&bitmap
->VAO
, &bitmap
->VBO
, false, 3, 2, 4);
2374 newTex
= _mesa_meta_alloc_texture(tex
, width
, height
, texIntFormat
);
2376 /* Silence valgrind warnings about reading uninitialized stack. */
2377 memset(verts
, 0, sizeof(verts
));
2379 /* vertex positions, texcoords, colors (after texture allocation!) */
2381 const GLfloat x0
= (GLfloat
) x
;
2382 const GLfloat y0
= (GLfloat
) y
;
2383 const GLfloat x1
= (GLfloat
) (x
+ width
);
2384 const GLfloat y1
= (GLfloat
) (y
+ height
);
2385 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2391 verts
[0].tex
[0] = 0.0F
;
2392 verts
[0].tex
[1] = 0.0F
;
2396 verts
[1].tex
[0] = tex
->Sright
;
2397 verts
[1].tex
[1] = 0.0F
;
2401 verts
[2].tex
[0] = tex
->Sright
;
2402 verts
[2].tex
[1] = tex
->Ttop
;
2406 verts
[3].tex
[0] = 0.0F
;
2407 verts
[3].tex
[1] = tex
->Ttop
;
2409 for (i
= 0; i
< 4; i
++) {
2410 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2411 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2412 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2413 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2416 /* upload new vertex data */
2417 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2420 /* choose different foreground/background alpha values */
2421 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2422 bg
= (fg
> 127 ? 0 : 255);
2424 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2426 _mesa_meta_end(ctx
);
2430 bitmap8
= malloc(width
* height
);
2432 memset(bitmap8
, bg
, width
* height
);
2433 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2434 bitmap8
, width
, fg
);
2436 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2438 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2439 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2441 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2442 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2444 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2446 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2451 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2453 _mesa_meta_end(ctx
);
2457 * Compute the texture coordinates for the four vertices of a quad for
2458 * drawing a 2D texture image or slice of a cube/3D texture.
2459 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2460 * \param slice slice of a 1D/2D array texture or 3D texture
2461 * \param width width of the texture image
2462 * \param height height of the texture image
2463 * \param coords0/1/2/3 returns the computed texcoords
2466 _mesa_meta_setup_texture_coords(GLenum faceTarget
,
2476 static const GLfloat st
[4][2] = {
2477 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2482 if (faceTarget
== GL_TEXTURE_CUBE_MAP_ARRAY
)
2483 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ slice
% 6;
2485 /* Currently all texture targets want the W component to be 1.0.
2492 switch (faceTarget
) {
2496 case GL_TEXTURE_2D_ARRAY
:
2497 if (faceTarget
== GL_TEXTURE_3D
) {
2498 assert(slice
< depth
);
2500 r
= (slice
+ 0.5f
) / depth
;
2502 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
2506 coords0
[0] = 0.0F
; /* s */
2507 coords0
[1] = 0.0F
; /* t */
2508 coords0
[2] = r
; /* r */
2519 case GL_TEXTURE_RECTANGLE_ARB
:
2520 coords0
[0] = 0.0F
; /* s */
2521 coords0
[1] = 0.0F
; /* t */
2522 coords0
[2] = 0.0F
; /* r */
2523 coords1
[0] = (float) width
;
2526 coords2
[0] = (float) width
;
2527 coords2
[1] = (float) height
;
2530 coords3
[1] = (float) height
;
2533 case GL_TEXTURE_1D_ARRAY
:
2534 coords0
[0] = 0.0F
; /* s */
2535 coords0
[1] = (float) slice
; /* t */
2536 coords0
[2] = 0.0F
; /* r */
2538 coords1
[1] = (float) slice
;
2541 coords2
[1] = (float) slice
;
2544 coords3
[1] = (float) slice
;
2548 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2549 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2550 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2551 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2552 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2553 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2554 /* loop over quad verts */
2555 for (i
= 0; i
< 4; i
++) {
2556 /* Compute sc = +/-scale and tc = +/-scale.
2557 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2558 * though that can still sometimes happen with this scale factor...
2560 const GLfloat scale
= 0.9999f
;
2561 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2562 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2582 coord
[3] = (float) (slice
/ 6);
2584 switch (faceTarget
) {
2585 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2590 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2595 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2600 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2605 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2610 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2621 assert(!"unexpected target in _mesa_meta_setup_texture_coords()");
2625 static struct blit_shader
*
2626 choose_blit_shader(GLenum target
, struct blit_shader_table
*table
)
2630 table
->sampler_1d
.type
= "sampler1D";
2631 table
->sampler_1d
.func
= "texture1D";
2632 table
->sampler_1d
.texcoords
= "texCoords.x";
2633 return &table
->sampler_1d
;
2635 table
->sampler_2d
.type
= "sampler2D";
2636 table
->sampler_2d
.func
= "texture2D";
2637 table
->sampler_2d
.texcoords
= "texCoords.xy";
2638 return &table
->sampler_2d
;
2639 case GL_TEXTURE_RECTANGLE
:
2640 table
->sampler_rect
.type
= "sampler2DRect";
2641 table
->sampler_rect
.func
= "texture2DRect";
2642 table
->sampler_rect
.texcoords
= "texCoords.xy";
2643 return &table
->sampler_rect
;
2645 /* Code for mipmap generation with 3D textures is not used yet.
2646 * It's a sw fallback.
2648 table
->sampler_3d
.type
= "sampler3D";
2649 table
->sampler_3d
.func
= "texture3D";
2650 table
->sampler_3d
.texcoords
= "texCoords.xyz";
2651 return &table
->sampler_3d
;
2652 case GL_TEXTURE_CUBE_MAP
:
2653 table
->sampler_cubemap
.type
= "samplerCube";
2654 table
->sampler_cubemap
.func
= "textureCube";
2655 table
->sampler_cubemap
.texcoords
= "texCoords.xyz";
2656 return &table
->sampler_cubemap
;
2657 case GL_TEXTURE_1D_ARRAY
:
2658 table
->sampler_1d_array
.type
= "sampler1DArray";
2659 table
->sampler_1d_array
.func
= "texture1DArray";
2660 table
->sampler_1d_array
.texcoords
= "texCoords.xy";
2661 return &table
->sampler_1d_array
;
2662 case GL_TEXTURE_2D_ARRAY
:
2663 table
->sampler_2d_array
.type
= "sampler2DArray";
2664 table
->sampler_2d_array
.func
= "texture2DArray";
2665 table
->sampler_2d_array
.texcoords
= "texCoords.xyz";
2666 return &table
->sampler_2d_array
;
2667 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2668 table
->sampler_cubemap_array
.type
= "samplerCubeArray";
2669 table
->sampler_cubemap_array
.func
= "textureCubeArray";
2670 table
->sampler_cubemap_array
.texcoords
= "texCoords.xyzw";
2671 return &table
->sampler_cubemap_array
;
2673 _mesa_problem(NULL
, "Unexpected texture target 0x%x in"
2674 " setup_texture_sampler()\n", target
);
2680 _mesa_meta_blit_shader_table_cleanup(struct blit_shader_table
*table
)
2682 _mesa_DeleteProgram(table
->sampler_1d
.shader_prog
);
2683 _mesa_DeleteProgram(table
->sampler_2d
.shader_prog
);
2684 _mesa_DeleteProgram(table
->sampler_3d
.shader_prog
);
2685 _mesa_DeleteProgram(table
->sampler_rect
.shader_prog
);
2686 _mesa_DeleteProgram(table
->sampler_cubemap
.shader_prog
);
2687 _mesa_DeleteProgram(table
->sampler_1d_array
.shader_prog
);
2688 _mesa_DeleteProgram(table
->sampler_2d_array
.shader_prog
);
2689 _mesa_DeleteProgram(table
->sampler_cubemap_array
.shader_prog
);
2691 table
->sampler_1d
.shader_prog
= 0;
2692 table
->sampler_2d
.shader_prog
= 0;
2693 table
->sampler_3d
.shader_prog
= 0;
2694 table
->sampler_rect
.shader_prog
= 0;
2695 table
->sampler_cubemap
.shader_prog
= 0;
2696 table
->sampler_1d_array
.shader_prog
= 0;
2697 table
->sampler_2d_array
.shader_prog
= 0;
2698 table
->sampler_cubemap_array
.shader_prog
= 0;
2702 * Determine the GL data type to use for the temporary image read with
2703 * ReadPixels() and passed to Tex[Sub]Image().
2706 get_temp_image_type(struct gl_context
*ctx
, mesa_format format
)
2710 baseFormat
= _mesa_get_format_base_format(format
);
2712 switch (baseFormat
) {
2719 case GL_LUMINANCE_ALPHA
:
2721 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8) {
2722 return GL_UNSIGNED_BYTE
;
2723 } else if (ctx
->DrawBuffer
->Visual
.redBits
<= 16) {
2724 return GL_UNSIGNED_SHORT
;
2726 GLenum datatype
= _mesa_get_format_datatype(format
);
2727 if (datatype
== GL_INT
|| datatype
== GL_UNSIGNED_INT
)
2731 case GL_DEPTH_COMPONENT
: {
2732 GLenum datatype
= _mesa_get_format_datatype(format
);
2733 if (datatype
== GL_FLOAT
)
2736 return GL_UNSIGNED_INT
;
2738 case GL_DEPTH_STENCIL
: {
2739 GLenum datatype
= _mesa_get_format_datatype(format
);
2740 if (datatype
== GL_FLOAT
)
2741 return GL_FLOAT_32_UNSIGNED_INT_24_8_REV
;
2743 return GL_UNSIGNED_INT_24_8
;
2746 _mesa_problem(ctx
, "Unexpected format %d in get_temp_image_type()",
2753 * Attempts to wrap the destination texture in an FBO and use
2754 * glBlitFramebuffer() to implement glCopyTexSubImage().
2757 copytexsubimage_using_blit_framebuffer(struct gl_context
*ctx
, GLuint dims
,
2758 struct gl_texture_image
*texImage
,
2762 struct gl_renderbuffer
*rb
,
2764 GLsizei width
, GLsizei height
)
2766 struct gl_texture_object
*texObj
= texImage
->TexObject
;
2768 bool success
= false;
2772 if (!ctx
->Extensions
.ARB_framebuffer_object
)
2775 _mesa_unlock_texture(ctx
, texObj
);
2777 _mesa_meta_begin(ctx
, MESA_META_ALL
);
2779 _mesa_GenFramebuffers(1, &fbo
);
2780 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, fbo
);
2782 if (rb
->_BaseFormat
== GL_DEPTH_STENCIL
||
2783 rb
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2784 _mesa_meta_bind_fbo_image(GL_DEPTH_ATTACHMENT
, texImage
, zoffset
);
2785 mask
= GL_DEPTH_BUFFER_BIT
;
2787 if (rb
->_BaseFormat
== GL_DEPTH_STENCIL
&&
2788 texImage
->_BaseFormat
== GL_DEPTH_STENCIL
) {
2789 _mesa_meta_bind_fbo_image(GL_STENCIL_ATTACHMENT
, texImage
, zoffset
);
2790 mask
|= GL_STENCIL_BUFFER_BIT
;
2792 _mesa_DrawBuffer(GL_NONE
);
2794 _mesa_meta_bind_fbo_image(GL_COLOR_ATTACHMENT0
, texImage
, zoffset
);
2795 mask
= GL_COLOR_BUFFER_BIT
;
2796 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0
);
2799 status
= _mesa_CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER
);
2800 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
2803 ctx
->Meta
->Blit
.no_ctsi_fallback
= true;
2805 /* Since we've bound a new draw framebuffer, we need to update
2806 * its derived state -- _Xmin, etc -- for BlitFramebuffer's clipping to
2809 _mesa_update_state(ctx
);
2811 /* We skip the core BlitFramebuffer checks for format consistency, which
2812 * are too strict for CopyTexImage. We know meta will be fine with format
2815 _mesa_meta_BlitFramebuffer(ctx
, x
, y
,
2816 x
+ width
, y
+ height
,
2818 xoffset
+ width
, yoffset
+ height
,
2820 ctx
->Meta
->Blit
.no_ctsi_fallback
= false;
2824 _mesa_lock_texture(ctx
, texObj
);
2825 _mesa_DeleteFramebuffers(1, &fbo
);
2826 _mesa_meta_end(ctx
);
2831 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2832 * Have to be careful with locking and meta state for pixel transfer.
2835 _mesa_meta_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2836 struct gl_texture_image
*texImage
,
2837 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2838 struct gl_renderbuffer
*rb
,
2840 GLsizei width
, GLsizei height
)
2842 struct gl_texture_object
*texObj
= texImage
->TexObject
;
2843 GLenum format
, type
;
2847 if (copytexsubimage_using_blit_framebuffer(ctx
, dims
,
2849 xoffset
, yoffset
, zoffset
,
2856 /* Choose format/type for temporary image buffer */
2857 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
2858 if (format
== GL_LUMINANCE
||
2859 format
== GL_LUMINANCE_ALPHA
||
2860 format
== GL_INTENSITY
) {
2861 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
2862 * temp image buffer because glReadPixels will do L=R+G+B which is
2863 * not what we want (should be L=R).
2868 type
= get_temp_image_type(ctx
, texImage
->TexFormat
);
2869 if (_mesa_is_format_integer_color(texImage
->TexFormat
)) {
2870 format
= _mesa_base_format_to_integer_format(format
);
2872 bpp
= _mesa_bytes_per_pixel(format
, type
);
2874 _mesa_problem(ctx
, "Bad bpp in _mesa_meta_CopyTexSubImage()");
2879 * Alloc image buffer (XXX could use a PBO)
2881 buf
= malloc(width
* height
* bpp
);
2883 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
2887 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2890 * Read image from framebuffer (disable pixel transfer ops)
2892 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
2893 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2894 format
, type
, &ctx
->Pack
, buf
);
2895 _mesa_meta_end(ctx
);
2897 _mesa_update_state(ctx
); /* to update pixel transfer state */
2900 * Store texture data (with pixel transfer ops)
2902 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
2904 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
2905 assert(yoffset
== 0);
2906 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
2907 xoffset
, zoffset
, 0, width
, 1, 1,
2908 format
, type
, buf
, &ctx
->Unpack
);
2910 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
2911 xoffset
, yoffset
, zoffset
, width
, height
, 1,
2912 format
, type
, buf
, &ctx
->Unpack
);
2915 _mesa_meta_end(ctx
);
2917 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
2924 meta_decompress_cleanup(struct decompress_state
*decompress
)
2926 if (decompress
->FBO
!= 0) {
2927 _mesa_DeleteFramebuffers(1, &decompress
->FBO
);
2928 _mesa_DeleteRenderbuffers(1, &decompress
->RBO
);
2931 if (decompress
->VAO
!= 0) {
2932 _mesa_DeleteVertexArrays(1, &decompress
->VAO
);
2933 _mesa_DeleteBuffers(1, &decompress
->VBO
);
2936 if (decompress
->Sampler
!= 0)
2937 _mesa_DeleteSamplers(1, &decompress
->Sampler
);
2939 memset(decompress
, 0, sizeof(*decompress
));
2943 * Decompress a texture image by drawing a quad with the compressed
2944 * texture and reading the pixels out of the color buffer.
2945 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
2946 * \param destFormat format, ala glReadPixels
2947 * \param destType type, ala glReadPixels
2948 * \param dest destination buffer
2949 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
2952 decompress_texture_image(struct gl_context
*ctx
,
2953 struct gl_texture_image
*texImage
,
2955 GLenum destFormat
, GLenum destType
,
2958 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
2959 struct gl_texture_object
*texObj
= texImage
->TexObject
;
2960 const GLint width
= texImage
->Width
;
2961 const GLint height
= texImage
->Height
;
2962 const GLint depth
= texImage
->Height
;
2963 const GLenum target
= texObj
->Target
;
2965 struct vertex verts
[4];
2967 const bool use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
2968 ctx
->Extensions
.ARB_fragment_shader
;
2971 assert(target
== GL_TEXTURE_3D
||
2972 target
== GL_TEXTURE_2D_ARRAY
||
2973 target
== GL_TEXTURE_CUBE_MAP_ARRAY
);
2978 case GL_TEXTURE_1D_ARRAY
:
2979 assert(!"No compressed 1D textures.");
2983 assert(!"No compressed 3D textures.");
2986 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2987 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ (slice
% 6);
2990 case GL_TEXTURE_CUBE_MAP
:
2991 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
2995 faceTarget
= target
;
2999 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_PIXEL_STORE
);
3001 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
3002 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
3004 /* Create/bind FBO/renderbuffer */
3005 if (decompress
->FBO
== 0) {
3006 _mesa_GenFramebuffers(1, &decompress
->FBO
);
3007 _mesa_GenRenderbuffers(1, &decompress
->RBO
);
3008 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3009 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3010 _mesa_FramebufferRenderbuffer(GL_FRAMEBUFFER_EXT
,
3011 GL_COLOR_ATTACHMENT0_EXT
,
3012 GL_RENDERBUFFER_EXT
,
3016 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3019 /* alloc dest surface */
3020 if (width
> decompress
->Width
|| height
> decompress
->Height
) {
3021 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3022 _mesa_RenderbufferStorage(GL_RENDERBUFFER_EXT
, GL_RGBA
,
3024 decompress
->Width
= width
;
3025 decompress
->Height
= height
;
3028 if (use_glsl_version
) {
3029 _mesa_meta_setup_vertex_objects(&decompress
->VAO
, &decompress
->VBO
, true,
3032 _mesa_meta_setup_blit_shader(ctx
, target
, &decompress
->shaders
);
3034 _mesa_meta_setup_ff_tnl_for_blit(&decompress
->VAO
, &decompress
->VBO
, 3);
3037 if (!decompress
->Sampler
) {
3038 _mesa_GenSamplers(1, &decompress
->Sampler
);
3039 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
3040 /* nearest filtering */
3041 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
3042 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
3043 /* No sRGB decode or encode.*/
3044 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3045 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
3046 GL_SKIP_DECODE_EXT
);
3050 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
3053 /* Silence valgrind warnings about reading uninitialized stack. */
3054 memset(verts
, 0, sizeof(verts
));
3056 _mesa_meta_setup_texture_coords(faceTarget
, slice
, width
, height
, depth
,
3062 /* setup vertex positions */
3072 _mesa_set_viewport(ctx
, 0, 0, 0, width
, height
);
3074 /* upload new vertex data */
3075 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3077 /* setup texture state */
3078 _mesa_BindTexture(target
, texObj
->Name
);
3080 if (!use_glsl_version
)
3081 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3084 /* save texture object state */
3085 const GLint baseLevelSave
= texObj
->BaseLevel
;
3086 const GLint maxLevelSave
= texObj
->MaxLevel
;
3088 /* restrict sampling to the texture level of interest */
3089 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3090 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, texImage
->Level
);
3091 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texImage
->Level
);
3094 /* render quad w/ texture into renderbuffer */
3095 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3097 /* Restore texture object state, the texture binding will
3098 * be restored by _mesa_meta_end().
3100 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3101 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
3102 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3107 /* read pixels from renderbuffer */
3109 GLenum baseTexFormat
= texImage
->_BaseFormat
;
3110 GLenum destBaseFormat
= _mesa_base_tex_format(ctx
, destFormat
);
3112 /* The pixel transfer state will be set to default values at this point
3113 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
3114 * turned off (as required by glGetTexImage) but we need to handle some
3115 * special cases. In particular, single-channel texture values are
3116 * returned as red and two-channel texture values are returned as
3119 if ((baseTexFormat
== GL_LUMINANCE
||
3120 baseTexFormat
== GL_LUMINANCE_ALPHA
||
3121 baseTexFormat
== GL_INTENSITY
) ||
3122 /* If we're reading back an RGB(A) texture (using glGetTexImage) as
3123 * luminance then we need to return L=tex(R).
3125 ((baseTexFormat
== GL_RGBA
||
3126 baseTexFormat
== GL_RGB
||
3127 baseTexFormat
== GL_RG
) &&
3128 (destBaseFormat
== GL_LUMINANCE
||
3129 destBaseFormat
== GL_LUMINANCE_ALPHA
||
3130 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
3131 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
))) {
3132 /* Green and blue must be zero */
3133 _mesa_PixelTransferf(GL_GREEN_SCALE
, 0.0f
);
3134 _mesa_PixelTransferf(GL_BLUE_SCALE
, 0.0f
);
3137 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
3140 /* disable texture unit */
3141 if (!use_glsl_version
)
3142 _mesa_set_enable(ctx
, target
, GL_FALSE
);
3144 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
3146 _mesa_meta_end(ctx
);
3151 * This is just a wrapper around _mesa_get_tex_image() and
3152 * decompress_texture_image(). Meta functions should not be directly called
3156 _mesa_meta_GetTexImage(struct gl_context
*ctx
,
3157 GLenum format
, GLenum type
, GLvoid
*pixels
,
3158 struct gl_texture_image
*texImage
)
3160 /* We can only use the decompress-with-blit method here if the texels are
3161 * unsigned, normalized values. We could handle signed and unnormalized
3162 * with floating point renderbuffers...
3164 if (_mesa_is_format_compressed(texImage
->TexFormat
) &&
3165 _mesa_get_format_datatype(texImage
->TexFormat
)
3166 == GL_UNSIGNED_NORMALIZED
) {
3167 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3169 /* Need to unlock the texture here to prevent deadlock... */
3170 _mesa_unlock_texture(ctx
, texObj
);
3171 for (slice
= 0; slice
< texImage
->Depth
; slice
++) {
3173 if (texImage
->TexObject
->Target
== GL_TEXTURE_2D_ARRAY
3174 || texImage
->TexObject
->Target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
3175 /* Setup pixel packing. SkipPixels and SkipRows will be applied
3176 * in the decompress_texture_image() function's call to
3177 * glReadPixels but we need to compute the dest slice's address
3178 * here (according to SkipImages and ImageHeight).
3180 struct gl_pixelstore_attrib packing
= ctx
->Pack
;
3181 packing
.SkipPixels
= 0;
3182 packing
.SkipRows
= 0;
3183 dst
= _mesa_image_address3d(&packing
, pixels
, texImage
->Width
,
3184 texImage
->Height
, format
, type
,
3190 decompress_texture_image(ctx
, texImage
, slice
, format
, type
, dst
);
3192 /* ... and relock it */
3193 _mesa_lock_texture(ctx
, texObj
);
3196 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
3202 * Meta implementation of ctx->Driver.DrawTex() in terms
3203 * of polygon rendering.
3206 _mesa_meta_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
3207 GLfloat width
, GLfloat height
)
3209 struct drawtex_state
*drawtex
= &ctx
->Meta
->DrawTex
;
3211 GLfloat x
, y
, z
, st
[MAX_TEXTURE_UNITS
][2];
3213 struct vertex verts
[4];
3216 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
3218 MESA_META_TRANSFORM
|
3220 MESA_META_VIEWPORT
));
3222 if (drawtex
->VAO
== 0) {
3223 /* one-time setup */
3224 GLint active_texture
;
3226 /* create vertex array object */
3227 _mesa_GenVertexArrays(1, &drawtex
->VAO
);
3228 _mesa_BindVertexArray(drawtex
->VAO
);
3230 /* create vertex array buffer */
3231 _mesa_GenBuffers(1, &drawtex
->VBO
);
3232 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3233 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3234 NULL
, GL_DYNAMIC_DRAW_ARB
);
3236 /* client active texture is not part of the array object */
3237 active_texture
= ctx
->Array
.ActiveTexture
;
3239 /* setup vertex arrays */
3240 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3241 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3242 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3243 _mesa_ClientActiveTexture(GL_TEXTURE0
+ i
);
3244 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(st
[i
]));
3245 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3248 /* restore client active texture */
3249 _mesa_ClientActiveTexture(GL_TEXTURE0
+ active_texture
);
3252 _mesa_BindVertexArray(drawtex
->VAO
);
3253 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3256 /* vertex positions, texcoords */
3258 const GLfloat x1
= x
+ width
;
3259 const GLfloat y1
= y
+ height
;
3261 z
= CLAMP(z
, 0.0f
, 1.0f
);
3280 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3281 const struct gl_texture_object
*texObj
;
3282 const struct gl_texture_image
*texImage
;
3283 GLfloat s
, t
, s1
, t1
;
3286 if (!ctx
->Texture
.Unit
[i
]._Current
) {
3288 for (j
= 0; j
< 4; j
++) {
3289 verts
[j
].st
[i
][0] = 0.0f
;
3290 verts
[j
].st
[i
][1] = 0.0f
;
3295 texObj
= ctx
->Texture
.Unit
[i
]._Current
;
3296 texImage
= texObj
->Image
[0][texObj
->BaseLevel
];
3297 tw
= texImage
->Width2
;
3298 th
= texImage
->Height2
;
3300 s
= (GLfloat
) texObj
->CropRect
[0] / tw
;
3301 t
= (GLfloat
) texObj
->CropRect
[1] / th
;
3302 s1
= (GLfloat
) (texObj
->CropRect
[0] + texObj
->CropRect
[2]) / tw
;
3303 t1
= (GLfloat
) (texObj
->CropRect
[1] + texObj
->CropRect
[3]) / th
;
3305 verts
[0].st
[i
][0] = s
;
3306 verts
[0].st
[i
][1] = t
;
3308 verts
[1].st
[i
][0] = s1
;
3309 verts
[1].st
[i
][1] = t
;
3311 verts
[2].st
[i
][0] = s1
;
3312 verts
[2].st
[i
][1] = t1
;
3314 verts
[3].st
[i
][0] = s
;
3315 verts
[3].st
[i
][1] = t1
;
3318 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3321 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3323 _mesa_meta_end(ctx
);