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