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