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