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