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