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