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