2 * Mesa 3-D graphics library
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
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.
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"
67 * State which we may save/restore across meta ops.
68 * XXX this may be incomplete...
72 GLbitfield SavedState; /**< bitmask of META_* flags */
74 /** META_ALPHA_TEST */
75 GLboolean AlphaEnabled;
78 GLboolean BlendEnabled;
79 GLboolean ColorLogicOpEnabled;
81 /** META_COLOR_MASK */
84 /** META_DEPTH_TEST */
85 struct gl_depthbuffer_attrib Depth;
90 /** META_PIXEL_STORE */
91 struct gl_pixelstore_attrib Pack, Unpack;
93 /** META_RASTERIZATION */
94 GLenum FrontPolygonMode, BackPolygonMode;
95 GLboolean PolygonOffset;
96 GLboolean PolygonSmooth;
97 GLboolean PolygonStipple;
98 GLboolean PolygonCull;
101 struct gl_scissor_attrib Scissor;
104 GLboolean VertexProgramEnabled;
105 struct gl_vertex_program *VertexProgram;
106 GLboolean FragmentProgramEnabled;
107 struct gl_fragment_program *FragmentProgram;
110 /** META_STENCIL_TEST */
111 struct gl_stencil_attrib Stencil;
113 /** META_TRANSFORM */
115 GLfloat ModelviewMatrix[16];
116 GLfloat ProjectionMatrix[16];
117 GLfloat TextureMatrix[16];
118 GLbitfield ClipPlanesEnabled;
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 */
131 struct gl_array_object *ArrayObj;
132 struct gl_buffer_object *ArrayBufferObj;
135 GLint ViewportX, ViewportY, ViewportW, ViewportH;
136 GLclampd DepthNear, DepthFar;
138 /** Miscellaneous (always disabled) */
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.
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 */
157 GLfloat Sright, Ttop; /**< right, top texcoords */
162 * State for glBlitFramebufer()
173 * State for glClear()
183 * State for glCopyPixels()
193 * State for glDrawPixels()
200 GLuint StencilFP; /**< Fragment program for drawing stencil images */
201 GLuint DepthFP; /**< Fragment program for drawing depth images */
206 * State for glBitmap()
212 struct temp_texture Tex; /**< separate texture from other meta ops */
217 * State for _mesa_meta_generate_mipmap()
219 struct gen_mipmap_state
228 * All per-context meta state.
232 struct save_state Save; /**< state saved during meta-ops */
234 struct temp_texture TempTex;
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() */
246 * Initialize meta-ops for a context.
247 * To be called once during context creation.
250 _mesa_meta_init(GLcontext *ctx)
254 ctx->Meta = CALLOC_STRUCT(gl_meta_state);
259 * Free context meta-op state.
260 * To be called once during context destruction.
263 _mesa_meta_free(GLcontext *ctx)
265 struct gl_meta_state *meta = ctx->Meta;
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().
272 /* the temporary texture */
273 _mesa_DeleteTextures(1, &meta->TempTex.TexObj);
275 /* glBlitFramebuffer */
276 _mesa_DeleteBuffersARB(1, & meta->Blit.VBO);
277 _mesa_DeleteVertexArraysAPPLE(1, &meta->Blit.ArrayObj);
278 _mesa_DeletePrograms(1, &meta->Blit.DepthFP);
281 _mesa_DeleteBuffersARB(1, & meta->Clear.VBO);
282 _mesa_DeleteVertexArraysAPPLE(1, &meta->Clear.ArrayObj);
285 _mesa_DeleteBuffersARB(1, & meta->CopyPix.VBO);
286 _mesa_DeleteVertexArraysAPPLE(1, &meta->CopyPix.ArrayObj);
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);
295 _mesa_DeleteBuffersARB(1, & meta->Bitmap.VBO);
296 _mesa_DeleteVertexArraysAPPLE(1, &meta->Bitmap.ArrayObj);
297 _mesa_DeleteTextures(1, &meta->Bitmap.Tex.TexObj);
300 _mesa_free(ctx->Meta);
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.
309 * \param state bitmask of META_* flags indicating which attribute groups
310 * to save and reset to their defaults
313 _mesa_meta_begin(GLcontext *ctx, GLbitfield state)
315 struct save_state *save = &ctx->Meta->Save;
317 save->SavedState = state;
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);
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);
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);
343 if (state & META_DEPTH_TEST) {
344 save->Depth = ctx->Depth; /* struct copy */
346 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_FALSE);
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);
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;
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);
376 if (state & META_SCISSOR) {
377 save->Scissor = ctx->Scissor; /* struct copy */
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);
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);
393 if (ctx->Extensions.ARB_shader_objects) {
394 save->Shader = ctx->Shader.CurrentProgram ?
395 ctx->Shader.CurrentProgram->Name : 0;
396 _mesa_UseProgramObjectARB(0);
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 */
407 if (state & META_TEXTURE) {
410 save->ActiveUnit = ctx->Texture.CurrentUnit;
411 save->ClientActiveUnit = ctx->Array.ActiveTexture;
412 save->EnvMode = ctx->Texture.Unit[0].EnvMode;
414 if (ctx->Texture._EnabledUnits |
415 ctx->Texture._EnabledCoordUnits |
416 ctx->Texture._TexGenEnabled |
417 ctx->Texture._TexMatEnabled) {
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);
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]);
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);
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,
472 save->ClipPlanesEnabled = ctx->Transform.ClipPlanesEnabled;
473 if (ctx->Transform.ClipPlanesEnabled) {
475 for (i = 0; i < ctx->Const.MaxClipPlanes; i++) {
476 _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_FALSE);
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? */
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);
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);
513 save->Lighting = ctx->Light.Enabled;
514 if (ctx->Light.Enabled)
515 _mesa_set_enable(ctx, GL_LIGHTING, GL_FALSE);
521 * Leave meta state. This is like a light-weight version of glPopAttrib().
524 _mesa_meta_end(GLcontext *ctx)
526 struct save_state *save = &ctx->Meta->Save;
527 const GLbitfield state = save->SavedState;
529 if (state & META_ALPHA_TEST) {
530 if (ctx->Color.AlphaEnabled != save->AlphaEnabled)
531 _mesa_set_enable(ctx, GL_ALPHA_TEST, save->AlphaEnabled);
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);
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]);
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);
554 if (state & META_FOG) {
555 _mesa_set_enable(ctx, GL_FOG, save->Fog);
558 if (state & META_PIXEL_STORE) {
559 ctx->Pack = save->Pack;
560 ctx->Unpack = save->Unpack;
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);
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);
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);
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);
593 if (ctx->Extensions.ARB_shader_objects) {
594 _mesa_UseProgramObjectARB(save->Shader);
598 if (state & META_STENCIL_TEST) {
599 const struct gl_stencil_attrib *stencil = &save->Stencil;
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);
610 _mesa_StencilFuncSeparate(GL_FRONT,
611 stencil->Function[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]);
619 _mesa_StencilFuncSeparate(GL_BACK,
620 stencil->Function[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]);
629 if (state & META_TEXTURE) {
632 ASSERT(ctx->Texture.CurrentUnit == 0);
634 /* restore texenv for unit[0] */
635 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, save->EnvMode);
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]);
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);
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);
660 if (save->TexGenEnabled[u]) {
661 _mesa_ActiveTextureARB(GL_TEXTURE0 + u);
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);
674 /* restore current unit state */
675 _mesa_ActiveTextureARB(GL_TEXTURE0 + save->ActiveUnit);
676 _mesa_ClientActiveTextureARB(GL_TEXTURE0 + save->ClientActiveUnit);
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);
686 _mesa_MatrixMode(GL_MODELVIEW);
687 _mesa_LoadMatrixf(save->ModelviewMatrix);
689 _mesa_MatrixMode(GL_PROJECTION);
690 _mesa_LoadMatrixf(save->ProjectionMatrix);
692 _mesa_MatrixMode(save->MatrixMode);
694 save->ClipPlanesEnabled = ctx->Transform.ClipPlanesEnabled;
695 if (save->ClipPlanesEnabled) {
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);
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);
710 /* restore vertex array object */
711 _mesa_BindVertexArray(save->ArrayObj->Name);
712 _mesa_reference_array_object(ctx, &save->ArrayObj, NULL);
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);
723 _mesa_DepthRange(save->DepthNear, save->DepthFar);
727 if (save->Lighting) {
728 _mesa_set_enable(ctx, GL_LIGHTING, GL_TRUE);
731 _mesa_set_enable(ctx, GL_FOG, GL_TRUE);
737 * One-time init for a temp_texture object.
738 * Choose tex target, compute max tex size, etc.
741 init_temp_texture(GLcontext *ctx, struct temp_texture *tex)
743 /* prefer texture rectangle */
744 if (ctx->Extensions.NV_texture_rectangle) {
745 tex->Target = GL_TEXTURE_RECTANGLE;
746 tex->MaxSize = ctx->Const.MaxTextureRectSize;
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;
755 tex->MinSize = 16; /* 16 x 16 at least */
756 assert(tex->MaxSize > 0);
758 _mesa_GenTextures(1, &tex->TexObj);
759 _mesa_BindTexture(tex->Target, tex->TexObj);
764 * Return pointer to temp_texture info for non-bitmap ops.
765 * This does some one-time init if needed.
767 static struct temp_texture *
768 get_temp_texture(GLcontext *ctx)
770 struct temp_texture *tex = &ctx->Meta->TempTex;
773 init_temp_texture(ctx, tex);
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.
785 static struct temp_texture *
786 get_bitmap_temp_texture(GLcontext *ctx)
788 struct temp_texture *tex = &ctx->Meta->Bitmap.Tex;
791 init_temp_texture(ctx, tex);
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.
805 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
808 alloc_texture(struct temp_texture *tex,
809 GLsizei width, GLsizei height, GLenum intFormat)
811 GLboolean newTex = GL_FALSE;
813 ASSERT(width <= tex->MaxSize);
814 ASSERT(height <= tex->MaxSize);
816 if (width > tex->Width ||
817 height > tex->Height ||
818 intFormat != tex->IntFormat) {
819 /* alloc new texture (larger or different format) */
822 /* use non-power of two size */
823 tex->Width = MAX2(tex->MinSize, width);
824 tex->Height = MAX2(tex->MinSize, height);
827 /* find power of two size */
829 w = h = tex->MinSize;
838 tex->IntFormat = intFormat;
843 /* compute texcoords */
844 if (tex->Target == GL_TEXTURE_RECTANGLE) {
845 tex->Sright = (GLfloat) width;
846 tex->Ttop = (GLfloat) height;
849 tex->Sright = (GLfloat) width / tex->Width;
850 tex->Ttop = (GLfloat) height / tex->Height;
858 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
861 setup_copypix_texture(struct temp_texture *tex,
863 GLint srcX, GLint srcY,
864 GLsizei width, GLsizei height, GLenum intFormat,
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);
872 /* copy framebuffer image to texture */
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);
881 /* create empty texture */
882 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
883 tex->Width, tex->Height, 0,
884 intFormat, GL_UNSIGNED_BYTE, NULL);
886 _mesa_CopyTexSubImage2D(tex->Target, 0,
887 0, 0, srcX, srcY, width, height);
891 /* replace existing tex image */
892 _mesa_CopyTexSubImage2D(tex->Target, 0,
893 0, 0, srcX, srcY, width, height);
899 * Setup/load texture for glDrawPixels.
902 setup_drawpix_texture(struct temp_texture *tex,
905 GLsizei width, GLsizei height,
906 GLenum format, GLenum type,
907 const GLvoid *pixels)
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);
914 /* copy pixel data to texture */
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);
923 /* create empty texture */
924 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
925 tex->Width, tex->Height, 0, format, type, NULL);
927 _mesa_TexSubImage2D(tex->Target, 0,
928 0, 0, width, height, format, type, pixels);
932 /* replace existing tex image */
933 _mesa_TexSubImage2D(tex->Target, 0,
934 0, 0, width, height, format, type, pixels);
941 * One-time init for drawing depth pixels.
944 init_blit_depth_pixels(GLcontext *ctx)
946 static const char *program =
948 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
951 struct blit_state *blit = &ctx->Meta->Blit;
952 struct temp_texture *tex = get_temp_texture(ctx);
953 const char *texTarget;
955 assert(blit->DepthFP == 0);
957 /* replace %s with "RECT" or "2D" */
958 assert(strlen(program) + 4 < sizeof(program2));
959 if (tex->Target == GL_TEXTURE_RECTANGLE)
963 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
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);
973 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
974 * of texture mapping and polygon rendering.
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)
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 */
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);
1013 /* only scissor effects blit so save/clear all other relevant state */
1014 _mesa_meta_begin(ctx, ~META_SCISSOR);
1016 if (blit->ArrayObj == 0) {
1017 /* one-time setup */
1019 /* create vertex array object */
1020 _mesa_GenVertexArrays(1, &blit->ArrayObj);
1021 _mesa_BindVertexArray(blit->ArrayObj);
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);
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);
1038 _mesa_BindVertexArray(blit->ArrayObj);
1039 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, blit->VBO);
1042 newTex = alloc_texture(tex, srcW, srcH, GL_RGBA);
1044 /* vertex positions/texcoords (after texture allocation!) */
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;
1057 verts[1][2] = tex->Sright;
1059 verts[2][2] = tex->Sright;
1060 verts[2][3] = tex->Ttop;
1062 verts[3][3] = tex->Ttop;
1064 /* upload new vertex data */
1065 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1068 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
1070 if (mask & GL_COLOR_BUFFER_BIT) {
1071 setup_copypix_texture(tex, newTex, srcX, srcY, srcW, srcH,
1073 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1074 mask &= ~GL_COLOR_BUFFER_BIT;
1077 if (mask & GL_DEPTH_BUFFER_BIT) {
1078 GLuint *tmp = (GLuint *) _mesa_malloc(srcW * srcH * sizeof(GLuint));
1081 init_blit_depth_pixels(ctx);
1083 /* maybe change tex format here */
1084 newTex = alloc_texture(tex, srcW, srcH, GL_DEPTH_COMPONENT);
1086 _mesa_ReadPixels(srcX, srcY, srcW, srcH,
1087 GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, tmp);
1089 setup_drawpix_texture(tex, newTex, GL_DEPTH_COMPONENT, srcW, srcH,
1090 GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, tmp);
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);
1099 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1100 mask &= ~GL_DEPTH_BUFFER_BIT;
1106 if (mask & GL_STENCIL_BUFFER_BIT) {
1107 /* XXX can't easily do stencil */
1110 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
1112 _mesa_meta_end(ctx);
1115 _swrast_BlitFramebuffer(ctx, srcX0, srcY0, srcX1, srcY1,
1116 dstX0, dstY0, dstX1, dstY1, mask, filter);
1122 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1125 _mesa_meta_clear(GLcontext *ctx, GLbitfield buffers)
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;
1132 if (buffers & BUFFER_BITS_COLOR) {
1133 /* if clearing color buffers, don't save/restore colormask */
1134 metaSave -= META_COLOR_MASK;
1137 _mesa_meta_begin(ctx, metaSave);
1139 if (clear->ArrayObj == 0) {
1140 /* one-time setup */
1142 /* create vertex array object */
1143 _mesa_GenVertexArrays(1, &clear->ArrayObj);
1144 _mesa_BindVertexArray(clear->ArrayObj);
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);
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);
1161 _mesa_BindVertexArray(clear->ArrayObj);
1162 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, clear->VBO);
1165 /* GL_COLOR_BUFFER_BIT */
1166 if (buffers & BUFFER_BITS_COLOR) {
1167 /* leave colormask, glDrawBuffer state as-is */
1170 ASSERT(metaSave & META_COLOR_MASK);
1171 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
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);
1181 assert(!ctx->Depth.Test);
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]);
1194 assert(!ctx->Stencil.Enabled);
1197 /* vertex positions/colors */
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;
1220 for (i = 0; i < 4; i++) {
1221 COPY_4FV(&verts[i][3], ctx->Color.ClearColor);
1224 /* upload new vertex data */
1225 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1229 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1231 _mesa_meta_end(ctx);
1236 * Meta implementation of ctx->Driver.CopyPixels() in terms
1237 * of texture mapping and polygon rendering.
1240 _mesa_meta_copy_pixels(GLcontext *ctx, GLint srcX, GLint srcY,
1241 GLsizei width, GLsizei height,
1242 GLint dstX, GLint dstY, GLenum type)
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 */
1248 GLenum intFormat = GL_RGBA;
1250 if (type != GL_COLOR ||
1251 ctx->_ImageTransferState ||
1253 width > tex->MaxSize ||
1254 height > tex->MaxSize) {
1255 /* XXX avoid this fallback */
1256 _swrast_CopyPixels(ctx, srcX, srcY, width, height, dstX, dstY, type);
1260 /* Most GL state applies to glCopyPixels, but a there's a few things
1261 * we need to override:
1263 _mesa_meta_begin(ctx, (META_RASTERIZATION |
1270 if (copypix->ArrayObj == 0) {
1271 /* one-time setup */
1273 /* create vertex array object */
1274 _mesa_GenVertexArrays(1, ©pix->ArrayObj);
1275 _mesa_BindVertexArray(copypix->ArrayObj);
1277 /* create vertex array buffer */
1278 _mesa_GenBuffersARB(1, ©pix->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);
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);
1292 _mesa_BindVertexArray(copypix->ArrayObj);
1293 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, copypix->VBO);
1296 newTex = alloc_texture(tex, width, height, intFormat);
1298 /* vertex positions, texcoords (after texture allocation!) */
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];
1306 verts[0][0] = dstX0;
1307 verts[0][1] = dstY0;
1311 verts[1][0] = dstX1;
1312 verts[1][1] = dstY0;
1314 verts[1][3] = tex->Sright;
1316 verts[2][0] = dstX1;
1317 verts[2][1] = dstY1;
1319 verts[2][3] = tex->Sright;
1320 verts[2][4] = tex->Ttop;
1321 verts[3][0] = dstX0;
1322 verts[3][1] = dstY1;
1325 verts[3][4] = tex->Ttop;
1327 /* upload new vertex data */
1328 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1331 /* Alloc/setup texture */
1332 setup_copypix_texture(tex, newTex, srcX, srcY, width, height,
1333 GL_RGBA, GL_NEAREST);
1335 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
1337 /* draw textured quad */
1338 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1340 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
1342 _mesa_meta_end(ctx);
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.
1353 tiled_draw_pixels(GLcontext *ctx,
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)
1360 struct gl_pixelstore_attrib tileUnpack = *unpack;
1363 if (tileUnpack.RowLength == 0)
1364 tileUnpack.RowLength = width;
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);
1370 tileUnpack.SkipPixels = unpack->SkipPixels + i;
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);
1376 tileUnpack.SkipRows = unpack->SkipRows + j;
1378 _mesa_meta_draw_pixels(ctx, tileX, tileY,
1379 tileWidth, tileHeight,
1380 format, type, &tileUnpack, pixels);
1387 * One-time init for drawing stencil pixels.
1390 init_draw_stencil_pixels(GLcontext *ctx)
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.
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).
1403 * The program parameter contains three values:
1404 * parm.x = 255 / (1 << bit)
1408 static const char *program =
1410 "PARAM parm = program.local[0]; \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"
1417 "SUB t.x, t.x, t.y; \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"
1425 "# for debug only \n"
1426 "#MOV result.color, t.x; \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;
1433 assert(drawpix->StencilFP == 0);
1435 /* replace %s with "RECT" or "2D" */
1436 assert(strlen(program) + 4 < sizeof(program2));
1437 if (tex->Target == GL_TEXTURE_RECTANGLE)
1441 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
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);
1451 * One-time init for drawing depth pixels.
1454 init_draw_depth_pixels(GLcontext *ctx)
1456 static const char *program =
1458 "PARAM color = program.local[0]; \n"
1459 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1460 "MOV result.color, color; \n"
1463 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
1464 struct temp_texture *tex = get_temp_texture(ctx);
1465 const char *texTarget;
1467 assert(drawpix->DepthFP == 0);
1469 /* replace %s with "RECT" or "2D" */
1470 assert(strlen(program) + 4 < sizeof(program2));
1471 if (tex->Target == GL_TEXTURE_RECTANGLE)
1475 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
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);
1485 * Meta implementation of ctx->Driver.DrawPixels() in terms
1486 * of texture mapping and polygon rendering.
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)
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;
1505 * Determine if we can do the glDrawPixels with texture mapping.
1507 fallback = GL_FALSE;
1508 if (ctx->_ImageTransferState ||
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).
1518 if (/*format == GL_LUMINANCE ||*/ format == GL_LUMINANCE_ALPHA)
1519 texIntFormat = format;
1521 texIntFormat = GL_RGBA;
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
1530 texIntFormat = GL_ALPHA;
1531 metaExtraSave = (META_COLOR_MASK |
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);
1555 _swrast_DrawPixels(ctx, x, y, width, height,
1556 format, type, unpack, pixels);
1561 * Check image size against max texture size, draw as tiles if needed.
1563 if (width > tex->MaxSize || height > tex->MaxSize) {
1564 tiled_draw_pixels(ctx, tex->MaxSize, x, y, width, height,
1565 format, type, unpack, pixels);
1569 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
1570 * but a there's a few things we need to override:
1572 _mesa_meta_begin(ctx, (META_RASTERIZATION |
1580 if (drawpix->ArrayObj == 0) {
1581 /* one-time setup */
1583 /* create vertex array object */
1584 _mesa_GenVertexArrays(1, &drawpix->ArrayObj);
1585 _mesa_BindVertexArray(drawpix->ArrayObj);
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);
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);
1602 _mesa_BindVertexArray(drawpix->ArrayObj);
1603 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, drawpix->VBO);
1606 newTex = alloc_texture(tex, width, height, texIntFormat);
1608 /* vertex positions, texcoords (after texture allocation!) */
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];
1624 verts[1][3] = tex->Sright;
1629 verts[2][3] = tex->Sright;
1630 verts[2][4] = tex->Ttop;
1635 verts[3][4] = tex->Ttop;
1637 /* upload new vertex data */
1638 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1641 /* set given unpack params */
1642 ctx->Unpack = *unpack;
1644 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
1646 if (_mesa_is_stencil_format(format)) {
1647 /* Drawing stencil */
1650 if (!drawpix->StencilFP)
1651 init_draw_stencil_pixels(ctx);
1653 setup_drawpix_texture(tex, newTex, texIntFormat, width, height,
1654 GL_ALPHA, type, pixels);
1656 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1658 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
1659 _mesa_StencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
1661 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
1662 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
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);
1670 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0,
1671 255.0 / mask, 0.5, 0.0, 0.0);
1673 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1677 else if (_mesa_is_depth_format(format)) {
1679 if (!drawpix->DepthFP)
1680 init_draw_depth_pixels(ctx);
1682 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
1683 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
1685 /* polygon color = current raster color */
1686 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0,
1687 ctx->Current.RasterColor);
1689 setup_drawpix_texture(tex, newTex, texIntFormat, width, height,
1690 format, type, pixels);
1692 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1696 setup_drawpix_texture(tex, newTex, texIntFormat, width, height,
1697 format, type, pixels);
1698 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1701 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
1703 /* restore unpack params */
1704 ctx->Unpack = unpackSave;
1706 _mesa_meta_end(ctx);
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.
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)
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 */
1732 * Check if swrast fallback is needed.
1734 if (ctx->_ImageTransferState ||
1735 ctx->Color.AlphaEnabled ||
1737 ctx->Texture._EnabledUnits ||
1738 width > tex->MaxSize ||
1739 height > tex->MaxSize) {
1740 _swrast_Bitmap(ctx, x, y, width, height, unpack, bitmap1);
1744 /* Most GL state applies to glBitmap (like blending, stencil, etc),
1745 * but a there's a few things we need to override:
1747 _mesa_meta_begin(ctx, (META_ALPHA_TEST |
1749 META_RASTERIZATION |
1756 if (bitmap->ArrayObj == 0) {
1757 /* one-time setup */
1759 /* create vertex array object */
1760 _mesa_GenVertexArraysAPPLE(1, &bitmap->ArrayObj);
1761 _mesa_BindVertexArrayAPPLE(bitmap->ArrayObj);
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);
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)));
1777 _mesa_EnableClientState(GL_VERTEX_ARRAY);
1778 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
1779 _mesa_EnableClientState(GL_COLOR_ARRAY);
1782 _mesa_BindVertexArray(bitmap->ArrayObj);
1783 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, bitmap->VBO);
1786 newTex = alloc_texture(tex, width, height, texIntFormat);
1788 /* vertex positions, texcoords, colors (after texture allocation!) */
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];
1805 verts[1][3] = tex->Sright;
1810 verts[2][3] = tex->Sright;
1811 verts[2][4] = tex->Ttop;
1816 verts[3][4] = tex->Ttop;
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];
1825 /* upload new vertex data */
1826 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1829 bitmap1 = _mesa_map_pbo_source(ctx, &unpackSave, bitmap1);
1833 bitmap8 = (GLubyte *) _mesa_calloc(width * height);
1835 _mesa_expand_bitmap(width, height, &unpackSave, bitmap1,
1836 bitmap8, width, 0xff);
1838 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
1840 _mesa_set_enable(ctx, GL_ALPHA_TEST, GL_TRUE);
1841 _mesa_AlphaFunc(GL_GREATER, 0.0);
1843 setup_drawpix_texture(tex, newTex, texIntFormat, width, height,
1844 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap8);
1846 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1848 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
1850 _mesa_free(bitmap8);
1853 _mesa_unmap_pbo_source(ctx, &unpackSave);
1855 _mesa_meta_end(ctx);
1860 _mesa_meta_generate_mipmap(GLcontext *ctx, GLenum target,
1861 struct gl_texture_object *texObj)
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;
1874 /* check for fallbacks */
1875 if (!ctx->Extensions.EXT_framebuffer_object) {
1876 _mesa_generate_mipmap(ctx, target, texObj);
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;
1886 faceTarget = target;
1889 _mesa_meta_begin(ctx, META_ALL);
1891 if (mipmap->ArrayObj == 0) {
1892 /* one-time setup */
1894 /* create vertex array object */
1895 _mesa_GenVertexArraysAPPLE(1, &mipmap->ArrayObj);
1896 _mesa_BindVertexArrayAPPLE(mipmap->ArrayObj);
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);
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)));
1910 _mesa_EnableClientState(GL_VERTEX_ARRAY);
1911 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
1914 _mesa_BindVertexArray(mipmap->ArrayObj);
1915 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
1919 /* Bind the new renderbuffer to the color attachment point. */
1920 _mesa_GenFramebuffersEXT(1, &mipmap->FBO);
1923 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, mipmap->FBO);
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);
1929 /* setup texcoords once (XXX what about border?) */
1930 switch (faceTarget) {
1931 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
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;
1957 srcImage = _mesa_select_tex_image(ctx, texObj, target, srcLevel);
1958 assert(srcImage->Border == 0); /* XXX we can fix this */
1960 srcWidth = srcImage->Width - 2 * border;
1961 srcHeight = srcImage->Height - 2 * border;
1963 newWidth = MAX2(1, srcWidth / 2) + 2 * border;
1964 newHeight = MAX2(1, srcHeight / 2) + 2 * border;
1966 if (newWidth == srcImage->Width && newHeight == srcImage->Height) {
1970 /* Create empty image */
1971 _mesa_TexImage2D(GL_TEXTURE_2D, level, srcImage->InternalFormat,
1972 newWidth, newHeight, border,
1973 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
1975 /* vertex positions */
1979 verts[1].x = (GLfloat) newWidth;
1981 verts[2].x = (GLfloat) newWidth;
1982 verts[2].y = (GLfloat) newHeight;
1984 verts[3].y = (GLfloat) newHeight;
1986 /* upload new vertex data */
1987 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
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);
1994 /* Set to draw into the current level */
1995 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
1996 GL_COLOR_ATTACHMENT0_EXT,
2001 /* Choose to render to the color attachment. */
2002 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
2004 status = _mesa_CheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
2005 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2010 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2013 _mesa_meta_end(ctx);
2015 _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilterSave);
2016 _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilterSave);
2018 /* restore (XXX add to meta_begin/end()? */
2019 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboSave);