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