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