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