mesa: replace VP/FP/ATIfs _Enabled flags with helper functions
[mesa.git] / src / mesa / drivers / common / meta.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
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.
23 */
24
25 /**
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.
29 *
30 * \author Brian Paul
31 */
32
33
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"
61 #include "main/pbo.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"
90
91 /** Return offset in bytes of the field within a vertex struct */
92 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
93
94 static void
95 meta_clear(struct gl_context *ctx, GLbitfield buffers, bool glsl);
96
97 static struct blit_shader *
98 choose_blit_shader(GLenum target, struct blit_shader_table *table);
99
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);
107
108 void
109 _mesa_meta_framebuffer_texture_image(struct gl_context *ctx,
110 struct gl_framebuffer *fb,
111 GLenum attachment,
112 struct gl_texture_image *texImage,
113 GLuint layer)
114 {
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
119 : texObj->Target;
120
121 struct gl_renderbuffer_attachment *att =
122 _mesa_get_and_validate_attachment(ctx, fb, attachment, __func__);
123 assert(att);
124
125 _mesa_framebuffer_texture(ctx, fb, attachment, att, texObj, texTarget,
126 level, layer, false);
127 }
128
129 static struct gl_shader *
130 meta_compile_shader_with_debug(struct gl_context *ctx, gl_shader_stage stage,
131 const GLcharARB *source)
132 {
133 const GLuint name = ~0;
134 struct gl_shader *sh;
135
136 sh = _mesa_new_shader(name, stage);
137 sh->Source = strdup(source);
138 sh->CompileStatus = compile_failure;
139 _mesa_compile_shader(ctx, sh);
140
141 if (!sh->CompileStatus) {
142 if (sh->InfoLog) {
143 _mesa_problem(ctx,
144 "meta program compile failed:\n%s\nsource:\n%s\n",
145 sh->InfoLog, source);
146 }
147
148 _mesa_reference_shader(ctx, &sh, NULL);
149 }
150
151 return sh;
152 }
153
154 void
155 _mesa_meta_link_program_with_debug(struct gl_context *ctx,
156 struct gl_shader_program *sh_prog)
157 {
158 _mesa_link_program(ctx, sh_prog);
159
160 if (!sh_prog->data->LinkStatus) {
161 _mesa_problem(ctx, "meta program link failed:\n%s",
162 sh_prog->data->InfoLog);
163 }
164 }
165
166 void
167 _mesa_meta_use_program(struct gl_context *ctx,
168 struct gl_shader_program *sh_prog)
169 {
170 /* Attach shader state to the binding point */
171 _mesa_reference_pipeline_object(ctx, &ctx->_Shader, &ctx->Shader);
172
173 /* Update the program */
174 _mesa_use_shader_program(ctx, sh_prog);
175 }
176
177 void
178 _mesa_meta_compile_and_link_program(struct gl_context *ctx,
179 const char *vs_source,
180 const char *fs_source,
181 const char *name,
182 struct gl_shader_program **out_sh_prog)
183 {
184 struct gl_shader_program *sh_prog;
185 const GLuint id = ~0;
186
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);
195
196 _mesa_meta_link_program_with_debug(ctx, sh_prog);
197
198 _mesa_meta_use_program(ctx, sh_prog);
199
200 *out_sh_prog = sh_prog;
201 }
202
203 /**
204 * Generate a generic shader to blit from a texture to a framebuffer
205 *
206 * \param ctx Current GL context
207 * \param texTarget Texture target that will be the source of the blit
208 *
209 * \returns a handle to a shader program on success or zero on failure.
210 */
211 void
212 _mesa_meta_setup_blit_shader(struct gl_context *ctx,
213 GLenum target,
214 bool do_depth,
215 struct blit_shader_table *table)
216 {
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;
220 void *mem_ctx;
221
222 if (ctx->Const.GLSLVersion < 130) {
223 vs_preprocess = "";
224 fs_preprocess = "#extension GL_EXT_texture_array : enable";
225 fs_input = "varying";
226 } else {
227 vs_preprocess = "#version 130";
228 fs_preprocess = "#version 130";
229 fs_input = "in";
230 shader->func = "texture";
231 }
232
233 assert(shader != NULL);
234
235 if (shader->shader_prog != NULL) {
236 _mesa_meta_use_program(ctx, shader->shader_prog);
237 return;
238 }
239
240 mem_ctx = ralloc_context(NULL);
241
242 vs_source = ralloc_asprintf(mem_ctx,
243 "%s\n"
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"
248 "void main()\n"
249 "{\n"
250 " texCoords = textureCoords;\n"
251 " gl_Position = vec4(position, 0.0, 1.0);\n"
252 "}\n",
253 vs_preprocess);
254
255 fs_source = ralloc_asprintf(mem_ctx,
256 "%s\n"
257 "#extension GL_ARB_texture_cube_map_array: enable\n"
258 "uniform %s texSampler;\n"
259 "%s vec4 texCoords;\n"
260 "void main()\n"
261 "{\n"
262 " gl_FragColor = %s(texSampler, %s);\n"
263 "%s"
264 "}\n",
265 fs_preprocess, shader->type, fs_input,
266 shader->func, shader->texcoords,
267 do_depth ? " gl_FragDepth = gl_FragColor.x;\n" : "");
268
269 _mesa_meta_compile_and_link_program(ctx, vs_source, fs_source,
270 ralloc_asprintf(mem_ctx, "%s blit",
271 shader->type),
272 &shader->shader_prog);
273 ralloc_free(mem_ctx);
274 }
275
276 /**
277 * Configure vertex buffer and vertex array objects for tests
278 *
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.
282 *
283 * \param VAO Storage for vertex array object handle. If 0, a new VAO
284 * will be created.
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
294 * enabled.
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.
297 *
298 * \note If \c use_generic_attributes is \c true, \c color_size must be zero.
299 * Use \c texcoord_size instead.
300 */
301 void
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,
306 unsigned color_size)
307 {
308 if (*VAO == 0) {
309 struct gl_vertex_array_object *array_obj;
310 assert(*buf_obj == NULL);
311
312 /* create vertex array object */
313 _mesa_GenVertexArrays(1, VAO);
314 _mesa_BindVertexArray(*VAO);
315
316 array_obj = _mesa_lookup_vao(ctx, *VAO);
317 assert(array_obj != NULL);
318
319 /* create vertex array buffer */
320 *buf_obj = ctx->Driver.NewBufferObject(ctx, 0xDEADBEEF);
321 if (*buf_obj == NULL)
322 return;
323
324 _mesa_buffer_data(ctx, *buf_obj, GL_NONE, 4 * sizeof(struct vertex), NULL,
325 GL_DYNAMIC_DRAW, __func__);
326
327 /* setup vertex arrays */
328 FLUSH_VERTICES(ctx, 0);
329 if (use_generic_attributes) {
330 assert(color_size == 0);
331
332 _mesa_update_array_format(ctx, array_obj, VERT_ATTRIB_GENERIC(0),
333 vertex_size, GL_FLOAT, GL_RGBA, GL_FALSE,
334 GL_FALSE, 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));
349 }
350 } else {
351 _mesa_update_array_format(ctx, array_obj, VERT_ATTRIB_POS,
352 vertex_size, GL_FLOAT, GL_RGBA, GL_FALSE,
353 GL_FALSE, 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);
358
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,
362 GL_FALSE, 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));
367 }
368
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,
372 GL_FALSE, 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);
377 }
378 }
379 } else {
380 _mesa_BindVertexArray(*VAO);
381 }
382 }
383
384 /**
385 * Initialize meta-ops for a context.
386 * To be called once during context creation.
387 */
388 void
389 _mesa_meta_init(struct gl_context *ctx)
390 {
391 assert(!ctx->Meta);
392
393 ctx->Meta = CALLOC_STRUCT(gl_meta_state);
394 }
395
396 /**
397 * Free context meta-op state.
398 * To be called once during context destruction.
399 */
400 void
401 _mesa_meta_free(struct gl_context *ctx)
402 {
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);
411 if (old_context)
412 _mesa_make_current(old_context, old_context->WinSysDrawBuffer, old_context->WinSysReadBuffer);
413 else
414 _mesa_make_current(NULL, NULL, NULL);
415 free(ctx->Meta);
416 ctx->Meta = NULL;
417 }
418
419
420 /**
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.
423 *
424 * \param state bitmask of MESA_META_* flags indicating which attribute groups
425 * to save and reset to their defaults
426 */
427 void
428 _mesa_meta_begin(struct gl_context *ctx, GLbitfield state)
429 {
430 struct save_state *save;
431
432 /* hope MAX_META_OPS_DEPTH is large enough */
433 assert(ctx->Meta->SaveStackDepth < MAX_META_OPS_DEPTH);
434
435 save = &ctx->Meta->Save[ctx->Meta->SaveStackDepth++];
436 memset(save, 0, sizeof(*save));
437 save->SavedState = state;
438
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.
442 */
443 save->API = ctx->API;
444 ctx->API = API_OPENGL_COMPAT;
445
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.
452 */
453 save->ExtensionsVersion = ctx->Extensions.Version;
454 ctx->Extensions.Version = ~0;
455
456 /* Pausing transform feedback needs to be done early, or else we won't be
457 * able to change other state.
458 */
459 save->TransformFeedbackNeedsResume =
460 _mesa_is_xfb_active_and_unpaused(ctx);
461 if (save->TransformFeedbackNeedsResume)
462 _mesa_PauseTransformFeedback();
463
464 /* After saving the current occlusion object, call EndQuery so that no
465 * occlusion querying will be active during the meta-operation.
466 */
467 if (state & MESA_META_OCCLUSION_QUERY) {
468 save->CurrentOcclusionObject = ctx->Query.CurrentOcclusionObject;
469 if (save->CurrentOcclusionObject)
470 _mesa_EndQuery(save->CurrentOcclusionObject->Target);
471 }
472
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);
479 }
480
481 if (state & MESA_META_BLEND) {
482 save->BlendEnabled = ctx->Color.BlendEnabled;
483 if (ctx->Color.BlendEnabled) {
484 if (ctx->Extensions.EXT_draw_buffers2) {
485 GLuint i;
486 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
487 _mesa_set_enablei(ctx, GL_BLEND, i, GL_FALSE);
488 }
489 }
490 else {
491 _mesa_set_enable(ctx, GL_BLEND, GL_FALSE);
492 }
493 }
494 save->ColorLogicOpEnabled = ctx->Color.ColorLogicOpEnabled;
495 if (ctx->Color.ColorLogicOpEnabled)
496 _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP, GL_FALSE);
497 }
498
499 if (state & MESA_META_DITHER) {
500 save->DitherFlag = ctx->Color.DitherFlag;
501 _mesa_set_enable(ctx, GL_DITHER, GL_TRUE);
502 }
503
504 if (state & MESA_META_COLOR_MASK) {
505 memcpy(save->ColorMask, ctx->Color.ColorMask,
506 sizeof(ctx->Color.ColorMask));
507 }
508
509 if (state & MESA_META_DEPTH_TEST) {
510 save->Depth = ctx->Depth; /* struct copy */
511 if (ctx->Depth.Test)
512 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_FALSE);
513 }
514
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);
519 }
520
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;
526 }
527
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;
547 /* XXX more state */
548 ctx->NewState |=_NEW_PIXEL;
549 }
550
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);
563 }
564
565 if (state & MESA_META_SCISSOR) {
566 save->Scissor = ctx->Scissor; /* struct copy */
567 _mesa_set_enable(ctx, GL_SCISSOR_TEST, GL_FALSE);
568 }
569
570 if (state & MESA_META_SHADER) {
571 int i;
572
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);
578 }
579
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);
585 }
586
587 if (ctx->Extensions.ATI_fragment_shader) {
588 save->ATIFragmentShaderEnabled = ctx->ATIFragmentShader.Enabled;
589 _mesa_set_enable(ctx, GL_FRAGMENT_SHADER_ATI, GL_FALSE);
590 }
591
592 if (ctx->Pipeline.Current) {
593 _mesa_reference_pipeline_object(ctx, &save->Pipeline,
594 ctx->Pipeline.Current);
595 _mesa_BindProgramPipeline(0);
596 }
597
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.
600 */
601 for (i = 0; i < MESA_SHADER_STAGES; i++) {
602 _mesa_reference_program(ctx, &save->Program[i],
603 ctx->Shader.CurrentProgram[i]);
604 }
605 _mesa_reference_shader_program(ctx, &save->ActiveShader,
606 ctx->Shader.ActiveProgram);
607
608 _mesa_UseProgram(0);
609 }
610
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 */
616 }
617
618 if (state & MESA_META_TEXTURE) {
619 GLuint u, tgt;
620
621 save->ActiveUnit = ctx->Texture.CurrentUnit;
622 save->EnvMode = ctx->Texture.Unit[0].EnvMode;
623
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);
634
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);
643 }
644 }
645
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]);
650 }
651
652 /* set defaults for unit[0] */
653 _mesa_ActiveTexture(GL_TEXTURE0);
654 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
655 }
656
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();
675
676 /* glOrtho with width = 0 or height = 0 generates GL_INVALID_VALUE.
677 * This can occur when there is no draw buffer.
678 */
679 if (ctx->DrawBuffer->Width != 0 && ctx->DrawBuffer->Height != 0)
680 _mesa_Ortho(0.0, ctx->DrawBuffer->Width,
681 0.0, ctx->DrawBuffer->Height,
682 -1.0, 1.0);
683
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);
688 }
689 }
690
691 if (state & MESA_META_CLIP) {
692 GLbitfield mask;
693 save->ClipPlanesEnabled = ctx->Transform.ClipPlanesEnabled;
694 mask = ctx->Transform.ClipPlanesEnabled;
695 while (mask) {
696 const int i = u_bit_scan(&mask);
697 _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_FALSE);
698 }
699 }
700
701 if (state & MESA_META_VERTEX) {
702 /* save vertex array object state */
703 _mesa_reference_vao(ctx, &save->VAO,
704 ctx->Array.VAO);
705 /* set some default state? */
706 }
707
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);
721 }
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);
727 }
728
729 if (state & MESA_META_CLAMP_FRAGMENT_COLOR) {
730 save->ClampFragmentColor = ctx->Color.ClampFragmentColor;
731
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.
735 */
736 if (ctx->Color.ClampFragmentColor != GL_TRUE &&
737 ctx->Extensions.ARB_color_buffer_float)
738 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
739 }
740
741 if (state & MESA_META_CLAMP_VERTEX_COLOR) {
742 save->ClampVertexColor = ctx->Light.ClampVertexColor;
743
744 /* Generally in here we never want vertex color clamping --
745 * result clamping is only dependent on fragment clamping.
746 */
747 if (ctx->Extensions.ARB_color_buffer_float)
748 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR, GL_FALSE);
749 }
750
751 if (state & MESA_META_CONDITIONAL_RENDER) {
752 save->CondRenderQuery = ctx->Query.CondRenderQuery;
753 save->CondRenderMode = ctx->Query.CondRenderMode;
754
755 if (ctx->Query.CondRenderQuery)
756 _mesa_EndConditionalRender();
757 }
758
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);
767 }
768 }
769
770 if (state & MESA_META_MULTISAMPLE) {
771 save->Multisample = ctx->Multisample; /* struct copy */
772
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);
785 }
786
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);
791 }
792
793 if (state & MESA_META_DRAW_BUFFERS) {
794 struct gl_framebuffer *fb = ctx->DrawBuffer;
795 memcpy(save->ColorDrawBuffers, fb->ColorDrawBuffer,
796 sizeof(save->ColorDrawBuffers));
797 }
798
799 /* misc */
800 {
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);
807
808 _mesa_reference_framebuffer(&save->DrawBuffer, ctx->DrawBuffer);
809 _mesa_reference_framebuffer(&save->ReadBuffer, ctx->ReadBuffer);
810 }
811 }
812
813
814 /**
815 * Leave meta state. This is like a light-weight version of glPopAttrib().
816 */
817 void
818 _mesa_meta_end(struct gl_context *ctx)
819 {
820 assert(ctx->Meta->SaveStackDepth > 0);
821
822 struct save_state *save = &ctx->Meta->Save[ctx->Meta->SaveStackDepth - 1];
823 const GLbitfield state = save->SavedState;
824 int i;
825
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;
832 GLuint64EXT result;
833 if (!q->Ready)
834 ctx->Driver.WaitQuery(ctx, q);
835 result = q->Result;
836 _mesa_BeginQuery(q->Target, q->Id);
837 ctx->Query.CurrentOcclusionObject->Result += result;
838 }
839 }
840
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);
845 }
846
847 if (state & MESA_META_BLEND) {
848 if (ctx->Color.BlendEnabled != save->BlendEnabled) {
849 if (ctx->Extensions.EXT_draw_buffers2) {
850 GLuint i;
851 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
852 _mesa_set_enablei(ctx, GL_BLEND, i, (save->BlendEnabled >> i) & 1);
853 }
854 }
855 else {
856 _mesa_set_enable(ctx, GL_BLEND, (save->BlendEnabled & 1));
857 }
858 }
859 if (ctx->Color.ColorLogicOpEnabled != save->ColorLogicOpEnabled)
860 _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP, save->ColorLogicOpEnabled);
861 }
862
863 if (state & MESA_META_DITHER)
864 _mesa_set_enable(ctx, GL_DITHER, save->DitherFlag);
865
866 if (state & MESA_META_COLOR_MASK) {
867 GLuint i;
868 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
869 if (!TEST_EQ_4V(ctx->Color.ColorMask[i], save->ColorMask[i])) {
870 if (i == 0) {
871 _mesa_ColorMask(save->ColorMask[i][0], save->ColorMask[i][1],
872 save->ColorMask[i][2], save->ColorMask[i][3]);
873 }
874 else {
875 _mesa_ColorMaski(i,
876 save->ColorMask[i][0],
877 save->ColorMask[i][1],
878 save->ColorMask[i][2],
879 save->ColorMask[i][3]);
880 }
881 }
882 }
883 }
884
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);
890 }
891
892 if (state & MESA_META_FOG) {
893 _mesa_set_enable(ctx, GL_FOG, save->Fog);
894 }
895
896 if (state & MESA_META_PIXEL_STORE) {
897 ctx->Pack = save->Pack;
898 ctx->Unpack = save->Unpack;
899 }
900
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;
911 /* XXX more state */
912 ctx->NewState |=_NEW_PIXEL;
913 }
914
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);
922 }
923
924 if (state & MESA_META_SCISSOR) {
925 unsigned i;
926
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);
935 }
936 }
937
938 if (state & MESA_META_SHADER) {
939 bool any_shader;
940
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);
947 }
948
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);
955 }
956
957 if (ctx->Extensions.ATI_fragment_shader) {
958 _mesa_set_enable(ctx, GL_FRAGMENT_SHADER_ATI,
959 save->ATIFragmentShaderEnabled);
960 }
961
962 any_shader = false;
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
968 * in that case.
969 */
970 _mesa_use_program(ctx, i, NULL, save->Program[i], &ctx->Shader);
971
972 /* Do this *before* killing the reference. :)
973 */
974 if (save->Program[i] != NULL)
975 any_shader = true;
976
977 _mesa_reference_program(ctx, &save->Program[i], NULL);
978 }
979
980 _mesa_reference_shader_program(ctx, &ctx->Shader.ActiveProgram,
981 save->ActiveShader);
982 _mesa_reference_shader_program(ctx, &save->ActiveShader, NULL);
983
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.
987 */
988 if (any_shader)
989 _mesa_reference_pipeline_object(ctx, &ctx->_Shader,
990 &ctx->Shader);
991 else
992 _mesa_reference_pipeline_object(ctx, &ctx->_Shader,
993 ctx->Pipeline.Default);
994
995 if (save->Pipeline) {
996 _mesa_bind_pipeline(ctx, save->Pipeline);
997
998 _mesa_reference_pipeline_object(ctx, &save->Pipeline, NULL);
999 }
1000 }
1001
1002 if (state & MESA_META_STENCIL_TEST) {
1003 const struct gl_stencil_attrib *stencil = &save->Stencil;
1004
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);
1012 }
1013 /* front state */
1014 _mesa_StencilFuncSeparate(GL_FRONT,
1015 stencil->Function[0],
1016 stencil->Ref[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]);
1022 /* back state */
1023 _mesa_StencilFuncSeparate(GL_BACK,
1024 stencil->Function[1],
1025 stencil->Ref[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]);
1031 }
1032
1033 if (state & MESA_META_TEXTURE) {
1034 GLuint u, tgt;
1035
1036 assert(ctx->Texture.CurrentUnit == 0);
1037
1038 /* restore texenv for unit[0] */
1039 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, save->EnvMode);
1040
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]);
1047 }
1048 _mesa_reference_texobj(&save->CurrentTexture[tgt], NULL);
1049 }
1050
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];
1056 }
1057
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];
1061 }
1062 }
1063
1064 /* restore current unit state */
1065 _mesa_ActiveTexture(GL_TEXTURE0 + save->ActiveUnit);
1066 }
1067
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);
1074
1075 _mesa_MatrixMode(GL_MODELVIEW);
1076 _mesa_LoadMatrixf(save->ModelviewMatrix);
1077
1078 _mesa_MatrixMode(GL_PROJECTION);
1079 _mesa_LoadMatrixf(save->ProjectionMatrix);
1080
1081 _mesa_MatrixMode(save->MatrixMode);
1082
1083 if (ctx->Extensions.ARB_clip_control)
1084 _mesa_ClipControl(save->ClipOrigin, save->ClipDepthMode);
1085 }
1086
1087 if (state & MESA_META_CLIP) {
1088 GLbitfield mask = save->ClipPlanesEnabled;
1089 while (mask) {
1090 const int i = u_bit_scan(&mask);
1091 _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_TRUE);
1092 }
1093 }
1094
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);
1099 }
1100
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);
1108 }
1109 _mesa_set_depth_range(ctx, 0, save->DepthNear, save->DepthFar);
1110 }
1111
1112 if (state & MESA_META_CLAMP_FRAGMENT_COLOR &&
1113 ctx->Extensions.ARB_color_buffer_float) {
1114 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR, save->ClampFragmentColor);
1115 }
1116
1117 if (state & MESA_META_CLAMP_VERTEX_COLOR &&
1118 ctx->Extensions.ARB_color_buffer_float) {
1119 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR, save->ClampVertexColor);
1120 }
1121
1122 if (state & MESA_META_CONDITIONAL_RENDER) {
1123 if (save->CondRenderQuery)
1124 _mesa_BeginConditionalRender(save->CondRenderQuery->Id,
1125 save->CondRenderMode);
1126 }
1127
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;
1135 }
1136 }
1137
1138 if (state & MESA_META_MULTISAMPLE) {
1139 struct gl_multisample_attrib *ctx_ms = &ctx->Multisample;
1140 struct gl_multisample_attrib *save_ms = &save->Multisample;
1141
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);
1154 }
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);
1163 }
1164
1165 if (state & MESA_META_FRAMEBUFFER_SRGB) {
1166 if (ctx->Color.sRGBEnabled != save->sRGBEnabled)
1167 _mesa_set_framebuffer_srgb(ctx, save->sRGBEnabled);
1168 }
1169
1170 /* misc */
1171 if (save->Lighting) {
1172 _mesa_set_enable(ctx, GL_LIGHTING, GL_TRUE);
1173 }
1174 if (save->RasterDiscard) {
1175 _mesa_set_enable(ctx, GL_RASTERIZER_DISCARD, GL_TRUE);
1176 }
1177 if (save->TransformFeedbackNeedsResume)
1178 _mesa_ResumeTransformFeedback();
1179
1180 _mesa_bind_framebuffers(ctx, save->DrawBuffer, save->ReadBuffer);
1181 _mesa_reference_framebuffer(&save->DrawBuffer, NULL);
1182 _mesa_reference_framebuffer(&save->ReadBuffer, NULL);
1183
1184 if (state & MESA_META_DRAW_BUFFERS) {
1185 _mesa_drawbuffers(ctx, ctx->DrawBuffer, ctx->Const.MaxDrawBuffers,
1186 save->ColorDrawBuffers, NULL);
1187 }
1188
1189 ctx->Meta->SaveStackDepth--;
1190
1191 ctx->API = save->API;
1192 ctx->Extensions.Version = save->ExtensionsVersion;
1193 }
1194
1195
1196 /**
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.
1202 */
1203 static inline GLfloat
1204 invert_z(GLfloat normZ)
1205 {
1206 GLfloat objZ = 1.0f - 2.0f * normZ;
1207 return objZ;
1208 }
1209
1210
1211 /**
1212 * One-time init for a temp_texture object.
1213 * Choose tex target, compute max tex size, etc.
1214 */
1215 static void
1216 init_temp_texture(struct gl_context *ctx, struct temp_texture *tex)
1217 {
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;
1223 }
1224 else {
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;
1229 }
1230 tex->MinSize = 16; /* 16 x 16 at least */
1231 assert(tex->MaxSize > 0);
1232
1233 _mesa_GenTextures(1, &tex->TexObj);
1234 }
1235
1236 static void
1237 cleanup_temp_texture(struct temp_texture *tex)
1238 {
1239 if (!tex->TexObj)
1240 return;
1241 _mesa_DeleteTextures(1, &tex->TexObj);
1242 tex->TexObj = 0;
1243 }
1244
1245
1246 /**
1247 * Return pointer to temp_texture info for non-bitmap ops.
1248 * This does some one-time init if needed.
1249 */
1250 struct temp_texture *
1251 _mesa_meta_get_temp_texture(struct gl_context *ctx)
1252 {
1253 struct temp_texture *tex = &ctx->Meta->TempTex;
1254
1255 if (!tex->TexObj) {
1256 init_temp_texture(ctx, tex);
1257 }
1258
1259 return tex;
1260 }
1261
1262
1263 /**
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.
1267 */
1268 static struct temp_texture *
1269 get_bitmap_temp_texture(struct gl_context *ctx)
1270 {
1271 struct temp_texture *tex = &ctx->Meta->Bitmap.Tex;
1272
1273 if (!tex->TexObj) {
1274 init_temp_texture(ctx, tex);
1275 }
1276
1277 return tex;
1278 }
1279
1280 /**
1281 * Return pointer to depth temp_texture.
1282 * This does some one-time init if needed.
1283 */
1284 struct temp_texture *
1285 _mesa_meta_get_temp_depth_texture(struct gl_context *ctx)
1286 {
1287 struct temp_texture *tex = &ctx->Meta->Blit.depthTex;
1288
1289 if (!tex->TexObj) {
1290 init_temp_texture(ctx, tex);
1291 }
1292
1293 return tex;
1294 }
1295
1296 /**
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.
1302 *
1303 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1304 */
1305 GLboolean
1306 _mesa_meta_alloc_texture(struct temp_texture *tex,
1307 GLsizei width, GLsizei height, GLenum intFormat)
1308 {
1309 GLboolean newTex = GL_FALSE;
1310
1311 assert(width <= tex->MaxSize);
1312 assert(height <= tex->MaxSize);
1313
1314 if (width > tex->Width ||
1315 height > tex->Height ||
1316 intFormat != tex->IntFormat) {
1317 /* alloc new texture (larger or different format) */
1318
1319 if (tex->NPOT) {
1320 /* use non-power of two size */
1321 tex->Width = MAX2(tex->MinSize, width);
1322 tex->Height = MAX2(tex->MinSize, height);
1323 }
1324 else {
1325 /* find power of two size */
1326 GLsizei w, h;
1327 w = h = tex->MinSize;
1328 while (w < width)
1329 w *= 2;
1330 while (h < height)
1331 h *= 2;
1332 tex->Width = w;
1333 tex->Height = h;
1334 }
1335
1336 tex->IntFormat = intFormat;
1337
1338 newTex = GL_TRUE;
1339 }
1340
1341 /* compute texcoords */
1342 if (tex->Target == GL_TEXTURE_RECTANGLE) {
1343 tex->Sright = (GLfloat) width;
1344 tex->Ttop = (GLfloat) height;
1345 }
1346 else {
1347 tex->Sright = (GLfloat) width / tex->Width;
1348 tex->Ttop = (GLfloat) height / tex->Height;
1349 }
1350
1351 return newTex;
1352 }
1353
1354
1355 /**
1356 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1357 */
1358 void
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,
1363 GLenum intFormat,
1364 GLenum filter)
1365 {
1366 bool newTex;
1367
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);
1372
1373 newTex = _mesa_meta_alloc_texture(tex, width, height, intFormat);
1374
1375 /* copy framebuffer image to texture */
1376 if (newTex) {
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);
1382 }
1383 else {
1384 /* create empty texture */
1385 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
1386 tex->Width, tex->Height, 0,
1387 intFormat, GL_UNSIGNED_BYTE, NULL);
1388 /* load image */
1389 _mesa_CopyTexSubImage2D(tex->Target, 0,
1390 0, 0, srcX, srcY, width, height);
1391 }
1392 }
1393 else {
1394 /* replace existing tex image */
1395 _mesa_CopyTexSubImage2D(tex->Target, 0,
1396 0, 0, srcX, srcY, width, height);
1397 }
1398 }
1399
1400
1401 /**
1402 * Setup/load texture for glDrawPixels.
1403 */
1404 void
1405 _mesa_meta_setup_drawpix_texture(struct gl_context *ctx,
1406 struct temp_texture *tex,
1407 GLboolean newTex,
1408 GLsizei width, GLsizei height,
1409 GLenum format, GLenum type,
1410 const GLvoid *pixels)
1411 {
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);
1416
1417 /* copy pixel data to texture */
1418 if (newTex) {
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);
1424 }
1425 else {
1426 struct gl_buffer_object *save_unpack_obj = NULL;
1427
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);
1437 /* load image */
1438 _mesa_TexSubImage2D(tex->Target, 0,
1439 0, 0, width, height, format, type, pixels);
1440 }
1441 }
1442 else {
1443 /* replace existing tex image */
1444 _mesa_TexSubImage2D(tex->Target, 0,
1445 0, 0, width, height, format, type, pixels);
1446 }
1447 }
1448
1449 void
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)
1453 {
1454 _mesa_meta_setup_vertex_objects(ctx, VAO, buf_obj, false, 2, texcoord_size,
1455 0);
1456
1457 /* setup projection matrix */
1458 _mesa_MatrixMode(GL_PROJECTION);
1459 _mesa_LoadIdentity();
1460 }
1461
1462 /**
1463 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1464 */
1465 void
1466 _mesa_meta_Clear(struct gl_context *ctx, GLbitfield buffers)
1467 {
1468 meta_clear(ctx, buffers, false);
1469 }
1470
1471 void
1472 _mesa_meta_glsl_Clear(struct gl_context *ctx, GLbitfield buffers)
1473 {
1474 meta_clear(ctx, buffers, true);
1475 }
1476
1477 static void
1478 meta_glsl_clear_init(struct gl_context *ctx, struct clear_state *clear)
1479 {
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"
1485 "void main()\n"
1486 "{\n"
1487 "#ifdef GL_AMD_vertex_shader_layer\n"
1488 " gl_Layer = gl_InstanceID;\n"
1489 "#endif\n"
1490 " gl_Position = position;\n"
1491 "}\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"
1496 "void main()\n"
1497 "{\n"
1498 " gl_FragColor = color;\n"
1499 "}\n";
1500 bool has_integer_textures;
1501
1502 _mesa_meta_setup_vertex_objects(ctx, &clear->VAO, &clear->buf_obj, true,
1503 3, 0, 0);
1504
1505 if (clear->ShaderProg != 0)
1506 return;
1507
1508 _mesa_meta_compile_and_link_program(ctx, vs_source, fs_source, "meta clear",
1509 &clear->ShaderProg);
1510
1511 has_integer_textures = _mesa_is_gles3(ctx) ||
1512 (_mesa_is_desktop_gl(ctx) && ctx->Const.GLSLVersion >= 130);
1513
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,
1518 "#version 130\n"
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"
1523 "void main()\n"
1524 "{\n"
1525 "#ifdef GL_AMD_vertex_shader_layer\n"
1526 " gl_Layer = gl_InstanceID;\n"
1527 "#endif\n"
1528 " gl_Position = position;\n"
1529 "}\n");
1530 const char *fs_int_source =
1531 ralloc_asprintf(shader_source_mem_ctx,
1532 "#version 130\n"
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"
1537 "\n"
1538 "void main()\n"
1539 "{\n"
1540 " out_color = color;\n"
1541 "}\n");
1542
1543 _mesa_meta_compile_and_link_program(ctx, vs_int_source, fs_int_source,
1544 "integer clear",
1545 &clear->IntegerShaderProg);
1546 ralloc_free(shader_source_mem_ctx);
1547
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.
1551 */
1552 }
1553 }
1554
1555 static void
1556 meta_glsl_clear_cleanup(struct gl_context *ctx, struct clear_state *clear)
1557 {
1558 if (clear->VAO == 0)
1559 return;
1560 _mesa_DeleteVertexArrays(1, &clear->VAO);
1561 clear->VAO = 0;
1562 _mesa_reference_buffer_object(ctx, &clear->buf_obj, NULL);
1563 _mesa_reference_shader_program(ctx, &clear->ShaderProg, NULL);
1564
1565 if (clear->IntegerShaderProg) {
1566 _mesa_reference_shader_program(ctx, &clear->IntegerShaderProg, NULL);
1567 }
1568 }
1569
1570 /**
1571 * Given a bitfield of BUFFER_BIT_x draw buffers, call glDrawBuffers to
1572 * set GL to only draw to those buffers.
1573 *
1574 * Since the bitfield has no associated order, the assignment of draw buffer
1575 * indices to color attachment indices is rather arbitrary.
1576 */
1577 void
1578 _mesa_meta_drawbuffers_from_bitfield(GLbitfield bits)
1579 {
1580 GLenum enums[MAX_DRAW_BUFFERS];
1581 int i = 0;
1582 int n;
1583
1584 /* This function is only legal for color buffer bitfields. */
1585 assert((bits & ~BUFFER_BITS_COLOR) == 0);
1586
1587 /* Make sure we don't overflow any arrays. */
1588 assert(_mesa_bitcount(bits) <= MAX_DRAW_BUFFERS);
1589
1590 enums[0] = GL_NONE;
1591
1592 if (bits & BUFFER_BIT_FRONT_LEFT)
1593 enums[i++] = GL_FRONT_LEFT;
1594
1595 if (bits & BUFFER_BIT_FRONT_RIGHT)
1596 enums[i++] = GL_FRONT_RIGHT;
1597
1598 if (bits & BUFFER_BIT_BACK_LEFT)
1599 enums[i++] = GL_BACK_LEFT;
1600
1601 if (bits & BUFFER_BIT_BACK_RIGHT)
1602 enums[i++] = GL_BACK_RIGHT;
1603
1604 for (n = 0; n < MAX_COLOR_ATTACHMENTS; n++) {
1605 if (bits & (1 << (BUFFER_COLOR0 + n)))
1606 enums[i++] = GL_COLOR_ATTACHMENT0 + n;
1607 }
1608
1609 _mesa_DrawBuffers(i, enums);
1610 }
1611
1612 /**
1613 * Return if all of the color channels are masked.
1614 */
1615 static inline GLboolean
1616 is_color_disabled(struct gl_context *ctx, int i)
1617 {
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];
1622 }
1623
1624 /**
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.
1628 */
1629 static void
1630 _mesa_meta_drawbuffers_and_colormask(struct gl_context *ctx, GLbitfield mask)
1631 {
1632 GLenum enums[MAX_DRAW_BUFFERS];
1633 GLubyte colormask[MAX_DRAW_BUFFERS][4];
1634 int num_bufs = 0;
1635
1636 /* This function is only legal for color buffer bitfields. */
1637 assert((mask & ~BUFFER_BITS_COLOR) == 0);
1638
1639 /* Make sure we don't overflow any arrays. */
1640 assert(_mesa_bitcount(mask) <= MAX_DRAW_BUFFERS);
1641
1642 enums[0] = GL_NONE;
1643
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;
1647
1648 if (b < 0 || !(mask & (1 << b)) || is_color_disabled(ctx, colormask_idx))
1649 continue;
1650
1651 switch (b) {
1652 case BUFFER_FRONT_LEFT:
1653 enums[num_bufs] = GL_FRONT_LEFT;
1654 break;
1655 case BUFFER_FRONT_RIGHT:
1656 enums[num_bufs] = GL_FRONT_RIGHT;
1657 break;
1658 case BUFFER_BACK_LEFT:
1659 enums[num_bufs] = GL_BACK_LEFT;
1660 break;
1661 case BUFFER_BACK_RIGHT:
1662 enums[num_bufs] = GL_BACK_RIGHT;
1663 break;
1664 default:
1665 assert(b >= BUFFER_COLOR0 && b <= BUFFER_COLOR7);
1666 enums[num_bufs] = GL_COLOR_ATTACHMENT0 + (b - BUFFER_COLOR0);
1667 break;
1668 }
1669
1670 for (int k = 0; k < 4; k++)
1671 colormask[num_bufs][k] = ctx->Color.ColorMask[colormask_idx][k];
1672
1673 num_bufs++;
1674 }
1675
1676 _mesa_DrawBuffers(num_bufs, enums);
1677
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]);
1681 }
1682 }
1683
1684
1685 /**
1686 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1687 */
1688 static void
1689 meta_clear(struct gl_context *ctx, GLbitfield buffers, bool glsl)
1690 {
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];
1697 int i;
1698
1699 metaSave = (MESA_META_ALPHA_TEST |
1700 MESA_META_BLEND |
1701 MESA_META_COLOR_MASK |
1702 MESA_META_DEPTH_TEST |
1703 MESA_META_RASTERIZATION |
1704 MESA_META_SHADER |
1705 MESA_META_STENCIL_TEST |
1706 MESA_META_VERTEX |
1707 MESA_META_VIEWPORT |
1708 MESA_META_CLIP |
1709 MESA_META_CLAMP_FRAGMENT_COLOR |
1710 MESA_META_MULTISAMPLE |
1711 MESA_META_OCCLUSION_QUERY);
1712
1713 if (!glsl) {
1714 metaSave |= MESA_META_FOG |
1715 MESA_META_PIXEL_TRANSFER |
1716 MESA_META_TRANSFORM |
1717 MESA_META_TEXTURE |
1718 MESA_META_CLAMP_VERTEX_COLOR |
1719 MESA_META_SELECT_FEEDBACK;
1720 }
1721
1722 if (buffers & BUFFER_BITS_COLOR) {
1723 metaSave |= MESA_META_DRAW_BUFFERS;
1724 }
1725
1726 _mesa_meta_begin(ctx, metaSave);
1727
1728 if (glsl) {
1729 meta_glsl_clear_init(ctx, clear);
1730
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);
1736 } else {
1737 _mesa_meta_setup_vertex_objects(ctx, &clear->VAO, &clear->buf_obj, false,
1738 3, 0, 4);
1739
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);
1745 }
1746
1747 if (fb->_IntegerBuffers) {
1748 assert(glsl);
1749 _mesa_meta_use_program(ctx, clear->IntegerShaderProg);
1750 _mesa_Uniform4iv(0, 1, ctx->Color.ClearColor.i);
1751 } else if (glsl) {
1752 _mesa_meta_use_program(ctx, clear->ShaderProg);
1753 _mesa_Uniform4fv(0, 1, ctx->Color.ClearColor.f);
1754 }
1755
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);
1760
1761 /* leave colormask state as-is */
1762
1763 /* Clears never have the color clamped. */
1764 if (ctx->Extensions.ARB_color_buffer_float)
1765 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
1766 }
1767 else {
1768 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1769 }
1770
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);
1776 }
1777 else {
1778 assert(!ctx->Depth.Test);
1779 }
1780
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]);
1789 }
1790 else {
1791 assert(!ctx->Stencil.Enabled);
1792 }
1793
1794 /* vertex positions */
1795 verts[0].x = x0;
1796 verts[0].y = y0;
1797 verts[0].z = z;
1798 verts[1].x = x1;
1799 verts[1].y = y0;
1800 verts[1].z = z;
1801 verts[2].x = x1;
1802 verts[2].y = y1;
1803 verts[2].z = z;
1804 verts[3].x = x0;
1805 verts[3].y = y1;
1806 verts[3].z = z;
1807
1808 if (!glsl) {
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];
1814 }
1815 }
1816
1817 /* upload new vertex data */
1818 _mesa_buffer_data(ctx, clear->buf_obj, GL_NONE, sizeof(verts), verts,
1819 GL_DYNAMIC_DRAW, __func__);
1820
1821 /* draw quad(s) */
1822 if (fb->MaxNumLayers > 0) {
1823 _mesa_DrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, fb->MaxNumLayers);
1824 } else {
1825 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1826 }
1827
1828 _mesa_meta_end(ctx);
1829 }
1830
1831 /**
1832 * Meta implementation of ctx->Driver.CopyPixels() in terms
1833 * of texture mapping and polygon rendering and GLSL shaders.
1834 */
1835 void
1836 _mesa_meta_CopyPixels(struct gl_context *ctx, GLint srcX, GLint srcY,
1837 GLsizei width, GLsizei height,
1838 GLint dstX, GLint dstY, GLenum type)
1839 {
1840 struct copypix_state *copypix = &ctx->Meta->CopyPix;
1841 struct temp_texture *tex = _mesa_meta_get_temp_texture(ctx);
1842 struct vertex verts[4];
1843
1844 if (type != GL_COLOR ||
1845 ctx->_ImageTransferState ||
1846 ctx->Fog.Enabled ||
1847 width > tex->MaxSize ||
1848 height > tex->MaxSize) {
1849 /* XXX avoid this fallback */
1850 _swrast_CopyPixels(ctx, srcX, srcY, width, height, dstX, dstY, type);
1851 return;
1852 }
1853
1854 /* Most GL state applies to glCopyPixels, but a there's a few things
1855 * we need to override:
1856 */
1857 _mesa_meta_begin(ctx, (MESA_META_RASTERIZATION |
1858 MESA_META_SHADER |
1859 MESA_META_TEXTURE |
1860 MESA_META_TRANSFORM |
1861 MESA_META_CLIP |
1862 MESA_META_VERTEX |
1863 MESA_META_VIEWPORT));
1864
1865 _mesa_meta_setup_vertex_objects(ctx, &copypix->VAO, &copypix->buf_obj, false,
1866 3, 2, 0);
1867
1868 /* Silence valgrind warnings about reading uninitialized stack. */
1869 memset(verts, 0, sizeof(verts));
1870
1871 /* Alloc/setup texture */
1872 _mesa_meta_setup_copypix_texture(ctx, tex, srcX, srcY, width, height,
1873 GL_RGBA, GL_NEAREST);
1874
1875 /* vertex positions, texcoords (after texture allocation!) */
1876 {
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]);
1882
1883 verts[0].x = dstX0;
1884 verts[0].y = dstY0;
1885 verts[0].z = z;
1886 verts[0].tex[0] = 0.0F;
1887 verts[0].tex[1] = 0.0F;
1888 verts[1].x = dstX1;
1889 verts[1].y = dstY0;
1890 verts[1].z = z;
1891 verts[1].tex[0] = tex->Sright;
1892 verts[1].tex[1] = 0.0F;
1893 verts[2].x = dstX1;
1894 verts[2].y = dstY1;
1895 verts[2].z = z;
1896 verts[2].tex[0] = tex->Sright;
1897 verts[2].tex[1] = tex->Ttop;
1898 verts[3].x = dstX0;
1899 verts[3].y = dstY1;
1900 verts[3].z = z;
1901 verts[3].tex[0] = 0.0F;
1902 verts[3].tex[1] = tex->Ttop;
1903
1904 /* upload new vertex data */
1905 _mesa_buffer_sub_data(ctx, copypix->buf_obj, 0, sizeof(verts), verts);
1906 }
1907
1908 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
1909
1910 /* draw textured quad */
1911 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1912
1913 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
1914
1915 _mesa_meta_end(ctx);
1916 }
1917
1918 static void
1919 meta_drawpix_cleanup(struct gl_context *ctx, struct drawpix_state *drawpix)
1920 {
1921 if (drawpix->VAO != 0) {
1922 _mesa_DeleteVertexArrays(1, &drawpix->VAO);
1923 drawpix->VAO = 0;
1924
1925 _mesa_reference_buffer_object(ctx, &drawpix->buf_obj, NULL);
1926 }
1927
1928 if (drawpix->StencilFP != 0) {
1929 _mesa_DeleteProgramsARB(1, &drawpix->StencilFP);
1930 drawpix->StencilFP = 0;
1931 }
1932
1933 if (drawpix->DepthFP != 0) {
1934 _mesa_DeleteProgramsARB(1, &drawpix->DepthFP);
1935 drawpix->DepthFP = 0;
1936 }
1937 }
1938
1939 /**
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.
1943 */
1944 static void
1945 tiled_draw_pixels(struct gl_context *ctx,
1946 GLint tileSize,
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)
1951 {
1952 struct gl_pixelstore_attrib tileUnpack = *unpack;
1953 GLint i, j;
1954
1955 if (tileUnpack.RowLength == 0)
1956 tileUnpack.RowLength = width;
1957
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);
1961
1962 tileUnpack.SkipPixels = unpack->SkipPixels + i;
1963
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);
1967
1968 tileUnpack.SkipRows = unpack->SkipRows + j;
1969
1970 _mesa_meta_DrawPixels(ctx, tileX, tileY, tileWidth, tileHeight,
1971 format, type, &tileUnpack, pixels);
1972 }
1973 }
1974 }
1975
1976
1977 /**
1978 * One-time init for drawing stencil pixels.
1979 */
1980 static void
1981 init_draw_stencil_pixels(struct gl_context *ctx)
1982 {
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.
1988 *
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).
1992 * else
1993 * result is zero.
1994 * The program parameter contains three values:
1995 * parm.x = 255 / (1 << bit)
1996 * parm.y = 0.5
1997 * parm.z = 0.0
1998 */
1999 static const char *program =
2000 "!!ARBfp1.0\n"
2001 "PARAM parm = program.local[0]; \n"
2002 "TEMP t; \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"
2006 "# t = (int) t \n"
2007 "FRC t.y, t.x; \n"
2008 "SUB t.x, t.x, t.y; \n"
2009 "# t = t * 0.5 \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"
2015 "KIL -t.x; \n"
2016 "# for debug only \n"
2017 "#MOV result.color, t.x; \n"
2018 "END \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;
2023
2024 assert(drawpix->StencilFP == 0);
2025
2026 /* replace %s with "RECT" or "2D" */
2027 assert(strlen(program) + 4 < sizeof(program2));
2028 if (tex->Target == GL_TEXTURE_RECTANGLE)
2029 texTarget = "RECT";
2030 else
2031 texTarget = "2D";
2032 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
2033
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);
2038 }
2039
2040
2041 /**
2042 * One-time init for drawing depth pixels.
2043 */
2044 static void
2045 init_draw_depth_pixels(struct gl_context *ctx)
2046 {
2047 static const char *program =
2048 "!!ARBfp1.0\n"
2049 "PARAM color = program.local[0]; \n"
2050 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2051 "MOV result.color, color; \n"
2052 "END \n";
2053 char program2[200];
2054 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
2055 struct temp_texture *tex = _mesa_meta_get_temp_texture(ctx);
2056 const char *texTarget;
2057
2058 assert(drawpix->DepthFP == 0);
2059
2060 /* replace %s with "RECT" or "2D" */
2061 assert(strlen(program) + 4 < sizeof(program2));
2062 if (tex->Target == GL_TEXTURE_RECTANGLE)
2063 texTarget = "RECT";
2064 else
2065 texTarget = "2D";
2066 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
2067
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);
2072 }
2073
2074
2075 /**
2076 * Meta implementation of ctx->Driver.DrawPixels() in terms
2077 * of texture mapping and polygon rendering.
2078 */
2079 void
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)
2085 {
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;
2094
2095 /*
2096 * Determine if we can do the glDrawPixels with texture mapping.
2097 */
2098 fallback = GL_FALSE;
2099 if (ctx->Fog.Enabled) {
2100 fallback = GL_TRUE;
2101 }
2102
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).
2107 */
2108 if (/*format == GL_LUMINANCE ||*/ format == GL_LUMINANCE_ALPHA)
2109 texIntFormat = format;
2110 else
2111 texIntFormat = GL_RGBA;
2112
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
2116 * point.
2117 */
2118 if (ctx->Color.ClampFragmentColor != GL_TRUE &&
2119 ctx->Extensions.ARB_texture_float)
2120 texIntFormat = GL_RGBA32F;
2121 }
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
2129 * in [0,1].
2130 */
2131 texIntFormat = GL_ALPHA;
2132 metaExtraSave = (MESA_META_COLOR_MASK |
2133 MESA_META_DEPTH_TEST |
2134 MESA_META_PIXEL_TRANSFER |
2135 MESA_META_SHADER |
2136 MESA_META_STENCIL_TEST);
2137 }
2138 else {
2139 fallback = GL_TRUE;
2140 }
2141 }
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);
2147 }
2148 else {
2149 fallback = GL_TRUE;
2150 }
2151 }
2152 else {
2153 fallback = GL_TRUE;
2154 }
2155
2156 if (fallback) {
2157 _swrast_DrawPixels(ctx, x, y, width, height,
2158 format, type, unpack, pixels);
2159 return;
2160 }
2161
2162 /*
2163 * Check image size against max texture size, draw as tiles if needed.
2164 */
2165 if (width > tex->MaxSize || height > tex->MaxSize) {
2166 tiled_draw_pixels(ctx, tex->MaxSize, x, y, width, height,
2167 format, type, unpack, pixels);
2168 return;
2169 }
2170
2171 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2172 * but a there's a few things we need to override:
2173 */
2174 _mesa_meta_begin(ctx, (MESA_META_RASTERIZATION |
2175 MESA_META_SHADER |
2176 MESA_META_TEXTURE |
2177 MESA_META_TRANSFORM |
2178 MESA_META_CLIP |
2179 MESA_META_VERTEX |
2180 MESA_META_VIEWPORT |
2181 metaExtraSave));
2182
2183 newTex = _mesa_meta_alloc_texture(tex, width, height, texIntFormat);
2184
2185 _mesa_meta_setup_vertex_objects(ctx, &drawpix->VAO, &drawpix->buf_obj, false,
2186 3, 2, 0);
2187
2188 /* Silence valgrind warnings about reading uninitialized stack. */
2189 memset(verts, 0, sizeof(verts));
2190
2191 /* vertex positions, texcoords (after texture allocation!) */
2192 {
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]);
2198
2199 verts[0].x = x0;
2200 verts[0].y = y0;
2201 verts[0].z = z;
2202 verts[0].tex[0] = 0.0F;
2203 verts[0].tex[1] = 0.0F;
2204 verts[1].x = x1;
2205 verts[1].y = y0;
2206 verts[1].z = z;
2207 verts[1].tex[0] = tex->Sright;
2208 verts[1].tex[1] = 0.0F;
2209 verts[2].x = x1;
2210 verts[2].y = y1;
2211 verts[2].z = z;
2212 verts[2].tex[0] = tex->Sright;
2213 verts[2].tex[1] = tex->Ttop;
2214 verts[3].x = x0;
2215 verts[3].y = y1;
2216 verts[3].z = z;
2217 verts[3].tex[0] = 0.0F;
2218 verts[3].tex[1] = tex->Ttop;
2219 }
2220
2221 /* upload new vertex data */
2222 _mesa_buffer_data(ctx, drawpix->buf_obj, GL_NONE, sizeof(verts), verts,
2223 GL_DYNAMIC_DRAW, __func__);
2224
2225 /* set given unpack params */
2226 ctx->Unpack = *unpack;
2227
2228 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
2229
2230 if (_mesa_is_stencil_format(format)) {
2231 /* Drawing stencil */
2232 GLint bit;
2233
2234 if (!drawpix->StencilFP)
2235 init_draw_stencil_pixels(ctx);
2236
2237 _mesa_meta_setup_drawpix_texture(ctx, tex, newTex, width, height,
2238 GL_ALPHA, type, pixels);
2239
2240 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
2241
2242 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
2243
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);
2248
2249 /* set stencil bits to 1 where needed */
2250 _mesa_StencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
2251
2252 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
2253 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
2254
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);
2260
2261 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0,
2262 255.0f / mask, 0.5f, 0.0f, 0.0f);
2263
2264 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2265 }
2266 }
2267 }
2268 else if (_mesa_is_depth_format(format)) {
2269 /* Drawing depth */
2270 if (!drawpix->DepthFP)
2271 init_draw_depth_pixels(ctx);
2272
2273 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
2274 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
2275
2276 /* polygon color = current raster color */
2277 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0,
2278 ctx->Current.RasterColor);
2279
2280 _mesa_meta_setup_drawpix_texture(ctx, tex, newTex, width, height,
2281 format, type, pixels);
2282
2283 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2284 }
2285 else {
2286 /* Drawing RGBA */
2287 _mesa_meta_setup_drawpix_texture(ctx, tex, newTex, width, height,
2288 format, type, pixels);
2289 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2290 }
2291
2292 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
2293
2294 /* restore unpack params */
2295 ctx->Unpack = unpackSave;
2296
2297 _mesa_meta_end(ctx);
2298 }
2299
2300 static GLboolean
2301 alpha_test_raster_color(struct gl_context *ctx)
2302 {
2303 GLfloat alpha = ctx->Current.RasterColor[ACOMP];
2304 GLfloat ref = ctx->Color.AlphaRef;
2305
2306 switch (ctx->Color.AlphaFunc) {
2307 case GL_NEVER:
2308 return GL_FALSE;
2309 case GL_LESS:
2310 return alpha < ref;
2311 case GL_EQUAL:
2312 return alpha == ref;
2313 case GL_LEQUAL:
2314 return alpha <= ref;
2315 case GL_GREATER:
2316 return alpha > ref;
2317 case GL_NOTEQUAL:
2318 return alpha != ref;
2319 case GL_GEQUAL:
2320 return alpha >= ref;
2321 case GL_ALWAYS:
2322 return GL_TRUE;
2323 default:
2324 assert(0);
2325 return GL_FALSE;
2326 }
2327 }
2328
2329 /**
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.
2333 */
2334 void
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)
2339 {
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;
2344 GLubyte fg, bg;
2345 struct vertex verts[4];
2346 GLboolean newTex;
2347 GLubyte *bitmap8;
2348
2349 /*
2350 * Check if swrast fallback is needed.
2351 */
2352 if (ctx->_ImageTransferState ||
2353 _mesa_arb_fragment_program_enabled(ctx) ||
2354 ctx->Fog.Enabled ||
2355 ctx->Texture._MaxEnabledTexImageUnit != -1 ||
2356 width > tex->MaxSize ||
2357 height > tex->MaxSize) {
2358 _swrast_Bitmap(ctx, x, y, width, height, unpack, bitmap1);
2359 return;
2360 }
2361
2362 if (ctx->Color.AlphaEnabled && !alpha_test_raster_color(ctx))
2363 return;
2364
2365 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2366 * but a there's a few things we need to override:
2367 */
2368 _mesa_meta_begin(ctx, (MESA_META_ALPHA_TEST |
2369 MESA_META_PIXEL_STORE |
2370 MESA_META_RASTERIZATION |
2371 MESA_META_SHADER |
2372 MESA_META_TEXTURE |
2373 MESA_META_TRANSFORM |
2374 MESA_META_CLIP |
2375 MESA_META_VERTEX |
2376 MESA_META_VIEWPORT));
2377
2378 _mesa_meta_setup_vertex_objects(ctx, &bitmap->VAO, &bitmap->buf_obj, false,
2379 3, 2, 4);
2380
2381 newTex = _mesa_meta_alloc_texture(tex, width, height, texIntFormat);
2382
2383 /* Silence valgrind warnings about reading uninitialized stack. */
2384 memset(verts, 0, sizeof(verts));
2385
2386 /* vertex positions, texcoords, colors (after texture allocation!) */
2387 {
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]);
2393 GLuint i;
2394
2395 verts[0].x = x0;
2396 verts[0].y = y0;
2397 verts[0].z = z;
2398 verts[0].tex[0] = 0.0F;
2399 verts[0].tex[1] = 0.0F;
2400 verts[1].x = x1;
2401 verts[1].y = y0;
2402 verts[1].z = z;
2403 verts[1].tex[0] = tex->Sright;
2404 verts[1].tex[1] = 0.0F;
2405 verts[2].x = x1;
2406 verts[2].y = y1;
2407 verts[2].z = z;
2408 verts[2].tex[0] = tex->Sright;
2409 verts[2].tex[1] = tex->Ttop;
2410 verts[3].x = x0;
2411 verts[3].y = y1;
2412 verts[3].z = z;
2413 verts[3].tex[0] = 0.0F;
2414 verts[3].tex[1] = tex->Ttop;
2415
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];
2421 }
2422
2423 /* upload new vertex data */
2424 _mesa_buffer_sub_data(ctx, bitmap->buf_obj, 0, sizeof(verts), verts);
2425 }
2426
2427 /* choose different foreground/background alpha values */
2428 CLAMPED_FLOAT_TO_UBYTE(fg, ctx->Current.RasterColor[ACOMP]);
2429 bg = (fg > 127 ? 0 : 255);
2430
2431 bitmap1 = _mesa_map_pbo_source(ctx, &unpackSave, bitmap1);
2432 if (!bitmap1) {
2433 _mesa_meta_end(ctx);
2434 return;
2435 }
2436
2437 bitmap8 = malloc(width * height);
2438 if (bitmap8) {
2439 memset(bitmap8, bg, width * height);
2440 _mesa_expand_bitmap(width, height, &unpackSave, bitmap1,
2441 bitmap8, width, fg);
2442
2443 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
2444
2445 _mesa_set_enable(ctx, GL_ALPHA_TEST, GL_TRUE);
2446 _mesa_AlphaFunc(GL_NOTEQUAL, UBYTE_TO_FLOAT(bg));
2447
2448 _mesa_meta_setup_drawpix_texture(ctx, tex, newTex, width, height,
2449 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap8);
2450
2451 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2452
2453 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
2454
2455 free(bitmap8);
2456 }
2457
2458 _mesa_unmap_pbo_source(ctx, &unpackSave);
2459
2460 _mesa_meta_end(ctx);
2461 }
2462
2463 /**
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.
2467 *
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
2478 */
2479 void
2480 _mesa_meta_setup_texture_coords(GLenum faceTarget,
2481 GLint slice,
2482 GLint xoffset,
2483 GLint yoffset,
2484 GLint width,
2485 GLint height,
2486 GLint total_width,
2487 GLint total_height,
2488 GLint total_depth,
2489 GLfloat coords0[4],
2490 GLfloat coords1[4],
2491 GLfloat coords2[4],
2492 GLfloat coords3[4])
2493 {
2494 float st[4][2];
2495 GLuint i;
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;
2500 GLfloat r;
2501
2502 /* setup the reference texcoords */
2503 st[0][0] = s0;
2504 st[0][1] = t0;
2505 st[1][0] = s1;
2506 st[1][1] = t0;
2507 st[2][0] = s1;
2508 st[2][1] = t1;
2509 st[3][0] = s0;
2510 st[3][1] = t1;
2511
2512 if (faceTarget == GL_TEXTURE_CUBE_MAP_ARRAY)
2513 faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + slice % 6;
2514
2515 /* Currently all texture targets want the W component to be 1.0.
2516 */
2517 coords0[3] = 1.0F;
2518 coords1[3] = 1.0F;
2519 coords2[3] = 1.0F;
2520 coords3[3] = 1.0F;
2521
2522 switch (faceTarget) {
2523 case GL_TEXTURE_1D:
2524 case GL_TEXTURE_2D:
2525 case GL_TEXTURE_3D:
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;
2531 }
2532 else if (faceTarget == GL_TEXTURE_2D_ARRAY)
2533 r = (float) slice;
2534 else
2535 r = 0.0F;
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];
2541 coords1[2] = r;
2542 coords2[0] = st[2][0];
2543 coords2[1] = st[2][1];
2544 coords2[2] = r;
2545 coords3[0] = st[3][0];
2546 coords3[1] = st[3][1];
2547 coords3[2] = r;
2548 break;
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;
2555 coords1[2] = 0.0F;
2556 coords2[0] = (float) (xoffset + width);
2557 coords2[1] = (float) (yoffset + height);
2558 coords2[2] = 0.0F;
2559 coords3[0] = (float) xoffset;
2560 coords3[1] = (float) (yoffset + height);
2561 coords3[2] = 0.0F;
2562 break;
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;
2569 coords1[2] = 0.0F;
2570 coords2[0] = st[2][0];
2571 coords2[1] = (float) slice;
2572 coords2[2] = 0.0F;
2573 coords3[0] = st[3][0];
2574 coords3[1] = (float) slice;
2575 coords3[2] = 0.0F;
2576 break;
2577
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...
2589 */
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;
2593 GLfloat *coord;
2594
2595 switch (i) {
2596 case 0:
2597 coord = coords0;
2598 break;
2599 case 1:
2600 coord = coords1;
2601 break;
2602 case 2:
2603 coord = coords2;
2604 break;
2605 case 3:
2606 coord = coords3;
2607 break;
2608 default:
2609 unreachable("not reached");
2610 }
2611
2612 coord[3] = (float) (slice / 6);
2613
2614 switch (faceTarget) {
2615 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2616 coord[0] = 1.0f;
2617 coord[1] = -tc;
2618 coord[2] = -sc;
2619 break;
2620 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2621 coord[0] = -1.0f;
2622 coord[1] = -tc;
2623 coord[2] = sc;
2624 break;
2625 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2626 coord[0] = sc;
2627 coord[1] = 1.0f;
2628 coord[2] = tc;
2629 break;
2630 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2631 coord[0] = sc;
2632 coord[1] = -1.0f;
2633 coord[2] = -tc;
2634 break;
2635 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2636 coord[0] = sc;
2637 coord[1] = -tc;
2638 coord[2] = 1.0f;
2639 break;
2640 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2641 coord[0] = -sc;
2642 coord[1] = -tc;
2643 coord[2] = -1.0f;
2644 break;
2645 default:
2646 assert(0);
2647 }
2648 }
2649 break;
2650 default:
2651 assert(!"unexpected target in _mesa_meta_setup_texture_coords()");
2652 }
2653 }
2654
2655 static struct blit_shader *
2656 choose_blit_shader(GLenum target, struct blit_shader_table *table)
2657 {
2658 switch(target) {
2659 case GL_TEXTURE_1D:
2660 table->sampler_1d.type = "sampler1D";
2661 table->sampler_1d.func = "texture1D";
2662 table->sampler_1d.texcoords = "texCoords.x";
2663 return &table->sampler_1d;
2664 case GL_TEXTURE_2D:
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;
2674 case GL_TEXTURE_3D:
2675 /* Code for mipmap generation with 3D textures is not used yet.
2676 * It's a sw fallback.
2677 */
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;
2702 default:
2703 _mesa_problem(NULL, "Unexpected texture target 0x%x in"
2704 " setup_texture_sampler()\n", target);
2705 return NULL;
2706 }
2707 }
2708
2709 void
2710 _mesa_meta_blit_shader_table_cleanup(struct gl_context *ctx,
2711 struct blit_shader_table *table)
2712 {
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);
2721 }
2722
2723 /**
2724 * Determine the GL data type to use for the temporary image read with
2725 * ReadPixels() and passed to Tex[Sub]Image().
2726 */
2727 static GLenum
2728 get_temp_image_type(struct gl_context *ctx, mesa_format format)
2729 {
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);
2733
2734 switch (baseFormat) {
2735 case GL_RGBA:
2736 case GL_RGB:
2737 case GL_RG:
2738 case GL_RED:
2739 case GL_ALPHA:
2740 case GL_LUMINANCE:
2741 case GL_LUMINANCE_ALPHA:
2742 case GL_INTENSITY:
2743 if (datatype == GL_INT || datatype == GL_UNSIGNED_INT) {
2744 return datatype;
2745 } else if (format_red_bits <= 8) {
2746 return GL_UNSIGNED_BYTE;
2747 } else if (format_red_bits <= 16) {
2748 return GL_UNSIGNED_SHORT;
2749 }
2750 return GL_FLOAT;
2751 case GL_DEPTH_COMPONENT:
2752 if (datatype == GL_FLOAT)
2753 return GL_FLOAT;
2754 else
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;
2759 else
2760 return GL_UNSIGNED_INT_24_8;
2761 default:
2762 _mesa_problem(ctx, "Unexpected format %d in get_temp_image_type()",
2763 baseFormat);
2764 return 0;
2765 }
2766 }
2767
2768 /**
2769 * Attempts to wrap the destination texture in an FBO and use
2770 * glBlitFramebuffer() to implement glCopyTexSubImage().
2771 */
2772 static bool
2773 copytexsubimage_using_blit_framebuffer(struct gl_context *ctx,
2774 struct gl_texture_image *texImage,
2775 GLint xoffset,
2776 GLint yoffset,
2777 GLint zoffset,
2778 struct gl_renderbuffer *rb,
2779 GLint x, GLint y,
2780 GLsizei width, GLsizei height)
2781 {
2782 struct gl_framebuffer *drawFb;
2783 bool success = false;
2784 GLbitfield mask;
2785 GLenum status;
2786
2787 if (!ctx->Extensions.ARB_framebuffer_object)
2788 return false;
2789
2790 drawFb = ctx->Driver.NewFramebuffer(ctx, 0xDEADBEEF);
2791 if (drawFb == NULL)
2792 return false;
2793
2794 _mesa_meta_begin(ctx, MESA_META_ALL & ~MESA_META_DRAW_BUFFERS);
2795 _mesa_bind_framebuffers(ctx, drawFb, ctx->ReadBuffer);
2796
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,
2801 texImage, zoffset);
2802 mask = GL_DEPTH_BUFFER_BIT;
2803
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,
2808 texImage, zoffset);
2809 mask |= GL_STENCIL_BUFFER_BIT;
2810 }
2811 _mesa_DrawBuffer(GL_NONE);
2812 } else {
2813 _mesa_meta_framebuffer_texture_image(ctx, ctx->DrawBuffer,
2814 GL_COLOR_ATTACHMENT0,
2815 texImage, zoffset);
2816 mask = GL_COLOR_BUFFER_BIT;
2817 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0);
2818 }
2819
2820 status = _mesa_check_framebuffer_status(ctx, ctx->DrawBuffer);
2821 if (status != GL_FRAMEBUFFER_COMPLETE)
2822 goto out;
2823
2824 ctx->Meta->Blit.no_ctsi_fallback = true;
2825
2826 /* Since we've bound a new draw framebuffer, we need to update
2827 * its derived state -- _Xmin, etc -- for BlitFramebuffer's clipping to
2828 * be correct.
2829 */
2830 _mesa_update_state(ctx);
2831
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
2834 * changes.
2835 */
2836 mask = _mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
2837 x, y,
2838 x + width, y + height,
2839 xoffset, yoffset,
2840 xoffset + width, yoffset + height,
2841 mask, GL_NEAREST);
2842 ctx->Meta->Blit.no_ctsi_fallback = false;
2843 success = mask == 0x0;
2844
2845 out:
2846 _mesa_reference_framebuffer(&drawFb, NULL);
2847 _mesa_meta_end(ctx);
2848 return success;
2849 }
2850
2851 /**
2852 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2853 * Have to be careful with locking and meta state for pixel transfer.
2854 */
2855 void
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,
2860 GLint x, GLint y,
2861 GLsizei width, GLsizei height)
2862 {
2863 GLenum format, type;
2864 GLint bpp;
2865 void *buf;
2866
2867 if (copytexsubimage_using_blit_framebuffer(ctx,
2868 texImage,
2869 xoffset, yoffset, zoffset,
2870 rb,
2871 x, y,
2872 width, height)) {
2873 return;
2874 }
2875
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).
2884 */
2885 format = GL_RGBA;
2886 }
2887
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);
2891 }
2892 bpp = _mesa_bytes_per_pixel(format, type);
2893 if (bpp <= 0) {
2894 _mesa_problem(ctx, "Bad bpp in _mesa_meta_CopyTexSubImage()");
2895 return;
2896 }
2897
2898 /*
2899 * Alloc image buffer (XXX could use a PBO)
2900 */
2901 buf = malloc(width * height * bpp);
2902 if (!buf) {
2903 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage%uD", dims);
2904 return;
2905 }
2906
2907 /*
2908 * Read image from framebuffer (disable pixel transfer ops)
2909 */
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);
2914
2915 _mesa_update_state(ctx); /* to update pixel transfer state */
2916
2917 /*
2918 * Store texture data (with pixel transfer ops)
2919 */
2920 _mesa_meta_begin(ctx, MESA_META_PIXEL_STORE);
2921
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);
2927 } else {
2928 ctx->Driver.TexSubImage(ctx, dims, texImage,
2929 xoffset, yoffset, zoffset, width, height, 1,
2930 format, type, buf, &ctx->Unpack);
2931 }
2932
2933 _mesa_meta_end(ctx);
2934
2935 free(buf);
2936 }
2937
2938 static void
2939 meta_decompress_fbo_cleanup(struct decompress_fbo_state *decompress_fbo)
2940 {
2941 if (decompress_fbo->fb != NULL) {
2942 _mesa_reference_framebuffer(&decompress_fbo->fb, NULL);
2943 _mesa_reference_renderbuffer(&decompress_fbo->rb, NULL);
2944 }
2945
2946 memset(decompress_fbo, 0, sizeof(*decompress_fbo));
2947 }
2948
2949 static void
2950 meta_decompress_cleanup(struct gl_context *ctx,
2951 struct decompress_state *decompress)
2952 {
2953 meta_decompress_fbo_cleanup(&decompress->byteFBO);
2954 meta_decompress_fbo_cleanup(&decompress->floatFBO);
2955
2956 if (decompress->VAO != 0) {
2957 _mesa_DeleteVertexArrays(1, &decompress->VAO);
2958 _mesa_reference_buffer_object(ctx, &decompress->buf_obj, NULL);
2959 }
2960
2961 _mesa_reference_sampler_object(ctx, &decompress->samp_obj, NULL);
2962
2963 memset(decompress, 0, sizeof(*decompress));
2964 }
2965
2966 /**
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)
2974 */
2975 static bool
2976 decompress_texture_image(struct gl_context *ctx,
2977 struct gl_texture_image *texImage,
2978 GLuint slice,
2979 GLint xoffset, GLint yoffset,
2980 GLsizei width, GLsizei height,
2981 GLenum destFormat, GLenum destType,
2982 GLvoid *dest)
2983 {
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;
2988 GLenum rbFormat;
2989 GLenum faceTarget;
2990 struct vertex verts[4];
2991 struct gl_sampler_object *samp_obj_save = NULL;
2992 GLenum status;
2993 const bool use_glsl_version = ctx->Extensions.ARB_vertex_shader &&
2994 ctx->Extensions.ARB_fragment_shader;
2995
2996 switch (_mesa_get_format_datatype(texImage->TexFormat)) {
2997 case GL_FLOAT:
2998 decompress_fbo = &decompress->floatFBO;
2999 rbFormat = GL_RGBA32F;
3000 break;
3001 case GL_UNSIGNED_NORMALIZED:
3002 decompress_fbo = &decompress->byteFBO;
3003 rbFormat = GL_RGBA;
3004 break;
3005 default:
3006 return false;
3007 }
3008
3009 if (slice > 0) {
3010 assert(target == GL_TEXTURE_3D ||
3011 target == GL_TEXTURE_2D_ARRAY ||
3012 target == GL_TEXTURE_CUBE_MAP_ARRAY);
3013 }
3014
3015 switch (target) {
3016 case GL_TEXTURE_1D:
3017 case GL_TEXTURE_1D_ARRAY:
3018 assert(!"No compressed 1D textures.");
3019 return false;
3020
3021 case GL_TEXTURE_CUBE_MAP_ARRAY:
3022 faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + (slice % 6);
3023 break;
3024
3025 case GL_TEXTURE_CUBE_MAP:
3026 faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + texImage->Face;
3027 break;
3028
3029 default:
3030 faceTarget = target;
3031 break;
3032 }
3033
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);
3037
3038 _mesa_reference_sampler_object(ctx, &samp_obj_save,
3039 ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler);
3040
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);
3046 return false;
3047 }
3048
3049 decompress_fbo->fb = ctx->Driver.NewFramebuffer(ctx, 0xDEADBEEF);
3050 if (decompress_fbo->fb == NULL) {
3051 _mesa_meta_end(ctx);
3052 return false;
3053 }
3054
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);
3058 }
3059 else {
3060 _mesa_bind_framebuffers(ctx, decompress_fbo->fb, decompress_fbo->fb);
3061 }
3062
3063 /* alloc dest surface */
3064 if (width > decompress_fbo->Width || height > decompress_fbo->Height) {
3065 _mesa_renderbuffer_storage(ctx, decompress_fbo->rb, rbFormat,
3066 width, height, 0);
3067
3068 /* Do the full completeness check to recompute
3069 * ctx->DrawBuffer->Width/Height.
3070 */
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
3076 * too */
3077 _mesa_meta_end(ctx);
3078 return false;
3079 }
3080 decompress_fbo->Width = width;
3081 decompress_fbo->Height = height;
3082 }
3083
3084 if (use_glsl_version) {
3085 _mesa_meta_setup_vertex_objects(ctx, &decompress->VAO,
3086 &decompress->buf_obj, true,
3087 2, 4, 0);
3088
3089 _mesa_meta_setup_blit_shader(ctx, target, false, &decompress->shaders);
3090 } else {
3091 _mesa_meta_setup_ff_tnl_for_blit(ctx, &decompress->VAO,
3092 &decompress->buf_obj, 3);
3093 }
3094
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);
3099
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
3103 * clean up.
3104 *
3105 * Returning true prevents other GetTexImage methods from attempting
3106 * anything since they will likely fail too.
3107 */
3108 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
3109 return true;
3110 }
3111
3112 /* nearest filtering */
3113 _mesa_set_sampler_filters(ctx, decompress->samp_obj, GL_NEAREST, GL_NEAREST);
3114
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);
3117 }
3118
3119 _mesa_bind_sampler(ctx, ctx->Texture.CurrentUnit, decompress->samp_obj);
3120
3121 /* Silence valgrind warnings about reading uninitialized stack. */
3122 memset(verts, 0, sizeof(verts));
3123
3124 _mesa_meta_setup_texture_coords(faceTarget, slice,
3125 xoffset, yoffset, width, height,
3126 texImage->Width, texImage->Height,
3127 texImage->Depth,
3128 verts[0].tex,
3129 verts[1].tex,
3130 verts[2].tex,
3131 verts[3].tex);
3132
3133 /* setup vertex positions */
3134 verts[0].x = -1.0F;
3135 verts[0].y = -1.0F;
3136 verts[1].x = 1.0F;
3137 verts[1].y = -1.0F;
3138 verts[2].x = 1.0F;
3139 verts[2].y = 1.0F;
3140 verts[3].x = -1.0F;
3141 verts[3].y = 1.0F;
3142
3143 _mesa_set_viewport(ctx, 0, 0, 0, width, height);
3144
3145 /* upload new vertex data */
3146 _mesa_buffer_sub_data(ctx, decompress->buf_obj, 0, sizeof(verts), verts);
3147
3148 /* setup texture state */
3149 _mesa_BindTexture(target, texObj->Name);
3150
3151 if (!use_glsl_version)
3152 _mesa_set_enable(ctx, target, GL_TRUE);
3153
3154 {
3155 /* save texture object state */
3156 const GLint baseLevelSave = texObj->BaseLevel;
3157 const GLint maxLevelSave = texObj->MaxLevel;
3158
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);
3165 }
3166
3167 /* render quad w/ texture into renderbuffer */
3168 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
3169
3170 /* Restore texture object state, the texture binding will
3171 * be restored by _mesa_meta_end().
3172 */
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);
3178 }
3179
3180 }
3181
3182 /* read pixels from renderbuffer */
3183 {
3184 GLenum baseTexFormat = texImage->_BaseFormat;
3185 GLenum destBaseFormat = _mesa_unpack_format_to_base_format(destFormat);
3186
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
3192 * red/alpha.
3193 */
3194 if (_mesa_need_luminance_to_rgb_conversion(baseTexFormat,
3195 destBaseFormat) ||
3196 /* If we're reading back an RGB(A) texture (using glGetTexImage) as
3197 * luminance then we need to return L=tex(R).
3198 */
3199 _mesa_need_rgb_to_luminance_conversion(baseTexFormat,
3200 destBaseFormat)) {
3201 /* Green and blue must be zero */
3202 _mesa_PixelTransferf(GL_GREEN_SCALE, 0.0f);
3203 _mesa_PixelTransferf(GL_BLUE_SCALE, 0.0f);
3204 }
3205
3206 _mesa_ReadPixels(0, 0, width, height, destFormat, destType, dest);
3207 }
3208
3209 /* disable texture unit */
3210 if (!use_glsl_version)
3211 _mesa_set_enable(ctx, target, GL_FALSE);
3212
3213 _mesa_bind_sampler(ctx, ctx->Texture.CurrentUnit, samp_obj_save);
3214 _mesa_reference_sampler_object(ctx, &samp_obj_save, NULL);
3215
3216 _mesa_meta_end(ctx);
3217
3218 return true;
3219 }
3220
3221
3222 /**
3223 * This is just a wrapper around _mesa_get_tex_image() and
3224 * decompress_texture_image(). Meta functions should not be directly called
3225 * from core Mesa.
3226 */
3227 void
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)
3233 {
3234 if (_mesa_is_format_compressed(texImage->TexFormat)) {
3235 GLuint slice;
3236 bool result = true;
3237
3238 for (slice = 0; slice < depth; slice++) {
3239 void *dst;
3240 /* Section 8.11.4 (Texture Image Queries) of the GL 4.5 spec says:
3241 *
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."
3248 */
3249 switch (texImage->TexObject->Target) {
3250 case GL_TEXTURE_3D:
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).
3258 */
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);
3264 break;
3265 }
3266 default:
3267 dst = pixels;
3268 break;
3269 }
3270 result = decompress_texture_image(ctx, texImage, slice,
3271 xoffset, yoffset, width, height,
3272 format, type, dst);
3273 if (!result)
3274 break;
3275 }
3276
3277 if (result)
3278 return;
3279 }
3280
3281 _mesa_GetTexSubImage_sw(ctx, xoffset, yoffset, zoffset,
3282 width, height, depth, format, type, pixels, texImage);
3283 }
3284
3285
3286 /**
3287 * Meta implementation of ctx->Driver.DrawTex() in terms
3288 * of polygon rendering.
3289 */
3290 void
3291 _mesa_meta_DrawTex(struct gl_context *ctx, GLfloat x, GLfloat y, GLfloat z,
3292 GLfloat width, GLfloat height)
3293 {
3294 struct drawtex_state *drawtex = &ctx->Meta->DrawTex;
3295 struct vertex {
3296 GLfloat x, y, z, st[MAX_TEXTURE_UNITS][2];
3297 };
3298 struct vertex verts[4];
3299 GLuint i;
3300
3301 _mesa_meta_begin(ctx, (MESA_META_RASTERIZATION |
3302 MESA_META_SHADER |
3303 MESA_META_TRANSFORM |
3304 MESA_META_VERTEX |
3305 MESA_META_VIEWPORT));
3306
3307 if (drawtex->VAO == 0) {
3308 /* one-time setup */
3309 struct gl_vertex_array_object *array_obj;
3310
3311 /* create vertex array object */
3312 _mesa_GenVertexArrays(1, &drawtex->VAO);
3313 _mesa_BindVertexArray(drawtex->VAO);
3314
3315 array_obj = _mesa_lookup_vao(ctx, drawtex->VAO);
3316 assert(array_obj != NULL);
3317
3318 /* create vertex array buffer */
3319 drawtex->buf_obj = ctx->Driver.NewBufferObject(ctx, 0xDEADBEEF);
3320 if (drawtex->buf_obj == NULL)
3321 return;
3322
3323 _mesa_buffer_data(ctx, drawtex->buf_obj, GL_NONE, sizeof(verts), verts,
3324 GL_DYNAMIC_DRAW, __func__);
3325
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,
3330 GL_FALSE, 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);
3335
3336
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,
3341 GL_FALSE, 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));
3346 }
3347 }
3348 else {
3349 _mesa_BindVertexArray(drawtex->VAO);
3350 }
3351
3352 /* vertex positions, texcoords */
3353 {
3354 const GLfloat x1 = x + width;
3355 const GLfloat y1 = y + height;
3356
3357 z = CLAMP(z, 0.0f, 1.0f);
3358 z = invert_z(z);
3359
3360 verts[0].x = x;
3361 verts[0].y = y;
3362 verts[0].z = z;
3363
3364 verts[1].x = x1;
3365 verts[1].y = y;
3366 verts[1].z = z;
3367
3368 verts[2].x = x1;
3369 verts[2].y = y1;
3370 verts[2].z = z;
3371
3372 verts[3].x = x;
3373 verts[3].y = y1;
3374 verts[3].z = z;
3375
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;
3380 GLuint tw, th;
3381
3382 if (!ctx->Texture.Unit[i]._Current) {
3383 GLuint j;
3384 for (j = 0; j < 4; j++) {
3385 verts[j].st[i][0] = 0.0f;
3386 verts[j].st[i][1] = 0.0f;
3387 }
3388 continue;
3389 }
3390
3391 texObj = ctx->Texture.Unit[i]._Current;
3392 texImage = texObj->Image[0][texObj->BaseLevel];
3393 tw = texImage->Width2;
3394 th = texImage->Height2;
3395
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;
3400
3401 verts[0].st[i][0] = s;
3402 verts[0].st[i][1] = t;
3403
3404 verts[1].st[i][0] = s1;
3405 verts[1].st[i][1] = t;
3406
3407 verts[2].st[i][0] = s1;
3408 verts[2].st[i][1] = t1;
3409
3410 verts[3].st[i][0] = s;
3411 verts[3].st[i][1] = t1;
3412 }
3413
3414 _mesa_buffer_sub_data(ctx, drawtex->buf_obj, 0, sizeof(verts), verts);
3415 }
3416
3417 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
3418
3419 _mesa_meta_end(ctx);
3420 }
3421
3422 static bool
3423 cleartexsubimage_color(struct gl_context *ctx,
3424 struct gl_texture_image *texImage,
3425 const GLvoid *clearValue,
3426 GLint zoffset)
3427 {
3428 mesa_format format;
3429 union gl_color_union colorValue;
3430 GLenum datatype;
3431 GLenum status;
3432
3433 _mesa_meta_framebuffer_texture_image(ctx, ctx->DrawBuffer,
3434 GL_COLOR_ATTACHMENT0,
3435 texImage, zoffset);
3436
3437 status = _mesa_check_framebuffer_status(ctx, ctx->DrawBuffer);
3438 if (status != GL_FRAMEBUFFER_COMPLETE)
3439 return false;
3440
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);
3444
3445 switch (datatype) {
3446 case GL_UNSIGNED_INT:
3447 case GL_INT:
3448 if (clearValue)
3449 _mesa_unpack_uint_rgba_row(format, 1, clearValue,
3450 (GLuint (*)[4]) colorValue.ui);
3451 else
3452 memset(&colorValue, 0, sizeof colorValue);
3453 if (datatype == GL_INT)
3454 _mesa_ClearBufferiv(GL_COLOR, 0, colorValue.i);
3455 else
3456 _mesa_ClearBufferuiv(GL_COLOR, 0, colorValue.ui);
3457 break;
3458 default:
3459 if (clearValue)
3460 _mesa_unpack_rgba_row(format, 1, clearValue,
3461 (GLfloat (*)[4]) colorValue.f);
3462 else
3463 memset(&colorValue, 0, sizeof colorValue);
3464 _mesa_ClearBufferfv(GL_COLOR, 0, colorValue.f);
3465 break;
3466 }
3467
3468 return true;
3469 }
3470
3471 static bool
3472 cleartexsubimage_depth_stencil(struct gl_context *ctx,
3473 struct gl_texture_image *texImage,
3474 const GLvoid *clearValue,
3475 GLint zoffset)
3476 {
3477 GLint stencilValue;
3478 GLfloat depthValue;
3479 GLenum status;
3480
3481 _mesa_meta_framebuffer_texture_image(ctx, ctx->DrawBuffer,
3482 GL_DEPTH_ATTACHMENT,
3483 texImage, zoffset);
3484
3485 if (texImage->_BaseFormat == GL_DEPTH_STENCIL)
3486 _mesa_meta_framebuffer_texture_image(ctx, ctx->DrawBuffer,
3487 GL_STENCIL_ATTACHMENT,
3488 texImage, zoffset);
3489
3490 status = _mesa_check_framebuffer_status(ctx, ctx->DrawBuffer);
3491 if (status != GL_FRAMEBUFFER_COMPLETE)
3492 return false;
3493
3494 if (clearValue) {
3495 GLuint depthStencilValue[2];
3496
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
3499 */
3500 _mesa_unpack_float_32_uint_24_8_depth_stencil_row(texImage->TexFormat,
3501 1, /* n */
3502 clearValue,
3503 depthStencilValue);
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
3506 */
3507 memcpy(&depthValue, depthStencilValue, sizeof depthValue);
3508 stencilValue = depthStencilValue[1] & 0xff;
3509 } else {
3510 depthValue = 0.0f;
3511 stencilValue = 0;
3512 }
3513
3514 if (texImage->_BaseFormat == GL_DEPTH_STENCIL)
3515 _mesa_ClearBufferfi(GL_DEPTH_STENCIL, 0, depthValue, stencilValue);
3516 else
3517 _mesa_ClearBufferfv(GL_DEPTH, 0, &depthValue);
3518
3519 return true;
3520 }
3521
3522 static bool
3523 cleartexsubimage_for_zoffset(struct gl_context *ctx,
3524 struct gl_texture_image *texImage,
3525 GLint zoffset,
3526 const GLvoid *clearValue)
3527 {
3528 struct gl_framebuffer *drawFb;
3529 bool success;
3530
3531 drawFb = ctx->Driver.NewFramebuffer(ctx, 0xDEADBEEF);
3532 if (drawFb == NULL)
3533 return false;
3534
3535 _mesa_bind_framebuffers(ctx, drawFb, ctx->ReadBuffer);
3536
3537 switch(texImage->_BaseFormat) {
3538 case GL_DEPTH_STENCIL:
3539 case GL_DEPTH_COMPONENT:
3540 success = cleartexsubimage_depth_stencil(ctx, texImage,
3541 clearValue, zoffset);
3542 break;
3543 default:
3544 success = cleartexsubimage_color(ctx, texImage, clearValue, zoffset);
3545 break;
3546 }
3547
3548 _mesa_reference_framebuffer(&drawFb, NULL);
3549
3550 return success;
3551 }
3552
3553 static bool
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)
3559 {
3560 bool success = true;
3561 GLint z;
3562
3563 _mesa_meta_begin(ctx,
3564 MESA_META_SCISSOR |
3565 MESA_META_COLOR_MASK |
3566 MESA_META_DITHER |
3567 MESA_META_FRAMEBUFFER_SRGB);
3568
3569 _mesa_ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
3570 _mesa_set_enable(ctx, GL_DITHER, GL_FALSE);
3571
3572 _mesa_set_enable(ctx, GL_SCISSOR_TEST, GL_TRUE);
3573 _mesa_Scissor(xoffset, yoffset, width, height);
3574
3575 for (z = zoffset; z < zoffset + depth; z++) {
3576 if (!cleartexsubimage_for_zoffset(ctx, texImage, z, clearValue)) {
3577 success = false;
3578 break;
3579 }
3580 }
3581
3582 _mesa_meta_end(ctx);
3583
3584 return success;
3585 }
3586
3587 extern void
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)
3593 {
3594 bool res;
3595
3596 res = cleartexsubimage_using_fbo(ctx, texImage,
3597 xoffset, yoffset, zoffset,
3598 width, height, depth,
3599 clearValue);
3600
3601 if (res)
3602 return;
3603
3604 _mesa_warning(ctx,
3605 "Falling back to mapping the texture in "
3606 "glClearTexSubImage\n");
3607
3608 _mesa_store_cleartexsubimage(ctx, texImage,
3609 xoffset, yoffset, zoffset,
3610 width, height, depth,
3611 clearValue);
3612 }