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