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