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