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