2 * Mesa 3-D graphics library
4 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
26 * Meta operations. Some GL operations can be expressed in terms of
27 * other GL operations. For example, glBlitFramebuffer() can be done
28 * with texture mapping and glClear() can be done with polygon rendering.
34 #include "main/glheader.h"
35 #include "main/mtypes.h"
36 #include "main/imports.h"
37 #include "main/arbprogram.h"
38 #include "main/arrayobj.h"
39 #include "main/blend.h"
40 #include "main/blit.h"
41 #include "main/bufferobj.h"
42 #include "main/buffers.h"
43 #include "main/clear.h"
44 #include "main/condrender.h"
45 #include "main/depth.h"
46 #include "main/enable.h"
47 #include "main/fbobject.h"
48 #include "main/feedback.h"
49 #include "main/formats.h"
50 #include "main/format_unpack.h"
51 #include "main/framebuffer.h"
52 #include "main/glformats.h"
53 #include "main/image.h"
54 #include "main/macros.h"
55 #include "main/matrix.h"
56 #include "main/mipmap.h"
57 #include "main/multisample.h"
58 #include "main/objectlabel.h"
59 #include "main/pipelineobj.h"
60 #include "main/pixel.h"
62 #include "main/polygon.h"
63 #include "main/queryobj.h"
64 #include "main/readpix.h"
65 #include "main/renderbuffer.h"
66 #include "main/scissor.h"
67 #include "main/shaderapi.h"
68 #include "main/shaderobj.h"
69 #include "main/state.h"
70 #include "main/stencil.h"
71 #include "main/texobj.h"
72 #include "main/texenv.h"
73 #include "main/texgetimage.h"
74 #include "main/teximage.h"
75 #include "main/texparam.h"
76 #include "main/texstate.h"
77 #include "main/texstore.h"
78 #include "main/transformfeedback.h"
79 #include "main/uniforms.h"
80 #include "main/varray.h"
81 #include "main/viewport.h"
82 #include "main/samplerobj.h"
83 #include "program/program.h"
84 #include "swrast/swrast.h"
85 #include "drivers/common/meta.h"
86 #include "main/enums.h"
87 #include "main/glformats.h"
88 #include "util/bitscan.h"
89 #include "util/ralloc.h"
91 /** Return offset in bytes of the field within a vertex struct */
92 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
95 meta_clear(struct gl_context
*ctx
, GLbitfield buffers
, bool glsl
);
97 static struct blit_shader
*
98 choose_blit_shader(GLenum target
, struct blit_shader_table
*table
);
100 static void cleanup_temp_texture(struct temp_texture
*tex
);
101 static void meta_glsl_clear_cleanup(struct gl_context
*ctx
,
102 struct clear_state
*clear
);
103 static void meta_decompress_cleanup(struct gl_context
*ctx
,
104 struct decompress_state
*decompress
);
105 static void meta_drawpix_cleanup(struct gl_context
*ctx
,
106 struct drawpix_state
*drawpix
);
109 _mesa_meta_framebuffer_texture_image(struct gl_context
*ctx
,
110 struct gl_framebuffer
*fb
,
112 struct gl_texture_image
*texImage
,
115 struct gl_texture_object
*texObj
= texImage
->TexObject
;
116 int level
= texImage
->Level
;
117 const GLenum texTarget
= texObj
->Target
== GL_TEXTURE_CUBE_MAP
118 ? GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
121 struct gl_renderbuffer_attachment
*att
=
122 _mesa_get_and_validate_attachment(ctx
, fb
, attachment
, __func__
);
125 _mesa_framebuffer_texture(ctx
, fb
, attachment
, att
, texObj
, texTarget
,
126 level
, layer
, false);
129 static struct gl_shader
*
130 meta_compile_shader_with_debug(struct gl_context
*ctx
, gl_shader_stage stage
,
131 const GLcharARB
*source
)
133 const GLuint name
= ~0;
134 struct gl_shader
*sh
;
136 sh
= _mesa_new_shader(name
, stage
);
137 sh
->Source
= strdup(source
);
138 sh
->CompileStatus
= compile_failure
;
139 _mesa_compile_shader(ctx
, sh
);
141 if (!sh
->CompileStatus
) {
144 "meta program compile failed:\n%s\nsource:\n%s\n",
145 sh
->InfoLog
, source
);
148 _mesa_reference_shader(ctx
, &sh
, NULL
);
155 _mesa_meta_link_program_with_debug(struct gl_context
*ctx
,
156 struct gl_shader_program
*sh_prog
)
158 _mesa_link_program(ctx
, sh_prog
);
160 if (!sh_prog
->data
->LinkStatus
) {
161 _mesa_problem(ctx
, "meta program link failed:\n%s",
162 sh_prog
->data
->InfoLog
);
167 _mesa_meta_use_program(struct gl_context
*ctx
,
168 struct gl_shader_program
*sh_prog
)
170 /* Attach shader state to the binding point */
171 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, &ctx
->Shader
);
173 /* Update the program */
174 _mesa_use_shader_program(ctx
, sh_prog
);
178 _mesa_meta_compile_and_link_program(struct gl_context
*ctx
,
179 const char *vs_source
,
180 const char *fs_source
,
182 struct gl_shader_program
**out_sh_prog
)
184 struct gl_shader_program
*sh_prog
;
185 const GLuint id
= ~0;
187 sh_prog
= _mesa_new_shader_program(id
);
188 sh_prog
->Label
= strdup(name
);
189 sh_prog
->NumShaders
= 2;
190 sh_prog
->Shaders
= malloc(2 * sizeof(struct gl_shader
*));
191 sh_prog
->Shaders
[0] =
192 meta_compile_shader_with_debug(ctx
, MESA_SHADER_VERTEX
, vs_source
);
193 sh_prog
->Shaders
[1] =
194 meta_compile_shader_with_debug(ctx
, MESA_SHADER_FRAGMENT
, fs_source
);
196 _mesa_meta_link_program_with_debug(ctx
, sh_prog
);
198 _mesa_meta_use_program(ctx
, sh_prog
);
200 *out_sh_prog
= sh_prog
;
204 * Generate a generic shader to blit from a texture to a framebuffer
206 * \param ctx Current GL context
207 * \param texTarget Texture target that will be the source of the blit
209 * \returns a handle to a shader program on success or zero on failure.
212 _mesa_meta_setup_blit_shader(struct gl_context
*ctx
,
215 struct blit_shader_table
*table
)
217 char *vs_source
, *fs_source
;
218 struct blit_shader
*shader
= choose_blit_shader(target
, table
);
219 const char *fs_input
, *vs_preprocess
, *fs_preprocess
;
222 if (ctx
->Const
.GLSLVersion
< 130) {
224 fs_preprocess
= "#extension GL_EXT_texture_array : enable";
225 fs_input
= "varying";
227 vs_preprocess
= "#version 130";
228 fs_preprocess
= "#version 130";
230 shader
->func
= "texture";
233 assert(shader
!= NULL
);
235 if (shader
->shader_prog
!= NULL
) {
236 _mesa_meta_use_program(ctx
, shader
->shader_prog
);
240 mem_ctx
= ralloc_context(NULL
);
242 vs_source
= ralloc_asprintf(mem_ctx
,
244 "#extension GL_ARB_explicit_attrib_location: enable\n"
245 "layout(location = 0) in vec2 position;\n"
246 "layout(location = 1) in vec4 textureCoords;\n"
247 "out vec4 texCoords;\n"
250 " texCoords = textureCoords;\n"
251 " gl_Position = vec4(position, 0.0, 1.0);\n"
255 fs_source
= ralloc_asprintf(mem_ctx
,
257 "#extension GL_ARB_texture_cube_map_array: enable\n"
258 "uniform %s texSampler;\n"
259 "%s vec4 texCoords;\n"
262 " gl_FragColor = %s(texSampler, %s);\n"
265 fs_preprocess
, shader
->type
, fs_input
,
266 shader
->func
, shader
->texcoords
,
267 do_depth
? " gl_FragDepth = gl_FragColor.x;\n" : "");
269 _mesa_meta_compile_and_link_program(ctx
, vs_source
, fs_source
,
270 ralloc_asprintf(mem_ctx
, "%s blit",
272 &shader
->shader_prog
);
273 ralloc_free(mem_ctx
);
277 * Configure vertex buffer and vertex array objects for tests
279 * Regardless of whether a new VAO is created, the object referenced by \c VAO
280 * will be bound into the GL state vector when this function terminates. The
281 * object referenced by \c VBO will \b not be bound.
283 * \param VAO Storage for vertex array object handle. If 0, a new VAO
285 * \param buf_obj Storage for vertex buffer object pointer. If \c NULL, a new VBO
286 * will be created. The new VBO will have storage for 4
287 * \c vertex structures.
288 * \param use_generic_attributes Should generic attributes 0 and 1 be used,
289 * or should traditional, fixed-function color and texture
290 * coordinate be used?
291 * \param vertex_size Number of components for attribute 0 / vertex.
292 * \param texcoord_size Number of components for attribute 1 / texture
293 * coordinate. If this is 0, attribute 1 will not be set or
295 * \param color_size Number of components for attribute 1 / primary color.
296 * If this is 0, attribute 1 will not be set or enabled.
298 * \note If \c use_generic_attributes is \c true, \c color_size must be zero.
299 * Use \c texcoord_size instead.
302 _mesa_meta_setup_vertex_objects(struct gl_context
*ctx
,
303 GLuint
*VAO
, struct gl_buffer_object
**buf_obj
,
304 bool use_generic_attributes
,
305 unsigned vertex_size
, unsigned texcoord_size
,
309 struct gl_vertex_array_object
*array_obj
;
310 assert(*buf_obj
== NULL
);
312 /* create vertex array object */
313 _mesa_GenVertexArrays(1, VAO
);
314 _mesa_BindVertexArray(*VAO
);
316 array_obj
= _mesa_lookup_vao(ctx
, *VAO
);
317 assert(array_obj
!= NULL
);
319 /* create vertex array buffer */
320 *buf_obj
= ctx
->Driver
.NewBufferObject(ctx
, 0xDEADBEEF);
321 if (*buf_obj
== NULL
)
324 _mesa_buffer_data(ctx
, *buf_obj
, GL_NONE
, 4 * sizeof(struct vertex
), NULL
,
325 GL_DYNAMIC_DRAW
, __func__
);
327 /* setup vertex arrays */
328 FLUSH_VERTICES(ctx
, 0);
329 if (use_generic_attributes
) {
330 assert(color_size
== 0);
332 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_GENERIC(0),
333 vertex_size
, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
335 offsetof(struct vertex
, x
));
336 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_GENERIC(0),
337 *buf_obj
, 0, sizeof(struct vertex
));
338 _mesa_enable_vertex_array_attrib(ctx
, array_obj
,
339 VERT_ATTRIB_GENERIC(0));
340 if (texcoord_size
> 0) {
341 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_GENERIC(1),
342 texcoord_size
, GL_FLOAT
, GL_RGBA
,
343 GL_FALSE
, GL_FALSE
, GL_FALSE
,
344 offsetof(struct vertex
, tex
));
345 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_GENERIC(1),
346 *buf_obj
, 0, sizeof(struct vertex
));
347 _mesa_enable_vertex_array_attrib(ctx
, array_obj
,
348 VERT_ATTRIB_GENERIC(1));
351 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_POS
,
352 vertex_size
, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
354 offsetof(struct vertex
, x
));
355 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_POS
,
356 *buf_obj
, 0, sizeof(struct vertex
));
357 _mesa_enable_vertex_array_attrib(ctx
, array_obj
, VERT_ATTRIB_POS
);
359 if (texcoord_size
> 0) {
360 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_TEX(0),
361 vertex_size
, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
363 offsetof(struct vertex
, tex
));
364 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_TEX(0),
365 *buf_obj
, 0, sizeof(struct vertex
));
366 _mesa_enable_vertex_array_attrib(ctx
, array_obj
, VERT_ATTRIB_TEX(0));
369 if (color_size
> 0) {
370 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_COLOR0
,
371 vertex_size
, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
373 offsetof(struct vertex
, r
));
374 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_COLOR0
,
375 *buf_obj
, 0, sizeof(struct vertex
));
376 _mesa_enable_vertex_array_attrib(ctx
, array_obj
, VERT_ATTRIB_COLOR0
);
380 _mesa_BindVertexArray(*VAO
);
385 * Initialize meta-ops for a context.
386 * To be called once during context creation.
389 _mesa_meta_init(struct gl_context
*ctx
)
393 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
397 * Free context meta-op state.
398 * To be called once during context destruction.
401 _mesa_meta_free(struct gl_context
*ctx
)
403 GET_CURRENT_CONTEXT(old_context
);
404 _mesa_make_current(ctx
, NULL
, NULL
);
405 _mesa_meta_glsl_blit_cleanup(ctx
, &ctx
->Meta
->Blit
);
406 meta_glsl_clear_cleanup(ctx
, &ctx
->Meta
->Clear
);
407 _mesa_meta_glsl_generate_mipmap_cleanup(ctx
, &ctx
->Meta
->Mipmap
);
408 cleanup_temp_texture(&ctx
->Meta
->TempTex
);
409 meta_decompress_cleanup(ctx
, &ctx
->Meta
->Decompress
);
410 meta_drawpix_cleanup(ctx
, &ctx
->Meta
->DrawPix
);
412 _mesa_make_current(old_context
, old_context
->WinSysDrawBuffer
, old_context
->WinSysReadBuffer
);
414 _mesa_make_current(NULL
, NULL
, NULL
);
421 * Enter meta state. This is like a light-weight version of glPushAttrib
422 * but it also resets most GL state back to default values.
424 * \param state bitmask of MESA_META_* flags indicating which attribute groups
425 * to save and reset to their defaults
428 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
430 struct save_state
*save
;
432 /* hope MAX_META_OPS_DEPTH is large enough */
433 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
435 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
436 memset(save
, 0, sizeof(*save
));
437 save
->SavedState
= state
;
439 /* We always push into desktop GL mode and pop out at the end. No sense in
440 * writing our shaders varying based on the user's context choice, when
441 * Mesa can handle either.
443 save
->API
= ctx
->API
;
444 ctx
->API
= API_OPENGL_COMPAT
;
446 /* Mesa's extension helper functions use the current context's API to look up
447 * the version required by an extension as a step in determining whether or
448 * not it has been advertised. Since meta aims to only be restricted by the
449 * driver capability (and not by whether or not an extension has been
450 * advertised), set the helper functions' Version variable to a value that
451 * will make the checks on the context API and version unconditionally pass.
453 save
->ExtensionsVersion
= ctx
->Extensions
.Version
;
454 ctx
->Extensions
.Version
= ~0;
456 /* Pausing transform feedback needs to be done early, or else we won't be
457 * able to change other state.
459 save
->TransformFeedbackNeedsResume
=
460 _mesa_is_xfb_active_and_unpaused(ctx
);
461 if (save
->TransformFeedbackNeedsResume
)
462 _mesa_PauseTransformFeedback();
464 /* After saving the current occlusion object, call EndQuery so that no
465 * occlusion querying will be active during the meta-operation.
467 if (state
& MESA_META_OCCLUSION_QUERY
) {
468 save
->CurrentOcclusionObject
= ctx
->Query
.CurrentOcclusionObject
;
469 if (save
->CurrentOcclusionObject
)
470 _mesa_EndQuery(save
->CurrentOcclusionObject
->Target
);
473 if (state
& MESA_META_ALPHA_TEST
) {
474 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
475 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
476 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
477 if (ctx
->Color
.AlphaEnabled
)
478 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
481 if (state
& MESA_META_BLEND
) {
482 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
483 if (ctx
->Color
.BlendEnabled
) {
484 if (ctx
->Extensions
.EXT_draw_buffers2
) {
486 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
487 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
491 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
494 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
495 if (ctx
->Color
.ColorLogicOpEnabled
)
496 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
499 if (state
& MESA_META_DITHER
) {
500 save
->DitherFlag
= ctx
->Color
.DitherFlag
;
501 _mesa_set_enable(ctx
, GL_DITHER
, GL_TRUE
);
504 if (state
& MESA_META_COLOR_MASK
) {
505 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
506 sizeof(ctx
->Color
.ColorMask
));
509 if (state
& MESA_META_DEPTH_TEST
) {
510 save
->Depth
= ctx
->Depth
; /* struct copy */
512 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
515 if (state
& MESA_META_FOG
) {
516 save
->Fog
= ctx
->Fog
.Enabled
;
517 if (ctx
->Fog
.Enabled
)
518 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
521 if (state
& MESA_META_PIXEL_STORE
) {
522 save
->Pack
= ctx
->Pack
;
523 save
->Unpack
= ctx
->Unpack
;
524 ctx
->Pack
= ctx
->DefaultPacking
;
525 ctx
->Unpack
= ctx
->DefaultPacking
;
528 if (state
& MESA_META_PIXEL_TRANSFER
) {
529 save
->RedScale
= ctx
->Pixel
.RedScale
;
530 save
->RedBias
= ctx
->Pixel
.RedBias
;
531 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
532 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
533 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
534 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
535 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
536 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
537 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
538 ctx
->Pixel
.RedScale
= 1.0F
;
539 ctx
->Pixel
.RedBias
= 0.0F
;
540 ctx
->Pixel
.GreenScale
= 1.0F
;
541 ctx
->Pixel
.GreenBias
= 0.0F
;
542 ctx
->Pixel
.BlueScale
= 1.0F
;
543 ctx
->Pixel
.BlueBias
= 0.0F
;
544 ctx
->Pixel
.AlphaScale
= 1.0F
;
545 ctx
->Pixel
.AlphaBias
= 0.0F
;
546 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
548 ctx
->NewState
|=_NEW_PIXEL
;
551 if (state
& MESA_META_RASTERIZATION
) {
552 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
553 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
554 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
555 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
556 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
557 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
558 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
559 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
560 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
561 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
562 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
565 if (state
& MESA_META_SCISSOR
) {
566 save
->Scissor
= ctx
->Scissor
; /* struct copy */
567 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
570 if (state
& MESA_META_SHADER
) {
573 if (ctx
->Extensions
.ARB_vertex_program
) {
574 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
575 _mesa_reference_program(ctx
, &save
->VertexProgram
,
576 ctx
->VertexProgram
.Current
);
577 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
580 if (ctx
->Extensions
.ARB_fragment_program
) {
581 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
582 _mesa_reference_program(ctx
, &save
->FragmentProgram
,
583 ctx
->FragmentProgram
.Current
);
584 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
587 if (ctx
->Extensions
.ATI_fragment_shader
) {
588 save
->ATIFragmentShaderEnabled
= ctx
->ATIFragmentShader
.Enabled
;
589 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
, GL_FALSE
);
592 if (ctx
->Pipeline
.Current
) {
593 _mesa_reference_pipeline_object(ctx
, &save
->Pipeline
,
594 ctx
->Pipeline
.Current
);
595 _mesa_BindProgramPipeline(0);
598 /* Save the shader state from ctx->Shader (instead of ctx->_Shader) so
599 * that we don't have to worry about the current pipeline state.
601 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
602 _mesa_reference_program(ctx
, &save
->Program
[i
],
603 ctx
->Shader
.CurrentProgram
[i
]);
605 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
606 ctx
->Shader
.ActiveProgram
);
611 if (state
& MESA_META_STENCIL_TEST
) {
612 save
->Stencil
= ctx
->Stencil
; /* struct copy */
613 if (ctx
->Stencil
.Enabled
)
614 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
615 /* NOTE: other stencil state not reset */
618 if (state
& MESA_META_TEXTURE
) {
621 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
622 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
624 /* Disable all texture units */
625 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
626 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
627 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
628 if (ctx
->Texture
.Unit
[u
].Enabled
||
629 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
630 _mesa_ActiveTexture(GL_TEXTURE0
+ u
);
631 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
632 if (ctx
->Extensions
.ARB_texture_cube_map
)
633 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
635 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
636 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
637 if (ctx
->Extensions
.NV_texture_rectangle
)
638 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
639 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
640 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
641 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
642 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
646 /* save current texture objects for unit[0] only */
647 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
648 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
649 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
652 /* set defaults for unit[0] */
653 _mesa_ActiveTexture(GL_TEXTURE0
);
654 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
657 if (state
& MESA_META_TRANSFORM
) {
658 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
659 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
660 16 * sizeof(GLfloat
));
661 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
662 16 * sizeof(GLfloat
));
663 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
664 16 * sizeof(GLfloat
));
665 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
666 /* set 1:1 vertex:pixel coordinate transform */
667 _mesa_ActiveTexture(GL_TEXTURE0
);
668 _mesa_MatrixMode(GL_TEXTURE
);
669 _mesa_LoadIdentity();
670 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
671 _mesa_MatrixMode(GL_MODELVIEW
);
672 _mesa_LoadIdentity();
673 _mesa_MatrixMode(GL_PROJECTION
);
674 _mesa_LoadIdentity();
676 /* glOrtho with width = 0 or height = 0 generates GL_INVALID_VALUE.
677 * This can occur when there is no draw buffer.
679 if (ctx
->DrawBuffer
->Width
!= 0 && ctx
->DrawBuffer
->Height
!= 0)
680 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
681 0.0, ctx
->DrawBuffer
->Height
,
684 if (ctx
->Extensions
.ARB_clip_control
) {
685 save
->ClipOrigin
= ctx
->Transform
.ClipOrigin
;
686 save
->ClipDepthMode
= ctx
->Transform
.ClipDepthMode
;
687 _mesa_ClipControl(GL_LOWER_LEFT
, GL_NEGATIVE_ONE_TO_ONE
);
691 if (state
& MESA_META_CLIP
) {
693 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
694 mask
= ctx
->Transform
.ClipPlanesEnabled
;
696 const int i
= u_bit_scan(&mask
);
697 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
701 if (state
& MESA_META_VERTEX
) {
702 /* save vertex array object state */
703 _mesa_reference_vao(ctx
, &save
->VAO
,
705 /* set some default state? */
708 if (state
& MESA_META_VIEWPORT
) {
709 /* save viewport state */
710 save
->ViewportX
= ctx
->ViewportArray
[0].X
;
711 save
->ViewportY
= ctx
->ViewportArray
[0].Y
;
712 save
->ViewportW
= ctx
->ViewportArray
[0].Width
;
713 save
->ViewportH
= ctx
->ViewportArray
[0].Height
;
714 /* set viewport to match window size */
715 if (ctx
->ViewportArray
[0].X
!= 0 ||
716 ctx
->ViewportArray
[0].Y
!= 0 ||
717 ctx
->ViewportArray
[0].Width
!= (float) ctx
->DrawBuffer
->Width
||
718 ctx
->ViewportArray
[0].Height
!= (float) ctx
->DrawBuffer
->Height
) {
719 _mesa_set_viewport(ctx
, 0, 0, 0,
720 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
722 /* save depth range state */
723 save
->DepthNear
= ctx
->ViewportArray
[0].Near
;
724 save
->DepthFar
= ctx
->ViewportArray
[0].Far
;
725 /* set depth range to default */
726 _mesa_set_depth_range(ctx
, 0, 0.0, 1.0);
729 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
730 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
732 /* Generally in here we want to do clamping according to whether
733 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
734 * regardless of the internal implementation of the metaops.
736 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
737 ctx
->Extensions
.ARB_color_buffer_float
)
738 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
741 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
742 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
744 /* Generally in here we never want vertex color clamping --
745 * result clamping is only dependent on fragment clamping.
747 if (ctx
->Extensions
.ARB_color_buffer_float
)
748 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
751 if (state
& MESA_META_CONDITIONAL_RENDER
) {
752 save
->CondRenderQuery
= ctx
->Query
.CondRenderQuery
;
753 save
->CondRenderMode
= ctx
->Query
.CondRenderMode
;
755 if (ctx
->Query
.CondRenderQuery
)
756 _mesa_EndConditionalRender();
759 if (state
& MESA_META_SELECT_FEEDBACK
) {
760 save
->RenderMode
= ctx
->RenderMode
;
761 if (ctx
->RenderMode
== GL_SELECT
) {
762 save
->Select
= ctx
->Select
; /* struct copy */
763 _mesa_RenderMode(GL_RENDER
);
764 } else if (ctx
->RenderMode
== GL_FEEDBACK
) {
765 save
->Feedback
= ctx
->Feedback
; /* struct copy */
766 _mesa_RenderMode(GL_RENDER
);
770 if (state
& MESA_META_MULTISAMPLE
) {
771 save
->Multisample
= ctx
->Multisample
; /* struct copy */
773 if (ctx
->Multisample
.Enabled
)
774 _mesa_set_multisample(ctx
, GL_FALSE
);
775 if (ctx
->Multisample
.SampleCoverage
)
776 _mesa_set_enable(ctx
, GL_SAMPLE_COVERAGE
, GL_FALSE
);
777 if (ctx
->Multisample
.SampleAlphaToCoverage
)
778 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_COVERAGE
, GL_FALSE
);
779 if (ctx
->Multisample
.SampleAlphaToOne
)
780 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_ONE
, GL_FALSE
);
781 if (ctx
->Multisample
.SampleShading
)
782 _mesa_set_enable(ctx
, GL_SAMPLE_SHADING
, GL_FALSE
);
783 if (ctx
->Multisample
.SampleMask
)
784 _mesa_set_enable(ctx
, GL_SAMPLE_MASK
, GL_FALSE
);
787 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
788 save
->sRGBEnabled
= ctx
->Color
.sRGBEnabled
;
789 if (ctx
->Color
.sRGBEnabled
)
790 _mesa_set_framebuffer_srgb(ctx
, GL_FALSE
);
793 if (state
& MESA_META_DRAW_BUFFERS
) {
794 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
795 memcpy(save
->ColorDrawBuffers
, fb
->ColorDrawBuffer
,
796 sizeof(save
->ColorDrawBuffers
));
801 save
->Lighting
= ctx
->Light
.Enabled
;
802 if (ctx
->Light
.Enabled
)
803 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
804 save
->RasterDiscard
= ctx
->RasterDiscard
;
805 if (ctx
->RasterDiscard
)
806 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_FALSE
);
808 _mesa_reference_framebuffer(&save
->DrawBuffer
, ctx
->DrawBuffer
);
809 _mesa_reference_framebuffer(&save
->ReadBuffer
, ctx
->ReadBuffer
);
815 * Leave meta state. This is like a light-weight version of glPopAttrib().
818 _mesa_meta_end(struct gl_context
*ctx
)
820 assert(ctx
->Meta
->SaveStackDepth
> 0);
822 struct save_state
*save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
- 1];
823 const GLbitfield state
= save
->SavedState
;
826 /* Grab the result of the old occlusion query before starting it again. The
827 * old result is added to the result of the new query so the driver will
828 * continue adding where it left off. */
829 if (state
& MESA_META_OCCLUSION_QUERY
) {
830 if (save
->CurrentOcclusionObject
) {
831 struct gl_query_object
*q
= save
->CurrentOcclusionObject
;
834 ctx
->Driver
.WaitQuery(ctx
, q
);
836 _mesa_BeginQuery(q
->Target
, q
->Id
);
837 ctx
->Query
.CurrentOcclusionObject
->Result
+= result
;
841 if (state
& MESA_META_ALPHA_TEST
) {
842 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
843 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
844 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
847 if (state
& MESA_META_BLEND
) {
848 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
849 if (ctx
->Extensions
.EXT_draw_buffers2
) {
851 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
852 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
856 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
859 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
860 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
863 if (state
& MESA_META_DITHER
)
864 _mesa_set_enable(ctx
, GL_DITHER
, save
->DitherFlag
);
866 if (state
& MESA_META_COLOR_MASK
) {
868 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
869 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
871 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
872 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
876 save
->ColorMask
[i
][0],
877 save
->ColorMask
[i
][1],
878 save
->ColorMask
[i
][2],
879 save
->ColorMask
[i
][3]);
885 if (state
& MESA_META_DEPTH_TEST
) {
886 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
887 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
888 _mesa_DepthFunc(save
->Depth
.Func
);
889 _mesa_DepthMask(save
->Depth
.Mask
);
892 if (state
& MESA_META_FOG
) {
893 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
896 if (state
& MESA_META_PIXEL_STORE
) {
897 ctx
->Pack
= save
->Pack
;
898 ctx
->Unpack
= save
->Unpack
;
901 if (state
& MESA_META_PIXEL_TRANSFER
) {
902 ctx
->Pixel
.RedScale
= save
->RedScale
;
903 ctx
->Pixel
.RedBias
= save
->RedBias
;
904 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
905 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
906 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
907 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
908 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
909 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
910 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
912 ctx
->NewState
|=_NEW_PIXEL
;
915 if (state
& MESA_META_RASTERIZATION
) {
916 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
917 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
918 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
919 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
920 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
921 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
924 if (state
& MESA_META_SCISSOR
) {
927 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++) {
928 _mesa_set_scissor(ctx
, i
,
929 save
->Scissor
.ScissorArray
[i
].X
,
930 save
->Scissor
.ScissorArray
[i
].Y
,
931 save
->Scissor
.ScissorArray
[i
].Width
,
932 save
->Scissor
.ScissorArray
[i
].Height
);
933 _mesa_set_enablei(ctx
, GL_SCISSOR_TEST
, i
,
934 (save
->Scissor
.EnableFlags
>> i
) & 1);
938 if (state
& MESA_META_SHADER
) {
941 if (ctx
->Extensions
.ARB_vertex_program
) {
942 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
943 save
->VertexProgramEnabled
);
944 _mesa_reference_program(ctx
, &ctx
->VertexProgram
.Current
,
945 save
->VertexProgram
);
946 _mesa_reference_program(ctx
, &save
->VertexProgram
, NULL
);
949 if (ctx
->Extensions
.ARB_fragment_program
) {
950 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
951 save
->FragmentProgramEnabled
);
952 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
.Current
,
953 save
->FragmentProgram
);
954 _mesa_reference_program(ctx
, &save
->FragmentProgram
, NULL
);
957 if (ctx
->Extensions
.ATI_fragment_shader
) {
958 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
,
959 save
->ATIFragmentShaderEnabled
);
963 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
964 /* It is safe to call _mesa_use_program even if the extension
965 * necessary for that program state is not supported. In that case,
966 * the saved program object must be NULL and the currently bound
967 * program object must be NULL. _mesa_use_program is a no-op
970 _mesa_use_program(ctx
, i
, NULL
, save
->Program
[i
], &ctx
->Shader
);
972 /* Do this *before* killing the reference. :)
974 if (save
->Program
[i
] != NULL
)
977 _mesa_reference_program(ctx
, &save
->Program
[i
], NULL
);
980 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
982 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
, NULL
);
984 /* If there were any stages set with programs, use ctx->Shader as the
985 * current shader state. Otherwise, use Pipeline.Default. The pipeline
986 * hasn't been restored yet, and that may modify ctx->_Shader further.
989 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
992 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
993 ctx
->Pipeline
.Default
);
995 if (save
->Pipeline
) {
996 _mesa_bind_pipeline(ctx
, save
->Pipeline
);
998 _mesa_reference_pipeline_object(ctx
, &save
->Pipeline
, NULL
);
1002 if (state
& MESA_META_STENCIL_TEST
) {
1003 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
1005 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
1006 _mesa_ClearStencil(stencil
->Clear
);
1007 if (ctx
->Extensions
.EXT_stencil_two_side
) {
1008 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
1009 stencil
->TestTwoSide
);
1010 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
1011 ? GL_BACK
: GL_FRONT
);
1014 _mesa_StencilFuncSeparate(GL_FRONT
,
1015 stencil
->Function
[0],
1017 stencil
->ValueMask
[0]);
1018 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
1019 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
1020 stencil
->ZFailFunc
[0],
1021 stencil
->ZPassFunc
[0]);
1023 _mesa_StencilFuncSeparate(GL_BACK
,
1024 stencil
->Function
[1],
1026 stencil
->ValueMask
[1]);
1027 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
1028 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
1029 stencil
->ZFailFunc
[1],
1030 stencil
->ZPassFunc
[1]);
1033 if (state
& MESA_META_TEXTURE
) {
1036 assert(ctx
->Texture
.CurrentUnit
== 0);
1038 /* restore texenv for unit[0] */
1039 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
1041 /* restore texture objects for unit[0] only */
1042 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
1043 if (ctx
->Texture
.Unit
[0].CurrentTex
[tgt
] != save
->CurrentTexture
[tgt
]) {
1044 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1045 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
1046 save
->CurrentTexture
[tgt
]);
1048 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
1051 /* Restore fixed function texture enables, texgen */
1052 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
1053 if (ctx
->Texture
.Unit
[u
].Enabled
!= save
->TexEnabled
[u
]) {
1054 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1055 ctx
->Texture
.Unit
[u
].Enabled
= save
->TexEnabled
[u
];
1058 if (ctx
->Texture
.Unit
[u
].TexGenEnabled
!= save
->TexGenEnabled
[u
]) {
1059 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1060 ctx
->Texture
.Unit
[u
].TexGenEnabled
= save
->TexGenEnabled
[u
];
1064 /* restore current unit state */
1065 _mesa_ActiveTexture(GL_TEXTURE0
+ save
->ActiveUnit
);
1068 if (state
& MESA_META_TRANSFORM
) {
1069 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
1070 _mesa_ActiveTexture(GL_TEXTURE0
);
1071 _mesa_MatrixMode(GL_TEXTURE
);
1072 _mesa_LoadMatrixf(save
->TextureMatrix
);
1073 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
1075 _mesa_MatrixMode(GL_MODELVIEW
);
1076 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
1078 _mesa_MatrixMode(GL_PROJECTION
);
1079 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
1081 _mesa_MatrixMode(save
->MatrixMode
);
1083 if (ctx
->Extensions
.ARB_clip_control
)
1084 _mesa_ClipControl(save
->ClipOrigin
, save
->ClipDepthMode
);
1087 if (state
& MESA_META_CLIP
) {
1088 GLbitfield mask
= save
->ClipPlanesEnabled
;
1090 const int i
= u_bit_scan(&mask
);
1091 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
1095 if (state
& MESA_META_VERTEX
) {
1096 /* restore vertex array object */
1097 _mesa_BindVertexArray(save
->VAO
->Name
);
1098 _mesa_reference_vao(ctx
, &save
->VAO
, NULL
);
1101 if (state
& MESA_META_VIEWPORT
) {
1102 if (save
->ViewportX
!= ctx
->ViewportArray
[0].X
||
1103 save
->ViewportY
!= ctx
->ViewportArray
[0].Y
||
1104 save
->ViewportW
!= ctx
->ViewportArray
[0].Width
||
1105 save
->ViewportH
!= ctx
->ViewportArray
[0].Height
) {
1106 _mesa_set_viewport(ctx
, 0, save
->ViewportX
, save
->ViewportY
,
1107 save
->ViewportW
, save
->ViewportH
);
1109 _mesa_set_depth_range(ctx
, 0, save
->DepthNear
, save
->DepthFar
);
1112 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
&&
1113 ctx
->Extensions
.ARB_color_buffer_float
) {
1114 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
1117 if (state
& MESA_META_CLAMP_VERTEX_COLOR
&&
1118 ctx
->Extensions
.ARB_color_buffer_float
) {
1119 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
1122 if (state
& MESA_META_CONDITIONAL_RENDER
) {
1123 if (save
->CondRenderQuery
)
1124 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
1125 save
->CondRenderMode
);
1128 if (state
& MESA_META_SELECT_FEEDBACK
) {
1129 if (save
->RenderMode
== GL_SELECT
) {
1130 _mesa_RenderMode(GL_SELECT
);
1131 ctx
->Select
= save
->Select
;
1132 } else if (save
->RenderMode
== GL_FEEDBACK
) {
1133 _mesa_RenderMode(GL_FEEDBACK
);
1134 ctx
->Feedback
= save
->Feedback
;
1138 if (state
& MESA_META_MULTISAMPLE
) {
1139 struct gl_multisample_attrib
*ctx_ms
= &ctx
->Multisample
;
1140 struct gl_multisample_attrib
*save_ms
= &save
->Multisample
;
1142 if (ctx_ms
->Enabled
!= save_ms
->Enabled
)
1143 _mesa_set_multisample(ctx
, save_ms
->Enabled
);
1144 if (ctx_ms
->SampleCoverage
!= save_ms
->SampleCoverage
)
1145 _mesa_set_enable(ctx
, GL_SAMPLE_COVERAGE
, save_ms
->SampleCoverage
);
1146 if (ctx_ms
->SampleAlphaToCoverage
!= save_ms
->SampleAlphaToCoverage
)
1147 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_COVERAGE
, save_ms
->SampleAlphaToCoverage
);
1148 if (ctx_ms
->SampleAlphaToOne
!= save_ms
->SampleAlphaToOne
)
1149 _mesa_set_enable(ctx
, GL_SAMPLE_ALPHA_TO_ONE
, save_ms
->SampleAlphaToOne
);
1150 if (ctx_ms
->SampleCoverageValue
!= save_ms
->SampleCoverageValue
||
1151 ctx_ms
->SampleCoverageInvert
!= save_ms
->SampleCoverageInvert
) {
1152 _mesa_SampleCoverage(save_ms
->SampleCoverageValue
,
1153 save_ms
->SampleCoverageInvert
);
1155 if (ctx_ms
->SampleShading
!= save_ms
->SampleShading
)
1156 _mesa_set_enable(ctx
, GL_SAMPLE_SHADING
, save_ms
->SampleShading
);
1157 if (ctx_ms
->SampleMask
!= save_ms
->SampleMask
)
1158 _mesa_set_enable(ctx
, GL_SAMPLE_MASK
, save_ms
->SampleMask
);
1159 if (ctx_ms
->SampleMaskValue
!= save_ms
->SampleMaskValue
)
1160 _mesa_SampleMaski(0, save_ms
->SampleMaskValue
);
1161 if (ctx_ms
->MinSampleShadingValue
!= save_ms
->MinSampleShadingValue
)
1162 _mesa_MinSampleShading(save_ms
->MinSampleShadingValue
);
1165 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
1166 if (ctx
->Color
.sRGBEnabled
!= save
->sRGBEnabled
)
1167 _mesa_set_framebuffer_srgb(ctx
, save
->sRGBEnabled
);
1171 if (save
->Lighting
) {
1172 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
1174 if (save
->RasterDiscard
) {
1175 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_TRUE
);
1177 if (save
->TransformFeedbackNeedsResume
)
1178 _mesa_ResumeTransformFeedback();
1180 _mesa_bind_framebuffers(ctx
, save
->DrawBuffer
, save
->ReadBuffer
);
1181 _mesa_reference_framebuffer(&save
->DrawBuffer
, NULL
);
1182 _mesa_reference_framebuffer(&save
->ReadBuffer
, NULL
);
1184 if (state
& MESA_META_DRAW_BUFFERS
) {
1185 _mesa_drawbuffers(ctx
, ctx
->DrawBuffer
, ctx
->Const
.MaxDrawBuffers
,
1186 save
->ColorDrawBuffers
, NULL
);
1189 ctx
->Meta
->SaveStackDepth
--;
1191 ctx
->API
= save
->API
;
1192 ctx
->Extensions
.Version
= save
->ExtensionsVersion
;
1197 * Convert Z from a normalized value in the range [0, 1] to an object-space
1198 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1199 * default/identity ortho projection results in the original Z value.
1200 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1201 * value comes from the clear value or raster position.
1203 static inline GLfloat
1204 invert_z(GLfloat normZ
)
1206 GLfloat objZ
= 1.0f
- 2.0f
* normZ
;
1212 * One-time init for a temp_texture object.
1213 * Choose tex target, compute max tex size, etc.
1216 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1218 /* prefer texture rectangle */
1219 if (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.NV_texture_rectangle
) {
1220 tex
->Target
= GL_TEXTURE_RECTANGLE
;
1221 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
1222 tex
->NPOT
= GL_TRUE
;
1225 /* use 2D texture, NPOT if possible */
1226 tex
->Target
= GL_TEXTURE_2D
;
1227 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1228 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
1230 tex
->MinSize
= 16; /* 16 x 16 at least */
1231 assert(tex
->MaxSize
> 0);
1233 _mesa_GenTextures(1, &tex
->TexObj
);
1237 cleanup_temp_texture(struct temp_texture
*tex
)
1241 _mesa_DeleteTextures(1, &tex
->TexObj
);
1247 * Return pointer to temp_texture info for non-bitmap ops.
1248 * This does some one-time init if needed.
1250 struct temp_texture
*
1251 _mesa_meta_get_temp_texture(struct gl_context
*ctx
)
1253 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
1256 init_temp_texture(ctx
, tex
);
1264 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1265 * We use a separate texture for bitmaps to reduce texture
1266 * allocation/deallocation.
1268 static struct temp_texture
*
1269 get_bitmap_temp_texture(struct gl_context
*ctx
)
1271 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
1274 init_temp_texture(ctx
, tex
);
1281 * Return pointer to depth temp_texture.
1282 * This does some one-time init if needed.
1284 struct temp_texture
*
1285 _mesa_meta_get_temp_depth_texture(struct gl_context
*ctx
)
1287 struct temp_texture
*tex
= &ctx
->Meta
->Blit
.depthTex
;
1290 init_temp_texture(ctx
, tex
);
1297 * Compute the width/height of texture needed to draw an image of the
1298 * given size. Return a flag indicating whether the current texture
1299 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1300 * allocated (glTexImage2D).
1301 * Also, compute s/t texcoords for drawing.
1303 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1306 _mesa_meta_alloc_texture(struct temp_texture
*tex
,
1307 GLsizei width
, GLsizei height
, GLenum intFormat
)
1309 GLboolean newTex
= GL_FALSE
;
1311 assert(width
<= tex
->MaxSize
);
1312 assert(height
<= tex
->MaxSize
);
1314 if (width
> tex
->Width
||
1315 height
> tex
->Height
||
1316 intFormat
!= tex
->IntFormat
) {
1317 /* alloc new texture (larger or different format) */
1320 /* use non-power of two size */
1321 tex
->Width
= MAX2(tex
->MinSize
, width
);
1322 tex
->Height
= MAX2(tex
->MinSize
, height
);
1325 /* find power of two size */
1327 w
= h
= tex
->MinSize
;
1336 tex
->IntFormat
= intFormat
;
1341 /* compute texcoords */
1342 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1343 tex
->Sright
= (GLfloat
) width
;
1344 tex
->Ttop
= (GLfloat
) height
;
1347 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1348 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1356 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1359 _mesa_meta_setup_copypix_texture(struct gl_context
*ctx
,
1360 struct temp_texture
*tex
,
1361 GLint srcX
, GLint srcY
,
1362 GLsizei width
, GLsizei height
,
1368 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1369 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1370 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1371 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1373 newTex
= _mesa_meta_alloc_texture(tex
, width
, height
, intFormat
);
1375 /* copy framebuffer image to texture */
1377 /* create new tex image */
1378 if (tex
->Width
== width
&& tex
->Height
== height
) {
1379 /* create new tex with framebuffer data */
1380 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1381 srcX
, srcY
, width
, height
, 0);
1384 /* create empty texture */
1385 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1386 tex
->Width
, tex
->Height
, 0,
1387 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1389 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1390 0, 0, srcX
, srcY
, width
, height
);
1394 /* replace existing tex image */
1395 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1396 0, 0, srcX
, srcY
, width
, height
);
1402 * Setup/load texture for glDrawPixels.
1405 _mesa_meta_setup_drawpix_texture(struct gl_context
*ctx
,
1406 struct temp_texture
*tex
,
1408 GLsizei width
, GLsizei height
,
1409 GLenum format
, GLenum type
,
1410 const GLvoid
*pixels
)
1412 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1413 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1414 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1415 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1417 /* copy pixel data to texture */
1419 /* create new tex image */
1420 if (tex
->Width
== width
&& tex
->Height
== height
) {
1421 /* create new tex and load image data */
1422 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1423 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1426 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1428 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1429 ctx
->Unpack
.BufferObj
);
1430 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1431 /* create empty texture */
1432 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1433 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1434 if (save_unpack_obj
!= NULL
)
1435 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
,
1436 save_unpack_obj
->Name
);
1438 _mesa_TexSubImage2D(tex
->Target
, 0,
1439 0, 0, width
, height
, format
, type
, pixels
);
1443 /* replace existing tex image */
1444 _mesa_TexSubImage2D(tex
->Target
, 0,
1445 0, 0, width
, height
, format
, type
, pixels
);
1450 _mesa_meta_setup_ff_tnl_for_blit(struct gl_context
*ctx
,
1451 GLuint
*VAO
, struct gl_buffer_object
**buf_obj
,
1452 unsigned texcoord_size
)
1454 _mesa_meta_setup_vertex_objects(ctx
, VAO
, buf_obj
, false, 2, texcoord_size
,
1457 /* setup projection matrix */
1458 _mesa_MatrixMode(GL_PROJECTION
);
1459 _mesa_LoadIdentity();
1463 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1466 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1468 meta_clear(ctx
, buffers
, false);
1472 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1474 meta_clear(ctx
, buffers
, true);
1478 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
1480 const char *vs_source
=
1481 "#extension GL_AMD_vertex_shader_layer : enable\n"
1482 "#extension GL_ARB_draw_instanced : enable\n"
1483 "#extension GL_ARB_explicit_attrib_location :enable\n"
1484 "layout(location = 0) in vec4 position;\n"
1487 "#ifdef GL_AMD_vertex_shader_layer\n"
1488 " gl_Layer = gl_InstanceID;\n"
1490 " gl_Position = position;\n"
1492 const char *fs_source
=
1493 "#extension GL_ARB_explicit_attrib_location :enable\n"
1494 "#extension GL_ARB_explicit_uniform_location :enable\n"
1495 "layout(location = 0) uniform vec4 color;\n"
1498 " gl_FragColor = color;\n"
1500 bool has_integer_textures
;
1502 _mesa_meta_setup_vertex_objects(ctx
, &clear
->VAO
, &clear
->buf_obj
, true,
1505 if (clear
->ShaderProg
!= 0)
1508 _mesa_meta_compile_and_link_program(ctx
, vs_source
, fs_source
, "meta clear",
1509 &clear
->ShaderProg
);
1511 has_integer_textures
= _mesa_is_gles3(ctx
) ||
1512 (_mesa_is_desktop_gl(ctx
) && ctx
->Const
.GLSLVersion
>= 130);
1514 if (has_integer_textures
) {
1515 void *shader_source_mem_ctx
= ralloc_context(NULL
);
1516 const char *vs_int_source
=
1517 ralloc_asprintf(shader_source_mem_ctx
,
1519 "#extension GL_AMD_vertex_shader_layer : enable\n"
1520 "#extension GL_ARB_draw_instanced : enable\n"
1521 "#extension GL_ARB_explicit_attrib_location :enable\n"
1522 "layout(location = 0) in vec4 position;\n"
1525 "#ifdef GL_AMD_vertex_shader_layer\n"
1526 " gl_Layer = gl_InstanceID;\n"
1528 " gl_Position = position;\n"
1530 const char *fs_int_source
=
1531 ralloc_asprintf(shader_source_mem_ctx
,
1533 "#extension GL_ARB_explicit_attrib_location :enable\n"
1534 "#extension GL_ARB_explicit_uniform_location :enable\n"
1535 "layout(location = 0) uniform ivec4 color;\n"
1536 "out ivec4 out_color;\n"
1540 " out_color = color;\n"
1543 _mesa_meta_compile_and_link_program(ctx
, vs_int_source
, fs_int_source
,
1545 &clear
->IntegerShaderProg
);
1546 ralloc_free(shader_source_mem_ctx
);
1548 /* Note that user-defined out attributes get automatically assigned
1549 * locations starting from 0, so we don't need to explicitly
1550 * BindFragDataLocation to 0.
1556 meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
)
1558 if (clear
->VAO
== 0)
1560 _mesa_DeleteVertexArrays(1, &clear
->VAO
);
1562 _mesa_reference_buffer_object(ctx
, &clear
->buf_obj
, NULL
);
1563 _mesa_reference_shader_program(ctx
, &clear
->ShaderProg
, NULL
);
1565 if (clear
->IntegerShaderProg
) {
1566 _mesa_reference_shader_program(ctx
, &clear
->IntegerShaderProg
, NULL
);
1571 * Given a bitfield of BUFFER_BIT_x draw buffers, call glDrawBuffers to
1572 * set GL to only draw to those buffers.
1574 * Since the bitfield has no associated order, the assignment of draw buffer
1575 * indices to color attachment indices is rather arbitrary.
1578 _mesa_meta_drawbuffers_from_bitfield(GLbitfield bits
)
1580 GLenum enums
[MAX_DRAW_BUFFERS
];
1584 /* This function is only legal for color buffer bitfields. */
1585 assert((bits
& ~BUFFER_BITS_COLOR
) == 0);
1587 /* Make sure we don't overflow any arrays. */
1588 assert(_mesa_bitcount(bits
) <= MAX_DRAW_BUFFERS
);
1592 if (bits
& BUFFER_BIT_FRONT_LEFT
)
1593 enums
[i
++] = GL_FRONT_LEFT
;
1595 if (bits
& BUFFER_BIT_FRONT_RIGHT
)
1596 enums
[i
++] = GL_FRONT_RIGHT
;
1598 if (bits
& BUFFER_BIT_BACK_LEFT
)
1599 enums
[i
++] = GL_BACK_LEFT
;
1601 if (bits
& BUFFER_BIT_BACK_RIGHT
)
1602 enums
[i
++] = GL_BACK_RIGHT
;
1604 for (n
= 0; n
< MAX_COLOR_ATTACHMENTS
; n
++) {
1605 if (bits
& (1 << (BUFFER_COLOR0
+ n
)))
1606 enums
[i
++] = GL_COLOR_ATTACHMENT0
+ n
;
1609 _mesa_DrawBuffers(i
, enums
);
1613 * Return if all of the color channels are masked.
1615 static inline GLboolean
1616 is_color_disabled(struct gl_context
*ctx
, int i
)
1618 return !ctx
->Color
.ColorMask
[i
][0] &&
1619 !ctx
->Color
.ColorMask
[i
][1] &&
1620 !ctx
->Color
.ColorMask
[i
][2] &&
1621 !ctx
->Color
.ColorMask
[i
][3];
1625 * Given a bitfield of BUFFER_BIT_x draw buffers, call glDrawBuffers to
1626 * set GL to only draw to those buffers. Also, update color masks to
1627 * reflect the new draw buffer ordering.
1630 _mesa_meta_drawbuffers_and_colormask(struct gl_context
*ctx
, GLbitfield mask
)
1632 GLenum enums
[MAX_DRAW_BUFFERS
];
1633 GLubyte colormask
[MAX_DRAW_BUFFERS
][4];
1636 /* This function is only legal for color buffer bitfields. */
1637 assert((mask
& ~BUFFER_BITS_COLOR
) == 0);
1639 /* Make sure we don't overflow any arrays. */
1640 assert(_mesa_bitcount(mask
) <= MAX_DRAW_BUFFERS
);
1644 for (int i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
1645 int b
= ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[i
];
1646 int colormask_idx
= ctx
->Extensions
.EXT_draw_buffers2
? i
: 0;
1648 if (b
< 0 || !(mask
& (1 << b
)) || is_color_disabled(ctx
, colormask_idx
))
1652 case BUFFER_FRONT_LEFT
:
1653 enums
[num_bufs
] = GL_FRONT_LEFT
;
1655 case BUFFER_FRONT_RIGHT
:
1656 enums
[num_bufs
] = GL_FRONT_RIGHT
;
1658 case BUFFER_BACK_LEFT
:
1659 enums
[num_bufs
] = GL_BACK_LEFT
;
1661 case BUFFER_BACK_RIGHT
:
1662 enums
[num_bufs
] = GL_BACK_RIGHT
;
1665 assert(b
>= BUFFER_COLOR0
&& b
<= BUFFER_COLOR7
);
1666 enums
[num_bufs
] = GL_COLOR_ATTACHMENT0
+ (b
- BUFFER_COLOR0
);
1670 for (int k
= 0; k
< 4; k
++)
1671 colormask
[num_bufs
][k
] = ctx
->Color
.ColorMask
[colormask_idx
][k
];
1676 _mesa_DrawBuffers(num_bufs
, enums
);
1678 for (int i
= 0; i
< num_bufs
; i
++) {
1679 _mesa_ColorMaski(i
, colormask
[i
][0], colormask
[i
][1],
1680 colormask
[i
][2], colormask
[i
][3]);
1686 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1689 meta_clear(struct gl_context
*ctx
, GLbitfield buffers
, bool glsl
)
1691 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1692 GLbitfield metaSave
;
1693 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1694 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1695 float x0
, y0
, x1
, y1
, z
;
1696 struct vertex verts
[4];
1699 metaSave
= (MESA_META_ALPHA_TEST
|
1701 MESA_META_COLOR_MASK
|
1702 MESA_META_DEPTH_TEST
|
1703 MESA_META_RASTERIZATION
|
1705 MESA_META_STENCIL_TEST
|
1707 MESA_META_VIEWPORT
|
1709 MESA_META_CLAMP_FRAGMENT_COLOR
|
1710 MESA_META_MULTISAMPLE
|
1711 MESA_META_OCCLUSION_QUERY
);
1714 metaSave
|= MESA_META_FOG
|
1715 MESA_META_PIXEL_TRANSFER
|
1716 MESA_META_TRANSFORM
|
1718 MESA_META_CLAMP_VERTEX_COLOR
|
1719 MESA_META_SELECT_FEEDBACK
;
1722 if (buffers
& BUFFER_BITS_COLOR
) {
1723 metaSave
|= MESA_META_DRAW_BUFFERS
;
1726 _mesa_meta_begin(ctx
, metaSave
);
1729 meta_glsl_clear_init(ctx
, clear
);
1731 x0
= ((float) fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
1732 y0
= ((float) fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
1733 x1
= ((float) fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
1734 y1
= ((float) fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
1735 z
= -invert_z(ctx
->Depth
.Clear
);
1737 _mesa_meta_setup_vertex_objects(ctx
, &clear
->VAO
, &clear
->buf_obj
, false,
1740 x0
= (float) fb
->_Xmin
;
1741 y0
= (float) fb
->_Ymin
;
1742 x1
= (float) fb
->_Xmax
;
1743 y1
= (float) fb
->_Ymax
;
1744 z
= invert_z(ctx
->Depth
.Clear
);
1747 if (fb
->_IntegerBuffers
) {
1749 _mesa_meta_use_program(ctx
, clear
->IntegerShaderProg
);
1750 _mesa_Uniform4iv(0, 1, ctx
->Color
.ClearColor
.i
);
1752 _mesa_meta_use_program(ctx
, clear
->ShaderProg
);
1753 _mesa_Uniform4fv(0, 1, ctx
->Color
.ClearColor
.f
);
1756 /* GL_COLOR_BUFFER_BIT */
1757 if (buffers
& BUFFER_BITS_COLOR
) {
1758 /* Only draw to the buffers we were asked to clear. */
1759 _mesa_meta_drawbuffers_and_colormask(ctx
, buffers
& BUFFER_BITS_COLOR
);
1761 /* leave colormask state as-is */
1763 /* Clears never have the color clamped. */
1764 if (ctx
->Extensions
.ARB_color_buffer_float
)
1765 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1768 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1771 /* GL_DEPTH_BUFFER_BIT */
1772 if (buffers
& BUFFER_BIT_DEPTH
) {
1773 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1774 _mesa_DepthFunc(GL_ALWAYS
);
1775 _mesa_DepthMask(GL_TRUE
);
1778 assert(!ctx
->Depth
.Test
);
1781 /* GL_STENCIL_BUFFER_BIT */
1782 if (buffers
& BUFFER_BIT_STENCIL
) {
1783 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1784 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1785 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1786 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1787 ctx
->Stencil
.Clear
& stencilMax
,
1788 ctx
->Stencil
.WriteMask
[0]);
1791 assert(!ctx
->Stencil
.Enabled
);
1794 /* vertex positions */
1809 for (i
= 0; i
< 4; i
++) {
1810 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
1811 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
1812 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
1813 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
1817 /* upload new vertex data */
1818 _mesa_buffer_data(ctx
, clear
->buf_obj
, GL_NONE
, sizeof(verts
), verts
,
1819 GL_DYNAMIC_DRAW
, __func__
);
1822 if (fb
->MaxNumLayers
> 0) {
1823 _mesa_DrawArraysInstanced(GL_TRIANGLE_FAN
, 0, 4, fb
->MaxNumLayers
);
1825 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1828 _mesa_meta_end(ctx
);
1832 * Meta implementation of ctx->Driver.CopyPixels() in terms
1833 * of texture mapping and polygon rendering and GLSL shaders.
1836 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
1837 GLsizei width
, GLsizei height
,
1838 GLint dstX
, GLint dstY
, GLenum type
)
1840 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1841 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
1842 struct vertex verts
[4];
1844 if (type
!= GL_COLOR
||
1845 ctx
->_ImageTransferState
||
1847 width
> tex
->MaxSize
||
1848 height
> tex
->MaxSize
) {
1849 /* XXX avoid this fallback */
1850 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1854 /* Most GL state applies to glCopyPixels, but a there's a few things
1855 * we need to override:
1857 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
1860 MESA_META_TRANSFORM
|
1863 MESA_META_VIEWPORT
));
1865 _mesa_meta_setup_vertex_objects(ctx
, ©pix
->VAO
, ©pix
->buf_obj
, false,
1868 /* Silence valgrind warnings about reading uninitialized stack. */
1869 memset(verts
, 0, sizeof(verts
));
1871 /* Alloc/setup texture */
1872 _mesa_meta_setup_copypix_texture(ctx
, tex
, srcX
, srcY
, width
, height
,
1873 GL_RGBA
, GL_NEAREST
);
1875 /* vertex positions, texcoords (after texture allocation!) */
1877 const GLfloat dstX0
= (GLfloat
) dstX
;
1878 const GLfloat dstY0
= (GLfloat
) dstY
;
1879 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1880 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1881 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1886 verts
[0].tex
[0] = 0.0F
;
1887 verts
[0].tex
[1] = 0.0F
;
1891 verts
[1].tex
[0] = tex
->Sright
;
1892 verts
[1].tex
[1] = 0.0F
;
1896 verts
[2].tex
[0] = tex
->Sright
;
1897 verts
[2].tex
[1] = tex
->Ttop
;
1901 verts
[3].tex
[0] = 0.0F
;
1902 verts
[3].tex
[1] = tex
->Ttop
;
1904 /* upload new vertex data */
1905 _mesa_buffer_sub_data(ctx
, copypix
->buf_obj
, 0, sizeof(verts
), verts
);
1908 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1910 /* draw textured quad */
1911 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1913 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1915 _mesa_meta_end(ctx
);
1919 meta_drawpix_cleanup(struct gl_context
*ctx
, struct drawpix_state
*drawpix
)
1921 if (drawpix
->VAO
!= 0) {
1922 _mesa_DeleteVertexArrays(1, &drawpix
->VAO
);
1925 _mesa_reference_buffer_object(ctx
, &drawpix
->buf_obj
, NULL
);
1928 if (drawpix
->StencilFP
!= 0) {
1929 _mesa_DeleteProgramsARB(1, &drawpix
->StencilFP
);
1930 drawpix
->StencilFP
= 0;
1933 if (drawpix
->DepthFP
!= 0) {
1934 _mesa_DeleteProgramsARB(1, &drawpix
->DepthFP
);
1935 drawpix
->DepthFP
= 0;
1940 * When the glDrawPixels() image size is greater than the max rectangle
1941 * texture size we use this function to break the glDrawPixels() image
1942 * into tiles which fit into the max texture size.
1945 tiled_draw_pixels(struct gl_context
*ctx
,
1947 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1948 GLenum format
, GLenum type
,
1949 const struct gl_pixelstore_attrib
*unpack
,
1950 const GLvoid
*pixels
)
1952 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
1955 if (tileUnpack
.RowLength
== 0)
1956 tileUnpack
.RowLength
= width
;
1958 for (i
= 0; i
< width
; i
+= tileSize
) {
1959 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
1960 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
1962 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
1964 for (j
= 0; j
< height
; j
+= tileSize
) {
1965 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
1966 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
1968 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
1970 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
1971 format
, type
, &tileUnpack
, pixels
);
1978 * One-time init for drawing stencil pixels.
1981 init_draw_stencil_pixels(struct gl_context
*ctx
)
1983 /* This program is run eight times, once for each stencil bit.
1984 * The stencil values to draw are found in an 8-bit alpha texture.
1985 * We read the texture/stencil value and test if bit 'b' is set.
1986 * If the bit is not set, use KIL to kill the fragment.
1987 * Finally, we use the stencil test to update the stencil buffer.
1989 * The basic algorithm for checking if a bit is set is:
1990 * if (is_odd(value / (1 << bit)))
1991 * result is one (or non-zero).
1994 * The program parameter contains three values:
1995 * parm.x = 255 / (1 << bit)
1999 static const char *program
=
2001 "PARAM parm = program.local[0]; \n"
2003 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
2004 "# t = t * 255 / bit \n"
2005 "MUL t.x, t.a, parm.x; \n"
2008 "SUB t.x, t.x, t.y; \n"
2010 "MUL t.x, t.x, parm.y; \n"
2011 "# t = fract(t.x) \n"
2012 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2013 "# t.x = (t.x == 0 ? 1 : 0) \n"
2014 "SGE t.x, -t.x, parm.z; \n"
2016 "# for debug only \n"
2017 "#MOV result.color, t.x; \n"
2019 char program2
[1000];
2020 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2021 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
2022 const char *texTarget
;
2024 assert(drawpix
->StencilFP
== 0);
2026 /* replace %s with "RECT" or "2D" */
2027 assert(strlen(program
) + 4 < sizeof(program2
));
2028 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2032 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2034 _mesa_GenProgramsARB(1, &drawpix
->StencilFP
);
2035 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2036 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2037 strlen(program2
), (const GLubyte
*) program2
);
2042 * One-time init for drawing depth pixels.
2045 init_draw_depth_pixels(struct gl_context
*ctx
)
2047 static const char *program
=
2049 "PARAM color = program.local[0]; \n"
2050 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2051 "MOV result.color, color; \n"
2054 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2055 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
2056 const char *texTarget
;
2058 assert(drawpix
->DepthFP
== 0);
2060 /* replace %s with "RECT" or "2D" */
2061 assert(strlen(program
) + 4 < sizeof(program2
));
2062 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2066 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2068 _mesa_GenProgramsARB(1, &drawpix
->DepthFP
);
2069 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2070 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2071 strlen(program2
), (const GLubyte
*) program2
);
2076 * Meta implementation of ctx->Driver.DrawPixels() in terms
2077 * of texture mapping and polygon rendering.
2080 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2081 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2082 GLenum format
, GLenum type
,
2083 const struct gl_pixelstore_attrib
*unpack
,
2084 const GLvoid
*pixels
)
2086 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2087 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
2088 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2089 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2090 struct vertex verts
[4];
2091 GLenum texIntFormat
;
2092 GLboolean fallback
, newTex
;
2093 GLbitfield metaExtraSave
= 0x0;
2096 * Determine if we can do the glDrawPixels with texture mapping.
2098 fallback
= GL_FALSE
;
2099 if (ctx
->Fog
.Enabled
) {
2103 if (_mesa_is_color_format(format
)) {
2104 /* use more compact format when possible */
2105 /* XXX disable special case for GL_LUMINANCE for now to work around
2106 * apparent i965 driver bug (see bug #23670).
2108 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2109 texIntFormat
= format
;
2111 texIntFormat
= GL_RGBA
;
2113 /* If we're not supposed to clamp the resulting color, then just
2114 * promote our texture to fully float. We could do better by
2115 * just going for the matching set of channels, in floating
2118 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2119 ctx
->Extensions
.ARB_texture_float
)
2120 texIntFormat
= GL_RGBA32F
;
2122 else if (_mesa_is_stencil_format(format
)) {
2123 if (ctx
->Extensions
.ARB_fragment_program
&&
2124 ctx
->Pixel
.IndexShift
== 0 &&
2125 ctx
->Pixel
.IndexOffset
== 0 &&
2126 type
== GL_UNSIGNED_BYTE
) {
2127 /* We'll store stencil as alpha. This only works for GLubyte
2128 * image data because of how incoming values are mapped to alpha
2131 texIntFormat
= GL_ALPHA
;
2132 metaExtraSave
= (MESA_META_COLOR_MASK
|
2133 MESA_META_DEPTH_TEST
|
2134 MESA_META_PIXEL_TRANSFER
|
2136 MESA_META_STENCIL_TEST
);
2142 else if (_mesa_is_depth_format(format
)) {
2143 if (ctx
->Extensions
.ARB_depth_texture
&&
2144 ctx
->Extensions
.ARB_fragment_program
) {
2145 texIntFormat
= GL_DEPTH_COMPONENT
;
2146 metaExtraSave
= (MESA_META_SHADER
);
2157 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2158 format
, type
, unpack
, pixels
);
2163 * Check image size against max texture size, draw as tiles if needed.
2165 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2166 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2167 format
, type
, unpack
, pixels
);
2171 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2172 * but a there's a few things we need to override:
2174 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2177 MESA_META_TRANSFORM
|
2180 MESA_META_VIEWPORT
|
2183 newTex
= _mesa_meta_alloc_texture(tex
, width
, height
, texIntFormat
);
2185 _mesa_meta_setup_vertex_objects(ctx
, &drawpix
->VAO
, &drawpix
->buf_obj
, false,
2188 /* Silence valgrind warnings about reading uninitialized stack. */
2189 memset(verts
, 0, sizeof(verts
));
2191 /* vertex positions, texcoords (after texture allocation!) */
2193 const GLfloat x0
= (GLfloat
) x
;
2194 const GLfloat y0
= (GLfloat
) y
;
2195 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2196 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2197 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2202 verts
[0].tex
[0] = 0.0F
;
2203 verts
[0].tex
[1] = 0.0F
;
2207 verts
[1].tex
[0] = tex
->Sright
;
2208 verts
[1].tex
[1] = 0.0F
;
2212 verts
[2].tex
[0] = tex
->Sright
;
2213 verts
[2].tex
[1] = tex
->Ttop
;
2217 verts
[3].tex
[0] = 0.0F
;
2218 verts
[3].tex
[1] = tex
->Ttop
;
2221 /* upload new vertex data */
2222 _mesa_buffer_data(ctx
, drawpix
->buf_obj
, GL_NONE
, sizeof(verts
), verts
,
2223 GL_DYNAMIC_DRAW
, __func__
);
2225 /* set given unpack params */
2226 ctx
->Unpack
= *unpack
;
2228 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2230 if (_mesa_is_stencil_format(format
)) {
2231 /* Drawing stencil */
2234 if (!drawpix
->StencilFP
)
2235 init_draw_stencil_pixels(ctx
);
2237 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2238 GL_ALPHA
, type
, pixels
);
2240 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2242 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2244 /* set all stencil bits to 0 */
2245 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2246 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2247 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2249 /* set stencil bits to 1 where needed */
2250 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2252 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2253 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2255 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2256 const GLuint mask
= 1 << bit
;
2257 if (mask
& origStencilMask
) {
2258 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2259 _mesa_StencilMask(mask
);
2261 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2262 255.0f
/ mask
, 0.5f
, 0.0f
, 0.0f
);
2264 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2268 else if (_mesa_is_depth_format(format
)) {
2270 if (!drawpix
->DepthFP
)
2271 init_draw_depth_pixels(ctx
);
2273 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2274 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2276 /* polygon color = current raster color */
2277 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2278 ctx
->Current
.RasterColor
);
2280 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2281 format
, type
, pixels
);
2283 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2287 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2288 format
, type
, pixels
);
2289 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2292 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2294 /* restore unpack params */
2295 ctx
->Unpack
= unpackSave
;
2297 _mesa_meta_end(ctx
);
2301 alpha_test_raster_color(struct gl_context
*ctx
)
2303 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2304 GLfloat ref
= ctx
->Color
.AlphaRef
;
2306 switch (ctx
->Color
.AlphaFunc
) {
2312 return alpha
== ref
;
2314 return alpha
<= ref
;
2318 return alpha
!= ref
;
2320 return alpha
>= ref
;
2330 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2331 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2332 * tracker would improve performance a lot.
2335 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2336 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2337 const struct gl_pixelstore_attrib
*unpack
,
2338 const GLubyte
*bitmap1
)
2340 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2341 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2342 const GLenum texIntFormat
= GL_ALPHA
;
2343 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2345 struct vertex verts
[4];
2350 * Check if swrast fallback is needed.
2352 if (ctx
->_ImageTransferState
||
2353 _mesa_arb_fragment_program_enabled(ctx
) ||
2355 ctx
->Texture
._MaxEnabledTexImageUnit
!= -1 ||
2356 width
> tex
->MaxSize
||
2357 height
> tex
->MaxSize
) {
2358 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2362 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2365 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2366 * but a there's a few things we need to override:
2368 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
2369 MESA_META_PIXEL_STORE
|
2370 MESA_META_RASTERIZATION
|
2373 MESA_META_TRANSFORM
|
2376 MESA_META_VIEWPORT
));
2378 _mesa_meta_setup_vertex_objects(ctx
, &bitmap
->VAO
, &bitmap
->buf_obj
, false,
2381 newTex
= _mesa_meta_alloc_texture(tex
, width
, height
, texIntFormat
);
2383 /* Silence valgrind warnings about reading uninitialized stack. */
2384 memset(verts
, 0, sizeof(verts
));
2386 /* vertex positions, texcoords, colors (after texture allocation!) */
2388 const GLfloat x0
= (GLfloat
) x
;
2389 const GLfloat y0
= (GLfloat
) y
;
2390 const GLfloat x1
= (GLfloat
) (x
+ width
);
2391 const GLfloat y1
= (GLfloat
) (y
+ height
);
2392 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2398 verts
[0].tex
[0] = 0.0F
;
2399 verts
[0].tex
[1] = 0.0F
;
2403 verts
[1].tex
[0] = tex
->Sright
;
2404 verts
[1].tex
[1] = 0.0F
;
2408 verts
[2].tex
[0] = tex
->Sright
;
2409 verts
[2].tex
[1] = tex
->Ttop
;
2413 verts
[3].tex
[0] = 0.0F
;
2414 verts
[3].tex
[1] = tex
->Ttop
;
2416 for (i
= 0; i
< 4; i
++) {
2417 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2418 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2419 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2420 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2423 /* upload new vertex data */
2424 _mesa_buffer_sub_data(ctx
, bitmap
->buf_obj
, 0, sizeof(verts
), verts
);
2427 /* choose different foreground/background alpha values */
2428 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2429 bg
= (fg
> 127 ? 0 : 255);
2431 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2433 _mesa_meta_end(ctx
);
2437 bitmap8
= malloc(width
* height
);
2439 memset(bitmap8
, bg
, width
* height
);
2440 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2441 bitmap8
, width
, fg
);
2443 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2445 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2446 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2448 _mesa_meta_setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2449 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2451 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2453 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2458 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2460 _mesa_meta_end(ctx
);
2464 * Compute the texture coordinates for the four vertices of a quad for
2465 * drawing a 2D texture image or slice of a cube/3D texture. The offset
2466 * and width, height specify a sub-region of the 2D image.
2468 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2469 * \param slice slice of a 1D/2D array texture or 3D texture
2470 * \param xoffset X position of sub texture
2471 * \param yoffset Y position of sub texture
2472 * \param width width of the sub texture image
2473 * \param height height of the sub texture image
2474 * \param total_width total width of the texture image
2475 * \param total_height total height of the texture image
2476 * \param total_depth total depth of the texture image
2477 * \param coords0/1/2/3 returns the computed texcoords
2480 _mesa_meta_setup_texture_coords(GLenum faceTarget
,
2496 const float s0
= (float) xoffset
/ (float) total_width
;
2497 const float s1
= (float) (xoffset
+ width
) / (float) total_width
;
2498 const float t0
= (float) yoffset
/ (float) total_height
;
2499 const float t1
= (float) (yoffset
+ height
) / (float) total_height
;
2502 /* setup the reference texcoords */
2512 if (faceTarget
== GL_TEXTURE_CUBE_MAP_ARRAY
)
2513 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ slice
% 6;
2515 /* Currently all texture targets want the W component to be 1.0.
2522 switch (faceTarget
) {
2526 case GL_TEXTURE_2D_ARRAY
:
2527 if (faceTarget
== GL_TEXTURE_3D
) {
2528 assert(slice
< total_depth
);
2529 assert(total_depth
>= 1);
2530 r
= (slice
+ 0.5f
) / total_depth
;
2532 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
2536 coords0
[0] = st
[0][0]; /* s */
2537 coords0
[1] = st
[0][1]; /* t */
2538 coords0
[2] = r
; /* r */
2539 coords1
[0] = st
[1][0];
2540 coords1
[1] = st
[1][1];
2542 coords2
[0] = st
[2][0];
2543 coords2
[1] = st
[2][1];
2545 coords3
[0] = st
[3][0];
2546 coords3
[1] = st
[3][1];
2549 case GL_TEXTURE_RECTANGLE_ARB
:
2550 coords0
[0] = (float) xoffset
; /* s */
2551 coords0
[1] = (float) yoffset
; /* t */
2552 coords0
[2] = 0.0F
; /* r */
2553 coords1
[0] = (float) (xoffset
+ width
);
2554 coords1
[1] = (float) yoffset
;
2556 coords2
[0] = (float) (xoffset
+ width
);
2557 coords2
[1] = (float) (yoffset
+ height
);
2559 coords3
[0] = (float) xoffset
;
2560 coords3
[1] = (float) (yoffset
+ height
);
2563 case GL_TEXTURE_1D_ARRAY
:
2564 coords0
[0] = st
[0][0]; /* s */
2565 coords0
[1] = (float) slice
; /* t */
2566 coords0
[2] = 0.0F
; /* r */
2567 coords1
[0] = st
[1][0];
2568 coords1
[1] = (float) slice
;
2570 coords2
[0] = st
[2][0];
2571 coords2
[1] = (float) slice
;
2573 coords3
[0] = st
[3][0];
2574 coords3
[1] = (float) slice
;
2578 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2579 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2580 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2581 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2582 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2583 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2584 /* loop over quad verts */
2585 for (i
= 0; i
< 4; i
++) {
2586 /* Compute sc = +/-scale and tc = +/-scale.
2587 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2588 * though that can still sometimes happen with this scale factor...
2590 const GLfloat scale
= 0.9999f
;
2591 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2592 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2609 unreachable("not reached");
2612 coord
[3] = (float) (slice
/ 6);
2614 switch (faceTarget
) {
2615 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2620 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2625 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2630 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2635 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2640 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2651 assert(!"unexpected target in _mesa_meta_setup_texture_coords()");
2655 static struct blit_shader
*
2656 choose_blit_shader(GLenum target
, struct blit_shader_table
*table
)
2660 table
->sampler_1d
.type
= "sampler1D";
2661 table
->sampler_1d
.func
= "texture1D";
2662 table
->sampler_1d
.texcoords
= "texCoords.x";
2663 return &table
->sampler_1d
;
2665 table
->sampler_2d
.type
= "sampler2D";
2666 table
->sampler_2d
.func
= "texture2D";
2667 table
->sampler_2d
.texcoords
= "texCoords.xy";
2668 return &table
->sampler_2d
;
2669 case GL_TEXTURE_RECTANGLE
:
2670 table
->sampler_rect
.type
= "sampler2DRect";
2671 table
->sampler_rect
.func
= "texture2DRect";
2672 table
->sampler_rect
.texcoords
= "texCoords.xy";
2673 return &table
->sampler_rect
;
2675 /* Code for mipmap generation with 3D textures is not used yet.
2676 * It's a sw fallback.
2678 table
->sampler_3d
.type
= "sampler3D";
2679 table
->sampler_3d
.func
= "texture3D";
2680 table
->sampler_3d
.texcoords
= "texCoords.xyz";
2681 return &table
->sampler_3d
;
2682 case GL_TEXTURE_CUBE_MAP
:
2683 table
->sampler_cubemap
.type
= "samplerCube";
2684 table
->sampler_cubemap
.func
= "textureCube";
2685 table
->sampler_cubemap
.texcoords
= "texCoords.xyz";
2686 return &table
->sampler_cubemap
;
2687 case GL_TEXTURE_1D_ARRAY
:
2688 table
->sampler_1d_array
.type
= "sampler1DArray";
2689 table
->sampler_1d_array
.func
= "texture1DArray";
2690 table
->sampler_1d_array
.texcoords
= "texCoords.xy";
2691 return &table
->sampler_1d_array
;
2692 case GL_TEXTURE_2D_ARRAY
:
2693 table
->sampler_2d_array
.type
= "sampler2DArray";
2694 table
->sampler_2d_array
.func
= "texture2DArray";
2695 table
->sampler_2d_array
.texcoords
= "texCoords.xyz";
2696 return &table
->sampler_2d_array
;
2697 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2698 table
->sampler_cubemap_array
.type
= "samplerCubeArray";
2699 table
->sampler_cubemap_array
.func
= "textureCubeArray";
2700 table
->sampler_cubemap_array
.texcoords
= "texCoords.xyzw";
2701 return &table
->sampler_cubemap_array
;
2703 _mesa_problem(NULL
, "Unexpected texture target 0x%x in"
2704 " setup_texture_sampler()\n", target
);
2710 _mesa_meta_blit_shader_table_cleanup(struct gl_context
*ctx
,
2711 struct blit_shader_table
*table
)
2713 _mesa_reference_shader_program(ctx
, &table
->sampler_1d
.shader_prog
, NULL
);
2714 _mesa_reference_shader_program(ctx
, &table
->sampler_2d
.shader_prog
, NULL
);
2715 _mesa_reference_shader_program(ctx
, &table
->sampler_3d
.shader_prog
, NULL
);
2716 _mesa_reference_shader_program(ctx
, &table
->sampler_rect
.shader_prog
, NULL
);
2717 _mesa_reference_shader_program(ctx
, &table
->sampler_cubemap
.shader_prog
, NULL
);
2718 _mesa_reference_shader_program(ctx
, &table
->sampler_1d_array
.shader_prog
, NULL
);
2719 _mesa_reference_shader_program(ctx
, &table
->sampler_2d_array
.shader_prog
, NULL
);
2720 _mesa_reference_shader_program(ctx
, &table
->sampler_cubemap_array
.shader_prog
, NULL
);
2724 * Determine the GL data type to use for the temporary image read with
2725 * ReadPixels() and passed to Tex[Sub]Image().
2728 get_temp_image_type(struct gl_context
*ctx
, mesa_format format
)
2730 const GLenum baseFormat
= _mesa_get_format_base_format(format
);
2731 const GLenum datatype
= _mesa_get_format_datatype(format
);
2732 const GLint format_red_bits
= _mesa_get_format_bits(format
, GL_RED_BITS
);
2734 switch (baseFormat
) {
2741 case GL_LUMINANCE_ALPHA
:
2743 if (datatype
== GL_INT
|| datatype
== GL_UNSIGNED_INT
) {
2745 } else if (format_red_bits
<= 8) {
2746 return GL_UNSIGNED_BYTE
;
2747 } else if (format_red_bits
<= 16) {
2748 return GL_UNSIGNED_SHORT
;
2751 case GL_DEPTH_COMPONENT
:
2752 if (datatype
== GL_FLOAT
)
2755 return GL_UNSIGNED_INT
;
2756 case GL_DEPTH_STENCIL
:
2757 if (datatype
== GL_FLOAT
)
2758 return GL_FLOAT_32_UNSIGNED_INT_24_8_REV
;
2760 return GL_UNSIGNED_INT_24_8
;
2762 _mesa_problem(ctx
, "Unexpected format %d in get_temp_image_type()",
2769 * Attempts to wrap the destination texture in an FBO and use
2770 * glBlitFramebuffer() to implement glCopyTexSubImage().
2773 copytexsubimage_using_blit_framebuffer(struct gl_context
*ctx
,
2774 struct gl_texture_image
*texImage
,
2778 struct gl_renderbuffer
*rb
,
2780 GLsizei width
, GLsizei height
)
2782 struct gl_framebuffer
*drawFb
;
2783 bool success
= false;
2787 if (!ctx
->Extensions
.ARB_framebuffer_object
)
2790 drawFb
= ctx
->Driver
.NewFramebuffer(ctx
, 0xDEADBEEF);
2794 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_DRAW_BUFFERS
);
2795 _mesa_bind_framebuffers(ctx
, drawFb
, ctx
->ReadBuffer
);
2797 if (rb
->_BaseFormat
== GL_DEPTH_STENCIL
||
2798 rb
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2799 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
2800 GL_DEPTH_ATTACHMENT
,
2802 mask
= GL_DEPTH_BUFFER_BIT
;
2804 if (rb
->_BaseFormat
== GL_DEPTH_STENCIL
&&
2805 texImage
->_BaseFormat
== GL_DEPTH_STENCIL
) {
2806 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
2807 GL_STENCIL_ATTACHMENT
,
2809 mask
|= GL_STENCIL_BUFFER_BIT
;
2811 _mesa_DrawBuffer(GL_NONE
);
2813 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
2814 GL_COLOR_ATTACHMENT0
,
2816 mask
= GL_COLOR_BUFFER_BIT
;
2817 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0
);
2820 status
= _mesa_check_framebuffer_status(ctx
, ctx
->DrawBuffer
);
2821 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
2824 ctx
->Meta
->Blit
.no_ctsi_fallback
= true;
2826 /* Since we've bound a new draw framebuffer, we need to update
2827 * its derived state -- _Xmin, etc -- for BlitFramebuffer's clipping to
2830 _mesa_update_state(ctx
);
2832 /* We skip the core BlitFramebuffer checks for format consistency, which
2833 * are too strict for CopyTexImage. We know meta will be fine with format
2836 mask
= _mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
2838 x
+ width
, y
+ height
,
2840 xoffset
+ width
, yoffset
+ height
,
2842 ctx
->Meta
->Blit
.no_ctsi_fallback
= false;
2843 success
= mask
== 0x0;
2846 _mesa_reference_framebuffer(&drawFb
, NULL
);
2847 _mesa_meta_end(ctx
);
2852 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2853 * Have to be careful with locking and meta state for pixel transfer.
2856 _mesa_meta_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2857 struct gl_texture_image
*texImage
,
2858 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2859 struct gl_renderbuffer
*rb
,
2861 GLsizei width
, GLsizei height
)
2863 GLenum format
, type
;
2867 if (copytexsubimage_using_blit_framebuffer(ctx
,
2869 xoffset
, yoffset
, zoffset
,
2876 /* Choose format/type for temporary image buffer */
2877 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
2878 if (format
== GL_LUMINANCE
||
2879 format
== GL_LUMINANCE_ALPHA
||
2880 format
== GL_INTENSITY
) {
2881 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
2882 * temp image buffer because glReadPixels will do L=R+G+B which is
2883 * not what we want (should be L=R).
2888 type
= get_temp_image_type(ctx
, texImage
->TexFormat
);
2889 if (_mesa_is_format_integer_color(texImage
->TexFormat
)) {
2890 format
= _mesa_base_format_to_integer_format(format
);
2892 bpp
= _mesa_bytes_per_pixel(format
, type
);
2894 _mesa_problem(ctx
, "Bad bpp in _mesa_meta_CopyTexSubImage()");
2899 * Alloc image buffer (XXX could use a PBO)
2901 buf
= malloc(width
* height
* bpp
);
2903 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
2908 * Read image from framebuffer (disable pixel transfer ops)
2910 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
2911 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2912 format
, type
, &ctx
->Pack
, buf
);
2913 _mesa_meta_end(ctx
);
2915 _mesa_update_state(ctx
); /* to update pixel transfer state */
2918 * Store texture data (with pixel transfer ops)
2920 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
2922 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
2923 assert(yoffset
== 0);
2924 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
2925 xoffset
, zoffset
, 0, width
, 1, 1,
2926 format
, type
, buf
, &ctx
->Unpack
);
2928 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
2929 xoffset
, yoffset
, zoffset
, width
, height
, 1,
2930 format
, type
, buf
, &ctx
->Unpack
);
2933 _mesa_meta_end(ctx
);
2939 meta_decompress_fbo_cleanup(struct decompress_fbo_state
*decompress_fbo
)
2941 if (decompress_fbo
->fb
!= NULL
) {
2942 _mesa_reference_framebuffer(&decompress_fbo
->fb
, NULL
);
2943 _mesa_reference_renderbuffer(&decompress_fbo
->rb
, NULL
);
2946 memset(decompress_fbo
, 0, sizeof(*decompress_fbo
));
2950 meta_decompress_cleanup(struct gl_context
*ctx
,
2951 struct decompress_state
*decompress
)
2953 meta_decompress_fbo_cleanup(&decompress
->byteFBO
);
2954 meta_decompress_fbo_cleanup(&decompress
->floatFBO
);
2956 if (decompress
->VAO
!= 0) {
2957 _mesa_DeleteVertexArrays(1, &decompress
->VAO
);
2958 _mesa_reference_buffer_object(ctx
, &decompress
->buf_obj
, NULL
);
2961 _mesa_reference_sampler_object(ctx
, &decompress
->samp_obj
, NULL
);
2963 memset(decompress
, 0, sizeof(*decompress
));
2967 * Decompress a texture image by drawing a quad with the compressed
2968 * texture and reading the pixels out of the color buffer.
2969 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
2970 * \param destFormat format, ala glReadPixels
2971 * \param destType type, ala glReadPixels
2972 * \param dest destination buffer
2973 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
2976 decompress_texture_image(struct gl_context
*ctx
,
2977 struct gl_texture_image
*texImage
,
2979 GLint xoffset
, GLint yoffset
,
2980 GLsizei width
, GLsizei height
,
2981 GLenum destFormat
, GLenum destType
,
2984 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
2985 struct decompress_fbo_state
*decompress_fbo
;
2986 struct gl_texture_object
*texObj
= texImage
->TexObject
;
2987 const GLenum target
= texObj
->Target
;
2990 struct vertex verts
[4];
2991 struct gl_sampler_object
*samp_obj_save
= NULL
;
2993 const bool use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
2994 ctx
->Extensions
.ARB_fragment_shader
;
2996 switch (_mesa_get_format_datatype(texImage
->TexFormat
)) {
2998 decompress_fbo
= &decompress
->floatFBO
;
2999 rbFormat
= GL_RGBA32F
;
3001 case GL_UNSIGNED_NORMALIZED
:
3002 decompress_fbo
= &decompress
->byteFBO
;
3010 assert(target
== GL_TEXTURE_3D
||
3011 target
== GL_TEXTURE_2D_ARRAY
||
3012 target
== GL_TEXTURE_CUBE_MAP_ARRAY
);
3017 case GL_TEXTURE_1D_ARRAY
:
3018 assert(!"No compressed 1D textures.");
3021 case GL_TEXTURE_CUBE_MAP_ARRAY
:
3022 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ (slice
% 6);
3025 case GL_TEXTURE_CUBE_MAP
:
3026 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
3030 faceTarget
= target
;
3034 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~(MESA_META_PIXEL_STORE
|
3035 MESA_META_DRAW_BUFFERS
));
3036 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
3038 _mesa_reference_sampler_object(ctx
, &samp_obj_save
,
3039 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
);
3041 /* Create/bind FBO/renderbuffer */
3042 if (decompress_fbo
->fb
== NULL
) {
3043 decompress_fbo
->rb
= ctx
->Driver
.NewRenderbuffer(ctx
, 0xDEADBEEF);
3044 if (decompress_fbo
->rb
== NULL
) {
3045 _mesa_meta_end(ctx
);
3049 decompress_fbo
->fb
= ctx
->Driver
.NewFramebuffer(ctx
, 0xDEADBEEF);
3050 if (decompress_fbo
->fb
== NULL
) {
3051 _mesa_meta_end(ctx
);
3055 _mesa_bind_framebuffers(ctx
, decompress_fbo
->fb
, decompress_fbo
->fb
);
3056 _mesa_framebuffer_renderbuffer(ctx
, ctx
->DrawBuffer
, GL_COLOR_ATTACHMENT0
,
3057 decompress_fbo
->rb
);
3060 _mesa_bind_framebuffers(ctx
, decompress_fbo
->fb
, decompress_fbo
->fb
);
3063 /* alloc dest surface */
3064 if (width
> decompress_fbo
->Width
|| height
> decompress_fbo
->Height
) {
3065 _mesa_renderbuffer_storage(ctx
, decompress_fbo
->rb
, rbFormat
,
3068 /* Do the full completeness check to recompute
3069 * ctx->DrawBuffer->Width/Height.
3071 ctx
->DrawBuffer
->_Status
= GL_FRAMEBUFFER_UNDEFINED
;
3072 status
= _mesa_check_framebuffer_status(ctx
, ctx
->DrawBuffer
);
3073 if (status
!= GL_FRAMEBUFFER_COMPLETE
) {
3074 /* If the framebuffer isn't complete then we'll leave
3075 * decompress_fbo->Width as zero so that it will fail again next time
3077 _mesa_meta_end(ctx
);
3080 decompress_fbo
->Width
= width
;
3081 decompress_fbo
->Height
= height
;
3084 if (use_glsl_version
) {
3085 _mesa_meta_setup_vertex_objects(ctx
, &decompress
->VAO
,
3086 &decompress
->buf_obj
, true,
3089 _mesa_meta_setup_blit_shader(ctx
, target
, false, &decompress
->shaders
);
3091 _mesa_meta_setup_ff_tnl_for_blit(ctx
, &decompress
->VAO
,
3092 &decompress
->buf_obj
, 3);
3095 if (decompress
->samp_obj
== NULL
) {
3096 decompress
->samp_obj
= ctx
->Driver
.NewSamplerObject(ctx
, 0xDEADBEEF);
3097 if (decompress
->samp_obj
== NULL
) {
3098 _mesa_meta_end(ctx
);
3100 /* This is a bit lazy. Flag out of memory, and then don't bother to
3101 * clean up. Once out of memory is flagged, the only realistic next
3102 * move is to destroy the context. That will trigger all the right
3105 * Returning true prevents other GetTexImage methods from attempting
3106 * anything since they will likely fail too.
3108 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
3112 /* nearest filtering */
3113 _mesa_set_sampler_filters(ctx
, decompress
->samp_obj
, GL_NEAREST
, GL_NEAREST
);
3115 /* We don't want to encode or decode sRGB values; treat them as linear. */
3116 _mesa_set_sampler_srgb_decode(ctx
, decompress
->samp_obj
, GL_SKIP_DECODE_EXT
);
3119 _mesa_bind_sampler(ctx
, ctx
->Texture
.CurrentUnit
, decompress
->samp_obj
);
3121 /* Silence valgrind warnings about reading uninitialized stack. */
3122 memset(verts
, 0, sizeof(verts
));
3124 _mesa_meta_setup_texture_coords(faceTarget
, slice
,
3125 xoffset
, yoffset
, width
, height
,
3126 texImage
->Width
, texImage
->Height
,
3133 /* setup vertex positions */
3143 _mesa_set_viewport(ctx
, 0, 0, 0, width
, height
);
3145 /* upload new vertex data */
3146 _mesa_buffer_sub_data(ctx
, decompress
->buf_obj
, 0, sizeof(verts
), verts
);
3148 /* setup texture state */
3149 _mesa_BindTexture(target
, texObj
->Name
);
3151 if (!use_glsl_version
)
3152 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3155 /* save texture object state */
3156 const GLint baseLevelSave
= texObj
->BaseLevel
;
3157 const GLint maxLevelSave
= texObj
->MaxLevel
;
3159 /* restrict sampling to the texture level of interest */
3160 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3161 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_BASE_LEVEL
,
3162 (GLint
*) &texImage
->Level
, false);
3163 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_MAX_LEVEL
,
3164 (GLint
*) &texImage
->Level
, false);
3167 /* render quad w/ texture into renderbuffer */
3168 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3170 /* Restore texture object state, the texture binding will
3171 * be restored by _mesa_meta_end().
3173 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3174 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_BASE_LEVEL
,
3175 &baseLevelSave
, false);
3176 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_MAX_LEVEL
,
3177 &maxLevelSave
, false);
3182 /* read pixels from renderbuffer */
3184 GLenum baseTexFormat
= texImage
->_BaseFormat
;
3185 GLenum destBaseFormat
= _mesa_unpack_format_to_base_format(destFormat
);
3187 /* The pixel transfer state will be set to default values at this point
3188 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
3189 * turned off (as required by glGetTexImage) but we need to handle some
3190 * special cases. In particular, single-channel texture values are
3191 * returned as red and two-channel texture values are returned as
3194 if (_mesa_need_luminance_to_rgb_conversion(baseTexFormat
,
3196 /* If we're reading back an RGB(A) texture (using glGetTexImage) as
3197 * luminance then we need to return L=tex(R).
3199 _mesa_need_rgb_to_luminance_conversion(baseTexFormat
,
3201 /* Green and blue must be zero */
3202 _mesa_PixelTransferf(GL_GREEN_SCALE
, 0.0f
);
3203 _mesa_PixelTransferf(GL_BLUE_SCALE
, 0.0f
);
3206 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
3209 /* disable texture unit */
3210 if (!use_glsl_version
)
3211 _mesa_set_enable(ctx
, target
, GL_FALSE
);
3213 _mesa_bind_sampler(ctx
, ctx
->Texture
.CurrentUnit
, samp_obj_save
);
3214 _mesa_reference_sampler_object(ctx
, &samp_obj_save
, NULL
);
3216 _mesa_meta_end(ctx
);
3223 * This is just a wrapper around _mesa_get_tex_image() and
3224 * decompress_texture_image(). Meta functions should not be directly called
3228 _mesa_meta_GetTexSubImage(struct gl_context
*ctx
,
3229 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3230 GLsizei width
, GLsizei height
, GLsizei depth
,
3231 GLenum format
, GLenum type
, GLvoid
*pixels
,
3232 struct gl_texture_image
*texImage
)
3234 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
3238 for (slice
= 0; slice
< depth
; slice
++) {
3240 /* Section 8.11.4 (Texture Image Queries) of the GL 4.5 spec says:
3242 * "For three-dimensional, two-dimensional array, cube map array,
3243 * and cube map textures pixel storage operations are applied as
3244 * if the image were two-dimensional, except that the additional
3245 * pixel storage state values PACK_IMAGE_HEIGHT and
3246 * PACK_SKIP_IMAGES are applied. The correspondence of texels to
3247 * memory locations is as defined for TexImage3D in section 8.5."
3249 switch (texImage
->TexObject
->Target
) {
3251 case GL_TEXTURE_2D_ARRAY
:
3252 case GL_TEXTURE_CUBE_MAP
:
3253 case GL_TEXTURE_CUBE_MAP_ARRAY
: {
3254 /* Setup pixel packing. SkipPixels and SkipRows will be applied
3255 * in the decompress_texture_image() function's call to
3256 * glReadPixels but we need to compute the dest slice's address
3257 * here (according to SkipImages and ImageHeight).
3259 struct gl_pixelstore_attrib packing
= ctx
->Pack
;
3260 packing
.SkipPixels
= 0;
3261 packing
.SkipRows
= 0;
3262 dst
= _mesa_image_address3d(&packing
, pixels
, width
, height
,
3263 format
, type
, slice
, 0, 0);
3270 result
= decompress_texture_image(ctx
, texImage
, slice
,
3271 xoffset
, yoffset
, width
, height
,
3281 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
3282 width
, height
, depth
, format
, type
, pixels
, texImage
);
3287 * Meta implementation of ctx->Driver.DrawTex() in terms
3288 * of polygon rendering.
3291 _mesa_meta_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
3292 GLfloat width
, GLfloat height
)
3294 struct drawtex_state
*drawtex
= &ctx
->Meta
->DrawTex
;
3296 GLfloat x
, y
, z
, st
[MAX_TEXTURE_UNITS
][2];
3298 struct vertex verts
[4];
3301 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
3303 MESA_META_TRANSFORM
|
3305 MESA_META_VIEWPORT
));
3307 if (drawtex
->VAO
== 0) {
3308 /* one-time setup */
3309 struct gl_vertex_array_object
*array_obj
;
3311 /* create vertex array object */
3312 _mesa_GenVertexArrays(1, &drawtex
->VAO
);
3313 _mesa_BindVertexArray(drawtex
->VAO
);
3315 array_obj
= _mesa_lookup_vao(ctx
, drawtex
->VAO
);
3316 assert(array_obj
!= NULL
);
3318 /* create vertex array buffer */
3319 drawtex
->buf_obj
= ctx
->Driver
.NewBufferObject(ctx
, 0xDEADBEEF);
3320 if (drawtex
->buf_obj
== NULL
)
3323 _mesa_buffer_data(ctx
, drawtex
->buf_obj
, GL_NONE
, sizeof(verts
), verts
,
3324 GL_DYNAMIC_DRAW
, __func__
);
3326 /* setup vertex arrays */
3327 FLUSH_VERTICES(ctx
, 0);
3328 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_POS
,
3329 3, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
3331 offsetof(struct vertex
, x
));
3332 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_POS
,
3333 drawtex
->buf_obj
, 0, sizeof(struct vertex
));
3334 _mesa_enable_vertex_array_attrib(ctx
, array_obj
, VERT_ATTRIB_POS
);
3337 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3338 FLUSH_VERTICES(ctx
, 0);
3339 _mesa_update_array_format(ctx
, array_obj
, VERT_ATTRIB_TEX(i
),
3340 2, GL_FLOAT
, GL_RGBA
, GL_FALSE
,
3342 offsetof(struct vertex
, st
[i
]));
3343 _mesa_bind_vertex_buffer(ctx
, array_obj
, VERT_ATTRIB_TEX(i
),
3344 drawtex
->buf_obj
, 0, sizeof(struct vertex
));
3345 _mesa_enable_vertex_array_attrib(ctx
, array_obj
, VERT_ATTRIB_TEX(i
));
3349 _mesa_BindVertexArray(drawtex
->VAO
);
3352 /* vertex positions, texcoords */
3354 const GLfloat x1
= x
+ width
;
3355 const GLfloat y1
= y
+ height
;
3357 z
= CLAMP(z
, 0.0f
, 1.0f
);
3376 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3377 const struct gl_texture_object
*texObj
;
3378 const struct gl_texture_image
*texImage
;
3379 GLfloat s
, t
, s1
, t1
;
3382 if (!ctx
->Texture
.Unit
[i
]._Current
) {
3384 for (j
= 0; j
< 4; j
++) {
3385 verts
[j
].st
[i
][0] = 0.0f
;
3386 verts
[j
].st
[i
][1] = 0.0f
;
3391 texObj
= ctx
->Texture
.Unit
[i
]._Current
;
3392 texImage
= texObj
->Image
[0][texObj
->BaseLevel
];
3393 tw
= texImage
->Width2
;
3394 th
= texImage
->Height2
;
3396 s
= (GLfloat
) texObj
->CropRect
[0] / tw
;
3397 t
= (GLfloat
) texObj
->CropRect
[1] / th
;
3398 s1
= (GLfloat
) (texObj
->CropRect
[0] + texObj
->CropRect
[2]) / tw
;
3399 t1
= (GLfloat
) (texObj
->CropRect
[1] + texObj
->CropRect
[3]) / th
;
3401 verts
[0].st
[i
][0] = s
;
3402 verts
[0].st
[i
][1] = t
;
3404 verts
[1].st
[i
][0] = s1
;
3405 verts
[1].st
[i
][1] = t
;
3407 verts
[2].st
[i
][0] = s1
;
3408 verts
[2].st
[i
][1] = t1
;
3410 verts
[3].st
[i
][0] = s
;
3411 verts
[3].st
[i
][1] = t1
;
3414 _mesa_buffer_sub_data(ctx
, drawtex
->buf_obj
, 0, sizeof(verts
), verts
);
3417 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3419 _mesa_meta_end(ctx
);
3423 cleartexsubimage_color(struct gl_context
*ctx
,
3424 struct gl_texture_image
*texImage
,
3425 const GLvoid
*clearValue
,
3429 union gl_color_union colorValue
;
3433 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
3434 GL_COLOR_ATTACHMENT0
,
3437 status
= _mesa_check_framebuffer_status(ctx
, ctx
->DrawBuffer
);
3438 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
3441 /* We don't want to apply an sRGB conversion so override the format */
3442 format
= _mesa_get_srgb_format_linear(texImage
->TexFormat
);
3443 datatype
= _mesa_get_format_datatype(format
);
3446 case GL_UNSIGNED_INT
:
3449 _mesa_unpack_uint_rgba_row(format
, 1, clearValue
,
3450 (GLuint (*)[4]) colorValue
.ui
);
3452 memset(&colorValue
, 0, sizeof colorValue
);
3453 if (datatype
== GL_INT
)
3454 _mesa_ClearBufferiv(GL_COLOR
, 0, colorValue
.i
);
3456 _mesa_ClearBufferuiv(GL_COLOR
, 0, colorValue
.ui
);
3460 _mesa_unpack_rgba_row(format
, 1, clearValue
,
3461 (GLfloat (*)[4]) colorValue
.f
);
3463 memset(&colorValue
, 0, sizeof colorValue
);
3464 _mesa_ClearBufferfv(GL_COLOR
, 0, colorValue
.f
);
3472 cleartexsubimage_depth_stencil(struct gl_context
*ctx
,
3473 struct gl_texture_image
*texImage
,
3474 const GLvoid
*clearValue
,
3481 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
3482 GL_DEPTH_ATTACHMENT
,
3485 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
)
3486 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
3487 GL_STENCIL_ATTACHMENT
,
3490 status
= _mesa_check_framebuffer_status(ctx
, ctx
->DrawBuffer
);
3491 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
3495 GLuint depthStencilValue
[2];
3497 /* Convert the clearValue from whatever format it's in to a floating
3498 * point value for the depth and an integer value for the stencil index
3500 _mesa_unpack_float_32_uint_24_8_depth_stencil_row(texImage
->TexFormat
,
3504 /* We need a memcpy here instead of a cast because we need to
3505 * reinterpret the bytes as a float rather than converting it
3507 memcpy(&depthValue
, depthStencilValue
, sizeof depthValue
);
3508 stencilValue
= depthStencilValue
[1] & 0xff;
3514 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
)
3515 _mesa_ClearBufferfi(GL_DEPTH_STENCIL
, 0, depthValue
, stencilValue
);
3517 _mesa_ClearBufferfv(GL_DEPTH
, 0, &depthValue
);
3523 cleartexsubimage_for_zoffset(struct gl_context
*ctx
,
3524 struct gl_texture_image
*texImage
,
3526 const GLvoid
*clearValue
)
3528 struct gl_framebuffer
*drawFb
;
3531 drawFb
= ctx
->Driver
.NewFramebuffer(ctx
, 0xDEADBEEF);
3535 _mesa_bind_framebuffers(ctx
, drawFb
, ctx
->ReadBuffer
);
3537 switch(texImage
->_BaseFormat
) {
3538 case GL_DEPTH_STENCIL
:
3539 case GL_DEPTH_COMPONENT
:
3540 success
= cleartexsubimage_depth_stencil(ctx
, texImage
,
3541 clearValue
, zoffset
);
3544 success
= cleartexsubimage_color(ctx
, texImage
, clearValue
, zoffset
);
3548 _mesa_reference_framebuffer(&drawFb
, NULL
);
3554 cleartexsubimage_using_fbo(struct gl_context
*ctx
,
3555 struct gl_texture_image
*texImage
,
3556 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3557 GLsizei width
, GLsizei height
, GLsizei depth
,
3558 const GLvoid
*clearValue
)
3560 bool success
= true;
3563 _mesa_meta_begin(ctx
,
3565 MESA_META_COLOR_MASK
|
3567 MESA_META_FRAMEBUFFER_SRGB
);
3569 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
3570 _mesa_set_enable(ctx
, GL_DITHER
, GL_FALSE
);
3572 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_TRUE
);
3573 _mesa_Scissor(xoffset
, yoffset
, width
, height
);
3575 for (z
= zoffset
; z
< zoffset
+ depth
; z
++) {
3576 if (!cleartexsubimage_for_zoffset(ctx
, texImage
, z
, clearValue
)) {
3582 _mesa_meta_end(ctx
);
3588 _mesa_meta_ClearTexSubImage(struct gl_context
*ctx
,
3589 struct gl_texture_image
*texImage
,
3590 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3591 GLsizei width
, GLsizei height
, GLsizei depth
,
3592 const GLvoid
*clearValue
)
3596 res
= cleartexsubimage_using_fbo(ctx
, texImage
,
3597 xoffset
, yoffset
, zoffset
,
3598 width
, height
, depth
,
3605 "Falling back to mapping the texture in "
3606 "glClearTexSubImage\n");
3608 _mesa_store_cleartexsubimage(ctx
, texImage
,
3609 xoffset
, yoffset
, zoffset
,
3610 width
, height
, depth
,