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