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