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