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