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