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