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/clear.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/format_unpack.h"
51 #include "main/framebuffer.h"
52 #include "main/glformats.h"
53 #include "main/image.h"
54 #include "main/macros.h"
55 #include "main/matrix.h"
56 #include "main/mipmap.h"
57 #include "main/multisample.h"
58 #include "main/objectlabel.h"
59 #include "main/pipelineobj.h"
60 #include "main/pixel.h"
62 #include "main/polygon.h"
63 #include "main/queryobj.h"
64 #include "main/readpix.h"
65 #include "main/renderbuffer.h"
66 #include "main/scissor.h"
67 #include "main/shaderapi.h"
68 #include "main/shaderobj.h"
69 #include "main/state.h"
70 #include "main/stencil.h"
71 #include "main/texobj.h"
72 #include "main/texenv.h"
73 #include "main/texgetimage.h"
74 #include "main/teximage.h"
75 #include "main/texparam.h"
76 #include "main/texstate.h"
77 #include "main/texstore.h"
78 #include "main/transformfeedback.h"
79 #include "main/uniforms.h"
80 #include "main/varray.h"
81 #include "main/viewport.h"
82 #include "main/samplerobj.h"
83 #include "program/program.h"
84 #include "swrast/swrast.h"
85 #include "drivers/common/meta.h"
86 #include "main/enums.h"
87 #include "main/glformats.h"
88 #include "util/ralloc.h"
90 /** Return offset in bytes of the field within a vertex struct */
91 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
94 meta_clear(struct gl_context
*ctx
, GLbitfield buffers
, bool glsl
);
96 static struct blit_shader
*
97 choose_blit_shader(GLenum target
, struct blit_shader_table
*table
);
99 static void cleanup_temp_texture(struct temp_texture
*tex
);
100 static void meta_glsl_clear_cleanup(struct gl_context
*ctx
,
101 struct clear_state
*clear
);
102 static void meta_decompress_cleanup(struct gl_context
*ctx
,
103 struct decompress_state
*decompress
);
104 static void meta_drawpix_cleanup(struct gl_context
*ctx
,
105 struct drawpix_state
*drawpix
);
108 _mesa_meta_framebuffer_texture_image(struct gl_context
*ctx
,
109 struct gl_framebuffer
*fb
,
111 struct gl_texture_image
*texImage
,
114 struct gl_texture_object
*texObj
= texImage
->TexObject
;
115 int level
= texImage
->Level
;
116 const GLenum texTarget
= texObj
->Target
== GL_TEXTURE_CUBE_MAP
117 ? GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
120 _mesa_framebuffer_texture(ctx
, fb
, attachment
, texObj
, texTarget
,
121 level
, layer
, false, __func__
);
125 _mesa_meta_compile_shader_with_debug(struct gl_context
*ctx
, GLenum target
,
126 const GLcharARB
*source
)
128 const GLuint name
= ~0;
129 struct gl_shader
*sh
;
131 sh
= ctx
->Driver
.NewShader(ctx
, name
, target
);
132 sh
->Source
= strdup(source
);
133 sh
->CompileStatus
= false;
134 _mesa_compile_shader(ctx
, sh
);
136 if (!sh
->CompileStatus
) {
139 "meta program compile failed:\n%s\nsource:\n%s\n",
140 sh
->InfoLog
, source
);
143 _mesa_reference_shader(ctx
, &sh
, NULL
);
150 _mesa_meta_link_program_with_debug(struct gl_context
*ctx
,
151 struct gl_shader_program
*sh_prog
)
153 _mesa_link_program(ctx
, sh_prog
);
155 if (!sh_prog
->LinkStatus
) {
156 _mesa_problem(ctx
, "meta program link failed:\n%s", sh_prog
->InfoLog
);
161 _mesa_meta_use_program(struct gl_context
*ctx
,
162 struct gl_shader_program
*sh_prog
)
164 /* Attach shader state to the binding point */
165 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
167 /* Update the program */
168 _mesa_use_program(ctx
, sh_prog
);
172 _mesa_meta_compile_and_link_program(struct gl_context
*ctx
,
173 const char *vs_source
,
174 const char *fs_source
,
176 struct gl_shader_program
**out_sh_prog
)
178 struct gl_shader_program
*sh_prog
;
179 const GLuint id
= ~0;
181 sh_prog
= _mesa_new_shader_program(id
);
182 sh_prog
->Label
= strdup(name
);
183 sh_prog
->NumShaders
= 2;
184 sh_prog
->Shaders
= malloc(2 * sizeof(struct gl_shader
*));
185 sh_prog
->Shaders
[0] =
186 _mesa_meta_compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_source
);
187 sh_prog
->Shaders
[1] =
188 _mesa_meta_compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_source
);
190 _mesa_meta_link_program_with_debug(ctx
, sh_prog
);
192 _mesa_meta_use_program(ctx
, sh_prog
);
194 *out_sh_prog
= sh_prog
;
198 * Generate a generic shader to blit from a texture to a framebuffer
200 * \param ctx Current GL context
201 * \param texTarget Texture target that will be the source of the blit
203 * \returns a handle to a shader program on success or zero on failure.
206 _mesa_meta_setup_blit_shader(struct gl_context
*ctx
,
209 struct blit_shader_table
*table
)
211 char *vs_source
, *fs_source
;
212 struct blit_shader
*shader
= choose_blit_shader(target
, table
);
213 const char *fs_input
, *vs_preprocess
, *fs_preprocess
;
216 if (ctx
->Const
.GLSLVersion
< 130) {
218 fs_preprocess
= "#extension GL_EXT_texture_array : enable";
219 fs_input
= "varying";
221 vs_preprocess
= "#version 130";
222 fs_preprocess
= "#version 130";
224 shader
->func
= "texture";
227 assert(shader
!= NULL
);
229 if (shader
->shader_prog
!= NULL
) {
230 _mesa_meta_use_program(ctx
, shader
->shader_prog
);
234 mem_ctx
= ralloc_context(NULL
);
236 vs_source
= ralloc_asprintf(mem_ctx
,
238 "#extension GL_ARB_explicit_attrib_location: enable\n"
239 "layout(location = 0) in vec2 position;\n"
240 "layout(location = 1) in vec4 textureCoords;\n"
241 "out vec4 texCoords;\n"
244 " texCoords = textureCoords;\n"
245 " gl_Position = vec4(position, 0.0, 1.0);\n"
249 fs_source
= ralloc_asprintf(mem_ctx
,
251 "#extension GL_ARB_texture_cube_map_array: enable\n"
252 "uniform %s texSampler;\n"
253 "%s vec4 texCoords;\n"
256 " gl_FragColor = %s(texSampler, %s);\n"
259 fs_preprocess
, shader
->type
, fs_input
,
260 shader
->func
, shader
->texcoords
,
261 do_depth
? " gl_FragDepth = gl_FragColor.x;\n" : "");
263 _mesa_meta_compile_and_link_program(ctx
, vs_source
, fs_source
,
264 ralloc_asprintf(mem_ctx
, "%s blit",
266 &shader
->shader_prog
);
267 ralloc_free(mem_ctx
);
271 * Configure vertex buffer and vertex array objects for tests
273 * Regardless of whether a new VAO is created, the object referenced by \c VAO
274 * will be bound into the GL state vector when this function terminates. The
275 * object referenced by \c VBO will \b not be bound.
277 * \param VAO Storage for vertex array object handle. If 0, a new VAO
279 * \param buf_obj Storage for vertex buffer object pointer. If \c NULL, a new VBO
280 * will be created. The new VBO will have storage for 4
281 * \c vertex structures.
282 * \param use_generic_attributes Should generic attributes 0 and 1 be used,
283 * or should traditional, fixed-function color and texture
284 * coordinate be used?
285 * \param vertex_size Number of components for attribute 0 / vertex.
286 * \param texcoord_size Number of components for attribute 1 / texture
287 * coordinate. If this is 0, attribute 1 will not be set or
289 * \param color_size Number of components for attribute 1 / primary color.
290 * If this is 0, attribute 1 will not be set or enabled.
292 * \note If \c use_generic_attributes is \c true, \c color_size must be zero.
293 * Use \c texcoord_size instead.
296 _mesa_meta_setup_vertex_objects(struct gl_context
*ctx
,
297 GLuint
*VAO
, struct gl_buffer_object
**buf_obj
,
298 bool use_generic_attributes
,
299 unsigned vertex_size
, unsigned texcoord_size
,
303 struct gl_vertex_array_object
*array_obj
;
304 assert(*buf_obj
== NULL
);
306 /* create vertex array object */
307 _mesa_GenVertexArrays(1, VAO
);
308 _mesa_BindVertexArray(*VAO
);
310 array_obj
= _mesa_lookup_vao(ctx
, *VAO
);
311 assert(array_obj
!= NULL
);
313 /* create vertex array buffer */
314 *buf_obj
= ctx
->Driver
.NewBufferObject(ctx
, 0xDEADBEEF);
315 if (*buf_obj
== NULL
)
318 _mesa_buffer_data(ctx
, *buf_obj
, GL_NONE
, 4 * sizeof(struct vertex
), NULL
,
319 GL_DYNAMIC_DRAW
, __func__
);
321 /* setup vertex arrays */
322 if (use_generic_attributes
) {
323 assert(color_size
== 0);
325 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_GENERIC(0),
326 vertex_size
, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
328 offsetof(struct vertex
, x
), true);
329 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_GENERIC(0),
330 *buf_obj
, 0, sizeof(struct vertex
));
331 _mesa_enable_vertex_array_attrib(ctx
, array_obj
,
332 VERT_ATTRIB_GENERIC(0));
333 if (texcoord_size
> 0) {
334 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_GENERIC(1),
335 texcoord_size
, GL_FLOAT
, GL_RGBA
,
336 GL_FALSE
, GL_FALSE
, GL_FALSE
,
337 offsetof(struct vertex
, tex
), false);
338 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_GENERIC(1),
339 *buf_obj
, 0, sizeof(struct vertex
));
340 _mesa_enable_vertex_array_attrib(ctx
, array_obj
,
341 VERT_ATTRIB_GENERIC(1));
344 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_POS
,
345 vertex_size
, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
347 offsetof(struct vertex
, x
), true);
348 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_POS
,
349 *buf_obj
, 0, sizeof(struct vertex
));
350 _mesa_enable_vertex_array_attrib(ctx
, array_obj
, VERT_ATTRIB_POS
);
352 if (texcoord_size
> 0) {
353 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_TEX(0),
354 vertex_size
, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
356 offsetof(struct vertex
, tex
), false);
357 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_TEX(0),
358 *buf_obj
, 0, sizeof(struct vertex
));
359 _mesa_enable_vertex_array_attrib(ctx
, array_obj
, VERT_ATTRIB_TEX(0));
362 if (color_size
> 0) {
363 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_COLOR0
,
364 vertex_size
, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
366 offsetof(struct vertex
, r
), false);
367 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_COLOR0
,
368 *buf_obj
, 0, sizeof(struct vertex
));
369 _mesa_enable_vertex_array_attrib(ctx
, array_obj
, VERT_ATTRIB_COLOR0
);
373 _mesa_BindVertexArray(*VAO
);
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
);
390 * Free context meta-op state.
391 * To be called once during context destruction.
394 _mesa_meta_free(struct gl_context
*ctx
)
396 GET_CURRENT_CONTEXT(old_context
);
397 _mesa_make_current(ctx
, NULL
, NULL
);
398 _mesa_meta_glsl_blit_cleanup(ctx
, &ctx
->Meta
->Blit
);
399 meta_glsl_clear_cleanup(ctx
, &ctx
->Meta
->Clear
);
400 _mesa_meta_glsl_generate_mipmap_cleanup(ctx
, &ctx
->Meta
->Mipmap
);
401 cleanup_temp_texture(&ctx
->Meta
->TempTex
);
402 meta_decompress_cleanup(ctx
, &ctx
->Meta
->Decompress
);
403 meta_drawpix_cleanup(ctx
, &ctx
->Meta
->DrawPix
);
405 _mesa_make_current(old_context
, old_context
->WinSysDrawBuffer
, old_context
->WinSysReadBuffer
);
407 _mesa_make_current(NULL
, NULL
, NULL
);
414 * Enter meta state. This is like a light-weight version of glPushAttrib
415 * but it also resets most GL state back to default values.
417 * \param state bitmask of MESA_META_* flags indicating which attribute groups
418 * to save and reset to their defaults
421 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
423 struct save_state
*save
;
425 /* hope MAX_META_OPS_DEPTH is large enough */
426 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
428 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
429 memset(save
, 0, sizeof(*save
));
430 save
->SavedState
= state
;
432 /* We always push into desktop GL mode and pop out at the end. No sense in
433 * writing our shaders varying based on the user's context choice, when
434 * Mesa can handle either.
436 save
->API
= ctx
->API
;
437 ctx
->API
= API_OPENGL_COMPAT
;
439 /* Mesa's extension helper functions use the current context's API to look up
440 * the version required by an extension as a step in determining whether or
441 * not it has been advertised. Since meta aims to only be restricted by the
442 * driver capability (and not by whether or not an extension has been
443 * advertised), set the helper functions' Version variable to a value that
444 * will make the checks on the context API and version unconditionally pass.
446 save
->ExtensionsVersion
= ctx
->Extensions
.Version
;
447 ctx
->Extensions
.Version
= ~0;
449 /* Pausing transform feedback needs to be done early, or else we won't be
450 * able to change other state.
452 save
->TransformFeedbackNeedsResume
=
453 _mesa_is_xfb_active_and_unpaused(ctx
);
454 if (save
->TransformFeedbackNeedsResume
)
455 _mesa_PauseTransformFeedback();
457 /* After saving the current occlusion object, call EndQuery so that no
458 * occlusion querying will be active during the meta-operation.
460 if (state
& MESA_META_OCCLUSION_QUERY
) {
461 save
->CurrentOcclusionObject
= ctx
->Query
.CurrentOcclusionObject
;
462 if (save
->CurrentOcclusionObject
)
463 _mesa_EndQuery(save
->CurrentOcclusionObject
->Target
);
466 if (state
& MESA_META_ALPHA_TEST
) {
467 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
468 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
469 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
470 if (ctx
->Color
.AlphaEnabled
)
471 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
474 if (state
& MESA_META_BLEND
) {
475 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
476 if (ctx
->Color
.BlendEnabled
) {
477 if (ctx
->Extensions
.EXT_draw_buffers2
) {
479 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
480 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
484 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
487 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
488 if (ctx
->Color
.ColorLogicOpEnabled
)
489 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
492 if (state
& MESA_META_DITHER
) {
493 save
->DitherFlag
= ctx
->Color
.DitherFlag
;
494 _mesa_set_enable(ctx
, GL_DITHER
, GL_TRUE
);
497 if (state
& MESA_META_COLOR_MASK
) {
498 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
499 sizeof(ctx
->Color
.ColorMask
));
500 if (!ctx
->Color
.ColorMask
[0][0] ||
501 !ctx
->Color
.ColorMask
[0][1] ||
502 !ctx
->Color
.ColorMask
[0][2] ||
503 !ctx
->Color
.ColorMask
[0][3])
504 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
507 if (state
& MESA_META_DEPTH_TEST
) {
508 save
->Depth
= ctx
->Depth
; /* struct copy */
510 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
513 if (state
& MESA_META_FOG
) {
514 save
->Fog
= ctx
->Fog
.Enabled
;
515 if (ctx
->Fog
.Enabled
)
516 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
519 if (state
& MESA_META_PIXEL_STORE
) {
520 save
->Pack
= ctx
->Pack
;
521 save
->Unpack
= ctx
->Unpack
;
522 ctx
->Pack
= ctx
->DefaultPacking
;
523 ctx
->Unpack
= ctx
->DefaultPacking
;
526 if (state
& MESA_META_PIXEL_TRANSFER
) {
527 save
->RedScale
= ctx
->Pixel
.RedScale
;
528 save
->RedBias
= ctx
->Pixel
.RedBias
;
529 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
530 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
531 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
532 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
533 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
534 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
535 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
536 ctx
->Pixel
.RedScale
= 1.0F
;
537 ctx
->Pixel
.RedBias
= 0.0F
;
538 ctx
->Pixel
.GreenScale
= 1.0F
;
539 ctx
->Pixel
.GreenBias
= 0.0F
;
540 ctx
->Pixel
.BlueScale
= 1.0F
;
541 ctx
->Pixel
.BlueBias
= 0.0F
;
542 ctx
->Pixel
.AlphaScale
= 1.0F
;
543 ctx
->Pixel
.AlphaBias
= 0.0F
;
544 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
546 ctx
->NewState
|=_NEW_PIXEL
;
549 if (state
& MESA_META_RASTERIZATION
) {
550 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
551 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
552 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
553 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
554 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
555 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
556 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
557 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
558 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
559 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
560 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
563 if (state
& MESA_META_SCISSOR
) {
564 save
->Scissor
= ctx
->Scissor
; /* struct copy */
565 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
568 if (state
& MESA_META_SHADER
) {
571 if (ctx
->Extensions
.ARB_vertex_program
) {
572 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
573 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
574 ctx
->VertexProgram
.Current
);
575 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
578 if (ctx
->Extensions
.ARB_fragment_program
) {
579 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
580 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
581 ctx
->FragmentProgram
.Current
);
582 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
585 if (ctx
->Extensions
.ATI_fragment_shader
) {
586 save
->ATIFragmentShaderEnabled
= ctx
->ATIFragmentShader
.Enabled
;
587 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
, GL_FALSE
);
590 if (ctx
->Pipeline
.Current
) {
591 _mesa_reference_pipeline_object(ctx
, &save
->Pipeline
,
592 ctx
->Pipeline
.Current
);
593 _mesa_BindProgramPipeline(0);
596 /* Save the shader state from ctx->Shader (instead of ctx->_Shader) so
597 * that we don't have to worry about the current pipeline state.
599 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
600 _mesa_reference_shader_program(ctx
, &save
->Shader
[i
],
601 ctx
->Shader
.CurrentProgram
[i
]);
603 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
604 ctx
->Shader
.ActiveProgram
);
609 if (state
& MESA_META_STENCIL_TEST
) {
610 save
->Stencil
= ctx
->Stencil
; /* struct copy */
611 if (ctx
->Stencil
.Enabled
)
612 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
613 /* NOTE: other stencil state not reset */
616 if (state
& MESA_META_TEXTURE
) {
619 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
620 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
622 /* Disable all texture units */
623 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
624 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
625 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
626 if (ctx
->Texture
.Unit
[u
].Enabled
||
627 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
628 _mesa_ActiveTexture(GL_TEXTURE0
+ u
);
629 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
630 if (ctx
->Extensions
.ARB_texture_cube_map
)
631 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
633 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
634 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
635 if (ctx
->Extensions
.NV_texture_rectangle
)
636 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
637 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
638 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
639 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
640 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
644 /* save current texture objects for unit[0] only */
645 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
646 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
647 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
650 /* set defaults for unit[0] */
651 _mesa_ActiveTexture(GL_TEXTURE0
);
652 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
655 if (state
& MESA_META_TRANSFORM
) {
656 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
657 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
658 16 * sizeof(GLfloat
));
659 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
660 16 * sizeof(GLfloat
));
661 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
662 16 * sizeof(GLfloat
));
663 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
664 /* set 1:1 vertex:pixel coordinate transform */
665 _mesa_ActiveTexture(GL_TEXTURE0
);
666 _mesa_MatrixMode(GL_TEXTURE
);
667 _mesa_LoadIdentity();
668 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
669 _mesa_MatrixMode(GL_MODELVIEW
);
670 _mesa_LoadIdentity();
671 _mesa_MatrixMode(GL_PROJECTION
);
672 _mesa_LoadIdentity();
674 /* glOrtho with width = 0 or height = 0 generates GL_INVALID_VALUE.
675 * This can occur when there is no draw buffer.
677 if (ctx
->DrawBuffer
->Width
!= 0 && ctx
->DrawBuffer
->Height
!= 0)
678 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
679 0.0, ctx
->DrawBuffer
->Height
,
682 if (ctx
->Extensions
.ARB_clip_control
) {
683 save
->ClipOrigin
= ctx
->Transform
.ClipOrigin
;
684 save
->ClipDepthMode
= ctx
->Transform
.ClipDepthMode
;
685 _mesa_ClipControl(GL_LOWER_LEFT
, GL_NEGATIVE_ONE_TO_ONE
);
689 if (state
& MESA_META_CLIP
) {
690 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
691 if (ctx
->Transform
.ClipPlanesEnabled
) {
693 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
694 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
699 if (state
& MESA_META_VERTEX
) {
700 /* save vertex array object state */
701 _mesa_reference_vao(ctx
, &save
->VAO
,
703 /* set some default state? */
706 if (state
& MESA_META_VIEWPORT
) {
707 /* save viewport state */
708 save
->ViewportX
= ctx
->ViewportArray
[0].X
;
709 save
->ViewportY
= ctx
->ViewportArray
[0].Y
;
710 save
->ViewportW
= ctx
->ViewportArray
[0].Width
;
711 save
->ViewportH
= ctx
->ViewportArray
[0].Height
;
712 /* set viewport to match window size */
713 if (ctx
->ViewportArray
[0].X
!= 0 ||
714 ctx
->ViewportArray
[0].Y
!= 0 ||
715 ctx
->ViewportArray
[0].Width
!= (float) ctx
->DrawBuffer
->Width
||
716 ctx
->ViewportArray
[0].Height
!= (float) ctx
->DrawBuffer
->Height
) {
717 _mesa_set_viewport(ctx
, 0, 0, 0,
718 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
720 /* save depth range state */
721 save
->DepthNear
= ctx
->ViewportArray
[0].Near
;
722 save
->DepthFar
= ctx
->ViewportArray
[0].Far
;
723 /* set depth range to default */
724 _mesa_set_depth_range(ctx
, 0, 0.0, 1.0);
727 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
728 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
730 /* Generally in here we want to do clamping according to whether
731 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
732 * regardless of the internal implementation of the metaops.
734 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
735 ctx
->Extensions
.ARB_color_buffer_float
)
736 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
739 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
740 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
742 /* Generally in here we never want vertex color clamping --
743 * result clamping is only dependent on fragment clamping.
745 if (ctx
->Extensions
.ARB_color_buffer_float
)
746 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
749 if (state
& MESA_META_CONDITIONAL_RENDER
) {
750 save
->CondRenderQuery
= ctx
->Query
.CondRenderQuery
;
751 save
->CondRenderMode
= ctx
->Query
.CondRenderMode
;
753 if (ctx
->Query
.CondRenderQuery
)
754 _mesa_EndConditionalRender();
757 if (state
& MESA_META_SELECT_FEEDBACK
) {
758 save
->RenderMode
= ctx
->RenderMode
;
759 if (ctx
->RenderMode
== GL_SELECT
) {
760 save
->Select
= ctx
->Select
; /* struct copy */
761 _mesa_RenderMode(GL_RENDER
);
762 } else if (ctx
->RenderMode
== GL_FEEDBACK
) {
763 save
->Feedback
= ctx
->Feedback
; /* struct copy */
764 _mesa_RenderMode(GL_RENDER
);
768 if (state
& MESA_META_MULTISAMPLE
) {
769 save
->Multisample
= ctx
->Multisample
; /* struct copy */
771 if (ctx
->Multisample
.Enabled
)
772 _mesa_set_multisample(ctx
, GL_FALSE
);
773 if (ctx
->Multisample
.SampleCoverage
)
774 _mesa_set_enable(ctx
, GL_SAMPLE_COVERAGE
, GL_FALSE
);
775 if (ctx
->Multisample
.SampleAlphaToCoverage
)
776 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_COVERAGE
, GL_FALSE
);
777 if (ctx
->Multisample
.SampleAlphaToOne
)
778 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_ONE
, GL_FALSE
);
779 if (ctx
->Multisample
.SampleShading
)
780 _mesa_set_enable(ctx
, GL_SAMPLE_SHADING
, GL_FALSE
);
781 if (ctx
->Multisample
.SampleMask
)
782 _mesa_set_enable(ctx
, GL_SAMPLE_MASK
, GL_FALSE
);
785 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
786 save
->sRGBEnabled
= ctx
->Color
.sRGBEnabled
;
787 if (ctx
->Color
.sRGBEnabled
)
788 _mesa_set_framebuffer_srgb(ctx
, GL_FALSE
);
791 if (state
& MESA_META_DRAW_BUFFERS
) {
792 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
793 memcpy(save
->ColorDrawBuffers
, fb
->ColorDrawBuffer
,
794 sizeof(save
->ColorDrawBuffers
));
799 save
->Lighting
= ctx
->Light
.Enabled
;
800 if (ctx
->Light
.Enabled
)
801 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
802 save
->RasterDiscard
= ctx
->RasterDiscard
;
803 if (ctx
->RasterDiscard
)
804 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_FALSE
);
806 _mesa_reference_framebuffer(&save
->DrawBuffer
, ctx
->DrawBuffer
);
807 _mesa_reference_framebuffer(&save
->ReadBuffer
, ctx
->ReadBuffer
);
813 * Leave meta state. This is like a light-weight version of glPopAttrib().
816 _mesa_meta_end(struct gl_context
*ctx
)
818 assert(ctx
->Meta
->SaveStackDepth
> 0);
820 struct save_state
*save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
- 1];
821 const GLbitfield state
= save
->SavedState
;
824 /* Grab the result of the old occlusion query before starting it again. The
825 * old result is added to the result of the new query so the driver will
826 * continue adding where it left off. */
827 if (state
& MESA_META_OCCLUSION_QUERY
) {
828 if (save
->CurrentOcclusionObject
) {
829 struct gl_query_object
*q
= save
->CurrentOcclusionObject
;
832 ctx
->Driver
.WaitQuery(ctx
, q
);
834 _mesa_BeginQuery(q
->Target
, q
->Id
);
835 ctx
->Query
.CurrentOcclusionObject
->Result
+= result
;
839 if (state
& MESA_META_ALPHA_TEST
) {
840 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
841 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
842 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
845 if (state
& MESA_META_BLEND
) {
846 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
847 if (ctx
->Extensions
.EXT_draw_buffers2
) {
849 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
850 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
854 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
857 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
858 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
861 if (state
& MESA_META_DITHER
)
862 _mesa_set_enable(ctx
, GL_DITHER
, save
->DitherFlag
);
864 if (state
& MESA_META_COLOR_MASK
) {
866 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
867 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
869 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
870 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
874 save
->ColorMask
[i
][0],
875 save
->ColorMask
[i
][1],
876 save
->ColorMask
[i
][2],
877 save
->ColorMask
[i
][3]);
883 if (state
& MESA_META_DEPTH_TEST
) {
884 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
885 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
886 _mesa_DepthFunc(save
->Depth
.Func
);
887 _mesa_DepthMask(save
->Depth
.Mask
);
890 if (state
& MESA_META_FOG
) {
891 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
894 if (state
& MESA_META_PIXEL_STORE
) {
895 ctx
->Pack
= save
->Pack
;
896 ctx
->Unpack
= save
->Unpack
;
899 if (state
& MESA_META_PIXEL_TRANSFER
) {
900 ctx
->Pixel
.RedScale
= save
->RedScale
;
901 ctx
->Pixel
.RedBias
= save
->RedBias
;
902 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
903 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
904 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
905 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
906 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
907 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
908 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
910 ctx
->NewState
|=_NEW_PIXEL
;
913 if (state
& MESA_META_RASTERIZATION
) {
914 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
915 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
916 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
917 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
918 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
919 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
922 if (state
& MESA_META_SCISSOR
) {
925 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++) {
926 _mesa_set_scissor(ctx
, i
,
927 save
->Scissor
.ScissorArray
[i
].X
,
928 save
->Scissor
.ScissorArray
[i
].Y
,
929 save
->Scissor
.ScissorArray
[i
].Width
,
930 save
->Scissor
.ScissorArray
[i
].Height
);
931 _mesa_set_enablei(ctx
, GL_SCISSOR_TEST
, i
,
932 (save
->Scissor
.EnableFlags
>> i
) & 1);
936 if (state
& MESA_META_SHADER
) {
937 static const GLenum targets
[] = {
939 GL_TESS_CONTROL_SHADER
,
940 GL_TESS_EVALUATION_SHADER
,
945 STATIC_ASSERT(MESA_SHADER_STAGES
== ARRAY_SIZE(targets
));
949 if (ctx
->Extensions
.ARB_vertex_program
) {
950 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
951 save
->VertexProgramEnabled
);
952 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
953 save
->VertexProgram
);
954 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
957 if (ctx
->Extensions
.ARB_fragment_program
) {
958 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
959 save
->FragmentProgramEnabled
);
960 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
961 save
->FragmentProgram
);
962 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
965 if (ctx
->Extensions
.ATI_fragment_shader
) {
966 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
,
967 save
->ATIFragmentShaderEnabled
);
971 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
972 /* It is safe to call _mesa_use_shader_program even if the extension
973 * necessary for that program state is not supported. In that case,
974 * the saved program object must be NULL and the currently bound
975 * program object must be NULL. _mesa_use_shader_program is a no-op
978 _mesa_use_shader_program(ctx
, targets
[i
],
982 /* Do this *before* killing the reference. :)
984 if (save
->Shader
[i
] != NULL
)
987 _mesa_reference_shader_program(ctx
, &save
->Shader
[i
], NULL
);
990 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
992 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
, NULL
);
994 /* If there were any stages set with programs, use ctx->Shader as the
995 * current shader state. Otherwise, use Pipeline.Default. The pipeline
996 * hasn't been restored yet, and that may modify ctx->_Shader further.
999 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
1002 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
1003 ctx
->Pipeline
.Default
);
1005 if (save
->Pipeline
) {
1006 _mesa_bind_pipeline(ctx
, save
->Pipeline
);
1008 _mesa_reference_pipeline_object(ctx
, &save
->Pipeline
, NULL
);
1012 if (state
& MESA_META_STENCIL_TEST
) {
1013 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
1015 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
1016 _mesa_ClearStencil(stencil
->Clear
);
1017 if (ctx
->Extensions
.EXT_stencil_two_side
) {
1018 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
1019 stencil
->TestTwoSide
);
1020 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
1021 ? GL_BACK
: GL_FRONT
);
1024 _mesa_StencilFuncSeparate(GL_FRONT
,
1025 stencil
->Function
[0],
1027 stencil
->ValueMask
[0]);
1028 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
1029 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
1030 stencil
->ZFailFunc
[0],
1031 stencil
->ZPassFunc
[0]);
1033 _mesa_StencilFuncSeparate(GL_BACK
,
1034 stencil
->Function
[1],
1036 stencil
->ValueMask
[1]);
1037 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
1038 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
1039 stencil
->ZFailFunc
[1],
1040 stencil
->ZPassFunc
[1]);
1043 if (state
& MESA_META_TEXTURE
) {
1046 assert(ctx
->Texture
.CurrentUnit
== 0);
1048 /* restore texenv for unit[0] */
1049 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
1051 /* restore texture objects for unit[0] only */
1052 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
1053 if (ctx
->Texture
.Unit
[0].CurrentTex
[tgt
] != save
->CurrentTexture
[tgt
]) {
1054 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1055 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
1056 save
->CurrentTexture
[tgt
]);
1058 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
1061 /* Restore fixed function texture enables, texgen */
1062 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
1063 if (ctx
->Texture
.Unit
[u
].Enabled
!= save
->TexEnabled
[u
]) {
1064 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1065 ctx
->Texture
.Unit
[u
].Enabled
= save
->TexEnabled
[u
];
1068 if (ctx
->Texture
.Unit
[u
].TexGenEnabled
!= save
->TexGenEnabled
[u
]) {
1069 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1070 ctx
->Texture
.Unit
[u
].TexGenEnabled
= save
->TexGenEnabled
[u
];
1074 /* restore current unit state */
1075 _mesa_ActiveTexture(GL_TEXTURE0
+ save
->ActiveUnit
);
1078 if (state
& MESA_META_TRANSFORM
) {
1079 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
1080 _mesa_ActiveTexture(GL_TEXTURE0
);
1081 _mesa_MatrixMode(GL_TEXTURE
);
1082 _mesa_LoadMatrixf(save
->TextureMatrix
);
1083 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
1085 _mesa_MatrixMode(GL_MODELVIEW
);
1086 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
1088 _mesa_MatrixMode(GL_PROJECTION
);
1089 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
1091 _mesa_MatrixMode(save
->MatrixMode
);
1093 if (ctx
->Extensions
.ARB_clip_control
)
1094 _mesa_ClipControl(save
->ClipOrigin
, save
->ClipDepthMode
);
1097 if (state
& MESA_META_CLIP
) {
1098 if (save
->ClipPlanesEnabled
) {
1100 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
1101 if (save
->ClipPlanesEnabled
& (1 << i
)) {
1102 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
1108 if (state
& MESA_META_VERTEX
) {
1109 /* restore vertex array object */
1110 _mesa_BindVertexArray(save
->VAO
->Name
);
1111 _mesa_reference_vao(ctx
, &save
->VAO
, NULL
);
1114 if (state
& MESA_META_VIEWPORT
) {
1115 if (save
->ViewportX
!= ctx
->ViewportArray
[0].X
||
1116 save
->ViewportY
!= ctx
->ViewportArray
[0].Y
||
1117 save
->ViewportW
!= ctx
->ViewportArray
[0].Width
||
1118 save
->ViewportH
!= ctx
->ViewportArray
[0].Height
) {
1119 _mesa_set_viewport(ctx
, 0, save
->ViewportX
, save
->ViewportY
,
1120 save
->ViewportW
, save
->ViewportH
);
1122 _mesa_set_depth_range(ctx
, 0, save
->DepthNear
, save
->DepthFar
);
1125 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
&&
1126 ctx
->Extensions
.ARB_color_buffer_float
) {
1127 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
1130 if (state
& MESA_META_CLAMP_VERTEX_COLOR
&&
1131 ctx
->Extensions
.ARB_color_buffer_float
) {
1132 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
1135 if (state
& MESA_META_CONDITIONAL_RENDER
) {
1136 if (save
->CondRenderQuery
)
1137 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
1138 save
->CondRenderMode
);
1141 if (state
& MESA_META_SELECT_FEEDBACK
) {
1142 if (save
->RenderMode
== GL_SELECT
) {
1143 _mesa_RenderMode(GL_SELECT
);
1144 ctx
->Select
= save
->Select
;
1145 } else if (save
->RenderMode
== GL_FEEDBACK
) {
1146 _mesa_RenderMode(GL_FEEDBACK
);
1147 ctx
->Feedback
= save
->Feedback
;
1151 if (state
& MESA_META_MULTISAMPLE
) {
1152 struct gl_multisample_attrib
*ctx_ms
= &ctx
->Multisample
;
1153 struct gl_multisample_attrib
*save_ms
= &save
->Multisample
;
1155 if (ctx_ms
->Enabled
!= save_ms
->Enabled
)
1156 _mesa_set_multisample(ctx
, save_ms
->Enabled
);
1157 if (ctx_ms
->SampleCoverage
!= save_ms
->SampleCoverage
)
1158 _mesa_set_enable(ctx
, GL_SAMPLE_COVERAGE
, save_ms
->SampleCoverage
);
1159 if (ctx_ms
->SampleAlphaToCoverage
!= save_ms
->SampleAlphaToCoverage
)
1160 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_COVERAGE
, save_ms
->SampleAlphaToCoverage
);
1161 if (ctx_ms
->SampleAlphaToOne
!= save_ms
->SampleAlphaToOne
)
1162 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_ONE
, save_ms
->SampleAlphaToOne
);
1163 if (ctx_ms
->SampleCoverageValue
!= save_ms
->SampleCoverageValue
||
1164 ctx_ms
->SampleCoverageInvert
!= save_ms
->SampleCoverageInvert
) {
1165 _mesa_SampleCoverage(save_ms
->SampleCoverageValue
,
1166 save_ms
->SampleCoverageInvert
);
1168 if (ctx_ms
->SampleShading
!= save_ms
->SampleShading
)
1169 _mesa_set_enable(ctx
, GL_SAMPLE_SHADING
, save_ms
->SampleShading
);
1170 if (ctx_ms
->SampleMask
!= save_ms
->SampleMask
)
1171 _mesa_set_enable(ctx
, GL_SAMPLE_MASK
, save_ms
->SampleMask
);
1172 if (ctx_ms
->SampleMaskValue
!= save_ms
->SampleMaskValue
)
1173 _mesa_SampleMaski(0, save_ms
->SampleMaskValue
);
1174 if (ctx_ms
->MinSampleShadingValue
!= save_ms
->MinSampleShadingValue
)
1175 _mesa_MinSampleShading(save_ms
->MinSampleShadingValue
);
1178 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
1179 if (ctx
->Color
.sRGBEnabled
!= save
->sRGBEnabled
)
1180 _mesa_set_framebuffer_srgb(ctx
, save
->sRGBEnabled
);
1184 if (save
->Lighting
) {
1185 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
1187 if (save
->RasterDiscard
) {
1188 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_TRUE
);
1190 if (save
->TransformFeedbackNeedsResume
)
1191 _mesa_ResumeTransformFeedback();
1193 _mesa_bind_framebuffers(ctx
, save
->DrawBuffer
, save
->ReadBuffer
);
1194 _mesa_reference_framebuffer(&save
->DrawBuffer
, NULL
);
1195 _mesa_reference_framebuffer(&save
->ReadBuffer
, NULL
);
1197 if (state
& MESA_META_DRAW_BUFFERS
) {
1198 _mesa_drawbuffers(ctx
, ctx
->DrawBuffer
, ctx
->Const
.MaxDrawBuffers
,
1199 save
->ColorDrawBuffers
, NULL
);
1202 ctx
->Meta
->SaveStackDepth
--;
1204 ctx
->API
= save
->API
;
1205 ctx
->Extensions
.Version
= save
->ExtensionsVersion
;
1210 * Convert Z from a normalized value in the range [0, 1] to an object-space
1211 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1212 * default/identity ortho projection results in the original Z value.
1213 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1214 * value comes from the clear value or raster position.
1216 static inline GLfloat
1217 invert_z(GLfloat normZ
)
1219 GLfloat objZ
= 1.0f
- 2.0f
* normZ
;
1225 * One-time init for a temp_texture object.
1226 * Choose tex target, compute max tex size, etc.
1229 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1231 /* prefer texture rectangle */
1232 if (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.NV_texture_rectangle
) {
1233 tex
->Target
= GL_TEXTURE_RECTANGLE
;
1234 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
1235 tex
->NPOT
= GL_TRUE
;
1238 /* use 2D texture, NPOT if possible */
1239 tex
->Target
= GL_TEXTURE_2D
;
1240 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1241 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
1243 tex
->MinSize
= 16; /* 16 x 16 at least */
1244 assert(tex
->MaxSize
> 0);
1246 _mesa_GenTextures(1, &tex
->TexObj
);
1250 cleanup_temp_texture(struct temp_texture
*tex
)
1254 _mesa_DeleteTextures(1, &tex
->TexObj
);
1260 * Return pointer to temp_texture info for non-bitmap ops.
1261 * This does some one-time init if needed.
1263 struct temp_texture
*
1264 _mesa_meta_get_temp_texture(struct gl_context
*ctx
)
1266 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
1269 init_temp_texture(ctx
, tex
);
1277 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1278 * We use a separate texture for bitmaps to reduce texture
1279 * allocation/deallocation.
1281 static struct temp_texture
*
1282 get_bitmap_temp_texture(struct gl_context
*ctx
)
1284 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
1287 init_temp_texture(ctx
, tex
);
1294 * Return pointer to depth temp_texture.
1295 * This does some one-time init if needed.
1297 struct temp_texture
*
1298 _mesa_meta_get_temp_depth_texture(struct gl_context
*ctx
)
1300 struct temp_texture
*tex
= &ctx
->Meta
->Blit
.depthTex
;
1303 init_temp_texture(ctx
, tex
);
1310 * Compute the width/height of texture needed to draw an image of the
1311 * given size. Return a flag indicating whether the current texture
1312 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1313 * allocated (glTexImage2D).
1314 * Also, compute s/t texcoords for drawing.
1316 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1319 _mesa_meta_alloc_texture(struct temp_texture
*tex
,
1320 GLsizei width
, GLsizei height
, GLenum intFormat
)
1322 GLboolean newTex
= GL_FALSE
;
1324 assert(width
<= tex
->MaxSize
);
1325 assert(height
<= tex
->MaxSize
);
1327 if (width
> tex
->Width
||
1328 height
> tex
->Height
||
1329 intFormat
!= tex
->IntFormat
) {
1330 /* alloc new texture (larger or different format) */
1333 /* use non-power of two size */
1334 tex
->Width
= MAX2(tex
->MinSize
, width
);
1335 tex
->Height
= MAX2(tex
->MinSize
, height
);
1338 /* find power of two size */
1340 w
= h
= tex
->MinSize
;
1349 tex
->IntFormat
= intFormat
;
1354 /* compute texcoords */
1355 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1356 tex
->Sright
= (GLfloat
) width
;
1357 tex
->Ttop
= (GLfloat
) height
;
1360 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1361 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1369 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1372 _mesa_meta_setup_copypix_texture(struct gl_context
*ctx
,
1373 struct temp_texture
*tex
,
1374 GLint srcX
, GLint srcY
,
1375 GLsizei width
, GLsizei height
,
1381 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1382 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1383 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1384 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1386 newTex
= _mesa_meta_alloc_texture(tex
, width
, height
, intFormat
);
1388 /* copy framebuffer image to texture */
1390 /* create new tex image */
1391 if (tex
->Width
== width
&& tex
->Height
== height
) {
1392 /* create new tex with framebuffer data */
1393 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1394 srcX
, srcY
, width
, height
, 0);
1397 /* create empty texture */
1398 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1399 tex
->Width
, tex
->Height
, 0,
1400 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1402 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1403 0, 0, srcX
, srcY
, width
, height
);
1407 /* replace existing tex image */
1408 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1409 0, 0, srcX
, srcY
, width
, height
);
1415 * Setup/load texture for glDrawPixels.
1418 _mesa_meta_setup_drawpix_texture(struct gl_context
*ctx
,
1419 struct temp_texture
*tex
,
1421 GLsizei width
, GLsizei height
,
1422 GLenum format
, GLenum type
,
1423 const GLvoid
*pixels
)
1425 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1426 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1427 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1428 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1430 /* copy pixel data to texture */
1432 /* create new tex image */
1433 if (tex
->Width
== width
&& tex
->Height
== height
) {
1434 /* create new tex and load image data */
1435 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1436 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1439 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1441 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1442 ctx
->Unpack
.BufferObj
);
1443 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1444 /* create empty texture */
1445 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1446 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1447 if (save_unpack_obj
!= NULL
)
1448 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
,
1449 save_unpack_obj
->Name
);
1451 _mesa_TexSubImage2D(tex
->Target
, 0,
1452 0, 0, width
, height
, format
, type
, pixels
);
1456 /* replace existing tex image */
1457 _mesa_TexSubImage2D(tex
->Target
, 0,
1458 0, 0, width
, height
, format
, type
, pixels
);
1463 _mesa_meta_setup_ff_tnl_for_blit(struct gl_context
*ctx
,
1464 GLuint
*VAO
, struct gl_buffer_object
**buf_obj
,
1465 unsigned texcoord_size
)
1467 _mesa_meta_setup_vertex_objects(ctx
, VAO
, buf_obj
, false, 2, texcoord_size
,
1470 /* setup projection matrix */
1471 _mesa_MatrixMode(GL_PROJECTION
);
1472 _mesa_LoadIdentity();
1476 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1479 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1481 meta_clear(ctx
, buffers
, false);
1485 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1487 meta_clear(ctx
, buffers
, true);
1491 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
1493 const char *vs_source
=
1494 "#extension GL_AMD_vertex_shader_layer : enable\n"
1495 "#extension GL_ARB_draw_instanced : enable\n"
1496 "#extension GL_ARB_explicit_attrib_location :enable\n"
1497 "layout(location = 0) in vec4 position;\n"
1500 "#ifdef GL_AMD_vertex_shader_layer\n"
1501 " gl_Layer = gl_InstanceID;\n"
1503 " gl_Position = position;\n"
1505 const char *fs_source
=
1506 "#extension GL_ARB_explicit_attrib_location :enable\n"
1507 "#extension GL_ARB_explicit_uniform_location :enable\n"
1508 "layout(location = 0) uniform vec4 color;\n"
1511 " gl_FragColor = color;\n"
1513 bool has_integer_textures
;
1515 _mesa_meta_setup_vertex_objects(ctx
, &clear
->VAO
, &clear
->buf_obj
, true,
1518 if (clear
->ShaderProg
!= 0)
1521 _mesa_meta_compile_and_link_program(ctx
, vs_source
, fs_source
, "meta clear",
1522 &clear
->ShaderProg
);
1524 has_integer_textures
= _mesa_is_gles3(ctx
) ||
1525 (_mesa_is_desktop_gl(ctx
) && ctx
->Const
.GLSLVersion
>= 130);
1527 if (has_integer_textures
) {
1528 void *shader_source_mem_ctx
= ralloc_context(NULL
);
1529 const char *vs_int_source
=
1530 ralloc_asprintf(shader_source_mem_ctx
,
1532 "#extension GL_AMD_vertex_shader_layer : enable\n"
1533 "#extension GL_ARB_draw_instanced : enable\n"
1534 "#extension GL_ARB_explicit_attrib_location :enable\n"
1535 "layout(location = 0) in vec4 position;\n"
1538 "#ifdef GL_AMD_vertex_shader_layer\n"
1539 " gl_Layer = gl_InstanceID;\n"
1541 " gl_Position = position;\n"
1543 const char *fs_int_source
=
1544 ralloc_asprintf(shader_source_mem_ctx
,
1546 "#extension GL_ARB_explicit_attrib_location :enable\n"
1547 "#extension GL_ARB_explicit_uniform_location :enable\n"
1548 "layout(location = 0) uniform ivec4 color;\n"
1549 "out ivec4 out_color;\n"
1553 " out_color = color;\n"
1556 _mesa_meta_compile_and_link_program(ctx
, vs_int_source
, fs_int_source
,
1558 &clear
->IntegerShaderProg
);
1559 ralloc_free(shader_source_mem_ctx
);
1561 /* Note that user-defined out attributes get automatically assigned
1562 * locations starting from 0, so we don't need to explicitly
1563 * BindFragDataLocation to 0.
1569 meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
)
1571 if (clear
->VAO
== 0)
1573 _mesa_DeleteVertexArrays(1, &clear
->VAO
);
1575 _mesa_reference_buffer_object(ctx
, &clear
->buf_obj
, NULL
);
1576 _mesa_reference_shader_program(ctx
, &clear
->ShaderProg
, NULL
);
1578 if (clear
->IntegerShaderProg
) {
1579 _mesa_reference_shader_program(ctx
, &clear
->IntegerShaderProg
, NULL
);
1584 * Given a bitfield of BUFFER_BIT_x draw buffers, call glDrawBuffers to
1585 * set GL to only draw to those buffers.
1587 * Since the bitfield has no associated order, the assignment of draw buffer
1588 * indices to color attachment indices is rather arbitrary.
1591 _mesa_meta_drawbuffers_from_bitfield(GLbitfield bits
)
1593 GLenum enums
[MAX_DRAW_BUFFERS
];
1597 /* This function is only legal for color buffer bitfields. */
1598 assert((bits
& ~BUFFER_BITS_COLOR
) == 0);
1600 /* Make sure we don't overflow any arrays. */
1601 assert(_mesa_bitcount(bits
) <= MAX_DRAW_BUFFERS
);
1605 if (bits
& BUFFER_BIT_FRONT_LEFT
)
1606 enums
[i
++] = GL_FRONT_LEFT
;
1608 if (bits
& BUFFER_BIT_FRONT_RIGHT
)
1609 enums
[i
++] = GL_FRONT_RIGHT
;
1611 if (bits
& BUFFER_BIT_BACK_LEFT
)
1612 enums
[i
++] = GL_BACK_LEFT
;
1614 if (bits
& BUFFER_BIT_BACK_RIGHT
)
1615 enums
[i
++] = GL_BACK_RIGHT
;
1617 for (n
= 0; n
< MAX_COLOR_ATTACHMENTS
; n
++) {
1618 if (bits
& (1 << (BUFFER_COLOR0
+ n
)))
1619 enums
[i
++] = GL_COLOR_ATTACHMENT0
+ n
;
1622 _mesa_DrawBuffers(i
, enums
);
1626 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1629 meta_clear(struct gl_context
*ctx
, GLbitfield buffers
, bool glsl
)
1631 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1632 GLbitfield metaSave
;
1633 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1634 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1635 float x0
, y0
, x1
, y1
, z
;
1636 struct vertex verts
[4];
1639 metaSave
= (MESA_META_ALPHA_TEST
|
1641 MESA_META_DEPTH_TEST
|
1642 MESA_META_RASTERIZATION
|
1644 MESA_META_STENCIL_TEST
|
1646 MESA_META_VIEWPORT
|
1648 MESA_META_CLAMP_FRAGMENT_COLOR
|
1649 MESA_META_MULTISAMPLE
|
1650 MESA_META_OCCLUSION_QUERY
);
1653 metaSave
|= MESA_META_FOG
|
1654 MESA_META_PIXEL_TRANSFER
|
1655 MESA_META_TRANSFORM
|
1657 MESA_META_CLAMP_VERTEX_COLOR
|
1658 MESA_META_SELECT_FEEDBACK
;
1661 if (buffers
& BUFFER_BITS_COLOR
) {
1662 metaSave
|= MESA_META_DRAW_BUFFERS
;
1664 /* We'll use colormask to disable color writes. Otherwise,
1665 * respect color mask
1667 metaSave
|= MESA_META_COLOR_MASK
;
1670 _mesa_meta_begin(ctx
, metaSave
);
1673 meta_glsl_clear_init(ctx
, clear
);
1675 x0
= ((float) fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
1676 y0
= ((float) fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
1677 x1
= ((float) fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
1678 y1
= ((float) fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
1679 z
= -invert_z(ctx
->Depth
.Clear
);
1681 _mesa_meta_setup_vertex_objects(ctx
, &clear
->VAO
, &clear
->buf_obj
, false,
1684 x0
= (float) fb
->_Xmin
;
1685 y0
= (float) fb
->_Ymin
;
1686 x1
= (float) fb
->_Xmax
;
1687 y1
= (float) fb
->_Ymax
;
1688 z
= invert_z(ctx
->Depth
.Clear
);
1691 if (fb
->_IntegerColor
) {
1693 _mesa_meta_use_program(ctx
, clear
->IntegerShaderProg
);
1694 _mesa_Uniform4iv(0, 1, ctx
->Color
.ClearColor
.i
);
1696 _mesa_meta_use_program(ctx
, clear
->ShaderProg
);
1697 _mesa_Uniform4fv(0, 1, ctx
->Color
.ClearColor
.f
);
1700 /* GL_COLOR_BUFFER_BIT */
1701 if (buffers
& BUFFER_BITS_COLOR
) {
1702 /* Only draw to the buffers we were asked to clear. */
1703 _mesa_meta_drawbuffers_from_bitfield(buffers
& BUFFER_BITS_COLOR
);
1705 /* leave colormask state as-is */
1707 /* Clears never have the color clamped. */
1708 if (ctx
->Extensions
.ARB_color_buffer_float
)
1709 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1712 assert(metaSave
& MESA_META_COLOR_MASK
);
1713 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1716 /* GL_DEPTH_BUFFER_BIT */
1717 if (buffers
& BUFFER_BIT_DEPTH
) {
1718 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1719 _mesa_DepthFunc(GL_ALWAYS
);
1720 _mesa_DepthMask(GL_TRUE
);
1723 assert(!ctx
->Depth
.Test
);
1726 /* GL_STENCIL_BUFFER_BIT */
1727 if (buffers
& BUFFER_BIT_STENCIL
) {
1728 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1729 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1730 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1731 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1732 ctx
->Stencil
.Clear
& stencilMax
,
1733 ctx
->Stencil
.WriteMask
[0]);
1736 assert(!ctx
->Stencil
.Enabled
);
1739 /* vertex positions */
1754 for (i
= 0; i
< 4; i
++) {
1755 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
1756 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
1757 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
1758 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
1762 /* upload new vertex data */
1763 _mesa_buffer_data(ctx
, clear
->buf_obj
, GL_NONE
, sizeof(verts
), verts
,
1764 GL_DYNAMIC_DRAW
, __func__
);
1767 if (fb
->MaxNumLayers
> 0) {
1768 _mesa_DrawArraysInstanced(GL_TRIANGLE_FAN
, 0, 4, fb
->MaxNumLayers
);
1770 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1773 _mesa_meta_end(ctx
);
1777 * Meta implementation of ctx->Driver.CopyPixels() in terms
1778 * of texture mapping and polygon rendering and GLSL shaders.
1781 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
1782 GLsizei width
, GLsizei height
,
1783 GLint dstX
, GLint dstY
, GLenum type
)
1785 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1786 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
1787 struct vertex verts
[4];
1789 if (type
!= GL_COLOR
||
1790 ctx
->_ImageTransferState
||
1792 width
> tex
->MaxSize
||
1793 height
> tex
->MaxSize
) {
1794 /* XXX avoid this fallback */
1795 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1799 /* Most GL state applies to glCopyPixels, but a there's a few things
1800 * we need to override:
1802 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
1805 MESA_META_TRANSFORM
|
1808 MESA_META_VIEWPORT
));
1810 _mesa_meta_setup_vertex_objects(ctx
, ©pix
->VAO
, ©pix
->buf_obj
, false,
1813 /* Silence valgrind warnings about reading uninitialized stack. */
1814 memset(verts
, 0, sizeof(verts
));
1816 /* Alloc/setup texture */
1817 _mesa_meta_setup_copypix_texture(ctx
, tex
, srcX
, srcY
, width
, height
,
1818 GL_RGBA
, GL_NEAREST
);
1820 /* vertex positions, texcoords (after texture allocation!) */
1822 const GLfloat dstX0
= (GLfloat
) dstX
;
1823 const GLfloat dstY0
= (GLfloat
) dstY
;
1824 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1825 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1826 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1831 verts
[0].tex
[0] = 0.0F
;
1832 verts
[0].tex
[1] = 0.0F
;
1836 verts
[1].tex
[0] = tex
->Sright
;
1837 verts
[1].tex
[1] = 0.0F
;
1841 verts
[2].tex
[0] = tex
->Sright
;
1842 verts
[2].tex
[1] = tex
->Ttop
;
1846 verts
[3].tex
[0] = 0.0F
;
1847 verts
[3].tex
[1] = tex
->Ttop
;
1849 /* upload new vertex data */
1850 _mesa_buffer_sub_data(ctx
, copypix
->buf_obj
, 0, sizeof(verts
), verts
,
1854 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1856 /* draw textured quad */
1857 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1859 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1861 _mesa_meta_end(ctx
);
1865 meta_drawpix_cleanup(struct gl_context
*ctx
, struct drawpix_state
*drawpix
)
1867 if (drawpix
->VAO
!= 0) {
1868 _mesa_DeleteVertexArrays(1, &drawpix
->VAO
);
1871 _mesa_reference_buffer_object(ctx
, &drawpix
->buf_obj
, NULL
);
1874 if (drawpix
->StencilFP
!= 0) {
1875 _mesa_DeleteProgramsARB(1, &drawpix
->StencilFP
);
1876 drawpix
->StencilFP
= 0;
1879 if (drawpix
->DepthFP
!= 0) {
1880 _mesa_DeleteProgramsARB(1, &drawpix
->DepthFP
);
1881 drawpix
->DepthFP
= 0;
1886 * When the glDrawPixels() image size is greater than the max rectangle
1887 * texture size we use this function to break the glDrawPixels() image
1888 * into tiles which fit into the max texture size.
1891 tiled_draw_pixels(struct gl_context
*ctx
,
1893 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1894 GLenum format
, GLenum type
,
1895 const struct gl_pixelstore_attrib
*unpack
,
1896 const GLvoid
*pixels
)
1898 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
1901 if (tileUnpack
.RowLength
== 0)
1902 tileUnpack
.RowLength
= width
;
1904 for (i
= 0; i
< width
; i
+= tileSize
) {
1905 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
1906 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
1908 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
1910 for (j
= 0; j
< height
; j
+= tileSize
) {
1911 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
1912 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
1914 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
1916 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
1917 format
, type
, &tileUnpack
, pixels
);
1924 * One-time init for drawing stencil pixels.
1927 init_draw_stencil_pixels(struct gl_context
*ctx
)
1929 /* This program is run eight times, once for each stencil bit.
1930 * The stencil values to draw are found in an 8-bit alpha texture.
1931 * We read the texture/stencil value and test if bit 'b' is set.
1932 * If the bit is not set, use KIL to kill the fragment.
1933 * Finally, we use the stencil test to update the stencil buffer.
1935 * The basic algorithm for checking if a bit is set is:
1936 * if (is_odd(value / (1 << bit)))
1937 * result is one (or non-zero).
1940 * The program parameter contains three values:
1941 * parm.x = 255 / (1 << bit)
1945 static const char *program
=
1947 "PARAM parm = program.local[0]; \n"
1949 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1950 "# t = t * 255 / bit \n"
1951 "MUL t.x, t.a, parm.x; \n"
1954 "SUB t.x, t.x, t.y; \n"
1956 "MUL t.x, t.x, parm.y; \n"
1957 "# t = fract(t.x) \n"
1958 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
1959 "# t.x = (t.x == 0 ? 1 : 0) \n"
1960 "SGE t.x, -t.x, parm.z; \n"
1962 "# for debug only \n"
1963 "#MOV result.color, t.x; \n"
1965 char program2
[1000];
1966 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1967 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
1968 const char *texTarget
;
1970 assert(drawpix
->StencilFP
== 0);
1972 /* replace %s with "RECT" or "2D" */
1973 assert(strlen(program
) + 4 < sizeof(program2
));
1974 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1978 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1980 _mesa_GenProgramsARB(1, &drawpix
->StencilFP
);
1981 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1982 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1983 strlen(program2
), (const GLubyte
*) program2
);
1988 * One-time init for drawing depth pixels.
1991 init_draw_depth_pixels(struct gl_context
*ctx
)
1993 static const char *program
=
1995 "PARAM color = program.local[0]; \n"
1996 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1997 "MOV result.color, color; \n"
2000 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2001 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
2002 const char *texTarget
;
2004 assert(drawpix
->DepthFP
== 0);
2006 /* replace %s with "RECT" or "2D" */
2007 assert(strlen(program
) + 4 < sizeof(program2
));
2008 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2012 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2014 _mesa_GenProgramsARB(1, &drawpix
->DepthFP
);
2015 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2016 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2017 strlen(program2
), (const GLubyte
*) program2
);
2022 * Meta implementation of ctx->Driver.DrawPixels() in terms
2023 * of texture mapping and polygon rendering.
2026 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2027 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2028 GLenum format
, GLenum type
,
2029 const struct gl_pixelstore_attrib
*unpack
,
2030 const GLvoid
*pixels
)
2032 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2033 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
2034 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2035 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2036 struct vertex verts
[4];
2037 GLenum texIntFormat
;
2038 GLboolean fallback
, newTex
;
2039 GLbitfield metaExtraSave
= 0x0;
2042 * Determine if we can do the glDrawPixels with texture mapping.
2044 fallback
= GL_FALSE
;
2045 if (ctx
->Fog
.Enabled
) {
2049 if (_mesa_is_color_format(format
)) {
2050 /* use more compact format when possible */
2051 /* XXX disable special case for GL_LUMINANCE for now to work around
2052 * apparent i965 driver bug (see bug #23670).
2054 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2055 texIntFormat
= format
;
2057 texIntFormat
= GL_RGBA
;
2059 /* If we're not supposed to clamp the resulting color, then just
2060 * promote our texture to fully float. We could do better by
2061 * just going for the matching set of channels, in floating
2064 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2065 ctx
->Extensions
.ARB_texture_float
)
2066 texIntFormat
= GL_RGBA32F
;
2068 else if (_mesa_is_stencil_format(format
)) {
2069 if (ctx
->Extensions
.ARB_fragment_program
&&
2070 ctx
->Pixel
.IndexShift
== 0 &&
2071 ctx
->Pixel
.IndexOffset
== 0 &&
2072 type
== GL_UNSIGNED_BYTE
) {
2073 /* We'll store stencil as alpha. This only works for GLubyte
2074 * image data because of how incoming values are mapped to alpha
2077 texIntFormat
= GL_ALPHA
;
2078 metaExtraSave
= (MESA_META_COLOR_MASK
|
2079 MESA_META_DEPTH_TEST
|
2080 MESA_META_PIXEL_TRANSFER
|
2082 MESA_META_STENCIL_TEST
);
2088 else if (_mesa_is_depth_format(format
)) {
2089 if (ctx
->Extensions
.ARB_depth_texture
&&
2090 ctx
->Extensions
.ARB_fragment_program
) {
2091 texIntFormat
= GL_DEPTH_COMPONENT
;
2092 metaExtraSave
= (MESA_META_SHADER
);
2103 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2104 format
, type
, unpack
, pixels
);
2109 * Check image size against max texture size, draw as tiles if needed.
2111 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2112 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2113 format
, type
, unpack
, pixels
);
2117 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2118 * but a there's a few things we need to override:
2120 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2123 MESA_META_TRANSFORM
|
2126 MESA_META_VIEWPORT
|
2129 newTex
= _mesa_meta_alloc_texture(tex
, width
, height
, texIntFormat
);
2131 _mesa_meta_setup_vertex_objects(ctx
, &drawpix
->VAO
, &drawpix
->buf_obj
, false,
2134 /* Silence valgrind warnings about reading uninitialized stack. */
2135 memset(verts
, 0, sizeof(verts
));
2137 /* vertex positions, texcoords (after texture allocation!) */
2139 const GLfloat x0
= (GLfloat
) x
;
2140 const GLfloat y0
= (GLfloat
) y
;
2141 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2142 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2143 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2148 verts
[0].tex
[0] = 0.0F
;
2149 verts
[0].tex
[1] = 0.0F
;
2153 verts
[1].tex
[0] = tex
->Sright
;
2154 verts
[1].tex
[1] = 0.0F
;
2158 verts
[2].tex
[0] = tex
->Sright
;
2159 verts
[2].tex
[1] = tex
->Ttop
;
2163 verts
[3].tex
[0] = 0.0F
;
2164 verts
[3].tex
[1] = tex
->Ttop
;
2167 /* upload new vertex data */
2168 _mesa_buffer_data(ctx
, drawpix
->buf_obj
, GL_NONE
, sizeof(verts
), verts
,
2169 GL_DYNAMIC_DRAW
, __func__
);
2171 /* set given unpack params */
2172 ctx
->Unpack
= *unpack
;
2174 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2176 if (_mesa_is_stencil_format(format
)) {
2177 /* Drawing stencil */
2180 if (!drawpix
->StencilFP
)
2181 init_draw_stencil_pixels(ctx
);
2183 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2184 GL_ALPHA
, type
, pixels
);
2186 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2188 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2190 /* set all stencil bits to 0 */
2191 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2192 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2193 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2195 /* set stencil bits to 1 where needed */
2196 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2198 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2199 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2201 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2202 const GLuint mask
= 1 << bit
;
2203 if (mask
& origStencilMask
) {
2204 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2205 _mesa_StencilMask(mask
);
2207 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2208 255.0f
/ mask
, 0.5f
, 0.0f
, 0.0f
);
2210 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2214 else if (_mesa_is_depth_format(format
)) {
2216 if (!drawpix
->DepthFP
)
2217 init_draw_depth_pixels(ctx
);
2219 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2220 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2222 /* polygon color = current raster color */
2223 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2224 ctx
->Current
.RasterColor
);
2226 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2227 format
, type
, pixels
);
2229 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2233 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2234 format
, type
, pixels
);
2235 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2238 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2240 /* restore unpack params */
2241 ctx
->Unpack
= unpackSave
;
2243 _mesa_meta_end(ctx
);
2247 alpha_test_raster_color(struct gl_context
*ctx
)
2249 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2250 GLfloat ref
= ctx
->Color
.AlphaRef
;
2252 switch (ctx
->Color
.AlphaFunc
) {
2258 return alpha
== ref
;
2260 return alpha
<= ref
;
2264 return alpha
!= ref
;
2266 return alpha
>= ref
;
2276 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2277 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2278 * tracker would improve performance a lot.
2281 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2282 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2283 const struct gl_pixelstore_attrib
*unpack
,
2284 const GLubyte
*bitmap1
)
2286 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2287 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2288 const GLenum texIntFormat
= GL_ALPHA
;
2289 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2291 struct vertex verts
[4];
2296 * Check if swrast fallback is needed.
2298 if (ctx
->_ImageTransferState
||
2299 ctx
->FragmentProgram
._Enabled
||
2301 ctx
->Texture
._MaxEnabledTexImageUnit
!= -1 ||
2302 width
> tex
->MaxSize
||
2303 height
> tex
->MaxSize
) {
2304 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2308 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2311 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2312 * but a there's a few things we need to override:
2314 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
2315 MESA_META_PIXEL_STORE
|
2316 MESA_META_RASTERIZATION
|
2319 MESA_META_TRANSFORM
|
2322 MESA_META_VIEWPORT
));
2324 _mesa_meta_setup_vertex_objects(ctx
, &bitmap
->VAO
, &bitmap
->buf_obj
, false,
2327 newTex
= _mesa_meta_alloc_texture(tex
, width
, height
, texIntFormat
);
2329 /* Silence valgrind warnings about reading uninitialized stack. */
2330 memset(verts
, 0, sizeof(verts
));
2332 /* vertex positions, texcoords, colors (after texture allocation!) */
2334 const GLfloat x0
= (GLfloat
) x
;
2335 const GLfloat y0
= (GLfloat
) y
;
2336 const GLfloat x1
= (GLfloat
) (x
+ width
);
2337 const GLfloat y1
= (GLfloat
) (y
+ height
);
2338 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2344 verts
[0].tex
[0] = 0.0F
;
2345 verts
[0].tex
[1] = 0.0F
;
2349 verts
[1].tex
[0] = tex
->Sright
;
2350 verts
[1].tex
[1] = 0.0F
;
2354 verts
[2].tex
[0] = tex
->Sright
;
2355 verts
[2].tex
[1] = tex
->Ttop
;
2359 verts
[3].tex
[0] = 0.0F
;
2360 verts
[3].tex
[1] = tex
->Ttop
;
2362 for (i
= 0; i
< 4; i
++) {
2363 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2364 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2365 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2366 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2369 /* upload new vertex data */
2370 _mesa_buffer_sub_data(ctx
, bitmap
->buf_obj
, 0, sizeof(verts
), verts
,
2374 /* choose different foreground/background alpha values */
2375 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2376 bg
= (fg
> 127 ? 0 : 255);
2378 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2380 _mesa_meta_end(ctx
);
2384 bitmap8
= malloc(width
* height
);
2386 memset(bitmap8
, bg
, width
* height
);
2387 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2388 bitmap8
, width
, fg
);
2390 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2392 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2393 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2395 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2396 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2398 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2400 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2405 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2407 _mesa_meta_end(ctx
);
2411 * Compute the texture coordinates for the four vertices of a quad for
2412 * drawing a 2D texture image or slice of a cube/3D texture. The offset
2413 * and width, height specify a sub-region of the 2D image.
2415 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2416 * \param slice slice of a 1D/2D array texture or 3D texture
2417 * \param xoffset X position of sub texture
2418 * \param yoffset Y position of sub texture
2419 * \param width width of the sub texture image
2420 * \param height height of the sub texture image
2421 * \param total_width total width of the texture image
2422 * \param total_height total height of the texture image
2423 * \param total_depth total depth of the texture image
2424 * \param coords0/1/2/3 returns the computed texcoords
2427 _mesa_meta_setup_texture_coords(GLenum faceTarget
,
2443 const float s0
= (float) xoffset
/ (float) total_width
;
2444 const float s1
= (float) (xoffset
+ width
) / (float) total_width
;
2445 const float t0
= (float) yoffset
/ (float) total_height
;
2446 const float t1
= (float) (yoffset
+ height
) / (float) total_height
;
2449 /* setup the reference texcoords */
2459 if (faceTarget
== GL_TEXTURE_CUBE_MAP_ARRAY
)
2460 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ slice
% 6;
2462 /* Currently all texture targets want the W component to be 1.0.
2469 switch (faceTarget
) {
2473 case GL_TEXTURE_2D_ARRAY
:
2474 if (faceTarget
== GL_TEXTURE_3D
) {
2475 assert(slice
< total_depth
);
2476 assert(total_depth
>= 1);
2477 r
= (slice
+ 0.5f
) / total_depth
;
2479 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
2483 coords0
[0] = st
[0][0]; /* s */
2484 coords0
[1] = st
[0][1]; /* t */
2485 coords0
[2] = r
; /* r */
2486 coords1
[0] = st
[1][0];
2487 coords1
[1] = st
[1][1];
2489 coords2
[0] = st
[2][0];
2490 coords2
[1] = st
[2][1];
2492 coords3
[0] = st
[3][0];
2493 coords3
[1] = st
[3][1];
2496 case GL_TEXTURE_RECTANGLE_ARB
:
2497 coords0
[0] = (float) xoffset
; /* s */
2498 coords0
[1] = (float) yoffset
; /* t */
2499 coords0
[2] = 0.0F
; /* r */
2500 coords1
[0] = (float) (xoffset
+ width
);
2501 coords1
[1] = (float) yoffset
;
2503 coords2
[0] = (float) (xoffset
+ width
);
2504 coords2
[1] = (float) (yoffset
+ height
);
2506 coords3
[0] = (float) xoffset
;
2507 coords3
[1] = (float) (yoffset
+ height
);
2510 case GL_TEXTURE_1D_ARRAY
:
2511 coords0
[0] = st
[0][0]; /* s */
2512 coords0
[1] = (float) slice
; /* t */
2513 coords0
[2] = 0.0F
; /* r */
2514 coords1
[0] = st
[1][0];
2515 coords1
[1] = (float) slice
;
2517 coords2
[0] = st
[2][0];
2518 coords2
[1] = (float) slice
;
2520 coords3
[0] = st
[3][0];
2521 coords3
[1] = (float) slice
;
2525 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2526 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2527 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2528 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2529 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2530 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2531 /* loop over quad verts */
2532 for (i
= 0; i
< 4; i
++) {
2533 /* Compute sc = +/-scale and tc = +/-scale.
2534 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2535 * though that can still sometimes happen with this scale factor...
2537 const GLfloat scale
= 0.9999f
;
2538 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2539 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2556 unreachable("not reached");
2559 coord
[3] = (float) (slice
/ 6);
2561 switch (faceTarget
) {
2562 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2567 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2572 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2577 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2582 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2587 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2598 assert(!"unexpected target in _mesa_meta_setup_texture_coords()");
2602 static struct blit_shader
*
2603 choose_blit_shader(GLenum target
, struct blit_shader_table
*table
)
2607 table
->sampler_1d
.type
= "sampler1D";
2608 table
->sampler_1d
.func
= "texture1D";
2609 table
->sampler_1d
.texcoords
= "texCoords.x";
2610 return &table
->sampler_1d
;
2612 table
->sampler_2d
.type
= "sampler2D";
2613 table
->sampler_2d
.func
= "texture2D";
2614 table
->sampler_2d
.texcoords
= "texCoords.xy";
2615 return &table
->sampler_2d
;
2616 case GL_TEXTURE_RECTANGLE
:
2617 table
->sampler_rect
.type
= "sampler2DRect";
2618 table
->sampler_rect
.func
= "texture2DRect";
2619 table
->sampler_rect
.texcoords
= "texCoords.xy";
2620 return &table
->sampler_rect
;
2622 /* Code for mipmap generation with 3D textures is not used yet.
2623 * It's a sw fallback.
2625 table
->sampler_3d
.type
= "sampler3D";
2626 table
->sampler_3d
.func
= "texture3D";
2627 table
->sampler_3d
.texcoords
= "texCoords.xyz";
2628 return &table
->sampler_3d
;
2629 case GL_TEXTURE_CUBE_MAP
:
2630 table
->sampler_cubemap
.type
= "samplerCube";
2631 table
->sampler_cubemap
.func
= "textureCube";
2632 table
->sampler_cubemap
.texcoords
= "texCoords.xyz";
2633 return &table
->sampler_cubemap
;
2634 case GL_TEXTURE_1D_ARRAY
:
2635 table
->sampler_1d_array
.type
= "sampler1DArray";
2636 table
->sampler_1d_array
.func
= "texture1DArray";
2637 table
->sampler_1d_array
.texcoords
= "texCoords.xy";
2638 return &table
->sampler_1d_array
;
2639 case GL_TEXTURE_2D_ARRAY
:
2640 table
->sampler_2d_array
.type
= "sampler2DArray";
2641 table
->sampler_2d_array
.func
= "texture2DArray";
2642 table
->sampler_2d_array
.texcoords
= "texCoords.xyz";
2643 return &table
->sampler_2d_array
;
2644 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2645 table
->sampler_cubemap_array
.type
= "samplerCubeArray";
2646 table
->sampler_cubemap_array
.func
= "textureCubeArray";
2647 table
->sampler_cubemap_array
.texcoords
= "texCoords.xyzw";
2648 return &table
->sampler_cubemap_array
;
2650 _mesa_problem(NULL
, "Unexpected texture target 0x%x in"
2651 " setup_texture_sampler()\n", target
);
2657 _mesa_meta_blit_shader_table_cleanup(struct gl_context
*ctx
,
2658 struct blit_shader_table
*table
)
2660 _mesa_reference_shader_program(ctx
, &table
->sampler_1d
.shader_prog
, NULL
);
2661 _mesa_reference_shader_program(ctx
, &table
->sampler_2d
.shader_prog
, NULL
);
2662 _mesa_reference_shader_program(ctx
, &table
->sampler_3d
.shader_prog
, NULL
);
2663 _mesa_reference_shader_program(ctx
, &table
->sampler_rect
.shader_prog
, NULL
);
2664 _mesa_reference_shader_program(ctx
, &table
->sampler_cubemap
.shader_prog
, NULL
);
2665 _mesa_reference_shader_program(ctx
, &table
->sampler_1d_array
.shader_prog
, NULL
);
2666 _mesa_reference_shader_program(ctx
, &table
->sampler_2d_array
.shader_prog
, NULL
);
2667 _mesa_reference_shader_program(ctx
, &table
->sampler_cubemap_array
.shader_prog
, NULL
);
2671 * Determine the GL data type to use for the temporary image read with
2672 * ReadPixels() and passed to Tex[Sub]Image().
2675 get_temp_image_type(struct gl_context
*ctx
, mesa_format format
)
2677 const GLenum baseFormat
= _mesa_get_format_base_format(format
);
2678 const GLenum datatype
= _mesa_get_format_datatype(format
);
2679 const GLint format_red_bits
= _mesa_get_format_bits(format
, GL_RED_BITS
);
2681 switch (baseFormat
) {
2688 case GL_LUMINANCE_ALPHA
:
2690 if (datatype
== GL_INT
|| datatype
== GL_UNSIGNED_INT
) {
2692 } else if (format_red_bits
<= 8) {
2693 return GL_UNSIGNED_BYTE
;
2694 } else if (format_red_bits
<= 16) {
2695 return GL_UNSIGNED_SHORT
;
2698 case GL_DEPTH_COMPONENT
:
2699 if (datatype
== GL_FLOAT
)
2702 return GL_UNSIGNED_INT
;
2703 case GL_DEPTH_STENCIL
:
2704 if (datatype
== GL_FLOAT
)
2705 return GL_FLOAT_32_UNSIGNED_INT_24_8_REV
;
2707 return GL_UNSIGNED_INT_24_8
;
2709 _mesa_problem(ctx
, "Unexpected format %d in get_temp_image_type()",
2716 * Attempts to wrap the destination texture in an FBO and use
2717 * glBlitFramebuffer() to implement glCopyTexSubImage().
2720 copytexsubimage_using_blit_framebuffer(struct gl_context
*ctx
, GLuint dims
,
2721 struct gl_texture_image
*texImage
,
2725 struct gl_renderbuffer
*rb
,
2727 GLsizei width
, GLsizei height
)
2729 struct gl_framebuffer
*drawFb
;
2730 bool success
= false;
2734 if (!ctx
->Extensions
.ARB_framebuffer_object
)
2737 drawFb
= ctx
->Driver
.NewFramebuffer(ctx
, 0xDEADBEEF);
2741 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_DRAW_BUFFERS
);
2742 _mesa_bind_framebuffers(ctx
, drawFb
, ctx
->ReadBuffer
);
2744 if (rb
->_BaseFormat
== GL_DEPTH_STENCIL
||
2745 rb
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2746 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
2747 GL_DEPTH_ATTACHMENT
,
2749 mask
= GL_DEPTH_BUFFER_BIT
;
2751 if (rb
->_BaseFormat
== GL_DEPTH_STENCIL
&&
2752 texImage
->_BaseFormat
== GL_DEPTH_STENCIL
) {
2753 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
2754 GL_STENCIL_ATTACHMENT
,
2756 mask
|= GL_STENCIL_BUFFER_BIT
;
2758 _mesa_DrawBuffer(GL_NONE
);
2760 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
2761 GL_COLOR_ATTACHMENT0
,
2763 mask
= GL_COLOR_BUFFER_BIT
;
2764 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0
);
2767 status
= _mesa_check_framebuffer_status(ctx
, ctx
->DrawBuffer
);
2768 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
2771 ctx
->Meta
->Blit
.no_ctsi_fallback
= true;
2773 /* Since we've bound a new draw framebuffer, we need to update
2774 * its derived state -- _Xmin, etc -- for BlitFramebuffer's clipping to
2777 _mesa_update_state(ctx
);
2779 /* We skip the core BlitFramebuffer checks for format consistency, which
2780 * are too strict for CopyTexImage. We know meta will be fine with format
2783 mask
= _mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
2785 x
+ width
, y
+ height
,
2787 xoffset
+ width
, yoffset
+ height
,
2789 ctx
->Meta
->Blit
.no_ctsi_fallback
= false;
2790 success
= mask
== 0x0;
2793 _mesa_reference_framebuffer(&drawFb
, NULL
);
2794 _mesa_meta_end(ctx
);
2799 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2800 * Have to be careful with locking and meta state for pixel transfer.
2803 _mesa_meta_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2804 struct gl_texture_image
*texImage
,
2805 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2806 struct gl_renderbuffer
*rb
,
2808 GLsizei width
, GLsizei height
)
2810 GLenum format
, type
;
2814 if (copytexsubimage_using_blit_framebuffer(ctx
, dims
,
2816 xoffset
, yoffset
, zoffset
,
2823 /* Choose format/type for temporary image buffer */
2824 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
2825 if (format
== GL_LUMINANCE
||
2826 format
== GL_LUMINANCE_ALPHA
||
2827 format
== GL_INTENSITY
) {
2828 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
2829 * temp image buffer because glReadPixels will do L=R+G+B which is
2830 * not what we want (should be L=R).
2835 type
= get_temp_image_type(ctx
, texImage
->TexFormat
);
2836 if (_mesa_is_format_integer_color(texImage
->TexFormat
)) {
2837 format
= _mesa_base_format_to_integer_format(format
);
2839 bpp
= _mesa_bytes_per_pixel(format
, type
);
2841 _mesa_problem(ctx
, "Bad bpp in _mesa_meta_CopyTexSubImage()");
2846 * Alloc image buffer (XXX could use a PBO)
2848 buf
= malloc(width
* height
* bpp
);
2850 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
2855 * Read image from framebuffer (disable pixel transfer ops)
2857 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
2858 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2859 format
, type
, &ctx
->Pack
, buf
);
2860 _mesa_meta_end(ctx
);
2862 _mesa_update_state(ctx
); /* to update pixel transfer state */
2865 * Store texture data (with pixel transfer ops)
2867 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
2869 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
2870 assert(yoffset
== 0);
2871 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
2872 xoffset
, zoffset
, 0, width
, 1, 1,
2873 format
, type
, buf
, &ctx
->Unpack
);
2875 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
2876 xoffset
, yoffset
, zoffset
, width
, height
, 1,
2877 format
, type
, buf
, &ctx
->Unpack
);
2880 _mesa_meta_end(ctx
);
2886 meta_decompress_fbo_cleanup(struct decompress_fbo_state
*decompress_fbo
)
2888 if (decompress_fbo
->fb
!= NULL
) {
2889 _mesa_reference_framebuffer(&decompress_fbo
->fb
, NULL
);
2890 _mesa_reference_renderbuffer(&decompress_fbo
->rb
, NULL
);
2893 memset(decompress_fbo
, 0, sizeof(*decompress_fbo
));
2897 meta_decompress_cleanup(struct gl_context
*ctx
,
2898 struct decompress_state
*decompress
)
2900 meta_decompress_fbo_cleanup(&decompress
->byteFBO
);
2901 meta_decompress_fbo_cleanup(&decompress
->floatFBO
);
2903 if (decompress
->VAO
!= 0) {
2904 _mesa_DeleteVertexArrays(1, &decompress
->VAO
);
2905 _mesa_reference_buffer_object(ctx
, &decompress
->buf_obj
, NULL
);
2908 _mesa_reference_sampler_object(ctx
, &decompress
->samp_obj
, NULL
);
2910 memset(decompress
, 0, sizeof(*decompress
));
2914 * Decompress a texture image by drawing a quad with the compressed
2915 * texture and reading the pixels out of the color buffer.
2916 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
2917 * \param destFormat format, ala glReadPixels
2918 * \param destType type, ala glReadPixels
2919 * \param dest destination buffer
2920 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
2923 decompress_texture_image(struct gl_context
*ctx
,
2924 struct gl_texture_image
*texImage
,
2926 GLint xoffset
, GLint yoffset
,
2927 GLsizei width
, GLsizei height
,
2928 GLenum destFormat
, GLenum destType
,
2931 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
2932 struct decompress_fbo_state
*decompress_fbo
;
2933 struct gl_texture_object
*texObj
= texImage
->TexObject
;
2934 const GLenum target
= texObj
->Target
;
2937 struct vertex verts
[4];
2938 struct gl_sampler_object
*samp_obj_save
= NULL
;
2940 const bool use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
2941 ctx
->Extensions
.ARB_fragment_shader
;
2943 switch (_mesa_get_format_datatype(texImage
->TexFormat
)) {
2945 decompress_fbo
= &decompress
->floatFBO
;
2946 rbFormat
= GL_RGBA32F
;
2948 case GL_UNSIGNED_NORMALIZED
:
2949 decompress_fbo
= &decompress
->byteFBO
;
2957 assert(target
== GL_TEXTURE_3D
||
2958 target
== GL_TEXTURE_2D_ARRAY
||
2959 target
== GL_TEXTURE_CUBE_MAP_ARRAY
);
2964 case GL_TEXTURE_1D_ARRAY
:
2965 assert(!"No compressed 1D textures.");
2969 assert(!"No compressed 3D textures.");
2972 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2973 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ (slice
% 6);
2976 case GL_TEXTURE_CUBE_MAP
:
2977 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
2981 faceTarget
= target
;
2985 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~(MESA_META_PIXEL_STORE
|
2986 MESA_META_DRAW_BUFFERS
));
2988 _mesa_reference_sampler_object(ctx
, &samp_obj_save
,
2989 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
);
2991 /* Create/bind FBO/renderbuffer */
2992 if (decompress_fbo
->fb
== NULL
) {
2993 decompress_fbo
->rb
= ctx
->Driver
.NewRenderbuffer(ctx
, 0xDEADBEEF);
2994 if (decompress_fbo
->rb
== NULL
) {
2995 _mesa_meta_end(ctx
);
2999 decompress_fbo
->rb
->RefCount
= 1;
3001 decompress_fbo
->fb
= ctx
->Driver
.NewFramebuffer(ctx
, 0xDEADBEEF);
3002 if (decompress_fbo
->fb
== NULL
) {
3003 _mesa_meta_end(ctx
);
3007 _mesa_bind_framebuffers(ctx
, decompress_fbo
->fb
, decompress_fbo
->fb
);
3008 _mesa_framebuffer_renderbuffer(ctx
, ctx
->DrawBuffer
, GL_COLOR_ATTACHMENT0
,
3009 decompress_fbo
->rb
);
3012 _mesa_bind_framebuffers(ctx
, decompress_fbo
->fb
, decompress_fbo
->fb
);
3015 /* alloc dest surface */
3016 if (width
> decompress_fbo
->Width
|| height
> decompress_fbo
->Height
) {
3017 _mesa_renderbuffer_storage(ctx
, decompress_fbo
->rb
, rbFormat
,
3019 status
= _mesa_check_framebuffer_status(ctx
, ctx
->DrawBuffer
);
3020 if (status
!= GL_FRAMEBUFFER_COMPLETE
) {
3021 /* If the framebuffer isn't complete then we'll leave
3022 * decompress_fbo->Width as zero so that it will fail again next time
3024 _mesa_meta_end(ctx
);
3027 decompress_fbo
->Width
= width
;
3028 decompress_fbo
->Height
= height
;
3031 if (use_glsl_version
) {
3032 _mesa_meta_setup_vertex_objects(ctx
, &decompress
->VAO
,
3033 &decompress
->buf_obj
, true,
3036 _mesa_meta_setup_blit_shader(ctx
, target
, false, &decompress
->shaders
);
3038 _mesa_meta_setup_ff_tnl_for_blit(ctx
, &decompress
->VAO
,
3039 &decompress
->buf_obj
, 3);
3042 if (decompress
->samp_obj
== NULL
) {
3043 decompress
->samp_obj
= ctx
->Driver
.NewSamplerObject(ctx
, 0xDEADBEEF);
3044 if (decompress
->samp_obj
== NULL
) {
3045 _mesa_meta_end(ctx
);
3047 /* This is a bit lazy. Flag out of memory, and then don't bother to
3048 * clean up. Once out of memory is flagged, the only realistic next
3049 * move is to destroy the context. That will trigger all the right
3052 * Returning true prevents other GetTexImage methods from attempting
3053 * anything since they will likely fail too.
3055 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
3059 /* nearest filtering */
3060 _mesa_set_sampler_filters(ctx
, decompress
->samp_obj
, GL_NEAREST
, GL_NEAREST
);
3062 /* We don't want to encode or decode sRGB values; treat them as linear. */
3063 _mesa_set_sampler_srgb_decode(ctx
, decompress
->samp_obj
, GL_SKIP_DECODE_EXT
);
3066 _mesa_bind_sampler(ctx
, ctx
->Texture
.CurrentUnit
, decompress
->samp_obj
);
3068 /* Silence valgrind warnings about reading uninitialized stack. */
3069 memset(verts
, 0, sizeof(verts
));
3071 _mesa_meta_setup_texture_coords(faceTarget
, slice
,
3072 xoffset
, yoffset
, width
, height
,
3073 texImage
->Width
, texImage
->Height
,
3080 /* setup vertex positions */
3090 _mesa_set_viewport(ctx
, 0, 0, 0, width
, height
);
3092 /* upload new vertex data */
3093 _mesa_buffer_sub_data(ctx
, decompress
->buf_obj
, 0, sizeof(verts
), verts
,
3096 /* setup texture state */
3097 _mesa_BindTexture(target
, texObj
->Name
);
3099 if (!use_glsl_version
)
3100 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3103 /* save texture object state */
3104 const GLint baseLevelSave
= texObj
->BaseLevel
;
3105 const GLint maxLevelSave
= texObj
->MaxLevel
;
3107 /* restrict sampling to the texture level of interest */
3108 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3109 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_BASE_LEVEL
,
3110 (GLint
*) &texImage
->Level
, false);
3111 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_MAX_LEVEL
,
3112 (GLint
*) &texImage
->Level
, false);
3115 /* render quad w/ texture into renderbuffer */
3116 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3118 /* Restore texture object state, the texture binding will
3119 * be restored by _mesa_meta_end().
3121 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3122 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_BASE_LEVEL
,
3123 &baseLevelSave
, false);
3124 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_MAX_LEVEL
,
3125 &maxLevelSave
, false);
3130 /* read pixels from renderbuffer */
3132 GLenum baseTexFormat
= texImage
->_BaseFormat
;
3133 GLenum destBaseFormat
= _mesa_unpack_format_to_base_format(destFormat
);
3135 /* The pixel transfer state will be set to default values at this point
3136 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
3137 * turned off (as required by glGetTexImage) but we need to handle some
3138 * special cases. In particular, single-channel texture values are
3139 * returned as red and two-channel texture values are returned as
3142 if (_mesa_need_luminance_to_rgb_conversion(baseTexFormat
,
3144 /* If we're reading back an RGB(A) texture (using glGetTexImage) as
3145 * luminance then we need to return L=tex(R).
3147 _mesa_need_rgb_to_luminance_conversion(baseTexFormat
,
3149 /* Green and blue must be zero */
3150 _mesa_PixelTransferf(GL_GREEN_SCALE
, 0.0f
);
3151 _mesa_PixelTransferf(GL_BLUE_SCALE
, 0.0f
);
3154 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
3157 /* disable texture unit */
3158 if (!use_glsl_version
)
3159 _mesa_set_enable(ctx
, target
, GL_FALSE
);
3161 _mesa_bind_sampler(ctx
, ctx
->Texture
.CurrentUnit
, samp_obj_save
);
3162 _mesa_reference_sampler_object(ctx
, &samp_obj_save
, NULL
);
3164 _mesa_meta_end(ctx
);
3171 * This is just a wrapper around _mesa_get_tex_image() and
3172 * decompress_texture_image(). Meta functions should not be directly called
3176 _mesa_meta_GetTexSubImage(struct gl_context
*ctx
,
3177 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3178 GLsizei width
, GLsizei height
, GLsizei depth
,
3179 GLenum format
, GLenum type
, GLvoid
*pixels
,
3180 struct gl_texture_image
*texImage
)
3182 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
3186 for (slice
= 0; slice
< depth
; slice
++) {
3188 if (texImage
->TexObject
->Target
== GL_TEXTURE_2D_ARRAY
3189 || texImage
->TexObject
->Target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
3190 /* Setup pixel packing. SkipPixels and SkipRows will be applied
3191 * in the decompress_texture_image() function's call to
3192 * glReadPixels but we need to compute the dest slice's address
3193 * here (according to SkipImages and ImageHeight).
3195 struct gl_pixelstore_attrib packing
= ctx
->Pack
;
3196 packing
.SkipPixels
= 0;
3197 packing
.SkipRows
= 0;
3198 dst
= _mesa_image_address3d(&packing
, pixels
, width
, height
,
3199 format
, type
, slice
, 0, 0);
3204 result
= decompress_texture_image(ctx
, texImage
, slice
,
3205 xoffset
, yoffset
, width
, height
,
3215 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
3216 width
, height
, depth
, format
, type
, pixels
, texImage
);
3221 * Meta implementation of ctx->Driver.DrawTex() in terms
3222 * of polygon rendering.
3225 _mesa_meta_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
3226 GLfloat width
, GLfloat height
)
3228 struct drawtex_state
*drawtex
= &ctx
->Meta
->DrawTex
;
3230 GLfloat x
, y
, z
, st
[MAX_TEXTURE_UNITS
][2];
3232 struct vertex verts
[4];
3235 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
3237 MESA_META_TRANSFORM
|
3239 MESA_META_VIEWPORT
));
3241 if (drawtex
->VAO
== 0) {
3242 /* one-time setup */
3243 struct gl_vertex_array_object
*array_obj
;
3245 /* create vertex array object */
3246 _mesa_GenVertexArrays(1, &drawtex
->VAO
);
3247 _mesa_BindVertexArray(drawtex
->VAO
);
3249 array_obj
= _mesa_lookup_vao(ctx
, drawtex
->VAO
);
3250 assert(array_obj
!= NULL
);
3252 /* create vertex array buffer */
3253 drawtex
->buf_obj
= ctx
->Driver
.NewBufferObject(ctx
, 0xDEADBEEF);
3254 if (drawtex
->buf_obj
== NULL
)
3257 _mesa_buffer_data(ctx
, drawtex
->buf_obj
, GL_NONE
, sizeof(verts
), verts
,
3258 GL_DYNAMIC_DRAW
, __func__
);
3260 /* setup vertex arrays */
3261 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_POS
,
3262 3, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
3264 offsetof(struct vertex
, x
), true);
3265 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_POS
,
3266 drawtex
->buf_obj
, 0, sizeof(struct vertex
));
3267 _mesa_enable_vertex_array_attrib(ctx
, array_obj
, VERT_ATTRIB_POS
);
3270 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3271 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_TEX(i
),
3272 2, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
3274 offsetof(struct vertex
, st
[i
]), true);
3275 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_TEX(i
),
3276 drawtex
->buf_obj
, 0, sizeof(struct vertex
));
3277 _mesa_enable_vertex_array_attrib(ctx
, array_obj
, VERT_ATTRIB_TEX(i
));
3281 _mesa_BindVertexArray(drawtex
->VAO
);
3284 /* vertex positions, texcoords */
3286 const GLfloat x1
= x
+ width
;
3287 const GLfloat y1
= y
+ height
;
3289 z
= CLAMP(z
, 0.0f
, 1.0f
);
3308 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3309 const struct gl_texture_object
*texObj
;
3310 const struct gl_texture_image
*texImage
;
3311 GLfloat s
, t
, s1
, t1
;
3314 if (!ctx
->Texture
.Unit
[i
]._Current
) {
3316 for (j
= 0; j
< 4; j
++) {
3317 verts
[j
].st
[i
][0] = 0.0f
;
3318 verts
[j
].st
[i
][1] = 0.0f
;
3323 texObj
= ctx
->Texture
.Unit
[i
]._Current
;
3324 texImage
= texObj
->Image
[0][texObj
->BaseLevel
];
3325 tw
= texImage
->Width2
;
3326 th
= texImage
->Height2
;
3328 s
= (GLfloat
) texObj
->CropRect
[0] / tw
;
3329 t
= (GLfloat
) texObj
->CropRect
[1] / th
;
3330 s1
= (GLfloat
) (texObj
->CropRect
[0] + texObj
->CropRect
[2]) / tw
;
3331 t1
= (GLfloat
) (texObj
->CropRect
[1] + texObj
->CropRect
[3]) / th
;
3333 verts
[0].st
[i
][0] = s
;
3334 verts
[0].st
[i
][1] = t
;
3336 verts
[1].st
[i
][0] = s1
;
3337 verts
[1].st
[i
][1] = t
;
3339 verts
[2].st
[i
][0] = s1
;
3340 verts
[2].st
[i
][1] = t1
;
3342 verts
[3].st
[i
][0] = s
;
3343 verts
[3].st
[i
][1] = t1
;
3346 _mesa_buffer_sub_data(ctx
, drawtex
->buf_obj
, 0, sizeof(verts
), verts
,
3350 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3352 _mesa_meta_end(ctx
);
3356 cleartexsubimage_color(struct gl_context
*ctx
,
3357 struct gl_texture_image
*texImage
,
3358 const GLvoid
*clearValue
,
3362 union gl_color_union colorValue
;
3366 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
3367 GL_COLOR_ATTACHMENT0
,
3370 status
= _mesa_check_framebuffer_status(ctx
, ctx
->DrawBuffer
);
3371 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
3374 /* We don't want to apply an sRGB conversion so override the format */
3375 format
= _mesa_get_srgb_format_linear(texImage
->TexFormat
);
3376 datatype
= _mesa_get_format_datatype(format
);
3379 case GL_UNSIGNED_INT
:
3382 _mesa_unpack_uint_rgba_row(format
, 1, clearValue
,
3383 (GLuint (*)[4]) colorValue
.ui
);
3385 memset(&colorValue
, 0, sizeof colorValue
);
3386 if (datatype
== GL_INT
)
3387 _mesa_ClearBufferiv(GL_COLOR
, 0, colorValue
.i
);
3389 _mesa_ClearBufferuiv(GL_COLOR
, 0, colorValue
.ui
);
3393 _mesa_unpack_rgba_row(format
, 1, clearValue
,
3394 (GLfloat (*)[4]) colorValue
.f
);
3396 memset(&colorValue
, 0, sizeof colorValue
);
3397 _mesa_ClearBufferfv(GL_COLOR
, 0, colorValue
.f
);
3405 cleartexsubimage_depth_stencil(struct gl_context
*ctx
,
3406 struct gl_texture_image
*texImage
,
3407 const GLvoid
*clearValue
,
3414 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
3415 GL_DEPTH_ATTACHMENT
,
3418 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
)
3419 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
3420 GL_STENCIL_ATTACHMENT
,
3423 status
= _mesa_check_framebuffer_status(ctx
, ctx
->DrawBuffer
);
3424 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
3428 GLuint depthStencilValue
[2];
3430 /* Convert the clearValue from whatever format it's in to a floating
3431 * point value for the depth and an integer value for the stencil index
3433 _mesa_unpack_float_32_uint_24_8_depth_stencil_row(texImage
->TexFormat
,
3437 /* We need a memcpy here instead of a cast because we need to
3438 * reinterpret the bytes as a float rather than converting it
3440 memcpy(&depthValue
, depthStencilValue
, sizeof depthValue
);
3441 stencilValue
= depthStencilValue
[1] & 0xff;
3447 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
)
3448 _mesa_ClearBufferfi(GL_DEPTH_STENCIL
, 0, depthValue
, stencilValue
);
3450 _mesa_ClearBufferfv(GL_DEPTH
, 0, &depthValue
);
3456 cleartexsubimage_for_zoffset(struct gl_context
*ctx
,
3457 struct gl_texture_image
*texImage
,
3459 const GLvoid
*clearValue
)
3461 struct gl_framebuffer
*drawFb
;
3464 drawFb
= ctx
->Driver
.NewFramebuffer(ctx
, 0xDEADBEEF);
3468 _mesa_bind_framebuffers(ctx
, drawFb
, ctx
->ReadBuffer
);
3470 switch(texImage
->_BaseFormat
) {
3471 case GL_DEPTH_STENCIL
:
3472 case GL_DEPTH_COMPONENT
:
3473 success
= cleartexsubimage_depth_stencil(ctx
, texImage
,
3474 clearValue
, zoffset
);
3477 success
= cleartexsubimage_color(ctx
, texImage
, clearValue
, zoffset
);
3481 _mesa_reference_framebuffer(&drawFb
, NULL
);
3487 cleartexsubimage_using_fbo(struct gl_context
*ctx
,
3488 struct gl_texture_image
*texImage
,
3489 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3490 GLsizei width
, GLsizei height
, GLsizei depth
,
3491 const GLvoid
*clearValue
)
3493 bool success
= true;
3496 _mesa_meta_begin(ctx
,
3498 MESA_META_COLOR_MASK
|
3500 MESA_META_FRAMEBUFFER_SRGB
);
3502 _mesa_set_enable(ctx
, GL_DITHER
, GL_FALSE
);
3504 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_TRUE
);
3505 _mesa_Scissor(xoffset
, yoffset
, width
, height
);
3507 for (z
= zoffset
; z
< zoffset
+ depth
; z
++) {
3508 if (!cleartexsubimage_for_zoffset(ctx
, texImage
, z
, clearValue
)) {
3514 _mesa_meta_end(ctx
);
3520 _mesa_meta_ClearTexSubImage(struct gl_context
*ctx
,
3521 struct gl_texture_image
*texImage
,
3522 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3523 GLsizei width
, GLsizei height
, GLsizei depth
,
3524 const GLvoid
*clearValue
)
3528 res
= cleartexsubimage_using_fbo(ctx
, texImage
,
3529 xoffset
, yoffset
, zoffset
,
3530 width
, height
, depth
,
3537 "Falling back to mapping the texture in "
3538 "glClearTexSubImage\n");
3540 _mesa_store_cleartexsubimage(ctx
, texImage
,
3541 xoffset
, yoffset
, zoffset
,
3542 width
, height
, depth
,