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