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