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