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