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