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