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