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