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/draw.h"
46 #include "main/depth.h"
47 #include "main/enable.h"
48 #include "main/fbobject.h"
49 #include "main/feedback.h"
50 #include "main/formats.h"
51 #include "main/format_unpack.h"
52 #include "main/framebuffer.h"
53 #include "main/glformats.h"
54 #include "main/image.h"
55 #include "main/macros.h"
56 #include "main/matrix.h"
57 #include "main/mipmap.h"
58 #include "main/multisample.h"
59 #include "main/objectlabel.h"
60 #include "main/pipelineobj.h"
61 #include "main/pixel.h"
63 #include "main/polygon.h"
64 #include "main/queryobj.h"
65 #include "main/readpix.h"
66 #include "main/renderbuffer.h"
67 #include "main/scissor.h"
68 #include "main/shaderapi.h"
69 #include "main/shaderobj.h"
70 #include "main/state.h"
71 #include "main/stencil.h"
72 #include "main/texobj.h"
73 #include "main/texenv.h"
74 #include "main/texgetimage.h"
75 #include "main/teximage.h"
76 #include "main/texparam.h"
77 #include "main/texstate.h"
78 #include "main/texstore.h"
79 #include "main/transformfeedback.h"
80 #include "main/uniforms.h"
81 #include "main/varray.h"
82 #include "main/viewport.h"
83 #include "main/samplerobj.h"
84 #include "program/program.h"
85 #include "swrast/swrast.h"
86 #include "drivers/common/meta.h"
87 #include "main/enums.h"
88 #include "main/glformats.h"
89 #include "util/bitscan.h"
90 #include "util/ralloc.h"
91 #include "compiler/nir/nir.h"
92 #include "util/u_math.h"
94 /** Return offset in bytes of the field within a vertex struct */
95 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
98 meta_clear(struct gl_context
*ctx
, GLbitfield buffers
, bool glsl
);
100 static struct blit_shader
*
101 choose_blit_shader(GLenum target
, struct blit_shader_table
*table
);
103 static void cleanup_temp_texture(struct gl_context
*ctx
,
104 struct temp_texture
*tex
);
105 static void meta_glsl_clear_cleanup(struct gl_context
*ctx
,
106 struct clear_state
*clear
);
107 static void meta_decompress_cleanup(struct gl_context
*ctx
,
108 struct decompress_state
*decompress
);
109 static void meta_drawpix_cleanup(struct gl_context
*ctx
,
110 struct drawpix_state
*drawpix
);
113 _mesa_meta_framebuffer_texture_image(struct gl_context
*ctx
,
114 struct gl_framebuffer
*fb
,
116 struct gl_texture_image
*texImage
,
119 struct gl_texture_object
*texObj
= texImage
->TexObject
;
120 int level
= texImage
->Level
;
121 const GLenum texTarget
= texObj
->Target
== GL_TEXTURE_CUBE_MAP
122 ? GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
125 struct gl_renderbuffer_attachment
*att
=
126 _mesa_get_and_validate_attachment(ctx
, fb
, attachment
, __func__
);
129 _mesa_framebuffer_texture(ctx
, fb
, attachment
, att
, texObj
, texTarget
,
130 level
, att
->NumSamples
, layer
, false);
133 static struct gl_shader
*
134 meta_compile_shader_with_debug(struct gl_context
*ctx
, gl_shader_stage stage
,
135 const GLcharARB
*source
)
137 const GLuint name
= ~0;
138 struct gl_shader
*sh
;
140 sh
= _mesa_new_shader(name
, stage
);
141 sh
->Source
= strdup(source
);
142 sh
->CompileStatus
= COMPILE_FAILURE
;
143 _mesa_compile_shader(ctx
, sh
);
145 if (!sh
->CompileStatus
) {
148 "meta program compile failed:\n%s\nsource:\n%s\n",
149 sh
->InfoLog
, source
);
152 _mesa_reference_shader(ctx
, &sh
, NULL
);
159 _mesa_meta_link_program_with_debug(struct gl_context
*ctx
,
160 struct gl_shader_program
*sh_prog
)
162 _mesa_link_program(ctx
, sh_prog
);
164 if (!sh_prog
->data
->LinkStatus
) {
165 _mesa_problem(ctx
, "meta program link failed:\n%s",
166 sh_prog
->data
->InfoLog
);
171 _mesa_meta_use_program(struct gl_context
*ctx
,
172 struct gl_shader_program
*sh_prog
)
174 /* Attach shader state to the binding point */
175 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
177 /* Update the program */
178 _mesa_use_shader_program(ctx
, sh_prog
);
182 _mesa_meta_compile_and_link_program(struct gl_context
*ctx
,
183 const char *vs_source
,
184 const char *fs_source
,
186 struct gl_shader_program
**out_sh_prog
)
188 struct gl_shader_program
*sh_prog
;
189 const GLuint id
= ~0;
191 sh_prog
= _mesa_new_shader_program(id
);
192 sh_prog
->Label
= strdup(name
);
193 sh_prog
->NumShaders
= 2;
194 sh_prog
->Shaders
= malloc(2 * sizeof(struct gl_shader
*));
195 sh_prog
->Shaders
[0] =
196 meta_compile_shader_with_debug(ctx
, MESA_SHADER_VERTEX
, vs_source
);
197 sh_prog
->Shaders
[1] =
198 meta_compile_shader_with_debug(ctx
, MESA_SHADER_FRAGMENT
, fs_source
);
200 _mesa_meta_link_program_with_debug(ctx
, sh_prog
);
202 struct gl_program
*fp
=
203 sh_prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
;
205 /* texelFetch() can break GL_SKIP_DECODE_EXT, but many meta passes want
206 * to use both together; pretend that we're not using texelFetch to hack
207 * around this bad interaction. This is a bit fragile as it may break
208 * if you re-run the pass that gathers this info, but we probably won't...
210 fp
->info
.textures_used_by_txf
= 0;
212 fp
->nir
->info
.textures_used_by_txf
= 0;
214 _mesa_meta_use_program(ctx
, sh_prog
);
216 *out_sh_prog
= sh_prog
;
220 * Generate a generic shader to blit from a texture to a framebuffer
222 * \param ctx Current GL context
223 * \param texTarget Texture target that will be the source of the blit
225 * \returns a handle to a shader program on success or zero on failure.
228 _mesa_meta_setup_blit_shader(struct gl_context
*ctx
,
231 struct blit_shader_table
*table
)
233 char *vs_source
, *fs_source
;
234 struct blit_shader
*shader
= choose_blit_shader(target
, table
);
235 const char *fs_input
, *vs_preprocess
, *fs_preprocess
;
238 if (ctx
->Const
.GLSLVersion
< 130) {
240 fs_preprocess
= "#extension GL_EXT_texture_array : enable";
241 fs_input
= "varying";
243 vs_preprocess
= "#version 130";
244 fs_preprocess
= "#version 130";
246 shader
->func
= "texture";
249 assert(shader
!= NULL
);
251 if (shader
->shader_prog
!= NULL
) {
252 _mesa_meta_use_program(ctx
, shader
->shader_prog
);
256 mem_ctx
= ralloc_context(NULL
);
258 vs_source
= ralloc_asprintf(mem_ctx
,
260 "#extension GL_ARB_explicit_attrib_location: enable\n"
261 "layout(location = 0) in vec2 position;\n"
262 "layout(location = 1) in vec4 textureCoords;\n"
263 "out vec4 texCoords;\n"
266 " texCoords = textureCoords;\n"
267 " gl_Position = vec4(position, 0.0, 1.0);\n"
271 fs_source
= ralloc_asprintf(mem_ctx
,
273 "#extension GL_ARB_texture_cube_map_array: enable\n"
274 "uniform %s texSampler;\n"
275 "%s vec4 texCoords;\n"
278 " gl_FragColor = %s(texSampler, %s);\n"
281 fs_preprocess
, shader
->type
, fs_input
,
282 shader
->func
, shader
->texcoords
,
283 do_depth
? " gl_FragDepth = gl_FragColor.x;\n" : "");
285 _mesa_meta_compile_and_link_program(ctx
, vs_source
, fs_source
,
286 ralloc_asprintf(mem_ctx
, "%s blit",
288 &shader
->shader_prog
);
289 ralloc_free(mem_ctx
);
293 * Configure vertex buffer and vertex array objects for tests
295 * Regardless of whether a new VAO is created, the object referenced by \c VAO
296 * will be bound into the GL state vector when this function terminates. The
297 * object referenced by \c VBO will \b not be bound.
299 * \param VAO Storage for vertex array object handle. If 0, a new VAO
301 * \param buf_obj Storage for vertex buffer object pointer. If \c NULL, a new VBO
302 * will be created. The new VBO will have storage for 4
303 * \c vertex structures.
304 * \param use_generic_attributes Should generic attributes 0 and 1 be used,
305 * or should traditional, fixed-function color and texture
306 * coordinate be used?
307 * \param vertex_size Number of components for attribute 0 / vertex.
308 * \param texcoord_size Number of components for attribute 1 / texture
309 * coordinate. If this is 0, attribute 1 will not be set or
311 * \param color_size Number of components for attribute 1 / primary color.
312 * If this is 0, attribute 1 will not be set or enabled.
314 * \note If \c use_generic_attributes is \c true, \c color_size must be zero.
315 * Use \c texcoord_size instead.
318 _mesa_meta_setup_vertex_objects(struct gl_context
*ctx
,
319 GLuint
*VAO
, struct gl_buffer_object
**buf_obj
,
320 bool use_generic_attributes
,
321 unsigned vertex_size
, unsigned texcoord_size
,
325 struct gl_vertex_array_object
*array_obj
;
326 assert(*buf_obj
== NULL
);
328 /* create vertex array object */
329 _mesa_GenVertexArrays(1, VAO
);
330 _mesa_BindVertexArray(*VAO
);
332 array_obj
= _mesa_lookup_vao(ctx
, *VAO
);
333 assert(array_obj
!= NULL
);
335 /* create vertex array buffer */
336 *buf_obj
= ctx
->Driver
.NewBufferObject(ctx
, 0xDEADBEEF);
337 if (*buf_obj
== NULL
)
340 _mesa_buffer_data(ctx
, *buf_obj
, GL_NONE
, 4 * sizeof(struct vertex
), NULL
,
341 GL_DYNAMIC_DRAW
, __func__
);
343 /* setup vertex arrays */
344 FLUSH_VERTICES(ctx
, 0);
345 if (use_generic_attributes
) {
346 assert(color_size
== 0);
348 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_GENERIC(0),
349 vertex_size
, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
351 offsetof(struct vertex
, x
));
352 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_GENERIC(0),
353 *buf_obj
, 0, sizeof(struct vertex
));
354 _mesa_enable_vertex_array_attrib(ctx
, array_obj
,
355 VERT_ATTRIB_GENERIC(0));
356 if (texcoord_size
> 0) {
357 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_GENERIC(1),
358 texcoord_size
, GL_FLOAT
, GL_RGBA
,
359 GL_FALSE
, GL_FALSE
, GL_FALSE
,
360 offsetof(struct vertex
, tex
));
361 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_GENERIC(1),
362 *buf_obj
, 0, sizeof(struct vertex
));
363 _mesa_enable_vertex_array_attrib(ctx
, array_obj
,
364 VERT_ATTRIB_GENERIC(1));
367 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_POS
,
368 vertex_size
, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
370 offsetof(struct vertex
, x
));
371 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_POS
,
372 *buf_obj
, 0, sizeof(struct vertex
));
373 _mesa_enable_vertex_array_attrib(ctx
, array_obj
, VERT_ATTRIB_POS
);
375 if (texcoord_size
> 0) {
376 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_TEX(0),
377 vertex_size
, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
379 offsetof(struct vertex
, tex
));
380 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_TEX(0),
381 *buf_obj
, 0, sizeof(struct vertex
));
382 _mesa_enable_vertex_array_attrib(ctx
, array_obj
,
386 if (color_size
> 0) {
387 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_COLOR0
,
388 vertex_size
, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
390 offsetof(struct vertex
, r
));
391 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_COLOR0
,
392 *buf_obj
, 0, sizeof(struct vertex
));
393 _mesa_enable_vertex_array_attrib(ctx
, array_obj
,
398 _mesa_BindVertexArray(*VAO
);
403 * Initialize meta-ops for a context.
404 * To be called once during context creation.
407 _mesa_meta_init(struct gl_context
*ctx
)
411 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
415 * Free context meta-op state.
416 * To be called once during context destruction.
419 _mesa_meta_free(struct gl_context
*ctx
)
421 GET_CURRENT_CONTEXT(old_context
);
422 _mesa_make_current(ctx
, NULL
, NULL
);
423 _mesa_meta_glsl_blit_cleanup(ctx
, &ctx
->Meta
->Blit
);
424 meta_glsl_clear_cleanup(ctx
, &ctx
->Meta
->Clear
);
425 _mesa_meta_glsl_generate_mipmap_cleanup(ctx
, &ctx
->Meta
->Mipmap
);
426 cleanup_temp_texture(ctx
, &ctx
->Meta
->TempTex
);
427 meta_decompress_cleanup(ctx
, &ctx
->Meta
->Decompress
);
428 meta_drawpix_cleanup(ctx
, &ctx
->Meta
->DrawPix
);
430 _mesa_make_current(old_context
, old_context
->WinSysDrawBuffer
, old_context
->WinSysReadBuffer
);
432 _mesa_make_current(NULL
, NULL
, NULL
);
439 * Enter meta state. This is like a light-weight version of glPushAttrib
440 * but it also resets most GL state back to default values.
442 * \param state bitmask of MESA_META_* flags indicating which attribute groups
443 * to save and reset to their defaults
446 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
448 struct save_state
*save
;
450 /* hope MAX_META_OPS_DEPTH is large enough */
451 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
453 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
454 memset(save
, 0, sizeof(*save
));
455 save
->SavedState
= state
;
457 /* We always push into desktop GL mode and pop out at the end. No sense in
458 * writing our shaders varying based on the user's context choice, when
459 * Mesa can handle either.
461 save
->API
= ctx
->API
;
462 ctx
->API
= API_OPENGL_COMPAT
;
464 /* Mesa's extension helper functions use the current context's API to look up
465 * the version required by an extension as a step in determining whether or
466 * not it has been advertised. Since meta aims to only be restricted by the
467 * driver capability (and not by whether or not an extension has been
468 * advertised), set the helper functions' Version variable to a value that
469 * will make the checks on the context API and version unconditionally pass.
471 save
->ExtensionsVersion
= ctx
->Extensions
.Version
;
472 ctx
->Extensions
.Version
= ~0;
474 /* Pausing transform feedback needs to be done early, or else we won't be
475 * able to change other state.
477 save
->TransformFeedbackNeedsResume
=
478 _mesa_is_xfb_active_and_unpaused(ctx
);
479 if (save
->TransformFeedbackNeedsResume
)
480 _mesa_PauseTransformFeedback();
482 /* After saving the current occlusion object, call EndQuery so that no
483 * occlusion querying will be active during the meta-operation.
485 if (state
& MESA_META_OCCLUSION_QUERY
) {
486 save
->CurrentOcclusionObject
= ctx
->Query
.CurrentOcclusionObject
;
487 if (save
->CurrentOcclusionObject
)
488 _mesa_EndQuery(save
->CurrentOcclusionObject
->Target
);
491 if (state
& MESA_META_ALPHA_TEST
) {
492 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
493 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
494 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
495 if (ctx
->Color
.AlphaEnabled
)
496 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
499 if (state
& MESA_META_BLEND
) {
500 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
501 if (ctx
->Color
.BlendEnabled
) {
502 if (ctx
->Extensions
.EXT_draw_buffers2
) {
504 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
505 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
509 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
512 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
513 if (ctx
->Color
.ColorLogicOpEnabled
)
514 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
517 if (state
& MESA_META_DITHER
) {
518 save
->DitherFlag
= ctx
->Color
.DitherFlag
;
519 _mesa_set_enable(ctx
, GL_DITHER
, GL_TRUE
);
522 if (state
& MESA_META_COLOR_MASK
)
523 save
->ColorMask
= ctx
->Color
.ColorMask
;
525 if (state
& MESA_META_DEPTH_TEST
) {
526 save
->Depth
= ctx
->Depth
; /* struct copy */
528 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
531 if (state
& MESA_META_FOG
) {
532 save
->Fog
= ctx
->Fog
.Enabled
;
533 if (ctx
->Fog
.Enabled
)
534 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
537 if (state
& MESA_META_PIXEL_STORE
) {
538 save
->Pack
= ctx
->Pack
;
539 save
->Unpack
= ctx
->Unpack
;
540 ctx
->Pack
= ctx
->DefaultPacking
;
541 ctx
->Unpack
= ctx
->DefaultPacking
;
544 if (state
& MESA_META_PIXEL_TRANSFER
) {
545 save
->RedScale
= ctx
->Pixel
.RedScale
;
546 save
->RedBias
= ctx
->Pixel
.RedBias
;
547 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
548 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
549 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
550 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
551 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
552 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
553 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
554 ctx
->Pixel
.RedScale
= 1.0F
;
555 ctx
->Pixel
.RedBias
= 0.0F
;
556 ctx
->Pixel
.GreenScale
= 1.0F
;
557 ctx
->Pixel
.GreenBias
= 0.0F
;
558 ctx
->Pixel
.BlueScale
= 1.0F
;
559 ctx
->Pixel
.BlueBias
= 0.0F
;
560 ctx
->Pixel
.AlphaScale
= 1.0F
;
561 ctx
->Pixel
.AlphaBias
= 0.0F
;
562 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
564 ctx
->NewState
|=_NEW_PIXEL
;
567 if (state
& MESA_META_RASTERIZATION
) {
568 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
569 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
570 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
571 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
572 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
573 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
574 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
575 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
576 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
577 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
578 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
581 if (state
& MESA_META_SCISSOR
) {
582 save
->Scissor
= ctx
->Scissor
; /* struct copy */
583 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
586 if (state
& MESA_META_SHADER
) {
589 if (ctx
->Extensions
.ARB_vertex_program
) {
590 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
591 _mesa_reference_program(ctx
, &save
->VertexProgram
,
592 ctx
->VertexProgram
.Current
);
593 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
596 if (ctx
->Extensions
.ARB_fragment_program
) {
597 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
598 _mesa_reference_program(ctx
, &save
->FragmentProgram
,
599 ctx
->FragmentProgram
.Current
);
600 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
603 if (ctx
->Extensions
.ATI_fragment_shader
) {
604 save
->ATIFragmentShaderEnabled
= ctx
->ATIFragmentShader
.Enabled
;
605 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
, GL_FALSE
);
608 if (ctx
->Pipeline
.Current
) {
609 _mesa_reference_pipeline_object(ctx
, &save
->Pipeline
,
610 ctx
->Pipeline
.Current
);
611 _mesa_BindProgramPipeline(0);
614 /* Save the shader state from ctx->Shader (instead of ctx->_Shader) so
615 * that we don't have to worry about the current pipeline state.
617 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
618 _mesa_reference_program(ctx
, &save
->Program
[i
],
619 ctx
->Shader
.CurrentProgram
[i
]);
621 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
622 ctx
->Shader
.ActiveProgram
);
627 if (state
& MESA_META_STENCIL_TEST
) {
628 save
->Stencil
= ctx
->Stencil
; /* struct copy */
629 if (ctx
->Stencil
.Enabled
)
630 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
631 /* NOTE: other stencil state not reset */
634 if (state
& MESA_META_TEXTURE
) {
637 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
638 save
->EnvMode
= ctx
->Texture
.FixedFuncUnit
[0].EnvMode
;
640 /* Disable all texture units */
641 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
642 save
->TexEnabled
[u
] = ctx
->Texture
.FixedFuncUnit
[u
].Enabled
;
643 save
->TexGenEnabled
[u
] = ctx
->Texture
.FixedFuncUnit
[u
].TexGenEnabled
;
644 if (ctx
->Texture
.FixedFuncUnit
[u
].Enabled
||
645 ctx
->Texture
.FixedFuncUnit
[u
].TexGenEnabled
) {
646 _mesa_ActiveTexture(GL_TEXTURE0
+ u
);
647 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
648 if (ctx
->Extensions
.ARB_texture_cube_map
)
649 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
651 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
652 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
653 if (ctx
->Extensions
.NV_texture_rectangle
)
654 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
655 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
656 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
657 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
658 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
662 /* save current texture objects for unit[0] only */
663 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
664 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
665 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
668 /* set defaults for unit[0] */
669 _mesa_ActiveTexture(GL_TEXTURE0
);
670 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
673 if (state
& MESA_META_TRANSFORM
) {
674 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
675 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
676 16 * sizeof(GLfloat
));
677 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
678 16 * sizeof(GLfloat
));
679 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
680 16 * sizeof(GLfloat
));
681 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
682 /* set 1:1 vertex:pixel coordinate transform */
683 _mesa_ActiveTexture(GL_TEXTURE0
);
684 _mesa_MatrixMode(GL_TEXTURE
);
685 _mesa_LoadIdentity();
686 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
687 _mesa_MatrixMode(GL_MODELVIEW
);
688 _mesa_LoadIdentity();
689 _mesa_MatrixMode(GL_PROJECTION
);
690 _mesa_LoadIdentity();
692 /* glOrtho with width = 0 or height = 0 generates GL_INVALID_VALUE.
693 * This can occur when there is no draw buffer.
695 if (ctx
->DrawBuffer
->Width
!= 0 && ctx
->DrawBuffer
->Height
!= 0)
696 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
697 0.0, ctx
->DrawBuffer
->Height
,
700 if (ctx
->Extensions
.ARB_clip_control
) {
701 save
->ClipOrigin
= ctx
->Transform
.ClipOrigin
;
702 save
->ClipDepthMode
= ctx
->Transform
.ClipDepthMode
;
703 _mesa_ClipControl(GL_LOWER_LEFT
, GL_NEGATIVE_ONE_TO_ONE
);
707 if (state
& MESA_META_CLIP
) {
709 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
710 mask
= ctx
->Transform
.ClipPlanesEnabled
;
712 const int i
= u_bit_scan(&mask
);
713 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
717 if (state
& MESA_META_VERTEX
) {
718 /* save vertex array object state */
719 _mesa_reference_vao(ctx
, &save
->VAO
,
721 /* set some default state? */
724 if (state
& MESA_META_VIEWPORT
) {
725 /* save viewport state */
726 save
->ViewportX
= ctx
->ViewportArray
[0].X
;
727 save
->ViewportY
= ctx
->ViewportArray
[0].Y
;
728 save
->ViewportW
= ctx
->ViewportArray
[0].Width
;
729 save
->ViewportH
= ctx
->ViewportArray
[0].Height
;
730 /* set viewport to match window size */
731 if (ctx
->ViewportArray
[0].X
!= 0 ||
732 ctx
->ViewportArray
[0].Y
!= 0 ||
733 ctx
->ViewportArray
[0].Width
!= (float) ctx
->DrawBuffer
->Width
||
734 ctx
->ViewportArray
[0].Height
!= (float) ctx
->DrawBuffer
->Height
) {
735 _mesa_set_viewport(ctx
, 0, 0, 0,
736 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
738 /* save depth range state */
739 save
->DepthNear
= ctx
->ViewportArray
[0].Near
;
740 save
->DepthFar
= ctx
->ViewportArray
[0].Far
;
741 /* set depth range to default */
742 _mesa_set_depth_range(ctx
, 0, 0.0, 1.0);
745 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
746 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
748 /* Generally in here we want to do clamping according to whether
749 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
750 * regardless of the internal implementation of the metaops.
752 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
753 ctx
->Extensions
.ARB_color_buffer_float
)
754 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
757 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
758 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
760 /* Generally in here we never want vertex color clamping --
761 * result clamping is only dependent on fragment clamping.
763 if (ctx
->Extensions
.ARB_color_buffer_float
)
764 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
767 if (state
& MESA_META_CONDITIONAL_RENDER
) {
768 save
->CondRenderQuery
= ctx
->Query
.CondRenderQuery
;
769 save
->CondRenderMode
= ctx
->Query
.CondRenderMode
;
771 if (ctx
->Query
.CondRenderQuery
)
772 _mesa_EndConditionalRender();
775 if (state
& MESA_META_SELECT_FEEDBACK
) {
776 save
->RenderMode
= ctx
->RenderMode
;
777 if (ctx
->RenderMode
== GL_SELECT
) {
778 save
->Select
= ctx
->Select
; /* struct copy */
779 _mesa_RenderMode(GL_RENDER
);
780 } else if (ctx
->RenderMode
== GL_FEEDBACK
) {
781 save
->Feedback
= ctx
->Feedback
; /* struct copy */
782 _mesa_RenderMode(GL_RENDER
);
786 if (state
& MESA_META_MULTISAMPLE
) {
787 save
->Multisample
= ctx
->Multisample
; /* struct copy */
789 if (ctx
->Multisample
.Enabled
)
790 _mesa_set_multisample(ctx
, GL_FALSE
);
791 if (ctx
->Multisample
.SampleCoverage
)
792 _mesa_set_enable(ctx
, GL_SAMPLE_COVERAGE
, GL_FALSE
);
793 if (ctx
->Multisample
.SampleAlphaToCoverage
)
794 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_COVERAGE
, GL_FALSE
);
795 if (ctx
->Multisample
.SampleAlphaToOne
)
796 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_ONE
, GL_FALSE
);
797 if (ctx
->Multisample
.SampleShading
)
798 _mesa_set_enable(ctx
, GL_SAMPLE_SHADING
, GL_FALSE
);
799 if (ctx
->Multisample
.SampleMask
)
800 _mesa_set_enable(ctx
, GL_SAMPLE_MASK
, GL_FALSE
);
803 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
804 save
->sRGBEnabled
= ctx
->Color
.sRGBEnabled
;
805 if (ctx
->Color
.sRGBEnabled
)
806 _mesa_set_framebuffer_srgb(ctx
, GL_FALSE
);
809 if (state
& MESA_META_DRAW_BUFFERS
) {
810 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
811 memcpy(save
->ColorDrawBuffers
, fb
->ColorDrawBuffer
,
812 sizeof(save
->ColorDrawBuffers
));
817 save
->Lighting
= ctx
->Light
.Enabled
;
818 if (ctx
->Light
.Enabled
)
819 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
820 save
->RasterDiscard
= ctx
->RasterDiscard
;
821 if (ctx
->RasterDiscard
)
822 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_FALSE
);
824 _mesa_reference_framebuffer(&save
->DrawBuffer
, ctx
->DrawBuffer
);
825 _mesa_reference_framebuffer(&save
->ReadBuffer
, ctx
->ReadBuffer
);
831 * Leave meta state. This is like a light-weight version of glPopAttrib().
834 _mesa_meta_end(struct gl_context
*ctx
)
836 assert(ctx
->Meta
->SaveStackDepth
> 0);
838 struct save_state
*save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
- 1];
839 const GLbitfield state
= save
->SavedState
;
842 /* Grab the result of the old occlusion query before starting it again. The
843 * old result is added to the result of the new query so the driver will
844 * continue adding where it left off. */
845 if (state
& MESA_META_OCCLUSION_QUERY
) {
846 if (save
->CurrentOcclusionObject
) {
847 struct gl_query_object
*q
= save
->CurrentOcclusionObject
;
850 ctx
->Driver
.WaitQuery(ctx
, q
);
852 _mesa_BeginQuery(q
->Target
, q
->Id
);
853 ctx
->Query
.CurrentOcclusionObject
->Result
+= result
;
857 if (state
& MESA_META_ALPHA_TEST
) {
858 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
859 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
860 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
863 if (state
& MESA_META_BLEND
) {
864 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
865 if (ctx
->Extensions
.EXT_draw_buffers2
) {
867 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
868 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
872 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
875 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
876 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
879 if (state
& MESA_META_DITHER
)
880 _mesa_set_enable(ctx
, GL_DITHER
, save
->DitherFlag
);
882 if (state
& MESA_META_COLOR_MASK
) {
884 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
885 if (GET_COLORMASK(ctx
->Color
.ColorMask
, i
) !=
886 GET_COLORMASK(save
->ColorMask
, i
)) {
888 _mesa_ColorMask(GET_COLORMASK_BIT(save
->ColorMask
, i
, 0),
889 GET_COLORMASK_BIT(save
->ColorMask
, i
, 1),
890 GET_COLORMASK_BIT(save
->ColorMask
, i
, 2),
891 GET_COLORMASK_BIT(save
->ColorMask
, i
, 3));
895 GET_COLORMASK_BIT(save
->ColorMask
, i
, 0),
896 GET_COLORMASK_BIT(save
->ColorMask
, i
, 1),
897 GET_COLORMASK_BIT(save
->ColorMask
, i
, 2),
898 GET_COLORMASK_BIT(save
->ColorMask
, i
, 3));
904 if (state
& MESA_META_DEPTH_TEST
) {
905 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
906 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
907 _mesa_DepthFunc(save
->Depth
.Func
);
908 _mesa_DepthMask(save
->Depth
.Mask
);
911 if (state
& MESA_META_FOG
) {
912 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
915 if (state
& MESA_META_PIXEL_STORE
) {
916 ctx
->Pack
= save
->Pack
;
917 ctx
->Unpack
= save
->Unpack
;
920 if (state
& MESA_META_PIXEL_TRANSFER
) {
921 ctx
->Pixel
.RedScale
= save
->RedScale
;
922 ctx
->Pixel
.RedBias
= save
->RedBias
;
923 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
924 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
925 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
926 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
927 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
928 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
929 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
931 ctx
->NewState
|=_NEW_PIXEL
;
934 if (state
& MESA_META_RASTERIZATION
) {
935 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
936 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
937 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
938 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
939 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
940 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
943 if (state
& MESA_META_SCISSOR
) {
946 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++) {
947 _mesa_set_scissor(ctx
, i
,
948 save
->Scissor
.ScissorArray
[i
].X
,
949 save
->Scissor
.ScissorArray
[i
].Y
,
950 save
->Scissor
.ScissorArray
[i
].Width
,
951 save
->Scissor
.ScissorArray
[i
].Height
);
952 _mesa_set_enablei(ctx
, GL_SCISSOR_TEST
, i
,
953 (save
->Scissor
.EnableFlags
>> i
) & 1);
957 if (state
& MESA_META_SHADER
) {
960 if (ctx
->Extensions
.ARB_vertex_program
) {
961 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
962 save
->VertexProgramEnabled
);
963 _mesa_reference_program(ctx
, &ctx
->VertexProgram
.Current
,
964 save
->VertexProgram
);
965 _mesa_reference_program(ctx
, &save
->VertexProgram
, NULL
);
968 if (ctx
->Extensions
.ARB_fragment_program
) {
969 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
970 save
->FragmentProgramEnabled
);
971 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
.Current
,
972 save
->FragmentProgram
);
973 _mesa_reference_program(ctx
, &save
->FragmentProgram
, NULL
);
976 if (ctx
->Extensions
.ATI_fragment_shader
) {
977 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
,
978 save
->ATIFragmentShaderEnabled
);
982 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
983 /* It is safe to call _mesa_use_program even if the extension
984 * necessary for that program state is not supported. In that case,
985 * the saved program object must be NULL and the currently bound
986 * program object must be NULL. _mesa_use_program is a no-op
989 _mesa_use_program(ctx
, i
, NULL
, save
->Program
[i
], &ctx
->Shader
);
991 /* Do this *before* killing the reference. :)
993 if (save
->Program
[i
] != NULL
)
996 _mesa_reference_program(ctx
, &save
->Program
[i
], NULL
);
999 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
1000 save
->ActiveShader
);
1001 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
, NULL
);
1003 /* If there were any stages set with programs, use ctx->Shader as the
1004 * current shader state. Otherwise, use Pipeline.Default. The pipeline
1005 * hasn't been restored yet, and that may modify ctx->_Shader further.
1008 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
1011 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
1012 ctx
->Pipeline
.Default
);
1014 if (save
->Pipeline
) {
1015 _mesa_bind_pipeline(ctx
, save
->Pipeline
);
1017 _mesa_reference_pipeline_object(ctx
, &save
->Pipeline
, NULL
);
1020 _mesa_update_vertex_processing_mode(ctx
);
1023 if (state
& MESA_META_STENCIL_TEST
) {
1024 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
1026 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
1027 _mesa_ClearStencil(stencil
->Clear
);
1028 if (ctx
->Extensions
.EXT_stencil_two_side
) {
1029 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
1030 stencil
->TestTwoSide
);
1031 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
1032 ? GL_BACK
: GL_FRONT
);
1035 _mesa_StencilFuncSeparate(GL_FRONT
,
1036 stencil
->Function
[0],
1038 stencil
->ValueMask
[0]);
1039 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
1040 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
1041 stencil
->ZFailFunc
[0],
1042 stencil
->ZPassFunc
[0]);
1044 _mesa_StencilFuncSeparate(GL_BACK
,
1045 stencil
->Function
[1],
1047 stencil
->ValueMask
[1]);
1048 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
1049 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
1050 stencil
->ZFailFunc
[1],
1051 stencil
->ZPassFunc
[1]);
1054 if (state
& MESA_META_TEXTURE
) {
1057 assert(ctx
->Texture
.CurrentUnit
== 0);
1059 /* restore texenv for unit[0] */
1060 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
1062 /* restore texture objects for unit[0] only */
1063 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
1064 if (ctx
->Texture
.Unit
[0].CurrentTex
[tgt
] != save
->CurrentTexture
[tgt
]) {
1065 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1066 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
1067 save
->CurrentTexture
[tgt
]);
1069 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
1072 /* Restore fixed function texture enables, texgen */
1073 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
1074 if (ctx
->Texture
.FixedFuncUnit
[u
].Enabled
!= save
->TexEnabled
[u
]) {
1075 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1076 ctx
->Texture
.FixedFuncUnit
[u
].Enabled
= save
->TexEnabled
[u
];
1079 if (ctx
->Texture
.FixedFuncUnit
[u
].TexGenEnabled
!= save
->TexGenEnabled
[u
]) {
1080 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1081 ctx
->Texture
.FixedFuncUnit
[u
].TexGenEnabled
= save
->TexGenEnabled
[u
];
1085 /* restore current unit state */
1086 _mesa_ActiveTexture(GL_TEXTURE0
+ save
->ActiveUnit
);
1089 if (state
& MESA_META_TRANSFORM
) {
1090 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
1091 _mesa_ActiveTexture(GL_TEXTURE0
);
1092 _mesa_MatrixMode(GL_TEXTURE
);
1093 _mesa_LoadMatrixf(save
->TextureMatrix
);
1094 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
1096 _mesa_MatrixMode(GL_MODELVIEW
);
1097 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
1099 _mesa_MatrixMode(GL_PROJECTION
);
1100 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
1102 _mesa_MatrixMode(save
->MatrixMode
);
1104 if (ctx
->Extensions
.ARB_clip_control
)
1105 _mesa_ClipControl(save
->ClipOrigin
, save
->ClipDepthMode
);
1108 if (state
& MESA_META_CLIP
) {
1109 GLbitfield mask
= save
->ClipPlanesEnabled
;
1111 const int i
= u_bit_scan(&mask
);
1112 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
1116 if (state
& MESA_META_VERTEX
) {
1117 /* restore vertex array object */
1118 _mesa_BindVertexArray(save
->VAO
->Name
);
1119 _mesa_reference_vao(ctx
, &save
->VAO
, NULL
);
1122 if (state
& MESA_META_VIEWPORT
) {
1123 if (save
->ViewportX
!= ctx
->ViewportArray
[0].X
||
1124 save
->ViewportY
!= ctx
->ViewportArray
[0].Y
||
1125 save
->ViewportW
!= ctx
->ViewportArray
[0].Width
||
1126 save
->ViewportH
!= ctx
->ViewportArray
[0].Height
) {
1127 _mesa_set_viewport(ctx
, 0, save
->ViewportX
, save
->ViewportY
,
1128 save
->ViewportW
, save
->ViewportH
);
1130 _mesa_set_depth_range(ctx
, 0, save
->DepthNear
, save
->DepthFar
);
1133 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
&&
1134 ctx
->Extensions
.ARB_color_buffer_float
) {
1135 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
1138 if (state
& MESA_META_CLAMP_VERTEX_COLOR
&&
1139 ctx
->Extensions
.ARB_color_buffer_float
) {
1140 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
1143 if (state
& MESA_META_CONDITIONAL_RENDER
) {
1144 if (save
->CondRenderQuery
)
1145 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
1146 save
->CondRenderMode
);
1149 if (state
& MESA_META_SELECT_FEEDBACK
) {
1150 if (save
->RenderMode
== GL_SELECT
) {
1151 _mesa_RenderMode(GL_SELECT
);
1152 ctx
->Select
= save
->Select
;
1153 } else if (save
->RenderMode
== GL_FEEDBACK
) {
1154 _mesa_RenderMode(GL_FEEDBACK
);
1155 ctx
->Feedback
= save
->Feedback
;
1159 if (state
& MESA_META_MULTISAMPLE
) {
1160 struct gl_multisample_attrib
*ctx_ms
= &ctx
->Multisample
;
1161 struct gl_multisample_attrib
*save_ms
= &save
->Multisample
;
1163 if (ctx_ms
->Enabled
!= save_ms
->Enabled
)
1164 _mesa_set_multisample(ctx
, save_ms
->Enabled
);
1165 if (ctx_ms
->SampleCoverage
!= save_ms
->SampleCoverage
)
1166 _mesa_set_enable(ctx
, GL_SAMPLE_COVERAGE
, save_ms
->SampleCoverage
);
1167 if (ctx_ms
->SampleAlphaToCoverage
!= save_ms
->SampleAlphaToCoverage
)
1168 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_COVERAGE
, save_ms
->SampleAlphaToCoverage
);
1169 if (ctx_ms
->SampleAlphaToOne
!= save_ms
->SampleAlphaToOne
)
1170 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_ONE
, save_ms
->SampleAlphaToOne
);
1171 if (ctx_ms
->SampleCoverageValue
!= save_ms
->SampleCoverageValue
||
1172 ctx_ms
->SampleCoverageInvert
!= save_ms
->SampleCoverageInvert
) {
1173 _mesa_SampleCoverage(save_ms
->SampleCoverageValue
,
1174 save_ms
->SampleCoverageInvert
);
1176 if (ctx_ms
->SampleShading
!= save_ms
->SampleShading
)
1177 _mesa_set_enable(ctx
, GL_SAMPLE_SHADING
, save_ms
->SampleShading
);
1178 if (ctx_ms
->SampleMask
!= save_ms
->SampleMask
)
1179 _mesa_set_enable(ctx
, GL_SAMPLE_MASK
, save_ms
->SampleMask
);
1180 if (ctx_ms
->SampleMaskValue
!= save_ms
->SampleMaskValue
)
1181 _mesa_SampleMaski(0, save_ms
->SampleMaskValue
);
1182 if (ctx_ms
->MinSampleShadingValue
!= save_ms
->MinSampleShadingValue
)
1183 _mesa_MinSampleShading(save_ms
->MinSampleShadingValue
);
1186 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
1187 if (ctx
->Color
.sRGBEnabled
!= save
->sRGBEnabled
)
1188 _mesa_set_framebuffer_srgb(ctx
, save
->sRGBEnabled
);
1192 if (save
->Lighting
) {
1193 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
1195 if (save
->RasterDiscard
) {
1196 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_TRUE
);
1198 if (save
->TransformFeedbackNeedsResume
)
1199 _mesa_ResumeTransformFeedback();
1201 _mesa_bind_framebuffers(ctx
, save
->DrawBuffer
, save
->ReadBuffer
);
1202 _mesa_reference_framebuffer(&save
->DrawBuffer
, NULL
);
1203 _mesa_reference_framebuffer(&save
->ReadBuffer
, NULL
);
1205 if (state
& MESA_META_DRAW_BUFFERS
) {
1206 _mesa_drawbuffers(ctx
, ctx
->DrawBuffer
, ctx
->Const
.MaxDrawBuffers
,
1207 save
->ColorDrawBuffers
, NULL
);
1210 ctx
->Meta
->SaveStackDepth
--;
1212 ctx
->API
= save
->API
;
1213 ctx
->Extensions
.Version
= save
->ExtensionsVersion
;
1218 * Convert Z from a normalized value in the range [0, 1] to an object-space
1219 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1220 * default/identity ortho projection results in the original Z value.
1221 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1222 * value comes from the clear value or raster position.
1224 static inline GLfloat
1225 invert_z(GLfloat normZ
)
1227 GLfloat objZ
= 1.0f
- 2.0f
* normZ
;
1233 * One-time init for a temp_texture object.
1234 * Choose tex target, compute max tex size, etc.
1237 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1239 /* prefer texture rectangle */
1240 if (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.NV_texture_rectangle
) {
1241 tex
->Target
= GL_TEXTURE_RECTANGLE
;
1242 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
1243 tex
->NPOT
= GL_TRUE
;
1246 /* use 2D texture, NPOT if possible */
1247 tex
->Target
= GL_TEXTURE_2D
;
1248 tex
->MaxSize
= ctx
->Const
.MaxTextureSize
;
1249 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
1251 tex
->MinSize
= 16; /* 16 x 16 at least */
1252 assert(tex
->MaxSize
> 0);
1254 tex
->tex_obj
= ctx
->Driver
.NewTextureObject(ctx
, 0xDEADBEEF, tex
->Target
);
1258 cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1260 _mesa_delete_nameless_texture(ctx
, tex
->tex_obj
);
1261 tex
->tex_obj
= NULL
;
1266 * Return pointer to temp_texture info for non-bitmap ops.
1267 * This does some one-time init if needed.
1269 struct temp_texture
*
1270 _mesa_meta_get_temp_texture(struct gl_context
*ctx
)
1272 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
1274 if (tex
->tex_obj
== NULL
) {
1275 init_temp_texture(ctx
, tex
);
1283 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1284 * We use a separate texture for bitmaps to reduce texture
1285 * allocation/deallocation.
1287 static struct temp_texture
*
1288 get_bitmap_temp_texture(struct gl_context
*ctx
)
1290 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
1292 if (tex
->tex_obj
== NULL
) {
1293 init_temp_texture(ctx
, tex
);
1300 * Return pointer to depth temp_texture.
1301 * This does some one-time init if needed.
1303 struct temp_texture
*
1304 _mesa_meta_get_temp_depth_texture(struct gl_context
*ctx
)
1306 struct temp_texture
*tex
= &ctx
->Meta
->Blit
.depthTex
;
1308 if (tex
->tex_obj
== NULL
) {
1309 init_temp_texture(ctx
, tex
);
1316 * Compute the width/height of texture needed to draw an image of the
1317 * given size. Return a flag indicating whether the current texture
1318 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1319 * allocated (glTexImage2D).
1320 * Also, compute s/t texcoords for drawing.
1322 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1325 _mesa_meta_alloc_texture(struct temp_texture
*tex
,
1326 GLsizei width
, GLsizei height
, GLenum intFormat
)
1328 GLboolean newTex
= GL_FALSE
;
1330 assert(width
<= tex
->MaxSize
);
1331 assert(height
<= tex
->MaxSize
);
1333 if (width
> tex
->Width
||
1334 height
> tex
->Height
||
1335 intFormat
!= tex
->IntFormat
) {
1336 /* alloc new texture (larger or different format) */
1339 /* use non-power of two size */
1340 tex
->Width
= MAX2(tex
->MinSize
, width
);
1341 tex
->Height
= MAX2(tex
->MinSize
, height
);
1344 /* find power of two size */
1346 w
= h
= tex
->MinSize
;
1355 tex
->IntFormat
= intFormat
;
1360 /* compute texcoords */
1361 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1362 tex
->Sright
= (GLfloat
) width
;
1363 tex
->Ttop
= (GLfloat
) height
;
1366 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1367 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1375 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1378 _mesa_meta_setup_copypix_texture(struct gl_context
*ctx
,
1379 struct temp_texture
*tex
,
1380 GLint srcX
, GLint srcY
,
1381 GLsizei width
, GLsizei height
,
1387 _mesa_bind_texture(ctx
, tex
->Target
, tex
->tex_obj
);
1388 _mesa_texture_parameteriv(ctx
, tex
->tex_obj
, GL_TEXTURE_MIN_FILTER
,
1389 (GLint
*) &filter
, false);
1390 _mesa_texture_parameteriv(ctx
, tex
->tex_obj
, GL_TEXTURE_MAG_FILTER
,
1391 (GLint
*) &filter
, false);
1392 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1394 newTex
= _mesa_meta_alloc_texture(tex
, width
, height
, intFormat
);
1396 /* copy framebuffer image to texture */
1398 /* create new tex image */
1399 if (tex
->Width
== width
&& tex
->Height
== height
) {
1400 /* create new tex with framebuffer data */
1401 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1402 srcX
, srcY
, width
, height
, 0);
1405 /* create empty texture */
1406 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1407 tex
->Width
, tex
->Height
, 0,
1408 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1410 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1411 0, 0, srcX
, srcY
, width
, height
);
1415 /* replace existing tex image */
1416 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1417 0, 0, srcX
, srcY
, width
, height
);
1423 * Setup/load texture for glDrawPixels.
1426 _mesa_meta_setup_drawpix_texture(struct gl_context
*ctx
,
1427 struct temp_texture
*tex
,
1429 GLsizei width
, GLsizei height
,
1430 GLenum format
, GLenum type
,
1431 const GLvoid
*pixels
)
1433 /* GLint so the compiler won't complain about type signedness mismatch in
1434 * the call to _mesa_texture_parameteriv below.
1436 static const GLint filter
= GL_NEAREST
;
1438 _mesa_bind_texture(ctx
, tex
->Target
, tex
->tex_obj
);
1439 _mesa_texture_parameteriv(ctx
, tex
->tex_obj
, GL_TEXTURE_MIN_FILTER
, &filter
,
1441 _mesa_texture_parameteriv(ctx
, tex
->tex_obj
, GL_TEXTURE_MAG_FILTER
, &filter
,
1443 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1445 /* copy pixel data to texture */
1447 /* create new tex image */
1448 if (tex
->Width
== width
&& tex
->Height
== height
) {
1449 /* create new tex and load image data */
1450 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1451 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1454 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1456 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1457 ctx
->Unpack
.BufferObj
);
1458 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1459 /* create empty texture */
1460 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1461 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1462 if (save_unpack_obj
!= NULL
)
1463 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
,
1464 save_unpack_obj
->Name
);
1466 _mesa_TexSubImage2D(tex
->Target
, 0,
1467 0, 0, width
, height
, format
, type
, pixels
);
1471 /* replace existing tex image */
1472 _mesa_TexSubImage2D(tex
->Target
, 0,
1473 0, 0, width
, height
, format
, type
, pixels
);
1478 _mesa_meta_setup_ff_tnl_for_blit(struct gl_context
*ctx
,
1479 GLuint
*VAO
, struct gl_buffer_object
**buf_obj
,
1480 unsigned texcoord_size
)
1482 _mesa_meta_setup_vertex_objects(ctx
, VAO
, buf_obj
, false, 2, texcoord_size
,
1485 /* setup projection matrix */
1486 _mesa_MatrixMode(GL_PROJECTION
);
1487 _mesa_LoadIdentity();
1491 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1494 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1496 meta_clear(ctx
, buffers
, false);
1500 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1502 meta_clear(ctx
, buffers
, true);
1506 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
1508 const char *vs_source
=
1509 "#extension GL_AMD_vertex_shader_layer : enable\n"
1510 "#extension GL_ARB_draw_instanced : enable\n"
1511 "#extension GL_ARB_explicit_attrib_location :enable\n"
1512 "layout(location = 0) in vec4 position;\n"
1515 "#ifdef GL_AMD_vertex_shader_layer\n"
1516 " gl_Layer = gl_InstanceID;\n"
1518 " gl_Position = position;\n"
1520 const char *fs_source
=
1521 "#extension GL_ARB_explicit_attrib_location :enable\n"
1522 "#extension GL_ARB_explicit_uniform_location :enable\n"
1523 "layout(location = 0) uniform vec4 color;\n"
1526 " gl_FragColor = color;\n"
1528 bool has_integer_textures
;
1530 _mesa_meta_setup_vertex_objects(ctx
, &clear
->VAO
, &clear
->buf_obj
, true,
1533 if (clear
->ShaderProg
!= 0)
1536 _mesa_meta_compile_and_link_program(ctx
, vs_source
, fs_source
, "meta clear",
1537 &clear
->ShaderProg
);
1539 has_integer_textures
= _mesa_is_gles3(ctx
) ||
1540 (_mesa_is_desktop_gl(ctx
) && ctx
->Const
.GLSLVersion
>= 130);
1542 if (has_integer_textures
) {
1543 void *shader_source_mem_ctx
= ralloc_context(NULL
);
1544 const char *vs_int_source
=
1545 ralloc_asprintf(shader_source_mem_ctx
,
1547 "#extension GL_AMD_vertex_shader_layer : enable\n"
1548 "#extension GL_ARB_draw_instanced : enable\n"
1549 "#extension GL_ARB_explicit_attrib_location :enable\n"
1550 "layout(location = 0) in vec4 position;\n"
1553 "#ifdef GL_AMD_vertex_shader_layer\n"
1554 " gl_Layer = gl_InstanceID;\n"
1556 " gl_Position = position;\n"
1558 const char *fs_int_source
=
1559 ralloc_asprintf(shader_source_mem_ctx
,
1561 "#extension GL_ARB_explicit_attrib_location :enable\n"
1562 "#extension GL_ARB_explicit_uniform_location :enable\n"
1563 "layout(location = 0) uniform ivec4 color;\n"
1564 "out ivec4 out_color;\n"
1568 " out_color = color;\n"
1571 _mesa_meta_compile_and_link_program(ctx
, vs_int_source
, fs_int_source
,
1573 &clear
->IntegerShaderProg
);
1574 ralloc_free(shader_source_mem_ctx
);
1576 /* Note that user-defined out attributes get automatically assigned
1577 * locations starting from 0, so we don't need to explicitly
1578 * BindFragDataLocation to 0.
1584 meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
)
1586 if (clear
->VAO
== 0)
1588 _mesa_DeleteVertexArrays(1, &clear
->VAO
);
1590 _mesa_reference_buffer_object(ctx
, &clear
->buf_obj
, NULL
);
1591 _mesa_reference_shader_program(ctx
, &clear
->ShaderProg
, NULL
);
1593 if (clear
->IntegerShaderProg
) {
1594 _mesa_reference_shader_program(ctx
, &clear
->IntegerShaderProg
, NULL
);
1599 * Given a bitfield of BUFFER_BIT_x draw buffers, call glDrawBuffers to
1600 * set GL to only draw to those buffers.
1602 * Since the bitfield has no associated order, the assignment of draw buffer
1603 * indices to color attachment indices is rather arbitrary.
1606 _mesa_meta_drawbuffers_from_bitfield(GLbitfield bits
)
1608 GLenum enums
[MAX_DRAW_BUFFERS
];
1612 /* This function is only legal for color buffer bitfields. */
1613 assert((bits
& ~BUFFER_BITS_COLOR
) == 0);
1615 /* Make sure we don't overflow any arrays. */
1616 assert(util_bitcount(bits
) <= MAX_DRAW_BUFFERS
);
1620 if (bits
& BUFFER_BIT_FRONT_LEFT
)
1621 enums
[i
++] = GL_FRONT_LEFT
;
1623 if (bits
& BUFFER_BIT_FRONT_RIGHT
)
1624 enums
[i
++] = GL_FRONT_RIGHT
;
1626 if (bits
& BUFFER_BIT_BACK_LEFT
)
1627 enums
[i
++] = GL_BACK_LEFT
;
1629 if (bits
& BUFFER_BIT_BACK_RIGHT
)
1630 enums
[i
++] = GL_BACK_RIGHT
;
1632 for (n
= 0; n
< MAX_COLOR_ATTACHMENTS
; n
++) {
1633 if (bits
& (1 << (BUFFER_COLOR0
+ n
)))
1634 enums
[i
++] = GL_COLOR_ATTACHMENT0
+ n
;
1637 _mesa_DrawBuffers(i
, enums
);
1641 * Given a bitfield of BUFFER_BIT_x draw buffers, call glDrawBuffers to
1642 * set GL to only draw to those buffers. Also, update color masks to
1643 * reflect the new draw buffer ordering.
1646 _mesa_meta_drawbuffers_and_colormask(struct gl_context
*ctx
, GLbitfield mask
)
1648 GLenum enums
[MAX_DRAW_BUFFERS
];
1649 GLubyte colormask
[MAX_DRAW_BUFFERS
][4];
1652 /* This function is only legal for color buffer bitfields. */
1653 assert((mask
& ~BUFFER_BITS_COLOR
) == 0);
1655 /* Make sure we don't overflow any arrays. */
1656 assert(util_bitcount(mask
) <= MAX_DRAW_BUFFERS
);
1660 for (int i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
1661 gl_buffer_index b
= ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[i
];
1662 int colormask_idx
= ctx
->Extensions
.EXT_draw_buffers2
? i
: 0;
1664 if (b
< 0 || !(mask
& (1 << b
)) ||
1665 GET_COLORMASK(ctx
->Color
.ColorMask
, colormask_idx
) == 0)
1669 case BUFFER_FRONT_LEFT
:
1670 enums
[num_bufs
] = GL_FRONT_LEFT
;
1672 case BUFFER_FRONT_RIGHT
:
1673 enums
[num_bufs
] = GL_FRONT_RIGHT
;
1675 case BUFFER_BACK_LEFT
:
1676 enums
[num_bufs
] = GL_BACK_LEFT
;
1678 case BUFFER_BACK_RIGHT
:
1679 enums
[num_bufs
] = GL_BACK_RIGHT
;
1682 assert(b
>= BUFFER_COLOR0
&& b
<= BUFFER_COLOR7
);
1683 enums
[num_bufs
] = GL_COLOR_ATTACHMENT0
+ (b
- BUFFER_COLOR0
);
1687 for (int k
= 0; k
< 4; k
++)
1688 colormask
[num_bufs
][k
] = GET_COLORMASK_BIT(ctx
->Color
.ColorMask
,
1694 _mesa_DrawBuffers(num_bufs
, enums
);
1696 for (int i
= 0; i
< num_bufs
; i
++) {
1697 _mesa_ColorMaski(i
, colormask
[i
][0], colormask
[i
][1],
1698 colormask
[i
][2], colormask
[i
][3]);
1704 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1707 meta_clear(struct gl_context
*ctx
, GLbitfield buffers
, bool glsl
)
1709 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1710 GLbitfield metaSave
;
1711 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1712 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1713 float x0
, y0
, x1
, y1
, z
;
1714 struct vertex verts
[4];
1717 metaSave
= (MESA_META_ALPHA_TEST
|
1719 MESA_META_COLOR_MASK
|
1720 MESA_META_DEPTH_TEST
|
1721 MESA_META_RASTERIZATION
|
1723 MESA_META_STENCIL_TEST
|
1725 MESA_META_VIEWPORT
|
1727 MESA_META_CLAMP_FRAGMENT_COLOR
|
1728 MESA_META_MULTISAMPLE
|
1729 MESA_META_OCCLUSION_QUERY
);
1732 metaSave
|= MESA_META_FOG
|
1733 MESA_META_PIXEL_TRANSFER
|
1734 MESA_META_TRANSFORM
|
1736 MESA_META_CLAMP_VERTEX_COLOR
|
1737 MESA_META_SELECT_FEEDBACK
;
1740 if (buffers
& BUFFER_BITS_COLOR
) {
1741 metaSave
|= MESA_META_DRAW_BUFFERS
;
1744 _mesa_meta_begin(ctx
, metaSave
);
1747 meta_glsl_clear_init(ctx
, clear
);
1749 x0
= ((float) fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
1750 y0
= ((float) fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
1751 x1
= ((float) fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
1752 y1
= ((float) fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
1753 z
= -invert_z(ctx
->Depth
.Clear
);
1755 _mesa_meta_setup_vertex_objects(ctx
, &clear
->VAO
, &clear
->buf_obj
, false,
1758 x0
= (float) fb
->_Xmin
;
1759 y0
= (float) fb
->_Ymin
;
1760 x1
= (float) fb
->_Xmax
;
1761 y1
= (float) fb
->_Ymax
;
1762 z
= invert_z(ctx
->Depth
.Clear
);
1765 if (fb
->_IntegerBuffers
) {
1767 _mesa_meta_use_program(ctx
, clear
->IntegerShaderProg
);
1768 _mesa_Uniform4iv(0, 1, ctx
->Color
.ClearColor
.i
);
1770 _mesa_meta_use_program(ctx
, clear
->ShaderProg
);
1771 _mesa_Uniform4fv(0, 1, ctx
->Color
.ClearColor
.f
);
1774 /* GL_COLOR_BUFFER_BIT */
1775 if (buffers
& BUFFER_BITS_COLOR
) {
1776 /* Only draw to the buffers we were asked to clear. */
1777 _mesa_meta_drawbuffers_and_colormask(ctx
, buffers
& BUFFER_BITS_COLOR
);
1779 /* leave colormask state as-is */
1781 /* Clears never have the color clamped. */
1782 if (ctx
->Extensions
.ARB_color_buffer_float
)
1783 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1786 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1789 /* GL_DEPTH_BUFFER_BIT */
1790 if (buffers
& BUFFER_BIT_DEPTH
) {
1791 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1792 _mesa_DepthFunc(GL_ALWAYS
);
1793 _mesa_DepthMask(GL_TRUE
);
1796 assert(!ctx
->Depth
.Test
);
1799 /* GL_STENCIL_BUFFER_BIT */
1800 if (buffers
& BUFFER_BIT_STENCIL
) {
1801 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1802 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1803 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1804 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1805 ctx
->Stencil
.Clear
& stencilMax
,
1806 ctx
->Stencil
.WriteMask
[0]);
1809 assert(!ctx
->Stencil
.Enabled
);
1812 /* vertex positions */
1827 for (i
= 0; i
< 4; i
++) {
1828 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
1829 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
1830 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
1831 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
1835 /* upload new vertex data */
1836 _mesa_buffer_data(ctx
, clear
->buf_obj
, GL_NONE
, sizeof(verts
), verts
,
1837 GL_DYNAMIC_DRAW
, __func__
);
1840 if (fb
->MaxNumLayers
> 0) {
1841 _mesa_DrawArraysInstanced(GL_TRIANGLE_FAN
, 0, 4, fb
->MaxNumLayers
);
1843 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1846 _mesa_meta_end(ctx
);
1850 * Meta implementation of ctx->Driver.CopyPixels() in terms
1851 * of texture mapping and polygon rendering and GLSL shaders.
1854 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
1855 GLsizei width
, GLsizei height
,
1856 GLint dstX
, GLint dstY
, GLenum type
)
1858 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1859 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
1860 struct vertex verts
[4];
1862 if (type
!= GL_COLOR
||
1863 ctx
->_ImageTransferState
||
1865 width
> tex
->MaxSize
||
1866 height
> tex
->MaxSize
) {
1867 /* XXX avoid this fallback */
1868 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1872 /* Most GL state applies to glCopyPixels, but a there's a few things
1873 * we need to override:
1875 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
1878 MESA_META_TRANSFORM
|
1881 MESA_META_VIEWPORT
));
1883 _mesa_meta_setup_vertex_objects(ctx
, ©pix
->VAO
, ©pix
->buf_obj
, false,
1886 /* Silence valgrind warnings about reading uninitialized stack. */
1887 memset(verts
, 0, sizeof(verts
));
1889 /* Alloc/setup texture */
1890 _mesa_meta_setup_copypix_texture(ctx
, tex
, srcX
, srcY
, width
, height
,
1891 GL_RGBA
, GL_NEAREST
);
1893 /* vertex positions, texcoords (after texture allocation!) */
1895 const GLfloat dstX0
= (GLfloat
) dstX
;
1896 const GLfloat dstY0
= (GLfloat
) dstY
;
1897 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1898 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1899 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1904 verts
[0].tex
[0] = 0.0F
;
1905 verts
[0].tex
[1] = 0.0F
;
1909 verts
[1].tex
[0] = tex
->Sright
;
1910 verts
[1].tex
[1] = 0.0F
;
1914 verts
[2].tex
[0] = tex
->Sright
;
1915 verts
[2].tex
[1] = tex
->Ttop
;
1919 verts
[3].tex
[0] = 0.0F
;
1920 verts
[3].tex
[1] = tex
->Ttop
;
1922 /* upload new vertex data */
1923 _mesa_buffer_sub_data(ctx
, copypix
->buf_obj
, 0, sizeof(verts
), verts
);
1926 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1928 /* draw textured quad */
1929 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1931 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1933 _mesa_meta_end(ctx
);
1937 meta_drawpix_cleanup(struct gl_context
*ctx
, struct drawpix_state
*drawpix
)
1939 if (drawpix
->VAO
!= 0) {
1940 _mesa_DeleteVertexArrays(1, &drawpix
->VAO
);
1943 _mesa_reference_buffer_object(ctx
, &drawpix
->buf_obj
, NULL
);
1946 if (drawpix
->StencilFP
!= 0) {
1947 _mesa_DeleteProgramsARB(1, &drawpix
->StencilFP
);
1948 drawpix
->StencilFP
= 0;
1951 if (drawpix
->DepthFP
!= 0) {
1952 _mesa_DeleteProgramsARB(1, &drawpix
->DepthFP
);
1953 drawpix
->DepthFP
= 0;
1958 * When the glDrawPixels() image size is greater than the max rectangle
1959 * texture size we use this function to break the glDrawPixels() image
1960 * into tiles which fit into the max texture size.
1963 tiled_draw_pixels(struct gl_context
*ctx
,
1965 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1966 GLenum format
, GLenum type
,
1967 const struct gl_pixelstore_attrib
*unpack
,
1968 const GLvoid
*pixels
)
1970 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
1973 if (tileUnpack
.RowLength
== 0)
1974 tileUnpack
.RowLength
= width
;
1976 for (i
= 0; i
< width
; i
+= tileSize
) {
1977 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
1978 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
1980 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
1982 for (j
= 0; j
< height
; j
+= tileSize
) {
1983 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
1984 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
1986 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
1988 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
1989 format
, type
, &tileUnpack
, pixels
);
1996 * One-time init for drawing stencil pixels.
1999 init_draw_stencil_pixels(struct gl_context
*ctx
)
2001 /* This program is run eight times, once for each stencil bit.
2002 * The stencil values to draw are found in an 8-bit alpha texture.
2003 * We read the texture/stencil value and test if bit 'b' is set.
2004 * If the bit is not set, use KIL to kill the fragment.
2005 * Finally, we use the stencil test to update the stencil buffer.
2007 * The basic algorithm for checking if a bit is set is:
2008 * if (is_odd(value / (1 << bit)))
2009 * result is one (or non-zero).
2012 * The program parameter contains three values:
2013 * parm.x = 255 / (1 << bit)
2017 static const char *program
=
2019 "PARAM parm = program.local[0]; \n"
2021 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
2022 "# t = t * 255 / bit \n"
2023 "MUL t.x, t.a, parm.x; \n"
2026 "SUB t.x, t.x, t.y; \n"
2028 "MUL t.x, t.x, parm.y; \n"
2029 "# t = fract(t.x) \n"
2030 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2031 "# t.x = (t.x == 0 ? 1 : 0) \n"
2032 "SGE t.x, -t.x, parm.z; \n"
2034 "# for debug only \n"
2035 "#MOV result.color, t.x; \n"
2037 char program2
[1000];
2038 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2039 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
2040 const char *texTarget
;
2042 assert(drawpix
->StencilFP
== 0);
2044 /* replace %s with "RECT" or "2D" */
2045 assert(strlen(program
) + 4 < sizeof(program2
));
2046 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2050 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2052 _mesa_GenProgramsARB(1, &drawpix
->StencilFP
);
2053 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2054 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2055 strlen(program2
), (const GLubyte
*) program2
);
2060 * One-time init for drawing depth pixels.
2063 init_draw_depth_pixels(struct gl_context
*ctx
)
2065 static const char *program
=
2067 "PARAM color = program.local[0]; \n"
2068 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2069 "MOV result.color, color; \n"
2072 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2073 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
2074 const char *texTarget
;
2076 assert(drawpix
->DepthFP
== 0);
2078 /* replace %s with "RECT" or "2D" */
2079 assert(strlen(program
) + 4 < sizeof(program2
));
2080 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2084 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2086 _mesa_GenProgramsARB(1, &drawpix
->DepthFP
);
2087 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2088 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2089 strlen(program2
), (const GLubyte
*) program2
);
2094 * Meta implementation of ctx->Driver.DrawPixels() in terms
2095 * of texture mapping and polygon rendering.
2098 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2099 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2100 GLenum format
, GLenum type
,
2101 const struct gl_pixelstore_attrib
*unpack
,
2102 const GLvoid
*pixels
)
2104 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2105 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
2106 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2107 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2108 struct vertex verts
[4];
2109 GLenum texIntFormat
;
2110 GLboolean fallback
, newTex
;
2111 GLbitfield metaExtraSave
= 0x0;
2114 * Determine if we can do the glDrawPixels with texture mapping.
2116 fallback
= GL_FALSE
;
2117 if (ctx
->Fog
.Enabled
) {
2121 if (_mesa_is_color_format(format
)) {
2122 /* use more compact format when possible */
2123 /* XXX disable special case for GL_LUMINANCE for now to work around
2124 * apparent i965 driver bug (see bug #23670).
2126 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2127 texIntFormat
= format
;
2129 texIntFormat
= GL_RGBA
;
2131 /* If we're not supposed to clamp the resulting color, then just
2132 * promote our texture to fully float. We could do better by
2133 * just going for the matching set of channels, in floating
2136 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2137 ctx
->Extensions
.ARB_texture_float
)
2138 texIntFormat
= GL_RGBA32F
;
2140 else if (_mesa_is_stencil_format(format
)) {
2141 if (ctx
->Extensions
.ARB_fragment_program
&&
2142 ctx
->Pixel
.IndexShift
== 0 &&
2143 ctx
->Pixel
.IndexOffset
== 0 &&
2144 type
== GL_UNSIGNED_BYTE
) {
2145 /* We'll store stencil as alpha. This only works for GLubyte
2146 * image data because of how incoming values are mapped to alpha
2149 texIntFormat
= GL_ALPHA
;
2150 metaExtraSave
= (MESA_META_COLOR_MASK
|
2151 MESA_META_DEPTH_TEST
|
2152 MESA_META_PIXEL_TRANSFER
|
2154 MESA_META_STENCIL_TEST
);
2160 else if (_mesa_is_depth_format(format
)) {
2161 if (ctx
->Extensions
.ARB_depth_texture
&&
2162 ctx
->Extensions
.ARB_fragment_program
) {
2163 texIntFormat
= GL_DEPTH_COMPONENT
;
2164 metaExtraSave
= (MESA_META_SHADER
);
2175 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2176 format
, type
, unpack
, pixels
);
2181 * Check image size against max texture size, draw as tiles if needed.
2183 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2184 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2185 format
, type
, unpack
, pixels
);
2189 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2190 * but a there's a few things we need to override:
2192 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2195 MESA_META_TRANSFORM
|
2198 MESA_META_VIEWPORT
|
2201 newTex
= _mesa_meta_alloc_texture(tex
, width
, height
, texIntFormat
);
2203 _mesa_meta_setup_vertex_objects(ctx
, &drawpix
->VAO
, &drawpix
->buf_obj
, false,
2206 /* Silence valgrind warnings about reading uninitialized stack. */
2207 memset(verts
, 0, sizeof(verts
));
2209 /* vertex positions, texcoords (after texture allocation!) */
2211 const GLfloat x0
= (GLfloat
) x
;
2212 const GLfloat y0
= (GLfloat
) y
;
2213 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2214 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2215 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2220 verts
[0].tex
[0] = 0.0F
;
2221 verts
[0].tex
[1] = 0.0F
;
2225 verts
[1].tex
[0] = tex
->Sright
;
2226 verts
[1].tex
[1] = 0.0F
;
2230 verts
[2].tex
[0] = tex
->Sright
;
2231 verts
[2].tex
[1] = tex
->Ttop
;
2235 verts
[3].tex
[0] = 0.0F
;
2236 verts
[3].tex
[1] = tex
->Ttop
;
2239 /* upload new vertex data */
2240 _mesa_buffer_data(ctx
, drawpix
->buf_obj
, GL_NONE
, sizeof(verts
), verts
,
2241 GL_DYNAMIC_DRAW
, __func__
);
2243 /* set given unpack params */
2244 ctx
->Unpack
= *unpack
;
2246 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2248 if (_mesa_is_stencil_format(format
)) {
2249 /* Drawing stencil */
2252 if (!drawpix
->StencilFP
)
2253 init_draw_stencil_pixels(ctx
);
2255 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2256 GL_ALPHA
, type
, pixels
);
2258 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2260 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2262 /* set all stencil bits to 0 */
2263 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2264 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2265 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2267 /* set stencil bits to 1 where needed */
2268 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2270 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2271 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2273 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2274 const GLuint mask
= 1 << bit
;
2275 if (mask
& origStencilMask
) {
2276 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2277 _mesa_StencilMask(mask
);
2279 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2280 255.0f
/ mask
, 0.5f
, 0.0f
, 0.0f
);
2282 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2286 else if (_mesa_is_depth_format(format
)) {
2288 if (!drawpix
->DepthFP
)
2289 init_draw_depth_pixels(ctx
);
2291 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2292 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2294 /* polygon color = current raster color */
2295 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2296 ctx
->Current
.RasterColor
);
2298 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2299 format
, type
, pixels
);
2301 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2305 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2306 format
, type
, pixels
);
2307 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2310 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2312 /* restore unpack params */
2313 ctx
->Unpack
= unpackSave
;
2315 _mesa_meta_end(ctx
);
2319 alpha_test_raster_color(struct gl_context
*ctx
)
2321 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2322 GLfloat ref
= ctx
->Color
.AlphaRef
;
2324 switch (ctx
->Color
.AlphaFunc
) {
2330 return alpha
== ref
;
2332 return alpha
<= ref
;
2336 return alpha
!= ref
;
2338 return alpha
>= ref
;
2348 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2349 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2350 * tracker would improve performance a lot.
2353 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2354 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2355 const struct gl_pixelstore_attrib
*unpack
,
2356 const GLubyte
*bitmap1
)
2358 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2359 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2360 const GLenum texIntFormat
= GL_ALPHA
;
2361 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2363 struct vertex verts
[4];
2368 * Check if swrast fallback is needed.
2370 if (ctx
->_ImageTransferState
||
2371 _mesa_arb_fragment_program_enabled(ctx
) ||
2373 ctx
->Texture
._MaxEnabledTexImageUnit
!= -1 ||
2374 width
> tex
->MaxSize
||
2375 height
> tex
->MaxSize
) {
2376 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2380 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2383 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2384 * but a there's a few things we need to override:
2386 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
2387 MESA_META_PIXEL_STORE
|
2388 MESA_META_RASTERIZATION
|
2391 MESA_META_TRANSFORM
|
2394 MESA_META_VIEWPORT
));
2396 _mesa_meta_setup_vertex_objects(ctx
, &bitmap
->VAO
, &bitmap
->buf_obj
, false,
2399 newTex
= _mesa_meta_alloc_texture(tex
, width
, height
, texIntFormat
);
2401 /* Silence valgrind warnings about reading uninitialized stack. */
2402 memset(verts
, 0, sizeof(verts
));
2404 /* vertex positions, texcoords, colors (after texture allocation!) */
2406 const GLfloat x0
= (GLfloat
) x
;
2407 const GLfloat y0
= (GLfloat
) y
;
2408 const GLfloat x1
= (GLfloat
) (x
+ width
);
2409 const GLfloat y1
= (GLfloat
) (y
+ height
);
2410 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2416 verts
[0].tex
[0] = 0.0F
;
2417 verts
[0].tex
[1] = 0.0F
;
2421 verts
[1].tex
[0] = tex
->Sright
;
2422 verts
[1].tex
[1] = 0.0F
;
2426 verts
[2].tex
[0] = tex
->Sright
;
2427 verts
[2].tex
[1] = tex
->Ttop
;
2431 verts
[3].tex
[0] = 0.0F
;
2432 verts
[3].tex
[1] = tex
->Ttop
;
2434 for (i
= 0; i
< 4; i
++) {
2435 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2436 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2437 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2438 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2441 /* upload new vertex data */
2442 _mesa_buffer_sub_data(ctx
, bitmap
->buf_obj
, 0, sizeof(verts
), verts
);
2445 /* choose different foreground/background alpha values */
2446 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2447 bg
= (fg
> 127 ? 0 : 255);
2449 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2451 _mesa_meta_end(ctx
);
2455 bitmap8
= malloc(width
* height
);
2457 memset(bitmap8
, bg
, width
* height
);
2458 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2459 bitmap8
, width
, fg
);
2461 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2463 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2464 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2466 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2467 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2469 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2471 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2476 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2478 _mesa_meta_end(ctx
);
2482 * Compute the texture coordinates for the four vertices of a quad for
2483 * drawing a 2D texture image or slice of a cube/3D texture. The offset
2484 * and width, height specify a sub-region of the 2D image.
2486 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2487 * \param slice slice of a 1D/2D array texture or 3D texture
2488 * \param xoffset X position of sub texture
2489 * \param yoffset Y position of sub texture
2490 * \param width width of the sub texture image
2491 * \param height height of the sub texture image
2492 * \param total_width total width of the texture image
2493 * \param total_height total height of the texture image
2494 * \param total_depth total depth of the texture image
2495 * \param coords0/1/2/3 returns the computed texcoords
2498 _mesa_meta_setup_texture_coords(GLenum faceTarget
,
2514 const float s0
= (float) xoffset
/ (float) total_width
;
2515 const float s1
= (float) (xoffset
+ width
) / (float) total_width
;
2516 const float t0
= (float) yoffset
/ (float) total_height
;
2517 const float t1
= (float) (yoffset
+ height
) / (float) total_height
;
2520 /* setup the reference texcoords */
2530 if (faceTarget
== GL_TEXTURE_CUBE_MAP_ARRAY
)
2531 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ slice
% 6;
2533 /* Currently all texture targets want the W component to be 1.0.
2540 switch (faceTarget
) {
2544 case GL_TEXTURE_2D_ARRAY
:
2545 if (faceTarget
== GL_TEXTURE_3D
) {
2546 assert(slice
< total_depth
);
2547 assert(total_depth
>= 1);
2548 r
= (slice
+ 0.5f
) / total_depth
;
2550 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
2554 coords0
[0] = st
[0][0]; /* s */
2555 coords0
[1] = st
[0][1]; /* t */
2556 coords0
[2] = r
; /* r */
2557 coords1
[0] = st
[1][0];
2558 coords1
[1] = st
[1][1];
2560 coords2
[0] = st
[2][0];
2561 coords2
[1] = st
[2][1];
2563 coords3
[0] = st
[3][0];
2564 coords3
[1] = st
[3][1];
2567 case GL_TEXTURE_RECTANGLE_ARB
:
2568 coords0
[0] = (float) xoffset
; /* s */
2569 coords0
[1] = (float) yoffset
; /* t */
2570 coords0
[2] = 0.0F
; /* r */
2571 coords1
[0] = (float) (xoffset
+ width
);
2572 coords1
[1] = (float) yoffset
;
2574 coords2
[0] = (float) (xoffset
+ width
);
2575 coords2
[1] = (float) (yoffset
+ height
);
2577 coords3
[0] = (float) xoffset
;
2578 coords3
[1] = (float) (yoffset
+ height
);
2581 case GL_TEXTURE_1D_ARRAY
:
2582 coords0
[0] = st
[0][0]; /* s */
2583 coords0
[1] = (float) slice
; /* t */
2584 coords0
[2] = 0.0F
; /* r */
2585 coords1
[0] = st
[1][0];
2586 coords1
[1] = (float) slice
;
2588 coords2
[0] = st
[2][0];
2589 coords2
[1] = (float) slice
;
2591 coords3
[0] = st
[3][0];
2592 coords3
[1] = (float) slice
;
2596 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2597 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2598 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2599 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2600 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2601 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2602 /* loop over quad verts */
2603 for (i
= 0; i
< 4; i
++) {
2604 /* Compute sc = +/-scale and tc = +/-scale.
2605 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2606 * though that can still sometimes happen with this scale factor...
2608 const GLfloat scale
= 0.9999f
;
2609 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2610 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2627 unreachable("not reached");
2630 coord
[3] = (float) (slice
/ 6);
2632 switch (faceTarget
) {
2633 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2638 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2643 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2648 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2653 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2658 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2669 assert(!"unexpected target in _mesa_meta_setup_texture_coords()");
2673 static struct blit_shader
*
2674 choose_blit_shader(GLenum target
, struct blit_shader_table
*table
)
2678 table
->sampler_1d
.type
= "sampler1D";
2679 table
->sampler_1d
.func
= "texture1D";
2680 table
->sampler_1d
.texcoords
= "texCoords.x";
2681 return &table
->sampler_1d
;
2683 table
->sampler_2d
.type
= "sampler2D";
2684 table
->sampler_2d
.func
= "texture2D";
2685 table
->sampler_2d
.texcoords
= "texCoords.xy";
2686 return &table
->sampler_2d
;
2687 case GL_TEXTURE_RECTANGLE
:
2688 table
->sampler_rect
.type
= "sampler2DRect";
2689 table
->sampler_rect
.func
= "texture2DRect";
2690 table
->sampler_rect
.texcoords
= "texCoords.xy";
2691 return &table
->sampler_rect
;
2693 /* Code for mipmap generation with 3D textures is not used yet.
2694 * It's a sw fallback.
2696 table
->sampler_3d
.type
= "sampler3D";
2697 table
->sampler_3d
.func
= "texture3D";
2698 table
->sampler_3d
.texcoords
= "texCoords.xyz";
2699 return &table
->sampler_3d
;
2700 case GL_TEXTURE_CUBE_MAP
:
2701 table
->sampler_cubemap
.type
= "samplerCube";
2702 table
->sampler_cubemap
.func
= "textureCube";
2703 table
->sampler_cubemap
.texcoords
= "texCoords.xyz";
2704 return &table
->sampler_cubemap
;
2705 case GL_TEXTURE_1D_ARRAY
:
2706 table
->sampler_1d_array
.type
= "sampler1DArray";
2707 table
->sampler_1d_array
.func
= "texture1DArray";
2708 table
->sampler_1d_array
.texcoords
= "texCoords.xy";
2709 return &table
->sampler_1d_array
;
2710 case GL_TEXTURE_2D_ARRAY
:
2711 table
->sampler_2d_array
.type
= "sampler2DArray";
2712 table
->sampler_2d_array
.func
= "texture2DArray";
2713 table
->sampler_2d_array
.texcoords
= "texCoords.xyz";
2714 return &table
->sampler_2d_array
;
2715 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2716 table
->sampler_cubemap_array
.type
= "samplerCubeArray";
2717 table
->sampler_cubemap_array
.func
= "textureCubeArray";
2718 table
->sampler_cubemap_array
.texcoords
= "texCoords.xyzw";
2719 return &table
->sampler_cubemap_array
;
2721 _mesa_problem(NULL
, "Unexpected texture target 0x%x in"
2722 " setup_texture_sampler()\n", target
);
2728 _mesa_meta_blit_shader_table_cleanup(struct gl_context
*ctx
,
2729 struct blit_shader_table
*table
)
2731 _mesa_reference_shader_program(ctx
, &table
->sampler_1d
.shader_prog
, NULL
);
2732 _mesa_reference_shader_program(ctx
, &table
->sampler_2d
.shader_prog
, NULL
);
2733 _mesa_reference_shader_program(ctx
, &table
->sampler_3d
.shader_prog
, NULL
);
2734 _mesa_reference_shader_program(ctx
, &table
->sampler_rect
.shader_prog
, NULL
);
2735 _mesa_reference_shader_program(ctx
, &table
->sampler_cubemap
.shader_prog
, NULL
);
2736 _mesa_reference_shader_program(ctx
, &table
->sampler_1d_array
.shader_prog
, NULL
);
2737 _mesa_reference_shader_program(ctx
, &table
->sampler_2d_array
.shader_prog
, NULL
);
2738 _mesa_reference_shader_program(ctx
, &table
->sampler_cubemap_array
.shader_prog
, NULL
);
2742 * Determine the GL data type to use for the temporary image read with
2743 * ReadPixels() and passed to Tex[Sub]Image().
2746 get_temp_image_type(struct gl_context
*ctx
, mesa_format format
)
2748 const GLenum baseFormat
= _mesa_get_format_base_format(format
);
2749 const GLenum datatype
= _mesa_get_format_datatype(format
);
2750 const GLint format_red_bits
= _mesa_get_format_bits(format
, GL_RED_BITS
);
2752 switch (baseFormat
) {
2759 case GL_LUMINANCE_ALPHA
:
2761 if (datatype
== GL_INT
|| datatype
== GL_UNSIGNED_INT
) {
2763 } else if (format_red_bits
<= 8) {
2764 return GL_UNSIGNED_BYTE
;
2765 } else if (format_red_bits
<= 16) {
2766 return GL_UNSIGNED_SHORT
;
2769 case GL_DEPTH_COMPONENT
:
2770 if (datatype
== GL_FLOAT
)
2773 return GL_UNSIGNED_INT
;
2774 case GL_DEPTH_STENCIL
:
2775 if (datatype
== GL_FLOAT
)
2776 return GL_FLOAT_32_UNSIGNED_INT_24_8_REV
;
2778 return GL_UNSIGNED_INT_24_8
;
2780 _mesa_problem(ctx
, "Unexpected format %d in get_temp_image_type()",
2787 * Attempts to wrap the destination texture in an FBO and use
2788 * glBlitFramebuffer() to implement glCopyTexSubImage().
2791 copytexsubimage_using_blit_framebuffer(struct gl_context
*ctx
,
2792 struct gl_texture_image
*texImage
,
2796 struct gl_renderbuffer
*rb
,
2798 GLsizei width
, GLsizei height
)
2800 struct gl_framebuffer
*drawFb
;
2801 bool success
= false;
2805 if (!ctx
->Extensions
.ARB_framebuffer_object
)
2808 drawFb
= ctx
->Driver
.NewFramebuffer(ctx
, 0xDEADBEEF);
2812 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_DRAW_BUFFERS
);
2813 _mesa_bind_framebuffers(ctx
, drawFb
, ctx
->ReadBuffer
);
2815 if (rb
->_BaseFormat
== GL_DEPTH_STENCIL
||
2816 rb
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2817 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
2818 GL_DEPTH_ATTACHMENT
,
2820 mask
= GL_DEPTH_BUFFER_BIT
;
2822 if (rb
->_BaseFormat
== GL_DEPTH_STENCIL
&&
2823 texImage
->_BaseFormat
== GL_DEPTH_STENCIL
) {
2824 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
2825 GL_STENCIL_ATTACHMENT
,
2827 mask
|= GL_STENCIL_BUFFER_BIT
;
2829 _mesa_DrawBuffer(GL_NONE
);
2831 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
2832 GL_COLOR_ATTACHMENT0
,
2834 mask
= GL_COLOR_BUFFER_BIT
;
2835 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0
);
2838 status
= _mesa_check_framebuffer_status(ctx
, ctx
->DrawBuffer
);
2839 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
2842 ctx
->Meta
->Blit
.no_ctsi_fallback
= true;
2844 /* Since we've bound a new draw framebuffer, we need to update
2845 * its derived state -- _Xmin, etc -- for BlitFramebuffer's clipping to
2848 _mesa_update_state(ctx
);
2850 /* We skip the core BlitFramebuffer checks for format consistency, which
2851 * are too strict for CopyTexImage. We know meta will be fine with format
2854 mask
= _mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
2856 x
+ width
, y
+ height
,
2858 xoffset
+ width
, yoffset
+ height
,
2860 ctx
->Meta
->Blit
.no_ctsi_fallback
= false;
2861 success
= mask
== 0x0;
2864 _mesa_reference_framebuffer(&drawFb
, NULL
);
2865 _mesa_meta_end(ctx
);
2870 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2871 * Have to be careful with locking and meta state for pixel transfer.
2874 _mesa_meta_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2875 struct gl_texture_image
*texImage
,
2876 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2877 struct gl_renderbuffer
*rb
,
2879 GLsizei width
, GLsizei height
)
2881 GLenum format
, type
;
2885 if (copytexsubimage_using_blit_framebuffer(ctx
,
2887 xoffset
, yoffset
, zoffset
,
2894 /* Choose format/type for temporary image buffer */
2895 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
2896 if (format
== GL_LUMINANCE
||
2897 format
== GL_LUMINANCE_ALPHA
||
2898 format
== GL_INTENSITY
) {
2899 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
2900 * temp image buffer because glReadPixels will do L=R+G+B which is
2901 * not what we want (should be L=R).
2906 type
= get_temp_image_type(ctx
, texImage
->TexFormat
);
2907 if (_mesa_is_format_integer_color(texImage
->TexFormat
)) {
2908 format
= _mesa_base_format_to_integer_format(format
);
2910 bpp
= _mesa_bytes_per_pixel(format
, type
);
2912 _mesa_problem(ctx
, "Bad bpp in _mesa_meta_CopyTexSubImage()");
2917 * Alloc image buffer (XXX could use a PBO)
2919 buf
= malloc(width
* height
* bpp
);
2921 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
2926 * Read image from framebuffer (disable pixel transfer ops)
2928 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
2929 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2930 format
, type
, &ctx
->Pack
, buf
);
2931 _mesa_meta_end(ctx
);
2933 _mesa_update_state(ctx
); /* to update pixel transfer state */
2936 * Store texture data (with pixel transfer ops)
2938 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
2940 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
2941 assert(yoffset
== 0);
2942 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
2943 xoffset
, zoffset
, 0, width
, 1, 1,
2944 format
, type
, buf
, &ctx
->Unpack
);
2946 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
2947 xoffset
, yoffset
, zoffset
, width
, height
, 1,
2948 format
, type
, buf
, &ctx
->Unpack
);
2951 _mesa_meta_end(ctx
);
2957 meta_decompress_fbo_cleanup(struct decompress_fbo_state
*decompress_fbo
)
2959 if (decompress_fbo
->fb
!= NULL
) {
2960 _mesa_reference_framebuffer(&decompress_fbo
->fb
, NULL
);
2961 _mesa_reference_renderbuffer(&decompress_fbo
->rb
, NULL
);
2964 memset(decompress_fbo
, 0, sizeof(*decompress_fbo
));
2968 meta_decompress_cleanup(struct gl_context
*ctx
,
2969 struct decompress_state
*decompress
)
2971 meta_decompress_fbo_cleanup(&decompress
->byteFBO
);
2972 meta_decompress_fbo_cleanup(&decompress
->floatFBO
);
2974 if (decompress
->VAO
!= 0) {
2975 _mesa_DeleteVertexArrays(1, &decompress
->VAO
);
2976 _mesa_reference_buffer_object(ctx
, &decompress
->buf_obj
, NULL
);
2979 _mesa_reference_sampler_object(ctx
, &decompress
->samp_obj
, NULL
);
2981 memset(decompress
, 0, sizeof(*decompress
));
2985 * Decompress a texture image by drawing a quad with the compressed
2986 * texture and reading the pixels out of the color buffer.
2987 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
2988 * \param destFormat format, ala glReadPixels
2989 * \param destType type, ala glReadPixels
2990 * \param dest destination buffer
2991 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
2994 decompress_texture_image(struct gl_context
*ctx
,
2995 struct gl_texture_image
*texImage
,
2997 GLint xoffset
, GLint yoffset
,
2998 GLsizei width
, GLsizei height
,
2999 GLenum destFormat
, GLenum destType
,
3002 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
3003 struct decompress_fbo_state
*decompress_fbo
;
3004 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3005 const GLenum target
= texObj
->Target
;
3008 struct vertex verts
[4];
3009 struct gl_sampler_object
*samp_obj_save
= NULL
;
3011 const bool use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
3012 ctx
->Extensions
.ARB_fragment_shader
;
3014 switch (_mesa_get_format_datatype(texImage
->TexFormat
)) {
3016 decompress_fbo
= &decompress
->floatFBO
;
3017 rbFormat
= GL_RGBA32F
;
3019 case GL_UNSIGNED_NORMALIZED
:
3020 decompress_fbo
= &decompress
->byteFBO
;
3028 assert(target
== GL_TEXTURE_3D
||
3029 target
== GL_TEXTURE_2D_ARRAY
||
3030 target
== GL_TEXTURE_CUBE_MAP_ARRAY
);
3035 case GL_TEXTURE_1D_ARRAY
:
3036 assert(!"No compressed 1D textures.");
3039 case GL_TEXTURE_CUBE_MAP_ARRAY
:
3040 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ (slice
% 6);
3043 case GL_TEXTURE_CUBE_MAP
:
3044 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
3048 faceTarget
= target
;
3052 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~(MESA_META_PIXEL_STORE
|
3053 MESA_META_DRAW_BUFFERS
));
3054 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
3056 _mesa_reference_sampler_object(ctx
, &samp_obj_save
,
3057 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
);
3059 /* Create/bind FBO/renderbuffer */
3060 if (decompress_fbo
->fb
== NULL
) {
3061 decompress_fbo
->rb
= ctx
->Driver
.NewRenderbuffer(ctx
, 0xDEADBEEF);
3062 if (decompress_fbo
->rb
== NULL
) {
3063 _mesa_meta_end(ctx
);
3067 decompress_fbo
->fb
= ctx
->Driver
.NewFramebuffer(ctx
, 0xDEADBEEF);
3068 if (decompress_fbo
->fb
== NULL
) {
3069 _mesa_meta_end(ctx
);
3073 _mesa_bind_framebuffers(ctx
, decompress_fbo
->fb
, decompress_fbo
->fb
);
3074 _mesa_framebuffer_renderbuffer(ctx
, ctx
->DrawBuffer
, GL_COLOR_ATTACHMENT0
,
3075 decompress_fbo
->rb
);
3078 _mesa_bind_framebuffers(ctx
, decompress_fbo
->fb
, decompress_fbo
->fb
);
3081 /* alloc dest surface */
3082 if (width
> decompress_fbo
->Width
|| height
> decompress_fbo
->Height
) {
3083 _mesa_renderbuffer_storage(ctx
, decompress_fbo
->rb
, rbFormat
,
3084 width
, height
, 0, 0);
3086 /* Do the full completeness check to recompute
3087 * ctx->DrawBuffer->Width/Height.
3089 ctx
->DrawBuffer
->_Status
= GL_FRAMEBUFFER_UNDEFINED
;
3090 status
= _mesa_check_framebuffer_status(ctx
, ctx
->DrawBuffer
);
3091 if (status
!= GL_FRAMEBUFFER_COMPLETE
) {
3092 /* If the framebuffer isn't complete then we'll leave
3093 * decompress_fbo->Width as zero so that it will fail again next time
3095 _mesa_meta_end(ctx
);
3098 decompress_fbo
->Width
= width
;
3099 decompress_fbo
->Height
= height
;
3102 if (use_glsl_version
) {
3103 _mesa_meta_setup_vertex_objects(ctx
, &decompress
->VAO
,
3104 &decompress
->buf_obj
, true,
3107 _mesa_meta_setup_blit_shader(ctx
, target
, false, &decompress
->shaders
);
3109 _mesa_meta_setup_ff_tnl_for_blit(ctx
, &decompress
->VAO
,
3110 &decompress
->buf_obj
, 3);
3113 if (decompress
->samp_obj
== NULL
) {
3114 decompress
->samp_obj
= ctx
->Driver
.NewSamplerObject(ctx
, 0xDEADBEEF);
3115 if (decompress
->samp_obj
== NULL
) {
3116 _mesa_meta_end(ctx
);
3118 /* This is a bit lazy. Flag out of memory, and then don't bother to
3119 * clean up. Once out of memory is flagged, the only realistic next
3120 * move is to destroy the context. That will trigger all the right
3123 * Returning true prevents other GetTexImage methods from attempting
3124 * anything since they will likely fail too.
3126 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
3130 /* nearest filtering */
3131 _mesa_set_sampler_filters(ctx
, decompress
->samp_obj
, GL_NEAREST
, GL_NEAREST
);
3133 /* We don't want to encode or decode sRGB values; treat them as linear. */
3134 _mesa_set_sampler_srgb_decode(ctx
, decompress
->samp_obj
, GL_SKIP_DECODE_EXT
);
3137 _mesa_bind_sampler(ctx
, ctx
->Texture
.CurrentUnit
, decompress
->samp_obj
);
3139 /* Silence valgrind warnings about reading uninitialized stack. */
3140 memset(verts
, 0, sizeof(verts
));
3142 _mesa_meta_setup_texture_coords(faceTarget
, slice
,
3143 xoffset
, yoffset
, width
, height
,
3144 texImage
->Width
, texImage
->Height
,
3151 /* setup vertex positions */
3161 _mesa_set_viewport(ctx
, 0, 0, 0, width
, height
);
3163 /* upload new vertex data */
3164 _mesa_buffer_sub_data(ctx
, decompress
->buf_obj
, 0, sizeof(verts
), verts
);
3166 /* setup texture state */
3167 _mesa_bind_texture(ctx
, target
, texObj
);
3169 if (!use_glsl_version
)
3170 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3173 /* save texture object state */
3174 const GLint baseLevelSave
= texObj
->BaseLevel
;
3175 const GLint maxLevelSave
= texObj
->MaxLevel
;
3177 /* restrict sampling to the texture level of interest */
3178 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3179 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_BASE_LEVEL
,
3180 (GLint
*) &texImage
->Level
, false);
3181 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_MAX_LEVEL
,
3182 (GLint
*) &texImage
->Level
, false);
3185 /* render quad w/ texture into renderbuffer */
3186 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3188 /* Restore texture object state, the texture binding will
3189 * be restored by _mesa_meta_end().
3191 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3192 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_BASE_LEVEL
,
3193 &baseLevelSave
, false);
3194 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_MAX_LEVEL
,
3195 &maxLevelSave
, false);
3200 /* read pixels from renderbuffer */
3202 GLenum baseTexFormat
= texImage
->_BaseFormat
;
3203 GLenum destBaseFormat
= _mesa_unpack_format_to_base_format(destFormat
);
3205 /* The pixel transfer state will be set to default values at this point
3206 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
3207 * turned off (as required by glGetTexImage) but we need to handle some
3208 * special cases. In particular, single-channel texture values are
3209 * returned as red and two-channel texture values are returned as
3212 if (_mesa_need_luminance_to_rgb_conversion(baseTexFormat
,
3214 /* If we're reading back an RGB(A) texture (using glGetTexImage) as
3215 * luminance then we need to return L=tex(R).
3217 _mesa_need_rgb_to_luminance_conversion(baseTexFormat
,
3219 /* Green and blue must be zero */
3220 _mesa_PixelTransferf(GL_GREEN_SCALE
, 0.0f
);
3221 _mesa_PixelTransferf(GL_BLUE_SCALE
, 0.0f
);
3224 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
3227 /* disable texture unit */
3228 if (!use_glsl_version
)
3229 _mesa_set_enable(ctx
, target
, GL_FALSE
);
3231 _mesa_bind_sampler(ctx
, ctx
->Texture
.CurrentUnit
, samp_obj_save
);
3232 _mesa_reference_sampler_object(ctx
, &samp_obj_save
, NULL
);
3234 _mesa_meta_end(ctx
);
3241 * This is just a wrapper around _mesa_get_tex_image() and
3242 * decompress_texture_image(). Meta functions should not be directly called
3246 _mesa_meta_GetTexSubImage(struct gl_context
*ctx
,
3247 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3248 GLsizei width
, GLsizei height
, GLsizei depth
,
3249 GLenum format
, GLenum type
, GLvoid
*pixels
,
3250 struct gl_texture_image
*texImage
)
3252 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
3256 for (slice
= 0; slice
< depth
; slice
++) {
3258 /* Section 8.11.4 (Texture Image Queries) of the GL 4.5 spec says:
3260 * "For three-dimensional, two-dimensional array, cube map array,
3261 * and cube map textures pixel storage operations are applied as
3262 * if the image were two-dimensional, except that the additional
3263 * pixel storage state values PACK_IMAGE_HEIGHT and
3264 * PACK_SKIP_IMAGES are applied. The correspondence of texels to
3265 * memory locations is as defined for TexImage3D in section 8.5."
3267 switch (texImage
->TexObject
->Target
) {
3269 case GL_TEXTURE_2D_ARRAY
:
3270 case GL_TEXTURE_CUBE_MAP
:
3271 case GL_TEXTURE_CUBE_MAP_ARRAY
: {
3272 /* Setup pixel packing. SkipPixels and SkipRows will be applied
3273 * in the decompress_texture_image() function's call to
3274 * glReadPixels but we need to compute the dest slice's address
3275 * here (according to SkipImages and ImageHeight).
3277 struct gl_pixelstore_attrib packing
= ctx
->Pack
;
3278 packing
.SkipPixels
= 0;
3279 packing
.SkipRows
= 0;
3280 dst
= _mesa_image_address3d(&packing
, pixels
, width
, height
,
3281 format
, type
, slice
, 0, 0);
3288 result
= decompress_texture_image(ctx
, texImage
, slice
,
3289 xoffset
, yoffset
, width
, height
,
3299 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
3300 width
, height
, depth
, format
, type
, pixels
, texImage
);
3305 * Meta implementation of ctx->Driver.DrawTex() in terms
3306 * of polygon rendering.
3309 _mesa_meta_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
3310 GLfloat width
, GLfloat height
)
3312 struct drawtex_state
*drawtex
= &ctx
->Meta
->DrawTex
;
3314 GLfloat x
, y
, z
, st
[MAX_TEXTURE_UNITS
][2];
3316 struct vertex verts
[4];
3319 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
3321 MESA_META_TRANSFORM
|
3323 MESA_META_VIEWPORT
));
3325 if (drawtex
->VAO
== 0) {
3326 /* one-time setup */
3327 struct gl_vertex_array_object
*array_obj
;
3329 /* create vertex array object */
3330 _mesa_GenVertexArrays(1, &drawtex
->VAO
);
3331 _mesa_BindVertexArray(drawtex
->VAO
);
3333 array_obj
= _mesa_lookup_vao(ctx
, drawtex
->VAO
);
3334 assert(array_obj
!= NULL
);
3336 /* create vertex array buffer */
3337 drawtex
->buf_obj
= ctx
->Driver
.NewBufferObject(ctx
, 0xDEADBEEF);
3338 if (drawtex
->buf_obj
== NULL
)
3341 _mesa_buffer_data(ctx
, drawtex
->buf_obj
, GL_NONE
, sizeof(verts
), verts
,
3342 GL_DYNAMIC_DRAW
, __func__
);
3344 /* setup vertex arrays */
3345 FLUSH_VERTICES(ctx
, 0);
3346 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_POS
,
3347 3, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
3349 offsetof(struct vertex
, x
));
3350 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_POS
,
3351 drawtex
->buf_obj
, 0, sizeof(struct vertex
));
3352 _mesa_enable_vertex_array_attrib(ctx
, array_obj
, VERT_ATTRIB_POS
);
3355 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3356 FLUSH_VERTICES(ctx
, 0);
3357 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_TEX(i
),
3358 2, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
3360 offsetof(struct vertex
, st
[i
]));
3361 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_TEX(i
),
3362 drawtex
->buf_obj
, 0, sizeof(struct vertex
));
3363 _mesa_enable_vertex_array_attrib(ctx
, array_obj
, VERT_ATTRIB_TEX(i
));
3367 _mesa_BindVertexArray(drawtex
->VAO
);
3370 /* vertex positions, texcoords */
3372 const GLfloat x1
= x
+ width
;
3373 const GLfloat y1
= y
+ height
;
3375 z
= CLAMP(z
, 0.0f
, 1.0f
);
3394 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3395 const struct gl_texture_object
*texObj
;
3396 const struct gl_texture_image
*texImage
;
3397 GLfloat s
, t
, s1
, t1
;
3400 if (!ctx
->Texture
.Unit
[i
]._Current
) {
3402 for (j
= 0; j
< 4; j
++) {
3403 verts
[j
].st
[i
][0] = 0.0f
;
3404 verts
[j
].st
[i
][1] = 0.0f
;
3409 texObj
= ctx
->Texture
.Unit
[i
]._Current
;
3410 texImage
= texObj
->Image
[0][texObj
->BaseLevel
];
3411 tw
= texImage
->Width2
;
3412 th
= texImage
->Height2
;
3414 s
= (GLfloat
) texObj
->CropRect
[0] / tw
;
3415 t
= (GLfloat
) texObj
->CropRect
[1] / th
;
3416 s1
= (GLfloat
) (texObj
->CropRect
[0] + texObj
->CropRect
[2]) / tw
;
3417 t1
= (GLfloat
) (texObj
->CropRect
[1] + texObj
->CropRect
[3]) / th
;
3419 verts
[0].st
[i
][0] = s
;
3420 verts
[0].st
[i
][1] = t
;
3422 verts
[1].st
[i
][0] = s1
;
3423 verts
[1].st
[i
][1] = t
;
3425 verts
[2].st
[i
][0] = s1
;
3426 verts
[2].st
[i
][1] = t1
;
3428 verts
[3].st
[i
][0] = s
;
3429 verts
[3].st
[i
][1] = t1
;
3432 _mesa_buffer_sub_data(ctx
, drawtex
->buf_obj
, 0, sizeof(verts
), verts
);
3435 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3437 _mesa_meta_end(ctx
);
3441 cleartexsubimage_color(struct gl_context
*ctx
,
3442 struct gl_texture_image
*texImage
,
3443 const GLvoid
*clearValue
,
3447 union gl_color_union colorValue
;
3451 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
3452 GL_COLOR_ATTACHMENT0
,
3455 status
= _mesa_check_framebuffer_status(ctx
, ctx
->DrawBuffer
);
3456 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
3459 /* We don't want to apply an sRGB conversion so override the format */
3460 format
= _mesa_get_srgb_format_linear(texImage
->TexFormat
);
3461 datatype
= _mesa_get_format_datatype(format
);
3464 case GL_UNSIGNED_INT
:
3467 _mesa_unpack_uint_rgba_row(format
, 1, clearValue
,
3468 (GLuint (*)[4]) colorValue
.ui
);
3470 memset(&colorValue
, 0, sizeof colorValue
);
3471 if (datatype
== GL_INT
)
3472 _mesa_ClearBufferiv(GL_COLOR
, 0, colorValue
.i
);
3474 _mesa_ClearBufferuiv(GL_COLOR
, 0, colorValue
.ui
);
3478 _mesa_unpack_rgba_row(format
, 1, clearValue
,
3479 (GLfloat (*)[4]) colorValue
.f
);
3481 memset(&colorValue
, 0, sizeof colorValue
);
3482 _mesa_ClearBufferfv(GL_COLOR
, 0, colorValue
.f
);
3490 cleartexsubimage_depth_stencil(struct gl_context
*ctx
,
3491 struct gl_texture_image
*texImage
,
3492 const GLvoid
*clearValue
,
3495 GLint stencilValue
= 0;
3496 GLfloat depthValue
= 0.0f
;
3499 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
3500 GL_DEPTH_ATTACHMENT
,
3503 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
)
3504 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
3505 GL_STENCIL_ATTACHMENT
,
3508 status
= _mesa_check_framebuffer_status(ctx
, ctx
->DrawBuffer
);
3509 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
3513 GLuint depthStencilValue
[2];
3515 /* Convert the clearValue from whatever format it's in to a floating
3516 * point value for the depth and an integer value for the stencil index
3518 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
) {
3519 _mesa_unpack_float_32_uint_24_8_depth_stencil_row(texImage
->TexFormat
,
3523 /* We need a memcpy here instead of a cast because we need to
3524 * reinterpret the bytes as a float rather than converting it
3526 memcpy(&depthValue
, depthStencilValue
, sizeof depthValue
);
3527 stencilValue
= depthStencilValue
[1] & 0xff;
3529 _mesa_unpack_float_z_row(texImage
->TexFormat
, 1 /* n */,
3530 clearValue
, &depthValue
);
3534 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
)
3535 _mesa_ClearBufferfi(GL_DEPTH_STENCIL
, 0, depthValue
, stencilValue
);
3537 _mesa_ClearBufferfv(GL_DEPTH
, 0, &depthValue
);
3543 cleartexsubimage_for_zoffset(struct gl_context
*ctx
,
3544 struct gl_texture_image
*texImage
,
3546 const GLvoid
*clearValue
)
3548 struct gl_framebuffer
*drawFb
;
3551 drawFb
= ctx
->Driver
.NewFramebuffer(ctx
, 0xDEADBEEF);
3555 _mesa_bind_framebuffers(ctx
, drawFb
, ctx
->ReadBuffer
);
3557 switch(texImage
->_BaseFormat
) {
3558 case GL_DEPTH_STENCIL
:
3559 case GL_DEPTH_COMPONENT
:
3560 success
= cleartexsubimage_depth_stencil(ctx
, texImage
,
3561 clearValue
, zoffset
);
3564 success
= cleartexsubimage_color(ctx
, texImage
, clearValue
, zoffset
);
3568 _mesa_reference_framebuffer(&drawFb
, NULL
);
3574 cleartexsubimage_using_fbo(struct gl_context
*ctx
,
3575 struct gl_texture_image
*texImage
,
3576 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3577 GLsizei width
, GLsizei height
, GLsizei depth
,
3578 const GLvoid
*clearValue
)
3580 bool success
= true;
3583 _mesa_meta_begin(ctx
,
3585 MESA_META_COLOR_MASK
|
3587 MESA_META_FRAMEBUFFER_SRGB
);
3589 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
3590 _mesa_set_enable(ctx
, GL_DITHER
, GL_FALSE
);
3592 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_TRUE
);
3593 _mesa_Scissor(xoffset
, yoffset
, width
, height
);
3595 for (z
= zoffset
; z
< zoffset
+ depth
; z
++) {
3596 if (!cleartexsubimage_for_zoffset(ctx
, texImage
, z
, clearValue
)) {
3602 _mesa_meta_end(ctx
);
3608 _mesa_meta_ClearTexSubImage(struct gl_context
*ctx
,
3609 struct gl_texture_image
*texImage
,
3610 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3611 GLsizei width
, GLsizei height
, GLsizei depth
,
3612 const GLvoid
*clearValue
)
3616 res
= cleartexsubimage_using_fbo(ctx
, texImage
,
3617 xoffset
, yoffset
, zoffset
,
3618 width
, height
, depth
,
3625 "Falling back to mapping the texture in "
3626 "glClearTexSubImage\n");
3628 _mesa_store_cleartexsubimage(ctx
, texImage
,
3629 xoffset
, yoffset
, zoffset
,
3630 width
, height
, depth
,