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