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