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