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