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