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