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