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