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