mesa: pass rb attachment to _mesa_framebuffer_texture()
[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 __func__);
1907 }
1908
1909 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
1910
1911 /* draw textured quad */
1912 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1913
1914 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
1915
1916 _mesa_meta_end(ctx);
1917 }
1918
1919 static void
1920 meta_drawpix_cleanup(struct gl_context *ctx, struct drawpix_state *drawpix)
1921 {
1922 if (drawpix->VAO != 0) {
1923 _mesa_DeleteVertexArrays(1, &drawpix->VAO);
1924 drawpix->VAO = 0;
1925
1926 _mesa_reference_buffer_object(ctx, &drawpix->buf_obj, NULL);
1927 }
1928
1929 if (drawpix->StencilFP != 0) {
1930 _mesa_DeleteProgramsARB(1, &drawpix->StencilFP);
1931 drawpix->StencilFP = 0;
1932 }
1933
1934 if (drawpix->DepthFP != 0) {
1935 _mesa_DeleteProgramsARB(1, &drawpix->DepthFP);
1936 drawpix->DepthFP = 0;
1937 }
1938 }
1939
1940 /**
1941 * When the glDrawPixels() image size is greater than the max rectangle
1942 * texture size we use this function to break the glDrawPixels() image
1943 * into tiles which fit into the max texture size.
1944 */
1945 static void
1946 tiled_draw_pixels(struct gl_context *ctx,
1947 GLint tileSize,
1948 GLint x, GLint y, GLsizei width, GLsizei height,
1949 GLenum format, GLenum type,
1950 const struct gl_pixelstore_attrib *unpack,
1951 const GLvoid *pixels)
1952 {
1953 struct gl_pixelstore_attrib tileUnpack = *unpack;
1954 GLint i, j;
1955
1956 if (tileUnpack.RowLength == 0)
1957 tileUnpack.RowLength = width;
1958
1959 for (i = 0; i < width; i += tileSize) {
1960 const GLint tileWidth = MIN2(tileSize, width - i);
1961 const GLint tileX = (GLint) (x + i * ctx->Pixel.ZoomX);
1962
1963 tileUnpack.SkipPixels = unpack->SkipPixels + i;
1964
1965 for (j = 0; j < height; j += tileSize) {
1966 const GLint tileHeight = MIN2(tileSize, height - j);
1967 const GLint tileY = (GLint) (y + j * ctx->Pixel.ZoomY);
1968
1969 tileUnpack.SkipRows = unpack->SkipRows + j;
1970
1971 _mesa_meta_DrawPixels(ctx, tileX, tileY, tileWidth, tileHeight,
1972 format, type, &tileUnpack, pixels);
1973 }
1974 }
1975 }
1976
1977
1978 /**
1979 * One-time init for drawing stencil pixels.
1980 */
1981 static void
1982 init_draw_stencil_pixels(struct gl_context *ctx)
1983 {
1984 /* This program is run eight times, once for each stencil bit.
1985 * The stencil values to draw are found in an 8-bit alpha texture.
1986 * We read the texture/stencil value and test if bit 'b' is set.
1987 * If the bit is not set, use KIL to kill the fragment.
1988 * Finally, we use the stencil test to update the stencil buffer.
1989 *
1990 * The basic algorithm for checking if a bit is set is:
1991 * if (is_odd(value / (1 << bit)))
1992 * result is one (or non-zero).
1993 * else
1994 * result is zero.
1995 * The program parameter contains three values:
1996 * parm.x = 255 / (1 << bit)
1997 * parm.y = 0.5
1998 * parm.z = 0.0
1999 */
2000 static const char *program =
2001 "!!ARBfp1.0\n"
2002 "PARAM parm = program.local[0]; \n"
2003 "TEMP t; \n"
2004 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
2005 "# t = t * 255 / bit \n"
2006 "MUL t.x, t.a, parm.x; \n"
2007 "# t = (int) t \n"
2008 "FRC t.y, t.x; \n"
2009 "SUB t.x, t.x, t.y; \n"
2010 "# t = t * 0.5 \n"
2011 "MUL t.x, t.x, parm.y; \n"
2012 "# t = fract(t.x) \n"
2013 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2014 "# t.x = (t.x == 0 ? 1 : 0) \n"
2015 "SGE t.x, -t.x, parm.z; \n"
2016 "KIL -t.x; \n"
2017 "# for debug only \n"
2018 "#MOV result.color, t.x; \n"
2019 "END \n";
2020 char program2[1000];
2021 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
2022 struct temp_texture *tex = _mesa_meta_get_temp_texture(ctx);
2023 const char *texTarget;
2024
2025 assert(drawpix->StencilFP == 0);
2026
2027 /* replace %s with "RECT" or "2D" */
2028 assert(strlen(program) + 4 < sizeof(program2));
2029 if (tex->Target == GL_TEXTURE_RECTANGLE)
2030 texTarget = "RECT";
2031 else
2032 texTarget = "2D";
2033 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
2034
2035 _mesa_GenProgramsARB(1, &drawpix->StencilFP);
2036 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
2037 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
2038 strlen(program2), (const GLubyte *) program2);
2039 }
2040
2041
2042 /**
2043 * One-time init for drawing depth pixels.
2044 */
2045 static void
2046 init_draw_depth_pixels(struct gl_context *ctx)
2047 {
2048 static const char *program =
2049 "!!ARBfp1.0\n"
2050 "PARAM color = program.local[0]; \n"
2051 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2052 "MOV result.color, color; \n"
2053 "END \n";
2054 char program2[200];
2055 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
2056 struct temp_texture *tex = _mesa_meta_get_temp_texture(ctx);
2057 const char *texTarget;
2058
2059 assert(drawpix->DepthFP == 0);
2060
2061 /* replace %s with "RECT" or "2D" */
2062 assert(strlen(program) + 4 < sizeof(program2));
2063 if (tex->Target == GL_TEXTURE_RECTANGLE)
2064 texTarget = "RECT";
2065 else
2066 texTarget = "2D";
2067 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
2068
2069 _mesa_GenProgramsARB(1, &drawpix->DepthFP);
2070 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
2071 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
2072 strlen(program2), (const GLubyte *) program2);
2073 }
2074
2075
2076 /**
2077 * Meta implementation of ctx->Driver.DrawPixels() in terms
2078 * of texture mapping and polygon rendering.
2079 */
2080 void
2081 _mesa_meta_DrawPixels(struct gl_context *ctx,
2082 GLint x, GLint y, GLsizei width, GLsizei height,
2083 GLenum format, GLenum type,
2084 const struct gl_pixelstore_attrib *unpack,
2085 const GLvoid *pixels)
2086 {
2087 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
2088 struct temp_texture *tex = _mesa_meta_get_temp_texture(ctx);
2089 const struct gl_pixelstore_attrib unpackSave = ctx->Unpack;
2090 const GLuint origStencilMask = ctx->Stencil.WriteMask[0];
2091 struct vertex verts[4];
2092 GLenum texIntFormat;
2093 GLboolean fallback, newTex;
2094 GLbitfield metaExtraSave = 0x0;
2095
2096 /*
2097 * Determine if we can do the glDrawPixels with texture mapping.
2098 */
2099 fallback = GL_FALSE;
2100 if (ctx->Fog.Enabled) {
2101 fallback = GL_TRUE;
2102 }
2103
2104 if (_mesa_is_color_format(format)) {
2105 /* use more compact format when possible */
2106 /* XXX disable special case for GL_LUMINANCE for now to work around
2107 * apparent i965 driver bug (see bug #23670).
2108 */
2109 if (/*format == GL_LUMINANCE ||*/ format == GL_LUMINANCE_ALPHA)
2110 texIntFormat = format;
2111 else
2112 texIntFormat = GL_RGBA;
2113
2114 /* If we're not supposed to clamp the resulting color, then just
2115 * promote our texture to fully float. We could do better by
2116 * just going for the matching set of channels, in floating
2117 * point.
2118 */
2119 if (ctx->Color.ClampFragmentColor != GL_TRUE &&
2120 ctx->Extensions.ARB_texture_float)
2121 texIntFormat = GL_RGBA32F;
2122 }
2123 else if (_mesa_is_stencil_format(format)) {
2124 if (ctx->Extensions.ARB_fragment_program &&
2125 ctx->Pixel.IndexShift == 0 &&
2126 ctx->Pixel.IndexOffset == 0 &&
2127 type == GL_UNSIGNED_BYTE) {
2128 /* We'll store stencil as alpha. This only works for GLubyte
2129 * image data because of how incoming values are mapped to alpha
2130 * in [0,1].
2131 */
2132 texIntFormat = GL_ALPHA;
2133 metaExtraSave = (MESA_META_COLOR_MASK |
2134 MESA_META_DEPTH_TEST |
2135 MESA_META_PIXEL_TRANSFER |
2136 MESA_META_SHADER |
2137 MESA_META_STENCIL_TEST);
2138 }
2139 else {
2140 fallback = GL_TRUE;
2141 }
2142 }
2143 else if (_mesa_is_depth_format(format)) {
2144 if (ctx->Extensions.ARB_depth_texture &&
2145 ctx->Extensions.ARB_fragment_program) {
2146 texIntFormat = GL_DEPTH_COMPONENT;
2147 metaExtraSave = (MESA_META_SHADER);
2148 }
2149 else {
2150 fallback = GL_TRUE;
2151 }
2152 }
2153 else {
2154 fallback = GL_TRUE;
2155 }
2156
2157 if (fallback) {
2158 _swrast_DrawPixels(ctx, x, y, width, height,
2159 format, type, unpack, pixels);
2160 return;
2161 }
2162
2163 /*
2164 * Check image size against max texture size, draw as tiles if needed.
2165 */
2166 if (width > tex->MaxSize || height > tex->MaxSize) {
2167 tiled_draw_pixels(ctx, tex->MaxSize, x, y, width, height,
2168 format, type, unpack, pixels);
2169 return;
2170 }
2171
2172 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2173 * but a there's a few things we need to override:
2174 */
2175 _mesa_meta_begin(ctx, (MESA_META_RASTERIZATION |
2176 MESA_META_SHADER |
2177 MESA_META_TEXTURE |
2178 MESA_META_TRANSFORM |
2179 MESA_META_CLIP |
2180 MESA_META_VERTEX |
2181 MESA_META_VIEWPORT |
2182 metaExtraSave));
2183
2184 newTex = _mesa_meta_alloc_texture(tex, width, height, texIntFormat);
2185
2186 _mesa_meta_setup_vertex_objects(ctx, &drawpix->VAO, &drawpix->buf_obj, false,
2187 3, 2, 0);
2188
2189 /* Silence valgrind warnings about reading uninitialized stack. */
2190 memset(verts, 0, sizeof(verts));
2191
2192 /* vertex positions, texcoords (after texture allocation!) */
2193 {
2194 const GLfloat x0 = (GLfloat) x;
2195 const GLfloat y0 = (GLfloat) y;
2196 const GLfloat x1 = x + width * ctx->Pixel.ZoomX;
2197 const GLfloat y1 = y + height * ctx->Pixel.ZoomY;
2198 const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
2199
2200 verts[0].x = x0;
2201 verts[0].y = y0;
2202 verts[0].z = z;
2203 verts[0].tex[0] = 0.0F;
2204 verts[0].tex[1] = 0.0F;
2205 verts[1].x = x1;
2206 verts[1].y = y0;
2207 verts[1].z = z;
2208 verts[1].tex[0] = tex->Sright;
2209 verts[1].tex[1] = 0.0F;
2210 verts[2].x = x1;
2211 verts[2].y = y1;
2212 verts[2].z = z;
2213 verts[2].tex[0] = tex->Sright;
2214 verts[2].tex[1] = tex->Ttop;
2215 verts[3].x = x0;
2216 verts[3].y = y1;
2217 verts[3].z = z;
2218 verts[3].tex[0] = 0.0F;
2219 verts[3].tex[1] = tex->Ttop;
2220 }
2221
2222 /* upload new vertex data */
2223 _mesa_buffer_data(ctx, drawpix->buf_obj, GL_NONE, sizeof(verts), verts,
2224 GL_DYNAMIC_DRAW, __func__);
2225
2226 /* set given unpack params */
2227 ctx->Unpack = *unpack;
2228
2229 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
2230
2231 if (_mesa_is_stencil_format(format)) {
2232 /* Drawing stencil */
2233 GLint bit;
2234
2235 if (!drawpix->StencilFP)
2236 init_draw_stencil_pixels(ctx);
2237
2238 _mesa_meta_setup_drawpix_texture(ctx, tex, newTex, width, height,
2239 GL_ALPHA, type, pixels);
2240
2241 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
2242
2243 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
2244
2245 /* set all stencil bits to 0 */
2246 _mesa_StencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
2247 _mesa_StencilFunc(GL_ALWAYS, 0, 255);
2248 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2249
2250 /* set stencil bits to 1 where needed */
2251 _mesa_StencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
2252
2253 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
2254 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
2255
2256 for (bit = 0; bit < ctx->DrawBuffer->Visual.stencilBits; bit++) {
2257 const GLuint mask = 1 << bit;
2258 if (mask & origStencilMask) {
2259 _mesa_StencilFunc(GL_ALWAYS, mask, mask);
2260 _mesa_StencilMask(mask);
2261
2262 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0,
2263 255.0f / mask, 0.5f, 0.0f, 0.0f);
2264
2265 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2266 }
2267 }
2268 }
2269 else if (_mesa_is_depth_format(format)) {
2270 /* Drawing depth */
2271 if (!drawpix->DepthFP)
2272 init_draw_depth_pixels(ctx);
2273
2274 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
2275 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
2276
2277 /* polygon color = current raster color */
2278 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0,
2279 ctx->Current.RasterColor);
2280
2281 _mesa_meta_setup_drawpix_texture(ctx, tex, newTex, width, height,
2282 format, type, pixels);
2283
2284 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2285 }
2286 else {
2287 /* Drawing RGBA */
2288 _mesa_meta_setup_drawpix_texture(ctx, tex, newTex, width, height,
2289 format, type, pixels);
2290 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2291 }
2292
2293 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
2294
2295 /* restore unpack params */
2296 ctx->Unpack = unpackSave;
2297
2298 _mesa_meta_end(ctx);
2299 }
2300
2301 static GLboolean
2302 alpha_test_raster_color(struct gl_context *ctx)
2303 {
2304 GLfloat alpha = ctx->Current.RasterColor[ACOMP];
2305 GLfloat ref = ctx->Color.AlphaRef;
2306
2307 switch (ctx->Color.AlphaFunc) {
2308 case GL_NEVER:
2309 return GL_FALSE;
2310 case GL_LESS:
2311 return alpha < ref;
2312 case GL_EQUAL:
2313 return alpha == ref;
2314 case GL_LEQUAL:
2315 return alpha <= ref;
2316 case GL_GREATER:
2317 return alpha > ref;
2318 case GL_NOTEQUAL:
2319 return alpha != ref;
2320 case GL_GEQUAL:
2321 return alpha >= ref;
2322 case GL_ALWAYS:
2323 return GL_TRUE;
2324 default:
2325 assert(0);
2326 return GL_FALSE;
2327 }
2328 }
2329
2330 /**
2331 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2332 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2333 * tracker would improve performance a lot.
2334 */
2335 void
2336 _mesa_meta_Bitmap(struct gl_context *ctx,
2337 GLint x, GLint y, GLsizei width, GLsizei height,
2338 const struct gl_pixelstore_attrib *unpack,
2339 const GLubyte *bitmap1)
2340 {
2341 struct bitmap_state *bitmap = &ctx->Meta->Bitmap;
2342 struct temp_texture *tex = get_bitmap_temp_texture(ctx);
2343 const GLenum texIntFormat = GL_ALPHA;
2344 const struct gl_pixelstore_attrib unpackSave = *unpack;
2345 GLubyte fg, bg;
2346 struct vertex verts[4];
2347 GLboolean newTex;
2348 GLubyte *bitmap8;
2349
2350 /*
2351 * Check if swrast fallback is needed.
2352 */
2353 if (ctx->_ImageTransferState ||
2354 ctx->FragmentProgram._Enabled ||
2355 ctx->Fog.Enabled ||
2356 ctx->Texture._MaxEnabledTexImageUnit != -1 ||
2357 width > tex->MaxSize ||
2358 height > tex->MaxSize) {
2359 _swrast_Bitmap(ctx, x, y, width, height, unpack, bitmap1);
2360 return;
2361 }
2362
2363 if (ctx->Color.AlphaEnabled && !alpha_test_raster_color(ctx))
2364 return;
2365
2366 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2367 * but a there's a few things we need to override:
2368 */
2369 _mesa_meta_begin(ctx, (MESA_META_ALPHA_TEST |
2370 MESA_META_PIXEL_STORE |
2371 MESA_META_RASTERIZATION |
2372 MESA_META_SHADER |
2373 MESA_META_TEXTURE |
2374 MESA_META_TRANSFORM |
2375 MESA_META_CLIP |
2376 MESA_META_VERTEX |
2377 MESA_META_VIEWPORT));
2378
2379 _mesa_meta_setup_vertex_objects(ctx, &bitmap->VAO, &bitmap->buf_obj, false,
2380 3, 2, 4);
2381
2382 newTex = _mesa_meta_alloc_texture(tex, width, height, texIntFormat);
2383
2384 /* Silence valgrind warnings about reading uninitialized stack. */
2385 memset(verts, 0, sizeof(verts));
2386
2387 /* vertex positions, texcoords, colors (after texture allocation!) */
2388 {
2389 const GLfloat x0 = (GLfloat) x;
2390 const GLfloat y0 = (GLfloat) y;
2391 const GLfloat x1 = (GLfloat) (x + width);
2392 const GLfloat y1 = (GLfloat) (y + height);
2393 const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
2394 GLuint i;
2395
2396 verts[0].x = x0;
2397 verts[0].y = y0;
2398 verts[0].z = z;
2399 verts[0].tex[0] = 0.0F;
2400 verts[0].tex[1] = 0.0F;
2401 verts[1].x = x1;
2402 verts[1].y = y0;
2403 verts[1].z = z;
2404 verts[1].tex[0] = tex->Sright;
2405 verts[1].tex[1] = 0.0F;
2406 verts[2].x = x1;
2407 verts[2].y = y1;
2408 verts[2].z = z;
2409 verts[2].tex[0] = tex->Sright;
2410 verts[2].tex[1] = tex->Ttop;
2411 verts[3].x = x0;
2412 verts[3].y = y1;
2413 verts[3].z = z;
2414 verts[3].tex[0] = 0.0F;
2415 verts[3].tex[1] = tex->Ttop;
2416
2417 for (i = 0; i < 4; i++) {
2418 verts[i].r = ctx->Current.RasterColor[0];
2419 verts[i].g = ctx->Current.RasterColor[1];
2420 verts[i].b = ctx->Current.RasterColor[2];
2421 verts[i].a = ctx->Current.RasterColor[3];
2422 }
2423
2424 /* upload new vertex data */
2425 _mesa_buffer_sub_data(ctx, bitmap->buf_obj, 0, sizeof(verts), verts,
2426 __func__);
2427 }
2428
2429 /* choose different foreground/background alpha values */
2430 CLAMPED_FLOAT_TO_UBYTE(fg, ctx->Current.RasterColor[ACOMP]);
2431 bg = (fg > 127 ? 0 : 255);
2432
2433 bitmap1 = _mesa_map_pbo_source(ctx, &unpackSave, bitmap1);
2434 if (!bitmap1) {
2435 _mesa_meta_end(ctx);
2436 return;
2437 }
2438
2439 bitmap8 = malloc(width * height);
2440 if (bitmap8) {
2441 memset(bitmap8, bg, width * height);
2442 _mesa_expand_bitmap(width, height, &unpackSave, bitmap1,
2443 bitmap8, width, fg);
2444
2445 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
2446
2447 _mesa_set_enable(ctx, GL_ALPHA_TEST, GL_TRUE);
2448 _mesa_AlphaFunc(GL_NOTEQUAL, UBYTE_TO_FLOAT(bg));
2449
2450 _mesa_meta_setup_drawpix_texture(ctx, tex, newTex, width, height,
2451 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap8);
2452
2453 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2454
2455 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
2456
2457 free(bitmap8);
2458 }
2459
2460 _mesa_unmap_pbo_source(ctx, &unpackSave);
2461
2462 _mesa_meta_end(ctx);
2463 }
2464
2465 /**
2466 * Compute the texture coordinates for the four vertices of a quad for
2467 * drawing a 2D texture image or slice of a cube/3D texture. The offset
2468 * and width, height specify a sub-region of the 2D image.
2469 *
2470 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2471 * \param slice slice of a 1D/2D array texture or 3D texture
2472 * \param xoffset X position of sub texture
2473 * \param yoffset Y position of sub texture
2474 * \param width width of the sub texture image
2475 * \param height height of the sub texture image
2476 * \param total_width total width of the texture image
2477 * \param total_height total height of the texture image
2478 * \param total_depth total depth of the texture image
2479 * \param coords0/1/2/3 returns the computed texcoords
2480 */
2481 void
2482 _mesa_meta_setup_texture_coords(GLenum faceTarget,
2483 GLint slice,
2484 GLint xoffset,
2485 GLint yoffset,
2486 GLint width,
2487 GLint height,
2488 GLint total_width,
2489 GLint total_height,
2490 GLint total_depth,
2491 GLfloat coords0[4],
2492 GLfloat coords1[4],
2493 GLfloat coords2[4],
2494 GLfloat coords3[4])
2495 {
2496 float st[4][2];
2497 GLuint i;
2498 const float s0 = (float) xoffset / (float) total_width;
2499 const float s1 = (float) (xoffset + width) / (float) total_width;
2500 const float t0 = (float) yoffset / (float) total_height;
2501 const float t1 = (float) (yoffset + height) / (float) total_height;
2502 GLfloat r;
2503
2504 /* setup the reference texcoords */
2505 st[0][0] = s0;
2506 st[0][1] = t0;
2507 st[1][0] = s1;
2508 st[1][1] = t0;
2509 st[2][0] = s1;
2510 st[2][1] = t1;
2511 st[3][0] = s0;
2512 st[3][1] = t1;
2513
2514 if (faceTarget == GL_TEXTURE_CUBE_MAP_ARRAY)
2515 faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + slice % 6;
2516
2517 /* Currently all texture targets want the W component to be 1.0.
2518 */
2519 coords0[3] = 1.0F;
2520 coords1[3] = 1.0F;
2521 coords2[3] = 1.0F;
2522 coords3[3] = 1.0F;
2523
2524 switch (faceTarget) {
2525 case GL_TEXTURE_1D:
2526 case GL_TEXTURE_2D:
2527 case GL_TEXTURE_3D:
2528 case GL_TEXTURE_2D_ARRAY:
2529 if (faceTarget == GL_TEXTURE_3D) {
2530 assert(slice < total_depth);
2531 assert(total_depth >= 1);
2532 r = (slice + 0.5f) / total_depth;
2533 }
2534 else if (faceTarget == GL_TEXTURE_2D_ARRAY)
2535 r = (float) slice;
2536 else
2537 r = 0.0F;
2538 coords0[0] = st[0][0]; /* s */
2539 coords0[1] = st[0][1]; /* t */
2540 coords0[2] = r; /* r */
2541 coords1[0] = st[1][0];
2542 coords1[1] = st[1][1];
2543 coords1[2] = r;
2544 coords2[0] = st[2][0];
2545 coords2[1] = st[2][1];
2546 coords2[2] = r;
2547 coords3[0] = st[3][0];
2548 coords3[1] = st[3][1];
2549 coords3[2] = r;
2550 break;
2551 case GL_TEXTURE_RECTANGLE_ARB:
2552 coords0[0] = (float) xoffset; /* s */
2553 coords0[1] = (float) yoffset; /* t */
2554 coords0[2] = 0.0F; /* r */
2555 coords1[0] = (float) (xoffset + width);
2556 coords1[1] = (float) yoffset;
2557 coords1[2] = 0.0F;
2558 coords2[0] = (float) (xoffset + width);
2559 coords2[1] = (float) (yoffset + height);
2560 coords2[2] = 0.0F;
2561 coords3[0] = (float) xoffset;
2562 coords3[1] = (float) (yoffset + height);
2563 coords3[2] = 0.0F;
2564 break;
2565 case GL_TEXTURE_1D_ARRAY:
2566 coords0[0] = st[0][0]; /* s */
2567 coords0[1] = (float) slice; /* t */
2568 coords0[2] = 0.0F; /* r */
2569 coords1[0] = st[1][0];
2570 coords1[1] = (float) slice;
2571 coords1[2] = 0.0F;
2572 coords2[0] = st[2][0];
2573 coords2[1] = (float) slice;
2574 coords2[2] = 0.0F;
2575 coords3[0] = st[3][0];
2576 coords3[1] = (float) slice;
2577 coords3[2] = 0.0F;
2578 break;
2579
2580 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2581 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2582 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2583 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2584 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2585 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2586 /* loop over quad verts */
2587 for (i = 0; i < 4; i++) {
2588 /* Compute sc = +/-scale and tc = +/-scale.
2589 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2590 * though that can still sometimes happen with this scale factor...
2591 */
2592 const GLfloat scale = 0.9999f;
2593 const GLfloat sc = (2.0f * st[i][0] - 1.0f) * scale;
2594 const GLfloat tc = (2.0f * st[i][1] - 1.0f) * scale;
2595 GLfloat *coord;
2596
2597 switch (i) {
2598 case 0:
2599 coord = coords0;
2600 break;
2601 case 1:
2602 coord = coords1;
2603 break;
2604 case 2:
2605 coord = coords2;
2606 break;
2607 case 3:
2608 coord = coords3;
2609 break;
2610 default:
2611 unreachable("not reached");
2612 }
2613
2614 coord[3] = (float) (slice / 6);
2615
2616 switch (faceTarget) {
2617 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2618 coord[0] = 1.0f;
2619 coord[1] = -tc;
2620 coord[2] = -sc;
2621 break;
2622 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2623 coord[0] = -1.0f;
2624 coord[1] = -tc;
2625 coord[2] = sc;
2626 break;
2627 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2628 coord[0] = sc;
2629 coord[1] = 1.0f;
2630 coord[2] = tc;
2631 break;
2632 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2633 coord[0] = sc;
2634 coord[1] = -1.0f;
2635 coord[2] = -tc;
2636 break;
2637 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2638 coord[0] = sc;
2639 coord[1] = -tc;
2640 coord[2] = 1.0f;
2641 break;
2642 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2643 coord[0] = -sc;
2644 coord[1] = -tc;
2645 coord[2] = -1.0f;
2646 break;
2647 default:
2648 assert(0);
2649 }
2650 }
2651 break;
2652 default:
2653 assert(!"unexpected target in _mesa_meta_setup_texture_coords()");
2654 }
2655 }
2656
2657 static struct blit_shader *
2658 choose_blit_shader(GLenum target, struct blit_shader_table *table)
2659 {
2660 switch(target) {
2661 case GL_TEXTURE_1D:
2662 table->sampler_1d.type = "sampler1D";
2663 table->sampler_1d.func = "texture1D";
2664 table->sampler_1d.texcoords = "texCoords.x";
2665 return &table->sampler_1d;
2666 case GL_TEXTURE_2D:
2667 table->sampler_2d.type = "sampler2D";
2668 table->sampler_2d.func = "texture2D";
2669 table->sampler_2d.texcoords = "texCoords.xy";
2670 return &table->sampler_2d;
2671 case GL_TEXTURE_RECTANGLE:
2672 table->sampler_rect.type = "sampler2DRect";
2673 table->sampler_rect.func = "texture2DRect";
2674 table->sampler_rect.texcoords = "texCoords.xy";
2675 return &table->sampler_rect;
2676 case GL_TEXTURE_3D:
2677 /* Code for mipmap generation with 3D textures is not used yet.
2678 * It's a sw fallback.
2679 */
2680 table->sampler_3d.type = "sampler3D";
2681 table->sampler_3d.func = "texture3D";
2682 table->sampler_3d.texcoords = "texCoords.xyz";
2683 return &table->sampler_3d;
2684 case GL_TEXTURE_CUBE_MAP:
2685 table->sampler_cubemap.type = "samplerCube";
2686 table->sampler_cubemap.func = "textureCube";
2687 table->sampler_cubemap.texcoords = "texCoords.xyz";
2688 return &table->sampler_cubemap;
2689 case GL_TEXTURE_1D_ARRAY:
2690 table->sampler_1d_array.type = "sampler1DArray";
2691 table->sampler_1d_array.func = "texture1DArray";
2692 table->sampler_1d_array.texcoords = "texCoords.xy";
2693 return &table->sampler_1d_array;
2694 case GL_TEXTURE_2D_ARRAY:
2695 table->sampler_2d_array.type = "sampler2DArray";
2696 table->sampler_2d_array.func = "texture2DArray";
2697 table->sampler_2d_array.texcoords = "texCoords.xyz";
2698 return &table->sampler_2d_array;
2699 case GL_TEXTURE_CUBE_MAP_ARRAY:
2700 table->sampler_cubemap_array.type = "samplerCubeArray";
2701 table->sampler_cubemap_array.func = "textureCubeArray";
2702 table->sampler_cubemap_array.texcoords = "texCoords.xyzw";
2703 return &table->sampler_cubemap_array;
2704 default:
2705 _mesa_problem(NULL, "Unexpected texture target 0x%x in"
2706 " setup_texture_sampler()\n", target);
2707 return NULL;
2708 }
2709 }
2710
2711 void
2712 _mesa_meta_blit_shader_table_cleanup(struct gl_context *ctx,
2713 struct blit_shader_table *table)
2714 {
2715 _mesa_reference_shader_program(ctx, &table->sampler_1d.shader_prog, NULL);
2716 _mesa_reference_shader_program(ctx, &table->sampler_2d.shader_prog, NULL);
2717 _mesa_reference_shader_program(ctx, &table->sampler_3d.shader_prog, NULL);
2718 _mesa_reference_shader_program(ctx, &table->sampler_rect.shader_prog, NULL);
2719 _mesa_reference_shader_program(ctx, &table->sampler_cubemap.shader_prog, NULL);
2720 _mesa_reference_shader_program(ctx, &table->sampler_1d_array.shader_prog, NULL);
2721 _mesa_reference_shader_program(ctx, &table->sampler_2d_array.shader_prog, NULL);
2722 _mesa_reference_shader_program(ctx, &table->sampler_cubemap_array.shader_prog, NULL);
2723 }
2724
2725 /**
2726 * Determine the GL data type to use for the temporary image read with
2727 * ReadPixels() and passed to Tex[Sub]Image().
2728 */
2729 static GLenum
2730 get_temp_image_type(struct gl_context *ctx, mesa_format format)
2731 {
2732 const GLenum baseFormat = _mesa_get_format_base_format(format);
2733 const GLenum datatype = _mesa_get_format_datatype(format);
2734 const GLint format_red_bits = _mesa_get_format_bits(format, GL_RED_BITS);
2735
2736 switch (baseFormat) {
2737 case GL_RGBA:
2738 case GL_RGB:
2739 case GL_RG:
2740 case GL_RED:
2741 case GL_ALPHA:
2742 case GL_LUMINANCE:
2743 case GL_LUMINANCE_ALPHA:
2744 case GL_INTENSITY:
2745 if (datatype == GL_INT || datatype == GL_UNSIGNED_INT) {
2746 return datatype;
2747 } else if (format_red_bits <= 8) {
2748 return GL_UNSIGNED_BYTE;
2749 } else if (format_red_bits <= 16) {
2750 return GL_UNSIGNED_SHORT;
2751 }
2752 return GL_FLOAT;
2753 case GL_DEPTH_COMPONENT:
2754 if (datatype == GL_FLOAT)
2755 return GL_FLOAT;
2756 else
2757 return GL_UNSIGNED_INT;
2758 case GL_DEPTH_STENCIL:
2759 if (datatype == GL_FLOAT)
2760 return GL_FLOAT_32_UNSIGNED_INT_24_8_REV;
2761 else
2762 return GL_UNSIGNED_INT_24_8;
2763 default:
2764 _mesa_problem(ctx, "Unexpected format %d in get_temp_image_type()",
2765 baseFormat);
2766 return 0;
2767 }
2768 }
2769
2770 /**
2771 * Attempts to wrap the destination texture in an FBO and use
2772 * glBlitFramebuffer() to implement glCopyTexSubImage().
2773 */
2774 static bool
2775 copytexsubimage_using_blit_framebuffer(struct gl_context *ctx, GLuint dims,
2776 struct gl_texture_image *texImage,
2777 GLint xoffset,
2778 GLint yoffset,
2779 GLint zoffset,
2780 struct gl_renderbuffer *rb,
2781 GLint x, GLint y,
2782 GLsizei width, GLsizei height)
2783 {
2784 struct gl_framebuffer *drawFb;
2785 bool success = false;
2786 GLbitfield mask;
2787 GLenum status;
2788
2789 if (!ctx->Extensions.ARB_framebuffer_object)
2790 return false;
2791
2792 drawFb = ctx->Driver.NewFramebuffer(ctx, 0xDEADBEEF);
2793 if (drawFb == NULL)
2794 return false;
2795
2796 _mesa_meta_begin(ctx, MESA_META_ALL & ~MESA_META_DRAW_BUFFERS);
2797 _mesa_bind_framebuffers(ctx, drawFb, ctx->ReadBuffer);
2798
2799 if (rb->_BaseFormat == GL_DEPTH_STENCIL ||
2800 rb->_BaseFormat == GL_DEPTH_COMPONENT) {
2801 _mesa_meta_framebuffer_texture_image(ctx, ctx->DrawBuffer,
2802 GL_DEPTH_ATTACHMENT,
2803 texImage, zoffset);
2804 mask = GL_DEPTH_BUFFER_BIT;
2805
2806 if (rb->_BaseFormat == GL_DEPTH_STENCIL &&
2807 texImage->_BaseFormat == GL_DEPTH_STENCIL) {
2808 _mesa_meta_framebuffer_texture_image(ctx, ctx->DrawBuffer,
2809 GL_STENCIL_ATTACHMENT,
2810 texImage, zoffset);
2811 mask |= GL_STENCIL_BUFFER_BIT;
2812 }
2813 _mesa_DrawBuffer(GL_NONE);
2814 } else {
2815 _mesa_meta_framebuffer_texture_image(ctx, ctx->DrawBuffer,
2816 GL_COLOR_ATTACHMENT0,
2817 texImage, zoffset);
2818 mask = GL_COLOR_BUFFER_BIT;
2819 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0);
2820 }
2821
2822 status = _mesa_check_framebuffer_status(ctx, ctx->DrawBuffer);
2823 if (status != GL_FRAMEBUFFER_COMPLETE)
2824 goto out;
2825
2826 ctx->Meta->Blit.no_ctsi_fallback = true;
2827
2828 /* Since we've bound a new draw framebuffer, we need to update
2829 * its derived state -- _Xmin, etc -- for BlitFramebuffer's clipping to
2830 * be correct.
2831 */
2832 _mesa_update_state(ctx);
2833
2834 /* We skip the core BlitFramebuffer checks for format consistency, which
2835 * are too strict for CopyTexImage. We know meta will be fine with format
2836 * changes.
2837 */
2838 mask = _mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
2839 x, y,
2840 x + width, y + height,
2841 xoffset, yoffset,
2842 xoffset + width, yoffset + height,
2843 mask, GL_NEAREST);
2844 ctx->Meta->Blit.no_ctsi_fallback = false;
2845 success = mask == 0x0;
2846
2847 out:
2848 _mesa_reference_framebuffer(&drawFb, NULL);
2849 _mesa_meta_end(ctx);
2850 return success;
2851 }
2852
2853 /**
2854 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2855 * Have to be careful with locking and meta state for pixel transfer.
2856 */
2857 void
2858 _mesa_meta_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
2859 struct gl_texture_image *texImage,
2860 GLint xoffset, GLint yoffset, GLint zoffset,
2861 struct gl_renderbuffer *rb,
2862 GLint x, GLint y,
2863 GLsizei width, GLsizei height)
2864 {
2865 GLenum format, type;
2866 GLint bpp;
2867 void *buf;
2868
2869 if (copytexsubimage_using_blit_framebuffer(ctx, dims,
2870 texImage,
2871 xoffset, yoffset, zoffset,
2872 rb,
2873 x, y,
2874 width, height)) {
2875 return;
2876 }
2877
2878 /* Choose format/type for temporary image buffer */
2879 format = _mesa_get_format_base_format(texImage->TexFormat);
2880 if (format == GL_LUMINANCE ||
2881 format == GL_LUMINANCE_ALPHA ||
2882 format == GL_INTENSITY) {
2883 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
2884 * temp image buffer because glReadPixels will do L=R+G+B which is
2885 * not what we want (should be L=R).
2886 */
2887 format = GL_RGBA;
2888 }
2889
2890 type = get_temp_image_type(ctx, texImage->TexFormat);
2891 if (_mesa_is_format_integer_color(texImage->TexFormat)) {
2892 format = _mesa_base_format_to_integer_format(format);
2893 }
2894 bpp = _mesa_bytes_per_pixel(format, type);
2895 if (bpp <= 0) {
2896 _mesa_problem(ctx, "Bad bpp in _mesa_meta_CopyTexSubImage()");
2897 return;
2898 }
2899
2900 /*
2901 * Alloc image buffer (XXX could use a PBO)
2902 */
2903 buf = malloc(width * height * bpp);
2904 if (!buf) {
2905 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage%uD", dims);
2906 return;
2907 }
2908
2909 /*
2910 * Read image from framebuffer (disable pixel transfer ops)
2911 */
2912 _mesa_meta_begin(ctx, MESA_META_PIXEL_STORE | MESA_META_PIXEL_TRANSFER);
2913 ctx->Driver.ReadPixels(ctx, x, y, width, height,
2914 format, type, &ctx->Pack, buf);
2915 _mesa_meta_end(ctx);
2916
2917 _mesa_update_state(ctx); /* to update pixel transfer state */
2918
2919 /*
2920 * Store texture data (with pixel transfer ops)
2921 */
2922 _mesa_meta_begin(ctx, MESA_META_PIXEL_STORE);
2923
2924 if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
2925 assert(yoffset == 0);
2926 ctx->Driver.TexSubImage(ctx, dims, texImage,
2927 xoffset, zoffset, 0, width, 1, 1,
2928 format, type, buf, &ctx->Unpack);
2929 } else {
2930 ctx->Driver.TexSubImage(ctx, dims, texImage,
2931 xoffset, yoffset, zoffset, width, height, 1,
2932 format, type, buf, &ctx->Unpack);
2933 }
2934
2935 _mesa_meta_end(ctx);
2936
2937 free(buf);
2938 }
2939
2940 static void
2941 meta_decompress_fbo_cleanup(struct decompress_fbo_state *decompress_fbo)
2942 {
2943 if (decompress_fbo->fb != NULL) {
2944 _mesa_reference_framebuffer(&decompress_fbo->fb, NULL);
2945 _mesa_reference_renderbuffer(&decompress_fbo->rb, NULL);
2946 }
2947
2948 memset(decompress_fbo, 0, sizeof(*decompress_fbo));
2949 }
2950
2951 static void
2952 meta_decompress_cleanup(struct gl_context *ctx,
2953 struct decompress_state *decompress)
2954 {
2955 meta_decompress_fbo_cleanup(&decompress->byteFBO);
2956 meta_decompress_fbo_cleanup(&decompress->floatFBO);
2957
2958 if (decompress->VAO != 0) {
2959 _mesa_DeleteVertexArrays(1, &decompress->VAO);
2960 _mesa_reference_buffer_object(ctx, &decompress->buf_obj, NULL);
2961 }
2962
2963 _mesa_reference_sampler_object(ctx, &decompress->samp_obj, NULL);
2964
2965 memset(decompress, 0, sizeof(*decompress));
2966 }
2967
2968 /**
2969 * Decompress a texture image by drawing a quad with the compressed
2970 * texture and reading the pixels out of the color buffer.
2971 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
2972 * \param destFormat format, ala glReadPixels
2973 * \param destType type, ala glReadPixels
2974 * \param dest destination buffer
2975 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
2976 */
2977 static bool
2978 decompress_texture_image(struct gl_context *ctx,
2979 struct gl_texture_image *texImage,
2980 GLuint slice,
2981 GLint xoffset, GLint yoffset,
2982 GLsizei width, GLsizei height,
2983 GLenum destFormat, GLenum destType,
2984 GLvoid *dest)
2985 {
2986 struct decompress_state *decompress = &ctx->Meta->Decompress;
2987 struct decompress_fbo_state *decompress_fbo;
2988 struct gl_texture_object *texObj = texImage->TexObject;
2989 const GLenum target = texObj->Target;
2990 GLenum rbFormat;
2991 GLenum faceTarget;
2992 struct vertex verts[4];
2993 struct gl_sampler_object *samp_obj_save = NULL;
2994 GLenum status;
2995 const bool use_glsl_version = ctx->Extensions.ARB_vertex_shader &&
2996 ctx->Extensions.ARB_fragment_shader;
2997
2998 switch (_mesa_get_format_datatype(texImage->TexFormat)) {
2999 case GL_FLOAT:
3000 decompress_fbo = &decompress->floatFBO;
3001 rbFormat = GL_RGBA32F;
3002 break;
3003 case GL_UNSIGNED_NORMALIZED:
3004 decompress_fbo = &decompress->byteFBO;
3005 rbFormat = GL_RGBA;
3006 break;
3007 default:
3008 return false;
3009 }
3010
3011 if (slice > 0) {
3012 assert(target == GL_TEXTURE_3D ||
3013 target == GL_TEXTURE_2D_ARRAY ||
3014 target == GL_TEXTURE_CUBE_MAP_ARRAY);
3015 }
3016
3017 switch (target) {
3018 case GL_TEXTURE_1D:
3019 case GL_TEXTURE_1D_ARRAY:
3020 assert(!"No compressed 1D textures.");
3021 return false;
3022
3023 case GL_TEXTURE_CUBE_MAP_ARRAY:
3024 faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + (slice % 6);
3025 break;
3026
3027 case GL_TEXTURE_CUBE_MAP:
3028 faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + texImage->Face;
3029 break;
3030
3031 default:
3032 faceTarget = target;
3033 break;
3034 }
3035
3036 _mesa_meta_begin(ctx, MESA_META_ALL & ~(MESA_META_PIXEL_STORE |
3037 MESA_META_DRAW_BUFFERS));
3038 _mesa_ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
3039
3040 _mesa_reference_sampler_object(ctx, &samp_obj_save,
3041 ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler);
3042
3043 /* Create/bind FBO/renderbuffer */
3044 if (decompress_fbo->fb == NULL) {
3045 decompress_fbo->rb = ctx->Driver.NewRenderbuffer(ctx, 0xDEADBEEF);
3046 if (decompress_fbo->rb == NULL) {
3047 _mesa_meta_end(ctx);
3048 return false;
3049 }
3050
3051 decompress_fbo->fb = ctx->Driver.NewFramebuffer(ctx, 0xDEADBEEF);
3052 if (decompress_fbo->fb == NULL) {
3053 _mesa_meta_end(ctx);
3054 return false;
3055 }
3056
3057 _mesa_bind_framebuffers(ctx, decompress_fbo->fb, decompress_fbo->fb);
3058 _mesa_framebuffer_renderbuffer(ctx, ctx->DrawBuffer, GL_COLOR_ATTACHMENT0,
3059 decompress_fbo->rb);
3060 }
3061 else {
3062 _mesa_bind_framebuffers(ctx, decompress_fbo->fb, decompress_fbo->fb);
3063 }
3064
3065 /* alloc dest surface */
3066 if (width > decompress_fbo->Width || height > decompress_fbo->Height) {
3067 _mesa_renderbuffer_storage(ctx, decompress_fbo->rb, rbFormat,
3068 width, height, 0);
3069 status = _mesa_check_framebuffer_status(ctx, ctx->DrawBuffer);
3070 if (status != GL_FRAMEBUFFER_COMPLETE) {
3071 /* If the framebuffer isn't complete then we'll leave
3072 * decompress_fbo->Width as zero so that it will fail again next time
3073 * too */
3074 _mesa_meta_end(ctx);
3075 return false;
3076 }
3077 decompress_fbo->Width = width;
3078 decompress_fbo->Height = height;
3079 }
3080
3081 if (use_glsl_version) {
3082 _mesa_meta_setup_vertex_objects(ctx, &decompress->VAO,
3083 &decompress->buf_obj, true,
3084 2, 4, 0);
3085
3086 _mesa_meta_setup_blit_shader(ctx, target, false, &decompress->shaders);
3087 } else {
3088 _mesa_meta_setup_ff_tnl_for_blit(ctx, &decompress->VAO,
3089 &decompress->buf_obj, 3);
3090 }
3091
3092 if (decompress->samp_obj == NULL) {
3093 decompress->samp_obj = ctx->Driver.NewSamplerObject(ctx, 0xDEADBEEF);
3094 if (decompress->samp_obj == NULL) {
3095 _mesa_meta_end(ctx);
3096
3097 /* This is a bit lazy. Flag out of memory, and then don't bother to
3098 * clean up. Once out of memory is flagged, the only realistic next
3099 * move is to destroy the context. That will trigger all the right
3100 * clean up.
3101 *
3102 * Returning true prevents other GetTexImage methods from attempting
3103 * anything since they will likely fail too.
3104 */
3105 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
3106 return true;
3107 }
3108
3109 /* nearest filtering */
3110 _mesa_set_sampler_filters(ctx, decompress->samp_obj, GL_NEAREST, GL_NEAREST);
3111
3112 /* We don't want to encode or decode sRGB values; treat them as linear. */
3113 _mesa_set_sampler_srgb_decode(ctx, decompress->samp_obj, GL_SKIP_DECODE_EXT);
3114 }
3115
3116 _mesa_bind_sampler(ctx, ctx->Texture.CurrentUnit, decompress->samp_obj);
3117
3118 /* Silence valgrind warnings about reading uninitialized stack. */
3119 memset(verts, 0, sizeof(verts));
3120
3121 _mesa_meta_setup_texture_coords(faceTarget, slice,
3122 xoffset, yoffset, width, height,
3123 texImage->Width, texImage->Height,
3124 texImage->Depth,
3125 verts[0].tex,
3126 verts[1].tex,
3127 verts[2].tex,
3128 verts[3].tex);
3129
3130 /* setup vertex positions */
3131 verts[0].x = -1.0F;
3132 verts[0].y = -1.0F;
3133 verts[1].x = 1.0F;
3134 verts[1].y = -1.0F;
3135 verts[2].x = 1.0F;
3136 verts[2].y = 1.0F;
3137 verts[3].x = -1.0F;
3138 verts[3].y = 1.0F;
3139
3140 _mesa_set_viewport(ctx, 0, 0, 0, width, height);
3141
3142 /* upload new vertex data */
3143 _mesa_buffer_sub_data(ctx, decompress->buf_obj, 0, sizeof(verts), verts,
3144 __func__);
3145
3146 /* setup texture state */
3147 _mesa_BindTexture(target, texObj->Name);
3148
3149 if (!use_glsl_version)
3150 _mesa_set_enable(ctx, target, GL_TRUE);
3151
3152 {
3153 /* save texture object state */
3154 const GLint baseLevelSave = texObj->BaseLevel;
3155 const GLint maxLevelSave = texObj->MaxLevel;
3156
3157 /* restrict sampling to the texture level of interest */
3158 if (target != GL_TEXTURE_RECTANGLE_ARB) {
3159 _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_BASE_LEVEL,
3160 (GLint *) &texImage->Level, false);
3161 _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_MAX_LEVEL,
3162 (GLint *) &texImage->Level, false);
3163 }
3164
3165 /* render quad w/ texture into renderbuffer */
3166 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
3167
3168 /* Restore texture object state, the texture binding will
3169 * be restored by _mesa_meta_end().
3170 */
3171 if (target != GL_TEXTURE_RECTANGLE_ARB) {
3172 _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_BASE_LEVEL,
3173 &baseLevelSave, false);
3174 _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_MAX_LEVEL,
3175 &maxLevelSave, false);
3176 }
3177
3178 }
3179
3180 /* read pixels from renderbuffer */
3181 {
3182 GLenum baseTexFormat = texImage->_BaseFormat;
3183 GLenum destBaseFormat = _mesa_unpack_format_to_base_format(destFormat);
3184
3185 /* The pixel transfer state will be set to default values at this point
3186 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
3187 * turned off (as required by glGetTexImage) but we need to handle some
3188 * special cases. In particular, single-channel texture values are
3189 * returned as red and two-channel texture values are returned as
3190 * red/alpha.
3191 */
3192 if (_mesa_need_luminance_to_rgb_conversion(baseTexFormat,
3193 destBaseFormat) ||
3194 /* If we're reading back an RGB(A) texture (using glGetTexImage) as
3195 * luminance then we need to return L=tex(R).
3196 */
3197 _mesa_need_rgb_to_luminance_conversion(baseTexFormat,
3198 destBaseFormat)) {
3199 /* Green and blue must be zero */
3200 _mesa_PixelTransferf(GL_GREEN_SCALE, 0.0f);
3201 _mesa_PixelTransferf(GL_BLUE_SCALE, 0.0f);
3202 }
3203
3204 _mesa_ReadPixels(0, 0, width, height, destFormat, destType, dest);
3205 }
3206
3207 /* disable texture unit */
3208 if (!use_glsl_version)
3209 _mesa_set_enable(ctx, target, GL_FALSE);
3210
3211 _mesa_bind_sampler(ctx, ctx->Texture.CurrentUnit, samp_obj_save);
3212 _mesa_reference_sampler_object(ctx, &samp_obj_save, NULL);
3213
3214 _mesa_meta_end(ctx);
3215
3216 return true;
3217 }
3218
3219
3220 /**
3221 * This is just a wrapper around _mesa_get_tex_image() and
3222 * decompress_texture_image(). Meta functions should not be directly called
3223 * from core Mesa.
3224 */
3225 void
3226 _mesa_meta_GetTexSubImage(struct gl_context *ctx,
3227 GLint xoffset, GLint yoffset, GLint zoffset,
3228 GLsizei width, GLsizei height, GLsizei depth,
3229 GLenum format, GLenum type, GLvoid *pixels,
3230 struct gl_texture_image *texImage)
3231 {
3232 if (_mesa_is_format_compressed(texImage->TexFormat)) {
3233 GLuint slice;
3234 bool result = true;
3235
3236 for (slice = 0; slice < depth; slice++) {
3237 void *dst;
3238 /* Section 8.11.4 (Texture Image Queries) of the GL 4.5 spec says:
3239 *
3240 * "For three-dimensional, two-dimensional array, cube map array,
3241 * and cube map textures pixel storage operations are applied as
3242 * if the image were two-dimensional, except that the additional
3243 * pixel storage state values PACK_IMAGE_HEIGHT and
3244 * PACK_SKIP_IMAGES are applied. The correspondence of texels to
3245 * memory locations is as defined for TexImage3D in section 8.5."
3246 */
3247 switch (texImage->TexObject->Target) {
3248 case GL_TEXTURE_3D:
3249 case GL_TEXTURE_2D_ARRAY:
3250 case GL_TEXTURE_CUBE_MAP:
3251 case GL_TEXTURE_CUBE_MAP_ARRAY: {
3252 /* Setup pixel packing. SkipPixels and SkipRows will be applied
3253 * in the decompress_texture_image() function's call to
3254 * glReadPixels but we need to compute the dest slice's address
3255 * here (according to SkipImages and ImageHeight).
3256 */
3257 struct gl_pixelstore_attrib packing = ctx->Pack;
3258 packing.SkipPixels = 0;
3259 packing.SkipRows = 0;
3260 dst = _mesa_image_address3d(&packing, pixels, width, height,
3261 format, type, slice, 0, 0);
3262 break;
3263 }
3264 default:
3265 dst = pixels;
3266 break;
3267 }
3268 result = decompress_texture_image(ctx, texImage, slice,
3269 xoffset, yoffset, width, height,
3270 format, type, dst);
3271 if (!result)
3272 break;
3273 }
3274
3275 if (result)
3276 return;
3277 }
3278
3279 _mesa_GetTexSubImage_sw(ctx, xoffset, yoffset, zoffset,
3280 width, height, depth, format, type, pixels, texImage);
3281 }
3282
3283
3284 /**
3285 * Meta implementation of ctx->Driver.DrawTex() in terms
3286 * of polygon rendering.
3287 */
3288 void
3289 _mesa_meta_DrawTex(struct gl_context *ctx, GLfloat x, GLfloat y, GLfloat z,
3290 GLfloat width, GLfloat height)
3291 {
3292 struct drawtex_state *drawtex = &ctx->Meta->DrawTex;
3293 struct vertex {
3294 GLfloat x, y, z, st[MAX_TEXTURE_UNITS][2];
3295 };
3296 struct vertex verts[4];
3297 GLuint i;
3298
3299 _mesa_meta_begin(ctx, (MESA_META_RASTERIZATION |
3300 MESA_META_SHADER |
3301 MESA_META_TRANSFORM |
3302 MESA_META_VERTEX |
3303 MESA_META_VIEWPORT));
3304
3305 if (drawtex->VAO == 0) {
3306 /* one-time setup */
3307 struct gl_vertex_array_object *array_obj;
3308
3309 /* create vertex array object */
3310 _mesa_GenVertexArrays(1, &drawtex->VAO);
3311 _mesa_BindVertexArray(drawtex->VAO);
3312
3313 array_obj = _mesa_lookup_vao(ctx, drawtex->VAO);
3314 assert(array_obj != NULL);
3315
3316 /* create vertex array buffer */
3317 drawtex->buf_obj = ctx->Driver.NewBufferObject(ctx, 0xDEADBEEF);
3318 if (drawtex->buf_obj == NULL)
3319 return;
3320
3321 _mesa_buffer_data(ctx, drawtex->buf_obj, GL_NONE, sizeof(verts), verts,
3322 GL_DYNAMIC_DRAW, __func__);
3323
3324 /* setup vertex arrays */
3325 FLUSH_VERTICES(ctx, 0);
3326 _mesa_update_array_format(ctx, array_obj, VERT_ATTRIB_POS,
3327 3, GL_FLOAT, GL_RGBA, GL_FALSE,
3328 GL_FALSE, GL_FALSE,
3329 offsetof(struct vertex, x));
3330 _mesa_bind_vertex_buffer(ctx, array_obj, VERT_ATTRIB_POS,
3331 drawtex->buf_obj, 0, sizeof(struct vertex));
3332 _mesa_enable_vertex_array_attrib(ctx, array_obj, VERT_ATTRIB_POS);
3333
3334
3335 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
3336 FLUSH_VERTICES(ctx, 0);
3337 _mesa_update_array_format(ctx, array_obj, VERT_ATTRIB_TEX(i),
3338 2, GL_FLOAT, GL_RGBA, GL_FALSE,
3339 GL_FALSE, GL_FALSE,
3340 offsetof(struct vertex, st[i]));
3341 _mesa_bind_vertex_buffer(ctx, array_obj, VERT_ATTRIB_TEX(i),
3342 drawtex->buf_obj, 0, sizeof(struct vertex));
3343 _mesa_enable_vertex_array_attrib(ctx, array_obj, VERT_ATTRIB_TEX(i));
3344 }
3345 }
3346 else {
3347 _mesa_BindVertexArray(drawtex->VAO);
3348 }
3349
3350 /* vertex positions, texcoords */
3351 {
3352 const GLfloat x1 = x + width;
3353 const GLfloat y1 = y + height;
3354
3355 z = CLAMP(z, 0.0f, 1.0f);
3356 z = invert_z(z);
3357
3358 verts[0].x = x;
3359 verts[0].y = y;
3360 verts[0].z = z;
3361
3362 verts[1].x = x1;
3363 verts[1].y = y;
3364 verts[1].z = z;
3365
3366 verts[2].x = x1;
3367 verts[2].y = y1;
3368 verts[2].z = z;
3369
3370 verts[3].x = x;
3371 verts[3].y = y1;
3372 verts[3].z = z;
3373
3374 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
3375 const struct gl_texture_object *texObj;
3376 const struct gl_texture_image *texImage;
3377 GLfloat s, t, s1, t1;
3378 GLuint tw, th;
3379
3380 if (!ctx->Texture.Unit[i]._Current) {
3381 GLuint j;
3382 for (j = 0; j < 4; j++) {
3383 verts[j].st[i][0] = 0.0f;
3384 verts[j].st[i][1] = 0.0f;
3385 }
3386 continue;
3387 }
3388
3389 texObj = ctx->Texture.Unit[i]._Current;
3390 texImage = texObj->Image[0][texObj->BaseLevel];
3391 tw = texImage->Width2;
3392 th = texImage->Height2;
3393
3394 s = (GLfloat) texObj->CropRect[0] / tw;
3395 t = (GLfloat) texObj->CropRect[1] / th;
3396 s1 = (GLfloat) (texObj->CropRect[0] + texObj->CropRect[2]) / tw;
3397 t1 = (GLfloat) (texObj->CropRect[1] + texObj->CropRect[3]) / th;
3398
3399 verts[0].st[i][0] = s;
3400 verts[0].st[i][1] = t;
3401
3402 verts[1].st[i][0] = s1;
3403 verts[1].st[i][1] = t;
3404
3405 verts[2].st[i][0] = s1;
3406 verts[2].st[i][1] = t1;
3407
3408 verts[3].st[i][0] = s;
3409 verts[3].st[i][1] = t1;
3410 }
3411
3412 _mesa_buffer_sub_data(ctx, drawtex->buf_obj, 0, sizeof(verts), verts,
3413 __func__);
3414 }
3415
3416 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
3417
3418 _mesa_meta_end(ctx);
3419 }
3420
3421 static bool
3422 cleartexsubimage_color(struct gl_context *ctx,
3423 struct gl_texture_image *texImage,
3424 const GLvoid *clearValue,
3425 GLint zoffset)
3426 {
3427 mesa_format format;
3428 union gl_color_union colorValue;
3429 GLenum datatype;
3430 GLenum status;
3431
3432 _mesa_meta_framebuffer_texture_image(ctx, ctx->DrawBuffer,
3433 GL_COLOR_ATTACHMENT0,
3434 texImage, zoffset);
3435
3436 status = _mesa_check_framebuffer_status(ctx, ctx->DrawBuffer);
3437 if (status != GL_FRAMEBUFFER_COMPLETE)
3438 return false;
3439
3440 /* We don't want to apply an sRGB conversion so override the format */
3441 format = _mesa_get_srgb_format_linear(texImage->TexFormat);
3442 datatype = _mesa_get_format_datatype(format);
3443
3444 switch (datatype) {
3445 case GL_UNSIGNED_INT:
3446 case GL_INT:
3447 if (clearValue)
3448 _mesa_unpack_uint_rgba_row(format, 1, clearValue,
3449 (GLuint (*)[4]) colorValue.ui);
3450 else
3451 memset(&colorValue, 0, sizeof colorValue);
3452 if (datatype == GL_INT)
3453 _mesa_ClearBufferiv(GL_COLOR, 0, colorValue.i);
3454 else
3455 _mesa_ClearBufferuiv(GL_COLOR, 0, colorValue.ui);
3456 break;
3457 default:
3458 if (clearValue)
3459 _mesa_unpack_rgba_row(format, 1, clearValue,
3460 (GLfloat (*)[4]) colorValue.f);
3461 else
3462 memset(&colorValue, 0, sizeof colorValue);
3463 _mesa_ClearBufferfv(GL_COLOR, 0, colorValue.f);
3464 break;
3465 }
3466
3467 return true;
3468 }
3469
3470 static bool
3471 cleartexsubimage_depth_stencil(struct gl_context *ctx,
3472 struct gl_texture_image *texImage,
3473 const GLvoid *clearValue,
3474 GLint zoffset)
3475 {
3476 GLint stencilValue;
3477 GLfloat depthValue;
3478 GLenum status;
3479
3480 _mesa_meta_framebuffer_texture_image(ctx, ctx->DrawBuffer,
3481 GL_DEPTH_ATTACHMENT,
3482 texImage, zoffset);
3483
3484 if (texImage->_BaseFormat == GL_DEPTH_STENCIL)
3485 _mesa_meta_framebuffer_texture_image(ctx, ctx->DrawBuffer,
3486 GL_STENCIL_ATTACHMENT,
3487 texImage, zoffset);
3488
3489 status = _mesa_check_framebuffer_status(ctx, ctx->DrawBuffer);
3490 if (status != GL_FRAMEBUFFER_COMPLETE)
3491 return false;
3492
3493 if (clearValue) {
3494 GLuint depthStencilValue[2];
3495
3496 /* Convert the clearValue from whatever format it's in to a floating
3497 * point value for the depth and an integer value for the stencil index
3498 */
3499 _mesa_unpack_float_32_uint_24_8_depth_stencil_row(texImage->TexFormat,
3500 1, /* n */
3501 clearValue,
3502 depthStencilValue);
3503 /* We need a memcpy here instead of a cast because we need to
3504 * reinterpret the bytes as a float rather than converting it
3505 */
3506 memcpy(&depthValue, depthStencilValue, sizeof depthValue);
3507 stencilValue = depthStencilValue[1] & 0xff;
3508 } else {
3509 depthValue = 0.0f;
3510 stencilValue = 0;
3511 }
3512
3513 if (texImage->_BaseFormat == GL_DEPTH_STENCIL)
3514 _mesa_ClearBufferfi(GL_DEPTH_STENCIL, 0, depthValue, stencilValue);
3515 else
3516 _mesa_ClearBufferfv(GL_DEPTH, 0, &depthValue);
3517
3518 return true;
3519 }
3520
3521 static bool
3522 cleartexsubimage_for_zoffset(struct gl_context *ctx,
3523 struct gl_texture_image *texImage,
3524 GLint zoffset,
3525 const GLvoid *clearValue)
3526 {
3527 struct gl_framebuffer *drawFb;
3528 bool success;
3529
3530 drawFb = ctx->Driver.NewFramebuffer(ctx, 0xDEADBEEF);
3531 if (drawFb == NULL)
3532 return false;
3533
3534 _mesa_bind_framebuffers(ctx, drawFb, ctx->ReadBuffer);
3535
3536 switch(texImage->_BaseFormat) {
3537 case GL_DEPTH_STENCIL:
3538 case GL_DEPTH_COMPONENT:
3539 success = cleartexsubimage_depth_stencil(ctx, texImage,
3540 clearValue, zoffset);
3541 break;
3542 default:
3543 success = cleartexsubimage_color(ctx, texImage, clearValue, zoffset);
3544 break;
3545 }
3546
3547 _mesa_reference_framebuffer(&drawFb, NULL);
3548
3549 return success;
3550 }
3551
3552 static bool
3553 cleartexsubimage_using_fbo(struct gl_context *ctx,
3554 struct gl_texture_image *texImage,
3555 GLint xoffset, GLint yoffset, GLint zoffset,
3556 GLsizei width, GLsizei height, GLsizei depth,
3557 const GLvoid *clearValue)
3558 {
3559 bool success = true;
3560 GLint z;
3561
3562 _mesa_meta_begin(ctx,
3563 MESA_META_SCISSOR |
3564 MESA_META_COLOR_MASK |
3565 MESA_META_DITHER |
3566 MESA_META_FRAMEBUFFER_SRGB);
3567
3568 _mesa_ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
3569 _mesa_set_enable(ctx, GL_DITHER, GL_FALSE);
3570
3571 _mesa_set_enable(ctx, GL_SCISSOR_TEST, GL_TRUE);
3572 _mesa_Scissor(xoffset, yoffset, width, height);
3573
3574 for (z = zoffset; z < zoffset + depth; z++) {
3575 if (!cleartexsubimage_for_zoffset(ctx, texImage, z, clearValue)) {
3576 success = false;
3577 break;
3578 }
3579 }
3580
3581 _mesa_meta_end(ctx);
3582
3583 return success;
3584 }
3585
3586 extern void
3587 _mesa_meta_ClearTexSubImage(struct gl_context *ctx,
3588 struct gl_texture_image *texImage,
3589 GLint xoffset, GLint yoffset, GLint zoffset,
3590 GLsizei width, GLsizei height, GLsizei depth,
3591 const GLvoid *clearValue)
3592 {
3593 bool res;
3594
3595 res = cleartexsubimage_using_fbo(ctx, texImage,
3596 xoffset, yoffset, zoffset,
3597 width, height, depth,
3598 clearValue);
3599
3600 if (res)
3601 return;
3602
3603 _mesa_warning(ctx,
3604 "Falling back to mapping the texture in "
3605 "glClearTexSubImage\n");
3606
3607 _mesa_store_cleartexsubimage(ctx, texImage,
3608 xoffset, yoffset, zoffset,
3609 width, height, depth,
3610 clearValue);
3611 }