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