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