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