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