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