mesa/sso: rename Shader to the pointer _Shader
[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_CreateShader(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_DeleteShader(shader);
114 return 0;
115 }
116
117 info = malloc(size);
118 if (!info) {
119 _mesa_DeleteShader(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_DeleteShader(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_CreateProgram();
245 _mesa_AttachShader(shader->shader_prog, fs);
246 _mesa_DeleteShader(fs);
247 _mesa_AttachShader(shader->shader_prog, vs);
248 _mesa_DeleteShader(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 save->DrawBufferName = ctx->DrawBuffer->Name;
738 save->ReadBufferName = ctx->ReadBuffer->Name;
739 save->RenderbufferName = (ctx->CurrentRenderbuffer ?
740 ctx->CurrentRenderbuffer->Name : 0);
741 }
742 }
743
744
745 /**
746 * Leave meta state. This is like a light-weight version of glPopAttrib().
747 */
748 void
749 _mesa_meta_end(struct gl_context *ctx)
750 {
751 struct save_state *save = &ctx->Meta->Save[ctx->Meta->SaveStackDepth - 1];
752 const GLbitfield state = save->SavedState;
753 int i;
754
755 /* After starting a new occlusion query, initialize the results to the
756 * values saved previously. The driver will then continue to increment
757 * these values.
758 */
759 if (state & MESA_META_OCCLUSION_QUERY) {
760 if (save->CurrentOcclusionObject) {
761 _mesa_BeginQuery(save->CurrentOcclusionObject->Target,
762 save->CurrentOcclusionObject->Id);
763 ctx->Query.CurrentOcclusionObject->Result = save->CurrentOcclusionObject->Result;
764 }
765 }
766
767 if (state & MESA_META_ALPHA_TEST) {
768 if (ctx->Color.AlphaEnabled != save->AlphaEnabled)
769 _mesa_set_enable(ctx, GL_ALPHA_TEST, save->AlphaEnabled);
770 _mesa_AlphaFunc(save->AlphaFunc, save->AlphaRef);
771 }
772
773 if (state & MESA_META_BLEND) {
774 if (ctx->Color.BlendEnabled != save->BlendEnabled) {
775 if (ctx->Extensions.EXT_draw_buffers2) {
776 GLuint i;
777 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
778 _mesa_set_enablei(ctx, GL_BLEND, i, (save->BlendEnabled >> i) & 1);
779 }
780 }
781 else {
782 _mesa_set_enable(ctx, GL_BLEND, (save->BlendEnabled & 1));
783 }
784 }
785 if (ctx->Color.ColorLogicOpEnabled != save->ColorLogicOpEnabled)
786 _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP, save->ColorLogicOpEnabled);
787 }
788
789 if (state & MESA_META_COLOR_MASK) {
790 GLuint i;
791 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
792 if (!TEST_EQ_4V(ctx->Color.ColorMask[i], save->ColorMask[i])) {
793 if (i == 0) {
794 _mesa_ColorMask(save->ColorMask[i][0], save->ColorMask[i][1],
795 save->ColorMask[i][2], save->ColorMask[i][3]);
796 }
797 else {
798 _mesa_ColorMaski(i,
799 save->ColorMask[i][0],
800 save->ColorMask[i][1],
801 save->ColorMask[i][2],
802 save->ColorMask[i][3]);
803 }
804 }
805 }
806 }
807
808 if (state & MESA_META_DEPTH_TEST) {
809 if (ctx->Depth.Test != save->Depth.Test)
810 _mesa_set_enable(ctx, GL_DEPTH_TEST, save->Depth.Test);
811 _mesa_DepthFunc(save->Depth.Func);
812 _mesa_DepthMask(save->Depth.Mask);
813 }
814
815 if (state & MESA_META_FOG) {
816 _mesa_set_enable(ctx, GL_FOG, save->Fog);
817 }
818
819 if (state & MESA_META_PIXEL_STORE) {
820 ctx->Pack = save->Pack;
821 ctx->Unpack = save->Unpack;
822 }
823
824 if (state & MESA_META_PIXEL_TRANSFER) {
825 ctx->Pixel.RedScale = save->RedScale;
826 ctx->Pixel.RedBias = save->RedBias;
827 ctx->Pixel.GreenScale = save->GreenScale;
828 ctx->Pixel.GreenBias = save->GreenBias;
829 ctx->Pixel.BlueScale = save->BlueScale;
830 ctx->Pixel.BlueBias = save->BlueBias;
831 ctx->Pixel.AlphaScale = save->AlphaScale;
832 ctx->Pixel.AlphaBias = save->AlphaBias;
833 ctx->Pixel.MapColorFlag = save->MapColorFlag;
834 /* XXX more state */
835 ctx->NewState |=_NEW_PIXEL;
836 }
837
838 if (state & MESA_META_RASTERIZATION) {
839 _mesa_PolygonMode(GL_FRONT, save->FrontPolygonMode);
840 _mesa_PolygonMode(GL_BACK, save->BackPolygonMode);
841 _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, save->PolygonStipple);
842 _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, save->PolygonSmooth);
843 _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL, save->PolygonOffset);
844 _mesa_set_enable(ctx, GL_CULL_FACE, save->PolygonCull);
845 }
846
847 if (state & MESA_META_SCISSOR) {
848 unsigned i;
849
850 for (i = 0; i < ctx->Const.MaxViewports; i++) {
851 _mesa_set_scissor(ctx, i,
852 save->Scissor.ScissorArray[i].X,
853 save->Scissor.ScissorArray[i].Y,
854 save->Scissor.ScissorArray[i].Width,
855 save->Scissor.ScissorArray[i].Height);
856 _mesa_set_enablei(ctx, GL_SCISSOR_TEST, i,
857 (save->Scissor.EnableFlags >> i) & 1);
858 }
859 }
860
861 if (state & MESA_META_SHADER) {
862 if (ctx->Extensions.ARB_vertex_program) {
863 _mesa_set_enable(ctx, GL_VERTEX_PROGRAM_ARB,
864 save->VertexProgramEnabled);
865 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
866 save->VertexProgram);
867 _mesa_reference_vertprog(ctx, &save->VertexProgram, NULL);
868 }
869
870 if (ctx->Extensions.ARB_fragment_program) {
871 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB,
872 save->FragmentProgramEnabled);
873 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
874 save->FragmentProgram);
875 _mesa_reference_fragprog(ctx, &save->FragmentProgram, NULL);
876 }
877
878 if (ctx->Extensions.ATI_fragment_shader) {
879 _mesa_set_enable(ctx, GL_FRAGMENT_SHADER_ATI,
880 save->ATIFragmentShaderEnabled);
881 }
882
883 if (ctx->Extensions.ARB_vertex_shader) {
884 _mesa_use_shader_program(ctx, GL_VERTEX_SHADER,
885 save->Shader[MESA_SHADER_VERTEX]);
886 }
887
888 if (_mesa_has_geometry_shaders(ctx))
889 _mesa_use_shader_program(ctx, GL_GEOMETRY_SHADER_ARB,
890 save->Shader[MESA_SHADER_GEOMETRY]);
891
892 if (ctx->Extensions.ARB_fragment_shader)
893 _mesa_use_shader_program(ctx, GL_FRAGMENT_SHADER,
894 save->Shader[MESA_SHADER_FRAGMENT]);
895
896 _mesa_reference_shader_program(ctx, &ctx->_Shader->ActiveProgram,
897 save->ActiveShader);
898
899 for (i = 0; i < MESA_SHADER_STAGES; i++)
900 _mesa_reference_shader_program(ctx, &save->Shader[i], NULL);
901 _mesa_reference_shader_program(ctx, &save->ActiveShader, NULL);
902 }
903
904 if (state & MESA_META_STENCIL_TEST) {
905 const struct gl_stencil_attrib *stencil = &save->Stencil;
906
907 _mesa_set_enable(ctx, GL_STENCIL_TEST, stencil->Enabled);
908 _mesa_ClearStencil(stencil->Clear);
909 if (ctx->Extensions.EXT_stencil_two_side) {
910 _mesa_set_enable(ctx, GL_STENCIL_TEST_TWO_SIDE_EXT,
911 stencil->TestTwoSide);
912 _mesa_ActiveStencilFaceEXT(stencil->ActiveFace
913 ? GL_BACK : GL_FRONT);
914 }
915 /* front state */
916 _mesa_StencilFuncSeparate(GL_FRONT,
917 stencil->Function[0],
918 stencil->Ref[0],
919 stencil->ValueMask[0]);
920 _mesa_StencilMaskSeparate(GL_FRONT, stencil->WriteMask[0]);
921 _mesa_StencilOpSeparate(GL_FRONT, stencil->FailFunc[0],
922 stencil->ZFailFunc[0],
923 stencil->ZPassFunc[0]);
924 /* back state */
925 _mesa_StencilFuncSeparate(GL_BACK,
926 stencil->Function[1],
927 stencil->Ref[1],
928 stencil->ValueMask[1]);
929 _mesa_StencilMaskSeparate(GL_BACK, stencil->WriteMask[1]);
930 _mesa_StencilOpSeparate(GL_BACK, stencil->FailFunc[1],
931 stencil->ZFailFunc[1],
932 stencil->ZPassFunc[1]);
933 }
934
935 if (state & MESA_META_TEXTURE) {
936 GLuint u, tgt;
937
938 ASSERT(ctx->Texture.CurrentUnit == 0);
939
940 /* restore texenv for unit[0] */
941 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, save->EnvMode);
942
943 /* restore texture objects for unit[0] only */
944 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
945 if (ctx->Texture.Unit[0].CurrentTex[tgt] != save->CurrentTexture[tgt]) {
946 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
947 _mesa_reference_texobj(&ctx->Texture.Unit[0].CurrentTex[tgt],
948 save->CurrentTexture[tgt]);
949 }
950 _mesa_reference_texobj(&save->CurrentTexture[tgt], NULL);
951 }
952
953 /* Restore fixed function texture enables, texgen */
954 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
955 if (ctx->Texture.Unit[u].Enabled != save->TexEnabled[u]) {
956 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
957 ctx->Texture.Unit[u].Enabled = save->TexEnabled[u];
958 }
959
960 if (ctx->Texture.Unit[u].TexGenEnabled != save->TexGenEnabled[u]) {
961 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
962 ctx->Texture.Unit[u].TexGenEnabled = save->TexGenEnabled[u];
963 }
964 }
965
966 /* restore current unit state */
967 _mesa_ActiveTexture(GL_TEXTURE0 + save->ActiveUnit);
968 _mesa_ClientActiveTexture(GL_TEXTURE0 + save->ClientActiveUnit);
969 }
970
971 if (state & MESA_META_TRANSFORM) {
972 GLuint activeTexture = ctx->Texture.CurrentUnit;
973 _mesa_ActiveTexture(GL_TEXTURE0);
974 _mesa_MatrixMode(GL_TEXTURE);
975 _mesa_LoadMatrixf(save->TextureMatrix);
976 _mesa_ActiveTexture(GL_TEXTURE0 + activeTexture);
977
978 _mesa_MatrixMode(GL_MODELVIEW);
979 _mesa_LoadMatrixf(save->ModelviewMatrix);
980
981 _mesa_MatrixMode(GL_PROJECTION);
982 _mesa_LoadMatrixf(save->ProjectionMatrix);
983
984 _mesa_MatrixMode(save->MatrixMode);
985 }
986
987 if (state & MESA_META_CLIP) {
988 if (save->ClipPlanesEnabled) {
989 GLuint i;
990 for (i = 0; i < ctx->Const.MaxClipPlanes; i++) {
991 if (save->ClipPlanesEnabled & (1 << i)) {
992 _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_TRUE);
993 }
994 }
995 }
996 }
997
998 if (state & MESA_META_VERTEX) {
999 /* restore vertex buffer object */
1000 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, save->ArrayBufferObj->Name);
1001 _mesa_reference_buffer_object(ctx, &save->ArrayBufferObj, NULL);
1002
1003 /* restore vertex array object */
1004 _mesa_BindVertexArray(save->VAO->Name);
1005 _mesa_reference_vao(ctx, &save->VAO, NULL);
1006 }
1007
1008 if (state & MESA_META_VIEWPORT) {
1009 if (save->ViewportX != ctx->ViewportArray[0].X ||
1010 save->ViewportY != ctx->ViewportArray[0].Y ||
1011 save->ViewportW != ctx->ViewportArray[0].Width ||
1012 save->ViewportH != ctx->ViewportArray[0].Height) {
1013 _mesa_set_viewport(ctx, 0, save->ViewportX, save->ViewportY,
1014 save->ViewportW, save->ViewportH);
1015 }
1016 _mesa_DepthRange(save->DepthNear, save->DepthFar);
1017 }
1018
1019 if (state & MESA_META_CLAMP_FRAGMENT_COLOR &&
1020 ctx->Extensions.ARB_color_buffer_float) {
1021 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR, save->ClampFragmentColor);
1022 }
1023
1024 if (state & MESA_META_CLAMP_VERTEX_COLOR &&
1025 ctx->Extensions.ARB_color_buffer_float) {
1026 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR, save->ClampVertexColor);
1027 }
1028
1029 if (state & MESA_META_CONDITIONAL_RENDER) {
1030 if (save->CondRenderQuery)
1031 _mesa_BeginConditionalRender(save->CondRenderQuery->Id,
1032 save->CondRenderMode);
1033 }
1034
1035 if (state & MESA_META_SELECT_FEEDBACK) {
1036 if (save->RenderMode == GL_SELECT) {
1037 _mesa_RenderMode(GL_SELECT);
1038 ctx->Select = save->Select;
1039 } else if (save->RenderMode == GL_FEEDBACK) {
1040 _mesa_RenderMode(GL_FEEDBACK);
1041 ctx->Feedback = save->Feedback;
1042 }
1043 }
1044
1045 if (state & MESA_META_MULTISAMPLE) {
1046 struct gl_multisample_attrib *ctx_ms = &ctx->Multisample;
1047 struct gl_multisample_attrib *save_ms = &save->Multisample;
1048
1049 if (ctx_ms->Enabled != save_ms->Enabled)
1050 _mesa_set_multisample(ctx, save_ms->Enabled);
1051 if (ctx_ms->SampleCoverage != save_ms->SampleCoverage)
1052 _mesa_set_enable(ctx, GL_SAMPLE_COVERAGE, save_ms->SampleCoverage);
1053 if (ctx_ms->SampleAlphaToCoverage != save_ms->SampleAlphaToCoverage)
1054 _mesa_set_enable(ctx, GL_SAMPLE_ALPHA_TO_COVERAGE, save_ms->SampleAlphaToCoverage);
1055 if (ctx_ms->SampleAlphaToOne != save_ms->SampleAlphaToOne)
1056 _mesa_set_enable(ctx, GL_SAMPLE_ALPHA_TO_ONE, save_ms->SampleAlphaToOne);
1057 if (ctx_ms->SampleCoverageValue != save_ms->SampleCoverageValue ||
1058 ctx_ms->SampleCoverageInvert != save_ms->SampleCoverageInvert) {
1059 _mesa_SampleCoverage(save_ms->SampleCoverageValue,
1060 save_ms->SampleCoverageInvert);
1061 }
1062 if (ctx_ms->SampleShading != save_ms->SampleShading)
1063 _mesa_set_enable(ctx, GL_SAMPLE_SHADING, save_ms->SampleShading);
1064 if (ctx_ms->SampleMask != save_ms->SampleMask)
1065 _mesa_set_enable(ctx, GL_SAMPLE_MASK, save_ms->SampleMask);
1066 if (ctx_ms->SampleMaskValue != save_ms->SampleMaskValue)
1067 _mesa_SampleMaski(0, save_ms->SampleMaskValue);
1068 if (ctx_ms->MinSampleShadingValue != save_ms->MinSampleShadingValue)
1069 _mesa_MinSampleShading(save_ms->MinSampleShadingValue);
1070 }
1071
1072 if (state & MESA_META_FRAMEBUFFER_SRGB) {
1073 if (ctx->Color.sRGBEnabled != save->sRGBEnabled)
1074 _mesa_set_framebuffer_srgb(ctx, save->sRGBEnabled);
1075 }
1076
1077 /* misc */
1078 if (save->Lighting) {
1079 _mesa_set_enable(ctx, GL_LIGHTING, GL_TRUE);
1080 }
1081 if (save->RasterDiscard) {
1082 _mesa_set_enable(ctx, GL_RASTERIZER_DISCARD, GL_TRUE);
1083 }
1084 if (save->TransformFeedbackNeedsResume)
1085 _mesa_ResumeTransformFeedback();
1086
1087 if (ctx->DrawBuffer->Name != save->DrawBufferName)
1088 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, save->DrawBufferName);
1089
1090 if (ctx->ReadBuffer->Name != save->ReadBufferName)
1091 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, save->ReadBufferName);
1092
1093 if (!ctx->CurrentRenderbuffer ||
1094 ctx->CurrentRenderbuffer->Name != save->RenderbufferName)
1095 _mesa_BindRenderbuffer(GL_RENDERBUFFER, save->RenderbufferName);
1096
1097 ctx->Meta->SaveStackDepth--;
1098
1099 ctx->API = save->API;
1100 }
1101
1102
1103 /**
1104 * Determine whether Mesa is currently in a meta state.
1105 */
1106 GLboolean
1107 _mesa_meta_in_progress(struct gl_context *ctx)
1108 {
1109 return ctx->Meta->SaveStackDepth != 0;
1110 }
1111
1112
1113 /**
1114 * Convert Z from a normalized value in the range [0, 1] to an object-space
1115 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1116 * default/identity ortho projection results in the original Z value.
1117 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1118 * value comes from the clear value or raster position.
1119 */
1120 static INLINE GLfloat
1121 invert_z(GLfloat normZ)
1122 {
1123 GLfloat objZ = 1.0f - 2.0f * normZ;
1124 return objZ;
1125 }
1126
1127
1128 /**
1129 * One-time init for a temp_texture object.
1130 * Choose tex target, compute max tex size, etc.
1131 */
1132 static void
1133 init_temp_texture(struct gl_context *ctx, struct temp_texture *tex)
1134 {
1135 /* prefer texture rectangle */
1136 if (_mesa_is_desktop_gl(ctx) && ctx->Extensions.NV_texture_rectangle) {
1137 tex->Target = GL_TEXTURE_RECTANGLE;
1138 tex->MaxSize = ctx->Const.MaxTextureRectSize;
1139 tex->NPOT = GL_TRUE;
1140 }
1141 else {
1142 /* use 2D texture, NPOT if possible */
1143 tex->Target = GL_TEXTURE_2D;
1144 tex->MaxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1145 tex->NPOT = ctx->Extensions.ARB_texture_non_power_of_two;
1146 }
1147 tex->MinSize = 16; /* 16 x 16 at least */
1148 assert(tex->MaxSize > 0);
1149
1150 _mesa_GenTextures(1, &tex->TexObj);
1151 }
1152
1153 static void
1154 cleanup_temp_texture(struct temp_texture *tex)
1155 {
1156 if (!tex->TexObj)
1157 return;
1158 _mesa_DeleteTextures(1, &tex->TexObj);
1159 tex->TexObj = 0;
1160 }
1161
1162
1163 /**
1164 * Return pointer to temp_texture info for non-bitmap ops.
1165 * This does some one-time init if needed.
1166 */
1167 struct temp_texture *
1168 _mesa_meta_get_temp_texture(struct gl_context *ctx)
1169 {
1170 struct temp_texture *tex = &ctx->Meta->TempTex;
1171
1172 if (!tex->TexObj) {
1173 init_temp_texture(ctx, tex);
1174 }
1175
1176 return tex;
1177 }
1178
1179
1180 /**
1181 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1182 * We use a separate texture for bitmaps to reduce texture
1183 * allocation/deallocation.
1184 */
1185 static struct temp_texture *
1186 get_bitmap_temp_texture(struct gl_context *ctx)
1187 {
1188 struct temp_texture *tex = &ctx->Meta->Bitmap.Tex;
1189
1190 if (!tex->TexObj) {
1191 init_temp_texture(ctx, tex);
1192 }
1193
1194 return tex;
1195 }
1196
1197 /**
1198 * Return pointer to depth temp_texture.
1199 * This does some one-time init if needed.
1200 */
1201 struct temp_texture *
1202 _mesa_meta_get_temp_depth_texture(struct gl_context *ctx)
1203 {
1204 struct temp_texture *tex = &ctx->Meta->Blit.depthTex;
1205
1206 if (!tex->TexObj) {
1207 init_temp_texture(ctx, tex);
1208 }
1209
1210 return tex;
1211 }
1212
1213 /**
1214 * Compute the width/height of texture needed to draw an image of the
1215 * given size. Return a flag indicating whether the current texture
1216 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1217 * allocated (glTexImage2D).
1218 * Also, compute s/t texcoords for drawing.
1219 *
1220 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1221 */
1222 GLboolean
1223 _mesa_meta_alloc_texture(struct temp_texture *tex,
1224 GLsizei width, GLsizei height, GLenum intFormat)
1225 {
1226 GLboolean newTex = GL_FALSE;
1227
1228 ASSERT(width <= tex->MaxSize);
1229 ASSERT(height <= tex->MaxSize);
1230
1231 if (width > tex->Width ||
1232 height > tex->Height ||
1233 intFormat != tex->IntFormat) {
1234 /* alloc new texture (larger or different format) */
1235
1236 if (tex->NPOT) {
1237 /* use non-power of two size */
1238 tex->Width = MAX2(tex->MinSize, width);
1239 tex->Height = MAX2(tex->MinSize, height);
1240 }
1241 else {
1242 /* find power of two size */
1243 GLsizei w, h;
1244 w = h = tex->MinSize;
1245 while (w < width)
1246 w *= 2;
1247 while (h < height)
1248 h *= 2;
1249 tex->Width = w;
1250 tex->Height = h;
1251 }
1252
1253 tex->IntFormat = intFormat;
1254
1255 newTex = GL_TRUE;
1256 }
1257
1258 /* compute texcoords */
1259 if (tex->Target == GL_TEXTURE_RECTANGLE) {
1260 tex->Sright = (GLfloat) width;
1261 tex->Ttop = (GLfloat) height;
1262 }
1263 else {
1264 tex->Sright = (GLfloat) width / tex->Width;
1265 tex->Ttop = (GLfloat) height / tex->Height;
1266 }
1267
1268 return newTex;
1269 }
1270
1271
1272 /**
1273 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1274 */
1275 void
1276 _mesa_meta_setup_copypix_texture(struct gl_context *ctx,
1277 struct temp_texture *tex,
1278 GLint srcX, GLint srcY,
1279 GLsizei width, GLsizei height,
1280 GLenum intFormat,
1281 GLenum filter)
1282 {
1283 bool newTex;
1284
1285 _mesa_BindTexture(tex->Target, tex->TexObj);
1286 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MIN_FILTER, filter);
1287 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MAG_FILTER, filter);
1288 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1289
1290 newTex = _mesa_meta_alloc_texture(tex, width, height, intFormat);
1291
1292 /* copy framebuffer image to texture */
1293 if (newTex) {
1294 /* create new tex image */
1295 if (tex->Width == width && tex->Height == height) {
1296 /* create new tex with framebuffer data */
1297 _mesa_CopyTexImage2D(tex->Target, 0, tex->IntFormat,
1298 srcX, srcY, width, height, 0);
1299 }
1300 else {
1301 /* create empty texture */
1302 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
1303 tex->Width, tex->Height, 0,
1304 intFormat, GL_UNSIGNED_BYTE, NULL);
1305 /* load image */
1306 _mesa_CopyTexSubImage2D(tex->Target, 0,
1307 0, 0, srcX, srcY, width, height);
1308 }
1309 }
1310 else {
1311 /* replace existing tex image */
1312 _mesa_CopyTexSubImage2D(tex->Target, 0,
1313 0, 0, srcX, srcY, width, height);
1314 }
1315 }
1316
1317
1318 /**
1319 * Setup/load texture for glDrawPixels.
1320 */
1321 void
1322 _mesa_meta_setup_drawpix_texture(struct gl_context *ctx,
1323 struct temp_texture *tex,
1324 GLboolean newTex,
1325 GLsizei width, GLsizei height,
1326 GLenum format, GLenum type,
1327 const GLvoid *pixels)
1328 {
1329 _mesa_BindTexture(tex->Target, tex->TexObj);
1330 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1331 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1332 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1333
1334 /* copy pixel data to texture */
1335 if (newTex) {
1336 /* create new tex image */
1337 if (tex->Width == width && tex->Height == height) {
1338 /* create new tex and load image data */
1339 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
1340 tex->Width, tex->Height, 0, format, type, pixels);
1341 }
1342 else {
1343 struct gl_buffer_object *save_unpack_obj = NULL;
1344
1345 _mesa_reference_buffer_object(ctx, &save_unpack_obj,
1346 ctx->Unpack.BufferObj);
1347 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
1348 /* create empty texture */
1349 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
1350 tex->Width, tex->Height, 0, format, type, NULL);
1351 if (save_unpack_obj != NULL)
1352 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB,
1353 save_unpack_obj->Name);
1354 /* load image */
1355 _mesa_TexSubImage2D(tex->Target, 0,
1356 0, 0, width, height, format, type, pixels);
1357 }
1358 }
1359 else {
1360 /* replace existing tex image */
1361 _mesa_TexSubImage2D(tex->Target, 0,
1362 0, 0, width, height, format, type, pixels);
1363 }
1364 }
1365
1366 void
1367 _mesa_meta_setup_ff_tnl_for_blit(GLuint *VAO, GLuint *VBO,
1368 unsigned texcoord_size)
1369 {
1370 _mesa_meta_setup_vertex_objects(VAO, VBO, false, 2, texcoord_size, 0);
1371
1372 /* setup projection matrix */
1373 _mesa_MatrixMode(GL_PROJECTION);
1374 _mesa_LoadIdentity();
1375 }
1376
1377 /**
1378 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1379 */
1380 void
1381 _mesa_meta_Clear(struct gl_context *ctx, GLbitfield buffers)
1382 {
1383 struct clear_state *clear = &ctx->Meta->Clear;
1384 struct vertex verts[4];
1385 /* save all state but scissor, pixel pack/unpack */
1386 GLbitfield metaSave = (MESA_META_ALL -
1387 MESA_META_SCISSOR -
1388 MESA_META_PIXEL_STORE -
1389 MESA_META_CONDITIONAL_RENDER -
1390 MESA_META_FRAMEBUFFER_SRGB);
1391 const GLuint stencilMax = (1 << ctx->DrawBuffer->Visual.stencilBits) - 1;
1392
1393 if (buffers & BUFFER_BITS_COLOR) {
1394 /* if clearing color buffers, don't save/restore colormask */
1395 metaSave -= MESA_META_COLOR_MASK;
1396 }
1397
1398 _mesa_meta_begin(ctx, metaSave);
1399
1400 _mesa_meta_setup_vertex_objects(&clear->VAO, &clear->VBO, false, 3, 0, 4);
1401
1402 /* GL_COLOR_BUFFER_BIT */
1403 if (buffers & BUFFER_BITS_COLOR) {
1404 /* leave colormask, glDrawBuffer state as-is */
1405
1406 /* Clears never have the color clamped. */
1407 if (ctx->Extensions.ARB_color_buffer_float)
1408 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
1409 }
1410 else {
1411 ASSERT(metaSave & MESA_META_COLOR_MASK);
1412 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1413 }
1414
1415 /* GL_DEPTH_BUFFER_BIT */
1416 if (buffers & BUFFER_BIT_DEPTH) {
1417 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE);
1418 _mesa_DepthFunc(GL_ALWAYS);
1419 _mesa_DepthMask(GL_TRUE);
1420 }
1421 else {
1422 assert(!ctx->Depth.Test);
1423 }
1424
1425 /* GL_STENCIL_BUFFER_BIT */
1426 if (buffers & BUFFER_BIT_STENCIL) {
1427 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
1428 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK,
1429 GL_REPLACE, GL_REPLACE, GL_REPLACE);
1430 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK, GL_ALWAYS,
1431 ctx->Stencil.Clear & stencilMax,
1432 ctx->Stencil.WriteMask[0]);
1433 }
1434 else {
1435 assert(!ctx->Stencil.Enabled);
1436 }
1437
1438 /* vertex positions/colors */
1439 {
1440 const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin;
1441 const GLfloat y0 = (GLfloat) ctx->DrawBuffer->_Ymin;
1442 const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax;
1443 const GLfloat y1 = (GLfloat) ctx->DrawBuffer->_Ymax;
1444 const GLfloat z = invert_z(ctx->Depth.Clear);
1445 GLuint i;
1446
1447 verts[0].x = x0;
1448 verts[0].y = y0;
1449 verts[0].z = z;
1450 verts[1].x = x1;
1451 verts[1].y = y0;
1452 verts[1].z = z;
1453 verts[2].x = x1;
1454 verts[2].y = y1;
1455 verts[2].z = z;
1456 verts[3].x = x0;
1457 verts[3].y = y1;
1458 verts[3].z = z;
1459
1460 /* vertex colors */
1461 for (i = 0; i < 4; i++) {
1462 verts[i].r = ctx->Color.ClearColor.f[0];
1463 verts[i].g = ctx->Color.ClearColor.f[1];
1464 verts[i].b = ctx->Color.ClearColor.f[2];
1465 verts[i].a = ctx->Color.ClearColor.f[3];
1466 }
1467
1468 /* upload new vertex data */
1469 _mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts), verts,
1470 GL_DYNAMIC_DRAW_ARB);
1471 }
1472
1473 /* draw quad */
1474 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1475
1476 _mesa_meta_end(ctx);
1477 }
1478
1479 static void
1480 meta_glsl_clear_init(struct gl_context *ctx, struct clear_state *clear)
1481 {
1482 const char *vs_source =
1483 "attribute vec4 position;\n"
1484 "void main()\n"
1485 "{\n"
1486 " gl_Position = position;\n"
1487 "}\n";
1488 const char *gs_source =
1489 "#version 150\n"
1490 "layout(triangles) in;\n"
1491 "layout(triangle_strip, max_vertices = 4) out;\n"
1492 "uniform int layer;\n"
1493 "void main()\n"
1494 "{\n"
1495 " for (int i = 0; i < 3; i++) {\n"
1496 " gl_Layer = layer;\n"
1497 " gl_Position = gl_in[i].gl_Position;\n"
1498 " EmitVertex();\n"
1499 " }\n"
1500 "}\n";
1501 const char *fs_source =
1502 "uniform vec4 color;\n"
1503 "void main()\n"
1504 "{\n"
1505 " gl_FragColor = color;\n"
1506 "}\n";
1507 GLuint vs, gs = 0, fs;
1508 bool has_integer_textures;
1509
1510 _mesa_meta_setup_vertex_objects(&clear->VAO, &clear->VBO, true, 3, 0, 0);
1511
1512 if (clear->ShaderProg != 0)
1513 return;
1514
1515 vs = _mesa_CreateShader(GL_VERTEX_SHADER);
1516 _mesa_ShaderSource(vs, 1, &vs_source, NULL);
1517 _mesa_CompileShader(vs);
1518
1519 if (_mesa_has_geometry_shaders(ctx)) {
1520 gs = _mesa_CreateShader(GL_GEOMETRY_SHADER);
1521 _mesa_ShaderSource(gs, 1, &gs_source, NULL);
1522 _mesa_CompileShader(gs);
1523 }
1524
1525 fs = _mesa_CreateShader(GL_FRAGMENT_SHADER);
1526 _mesa_ShaderSource(fs, 1, &fs_source, NULL);
1527 _mesa_CompileShader(fs);
1528
1529 clear->ShaderProg = _mesa_CreateProgram();
1530 _mesa_AttachShader(clear->ShaderProg, fs);
1531 _mesa_DeleteShader(fs);
1532 if (gs != 0)
1533 _mesa_AttachShader(clear->ShaderProg, gs);
1534 _mesa_AttachShader(clear->ShaderProg, vs);
1535 _mesa_DeleteShader(vs);
1536 _mesa_BindAttribLocation(clear->ShaderProg, 0, "position");
1537 _mesa_LinkProgram(clear->ShaderProg);
1538
1539 clear->ColorLocation = _mesa_GetUniformLocation(clear->ShaderProg,
1540 "color");
1541 if (gs != 0) {
1542 clear->LayerLocation = _mesa_GetUniformLocation(clear->ShaderProg,
1543 "layer");
1544 }
1545
1546 has_integer_textures = _mesa_is_gles3(ctx) ||
1547 (_mesa_is_desktop_gl(ctx) && ctx->Const.GLSLVersion >= 130);
1548
1549 if (has_integer_textures) {
1550 void *shader_source_mem_ctx = ralloc_context(NULL);
1551 const char *vs_int_source =
1552 ralloc_asprintf(shader_source_mem_ctx,
1553 "#version 130\n"
1554 "in vec4 position;\n"
1555 "void main()\n"
1556 "{\n"
1557 " gl_Position = position;\n"
1558 "}\n");
1559 const char *fs_int_source =
1560 ralloc_asprintf(shader_source_mem_ctx,
1561 "#version 130\n"
1562 "uniform ivec4 color;\n"
1563 "out ivec4 out_color;\n"
1564 "\n"
1565 "void main()\n"
1566 "{\n"
1567 " out_color = color;\n"
1568 "}\n");
1569
1570 vs = _mesa_meta_compile_shader_with_debug(ctx, GL_VERTEX_SHADER,
1571 vs_int_source);
1572 fs = _mesa_meta_compile_shader_with_debug(ctx, GL_FRAGMENT_SHADER,
1573 fs_int_source);
1574 ralloc_free(shader_source_mem_ctx);
1575
1576 clear->IntegerShaderProg = _mesa_CreateProgram();
1577 _mesa_AttachShader(clear->IntegerShaderProg, fs);
1578 _mesa_DeleteShader(fs);
1579 if (gs != 0)
1580 _mesa_AttachShader(clear->IntegerShaderProg, gs);
1581 _mesa_AttachShader(clear->IntegerShaderProg, vs);
1582 _mesa_DeleteShader(vs);
1583 _mesa_BindAttribLocation(clear->IntegerShaderProg, 0, "position");
1584
1585 /* Note that user-defined out attributes get automatically assigned
1586 * locations starting from 0, so we don't need to explicitly
1587 * BindFragDataLocation to 0.
1588 */
1589
1590 _mesa_ObjectLabel(GL_PROGRAM, clear->IntegerShaderProg, -1,
1591 "integer clear");
1592 _mesa_meta_link_program_with_debug(ctx, clear->IntegerShaderProg);
1593
1594 clear->IntegerColorLocation =
1595 _mesa_GetUniformLocation(clear->IntegerShaderProg, "color");
1596 if (gs != 0) {
1597 clear->IntegerLayerLocation =
1598 _mesa_GetUniformLocation(clear->IntegerShaderProg, "layer");
1599 }
1600 }
1601 if (gs != 0)
1602 _mesa_DeleteShader(gs);
1603 }
1604
1605 static void
1606 meta_glsl_clear_cleanup(struct clear_state *clear)
1607 {
1608 if (clear->VAO == 0)
1609 return;
1610 _mesa_DeleteVertexArrays(1, &clear->VAO);
1611 clear->VAO = 0;
1612 _mesa_DeleteBuffers(1, &clear->VBO);
1613 clear->VBO = 0;
1614 _mesa_DeleteProgram(clear->ShaderProg);
1615 clear->ShaderProg = 0;
1616
1617 if (clear->IntegerShaderProg) {
1618 _mesa_DeleteProgram(clear->IntegerShaderProg);
1619 clear->IntegerShaderProg = 0;
1620 }
1621 }
1622
1623 /**
1624 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1625 */
1626 void
1627 _mesa_meta_glsl_Clear(struct gl_context *ctx, GLbitfield buffers)
1628 {
1629 struct clear_state *clear = &ctx->Meta->Clear;
1630 GLbitfield metaSave;
1631 const GLuint stencilMax = (1 << ctx->DrawBuffer->Visual.stencilBits) - 1;
1632 struct gl_framebuffer *fb = ctx->DrawBuffer;
1633 const float x0 = ((float)fb->_Xmin / fb->Width) * 2.0f - 1.0f;
1634 const float y0 = ((float)fb->_Ymin / fb->Height) * 2.0f - 1.0f;
1635 const float x1 = ((float)fb->_Xmax / fb->Width) * 2.0f - 1.0f;
1636 const float y1 = ((float)fb->_Ymax / fb->Height) * 2.0f - 1.0f;
1637 const float z = -invert_z(ctx->Depth.Clear);
1638 struct vertex verts[4];
1639
1640 metaSave = (MESA_META_ALPHA_TEST |
1641 MESA_META_BLEND |
1642 MESA_META_DEPTH_TEST |
1643 MESA_META_RASTERIZATION |
1644 MESA_META_SHADER |
1645 MESA_META_STENCIL_TEST |
1646 MESA_META_VERTEX |
1647 MESA_META_VIEWPORT |
1648 MESA_META_CLIP |
1649 MESA_META_CLAMP_FRAGMENT_COLOR |
1650 MESA_META_MULTISAMPLE |
1651 MESA_META_OCCLUSION_QUERY);
1652
1653 if (!(buffers & BUFFER_BITS_COLOR)) {
1654 /* We'll use colormask to disable color writes. Otherwise,
1655 * respect color mask
1656 */
1657 metaSave |= MESA_META_COLOR_MASK;
1658 }
1659
1660 _mesa_meta_begin(ctx, metaSave);
1661
1662 meta_glsl_clear_init(ctx, clear);
1663
1664 if (fb->_IntegerColor) {
1665 _mesa_UseProgram(clear->IntegerShaderProg);
1666 _mesa_Uniform4iv(clear->IntegerColorLocation, 1,
1667 ctx->Color.ClearColor.i);
1668 } else {
1669 _mesa_UseProgram(clear->ShaderProg);
1670 _mesa_Uniform4fv(clear->ColorLocation, 1,
1671 ctx->Color.ClearColor.f);
1672 }
1673
1674 /* GL_COLOR_BUFFER_BIT */
1675 if (buffers & BUFFER_BITS_COLOR) {
1676 /* leave colormask, glDrawBuffer state as-is */
1677
1678 /* Clears never have the color clamped. */
1679 if (ctx->Extensions.ARB_color_buffer_float)
1680 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
1681 }
1682 else {
1683 ASSERT(metaSave & MESA_META_COLOR_MASK);
1684 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1685 }
1686
1687 /* GL_DEPTH_BUFFER_BIT */
1688 if (buffers & BUFFER_BIT_DEPTH) {
1689 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE);
1690 _mesa_DepthFunc(GL_ALWAYS);
1691 _mesa_DepthMask(GL_TRUE);
1692 }
1693 else {
1694 assert(!ctx->Depth.Test);
1695 }
1696
1697 /* GL_STENCIL_BUFFER_BIT */
1698 if (buffers & BUFFER_BIT_STENCIL) {
1699 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
1700 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK,
1701 GL_REPLACE, GL_REPLACE, GL_REPLACE);
1702 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK, GL_ALWAYS,
1703 ctx->Stencil.Clear & stencilMax,
1704 ctx->Stencil.WriteMask[0]);
1705 }
1706 else {
1707 assert(!ctx->Stencil.Enabled);
1708 }
1709
1710 /* vertex positions */
1711 verts[0].x = x0;
1712 verts[0].y = y0;
1713 verts[0].z = z;
1714 verts[1].x = x1;
1715 verts[1].y = y0;
1716 verts[1].z = z;
1717 verts[2].x = x1;
1718 verts[2].y = y1;
1719 verts[2].z = z;
1720 verts[3].x = x0;
1721 verts[3].y = y1;
1722 verts[3].z = z;
1723
1724 /* upload new vertex data */
1725 _mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts), verts,
1726 GL_DYNAMIC_DRAW_ARB);
1727
1728 /* draw quad(s) */
1729 if (fb->MaxNumLayers > 0) {
1730 unsigned layer;
1731 for (layer = 0; layer < fb->MaxNumLayers; layer++) {
1732 if (fb->_IntegerColor)
1733 _mesa_Uniform1i(clear->IntegerLayerLocation, layer);
1734 else
1735 _mesa_Uniform1i(clear->LayerLocation, layer);
1736 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1737 }
1738 } else {
1739 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1740 }
1741
1742 _mesa_meta_end(ctx);
1743 }
1744
1745 /**
1746 * Meta implementation of ctx->Driver.CopyPixels() in terms
1747 * of texture mapping and polygon rendering and GLSL shaders.
1748 */
1749 void
1750 _mesa_meta_CopyPixels(struct gl_context *ctx, GLint srcX, GLint srcY,
1751 GLsizei width, GLsizei height,
1752 GLint dstX, GLint dstY, GLenum type)
1753 {
1754 struct copypix_state *copypix = &ctx->Meta->CopyPix;
1755 struct temp_texture *tex = _mesa_meta_get_temp_texture(ctx);
1756 struct vertex verts[4];
1757
1758 if (type != GL_COLOR ||
1759 ctx->_ImageTransferState ||
1760 ctx->Fog.Enabled ||
1761 width > tex->MaxSize ||
1762 height > tex->MaxSize) {
1763 /* XXX avoid this fallback */
1764 _swrast_CopyPixels(ctx, srcX, srcY, width, height, dstX, dstY, type);
1765 return;
1766 }
1767
1768 /* Most GL state applies to glCopyPixels, but a there's a few things
1769 * we need to override:
1770 */
1771 _mesa_meta_begin(ctx, (MESA_META_RASTERIZATION |
1772 MESA_META_SHADER |
1773 MESA_META_TEXTURE |
1774 MESA_META_TRANSFORM |
1775 MESA_META_CLIP |
1776 MESA_META_VERTEX |
1777 MESA_META_VIEWPORT));
1778
1779 _mesa_meta_setup_vertex_objects(&copypix->VAO, &copypix->VBO, false,
1780 3, 2, 0);
1781
1782 /* Silence valgrind warnings about reading uninitialized stack. */
1783 memset(verts, 0, sizeof(verts));
1784
1785 /* Alloc/setup texture */
1786 _mesa_meta_setup_copypix_texture(ctx, tex, srcX, srcY, width, height,
1787 GL_RGBA, GL_NEAREST);
1788
1789 /* vertex positions, texcoords (after texture allocation!) */
1790 {
1791 const GLfloat dstX0 = (GLfloat) dstX;
1792 const GLfloat dstY0 = (GLfloat) dstY;
1793 const GLfloat dstX1 = dstX + width * ctx->Pixel.ZoomX;
1794 const GLfloat dstY1 = dstY + height * ctx->Pixel.ZoomY;
1795 const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
1796
1797 verts[0].x = dstX0;
1798 verts[0].y = dstY0;
1799 verts[0].z = z;
1800 verts[0].tex[0] = 0.0F;
1801 verts[0].tex[1] = 0.0F;
1802 verts[1].x = dstX1;
1803 verts[1].y = dstY0;
1804 verts[1].z = z;
1805 verts[1].tex[0] = tex->Sright;
1806 verts[1].tex[1] = 0.0F;
1807 verts[2].x = dstX1;
1808 verts[2].y = dstY1;
1809 verts[2].z = z;
1810 verts[2].tex[0] = tex->Sright;
1811 verts[2].tex[1] = tex->Ttop;
1812 verts[3].x = dstX0;
1813 verts[3].y = dstY1;
1814 verts[3].z = z;
1815 verts[3].tex[0] = 0.0F;
1816 verts[3].tex[1] = tex->Ttop;
1817
1818 /* upload new vertex data */
1819 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1820 }
1821
1822 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
1823
1824 /* draw textured quad */
1825 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1826
1827 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
1828
1829 _mesa_meta_end(ctx);
1830 }
1831
1832 static void
1833 meta_drawpix_cleanup(struct drawpix_state *drawpix)
1834 {
1835 if (drawpix->VAO != 0) {
1836 _mesa_DeleteVertexArrays(1, &drawpix->VAO);
1837 drawpix->VAO = 0;
1838
1839 _mesa_DeleteBuffers(1, &drawpix->VBO);
1840 drawpix->VBO = 0;
1841 }
1842
1843 if (drawpix->StencilFP != 0) {
1844 _mesa_DeleteProgramsARB(1, &drawpix->StencilFP);
1845 drawpix->StencilFP = 0;
1846 }
1847
1848 if (drawpix->DepthFP != 0) {
1849 _mesa_DeleteProgramsARB(1, &drawpix->DepthFP);
1850 drawpix->DepthFP = 0;
1851 }
1852 }
1853
1854 /**
1855 * When the glDrawPixels() image size is greater than the max rectangle
1856 * texture size we use this function to break the glDrawPixels() image
1857 * into tiles which fit into the max texture size.
1858 */
1859 static void
1860 tiled_draw_pixels(struct gl_context *ctx,
1861 GLint tileSize,
1862 GLint x, GLint y, GLsizei width, GLsizei height,
1863 GLenum format, GLenum type,
1864 const struct gl_pixelstore_attrib *unpack,
1865 const GLvoid *pixels)
1866 {
1867 struct gl_pixelstore_attrib tileUnpack = *unpack;
1868 GLint i, j;
1869
1870 if (tileUnpack.RowLength == 0)
1871 tileUnpack.RowLength = width;
1872
1873 for (i = 0; i < width; i += tileSize) {
1874 const GLint tileWidth = MIN2(tileSize, width - i);
1875 const GLint tileX = (GLint) (x + i * ctx->Pixel.ZoomX);
1876
1877 tileUnpack.SkipPixels = unpack->SkipPixels + i;
1878
1879 for (j = 0; j < height; j += tileSize) {
1880 const GLint tileHeight = MIN2(tileSize, height - j);
1881 const GLint tileY = (GLint) (y + j * ctx->Pixel.ZoomY);
1882
1883 tileUnpack.SkipRows = unpack->SkipRows + j;
1884
1885 _mesa_meta_DrawPixels(ctx, tileX, tileY, tileWidth, tileHeight,
1886 format, type, &tileUnpack, pixels);
1887 }
1888 }
1889 }
1890
1891
1892 /**
1893 * One-time init for drawing stencil pixels.
1894 */
1895 static void
1896 init_draw_stencil_pixels(struct gl_context *ctx)
1897 {
1898 /* This program is run eight times, once for each stencil bit.
1899 * The stencil values to draw are found in an 8-bit alpha texture.
1900 * We read the texture/stencil value and test if bit 'b' is set.
1901 * If the bit is not set, use KIL to kill the fragment.
1902 * Finally, we use the stencil test to update the stencil buffer.
1903 *
1904 * The basic algorithm for checking if a bit is set is:
1905 * if (is_odd(value / (1 << bit)))
1906 * result is one (or non-zero).
1907 * else
1908 * result is zero.
1909 * The program parameter contains three values:
1910 * parm.x = 255 / (1 << bit)
1911 * parm.y = 0.5
1912 * parm.z = 0.0
1913 */
1914 static const char *program =
1915 "!!ARBfp1.0\n"
1916 "PARAM parm = program.local[0]; \n"
1917 "TEMP t; \n"
1918 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1919 "# t = t * 255 / bit \n"
1920 "MUL t.x, t.a, parm.x; \n"
1921 "# t = (int) t \n"
1922 "FRC t.y, t.x; \n"
1923 "SUB t.x, t.x, t.y; \n"
1924 "# t = t * 0.5 \n"
1925 "MUL t.x, t.x, parm.y; \n"
1926 "# t = fract(t.x) \n"
1927 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
1928 "# t.x = (t.x == 0 ? 1 : 0) \n"
1929 "SGE t.x, -t.x, parm.z; \n"
1930 "KIL -t.x; \n"
1931 "# for debug only \n"
1932 "#MOV result.color, t.x; \n"
1933 "END \n";
1934 char program2[1000];
1935 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
1936 struct temp_texture *tex = _mesa_meta_get_temp_texture(ctx);
1937 const char *texTarget;
1938
1939 assert(drawpix->StencilFP == 0);
1940
1941 /* replace %s with "RECT" or "2D" */
1942 assert(strlen(program) + 4 < sizeof(program2));
1943 if (tex->Target == GL_TEXTURE_RECTANGLE)
1944 texTarget = "RECT";
1945 else
1946 texTarget = "2D";
1947 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
1948
1949 _mesa_GenProgramsARB(1, &drawpix->StencilFP);
1950 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
1951 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
1952 strlen(program2), (const GLubyte *) program2);
1953 }
1954
1955
1956 /**
1957 * One-time init for drawing depth pixels.
1958 */
1959 static void
1960 init_draw_depth_pixels(struct gl_context *ctx)
1961 {
1962 static const char *program =
1963 "!!ARBfp1.0\n"
1964 "PARAM color = program.local[0]; \n"
1965 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1966 "MOV result.color, color; \n"
1967 "END \n";
1968 char program2[200];
1969 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
1970 struct temp_texture *tex = _mesa_meta_get_temp_texture(ctx);
1971 const char *texTarget;
1972
1973 assert(drawpix->DepthFP == 0);
1974
1975 /* replace %s with "RECT" or "2D" */
1976 assert(strlen(program) + 4 < sizeof(program2));
1977 if (tex->Target == GL_TEXTURE_RECTANGLE)
1978 texTarget = "RECT";
1979 else
1980 texTarget = "2D";
1981 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
1982
1983 _mesa_GenProgramsARB(1, &drawpix->DepthFP);
1984 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
1985 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
1986 strlen(program2), (const GLubyte *) program2);
1987 }
1988
1989
1990 /**
1991 * Meta implementation of ctx->Driver.DrawPixels() in terms
1992 * of texture mapping and polygon rendering.
1993 */
1994 void
1995 _mesa_meta_DrawPixels(struct gl_context *ctx,
1996 GLint x, GLint y, GLsizei width, GLsizei height,
1997 GLenum format, GLenum type,
1998 const struct gl_pixelstore_attrib *unpack,
1999 const GLvoid *pixels)
2000 {
2001 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
2002 struct temp_texture *tex = _mesa_meta_get_temp_texture(ctx);
2003 const struct gl_pixelstore_attrib unpackSave = ctx->Unpack;
2004 const GLuint origStencilMask = ctx->Stencil.WriteMask[0];
2005 struct vertex verts[4];
2006 GLenum texIntFormat;
2007 GLboolean fallback, newTex;
2008 GLbitfield metaExtraSave = 0x0;
2009
2010 /*
2011 * Determine if we can do the glDrawPixels with texture mapping.
2012 */
2013 fallback = GL_FALSE;
2014 if (ctx->Fog.Enabled) {
2015 fallback = GL_TRUE;
2016 }
2017
2018 if (_mesa_is_color_format(format)) {
2019 /* use more compact format when possible */
2020 /* XXX disable special case for GL_LUMINANCE for now to work around
2021 * apparent i965 driver bug (see bug #23670).
2022 */
2023 if (/*format == GL_LUMINANCE ||*/ format == GL_LUMINANCE_ALPHA)
2024 texIntFormat = format;
2025 else
2026 texIntFormat = GL_RGBA;
2027
2028 /* If we're not supposed to clamp the resulting color, then just
2029 * promote our texture to fully float. We could do better by
2030 * just going for the matching set of channels, in floating
2031 * point.
2032 */
2033 if (ctx->Color.ClampFragmentColor != GL_TRUE &&
2034 ctx->Extensions.ARB_texture_float)
2035 texIntFormat = GL_RGBA32F;
2036 }
2037 else if (_mesa_is_stencil_format(format)) {
2038 if (ctx->Extensions.ARB_fragment_program &&
2039 ctx->Pixel.IndexShift == 0 &&
2040 ctx->Pixel.IndexOffset == 0 &&
2041 type == GL_UNSIGNED_BYTE) {
2042 /* We'll store stencil as alpha. This only works for GLubyte
2043 * image data because of how incoming values are mapped to alpha
2044 * in [0,1].
2045 */
2046 texIntFormat = GL_ALPHA;
2047 metaExtraSave = (MESA_META_COLOR_MASK |
2048 MESA_META_DEPTH_TEST |
2049 MESA_META_PIXEL_TRANSFER |
2050 MESA_META_SHADER |
2051 MESA_META_STENCIL_TEST);
2052 }
2053 else {
2054 fallback = GL_TRUE;
2055 }
2056 }
2057 else if (_mesa_is_depth_format(format)) {
2058 if (ctx->Extensions.ARB_depth_texture &&
2059 ctx->Extensions.ARB_fragment_program) {
2060 texIntFormat = GL_DEPTH_COMPONENT;
2061 metaExtraSave = (MESA_META_SHADER);
2062 }
2063 else {
2064 fallback = GL_TRUE;
2065 }
2066 }
2067 else {
2068 fallback = GL_TRUE;
2069 }
2070
2071 if (fallback) {
2072 _swrast_DrawPixels(ctx, x, y, width, height,
2073 format, type, unpack, pixels);
2074 return;
2075 }
2076
2077 /*
2078 * Check image size against max texture size, draw as tiles if needed.
2079 */
2080 if (width > tex->MaxSize || height > tex->MaxSize) {
2081 tiled_draw_pixels(ctx, tex->MaxSize, x, y, width, height,
2082 format, type, unpack, pixels);
2083 return;
2084 }
2085
2086 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2087 * but a there's a few things we need to override:
2088 */
2089 _mesa_meta_begin(ctx, (MESA_META_RASTERIZATION |
2090 MESA_META_SHADER |
2091 MESA_META_TEXTURE |
2092 MESA_META_TRANSFORM |
2093 MESA_META_CLIP |
2094 MESA_META_VERTEX |
2095 MESA_META_VIEWPORT |
2096 metaExtraSave));
2097
2098 newTex = _mesa_meta_alloc_texture(tex, width, height, texIntFormat);
2099
2100 _mesa_meta_setup_vertex_objects(&drawpix->VAO, &drawpix->VBO, false,
2101 3, 2, 0);
2102
2103 /* Silence valgrind warnings about reading uninitialized stack. */
2104 memset(verts, 0, sizeof(verts));
2105
2106 /* vertex positions, texcoords (after texture allocation!) */
2107 {
2108 const GLfloat x0 = (GLfloat) x;
2109 const GLfloat y0 = (GLfloat) y;
2110 const GLfloat x1 = x + width * ctx->Pixel.ZoomX;
2111 const GLfloat y1 = y + height * ctx->Pixel.ZoomY;
2112 const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
2113
2114 verts[0].x = x0;
2115 verts[0].y = y0;
2116 verts[0].z = z;
2117 verts[0].tex[0] = 0.0F;
2118 verts[0].tex[1] = 0.0F;
2119 verts[1].x = x1;
2120 verts[1].y = y0;
2121 verts[1].z = z;
2122 verts[1].tex[0] = tex->Sright;
2123 verts[1].tex[1] = 0.0F;
2124 verts[2].x = x1;
2125 verts[2].y = y1;
2126 verts[2].z = z;
2127 verts[2].tex[0] = tex->Sright;
2128 verts[2].tex[1] = tex->Ttop;
2129 verts[3].x = x0;
2130 verts[3].y = y1;
2131 verts[3].z = z;
2132 verts[3].tex[0] = 0.0F;
2133 verts[3].tex[1] = tex->Ttop;
2134 }
2135
2136 /* upload new vertex data */
2137 _mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts),
2138 verts, GL_DYNAMIC_DRAW_ARB);
2139
2140 /* set given unpack params */
2141 ctx->Unpack = *unpack;
2142
2143 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
2144
2145 if (_mesa_is_stencil_format(format)) {
2146 /* Drawing stencil */
2147 GLint bit;
2148
2149 if (!drawpix->StencilFP)
2150 init_draw_stencil_pixels(ctx);
2151
2152 _mesa_meta_setup_drawpix_texture(ctx, tex, newTex, width, height,
2153 GL_ALPHA, type, pixels);
2154
2155 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
2156
2157 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
2158
2159 /* set all stencil bits to 0 */
2160 _mesa_StencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
2161 _mesa_StencilFunc(GL_ALWAYS, 0, 255);
2162 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2163
2164 /* set stencil bits to 1 where needed */
2165 _mesa_StencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
2166
2167 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
2168 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
2169
2170 for (bit = 0; bit < ctx->DrawBuffer->Visual.stencilBits; bit++) {
2171 const GLuint mask = 1 << bit;
2172 if (mask & origStencilMask) {
2173 _mesa_StencilFunc(GL_ALWAYS, mask, mask);
2174 _mesa_StencilMask(mask);
2175
2176 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0,
2177 255.0f / mask, 0.5f, 0.0f, 0.0f);
2178
2179 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2180 }
2181 }
2182 }
2183 else if (_mesa_is_depth_format(format)) {
2184 /* Drawing depth */
2185 if (!drawpix->DepthFP)
2186 init_draw_depth_pixels(ctx);
2187
2188 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
2189 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
2190
2191 /* polygon color = current raster color */
2192 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0,
2193 ctx->Current.RasterColor);
2194
2195 _mesa_meta_setup_drawpix_texture(ctx, tex, newTex, width, height,
2196 format, type, pixels);
2197
2198 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2199 }
2200 else {
2201 /* Drawing RGBA */
2202 _mesa_meta_setup_drawpix_texture(ctx, tex, newTex, width, height,
2203 format, type, pixels);
2204 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2205 }
2206
2207 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
2208
2209 /* restore unpack params */
2210 ctx->Unpack = unpackSave;
2211
2212 _mesa_meta_end(ctx);
2213 }
2214
2215 static GLboolean
2216 alpha_test_raster_color(struct gl_context *ctx)
2217 {
2218 GLfloat alpha = ctx->Current.RasterColor[ACOMP];
2219 GLfloat ref = ctx->Color.AlphaRef;
2220
2221 switch (ctx->Color.AlphaFunc) {
2222 case GL_NEVER:
2223 return GL_FALSE;
2224 case GL_LESS:
2225 return alpha < ref;
2226 case GL_EQUAL:
2227 return alpha == ref;
2228 case GL_LEQUAL:
2229 return alpha <= ref;
2230 case GL_GREATER:
2231 return alpha > ref;
2232 case GL_NOTEQUAL:
2233 return alpha != ref;
2234 case GL_GEQUAL:
2235 return alpha >= ref;
2236 case GL_ALWAYS:
2237 return GL_TRUE;
2238 default:
2239 assert(0);
2240 return GL_FALSE;
2241 }
2242 }
2243
2244 /**
2245 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2246 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2247 * tracker would improve performance a lot.
2248 */
2249 void
2250 _mesa_meta_Bitmap(struct gl_context *ctx,
2251 GLint x, GLint y, GLsizei width, GLsizei height,
2252 const struct gl_pixelstore_attrib *unpack,
2253 const GLubyte *bitmap1)
2254 {
2255 struct bitmap_state *bitmap = &ctx->Meta->Bitmap;
2256 struct temp_texture *tex = get_bitmap_temp_texture(ctx);
2257 const GLenum texIntFormat = GL_ALPHA;
2258 const struct gl_pixelstore_attrib unpackSave = *unpack;
2259 GLubyte fg, bg;
2260 struct vertex verts[4];
2261 GLboolean newTex;
2262 GLubyte *bitmap8;
2263
2264 /*
2265 * Check if swrast fallback is needed.
2266 */
2267 if (ctx->_ImageTransferState ||
2268 ctx->FragmentProgram._Enabled ||
2269 ctx->Fog.Enabled ||
2270 ctx->Texture._EnabledUnits ||
2271 width > tex->MaxSize ||
2272 height > tex->MaxSize) {
2273 _swrast_Bitmap(ctx, x, y, width, height, unpack, bitmap1);
2274 return;
2275 }
2276
2277 if (ctx->Color.AlphaEnabled && !alpha_test_raster_color(ctx))
2278 return;
2279
2280 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2281 * but a there's a few things we need to override:
2282 */
2283 _mesa_meta_begin(ctx, (MESA_META_ALPHA_TEST |
2284 MESA_META_PIXEL_STORE |
2285 MESA_META_RASTERIZATION |
2286 MESA_META_SHADER |
2287 MESA_META_TEXTURE |
2288 MESA_META_TRANSFORM |
2289 MESA_META_CLIP |
2290 MESA_META_VERTEX |
2291 MESA_META_VIEWPORT));
2292
2293 _mesa_meta_setup_vertex_objects(&bitmap->VAO, &bitmap->VBO, false, 3, 2, 4);
2294
2295 newTex = _mesa_meta_alloc_texture(tex, width, height, texIntFormat);
2296
2297 /* Silence valgrind warnings about reading uninitialized stack. */
2298 memset(verts, 0, sizeof(verts));
2299
2300 /* vertex positions, texcoords, colors (after texture allocation!) */
2301 {
2302 const GLfloat x0 = (GLfloat) x;
2303 const GLfloat y0 = (GLfloat) y;
2304 const GLfloat x1 = (GLfloat) (x + width);
2305 const GLfloat y1 = (GLfloat) (y + height);
2306 const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
2307 GLuint i;
2308
2309 verts[0].x = x0;
2310 verts[0].y = y0;
2311 verts[0].z = z;
2312 verts[0].tex[0] = 0.0F;
2313 verts[0].tex[1] = 0.0F;
2314 verts[1].x = x1;
2315 verts[1].y = y0;
2316 verts[1].z = z;
2317 verts[1].tex[0] = tex->Sright;
2318 verts[1].tex[1] = 0.0F;
2319 verts[2].x = x1;
2320 verts[2].y = y1;
2321 verts[2].z = z;
2322 verts[2].tex[0] = tex->Sright;
2323 verts[2].tex[1] = tex->Ttop;
2324 verts[3].x = x0;
2325 verts[3].y = y1;
2326 verts[3].z = z;
2327 verts[3].tex[0] = 0.0F;
2328 verts[3].tex[1] = tex->Ttop;
2329
2330 for (i = 0; i < 4; i++) {
2331 verts[i].r = ctx->Current.RasterColor[0];
2332 verts[i].g = ctx->Current.RasterColor[1];
2333 verts[i].b = ctx->Current.RasterColor[2];
2334 verts[i].a = ctx->Current.RasterColor[3];
2335 }
2336
2337 /* upload new vertex data */
2338 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
2339 }
2340
2341 /* choose different foreground/background alpha values */
2342 CLAMPED_FLOAT_TO_UBYTE(fg, ctx->Current.RasterColor[ACOMP]);
2343 bg = (fg > 127 ? 0 : 255);
2344
2345 bitmap1 = _mesa_map_pbo_source(ctx, &unpackSave, bitmap1);
2346 if (!bitmap1) {
2347 _mesa_meta_end(ctx);
2348 return;
2349 }
2350
2351 bitmap8 = malloc(width * height);
2352 if (bitmap8) {
2353 memset(bitmap8, bg, width * height);
2354 _mesa_expand_bitmap(width, height, &unpackSave, bitmap1,
2355 bitmap8, width, fg);
2356
2357 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
2358
2359 _mesa_set_enable(ctx, GL_ALPHA_TEST, GL_TRUE);
2360 _mesa_AlphaFunc(GL_NOTEQUAL, UBYTE_TO_FLOAT(bg));
2361
2362 _mesa_meta_setup_drawpix_texture(ctx, tex, newTex, width, height,
2363 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap8);
2364
2365 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2366
2367 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
2368
2369 free(bitmap8);
2370 }
2371
2372 _mesa_unmap_pbo_source(ctx, &unpackSave);
2373
2374 _mesa_meta_end(ctx);
2375 }
2376
2377 /**
2378 * Compute the texture coordinates for the four vertices of a quad for
2379 * drawing a 2D texture image or slice of a cube/3D texture.
2380 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2381 * \param slice slice of a 1D/2D array texture or 3D texture
2382 * \param width width of the texture image
2383 * \param height height of the texture image
2384 * \param coords0/1/2/3 returns the computed texcoords
2385 */
2386 void
2387 _mesa_meta_setup_texture_coords(GLenum faceTarget,
2388 GLint slice,
2389 GLint width,
2390 GLint height,
2391 GLint depth,
2392 GLfloat coords0[4],
2393 GLfloat coords1[4],
2394 GLfloat coords2[4],
2395 GLfloat coords3[4])
2396 {
2397 static const GLfloat st[4][2] = {
2398 {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}
2399 };
2400 GLuint i;
2401 GLfloat r;
2402
2403 /* Currently all texture targets want the W component to be 1.0.
2404 */
2405 coords0[3] = 1.0F;
2406 coords1[3] = 1.0F;
2407 coords2[3] = 1.0F;
2408 coords3[3] = 1.0F;
2409
2410 switch (faceTarget) {
2411 case GL_TEXTURE_1D:
2412 case GL_TEXTURE_2D:
2413 case GL_TEXTURE_3D:
2414 case GL_TEXTURE_2D_ARRAY:
2415 if (faceTarget == GL_TEXTURE_3D) {
2416 assert(slice < depth);
2417 assert(depth >= 1);
2418 r = (slice + 0.5f) / depth;
2419 }
2420 else if (faceTarget == GL_TEXTURE_2D_ARRAY)
2421 r = (float) slice;
2422 else
2423 r = 0.0F;
2424 coords0[0] = 0.0F; /* s */
2425 coords0[1] = 0.0F; /* t */
2426 coords0[2] = r; /* r */
2427 coords1[0] = 1.0F;
2428 coords1[1] = 0.0F;
2429 coords1[2] = r;
2430 coords2[0] = 1.0F;
2431 coords2[1] = 1.0F;
2432 coords2[2] = r;
2433 coords3[0] = 0.0F;
2434 coords3[1] = 1.0F;
2435 coords3[2] = r;
2436 break;
2437 case GL_TEXTURE_RECTANGLE_ARB:
2438 coords0[0] = 0.0F; /* s */
2439 coords0[1] = 0.0F; /* t */
2440 coords0[2] = 0.0F; /* r */
2441 coords1[0] = (float) width;
2442 coords1[1] = 0.0F;
2443 coords1[2] = 0.0F;
2444 coords2[0] = (float) width;
2445 coords2[1] = (float) height;
2446 coords2[2] = 0.0F;
2447 coords3[0] = 0.0F;
2448 coords3[1] = (float) height;
2449 coords3[2] = 0.0F;
2450 break;
2451 case GL_TEXTURE_1D_ARRAY:
2452 coords0[0] = 0.0F; /* s */
2453 coords0[1] = (float) slice; /* t */
2454 coords0[2] = 0.0F; /* r */
2455 coords1[0] = 1.0f;
2456 coords1[1] = (float) slice;
2457 coords1[2] = 0.0F;
2458 coords2[0] = 1.0F;
2459 coords2[1] = (float) slice;
2460 coords2[2] = 0.0F;
2461 coords3[0] = 0.0F;
2462 coords3[1] = (float) slice;
2463 coords3[2] = 0.0F;
2464 break;
2465
2466 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2467 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2468 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2469 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2470 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2471 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2472 /* loop over quad verts */
2473 for (i = 0; i < 4; i++) {
2474 /* Compute sc = +/-scale and tc = +/-scale.
2475 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2476 * though that can still sometimes happen with this scale factor...
2477 */
2478 const GLfloat scale = 0.9999f;
2479 const GLfloat sc = (2.0f * st[i][0] - 1.0f) * scale;
2480 const GLfloat tc = (2.0f * st[i][1] - 1.0f) * scale;
2481 GLfloat *coord;
2482
2483 switch (i) {
2484 case 0:
2485 coord = coords0;
2486 break;
2487 case 1:
2488 coord = coords1;
2489 break;
2490 case 2:
2491 coord = coords2;
2492 break;
2493 case 3:
2494 coord = coords3;
2495 break;
2496 default:
2497 assert(0);
2498 }
2499
2500 coord[3] = (float) (slice / 6);
2501
2502 switch (faceTarget) {
2503 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2504 coord[0] = 1.0f;
2505 coord[1] = -tc;
2506 coord[2] = -sc;
2507 break;
2508 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2509 coord[0] = -1.0f;
2510 coord[1] = -tc;
2511 coord[2] = sc;
2512 break;
2513 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2514 coord[0] = sc;
2515 coord[1] = 1.0f;
2516 coord[2] = tc;
2517 break;
2518 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2519 coord[0] = sc;
2520 coord[1] = -1.0f;
2521 coord[2] = -tc;
2522 break;
2523 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2524 coord[0] = sc;
2525 coord[1] = -tc;
2526 coord[2] = 1.0f;
2527 break;
2528 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2529 coord[0] = -sc;
2530 coord[1] = -tc;
2531 coord[2] = -1.0f;
2532 break;
2533 default:
2534 assert(0);
2535 }
2536 }
2537 break;
2538 default:
2539 assert(!"unexpected target in _mesa_meta_setup_texture_coords()");
2540 }
2541 }
2542
2543 static struct blit_shader *
2544 choose_blit_shader(GLenum target, struct blit_shader_table *table)
2545 {
2546 switch(target) {
2547 case GL_TEXTURE_1D:
2548 table->sampler_1d.type = "sampler1D";
2549 table->sampler_1d.func = "texture1D";
2550 table->sampler_1d.texcoords = "texCoords.x";
2551 return &table->sampler_1d;
2552 case GL_TEXTURE_2D:
2553 table->sampler_2d.type = "sampler2D";
2554 table->sampler_2d.func = "texture2D";
2555 table->sampler_2d.texcoords = "texCoords.xy";
2556 return &table->sampler_2d;
2557 case GL_TEXTURE_RECTANGLE:
2558 table->sampler_rect.type = "sampler2DRect";
2559 table->sampler_rect.func = "texture2DRect";
2560 table->sampler_rect.texcoords = "texCoords.xy";
2561 return &table->sampler_rect;
2562 case GL_TEXTURE_3D:
2563 /* Code for mipmap generation with 3D textures is not used yet.
2564 * It's a sw fallback.
2565 */
2566 table->sampler_3d.type = "sampler3D";
2567 table->sampler_3d.func = "texture3D";
2568 table->sampler_3d.texcoords = "texCoords.xyz";
2569 return &table->sampler_3d;
2570 case GL_TEXTURE_CUBE_MAP:
2571 table->sampler_cubemap.type = "samplerCube";
2572 table->sampler_cubemap.func = "textureCube";
2573 table->sampler_cubemap.texcoords = "texCoords.xyz";
2574 return &table->sampler_cubemap;
2575 case GL_TEXTURE_1D_ARRAY:
2576 table->sampler_1d_array.type = "sampler1DArray";
2577 table->sampler_1d_array.func = "texture1DArray";
2578 table->sampler_1d_array.texcoords = "texCoords.xy";
2579 return &table->sampler_1d_array;
2580 case GL_TEXTURE_2D_ARRAY:
2581 table->sampler_2d_array.type = "sampler2DArray";
2582 table->sampler_2d_array.func = "texture2DArray";
2583 table->sampler_2d_array.texcoords = "texCoords.xyz";
2584 return &table->sampler_2d_array;
2585 case GL_TEXTURE_CUBE_MAP_ARRAY:
2586 table->sampler_cubemap_array.type = "samplerCubeArray";
2587 table->sampler_cubemap_array.func = "textureCubeArray";
2588 table->sampler_cubemap_array.texcoords = "texCoords.xyzw";
2589 return &table->sampler_cubemap_array;
2590 default:
2591 _mesa_problem(NULL, "Unexpected texture target 0x%x in"
2592 " setup_texture_sampler()\n", target);
2593 return NULL;
2594 }
2595 }
2596
2597 void
2598 _mesa_meta_blit_shader_table_cleanup(struct blit_shader_table *table)
2599 {
2600 _mesa_DeleteProgram(table->sampler_1d.shader_prog);
2601 _mesa_DeleteProgram(table->sampler_2d.shader_prog);
2602 _mesa_DeleteProgram(table->sampler_3d.shader_prog);
2603 _mesa_DeleteProgram(table->sampler_rect.shader_prog);
2604 _mesa_DeleteProgram(table->sampler_cubemap.shader_prog);
2605 _mesa_DeleteProgram(table->sampler_1d_array.shader_prog);
2606 _mesa_DeleteProgram(table->sampler_2d_array.shader_prog);
2607 _mesa_DeleteProgram(table->sampler_cubemap_array.shader_prog);
2608
2609 table->sampler_1d.shader_prog = 0;
2610 table->sampler_2d.shader_prog = 0;
2611 table->sampler_3d.shader_prog = 0;
2612 table->sampler_rect.shader_prog = 0;
2613 table->sampler_cubemap.shader_prog = 0;
2614 table->sampler_1d_array.shader_prog = 0;
2615 table->sampler_2d_array.shader_prog = 0;
2616 table->sampler_cubemap_array.shader_prog = 0;
2617 }
2618
2619 /**
2620 * Determine the GL data type to use for the temporary image read with
2621 * ReadPixels() and passed to Tex[Sub]Image().
2622 */
2623 static GLenum
2624 get_temp_image_type(struct gl_context *ctx, mesa_format format)
2625 {
2626 GLenum baseFormat;
2627
2628 baseFormat = _mesa_get_format_base_format(format);
2629
2630 switch (baseFormat) {
2631 case GL_RGBA:
2632 case GL_RGB:
2633 case GL_RG:
2634 case GL_RED:
2635 case GL_ALPHA:
2636 case GL_LUMINANCE:
2637 case GL_LUMINANCE_ALPHA:
2638 case GL_INTENSITY:
2639 if (ctx->DrawBuffer->Visual.redBits <= 8) {
2640 return GL_UNSIGNED_BYTE;
2641 } else if (ctx->DrawBuffer->Visual.redBits <= 16) {
2642 return GL_UNSIGNED_SHORT;
2643 } else {
2644 GLenum datatype = _mesa_get_format_datatype(format);
2645 if (datatype == GL_INT || datatype == GL_UNSIGNED_INT)
2646 return datatype;
2647 return GL_FLOAT;
2648 }
2649 case GL_DEPTH_COMPONENT: {
2650 GLenum datatype = _mesa_get_format_datatype(format);
2651 if (datatype == GL_FLOAT)
2652 return GL_FLOAT;
2653 else
2654 return GL_UNSIGNED_INT;
2655 }
2656 case GL_DEPTH_STENCIL: {
2657 GLenum datatype = _mesa_get_format_datatype(format);
2658 if (datatype == GL_FLOAT)
2659 return GL_FLOAT_32_UNSIGNED_INT_24_8_REV;
2660 else
2661 return GL_UNSIGNED_INT_24_8;
2662 }
2663 default:
2664 _mesa_problem(ctx, "Unexpected format %d in get_temp_image_type()",
2665 baseFormat);
2666 return 0;
2667 }
2668 }
2669
2670 /**
2671 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2672 * Have to be careful with locking and meta state for pixel transfer.
2673 */
2674 void
2675 _mesa_meta_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
2676 struct gl_texture_image *texImage,
2677 GLint xoffset, GLint yoffset, GLint zoffset,
2678 struct gl_renderbuffer *rb,
2679 GLint x, GLint y,
2680 GLsizei width, GLsizei height)
2681 {
2682 struct gl_texture_object *texObj = texImage->TexObject;
2683 GLenum format, type;
2684 GLint bpp;
2685 void *buf;
2686
2687 /* The gl_renderbuffer is part of the interface for
2688 * dd_function_table::CopyTexSubImage, but this implementation does not use
2689 * it.
2690 */
2691 (void) rb;
2692
2693 /* Choose format/type for temporary image buffer */
2694 format = _mesa_get_format_base_format(texImage->TexFormat);
2695 if (format == GL_LUMINANCE ||
2696 format == GL_LUMINANCE_ALPHA ||
2697 format == GL_INTENSITY) {
2698 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
2699 * temp image buffer because glReadPixels will do L=R+G+B which is
2700 * not what we want (should be L=R).
2701 */
2702 format = GL_RGBA;
2703 }
2704
2705 type = get_temp_image_type(ctx, texImage->TexFormat);
2706 if (_mesa_is_format_integer_color(texImage->TexFormat)) {
2707 format = _mesa_base_format_to_integer_format(format);
2708 }
2709 bpp = _mesa_bytes_per_pixel(format, type);
2710 if (bpp <= 0) {
2711 _mesa_problem(ctx, "Bad bpp in _mesa_meta_CopyTexSubImage()");
2712 return;
2713 }
2714
2715 /*
2716 * Alloc image buffer (XXX could use a PBO)
2717 */
2718 buf = malloc(width * height * bpp);
2719 if (!buf) {
2720 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage%uD", dims);
2721 return;
2722 }
2723
2724 _mesa_unlock_texture(ctx, texObj); /* need to unlock first */
2725
2726 /*
2727 * Read image from framebuffer (disable pixel transfer ops)
2728 */
2729 _mesa_meta_begin(ctx, MESA_META_PIXEL_STORE | MESA_META_PIXEL_TRANSFER);
2730 ctx->Driver.ReadPixels(ctx, x, y, width, height,
2731 format, type, &ctx->Pack, buf);
2732 _mesa_meta_end(ctx);
2733
2734 _mesa_update_state(ctx); /* to update pixel transfer state */
2735
2736 /*
2737 * Store texture data (with pixel transfer ops)
2738 */
2739 _mesa_meta_begin(ctx, MESA_META_PIXEL_STORE);
2740
2741 if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
2742 assert(yoffset == 0);
2743 ctx->Driver.TexSubImage(ctx, dims, texImage,
2744 xoffset, zoffset, 0, width, 1, 1,
2745 format, type, buf, &ctx->Unpack);
2746 } else {
2747 ctx->Driver.TexSubImage(ctx, dims, texImage,
2748 xoffset, yoffset, zoffset, width, height, 1,
2749 format, type, buf, &ctx->Unpack);
2750 }
2751
2752 _mesa_meta_end(ctx);
2753
2754 _mesa_lock_texture(ctx, texObj); /* re-lock */
2755
2756 free(buf);
2757 }
2758
2759
2760 static void
2761 meta_decompress_cleanup(struct decompress_state *decompress)
2762 {
2763 if (decompress->FBO != 0) {
2764 _mesa_DeleteFramebuffers(1, &decompress->FBO);
2765 _mesa_DeleteRenderbuffers(1, &decompress->RBO);
2766 }
2767
2768 if (decompress->VAO != 0) {
2769 _mesa_DeleteVertexArrays(1, &decompress->VAO);
2770 _mesa_DeleteBuffers(1, &decompress->VBO);
2771 }
2772
2773 if (decompress->Sampler != 0)
2774 _mesa_DeleteSamplers(1, &decompress->Sampler);
2775
2776 memset(decompress, 0, sizeof(*decompress));
2777 }
2778
2779 /**
2780 * Decompress a texture image by drawing a quad with the compressed
2781 * texture and reading the pixels out of the color buffer.
2782 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
2783 * \param destFormat format, ala glReadPixels
2784 * \param destType type, ala glReadPixels
2785 * \param dest destination buffer
2786 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
2787 */
2788 static void
2789 decompress_texture_image(struct gl_context *ctx,
2790 struct gl_texture_image *texImage,
2791 GLuint slice,
2792 GLenum destFormat, GLenum destType,
2793 GLvoid *dest)
2794 {
2795 struct decompress_state *decompress = &ctx->Meta->Decompress;
2796 struct gl_texture_object *texObj = texImage->TexObject;
2797 const GLint width = texImage->Width;
2798 const GLint height = texImage->Height;
2799 const GLint depth = texImage->Height;
2800 const GLenum target = texObj->Target;
2801 GLenum faceTarget;
2802 struct vertex verts[4];
2803 GLuint samplerSave;
2804 const bool use_glsl_version = ctx->Extensions.ARB_vertex_shader &&
2805 ctx->Extensions.ARB_fragment_shader;
2806
2807 if (slice > 0) {
2808 assert(target == GL_TEXTURE_3D ||
2809 target == GL_TEXTURE_2D_ARRAY ||
2810 target == GL_TEXTURE_CUBE_MAP_ARRAY);
2811 }
2812
2813 switch (target) {
2814 case GL_TEXTURE_1D:
2815 case GL_TEXTURE_1D_ARRAY:
2816 assert(!"No compressed 1D textures.");
2817 return;
2818
2819 case GL_TEXTURE_3D:
2820 assert(!"No compressed 3D textures.");
2821 return;
2822
2823 case GL_TEXTURE_CUBE_MAP_ARRAY:
2824 faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + (slice % 6);
2825 break;
2826
2827 case GL_TEXTURE_CUBE_MAP:
2828 faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + texImage->Face;
2829 break;
2830
2831 default:
2832 faceTarget = target;
2833 break;
2834 }
2835
2836 _mesa_meta_begin(ctx, MESA_META_ALL & ~MESA_META_PIXEL_STORE);
2837
2838 samplerSave = ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler ?
2839 ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler->Name : 0;
2840
2841 /* Create/bind FBO/renderbuffer */
2842 if (decompress->FBO == 0) {
2843 _mesa_GenFramebuffers(1, &decompress->FBO);
2844 _mesa_GenRenderbuffers(1, &decompress->RBO);
2845 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, decompress->FBO);
2846 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT, decompress->RBO);
2847 _mesa_FramebufferRenderbuffer(GL_FRAMEBUFFER_EXT,
2848 GL_COLOR_ATTACHMENT0_EXT,
2849 GL_RENDERBUFFER_EXT,
2850 decompress->RBO);
2851 }
2852 else {
2853 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, decompress->FBO);
2854 }
2855
2856 /* alloc dest surface */
2857 if (width > decompress->Width || height > decompress->Height) {
2858 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT, decompress->RBO);
2859 _mesa_RenderbufferStorage(GL_RENDERBUFFER_EXT, GL_RGBA,
2860 width, height);
2861 decompress->Width = width;
2862 decompress->Height = height;
2863 }
2864
2865 if (use_glsl_version) {
2866 _mesa_meta_setup_vertex_objects(&decompress->VAO, &decompress->VBO, true,
2867 2, 4, 0);
2868
2869 _mesa_meta_setup_blit_shader(ctx, target, &decompress->shaders);
2870 } else {
2871 _mesa_meta_setup_ff_tnl_for_blit(&decompress->VAO, &decompress->VBO, 3);
2872 }
2873
2874 if (!decompress->Sampler) {
2875 _mesa_GenSamplers(1, &decompress->Sampler);
2876 _mesa_BindSampler(ctx->Texture.CurrentUnit, decompress->Sampler);
2877 /* nearest filtering */
2878 _mesa_SamplerParameteri(decompress->Sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2879 _mesa_SamplerParameteri(decompress->Sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2880 /* No sRGB decode or encode.*/
2881 if (ctx->Extensions.EXT_texture_sRGB_decode) {
2882 _mesa_SamplerParameteri(decompress->Sampler, GL_TEXTURE_SRGB_DECODE_EXT,
2883 GL_SKIP_DECODE_EXT);
2884 }
2885
2886 } else {
2887 _mesa_BindSampler(ctx->Texture.CurrentUnit, decompress->Sampler);
2888 }
2889
2890 /* Silence valgrind warnings about reading uninitialized stack. */
2891 memset(verts, 0, sizeof(verts));
2892
2893 _mesa_meta_setup_texture_coords(faceTarget, slice, width, height, depth,
2894 verts[0].tex,
2895 verts[1].tex,
2896 verts[2].tex,
2897 verts[3].tex);
2898
2899 /* setup vertex positions */
2900 verts[0].x = -1.0F;
2901 verts[0].y = -1.0F;
2902 verts[1].x = 1.0F;
2903 verts[1].y = -1.0F;
2904 verts[2].x = 1.0F;
2905 verts[2].y = 1.0F;
2906 verts[3].x = -1.0F;
2907 verts[3].y = 1.0F;
2908
2909 _mesa_set_viewport(ctx, 0, 0, 0, width, height);
2910
2911 /* upload new vertex data */
2912 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
2913
2914 /* setup texture state */
2915 _mesa_BindTexture(target, texObj->Name);
2916
2917 if (!use_glsl_version)
2918 _mesa_set_enable(ctx, target, GL_TRUE);
2919
2920 {
2921 /* save texture object state */
2922 const GLint baseLevelSave = texObj->BaseLevel;
2923 const GLint maxLevelSave = texObj->MaxLevel;
2924
2925 /* restrict sampling to the texture level of interest */
2926 if (target != GL_TEXTURE_RECTANGLE_ARB) {
2927 _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, texImage->Level);
2928 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, texImage->Level);
2929 }
2930
2931 /* render quad w/ texture into renderbuffer */
2932 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2933
2934 /* Restore texture object state, the texture binding will
2935 * be restored by _mesa_meta_end().
2936 */
2937 if (target != GL_TEXTURE_RECTANGLE_ARB) {
2938 _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, baseLevelSave);
2939 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, maxLevelSave);
2940 }
2941
2942 }
2943
2944 /* read pixels from renderbuffer */
2945 {
2946 GLenum baseTexFormat = texImage->_BaseFormat;
2947 GLenum destBaseFormat = _mesa_base_tex_format(ctx, destFormat);
2948
2949 /* The pixel transfer state will be set to default values at this point
2950 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
2951 * turned off (as required by glGetTexImage) but we need to handle some
2952 * special cases. In particular, single-channel texture values are
2953 * returned as red and two-channel texture values are returned as
2954 * red/alpha.
2955 */
2956 if ((baseTexFormat == GL_LUMINANCE ||
2957 baseTexFormat == GL_LUMINANCE_ALPHA ||
2958 baseTexFormat == GL_INTENSITY) ||
2959 /* If we're reading back an RGB(A) texture (using glGetTexImage) as
2960 * luminance then we need to return L=tex(R).
2961 */
2962 ((baseTexFormat == GL_RGBA ||
2963 baseTexFormat == GL_RGB ||
2964 baseTexFormat == GL_RG) &&
2965 (destBaseFormat == GL_LUMINANCE ||
2966 destBaseFormat == GL_LUMINANCE_ALPHA ||
2967 destBaseFormat == GL_LUMINANCE_INTEGER_EXT ||
2968 destBaseFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT))) {
2969 /* Green and blue must be zero */
2970 _mesa_PixelTransferf(GL_GREEN_SCALE, 0.0f);
2971 _mesa_PixelTransferf(GL_BLUE_SCALE, 0.0f);
2972 }
2973
2974 _mesa_ReadPixels(0, 0, width, height, destFormat, destType, dest);
2975 }
2976
2977 /* disable texture unit */
2978 if (!use_glsl_version)
2979 _mesa_set_enable(ctx, target, GL_FALSE);
2980
2981 _mesa_BindSampler(ctx->Texture.CurrentUnit, samplerSave);
2982
2983 _mesa_meta_end(ctx);
2984 }
2985
2986
2987 /**
2988 * This is just a wrapper around _mesa_get_tex_image() and
2989 * decompress_texture_image(). Meta functions should not be directly called
2990 * from core Mesa.
2991 */
2992 void
2993 _mesa_meta_GetTexImage(struct gl_context *ctx,
2994 GLenum format, GLenum type, GLvoid *pixels,
2995 struct gl_texture_image *texImage)
2996 {
2997 /* We can only use the decompress-with-blit method here if the texels are
2998 * unsigned, normalized values. We could handle signed and unnormalized
2999 * with floating point renderbuffers...
3000 */
3001 if (_mesa_is_format_compressed(texImage->TexFormat) &&
3002 _mesa_get_format_datatype(texImage->TexFormat)
3003 == GL_UNSIGNED_NORMALIZED) {
3004 struct gl_texture_object *texObj = texImage->TexObject;
3005 GLuint slice;
3006 /* Need to unlock the texture here to prevent deadlock... */
3007 _mesa_unlock_texture(ctx, texObj);
3008 for (slice = 0; slice < texImage->Depth; slice++) {
3009 void *dst;
3010 if (texImage->TexObject->Target == GL_TEXTURE_2D_ARRAY
3011 || texImage->TexObject->Target == GL_TEXTURE_CUBE_MAP_ARRAY) {
3012 /* Setup pixel packing. SkipPixels and SkipRows will be applied
3013 * in the decompress_texture_image() function's call to
3014 * glReadPixels but we need to compute the dest slice's address
3015 * here (according to SkipImages and ImageHeight).
3016 */
3017 struct gl_pixelstore_attrib packing = ctx->Pack;
3018 packing.SkipPixels = 0;
3019 packing.SkipRows = 0;
3020 dst = _mesa_image_address3d(&packing, pixels, texImage->Width,
3021 texImage->Height, format, type,
3022 slice, 0, 0);
3023 }
3024 else {
3025 dst = pixels;
3026 }
3027 decompress_texture_image(ctx, texImage, slice, format, type, dst);
3028 }
3029 /* ... and relock it */
3030 _mesa_lock_texture(ctx, texObj);
3031 }
3032 else {
3033 _mesa_get_teximage(ctx, format, type, pixels, texImage);
3034 }
3035 }
3036
3037
3038 /**
3039 * Meta implementation of ctx->Driver.DrawTex() in terms
3040 * of polygon rendering.
3041 */
3042 void
3043 _mesa_meta_DrawTex(struct gl_context *ctx, GLfloat x, GLfloat y, GLfloat z,
3044 GLfloat width, GLfloat height)
3045 {
3046 struct drawtex_state *drawtex = &ctx->Meta->DrawTex;
3047 struct vertex {
3048 GLfloat x, y, z, st[MAX_TEXTURE_UNITS][2];
3049 };
3050 struct vertex verts[4];
3051 GLuint i;
3052
3053 _mesa_meta_begin(ctx, (MESA_META_RASTERIZATION |
3054 MESA_META_SHADER |
3055 MESA_META_TRANSFORM |
3056 MESA_META_VERTEX |
3057 MESA_META_VIEWPORT));
3058
3059 if (drawtex->VAO == 0) {
3060 /* one-time setup */
3061 GLint active_texture;
3062
3063 /* create vertex array object */
3064 _mesa_GenVertexArrays(1, &drawtex->VAO);
3065 _mesa_BindVertexArray(drawtex->VAO);
3066
3067 /* create vertex array buffer */
3068 _mesa_GenBuffers(1, &drawtex->VBO);
3069 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, drawtex->VBO);
3070 _mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts),
3071 NULL, GL_DYNAMIC_DRAW_ARB);
3072
3073 /* client active texture is not part of the array object */
3074 active_texture = ctx->Array.ActiveTexture;
3075
3076 /* setup vertex arrays */
3077 _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
3078 _mesa_EnableClientState(GL_VERTEX_ARRAY);
3079 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
3080 _mesa_ClientActiveTexture(GL_TEXTURE0 + i);
3081 _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(st[i]));
3082 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
3083 }
3084
3085 /* restore client active texture */
3086 _mesa_ClientActiveTexture(GL_TEXTURE0 + active_texture);
3087 }
3088 else {
3089 _mesa_BindVertexArray(drawtex->VAO);
3090 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, drawtex->VBO);
3091 }
3092
3093 /* vertex positions, texcoords */
3094 {
3095 const GLfloat x1 = x + width;
3096 const GLfloat y1 = y + height;
3097
3098 z = CLAMP(z, 0.0f, 1.0f);
3099 z = invert_z(z);
3100
3101 verts[0].x = x;
3102 verts[0].y = y;
3103 verts[0].z = z;
3104
3105 verts[1].x = x1;
3106 verts[1].y = y;
3107 verts[1].z = z;
3108
3109 verts[2].x = x1;
3110 verts[2].y = y1;
3111 verts[2].z = z;
3112
3113 verts[3].x = x;
3114 verts[3].y = y1;
3115 verts[3].z = z;
3116
3117 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
3118 const struct gl_texture_object *texObj;
3119 const struct gl_texture_image *texImage;
3120 GLfloat s, t, s1, t1;
3121 GLuint tw, th;
3122
3123 if (!ctx->Texture.Unit[i]._ReallyEnabled) {
3124 GLuint j;
3125 for (j = 0; j < 4; j++) {
3126 verts[j].st[i][0] = 0.0f;
3127 verts[j].st[i][1] = 0.0f;
3128 }
3129 continue;
3130 }
3131
3132 texObj = ctx->Texture.Unit[i]._Current;
3133 texImage = texObj->Image[0][texObj->BaseLevel];
3134 tw = texImage->Width2;
3135 th = texImage->Height2;
3136
3137 s = (GLfloat) texObj->CropRect[0] / tw;
3138 t = (GLfloat) texObj->CropRect[1] / th;
3139 s1 = (GLfloat) (texObj->CropRect[0] + texObj->CropRect[2]) / tw;
3140 t1 = (GLfloat) (texObj->CropRect[1] + texObj->CropRect[3]) / th;
3141
3142 verts[0].st[i][0] = s;
3143 verts[0].st[i][1] = t;
3144
3145 verts[1].st[i][0] = s1;
3146 verts[1].st[i][1] = t;
3147
3148 verts[2].st[i][0] = s1;
3149 verts[2].st[i][1] = t1;
3150
3151 verts[3].st[i][0] = s;
3152 verts[3].st[i][1] = t1;
3153 }
3154
3155 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
3156 }
3157
3158 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
3159
3160 _mesa_meta_end(ctx);
3161 }