Merge branch 'lp-offset-twoside'
[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 static GLboolean
1984 alpha_test_raster_color(struct gl_context *ctx)
1985 {
1986 GLfloat alpha = ctx->Current.RasterColor[ACOMP];
1987 GLfloat ref = ctx->Color.AlphaRef;
1988
1989 switch (ctx->Color.AlphaFunc) {
1990 case GL_NEVER:
1991 return GL_FALSE;
1992 case GL_LESS:
1993 return alpha < ref;
1994 case GL_EQUAL:
1995 return alpha == ref;
1996 case GL_LEQUAL:
1997 return alpha <= ref;
1998 case GL_GREATER:
1999 return alpha > ref;
2000 case GL_NOTEQUAL:
2001 return alpha != ref;
2002 case GL_GEQUAL:
2003 return alpha >= ref;
2004 case GL_ALWAYS:
2005 return GL_TRUE;
2006 default:
2007 assert(0);
2008 return GL_FALSE;
2009 }
2010 }
2011
2012 /**
2013 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2014 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2015 * tracker would improve performance a lot.
2016 */
2017 void
2018 _mesa_meta_Bitmap(struct gl_context *ctx,
2019 GLint x, GLint y, GLsizei width, GLsizei height,
2020 const struct gl_pixelstore_attrib *unpack,
2021 const GLubyte *bitmap1)
2022 {
2023 struct bitmap_state *bitmap = &ctx->Meta->Bitmap;
2024 struct temp_texture *tex = get_bitmap_temp_texture(ctx);
2025 const GLenum texIntFormat = GL_ALPHA;
2026 const struct gl_pixelstore_attrib unpackSave = *unpack;
2027 GLubyte fg, bg;
2028 struct vertex {
2029 GLfloat x, y, z, s, t, r, g, b, a;
2030 };
2031 struct vertex verts[4];
2032 GLboolean newTex;
2033 GLubyte *bitmap8;
2034
2035 /*
2036 * Check if swrast fallback is needed.
2037 */
2038 if (ctx->_ImageTransferState ||
2039 ctx->FragmentProgram._Enabled ||
2040 ctx->Fog.Enabled ||
2041 ctx->Texture._EnabledUnits ||
2042 width > tex->MaxSize ||
2043 height > tex->MaxSize) {
2044 _swrast_Bitmap(ctx, x, y, width, height, unpack, bitmap1);
2045 return;
2046 }
2047
2048 if (ctx->Color.AlphaEnabled && !alpha_test_raster_color(ctx))
2049 return;
2050
2051 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2052 * but a there's a few things we need to override:
2053 */
2054 _mesa_meta_begin(ctx, (META_ALPHA_TEST |
2055 META_PIXEL_STORE |
2056 META_RASTERIZATION |
2057 META_SHADER |
2058 META_TEXTURE |
2059 META_TRANSFORM |
2060 META_VERTEX |
2061 META_VIEWPORT));
2062
2063 if (bitmap->ArrayObj == 0) {
2064 /* one-time setup */
2065
2066 /* create vertex array object */
2067 _mesa_GenVertexArraysAPPLE(1, &bitmap->ArrayObj);
2068 _mesa_BindVertexArrayAPPLE(bitmap->ArrayObj);
2069
2070 /* create vertex array buffer */
2071 _mesa_GenBuffersARB(1, &bitmap->VBO);
2072 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, bitmap->VBO);
2073 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
2074 NULL, GL_DYNAMIC_DRAW_ARB);
2075
2076 /* setup vertex arrays */
2077 _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
2078 _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
2079 _mesa_ColorPointer(4, GL_FLOAT, sizeof(struct vertex), OFFSET(r));
2080 _mesa_EnableClientState(GL_VERTEX_ARRAY);
2081 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
2082 _mesa_EnableClientState(GL_COLOR_ARRAY);
2083 }
2084 else {
2085 _mesa_BindVertexArray(bitmap->ArrayObj);
2086 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, bitmap->VBO);
2087 }
2088
2089 newTex = alloc_texture(tex, width, height, texIntFormat);
2090
2091 /* vertex positions, texcoords, colors (after texture allocation!) */
2092 {
2093 const GLfloat x0 = (GLfloat) x;
2094 const GLfloat y0 = (GLfloat) y;
2095 const GLfloat x1 = (GLfloat) (x + width);
2096 const GLfloat y1 = (GLfloat) (y + height);
2097 const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
2098 GLuint i;
2099
2100 verts[0].x = x0;
2101 verts[0].y = y0;
2102 verts[0].z = z;
2103 verts[0].s = 0.0F;
2104 verts[0].t = 0.0F;
2105 verts[1].x = x1;
2106 verts[1].y = y0;
2107 verts[1].z = z;
2108 verts[1].s = tex->Sright;
2109 verts[1].t = 0.0F;
2110 verts[2].x = x1;
2111 verts[2].y = y1;
2112 verts[2].z = z;
2113 verts[2].s = tex->Sright;
2114 verts[2].t = tex->Ttop;
2115 verts[3].x = x0;
2116 verts[3].y = y1;
2117 verts[3].z = z;
2118 verts[3].s = 0.0F;
2119 verts[3].t = tex->Ttop;
2120
2121 for (i = 0; i < 4; i++) {
2122 verts[i].r = ctx->Current.RasterColor[0];
2123 verts[i].g = ctx->Current.RasterColor[1];
2124 verts[i].b = ctx->Current.RasterColor[2];
2125 verts[i].a = ctx->Current.RasterColor[3];
2126 }
2127
2128 /* upload new vertex data */
2129 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
2130 }
2131
2132 /* choose different foreground/background alpha values */
2133 CLAMPED_FLOAT_TO_UBYTE(fg, ctx->Current.RasterColor[ACOMP]);
2134 bg = (fg > 127 ? 0 : 255);
2135
2136 bitmap1 = _mesa_map_pbo_source(ctx, &unpackSave, bitmap1);
2137 if (!bitmap1) {
2138 _mesa_meta_end(ctx);
2139 return;
2140 }
2141
2142 bitmap8 = (GLubyte *) malloc(width * height);
2143 if (bitmap8) {
2144 memset(bitmap8, bg, width * height);
2145 _mesa_expand_bitmap(width, height, &unpackSave, bitmap1,
2146 bitmap8, width, fg);
2147
2148 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
2149
2150 _mesa_set_enable(ctx, GL_ALPHA_TEST, GL_TRUE);
2151 _mesa_AlphaFunc(GL_NOTEQUAL, UBYTE_TO_FLOAT(bg));
2152
2153 setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height,
2154 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap8);
2155
2156 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2157
2158 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
2159
2160 free(bitmap8);
2161 }
2162
2163 _mesa_unmap_pbo_source(ctx, &unpackSave);
2164
2165 _mesa_meta_end(ctx);
2166 }
2167
2168
2169 /**
2170 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2171 * software fallback. The fallback path will require that the texture
2172 * images are mapped.
2173 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2174 */
2175 GLboolean
2176 _mesa_meta_check_generate_mipmap_fallback(struct gl_context *ctx, GLenum target,
2177 struct gl_texture_object *texObj)
2178 {
2179 const GLuint fboSave = ctx->DrawBuffer->Name;
2180 struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap;
2181 struct gl_texture_image *baseImage;
2182 GLuint srcLevel;
2183 GLenum status;
2184
2185 /* check for fallbacks */
2186 if (!ctx->Extensions.EXT_framebuffer_object ||
2187 target == GL_TEXTURE_3D) {
2188 return GL_TRUE;
2189 }
2190
2191 srcLevel = texObj->BaseLevel;
2192 baseImage = _mesa_select_tex_image(ctx, texObj, target, srcLevel);
2193 if (!baseImage || _mesa_is_format_compressed(baseImage->TexFormat)) {
2194 return GL_TRUE;
2195 }
2196
2197 /*
2198 * Test that we can actually render in the texture's format.
2199 */
2200 if (!mipmap->FBO)
2201 _mesa_GenFramebuffersEXT(1, &mipmap->FBO);
2202 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, mipmap->FBO);
2203
2204 if (target == GL_TEXTURE_1D) {
2205 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT,
2206 GL_COLOR_ATTACHMENT0_EXT,
2207 target, texObj->Name, srcLevel);
2208 }
2209 #if 0
2210 /* other work is needed to enable 3D mipmap generation */
2211 else if (target == GL_TEXTURE_3D) {
2212 GLint zoffset = 0;
2213 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT,
2214 GL_COLOR_ATTACHMENT0_EXT,
2215 target, texObj->Name, srcLevel, zoffset);
2216 }
2217 #endif
2218 else {
2219 /* 2D / cube */
2220 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
2221 GL_COLOR_ATTACHMENT0_EXT,
2222 target, texObj->Name, srcLevel);
2223 }
2224
2225 status = _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
2226
2227 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboSave);
2228
2229 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2230 return GL_TRUE;
2231 }
2232
2233 return GL_FALSE;
2234 }
2235
2236
2237 /**
2238 * Called via ctx->Driver.GenerateMipmap()
2239 * Note: texture borders and 3D texture support not yet complete.
2240 */
2241 void
2242 _mesa_meta_GenerateMipmap(struct gl_context *ctx, GLenum target,
2243 struct gl_texture_object *texObj)
2244 {
2245 struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap;
2246 struct vertex {
2247 GLfloat x, y, s, t, r;
2248 };
2249 struct vertex verts[4];
2250 const GLuint baseLevel = texObj->BaseLevel;
2251 const GLuint maxLevel = texObj->MaxLevel;
2252 const GLenum minFilterSave = texObj->MinFilter;
2253 const GLenum magFilterSave = texObj->MagFilter;
2254 const GLint baseLevelSave = texObj->BaseLevel;
2255 const GLint maxLevelSave = texObj->MaxLevel;
2256 const GLboolean genMipmapSave = texObj->GenerateMipmap;
2257 const GLenum wrapSSave = texObj->WrapS;
2258 const GLenum wrapTSave = texObj->WrapT;
2259 const GLenum wrapRSave = texObj->WrapR;
2260 const GLuint fboSave = ctx->DrawBuffer->Name;
2261 const GLuint original_active_unit = ctx->Texture.CurrentUnit;
2262 GLenum faceTarget;
2263 GLuint dstLevel;
2264 GLuint border = 0;
2265
2266 if (_mesa_meta_check_generate_mipmap_fallback(ctx, target, texObj)) {
2267 _mesa_generate_mipmap(ctx, target, texObj);
2268 return;
2269 }
2270
2271 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
2272 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) {
2273 faceTarget = target;
2274 target = GL_TEXTURE_CUBE_MAP;
2275 }
2276 else {
2277 faceTarget = target;
2278 }
2279
2280 _mesa_meta_begin(ctx, META_ALL);
2281
2282 if (original_active_unit != 0)
2283 _mesa_BindTexture(target, texObj->Name);
2284
2285 if (mipmap->ArrayObj == 0) {
2286 /* one-time setup */
2287
2288 /* create vertex array object */
2289 _mesa_GenVertexArraysAPPLE(1, &mipmap->ArrayObj);
2290 _mesa_BindVertexArrayAPPLE(mipmap->ArrayObj);
2291
2292 /* create vertex array buffer */
2293 _mesa_GenBuffersARB(1, &mipmap->VBO);
2294 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
2295 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
2296 NULL, GL_DYNAMIC_DRAW_ARB);
2297
2298 /* setup vertex arrays */
2299 _mesa_VertexPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
2300 _mesa_TexCoordPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
2301 _mesa_EnableClientState(GL_VERTEX_ARRAY);
2302 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
2303 }
2304 else {
2305 _mesa_BindVertexArray(mipmap->ArrayObj);
2306 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
2307 }
2308
2309 if (!mipmap->FBO) {
2310 _mesa_GenFramebuffersEXT(1, &mipmap->FBO);
2311 }
2312 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, mipmap->FBO);
2313
2314 _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2315 _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2316 _mesa_TexParameteri(target, GL_GENERATE_MIPMAP, GL_FALSE);
2317 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2318 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2319 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
2320
2321 _mesa_set_enable(ctx, target, GL_TRUE);
2322
2323 /* setup texcoords once (XXX what about border?) */
2324 switch (faceTarget) {
2325 case GL_TEXTURE_1D:
2326 case GL_TEXTURE_2D:
2327 verts[0].s = 0.0F;
2328 verts[0].t = 0.0F;
2329 verts[0].r = 0.0F;
2330 verts[1].s = 1.0F;
2331 verts[1].t = 0.0F;
2332 verts[1].r = 0.0F;
2333 verts[2].s = 1.0F;
2334 verts[2].t = 1.0F;
2335 verts[2].r = 0.0F;
2336 verts[3].s = 0.0F;
2337 verts[3].t = 1.0F;
2338 verts[3].r = 0.0F;
2339 break;
2340 case GL_TEXTURE_3D:
2341 abort();
2342 break;
2343 default:
2344 /* cube face */
2345 {
2346 static const GLfloat st[4][2] = {
2347 {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}
2348 };
2349 GLuint i;
2350
2351 /* loop over quad verts */
2352 for (i = 0; i < 4; i++) {
2353 /* Compute sc = +/-scale and tc = +/-scale.
2354 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2355 * though that can still sometimes happen with this scale factor...
2356 */
2357 const GLfloat scale = 0.9999f;
2358 const GLfloat sc = (2.0f * st[i][0] - 1.0f) * scale;
2359 const GLfloat tc = (2.0f * st[i][1] - 1.0f) * scale;
2360
2361 switch (faceTarget) {
2362 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2363 verts[i].s = 1.0f;
2364 verts[i].t = -tc;
2365 verts[i].r = -sc;
2366 break;
2367 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2368 verts[i].s = -1.0f;
2369 verts[i].t = -tc;
2370 verts[i].r = sc;
2371 break;
2372 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2373 verts[i].s = sc;
2374 verts[i].t = 1.0f;
2375 verts[i].r = tc;
2376 break;
2377 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2378 verts[i].s = sc;
2379 verts[i].t = -1.0f;
2380 verts[i].r = -tc;
2381 break;
2382 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2383 verts[i].s = sc;
2384 verts[i].t = -tc;
2385 verts[i].r = 1.0f;
2386 break;
2387 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2388 verts[i].s = -sc;
2389 verts[i].t = -tc;
2390 verts[i].r = -1.0f;
2391 break;
2392 default:
2393 assert(0);
2394 }
2395 }
2396 }
2397 }
2398
2399 _mesa_set_enable(ctx, target, GL_TRUE);
2400
2401 /* setup vertex positions */
2402 {
2403 verts[0].x = 0.0F;
2404 verts[0].y = 0.0F;
2405 verts[1].x = 1.0F;
2406 verts[1].y = 0.0F;
2407 verts[2].x = 1.0F;
2408 verts[2].y = 1.0F;
2409 verts[3].x = 0.0F;
2410 verts[3].y = 1.0F;
2411
2412 /* upload new vertex data */
2413 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
2414 }
2415
2416 /* setup projection matrix */
2417 _mesa_MatrixMode(GL_PROJECTION);
2418 _mesa_LoadIdentity();
2419 _mesa_Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
2420
2421 /* texture is already locked, unlock now */
2422 _mesa_unlock_texture(ctx, texObj);
2423
2424 for (dstLevel = baseLevel + 1; dstLevel <= maxLevel; dstLevel++) {
2425 const struct gl_texture_image *srcImage;
2426 const GLuint srcLevel = dstLevel - 1;
2427 GLsizei srcWidth, srcHeight, srcDepth;
2428 GLsizei dstWidth, dstHeight, dstDepth;
2429 GLenum status;
2430
2431 srcImage = _mesa_select_tex_image(ctx, texObj, faceTarget, srcLevel);
2432 assert(srcImage->Border == 0); /* XXX we can fix this */
2433
2434 /* src size w/out border */
2435 srcWidth = srcImage->Width - 2 * border;
2436 srcHeight = srcImage->Height - 2 * border;
2437 srcDepth = srcImage->Depth - 2 * border;
2438
2439 /* new dst size w/ border */
2440 dstWidth = MAX2(1, srcWidth / 2) + 2 * border;
2441 dstHeight = MAX2(1, srcHeight / 2) + 2 * border;
2442 dstDepth = MAX2(1, srcDepth / 2) + 2 * border;
2443
2444 if (dstWidth == srcImage->Width &&
2445 dstHeight == srcImage->Height &&
2446 dstDepth == srcImage->Depth) {
2447 /* all done */
2448 break;
2449 }
2450
2451 /* Set MaxLevel large enough to hold the new level when we allocate it */
2452 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, dstLevel);
2453
2454 /* Create empty dest image */
2455 if (target == GL_TEXTURE_1D) {
2456 _mesa_TexImage1D(target, dstLevel, srcImage->InternalFormat,
2457 dstWidth, border,
2458 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2459 }
2460 else if (target == GL_TEXTURE_3D) {
2461 _mesa_TexImage3D(target, dstLevel, srcImage->InternalFormat,
2462 dstWidth, dstHeight, dstDepth, border,
2463 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2464 }
2465 else {
2466 /* 2D or cube */
2467 _mesa_TexImage2D(faceTarget, dstLevel, srcImage->InternalFormat,
2468 dstWidth, dstHeight, border,
2469 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2470
2471 if (target == GL_TEXTURE_CUBE_MAP) {
2472 /* If texturing from a cube, we need to make sure all src faces
2473 * have been defined (even if we're not sampling from them.)
2474 * Otherwise the texture object will be 'incomplete' and
2475 * texturing from it will not be allowed.
2476 */
2477 GLuint face;
2478 for (face = 0; face < 6; face++) {
2479 if (!texObj->Image[face][srcLevel] ||
2480 texObj->Image[face][srcLevel]->Width != srcWidth) {
2481 _mesa_TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face,
2482 srcLevel, srcImage->InternalFormat,
2483 srcWidth, srcHeight, border,
2484 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2485 }
2486 }
2487 }
2488 }
2489
2490 /* limit sampling to src level */
2491 _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, srcLevel);
2492 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, srcLevel);
2493
2494 /* Set to draw into the current dstLevel */
2495 if (target == GL_TEXTURE_1D) {
2496 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT,
2497 GL_COLOR_ATTACHMENT0_EXT,
2498 target,
2499 texObj->Name,
2500 dstLevel);
2501 }
2502 else if (target == GL_TEXTURE_3D) {
2503 GLint zoffset = 0; /* XXX unfinished */
2504 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT,
2505 GL_COLOR_ATTACHMENT0_EXT,
2506 target,
2507 texObj->Name,
2508 dstLevel, zoffset);
2509 }
2510 else {
2511 /* 2D / cube */
2512 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
2513 GL_COLOR_ATTACHMENT0_EXT,
2514 faceTarget,
2515 texObj->Name,
2516 dstLevel);
2517 }
2518
2519 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
2520
2521 /* sanity check */
2522 status = _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
2523 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2524 abort();
2525 break;
2526 }
2527
2528 assert(dstWidth == ctx->DrawBuffer->Width);
2529 assert(dstHeight == ctx->DrawBuffer->Height);
2530
2531 /* setup viewport */
2532 _mesa_set_viewport(ctx, 0, 0, dstWidth, dstHeight);
2533
2534 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2535 }
2536
2537 _mesa_lock_texture(ctx, texObj); /* relock */
2538
2539 _mesa_meta_end(ctx);
2540
2541 _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilterSave);
2542 _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilterSave);
2543 _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, baseLevelSave);
2544 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, maxLevelSave);
2545 _mesa_TexParameteri(target, GL_GENERATE_MIPMAP, genMipmapSave);
2546 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, wrapSSave);
2547 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, wrapTSave);
2548 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_R, wrapRSave);
2549
2550 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboSave);
2551 }
2552
2553
2554 /**
2555 * Determine the GL data type to use for the temporary image read with
2556 * ReadPixels() and passed to Tex[Sub]Image().
2557 */
2558 static GLenum
2559 get_temp_image_type(struct gl_context *ctx, GLenum baseFormat)
2560 {
2561 switch (baseFormat) {
2562 case GL_RGBA:
2563 case GL_RGB:
2564 case GL_ALPHA:
2565 case GL_LUMINANCE:
2566 case GL_LUMINANCE_ALPHA:
2567 case GL_INTENSITY:
2568 if (ctx->DrawBuffer->Visual.redBits <= 8)
2569 return GL_UNSIGNED_BYTE;
2570 else if (ctx->DrawBuffer->Visual.redBits <= 8)
2571 return GL_UNSIGNED_SHORT;
2572 else
2573 return GL_FLOAT;
2574 case GL_DEPTH_COMPONENT:
2575 return GL_UNSIGNED_INT;
2576 case GL_DEPTH_STENCIL:
2577 return GL_UNSIGNED_INT_24_8;
2578 default:
2579 _mesa_problem(ctx, "Unexpected format in get_temp_image_type()");
2580 return 0;
2581 }
2582 }
2583
2584
2585 /**
2586 * Helper for _mesa_meta_CopyTexImage1/2D() functions.
2587 * Have to be careful with locking and meta state for pixel transfer.
2588 */
2589 static void
2590 copy_tex_image(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
2591 GLenum internalFormat, GLint x, GLint y,
2592 GLsizei width, GLsizei height, GLint border)
2593 {
2594 struct gl_texture_object *texObj;
2595 struct gl_texture_image *texImage;
2596 GLenum format, type;
2597 GLint bpp;
2598 void *buf;
2599
2600 texObj = _mesa_get_current_tex_object(ctx, target);
2601 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2602
2603 /* Choose format/type for temporary image buffer */
2604 format = _mesa_base_tex_format(ctx, internalFormat);
2605 type = get_temp_image_type(ctx, format);
2606 bpp = _mesa_bytes_per_pixel(format, type);
2607 if (bpp <= 0) {
2608 _mesa_problem(ctx, "Bad bpp in meta copy_tex_image()");
2609 return;
2610 }
2611
2612 /*
2613 * Alloc image buffer (XXX could use a PBO)
2614 */
2615 buf = malloc(width * height * bpp);
2616 if (!buf) {
2617 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
2618 return;
2619 }
2620
2621 _mesa_unlock_texture(ctx, texObj); /* need to unlock first */
2622
2623 /*
2624 * Read image from framebuffer (disable pixel transfer ops)
2625 */
2626 _mesa_meta_begin(ctx, META_PIXEL_STORE | META_PIXEL_TRANSFER);
2627 ctx->Driver.ReadPixels(ctx, x, y, width, height,
2628 format, type, &ctx->Pack, buf);
2629 _mesa_meta_end(ctx);
2630
2631 if (texImage->Data) {
2632 ctx->Driver.FreeTexImageData(ctx, texImage);
2633 }
2634
2635 /* The texture's format was already chosen in _mesa_CopyTexImage() */
2636 ASSERT(texImage->TexFormat != MESA_FORMAT_NONE);
2637
2638 /*
2639 * Store texture data (with pixel transfer ops)
2640 */
2641 _mesa_meta_begin(ctx, META_PIXEL_STORE);
2642
2643 _mesa_update_state(ctx); /* to update pixel transfer state */
2644
2645 if (target == GL_TEXTURE_1D) {
2646 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
2647 width, border, format, type,
2648 buf, &ctx->Unpack, texObj, texImage);
2649 }
2650 else {
2651 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
2652 width, height, border, format, type,
2653 buf, &ctx->Unpack, texObj, texImage);
2654 }
2655 _mesa_meta_end(ctx);
2656
2657 _mesa_lock_texture(ctx, texObj); /* re-lock */
2658
2659 free(buf);
2660 }
2661
2662
2663 void
2664 _mesa_meta_CopyTexImage1D(struct gl_context *ctx, GLenum target, GLint level,
2665 GLenum internalFormat, GLint x, GLint y,
2666 GLsizei width, GLint border)
2667 {
2668 copy_tex_image(ctx, 1, target, level, internalFormat, x, y,
2669 width, 1, border);
2670 }
2671
2672
2673 void
2674 _mesa_meta_CopyTexImage2D(struct gl_context *ctx, GLenum target, GLint level,
2675 GLenum internalFormat, GLint x, GLint y,
2676 GLsizei width, GLsizei height, GLint border)
2677 {
2678 copy_tex_image(ctx, 2, target, level, internalFormat, x, y,
2679 width, height, border);
2680 }
2681
2682
2683
2684 /**
2685 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2686 * Have to be careful with locking and meta state for pixel transfer.
2687 */
2688 static void
2689 copy_tex_sub_image(struct gl_context *ctx,
2690 GLuint dims, GLenum target, GLint level,
2691 GLint xoffset, GLint yoffset, GLint zoffset,
2692 GLint x, GLint y,
2693 GLsizei width, GLsizei height)
2694 {
2695 struct gl_texture_object *texObj;
2696 struct gl_texture_image *texImage;
2697 GLenum format, type;
2698 GLint bpp;
2699 void *buf;
2700
2701 texObj = _mesa_get_current_tex_object(ctx, target);
2702 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2703
2704 /* Choose format/type for temporary image buffer */
2705 format = _mesa_get_format_base_format(texImage->TexFormat);
2706 type = get_temp_image_type(ctx, format);
2707 bpp = _mesa_bytes_per_pixel(format, type);
2708 if (bpp <= 0) {
2709 _mesa_problem(ctx, "Bad bpp in meta copy_tex_sub_image()");
2710 return;
2711 }
2712
2713 /*
2714 * Alloc image buffer (XXX could use a PBO)
2715 */
2716 buf = malloc(width * height * bpp);
2717 if (!buf) {
2718 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage%uD", dims);
2719 return;
2720 }
2721
2722 _mesa_unlock_texture(ctx, texObj); /* need to unlock first */
2723
2724 /*
2725 * Read image from framebuffer (disable pixel transfer ops)
2726 */
2727 _mesa_meta_begin(ctx, META_PIXEL_STORE | META_PIXEL_TRANSFER);
2728 ctx->Driver.ReadPixels(ctx, x, y, width, height,
2729 format, type, &ctx->Pack, buf);
2730 _mesa_meta_end(ctx);
2731
2732 _mesa_update_state(ctx); /* to update pixel transfer state */
2733
2734 /*
2735 * Store texture data (with pixel transfer ops)
2736 */
2737 _mesa_meta_begin(ctx, META_PIXEL_STORE);
2738 if (target == GL_TEXTURE_1D) {
2739 ctx->Driver.TexSubImage1D(ctx, target, level, xoffset,
2740 width, format, type, buf,
2741 &ctx->Unpack, texObj, texImage);
2742 }
2743 else if (target == GL_TEXTURE_3D) {
2744 ctx->Driver.TexSubImage3D(ctx, target, level, xoffset, yoffset, zoffset,
2745 width, height, 1, format, type, buf,
2746 &ctx->Unpack, texObj, texImage);
2747 }
2748 else {
2749 ctx->Driver.TexSubImage2D(ctx, target, level, xoffset, yoffset,
2750 width, height, format, type, buf,
2751 &ctx->Unpack, texObj, texImage);
2752 }
2753 _mesa_meta_end(ctx);
2754
2755 _mesa_lock_texture(ctx, texObj); /* re-lock */
2756
2757 free(buf);
2758 }
2759
2760
2761 void
2762 _mesa_meta_CopyTexSubImage1D(struct gl_context *ctx, GLenum target, GLint level,
2763 GLint xoffset,
2764 GLint x, GLint y, GLsizei width)
2765 {
2766 copy_tex_sub_image(ctx, 1, target, level, xoffset, 0, 0,
2767 x, y, width, 1);
2768 }
2769
2770
2771 void
2772 _mesa_meta_CopyTexSubImage2D(struct gl_context *ctx, GLenum target, GLint level,
2773 GLint xoffset, GLint yoffset,
2774 GLint x, GLint y,
2775 GLsizei width, GLsizei height)
2776 {
2777 copy_tex_sub_image(ctx, 2, target, level, xoffset, yoffset, 0,
2778 x, y, width, height);
2779 }
2780
2781
2782 void
2783 _mesa_meta_CopyTexSubImage3D(struct gl_context *ctx, GLenum target, GLint level,
2784 GLint xoffset, GLint yoffset, GLint zoffset,
2785 GLint x, GLint y,
2786 GLsizei width, GLsizei height)
2787 {
2788 copy_tex_sub_image(ctx, 3, target, level, xoffset, yoffset, zoffset,
2789 x, y, width, height);
2790 }
2791
2792
2793 void
2794 _mesa_meta_CopyColorTable(struct gl_context *ctx,
2795 GLenum target, GLenum internalformat,
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, "glCopyColorTable");
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_ColorTable(target, internalformat, width, GL_RGBA, GL_FLOAT, buf);
2814
2815 _mesa_meta_end(ctx);
2816
2817 free(buf);
2818 }
2819
2820
2821 void
2822 _mesa_meta_CopyColorSubTable(struct gl_context *ctx,GLenum target, GLsizei start,
2823 GLint x, GLint y, GLsizei width)
2824 {
2825 GLfloat *buf;
2826
2827 buf = (GLfloat *) malloc(width * 4 * sizeof(GLfloat));
2828 if (!buf) {
2829 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyColorSubTable");
2830 return;
2831 }
2832
2833 /*
2834 * Read image from framebuffer (disable pixel transfer ops)
2835 */
2836 _mesa_meta_begin(ctx, META_PIXEL_STORE | META_PIXEL_TRANSFER);
2837 ctx->Driver.ReadPixels(ctx, x, y, width, 1,
2838 GL_RGBA, GL_FLOAT, &ctx->Pack, buf);
2839
2840 _mesa_ColorSubTable(target, start, width, GL_RGBA, GL_FLOAT, buf);
2841
2842 _mesa_meta_end(ctx);
2843
2844 free(buf);
2845 }