mesa: remove incorrect texture state check
[mesa.git] / src / mesa / drivers / common / meta.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.6
4 *
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * Meta operations. Some GL operations can be expressed in terms of
27 * other GL operations. For example, glBlitFramebuffer() can be done
28 * with texture mapping and glClear() can be done with polygon rendering.
29 *
30 * \author Brian Paul
31 */
32
33
34 #include "main/glheader.h"
35 #include "main/mtypes.h"
36 #include "main/imports.h"
37 #include "main/arrayobj.h"
38 #include "main/blend.h"
39 #include "main/bufferobj.h"
40 #include "main/buffers.h"
41 #include "main/depth.h"
42 #include "main/enable.h"
43 #include "main/fbobject.h"
44 #include "main/image.h"
45 #include "main/macros.h"
46 #include "main/matrix.h"
47 #include "main/mipmap.h"
48 #include "main/polygon.h"
49 #include "main/readpix.h"
50 #include "main/scissor.h"
51 #include "main/shaders.h"
52 #include "main/stencil.h"
53 #include "main/texobj.h"
54 #include "main/texenv.h"
55 #include "main/teximage.h"
56 #include "main/texparam.h"
57 #include "main/texstate.h"
58 #include "main/varray.h"
59 #include "main/viewport.h"
60 #include "shader/program.h"
61 #include "shader/arbprogram.h"
62 #include "swrast/swrast.h"
63 #include "drivers/common/meta.h"
64
65
66 /** Return offset in bytes of the field within a vertex struct */
67 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
68
69
70 /**
71 * State which we may save/restore across meta ops.
72 * XXX this may be incomplete...
73 */
74 struct save_state
75 {
76 GLbitfield SavedState; /**< bitmask of META_* flags */
77
78 /** META_ALPHA_TEST */
79 GLboolean AlphaEnabled;
80
81 /** META_BLEND */
82 GLboolean BlendEnabled;
83 GLboolean ColorLogicOpEnabled;
84
85 /** META_COLOR_MASK */
86 GLubyte ColorMask[4];
87
88 /** META_DEPTH_TEST */
89 struct gl_depthbuffer_attrib Depth;
90
91 /** META_FOG */
92 GLboolean Fog;
93
94 /** META_PIXEL_STORE */
95 struct gl_pixelstore_attrib Pack, Unpack;
96
97 /** META_RASTERIZATION */
98 GLenum FrontPolygonMode, BackPolygonMode;
99 GLboolean PolygonOffset;
100 GLboolean PolygonSmooth;
101 GLboolean PolygonStipple;
102 GLboolean PolygonCull;
103
104 /** META_SCISSOR */
105 struct gl_scissor_attrib Scissor;
106
107 /** META_SHADER */
108 GLboolean VertexProgramEnabled;
109 struct gl_vertex_program *VertexProgram;
110 GLboolean FragmentProgramEnabled;
111 struct gl_fragment_program *FragmentProgram;
112 GLuint Shader;
113
114 /** META_STENCIL_TEST */
115 struct gl_stencil_attrib Stencil;
116
117 /** META_TRANSFORM */
118 GLenum MatrixMode;
119 GLfloat ModelviewMatrix[16];
120 GLfloat ProjectionMatrix[16];
121 GLfloat TextureMatrix[16];
122 GLbitfield ClipPlanesEnabled;
123
124 /** META_TEXTURE */
125 GLuint ActiveUnit;
126 GLuint ClientActiveUnit;
127 /** for unit[0] only */
128 struct gl_texture_object *CurrentTexture[NUM_TEXTURE_TARGETS];
129 /** mask of TEXTURE_2D_BIT, etc */
130 GLbitfield TexEnabled[MAX_TEXTURE_UNITS];
131 GLbitfield TexGenEnabled[MAX_TEXTURE_UNITS];
132 GLuint EnvMode; /* unit[0] only */
133
134 /** META_VERTEX */
135 struct gl_array_object *ArrayObj;
136 struct gl_buffer_object *ArrayBufferObj;
137
138 /** META_VIEWPORT */
139 GLint ViewportX, ViewportY, ViewportW, ViewportH;
140 GLclampd DepthNear, DepthFar;
141
142 /** Miscellaneous (always disabled) */
143 GLboolean Lighting;
144 };
145
146
147 /**
148 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
149 * This is currently shared by all the meta ops. But we could create a
150 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
151 */
152 struct temp_texture
153 {
154 GLuint TexObj;
155 GLenum Target; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
156 GLsizei MinSize; /**< Min texture size to allocate */
157 GLsizei MaxSize; /**< Max possible texture size */
158 GLboolean NPOT; /**< Non-power of two size OK? */
159 GLsizei Width, Height; /**< Current texture size */
160 GLenum IntFormat;
161 GLfloat Sright, Ttop; /**< right, top texcoords */
162 };
163
164
165 /**
166 * State for glBlitFramebufer()
167 */
168 struct blit_state
169 {
170 GLuint ArrayObj;
171 GLuint VBO;
172 GLuint DepthFP;
173 };
174
175
176 /**
177 * State for glClear()
178 */
179 struct clear_state
180 {
181 GLuint ArrayObj;
182 GLuint VBO;
183 };
184
185
186 /**
187 * State for glCopyPixels()
188 */
189 struct copypix_state
190 {
191 GLuint ArrayObj;
192 GLuint VBO;
193 };
194
195
196 /**
197 * State for glDrawPixels()
198 */
199 struct drawpix_state
200 {
201 GLuint ArrayObj;
202 GLuint VBO;
203
204 GLuint StencilFP; /**< Fragment program for drawing stencil images */
205 GLuint DepthFP; /**< Fragment program for drawing depth images */
206 };
207
208
209 /**
210 * State for glBitmap()
211 */
212 struct bitmap_state
213 {
214 GLuint ArrayObj;
215 GLuint VBO;
216 struct temp_texture Tex; /**< separate texture from other meta ops */
217 };
218
219
220 /**
221 * State for _mesa_meta_generate_mipmap()
222 */
223 struct gen_mipmap_state
224 {
225 GLuint ArrayObj;
226 GLuint VBO;
227 GLuint FBO;
228 };
229
230
231 /**
232 * All per-context meta state.
233 */
234 struct gl_meta_state
235 {
236 struct save_state Save; /**< state saved during meta-ops */
237
238 struct temp_texture TempTex;
239
240 struct blit_state Blit; /**< For _mesa_meta_blit_framebuffer() */
241 struct clear_state Clear; /**< For _mesa_meta_clear() */
242 struct copypix_state CopyPix; /**< For _mesa_meta_copy_pixels() */
243 struct drawpix_state DrawPix; /**< For _mesa_meta_draw_pixels() */
244 struct bitmap_state Bitmap; /**< For _mesa_meta_bitmap() */
245 struct gen_mipmap_state Mipmap; /**< For _mesa_meta_generate_mipmap() */
246 };
247
248
249 /**
250 * Initialize meta-ops for a context.
251 * To be called once during context creation.
252 */
253 void
254 _mesa_meta_init(GLcontext *ctx)
255 {
256 ASSERT(!ctx->Meta);
257
258 ctx->Meta = CALLOC_STRUCT(gl_meta_state);
259 }
260
261
262 /**
263 * Free context meta-op state.
264 * To be called once during context destruction.
265 */
266 void
267 _mesa_meta_free(GLcontext *ctx)
268 {
269 struct gl_meta_state *meta = ctx->Meta;
270
271 if (_mesa_get_current_context()) {
272 /* if there's no current context, these textures, buffers, etc should
273 * still get freed by _mesa_free_context_data().
274 */
275
276 /* the temporary texture */
277 _mesa_DeleteTextures(1, &meta->TempTex.TexObj);
278
279 /* glBlitFramebuffer */
280 _mesa_DeleteBuffersARB(1, & meta->Blit.VBO);
281 _mesa_DeleteVertexArraysAPPLE(1, &meta->Blit.ArrayObj);
282 _mesa_DeletePrograms(1, &meta->Blit.DepthFP);
283
284 /* glClear */
285 _mesa_DeleteBuffersARB(1, & meta->Clear.VBO);
286 _mesa_DeleteVertexArraysAPPLE(1, &meta->Clear.ArrayObj);
287
288 /* glCopyPixels */
289 _mesa_DeleteBuffersARB(1, & meta->CopyPix.VBO);
290 _mesa_DeleteVertexArraysAPPLE(1, &meta->CopyPix.ArrayObj);
291
292 /* glDrawPixels */
293 _mesa_DeleteBuffersARB(1, & meta->DrawPix.VBO);
294 _mesa_DeleteVertexArraysAPPLE(1, &meta->DrawPix.ArrayObj);
295 _mesa_DeletePrograms(1, &meta->DrawPix.DepthFP);
296 _mesa_DeletePrograms(1, &meta->DrawPix.StencilFP);
297
298 /* glBitmap */
299 _mesa_DeleteBuffersARB(1, & meta->Bitmap.VBO);
300 _mesa_DeleteVertexArraysAPPLE(1, &meta->Bitmap.ArrayObj);
301 _mesa_DeleteTextures(1, &meta->Bitmap.Tex.TexObj);
302 }
303
304 _mesa_free(ctx->Meta);
305 ctx->Meta = NULL;
306 }
307
308
309 /**
310 * Enter meta state. This is like a light-weight version of glPushAttrib
311 * but it also resets most GL state back to default values.
312 *
313 * \param state bitmask of META_* flags indicating which attribute groups
314 * to save and reset to their defaults
315 */
316 static void
317 _mesa_meta_begin(GLcontext *ctx, GLbitfield state)
318 {
319 struct save_state *save = &ctx->Meta->Save;
320
321 save->SavedState = state;
322
323 if (state & META_ALPHA_TEST) {
324 save->AlphaEnabled = ctx->Color.AlphaEnabled;
325 if (ctx->Color.AlphaEnabled)
326 _mesa_set_enable(ctx, GL_ALPHA_TEST, GL_FALSE);
327 }
328
329 if (state & META_BLEND) {
330 save->BlendEnabled = ctx->Color.BlendEnabled;
331 if (ctx->Color.BlendEnabled)
332 _mesa_set_enable(ctx, GL_BLEND, GL_FALSE);
333 save->ColorLogicOpEnabled = ctx->Color.ColorLogicOpEnabled;
334 if (ctx->Color.ColorLogicOpEnabled)
335 _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP, GL_FALSE);
336 }
337
338 if (state & META_COLOR_MASK) {
339 COPY_4V(save->ColorMask, ctx->Color.ColorMask);
340 if (!ctx->Color.ColorMask[0] ||
341 !ctx->Color.ColorMask[1] ||
342 !ctx->Color.ColorMask[2] ||
343 !ctx->Color.ColorMask[3])
344 _mesa_ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
345 }
346
347 if (state & META_DEPTH_TEST) {
348 save->Depth = ctx->Depth; /* struct copy */
349 if (ctx->Depth.Test)
350 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_FALSE);
351 }
352
353 if (state & META_FOG) {
354 save->Fog = ctx->Fog.Enabled;
355 if (ctx->Fog.Enabled)
356 _mesa_set_enable(ctx, GL_FOG, GL_FALSE);
357 }
358
359 if (state & META_PIXEL_STORE) {
360 save->Pack = ctx->Pack;
361 save->Unpack = ctx->Unpack;
362 ctx->Pack = ctx->DefaultPacking;
363 ctx->Unpack = ctx->DefaultPacking;
364 }
365
366 if (state & META_RASTERIZATION) {
367 save->FrontPolygonMode = ctx->Polygon.FrontMode;
368 save->BackPolygonMode = ctx->Polygon.BackMode;
369 save->PolygonOffset = ctx->Polygon.OffsetFill;
370 save->PolygonSmooth = ctx->Polygon.SmoothFlag;
371 save->PolygonStipple = ctx->Polygon.StippleFlag;
372 save->PolygonCull = ctx->Polygon.CullFlag;
373 _mesa_PolygonMode(GL_FRONT_AND_BACK, GL_FILL);
374 _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL, GL_FALSE);
375 _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, GL_FALSE);
376 _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, GL_FALSE);
377 _mesa_set_enable(ctx, GL_CULL_FACE, GL_FALSE);
378 }
379
380 if (state & META_SCISSOR) {
381 save->Scissor = ctx->Scissor; /* struct copy */
382 }
383
384 if (state & META_SHADER) {
385 if (ctx->Extensions.ARB_vertex_program) {
386 save->VertexProgramEnabled = ctx->VertexProgram.Enabled;
387 save->VertexProgram = ctx->VertexProgram.Current;
388 _mesa_set_enable(ctx, GL_VERTEX_PROGRAM_ARB, GL_FALSE);
389 }
390
391 if (ctx->Extensions.ARB_fragment_program) {
392 save->FragmentProgramEnabled = ctx->FragmentProgram.Enabled;
393 save->FragmentProgram = ctx->FragmentProgram.Current;
394 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_FALSE);
395 }
396
397 if (ctx->Extensions.ARB_shader_objects) {
398 save->Shader = ctx->Shader.CurrentProgram ?
399 ctx->Shader.CurrentProgram->Name : 0;
400 _mesa_UseProgramObjectARB(0);
401 }
402 }
403
404 if (state & META_STENCIL_TEST) {
405 save->Stencil = ctx->Stencil; /* struct copy */
406 if (ctx->Stencil.Enabled)
407 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_FALSE);
408 /* NOTE: other stencil state not reset */
409 }
410
411 if (state & META_TEXTURE) {
412 GLuint u, tgt;
413
414 save->ActiveUnit = ctx->Texture.CurrentUnit;
415 save->ClientActiveUnit = ctx->Array.ActiveTexture;
416 save->EnvMode = ctx->Texture.Unit[0].EnvMode;
417
418 /* Disable all texture units */
419 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
420 save->TexEnabled[u] = ctx->Texture.Unit[u].Enabled;
421 save->TexGenEnabled[u] = ctx->Texture.Unit[u].TexGenEnabled;
422 if (ctx->Texture.Unit[u].Enabled ||
423 ctx->Texture.Unit[u].TexGenEnabled) {
424 _mesa_ActiveTextureARB(GL_TEXTURE0 + u);
425 _mesa_set_enable(ctx, GL_TEXTURE_1D, GL_FALSE);
426 _mesa_set_enable(ctx, GL_TEXTURE_2D, GL_FALSE);
427 _mesa_set_enable(ctx, GL_TEXTURE_3D, GL_FALSE);
428 _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP, GL_FALSE);
429 _mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE, GL_FALSE);
430 _mesa_set_enable(ctx, GL_TEXTURE_GEN_S, GL_FALSE);
431 _mesa_set_enable(ctx, GL_TEXTURE_GEN_T, GL_FALSE);
432 _mesa_set_enable(ctx, GL_TEXTURE_GEN_R, GL_FALSE);
433 _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q, GL_FALSE);
434 }
435 }
436
437 /* save current texture objects for unit[0] only */
438 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
439 _mesa_reference_texobj(&save->CurrentTexture[tgt],
440 ctx->Texture.Unit[0].CurrentTex[tgt]);
441 }
442
443 /* set defaults for unit[0] */
444 _mesa_ActiveTextureARB(GL_TEXTURE0);
445 _mesa_ClientActiveTextureARB(GL_TEXTURE0);
446 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
447 }
448
449 if (state & META_TRANSFORM) {
450 GLuint activeTexture = ctx->Texture.CurrentUnit;
451 _mesa_memcpy(save->ModelviewMatrix, ctx->ModelviewMatrixStack.Top->m,
452 16 * sizeof(GLfloat));
453 _mesa_memcpy(save->ProjectionMatrix, ctx->ProjectionMatrixStack.Top->m,
454 16 * sizeof(GLfloat));
455 _mesa_memcpy(save->TextureMatrix, ctx->TextureMatrixStack[0].Top->m,
456 16 * sizeof(GLfloat));
457 save->MatrixMode = ctx->Transform.MatrixMode;
458 /* set 1:1 vertex:pixel coordinate transform */
459 _mesa_ActiveTextureARB(GL_TEXTURE0);
460 _mesa_MatrixMode(GL_TEXTURE);
461 _mesa_LoadIdentity();
462 _mesa_ActiveTextureARB(GL_TEXTURE0 + activeTexture);
463 _mesa_MatrixMode(GL_MODELVIEW);
464 _mesa_LoadIdentity();
465 _mesa_MatrixMode(GL_PROJECTION);
466 _mesa_LoadIdentity();
467 _mesa_Ortho(0.0F, ctx->DrawBuffer->Width,
468 0.0F, ctx->DrawBuffer->Height,
469 -1.0F, 1.0F);
470 save->ClipPlanesEnabled = ctx->Transform.ClipPlanesEnabled;
471 if (ctx->Transform.ClipPlanesEnabled) {
472 GLuint i;
473 for (i = 0; i < ctx->Const.MaxClipPlanes; i++) {
474 _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_FALSE);
475 }
476 }
477 }
478
479 if (state & META_VERTEX) {
480 /* save vertex array object state */
481 _mesa_reference_array_object(ctx, &save->ArrayObj,
482 ctx->Array.ArrayObj);
483 _mesa_reference_buffer_object(ctx, &save->ArrayBufferObj,
484 ctx->Array.ArrayBufferObj);
485 /* set some default state? */
486 }
487
488 if (state & META_VIEWPORT) {
489 /* save viewport state */
490 save->ViewportX = ctx->Viewport.X;
491 save->ViewportY = ctx->Viewport.Y;
492 save->ViewportW = ctx->Viewport.Width;
493 save->ViewportH = ctx->Viewport.Height;
494 /* set viewport to match window size */
495 if (ctx->Viewport.X != 0 ||
496 ctx->Viewport.Y != 0 ||
497 ctx->Viewport.Width != ctx->DrawBuffer->Width ||
498 ctx->Viewport.Height != ctx->DrawBuffer->Height) {
499 _mesa_set_viewport(ctx, 0, 0,
500 ctx->DrawBuffer->Width, ctx->DrawBuffer->Height);
501 }
502 /* save depth range state */
503 save->DepthNear = ctx->Viewport.Near;
504 save->DepthFar = ctx->Viewport.Far;
505 /* set depth range to default */
506 _mesa_DepthRange(0.0, 1.0);
507 }
508
509 /* misc */
510 {
511 save->Lighting = ctx->Light.Enabled;
512 if (ctx->Light.Enabled)
513 _mesa_set_enable(ctx, GL_LIGHTING, GL_FALSE);
514 }
515 }
516
517
518 /**
519 * Leave meta state. This is like a light-weight version of glPopAttrib().
520 */
521 static void
522 _mesa_meta_end(GLcontext *ctx)
523 {
524 struct save_state *save = &ctx->Meta->Save;
525 const GLbitfield state = save->SavedState;
526
527 if (state & META_ALPHA_TEST) {
528 if (ctx->Color.AlphaEnabled != save->AlphaEnabled)
529 _mesa_set_enable(ctx, GL_ALPHA_TEST, save->AlphaEnabled);
530 }
531
532 if (state & META_BLEND) {
533 if (ctx->Color.BlendEnabled != save->BlendEnabled)
534 _mesa_set_enable(ctx, GL_BLEND, save->BlendEnabled);
535 if (ctx->Color.ColorLogicOpEnabled != save->ColorLogicOpEnabled)
536 _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP, save->ColorLogicOpEnabled);
537 }
538
539 if (state & META_COLOR_MASK) {
540 if (!TEST_EQ_4V(ctx->Color.ColorMask, save->ColorMask))
541 _mesa_ColorMask(save->ColorMask[0], save->ColorMask[1],
542 save->ColorMask[2], save->ColorMask[3]);
543 }
544
545 if (state & META_DEPTH_TEST) {
546 if (ctx->Depth.Test != save->Depth.Test)
547 _mesa_set_enable(ctx, GL_DEPTH_TEST, save->Depth.Test);
548 _mesa_DepthFunc(save->Depth.Func);
549 _mesa_DepthMask(save->Depth.Mask);
550 }
551
552 if (state & META_FOG) {
553 _mesa_set_enable(ctx, GL_FOG, save->Fog);
554 }
555
556 if (state & META_PIXEL_STORE) {
557 ctx->Pack = save->Pack;
558 ctx->Unpack = save->Unpack;
559 }
560
561 if (state & META_RASTERIZATION) {
562 _mesa_PolygonMode(GL_FRONT, save->FrontPolygonMode);
563 _mesa_PolygonMode(GL_BACK, save->BackPolygonMode);
564 _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, save->PolygonStipple);
565 _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL, save->PolygonOffset);
566 _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, save->PolygonSmooth);
567 _mesa_set_enable(ctx, GL_CULL_FACE, save->PolygonCull);
568 }
569
570 if (state & META_SCISSOR) {
571 _mesa_set_enable(ctx, GL_SCISSOR_TEST, save->Scissor.Enabled);
572 _mesa_Scissor(save->Scissor.X, save->Scissor.Y,
573 save->Scissor.Width, save->Scissor.Height);
574 }
575
576 if (state & META_SHADER) {
577 if (ctx->Extensions.ARB_vertex_program) {
578 _mesa_set_enable(ctx, GL_VERTEX_PROGRAM_ARB,
579 save->VertexProgramEnabled);
580 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
581 save->VertexProgram);
582 }
583
584 if (ctx->Extensions.ARB_fragment_program) {
585 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB,
586 save->FragmentProgramEnabled);
587 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
588 save->FragmentProgram);
589 }
590
591 if (ctx->Extensions.ARB_shader_objects) {
592 _mesa_UseProgramObjectARB(save->Shader);
593 }
594 }
595
596 if (state & META_STENCIL_TEST) {
597 const struct gl_stencil_attrib *stencil = &save->Stencil;
598
599 _mesa_set_enable(ctx, GL_STENCIL_TEST, stencil->Enabled);
600 _mesa_ClearStencil(stencil->Clear);
601 if (ctx->Extensions.EXT_stencil_two_side) {
602 _mesa_set_enable(ctx, GL_STENCIL_TEST_TWO_SIDE_EXT,
603 stencil->TestTwoSide);
604 _mesa_ActiveStencilFaceEXT(stencil->ActiveFace
605 ? GL_BACK : GL_FRONT);
606 }
607 /* front state */
608 _mesa_StencilFuncSeparate(GL_FRONT,
609 stencil->Function[0],
610 stencil->Ref[0],
611 stencil->ValueMask[0]);
612 _mesa_StencilMaskSeparate(GL_FRONT, stencil->WriteMask[0]);
613 _mesa_StencilOpSeparate(GL_FRONT, stencil->FailFunc[0],
614 stencil->ZFailFunc[0],
615 stencil->ZPassFunc[0]);
616 /* back state */
617 _mesa_StencilFuncSeparate(GL_BACK,
618 stencil->Function[1],
619 stencil->Ref[1],
620 stencil->ValueMask[1]);
621 _mesa_StencilMaskSeparate(GL_BACK, stencil->WriteMask[1]);
622 _mesa_StencilOpSeparate(GL_BACK, stencil->FailFunc[1],
623 stencil->ZFailFunc[1],
624 stencil->ZPassFunc[1]);
625 }
626
627 if (state & META_TEXTURE) {
628 GLuint u, tgt;
629
630 ASSERT(ctx->Texture.CurrentUnit == 0);
631
632 /* restore texenv for unit[0] */
633 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, save->EnvMode);
634
635 /* restore texture objects for unit[0] only */
636 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
637 _mesa_reference_texobj(&ctx->Texture.Unit[0].CurrentTex[tgt],
638 save->CurrentTexture[tgt]);
639 }
640
641 /* Re-enable textures, texgen */
642 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
643 if (save->TexEnabled[u]) {
644 _mesa_ActiveTextureARB(GL_TEXTURE0 + u);
645
646 if (save->TexEnabled[u] & TEXTURE_1D_BIT)
647 _mesa_set_enable(ctx, GL_TEXTURE_1D, GL_TRUE);
648 if (save->TexEnabled[u] & TEXTURE_2D_BIT)
649 _mesa_set_enable(ctx, GL_TEXTURE_2D, GL_TRUE);
650 if (save->TexEnabled[u] & TEXTURE_3D_BIT)
651 _mesa_set_enable(ctx, GL_TEXTURE_3D, GL_TRUE);
652 if (save->TexEnabled[u] & TEXTURE_CUBE_BIT)
653 _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP, GL_TRUE);
654 if (save->TexEnabled[u] & TEXTURE_RECT_BIT)
655 _mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE, GL_TRUE);
656 }
657
658 if (save->TexGenEnabled[u]) {
659 _mesa_ActiveTextureARB(GL_TEXTURE0 + u);
660
661 if (save->TexGenEnabled[u] & S_BIT)
662 _mesa_set_enable(ctx, GL_TEXTURE_GEN_S, GL_TRUE);
663 if (save->TexGenEnabled[u] & T_BIT)
664 _mesa_set_enable(ctx, GL_TEXTURE_GEN_T, GL_TRUE);
665 if (save->TexGenEnabled[u] & R_BIT)
666 _mesa_set_enable(ctx, GL_TEXTURE_GEN_R, GL_TRUE);
667 if (save->TexGenEnabled[u] & Q_BIT)
668 _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q, GL_TRUE);
669 }
670 }
671
672 /* restore current unit state */
673 _mesa_ActiveTextureARB(GL_TEXTURE0 + save->ActiveUnit);
674 _mesa_ClientActiveTextureARB(GL_TEXTURE0 + save->ClientActiveUnit);
675 }
676
677 if (state & META_TRANSFORM) {
678 GLuint activeTexture = ctx->Texture.CurrentUnit;
679 _mesa_ActiveTextureARB(GL_TEXTURE0);
680 _mesa_MatrixMode(GL_TEXTURE);
681 _mesa_LoadMatrixf(save->TextureMatrix);
682 _mesa_ActiveTextureARB(GL_TEXTURE0 + activeTexture);
683
684 _mesa_MatrixMode(GL_MODELVIEW);
685 _mesa_LoadMatrixf(save->ModelviewMatrix);
686
687 _mesa_MatrixMode(GL_PROJECTION);
688 _mesa_LoadMatrixf(save->ProjectionMatrix);
689
690 _mesa_MatrixMode(save->MatrixMode);
691
692 save->ClipPlanesEnabled = ctx->Transform.ClipPlanesEnabled;
693 if (save->ClipPlanesEnabled) {
694 GLuint i;
695 for (i = 0; i < ctx->Const.MaxClipPlanes; i++) {
696 if (save->ClipPlanesEnabled & (1 << i)) {
697 _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_TRUE);
698 }
699 }
700 }
701 }
702
703 if (state & META_VERTEX) {
704 /* restore vertex buffer object */
705 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, save->ArrayBufferObj->Name);
706 _mesa_reference_buffer_object(ctx, &save->ArrayBufferObj, NULL);
707
708 /* restore vertex array object */
709 _mesa_BindVertexArray(save->ArrayObj->Name);
710 _mesa_reference_array_object(ctx, &save->ArrayObj, NULL);
711 }
712
713 if (state & META_VIEWPORT) {
714 if (save->ViewportX != ctx->Viewport.X ||
715 save->ViewportY != ctx->Viewport.Y ||
716 save->ViewportW != ctx->Viewport.Width ||
717 save->ViewportH != ctx->Viewport.Height) {
718 _mesa_set_viewport(ctx, save->ViewportX, save->ViewportY,
719 save->ViewportW, save->ViewportH);
720 }
721 _mesa_DepthRange(save->DepthNear, save->DepthFar);
722 }
723
724 /* misc */
725 if (save->Lighting) {
726 _mesa_set_enable(ctx, GL_LIGHTING, GL_TRUE);
727 }
728 if (save->Fog) {
729 _mesa_set_enable(ctx, GL_FOG, GL_TRUE);
730 }
731 }
732
733
734 /**
735 * One-time init for a temp_texture object.
736 * Choose tex target, compute max tex size, etc.
737 */
738 static void
739 init_temp_texture(GLcontext *ctx, struct temp_texture *tex)
740 {
741 /* prefer texture rectangle */
742 if (ctx->Extensions.NV_texture_rectangle) {
743 tex->Target = GL_TEXTURE_RECTANGLE;
744 tex->MaxSize = ctx->Const.MaxTextureRectSize;
745 tex->NPOT = GL_TRUE;
746 }
747 else {
748 /* use 2D texture, NPOT if possible */
749 tex->Target = GL_TEXTURE_2D;
750 tex->MaxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
751 tex->NPOT = ctx->Extensions.ARB_texture_non_power_of_two;
752 }
753 tex->MinSize = 16; /* 16 x 16 at least */
754 assert(tex->MaxSize > 0);
755
756 _mesa_GenTextures(1, &tex->TexObj);
757 _mesa_BindTexture(tex->Target, tex->TexObj);
758 }
759
760
761 /**
762 * Return pointer to temp_texture info for non-bitmap ops.
763 * This does some one-time init if needed.
764 */
765 static struct temp_texture *
766 get_temp_texture(GLcontext *ctx)
767 {
768 struct temp_texture *tex = &ctx->Meta->TempTex;
769
770 if (!tex->TexObj) {
771 init_temp_texture(ctx, tex);
772 }
773
774 return tex;
775 }
776
777
778 /**
779 * Return pointer to temp_texture info for _mesa_meta_bitmap().
780 * We use a separate texture for bitmaps to reduce texture
781 * allocation/deallocation.
782 */
783 static struct temp_texture *
784 get_bitmap_temp_texture(GLcontext *ctx)
785 {
786 struct temp_texture *tex = &ctx->Meta->Bitmap.Tex;
787
788 if (!tex->TexObj) {
789 init_temp_texture(ctx, tex);
790 }
791
792 return tex;
793 }
794
795
796 /**
797 * Compute the width/height of texture needed to draw an image of the
798 * given size. Return a flag indicating whether the current texture
799 * can be re-used (glTexSubImage2D) or if a new texture needs to be
800 * allocated (glTexImage2D).
801 * Also, compute s/t texcoords for drawing.
802 *
803 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
804 */
805 static GLboolean
806 alloc_texture(struct temp_texture *tex,
807 GLsizei width, GLsizei height, GLenum intFormat)
808 {
809 GLboolean newTex = GL_FALSE;
810
811 ASSERT(width <= tex->MaxSize);
812 ASSERT(height <= tex->MaxSize);
813
814 if (width > tex->Width ||
815 height > tex->Height ||
816 intFormat != tex->IntFormat) {
817 /* alloc new texture (larger or different format) */
818
819 if (tex->NPOT) {
820 /* use non-power of two size */
821 tex->Width = MAX2(tex->MinSize, width);
822 tex->Height = MAX2(tex->MinSize, height);
823 }
824 else {
825 /* find power of two size */
826 GLsizei w, h;
827 w = h = tex->MinSize;
828 while (w < width)
829 w *= 2;
830 while (h < height)
831 h *= 2;
832 tex->Width = w;
833 tex->Height = h;
834 }
835
836 tex->IntFormat = intFormat;
837
838 newTex = GL_TRUE;
839 }
840
841 /* compute texcoords */
842 if (tex->Target == GL_TEXTURE_RECTANGLE) {
843 tex->Sright = (GLfloat) width;
844 tex->Ttop = (GLfloat) height;
845 }
846 else {
847 tex->Sright = (GLfloat) width / tex->Width;
848 tex->Ttop = (GLfloat) height / tex->Height;
849 }
850
851 return newTex;
852 }
853
854
855 /**
856 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
857 */
858 static void
859 setup_copypix_texture(struct temp_texture *tex,
860 GLboolean newTex,
861 GLint srcX, GLint srcY,
862 GLsizei width, GLsizei height, GLenum intFormat,
863 GLenum filter)
864 {
865 _mesa_BindTexture(tex->Target, tex->TexObj);
866 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MIN_FILTER, filter);
867 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MAG_FILTER, filter);
868 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
869
870 /* copy framebuffer image to texture */
871 if (newTex) {
872 /* create new tex image */
873 if (tex->Width == width && tex->Height == height) {
874 /* create new tex with framebuffer data */
875 _mesa_CopyTexImage2D(tex->Target, 0, tex->IntFormat,
876 srcX, srcY, width, height, 0);
877 }
878 else {
879 /* create empty texture */
880 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
881 tex->Width, tex->Height, 0,
882 intFormat, GL_UNSIGNED_BYTE, NULL);
883 /* load image */
884 _mesa_CopyTexSubImage2D(tex->Target, 0,
885 0, 0, srcX, srcY, width, height);
886 }
887 }
888 else {
889 /* replace existing tex image */
890 _mesa_CopyTexSubImage2D(tex->Target, 0,
891 0, 0, srcX, srcY, width, height);
892 }
893 }
894
895
896 /**
897 * Setup/load texture for glDrawPixels.
898 */
899 static void
900 setup_drawpix_texture(struct temp_texture *tex,
901 GLboolean newTex,
902 GLenum texIntFormat,
903 GLsizei width, GLsizei height,
904 GLenum format, GLenum type,
905 const GLvoid *pixels)
906 {
907 _mesa_BindTexture(tex->Target, tex->TexObj);
908 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
909 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
910 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
911
912 /* copy pixel data to texture */
913 if (newTex) {
914 /* create new tex image */
915 if (tex->Width == width && tex->Height == height) {
916 /* create new tex and load image data */
917 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
918 tex->Width, tex->Height, 0, format, type, pixels);
919 }
920 else {
921 /* create empty texture */
922 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
923 tex->Width, tex->Height, 0, format, type, NULL);
924 /* load image */
925 _mesa_TexSubImage2D(tex->Target, 0,
926 0, 0, width, height, format, type, pixels);
927 }
928 }
929 else {
930 /* replace existing tex image */
931 _mesa_TexSubImage2D(tex->Target, 0,
932 0, 0, width, height, format, type, pixels);
933 }
934 }
935
936
937
938 /**
939 * One-time init for drawing depth pixels.
940 */
941 static void
942 init_blit_depth_pixels(GLcontext *ctx)
943 {
944 static const char *program =
945 "!!ARBfp1.0\n"
946 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
947 "END \n";
948 char program2[200];
949 struct blit_state *blit = &ctx->Meta->Blit;
950 struct temp_texture *tex = get_temp_texture(ctx);
951 const char *texTarget;
952
953 assert(blit->DepthFP == 0);
954
955 /* replace %s with "RECT" or "2D" */
956 assert(strlen(program) + 4 < sizeof(program2));
957 if (tex->Target == GL_TEXTURE_RECTANGLE)
958 texTarget = "RECT";
959 else
960 texTarget = "2D";
961 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
962
963 _mesa_GenPrograms(1, &blit->DepthFP);
964 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, blit->DepthFP);
965 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
966 strlen(program2), (const GLubyte *) program2);
967 }
968
969
970 /**
971 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
972 * of texture mapping and polygon rendering.
973 */
974 void
975 _mesa_meta_blit_framebuffer(GLcontext *ctx,
976 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
977 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
978 GLbitfield mask, GLenum filter)
979 {
980 struct blit_state *blit = &ctx->Meta->Blit;
981 struct temp_texture *tex = get_temp_texture(ctx);
982 const GLsizei maxTexSize = tex->MaxSize;
983 const GLint srcX = MIN2(srcX0, srcX1);
984 const GLint srcY = MIN2(srcY0, srcY1);
985 const GLint srcW = abs(srcX1 - srcX0);
986 const GLint srcH = abs(srcY1 - srcY0);
987 const GLboolean srcFlipX = srcX1 < srcX0;
988 const GLboolean srcFlipY = srcY1 < srcY0;
989 struct vertex {
990 GLfloat x, y, s, t;
991 };
992 struct vertex verts[4];
993 GLboolean newTex;
994
995 if (srcW > maxTexSize || srcH > maxTexSize) {
996 /* XXX avoid this fallback */
997 _swrast_BlitFramebuffer(ctx, srcX0, srcY0, srcX1, srcY1,
998 dstX0, dstY0, dstX1, dstY1, mask, filter);
999 return;
1000 }
1001
1002 if (srcFlipX) {
1003 GLint tmp = dstX0;
1004 dstX0 = dstX1;
1005 dstX1 = tmp;
1006 }
1007
1008 if (srcFlipY) {
1009 GLint tmp = dstY0;
1010 dstY0 = dstY1;
1011 dstY1 = tmp;
1012 }
1013
1014 /* only scissor effects blit so save/clear all other relevant state */
1015 _mesa_meta_begin(ctx, ~META_SCISSOR);
1016
1017 if (blit->ArrayObj == 0) {
1018 /* one-time setup */
1019
1020 /* create vertex array object */
1021 _mesa_GenVertexArrays(1, &blit->ArrayObj);
1022 _mesa_BindVertexArray(blit->ArrayObj);
1023
1024 /* create vertex array buffer */
1025 _mesa_GenBuffersARB(1, &blit->VBO);
1026 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, blit->VBO);
1027 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
1028 NULL, GL_DYNAMIC_DRAW_ARB);
1029
1030 /* setup vertex arrays */
1031 _mesa_VertexPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
1032 _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
1033 _mesa_EnableClientState(GL_VERTEX_ARRAY);
1034 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
1035 }
1036 else {
1037 _mesa_BindVertexArray(blit->ArrayObj);
1038 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, blit->VBO);
1039 }
1040
1041 newTex = alloc_texture(tex, srcW, srcH, GL_RGBA);
1042
1043 /* vertex positions/texcoords (after texture allocation!) */
1044 {
1045 verts[0].x = (GLfloat) dstX0;
1046 verts[0].y = (GLfloat) dstY0;
1047 verts[1].x = (GLfloat) dstX1;
1048 verts[1].y = (GLfloat) dstY0;
1049 verts[2].x = (GLfloat) dstX1;
1050 verts[2].y = (GLfloat) dstY1;
1051 verts[3].x = (GLfloat) dstX0;
1052 verts[3].y = (GLfloat) dstY1;
1053
1054 verts[0].s = 0.0F;
1055 verts[0].t = 0.0F;
1056 verts[1].s = tex->Sright;
1057 verts[1].t = 0.0F;
1058 verts[2].s = tex->Sright;
1059 verts[2].t = tex->Ttop;
1060 verts[3].s = 0.0F;
1061 verts[3].t = tex->Ttop;
1062
1063 /* upload new vertex data */
1064 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1065 }
1066
1067 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
1068
1069 if (mask & GL_COLOR_BUFFER_BIT) {
1070 setup_copypix_texture(tex, newTex, srcX, srcY, srcW, srcH,
1071 GL_RGBA, filter);
1072 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1073 mask &= ~GL_COLOR_BUFFER_BIT;
1074 }
1075
1076 if (mask & GL_DEPTH_BUFFER_BIT) {
1077 GLuint *tmp = (GLuint *) _mesa_malloc(srcW * srcH * sizeof(GLuint));
1078 if (tmp) {
1079 if (!blit->DepthFP)
1080 init_blit_depth_pixels(ctx);
1081
1082 /* maybe change tex format here */
1083 newTex = alloc_texture(tex, srcW, srcH, GL_DEPTH_COMPONENT);
1084
1085 _mesa_ReadPixels(srcX, srcY, srcW, srcH,
1086 GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, tmp);
1087
1088 setup_drawpix_texture(tex, newTex, GL_DEPTH_COMPONENT, srcW, srcH,
1089 GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, tmp);
1090
1091 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, blit->DepthFP);
1092 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
1093 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1094 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE);
1095 _mesa_DepthFunc(GL_ALWAYS);
1096 _mesa_DepthMask(GL_TRUE);
1097
1098 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1099 mask &= ~GL_DEPTH_BUFFER_BIT;
1100
1101 _mesa_free(tmp);
1102 }
1103 }
1104
1105 if (mask & GL_STENCIL_BUFFER_BIT) {
1106 /* XXX can't easily do stencil */
1107 }
1108
1109 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
1110
1111 _mesa_meta_end(ctx);
1112
1113 if (mask) {
1114 _swrast_BlitFramebuffer(ctx, srcX0, srcY0, srcX1, srcY1,
1115 dstX0, dstY0, dstX1, dstY1, mask, filter);
1116 }
1117 }
1118
1119
1120 /**
1121 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1122 */
1123 void
1124 _mesa_meta_clear(GLcontext *ctx, GLbitfield buffers)
1125 {
1126 struct clear_state *clear = &ctx->Meta->Clear;
1127 struct vertex {
1128 GLfloat x, y, z, r, g, b, a;
1129 };
1130 struct vertex verts[4];
1131 /* save all state but scissor, pixel pack/unpack */
1132 GLbitfield metaSave = META_ALL - META_SCISSOR - META_PIXEL_STORE;
1133
1134 if (buffers & BUFFER_BITS_COLOR) {
1135 /* if clearing color buffers, don't save/restore colormask */
1136 metaSave -= META_COLOR_MASK;
1137 }
1138
1139 _mesa_meta_begin(ctx, metaSave);
1140
1141 if (clear->ArrayObj == 0) {
1142 /* one-time setup */
1143
1144 /* create vertex array object */
1145 _mesa_GenVertexArrays(1, &clear->ArrayObj);
1146 _mesa_BindVertexArray(clear->ArrayObj);
1147
1148 /* create vertex array buffer */
1149 _mesa_GenBuffersARB(1, &clear->VBO);
1150 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, clear->VBO);
1151 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
1152 NULL, GL_DYNAMIC_DRAW_ARB);
1153
1154 /* setup vertex arrays */
1155 _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
1156 _mesa_ColorPointer(4, GL_FLOAT, sizeof(struct vertex), OFFSET(r));
1157 _mesa_EnableClientState(GL_VERTEX_ARRAY);
1158 _mesa_EnableClientState(GL_COLOR_ARRAY);
1159 }
1160 else {
1161 _mesa_BindVertexArray(clear->ArrayObj);
1162 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, clear->VBO);
1163 }
1164
1165 /* GL_COLOR_BUFFER_BIT */
1166 if (buffers & BUFFER_BITS_COLOR) {
1167 /* leave colormask, glDrawBuffer state as-is */
1168 }
1169 else {
1170 ASSERT(metaSave & META_COLOR_MASK);
1171 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1172 }
1173
1174 /* GL_DEPTH_BUFFER_BIT */
1175 if (buffers & BUFFER_BIT_DEPTH) {
1176 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE);
1177 _mesa_DepthFunc(GL_ALWAYS);
1178 _mesa_DepthMask(GL_TRUE);
1179 }
1180 else {
1181 assert(!ctx->Depth.Test);
1182 }
1183
1184 /* GL_STENCIL_BUFFER_BIT */
1185 if (buffers & BUFFER_BIT_STENCIL) {
1186 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
1187 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK,
1188 GL_REPLACE, GL_REPLACE, GL_REPLACE);
1189 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK, GL_ALWAYS,
1190 ctx->Stencil.Clear & 0x7fffffff,
1191 ctx->Stencil.WriteMask[0]);
1192 }
1193 else {
1194 assert(!ctx->Stencil.Enabled);
1195 }
1196
1197 /* vertex positions/colors */
1198 {
1199 const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin;
1200 const GLfloat y0 = (GLfloat) ctx->DrawBuffer->_Ymin;
1201 const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax;
1202 const GLfloat y1 = (GLfloat) ctx->DrawBuffer->_Ymax;
1203 const GLfloat z = 1.0 - 2.0 * ctx->Depth.Clear;
1204 GLuint i;
1205
1206 verts[0].x = x0;
1207 verts[0].y = y0;
1208 verts[0].z = z;
1209 verts[1].x = x1;
1210 verts[1].y = y0;
1211 verts[1].z = z;
1212 verts[2].x = x1;
1213 verts[2].y = y1;
1214 verts[2].z = z;
1215 verts[3].x = x0;
1216 verts[3].y = y1;
1217 verts[3].z = z;
1218
1219 /* vertex colors */
1220 for (i = 0; i < 4; i++) {
1221 verts[i].r = ctx->Color.ClearColor[0];
1222 verts[i].g = ctx->Color.ClearColor[1];
1223 verts[i].b = ctx->Color.ClearColor[2];
1224 verts[i].a = ctx->Color.ClearColor[3];
1225 }
1226
1227 /* upload new vertex data */
1228 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1229 }
1230
1231 /* draw quad */
1232 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1233
1234 _mesa_meta_end(ctx);
1235 }
1236
1237
1238 /**
1239 * Meta implementation of ctx->Driver.CopyPixels() in terms
1240 * of texture mapping and polygon rendering.
1241 */
1242 void
1243 _mesa_meta_copy_pixels(GLcontext *ctx, GLint srcX, GLint srcY,
1244 GLsizei width, GLsizei height,
1245 GLint dstX, GLint dstY, GLenum type)
1246 {
1247 struct copypix_state *copypix = &ctx->Meta->CopyPix;
1248 struct temp_texture *tex = get_temp_texture(ctx);
1249 struct vertex {
1250 GLfloat x, y, z, s, t;
1251 };
1252 struct vertex verts[4];
1253 GLboolean newTex;
1254 GLenum intFormat = GL_RGBA;
1255
1256 if (type != GL_COLOR ||
1257 ctx->_ImageTransferState ||
1258 ctx->Fog.Enabled ||
1259 width > tex->MaxSize ||
1260 height > tex->MaxSize) {
1261 /* XXX avoid this fallback */
1262 _swrast_CopyPixels(ctx, srcX, srcY, width, height, dstX, dstY, type);
1263 return;
1264 }
1265
1266 /* Most GL state applies to glCopyPixels, but a there's a few things
1267 * we need to override:
1268 */
1269 _mesa_meta_begin(ctx, (META_RASTERIZATION |
1270 META_SHADER |
1271 META_TEXTURE |
1272 META_TRANSFORM |
1273 META_VERTEX |
1274 META_VIEWPORT));
1275
1276 if (copypix->ArrayObj == 0) {
1277 /* one-time setup */
1278
1279 /* create vertex array object */
1280 _mesa_GenVertexArrays(1, &copypix->ArrayObj);
1281 _mesa_BindVertexArray(copypix->ArrayObj);
1282
1283 /* create vertex array buffer */
1284 _mesa_GenBuffersARB(1, &copypix->VBO);
1285 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, copypix->VBO);
1286 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
1287 NULL, GL_DYNAMIC_DRAW_ARB);
1288
1289 /* setup vertex arrays */
1290 _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
1291 _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
1292 _mesa_EnableClientState(GL_VERTEX_ARRAY);
1293 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
1294 }
1295 else {
1296 _mesa_BindVertexArray(copypix->ArrayObj);
1297 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, copypix->VBO);
1298 }
1299
1300 newTex = alloc_texture(tex, width, height, intFormat);
1301
1302 /* vertex positions, texcoords (after texture allocation!) */
1303 {
1304 const GLfloat dstX0 = (GLfloat) dstX;
1305 const GLfloat dstY0 = (GLfloat) dstY;
1306 const GLfloat dstX1 = dstX + width * ctx->Pixel.ZoomX;
1307 const GLfloat dstY1 = dstY + height * ctx->Pixel.ZoomY;
1308 const GLfloat z = ctx->Current.RasterPos[2];
1309
1310 verts[0].x = dstX0;
1311 verts[0].y = dstY0;
1312 verts[0].z = z;
1313 verts[0].s = 0.0F;
1314 verts[0].t = 0.0F;
1315 verts[1].x = dstX1;
1316 verts[1].y = dstY0;
1317 verts[1].z = z;
1318 verts[1].s = tex->Sright;
1319 verts[1].t = 0.0F;
1320 verts[2].x = dstX1;
1321 verts[2].y = dstY1;
1322 verts[2].z = z;
1323 verts[2].s = tex->Sright;
1324 verts[2].t = tex->Ttop;
1325 verts[3].x = dstX0;
1326 verts[3].y = dstY1;
1327 verts[3].z = z;
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 /* Alloc/setup texture */
1336 setup_copypix_texture(tex, newTex, srcX, srcY, width, height,
1337 GL_RGBA, GL_NEAREST);
1338
1339 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
1340
1341 /* draw textured quad */
1342 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1343
1344 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
1345
1346 _mesa_meta_end(ctx);
1347 }
1348
1349
1350
1351 /**
1352 * When the glDrawPixels() image size is greater than the max rectangle
1353 * texture size we use this function to break the glDrawPixels() image
1354 * into tiles which fit into the max texture size.
1355 */
1356 static void
1357 tiled_draw_pixels(GLcontext *ctx,
1358 GLint tileSize,
1359 GLint x, GLint y, GLsizei width, GLsizei height,
1360 GLenum format, GLenum type,
1361 const struct gl_pixelstore_attrib *unpack,
1362 const GLvoid *pixels)
1363 {
1364 struct gl_pixelstore_attrib tileUnpack = *unpack;
1365 GLint i, j;
1366
1367 if (tileUnpack.RowLength == 0)
1368 tileUnpack.RowLength = width;
1369
1370 for (i = 0; i < width; i += tileSize) {
1371 const GLint tileWidth = MIN2(tileSize, width - i);
1372 const GLint tileX = (GLint) (x + i * ctx->Pixel.ZoomX);
1373
1374 tileUnpack.SkipPixels = unpack->SkipPixels + i;
1375
1376 for (j = 0; j < height; j += tileSize) {
1377 const GLint tileHeight = MIN2(tileSize, height - j);
1378 const GLint tileY = (GLint) (y + j * ctx->Pixel.ZoomY);
1379
1380 tileUnpack.SkipRows = unpack->SkipRows + j;
1381
1382 _mesa_meta_draw_pixels(ctx, tileX, tileY,
1383 tileWidth, tileHeight,
1384 format, type, &tileUnpack, pixels);
1385 }
1386 }
1387 }
1388
1389
1390 /**
1391 * One-time init for drawing stencil pixels.
1392 */
1393 static void
1394 init_draw_stencil_pixels(GLcontext *ctx)
1395 {
1396 /* This program is run eight times, once for each stencil bit.
1397 * The stencil values to draw are found in an 8-bit alpha texture.
1398 * We read the texture/stencil value and test if bit 'b' is set.
1399 * If the bit is not set, use KIL to kill the fragment.
1400 * Finally, we use the stencil test to update the stencil buffer.
1401 *
1402 * The basic algorithm for checking if a bit is set is:
1403 * if (is_odd(value / (1 << bit)))
1404 * result is one (or non-zero).
1405 * else
1406 * result is zero.
1407 * The program parameter contains three values:
1408 * parm.x = 255 / (1 << bit)
1409 * parm.y = 0.5
1410 * parm.z = 0.0
1411 */
1412 static const char *program =
1413 "!!ARBfp1.0\n"
1414 "PARAM parm = program.local[0]; \n"
1415 "TEMP t; \n"
1416 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1417 "# t = t * 255 / bit \n"
1418 "MUL t.x, t.a, parm.x; \n"
1419 "# t = (int) t \n"
1420 "FRC t.y, t.x; \n"
1421 "SUB t.x, t.x, t.y; \n"
1422 "# t = t * 0.5 \n"
1423 "MUL t.x, t.x, parm.y; \n"
1424 "# t = fract(t.x) \n"
1425 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
1426 "# t.x = (t.x == 0 ? 1 : 0) \n"
1427 "SGE t.x, -t.x, parm.z; \n"
1428 "KIL -t.x; \n"
1429 "# for debug only \n"
1430 "#MOV result.color, t.x; \n"
1431 "END \n";
1432 char program2[1000];
1433 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
1434 struct temp_texture *tex = get_temp_texture(ctx);
1435 const char *texTarget;
1436
1437 assert(drawpix->StencilFP == 0);
1438
1439 /* replace %s with "RECT" or "2D" */
1440 assert(strlen(program) + 4 < sizeof(program2));
1441 if (tex->Target == GL_TEXTURE_RECTANGLE)
1442 texTarget = "RECT";
1443 else
1444 texTarget = "2D";
1445 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
1446
1447 _mesa_GenPrograms(1, &drawpix->StencilFP);
1448 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
1449 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
1450 strlen(program2), (const GLubyte *) program2);
1451 }
1452
1453
1454 /**
1455 * One-time init for drawing depth pixels.
1456 */
1457 static void
1458 init_draw_depth_pixels(GLcontext *ctx)
1459 {
1460 static const char *program =
1461 "!!ARBfp1.0\n"
1462 "PARAM color = program.local[0]; \n"
1463 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1464 "MOV result.color, color; \n"
1465 "END \n";
1466 char program2[200];
1467 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
1468 struct temp_texture *tex = get_temp_texture(ctx);
1469 const char *texTarget;
1470
1471 assert(drawpix->DepthFP == 0);
1472
1473 /* replace %s with "RECT" or "2D" */
1474 assert(strlen(program) + 4 < sizeof(program2));
1475 if (tex->Target == GL_TEXTURE_RECTANGLE)
1476 texTarget = "RECT";
1477 else
1478 texTarget = "2D";
1479 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
1480
1481 _mesa_GenPrograms(1, &drawpix->DepthFP);
1482 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
1483 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
1484 strlen(program2), (const GLubyte *) program2);
1485 }
1486
1487
1488 /**
1489 * Meta implementation of ctx->Driver.DrawPixels() in terms
1490 * of texture mapping and polygon rendering.
1491 */
1492 void
1493 _mesa_meta_draw_pixels(GLcontext *ctx,
1494 GLint x, GLint y, GLsizei width, GLsizei height,
1495 GLenum format, GLenum type,
1496 const struct gl_pixelstore_attrib *unpack,
1497 const GLvoid *pixels)
1498 {
1499 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
1500 struct temp_texture *tex = get_temp_texture(ctx);
1501 const struct gl_pixelstore_attrib unpackSave = ctx->Unpack;
1502 const GLuint origStencilMask = ctx->Stencil.WriteMask[0];
1503 struct vertex {
1504 GLfloat x, y, z, s, t;
1505 };
1506 struct vertex verts[4];
1507 GLenum texIntFormat;
1508 GLboolean fallback, newTex;
1509 GLbitfield metaExtraSave = 0x0;
1510
1511 /*
1512 * Determine if we can do the glDrawPixels with texture mapping.
1513 */
1514 fallback = GL_FALSE;
1515 if (ctx->_ImageTransferState ||
1516 ctx->Fog.Enabled) {
1517 fallback = GL_TRUE;
1518 }
1519
1520 if (_mesa_is_color_format(format)) {
1521 /* use more compact format when possible */
1522 /* XXX disable special case for GL_LUMINANCE for now to work around
1523 * apparent i965 driver bug (see bug #23670).
1524 */
1525 if (/*format == GL_LUMINANCE ||*/ format == GL_LUMINANCE_ALPHA)
1526 texIntFormat = format;
1527 else
1528 texIntFormat = GL_RGBA;
1529 }
1530 else if (_mesa_is_stencil_format(format)) {
1531 if (ctx->Extensions.ARB_fragment_program &&
1532 ctx->Pixel.IndexShift == 0 &&
1533 ctx->Pixel.IndexOffset == 0 &&
1534 type == GL_UNSIGNED_BYTE) {
1535 /* We'll store stencil as alpha. This only works for GLubyte
1536 * image data because of how incoming values are mapped to alpha
1537 * in [0,1].
1538 */
1539 texIntFormat = GL_ALPHA;
1540 metaExtraSave = (META_COLOR_MASK |
1541 META_DEPTH_TEST |
1542 META_SHADER |
1543 META_STENCIL_TEST);
1544 }
1545 else {
1546 fallback = GL_TRUE;
1547 }
1548 }
1549 else if (_mesa_is_depth_format(format)) {
1550 if (ctx->Extensions.ARB_depth_texture &&
1551 ctx->Extensions.ARB_fragment_program) {
1552 texIntFormat = GL_DEPTH_COMPONENT;
1553 metaExtraSave = (META_SHADER);
1554 }
1555 else {
1556 fallback = GL_TRUE;
1557 }
1558 }
1559 else {
1560 fallback = GL_TRUE;
1561 }
1562
1563 if (fallback) {
1564 _swrast_DrawPixels(ctx, x, y, width, height,
1565 format, type, unpack, pixels);
1566 return;
1567 }
1568
1569 /*
1570 * Check image size against max texture size, draw as tiles if needed.
1571 */
1572 if (width > tex->MaxSize || height > tex->MaxSize) {
1573 tiled_draw_pixels(ctx, tex->MaxSize, x, y, width, height,
1574 format, type, unpack, pixels);
1575 return;
1576 }
1577
1578 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
1579 * but a there's a few things we need to override:
1580 */
1581 _mesa_meta_begin(ctx, (META_RASTERIZATION |
1582 META_SHADER |
1583 META_TEXTURE |
1584 META_TRANSFORM |
1585 META_VERTEX |
1586 META_VIEWPORT |
1587 metaExtraSave));
1588
1589 if (drawpix->ArrayObj == 0) {
1590 /* one-time setup */
1591
1592 /* create vertex array object */
1593 _mesa_GenVertexArrays(1, &drawpix->ArrayObj);
1594 _mesa_BindVertexArray(drawpix->ArrayObj);
1595
1596 /* create vertex array buffer */
1597 _mesa_GenBuffersARB(1, &drawpix->VBO);
1598 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, drawpix->VBO);
1599 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
1600 NULL, GL_DYNAMIC_DRAW_ARB);
1601
1602 /* setup vertex arrays */
1603 _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
1604 _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
1605 _mesa_EnableClientState(GL_VERTEX_ARRAY);
1606 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
1607 }
1608 else {
1609 _mesa_BindVertexArray(drawpix->ArrayObj);
1610 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, drawpix->VBO);
1611 }
1612
1613 newTex = alloc_texture(tex, width, height, texIntFormat);
1614
1615 /* vertex positions, texcoords (after texture allocation!) */
1616 {
1617 const GLfloat x0 = (GLfloat) x;
1618 const GLfloat y0 = (GLfloat) y;
1619 const GLfloat x1 = x + width * ctx->Pixel.ZoomX;
1620 const GLfloat y1 = y + height * ctx->Pixel.ZoomY;
1621 const GLfloat z = ctx->Current.RasterPos[2];
1622
1623 verts[0].x = x0;
1624 verts[0].y = y0;
1625 verts[0].z = z;
1626 verts[0].s = 0.0F;
1627 verts[0].t = 0.0F;
1628 verts[1].x = x1;
1629 verts[1].y = y0;
1630 verts[1].z = z;
1631 verts[1].s = tex->Sright;
1632 verts[1].t = 0.0F;
1633 verts[2].x = x1;
1634 verts[2].y = y1;
1635 verts[2].z = z;
1636 verts[2].s = tex->Sright;
1637 verts[2].t = tex->Ttop;
1638 verts[3].x = x0;
1639 verts[3].y = y1;
1640 verts[3].z = z;
1641 verts[3].s = 0.0F;
1642 verts[3].t = tex->Ttop;
1643
1644 /* upload new vertex data */
1645 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1646 }
1647
1648 /* set given unpack params */
1649 ctx->Unpack = *unpack;
1650
1651 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
1652
1653 if (_mesa_is_stencil_format(format)) {
1654 /* Drawing stencil */
1655 GLint bit;
1656
1657 if (!drawpix->StencilFP)
1658 init_draw_stencil_pixels(ctx);
1659
1660 setup_drawpix_texture(tex, newTex, texIntFormat, width, height,
1661 GL_ALPHA, type, pixels);
1662
1663 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1664
1665 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
1666
1667 /* set all stencil bits to 0 */
1668 _mesa_StencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
1669 _mesa_StencilFunc(GL_ALWAYS, 0, 255);
1670 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1671
1672 /* set stencil bits to 1 where needed */
1673 _mesa_StencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
1674
1675 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
1676 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
1677
1678 for (bit = 0; bit < ctx->Visual.stencilBits; bit++) {
1679 const GLuint mask = 1 << bit;
1680 if (mask & origStencilMask) {
1681 _mesa_StencilFunc(GL_ALWAYS, mask, mask);
1682 _mesa_StencilMask(mask);
1683
1684 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0,
1685 255.0 / mask, 0.5, 0.0, 0.0);
1686
1687 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1688 }
1689 }
1690 }
1691 else if (_mesa_is_depth_format(format)) {
1692 /* Drawing depth */
1693 if (!drawpix->DepthFP)
1694 init_draw_depth_pixels(ctx);
1695
1696 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
1697 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
1698
1699 /* polygon color = current raster color */
1700 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0,
1701 ctx->Current.RasterColor);
1702
1703 setup_drawpix_texture(tex, newTex, texIntFormat, width, height,
1704 format, type, pixels);
1705
1706 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1707 }
1708 else {
1709 /* Drawing RGBA */
1710 setup_drawpix_texture(tex, newTex, texIntFormat, width, height,
1711 format, type, pixels);
1712 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1713 }
1714
1715 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
1716
1717 /* restore unpack params */
1718 ctx->Unpack = unpackSave;
1719
1720 _mesa_meta_end(ctx);
1721 }
1722
1723
1724 /**
1725 * Do glBitmap with a alpha texture quad. Use the alpha test to
1726 * cull the 'off' bits. If alpha test is already enabled, fall back
1727 * to swrast (should be a rare case).
1728 * A bitmap cache as in the gallium/mesa state tracker would
1729 * improve performance a lot.
1730 */
1731 void
1732 _mesa_meta_bitmap(GLcontext *ctx,
1733 GLint x, GLint y, GLsizei width, GLsizei height,
1734 const struct gl_pixelstore_attrib *unpack,
1735 const GLubyte *bitmap1)
1736 {
1737 struct bitmap_state *bitmap = &ctx->Meta->Bitmap;
1738 struct temp_texture *tex = get_bitmap_temp_texture(ctx);
1739 const GLenum texIntFormat = GL_ALPHA;
1740 const struct gl_pixelstore_attrib unpackSave = *unpack;
1741 struct vertex {
1742 GLfloat x, y, z, s, t, r, g, b, a;
1743 };
1744 struct vertex verts[4];
1745 GLboolean newTex;
1746 GLubyte *bitmap8;
1747
1748 /*
1749 * Check if swrast fallback is needed.
1750 */
1751 if (ctx->_ImageTransferState ||
1752 ctx->Color.AlphaEnabled ||
1753 ctx->Fog.Enabled ||
1754 ctx->Texture._EnabledUnits ||
1755 width > tex->MaxSize ||
1756 height > tex->MaxSize) {
1757 _swrast_Bitmap(ctx, x, y, width, height, unpack, bitmap1);
1758 return;
1759 }
1760
1761 /* Most GL state applies to glBitmap (like blending, stencil, etc),
1762 * but a there's a few things we need to override:
1763 */
1764 _mesa_meta_begin(ctx, (META_ALPHA_TEST |
1765 META_PIXEL_STORE |
1766 META_RASTERIZATION |
1767 META_SHADER |
1768 META_TEXTURE |
1769 META_TRANSFORM |
1770 META_VERTEX |
1771 META_VIEWPORT));
1772
1773 if (bitmap->ArrayObj == 0) {
1774 /* one-time setup */
1775
1776 /* create vertex array object */
1777 _mesa_GenVertexArraysAPPLE(1, &bitmap->ArrayObj);
1778 _mesa_BindVertexArrayAPPLE(bitmap->ArrayObj);
1779
1780 /* create vertex array buffer */
1781 _mesa_GenBuffersARB(1, &bitmap->VBO);
1782 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, bitmap->VBO);
1783 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
1784 NULL, GL_DYNAMIC_DRAW_ARB);
1785
1786 /* setup vertex arrays */
1787 _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
1788 _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
1789 _mesa_ColorPointer(4, GL_FLOAT, sizeof(struct vertex), OFFSET(r));
1790 _mesa_EnableClientState(GL_VERTEX_ARRAY);
1791 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
1792 _mesa_EnableClientState(GL_COLOR_ARRAY);
1793 }
1794 else {
1795 _mesa_BindVertexArray(bitmap->ArrayObj);
1796 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, bitmap->VBO);
1797 }
1798
1799 newTex = alloc_texture(tex, width, height, texIntFormat);
1800
1801 /* vertex positions, texcoords, colors (after texture allocation!) */
1802 {
1803 const GLfloat x0 = (GLfloat) x;
1804 const GLfloat y0 = (GLfloat) y;
1805 const GLfloat x1 = (GLfloat) (x + width);
1806 const GLfloat y1 = (GLfloat) (y + height);
1807 const GLfloat z = ctx->Current.RasterPos[2];
1808 GLuint i;
1809
1810 verts[0].x = x0;
1811 verts[0].y = y0;
1812 verts[0].z = z;
1813 verts[0].s = 0.0F;
1814 verts[0].t = 0.0F;
1815 verts[1].x = x1;
1816 verts[1].y = y0;
1817 verts[1].z = z;
1818 verts[1].s = tex->Sright;
1819 verts[1].t = 0.0F;
1820 verts[2].x = x1;
1821 verts[2].y = y1;
1822 verts[2].z = z;
1823 verts[2].s = tex->Sright;
1824 verts[2].t = tex->Ttop;
1825 verts[3].x = x0;
1826 verts[3].y = y1;
1827 verts[3].z = z;
1828 verts[3].s = 0.0F;
1829 verts[3].t = tex->Ttop;
1830
1831 for (i = 0; i < 4; i++) {
1832 verts[i].r = ctx->Current.RasterColor[0];
1833 verts[i].g = ctx->Current.RasterColor[1];
1834 verts[i].b = ctx->Current.RasterColor[2];
1835 verts[i].a = ctx->Current.RasterColor[3];
1836 }
1837
1838 /* upload new vertex data */
1839 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1840 }
1841
1842 bitmap1 = _mesa_map_pbo_source(ctx, &unpackSave, bitmap1);
1843 if (!bitmap1)
1844 return;
1845
1846 bitmap8 = (GLubyte *) _mesa_calloc(width * height);
1847 if (bitmap8) {
1848 _mesa_expand_bitmap(width, height, &unpackSave, bitmap1,
1849 bitmap8, width, 0xff);
1850
1851 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
1852
1853 _mesa_set_enable(ctx, GL_ALPHA_TEST, GL_TRUE);
1854 _mesa_AlphaFunc(GL_GREATER, 0.0);
1855
1856 setup_drawpix_texture(tex, newTex, texIntFormat, width, height,
1857 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap8);
1858
1859 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1860
1861 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
1862
1863 _mesa_free(bitmap8);
1864 }
1865
1866 _mesa_unmap_pbo_source(ctx, &unpackSave);
1867
1868 _mesa_meta_end(ctx);
1869 }
1870
1871
1872 void
1873 _mesa_meta_generate_mipmap(GLcontext *ctx, GLenum target,
1874 struct gl_texture_object *texObj)
1875 {
1876 struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap;
1877 struct vertex {
1878 GLfloat x, y, s, t, r;
1879 };
1880 struct vertex verts[4];
1881 const GLuint baseLevel = texObj->BaseLevel;
1882 const GLuint maxLevel = texObj->MaxLevel;
1883 const GLenum minFilterSave = texObj->MinFilter;
1884 const GLenum magFilterSave = texObj->MagFilter;
1885 const GLuint fboSave = ctx->DrawBuffer->Name;
1886 GLenum faceTarget;
1887 GLuint level;
1888 GLuint border = 0;
1889
1890 /* check for fallbacks */
1891 if (!ctx->Extensions.EXT_framebuffer_object) {
1892 _mesa_generate_mipmap(ctx, target, texObj);
1893 return;
1894 }
1895
1896 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
1897 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) {
1898 faceTarget = target;
1899 target = GL_TEXTURE_CUBE_MAP;
1900 }
1901 else {
1902 faceTarget = target;
1903 }
1904
1905 _mesa_meta_begin(ctx, META_ALL);
1906
1907 if (mipmap->ArrayObj == 0) {
1908 /* one-time setup */
1909
1910 /* create vertex array object */
1911 _mesa_GenVertexArraysAPPLE(1, &mipmap->ArrayObj);
1912 _mesa_BindVertexArrayAPPLE(mipmap->ArrayObj);
1913
1914 /* create vertex array buffer */
1915 _mesa_GenBuffersARB(1, &mipmap->VBO);
1916 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
1917 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
1918 NULL, GL_DYNAMIC_DRAW_ARB);
1919
1920 /* setup vertex arrays */
1921 _mesa_VertexPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
1922 _mesa_TexCoordPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
1923 _mesa_EnableClientState(GL_VERTEX_ARRAY);
1924 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
1925 }
1926 else {
1927 _mesa_BindVertexArray(mipmap->ArrayObj);
1928 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
1929 }
1930
1931 if (!mipmap->FBO) {
1932 /* Bind the new renderbuffer to the color attachment point. */
1933 _mesa_GenFramebuffersEXT(1, &mipmap->FBO);
1934 }
1935
1936 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, mipmap->FBO);
1937
1938 _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1939 _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1940 _mesa_set_enable(ctx, target, GL_TRUE);
1941
1942 /* setup texcoords once (XXX what about border?) */
1943 switch (faceTarget) {
1944 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1945 break;
1946 case GL_TEXTURE_2D:
1947 verts[0].s = 0.0F;
1948 verts[0].t = 0.0F;
1949 verts[0].r = 0.0F;
1950 verts[1].s = 1.0F;
1951 verts[1].t = 0.0F;
1952 verts[1].r = 0.0F;
1953 verts[2].s = 1.0F;
1954 verts[2].t = 1.0F;
1955 verts[2].r = 0.0F;
1956 verts[3].s = 0.0F;
1957 verts[3].t = 1.0F;
1958 verts[3].r = 0.0F;
1959 break;
1960 }
1961
1962
1963 for (level = baseLevel + 1; level <= maxLevel; level++) {
1964 const struct gl_texture_image *srcImage;
1965 const GLuint srcLevel = level - 1;
1966 GLsizei srcWidth, srcHeight;
1967 GLsizei newWidth, newHeight;
1968 GLenum status;
1969
1970 srcImage = _mesa_select_tex_image(ctx, texObj, target, srcLevel);
1971 assert(srcImage->Border == 0); /* XXX we can fix this */
1972
1973 srcWidth = srcImage->Width - 2 * border;
1974 srcHeight = srcImage->Height - 2 * border;
1975
1976 newWidth = MAX2(1, srcWidth / 2) + 2 * border;
1977 newHeight = MAX2(1, srcHeight / 2) + 2 * border;
1978
1979 if (newWidth == srcImage->Width && newHeight == srcImage->Height) {
1980 break;
1981 }
1982
1983 /* Create empty image */
1984 _mesa_TexImage2D(GL_TEXTURE_2D, level, srcImage->InternalFormat,
1985 newWidth, newHeight, border,
1986 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
1987
1988 /* vertex positions */
1989 {
1990 verts[0].x = 0.0F;
1991 verts[0].y = 0.0F;
1992 verts[1].x = (GLfloat) newWidth;
1993 verts[1].y = 0.0F;
1994 verts[2].x = (GLfloat) newWidth;
1995 verts[2].y = (GLfloat) newHeight;
1996 verts[3].x = 0.0F;
1997 verts[3].y = (GLfloat) newHeight;
1998
1999 /* upload new vertex data */
2000 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
2001 }
2002
2003 /* limit sampling to src level */
2004 _mesa_TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, srcLevel);
2005 _mesa_TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, srcLevel);
2006
2007 /* Set to draw into the current level */
2008 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
2009 GL_COLOR_ATTACHMENT0_EXT,
2010 target,
2011 texObj->Name,
2012 level);
2013
2014 /* Choose to render to the color attachment. */
2015 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
2016
2017 status = _mesa_CheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
2018 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2019 abort();
2020 break;
2021 }
2022
2023 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2024 }
2025
2026 _mesa_meta_end(ctx);
2027
2028 _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilterSave);
2029 _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilterSave);
2030
2031 /* restore (XXX add to meta_begin/end()? */
2032 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboSave);
2033 }