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