mesa: remove exec="dynamic" from Draw functions that are not really dynamic
[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/arbprogram.h"
37 #include "main/arrayobj.h"
38 #include "main/blend.h"
39 #include "main/blit.h"
40 #include "main/bufferobj.h"
41 #include "main/buffers.h"
42 #include "main/clear.h"
43 #include "main/condrender.h"
44 #include "main/draw.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 #include "compiler/nir/nir.h"
91 #include "util/u_math.h"
92 #include "util/u_memory.h"
93
94 /** Return offset in bytes of the field within a vertex struct */
95 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
96
97 static void
98 meta_clear(struct gl_context *ctx, GLbitfield buffers, bool glsl);
99
100 static struct blit_shader *
101 choose_blit_shader(GLenum target, struct blit_shader_table *table);
102
103 static void cleanup_temp_texture(struct gl_context *ctx,
104 struct temp_texture *tex);
105 static void meta_glsl_clear_cleanup(struct gl_context *ctx,
106 struct clear_state *clear);
107 static void meta_copypix_cleanup(struct gl_context *ctx,
108 struct copypix_state *copypix);
109 static void meta_decompress_cleanup(struct gl_context *ctx,
110 struct decompress_state *decompress);
111 static void meta_drawpix_cleanup(struct gl_context *ctx,
112 struct drawpix_state *drawpix);
113 static void meta_drawtex_cleanup(struct gl_context *ctx,
114 struct drawtex_state *drawtex);
115 static void meta_bitmap_cleanup(struct gl_context *ctx,
116 struct bitmap_state *bitmap);
117
118 void
119 _mesa_meta_framebuffer_texture_image(struct gl_context *ctx,
120 struct gl_framebuffer *fb,
121 GLenum attachment,
122 struct gl_texture_image *texImage,
123 GLuint layer)
124 {
125 struct gl_texture_object *texObj = texImage->TexObject;
126 int level = texImage->Level;
127 const GLenum texTarget = texObj->Target == GL_TEXTURE_CUBE_MAP
128 ? GL_TEXTURE_CUBE_MAP_POSITIVE_X + texImage->Face
129 : texObj->Target;
130
131 struct gl_renderbuffer_attachment *att =
132 _mesa_get_and_validate_attachment(ctx, fb, attachment, __func__);
133 assert(att);
134
135 _mesa_framebuffer_texture(ctx, fb, attachment, att, texObj, texTarget,
136 level, att->NumSamples, layer, false);
137 }
138
139 static struct gl_shader *
140 meta_compile_shader_with_debug(struct gl_context *ctx, gl_shader_stage stage,
141 const GLcharARB *source)
142 {
143 const GLuint name = ~0;
144 struct gl_shader *sh;
145
146 sh = _mesa_new_shader(name, stage);
147 sh->Source = strdup(source);
148 sh->CompileStatus = COMPILE_FAILURE;
149 _mesa_compile_shader(ctx, sh);
150
151 if (!sh->CompileStatus) {
152 if (sh->InfoLog) {
153 _mesa_problem(ctx,
154 "meta program compile failed:\n%s\nsource:\n%s\n",
155 sh->InfoLog, source);
156 }
157
158 _mesa_reference_shader(ctx, &sh, NULL);
159 }
160
161 return sh;
162 }
163
164 void
165 _mesa_meta_link_program_with_debug(struct gl_context *ctx,
166 struct gl_shader_program *sh_prog)
167 {
168 _mesa_link_program(ctx, sh_prog);
169
170 if (!sh_prog->data->LinkStatus) {
171 _mesa_problem(ctx, "meta program link failed:\n%s",
172 sh_prog->data->InfoLog);
173 }
174 }
175
176 void
177 _mesa_meta_use_program(struct gl_context *ctx,
178 struct gl_shader_program *sh_prog)
179 {
180 /* Attach shader state to the binding point */
181 _mesa_reference_pipeline_object(ctx, &ctx->_Shader, &ctx->Shader);
182
183 /* Update the program */
184 _mesa_use_shader_program(ctx, sh_prog);
185 }
186
187 void
188 _mesa_meta_compile_and_link_program(struct gl_context *ctx,
189 const char *vs_source,
190 const char *fs_source,
191 const char *name,
192 struct gl_shader_program **out_sh_prog)
193 {
194 struct gl_shader_program *sh_prog;
195 const GLuint id = ~0;
196
197 sh_prog = _mesa_new_shader_program(id);
198 sh_prog->Label = strdup(name);
199 sh_prog->NumShaders = 2;
200 sh_prog->Shaders = malloc(2 * sizeof(struct gl_shader *));
201 sh_prog->Shaders[0] =
202 meta_compile_shader_with_debug(ctx, MESA_SHADER_VERTEX, vs_source);
203 sh_prog->Shaders[1] =
204 meta_compile_shader_with_debug(ctx, MESA_SHADER_FRAGMENT, fs_source);
205
206 _mesa_meta_link_program_with_debug(ctx, sh_prog);
207
208 struct gl_program *fp =
209 sh_prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program;
210
211 /* texelFetch() can break GL_SKIP_DECODE_EXT, but many meta passes want
212 * to use both together; pretend that we're not using texelFetch to hack
213 * around this bad interaction. This is a bit fragile as it may break
214 * if you re-run the pass that gathers this info, but we probably won't...
215 */
216 fp->info.textures_used_by_txf = 0;
217 if (fp->nir)
218 fp->nir->info.textures_used_by_txf = 0;
219
220 _mesa_meta_use_program(ctx, sh_prog);
221
222 *out_sh_prog = sh_prog;
223 }
224
225 /**
226 * Generate a generic shader to blit from a texture to a framebuffer
227 *
228 * \param ctx Current GL context
229 * \param texTarget Texture target that will be the source of the blit
230 *
231 * \returns a handle to a shader program on success or zero on failure.
232 */
233 void
234 _mesa_meta_setup_blit_shader(struct gl_context *ctx,
235 GLenum target,
236 bool do_depth,
237 struct blit_shader_table *table)
238 {
239 char *vs_source, *fs_source;
240 struct blit_shader *shader = choose_blit_shader(target, table);
241 const char *fs_input, *vs_preprocess, *fs_preprocess;
242 void *mem_ctx;
243
244 if (ctx->Const.GLSLVersion < 130) {
245 vs_preprocess = "";
246 fs_preprocess = "#extension GL_EXT_texture_array : enable";
247 fs_input = "varying";
248 } else {
249 vs_preprocess = "#version 130";
250 fs_preprocess = "#version 130";
251 fs_input = "in";
252 shader->func = "texture";
253 }
254
255 assert(shader != NULL);
256
257 if (shader->shader_prog != NULL) {
258 _mesa_meta_use_program(ctx, shader->shader_prog);
259 return;
260 }
261
262 mem_ctx = ralloc_context(NULL);
263
264 vs_source = ralloc_asprintf(mem_ctx,
265 "%s\n"
266 "#extension GL_ARB_explicit_attrib_location: enable\n"
267 "layout(location = 0) in vec2 position;\n"
268 "layout(location = 1) in vec4 textureCoords;\n"
269 "out vec4 texCoords;\n"
270 "void main()\n"
271 "{\n"
272 " texCoords = textureCoords;\n"
273 " gl_Position = vec4(position, 0.0, 1.0);\n"
274 "}\n",
275 vs_preprocess);
276
277 fs_source = ralloc_asprintf(mem_ctx,
278 "%s\n"
279 "#extension GL_ARB_texture_cube_map_array: enable\n"
280 "uniform %s texSampler;\n"
281 "%s vec4 texCoords;\n"
282 "void main()\n"
283 "{\n"
284 " gl_FragColor = %s(texSampler, %s);\n"
285 "%s"
286 "}\n",
287 fs_preprocess, shader->type, fs_input,
288 shader->func, shader->texcoords,
289 do_depth ? " gl_FragDepth = gl_FragColor.x;\n" : "");
290
291 _mesa_meta_compile_and_link_program(ctx, vs_source, fs_source,
292 ralloc_asprintf(mem_ctx, "%s blit",
293 shader->type),
294 &shader->shader_prog);
295 ralloc_free(mem_ctx);
296 }
297
298 /**
299 * Configure vertex buffer and vertex array objects for tests
300 *
301 * Regardless of whether a new VAO is created, the object referenced by \c VAO
302 * will be bound into the GL state vector when this function terminates. The
303 * object referenced by \c VBO will \b not be bound.
304 *
305 * \param VAO Storage for vertex array object handle. If 0, a new VAO
306 * will be created.
307 * \param buf_obj Storage for vertex buffer object pointer. If \c NULL, a new VBO
308 * will be created. The new VBO will have storage for 4
309 * \c vertex structures.
310 * \param use_generic_attributes Should generic attributes 0 and 1 be used,
311 * or should traditional, fixed-function color and texture
312 * coordinate be used?
313 * \param vertex_size Number of components for attribute 0 / vertex.
314 * \param texcoord_size Number of components for attribute 1 / texture
315 * coordinate. If this is 0, attribute 1 will not be set or
316 * enabled.
317 * \param color_size Number of components for attribute 1 / primary color.
318 * If this is 0, attribute 1 will not be set or enabled.
319 *
320 * \note If \c use_generic_attributes is \c true, \c color_size must be zero.
321 * Use \c texcoord_size instead.
322 */
323 void
324 _mesa_meta_setup_vertex_objects(struct gl_context *ctx,
325 GLuint *VAO, struct gl_buffer_object **buf_obj,
326 bool use_generic_attributes,
327 unsigned vertex_size, unsigned texcoord_size,
328 unsigned color_size)
329 {
330 if (*VAO == 0) {
331 struct gl_vertex_array_object *array_obj;
332 assert(*buf_obj == NULL);
333
334 /* create vertex array object */
335 _mesa_GenVertexArrays(1, VAO);
336 _mesa_BindVertexArray(*VAO);
337
338 array_obj = _mesa_lookup_vao(ctx, *VAO);
339 assert(array_obj != NULL);
340
341 /* create vertex array buffer */
342 *buf_obj = ctx->Driver.NewBufferObject(ctx, 0xDEADBEEF);
343 if (*buf_obj == NULL)
344 return;
345
346 _mesa_buffer_data(ctx, *buf_obj, GL_NONE, 4 * sizeof(struct vertex), NULL,
347 GL_DYNAMIC_DRAW, __func__);
348
349 /* setup vertex arrays */
350 FLUSH_VERTICES(ctx, 0);
351 if (use_generic_attributes) {
352 assert(color_size == 0);
353
354 _mesa_update_array_format(ctx, array_obj, VERT_ATTRIB_GENERIC(0),
355 vertex_size, GL_FLOAT, GL_RGBA, GL_FALSE,
356 GL_FALSE, GL_FALSE,
357 offsetof(struct vertex, x));
358 _mesa_bind_vertex_buffer(ctx, array_obj, VERT_ATTRIB_GENERIC(0),
359 *buf_obj, 0, sizeof(struct vertex));
360 _mesa_enable_vertex_array_attrib(ctx, array_obj,
361 VERT_ATTRIB_GENERIC(0));
362 if (texcoord_size > 0) {
363 _mesa_update_array_format(ctx, array_obj, VERT_ATTRIB_GENERIC(1),
364 texcoord_size, GL_FLOAT, GL_RGBA,
365 GL_FALSE, GL_FALSE, GL_FALSE,
366 offsetof(struct vertex, tex));
367 _mesa_bind_vertex_buffer(ctx, array_obj, VERT_ATTRIB_GENERIC(1),
368 *buf_obj, 0, sizeof(struct vertex));
369 _mesa_enable_vertex_array_attrib(ctx, array_obj,
370 VERT_ATTRIB_GENERIC(1));
371 }
372 } else {
373 _mesa_update_array_format(ctx, array_obj, VERT_ATTRIB_POS,
374 vertex_size, GL_FLOAT, GL_RGBA, GL_FALSE,
375 GL_FALSE, GL_FALSE,
376 offsetof(struct vertex, x));
377 _mesa_bind_vertex_buffer(ctx, array_obj, VERT_ATTRIB_POS,
378 *buf_obj, 0, sizeof(struct vertex));
379 _mesa_enable_vertex_array_attrib(ctx, array_obj, VERT_ATTRIB_POS);
380
381 if (texcoord_size > 0) {
382 _mesa_update_array_format(ctx, array_obj, VERT_ATTRIB_TEX(0),
383 vertex_size, GL_FLOAT, GL_RGBA, GL_FALSE,
384 GL_FALSE, GL_FALSE,
385 offsetof(struct vertex, tex));
386 _mesa_bind_vertex_buffer(ctx, array_obj, VERT_ATTRIB_TEX(0),
387 *buf_obj, 0, sizeof(struct vertex));
388 _mesa_enable_vertex_array_attrib(ctx, array_obj,
389 VERT_ATTRIB_TEX(0));
390 }
391
392 if (color_size > 0) {
393 _mesa_update_array_format(ctx, array_obj, VERT_ATTRIB_COLOR0,
394 vertex_size, GL_FLOAT, GL_RGBA, GL_FALSE,
395 GL_FALSE, GL_FALSE,
396 offsetof(struct vertex, r));
397 _mesa_bind_vertex_buffer(ctx, array_obj, VERT_ATTRIB_COLOR0,
398 *buf_obj, 0, sizeof(struct vertex));
399 _mesa_enable_vertex_array_attrib(ctx, array_obj,
400 VERT_ATTRIB_COLOR0);
401 }
402 }
403 } else {
404 _mesa_BindVertexArray(*VAO);
405 }
406 }
407
408 /**
409 * Initialize meta-ops for a context.
410 * To be called once during context creation.
411 */
412 void
413 _mesa_meta_init(struct gl_context *ctx)
414 {
415 assert(!ctx->Meta);
416
417 ctx->Meta = CALLOC_STRUCT(gl_meta_state);
418 }
419
420 /**
421 * Free context meta-op state.
422 * To be called once during context destruction.
423 */
424 void
425 _mesa_meta_free(struct gl_context *ctx)
426 {
427 GET_CURRENT_CONTEXT(old_context);
428 _mesa_make_current(ctx, NULL, NULL);
429 _mesa_meta_glsl_blit_cleanup(ctx, &ctx->Meta->Blit);
430 meta_glsl_clear_cleanup(ctx, &ctx->Meta->Clear);
431 meta_copypix_cleanup(ctx, &ctx->Meta->CopyPix);
432 _mesa_meta_glsl_generate_mipmap_cleanup(ctx, &ctx->Meta->Mipmap);
433 cleanup_temp_texture(ctx, &ctx->Meta->TempTex);
434 meta_decompress_cleanup(ctx, &ctx->Meta->Decompress);
435 meta_drawpix_cleanup(ctx, &ctx->Meta->DrawPix);
436 meta_drawtex_cleanup(ctx, &ctx->Meta->DrawTex);
437 meta_bitmap_cleanup(ctx, &ctx->Meta->Bitmap);
438
439 if (old_context)
440 _mesa_make_current(old_context, old_context->WinSysDrawBuffer, old_context->WinSysReadBuffer);
441 else
442 _mesa_make_current(NULL, NULL, NULL);
443 free(ctx->Meta);
444 ctx->Meta = NULL;
445 }
446
447
448 /**
449 * Enter meta state. This is like a light-weight version of glPushAttrib
450 * but it also resets most GL state back to default values.
451 *
452 * \param state bitmask of MESA_META_* flags indicating which attribute groups
453 * to save and reset to their defaults
454 */
455 void
456 _mesa_meta_begin(struct gl_context *ctx, GLbitfield state)
457 {
458 struct save_state *save;
459
460 /* hope MAX_META_OPS_DEPTH is large enough */
461 assert(ctx->Meta->SaveStackDepth < MAX_META_OPS_DEPTH);
462
463 save = &ctx->Meta->Save[ctx->Meta->SaveStackDepth++];
464 memset(save, 0, sizeof(*save));
465 save->SavedState = state;
466
467 /* We always push into desktop GL mode and pop out at the end. No sense in
468 * writing our shaders varying based on the user's context choice, when
469 * Mesa can handle either.
470 */
471 save->API = ctx->API;
472 ctx->API = API_OPENGL_COMPAT;
473
474 /* Mesa's extension helper functions use the current context's API to look up
475 * the version required by an extension as a step in determining whether or
476 * not it has been advertised. Since meta aims to only be restricted by the
477 * driver capability (and not by whether or not an extension has been
478 * advertised), set the helper functions' Version variable to a value that
479 * will make the checks on the context API and version unconditionally pass.
480 */
481 save->ExtensionsVersion = ctx->Extensions.Version;
482 ctx->Extensions.Version = ~0;
483
484 /* Pausing transform feedback needs to be done early, or else we won't be
485 * able to change other state.
486 */
487 save->TransformFeedbackNeedsResume =
488 _mesa_is_xfb_active_and_unpaused(ctx);
489 if (save->TransformFeedbackNeedsResume)
490 _mesa_PauseTransformFeedback();
491
492 /* After saving the current occlusion object, call EndQuery so that no
493 * occlusion querying will be active during the meta-operation.
494 */
495 if (state & MESA_META_OCCLUSION_QUERY) {
496 save->CurrentOcclusionObject = ctx->Query.CurrentOcclusionObject;
497 if (save->CurrentOcclusionObject)
498 _mesa_EndQuery(save->CurrentOcclusionObject->Target);
499 }
500
501 if (state & MESA_META_ALPHA_TEST) {
502 save->AlphaEnabled = ctx->Color.AlphaEnabled;
503 save->AlphaFunc = ctx->Color.AlphaFunc;
504 save->AlphaRef = ctx->Color.AlphaRef;
505 if (ctx->Color.AlphaEnabled)
506 _mesa_set_enable(ctx, GL_ALPHA_TEST, GL_FALSE);
507 }
508
509 if (state & MESA_META_BLEND) {
510 save->BlendEnabled = ctx->Color.BlendEnabled;
511 if (ctx->Color.BlendEnabled) {
512 if (ctx->Extensions.EXT_draw_buffers2) {
513 GLuint i;
514 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
515 _mesa_set_enablei(ctx, GL_BLEND, i, GL_FALSE);
516 }
517 }
518 else {
519 _mesa_set_enable(ctx, GL_BLEND, GL_FALSE);
520 }
521 }
522 save->ColorLogicOpEnabled = ctx->Color.ColorLogicOpEnabled;
523 if (ctx->Color.ColorLogicOpEnabled)
524 _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP, GL_FALSE);
525 }
526
527 if (state & MESA_META_DITHER) {
528 save->DitherFlag = ctx->Color.DitherFlag;
529 _mesa_set_enable(ctx, GL_DITHER, GL_TRUE);
530 }
531
532 if (state & MESA_META_COLOR_MASK)
533 save->ColorMask = ctx->Color.ColorMask;
534
535 if (state & MESA_META_DEPTH_TEST) {
536 save->Depth = ctx->Depth; /* struct copy */
537 if (ctx->Depth.Test)
538 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_FALSE);
539 }
540
541 if (state & MESA_META_FOG) {
542 save->Fog = ctx->Fog.Enabled;
543 if (ctx->Fog.Enabled)
544 _mesa_set_enable(ctx, GL_FOG, GL_FALSE);
545 }
546
547 if (state & MESA_META_PIXEL_STORE) {
548 save->Pack = ctx->Pack;
549 save->Unpack = ctx->Unpack;
550 ctx->Pack = ctx->DefaultPacking;
551 ctx->Unpack = ctx->DefaultPacking;
552 }
553
554 if (state & MESA_META_PIXEL_TRANSFER) {
555 save->RedScale = ctx->Pixel.RedScale;
556 save->RedBias = ctx->Pixel.RedBias;
557 save->GreenScale = ctx->Pixel.GreenScale;
558 save->GreenBias = ctx->Pixel.GreenBias;
559 save->BlueScale = ctx->Pixel.BlueScale;
560 save->BlueBias = ctx->Pixel.BlueBias;
561 save->AlphaScale = ctx->Pixel.AlphaScale;
562 save->AlphaBias = ctx->Pixel.AlphaBias;
563 save->MapColorFlag = ctx->Pixel.MapColorFlag;
564 ctx->Pixel.RedScale = 1.0F;
565 ctx->Pixel.RedBias = 0.0F;
566 ctx->Pixel.GreenScale = 1.0F;
567 ctx->Pixel.GreenBias = 0.0F;
568 ctx->Pixel.BlueScale = 1.0F;
569 ctx->Pixel.BlueBias = 0.0F;
570 ctx->Pixel.AlphaScale = 1.0F;
571 ctx->Pixel.AlphaBias = 0.0F;
572 ctx->Pixel.MapColorFlag = GL_FALSE;
573 /* XXX more state */
574 ctx->NewState |=_NEW_PIXEL;
575 }
576
577 if (state & MESA_META_RASTERIZATION) {
578 save->FrontPolygonMode = ctx->Polygon.FrontMode;
579 save->BackPolygonMode = ctx->Polygon.BackMode;
580 save->PolygonOffset = ctx->Polygon.OffsetFill;
581 save->PolygonSmooth = ctx->Polygon.SmoothFlag;
582 save->PolygonStipple = ctx->Polygon.StippleFlag;
583 save->PolygonCull = ctx->Polygon.CullFlag;
584 _mesa_PolygonMode(GL_FRONT_AND_BACK, GL_FILL);
585 _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL, GL_FALSE);
586 _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, GL_FALSE);
587 _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, GL_FALSE);
588 _mesa_set_enable(ctx, GL_CULL_FACE, GL_FALSE);
589 }
590
591 if (state & MESA_META_SCISSOR) {
592 save->Scissor = ctx->Scissor; /* struct copy */
593 _mesa_set_enable(ctx, GL_SCISSOR_TEST, GL_FALSE);
594 }
595
596 if (state & MESA_META_SHADER) {
597 int i;
598
599 if (ctx->Extensions.ARB_vertex_program) {
600 save->VertexProgramEnabled = ctx->VertexProgram.Enabled;
601 _mesa_reference_program(ctx, &save->VertexProgram,
602 ctx->VertexProgram.Current);
603 _mesa_set_enable(ctx, GL_VERTEX_PROGRAM_ARB, GL_FALSE);
604 }
605
606 if (ctx->Extensions.ARB_fragment_program) {
607 save->FragmentProgramEnabled = ctx->FragmentProgram.Enabled;
608 _mesa_reference_program(ctx, &save->FragmentProgram,
609 ctx->FragmentProgram.Current);
610 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_FALSE);
611 }
612
613 if (ctx->Extensions.ATI_fragment_shader) {
614 save->ATIFragmentShaderEnabled = ctx->ATIFragmentShader.Enabled;
615 _mesa_set_enable(ctx, GL_FRAGMENT_SHADER_ATI, GL_FALSE);
616 }
617
618 if (ctx->Pipeline.Current) {
619 _mesa_reference_pipeline_object(ctx, &save->Pipeline,
620 ctx->Pipeline.Current);
621 _mesa_BindProgramPipeline(0);
622 }
623
624 /* Save the shader state from ctx->Shader (instead of ctx->_Shader) so
625 * that we don't have to worry about the current pipeline state.
626 */
627 for (i = 0; i < MESA_SHADER_STAGES; i++) {
628 _mesa_reference_program(ctx, &save->Program[i],
629 ctx->Shader.CurrentProgram[i]);
630 }
631 _mesa_reference_shader_program(ctx, &save->ActiveShader,
632 ctx->Shader.ActiveProgram);
633
634 _mesa_UseProgram(0);
635 }
636
637 if (state & MESA_META_STENCIL_TEST) {
638 save->Stencil = ctx->Stencil; /* struct copy */
639 if (ctx->Stencil.Enabled)
640 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_FALSE);
641 /* NOTE: other stencil state not reset */
642 }
643
644 if (state & MESA_META_TEXTURE) {
645 GLuint u, tgt;
646
647 save->ActiveUnit = ctx->Texture.CurrentUnit;
648 save->EnvMode = ctx->Texture.FixedFuncUnit[0].EnvMode;
649
650 /* Disable all texture units */
651 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
652 save->TexEnabled[u] = ctx->Texture.FixedFuncUnit[u].Enabled;
653 save->TexGenEnabled[u] = ctx->Texture.FixedFuncUnit[u].TexGenEnabled;
654 if (ctx->Texture.FixedFuncUnit[u].Enabled ||
655 ctx->Texture.FixedFuncUnit[u].TexGenEnabled) {
656 _mesa_ActiveTexture(GL_TEXTURE0 + u);
657 _mesa_set_enable(ctx, GL_TEXTURE_2D, GL_FALSE);
658 if (ctx->Extensions.ARB_texture_cube_map)
659 _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP, GL_FALSE);
660
661 _mesa_set_enable(ctx, GL_TEXTURE_1D, GL_FALSE);
662 _mesa_set_enable(ctx, GL_TEXTURE_3D, GL_FALSE);
663 if (ctx->Extensions.NV_texture_rectangle)
664 _mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE, GL_FALSE);
665 _mesa_set_enable(ctx, GL_TEXTURE_GEN_S, GL_FALSE);
666 _mesa_set_enable(ctx, GL_TEXTURE_GEN_T, GL_FALSE);
667 _mesa_set_enable(ctx, GL_TEXTURE_GEN_R, GL_FALSE);
668 _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q, GL_FALSE);
669 }
670 }
671
672 /* save current texture objects for unit[0] only */
673 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
674 _mesa_reference_texobj(&save->CurrentTexture[tgt],
675 ctx->Texture.Unit[0].CurrentTex[tgt]);
676 }
677
678 /* set defaults for unit[0] */
679 _mesa_ActiveTexture(GL_TEXTURE0);
680 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
681 }
682
683 if (state & MESA_META_TRANSFORM) {
684 GLuint activeTexture = ctx->Texture.CurrentUnit;
685 memcpy(save->ModelviewMatrix, ctx->ModelviewMatrixStack.Top->m,
686 16 * sizeof(GLfloat));
687 memcpy(save->ProjectionMatrix, ctx->ProjectionMatrixStack.Top->m,
688 16 * sizeof(GLfloat));
689 memcpy(save->TextureMatrix, ctx->TextureMatrixStack[0].Top->m,
690 16 * sizeof(GLfloat));
691 save->MatrixMode = ctx->Transform.MatrixMode;
692 /* set 1:1 vertex:pixel coordinate transform */
693 _mesa_ActiveTexture(GL_TEXTURE0);
694 _mesa_MatrixMode(GL_TEXTURE);
695 _mesa_LoadIdentity();
696 _mesa_ActiveTexture(GL_TEXTURE0 + activeTexture);
697 _mesa_MatrixMode(GL_MODELVIEW);
698 _mesa_LoadIdentity();
699 _mesa_MatrixMode(GL_PROJECTION);
700 _mesa_LoadIdentity();
701
702 /* glOrtho with width = 0 or height = 0 generates GL_INVALID_VALUE.
703 * This can occur when there is no draw buffer.
704 */
705 if (ctx->DrawBuffer->Width != 0 && ctx->DrawBuffer->Height != 0)
706 _mesa_Ortho(0.0, ctx->DrawBuffer->Width,
707 0.0, ctx->DrawBuffer->Height,
708 -1.0, 1.0);
709
710 if (ctx->Extensions.ARB_clip_control) {
711 save->ClipOrigin = ctx->Transform.ClipOrigin;
712 save->ClipDepthMode = ctx->Transform.ClipDepthMode;
713 _mesa_ClipControl(GL_LOWER_LEFT, GL_NEGATIVE_ONE_TO_ONE);
714 }
715 }
716
717 if (state & MESA_META_CLIP) {
718 GLbitfield mask;
719 save->ClipPlanesEnabled = ctx->Transform.ClipPlanesEnabled;
720 mask = ctx->Transform.ClipPlanesEnabled;
721 while (mask) {
722 const int i = u_bit_scan(&mask);
723 _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_FALSE);
724 }
725 }
726
727 if (state & MESA_META_VERTEX) {
728 /* save vertex array object state */
729 _mesa_reference_vao(ctx, &save->VAO,
730 ctx->Array.VAO);
731 /* set some default state? */
732 }
733
734 if (state & MESA_META_VIEWPORT) {
735 /* save viewport state */
736 save->ViewportX = ctx->ViewportArray[0].X;
737 save->ViewportY = ctx->ViewportArray[0].Y;
738 save->ViewportW = ctx->ViewportArray[0].Width;
739 save->ViewportH = ctx->ViewportArray[0].Height;
740 /* set viewport to match window size */
741 if (ctx->ViewportArray[0].X != 0 ||
742 ctx->ViewportArray[0].Y != 0 ||
743 ctx->ViewportArray[0].Width != (float) ctx->DrawBuffer->Width ||
744 ctx->ViewportArray[0].Height != (float) ctx->DrawBuffer->Height) {
745 _mesa_set_viewport(ctx, 0, 0, 0,
746 ctx->DrawBuffer->Width, ctx->DrawBuffer->Height);
747 }
748 /* save depth range state */
749 save->DepthNear = ctx->ViewportArray[0].Near;
750 save->DepthFar = ctx->ViewportArray[0].Far;
751 /* set depth range to default */
752 _mesa_set_depth_range(ctx, 0, 0.0, 1.0);
753 }
754
755 if (state & MESA_META_CLAMP_FRAGMENT_COLOR) {
756 save->ClampFragmentColor = ctx->Color.ClampFragmentColor;
757
758 /* Generally in here we want to do clamping according to whether
759 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
760 * regardless of the internal implementation of the metaops.
761 */
762 if (ctx->Color.ClampFragmentColor != GL_TRUE &&
763 ctx->Extensions.ARB_color_buffer_float)
764 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
765 }
766
767 if (state & MESA_META_CLAMP_VERTEX_COLOR) {
768 save->ClampVertexColor = ctx->Light.ClampVertexColor;
769
770 /* Generally in here we never want vertex color clamping --
771 * result clamping is only dependent on fragment clamping.
772 */
773 if (ctx->Extensions.ARB_color_buffer_float)
774 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR, GL_FALSE);
775 }
776
777 if (state & MESA_META_CONDITIONAL_RENDER) {
778 save->CondRenderQuery = ctx->Query.CondRenderQuery;
779 save->CondRenderMode = ctx->Query.CondRenderMode;
780
781 if (ctx->Query.CondRenderQuery)
782 _mesa_EndConditionalRender();
783 }
784
785 if (state & MESA_META_SELECT_FEEDBACK) {
786 save->RenderMode = ctx->RenderMode;
787 if (ctx->RenderMode == GL_SELECT) {
788 save->Select = ctx->Select; /* struct copy */
789 _mesa_RenderMode(GL_RENDER);
790 } else if (ctx->RenderMode == GL_FEEDBACK) {
791 save->Feedback = ctx->Feedback; /* struct copy */
792 _mesa_RenderMode(GL_RENDER);
793 }
794 }
795
796 if (state & MESA_META_MULTISAMPLE) {
797 save->Multisample = ctx->Multisample; /* struct copy */
798
799 if (ctx->Multisample.Enabled)
800 _mesa_set_multisample(ctx, GL_FALSE);
801 if (ctx->Multisample.SampleCoverage)
802 _mesa_set_enable(ctx, GL_SAMPLE_COVERAGE, GL_FALSE);
803 if (ctx->Multisample.SampleAlphaToCoverage)
804 _mesa_set_enable(ctx, GL_SAMPLE_ALPHA_TO_COVERAGE, GL_FALSE);
805 if (ctx->Multisample.SampleAlphaToOne)
806 _mesa_set_enable(ctx, GL_SAMPLE_ALPHA_TO_ONE, GL_FALSE);
807 if (ctx->Multisample.SampleShading)
808 _mesa_set_enable(ctx, GL_SAMPLE_SHADING, GL_FALSE);
809 if (ctx->Multisample.SampleMask)
810 _mesa_set_enable(ctx, GL_SAMPLE_MASK, GL_FALSE);
811 }
812
813 if (state & MESA_META_FRAMEBUFFER_SRGB) {
814 save->sRGBEnabled = ctx->Color.sRGBEnabled;
815 if (ctx->Color.sRGBEnabled)
816 _mesa_set_framebuffer_srgb(ctx, GL_FALSE);
817 }
818
819 if (state & MESA_META_DRAW_BUFFERS) {
820 struct gl_framebuffer *fb = ctx->DrawBuffer;
821 memcpy(save->ColorDrawBuffers, fb->ColorDrawBuffer,
822 sizeof(save->ColorDrawBuffers));
823 }
824
825 /* misc */
826 {
827 save->Lighting = ctx->Light.Enabled;
828 if (ctx->Light.Enabled)
829 _mesa_set_enable(ctx, GL_LIGHTING, GL_FALSE);
830 save->RasterDiscard = ctx->RasterDiscard;
831 if (ctx->RasterDiscard)
832 _mesa_set_enable(ctx, GL_RASTERIZER_DISCARD, GL_FALSE);
833
834 _mesa_reference_framebuffer(&save->DrawBuffer, ctx->DrawBuffer);
835 _mesa_reference_framebuffer(&save->ReadBuffer, ctx->ReadBuffer);
836 }
837 }
838
839
840 /**
841 * Leave meta state. This is like a light-weight version of glPopAttrib().
842 */
843 void
844 _mesa_meta_end(struct gl_context *ctx)
845 {
846 assert(ctx->Meta->SaveStackDepth > 0);
847
848 struct save_state *save = &ctx->Meta->Save[ctx->Meta->SaveStackDepth - 1];
849 const GLbitfield state = save->SavedState;
850 int i;
851
852 /* Grab the result of the old occlusion query before starting it again. The
853 * old result is added to the result of the new query so the driver will
854 * continue adding where it left off. */
855 if (state & MESA_META_OCCLUSION_QUERY) {
856 if (save->CurrentOcclusionObject) {
857 struct gl_query_object *q = save->CurrentOcclusionObject;
858 GLuint64EXT result;
859 if (!q->Ready)
860 ctx->Driver.WaitQuery(ctx, q);
861 result = q->Result;
862 _mesa_BeginQuery(q->Target, q->Id);
863 ctx->Query.CurrentOcclusionObject->Result += result;
864 }
865 }
866
867 if (state & MESA_META_ALPHA_TEST) {
868 if (ctx->Color.AlphaEnabled != save->AlphaEnabled)
869 _mesa_set_enable(ctx, GL_ALPHA_TEST, save->AlphaEnabled);
870 _mesa_AlphaFunc(save->AlphaFunc, save->AlphaRef);
871 }
872
873 if (state & MESA_META_BLEND) {
874 if (ctx->Color.BlendEnabled != save->BlendEnabled) {
875 if (ctx->Extensions.EXT_draw_buffers2) {
876 GLuint i;
877 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
878 _mesa_set_enablei(ctx, GL_BLEND, i, (save->BlendEnabled >> i) & 1);
879 }
880 }
881 else {
882 _mesa_set_enable(ctx, GL_BLEND, (save->BlendEnabled & 1));
883 }
884 }
885 if (ctx->Color.ColorLogicOpEnabled != save->ColorLogicOpEnabled)
886 _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP, save->ColorLogicOpEnabled);
887 }
888
889 if (state & MESA_META_DITHER)
890 _mesa_set_enable(ctx, GL_DITHER, save->DitherFlag);
891
892 if (state & MESA_META_COLOR_MASK) {
893 GLuint i;
894 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
895 if (GET_COLORMASK(ctx->Color.ColorMask, i) !=
896 GET_COLORMASK(save->ColorMask, i)) {
897 if (i == 0) {
898 _mesa_ColorMask(GET_COLORMASK_BIT(save->ColorMask, i, 0),
899 GET_COLORMASK_BIT(save->ColorMask, i, 1),
900 GET_COLORMASK_BIT(save->ColorMask, i, 2),
901 GET_COLORMASK_BIT(save->ColorMask, i, 3));
902 }
903 else {
904 _mesa_ColorMaski(i,
905 GET_COLORMASK_BIT(save->ColorMask, i, 0),
906 GET_COLORMASK_BIT(save->ColorMask, i, 1),
907 GET_COLORMASK_BIT(save->ColorMask, i, 2),
908 GET_COLORMASK_BIT(save->ColorMask, i, 3));
909 }
910 }
911 }
912 }
913
914 if (state & MESA_META_DEPTH_TEST) {
915 if (ctx->Depth.Test != save->Depth.Test)
916 _mesa_set_enable(ctx, GL_DEPTH_TEST, save->Depth.Test);
917 _mesa_DepthFunc(save->Depth.Func);
918 _mesa_DepthMask(save->Depth.Mask);
919 }
920
921 if (state & MESA_META_FOG) {
922 _mesa_set_enable(ctx, GL_FOG, save->Fog);
923 }
924
925 if (state & MESA_META_PIXEL_STORE) {
926 ctx->Pack = save->Pack;
927 ctx->Unpack = save->Unpack;
928 }
929
930 if (state & MESA_META_PIXEL_TRANSFER) {
931 ctx->Pixel.RedScale = save->RedScale;
932 ctx->Pixel.RedBias = save->RedBias;
933 ctx->Pixel.GreenScale = save->GreenScale;
934 ctx->Pixel.GreenBias = save->GreenBias;
935 ctx->Pixel.BlueScale = save->BlueScale;
936 ctx->Pixel.BlueBias = save->BlueBias;
937 ctx->Pixel.AlphaScale = save->AlphaScale;
938 ctx->Pixel.AlphaBias = save->AlphaBias;
939 ctx->Pixel.MapColorFlag = save->MapColorFlag;
940 /* XXX more state */
941 ctx->NewState |=_NEW_PIXEL;
942 }
943
944 if (state & MESA_META_RASTERIZATION) {
945 _mesa_PolygonMode(GL_FRONT, save->FrontPolygonMode);
946 _mesa_PolygonMode(GL_BACK, save->BackPolygonMode);
947 _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, save->PolygonStipple);
948 _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, save->PolygonSmooth);
949 _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL, save->PolygonOffset);
950 _mesa_set_enable(ctx, GL_CULL_FACE, save->PolygonCull);
951 }
952
953 if (state & MESA_META_SCISSOR) {
954 unsigned i;
955
956 for (i = 0; i < ctx->Const.MaxViewports; i++) {
957 _mesa_set_scissor(ctx, i,
958 save->Scissor.ScissorArray[i].X,
959 save->Scissor.ScissorArray[i].Y,
960 save->Scissor.ScissorArray[i].Width,
961 save->Scissor.ScissorArray[i].Height);
962 _mesa_set_enablei(ctx, GL_SCISSOR_TEST, i,
963 (save->Scissor.EnableFlags >> i) & 1);
964 }
965 }
966
967 if (state & MESA_META_SHADER) {
968 bool any_shader;
969
970 if (ctx->Extensions.ARB_vertex_program) {
971 _mesa_set_enable(ctx, GL_VERTEX_PROGRAM_ARB,
972 save->VertexProgramEnabled);
973 _mesa_reference_program(ctx, &ctx->VertexProgram.Current,
974 save->VertexProgram);
975 _mesa_reference_program(ctx, &save->VertexProgram, NULL);
976 }
977
978 if (ctx->Extensions.ARB_fragment_program) {
979 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB,
980 save->FragmentProgramEnabled);
981 _mesa_reference_program(ctx, &ctx->FragmentProgram.Current,
982 save->FragmentProgram);
983 _mesa_reference_program(ctx, &save->FragmentProgram, NULL);
984 }
985
986 if (ctx->Extensions.ATI_fragment_shader) {
987 _mesa_set_enable(ctx, GL_FRAGMENT_SHADER_ATI,
988 save->ATIFragmentShaderEnabled);
989 }
990
991 any_shader = false;
992 for (i = 0; i < MESA_SHADER_STAGES; i++) {
993 /* It is safe to call _mesa_use_program even if the extension
994 * necessary for that program state is not supported. In that case,
995 * the saved program object must be NULL and the currently bound
996 * program object must be NULL. _mesa_use_program is a no-op
997 * in that case.
998 */
999 _mesa_use_program(ctx, i, NULL, save->Program[i], &ctx->Shader);
1000
1001 /* Do this *before* killing the reference. :)
1002 */
1003 if (save->Program[i] != NULL)
1004 any_shader = true;
1005
1006 _mesa_reference_program(ctx, &save->Program[i], NULL);
1007 }
1008
1009 _mesa_reference_shader_program(ctx, &ctx->Shader.ActiveProgram,
1010 save->ActiveShader);
1011 _mesa_reference_shader_program(ctx, &save->ActiveShader, NULL);
1012
1013 /* If there were any stages set with programs, use ctx->Shader as the
1014 * current shader state. Otherwise, use Pipeline.Default. The pipeline
1015 * hasn't been restored yet, and that may modify ctx->_Shader further.
1016 */
1017 if (any_shader)
1018 _mesa_reference_pipeline_object(ctx, &ctx->_Shader,
1019 &ctx->Shader);
1020 else
1021 _mesa_reference_pipeline_object(ctx, &ctx->_Shader,
1022 ctx->Pipeline.Default);
1023
1024 if (save->Pipeline) {
1025 _mesa_bind_pipeline(ctx, save->Pipeline);
1026
1027 _mesa_reference_pipeline_object(ctx, &save->Pipeline, NULL);
1028 }
1029
1030 _mesa_update_vertex_processing_mode(ctx);
1031 }
1032
1033 if (state & MESA_META_STENCIL_TEST) {
1034 const struct gl_stencil_attrib *stencil = &save->Stencil;
1035
1036 _mesa_set_enable(ctx, GL_STENCIL_TEST, stencil->Enabled);
1037 _mesa_ClearStencil(stencil->Clear);
1038 if (ctx->Extensions.EXT_stencil_two_side) {
1039 _mesa_set_enable(ctx, GL_STENCIL_TEST_TWO_SIDE_EXT,
1040 stencil->TestTwoSide);
1041 _mesa_ActiveStencilFaceEXT(stencil->ActiveFace
1042 ? GL_BACK : GL_FRONT);
1043 }
1044 /* front state */
1045 _mesa_StencilFuncSeparate(GL_FRONT,
1046 stencil->Function[0],
1047 stencil->Ref[0],
1048 stencil->ValueMask[0]);
1049 _mesa_StencilMaskSeparate(GL_FRONT, stencil->WriteMask[0]);
1050 _mesa_StencilOpSeparate(GL_FRONT, stencil->FailFunc[0],
1051 stencil->ZFailFunc[0],
1052 stencil->ZPassFunc[0]);
1053 /* back state */
1054 _mesa_StencilFuncSeparate(GL_BACK,
1055 stencil->Function[1],
1056 stencil->Ref[1],
1057 stencil->ValueMask[1]);
1058 _mesa_StencilMaskSeparate(GL_BACK, stencil->WriteMask[1]);
1059 _mesa_StencilOpSeparate(GL_BACK, stencil->FailFunc[1],
1060 stencil->ZFailFunc[1],
1061 stencil->ZPassFunc[1]);
1062 }
1063
1064 if (state & MESA_META_TEXTURE) {
1065 GLuint u, tgt;
1066
1067 assert(ctx->Texture.CurrentUnit == 0);
1068
1069 /* restore texenv for unit[0] */
1070 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, save->EnvMode);
1071
1072 /* restore texture objects for unit[0] only */
1073 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
1074 if (ctx->Texture.Unit[0].CurrentTex[tgt] != save->CurrentTexture[tgt]) {
1075 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1076 _mesa_reference_texobj(&ctx->Texture.Unit[0].CurrentTex[tgt],
1077 save->CurrentTexture[tgt]);
1078 }
1079 _mesa_reference_texobj(&save->CurrentTexture[tgt], NULL);
1080 }
1081
1082 /* Restore fixed function texture enables, texgen */
1083 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
1084 if (ctx->Texture.FixedFuncUnit[u].Enabled != save->TexEnabled[u]) {
1085 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1086 ctx->Texture.FixedFuncUnit[u].Enabled = save->TexEnabled[u];
1087 }
1088
1089 if (ctx->Texture.FixedFuncUnit[u].TexGenEnabled != save->TexGenEnabled[u]) {
1090 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1091 ctx->Texture.FixedFuncUnit[u].TexGenEnabled = save->TexGenEnabled[u];
1092 }
1093 }
1094
1095 /* restore current unit state */
1096 _mesa_ActiveTexture(GL_TEXTURE0 + save->ActiveUnit);
1097 }
1098
1099 if (state & MESA_META_TRANSFORM) {
1100 GLuint activeTexture = ctx->Texture.CurrentUnit;
1101 _mesa_ActiveTexture(GL_TEXTURE0);
1102 _mesa_MatrixMode(GL_TEXTURE);
1103 _mesa_LoadMatrixf(save->TextureMatrix);
1104 _mesa_ActiveTexture(GL_TEXTURE0 + activeTexture);
1105
1106 _mesa_MatrixMode(GL_MODELVIEW);
1107 _mesa_LoadMatrixf(save->ModelviewMatrix);
1108
1109 _mesa_MatrixMode(GL_PROJECTION);
1110 _mesa_LoadMatrixf(save->ProjectionMatrix);
1111
1112 _mesa_MatrixMode(save->MatrixMode);
1113
1114 if (ctx->Extensions.ARB_clip_control)
1115 _mesa_ClipControl(save->ClipOrigin, save->ClipDepthMode);
1116 }
1117
1118 if (state & MESA_META_CLIP) {
1119 GLbitfield mask = save->ClipPlanesEnabled;
1120 while (mask) {
1121 const int i = u_bit_scan(&mask);
1122 _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_TRUE);
1123 }
1124 }
1125
1126 if (state & MESA_META_VERTEX) {
1127 /* restore vertex array object */
1128 _mesa_BindVertexArray(save->VAO->Name);
1129 _mesa_reference_vao(ctx, &save->VAO, NULL);
1130 }
1131
1132 if (state & MESA_META_VIEWPORT) {
1133 if (save->ViewportX != ctx->ViewportArray[0].X ||
1134 save->ViewportY != ctx->ViewportArray[0].Y ||
1135 save->ViewportW != ctx->ViewportArray[0].Width ||
1136 save->ViewportH != ctx->ViewportArray[0].Height) {
1137 _mesa_set_viewport(ctx, 0, save->ViewportX, save->ViewportY,
1138 save->ViewportW, save->ViewportH);
1139 }
1140 _mesa_set_depth_range(ctx, 0, save->DepthNear, save->DepthFar);
1141 }
1142
1143 if (state & MESA_META_CLAMP_FRAGMENT_COLOR &&
1144 ctx->Extensions.ARB_color_buffer_float) {
1145 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR, save->ClampFragmentColor);
1146 }
1147
1148 if (state & MESA_META_CLAMP_VERTEX_COLOR &&
1149 ctx->Extensions.ARB_color_buffer_float) {
1150 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR, save->ClampVertexColor);
1151 }
1152
1153 if (state & MESA_META_CONDITIONAL_RENDER) {
1154 if (save->CondRenderQuery)
1155 _mesa_BeginConditionalRender(save->CondRenderQuery->Id,
1156 save->CondRenderMode);
1157 }
1158
1159 if (state & MESA_META_SELECT_FEEDBACK) {
1160 if (save->RenderMode == GL_SELECT) {
1161 _mesa_RenderMode(GL_SELECT);
1162 ctx->Select = save->Select;
1163 } else if (save->RenderMode == GL_FEEDBACK) {
1164 _mesa_RenderMode(GL_FEEDBACK);
1165 ctx->Feedback = save->Feedback;
1166 }
1167 }
1168
1169 if (state & MESA_META_MULTISAMPLE) {
1170 struct gl_multisample_attrib *ctx_ms = &ctx->Multisample;
1171 struct gl_multisample_attrib *save_ms = &save->Multisample;
1172
1173 if (ctx_ms->Enabled != save_ms->Enabled)
1174 _mesa_set_multisample(ctx, save_ms->Enabled);
1175 if (ctx_ms->SampleCoverage != save_ms->SampleCoverage)
1176 _mesa_set_enable(ctx, GL_SAMPLE_COVERAGE, save_ms->SampleCoverage);
1177 if (ctx_ms->SampleAlphaToCoverage != save_ms->SampleAlphaToCoverage)
1178 _mesa_set_enable(ctx, GL_SAMPLE_ALPHA_TO_COVERAGE, save_ms->SampleAlphaToCoverage);
1179 if (ctx_ms->SampleAlphaToOne != save_ms->SampleAlphaToOne)
1180 _mesa_set_enable(ctx, GL_SAMPLE_ALPHA_TO_ONE, save_ms->SampleAlphaToOne);
1181 if (ctx_ms->SampleCoverageValue != save_ms->SampleCoverageValue ||
1182 ctx_ms->SampleCoverageInvert != save_ms->SampleCoverageInvert) {
1183 _mesa_SampleCoverage(save_ms->SampleCoverageValue,
1184 save_ms->SampleCoverageInvert);
1185 }
1186 if (ctx_ms->SampleShading != save_ms->SampleShading)
1187 _mesa_set_enable(ctx, GL_SAMPLE_SHADING, save_ms->SampleShading);
1188 if (ctx_ms->SampleMask != save_ms->SampleMask)
1189 _mesa_set_enable(ctx, GL_SAMPLE_MASK, save_ms->SampleMask);
1190 if (ctx_ms->SampleMaskValue != save_ms->SampleMaskValue)
1191 _mesa_SampleMaski(0, save_ms->SampleMaskValue);
1192 if (ctx_ms->MinSampleShadingValue != save_ms->MinSampleShadingValue)
1193 _mesa_MinSampleShading(save_ms->MinSampleShadingValue);
1194 }
1195
1196 if (state & MESA_META_FRAMEBUFFER_SRGB) {
1197 if (ctx->Color.sRGBEnabled != save->sRGBEnabled)
1198 _mesa_set_framebuffer_srgb(ctx, save->sRGBEnabled);
1199 }
1200
1201 /* misc */
1202 if (save->Lighting) {
1203 _mesa_set_enable(ctx, GL_LIGHTING, GL_TRUE);
1204 }
1205 if (save->RasterDiscard) {
1206 _mesa_set_enable(ctx, GL_RASTERIZER_DISCARD, GL_TRUE);
1207 }
1208 if (save->TransformFeedbackNeedsResume)
1209 _mesa_ResumeTransformFeedback();
1210
1211 _mesa_bind_framebuffers(ctx, save->DrawBuffer, save->ReadBuffer);
1212 _mesa_reference_framebuffer(&save->DrawBuffer, NULL);
1213 _mesa_reference_framebuffer(&save->ReadBuffer, NULL);
1214
1215 if (state & MESA_META_DRAW_BUFFERS) {
1216 _mesa_drawbuffers(ctx, ctx->DrawBuffer, ctx->Const.MaxDrawBuffers,
1217 save->ColorDrawBuffers, NULL);
1218 }
1219
1220 ctx->Meta->SaveStackDepth--;
1221
1222 ctx->API = save->API;
1223 ctx->Extensions.Version = save->ExtensionsVersion;
1224 }
1225
1226
1227 /**
1228 * Convert Z from a normalized value in the range [0, 1] to an object-space
1229 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1230 * default/identity ortho projection results in the original Z value.
1231 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1232 * value comes from the clear value or raster position.
1233 */
1234 static inline GLfloat
1235 invert_z(GLfloat normZ)
1236 {
1237 GLfloat objZ = 1.0f - 2.0f * normZ;
1238 return objZ;
1239 }
1240
1241
1242 /**
1243 * One-time init for a temp_texture object.
1244 * Choose tex target, compute max tex size, etc.
1245 */
1246 static void
1247 init_temp_texture(struct gl_context *ctx, struct temp_texture *tex)
1248 {
1249 /* prefer texture rectangle */
1250 if (_mesa_is_desktop_gl(ctx) && ctx->Extensions.NV_texture_rectangle) {
1251 tex->Target = GL_TEXTURE_RECTANGLE;
1252 tex->MaxSize = ctx->Const.MaxTextureRectSize;
1253 tex->NPOT = GL_TRUE;
1254 }
1255 else {
1256 /* use 2D texture, NPOT if possible */
1257 tex->Target = GL_TEXTURE_2D;
1258 tex->MaxSize = ctx->Const.MaxTextureSize;
1259 tex->NPOT = ctx->Extensions.ARB_texture_non_power_of_two;
1260 }
1261 tex->MinSize = 16; /* 16 x 16 at least */
1262 assert(tex->MaxSize > 0);
1263
1264 tex->tex_obj = ctx->Driver.NewTextureObject(ctx, 0xDEADBEEF, tex->Target);
1265 }
1266
1267 static void
1268 cleanup_temp_texture(struct gl_context *ctx, struct temp_texture *tex)
1269 {
1270 _mesa_delete_nameless_texture(ctx, tex->tex_obj);
1271 tex->tex_obj = NULL;
1272 }
1273
1274
1275 /**
1276 * Return pointer to temp_texture info for non-bitmap ops.
1277 * This does some one-time init if needed.
1278 */
1279 struct temp_texture *
1280 _mesa_meta_get_temp_texture(struct gl_context *ctx)
1281 {
1282 struct temp_texture *tex = &ctx->Meta->TempTex;
1283
1284 if (tex->tex_obj == NULL) {
1285 init_temp_texture(ctx, tex);
1286 }
1287
1288 return tex;
1289 }
1290
1291
1292 /**
1293 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1294 * We use a separate texture for bitmaps to reduce texture
1295 * allocation/deallocation.
1296 */
1297 static struct temp_texture *
1298 get_bitmap_temp_texture(struct gl_context *ctx)
1299 {
1300 struct temp_texture *tex = &ctx->Meta->Bitmap.Tex;
1301
1302 if (tex->tex_obj == NULL) {
1303 init_temp_texture(ctx, tex);
1304 }
1305
1306 return tex;
1307 }
1308
1309 /**
1310 * Return pointer to depth temp_texture.
1311 * This does some one-time init if needed.
1312 */
1313 struct temp_texture *
1314 _mesa_meta_get_temp_depth_texture(struct gl_context *ctx)
1315 {
1316 struct temp_texture *tex = &ctx->Meta->Blit.depthTex;
1317
1318 if (tex->tex_obj == NULL) {
1319 init_temp_texture(ctx, tex);
1320 }
1321
1322 return tex;
1323 }
1324
1325 /**
1326 * Compute the width/height of texture needed to draw an image of the
1327 * given size. Return a flag indicating whether the current texture
1328 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1329 * allocated (glTexImage2D).
1330 * Also, compute s/t texcoords for drawing.
1331 *
1332 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1333 */
1334 GLboolean
1335 _mesa_meta_alloc_texture(struct temp_texture *tex,
1336 GLsizei width, GLsizei height, GLenum intFormat)
1337 {
1338 GLboolean newTex = GL_FALSE;
1339
1340 assert(width <= tex->MaxSize);
1341 assert(height <= tex->MaxSize);
1342
1343 if (width > tex->Width ||
1344 height > tex->Height ||
1345 intFormat != tex->IntFormat) {
1346 /* alloc new texture (larger or different format) */
1347
1348 if (tex->NPOT) {
1349 /* use non-power of two size */
1350 tex->Width = MAX2(tex->MinSize, width);
1351 tex->Height = MAX2(tex->MinSize, height);
1352 }
1353 else {
1354 /* find power of two size */
1355 GLsizei w, h;
1356 w = h = tex->MinSize;
1357 while (w < width)
1358 w *= 2;
1359 while (h < height)
1360 h *= 2;
1361 tex->Width = w;
1362 tex->Height = h;
1363 }
1364
1365 tex->IntFormat = intFormat;
1366
1367 newTex = GL_TRUE;
1368 }
1369
1370 /* compute texcoords */
1371 if (tex->Target == GL_TEXTURE_RECTANGLE) {
1372 tex->Sright = (GLfloat) width;
1373 tex->Ttop = (GLfloat) height;
1374 }
1375 else {
1376 tex->Sright = (GLfloat) width / tex->Width;
1377 tex->Ttop = (GLfloat) height / tex->Height;
1378 }
1379
1380 return newTex;
1381 }
1382
1383
1384 /**
1385 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1386 */
1387 void
1388 _mesa_meta_setup_copypix_texture(struct gl_context *ctx,
1389 struct temp_texture *tex,
1390 GLint srcX, GLint srcY,
1391 GLsizei width, GLsizei height,
1392 GLenum intFormat,
1393 GLenum filter)
1394 {
1395 bool newTex;
1396
1397 _mesa_bind_texture(ctx, tex->Target, tex->tex_obj);
1398 _mesa_texture_parameteriv(ctx, tex->tex_obj, GL_TEXTURE_MIN_FILTER,
1399 (GLint *) &filter, false);
1400 _mesa_texture_parameteriv(ctx, tex->tex_obj, GL_TEXTURE_MAG_FILTER,
1401 (GLint *) &filter, false);
1402 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1403
1404 newTex = _mesa_meta_alloc_texture(tex, width, height, intFormat);
1405
1406 /* copy framebuffer image to texture */
1407 if (newTex) {
1408 /* create new tex image */
1409 if (tex->Width == width && tex->Height == height) {
1410 /* create new tex with framebuffer data */
1411 _mesa_CopyTexImage2D(tex->Target, 0, tex->IntFormat,
1412 srcX, srcY, width, height, 0);
1413 }
1414 else {
1415 /* create empty texture */
1416 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
1417 tex->Width, tex->Height, 0,
1418 intFormat, GL_UNSIGNED_BYTE, NULL);
1419 /* load image */
1420 _mesa_CopyTexSubImage2D(tex->Target, 0,
1421 0, 0, srcX, srcY, width, height);
1422 }
1423 }
1424 else {
1425 /* replace existing tex image */
1426 _mesa_CopyTexSubImage2D(tex->Target, 0,
1427 0, 0, srcX, srcY, width, height);
1428 }
1429 }
1430
1431
1432 /**
1433 * Setup/load texture for glDrawPixels.
1434 */
1435 void
1436 _mesa_meta_setup_drawpix_texture(struct gl_context *ctx,
1437 struct temp_texture *tex,
1438 GLboolean newTex,
1439 GLsizei width, GLsizei height,
1440 GLenum format, GLenum type,
1441 const GLvoid *pixels)
1442 {
1443 /* GLint so the compiler won't complain about type signedness mismatch in
1444 * the call to _mesa_texture_parameteriv below.
1445 */
1446 static const GLint filter = GL_NEAREST;
1447
1448 _mesa_bind_texture(ctx, tex->Target, tex->tex_obj);
1449 _mesa_texture_parameteriv(ctx, tex->tex_obj, GL_TEXTURE_MIN_FILTER, &filter,
1450 false);
1451 _mesa_texture_parameteriv(ctx, tex->tex_obj, GL_TEXTURE_MAG_FILTER, &filter,
1452 false);
1453 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1454
1455 /* copy pixel data to texture */
1456 if (newTex) {
1457 /* create new tex image */
1458 if (tex->Width == width && tex->Height == height) {
1459 /* create new tex and load image data */
1460 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
1461 tex->Width, tex->Height, 0, format, type, pixels);
1462 }
1463 else {
1464 struct gl_buffer_object *save_unpack_obj = NULL;
1465
1466 _mesa_reference_buffer_object(ctx, &save_unpack_obj,
1467 ctx->Unpack.BufferObj);
1468 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
1469 /* create empty texture */
1470 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
1471 tex->Width, tex->Height, 0, format, type, NULL);
1472 if (save_unpack_obj != NULL)
1473 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB,
1474 save_unpack_obj->Name);
1475 /* load image */
1476 _mesa_TexSubImage2D(tex->Target, 0,
1477 0, 0, width, height, format, type, pixels);
1478
1479 _mesa_reference_buffer_object(ctx, &save_unpack_obj, NULL);
1480 }
1481 }
1482 else {
1483 /* replace existing tex image */
1484 _mesa_TexSubImage2D(tex->Target, 0,
1485 0, 0, width, height, format, type, pixels);
1486 }
1487 }
1488
1489 void
1490 _mesa_meta_setup_ff_tnl_for_blit(struct gl_context *ctx,
1491 GLuint *VAO, struct gl_buffer_object **buf_obj,
1492 unsigned texcoord_size)
1493 {
1494 _mesa_meta_setup_vertex_objects(ctx, VAO, buf_obj, false, 2, texcoord_size,
1495 0);
1496
1497 /* setup projection matrix */
1498 _mesa_MatrixMode(GL_PROJECTION);
1499 _mesa_LoadIdentity();
1500 }
1501
1502 /**
1503 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1504 */
1505 void
1506 _mesa_meta_Clear(struct gl_context *ctx, GLbitfield buffers)
1507 {
1508 meta_clear(ctx, buffers, false);
1509 }
1510
1511 void
1512 _mesa_meta_glsl_Clear(struct gl_context *ctx, GLbitfield buffers)
1513 {
1514 meta_clear(ctx, buffers, true);
1515 }
1516
1517 static void
1518 meta_glsl_clear_init(struct gl_context *ctx, struct clear_state *clear)
1519 {
1520 const char *vs_source =
1521 "#extension GL_AMD_vertex_shader_layer : enable\n"
1522 "#extension GL_ARB_draw_instanced : enable\n"
1523 "#extension GL_ARB_explicit_attrib_location :enable\n"
1524 "layout(location = 0) in vec4 position;\n"
1525 "void main()\n"
1526 "{\n"
1527 "#ifdef GL_AMD_vertex_shader_layer\n"
1528 " gl_Layer = gl_InstanceID;\n"
1529 "#endif\n"
1530 " gl_Position = position;\n"
1531 "}\n";
1532 const char *fs_source =
1533 "#extension GL_ARB_explicit_attrib_location :enable\n"
1534 "#extension GL_ARB_explicit_uniform_location :enable\n"
1535 "layout(location = 0) uniform vec4 color;\n"
1536 "void main()\n"
1537 "{\n"
1538 " gl_FragColor = color;\n"
1539 "}\n";
1540 bool has_integer_textures;
1541
1542 _mesa_meta_setup_vertex_objects(ctx, &clear->VAO, &clear->buf_obj, true,
1543 3, 0, 0);
1544
1545 if (clear->ShaderProg != 0)
1546 return;
1547
1548 _mesa_meta_compile_and_link_program(ctx, vs_source, fs_source, "meta clear",
1549 &clear->ShaderProg);
1550
1551 has_integer_textures = _mesa_is_gles3(ctx) ||
1552 (_mesa_is_desktop_gl(ctx) && ctx->Const.GLSLVersion >= 130);
1553
1554 if (has_integer_textures) {
1555 void *shader_source_mem_ctx = ralloc_context(NULL);
1556 const char *vs_int_source =
1557 ralloc_asprintf(shader_source_mem_ctx,
1558 "#version 130\n"
1559 "#extension GL_AMD_vertex_shader_layer : enable\n"
1560 "#extension GL_ARB_draw_instanced : enable\n"
1561 "#extension GL_ARB_explicit_attrib_location :enable\n"
1562 "layout(location = 0) in vec4 position;\n"
1563 "void main()\n"
1564 "{\n"
1565 "#ifdef GL_AMD_vertex_shader_layer\n"
1566 " gl_Layer = gl_InstanceID;\n"
1567 "#endif\n"
1568 " gl_Position = position;\n"
1569 "}\n");
1570 const char *fs_int_source =
1571 ralloc_asprintf(shader_source_mem_ctx,
1572 "#version 130\n"
1573 "#extension GL_ARB_explicit_attrib_location :enable\n"
1574 "#extension GL_ARB_explicit_uniform_location :enable\n"
1575 "layout(location = 0) uniform ivec4 color;\n"
1576 "out ivec4 out_color;\n"
1577 "\n"
1578 "void main()\n"
1579 "{\n"
1580 " out_color = color;\n"
1581 "}\n");
1582
1583 _mesa_meta_compile_and_link_program(ctx, vs_int_source, fs_int_source,
1584 "integer clear",
1585 &clear->IntegerShaderProg);
1586 ralloc_free(shader_source_mem_ctx);
1587
1588 /* Note that user-defined out attributes get automatically assigned
1589 * locations starting from 0, so we don't need to explicitly
1590 * BindFragDataLocation to 0.
1591 */
1592 }
1593 }
1594
1595 static void
1596 meta_glsl_clear_cleanup(struct gl_context *ctx, struct clear_state *clear)
1597 {
1598 if (clear->VAO == 0)
1599 return;
1600 _mesa_DeleteVertexArrays(1, &clear->VAO);
1601 clear->VAO = 0;
1602 _mesa_reference_buffer_object(ctx, &clear->buf_obj, NULL);
1603 _mesa_reference_shader_program(ctx, &clear->ShaderProg, NULL);
1604
1605 if (clear->IntegerShaderProg) {
1606 _mesa_reference_shader_program(ctx, &clear->IntegerShaderProg, NULL);
1607 }
1608 }
1609
1610 static void
1611 meta_copypix_cleanup(struct gl_context *ctx, struct copypix_state *copypix)
1612 {
1613 if (copypix->VAO == 0)
1614 return;
1615 _mesa_DeleteVertexArrays(1, &copypix->VAO);
1616 copypix->VAO = 0;
1617 _mesa_reference_buffer_object(ctx, &copypix->buf_obj, NULL);
1618 }
1619
1620
1621 /**
1622 * Given a bitfield of BUFFER_BIT_x draw buffers, call glDrawBuffers to
1623 * set GL to only draw to those buffers.
1624 *
1625 * Since the bitfield has no associated order, the assignment of draw buffer
1626 * indices to color attachment indices is rather arbitrary.
1627 */
1628 void
1629 _mesa_meta_drawbuffers_from_bitfield(GLbitfield bits)
1630 {
1631 GLenum enums[MAX_DRAW_BUFFERS];
1632 int i = 0;
1633 int n;
1634
1635 /* This function is only legal for color buffer bitfields. */
1636 assert((bits & ~BUFFER_BITS_COLOR) == 0);
1637
1638 /* Make sure we don't overflow any arrays. */
1639 assert(util_bitcount(bits) <= MAX_DRAW_BUFFERS);
1640
1641 enums[0] = GL_NONE;
1642
1643 if (bits & BUFFER_BIT_FRONT_LEFT)
1644 enums[i++] = GL_FRONT_LEFT;
1645
1646 if (bits & BUFFER_BIT_FRONT_RIGHT)
1647 enums[i++] = GL_FRONT_RIGHT;
1648
1649 if (bits & BUFFER_BIT_BACK_LEFT)
1650 enums[i++] = GL_BACK_LEFT;
1651
1652 if (bits & BUFFER_BIT_BACK_RIGHT)
1653 enums[i++] = GL_BACK_RIGHT;
1654
1655 for (n = 0; n < MAX_COLOR_ATTACHMENTS; n++) {
1656 if (bits & (1 << (BUFFER_COLOR0 + n)))
1657 enums[i++] = GL_COLOR_ATTACHMENT0 + n;
1658 }
1659
1660 _mesa_DrawBuffers(i, enums);
1661 }
1662
1663 /**
1664 * Given a bitfield of BUFFER_BIT_x draw buffers, call glDrawBuffers to
1665 * set GL to only draw to those buffers. Also, update color masks to
1666 * reflect the new draw buffer ordering.
1667 */
1668 static void
1669 _mesa_meta_drawbuffers_and_colormask(struct gl_context *ctx, GLbitfield mask)
1670 {
1671 GLenum enums[MAX_DRAW_BUFFERS];
1672 GLubyte colormask[MAX_DRAW_BUFFERS][4];
1673 int num_bufs = 0;
1674
1675 /* This function is only legal for color buffer bitfields. */
1676 assert((mask & ~BUFFER_BITS_COLOR) == 0);
1677
1678 /* Make sure we don't overflow any arrays. */
1679 assert(util_bitcount(mask) <= MAX_DRAW_BUFFERS);
1680
1681 enums[0] = GL_NONE;
1682
1683 for (int i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
1684 gl_buffer_index b = ctx->DrawBuffer->_ColorDrawBufferIndexes[i];
1685 int colormask_idx = ctx->Extensions.EXT_draw_buffers2 ? i : 0;
1686
1687 if (b < 0 || !(mask & (1 << b)) ||
1688 GET_COLORMASK(ctx->Color.ColorMask, colormask_idx) == 0)
1689 continue;
1690
1691 switch (b) {
1692 case BUFFER_FRONT_LEFT:
1693 enums[num_bufs] = GL_FRONT_LEFT;
1694 break;
1695 case BUFFER_FRONT_RIGHT:
1696 enums[num_bufs] = GL_FRONT_RIGHT;
1697 break;
1698 case BUFFER_BACK_LEFT:
1699 enums[num_bufs] = GL_BACK_LEFT;
1700 break;
1701 case BUFFER_BACK_RIGHT:
1702 enums[num_bufs] = GL_BACK_RIGHT;
1703 break;
1704 default:
1705 assert(b >= BUFFER_COLOR0 && b <= BUFFER_COLOR7);
1706 enums[num_bufs] = GL_COLOR_ATTACHMENT0 + (b - BUFFER_COLOR0);
1707 break;
1708 }
1709
1710 for (int k = 0; k < 4; k++)
1711 colormask[num_bufs][k] = GET_COLORMASK_BIT(ctx->Color.ColorMask,
1712 colormask_idx, k);
1713
1714 num_bufs++;
1715 }
1716
1717 _mesa_DrawBuffers(num_bufs, enums);
1718
1719 for (int i = 0; i < num_bufs; i++) {
1720 _mesa_ColorMaski(i, colormask[i][0], colormask[i][1],
1721 colormask[i][2], colormask[i][3]);
1722 }
1723 }
1724
1725
1726 /**
1727 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1728 */
1729 static void
1730 meta_clear(struct gl_context *ctx, GLbitfield buffers, bool glsl)
1731 {
1732 struct clear_state *clear = &ctx->Meta->Clear;
1733 GLbitfield metaSave;
1734 const GLuint stencilMax = (1 << ctx->DrawBuffer->Visual.stencilBits) - 1;
1735 struct gl_framebuffer *fb = ctx->DrawBuffer;
1736 float x0, y0, x1, y1, z;
1737 struct vertex verts[4];
1738 int i;
1739
1740 metaSave = (MESA_META_ALPHA_TEST |
1741 MESA_META_BLEND |
1742 MESA_META_COLOR_MASK |
1743 MESA_META_DEPTH_TEST |
1744 MESA_META_RASTERIZATION |
1745 MESA_META_SHADER |
1746 MESA_META_STENCIL_TEST |
1747 MESA_META_VERTEX |
1748 MESA_META_VIEWPORT |
1749 MESA_META_CLIP |
1750 MESA_META_CLAMP_FRAGMENT_COLOR |
1751 MESA_META_MULTISAMPLE |
1752 MESA_META_OCCLUSION_QUERY);
1753
1754 if (!glsl) {
1755 metaSave |= MESA_META_FOG |
1756 MESA_META_PIXEL_TRANSFER |
1757 MESA_META_TRANSFORM |
1758 MESA_META_TEXTURE |
1759 MESA_META_CLAMP_VERTEX_COLOR |
1760 MESA_META_SELECT_FEEDBACK;
1761 }
1762
1763 if (buffers & BUFFER_BITS_COLOR) {
1764 metaSave |= MESA_META_DRAW_BUFFERS;
1765 }
1766
1767 _mesa_meta_begin(ctx, metaSave);
1768
1769 if (glsl) {
1770 meta_glsl_clear_init(ctx, clear);
1771
1772 x0 = ((float) fb->_Xmin / fb->Width) * 2.0f - 1.0f;
1773 y0 = ((float) fb->_Ymin / fb->Height) * 2.0f - 1.0f;
1774 x1 = ((float) fb->_Xmax / fb->Width) * 2.0f - 1.0f;
1775 y1 = ((float) fb->_Ymax / fb->Height) * 2.0f - 1.0f;
1776 z = -invert_z(ctx->Depth.Clear);
1777 } else {
1778 _mesa_meta_setup_vertex_objects(ctx, &clear->VAO, &clear->buf_obj, false,
1779 3, 0, 4);
1780
1781 x0 = (float) fb->_Xmin;
1782 y0 = (float) fb->_Ymin;
1783 x1 = (float) fb->_Xmax;
1784 y1 = (float) fb->_Ymax;
1785 z = invert_z(ctx->Depth.Clear);
1786 }
1787
1788 if (fb->_IntegerBuffers) {
1789 assert(glsl);
1790 _mesa_meta_use_program(ctx, clear->IntegerShaderProg);
1791 _mesa_Uniform4iv(0, 1, ctx->Color.ClearColor.i);
1792 } else if (glsl) {
1793 _mesa_meta_use_program(ctx, clear->ShaderProg);
1794 _mesa_Uniform4fv(0, 1, ctx->Color.ClearColor.f);
1795 }
1796
1797 /* GL_COLOR_BUFFER_BIT */
1798 if (buffers & BUFFER_BITS_COLOR) {
1799 /* Only draw to the buffers we were asked to clear. */
1800 _mesa_meta_drawbuffers_and_colormask(ctx, buffers & BUFFER_BITS_COLOR);
1801
1802 /* leave colormask state as-is */
1803
1804 /* Clears never have the color clamped. */
1805 if (ctx->Extensions.ARB_color_buffer_float)
1806 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
1807 }
1808 else {
1809 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1810 }
1811
1812 /* GL_DEPTH_BUFFER_BIT */
1813 if (buffers & BUFFER_BIT_DEPTH) {
1814 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE);
1815 _mesa_DepthFunc(GL_ALWAYS);
1816 _mesa_DepthMask(GL_TRUE);
1817 }
1818 else {
1819 assert(!ctx->Depth.Test);
1820 }
1821
1822 /* GL_STENCIL_BUFFER_BIT */
1823 if (buffers & BUFFER_BIT_STENCIL) {
1824 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
1825 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK,
1826 GL_REPLACE, GL_REPLACE, GL_REPLACE);
1827 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK, GL_ALWAYS,
1828 ctx->Stencil.Clear & stencilMax,
1829 ctx->Stencil.WriteMask[0]);
1830 }
1831 else {
1832 assert(!ctx->Stencil.Enabled);
1833 }
1834
1835 /* vertex positions */
1836 verts[0].x = x0;
1837 verts[0].y = y0;
1838 verts[0].z = z;
1839 verts[1].x = x1;
1840 verts[1].y = y0;
1841 verts[1].z = z;
1842 verts[2].x = x1;
1843 verts[2].y = y1;
1844 verts[2].z = z;
1845 verts[3].x = x0;
1846 verts[3].y = y1;
1847 verts[3].z = z;
1848
1849 if (!glsl) {
1850 for (i = 0; i < 4; i++) {
1851 verts[i].r = ctx->Color.ClearColor.f[0];
1852 verts[i].g = ctx->Color.ClearColor.f[1];
1853 verts[i].b = ctx->Color.ClearColor.f[2];
1854 verts[i].a = ctx->Color.ClearColor.f[3];
1855 }
1856 }
1857
1858 /* upload new vertex data */
1859 _mesa_buffer_data(ctx, clear->buf_obj, GL_NONE, sizeof(verts), verts,
1860 GL_DYNAMIC_DRAW, __func__);
1861
1862 /* draw quad(s) */
1863 if (fb->MaxNumLayers > 0) {
1864 _mesa_DrawArraysInstancedARB(GL_TRIANGLE_FAN, 0, 4, fb->MaxNumLayers);
1865 } else {
1866 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1867 }
1868
1869 _mesa_meta_end(ctx);
1870 }
1871
1872 /**
1873 * Meta implementation of ctx->Driver.CopyPixels() in terms
1874 * of texture mapping and polygon rendering and GLSL shaders.
1875 */
1876 void
1877 _mesa_meta_CopyPixels(struct gl_context *ctx, GLint srcX, GLint srcY,
1878 GLsizei width, GLsizei height,
1879 GLint dstX, GLint dstY, GLenum type)
1880 {
1881 struct copypix_state *copypix = &ctx->Meta->CopyPix;
1882 struct temp_texture *tex = _mesa_meta_get_temp_texture(ctx);
1883 struct vertex verts[4];
1884
1885 if (type != GL_COLOR ||
1886 ctx->_ImageTransferState ||
1887 ctx->Fog.Enabled ||
1888 width > tex->MaxSize ||
1889 height > tex->MaxSize) {
1890 /* XXX avoid this fallback */
1891 _swrast_CopyPixels(ctx, srcX, srcY, width, height, dstX, dstY, type);
1892 return;
1893 }
1894
1895 /* Most GL state applies to glCopyPixels, but a there's a few things
1896 * we need to override:
1897 */
1898 _mesa_meta_begin(ctx, (MESA_META_RASTERIZATION |
1899 MESA_META_SHADER |
1900 MESA_META_TEXTURE |
1901 MESA_META_TRANSFORM |
1902 MESA_META_CLIP |
1903 MESA_META_VERTEX |
1904 MESA_META_VIEWPORT));
1905
1906 _mesa_meta_setup_vertex_objects(ctx, &copypix->VAO, &copypix->buf_obj, false,
1907 3, 2, 0);
1908
1909 /* Silence valgrind warnings about reading uninitialized stack. */
1910 memset(verts, 0, sizeof(verts));
1911
1912 /* Alloc/setup texture */
1913 _mesa_meta_setup_copypix_texture(ctx, tex, srcX, srcY, width, height,
1914 GL_RGBA, GL_NEAREST);
1915
1916 /* vertex positions, texcoords (after texture allocation!) */
1917 {
1918 const GLfloat dstX0 = (GLfloat) dstX;
1919 const GLfloat dstY0 = (GLfloat) dstY;
1920 const GLfloat dstX1 = dstX + width * ctx->Pixel.ZoomX;
1921 const GLfloat dstY1 = dstY + height * ctx->Pixel.ZoomY;
1922 const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
1923
1924 verts[0].x = dstX0;
1925 verts[0].y = dstY0;
1926 verts[0].z = z;
1927 verts[0].tex[0] = 0.0F;
1928 verts[0].tex[1] = 0.0F;
1929 verts[1].x = dstX1;
1930 verts[1].y = dstY0;
1931 verts[1].z = z;
1932 verts[1].tex[0] = tex->Sright;
1933 verts[1].tex[1] = 0.0F;
1934 verts[2].x = dstX1;
1935 verts[2].y = dstY1;
1936 verts[2].z = z;
1937 verts[2].tex[0] = tex->Sright;
1938 verts[2].tex[1] = tex->Ttop;
1939 verts[3].x = dstX0;
1940 verts[3].y = dstY1;
1941 verts[3].z = z;
1942 verts[3].tex[0] = 0.0F;
1943 verts[3].tex[1] = tex->Ttop;
1944
1945 /* upload new vertex data */
1946 _mesa_buffer_sub_data(ctx, copypix->buf_obj, 0, sizeof(verts), verts);
1947 }
1948
1949 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
1950
1951 /* draw textured quad */
1952 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1953
1954 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
1955
1956 _mesa_meta_end(ctx);
1957 }
1958
1959 static void
1960 meta_drawpix_cleanup(struct gl_context *ctx, struct drawpix_state *drawpix)
1961 {
1962 if (drawpix->VAO != 0) {
1963 _mesa_DeleteVertexArrays(1, &drawpix->VAO);
1964 drawpix->VAO = 0;
1965
1966 _mesa_reference_buffer_object(ctx, &drawpix->buf_obj, NULL);
1967 }
1968
1969 if (drawpix->StencilFP != 0) {
1970 _mesa_DeleteProgramsARB(1, &drawpix->StencilFP);
1971 drawpix->StencilFP = 0;
1972 }
1973
1974 if (drawpix->DepthFP != 0) {
1975 _mesa_DeleteProgramsARB(1, &drawpix->DepthFP);
1976 drawpix->DepthFP = 0;
1977 }
1978 }
1979
1980 static void
1981 meta_drawtex_cleanup(struct gl_context *ctx, struct drawtex_state *drawtex)
1982 {
1983 if (drawtex->VAO != 0) {
1984 _mesa_DeleteVertexArrays(1, &drawtex->VAO);
1985 drawtex->VAO = 0;
1986
1987 _mesa_reference_buffer_object(ctx, &drawtex->buf_obj, NULL);
1988 }
1989 }
1990
1991 static void
1992 meta_bitmap_cleanup(struct gl_context *ctx, struct bitmap_state *bitmap)
1993 {
1994 if (bitmap->VAO != 0) {
1995 _mesa_DeleteVertexArrays(1, &bitmap->VAO);
1996 bitmap->VAO = 0;
1997
1998 _mesa_reference_buffer_object(ctx, &bitmap->buf_obj, NULL);
1999
2000 cleanup_temp_texture(ctx, &bitmap->Tex);
2001 }
2002 }
2003
2004 /**
2005 * When the glDrawPixels() image size is greater than the max rectangle
2006 * texture size we use this function to break the glDrawPixels() image
2007 * into tiles which fit into the max texture size.
2008 */
2009 static void
2010 tiled_draw_pixels(struct gl_context *ctx,
2011 GLint tileSize,
2012 GLint x, GLint y, GLsizei width, GLsizei height,
2013 GLenum format, GLenum type,
2014 const struct gl_pixelstore_attrib *unpack,
2015 const GLvoid *pixels)
2016 {
2017 struct gl_pixelstore_attrib tileUnpack = *unpack;
2018 GLint i, j;
2019
2020 if (tileUnpack.RowLength == 0)
2021 tileUnpack.RowLength = width;
2022
2023 for (i = 0; i < width; i += tileSize) {
2024 const GLint tileWidth = MIN2(tileSize, width - i);
2025 const GLint tileX = (GLint) (x + i * ctx->Pixel.ZoomX);
2026
2027 tileUnpack.SkipPixels = unpack->SkipPixels + i;
2028
2029 for (j = 0; j < height; j += tileSize) {
2030 const GLint tileHeight = MIN2(tileSize, height - j);
2031 const GLint tileY = (GLint) (y + j * ctx->Pixel.ZoomY);
2032
2033 tileUnpack.SkipRows = unpack->SkipRows + j;
2034
2035 _mesa_meta_DrawPixels(ctx, tileX, tileY, tileWidth, tileHeight,
2036 format, type, &tileUnpack, pixels);
2037 }
2038 }
2039 }
2040
2041
2042 /**
2043 * One-time init for drawing stencil pixels.
2044 */
2045 static void
2046 init_draw_stencil_pixels(struct gl_context *ctx)
2047 {
2048 /* This program is run eight times, once for each stencil bit.
2049 * The stencil values to draw are found in an 8-bit alpha texture.
2050 * We read the texture/stencil value and test if bit 'b' is set.
2051 * If the bit is not set, use KIL to kill the fragment.
2052 * Finally, we use the stencil test to update the stencil buffer.
2053 *
2054 * The basic algorithm for checking if a bit is set is:
2055 * if (is_odd(value / (1 << bit)))
2056 * result is one (or non-zero).
2057 * else
2058 * result is zero.
2059 * The program parameter contains three values:
2060 * parm.x = 255 / (1 << bit)
2061 * parm.y = 0.5
2062 * parm.z = 0.0
2063 */
2064 static const char *program =
2065 "!!ARBfp1.0\n"
2066 "PARAM parm = program.local[0]; \n"
2067 "TEMP t; \n"
2068 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
2069 "# t = t * 255 / bit \n"
2070 "MUL t.x, t.a, parm.x; \n"
2071 "# t = (int) t \n"
2072 "FRC t.y, t.x; \n"
2073 "SUB t.x, t.x, t.y; \n"
2074 "# t = t * 0.5 \n"
2075 "MUL t.x, t.x, parm.y; \n"
2076 "# t = fract(t.x) \n"
2077 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2078 "# t.x = (t.x == 0 ? 1 : 0) \n"
2079 "SGE t.x, -t.x, parm.z; \n"
2080 "KIL -t.x; \n"
2081 "# for debug only \n"
2082 "#MOV result.color, t.x; \n"
2083 "END \n";
2084 char program2[1000];
2085 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
2086 struct temp_texture *tex = _mesa_meta_get_temp_texture(ctx);
2087 const char *texTarget;
2088
2089 assert(drawpix->StencilFP == 0);
2090
2091 /* replace %s with "RECT" or "2D" */
2092 assert(strlen(program) + 4 < sizeof(program2));
2093 if (tex->Target == GL_TEXTURE_RECTANGLE)
2094 texTarget = "RECT";
2095 else
2096 texTarget = "2D";
2097 snprintf(program2, sizeof(program2), program, texTarget);
2098
2099 _mesa_GenProgramsARB(1, &drawpix->StencilFP);
2100 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
2101 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
2102 strlen(program2), (const GLubyte *) program2);
2103 }
2104
2105
2106 /**
2107 * One-time init for drawing depth pixels.
2108 */
2109 static void
2110 init_draw_depth_pixels(struct gl_context *ctx)
2111 {
2112 static const char *program =
2113 "!!ARBfp1.0\n"
2114 "PARAM color = program.local[0]; \n"
2115 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2116 "MOV result.color, color; \n"
2117 "END \n";
2118 char program2[200];
2119 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
2120 struct temp_texture *tex = _mesa_meta_get_temp_texture(ctx);
2121 const char *texTarget;
2122
2123 assert(drawpix->DepthFP == 0);
2124
2125 /* replace %s with "RECT" or "2D" */
2126 assert(strlen(program) + 4 < sizeof(program2));
2127 if (tex->Target == GL_TEXTURE_RECTANGLE)
2128 texTarget = "RECT";
2129 else
2130 texTarget = "2D";
2131 snprintf(program2, sizeof(program2), program, texTarget);
2132
2133 _mesa_GenProgramsARB(1, &drawpix->DepthFP);
2134 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
2135 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
2136 strlen(program2), (const GLubyte *) program2);
2137 }
2138
2139
2140 /**
2141 * Meta implementation of ctx->Driver.DrawPixels() in terms
2142 * of texture mapping and polygon rendering.
2143 */
2144 void
2145 _mesa_meta_DrawPixels(struct gl_context *ctx,
2146 GLint x, GLint y, GLsizei width, GLsizei height,
2147 GLenum format, GLenum type,
2148 const struct gl_pixelstore_attrib *unpack,
2149 const GLvoid *pixels)
2150 {
2151 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
2152 struct temp_texture *tex = _mesa_meta_get_temp_texture(ctx);
2153 const struct gl_pixelstore_attrib unpackSave = ctx->Unpack;
2154 const GLuint origStencilMask = ctx->Stencil.WriteMask[0];
2155 struct vertex verts[4];
2156 GLenum texIntFormat;
2157 GLboolean fallback, newTex;
2158 GLbitfield metaExtraSave = 0x0;
2159
2160 /*
2161 * Determine if we can do the glDrawPixels with texture mapping.
2162 */
2163 fallback = GL_FALSE;
2164 if (ctx->Fog.Enabled) {
2165 fallback = GL_TRUE;
2166 }
2167
2168 if (_mesa_is_color_format(format)) {
2169 /* use more compact format when possible */
2170 /* XXX disable special case for GL_LUMINANCE for now to work around
2171 * apparent i965 driver bug (see bug #23670).
2172 */
2173 if (/*format == GL_LUMINANCE ||*/ format == GL_LUMINANCE_ALPHA)
2174 texIntFormat = format;
2175 else
2176 texIntFormat = GL_RGBA;
2177
2178 /* If we're not supposed to clamp the resulting color, then just
2179 * promote our texture to fully float. We could do better by
2180 * just going for the matching set of channels, in floating
2181 * point.
2182 */
2183 if (ctx->Color.ClampFragmentColor != GL_TRUE &&
2184 ctx->Extensions.ARB_texture_float)
2185 texIntFormat = GL_RGBA32F;
2186 }
2187 else if (_mesa_is_stencil_format(format)) {
2188 if (ctx->Extensions.ARB_fragment_program &&
2189 ctx->Pixel.IndexShift == 0 &&
2190 ctx->Pixel.IndexOffset == 0 &&
2191 type == GL_UNSIGNED_BYTE) {
2192 /* We'll store stencil as alpha. This only works for GLubyte
2193 * image data because of how incoming values are mapped to alpha
2194 * in [0,1].
2195 */
2196 texIntFormat = GL_ALPHA;
2197 metaExtraSave = (MESA_META_COLOR_MASK |
2198 MESA_META_DEPTH_TEST |
2199 MESA_META_PIXEL_TRANSFER |
2200 MESA_META_SHADER |
2201 MESA_META_STENCIL_TEST);
2202 }
2203 else {
2204 fallback = GL_TRUE;
2205 }
2206 }
2207 else if (_mesa_is_depth_format(format)) {
2208 if (ctx->Extensions.ARB_depth_texture &&
2209 ctx->Extensions.ARB_fragment_program) {
2210 texIntFormat = GL_DEPTH_COMPONENT;
2211 metaExtraSave = (MESA_META_SHADER);
2212 }
2213 else {
2214 fallback = GL_TRUE;
2215 }
2216 }
2217 else {
2218 fallback = GL_TRUE;
2219 }
2220
2221 if (fallback) {
2222 _swrast_DrawPixels(ctx, x, y, width, height,
2223 format, type, unpack, pixels);
2224 return;
2225 }
2226
2227 /*
2228 * Check image size against max texture size, draw as tiles if needed.
2229 */
2230 if (width > tex->MaxSize || height > tex->MaxSize) {
2231 tiled_draw_pixels(ctx, tex->MaxSize, x, y, width, height,
2232 format, type, unpack, pixels);
2233 return;
2234 }
2235
2236 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2237 * but a there's a few things we need to override:
2238 */
2239 _mesa_meta_begin(ctx, (MESA_META_RASTERIZATION |
2240 MESA_META_SHADER |
2241 MESA_META_TEXTURE |
2242 MESA_META_TRANSFORM |
2243 MESA_META_CLIP |
2244 MESA_META_VERTEX |
2245 MESA_META_VIEWPORT |
2246 metaExtraSave));
2247
2248 newTex = _mesa_meta_alloc_texture(tex, width, height, texIntFormat);
2249
2250 _mesa_meta_setup_vertex_objects(ctx, &drawpix->VAO, &drawpix->buf_obj, false,
2251 3, 2, 0);
2252
2253 /* Silence valgrind warnings about reading uninitialized stack. */
2254 memset(verts, 0, sizeof(verts));
2255
2256 /* vertex positions, texcoords (after texture allocation!) */
2257 {
2258 const GLfloat x0 = (GLfloat) x;
2259 const GLfloat y0 = (GLfloat) y;
2260 const GLfloat x1 = x + width * ctx->Pixel.ZoomX;
2261 const GLfloat y1 = y + height * ctx->Pixel.ZoomY;
2262 const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
2263
2264 verts[0].x = x0;
2265 verts[0].y = y0;
2266 verts[0].z = z;
2267 verts[0].tex[0] = 0.0F;
2268 verts[0].tex[1] = 0.0F;
2269 verts[1].x = x1;
2270 verts[1].y = y0;
2271 verts[1].z = z;
2272 verts[1].tex[0] = tex->Sright;
2273 verts[1].tex[1] = 0.0F;
2274 verts[2].x = x1;
2275 verts[2].y = y1;
2276 verts[2].z = z;
2277 verts[2].tex[0] = tex->Sright;
2278 verts[2].tex[1] = tex->Ttop;
2279 verts[3].x = x0;
2280 verts[3].y = y1;
2281 verts[3].z = z;
2282 verts[3].tex[0] = 0.0F;
2283 verts[3].tex[1] = tex->Ttop;
2284 }
2285
2286 /* upload new vertex data */
2287 _mesa_buffer_data(ctx, drawpix->buf_obj, GL_NONE, sizeof(verts), verts,
2288 GL_DYNAMIC_DRAW, __func__);
2289
2290 /* set given unpack params */
2291 ctx->Unpack = *unpack;
2292
2293 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
2294
2295 if (_mesa_is_stencil_format(format)) {
2296 /* Drawing stencil */
2297 GLint bit;
2298
2299 if (!drawpix->StencilFP)
2300 init_draw_stencil_pixels(ctx);
2301
2302 _mesa_meta_setup_drawpix_texture(ctx, tex, newTex, width, height,
2303 GL_ALPHA, type, pixels);
2304
2305 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
2306
2307 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
2308
2309 /* set all stencil bits to 0 */
2310 _mesa_StencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
2311 _mesa_StencilFunc(GL_ALWAYS, 0, 255);
2312 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2313
2314 /* set stencil bits to 1 where needed */
2315 _mesa_StencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
2316
2317 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
2318 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
2319
2320 for (bit = 0; bit < ctx->DrawBuffer->Visual.stencilBits; bit++) {
2321 const GLuint mask = 1 << bit;
2322 if (mask & origStencilMask) {
2323 _mesa_StencilFunc(GL_ALWAYS, mask, mask);
2324 _mesa_StencilMask(mask);
2325
2326 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0,
2327 255.0f / mask, 0.5f, 0.0f, 0.0f);
2328
2329 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2330 }
2331 }
2332 }
2333 else if (_mesa_is_depth_format(format)) {
2334 /* Drawing depth */
2335 if (!drawpix->DepthFP)
2336 init_draw_depth_pixels(ctx);
2337
2338 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
2339 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
2340
2341 /* polygon color = current raster color */
2342 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0,
2343 ctx->Current.RasterColor);
2344
2345 _mesa_meta_setup_drawpix_texture(ctx, tex, newTex, width, height,
2346 format, type, pixels);
2347
2348 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2349 }
2350 else {
2351 /* Drawing RGBA */
2352 _mesa_meta_setup_drawpix_texture(ctx, tex, newTex, width, height,
2353 format, type, pixels);
2354 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2355 }
2356
2357 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
2358
2359 /* restore unpack params */
2360 ctx->Unpack = unpackSave;
2361
2362 _mesa_meta_end(ctx);
2363 }
2364
2365 static GLboolean
2366 alpha_test_raster_color(struct gl_context *ctx)
2367 {
2368 GLfloat alpha = ctx->Current.RasterColor[ACOMP];
2369 GLfloat ref = ctx->Color.AlphaRef;
2370
2371 switch (ctx->Color.AlphaFunc) {
2372 case GL_NEVER:
2373 return GL_FALSE;
2374 case GL_LESS:
2375 return alpha < ref;
2376 case GL_EQUAL:
2377 return alpha == ref;
2378 case GL_LEQUAL:
2379 return alpha <= ref;
2380 case GL_GREATER:
2381 return alpha > ref;
2382 case GL_NOTEQUAL:
2383 return alpha != ref;
2384 case GL_GEQUAL:
2385 return alpha >= ref;
2386 case GL_ALWAYS:
2387 return GL_TRUE;
2388 default:
2389 assert(0);
2390 return GL_FALSE;
2391 }
2392 }
2393
2394 /**
2395 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2396 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2397 * tracker would improve performance a lot.
2398 */
2399 void
2400 _mesa_meta_Bitmap(struct gl_context *ctx,
2401 GLint x, GLint y, GLsizei width, GLsizei height,
2402 const struct gl_pixelstore_attrib *unpack,
2403 const GLubyte *bitmap1)
2404 {
2405 struct bitmap_state *bitmap = &ctx->Meta->Bitmap;
2406 struct temp_texture *tex = get_bitmap_temp_texture(ctx);
2407 const GLenum texIntFormat = GL_ALPHA;
2408 const struct gl_pixelstore_attrib unpackSave = *unpack;
2409 GLubyte fg, bg;
2410 struct vertex verts[4];
2411 GLboolean newTex;
2412 GLubyte *bitmap8;
2413
2414 /*
2415 * Check if swrast fallback is needed.
2416 */
2417 if (ctx->_ImageTransferState ||
2418 _mesa_arb_fragment_program_enabled(ctx) ||
2419 ctx->Fog.Enabled ||
2420 ctx->Texture._MaxEnabledTexImageUnit != -1 ||
2421 width > tex->MaxSize ||
2422 height > tex->MaxSize) {
2423 _swrast_Bitmap(ctx, x, y, width, height, unpack, bitmap1);
2424 return;
2425 }
2426
2427 if (ctx->Color.AlphaEnabled && !alpha_test_raster_color(ctx))
2428 return;
2429
2430 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2431 * but a there's a few things we need to override:
2432 */
2433 _mesa_meta_begin(ctx, (MESA_META_ALPHA_TEST |
2434 MESA_META_PIXEL_STORE |
2435 MESA_META_RASTERIZATION |
2436 MESA_META_SHADER |
2437 MESA_META_TEXTURE |
2438 MESA_META_TRANSFORM |
2439 MESA_META_CLIP |
2440 MESA_META_VERTEX |
2441 MESA_META_VIEWPORT));
2442
2443 _mesa_meta_setup_vertex_objects(ctx, &bitmap->VAO, &bitmap->buf_obj, false,
2444 3, 2, 4);
2445
2446 newTex = _mesa_meta_alloc_texture(tex, width, height, texIntFormat);
2447
2448 /* Silence valgrind warnings about reading uninitialized stack. */
2449 memset(verts, 0, sizeof(verts));
2450
2451 /* vertex positions, texcoords, colors (after texture allocation!) */
2452 {
2453 const GLfloat x0 = (GLfloat) x;
2454 const GLfloat y0 = (GLfloat) y;
2455 const GLfloat x1 = (GLfloat) (x + width);
2456 const GLfloat y1 = (GLfloat) (y + height);
2457 const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
2458 GLuint i;
2459
2460 verts[0].x = x0;
2461 verts[0].y = y0;
2462 verts[0].z = z;
2463 verts[0].tex[0] = 0.0F;
2464 verts[0].tex[1] = 0.0F;
2465 verts[1].x = x1;
2466 verts[1].y = y0;
2467 verts[1].z = z;
2468 verts[1].tex[0] = tex->Sright;
2469 verts[1].tex[1] = 0.0F;
2470 verts[2].x = x1;
2471 verts[2].y = y1;
2472 verts[2].z = z;
2473 verts[2].tex[0] = tex->Sright;
2474 verts[2].tex[1] = tex->Ttop;
2475 verts[3].x = x0;
2476 verts[3].y = y1;
2477 verts[3].z = z;
2478 verts[3].tex[0] = 0.0F;
2479 verts[3].tex[1] = tex->Ttop;
2480
2481 for (i = 0; i < 4; i++) {
2482 verts[i].r = ctx->Current.RasterColor[0];
2483 verts[i].g = ctx->Current.RasterColor[1];
2484 verts[i].b = ctx->Current.RasterColor[2];
2485 verts[i].a = ctx->Current.RasterColor[3];
2486 }
2487
2488 /* upload new vertex data */
2489 _mesa_buffer_sub_data(ctx, bitmap->buf_obj, 0, sizeof(verts), verts);
2490 }
2491
2492 /* choose different foreground/background alpha values */
2493 CLAMPED_FLOAT_TO_UBYTE(fg, ctx->Current.RasterColor[ACOMP]);
2494 bg = (fg > 127 ? 0 : 255);
2495
2496 bitmap1 = _mesa_map_pbo_source(ctx, &unpackSave, bitmap1);
2497 if (!bitmap1) {
2498 _mesa_meta_end(ctx);
2499 return;
2500 }
2501
2502 bitmap8 = malloc(width * height);
2503 if (bitmap8) {
2504 memset(bitmap8, bg, width * height);
2505 _mesa_expand_bitmap(width, height, &unpackSave, bitmap1,
2506 bitmap8, width, fg);
2507
2508 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
2509
2510 _mesa_set_enable(ctx, GL_ALPHA_TEST, GL_TRUE);
2511 _mesa_AlphaFunc(GL_NOTEQUAL, UBYTE_TO_FLOAT(bg));
2512
2513 _mesa_meta_setup_drawpix_texture(ctx, tex, newTex, width, height,
2514 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap8);
2515
2516 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2517
2518 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
2519
2520 free(bitmap8);
2521 }
2522
2523 _mesa_unmap_pbo_source(ctx, &unpackSave);
2524
2525 _mesa_meta_end(ctx);
2526 }
2527
2528 /**
2529 * Compute the texture coordinates for the four vertices of a quad for
2530 * drawing a 2D texture image or slice of a cube/3D texture. The offset
2531 * and width, height specify a sub-region of the 2D image.
2532 *
2533 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2534 * \param slice slice of a 1D/2D array texture or 3D texture
2535 * \param xoffset X position of sub texture
2536 * \param yoffset Y position of sub texture
2537 * \param width width of the sub texture image
2538 * \param height height of the sub texture image
2539 * \param total_width total width of the texture image
2540 * \param total_height total height of the texture image
2541 * \param total_depth total depth of the texture image
2542 * \param coords0/1/2/3 returns the computed texcoords
2543 */
2544 void
2545 _mesa_meta_setup_texture_coords(GLenum faceTarget,
2546 GLint slice,
2547 GLint xoffset,
2548 GLint yoffset,
2549 GLint width,
2550 GLint height,
2551 GLint total_width,
2552 GLint total_height,
2553 GLint total_depth,
2554 GLfloat coords0[4],
2555 GLfloat coords1[4],
2556 GLfloat coords2[4],
2557 GLfloat coords3[4])
2558 {
2559 float st[4][2];
2560 GLuint i;
2561 const float s0 = (float) xoffset / (float) total_width;
2562 const float s1 = (float) (xoffset + width) / (float) total_width;
2563 const float t0 = (float) yoffset / (float) total_height;
2564 const float t1 = (float) (yoffset + height) / (float) total_height;
2565 GLfloat r;
2566
2567 /* setup the reference texcoords */
2568 st[0][0] = s0;
2569 st[0][1] = t0;
2570 st[1][0] = s1;
2571 st[1][1] = t0;
2572 st[2][0] = s1;
2573 st[2][1] = t1;
2574 st[3][0] = s0;
2575 st[3][1] = t1;
2576
2577 if (faceTarget == GL_TEXTURE_CUBE_MAP_ARRAY)
2578 faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + slice % 6;
2579
2580 /* Currently all texture targets want the W component to be 1.0.
2581 */
2582 coords0[3] = 1.0F;
2583 coords1[3] = 1.0F;
2584 coords2[3] = 1.0F;
2585 coords3[3] = 1.0F;
2586
2587 switch (faceTarget) {
2588 case GL_TEXTURE_1D:
2589 case GL_TEXTURE_2D:
2590 case GL_TEXTURE_3D:
2591 case GL_TEXTURE_2D_ARRAY:
2592 if (faceTarget == GL_TEXTURE_3D) {
2593 assert(slice < total_depth);
2594 assert(total_depth >= 1);
2595 r = (slice + 0.5f) / total_depth;
2596 }
2597 else if (faceTarget == GL_TEXTURE_2D_ARRAY)
2598 r = (float) slice;
2599 else
2600 r = 0.0F;
2601 coords0[0] = st[0][0]; /* s */
2602 coords0[1] = st[0][1]; /* t */
2603 coords0[2] = r; /* r */
2604 coords1[0] = st[1][0];
2605 coords1[1] = st[1][1];
2606 coords1[2] = r;
2607 coords2[0] = st[2][0];
2608 coords2[1] = st[2][1];
2609 coords2[2] = r;
2610 coords3[0] = st[3][0];
2611 coords3[1] = st[3][1];
2612 coords3[2] = r;
2613 break;
2614 case GL_TEXTURE_RECTANGLE_ARB:
2615 coords0[0] = (float) xoffset; /* s */
2616 coords0[1] = (float) yoffset; /* t */
2617 coords0[2] = 0.0F; /* r */
2618 coords1[0] = (float) (xoffset + width);
2619 coords1[1] = (float) yoffset;
2620 coords1[2] = 0.0F;
2621 coords2[0] = (float) (xoffset + width);
2622 coords2[1] = (float) (yoffset + height);
2623 coords2[2] = 0.0F;
2624 coords3[0] = (float) xoffset;
2625 coords3[1] = (float) (yoffset + height);
2626 coords3[2] = 0.0F;
2627 break;
2628 case GL_TEXTURE_1D_ARRAY:
2629 coords0[0] = st[0][0]; /* s */
2630 coords0[1] = (float) slice; /* t */
2631 coords0[2] = 0.0F; /* r */
2632 coords1[0] = st[1][0];
2633 coords1[1] = (float) slice;
2634 coords1[2] = 0.0F;
2635 coords2[0] = st[2][0];
2636 coords2[1] = (float) slice;
2637 coords2[2] = 0.0F;
2638 coords3[0] = st[3][0];
2639 coords3[1] = (float) slice;
2640 coords3[2] = 0.0F;
2641 break;
2642
2643 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2644 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2645 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2646 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2647 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2648 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2649 /* loop over quad verts */
2650 for (i = 0; i < 4; i++) {
2651 /* Compute sc = +/-scale and tc = +/-scale.
2652 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2653 * though that can still sometimes happen with this scale factor...
2654 */
2655 const GLfloat scale = 0.9999f;
2656 const GLfloat sc = (2.0f * st[i][0] - 1.0f) * scale;
2657 const GLfloat tc = (2.0f * st[i][1] - 1.0f) * scale;
2658 GLfloat *coord;
2659
2660 switch (i) {
2661 case 0:
2662 coord = coords0;
2663 break;
2664 case 1:
2665 coord = coords1;
2666 break;
2667 case 2:
2668 coord = coords2;
2669 break;
2670 case 3:
2671 coord = coords3;
2672 break;
2673 default:
2674 unreachable("not reached");
2675 }
2676
2677 coord[3] = (float) (slice / 6);
2678
2679 switch (faceTarget) {
2680 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2681 coord[0] = 1.0f;
2682 coord[1] = -tc;
2683 coord[2] = -sc;
2684 break;
2685 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2686 coord[0] = -1.0f;
2687 coord[1] = -tc;
2688 coord[2] = sc;
2689 break;
2690 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2691 coord[0] = sc;
2692 coord[1] = 1.0f;
2693 coord[2] = tc;
2694 break;
2695 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2696 coord[0] = sc;
2697 coord[1] = -1.0f;
2698 coord[2] = -tc;
2699 break;
2700 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2701 coord[0] = sc;
2702 coord[1] = -tc;
2703 coord[2] = 1.0f;
2704 break;
2705 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2706 coord[0] = -sc;
2707 coord[1] = -tc;
2708 coord[2] = -1.0f;
2709 break;
2710 default:
2711 assert(0);
2712 }
2713 }
2714 break;
2715 default:
2716 assert(!"unexpected target in _mesa_meta_setup_texture_coords()");
2717 }
2718 }
2719
2720 static struct blit_shader *
2721 choose_blit_shader(GLenum target, struct blit_shader_table *table)
2722 {
2723 switch(target) {
2724 case GL_TEXTURE_1D:
2725 table->sampler_1d.type = "sampler1D";
2726 table->sampler_1d.func = "texture1D";
2727 table->sampler_1d.texcoords = "texCoords.x";
2728 return &table->sampler_1d;
2729 case GL_TEXTURE_2D:
2730 table->sampler_2d.type = "sampler2D";
2731 table->sampler_2d.func = "texture2D";
2732 table->sampler_2d.texcoords = "texCoords.xy";
2733 return &table->sampler_2d;
2734 case GL_TEXTURE_RECTANGLE:
2735 table->sampler_rect.type = "sampler2DRect";
2736 table->sampler_rect.func = "texture2DRect";
2737 table->sampler_rect.texcoords = "texCoords.xy";
2738 return &table->sampler_rect;
2739 case GL_TEXTURE_3D:
2740 /* Code for mipmap generation with 3D textures is not used yet.
2741 * It's a sw fallback.
2742 */
2743 table->sampler_3d.type = "sampler3D";
2744 table->sampler_3d.func = "texture3D";
2745 table->sampler_3d.texcoords = "texCoords.xyz";
2746 return &table->sampler_3d;
2747 case GL_TEXTURE_CUBE_MAP:
2748 table->sampler_cubemap.type = "samplerCube";
2749 table->sampler_cubemap.func = "textureCube";
2750 table->sampler_cubemap.texcoords = "texCoords.xyz";
2751 return &table->sampler_cubemap;
2752 case GL_TEXTURE_1D_ARRAY:
2753 table->sampler_1d_array.type = "sampler1DArray";
2754 table->sampler_1d_array.func = "texture1DArray";
2755 table->sampler_1d_array.texcoords = "texCoords.xy";
2756 return &table->sampler_1d_array;
2757 case GL_TEXTURE_2D_ARRAY:
2758 table->sampler_2d_array.type = "sampler2DArray";
2759 table->sampler_2d_array.func = "texture2DArray";
2760 table->sampler_2d_array.texcoords = "texCoords.xyz";
2761 return &table->sampler_2d_array;
2762 case GL_TEXTURE_CUBE_MAP_ARRAY:
2763 table->sampler_cubemap_array.type = "samplerCubeArray";
2764 table->sampler_cubemap_array.func = "textureCubeArray";
2765 table->sampler_cubemap_array.texcoords = "texCoords.xyzw";
2766 return &table->sampler_cubemap_array;
2767 default:
2768 _mesa_problem(NULL, "Unexpected texture target 0x%x in"
2769 " setup_texture_sampler()\n", target);
2770 return NULL;
2771 }
2772 }
2773
2774 void
2775 _mesa_meta_blit_shader_table_cleanup(struct gl_context *ctx,
2776 struct blit_shader_table *table)
2777 {
2778 _mesa_reference_shader_program(ctx, &table->sampler_1d.shader_prog, NULL);
2779 _mesa_reference_shader_program(ctx, &table->sampler_2d.shader_prog, NULL);
2780 _mesa_reference_shader_program(ctx, &table->sampler_3d.shader_prog, NULL);
2781 _mesa_reference_shader_program(ctx, &table->sampler_rect.shader_prog, NULL);
2782 _mesa_reference_shader_program(ctx, &table->sampler_cubemap.shader_prog, NULL);
2783 _mesa_reference_shader_program(ctx, &table->sampler_1d_array.shader_prog, NULL);
2784 _mesa_reference_shader_program(ctx, &table->sampler_2d_array.shader_prog, NULL);
2785 _mesa_reference_shader_program(ctx, &table->sampler_cubemap_array.shader_prog, NULL);
2786 }
2787
2788 /**
2789 * Determine the GL data type to use for the temporary image read with
2790 * ReadPixels() and passed to Tex[Sub]Image().
2791 */
2792 static GLenum
2793 get_temp_image_type(struct gl_context *ctx, mesa_format format)
2794 {
2795 const GLenum baseFormat = _mesa_get_format_base_format(format);
2796 const GLenum datatype = _mesa_get_format_datatype(format);
2797 const GLint format_red_bits = _mesa_get_format_bits(format, GL_RED_BITS);
2798
2799 switch (baseFormat) {
2800 case GL_RGBA:
2801 case GL_RGB:
2802 case GL_RG:
2803 case GL_RED:
2804 case GL_ALPHA:
2805 case GL_LUMINANCE:
2806 case GL_LUMINANCE_ALPHA:
2807 case GL_INTENSITY:
2808 if (datatype == GL_INT || datatype == GL_UNSIGNED_INT) {
2809 return datatype;
2810 } else if (format_red_bits <= 8) {
2811 return GL_UNSIGNED_BYTE;
2812 } else if (format_red_bits <= 16) {
2813 return GL_UNSIGNED_SHORT;
2814 }
2815 return GL_FLOAT;
2816 case GL_DEPTH_COMPONENT:
2817 if (datatype == GL_FLOAT)
2818 return GL_FLOAT;
2819 else
2820 return GL_UNSIGNED_INT;
2821 case GL_DEPTH_STENCIL:
2822 if (datatype == GL_FLOAT)
2823 return GL_FLOAT_32_UNSIGNED_INT_24_8_REV;
2824 else
2825 return GL_UNSIGNED_INT_24_8;
2826 default:
2827 _mesa_problem(ctx, "Unexpected format %d in get_temp_image_type()",
2828 baseFormat);
2829 return 0;
2830 }
2831 }
2832
2833 /**
2834 * Attempts to wrap the destination texture in an FBO and use
2835 * glBlitFramebuffer() to implement glCopyTexSubImage().
2836 */
2837 static bool
2838 copytexsubimage_using_blit_framebuffer(struct gl_context *ctx,
2839 struct gl_texture_image *texImage,
2840 GLint xoffset,
2841 GLint yoffset,
2842 GLint zoffset,
2843 struct gl_renderbuffer *rb,
2844 GLint x, GLint y,
2845 GLsizei width, GLsizei height)
2846 {
2847 struct gl_framebuffer *drawFb;
2848 bool success = false;
2849 GLbitfield mask;
2850 GLenum status;
2851
2852 if (!ctx->Extensions.ARB_framebuffer_object)
2853 return false;
2854
2855 drawFb = ctx->Driver.NewFramebuffer(ctx, 0xDEADBEEF);
2856 if (drawFb == NULL)
2857 return false;
2858
2859 _mesa_meta_begin(ctx, MESA_META_ALL & ~MESA_META_DRAW_BUFFERS);
2860 _mesa_bind_framebuffers(ctx, drawFb, ctx->ReadBuffer);
2861
2862 if (rb->_BaseFormat == GL_DEPTH_STENCIL ||
2863 rb->_BaseFormat == GL_DEPTH_COMPONENT) {
2864 _mesa_meta_framebuffer_texture_image(ctx, ctx->DrawBuffer,
2865 GL_DEPTH_ATTACHMENT,
2866 texImage, zoffset);
2867 mask = GL_DEPTH_BUFFER_BIT;
2868
2869 if (rb->_BaseFormat == GL_DEPTH_STENCIL &&
2870 texImage->_BaseFormat == GL_DEPTH_STENCIL) {
2871 _mesa_meta_framebuffer_texture_image(ctx, ctx->DrawBuffer,
2872 GL_STENCIL_ATTACHMENT,
2873 texImage, zoffset);
2874 mask |= GL_STENCIL_BUFFER_BIT;
2875 }
2876 _mesa_DrawBuffer(GL_NONE);
2877 } else {
2878 _mesa_meta_framebuffer_texture_image(ctx, ctx->DrawBuffer,
2879 GL_COLOR_ATTACHMENT0,
2880 texImage, zoffset);
2881 mask = GL_COLOR_BUFFER_BIT;
2882 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0);
2883 }
2884
2885 status = _mesa_check_framebuffer_status(ctx, ctx->DrawBuffer);
2886 if (status != GL_FRAMEBUFFER_COMPLETE)
2887 goto out;
2888
2889 ctx->Meta->Blit.no_ctsi_fallback = true;
2890
2891 /* Since we've bound a new draw framebuffer, we need to update
2892 * its derived state -- _Xmin, etc -- for BlitFramebuffer's clipping to
2893 * be correct.
2894 */
2895 _mesa_update_state(ctx);
2896
2897 /* We skip the core BlitFramebuffer checks for format consistency, which
2898 * are too strict for CopyTexImage. We know meta will be fine with format
2899 * changes.
2900 */
2901 mask = _mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
2902 x, y,
2903 x + width, y + height,
2904 xoffset, yoffset,
2905 xoffset + width, yoffset + height,
2906 mask, GL_NEAREST);
2907 ctx->Meta->Blit.no_ctsi_fallback = false;
2908 success = mask == 0x0;
2909
2910 out:
2911 _mesa_reference_framebuffer(&drawFb, NULL);
2912 _mesa_meta_end(ctx);
2913 return success;
2914 }
2915
2916 /**
2917 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2918 * Have to be careful with locking and meta state for pixel transfer.
2919 */
2920 void
2921 _mesa_meta_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
2922 struct gl_texture_image *texImage,
2923 GLint xoffset, GLint yoffset, GLint zoffset,
2924 struct gl_renderbuffer *rb,
2925 GLint x, GLint y,
2926 GLsizei width, GLsizei height)
2927 {
2928 GLenum format, type;
2929 GLint bpp;
2930 void *buf;
2931
2932 if (copytexsubimage_using_blit_framebuffer(ctx,
2933 texImage,
2934 xoffset, yoffset, zoffset,
2935 rb,
2936 x, y,
2937 width, height)) {
2938 return;
2939 }
2940
2941 /* Choose format/type for temporary image buffer */
2942 format = _mesa_get_format_base_format(texImage->TexFormat);
2943 if (format == GL_LUMINANCE ||
2944 format == GL_LUMINANCE_ALPHA ||
2945 format == GL_INTENSITY) {
2946 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
2947 * temp image buffer because glReadPixels will do L=R+G+B which is
2948 * not what we want (should be L=R).
2949 */
2950 format = GL_RGBA;
2951 }
2952
2953 type = get_temp_image_type(ctx, texImage->TexFormat);
2954 if (_mesa_is_format_integer_color(texImage->TexFormat)) {
2955 format = _mesa_base_format_to_integer_format(format);
2956 }
2957 bpp = _mesa_bytes_per_pixel(format, type);
2958 if (bpp <= 0) {
2959 _mesa_problem(ctx, "Bad bpp in _mesa_meta_CopyTexSubImage()");
2960 return;
2961 }
2962
2963 /*
2964 * Alloc image buffer (XXX could use a PBO)
2965 */
2966 buf = malloc(width * height * bpp);
2967 if (!buf) {
2968 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage%uD", dims);
2969 return;
2970 }
2971
2972 /*
2973 * Read image from framebuffer (disable pixel transfer ops)
2974 */
2975 _mesa_meta_begin(ctx, MESA_META_PIXEL_STORE | MESA_META_PIXEL_TRANSFER);
2976 ctx->Driver.ReadPixels(ctx, x, y, width, height,
2977 format, type, &ctx->Pack, buf);
2978 _mesa_meta_end(ctx);
2979
2980 _mesa_update_state(ctx); /* to update pixel transfer state */
2981
2982 /*
2983 * Store texture data (with pixel transfer ops)
2984 */
2985 _mesa_meta_begin(ctx, MESA_META_PIXEL_STORE);
2986
2987 if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
2988 assert(yoffset == 0);
2989 ctx->Driver.TexSubImage(ctx, dims, texImage,
2990 xoffset, zoffset, 0, width, 1, 1,
2991 format, type, buf, &ctx->Unpack);
2992 } else {
2993 ctx->Driver.TexSubImage(ctx, dims, texImage,
2994 xoffset, yoffset, zoffset, width, height, 1,
2995 format, type, buf, &ctx->Unpack);
2996 }
2997
2998 _mesa_meta_end(ctx);
2999
3000 free(buf);
3001 }
3002
3003 static void
3004 meta_decompress_fbo_cleanup(struct decompress_fbo_state *decompress_fbo)
3005 {
3006 if (decompress_fbo->fb != NULL) {
3007 _mesa_reference_framebuffer(&decompress_fbo->fb, NULL);
3008 _mesa_reference_renderbuffer(&decompress_fbo->rb, NULL);
3009 }
3010
3011 memset(decompress_fbo, 0, sizeof(*decompress_fbo));
3012 }
3013
3014 static void
3015 meta_decompress_cleanup(struct gl_context *ctx,
3016 struct decompress_state *decompress)
3017 {
3018 meta_decompress_fbo_cleanup(&decompress->byteFBO);
3019 meta_decompress_fbo_cleanup(&decompress->floatFBO);
3020
3021 if (decompress->VAO != 0) {
3022 _mesa_DeleteVertexArrays(1, &decompress->VAO);
3023 _mesa_reference_buffer_object(ctx, &decompress->buf_obj, NULL);
3024 }
3025
3026 _mesa_reference_sampler_object(ctx, &decompress->samp_obj, NULL);
3027 _mesa_meta_blit_shader_table_cleanup(ctx, &decompress->shaders);
3028
3029 memset(decompress, 0, sizeof(*decompress));
3030 }
3031
3032 /**
3033 * Decompress a texture image by drawing a quad with the compressed
3034 * texture and reading the pixels out of the color buffer.
3035 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
3036 * \param destFormat format, ala glReadPixels
3037 * \param destType type, ala glReadPixels
3038 * \param dest destination buffer
3039 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
3040 */
3041 static bool
3042 decompress_texture_image(struct gl_context *ctx,
3043 struct gl_texture_image *texImage,
3044 GLuint slice,
3045 GLint xoffset, GLint yoffset,
3046 GLsizei width, GLsizei height,
3047 GLenum destFormat, GLenum destType,
3048 GLvoid *dest)
3049 {
3050 struct decompress_state *decompress = &ctx->Meta->Decompress;
3051 struct decompress_fbo_state *decompress_fbo;
3052 struct gl_texture_object *texObj = texImage->TexObject;
3053 const GLenum target = texObj->Target;
3054 GLenum rbFormat;
3055 GLenum faceTarget;
3056 struct vertex verts[4];
3057 struct gl_sampler_object *samp_obj_save = NULL;
3058 GLenum status;
3059 const bool use_glsl_version = ctx->Extensions.ARB_vertex_shader &&
3060 ctx->Extensions.ARB_fragment_shader;
3061
3062 switch (_mesa_get_format_datatype(texImage->TexFormat)) {
3063 case GL_FLOAT:
3064 decompress_fbo = &decompress->floatFBO;
3065 rbFormat = GL_RGBA32F;
3066 break;
3067 case GL_UNSIGNED_NORMALIZED:
3068 decompress_fbo = &decompress->byteFBO;
3069 rbFormat = GL_RGBA;
3070 break;
3071 default:
3072 return false;
3073 }
3074
3075 if (slice > 0) {
3076 assert(target == GL_TEXTURE_3D ||
3077 target == GL_TEXTURE_2D_ARRAY ||
3078 target == GL_TEXTURE_CUBE_MAP_ARRAY);
3079 }
3080
3081 switch (target) {
3082 case GL_TEXTURE_1D:
3083 case GL_TEXTURE_1D_ARRAY:
3084 assert(!"No compressed 1D textures.");
3085 return false;
3086
3087 case GL_TEXTURE_CUBE_MAP_ARRAY:
3088 faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + (slice % 6);
3089 break;
3090
3091 case GL_TEXTURE_CUBE_MAP:
3092 faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + texImage->Face;
3093 break;
3094
3095 default:
3096 faceTarget = target;
3097 break;
3098 }
3099
3100 _mesa_meta_begin(ctx, MESA_META_ALL & ~(MESA_META_PIXEL_STORE |
3101 MESA_META_DRAW_BUFFERS));
3102 _mesa_ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
3103
3104 _mesa_reference_sampler_object(ctx, &samp_obj_save,
3105 ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler);
3106
3107 /* Create/bind FBO/renderbuffer */
3108 if (decompress_fbo->fb == NULL) {
3109 decompress_fbo->rb = ctx->Driver.NewRenderbuffer(ctx, 0xDEADBEEF);
3110 if (decompress_fbo->rb == NULL) {
3111 _mesa_meta_end(ctx);
3112 return false;
3113 }
3114
3115 decompress_fbo->fb = ctx->Driver.NewFramebuffer(ctx, 0xDEADBEEF);
3116 if (decompress_fbo->fb == NULL) {
3117 _mesa_meta_end(ctx);
3118 return false;
3119 }
3120
3121 _mesa_bind_framebuffers(ctx, decompress_fbo->fb, decompress_fbo->fb);
3122 _mesa_framebuffer_renderbuffer(ctx, ctx->DrawBuffer, GL_COLOR_ATTACHMENT0,
3123 decompress_fbo->rb);
3124 }
3125 else {
3126 _mesa_bind_framebuffers(ctx, decompress_fbo->fb, decompress_fbo->fb);
3127 }
3128
3129 /* alloc dest surface */
3130 if (width > decompress_fbo->Width || height > decompress_fbo->Height) {
3131 _mesa_renderbuffer_storage(ctx, decompress_fbo->rb, rbFormat,
3132 width, height, 0, 0);
3133
3134 /* Do the full completeness check to recompute
3135 * ctx->DrawBuffer->Width/Height.
3136 */
3137 ctx->DrawBuffer->_Status = GL_FRAMEBUFFER_UNDEFINED;
3138 status = _mesa_check_framebuffer_status(ctx, ctx->DrawBuffer);
3139 if (status != GL_FRAMEBUFFER_COMPLETE) {
3140 /* If the framebuffer isn't complete then we'll leave
3141 * decompress_fbo->Width as zero so that it will fail again next time
3142 * too */
3143 _mesa_meta_end(ctx);
3144 return false;
3145 }
3146 decompress_fbo->Width = width;
3147 decompress_fbo->Height = height;
3148 }
3149
3150 if (use_glsl_version) {
3151 _mesa_meta_setup_vertex_objects(ctx, &decompress->VAO,
3152 &decompress->buf_obj, true,
3153 2, 4, 0);
3154
3155 _mesa_meta_setup_blit_shader(ctx, target, false, &decompress->shaders);
3156 } else {
3157 _mesa_meta_setup_ff_tnl_for_blit(ctx, &decompress->VAO,
3158 &decompress->buf_obj, 3);
3159 }
3160
3161 if (decompress->samp_obj == NULL) {
3162 decompress->samp_obj = ctx->Driver.NewSamplerObject(ctx, 0xDEADBEEF);
3163 if (decompress->samp_obj == NULL) {
3164 _mesa_meta_end(ctx);
3165
3166 /* This is a bit lazy. Flag out of memory, and then don't bother to
3167 * clean up. Once out of memory is flagged, the only realistic next
3168 * move is to destroy the context. That will trigger all the right
3169 * clean up.
3170 *
3171 * Returning true prevents other GetTexImage methods from attempting
3172 * anything since they will likely fail too.
3173 */
3174 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
3175 return true;
3176 }
3177
3178 /* nearest filtering */
3179 _mesa_set_sampler_filters(ctx, decompress->samp_obj, GL_NEAREST, GL_NEAREST);
3180
3181 /* We don't want to encode or decode sRGB values; treat them as linear. */
3182 _mesa_set_sampler_srgb_decode(ctx, decompress->samp_obj, GL_SKIP_DECODE_EXT);
3183 }
3184
3185 _mesa_bind_sampler(ctx, ctx->Texture.CurrentUnit, decompress->samp_obj);
3186
3187 /* Silence valgrind warnings about reading uninitialized stack. */
3188 memset(verts, 0, sizeof(verts));
3189
3190 _mesa_meta_setup_texture_coords(faceTarget, slice,
3191 xoffset, yoffset, width, height,
3192 texImage->Width, texImage->Height,
3193 texImage->Depth,
3194 verts[0].tex,
3195 verts[1].tex,
3196 verts[2].tex,
3197 verts[3].tex);
3198
3199 /* setup vertex positions */
3200 verts[0].x = -1.0F;
3201 verts[0].y = -1.0F;
3202 verts[1].x = 1.0F;
3203 verts[1].y = -1.0F;
3204 verts[2].x = 1.0F;
3205 verts[2].y = 1.0F;
3206 verts[3].x = -1.0F;
3207 verts[3].y = 1.0F;
3208
3209 _mesa_set_viewport(ctx, 0, 0, 0, width, height);
3210
3211 /* upload new vertex data */
3212 _mesa_buffer_sub_data(ctx, decompress->buf_obj, 0, sizeof(verts), verts);
3213
3214 /* setup texture state */
3215 _mesa_bind_texture(ctx, target, texObj);
3216
3217 if (!use_glsl_version)
3218 _mesa_set_enable(ctx, target, GL_TRUE);
3219
3220 {
3221 /* save texture object state */
3222 const GLint baseLevelSave = texObj->BaseLevel;
3223 const GLint maxLevelSave = texObj->MaxLevel;
3224
3225 /* restrict sampling to the texture level of interest */
3226 if (target != GL_TEXTURE_RECTANGLE_ARB) {
3227 _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_BASE_LEVEL,
3228 (GLint *) &texImage->Level, false);
3229 _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_MAX_LEVEL,
3230 (GLint *) &texImage->Level, false);
3231 }
3232
3233 /* render quad w/ texture into renderbuffer */
3234 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
3235
3236 /* Restore texture object state, the texture binding will
3237 * be restored by _mesa_meta_end().
3238 */
3239 if (target != GL_TEXTURE_RECTANGLE_ARB) {
3240 _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_BASE_LEVEL,
3241 &baseLevelSave, false);
3242 _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_MAX_LEVEL,
3243 &maxLevelSave, false);
3244 }
3245
3246 }
3247
3248 /* read pixels from renderbuffer */
3249 {
3250 GLenum baseTexFormat = texImage->_BaseFormat;
3251 GLenum destBaseFormat = _mesa_unpack_format_to_base_format(destFormat);
3252
3253 /* The pixel transfer state will be set to default values at this point
3254 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
3255 * turned off (as required by glGetTexImage) but we need to handle some
3256 * special cases. In particular, single-channel texture values are
3257 * returned as red and two-channel texture values are returned as
3258 * red/alpha.
3259 */
3260 if (_mesa_need_luminance_to_rgb_conversion(baseTexFormat,
3261 destBaseFormat) ||
3262 /* If we're reading back an RGB(A) texture (using glGetTexImage) as
3263 * luminance then we need to return L=tex(R).
3264 */
3265 _mesa_need_rgb_to_luminance_conversion(baseTexFormat,
3266 destBaseFormat)) {
3267 /* Green and blue must be zero */
3268 _mesa_PixelTransferf(GL_GREEN_SCALE, 0.0f);
3269 _mesa_PixelTransferf(GL_BLUE_SCALE, 0.0f);
3270 }
3271
3272 _mesa_ReadPixels(0, 0, width, height, destFormat, destType, dest);
3273 }
3274
3275 /* disable texture unit */
3276 if (!use_glsl_version)
3277 _mesa_set_enable(ctx, target, GL_FALSE);
3278
3279 _mesa_bind_sampler(ctx, ctx->Texture.CurrentUnit, samp_obj_save);
3280 _mesa_reference_sampler_object(ctx, &samp_obj_save, NULL);
3281
3282 _mesa_meta_end(ctx);
3283
3284 return true;
3285 }
3286
3287
3288 /**
3289 * This is just a wrapper around _mesa_get_tex_image() and
3290 * decompress_texture_image(). Meta functions should not be directly called
3291 * from core Mesa.
3292 */
3293 void
3294 _mesa_meta_GetTexSubImage(struct gl_context *ctx,
3295 GLint xoffset, GLint yoffset, GLint zoffset,
3296 GLsizei width, GLsizei height, GLsizei depth,
3297 GLenum format, GLenum type, GLvoid *pixels,
3298 struct gl_texture_image *texImage)
3299 {
3300 if (_mesa_is_format_compressed(texImage->TexFormat)) {
3301 GLuint slice;
3302 bool result = true;
3303
3304 for (slice = 0; slice < depth; slice++) {
3305 void *dst;
3306 /* Section 8.11.4 (Texture Image Queries) of the GL 4.5 spec says:
3307 *
3308 * "For three-dimensional, two-dimensional array, cube map array,
3309 * and cube map textures pixel storage operations are applied as
3310 * if the image were two-dimensional, except that the additional
3311 * pixel storage state values PACK_IMAGE_HEIGHT and
3312 * PACK_SKIP_IMAGES are applied. The correspondence of texels to
3313 * memory locations is as defined for TexImage3D in section 8.5."
3314 */
3315 switch (texImage->TexObject->Target) {
3316 case GL_TEXTURE_3D:
3317 case GL_TEXTURE_2D_ARRAY:
3318 case GL_TEXTURE_CUBE_MAP:
3319 case GL_TEXTURE_CUBE_MAP_ARRAY: {
3320 /* Setup pixel packing. SkipPixels and SkipRows will be applied
3321 * in the decompress_texture_image() function's call to
3322 * glReadPixels but we need to compute the dest slice's address
3323 * here (according to SkipImages and ImageHeight).
3324 */
3325 struct gl_pixelstore_attrib packing = ctx->Pack;
3326 packing.SkipPixels = 0;
3327 packing.SkipRows = 0;
3328 dst = _mesa_image_address3d(&packing, pixels, width, height,
3329 format, type, slice, 0, 0);
3330 break;
3331 }
3332 default:
3333 dst = pixels;
3334 break;
3335 }
3336 result = decompress_texture_image(ctx, texImage, slice,
3337 xoffset, yoffset, width, height,
3338 format, type, dst);
3339 if (!result)
3340 break;
3341 }
3342
3343 if (result)
3344 return;
3345 }
3346
3347 _mesa_GetTexSubImage_sw(ctx, xoffset, yoffset, zoffset,
3348 width, height, depth, format, type, pixels, texImage);
3349 }
3350
3351
3352 /**
3353 * Meta implementation of ctx->Driver.DrawTex() in terms
3354 * of polygon rendering.
3355 */
3356 void
3357 _mesa_meta_DrawTex(struct gl_context *ctx, GLfloat x, GLfloat y, GLfloat z,
3358 GLfloat width, GLfloat height)
3359 {
3360 struct drawtex_state *drawtex = &ctx->Meta->DrawTex;
3361 struct vertex {
3362 GLfloat x, y, z, st[MAX_TEXTURE_UNITS][2];
3363 };
3364 struct vertex verts[4];
3365 GLuint i;
3366
3367 _mesa_meta_begin(ctx, (MESA_META_RASTERIZATION |
3368 MESA_META_SHADER |
3369 MESA_META_TRANSFORM |
3370 MESA_META_VERTEX |
3371 MESA_META_VIEWPORT));
3372
3373 if (drawtex->VAO == 0) {
3374 /* one-time setup */
3375 struct gl_vertex_array_object *array_obj;
3376
3377 /* create vertex array object */
3378 _mesa_GenVertexArrays(1, &drawtex->VAO);
3379 _mesa_BindVertexArray(drawtex->VAO);
3380
3381 array_obj = _mesa_lookup_vao(ctx, drawtex->VAO);
3382 assert(array_obj != NULL);
3383
3384 /* create vertex array buffer */
3385 drawtex->buf_obj = ctx->Driver.NewBufferObject(ctx, 0xDEADBEEF);
3386 if (drawtex->buf_obj == NULL)
3387 return;
3388
3389 _mesa_buffer_data(ctx, drawtex->buf_obj, GL_NONE, sizeof(verts), verts,
3390 GL_DYNAMIC_DRAW, __func__);
3391
3392 /* setup vertex arrays */
3393 FLUSH_VERTICES(ctx, 0);
3394 _mesa_update_array_format(ctx, array_obj, VERT_ATTRIB_POS,
3395 3, GL_FLOAT, GL_RGBA, GL_FALSE,
3396 GL_FALSE, GL_FALSE,
3397 offsetof(struct vertex, x));
3398 _mesa_bind_vertex_buffer(ctx, array_obj, VERT_ATTRIB_POS,
3399 drawtex->buf_obj, 0, sizeof(struct vertex));
3400 _mesa_enable_vertex_array_attrib(ctx, array_obj, VERT_ATTRIB_POS);
3401
3402
3403 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
3404 FLUSH_VERTICES(ctx, 0);
3405 _mesa_update_array_format(ctx, array_obj, VERT_ATTRIB_TEX(i),
3406 2, GL_FLOAT, GL_RGBA, GL_FALSE,
3407 GL_FALSE, GL_FALSE,
3408 offsetof(struct vertex, st[i]));
3409 _mesa_bind_vertex_buffer(ctx, array_obj, VERT_ATTRIB_TEX(i),
3410 drawtex->buf_obj, 0, sizeof(struct vertex));
3411 _mesa_enable_vertex_array_attrib(ctx, array_obj, VERT_ATTRIB_TEX(i));
3412 }
3413 }
3414 else {
3415 _mesa_BindVertexArray(drawtex->VAO);
3416 }
3417
3418 /* vertex positions, texcoords */
3419 {
3420 const GLfloat x1 = x + width;
3421 const GLfloat y1 = y + height;
3422
3423 z = CLAMP(z, 0.0f, 1.0f);
3424 z = invert_z(z);
3425
3426 verts[0].x = x;
3427 verts[0].y = y;
3428 verts[0].z = z;
3429
3430 verts[1].x = x1;
3431 verts[1].y = y;
3432 verts[1].z = z;
3433
3434 verts[2].x = x1;
3435 verts[2].y = y1;
3436 verts[2].z = z;
3437
3438 verts[3].x = x;
3439 verts[3].y = y1;
3440 verts[3].z = z;
3441
3442 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
3443 const struct gl_texture_object *texObj;
3444 const struct gl_texture_image *texImage;
3445 GLfloat s, t, s1, t1;
3446 GLuint tw, th;
3447
3448 if (!ctx->Texture.Unit[i]._Current) {
3449 GLuint j;
3450 for (j = 0; j < 4; j++) {
3451 verts[j].st[i][0] = 0.0f;
3452 verts[j].st[i][1] = 0.0f;
3453 }
3454 continue;
3455 }
3456
3457 texObj = ctx->Texture.Unit[i]._Current;
3458 texImage = texObj->Image[0][texObj->BaseLevel];
3459 tw = texImage->Width2;
3460 th = texImage->Height2;
3461
3462 s = (GLfloat) texObj->CropRect[0] / tw;
3463 t = (GLfloat) texObj->CropRect[1] / th;
3464 s1 = (GLfloat) (texObj->CropRect[0] + texObj->CropRect[2]) / tw;
3465 t1 = (GLfloat) (texObj->CropRect[1] + texObj->CropRect[3]) / th;
3466
3467 verts[0].st[i][0] = s;
3468 verts[0].st[i][1] = t;
3469
3470 verts[1].st[i][0] = s1;
3471 verts[1].st[i][1] = t;
3472
3473 verts[2].st[i][0] = s1;
3474 verts[2].st[i][1] = t1;
3475
3476 verts[3].st[i][0] = s;
3477 verts[3].st[i][1] = t1;
3478 }
3479
3480 _mesa_buffer_sub_data(ctx, drawtex->buf_obj, 0, sizeof(verts), verts);
3481 }
3482
3483 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
3484
3485 _mesa_meta_end(ctx);
3486 }
3487
3488 static bool
3489 cleartexsubimage_color(struct gl_context *ctx,
3490 struct gl_texture_image *texImage,
3491 const GLvoid *clearValue,
3492 GLint zoffset)
3493 {
3494 mesa_format format;
3495 union gl_color_union colorValue;
3496 GLenum datatype;
3497 GLenum status;
3498
3499 _mesa_meta_framebuffer_texture_image(ctx, ctx->DrawBuffer,
3500 GL_COLOR_ATTACHMENT0,
3501 texImage, zoffset);
3502
3503 status = _mesa_check_framebuffer_status(ctx, ctx->DrawBuffer);
3504 if (status != GL_FRAMEBUFFER_COMPLETE)
3505 return false;
3506
3507 /* We don't want to apply an sRGB conversion so override the format */
3508 format = _mesa_get_srgb_format_linear(texImage->TexFormat);
3509 datatype = _mesa_get_format_datatype(format);
3510
3511 switch (datatype) {
3512 case GL_UNSIGNED_INT:
3513 case GL_INT:
3514 if (clearValue)
3515 _mesa_unpack_uint_rgba_row(format, 1, clearValue,
3516 (GLuint (*)[4]) colorValue.ui);
3517 else
3518 memset(&colorValue, 0, sizeof colorValue);
3519 if (datatype == GL_INT)
3520 _mesa_ClearBufferiv(GL_COLOR, 0, colorValue.i);
3521 else
3522 _mesa_ClearBufferuiv(GL_COLOR, 0, colorValue.ui);
3523 break;
3524 default:
3525 if (clearValue)
3526 _mesa_unpack_rgba_row(format, 1, clearValue,
3527 (GLfloat (*)[4]) colorValue.f);
3528 else
3529 memset(&colorValue, 0, sizeof colorValue);
3530 _mesa_ClearBufferfv(GL_COLOR, 0, colorValue.f);
3531 break;
3532 }
3533
3534 return true;
3535 }
3536
3537 static bool
3538 cleartexsubimage_depth_stencil(struct gl_context *ctx,
3539 struct gl_texture_image *texImage,
3540 const GLvoid *clearValue,
3541 GLint zoffset)
3542 {
3543 GLint stencilValue = 0;
3544 GLfloat depthValue = 0.0f;
3545 GLenum status;
3546
3547 _mesa_meta_framebuffer_texture_image(ctx, ctx->DrawBuffer,
3548 GL_DEPTH_ATTACHMENT,
3549 texImage, zoffset);
3550
3551 if (texImage->_BaseFormat == GL_DEPTH_STENCIL)
3552 _mesa_meta_framebuffer_texture_image(ctx, ctx->DrawBuffer,
3553 GL_STENCIL_ATTACHMENT,
3554 texImage, zoffset);
3555
3556 status = _mesa_check_framebuffer_status(ctx, ctx->DrawBuffer);
3557 if (status != GL_FRAMEBUFFER_COMPLETE)
3558 return false;
3559
3560 if (clearValue) {
3561 GLuint depthStencilValue[2];
3562
3563 /* Convert the clearValue from whatever format it's in to a floating
3564 * point value for the depth and an integer value for the stencil index
3565 */
3566 if (texImage->_BaseFormat == GL_DEPTH_STENCIL) {
3567 _mesa_unpack_float_32_uint_24_8_depth_stencil_row(texImage->TexFormat,
3568 1, /* n */
3569 clearValue,
3570 depthStencilValue);
3571 /* We need a memcpy here instead of a cast because we need to
3572 * reinterpret the bytes as a float rather than converting it
3573 */
3574 memcpy(&depthValue, depthStencilValue, sizeof depthValue);
3575 stencilValue = depthStencilValue[1] & 0xff;
3576 } else {
3577 _mesa_unpack_float_z_row(texImage->TexFormat, 1 /* n */,
3578 clearValue, &depthValue);
3579 }
3580 }
3581
3582 if (texImage->_BaseFormat == GL_DEPTH_STENCIL)
3583 _mesa_ClearBufferfi(GL_DEPTH_STENCIL, 0, depthValue, stencilValue);
3584 else
3585 _mesa_ClearBufferfv(GL_DEPTH, 0, &depthValue);
3586
3587 return true;
3588 }
3589
3590 static bool
3591 cleartexsubimage_for_zoffset(struct gl_context *ctx,
3592 struct gl_texture_image *texImage,
3593 GLint zoffset,
3594 const GLvoid *clearValue)
3595 {
3596 struct gl_framebuffer *drawFb;
3597 bool success;
3598
3599 drawFb = ctx->Driver.NewFramebuffer(ctx, 0xDEADBEEF);
3600 if (drawFb == NULL)
3601 return false;
3602
3603 _mesa_bind_framebuffers(ctx, drawFb, ctx->ReadBuffer);
3604
3605 switch(texImage->_BaseFormat) {
3606 case GL_DEPTH_STENCIL:
3607 case GL_DEPTH_COMPONENT:
3608 success = cleartexsubimage_depth_stencil(ctx, texImage,
3609 clearValue, zoffset);
3610 break;
3611 default:
3612 success = cleartexsubimage_color(ctx, texImage, clearValue, zoffset);
3613 break;
3614 }
3615
3616 _mesa_reference_framebuffer(&drawFb, NULL);
3617
3618 return success;
3619 }
3620
3621 static bool
3622 cleartexsubimage_using_fbo(struct gl_context *ctx,
3623 struct gl_texture_image *texImage,
3624 GLint xoffset, GLint yoffset, GLint zoffset,
3625 GLsizei width, GLsizei height, GLsizei depth,
3626 const GLvoid *clearValue)
3627 {
3628 bool success = true;
3629 GLint z;
3630
3631 _mesa_meta_begin(ctx,
3632 MESA_META_SCISSOR |
3633 MESA_META_COLOR_MASK |
3634 MESA_META_DITHER |
3635 MESA_META_FRAMEBUFFER_SRGB);
3636
3637 _mesa_ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
3638 _mesa_set_enable(ctx, GL_DITHER, GL_FALSE);
3639
3640 _mesa_set_enable(ctx, GL_SCISSOR_TEST, GL_TRUE);
3641 _mesa_Scissor(xoffset, yoffset, width, height);
3642
3643 for (z = zoffset; z < zoffset + depth; z++) {
3644 if (!cleartexsubimage_for_zoffset(ctx, texImage, z, clearValue)) {
3645 success = false;
3646 break;
3647 }
3648 }
3649
3650 _mesa_meta_end(ctx);
3651
3652 return success;
3653 }
3654
3655 extern void
3656 _mesa_meta_ClearTexSubImage(struct gl_context *ctx,
3657 struct gl_texture_image *texImage,
3658 GLint xoffset, GLint yoffset, GLint zoffset,
3659 GLsizei width, GLsizei height, GLsizei depth,
3660 const GLvoid *clearValue)
3661 {
3662 bool res;
3663
3664 res = cleartexsubimage_using_fbo(ctx, texImage,
3665 xoffset, yoffset, zoffset,
3666 width, height, depth,
3667 clearValue);
3668
3669 if (res)
3670 return;
3671
3672 _mesa_warning(ctx,
3673 "Falling back to mapping the texture in "
3674 "glClearTexSubImage\n");
3675
3676 _mesa_store_cleartexsubimage(ctx, texImage,
3677 xoffset, yoffset, zoffset,
3678 width, height, depth,
3679 clearValue);
3680 }