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_copypix_cleanup(struct gl_context
*ctx
,
108 struct copypix_state
*copypix
);
109 static void meta_decompress_cleanup(struct gl_context
*ctx
,
110 struct decompress_state
*decompress
);
111 static void meta_drawpix_cleanup(struct gl_context
*ctx
,
112 struct drawpix_state
*drawpix
);
113 static void meta_drawtex_cleanup(struct gl_context
*ctx
,
114 struct drawtex_state
*drawtex
);
117 _mesa_meta_framebuffer_texture_image(struct gl_context
*ctx
,
118 struct gl_framebuffer
*fb
,
120 struct gl_texture_image
*texImage
,
123 struct gl_texture_object
*texObj
= texImage
->TexObject
;
124 int level
= texImage
->Level
;
125 const GLenum texTarget
= texObj
->Target
== GL_TEXTURE_CUBE_MAP
126 ? GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
129 struct gl_renderbuffer_attachment
*att
=
130 _mesa_get_and_validate_attachment(ctx
, fb
, attachment
, __func__
);
133 _mesa_framebuffer_texture(ctx
, fb
, attachment
, att
, texObj
, texTarget
,
134 level
, att
->NumSamples
, layer
, false);
137 static struct gl_shader
*
138 meta_compile_shader_with_debug(struct gl_context
*ctx
, gl_shader_stage stage
,
139 const GLcharARB
*source
)
141 const GLuint name
= ~0;
142 struct gl_shader
*sh
;
144 sh
= _mesa_new_shader(name
, stage
);
145 sh
->Source
= strdup(source
);
146 sh
->CompileStatus
= COMPILE_FAILURE
;
147 _mesa_compile_shader(ctx
, sh
);
149 if (!sh
->CompileStatus
) {
152 "meta program compile failed:\n%s\nsource:\n%s\n",
153 sh
->InfoLog
, source
);
156 _mesa_reference_shader(ctx
, &sh
, NULL
);
163 _mesa_meta_link_program_with_debug(struct gl_context
*ctx
,
164 struct gl_shader_program
*sh_prog
)
166 _mesa_link_program(ctx
, sh_prog
);
168 if (!sh_prog
->data
->LinkStatus
) {
169 _mesa_problem(ctx
, "meta program link failed:\n%s",
170 sh_prog
->data
->InfoLog
);
175 _mesa_meta_use_program(struct gl_context
*ctx
,
176 struct gl_shader_program
*sh_prog
)
178 /* Attach shader state to the binding point */
179 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
181 /* Update the program */
182 _mesa_use_shader_program(ctx
, sh_prog
);
186 _mesa_meta_compile_and_link_program(struct gl_context
*ctx
,
187 const char *vs_source
,
188 const char *fs_source
,
190 struct gl_shader_program
**out_sh_prog
)
192 struct gl_shader_program
*sh_prog
;
193 const GLuint id
= ~0;
195 sh_prog
= _mesa_new_shader_program(id
);
196 sh_prog
->Label
= strdup(name
);
197 sh_prog
->NumShaders
= 2;
198 sh_prog
->Shaders
= malloc(2 * sizeof(struct gl_shader
*));
199 sh_prog
->Shaders
[0] =
200 meta_compile_shader_with_debug(ctx
, MESA_SHADER_VERTEX
, vs_source
);
201 sh_prog
->Shaders
[1] =
202 meta_compile_shader_with_debug(ctx
, MESA_SHADER_FRAGMENT
, fs_source
);
204 _mesa_meta_link_program_with_debug(ctx
, sh_prog
);
206 struct gl_program
*fp
=
207 sh_prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
;
209 /* texelFetch() can break GL_SKIP_DECODE_EXT, but many meta passes want
210 * to use both together; pretend that we're not using texelFetch to hack
211 * around this bad interaction. This is a bit fragile as it may break
212 * if you re-run the pass that gathers this info, but we probably won't...
214 fp
->info
.textures_used_by_txf
= 0;
216 fp
->nir
->info
.textures_used_by_txf
= 0;
218 _mesa_meta_use_program(ctx
, sh_prog
);
220 *out_sh_prog
= sh_prog
;
224 * Generate a generic shader to blit from a texture to a framebuffer
226 * \param ctx Current GL context
227 * \param texTarget Texture target that will be the source of the blit
229 * \returns a handle to a shader program on success or zero on failure.
232 _mesa_meta_setup_blit_shader(struct gl_context
*ctx
,
235 struct blit_shader_table
*table
)
237 char *vs_source
, *fs_source
;
238 struct blit_shader
*shader
= choose_blit_shader(target
, table
);
239 const char *fs_input
, *vs_preprocess
, *fs_preprocess
;
242 if (ctx
->Const
.GLSLVersion
< 130) {
244 fs_preprocess
= "#extension GL_EXT_texture_array : enable";
245 fs_input
= "varying";
247 vs_preprocess
= "#version 130";
248 fs_preprocess
= "#version 130";
250 shader
->func
= "texture";
253 assert(shader
!= NULL
);
255 if (shader
->shader_prog
!= NULL
) {
256 _mesa_meta_use_program(ctx
, shader
->shader_prog
);
260 mem_ctx
= ralloc_context(NULL
);
262 vs_source
= ralloc_asprintf(mem_ctx
,
264 "#extension GL_ARB_explicit_attrib_location: enable\n"
265 "layout(location = 0) in vec2 position;\n"
266 "layout(location = 1) in vec4 textureCoords;\n"
267 "out vec4 texCoords;\n"
270 " texCoords = textureCoords;\n"
271 " gl_Position = vec4(position, 0.0, 1.0);\n"
275 fs_source
= ralloc_asprintf(mem_ctx
,
277 "#extension GL_ARB_texture_cube_map_array: enable\n"
278 "uniform %s texSampler;\n"
279 "%s vec4 texCoords;\n"
282 " gl_FragColor = %s(texSampler, %s);\n"
285 fs_preprocess
, shader
->type
, fs_input
,
286 shader
->func
, shader
->texcoords
,
287 do_depth
? " gl_FragDepth = gl_FragColor.x;\n" : "");
289 _mesa_meta_compile_and_link_program(ctx
, vs_source
, fs_source
,
290 ralloc_asprintf(mem_ctx
, "%s blit",
292 &shader
->shader_prog
);
293 ralloc_free(mem_ctx
);
297 * Configure vertex buffer and vertex array objects for tests
299 * Regardless of whether a new VAO is created, the object referenced by \c VAO
300 * will be bound into the GL state vector when this function terminates. The
301 * object referenced by \c VBO will \b not be bound.
303 * \param VAO Storage for vertex array object handle. If 0, a new VAO
305 * \param buf_obj Storage for vertex buffer object pointer. If \c NULL, a new VBO
306 * will be created. The new VBO will have storage for 4
307 * \c vertex structures.
308 * \param use_generic_attributes Should generic attributes 0 and 1 be used,
309 * or should traditional, fixed-function color and texture
310 * coordinate be used?
311 * \param vertex_size Number of components for attribute 0 / vertex.
312 * \param texcoord_size Number of components for attribute 1 / texture
313 * coordinate. If this is 0, attribute 1 will not be set or
315 * \param color_size Number of components for attribute 1 / primary color.
316 * If this is 0, attribute 1 will not be set or enabled.
318 * \note If \c use_generic_attributes is \c true, \c color_size must be zero.
319 * Use \c texcoord_size instead.
322 _mesa_meta_setup_vertex_objects(struct gl_context
*ctx
,
323 GLuint
*VAO
, struct gl_buffer_object
**buf_obj
,
324 bool use_generic_attributes
,
325 unsigned vertex_size
, unsigned texcoord_size
,
329 struct gl_vertex_array_object
*array_obj
;
330 assert(*buf_obj
== NULL
);
332 /* create vertex array object */
333 _mesa_GenVertexArrays(1, VAO
);
334 _mesa_BindVertexArray(*VAO
);
336 array_obj
= _mesa_lookup_vao(ctx
, *VAO
);
337 assert(array_obj
!= NULL
);
339 /* create vertex array buffer */
340 *buf_obj
= ctx
->Driver
.NewBufferObject(ctx
, 0xDEADBEEF);
341 if (*buf_obj
== NULL
)
344 _mesa_buffer_data(ctx
, *buf_obj
, GL_NONE
, 4 * sizeof(struct vertex
), NULL
,
345 GL_DYNAMIC_DRAW
, __func__
);
347 /* setup vertex arrays */
348 FLUSH_VERTICES(ctx
, 0);
349 if (use_generic_attributes
) {
350 assert(color_size
== 0);
352 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_GENERIC(0),
353 vertex_size
, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
355 offsetof(struct vertex
, x
));
356 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_GENERIC(0),
357 *buf_obj
, 0, sizeof(struct vertex
));
358 _mesa_enable_vertex_array_attrib(ctx
, array_obj
,
359 VERT_ATTRIB_GENERIC(0));
360 if (texcoord_size
> 0) {
361 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_GENERIC(1),
362 texcoord_size
, GL_FLOAT
, GL_RGBA
,
363 GL_FALSE
, GL_FALSE
, GL_FALSE
,
364 offsetof(struct vertex
, tex
));
365 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_GENERIC(1),
366 *buf_obj
, 0, sizeof(struct vertex
));
367 _mesa_enable_vertex_array_attrib(ctx
, array_obj
,
368 VERT_ATTRIB_GENERIC(1));
371 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_POS
,
372 vertex_size
, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
374 offsetof(struct vertex
, x
));
375 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_POS
,
376 *buf_obj
, 0, sizeof(struct vertex
));
377 _mesa_enable_vertex_array_attrib(ctx
, array_obj
, VERT_ATTRIB_POS
);
379 if (texcoord_size
> 0) {
380 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_TEX(0),
381 vertex_size
, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
383 offsetof(struct vertex
, tex
));
384 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_TEX(0),
385 *buf_obj
, 0, sizeof(struct vertex
));
386 _mesa_enable_vertex_array_attrib(ctx
, array_obj
,
390 if (color_size
> 0) {
391 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_COLOR0
,
392 vertex_size
, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
394 offsetof(struct vertex
, r
));
395 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_COLOR0
,
396 *buf_obj
, 0, sizeof(struct vertex
));
397 _mesa_enable_vertex_array_attrib(ctx
, array_obj
,
402 _mesa_BindVertexArray(*VAO
);
407 * Initialize meta-ops for a context.
408 * To be called once during context creation.
411 _mesa_meta_init(struct gl_context
*ctx
)
415 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
419 * Free context meta-op state.
420 * To be called once during context destruction.
423 _mesa_meta_free(struct gl_context
*ctx
)
425 GET_CURRENT_CONTEXT(old_context
);
426 _mesa_make_current(ctx
, NULL
, NULL
);
427 _mesa_meta_glsl_blit_cleanup(ctx
, &ctx
->Meta
->Blit
);
428 meta_glsl_clear_cleanup(ctx
, &ctx
->Meta
->Clear
);
429 meta_copypix_cleanup(ctx
, &ctx
->Meta
->CopyPix
);
430 _mesa_meta_glsl_generate_mipmap_cleanup(ctx
, &ctx
->Meta
->Mipmap
);
431 cleanup_temp_texture(ctx
, &ctx
->Meta
->TempTex
);
432 meta_decompress_cleanup(ctx
, &ctx
->Meta
->Decompress
);
433 meta_drawpix_cleanup(ctx
, &ctx
->Meta
->DrawPix
);
434 meta_drawtex_cleanup(ctx
, &ctx
->Meta
->DrawTex
);
436 _mesa_make_current(old_context
, old_context
->WinSysDrawBuffer
, old_context
->WinSysReadBuffer
);
438 _mesa_make_current(NULL
, NULL
, NULL
);
445 * Enter meta state. This is like a light-weight version of glPushAttrib
446 * but it also resets most GL state back to default values.
448 * \param state bitmask of MESA_META_* flags indicating which attribute groups
449 * to save and reset to their defaults
452 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
454 struct save_state
*save
;
456 /* hope MAX_META_OPS_DEPTH is large enough */
457 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
459 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
460 memset(save
, 0, sizeof(*save
));
461 save
->SavedState
= state
;
463 /* We always push into desktop GL mode and pop out at the end. No sense in
464 * writing our shaders varying based on the user's context choice, when
465 * Mesa can handle either.
467 save
->API
= ctx
->API
;
468 ctx
->API
= API_OPENGL_COMPAT
;
470 /* Mesa's extension helper functions use the current context's API to look up
471 * the version required by an extension as a step in determining whether or
472 * not it has been advertised. Since meta aims to only be restricted by the
473 * driver capability (and not by whether or not an extension has been
474 * advertised), set the helper functions' Version variable to a value that
475 * will make the checks on the context API and version unconditionally pass.
477 save
->ExtensionsVersion
= ctx
->Extensions
.Version
;
478 ctx
->Extensions
.Version
= ~0;
480 /* Pausing transform feedback needs to be done early, or else we won't be
481 * able to change other state.
483 save
->TransformFeedbackNeedsResume
=
484 _mesa_is_xfb_active_and_unpaused(ctx
);
485 if (save
->TransformFeedbackNeedsResume
)
486 _mesa_PauseTransformFeedback();
488 /* After saving the current occlusion object, call EndQuery so that no
489 * occlusion querying will be active during the meta-operation.
491 if (state
& MESA_META_OCCLUSION_QUERY
) {
492 save
->CurrentOcclusionObject
= ctx
->Query
.CurrentOcclusionObject
;
493 if (save
->CurrentOcclusionObject
)
494 _mesa_EndQuery(save
->CurrentOcclusionObject
->Target
);
497 if (state
& MESA_META_ALPHA_TEST
) {
498 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
499 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
500 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
501 if (ctx
->Color
.AlphaEnabled
)
502 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
505 if (state
& MESA_META_BLEND
) {
506 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
507 if (ctx
->Color
.BlendEnabled
) {
508 if (ctx
->Extensions
.EXT_draw_buffers2
) {
510 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
511 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
515 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
518 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
519 if (ctx
->Color
.ColorLogicOpEnabled
)
520 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
523 if (state
& MESA_META_DITHER
) {
524 save
->DitherFlag
= ctx
->Color
.DitherFlag
;
525 _mesa_set_enable(ctx
, GL_DITHER
, GL_TRUE
);
528 if (state
& MESA_META_COLOR_MASK
)
529 save
->ColorMask
= ctx
->Color
.ColorMask
;
531 if (state
& MESA_META_DEPTH_TEST
) {
532 save
->Depth
= ctx
->Depth
; /* struct copy */
534 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
537 if (state
& MESA_META_FOG
) {
538 save
->Fog
= ctx
->Fog
.Enabled
;
539 if (ctx
->Fog
.Enabled
)
540 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
543 if (state
& MESA_META_PIXEL_STORE
) {
544 save
->Pack
= ctx
->Pack
;
545 save
->Unpack
= ctx
->Unpack
;
546 ctx
->Pack
= ctx
->DefaultPacking
;
547 ctx
->Unpack
= ctx
->DefaultPacking
;
550 if (state
& MESA_META_PIXEL_TRANSFER
) {
551 save
->RedScale
= ctx
->Pixel
.RedScale
;
552 save
->RedBias
= ctx
->Pixel
.RedBias
;
553 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
554 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
555 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
556 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
557 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
558 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
559 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
560 ctx
->Pixel
.RedScale
= 1.0F
;
561 ctx
->Pixel
.RedBias
= 0.0F
;
562 ctx
->Pixel
.GreenScale
= 1.0F
;
563 ctx
->Pixel
.GreenBias
= 0.0F
;
564 ctx
->Pixel
.BlueScale
= 1.0F
;
565 ctx
->Pixel
.BlueBias
= 0.0F
;
566 ctx
->Pixel
.AlphaScale
= 1.0F
;
567 ctx
->Pixel
.AlphaBias
= 0.0F
;
568 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
570 ctx
->NewState
|=_NEW_PIXEL
;
573 if (state
& MESA_META_RASTERIZATION
) {
574 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
575 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
576 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
577 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
578 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
579 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
580 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
581 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
582 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
583 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
584 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
587 if (state
& MESA_META_SCISSOR
) {
588 save
->Scissor
= ctx
->Scissor
; /* struct copy */
589 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
592 if (state
& MESA_META_SHADER
) {
595 if (ctx
->Extensions
.ARB_vertex_program
) {
596 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
597 _mesa_reference_program(ctx
, &save
->VertexProgram
,
598 ctx
->VertexProgram
.Current
);
599 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
602 if (ctx
->Extensions
.ARB_fragment_program
) {
603 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
604 _mesa_reference_program(ctx
, &save
->FragmentProgram
,
605 ctx
->FragmentProgram
.Current
);
606 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
609 if (ctx
->Extensions
.ATI_fragment_shader
) {
610 save
->ATIFragmentShaderEnabled
= ctx
->ATIFragmentShader
.Enabled
;
611 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
, GL_FALSE
);
614 if (ctx
->Pipeline
.Current
) {
615 _mesa_reference_pipeline_object(ctx
, &save
->Pipeline
,
616 ctx
->Pipeline
.Current
);
617 _mesa_BindProgramPipeline(0);
620 /* Save the shader state from ctx->Shader (instead of ctx->_Shader) so
621 * that we don't have to worry about the current pipeline state.
623 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
624 _mesa_reference_program(ctx
, &save
->Program
[i
],
625 ctx
->Shader
.CurrentProgram
[i
]);
627 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
628 ctx
->Shader
.ActiveProgram
);
633 if (state
& MESA_META_STENCIL_TEST
) {
634 save
->Stencil
= ctx
->Stencil
; /* struct copy */
635 if (ctx
->Stencil
.Enabled
)
636 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
637 /* NOTE: other stencil state not reset */
640 if (state
& MESA_META_TEXTURE
) {
643 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
644 save
->EnvMode
= ctx
->Texture
.FixedFuncUnit
[0].EnvMode
;
646 /* Disable all texture units */
647 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
648 save
->TexEnabled
[u
] = ctx
->Texture
.FixedFuncUnit
[u
].Enabled
;
649 save
->TexGenEnabled
[u
] = ctx
->Texture
.FixedFuncUnit
[u
].TexGenEnabled
;
650 if (ctx
->Texture
.FixedFuncUnit
[u
].Enabled
||
651 ctx
->Texture
.FixedFuncUnit
[u
].TexGenEnabled
) {
652 _mesa_ActiveTexture(GL_TEXTURE0
+ u
);
653 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
654 if (ctx
->Extensions
.ARB_texture_cube_map
)
655 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
657 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
658 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
659 if (ctx
->Extensions
.NV_texture_rectangle
)
660 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
661 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
662 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
663 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
664 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
668 /* save current texture objects for unit[0] only */
669 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
670 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
671 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
674 /* set defaults for unit[0] */
675 _mesa_ActiveTexture(GL_TEXTURE0
);
676 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
679 if (state
& MESA_META_TRANSFORM
) {
680 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
681 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
682 16 * sizeof(GLfloat
));
683 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
684 16 * sizeof(GLfloat
));
685 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
686 16 * sizeof(GLfloat
));
687 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
688 /* set 1:1 vertex:pixel coordinate transform */
689 _mesa_ActiveTexture(GL_TEXTURE0
);
690 _mesa_MatrixMode(GL_TEXTURE
);
691 _mesa_LoadIdentity();
692 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
693 _mesa_MatrixMode(GL_MODELVIEW
);
694 _mesa_LoadIdentity();
695 _mesa_MatrixMode(GL_PROJECTION
);
696 _mesa_LoadIdentity();
698 /* glOrtho with width = 0 or height = 0 generates GL_INVALID_VALUE.
699 * This can occur when there is no draw buffer.
701 if (ctx
->DrawBuffer
->Width
!= 0 && ctx
->DrawBuffer
->Height
!= 0)
702 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
703 0.0, ctx
->DrawBuffer
->Height
,
706 if (ctx
->Extensions
.ARB_clip_control
) {
707 save
->ClipOrigin
= ctx
->Transform
.ClipOrigin
;
708 save
->ClipDepthMode
= ctx
->Transform
.ClipDepthMode
;
709 _mesa_ClipControl(GL_LOWER_LEFT
, GL_NEGATIVE_ONE_TO_ONE
);
713 if (state
& MESA_META_CLIP
) {
715 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
716 mask
= ctx
->Transform
.ClipPlanesEnabled
;
718 const int i
= u_bit_scan(&mask
);
719 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
723 if (state
& MESA_META_VERTEX
) {
724 /* save vertex array object state */
725 _mesa_reference_vao(ctx
, &save
->VAO
,
727 /* set some default state? */
730 if (state
& MESA_META_VIEWPORT
) {
731 /* save viewport state */
732 save
->ViewportX
= ctx
->ViewportArray
[0].X
;
733 save
->ViewportY
= ctx
->ViewportArray
[0].Y
;
734 save
->ViewportW
= ctx
->ViewportArray
[0].Width
;
735 save
->ViewportH
= ctx
->ViewportArray
[0].Height
;
736 /* set viewport to match window size */
737 if (ctx
->ViewportArray
[0].X
!= 0 ||
738 ctx
->ViewportArray
[0].Y
!= 0 ||
739 ctx
->ViewportArray
[0].Width
!= (float) ctx
->DrawBuffer
->Width
||
740 ctx
->ViewportArray
[0].Height
!= (float) ctx
->DrawBuffer
->Height
) {
741 _mesa_set_viewport(ctx
, 0, 0, 0,
742 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
744 /* save depth range state */
745 save
->DepthNear
= ctx
->ViewportArray
[0].Near
;
746 save
->DepthFar
= ctx
->ViewportArray
[0].Far
;
747 /* set depth range to default */
748 _mesa_set_depth_range(ctx
, 0, 0.0, 1.0);
751 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
752 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
754 /* Generally in here we want to do clamping according to whether
755 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
756 * regardless of the internal implementation of the metaops.
758 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
759 ctx
->Extensions
.ARB_color_buffer_float
)
760 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
763 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
764 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
766 /* Generally in here we never want vertex color clamping --
767 * result clamping is only dependent on fragment clamping.
769 if (ctx
->Extensions
.ARB_color_buffer_float
)
770 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
773 if (state
& MESA_META_CONDITIONAL_RENDER
) {
774 save
->CondRenderQuery
= ctx
->Query
.CondRenderQuery
;
775 save
->CondRenderMode
= ctx
->Query
.CondRenderMode
;
777 if (ctx
->Query
.CondRenderQuery
)
778 _mesa_EndConditionalRender();
781 if (state
& MESA_META_SELECT_FEEDBACK
) {
782 save
->RenderMode
= ctx
->RenderMode
;
783 if (ctx
->RenderMode
== GL_SELECT
) {
784 save
->Select
= ctx
->Select
; /* struct copy */
785 _mesa_RenderMode(GL_RENDER
);
786 } else if (ctx
->RenderMode
== GL_FEEDBACK
) {
787 save
->Feedback
= ctx
->Feedback
; /* struct copy */
788 _mesa_RenderMode(GL_RENDER
);
792 if (state
& MESA_META_MULTISAMPLE
) {
793 save
->Multisample
= ctx
->Multisample
; /* struct copy */
795 if (ctx
->Multisample
.Enabled
)
796 _mesa_set_multisample(ctx
, GL_FALSE
);
797 if (ctx
->Multisample
.SampleCoverage
)
798 _mesa_set_enable(ctx
, GL_SAMPLE_COVERAGE
, GL_FALSE
);
799 if (ctx
->Multisample
.SampleAlphaToCoverage
)
800 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_COVERAGE
, GL_FALSE
);
801 if (ctx
->Multisample
.SampleAlphaToOne
)
802 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_ONE
, GL_FALSE
);
803 if (ctx
->Multisample
.SampleShading
)
804 _mesa_set_enable(ctx
, GL_SAMPLE_SHADING
, GL_FALSE
);
805 if (ctx
->Multisample
.SampleMask
)
806 _mesa_set_enable(ctx
, GL_SAMPLE_MASK
, GL_FALSE
);
809 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
810 save
->sRGBEnabled
= ctx
->Color
.sRGBEnabled
;
811 if (ctx
->Color
.sRGBEnabled
)
812 _mesa_set_framebuffer_srgb(ctx
, GL_FALSE
);
815 if (state
& MESA_META_DRAW_BUFFERS
) {
816 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
817 memcpy(save
->ColorDrawBuffers
, fb
->ColorDrawBuffer
,
818 sizeof(save
->ColorDrawBuffers
));
823 save
->Lighting
= ctx
->Light
.Enabled
;
824 if (ctx
->Light
.Enabled
)
825 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
826 save
->RasterDiscard
= ctx
->RasterDiscard
;
827 if (ctx
->RasterDiscard
)
828 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_FALSE
);
830 _mesa_reference_framebuffer(&save
->DrawBuffer
, ctx
->DrawBuffer
);
831 _mesa_reference_framebuffer(&save
->ReadBuffer
, ctx
->ReadBuffer
);
837 * Leave meta state. This is like a light-weight version of glPopAttrib().
840 _mesa_meta_end(struct gl_context
*ctx
)
842 assert(ctx
->Meta
->SaveStackDepth
> 0);
844 struct save_state
*save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
- 1];
845 const GLbitfield state
= save
->SavedState
;
848 /* Grab the result of the old occlusion query before starting it again. The
849 * old result is added to the result of the new query so the driver will
850 * continue adding where it left off. */
851 if (state
& MESA_META_OCCLUSION_QUERY
) {
852 if (save
->CurrentOcclusionObject
) {
853 struct gl_query_object
*q
= save
->CurrentOcclusionObject
;
856 ctx
->Driver
.WaitQuery(ctx
, q
);
858 _mesa_BeginQuery(q
->Target
, q
->Id
);
859 ctx
->Query
.CurrentOcclusionObject
->Result
+= result
;
863 if (state
& MESA_META_ALPHA_TEST
) {
864 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
865 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
866 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
869 if (state
& MESA_META_BLEND
) {
870 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
871 if (ctx
->Extensions
.EXT_draw_buffers2
) {
873 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
874 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
878 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
881 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
882 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
885 if (state
& MESA_META_DITHER
)
886 _mesa_set_enable(ctx
, GL_DITHER
, save
->DitherFlag
);
888 if (state
& MESA_META_COLOR_MASK
) {
890 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
891 if (GET_COLORMASK(ctx
->Color
.ColorMask
, i
) !=
892 GET_COLORMASK(save
->ColorMask
, i
)) {
894 _mesa_ColorMask(GET_COLORMASK_BIT(save
->ColorMask
, i
, 0),
895 GET_COLORMASK_BIT(save
->ColorMask
, i
, 1),
896 GET_COLORMASK_BIT(save
->ColorMask
, i
, 2),
897 GET_COLORMASK_BIT(save
->ColorMask
, i
, 3));
901 GET_COLORMASK_BIT(save
->ColorMask
, i
, 0),
902 GET_COLORMASK_BIT(save
->ColorMask
, i
, 1),
903 GET_COLORMASK_BIT(save
->ColorMask
, i
, 2),
904 GET_COLORMASK_BIT(save
->ColorMask
, i
, 3));
910 if (state
& MESA_META_DEPTH_TEST
) {
911 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
912 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
913 _mesa_DepthFunc(save
->Depth
.Func
);
914 _mesa_DepthMask(save
->Depth
.Mask
);
917 if (state
& MESA_META_FOG
) {
918 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
921 if (state
& MESA_META_PIXEL_STORE
) {
922 ctx
->Pack
= save
->Pack
;
923 ctx
->Unpack
= save
->Unpack
;
926 if (state
& MESA_META_PIXEL_TRANSFER
) {
927 ctx
->Pixel
.RedScale
= save
->RedScale
;
928 ctx
->Pixel
.RedBias
= save
->RedBias
;
929 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
930 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
931 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
932 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
933 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
934 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
935 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
937 ctx
->NewState
|=_NEW_PIXEL
;
940 if (state
& MESA_META_RASTERIZATION
) {
941 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
942 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
943 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
944 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
945 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
946 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
949 if (state
& MESA_META_SCISSOR
) {
952 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++) {
953 _mesa_set_scissor(ctx
, i
,
954 save
->Scissor
.ScissorArray
[i
].X
,
955 save
->Scissor
.ScissorArray
[i
].Y
,
956 save
->Scissor
.ScissorArray
[i
].Width
,
957 save
->Scissor
.ScissorArray
[i
].Height
);
958 _mesa_set_enablei(ctx
, GL_SCISSOR_TEST
, i
,
959 (save
->Scissor
.EnableFlags
>> i
) & 1);
963 if (state
& MESA_META_SHADER
) {
966 if (ctx
->Extensions
.ARB_vertex_program
) {
967 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
968 save
->VertexProgramEnabled
);
969 _mesa_reference_program(ctx
, &ctx
->VertexProgram
.Current
,
970 save
->VertexProgram
);
971 _mesa_reference_program(ctx
, &save
->VertexProgram
, NULL
);
974 if (ctx
->Extensions
.ARB_fragment_program
) {
975 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
976 save
->FragmentProgramEnabled
);
977 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
.Current
,
978 save
->FragmentProgram
);
979 _mesa_reference_program(ctx
, &save
->FragmentProgram
, NULL
);
982 if (ctx
->Extensions
.ATI_fragment_shader
) {
983 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
,
984 save
->ATIFragmentShaderEnabled
);
988 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
989 /* It is safe to call _mesa_use_program even if the extension
990 * necessary for that program state is not supported. In that case,
991 * the saved program object must be NULL and the currently bound
992 * program object must be NULL. _mesa_use_program is a no-op
995 _mesa_use_program(ctx
, i
, NULL
, save
->Program
[i
], &ctx
->Shader
);
997 /* Do this *before* killing the reference. :)
999 if (save
->Program
[i
] != NULL
)
1002 _mesa_reference_program(ctx
, &save
->Program
[i
], NULL
);
1005 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
1006 save
->ActiveShader
);
1007 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
, NULL
);
1009 /* If there were any stages set with programs, use ctx->Shader as the
1010 * current shader state. Otherwise, use Pipeline.Default. The pipeline
1011 * hasn't been restored yet, and that may modify ctx->_Shader further.
1014 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
1017 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
1018 ctx
->Pipeline
.Default
);
1020 if (save
->Pipeline
) {
1021 _mesa_bind_pipeline(ctx
, save
->Pipeline
);
1023 _mesa_reference_pipeline_object(ctx
, &save
->Pipeline
, NULL
);
1026 _mesa_update_vertex_processing_mode(ctx
);
1029 if (state
& MESA_META_STENCIL_TEST
) {
1030 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
1032 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
1033 _mesa_ClearStencil(stencil
->Clear
);
1034 if (ctx
->Extensions
.EXT_stencil_two_side
) {
1035 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
1036 stencil
->TestTwoSide
);
1037 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
1038 ? GL_BACK
: GL_FRONT
);
1041 _mesa_StencilFuncSeparate(GL_FRONT
,
1042 stencil
->Function
[0],
1044 stencil
->ValueMask
[0]);
1045 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
1046 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
1047 stencil
->ZFailFunc
[0],
1048 stencil
->ZPassFunc
[0]);
1050 _mesa_StencilFuncSeparate(GL_BACK
,
1051 stencil
->Function
[1],
1053 stencil
->ValueMask
[1]);
1054 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
1055 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
1056 stencil
->ZFailFunc
[1],
1057 stencil
->ZPassFunc
[1]);
1060 if (state
& MESA_META_TEXTURE
) {
1063 assert(ctx
->Texture
.CurrentUnit
== 0);
1065 /* restore texenv for unit[0] */
1066 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
1068 /* restore texture objects for unit[0] only */
1069 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
1070 if (ctx
->Texture
.Unit
[0].CurrentTex
[tgt
] != save
->CurrentTexture
[tgt
]) {
1071 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1072 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
1073 save
->CurrentTexture
[tgt
]);
1075 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
1078 /* Restore fixed function texture enables, texgen */
1079 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
1080 if (ctx
->Texture
.FixedFuncUnit
[u
].Enabled
!= save
->TexEnabled
[u
]) {
1081 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1082 ctx
->Texture
.FixedFuncUnit
[u
].Enabled
= save
->TexEnabled
[u
];
1085 if (ctx
->Texture
.FixedFuncUnit
[u
].TexGenEnabled
!= save
->TexGenEnabled
[u
]) {
1086 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1087 ctx
->Texture
.FixedFuncUnit
[u
].TexGenEnabled
= save
->TexGenEnabled
[u
];
1091 /* restore current unit state */
1092 _mesa_ActiveTexture(GL_TEXTURE0
+ save
->ActiveUnit
);
1095 if (state
& MESA_META_TRANSFORM
) {
1096 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
1097 _mesa_ActiveTexture(GL_TEXTURE0
);
1098 _mesa_MatrixMode(GL_TEXTURE
);
1099 _mesa_LoadMatrixf(save
->TextureMatrix
);
1100 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
1102 _mesa_MatrixMode(GL_MODELVIEW
);
1103 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
1105 _mesa_MatrixMode(GL_PROJECTION
);
1106 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
1108 _mesa_MatrixMode(save
->MatrixMode
);
1110 if (ctx
->Extensions
.ARB_clip_control
)
1111 _mesa_ClipControl(save
->ClipOrigin
, save
->ClipDepthMode
);
1114 if (state
& MESA_META_CLIP
) {
1115 GLbitfield mask
= save
->ClipPlanesEnabled
;
1117 const int i
= u_bit_scan(&mask
);
1118 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
1122 if (state
& MESA_META_VERTEX
) {
1123 /* restore vertex array object */
1124 _mesa_BindVertexArray(save
->VAO
->Name
);
1125 _mesa_reference_vao(ctx
, &save
->VAO
, NULL
);
1128 if (state
& MESA_META_VIEWPORT
) {
1129 if (save
->ViewportX
!= ctx
->ViewportArray
[0].X
||
1130 save
->ViewportY
!= ctx
->ViewportArray
[0].Y
||
1131 save
->ViewportW
!= ctx
->ViewportArray
[0].Width
||
1132 save
->ViewportH
!= ctx
->ViewportArray
[0].Height
) {
1133 _mesa_set_viewport(ctx
, 0, save
->ViewportX
, save
->ViewportY
,
1134 save
->ViewportW
, save
->ViewportH
);
1136 _mesa_set_depth_range(ctx
, 0, save
->DepthNear
, save
->DepthFar
);
1139 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
&&
1140 ctx
->Extensions
.ARB_color_buffer_float
) {
1141 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
1144 if (state
& MESA_META_CLAMP_VERTEX_COLOR
&&
1145 ctx
->Extensions
.ARB_color_buffer_float
) {
1146 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
1149 if (state
& MESA_META_CONDITIONAL_RENDER
) {
1150 if (save
->CondRenderQuery
)
1151 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
1152 save
->CondRenderMode
);
1155 if (state
& MESA_META_SELECT_FEEDBACK
) {
1156 if (save
->RenderMode
== GL_SELECT
) {
1157 _mesa_RenderMode(GL_SELECT
);
1158 ctx
->Select
= save
->Select
;
1159 } else if (save
->RenderMode
== GL_FEEDBACK
) {
1160 _mesa_RenderMode(GL_FEEDBACK
);
1161 ctx
->Feedback
= save
->Feedback
;
1165 if (state
& MESA_META_MULTISAMPLE
) {
1166 struct gl_multisample_attrib
*ctx_ms
= &ctx
->Multisample
;
1167 struct gl_multisample_attrib
*save_ms
= &save
->Multisample
;
1169 if (ctx_ms
->Enabled
!= save_ms
->Enabled
)
1170 _mesa_set_multisample(ctx
, save_ms
->Enabled
);
1171 if (ctx_ms
->SampleCoverage
!= save_ms
->SampleCoverage
)
1172 _mesa_set_enable(ctx
, GL_SAMPLE_COVERAGE
, save_ms
->SampleCoverage
);
1173 if (ctx_ms
->SampleAlphaToCoverage
!= save_ms
->SampleAlphaToCoverage
)
1174 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_COVERAGE
, save_ms
->SampleAlphaToCoverage
);
1175 if (ctx_ms
->SampleAlphaToOne
!= save_ms
->SampleAlphaToOne
)
1176 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_ONE
, save_ms
->SampleAlphaToOne
);
1177 if (ctx_ms
->SampleCoverageValue
!= save_ms
->SampleCoverageValue
||
1178 ctx_ms
->SampleCoverageInvert
!= save_ms
->SampleCoverageInvert
) {
1179 _mesa_SampleCoverage(save_ms
->SampleCoverageValue
,
1180 save_ms
->SampleCoverageInvert
);
1182 if (ctx_ms
->SampleShading
!= save_ms
->SampleShading
)
1183 _mesa_set_enable(ctx
, GL_SAMPLE_SHADING
, save_ms
->SampleShading
);
1184 if (ctx_ms
->SampleMask
!= save_ms
->SampleMask
)
1185 _mesa_set_enable(ctx
, GL_SAMPLE_MASK
, save_ms
->SampleMask
);
1186 if (ctx_ms
->SampleMaskValue
!= save_ms
->SampleMaskValue
)
1187 _mesa_SampleMaski(0, save_ms
->SampleMaskValue
);
1188 if (ctx_ms
->MinSampleShadingValue
!= save_ms
->MinSampleShadingValue
)
1189 _mesa_MinSampleShading(save_ms
->MinSampleShadingValue
);
1192 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
1193 if (ctx
->Color
.sRGBEnabled
!= save
->sRGBEnabled
)
1194 _mesa_set_framebuffer_srgb(ctx
, save
->sRGBEnabled
);
1198 if (save
->Lighting
) {
1199 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
1201 if (save
->RasterDiscard
) {
1202 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_TRUE
);
1204 if (save
->TransformFeedbackNeedsResume
)
1205 _mesa_ResumeTransformFeedback();
1207 _mesa_bind_framebuffers(ctx
, save
->DrawBuffer
, save
->ReadBuffer
);
1208 _mesa_reference_framebuffer(&save
->DrawBuffer
, NULL
);
1209 _mesa_reference_framebuffer(&save
->ReadBuffer
, NULL
);
1211 if (state
& MESA_META_DRAW_BUFFERS
) {
1212 _mesa_drawbuffers(ctx
, ctx
->DrawBuffer
, ctx
->Const
.MaxDrawBuffers
,
1213 save
->ColorDrawBuffers
, NULL
);
1216 ctx
->Meta
->SaveStackDepth
--;
1218 ctx
->API
= save
->API
;
1219 ctx
->Extensions
.Version
= save
->ExtensionsVersion
;
1224 * Convert Z from a normalized value in the range [0, 1] to an object-space
1225 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1226 * default/identity ortho projection results in the original Z value.
1227 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1228 * value comes from the clear value or raster position.
1230 static inline GLfloat
1231 invert_z(GLfloat normZ
)
1233 GLfloat objZ
= 1.0f
- 2.0f
* normZ
;
1239 * One-time init for a temp_texture object.
1240 * Choose tex target, compute max tex size, etc.
1243 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1245 /* prefer texture rectangle */
1246 if (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.NV_texture_rectangle
) {
1247 tex
->Target
= GL_TEXTURE_RECTANGLE
;
1248 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
1249 tex
->NPOT
= GL_TRUE
;
1252 /* use 2D texture, NPOT if possible */
1253 tex
->Target
= GL_TEXTURE_2D
;
1254 tex
->MaxSize
= ctx
->Const
.MaxTextureSize
;
1255 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
1257 tex
->MinSize
= 16; /* 16 x 16 at least */
1258 assert(tex
->MaxSize
> 0);
1260 tex
->tex_obj
= ctx
->Driver
.NewTextureObject(ctx
, 0xDEADBEEF, tex
->Target
);
1264 cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1266 _mesa_delete_nameless_texture(ctx
, tex
->tex_obj
);
1267 tex
->tex_obj
= NULL
;
1272 * Return pointer to temp_texture info for non-bitmap ops.
1273 * This does some one-time init if needed.
1275 struct temp_texture
*
1276 _mesa_meta_get_temp_texture(struct gl_context
*ctx
)
1278 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
1280 if (tex
->tex_obj
== NULL
) {
1281 init_temp_texture(ctx
, tex
);
1289 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1290 * We use a separate texture for bitmaps to reduce texture
1291 * allocation/deallocation.
1293 static struct temp_texture
*
1294 get_bitmap_temp_texture(struct gl_context
*ctx
)
1296 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
1298 if (tex
->tex_obj
== NULL
) {
1299 init_temp_texture(ctx
, tex
);
1306 * Return pointer to depth temp_texture.
1307 * This does some one-time init if needed.
1309 struct temp_texture
*
1310 _mesa_meta_get_temp_depth_texture(struct gl_context
*ctx
)
1312 struct temp_texture
*tex
= &ctx
->Meta
->Blit
.depthTex
;
1314 if (tex
->tex_obj
== NULL
) {
1315 init_temp_texture(ctx
, tex
);
1322 * Compute the width/height of texture needed to draw an image of the
1323 * given size. Return a flag indicating whether the current texture
1324 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1325 * allocated (glTexImage2D).
1326 * Also, compute s/t texcoords for drawing.
1328 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1331 _mesa_meta_alloc_texture(struct temp_texture
*tex
,
1332 GLsizei width
, GLsizei height
, GLenum intFormat
)
1334 GLboolean newTex
= GL_FALSE
;
1336 assert(width
<= tex
->MaxSize
);
1337 assert(height
<= tex
->MaxSize
);
1339 if (width
> tex
->Width
||
1340 height
> tex
->Height
||
1341 intFormat
!= tex
->IntFormat
) {
1342 /* alloc new texture (larger or different format) */
1345 /* use non-power of two size */
1346 tex
->Width
= MAX2(tex
->MinSize
, width
);
1347 tex
->Height
= MAX2(tex
->MinSize
, height
);
1350 /* find power of two size */
1352 w
= h
= tex
->MinSize
;
1361 tex
->IntFormat
= intFormat
;
1366 /* compute texcoords */
1367 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1368 tex
->Sright
= (GLfloat
) width
;
1369 tex
->Ttop
= (GLfloat
) height
;
1372 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1373 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1381 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1384 _mesa_meta_setup_copypix_texture(struct gl_context
*ctx
,
1385 struct temp_texture
*tex
,
1386 GLint srcX
, GLint srcY
,
1387 GLsizei width
, GLsizei height
,
1393 _mesa_bind_texture(ctx
, tex
->Target
, tex
->tex_obj
);
1394 _mesa_texture_parameteriv(ctx
, tex
->tex_obj
, GL_TEXTURE_MIN_FILTER
,
1395 (GLint
*) &filter
, false);
1396 _mesa_texture_parameteriv(ctx
, tex
->tex_obj
, GL_TEXTURE_MAG_FILTER
,
1397 (GLint
*) &filter
, false);
1398 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1400 newTex
= _mesa_meta_alloc_texture(tex
, width
, height
, intFormat
);
1402 /* copy framebuffer image to texture */
1404 /* create new tex image */
1405 if (tex
->Width
== width
&& tex
->Height
== height
) {
1406 /* create new tex with framebuffer data */
1407 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1408 srcX
, srcY
, width
, height
, 0);
1411 /* create empty texture */
1412 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1413 tex
->Width
, tex
->Height
, 0,
1414 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1416 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1417 0, 0, srcX
, srcY
, width
, height
);
1421 /* replace existing tex image */
1422 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1423 0, 0, srcX
, srcY
, width
, height
);
1429 * Setup/load texture for glDrawPixels.
1432 _mesa_meta_setup_drawpix_texture(struct gl_context
*ctx
,
1433 struct temp_texture
*tex
,
1435 GLsizei width
, GLsizei height
,
1436 GLenum format
, GLenum type
,
1437 const GLvoid
*pixels
)
1439 /* GLint so the compiler won't complain about type signedness mismatch in
1440 * the call to _mesa_texture_parameteriv below.
1442 static const GLint filter
= GL_NEAREST
;
1444 _mesa_bind_texture(ctx
, tex
->Target
, tex
->tex_obj
);
1445 _mesa_texture_parameteriv(ctx
, tex
->tex_obj
, GL_TEXTURE_MIN_FILTER
, &filter
,
1447 _mesa_texture_parameteriv(ctx
, tex
->tex_obj
, GL_TEXTURE_MAG_FILTER
, &filter
,
1449 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1451 /* copy pixel data to texture */
1453 /* create new tex image */
1454 if (tex
->Width
== width
&& tex
->Height
== height
) {
1455 /* create new tex and load image data */
1456 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1457 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1460 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1462 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1463 ctx
->Unpack
.BufferObj
);
1464 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1465 /* create empty texture */
1466 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1467 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1468 if (save_unpack_obj
!= NULL
)
1469 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
,
1470 save_unpack_obj
->Name
);
1472 _mesa_TexSubImage2D(tex
->Target
, 0,
1473 0, 0, width
, height
, format
, type
, pixels
);
1475 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
, NULL
);
1479 /* replace existing tex image */
1480 _mesa_TexSubImage2D(tex
->Target
, 0,
1481 0, 0, width
, height
, format
, type
, pixels
);
1486 _mesa_meta_setup_ff_tnl_for_blit(struct gl_context
*ctx
,
1487 GLuint
*VAO
, struct gl_buffer_object
**buf_obj
,
1488 unsigned texcoord_size
)
1490 _mesa_meta_setup_vertex_objects(ctx
, VAO
, buf_obj
, false, 2, texcoord_size
,
1493 /* setup projection matrix */
1494 _mesa_MatrixMode(GL_PROJECTION
);
1495 _mesa_LoadIdentity();
1499 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1502 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1504 meta_clear(ctx
, buffers
, false);
1508 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1510 meta_clear(ctx
, buffers
, true);
1514 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
1516 const char *vs_source
=
1517 "#extension GL_AMD_vertex_shader_layer : enable\n"
1518 "#extension GL_ARB_draw_instanced : enable\n"
1519 "#extension GL_ARB_explicit_attrib_location :enable\n"
1520 "layout(location = 0) in vec4 position;\n"
1523 "#ifdef GL_AMD_vertex_shader_layer\n"
1524 " gl_Layer = gl_InstanceID;\n"
1526 " gl_Position = position;\n"
1528 const char *fs_source
=
1529 "#extension GL_ARB_explicit_attrib_location :enable\n"
1530 "#extension GL_ARB_explicit_uniform_location :enable\n"
1531 "layout(location = 0) uniform vec4 color;\n"
1534 " gl_FragColor = color;\n"
1536 bool has_integer_textures
;
1538 _mesa_meta_setup_vertex_objects(ctx
, &clear
->VAO
, &clear
->buf_obj
, true,
1541 if (clear
->ShaderProg
!= 0)
1544 _mesa_meta_compile_and_link_program(ctx
, vs_source
, fs_source
, "meta clear",
1545 &clear
->ShaderProg
);
1547 has_integer_textures
= _mesa_is_gles3(ctx
) ||
1548 (_mesa_is_desktop_gl(ctx
) && ctx
->Const
.GLSLVersion
>= 130);
1550 if (has_integer_textures
) {
1551 void *shader_source_mem_ctx
= ralloc_context(NULL
);
1552 const char *vs_int_source
=
1553 ralloc_asprintf(shader_source_mem_ctx
,
1555 "#extension GL_AMD_vertex_shader_layer : enable\n"
1556 "#extension GL_ARB_draw_instanced : enable\n"
1557 "#extension GL_ARB_explicit_attrib_location :enable\n"
1558 "layout(location = 0) in vec4 position;\n"
1561 "#ifdef GL_AMD_vertex_shader_layer\n"
1562 " gl_Layer = gl_InstanceID;\n"
1564 " gl_Position = position;\n"
1566 const char *fs_int_source
=
1567 ralloc_asprintf(shader_source_mem_ctx
,
1569 "#extension GL_ARB_explicit_attrib_location :enable\n"
1570 "#extension GL_ARB_explicit_uniform_location :enable\n"
1571 "layout(location = 0) uniform ivec4 color;\n"
1572 "out ivec4 out_color;\n"
1576 " out_color = color;\n"
1579 _mesa_meta_compile_and_link_program(ctx
, vs_int_source
, fs_int_source
,
1581 &clear
->IntegerShaderProg
);
1582 ralloc_free(shader_source_mem_ctx
);
1584 /* Note that user-defined out attributes get automatically assigned
1585 * locations starting from 0, so we don't need to explicitly
1586 * BindFragDataLocation to 0.
1592 meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
)
1594 if (clear
->VAO
== 0)
1596 _mesa_DeleteVertexArrays(1, &clear
->VAO
);
1598 _mesa_reference_buffer_object(ctx
, &clear
->buf_obj
, NULL
);
1599 _mesa_reference_shader_program(ctx
, &clear
->ShaderProg
, NULL
);
1601 if (clear
->IntegerShaderProg
) {
1602 _mesa_reference_shader_program(ctx
, &clear
->IntegerShaderProg
, NULL
);
1607 meta_copypix_cleanup(struct gl_context
*ctx
, struct copypix_state
*copypix
)
1609 if (copypix
->VAO
== 0)
1611 _mesa_DeleteVertexArrays(1, ©pix
->VAO
);
1613 _mesa_reference_buffer_object(ctx
, ©pix
->buf_obj
, NULL
);
1618 * Given a bitfield of BUFFER_BIT_x draw buffers, call glDrawBuffers to
1619 * set GL to only draw to those buffers.
1621 * Since the bitfield has no associated order, the assignment of draw buffer
1622 * indices to color attachment indices is rather arbitrary.
1625 _mesa_meta_drawbuffers_from_bitfield(GLbitfield bits
)
1627 GLenum enums
[MAX_DRAW_BUFFERS
];
1631 /* This function is only legal for color buffer bitfields. */
1632 assert((bits
& ~BUFFER_BITS_COLOR
) == 0);
1634 /* Make sure we don't overflow any arrays. */
1635 assert(util_bitcount(bits
) <= MAX_DRAW_BUFFERS
);
1639 if (bits
& BUFFER_BIT_FRONT_LEFT
)
1640 enums
[i
++] = GL_FRONT_LEFT
;
1642 if (bits
& BUFFER_BIT_FRONT_RIGHT
)
1643 enums
[i
++] = GL_FRONT_RIGHT
;
1645 if (bits
& BUFFER_BIT_BACK_LEFT
)
1646 enums
[i
++] = GL_BACK_LEFT
;
1648 if (bits
& BUFFER_BIT_BACK_RIGHT
)
1649 enums
[i
++] = GL_BACK_RIGHT
;
1651 for (n
= 0; n
< MAX_COLOR_ATTACHMENTS
; n
++) {
1652 if (bits
& (1 << (BUFFER_COLOR0
+ n
)))
1653 enums
[i
++] = GL_COLOR_ATTACHMENT0
+ n
;
1656 _mesa_DrawBuffers(i
, enums
);
1660 * Given a bitfield of BUFFER_BIT_x draw buffers, call glDrawBuffers to
1661 * set GL to only draw to those buffers. Also, update color masks to
1662 * reflect the new draw buffer ordering.
1665 _mesa_meta_drawbuffers_and_colormask(struct gl_context
*ctx
, GLbitfield mask
)
1667 GLenum enums
[MAX_DRAW_BUFFERS
];
1668 GLubyte colormask
[MAX_DRAW_BUFFERS
][4];
1671 /* This function is only legal for color buffer bitfields. */
1672 assert((mask
& ~BUFFER_BITS_COLOR
) == 0);
1674 /* Make sure we don't overflow any arrays. */
1675 assert(util_bitcount(mask
) <= MAX_DRAW_BUFFERS
);
1679 for (int i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
1680 gl_buffer_index b
= ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[i
];
1681 int colormask_idx
= ctx
->Extensions
.EXT_draw_buffers2
? i
: 0;
1683 if (b
< 0 || !(mask
& (1 << b
)) ||
1684 GET_COLORMASK(ctx
->Color
.ColorMask
, colormask_idx
) == 0)
1688 case BUFFER_FRONT_LEFT
:
1689 enums
[num_bufs
] = GL_FRONT_LEFT
;
1691 case BUFFER_FRONT_RIGHT
:
1692 enums
[num_bufs
] = GL_FRONT_RIGHT
;
1694 case BUFFER_BACK_LEFT
:
1695 enums
[num_bufs
] = GL_BACK_LEFT
;
1697 case BUFFER_BACK_RIGHT
:
1698 enums
[num_bufs
] = GL_BACK_RIGHT
;
1701 assert(b
>= BUFFER_COLOR0
&& b
<= BUFFER_COLOR7
);
1702 enums
[num_bufs
] = GL_COLOR_ATTACHMENT0
+ (b
- BUFFER_COLOR0
);
1706 for (int k
= 0; k
< 4; k
++)
1707 colormask
[num_bufs
][k
] = GET_COLORMASK_BIT(ctx
->Color
.ColorMask
,
1713 _mesa_DrawBuffers(num_bufs
, enums
);
1715 for (int i
= 0; i
< num_bufs
; i
++) {
1716 _mesa_ColorMaski(i
, colormask
[i
][0], colormask
[i
][1],
1717 colormask
[i
][2], colormask
[i
][3]);
1723 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1726 meta_clear(struct gl_context
*ctx
, GLbitfield buffers
, bool glsl
)
1728 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1729 GLbitfield metaSave
;
1730 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1731 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1732 float x0
, y0
, x1
, y1
, z
;
1733 struct vertex verts
[4];
1736 metaSave
= (MESA_META_ALPHA_TEST
|
1738 MESA_META_COLOR_MASK
|
1739 MESA_META_DEPTH_TEST
|
1740 MESA_META_RASTERIZATION
|
1742 MESA_META_STENCIL_TEST
|
1744 MESA_META_VIEWPORT
|
1746 MESA_META_CLAMP_FRAGMENT_COLOR
|
1747 MESA_META_MULTISAMPLE
|
1748 MESA_META_OCCLUSION_QUERY
);
1751 metaSave
|= MESA_META_FOG
|
1752 MESA_META_PIXEL_TRANSFER
|
1753 MESA_META_TRANSFORM
|
1755 MESA_META_CLAMP_VERTEX_COLOR
|
1756 MESA_META_SELECT_FEEDBACK
;
1759 if (buffers
& BUFFER_BITS_COLOR
) {
1760 metaSave
|= MESA_META_DRAW_BUFFERS
;
1763 _mesa_meta_begin(ctx
, metaSave
);
1766 meta_glsl_clear_init(ctx
, clear
);
1768 x0
= ((float) fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
1769 y0
= ((float) fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
1770 x1
= ((float) fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
1771 y1
= ((float) fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
1772 z
= -invert_z(ctx
->Depth
.Clear
);
1774 _mesa_meta_setup_vertex_objects(ctx
, &clear
->VAO
, &clear
->buf_obj
, false,
1777 x0
= (float) fb
->_Xmin
;
1778 y0
= (float) fb
->_Ymin
;
1779 x1
= (float) fb
->_Xmax
;
1780 y1
= (float) fb
->_Ymax
;
1781 z
= invert_z(ctx
->Depth
.Clear
);
1784 if (fb
->_IntegerBuffers
) {
1786 _mesa_meta_use_program(ctx
, clear
->IntegerShaderProg
);
1787 _mesa_Uniform4iv(0, 1, ctx
->Color
.ClearColor
.i
);
1789 _mesa_meta_use_program(ctx
, clear
->ShaderProg
);
1790 _mesa_Uniform4fv(0, 1, ctx
->Color
.ClearColor
.f
);
1793 /* GL_COLOR_BUFFER_BIT */
1794 if (buffers
& BUFFER_BITS_COLOR
) {
1795 /* Only draw to the buffers we were asked to clear. */
1796 _mesa_meta_drawbuffers_and_colormask(ctx
, buffers
& BUFFER_BITS_COLOR
);
1798 /* leave colormask state as-is */
1800 /* Clears never have the color clamped. */
1801 if (ctx
->Extensions
.ARB_color_buffer_float
)
1802 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1805 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1808 /* GL_DEPTH_BUFFER_BIT */
1809 if (buffers
& BUFFER_BIT_DEPTH
) {
1810 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1811 _mesa_DepthFunc(GL_ALWAYS
);
1812 _mesa_DepthMask(GL_TRUE
);
1815 assert(!ctx
->Depth
.Test
);
1818 /* GL_STENCIL_BUFFER_BIT */
1819 if (buffers
& BUFFER_BIT_STENCIL
) {
1820 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1821 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1822 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1823 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1824 ctx
->Stencil
.Clear
& stencilMax
,
1825 ctx
->Stencil
.WriteMask
[0]);
1828 assert(!ctx
->Stencil
.Enabled
);
1831 /* vertex positions */
1846 for (i
= 0; i
< 4; i
++) {
1847 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
1848 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
1849 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
1850 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
1854 /* upload new vertex data */
1855 _mesa_buffer_data(ctx
, clear
->buf_obj
, GL_NONE
, sizeof(verts
), verts
,
1856 GL_DYNAMIC_DRAW
, __func__
);
1859 if (fb
->MaxNumLayers
> 0) {
1860 _mesa_DrawArraysInstanced(GL_TRIANGLE_FAN
, 0, 4, fb
->MaxNumLayers
);
1862 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1865 _mesa_meta_end(ctx
);
1869 * Meta implementation of ctx->Driver.CopyPixels() in terms
1870 * of texture mapping and polygon rendering and GLSL shaders.
1873 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
1874 GLsizei width
, GLsizei height
,
1875 GLint dstX
, GLint dstY
, GLenum type
)
1877 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1878 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
1879 struct vertex verts
[4];
1881 if (type
!= GL_COLOR
||
1882 ctx
->_ImageTransferState
||
1884 width
> tex
->MaxSize
||
1885 height
> tex
->MaxSize
) {
1886 /* XXX avoid this fallback */
1887 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1891 /* Most GL state applies to glCopyPixels, but a there's a few things
1892 * we need to override:
1894 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
1897 MESA_META_TRANSFORM
|
1900 MESA_META_VIEWPORT
));
1902 _mesa_meta_setup_vertex_objects(ctx
, ©pix
->VAO
, ©pix
->buf_obj
, false,
1905 /* Silence valgrind warnings about reading uninitialized stack. */
1906 memset(verts
, 0, sizeof(verts
));
1908 /* Alloc/setup texture */
1909 _mesa_meta_setup_copypix_texture(ctx
, tex
, srcX
, srcY
, width
, height
,
1910 GL_RGBA
, GL_NEAREST
);
1912 /* vertex positions, texcoords (after texture allocation!) */
1914 const GLfloat dstX0
= (GLfloat
) dstX
;
1915 const GLfloat dstY0
= (GLfloat
) dstY
;
1916 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1917 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1918 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1923 verts
[0].tex
[0] = 0.0F
;
1924 verts
[0].tex
[1] = 0.0F
;
1928 verts
[1].tex
[0] = tex
->Sright
;
1929 verts
[1].tex
[1] = 0.0F
;
1933 verts
[2].tex
[0] = tex
->Sright
;
1934 verts
[2].tex
[1] = tex
->Ttop
;
1938 verts
[3].tex
[0] = 0.0F
;
1939 verts
[3].tex
[1] = tex
->Ttop
;
1941 /* upload new vertex data */
1942 _mesa_buffer_sub_data(ctx
, copypix
->buf_obj
, 0, sizeof(verts
), verts
);
1945 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1947 /* draw textured quad */
1948 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1950 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1952 _mesa_meta_end(ctx
);
1956 meta_drawpix_cleanup(struct gl_context
*ctx
, struct drawpix_state
*drawpix
)
1958 if (drawpix
->VAO
!= 0) {
1959 _mesa_DeleteVertexArrays(1, &drawpix
->VAO
);
1962 _mesa_reference_buffer_object(ctx
, &drawpix
->buf_obj
, NULL
);
1965 if (drawpix
->StencilFP
!= 0) {
1966 _mesa_DeleteProgramsARB(1, &drawpix
->StencilFP
);
1967 drawpix
->StencilFP
= 0;
1970 if (drawpix
->DepthFP
!= 0) {
1971 _mesa_DeleteProgramsARB(1, &drawpix
->DepthFP
);
1972 drawpix
->DepthFP
= 0;
1977 meta_drawtex_cleanup(struct gl_context
*ctx
, struct drawtex_state
*drawtex
)
1979 if (drawtex
->VAO
!= 0) {
1980 _mesa_DeleteVertexArrays(1, &drawtex
->VAO
);
1983 _mesa_reference_buffer_object(ctx
, &drawtex
->buf_obj
, NULL
);
1988 * When the glDrawPixels() image size is greater than the max rectangle
1989 * texture size we use this function to break the glDrawPixels() image
1990 * into tiles which fit into the max texture size.
1993 tiled_draw_pixels(struct gl_context
*ctx
,
1995 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1996 GLenum format
, GLenum type
,
1997 const struct gl_pixelstore_attrib
*unpack
,
1998 const GLvoid
*pixels
)
2000 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
2003 if (tileUnpack
.RowLength
== 0)
2004 tileUnpack
.RowLength
= width
;
2006 for (i
= 0; i
< width
; i
+= tileSize
) {
2007 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
2008 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
2010 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
2012 for (j
= 0; j
< height
; j
+= tileSize
) {
2013 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
2014 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
2016 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
2018 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
2019 format
, type
, &tileUnpack
, pixels
);
2026 * One-time init for drawing stencil pixels.
2029 init_draw_stencil_pixels(struct gl_context
*ctx
)
2031 /* This program is run eight times, once for each stencil bit.
2032 * The stencil values to draw are found in an 8-bit alpha texture.
2033 * We read the texture/stencil value and test if bit 'b' is set.
2034 * If the bit is not set, use KIL to kill the fragment.
2035 * Finally, we use the stencil test to update the stencil buffer.
2037 * The basic algorithm for checking if a bit is set is:
2038 * if (is_odd(value / (1 << bit)))
2039 * result is one (or non-zero).
2042 * The program parameter contains three values:
2043 * parm.x = 255 / (1 << bit)
2047 static const char *program
=
2049 "PARAM parm = program.local[0]; \n"
2051 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
2052 "# t = t * 255 / bit \n"
2053 "MUL t.x, t.a, parm.x; \n"
2056 "SUB t.x, t.x, t.y; \n"
2058 "MUL t.x, t.x, parm.y; \n"
2059 "# t = fract(t.x) \n"
2060 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2061 "# t.x = (t.x == 0 ? 1 : 0) \n"
2062 "SGE t.x, -t.x, parm.z; \n"
2064 "# for debug only \n"
2065 "#MOV result.color, t.x; \n"
2067 char program2
[1000];
2068 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2069 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
2070 const char *texTarget
;
2072 assert(drawpix
->StencilFP
== 0);
2074 /* replace %s with "RECT" or "2D" */
2075 assert(strlen(program
) + 4 < sizeof(program2
));
2076 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2080 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2082 _mesa_GenProgramsARB(1, &drawpix
->StencilFP
);
2083 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2084 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2085 strlen(program2
), (const GLubyte
*) program2
);
2090 * One-time init for drawing depth pixels.
2093 init_draw_depth_pixels(struct gl_context
*ctx
)
2095 static const char *program
=
2097 "PARAM color = program.local[0]; \n"
2098 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2099 "MOV result.color, color; \n"
2102 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2103 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
2104 const char *texTarget
;
2106 assert(drawpix
->DepthFP
== 0);
2108 /* replace %s with "RECT" or "2D" */
2109 assert(strlen(program
) + 4 < sizeof(program2
));
2110 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2114 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2116 _mesa_GenProgramsARB(1, &drawpix
->DepthFP
);
2117 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2118 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2119 strlen(program2
), (const GLubyte
*) program2
);
2124 * Meta implementation of ctx->Driver.DrawPixels() in terms
2125 * of texture mapping and polygon rendering.
2128 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2129 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2130 GLenum format
, GLenum type
,
2131 const struct gl_pixelstore_attrib
*unpack
,
2132 const GLvoid
*pixels
)
2134 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2135 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
2136 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2137 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2138 struct vertex verts
[4];
2139 GLenum texIntFormat
;
2140 GLboolean fallback
, newTex
;
2141 GLbitfield metaExtraSave
= 0x0;
2144 * Determine if we can do the glDrawPixels with texture mapping.
2146 fallback
= GL_FALSE
;
2147 if (ctx
->Fog
.Enabled
) {
2151 if (_mesa_is_color_format(format
)) {
2152 /* use more compact format when possible */
2153 /* XXX disable special case for GL_LUMINANCE for now to work around
2154 * apparent i965 driver bug (see bug #23670).
2156 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2157 texIntFormat
= format
;
2159 texIntFormat
= GL_RGBA
;
2161 /* If we're not supposed to clamp the resulting color, then just
2162 * promote our texture to fully float. We could do better by
2163 * just going for the matching set of channels, in floating
2166 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2167 ctx
->Extensions
.ARB_texture_float
)
2168 texIntFormat
= GL_RGBA32F
;
2170 else if (_mesa_is_stencil_format(format
)) {
2171 if (ctx
->Extensions
.ARB_fragment_program
&&
2172 ctx
->Pixel
.IndexShift
== 0 &&
2173 ctx
->Pixel
.IndexOffset
== 0 &&
2174 type
== GL_UNSIGNED_BYTE
) {
2175 /* We'll store stencil as alpha. This only works for GLubyte
2176 * image data because of how incoming values are mapped to alpha
2179 texIntFormat
= GL_ALPHA
;
2180 metaExtraSave
= (MESA_META_COLOR_MASK
|
2181 MESA_META_DEPTH_TEST
|
2182 MESA_META_PIXEL_TRANSFER
|
2184 MESA_META_STENCIL_TEST
);
2190 else if (_mesa_is_depth_format(format
)) {
2191 if (ctx
->Extensions
.ARB_depth_texture
&&
2192 ctx
->Extensions
.ARB_fragment_program
) {
2193 texIntFormat
= GL_DEPTH_COMPONENT
;
2194 metaExtraSave
= (MESA_META_SHADER
);
2205 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2206 format
, type
, unpack
, pixels
);
2211 * Check image size against max texture size, draw as tiles if needed.
2213 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2214 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2215 format
, type
, unpack
, pixels
);
2219 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2220 * but a there's a few things we need to override:
2222 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2225 MESA_META_TRANSFORM
|
2228 MESA_META_VIEWPORT
|
2231 newTex
= _mesa_meta_alloc_texture(tex
, width
, height
, texIntFormat
);
2233 _mesa_meta_setup_vertex_objects(ctx
, &drawpix
->VAO
, &drawpix
->buf_obj
, false,
2236 /* Silence valgrind warnings about reading uninitialized stack. */
2237 memset(verts
, 0, sizeof(verts
));
2239 /* vertex positions, texcoords (after texture allocation!) */
2241 const GLfloat x0
= (GLfloat
) x
;
2242 const GLfloat y0
= (GLfloat
) y
;
2243 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2244 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2245 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2250 verts
[0].tex
[0] = 0.0F
;
2251 verts
[0].tex
[1] = 0.0F
;
2255 verts
[1].tex
[0] = tex
->Sright
;
2256 verts
[1].tex
[1] = 0.0F
;
2260 verts
[2].tex
[0] = tex
->Sright
;
2261 verts
[2].tex
[1] = tex
->Ttop
;
2265 verts
[3].tex
[0] = 0.0F
;
2266 verts
[3].tex
[1] = tex
->Ttop
;
2269 /* upload new vertex data */
2270 _mesa_buffer_data(ctx
, drawpix
->buf_obj
, GL_NONE
, sizeof(verts
), verts
,
2271 GL_DYNAMIC_DRAW
, __func__
);
2273 /* set given unpack params */
2274 ctx
->Unpack
= *unpack
;
2276 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2278 if (_mesa_is_stencil_format(format
)) {
2279 /* Drawing stencil */
2282 if (!drawpix
->StencilFP
)
2283 init_draw_stencil_pixels(ctx
);
2285 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2286 GL_ALPHA
, type
, pixels
);
2288 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2290 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2292 /* set all stencil bits to 0 */
2293 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2294 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2295 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2297 /* set stencil bits to 1 where needed */
2298 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2300 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2301 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2303 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2304 const GLuint mask
= 1 << bit
;
2305 if (mask
& origStencilMask
) {
2306 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2307 _mesa_StencilMask(mask
);
2309 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2310 255.0f
/ mask
, 0.5f
, 0.0f
, 0.0f
);
2312 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2316 else if (_mesa_is_depth_format(format
)) {
2318 if (!drawpix
->DepthFP
)
2319 init_draw_depth_pixels(ctx
);
2321 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2322 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2324 /* polygon color = current raster color */
2325 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2326 ctx
->Current
.RasterColor
);
2328 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2329 format
, type
, pixels
);
2331 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2335 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2336 format
, type
, pixels
);
2337 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2340 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2342 /* restore unpack params */
2343 ctx
->Unpack
= unpackSave
;
2345 _mesa_meta_end(ctx
);
2349 alpha_test_raster_color(struct gl_context
*ctx
)
2351 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2352 GLfloat ref
= ctx
->Color
.AlphaRef
;
2354 switch (ctx
->Color
.AlphaFunc
) {
2360 return alpha
== ref
;
2362 return alpha
<= ref
;
2366 return alpha
!= ref
;
2368 return alpha
>= ref
;
2378 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2379 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2380 * tracker would improve performance a lot.
2383 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2384 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2385 const struct gl_pixelstore_attrib
*unpack
,
2386 const GLubyte
*bitmap1
)
2388 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2389 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2390 const GLenum texIntFormat
= GL_ALPHA
;
2391 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2393 struct vertex verts
[4];
2398 * Check if swrast fallback is needed.
2400 if (ctx
->_ImageTransferState
||
2401 _mesa_arb_fragment_program_enabled(ctx
) ||
2403 ctx
->Texture
._MaxEnabledTexImageUnit
!= -1 ||
2404 width
> tex
->MaxSize
||
2405 height
> tex
->MaxSize
) {
2406 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2410 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2413 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2414 * but a there's a few things we need to override:
2416 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
2417 MESA_META_PIXEL_STORE
|
2418 MESA_META_RASTERIZATION
|
2421 MESA_META_TRANSFORM
|
2424 MESA_META_VIEWPORT
));
2426 _mesa_meta_setup_vertex_objects(ctx
, &bitmap
->VAO
, &bitmap
->buf_obj
, false,
2429 newTex
= _mesa_meta_alloc_texture(tex
, width
, height
, texIntFormat
);
2431 /* Silence valgrind warnings about reading uninitialized stack. */
2432 memset(verts
, 0, sizeof(verts
));
2434 /* vertex positions, texcoords, colors (after texture allocation!) */
2436 const GLfloat x0
= (GLfloat
) x
;
2437 const GLfloat y0
= (GLfloat
) y
;
2438 const GLfloat x1
= (GLfloat
) (x
+ width
);
2439 const GLfloat y1
= (GLfloat
) (y
+ height
);
2440 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2446 verts
[0].tex
[0] = 0.0F
;
2447 verts
[0].tex
[1] = 0.0F
;
2451 verts
[1].tex
[0] = tex
->Sright
;
2452 verts
[1].tex
[1] = 0.0F
;
2456 verts
[2].tex
[0] = tex
->Sright
;
2457 verts
[2].tex
[1] = tex
->Ttop
;
2461 verts
[3].tex
[0] = 0.0F
;
2462 verts
[3].tex
[1] = tex
->Ttop
;
2464 for (i
= 0; i
< 4; i
++) {
2465 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2466 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2467 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2468 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2471 /* upload new vertex data */
2472 _mesa_buffer_sub_data(ctx
, bitmap
->buf_obj
, 0, sizeof(verts
), verts
);
2475 /* choose different foreground/background alpha values */
2476 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2477 bg
= (fg
> 127 ? 0 : 255);
2479 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2481 _mesa_meta_end(ctx
);
2485 bitmap8
= malloc(width
* height
);
2487 memset(bitmap8
, bg
, width
* height
);
2488 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2489 bitmap8
, width
, fg
);
2491 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2493 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2494 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2496 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2497 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2499 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2501 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2506 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2508 _mesa_meta_end(ctx
);
2512 * Compute the texture coordinates for the four vertices of a quad for
2513 * drawing a 2D texture image or slice of a cube/3D texture. The offset
2514 * and width, height specify a sub-region of the 2D image.
2516 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2517 * \param slice slice of a 1D/2D array texture or 3D texture
2518 * \param xoffset X position of sub texture
2519 * \param yoffset Y position of sub texture
2520 * \param width width of the sub texture image
2521 * \param height height of the sub texture image
2522 * \param total_width total width of the texture image
2523 * \param total_height total height of the texture image
2524 * \param total_depth total depth of the texture image
2525 * \param coords0/1/2/3 returns the computed texcoords
2528 _mesa_meta_setup_texture_coords(GLenum faceTarget
,
2544 const float s0
= (float) xoffset
/ (float) total_width
;
2545 const float s1
= (float) (xoffset
+ width
) / (float) total_width
;
2546 const float t0
= (float) yoffset
/ (float) total_height
;
2547 const float t1
= (float) (yoffset
+ height
) / (float) total_height
;
2550 /* setup the reference texcoords */
2560 if (faceTarget
== GL_TEXTURE_CUBE_MAP_ARRAY
)
2561 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ slice
% 6;
2563 /* Currently all texture targets want the W component to be 1.0.
2570 switch (faceTarget
) {
2574 case GL_TEXTURE_2D_ARRAY
:
2575 if (faceTarget
== GL_TEXTURE_3D
) {
2576 assert(slice
< total_depth
);
2577 assert(total_depth
>= 1);
2578 r
= (slice
+ 0.5f
) / total_depth
;
2580 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
2584 coords0
[0] = st
[0][0]; /* s */
2585 coords0
[1] = st
[0][1]; /* t */
2586 coords0
[2] = r
; /* r */
2587 coords1
[0] = st
[1][0];
2588 coords1
[1] = st
[1][1];
2590 coords2
[0] = st
[2][0];
2591 coords2
[1] = st
[2][1];
2593 coords3
[0] = st
[3][0];
2594 coords3
[1] = st
[3][1];
2597 case GL_TEXTURE_RECTANGLE_ARB
:
2598 coords0
[0] = (float) xoffset
; /* s */
2599 coords0
[1] = (float) yoffset
; /* t */
2600 coords0
[2] = 0.0F
; /* r */
2601 coords1
[0] = (float) (xoffset
+ width
);
2602 coords1
[1] = (float) yoffset
;
2604 coords2
[0] = (float) (xoffset
+ width
);
2605 coords2
[1] = (float) (yoffset
+ height
);
2607 coords3
[0] = (float) xoffset
;
2608 coords3
[1] = (float) (yoffset
+ height
);
2611 case GL_TEXTURE_1D_ARRAY
:
2612 coords0
[0] = st
[0][0]; /* s */
2613 coords0
[1] = (float) slice
; /* t */
2614 coords0
[2] = 0.0F
; /* r */
2615 coords1
[0] = st
[1][0];
2616 coords1
[1] = (float) slice
;
2618 coords2
[0] = st
[2][0];
2619 coords2
[1] = (float) slice
;
2621 coords3
[0] = st
[3][0];
2622 coords3
[1] = (float) slice
;
2626 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2627 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2628 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2629 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2630 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2631 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2632 /* loop over quad verts */
2633 for (i
= 0; i
< 4; i
++) {
2634 /* Compute sc = +/-scale and tc = +/-scale.
2635 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2636 * though that can still sometimes happen with this scale factor...
2638 const GLfloat scale
= 0.9999f
;
2639 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2640 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2657 unreachable("not reached");
2660 coord
[3] = (float) (slice
/ 6);
2662 switch (faceTarget
) {
2663 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2668 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2673 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2678 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2683 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2688 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2699 assert(!"unexpected target in _mesa_meta_setup_texture_coords()");
2703 static struct blit_shader
*
2704 choose_blit_shader(GLenum target
, struct blit_shader_table
*table
)
2708 table
->sampler_1d
.type
= "sampler1D";
2709 table
->sampler_1d
.func
= "texture1D";
2710 table
->sampler_1d
.texcoords
= "texCoords.x";
2711 return &table
->sampler_1d
;
2713 table
->sampler_2d
.type
= "sampler2D";
2714 table
->sampler_2d
.func
= "texture2D";
2715 table
->sampler_2d
.texcoords
= "texCoords.xy";
2716 return &table
->sampler_2d
;
2717 case GL_TEXTURE_RECTANGLE
:
2718 table
->sampler_rect
.type
= "sampler2DRect";
2719 table
->sampler_rect
.func
= "texture2DRect";
2720 table
->sampler_rect
.texcoords
= "texCoords.xy";
2721 return &table
->sampler_rect
;
2723 /* Code for mipmap generation with 3D textures is not used yet.
2724 * It's a sw fallback.
2726 table
->sampler_3d
.type
= "sampler3D";
2727 table
->sampler_3d
.func
= "texture3D";
2728 table
->sampler_3d
.texcoords
= "texCoords.xyz";
2729 return &table
->sampler_3d
;
2730 case GL_TEXTURE_CUBE_MAP
:
2731 table
->sampler_cubemap
.type
= "samplerCube";
2732 table
->sampler_cubemap
.func
= "textureCube";
2733 table
->sampler_cubemap
.texcoords
= "texCoords.xyz";
2734 return &table
->sampler_cubemap
;
2735 case GL_TEXTURE_1D_ARRAY
:
2736 table
->sampler_1d_array
.type
= "sampler1DArray";
2737 table
->sampler_1d_array
.func
= "texture1DArray";
2738 table
->sampler_1d_array
.texcoords
= "texCoords.xy";
2739 return &table
->sampler_1d_array
;
2740 case GL_TEXTURE_2D_ARRAY
:
2741 table
->sampler_2d_array
.type
= "sampler2DArray";
2742 table
->sampler_2d_array
.func
= "texture2DArray";
2743 table
->sampler_2d_array
.texcoords
= "texCoords.xyz";
2744 return &table
->sampler_2d_array
;
2745 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2746 table
->sampler_cubemap_array
.type
= "samplerCubeArray";
2747 table
->sampler_cubemap_array
.func
= "textureCubeArray";
2748 table
->sampler_cubemap_array
.texcoords
= "texCoords.xyzw";
2749 return &table
->sampler_cubemap_array
;
2751 _mesa_problem(NULL
, "Unexpected texture target 0x%x in"
2752 " setup_texture_sampler()\n", target
);
2758 _mesa_meta_blit_shader_table_cleanup(struct gl_context
*ctx
,
2759 struct blit_shader_table
*table
)
2761 _mesa_reference_shader_program(ctx
, &table
->sampler_1d
.shader_prog
, NULL
);
2762 _mesa_reference_shader_program(ctx
, &table
->sampler_2d
.shader_prog
, NULL
);
2763 _mesa_reference_shader_program(ctx
, &table
->sampler_3d
.shader_prog
, NULL
);
2764 _mesa_reference_shader_program(ctx
, &table
->sampler_rect
.shader_prog
, NULL
);
2765 _mesa_reference_shader_program(ctx
, &table
->sampler_cubemap
.shader_prog
, NULL
);
2766 _mesa_reference_shader_program(ctx
, &table
->sampler_1d_array
.shader_prog
, NULL
);
2767 _mesa_reference_shader_program(ctx
, &table
->sampler_2d_array
.shader_prog
, NULL
);
2768 _mesa_reference_shader_program(ctx
, &table
->sampler_cubemap_array
.shader_prog
, NULL
);
2772 * Determine the GL data type to use for the temporary image read with
2773 * ReadPixels() and passed to Tex[Sub]Image().
2776 get_temp_image_type(struct gl_context
*ctx
, mesa_format format
)
2778 const GLenum baseFormat
= _mesa_get_format_base_format(format
);
2779 const GLenum datatype
= _mesa_get_format_datatype(format
);
2780 const GLint format_red_bits
= _mesa_get_format_bits(format
, GL_RED_BITS
);
2782 switch (baseFormat
) {
2789 case GL_LUMINANCE_ALPHA
:
2791 if (datatype
== GL_INT
|| datatype
== GL_UNSIGNED_INT
) {
2793 } else if (format_red_bits
<= 8) {
2794 return GL_UNSIGNED_BYTE
;
2795 } else if (format_red_bits
<= 16) {
2796 return GL_UNSIGNED_SHORT
;
2799 case GL_DEPTH_COMPONENT
:
2800 if (datatype
== GL_FLOAT
)
2803 return GL_UNSIGNED_INT
;
2804 case GL_DEPTH_STENCIL
:
2805 if (datatype
== GL_FLOAT
)
2806 return GL_FLOAT_32_UNSIGNED_INT_24_8_REV
;
2808 return GL_UNSIGNED_INT_24_8
;
2810 _mesa_problem(ctx
, "Unexpected format %d in get_temp_image_type()",
2817 * Attempts to wrap the destination texture in an FBO and use
2818 * glBlitFramebuffer() to implement glCopyTexSubImage().
2821 copytexsubimage_using_blit_framebuffer(struct gl_context
*ctx
,
2822 struct gl_texture_image
*texImage
,
2826 struct gl_renderbuffer
*rb
,
2828 GLsizei width
, GLsizei height
)
2830 struct gl_framebuffer
*drawFb
;
2831 bool success
= false;
2835 if (!ctx
->Extensions
.ARB_framebuffer_object
)
2838 drawFb
= ctx
->Driver
.NewFramebuffer(ctx
, 0xDEADBEEF);
2842 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_DRAW_BUFFERS
);
2843 _mesa_bind_framebuffers(ctx
, drawFb
, ctx
->ReadBuffer
);
2845 if (rb
->_BaseFormat
== GL_DEPTH_STENCIL
||
2846 rb
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2847 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
2848 GL_DEPTH_ATTACHMENT
,
2850 mask
= GL_DEPTH_BUFFER_BIT
;
2852 if (rb
->_BaseFormat
== GL_DEPTH_STENCIL
&&
2853 texImage
->_BaseFormat
== GL_DEPTH_STENCIL
) {
2854 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
2855 GL_STENCIL_ATTACHMENT
,
2857 mask
|= GL_STENCIL_BUFFER_BIT
;
2859 _mesa_DrawBuffer(GL_NONE
);
2861 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
2862 GL_COLOR_ATTACHMENT0
,
2864 mask
= GL_COLOR_BUFFER_BIT
;
2865 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0
);
2868 status
= _mesa_check_framebuffer_status(ctx
, ctx
->DrawBuffer
);
2869 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
2872 ctx
->Meta
->Blit
.no_ctsi_fallback
= true;
2874 /* Since we've bound a new draw framebuffer, we need to update
2875 * its derived state -- _Xmin, etc -- for BlitFramebuffer's clipping to
2878 _mesa_update_state(ctx
);
2880 /* We skip the core BlitFramebuffer checks for format consistency, which
2881 * are too strict for CopyTexImage. We know meta will be fine with format
2884 mask
= _mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
2886 x
+ width
, y
+ height
,
2888 xoffset
+ width
, yoffset
+ height
,
2890 ctx
->Meta
->Blit
.no_ctsi_fallback
= false;
2891 success
= mask
== 0x0;
2894 _mesa_reference_framebuffer(&drawFb
, NULL
);
2895 _mesa_meta_end(ctx
);
2900 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2901 * Have to be careful with locking and meta state for pixel transfer.
2904 _mesa_meta_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2905 struct gl_texture_image
*texImage
,
2906 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2907 struct gl_renderbuffer
*rb
,
2909 GLsizei width
, GLsizei height
)
2911 GLenum format
, type
;
2915 if (copytexsubimage_using_blit_framebuffer(ctx
,
2917 xoffset
, yoffset
, zoffset
,
2924 /* Choose format/type for temporary image buffer */
2925 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
2926 if (format
== GL_LUMINANCE
||
2927 format
== GL_LUMINANCE_ALPHA
||
2928 format
== GL_INTENSITY
) {
2929 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
2930 * temp image buffer because glReadPixels will do L=R+G+B which is
2931 * not what we want (should be L=R).
2936 type
= get_temp_image_type(ctx
, texImage
->TexFormat
);
2937 if (_mesa_is_format_integer_color(texImage
->TexFormat
)) {
2938 format
= _mesa_base_format_to_integer_format(format
);
2940 bpp
= _mesa_bytes_per_pixel(format
, type
);
2942 _mesa_problem(ctx
, "Bad bpp in _mesa_meta_CopyTexSubImage()");
2947 * Alloc image buffer (XXX could use a PBO)
2949 buf
= malloc(width
* height
* bpp
);
2951 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
2956 * Read image from framebuffer (disable pixel transfer ops)
2958 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
2959 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2960 format
, type
, &ctx
->Pack
, buf
);
2961 _mesa_meta_end(ctx
);
2963 _mesa_update_state(ctx
); /* to update pixel transfer state */
2966 * Store texture data (with pixel transfer ops)
2968 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
2970 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
2971 assert(yoffset
== 0);
2972 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
2973 xoffset
, zoffset
, 0, width
, 1, 1,
2974 format
, type
, buf
, &ctx
->Unpack
);
2976 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
2977 xoffset
, yoffset
, zoffset
, width
, height
, 1,
2978 format
, type
, buf
, &ctx
->Unpack
);
2981 _mesa_meta_end(ctx
);
2987 meta_decompress_fbo_cleanup(struct decompress_fbo_state
*decompress_fbo
)
2989 if (decompress_fbo
->fb
!= NULL
) {
2990 _mesa_reference_framebuffer(&decompress_fbo
->fb
, NULL
);
2991 _mesa_reference_renderbuffer(&decompress_fbo
->rb
, NULL
);
2994 memset(decompress_fbo
, 0, sizeof(*decompress_fbo
));
2998 meta_decompress_cleanup(struct gl_context
*ctx
,
2999 struct decompress_state
*decompress
)
3001 meta_decompress_fbo_cleanup(&decompress
->byteFBO
);
3002 meta_decompress_fbo_cleanup(&decompress
->floatFBO
);
3004 if (decompress
->VAO
!= 0) {
3005 _mesa_DeleteVertexArrays(1, &decompress
->VAO
);
3006 _mesa_reference_buffer_object(ctx
, &decompress
->buf_obj
, NULL
);
3009 _mesa_reference_sampler_object(ctx
, &decompress
->samp_obj
, NULL
);
3010 _mesa_meta_blit_shader_table_cleanup(ctx
, &decompress
->shaders
);
3012 memset(decompress
, 0, sizeof(*decompress
));
3016 * Decompress a texture image by drawing a quad with the compressed
3017 * texture and reading the pixels out of the color buffer.
3018 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
3019 * \param destFormat format, ala glReadPixels
3020 * \param destType type, ala glReadPixels
3021 * \param dest destination buffer
3022 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
3025 decompress_texture_image(struct gl_context
*ctx
,
3026 struct gl_texture_image
*texImage
,
3028 GLint xoffset
, GLint yoffset
,
3029 GLsizei width
, GLsizei height
,
3030 GLenum destFormat
, GLenum destType
,
3033 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
3034 struct decompress_fbo_state
*decompress_fbo
;
3035 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3036 const GLenum target
= texObj
->Target
;
3039 struct vertex verts
[4];
3040 struct gl_sampler_object
*samp_obj_save
= NULL
;
3042 const bool use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
3043 ctx
->Extensions
.ARB_fragment_shader
;
3045 switch (_mesa_get_format_datatype(texImage
->TexFormat
)) {
3047 decompress_fbo
= &decompress
->floatFBO
;
3048 rbFormat
= GL_RGBA32F
;
3050 case GL_UNSIGNED_NORMALIZED
:
3051 decompress_fbo
= &decompress
->byteFBO
;
3059 assert(target
== GL_TEXTURE_3D
||
3060 target
== GL_TEXTURE_2D_ARRAY
||
3061 target
== GL_TEXTURE_CUBE_MAP_ARRAY
);
3066 case GL_TEXTURE_1D_ARRAY
:
3067 assert(!"No compressed 1D textures.");
3070 case GL_TEXTURE_CUBE_MAP_ARRAY
:
3071 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ (slice
% 6);
3074 case GL_TEXTURE_CUBE_MAP
:
3075 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
3079 faceTarget
= target
;
3083 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~(MESA_META_PIXEL_STORE
|
3084 MESA_META_DRAW_BUFFERS
));
3085 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
3087 _mesa_reference_sampler_object(ctx
, &samp_obj_save
,
3088 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
);
3090 /* Create/bind FBO/renderbuffer */
3091 if (decompress_fbo
->fb
== NULL
) {
3092 decompress_fbo
->rb
= ctx
->Driver
.NewRenderbuffer(ctx
, 0xDEADBEEF);
3093 if (decompress_fbo
->rb
== NULL
) {
3094 _mesa_meta_end(ctx
);
3098 decompress_fbo
->fb
= ctx
->Driver
.NewFramebuffer(ctx
, 0xDEADBEEF);
3099 if (decompress_fbo
->fb
== NULL
) {
3100 _mesa_meta_end(ctx
);
3104 _mesa_bind_framebuffers(ctx
, decompress_fbo
->fb
, decompress_fbo
->fb
);
3105 _mesa_framebuffer_renderbuffer(ctx
, ctx
->DrawBuffer
, GL_COLOR_ATTACHMENT0
,
3106 decompress_fbo
->rb
);
3109 _mesa_bind_framebuffers(ctx
, decompress_fbo
->fb
, decompress_fbo
->fb
);
3112 /* alloc dest surface */
3113 if (width
> decompress_fbo
->Width
|| height
> decompress_fbo
->Height
) {
3114 _mesa_renderbuffer_storage(ctx
, decompress_fbo
->rb
, rbFormat
,
3115 width
, height
, 0, 0);
3117 /* Do the full completeness check to recompute
3118 * ctx->DrawBuffer->Width/Height.
3120 ctx
->DrawBuffer
->_Status
= GL_FRAMEBUFFER_UNDEFINED
;
3121 status
= _mesa_check_framebuffer_status(ctx
, ctx
->DrawBuffer
);
3122 if (status
!= GL_FRAMEBUFFER_COMPLETE
) {
3123 /* If the framebuffer isn't complete then we'll leave
3124 * decompress_fbo->Width as zero so that it will fail again next time
3126 _mesa_meta_end(ctx
);
3129 decompress_fbo
->Width
= width
;
3130 decompress_fbo
->Height
= height
;
3133 if (use_glsl_version
) {
3134 _mesa_meta_setup_vertex_objects(ctx
, &decompress
->VAO
,
3135 &decompress
->buf_obj
, true,
3138 _mesa_meta_setup_blit_shader(ctx
, target
, false, &decompress
->shaders
);
3140 _mesa_meta_setup_ff_tnl_for_blit(ctx
, &decompress
->VAO
,
3141 &decompress
->buf_obj
, 3);
3144 if (decompress
->samp_obj
== NULL
) {
3145 decompress
->samp_obj
= ctx
->Driver
.NewSamplerObject(ctx
, 0xDEADBEEF);
3146 if (decompress
->samp_obj
== NULL
) {
3147 _mesa_meta_end(ctx
);
3149 /* This is a bit lazy. Flag out of memory, and then don't bother to
3150 * clean up. Once out of memory is flagged, the only realistic next
3151 * move is to destroy the context. That will trigger all the right
3154 * Returning true prevents other GetTexImage methods from attempting
3155 * anything since they will likely fail too.
3157 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
3161 /* nearest filtering */
3162 _mesa_set_sampler_filters(ctx
, decompress
->samp_obj
, GL_NEAREST
, GL_NEAREST
);
3164 /* We don't want to encode or decode sRGB values; treat them as linear. */
3165 _mesa_set_sampler_srgb_decode(ctx
, decompress
->samp_obj
, GL_SKIP_DECODE_EXT
);
3168 _mesa_bind_sampler(ctx
, ctx
->Texture
.CurrentUnit
, decompress
->samp_obj
);
3170 /* Silence valgrind warnings about reading uninitialized stack. */
3171 memset(verts
, 0, sizeof(verts
));
3173 _mesa_meta_setup_texture_coords(faceTarget
, slice
,
3174 xoffset
, yoffset
, width
, height
,
3175 texImage
->Width
, texImage
->Height
,
3182 /* setup vertex positions */
3192 _mesa_set_viewport(ctx
, 0, 0, 0, width
, height
);
3194 /* upload new vertex data */
3195 _mesa_buffer_sub_data(ctx
, decompress
->buf_obj
, 0, sizeof(verts
), verts
);
3197 /* setup texture state */
3198 _mesa_bind_texture(ctx
, target
, texObj
);
3200 if (!use_glsl_version
)
3201 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3204 /* save texture object state */
3205 const GLint baseLevelSave
= texObj
->BaseLevel
;
3206 const GLint maxLevelSave
= texObj
->MaxLevel
;
3208 /* restrict sampling to the texture level of interest */
3209 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3210 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_BASE_LEVEL
,
3211 (GLint
*) &texImage
->Level
, false);
3212 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_MAX_LEVEL
,
3213 (GLint
*) &texImage
->Level
, false);
3216 /* render quad w/ texture into renderbuffer */
3217 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3219 /* Restore texture object state, the texture binding will
3220 * be restored by _mesa_meta_end().
3222 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3223 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_BASE_LEVEL
,
3224 &baseLevelSave
, false);
3225 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_MAX_LEVEL
,
3226 &maxLevelSave
, false);
3231 /* read pixels from renderbuffer */
3233 GLenum baseTexFormat
= texImage
->_BaseFormat
;
3234 GLenum destBaseFormat
= _mesa_unpack_format_to_base_format(destFormat
);
3236 /* The pixel transfer state will be set to default values at this point
3237 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
3238 * turned off (as required by glGetTexImage) but we need to handle some
3239 * special cases. In particular, single-channel texture values are
3240 * returned as red and two-channel texture values are returned as
3243 if (_mesa_need_luminance_to_rgb_conversion(baseTexFormat
,
3245 /* If we're reading back an RGB(A) texture (using glGetTexImage) as
3246 * luminance then we need to return L=tex(R).
3248 _mesa_need_rgb_to_luminance_conversion(baseTexFormat
,
3250 /* Green and blue must be zero */
3251 _mesa_PixelTransferf(GL_GREEN_SCALE
, 0.0f
);
3252 _mesa_PixelTransferf(GL_BLUE_SCALE
, 0.0f
);
3255 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
3258 /* disable texture unit */
3259 if (!use_glsl_version
)
3260 _mesa_set_enable(ctx
, target
, GL_FALSE
);
3262 _mesa_bind_sampler(ctx
, ctx
->Texture
.CurrentUnit
, samp_obj_save
);
3263 _mesa_reference_sampler_object(ctx
, &samp_obj_save
, NULL
);
3265 _mesa_meta_end(ctx
);
3272 * This is just a wrapper around _mesa_get_tex_image() and
3273 * decompress_texture_image(). Meta functions should not be directly called
3277 _mesa_meta_GetTexSubImage(struct gl_context
*ctx
,
3278 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3279 GLsizei width
, GLsizei height
, GLsizei depth
,
3280 GLenum format
, GLenum type
, GLvoid
*pixels
,
3281 struct gl_texture_image
*texImage
)
3283 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
3287 for (slice
= 0; slice
< depth
; slice
++) {
3289 /* Section 8.11.4 (Texture Image Queries) of the GL 4.5 spec says:
3291 * "For three-dimensional, two-dimensional array, cube map array,
3292 * and cube map textures pixel storage operations are applied as
3293 * if the image were two-dimensional, except that the additional
3294 * pixel storage state values PACK_IMAGE_HEIGHT and
3295 * PACK_SKIP_IMAGES are applied. The correspondence of texels to
3296 * memory locations is as defined for TexImage3D in section 8.5."
3298 switch (texImage
->TexObject
->Target
) {
3300 case GL_TEXTURE_2D_ARRAY
:
3301 case GL_TEXTURE_CUBE_MAP
:
3302 case GL_TEXTURE_CUBE_MAP_ARRAY
: {
3303 /* Setup pixel packing. SkipPixels and SkipRows will be applied
3304 * in the decompress_texture_image() function's call to
3305 * glReadPixels but we need to compute the dest slice's address
3306 * here (according to SkipImages and ImageHeight).
3308 struct gl_pixelstore_attrib packing
= ctx
->Pack
;
3309 packing
.SkipPixels
= 0;
3310 packing
.SkipRows
= 0;
3311 dst
= _mesa_image_address3d(&packing
, pixels
, width
, height
,
3312 format
, type
, slice
, 0, 0);
3319 result
= decompress_texture_image(ctx
, texImage
, slice
,
3320 xoffset
, yoffset
, width
, height
,
3330 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
3331 width
, height
, depth
, format
, type
, pixels
, texImage
);
3336 * Meta implementation of ctx->Driver.DrawTex() in terms
3337 * of polygon rendering.
3340 _mesa_meta_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
3341 GLfloat width
, GLfloat height
)
3343 struct drawtex_state
*drawtex
= &ctx
->Meta
->DrawTex
;
3345 GLfloat x
, y
, z
, st
[MAX_TEXTURE_UNITS
][2];
3347 struct vertex verts
[4];
3350 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
3352 MESA_META_TRANSFORM
|
3354 MESA_META_VIEWPORT
));
3356 if (drawtex
->VAO
== 0) {
3357 /* one-time setup */
3358 struct gl_vertex_array_object
*array_obj
;
3360 /* create vertex array object */
3361 _mesa_GenVertexArrays(1, &drawtex
->VAO
);
3362 _mesa_BindVertexArray(drawtex
->VAO
);
3364 array_obj
= _mesa_lookup_vao(ctx
, drawtex
->VAO
);
3365 assert(array_obj
!= NULL
);
3367 /* create vertex array buffer */
3368 drawtex
->buf_obj
= ctx
->Driver
.NewBufferObject(ctx
, 0xDEADBEEF);
3369 if (drawtex
->buf_obj
== NULL
)
3372 _mesa_buffer_data(ctx
, drawtex
->buf_obj
, GL_NONE
, sizeof(verts
), verts
,
3373 GL_DYNAMIC_DRAW
, __func__
);
3375 /* setup vertex arrays */
3376 FLUSH_VERTICES(ctx
, 0);
3377 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_POS
,
3378 3, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
3380 offsetof(struct vertex
, x
));
3381 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_POS
,
3382 drawtex
->buf_obj
, 0, sizeof(struct vertex
));
3383 _mesa_enable_vertex_array_attrib(ctx
, array_obj
, VERT_ATTRIB_POS
);
3386 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3387 FLUSH_VERTICES(ctx
, 0);
3388 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_TEX(i
),
3389 2, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
3391 offsetof(struct vertex
, st
[i
]));
3392 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_TEX(i
),
3393 drawtex
->buf_obj
, 0, sizeof(struct vertex
));
3394 _mesa_enable_vertex_array_attrib(ctx
, array_obj
, VERT_ATTRIB_TEX(i
));
3398 _mesa_BindVertexArray(drawtex
->VAO
);
3401 /* vertex positions, texcoords */
3403 const GLfloat x1
= x
+ width
;
3404 const GLfloat y1
= y
+ height
;
3406 z
= CLAMP(z
, 0.0f
, 1.0f
);
3425 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3426 const struct gl_texture_object
*texObj
;
3427 const struct gl_texture_image
*texImage
;
3428 GLfloat s
, t
, s1
, t1
;
3431 if (!ctx
->Texture
.Unit
[i
]._Current
) {
3433 for (j
= 0; j
< 4; j
++) {
3434 verts
[j
].st
[i
][0] = 0.0f
;
3435 verts
[j
].st
[i
][1] = 0.0f
;
3440 texObj
= ctx
->Texture
.Unit
[i
]._Current
;
3441 texImage
= texObj
->Image
[0][texObj
->BaseLevel
];
3442 tw
= texImage
->Width2
;
3443 th
= texImage
->Height2
;
3445 s
= (GLfloat
) texObj
->CropRect
[0] / tw
;
3446 t
= (GLfloat
) texObj
->CropRect
[1] / th
;
3447 s1
= (GLfloat
) (texObj
->CropRect
[0] + texObj
->CropRect
[2]) / tw
;
3448 t1
= (GLfloat
) (texObj
->CropRect
[1] + texObj
->CropRect
[3]) / th
;
3450 verts
[0].st
[i
][0] = s
;
3451 verts
[0].st
[i
][1] = t
;
3453 verts
[1].st
[i
][0] = s1
;
3454 verts
[1].st
[i
][1] = t
;
3456 verts
[2].st
[i
][0] = s1
;
3457 verts
[2].st
[i
][1] = t1
;
3459 verts
[3].st
[i
][0] = s
;
3460 verts
[3].st
[i
][1] = t1
;
3463 _mesa_buffer_sub_data(ctx
, drawtex
->buf_obj
, 0, sizeof(verts
), verts
);
3466 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3468 _mesa_meta_end(ctx
);
3472 cleartexsubimage_color(struct gl_context
*ctx
,
3473 struct gl_texture_image
*texImage
,
3474 const GLvoid
*clearValue
,
3478 union gl_color_union colorValue
;
3482 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
3483 GL_COLOR_ATTACHMENT0
,
3486 status
= _mesa_check_framebuffer_status(ctx
, ctx
->DrawBuffer
);
3487 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
3490 /* We don't want to apply an sRGB conversion so override the format */
3491 format
= _mesa_get_srgb_format_linear(texImage
->TexFormat
);
3492 datatype
= _mesa_get_format_datatype(format
);
3495 case GL_UNSIGNED_INT
:
3498 _mesa_unpack_uint_rgba_row(format
, 1, clearValue
,
3499 (GLuint (*)[4]) colorValue
.ui
);
3501 memset(&colorValue
, 0, sizeof colorValue
);
3502 if (datatype
== GL_INT
)
3503 _mesa_ClearBufferiv(GL_COLOR
, 0, colorValue
.i
);
3505 _mesa_ClearBufferuiv(GL_COLOR
, 0, colorValue
.ui
);
3509 _mesa_unpack_rgba_row(format
, 1, clearValue
,
3510 (GLfloat (*)[4]) colorValue
.f
);
3512 memset(&colorValue
, 0, sizeof colorValue
);
3513 _mesa_ClearBufferfv(GL_COLOR
, 0, colorValue
.f
);
3521 cleartexsubimage_depth_stencil(struct gl_context
*ctx
,
3522 struct gl_texture_image
*texImage
,
3523 const GLvoid
*clearValue
,
3526 GLint stencilValue
= 0;
3527 GLfloat depthValue
= 0.0f
;
3530 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
3531 GL_DEPTH_ATTACHMENT
,
3534 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
)
3535 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
3536 GL_STENCIL_ATTACHMENT
,
3539 status
= _mesa_check_framebuffer_status(ctx
, ctx
->DrawBuffer
);
3540 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
3544 GLuint depthStencilValue
[2];
3546 /* Convert the clearValue from whatever format it's in to a floating
3547 * point value for the depth and an integer value for the stencil index
3549 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
) {
3550 _mesa_unpack_float_32_uint_24_8_depth_stencil_row(texImage
->TexFormat
,
3554 /* We need a memcpy here instead of a cast because we need to
3555 * reinterpret the bytes as a float rather than converting it
3557 memcpy(&depthValue
, depthStencilValue
, sizeof depthValue
);
3558 stencilValue
= depthStencilValue
[1] & 0xff;
3560 _mesa_unpack_float_z_row(texImage
->TexFormat
, 1 /* n */,
3561 clearValue
, &depthValue
);
3565 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
)
3566 _mesa_ClearBufferfi(GL_DEPTH_STENCIL
, 0, depthValue
, stencilValue
);
3568 _mesa_ClearBufferfv(GL_DEPTH
, 0, &depthValue
);
3574 cleartexsubimage_for_zoffset(struct gl_context
*ctx
,
3575 struct gl_texture_image
*texImage
,
3577 const GLvoid
*clearValue
)
3579 struct gl_framebuffer
*drawFb
;
3582 drawFb
= ctx
->Driver
.NewFramebuffer(ctx
, 0xDEADBEEF);
3586 _mesa_bind_framebuffers(ctx
, drawFb
, ctx
->ReadBuffer
);
3588 switch(texImage
->_BaseFormat
) {
3589 case GL_DEPTH_STENCIL
:
3590 case GL_DEPTH_COMPONENT
:
3591 success
= cleartexsubimage_depth_stencil(ctx
, texImage
,
3592 clearValue
, zoffset
);
3595 success
= cleartexsubimage_color(ctx
, texImage
, clearValue
, zoffset
);
3599 _mesa_reference_framebuffer(&drawFb
, NULL
);
3605 cleartexsubimage_using_fbo(struct gl_context
*ctx
,
3606 struct gl_texture_image
*texImage
,
3607 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3608 GLsizei width
, GLsizei height
, GLsizei depth
,
3609 const GLvoid
*clearValue
)
3611 bool success
= true;
3614 _mesa_meta_begin(ctx
,
3616 MESA_META_COLOR_MASK
|
3618 MESA_META_FRAMEBUFFER_SRGB
);
3620 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
3621 _mesa_set_enable(ctx
, GL_DITHER
, GL_FALSE
);
3623 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_TRUE
);
3624 _mesa_Scissor(xoffset
, yoffset
, width
, height
);
3626 for (z
= zoffset
; z
< zoffset
+ depth
; z
++) {
3627 if (!cleartexsubimage_for_zoffset(ctx
, texImage
, z
, clearValue
)) {
3633 _mesa_meta_end(ctx
);
3639 _mesa_meta_ClearTexSubImage(struct gl_context
*ctx
,
3640 struct gl_texture_image
*texImage
,
3641 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3642 GLsizei width
, GLsizei height
, GLsizei depth
,
3643 const GLvoid
*clearValue
)
3647 res
= cleartexsubimage_using_fbo(ctx
, texImage
,
3648 xoffset
, yoffset
, zoffset
,
3649 width
, height
, depth
,
3656 "Falling back to mapping the texture in "
3657 "glClearTexSubImage\n");
3659 _mesa_store_cleartexsubimage(ctx
, texImage
,
3660 xoffset
, yoffset
, zoffset
,
3661 width
, height
, depth
,