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