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