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