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/arbprogram.h"
38 #include "main/arrayobj.h"
39 #include "main/blend.h"
40 #include "main/bufferobj.h"
41 #include "main/buffers.h"
42 #include "main/colortab.h"
43 #include "main/condrender.h"
44 #include "main/depth.h"
45 #include "main/enable.h"
46 #include "main/fbobject.h"
47 #include "main/formats.h"
48 #include "main/image.h"
49 #include "main/macros.h"
50 #include "main/matrix.h"
51 #include "main/mipmap.h"
53 #include "main/polygon.h"
54 #include "main/readpix.h"
55 #include "main/scissor.h"
56 #include "main/shaderapi.h"
57 #include "main/shaderobj.h"
58 #include "main/state.h"
59 #include "main/stencil.h"
60 #include "main/texobj.h"
61 #include "main/texenv.h"
62 #include "main/teximage.h"
63 #include "main/texparam.h"
64 #include "main/texstate.h"
65 #include "main/uniforms.h"
66 #include "main/varray.h"
67 #include "main/viewport.h"
68 #include "program/program.h"
69 #include "swrast/swrast.h"
70 #include "drivers/common/meta.h"
73 /** Return offset in bytes of the field within a vertex struct */
74 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
77 * State which we may save/restore across meta ops.
78 * XXX this may be incomplete...
82 GLbitfield SavedState; /**< bitmask of MESA_META_* flags */
84 /** MESA_META_ALPHA_TEST */
85 GLboolean AlphaEnabled;
89 /** MESA_META_BLEND */
90 GLbitfield BlendEnabled;
91 GLboolean ColorLogicOpEnabled;
93 /** MESA_META_COLOR_MASK */
94 GLubyte ColorMask[MAX_DRAW_BUFFERS][4];
96 /** MESA_META_DEPTH_TEST */
97 struct gl_depthbuffer_attrib Depth;
102 /** MESA_META_PIXEL_STORE */
103 struct gl_pixelstore_attrib Pack, Unpack;
105 /** MESA_META_PIXEL_TRANSFER */
106 GLfloat RedBias, RedScale;
107 GLfloat GreenBias, GreenScale;
108 GLfloat BlueBias, BlueScale;
109 GLfloat AlphaBias, AlphaScale;
110 GLfloat DepthBias, DepthScale;
111 GLboolean MapColorFlag;
113 /** MESA_META_RASTERIZATION */
114 GLenum FrontPolygonMode, BackPolygonMode;
115 GLboolean PolygonOffset;
116 GLboolean PolygonSmooth;
117 GLboolean PolygonStipple;
118 GLboolean PolygonCull;
120 /** MESA_META_SCISSOR */
121 struct gl_scissor_attrib Scissor;
123 /** MESA_META_SHADER */
124 GLboolean VertexProgramEnabled;
125 struct gl_vertex_program *VertexProgram;
126 GLboolean FragmentProgramEnabled;
127 struct gl_fragment_program *FragmentProgram;
128 struct gl_shader_program *VertexShader;
129 struct gl_shader_program *GeometryShader;
130 struct gl_shader_program *FragmentShader;
131 struct gl_shader_program *ActiveShader;
133 /** MESA_META_STENCIL_TEST */
134 struct gl_stencil_attrib Stencil;
136 /** MESA_META_TRANSFORM */
138 GLfloat ModelviewMatrix[16];
139 GLfloat ProjectionMatrix[16];
140 GLfloat TextureMatrix[16];
142 /** MESA_META_CLIP */
143 GLbitfield ClipPlanesEnabled;
145 /** MESA_META_TEXTURE */
147 GLuint ClientActiveUnit;
148 /** for unit[0] only */
149 struct gl_texture_object *CurrentTexture[NUM_TEXTURE_TARGETS];
150 /** mask of TEXTURE_2D_BIT, etc */
151 GLbitfield TexEnabled[MAX_TEXTURE_UNITS];
152 GLbitfield TexGenEnabled[MAX_TEXTURE_UNITS];
153 GLuint EnvMode; /* unit[0] only */
155 /** MESA_META_VERTEX */
156 struct gl_array_object *ArrayObj;
157 struct gl_buffer_object *ArrayBufferObj;
159 /** MESA_META_VIEWPORT */
160 GLint ViewportX, ViewportY, ViewportW, ViewportH;
161 GLclampd DepthNear, DepthFar;
163 /** MESA_META_CLAMP_FRAGMENT_COLOR */
164 GLenum ClampFragmentColor;
166 /** MESA_META_CLAMP_VERTEX_COLOR */
167 GLenum ClampVertexColor;
169 /** MESA_META_CONDITIONAL_RENDER */
170 struct gl_query_object *CondRenderQuery;
171 GLenum CondRenderMode;
173 /** Miscellaneous (always disabled) */
179 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
180 * This is currently shared by all the meta ops. But we could create a
181 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
186 GLenum Target; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
187 GLsizei MinSize; /**< Min texture size to allocate */
188 GLsizei MaxSize; /**< Max possible texture size */
189 GLboolean NPOT; /**< Non-power of two size OK? */
190 GLsizei Width, Height; /**< Current texture size */
192 GLfloat Sright, Ttop; /**< right, top texcoords */
197 * State for glBlitFramebufer()
208 * State for glClear()
220 * State for glCopyPixels()
230 * State for glDrawPixels()
236 GLuint StencilFP; /**< Fragment program for drawing stencil images */
237 GLuint DepthFP; /**< Fragment program for drawing depth images */
242 * State for glBitmap()
248 struct temp_texture Tex; /**< separate texture from other meta ops */
253 * State for _mesa_meta_generate_mipmap()
255 struct gen_mipmap_state
262 #define MAX_META_OPS_DEPTH 2
264 * All per-context meta state.
268 /** Stack of state saved during meta-ops */
269 struct save_state Save[MAX_META_OPS_DEPTH];
270 /** Save stack depth */
271 GLuint SaveStackDepth;
273 struct temp_texture TempTex;
275 struct blit_state Blit; /**< For _mesa_meta_BlitFramebuffer() */
276 struct clear_state Clear; /**< For _mesa_meta_Clear() */
277 struct copypix_state CopyPix; /**< For _mesa_meta_CopyPixels() */
278 struct drawpix_state DrawPix; /**< For _mesa_meta_DrawPixels() */
279 struct bitmap_state Bitmap; /**< For _mesa_meta_Bitmap() */
280 struct gen_mipmap_state Mipmap; /**< For _mesa_meta_GenerateMipmap() */
285 * Initialize meta-ops for a context.
286 * To be called once during context creation.
289 _mesa_meta_init(struct gl_context *ctx)
293 ctx->Meta = CALLOC_STRUCT(gl_meta_state);
298 * Free context meta-op state.
299 * To be called once during context destruction.
302 _mesa_meta_free(struct gl_context *ctx)
304 /* Note: Any textures, VBOs, etc, that we allocate should get
305 * freed by the normal context destruction code. But this would be
306 * the place to free other meta data someday.
314 * Enter meta state. This is like a light-weight version of glPushAttrib
315 * but it also resets most GL state back to default values.
317 * \param state bitmask of MESA_META_* flags indicating which attribute groups
318 * to save and reset to their defaults
321 _mesa_meta_begin(struct gl_context *ctx, GLbitfield state)
323 struct save_state *save;
325 /* hope MAX_META_OPS_DEPTH is large enough */
326 assert(ctx->Meta->SaveStackDepth < MAX_META_OPS_DEPTH);
328 save = &ctx->Meta->Save[ctx->Meta->SaveStackDepth++];
329 memset(save, 0, sizeof(*save));
330 save->SavedState = state;
332 if (state & MESA_META_ALPHA_TEST) {
333 save->AlphaEnabled = ctx->Color.AlphaEnabled;
334 save->AlphaFunc = ctx->Color.AlphaFunc;
335 save->AlphaRef = ctx->Color.AlphaRef;
336 if (ctx->Color.AlphaEnabled)
337 _mesa_set_enable(ctx, GL_ALPHA_TEST, GL_FALSE);
340 if (state & MESA_META_BLEND) {
341 save->BlendEnabled = ctx->Color.BlendEnabled;
342 if (ctx->Color.BlendEnabled) {
343 if (ctx->Extensions.EXT_draw_buffers2) {
345 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
346 _mesa_set_enablei(ctx, GL_BLEND, i, GL_FALSE);
350 _mesa_set_enable(ctx, GL_BLEND, GL_FALSE);
353 save->ColorLogicOpEnabled = ctx->Color.ColorLogicOpEnabled;
354 if (ctx->Color.ColorLogicOpEnabled)
355 _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP, GL_FALSE);
358 if (state & MESA_META_COLOR_MASK) {
359 memcpy(save->ColorMask, ctx->Color.ColorMask,
360 sizeof(ctx->Color.ColorMask));
361 if (!ctx->Color.ColorMask[0][0] ||
362 !ctx->Color.ColorMask[0][1] ||
363 !ctx->Color.ColorMask[0][2] ||
364 !ctx->Color.ColorMask[0][3])
365 _mesa_ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
368 if (state & MESA_META_DEPTH_TEST) {
369 save->Depth = ctx->Depth; /* struct copy */
371 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_FALSE);
374 if (state & MESA_META_FOG) {
375 save->Fog = ctx->Fog.Enabled;
376 if (ctx->Fog.Enabled)
377 _mesa_set_enable(ctx, GL_FOG, GL_FALSE);
380 if (state & MESA_META_PIXEL_STORE) {
381 save->Pack = ctx->Pack;
382 save->Unpack = ctx->Unpack;
383 ctx->Pack = ctx->DefaultPacking;
384 ctx->Unpack = ctx->DefaultPacking;
387 if (state & MESA_META_PIXEL_TRANSFER) {
388 save->RedScale = ctx->Pixel.RedScale;
389 save->RedBias = ctx->Pixel.RedBias;
390 save->GreenScale = ctx->Pixel.GreenScale;
391 save->GreenBias = ctx->Pixel.GreenBias;
392 save->BlueScale = ctx->Pixel.BlueScale;
393 save->BlueBias = ctx->Pixel.BlueBias;
394 save->AlphaScale = ctx->Pixel.AlphaScale;
395 save->AlphaBias = ctx->Pixel.AlphaBias;
396 save->MapColorFlag = ctx->Pixel.MapColorFlag;
397 ctx->Pixel.RedScale = 1.0F;
398 ctx->Pixel.RedBias = 0.0F;
399 ctx->Pixel.GreenScale = 1.0F;
400 ctx->Pixel.GreenBias = 0.0F;
401 ctx->Pixel.BlueScale = 1.0F;
402 ctx->Pixel.BlueBias = 0.0F;
403 ctx->Pixel.AlphaScale = 1.0F;
404 ctx->Pixel.AlphaBias = 0.0F;
405 ctx->Pixel.MapColorFlag = GL_FALSE;
407 ctx->NewState |=_NEW_PIXEL;
410 if (state & MESA_META_RASTERIZATION) {
411 save->FrontPolygonMode = ctx->Polygon.FrontMode;
412 save->BackPolygonMode = ctx->Polygon.BackMode;
413 save->PolygonOffset = ctx->Polygon.OffsetFill;
414 save->PolygonSmooth = ctx->Polygon.SmoothFlag;
415 save->PolygonStipple = ctx->Polygon.StippleFlag;
416 save->PolygonCull = ctx->Polygon.CullFlag;
417 _mesa_PolygonMode(GL_FRONT_AND_BACK, GL_FILL);
418 _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL, GL_FALSE);
419 _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, GL_FALSE);
420 _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, GL_FALSE);
421 _mesa_set_enable(ctx, GL_CULL_FACE, GL_FALSE);
424 if (state & MESA_META_SCISSOR) {
425 save->Scissor = ctx->Scissor; /* struct copy */
426 _mesa_set_enable(ctx, GL_SCISSOR_TEST, GL_FALSE);
429 if (state & MESA_META_SHADER) {
430 if (ctx->Extensions.ARB_vertex_program) {
431 save->VertexProgramEnabled = ctx->VertexProgram.Enabled;
432 _mesa_reference_vertprog(ctx, &save->VertexProgram,
433 ctx->VertexProgram.Current);
434 _mesa_set_enable(ctx, GL_VERTEX_PROGRAM_ARB, GL_FALSE);
437 if (ctx->Extensions.ARB_fragment_program) {
438 save->FragmentProgramEnabled = ctx->FragmentProgram.Enabled;
439 _mesa_reference_fragprog(ctx, &save->FragmentProgram,
440 ctx->FragmentProgram.Current);
441 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_FALSE);
444 if (ctx->Extensions.ARB_shader_objects) {
445 _mesa_reference_shader_program(ctx, &save->VertexShader,
446 ctx->Shader.CurrentVertexProgram);
447 _mesa_reference_shader_program(ctx, &save->GeometryShader,
448 ctx->Shader.CurrentGeometryProgram);
449 _mesa_reference_shader_program(ctx, &save->FragmentShader,
450 ctx->Shader.CurrentFragmentProgram);
451 _mesa_reference_shader_program(ctx, &save->ActiveShader,
452 ctx->Shader.CurrentFragmentProgram);
454 _mesa_UseProgramObjectARB(0);
458 if (state & MESA_META_STENCIL_TEST) {
459 save->Stencil = ctx->Stencil; /* struct copy */
460 if (ctx->Stencil.Enabled)
461 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_FALSE);
462 /* NOTE: other stencil state not reset */
465 if (state & MESA_META_TEXTURE) {
468 save->ActiveUnit = ctx->Texture.CurrentUnit;
469 save->ClientActiveUnit = ctx->Array.ActiveTexture;
470 save->EnvMode = ctx->Texture.Unit[0].EnvMode;
472 /* Disable all texture units */
473 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
474 save->TexEnabled[u] = ctx->Texture.Unit[u].Enabled;
475 save->TexGenEnabled[u] = ctx->Texture.Unit[u].TexGenEnabled;
476 if (ctx->Texture.Unit[u].Enabled ||
477 ctx->Texture.Unit[u].TexGenEnabled) {
478 _mesa_ActiveTextureARB(GL_TEXTURE0 + u);
479 _mesa_set_enable(ctx, GL_TEXTURE_1D, GL_FALSE);
480 _mesa_set_enable(ctx, GL_TEXTURE_2D, GL_FALSE);
481 _mesa_set_enable(ctx, GL_TEXTURE_3D, GL_FALSE);
482 if (ctx->Extensions.ARB_texture_cube_map)
483 _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP, GL_FALSE);
484 _mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE, GL_FALSE);
485 _mesa_set_enable(ctx, GL_TEXTURE_GEN_S, GL_FALSE);
486 _mesa_set_enable(ctx, GL_TEXTURE_GEN_T, GL_FALSE);
487 _mesa_set_enable(ctx, GL_TEXTURE_GEN_R, GL_FALSE);
488 _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q, GL_FALSE);
492 /* save current texture objects for unit[0] only */
493 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
494 _mesa_reference_texobj(&save->CurrentTexture[tgt],
495 ctx->Texture.Unit[0].CurrentTex[tgt]);
498 /* set defaults for unit[0] */
499 _mesa_ActiveTextureARB(GL_TEXTURE0);
500 _mesa_ClientActiveTextureARB(GL_TEXTURE0);
501 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
504 if (state & MESA_META_TRANSFORM) {
505 GLuint activeTexture = ctx->Texture.CurrentUnit;
506 memcpy(save->ModelviewMatrix, ctx->ModelviewMatrixStack.Top->m,
507 16 * sizeof(GLfloat));
508 memcpy(save->ProjectionMatrix, ctx->ProjectionMatrixStack.Top->m,
509 16 * sizeof(GLfloat));
510 memcpy(save->TextureMatrix, ctx->TextureMatrixStack[0].Top->m,
511 16 * sizeof(GLfloat));
512 save->MatrixMode = ctx->Transform.MatrixMode;
513 /* set 1:1 vertex:pixel coordinate transform */
514 _mesa_ActiveTextureARB(GL_TEXTURE0);
515 _mesa_MatrixMode(GL_TEXTURE);
516 _mesa_LoadIdentity();
517 _mesa_ActiveTextureARB(GL_TEXTURE0 + activeTexture);
518 _mesa_MatrixMode(GL_MODELVIEW);
519 _mesa_LoadIdentity();
520 _mesa_MatrixMode(GL_PROJECTION);
521 _mesa_LoadIdentity();
522 _mesa_Ortho(0.0, ctx->DrawBuffer->Width,
523 0.0, ctx->DrawBuffer->Height,
527 if (state & MESA_META_CLIP) {
528 save->ClipPlanesEnabled = ctx->Transform.ClipPlanesEnabled;
529 if (ctx->Transform.ClipPlanesEnabled) {
531 for (i = 0; i < ctx->Const.MaxClipPlanes; i++) {
532 _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_FALSE);
537 if (state & MESA_META_VERTEX) {
538 /* save vertex array object state */
539 _mesa_reference_array_object(ctx, &save->ArrayObj,
540 ctx->Array.ArrayObj);
541 _mesa_reference_buffer_object(ctx, &save->ArrayBufferObj,
542 ctx->Array.ArrayBufferObj);
543 /* set some default state? */
546 if (state & MESA_META_VIEWPORT) {
547 /* save viewport state */
548 save->ViewportX = ctx->Viewport.X;
549 save->ViewportY = ctx->Viewport.Y;
550 save->ViewportW = ctx->Viewport.Width;
551 save->ViewportH = ctx->Viewport.Height;
552 /* set viewport to match window size */
553 if (ctx->Viewport.X != 0 ||
554 ctx->Viewport.Y != 0 ||
555 ctx->Viewport.Width != ctx->DrawBuffer->Width ||
556 ctx->Viewport.Height != ctx->DrawBuffer->Height) {
557 _mesa_set_viewport(ctx, 0, 0,
558 ctx->DrawBuffer->Width, ctx->DrawBuffer->Height);
560 /* save depth range state */
561 save->DepthNear = ctx->Viewport.Near;
562 save->DepthFar = ctx->Viewport.Far;
563 /* set depth range to default */
564 _mesa_DepthRange(0.0, 1.0);
567 if (state & MESA_META_CLAMP_FRAGMENT_COLOR) {
568 save->ClampFragmentColor = ctx->Color.ClampFragmentColor;
570 /* Generally in here we want to do clamping according to whether
571 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
572 * regardless of the internal implementation of the metaops.
574 if (ctx->Color.ClampFragmentColor != GL_TRUE)
575 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
578 if (state & MESA_META_CLAMP_VERTEX_COLOR) {
579 save->ClampVertexColor = ctx->Light.ClampVertexColor;
581 /* Generally in here we never want vertex color clamping --
582 * result clamping is only dependent on fragment clamping.
584 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR, GL_FALSE);
587 if (state & MESA_META_CONDITIONAL_RENDER) {
588 save->CondRenderQuery = ctx->Query.CondRenderQuery;
589 save->CondRenderMode = ctx->Query.CondRenderMode;
591 if (ctx->Query.CondRenderQuery)
592 _mesa_EndConditionalRender();
597 save->Lighting = ctx->Light.Enabled;
598 if (ctx->Light.Enabled)
599 _mesa_set_enable(ctx, GL_LIGHTING, GL_FALSE);
605 * Leave meta state. This is like a light-weight version of glPopAttrib().
608 _mesa_meta_end(struct gl_context *ctx)
610 struct save_state *save = &ctx->Meta->Save[--ctx->Meta->SaveStackDepth];
611 const GLbitfield state = save->SavedState;
613 if (state & MESA_META_ALPHA_TEST) {
614 if (ctx->Color.AlphaEnabled != save->AlphaEnabled)
615 _mesa_set_enable(ctx, GL_ALPHA_TEST, save->AlphaEnabled);
616 _mesa_AlphaFunc(save->AlphaFunc, save->AlphaRef);
619 if (state & MESA_META_BLEND) {
620 if (ctx->Color.BlendEnabled != save->BlendEnabled) {
621 if (ctx->Extensions.EXT_draw_buffers2) {
623 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
624 _mesa_set_enablei(ctx, GL_BLEND, i, (save->BlendEnabled >> i) & 1);
628 _mesa_set_enable(ctx, GL_BLEND, (save->BlendEnabled & 1));
631 if (ctx->Color.ColorLogicOpEnabled != save->ColorLogicOpEnabled)
632 _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP, save->ColorLogicOpEnabled);
635 if (state & MESA_META_COLOR_MASK) {
637 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
638 if (!TEST_EQ_4V(ctx->Color.ColorMask[i], save->ColorMask[i])) {
640 _mesa_ColorMask(save->ColorMask[i][0], save->ColorMask[i][1],
641 save->ColorMask[i][2], save->ColorMask[i][3]);
644 _mesa_ColorMaskIndexed(i,
645 save->ColorMask[i][0],
646 save->ColorMask[i][1],
647 save->ColorMask[i][2],
648 save->ColorMask[i][3]);
654 if (state & MESA_META_DEPTH_TEST) {
655 if (ctx->Depth.Test != save->Depth.Test)
656 _mesa_set_enable(ctx, GL_DEPTH_TEST, save->Depth.Test);
657 _mesa_DepthFunc(save->Depth.Func);
658 _mesa_DepthMask(save->Depth.Mask);
661 if (state & MESA_META_FOG) {
662 _mesa_set_enable(ctx, GL_FOG, save->Fog);
665 if (state & MESA_META_PIXEL_STORE) {
666 ctx->Pack = save->Pack;
667 ctx->Unpack = save->Unpack;
670 if (state & MESA_META_PIXEL_TRANSFER) {
671 ctx->Pixel.RedScale = save->RedScale;
672 ctx->Pixel.RedBias = save->RedBias;
673 ctx->Pixel.GreenScale = save->GreenScale;
674 ctx->Pixel.GreenBias = save->GreenBias;
675 ctx->Pixel.BlueScale = save->BlueScale;
676 ctx->Pixel.BlueBias = save->BlueBias;
677 ctx->Pixel.AlphaScale = save->AlphaScale;
678 ctx->Pixel.AlphaBias = save->AlphaBias;
679 ctx->Pixel.MapColorFlag = save->MapColorFlag;
681 ctx->NewState |=_NEW_PIXEL;
684 if (state & MESA_META_RASTERIZATION) {
685 _mesa_PolygonMode(GL_FRONT, save->FrontPolygonMode);
686 _mesa_PolygonMode(GL_BACK, save->BackPolygonMode);
687 _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, save->PolygonStipple);
688 _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL, save->PolygonOffset);
689 _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, save->PolygonSmooth);
690 _mesa_set_enable(ctx, GL_CULL_FACE, save->PolygonCull);
693 if (state & MESA_META_SCISSOR) {
694 _mesa_set_enable(ctx, GL_SCISSOR_TEST, save->Scissor.Enabled);
695 _mesa_Scissor(save->Scissor.X, save->Scissor.Y,
696 save->Scissor.Width, save->Scissor.Height);
699 if (state & MESA_META_SHADER) {
700 if (ctx->Extensions.ARB_vertex_program) {
701 _mesa_set_enable(ctx, GL_VERTEX_PROGRAM_ARB,
702 save->VertexProgramEnabled);
703 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
704 save->VertexProgram);
705 _mesa_reference_vertprog(ctx, &save->VertexProgram, NULL);
708 if (ctx->Extensions.ARB_fragment_program) {
709 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB,
710 save->FragmentProgramEnabled);
711 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
712 save->FragmentProgram);
713 _mesa_reference_fragprog(ctx, &save->FragmentProgram, NULL);
716 if (ctx->Extensions.ARB_vertex_shader)
717 _mesa_use_shader_program(ctx, GL_VERTEX_SHADER, save->VertexShader);
719 if (ctx->Extensions.ARB_geometry_shader4)
720 _mesa_use_shader_program(ctx, GL_GEOMETRY_SHADER_ARB,
721 save->GeometryShader);
723 if (ctx->Extensions.ARB_fragment_shader)
724 _mesa_use_shader_program(ctx, GL_FRAGMENT_SHADER,
725 save->FragmentShader);
727 _mesa_reference_shader_program(ctx, &ctx->Shader.ActiveProgram,
731 if (state & MESA_META_STENCIL_TEST) {
732 const struct gl_stencil_attrib *stencil = &save->Stencil;
734 _mesa_set_enable(ctx, GL_STENCIL_TEST, stencil->Enabled);
735 _mesa_ClearStencil(stencil->Clear);
736 if (ctx->Extensions.EXT_stencil_two_side) {
737 _mesa_set_enable(ctx, GL_STENCIL_TEST_TWO_SIDE_EXT,
738 stencil->TestTwoSide);
739 _mesa_ActiveStencilFaceEXT(stencil->ActiveFace
740 ? GL_BACK : GL_FRONT);
743 _mesa_StencilFuncSeparate(GL_FRONT,
744 stencil->Function[0],
746 stencil->ValueMask[0]);
747 _mesa_StencilMaskSeparate(GL_FRONT, stencil->WriteMask[0]);
748 _mesa_StencilOpSeparate(GL_FRONT, stencil->FailFunc[0],
749 stencil->ZFailFunc[0],
750 stencil->ZPassFunc[0]);
752 _mesa_StencilFuncSeparate(GL_BACK,
753 stencil->Function[1],
755 stencil->ValueMask[1]);
756 _mesa_StencilMaskSeparate(GL_BACK, stencil->WriteMask[1]);
757 _mesa_StencilOpSeparate(GL_BACK, stencil->FailFunc[1],
758 stencil->ZFailFunc[1],
759 stencil->ZPassFunc[1]);
762 if (state & MESA_META_TEXTURE) {
765 ASSERT(ctx->Texture.CurrentUnit == 0);
767 /* restore texenv for unit[0] */
768 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, save->EnvMode);
770 /* restore texture objects for unit[0] only */
771 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
772 _mesa_reference_texobj(&ctx->Texture.Unit[0].CurrentTex[tgt],
773 save->CurrentTexture[tgt]);
774 _mesa_reference_texobj(&save->CurrentTexture[tgt], NULL);
777 /* Re-enable textures, texgen */
778 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
779 if (save->TexEnabled[u]) {
780 _mesa_ActiveTextureARB(GL_TEXTURE0 + u);
782 if (save->TexEnabled[u] & TEXTURE_1D_BIT)
783 _mesa_set_enable(ctx, GL_TEXTURE_1D, GL_TRUE);
784 if (save->TexEnabled[u] & TEXTURE_2D_BIT)
785 _mesa_set_enable(ctx, GL_TEXTURE_2D, GL_TRUE);
786 if (save->TexEnabled[u] & TEXTURE_3D_BIT)
787 _mesa_set_enable(ctx, GL_TEXTURE_3D, GL_TRUE);
788 if (save->TexEnabled[u] & TEXTURE_CUBE_BIT)
789 _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP, GL_TRUE);
790 if (save->TexEnabled[u] & TEXTURE_RECT_BIT)
791 _mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE, GL_TRUE);
794 if (save->TexGenEnabled[u]) {
795 _mesa_ActiveTextureARB(GL_TEXTURE0 + u);
797 if (save->TexGenEnabled[u] & S_BIT)
798 _mesa_set_enable(ctx, GL_TEXTURE_GEN_S, GL_TRUE);
799 if (save->TexGenEnabled[u] & T_BIT)
800 _mesa_set_enable(ctx, GL_TEXTURE_GEN_T, GL_TRUE);
801 if (save->TexGenEnabled[u] & R_BIT)
802 _mesa_set_enable(ctx, GL_TEXTURE_GEN_R, GL_TRUE);
803 if (save->TexGenEnabled[u] & Q_BIT)
804 _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q, GL_TRUE);
808 /* restore current unit state */
809 _mesa_ActiveTextureARB(GL_TEXTURE0 + save->ActiveUnit);
810 _mesa_ClientActiveTextureARB(GL_TEXTURE0 + save->ClientActiveUnit);
813 if (state & MESA_META_TRANSFORM) {
814 GLuint activeTexture = ctx->Texture.CurrentUnit;
815 _mesa_ActiveTextureARB(GL_TEXTURE0);
816 _mesa_MatrixMode(GL_TEXTURE);
817 _mesa_LoadMatrixf(save->TextureMatrix);
818 _mesa_ActiveTextureARB(GL_TEXTURE0 + activeTexture);
820 _mesa_MatrixMode(GL_MODELVIEW);
821 _mesa_LoadMatrixf(save->ModelviewMatrix);
823 _mesa_MatrixMode(GL_PROJECTION);
824 _mesa_LoadMatrixf(save->ProjectionMatrix);
826 _mesa_MatrixMode(save->MatrixMode);
829 if (state & MESA_META_CLIP) {
830 if (save->ClipPlanesEnabled) {
832 for (i = 0; i < ctx->Const.MaxClipPlanes; i++) {
833 if (save->ClipPlanesEnabled & (1 << i)) {
834 _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_TRUE);
840 if (state & MESA_META_VERTEX) {
841 /* restore vertex buffer object */
842 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, save->ArrayBufferObj->Name);
843 _mesa_reference_buffer_object(ctx, &save->ArrayBufferObj, NULL);
845 /* restore vertex array object */
846 _mesa_BindVertexArray(save->ArrayObj->Name);
847 _mesa_reference_array_object(ctx, &save->ArrayObj, NULL);
850 if (state & MESA_META_VIEWPORT) {
851 if (save->ViewportX != ctx->Viewport.X ||
852 save->ViewportY != ctx->Viewport.Y ||
853 save->ViewportW != ctx->Viewport.Width ||
854 save->ViewportH != ctx->Viewport.Height) {
855 _mesa_set_viewport(ctx, save->ViewportX, save->ViewportY,
856 save->ViewportW, save->ViewportH);
858 _mesa_DepthRange(save->DepthNear, save->DepthFar);
861 if (state & MESA_META_CLAMP_FRAGMENT_COLOR) {
862 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR, save->ClampFragmentColor);
865 if (state & MESA_META_CLAMP_VERTEX_COLOR) {
866 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR, save->ClampVertexColor);
869 if (state & MESA_META_CONDITIONAL_RENDER) {
870 if (save->CondRenderQuery)
871 _mesa_BeginConditionalRender(save->CondRenderQuery->Id,
872 save->CondRenderMode);
876 if (save->Lighting) {
877 _mesa_set_enable(ctx, GL_LIGHTING, GL_TRUE);
883 * Convert Z from a normalized value in the range [0, 1] to an object-space
884 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
885 * default/identity ortho projection results in the original Z value.
886 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
887 * value comes from the clear value or raster position.
889 static INLINE GLfloat
890 invert_z(GLfloat normZ)
892 GLfloat objZ = 1.0 - 2.0 * normZ;
898 * One-time init for a temp_texture object.
899 * Choose tex target, compute max tex size, etc.
902 init_temp_texture(struct gl_context *ctx, struct temp_texture *tex)
904 /* prefer texture rectangle */
905 if (ctx->Extensions.NV_texture_rectangle) {
906 tex->Target = GL_TEXTURE_RECTANGLE;
907 tex->MaxSize = ctx->Const.MaxTextureRectSize;
911 /* use 2D texture, NPOT if possible */
912 tex->Target = GL_TEXTURE_2D;
913 tex->MaxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
914 tex->NPOT = ctx->Extensions.ARB_texture_non_power_of_two;
916 tex->MinSize = 16; /* 16 x 16 at least */
917 assert(tex->MaxSize > 0);
919 _mesa_GenTextures(1, &tex->TexObj);
924 * Return pointer to temp_texture info for non-bitmap ops.
925 * This does some one-time init if needed.
927 static struct temp_texture *
928 get_temp_texture(struct gl_context *ctx)
930 struct temp_texture *tex = &ctx->Meta->TempTex;
933 init_temp_texture(ctx, tex);
941 * Return pointer to temp_texture info for _mesa_meta_bitmap().
942 * We use a separate texture for bitmaps to reduce texture
943 * allocation/deallocation.
945 static struct temp_texture *
946 get_bitmap_temp_texture(struct gl_context *ctx)
948 struct temp_texture *tex = &ctx->Meta->Bitmap.Tex;
951 init_temp_texture(ctx, tex);
959 * Compute the width/height of texture needed to draw an image of the
960 * given size. Return a flag indicating whether the current texture
961 * can be re-used (glTexSubImage2D) or if a new texture needs to be
962 * allocated (glTexImage2D).
963 * Also, compute s/t texcoords for drawing.
965 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
968 alloc_texture(struct temp_texture *tex,
969 GLsizei width, GLsizei height, GLenum intFormat)
971 GLboolean newTex = GL_FALSE;
973 ASSERT(width <= tex->MaxSize);
974 ASSERT(height <= tex->MaxSize);
976 if (width > tex->Width ||
977 height > tex->Height ||
978 intFormat != tex->IntFormat) {
979 /* alloc new texture (larger or different format) */
982 /* use non-power of two size */
983 tex->Width = MAX2(tex->MinSize, width);
984 tex->Height = MAX2(tex->MinSize, height);
987 /* find power of two size */
989 w = h = tex->MinSize;
998 tex->IntFormat = intFormat;
1003 /* compute texcoords */
1004 if (tex->Target == GL_TEXTURE_RECTANGLE) {
1005 tex->Sright = (GLfloat) width;
1006 tex->Ttop = (GLfloat) height;
1009 tex->Sright = (GLfloat) width / tex->Width;
1010 tex->Ttop = (GLfloat) height / tex->Height;
1018 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1021 setup_copypix_texture(struct temp_texture *tex,
1023 GLint srcX, GLint srcY,
1024 GLsizei width, GLsizei height, GLenum intFormat,
1027 _mesa_BindTexture(tex->Target, tex->TexObj);
1028 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MIN_FILTER, filter);
1029 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MAG_FILTER, filter);
1030 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1032 /* copy framebuffer image to texture */
1034 /* create new tex image */
1035 if (tex->Width == width && tex->Height == height) {
1036 /* create new tex with framebuffer data */
1037 _mesa_CopyTexImage2D(tex->Target, 0, tex->IntFormat,
1038 srcX, srcY, width, height, 0);
1041 /* create empty texture */
1042 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
1043 tex->Width, tex->Height, 0,
1044 intFormat, GL_UNSIGNED_BYTE, NULL);
1046 _mesa_CopyTexSubImage2D(tex->Target, 0,
1047 0, 0, srcX, srcY, width, height);
1051 /* replace existing tex image */
1052 _mesa_CopyTexSubImage2D(tex->Target, 0,
1053 0, 0, srcX, srcY, width, height);
1059 * Setup/load texture for glDrawPixels.
1062 setup_drawpix_texture(struct gl_context *ctx,
1063 struct temp_texture *tex,
1065 GLenum texIntFormat,
1066 GLsizei width, GLsizei height,
1067 GLenum format, GLenum type,
1068 const GLvoid *pixels)
1070 _mesa_BindTexture(tex->Target, tex->TexObj);
1071 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1072 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1073 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1075 /* copy pixel data to texture */
1077 /* create new tex image */
1078 if (tex->Width == width && tex->Height == height) {
1079 /* create new tex and load image data */
1080 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
1081 tex->Width, tex->Height, 0, format, type, pixels);
1084 struct gl_buffer_object *save_unpack_obj = NULL;
1086 _mesa_reference_buffer_object(ctx, &save_unpack_obj,
1087 ctx->Unpack.BufferObj);
1088 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
1089 /* create empty texture */
1090 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
1091 tex->Width, tex->Height, 0, format, type, NULL);
1092 if (save_unpack_obj != NULL)
1093 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB,
1094 save_unpack_obj->Name);
1096 _mesa_TexSubImage2D(tex->Target, 0,
1097 0, 0, width, height, format, type, pixels);
1101 /* replace existing tex image */
1102 _mesa_TexSubImage2D(tex->Target, 0,
1103 0, 0, width, height, format, type, pixels);
1110 * One-time init for drawing depth pixels.
1113 init_blit_depth_pixels(struct gl_context *ctx)
1115 static const char *program =
1117 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1120 struct blit_state *blit = &ctx->Meta->Blit;
1121 struct temp_texture *tex = get_temp_texture(ctx);
1122 const char *texTarget;
1124 assert(blit->DepthFP == 0);
1126 /* replace %s with "RECT" or "2D" */
1127 assert(strlen(program) + 4 < sizeof(program2));
1128 if (tex->Target == GL_TEXTURE_RECTANGLE)
1132 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
1134 _mesa_GenPrograms(1, &blit->DepthFP);
1135 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, blit->DepthFP);
1136 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
1137 strlen(program2), (const GLubyte *) program2);
1142 * Try to do a glBlitFramebuffer using no-copy texturing.
1143 * We can do this when the src renderbuffer is actually a texture.
1144 * But if the src buffer == dst buffer we cannot do this.
1146 * \return new buffer mask indicating the buffers left to blit using the
1150 blitframebuffer_texture(struct gl_context *ctx,
1151 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
1152 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
1153 GLbitfield mask, GLenum filter)
1155 if (mask & GL_COLOR_BUFFER_BIT) {
1156 const struct gl_framebuffer *drawFb = ctx->DrawBuffer;
1157 const struct gl_framebuffer *readFb = ctx->ReadBuffer;
1158 const struct gl_renderbuffer_attachment *drawAtt =
1159 &drawFb->Attachment[drawFb->_ColorDrawBufferIndexes[0]];
1160 const struct gl_renderbuffer_attachment *readAtt =
1161 &readFb->Attachment[readFb->_ColorReadBufferIndex];
1163 if (readAtt && readAtt->Texture) {
1164 const struct gl_texture_object *texObj = readAtt->Texture;
1165 const GLuint srcLevel = readAtt->TextureLevel;
1166 const GLenum minFilterSave = texObj->Sampler.MinFilter;
1167 const GLenum magFilterSave = texObj->Sampler.MagFilter;
1168 const GLint baseLevelSave = texObj->BaseLevel;
1169 const GLint maxLevelSave = texObj->MaxLevel;
1170 const GLenum wrapSSave = texObj->Sampler.WrapS;
1171 const GLenum wrapTSave = texObj->Sampler.WrapT;
1172 const GLenum srgbSave = texObj->Sampler.sRGBDecode;
1173 const GLenum fbo_srgb_save = ctx->Color.sRGBEnabled;
1174 const GLenum target = texObj->Target;
1176 if (drawAtt->Texture == readAtt->Texture) {
1177 /* Can't use same texture as both the source and dest. We need
1178 * to handle overlapping blits and besides, some hw may not
1184 if (target != GL_TEXTURE_2D && target != GL_TEXTURE_RECTANGLE_ARB) {
1185 /* Can't handle other texture types at this time */
1190 printf("Blit from texture!\n");
1191 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1192 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1195 /* Prepare src texture state */
1196 _mesa_BindTexture(target, texObj->Name);
1197 _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
1198 _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
1199 if (target != GL_TEXTURE_RECTANGLE_ARB) {
1200 _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, srcLevel);
1201 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, srcLevel);
1203 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1204 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1206 /* Always do our blits with no sRGB decode or encode.*/
1207 if (ctx->Extensions.EXT_texture_sRGB_decode) {
1208 _mesa_TexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
1209 GL_SKIP_DECODE_EXT);
1211 _mesa_Disable(GL_FRAMEBUFFER_SRGB_EXT);
1213 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1214 _mesa_set_enable(ctx, target, GL_TRUE);
1216 /* Prepare vertex data (the VBO was previously created and bound) */
1221 struct vertex verts[4];
1222 GLfloat s0, t0, s1, t1;
1224 if (target == GL_TEXTURE_2D) {
1225 const struct gl_texture_image *texImage
1226 = _mesa_select_tex_image(ctx, texObj, target, srcLevel);
1227 s0 = srcX0 / (float) texImage->Width;
1228 s1 = srcX1 / (float) texImage->Width;
1229 t0 = srcY0 / (float) texImage->Height;
1230 t1 = srcY1 / (float) texImage->Height;
1233 assert(target == GL_TEXTURE_RECTANGLE_ARB);
1240 verts[0].x = (GLfloat) dstX0;
1241 verts[0].y = (GLfloat) dstY0;
1242 verts[1].x = (GLfloat) dstX1;
1243 verts[1].y = (GLfloat) dstY0;
1244 verts[2].x = (GLfloat) dstX1;
1245 verts[2].y = (GLfloat) dstY1;
1246 verts[3].x = (GLfloat) dstX0;
1247 verts[3].y = (GLfloat) dstY1;
1258 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1261 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1263 /* Restore texture object state, the texture binding will
1264 * be restored by _mesa_meta_end().
1266 _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilterSave);
1267 _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilterSave);
1268 if (target != GL_TEXTURE_RECTANGLE_ARB) {
1269 _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, baseLevelSave);
1270 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, maxLevelSave);
1272 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, wrapSSave);
1273 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, wrapTSave);
1274 if (ctx->Extensions.EXT_texture_sRGB_decode) {
1275 _mesa_TexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT, srgbSave);
1277 if (ctx->Extensions.EXT_texture_sRGB_decode && fbo_srgb_save) {
1278 _mesa_Enable(GL_FRAMEBUFFER_SRGB_EXT);
1281 /* Done with color buffer */
1282 mask &= ~GL_COLOR_BUFFER_BIT;
1291 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1292 * of texture mapping and polygon rendering.
1295 _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
1296 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
1297 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
1298 GLbitfield mask, GLenum filter)
1300 struct blit_state *blit = &ctx->Meta->Blit;
1301 struct temp_texture *tex = get_temp_texture(ctx);
1302 const GLsizei maxTexSize = tex->MaxSize;
1303 const GLint srcX = MIN2(srcX0, srcX1);
1304 const GLint srcY = MIN2(srcY0, srcY1);
1305 const GLint srcW = abs(srcX1 - srcX0);
1306 const GLint srcH = abs(srcY1 - srcY0);
1307 const GLboolean srcFlipX = srcX1 < srcX0;
1308 const GLboolean srcFlipY = srcY1 < srcY0;
1312 struct vertex verts[4];
1315 if (srcW > maxTexSize || srcH > maxTexSize) {
1316 /* XXX avoid this fallback */
1317 _swrast_BlitFramebuffer(ctx, srcX0, srcY0, srcX1, srcY1,
1318 dstX0, dstY0, dstX1, dstY1, mask, filter);
1334 /* only scissor effects blit so save/clear all other relevant state */
1335 _mesa_meta_begin(ctx, ~MESA_META_SCISSOR);
1337 if (blit->ArrayObj == 0) {
1338 /* one-time setup */
1340 /* create vertex array object */
1341 _mesa_GenVertexArrays(1, &blit->ArrayObj);
1342 _mesa_BindVertexArray(blit->ArrayObj);
1344 /* create vertex array buffer */
1345 _mesa_GenBuffersARB(1, &blit->VBO);
1346 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, blit->VBO);
1347 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
1348 NULL, GL_DYNAMIC_DRAW_ARB);
1350 /* setup vertex arrays */
1351 _mesa_VertexPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
1352 _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
1353 _mesa_EnableClientState(GL_VERTEX_ARRAY);
1354 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
1357 _mesa_BindVertexArray(blit->ArrayObj);
1358 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, blit->VBO);
1361 /* Try faster, direct texture approach first */
1362 mask = blitframebuffer_texture(ctx, srcX0, srcY0, srcX1, srcY1,
1363 dstX0, dstY0, dstX1, dstY1, mask, filter);
1365 _mesa_meta_end(ctx);
1369 /* Continue with "normal" approach which involves copying the src rect
1370 * into a temporary texture and is "blitted" by drawing a textured quad.
1373 newTex = alloc_texture(tex, srcW, srcH, GL_RGBA);
1375 /* vertex positions/texcoords (after texture allocation!) */
1377 verts[0].x = (GLfloat) dstX0;
1378 verts[0].y = (GLfloat) dstY0;
1379 verts[1].x = (GLfloat) dstX1;
1380 verts[1].y = (GLfloat) dstY0;
1381 verts[2].x = (GLfloat) dstX1;
1382 verts[2].y = (GLfloat) dstY1;
1383 verts[3].x = (GLfloat) dstX0;
1384 verts[3].y = (GLfloat) dstY1;
1388 verts[1].s = tex->Sright;
1390 verts[2].s = tex->Sright;
1391 verts[2].t = tex->Ttop;
1393 verts[3].t = tex->Ttop;
1395 /* upload new vertex data */
1396 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1399 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
1401 if (mask & GL_COLOR_BUFFER_BIT) {
1402 setup_copypix_texture(tex, newTex, srcX, srcY, srcW, srcH,
1404 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1405 mask &= ~GL_COLOR_BUFFER_BIT;
1408 if (mask & GL_DEPTH_BUFFER_BIT) {
1409 GLuint *tmp = (GLuint *) malloc(srcW * srcH * sizeof(GLuint));
1412 init_blit_depth_pixels(ctx);
1414 /* maybe change tex format here */
1415 newTex = alloc_texture(tex, srcW, srcH, GL_DEPTH_COMPONENT);
1417 _mesa_ReadPixels(srcX, srcY, srcW, srcH,
1418 GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, tmp);
1420 setup_drawpix_texture(ctx, tex, newTex, GL_DEPTH_COMPONENT, srcW, srcH,
1421 GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, tmp);
1423 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, blit->DepthFP);
1424 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
1425 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1426 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE);
1427 _mesa_DepthFunc(GL_ALWAYS);
1428 _mesa_DepthMask(GL_TRUE);
1430 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1431 mask &= ~GL_DEPTH_BUFFER_BIT;
1437 if (mask & GL_STENCIL_BUFFER_BIT) {
1438 /* XXX can't easily do stencil */
1441 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
1443 _mesa_meta_end(ctx);
1446 _swrast_BlitFramebuffer(ctx, srcX0, srcY0, srcX1, srcY1,
1447 dstX0, dstY0, dstX1, dstY1, mask, filter);
1453 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1456 _mesa_meta_Clear(struct gl_context *ctx, GLbitfield buffers)
1458 struct clear_state *clear = &ctx->Meta->Clear;
1460 GLfloat x, y, z, r, g, b, a;
1462 struct vertex verts[4];
1463 /* save all state but scissor, pixel pack/unpack */
1464 GLbitfield metaSave = (MESA_META_ALL -
1466 MESA_META_PIXEL_STORE -
1467 MESA_META_CONDITIONAL_RENDER);
1468 const GLuint stencilMax = (1 << ctx->DrawBuffer->Visual.stencilBits) - 1;
1470 if (buffers & BUFFER_BITS_COLOR) {
1471 /* if clearing color buffers, don't save/restore colormask */
1472 metaSave -= MESA_META_COLOR_MASK;
1475 _mesa_meta_begin(ctx, metaSave);
1477 if (clear->ArrayObj == 0) {
1478 /* one-time setup */
1480 /* create vertex array object */
1481 _mesa_GenVertexArrays(1, &clear->ArrayObj);
1482 _mesa_BindVertexArray(clear->ArrayObj);
1484 /* create vertex array buffer */
1485 _mesa_GenBuffersARB(1, &clear->VBO);
1486 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, clear->VBO);
1488 /* setup vertex arrays */
1489 _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
1490 _mesa_ColorPointer(4, GL_FLOAT, sizeof(struct vertex), OFFSET(r));
1491 _mesa_EnableClientState(GL_VERTEX_ARRAY);
1492 _mesa_EnableClientState(GL_COLOR_ARRAY);
1495 _mesa_BindVertexArray(clear->ArrayObj);
1496 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, clear->VBO);
1499 /* GL_COLOR_BUFFER_BIT */
1500 if (buffers & BUFFER_BITS_COLOR) {
1501 /* leave colormask, glDrawBuffer state as-is */
1503 /* Clears never have the color clamped. */
1504 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
1507 ASSERT(metaSave & MESA_META_COLOR_MASK);
1508 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1511 /* GL_DEPTH_BUFFER_BIT */
1512 if (buffers & BUFFER_BIT_DEPTH) {
1513 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE);
1514 _mesa_DepthFunc(GL_ALWAYS);
1515 _mesa_DepthMask(GL_TRUE);
1518 assert(!ctx->Depth.Test);
1521 /* GL_STENCIL_BUFFER_BIT */
1522 if (buffers & BUFFER_BIT_STENCIL) {
1523 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
1524 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK,
1525 GL_REPLACE, GL_REPLACE, GL_REPLACE);
1526 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK, GL_ALWAYS,
1527 ctx->Stencil.Clear & stencilMax,
1528 ctx->Stencil.WriteMask[0]);
1531 assert(!ctx->Stencil.Enabled);
1534 /* vertex positions/colors */
1536 const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin;
1537 const GLfloat y0 = (GLfloat) ctx->DrawBuffer->_Ymin;
1538 const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax;
1539 const GLfloat y1 = (GLfloat) ctx->DrawBuffer->_Ymax;
1540 const GLfloat z = invert_z(ctx->Depth.Clear);
1557 for (i = 0; i < 4; i++) {
1558 verts[i].r = ctx->Color.ClearColorUnclamped[0];
1559 verts[i].g = ctx->Color.ClearColorUnclamped[1];
1560 verts[i].b = ctx->Color.ClearColorUnclamped[2];
1561 verts[i].a = ctx->Color.ClearColorUnclamped[3];
1564 /* upload new vertex data */
1565 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts), verts,
1566 GL_DYNAMIC_DRAW_ARB);
1570 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1572 _mesa_meta_end(ctx);
1576 meta_glsl_clear_init(struct gl_context *ctx, struct clear_state *clear)
1578 const char *vs_source =
1579 "attribute vec4 position;\n"
1582 " gl_Position = position;\n"
1584 const char *fs_source =
1585 "uniform vec4 color;\n"
1588 " gl_FragColor = color;\n"
1592 if (clear->ArrayObj != 0)
1595 /* create vertex array object */
1596 _mesa_GenVertexArrays(1, &clear->ArrayObj);
1597 _mesa_BindVertexArray(clear->ArrayObj);
1599 /* create vertex array buffer */
1600 _mesa_GenBuffersARB(1, &clear->VBO);
1601 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, clear->VBO);
1603 /* setup vertex arrays */
1604 _mesa_VertexAttribPointerARB(0, 3, GL_FLOAT, GL_FALSE, 0, (void *)0);
1605 _mesa_EnableVertexAttribArrayARB(0);
1607 vs = _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER);
1608 _mesa_ShaderSourceARB(vs, 1, &vs_source, NULL);
1609 _mesa_CompileShaderARB(vs);
1611 fs = _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER);
1612 _mesa_ShaderSourceARB(fs, 1, &fs_source, NULL);
1613 _mesa_CompileShaderARB(fs);
1615 clear->ShaderProg = _mesa_CreateProgramObjectARB();
1616 _mesa_AttachShader(clear->ShaderProg, fs);
1617 _mesa_AttachShader(clear->ShaderProg, vs);
1618 _mesa_BindAttribLocationARB(clear->ShaderProg, 0, "position");
1619 _mesa_LinkProgramARB(clear->ShaderProg);
1621 clear->ColorLocation = _mesa_GetUniformLocationARB(clear->ShaderProg,
1626 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1629 _mesa_meta_glsl_Clear(struct gl_context *ctx, GLbitfield buffers)
1631 struct clear_state *clear = &ctx->Meta->Clear;
1632 GLbitfield metaSave;
1633 const GLuint stencilMax = (1 << ctx->DrawBuffer->Visual.stencilBits) - 1;
1634 struct gl_framebuffer *fb = ctx->DrawBuffer;
1635 const float x0 = ((float)fb->_Xmin / fb->Width) * 2.0f - 1.0f;
1636 const float y0 = ((float)fb->_Ymin / fb->Height) * 2.0f - 1.0f;
1637 const float x1 = ((float)fb->_Xmax / fb->Width) * 2.0f - 1.0f;
1638 const float y1 = ((float)fb->_Ymax / fb->Height) * 2.0f - 1.0f;
1639 const float z = -invert_z(ctx->Depth.Clear);
1644 metaSave = (MESA_META_ALPHA_TEST |
1646 MESA_META_DEPTH_TEST |
1647 MESA_META_RASTERIZATION |
1649 MESA_META_STENCIL_TEST |
1651 MESA_META_VIEWPORT |
1653 MESA_META_CLAMP_FRAGMENT_COLOR);
1655 if (!(buffers & BUFFER_BITS_COLOR)) {
1656 /* We'll use colormask to disable color writes. Otherwise,
1657 * respect color mask
1659 metaSave |= MESA_META_COLOR_MASK;
1662 _mesa_meta_begin(ctx, metaSave);
1664 meta_glsl_clear_init(ctx, clear);
1666 _mesa_UseProgramObjectARB(clear->ShaderProg);
1667 _mesa_Uniform4fvARB(clear->ColorLocation, 1,
1668 ctx->Color.ClearColorUnclamped);
1670 _mesa_BindVertexArray(clear->ArrayObj);
1671 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, clear->VBO);
1673 /* GL_COLOR_BUFFER_BIT */
1674 if (buffers & BUFFER_BITS_COLOR) {
1675 /* leave colormask, glDrawBuffer state as-is */
1677 /* Clears never have the color clamped. */
1678 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
1681 ASSERT(metaSave & MESA_META_COLOR_MASK);
1682 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1685 /* GL_DEPTH_BUFFER_BIT */
1686 if (buffers & BUFFER_BIT_DEPTH) {
1687 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE);
1688 _mesa_DepthFunc(GL_ALWAYS);
1689 _mesa_DepthMask(GL_TRUE);
1692 assert(!ctx->Depth.Test);
1695 /* GL_STENCIL_BUFFER_BIT */
1696 if (buffers & BUFFER_BIT_STENCIL) {
1697 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
1698 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK,
1699 GL_REPLACE, GL_REPLACE, GL_REPLACE);
1700 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK, GL_ALWAYS,
1701 ctx->Stencil.Clear & stencilMax,
1702 ctx->Stencil.WriteMask[0]);
1705 assert(!ctx->Stencil.Enabled);
1708 /* vertex positions */
1722 /* upload new vertex data */
1723 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts), verts,
1724 GL_DYNAMIC_DRAW_ARB);
1727 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1729 _mesa_meta_end(ctx);
1733 * Meta implementation of ctx->Driver.CopyPixels() in terms
1734 * of texture mapping and polygon rendering and GLSL shaders.
1737 _mesa_meta_CopyPixels(struct gl_context *ctx, GLint srcX, GLint srcY,
1738 GLsizei width, GLsizei height,
1739 GLint dstX, GLint dstY, GLenum type)
1741 struct copypix_state *copypix = &ctx->Meta->CopyPix;
1742 struct temp_texture *tex = get_temp_texture(ctx);
1744 GLfloat x, y, z, s, t;
1746 struct vertex verts[4];
1748 GLenum intFormat = GL_RGBA;
1750 if (type != GL_COLOR ||
1751 ctx->_ImageTransferState ||
1753 width > tex->MaxSize ||
1754 height > tex->MaxSize) {
1755 /* XXX avoid this fallback */
1756 _swrast_CopyPixels(ctx, srcX, srcY, width, height, dstX, dstY, type);
1760 /* Most GL state applies to glCopyPixels, but a there's a few things
1761 * we need to override:
1763 _mesa_meta_begin(ctx, (MESA_META_RASTERIZATION |
1766 MESA_META_TRANSFORM |
1769 MESA_META_VIEWPORT));
1771 if (copypix->ArrayObj == 0) {
1772 /* one-time setup */
1774 /* create vertex array object */
1775 _mesa_GenVertexArrays(1, ©pix->ArrayObj);
1776 _mesa_BindVertexArray(copypix->ArrayObj);
1778 /* create vertex array buffer */
1779 _mesa_GenBuffersARB(1, ©pix->VBO);
1780 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, copypix->VBO);
1781 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
1782 NULL, GL_DYNAMIC_DRAW_ARB);
1784 /* setup vertex arrays */
1785 _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
1786 _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
1787 _mesa_EnableClientState(GL_VERTEX_ARRAY);
1788 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
1791 _mesa_BindVertexArray(copypix->ArrayObj);
1792 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, copypix->VBO);
1795 newTex = alloc_texture(tex, width, height, intFormat);
1797 /* vertex positions, texcoords (after texture allocation!) */
1799 const GLfloat dstX0 = (GLfloat) dstX;
1800 const GLfloat dstY0 = (GLfloat) dstY;
1801 const GLfloat dstX1 = dstX + width * ctx->Pixel.ZoomX;
1802 const GLfloat dstY1 = dstY + height * ctx->Pixel.ZoomY;
1803 const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
1813 verts[1].s = tex->Sright;
1818 verts[2].s = tex->Sright;
1819 verts[2].t = tex->Ttop;
1824 verts[3].t = tex->Ttop;
1826 /* upload new vertex data */
1827 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1830 /* Alloc/setup texture */
1831 setup_copypix_texture(tex, newTex, srcX, srcY, width, height,
1832 GL_RGBA, GL_NEAREST);
1834 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
1836 /* draw textured quad */
1837 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1839 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
1841 _mesa_meta_end(ctx);
1847 * When the glDrawPixels() image size is greater than the max rectangle
1848 * texture size we use this function to break the glDrawPixels() image
1849 * into tiles which fit into the max texture size.
1852 tiled_draw_pixels(struct gl_context *ctx,
1854 GLint x, GLint y, GLsizei width, GLsizei height,
1855 GLenum format, GLenum type,
1856 const struct gl_pixelstore_attrib *unpack,
1857 const GLvoid *pixels)
1859 struct gl_pixelstore_attrib tileUnpack = *unpack;
1862 if (tileUnpack.RowLength == 0)
1863 tileUnpack.RowLength = width;
1865 for (i = 0; i < width; i += tileSize) {
1866 const GLint tileWidth = MIN2(tileSize, width - i);
1867 const GLint tileX = (GLint) (x + i * ctx->Pixel.ZoomX);
1869 tileUnpack.SkipPixels = unpack->SkipPixels + i;
1871 for (j = 0; j < height; j += tileSize) {
1872 const GLint tileHeight = MIN2(tileSize, height - j);
1873 const GLint tileY = (GLint) (y + j * ctx->Pixel.ZoomY);
1875 tileUnpack.SkipRows = unpack->SkipRows + j;
1877 _mesa_meta_DrawPixels(ctx, tileX, tileY, tileWidth, tileHeight,
1878 format, type, &tileUnpack, pixels);
1885 * One-time init for drawing stencil pixels.
1888 init_draw_stencil_pixels(struct gl_context *ctx)
1890 /* This program is run eight times, once for each stencil bit.
1891 * The stencil values to draw are found in an 8-bit alpha texture.
1892 * We read the texture/stencil value and test if bit 'b' is set.
1893 * If the bit is not set, use KIL to kill the fragment.
1894 * Finally, we use the stencil test to update the stencil buffer.
1896 * The basic algorithm for checking if a bit is set is:
1897 * if (is_odd(value / (1 << bit)))
1898 * result is one (or non-zero).
1901 * The program parameter contains three values:
1902 * parm.x = 255 / (1 << bit)
1906 static const char *program =
1908 "PARAM parm = program.local[0]; \n"
1910 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1911 "# t = t * 255 / bit \n"
1912 "MUL t.x, t.a, parm.x; \n"
1915 "SUB t.x, t.x, t.y; \n"
1917 "MUL t.x, t.x, parm.y; \n"
1918 "# t = fract(t.x) \n"
1919 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
1920 "# t.x = (t.x == 0 ? 1 : 0) \n"
1921 "SGE t.x, -t.x, parm.z; \n"
1923 "# for debug only \n"
1924 "#MOV result.color, t.x; \n"
1926 char program2[1000];
1927 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
1928 struct temp_texture *tex = get_temp_texture(ctx);
1929 const char *texTarget;
1931 assert(drawpix->StencilFP == 0);
1933 /* replace %s with "RECT" or "2D" */
1934 assert(strlen(program) + 4 < sizeof(program2));
1935 if (tex->Target == GL_TEXTURE_RECTANGLE)
1939 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
1941 _mesa_GenPrograms(1, &drawpix->StencilFP);
1942 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
1943 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
1944 strlen(program2), (const GLubyte *) program2);
1949 * One-time init for drawing depth pixels.
1952 init_draw_depth_pixels(struct gl_context *ctx)
1954 static const char *program =
1956 "PARAM color = program.local[0]; \n"
1957 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1958 "MOV result.color, color; \n"
1961 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
1962 struct temp_texture *tex = get_temp_texture(ctx);
1963 const char *texTarget;
1965 assert(drawpix->DepthFP == 0);
1967 /* replace %s with "RECT" or "2D" */
1968 assert(strlen(program) + 4 < sizeof(program2));
1969 if (tex->Target == GL_TEXTURE_RECTANGLE)
1973 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
1975 _mesa_GenPrograms(1, &drawpix->DepthFP);
1976 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
1977 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
1978 strlen(program2), (const GLubyte *) program2);
1983 * Meta implementation of ctx->Driver.DrawPixels() in terms
1984 * of texture mapping and polygon rendering.
1987 _mesa_meta_DrawPixels(struct gl_context *ctx,
1988 GLint x, GLint y, GLsizei width, GLsizei height,
1989 GLenum format, GLenum type,
1990 const struct gl_pixelstore_attrib *unpack,
1991 const GLvoid *pixels)
1993 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
1994 struct temp_texture *tex = get_temp_texture(ctx);
1995 const struct gl_pixelstore_attrib unpackSave = ctx->Unpack;
1996 const GLuint origStencilMask = ctx->Stencil.WriteMask[0];
1998 GLfloat x, y, z, s, t;
2000 struct vertex verts[4];
2001 GLenum texIntFormat;
2002 GLboolean fallback, newTex;
2003 GLbitfield metaExtraSave = 0x0;
2007 * Determine if we can do the glDrawPixels with texture mapping.
2009 fallback = GL_FALSE;
2010 if (ctx->_ImageTransferState ||
2015 if (_mesa_is_color_format(format)) {
2016 /* use more compact format when possible */
2017 /* XXX disable special case for GL_LUMINANCE for now to work around
2018 * apparent i965 driver bug (see bug #23670).
2020 if (/*format == GL_LUMINANCE ||*/ format == GL_LUMINANCE_ALPHA)
2021 texIntFormat = format;
2023 texIntFormat = GL_RGBA;
2025 /* If we're not supposed to clamp the resulting color, then just
2026 * promote our texture to fully float. We could do better by
2027 * just going for the matching set of channels, in floating
2030 if (ctx->Color.ClampFragmentColor != GL_TRUE &&
2031 ctx->Extensions.ARB_texture_float)
2032 texIntFormat = GL_RGBA32F;
2034 else if (_mesa_is_stencil_format(format)) {
2035 if (ctx->Extensions.ARB_fragment_program &&
2036 ctx->Pixel.IndexShift == 0 &&
2037 ctx->Pixel.IndexOffset == 0 &&
2038 type == GL_UNSIGNED_BYTE) {
2039 /* We'll store stencil as alpha. This only works for GLubyte
2040 * image data because of how incoming values are mapped to alpha
2043 texIntFormat = GL_ALPHA;
2044 metaExtraSave = (MESA_META_COLOR_MASK |
2045 MESA_META_DEPTH_TEST |
2047 MESA_META_STENCIL_TEST);
2053 else if (_mesa_is_depth_format(format)) {
2054 if (ctx->Extensions.ARB_depth_texture &&
2055 ctx->Extensions.ARB_fragment_program) {
2056 texIntFormat = GL_DEPTH_COMPONENT;
2057 metaExtraSave = (MESA_META_SHADER);
2068 _swrast_DrawPixels(ctx, x, y, width, height,
2069 format, type, unpack, pixels);
2074 * Check image size against max texture size, draw as tiles if needed.
2076 if (width > tex->MaxSize || height > tex->MaxSize) {
2077 tiled_draw_pixels(ctx, tex->MaxSize, x, y, width, height,
2078 format, type, unpack, pixels);
2082 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2083 * but a there's a few things we need to override:
2085 _mesa_meta_begin(ctx, (MESA_META_RASTERIZATION |
2088 MESA_META_TRANSFORM |
2091 MESA_META_VIEWPORT |
2092 MESA_META_CLAMP_FRAGMENT_COLOR |
2095 newTex = alloc_texture(tex, width, height, texIntFormat);
2097 /* vertex positions, texcoords (after texture allocation!) */
2099 const GLfloat x0 = (GLfloat) x;
2100 const GLfloat y0 = (GLfloat) y;
2101 const GLfloat x1 = x + width * ctx->Pixel.ZoomX;
2102 const GLfloat y1 = y + height * ctx->Pixel.ZoomY;
2103 const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
2113 verts[1].s = tex->Sright;
2118 verts[2].s = tex->Sright;
2119 verts[2].t = tex->Ttop;
2124 verts[3].t = tex->Ttop;
2127 if (drawpix->ArrayObj == 0) {
2128 /* one-time setup: create vertex array object */
2129 _mesa_GenVertexArrays(1, &drawpix->ArrayObj);
2131 _mesa_BindVertexArray(drawpix->ArrayObj);
2133 /* create vertex array buffer */
2134 _mesa_GenBuffersARB(1, &vbo);
2135 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, vbo);
2136 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
2137 verts, GL_DYNAMIC_DRAW_ARB);
2139 /* setup vertex arrays */
2140 _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
2141 _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
2142 _mesa_EnableClientState(GL_VERTEX_ARRAY);
2143 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
2145 /* set given unpack params */
2146 ctx->Unpack = *unpack;
2148 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
2150 if (_mesa_is_stencil_format(format)) {
2151 /* Drawing stencil */
2154 if (!drawpix->StencilFP)
2155 init_draw_stencil_pixels(ctx);
2157 setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height,
2158 GL_ALPHA, type, pixels);
2160 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
2162 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
2164 /* set all stencil bits to 0 */
2165 _mesa_StencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
2166 _mesa_StencilFunc(GL_ALWAYS, 0, 255);
2167 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2169 /* set stencil bits to 1 where needed */
2170 _mesa_StencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
2172 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
2173 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
2175 for (bit = 0; bit < ctx->DrawBuffer->Visual.stencilBits; bit++) {
2176 const GLuint mask = 1 << bit;
2177 if (mask & origStencilMask) {
2178 _mesa_StencilFunc(GL_ALWAYS, mask, mask);
2179 _mesa_StencilMask(mask);
2181 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0,
2182 255.0 / mask, 0.5, 0.0, 0.0);
2184 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2188 else if (_mesa_is_depth_format(format)) {
2190 if (!drawpix->DepthFP)
2191 init_draw_depth_pixels(ctx);
2193 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
2194 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
2196 /* polygon color = current raster color */
2197 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0,
2198 ctx->Current.RasterColor);
2200 setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height,
2201 format, type, pixels);
2203 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2207 setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height,
2208 format, type, pixels);
2209 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2212 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
2214 _mesa_DeleteBuffersARB(1, &vbo);
2216 /* restore unpack params */
2217 ctx->Unpack = unpackSave;
2219 _mesa_meta_end(ctx);
2223 alpha_test_raster_color(struct gl_context *ctx)
2225 GLfloat alpha = ctx->Current.RasterColor[ACOMP];
2226 GLfloat ref = ctx->Color.AlphaRef;
2228 switch (ctx->Color.AlphaFunc) {
2234 return alpha == ref;
2236 return alpha <= ref;
2240 return alpha != ref;
2242 return alpha >= ref;
2252 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2253 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2254 * tracker would improve performance a lot.
2257 _mesa_meta_Bitmap(struct gl_context *ctx,
2258 GLint x, GLint y, GLsizei width, GLsizei height,
2259 const struct gl_pixelstore_attrib *unpack,
2260 const GLubyte *bitmap1)
2262 struct bitmap_state *bitmap = &ctx->Meta->Bitmap;
2263 struct temp_texture *tex = get_bitmap_temp_texture(ctx);
2264 const GLenum texIntFormat = GL_ALPHA;
2265 const struct gl_pixelstore_attrib unpackSave = *unpack;
2268 GLfloat x, y, z, s, t, r, g, b, a;
2270 struct vertex verts[4];
2275 * Check if swrast fallback is needed.
2277 if (ctx->_ImageTransferState ||
2278 ctx->FragmentProgram._Enabled ||
2280 ctx->Texture._EnabledUnits ||
2281 width > tex->MaxSize ||
2282 height > tex->MaxSize) {
2283 _swrast_Bitmap(ctx, x, y, width, height, unpack, bitmap1);
2287 if (ctx->Color.AlphaEnabled && !alpha_test_raster_color(ctx))
2290 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2291 * but a there's a few things we need to override:
2293 _mesa_meta_begin(ctx, (MESA_META_ALPHA_TEST |
2294 MESA_META_PIXEL_STORE |
2295 MESA_META_RASTERIZATION |
2298 MESA_META_TRANSFORM |
2301 MESA_META_VIEWPORT));
2303 if (bitmap->ArrayObj == 0) {
2304 /* one-time setup */
2306 /* create vertex array object */
2307 _mesa_GenVertexArraysAPPLE(1, &bitmap->ArrayObj);
2308 _mesa_BindVertexArrayAPPLE(bitmap->ArrayObj);
2310 /* create vertex array buffer */
2311 _mesa_GenBuffersARB(1, &bitmap->VBO);
2312 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, bitmap->VBO);
2313 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
2314 NULL, GL_DYNAMIC_DRAW_ARB);
2316 /* setup vertex arrays */
2317 _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
2318 _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
2319 _mesa_ColorPointer(4, GL_FLOAT, sizeof(struct vertex), OFFSET(r));
2320 _mesa_EnableClientState(GL_VERTEX_ARRAY);
2321 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
2322 _mesa_EnableClientState(GL_COLOR_ARRAY);
2325 _mesa_BindVertexArray(bitmap->ArrayObj);
2326 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, bitmap->VBO);
2329 newTex = alloc_texture(tex, width, height, texIntFormat);
2331 /* vertex positions, texcoords, colors (after texture allocation!) */
2333 const GLfloat x0 = (GLfloat) x;
2334 const GLfloat y0 = (GLfloat) y;
2335 const GLfloat x1 = (GLfloat) (x + width);
2336 const GLfloat y1 = (GLfloat) (y + height);
2337 const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
2348 verts[1].s = tex->Sright;
2353 verts[2].s = tex->Sright;
2354 verts[2].t = tex->Ttop;
2359 verts[3].t = tex->Ttop;
2361 for (i = 0; i < 4; i++) {
2362 verts[i].r = ctx->Current.RasterColor[0];
2363 verts[i].g = ctx->Current.RasterColor[1];
2364 verts[i].b = ctx->Current.RasterColor[2];
2365 verts[i].a = ctx->Current.RasterColor[3];
2368 /* upload new vertex data */
2369 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
2372 /* choose different foreground/background alpha values */
2373 CLAMPED_FLOAT_TO_UBYTE(fg, ctx->Current.RasterColor[ACOMP]);
2374 bg = (fg > 127 ? 0 : 255);
2376 bitmap1 = _mesa_map_pbo_source(ctx, &unpackSave, bitmap1);
2378 _mesa_meta_end(ctx);
2382 bitmap8 = (GLubyte *) malloc(width * height);
2384 memset(bitmap8, bg, width * height);
2385 _mesa_expand_bitmap(width, height, &unpackSave, bitmap1,
2386 bitmap8, width, fg);
2388 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
2390 _mesa_set_enable(ctx, GL_ALPHA_TEST, GL_TRUE);
2391 _mesa_AlphaFunc(GL_NOTEQUAL, UBYTE_TO_FLOAT(bg));
2393 setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height,
2394 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap8);
2396 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2398 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
2403 _mesa_unmap_pbo_source(ctx, &unpackSave);
2405 _mesa_meta_end(ctx);
2410 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2411 * software fallback. The fallback path will require that the texture
2412 * images are mapped.
2413 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2416 _mesa_meta_check_generate_mipmap_fallback(struct gl_context *ctx, GLenum target,
2417 struct gl_texture_object *texObj)
2419 const GLuint fboSave = ctx->DrawBuffer->Name;
2420 struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap;
2421 struct gl_texture_image *baseImage;
2425 /* check for fallbacks */
2426 if (!ctx->Extensions.EXT_framebuffer_object ||
2427 target == GL_TEXTURE_3D) {
2431 srcLevel = texObj->BaseLevel;
2432 baseImage = _mesa_select_tex_image(ctx, texObj, target, srcLevel);
2433 if (!baseImage || _mesa_is_format_compressed(baseImage->TexFormat)) {
2438 * Test that we can actually render in the texture's format.
2441 _mesa_GenFramebuffersEXT(1, &mipmap->FBO);
2442 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, mipmap->FBO);
2444 if (target == GL_TEXTURE_1D) {
2445 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT,
2446 GL_COLOR_ATTACHMENT0_EXT,
2447 target, texObj->Name, srcLevel);
2450 /* other work is needed to enable 3D mipmap generation */
2451 else if (target == GL_TEXTURE_3D) {
2453 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT,
2454 GL_COLOR_ATTACHMENT0_EXT,
2455 target, texObj->Name, srcLevel, zoffset);
2460 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
2461 GL_COLOR_ATTACHMENT0_EXT,
2462 target, texObj->Name, srcLevel);
2465 status = _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
2467 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboSave);
2469 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2478 * Called via ctx->Driver.GenerateMipmap()
2479 * Note: texture borders and 3D texture support not yet complete.
2482 _mesa_meta_GenerateMipmap(struct gl_context *ctx, GLenum target,
2483 struct gl_texture_object *texObj)
2485 struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap;
2487 GLfloat x, y, s, t, r;
2489 struct vertex verts[4];
2490 const GLuint baseLevel = texObj->BaseLevel;
2491 const GLuint maxLevel = texObj->MaxLevel;
2492 const GLenum minFilterSave = texObj->Sampler.MinFilter;
2493 const GLenum magFilterSave = texObj->Sampler.MagFilter;
2494 const GLint maxLevelSave = texObj->MaxLevel;
2495 const GLboolean genMipmapSave = texObj->GenerateMipmap;
2496 const GLenum wrapSSave = texObj->Sampler.WrapS;
2497 const GLenum wrapTSave = texObj->Sampler.WrapT;
2498 const GLenum wrapRSave = texObj->Sampler.WrapR;
2499 const GLuint fboSave = ctx->DrawBuffer->Name;
2500 const GLuint original_active_unit = ctx->Texture.CurrentUnit;
2505 if (_mesa_meta_check_generate_mipmap_fallback(ctx, target, texObj)) {
2506 _mesa_generate_mipmap(ctx, target, texObj);
2510 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
2511 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) {
2512 faceTarget = target;
2513 target = GL_TEXTURE_CUBE_MAP;
2516 faceTarget = target;
2519 _mesa_meta_begin(ctx, MESA_META_ALL);
2521 if (original_active_unit != 0)
2522 _mesa_BindTexture(target, texObj->Name);
2524 if (mipmap->ArrayObj == 0) {
2525 /* one-time setup */
2527 /* create vertex array object */
2528 _mesa_GenVertexArraysAPPLE(1, &mipmap->ArrayObj);
2529 _mesa_BindVertexArrayAPPLE(mipmap->ArrayObj);
2531 /* create vertex array buffer */
2532 _mesa_GenBuffersARB(1, &mipmap->VBO);
2533 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
2534 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
2535 NULL, GL_DYNAMIC_DRAW_ARB);
2537 /* setup vertex arrays */
2538 _mesa_VertexPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
2539 _mesa_TexCoordPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
2540 _mesa_EnableClientState(GL_VERTEX_ARRAY);
2541 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
2544 _mesa_BindVertexArray(mipmap->ArrayObj);
2545 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
2549 _mesa_GenFramebuffersEXT(1, &mipmap->FBO);
2551 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, mipmap->FBO);
2553 _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
2554 _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2555 _mesa_TexParameteri(target, GL_GENERATE_MIPMAP, GL_FALSE);
2556 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2557 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2558 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
2560 _mesa_set_enable(ctx, target, GL_TRUE);
2562 /* setup texcoords once (XXX what about border?) */
2563 switch (faceTarget) {
2585 static const GLfloat st[4][2] = {
2586 {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}
2590 /* loop over quad verts */
2591 for (i = 0; i < 4; i++) {
2592 /* Compute sc = +/-scale and tc = +/-scale.
2593 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2594 * though that can still sometimes happen with this scale factor...
2596 const GLfloat scale = 0.9999f;
2597 const GLfloat sc = (2.0f * st[i][0] - 1.0f) * scale;
2598 const GLfloat tc = (2.0f * st[i][1] - 1.0f) * scale;
2600 switch (faceTarget) {
2601 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2606 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2611 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2616 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2621 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2626 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2638 _mesa_set_enable(ctx, target, GL_TRUE);
2640 /* setup vertex positions */
2651 /* upload new vertex data */
2652 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
2655 /* setup projection matrix */
2656 _mesa_MatrixMode(GL_PROJECTION);
2657 _mesa_LoadIdentity();
2658 _mesa_Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
2660 /* texture is already locked, unlock now */
2661 _mesa_unlock_texture(ctx, texObj);
2663 for (dstLevel = baseLevel + 1; dstLevel <= maxLevel; dstLevel++) {
2664 const struct gl_texture_image *srcImage;
2665 const GLuint srcLevel = dstLevel - 1;
2666 GLsizei srcWidth, srcHeight, srcDepth;
2667 GLsizei dstWidth, dstHeight, dstDepth;
2670 srcImage = _mesa_select_tex_image(ctx, texObj, faceTarget, srcLevel);
2671 assert(srcImage->Border == 0); /* XXX we can fix this */
2673 /* src size w/out border */
2674 srcWidth = srcImage->Width - 2 * border;
2675 srcHeight = srcImage->Height - 2 * border;
2676 srcDepth = srcImage->Depth - 2 * border;
2678 /* new dst size w/ border */
2679 dstWidth = MAX2(1, srcWidth / 2) + 2 * border;
2680 dstHeight = MAX2(1, srcHeight / 2) + 2 * border;
2681 dstDepth = MAX2(1, srcDepth / 2) + 2 * border;
2683 if (dstWidth == srcImage->Width &&
2684 dstHeight == srcImage->Height &&
2685 dstDepth == srcImage->Depth) {
2690 /* Set MaxLevel large enough to hold the new level when we allocate it */
2691 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, dstLevel);
2693 /* Create empty dest image */
2694 if (target == GL_TEXTURE_1D) {
2695 _mesa_TexImage1D(target, dstLevel, srcImage->InternalFormat,
2697 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2699 else if (target == GL_TEXTURE_3D) {
2700 _mesa_TexImage3D(target, dstLevel, srcImage->InternalFormat,
2701 dstWidth, dstHeight, dstDepth, border,
2702 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2706 _mesa_TexImage2D(faceTarget, dstLevel, srcImage->InternalFormat,
2707 dstWidth, dstHeight, border,
2708 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2710 if (target == GL_TEXTURE_CUBE_MAP) {
2711 /* If texturing from a cube, we need to make sure all src faces
2712 * have been defined (even if we're not sampling from them.)
2713 * Otherwise the texture object will be 'incomplete' and
2714 * texturing from it will not be allowed.
2717 for (face = 0; face < 6; face++) {
2718 if (!texObj->Image[face][srcLevel] ||
2719 texObj->Image[face][srcLevel]->Width != srcWidth) {
2720 _mesa_TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face,
2721 srcLevel, srcImage->InternalFormat,
2722 srcWidth, srcHeight, border,
2723 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2729 /* limit minification to src level */
2730 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, srcLevel);
2732 /* Set to draw into the current dstLevel */
2733 if (target == GL_TEXTURE_1D) {
2734 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT,
2735 GL_COLOR_ATTACHMENT0_EXT,
2740 else if (target == GL_TEXTURE_3D) {
2741 GLint zoffset = 0; /* XXX unfinished */
2742 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT,
2743 GL_COLOR_ATTACHMENT0_EXT,
2750 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
2751 GL_COLOR_ATTACHMENT0_EXT,
2757 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
2760 status = _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
2761 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2766 assert(dstWidth == ctx->DrawBuffer->Width);
2767 assert(dstHeight == ctx->DrawBuffer->Height);
2769 /* setup viewport */
2770 _mesa_set_viewport(ctx, 0, 0, dstWidth, dstHeight);
2772 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2775 _mesa_lock_texture(ctx, texObj); /* relock */
2777 _mesa_meta_end(ctx);
2779 _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilterSave);
2780 _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilterSave);
2781 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, maxLevelSave);
2782 _mesa_TexParameteri(target, GL_GENERATE_MIPMAP, genMipmapSave);
2783 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, wrapSSave);
2784 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, wrapTSave);
2785 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_R, wrapRSave);
2787 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboSave);
2792 * Determine the GL data type to use for the temporary image read with
2793 * ReadPixels() and passed to Tex[Sub]Image().
2796 get_temp_image_type(struct gl_context *ctx, GLenum baseFormat)
2798 switch (baseFormat) {
2803 case GL_LUMINANCE_ALPHA:
2805 if (ctx->DrawBuffer->Visual.redBits <= 8)
2806 return GL_UNSIGNED_BYTE;
2807 else if (ctx->DrawBuffer->Visual.redBits <= 8)
2808 return GL_UNSIGNED_SHORT;
2811 case GL_DEPTH_COMPONENT:
2812 return GL_UNSIGNED_INT;
2813 case GL_DEPTH_STENCIL:
2814 return GL_UNSIGNED_INT_24_8;
2816 _mesa_problem(ctx, "Unexpected format in get_temp_image_type()");
2823 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2824 * Have to be careful with locking and meta state for pixel transfer.
2827 copy_tex_sub_image(struct gl_context *ctx,
2828 GLuint dims, GLenum target, GLint level,
2829 GLint xoffset, GLint yoffset, GLint zoffset,
2831 GLsizei width, GLsizei height)
2833 struct gl_texture_object *texObj;
2834 struct gl_texture_image *texImage;
2835 GLenum format, type;
2839 texObj = _mesa_get_current_tex_object(ctx, target);
2840 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2842 /* Choose format/type for temporary image buffer */
2843 format = _mesa_get_format_base_format(texImage->TexFormat);
2844 if (format == GL_LUMINANCE ||
2845 format == GL_LUMINANCE_ALPHA ||
2846 format == GL_INTENSITY) {
2847 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
2848 * temp image buffer because glReadPixels will do L=R+G+B which is
2849 * not what we want (should be L=R).
2854 type = get_temp_image_type(ctx, format);
2855 bpp = _mesa_bytes_per_pixel(format, type);
2857 _mesa_problem(ctx, "Bad bpp in meta copy_tex_sub_image()");
2862 * Alloc image buffer (XXX could use a PBO)
2864 buf = malloc(width * height * bpp);
2866 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage%uD", dims);
2870 _mesa_unlock_texture(ctx, texObj); /* need to unlock first */
2873 * Read image from framebuffer (disable pixel transfer ops)
2875 _mesa_meta_begin(ctx, MESA_META_PIXEL_STORE | MESA_META_PIXEL_TRANSFER);
2876 ctx->Driver.ReadPixels(ctx, x, y, width, height,
2877 format, type, &ctx->Pack, buf);
2878 _mesa_meta_end(ctx);
2880 _mesa_update_state(ctx); /* to update pixel transfer state */
2883 * Store texture data (with pixel transfer ops)
2885 _mesa_meta_begin(ctx, MESA_META_PIXEL_STORE);
2886 if (target == GL_TEXTURE_1D) {
2887 ctx->Driver.TexSubImage1D(ctx, target, level, xoffset,
2888 width, format, type, buf,
2889 &ctx->Unpack, texObj, texImage);
2891 else if (target == GL_TEXTURE_3D) {
2892 ctx->Driver.TexSubImage3D(ctx, target, level, xoffset, yoffset, zoffset,
2893 width, height, 1, format, type, buf,
2894 &ctx->Unpack, texObj, texImage);
2897 ctx->Driver.TexSubImage2D(ctx, target, level, xoffset, yoffset,
2898 width, height, format, type, buf,
2899 &ctx->Unpack, texObj, texImage);
2901 _mesa_meta_end(ctx);
2903 _mesa_lock_texture(ctx, texObj); /* re-lock */
2910 _mesa_meta_CopyTexSubImage1D(struct gl_context *ctx, GLenum target, GLint level,
2912 GLint x, GLint y, GLsizei width)
2914 copy_tex_sub_image(ctx, 1, target, level, xoffset, 0, 0,
2920 _mesa_meta_CopyTexSubImage2D(struct gl_context *ctx, GLenum target, GLint level,
2921 GLint xoffset, GLint yoffset,
2923 GLsizei width, GLsizei height)
2925 copy_tex_sub_image(ctx, 2, target, level, xoffset, yoffset, 0,
2926 x, y, width, height);
2931 _mesa_meta_CopyTexSubImage3D(struct gl_context *ctx, GLenum target, GLint level,
2932 GLint xoffset, GLint yoffset, GLint zoffset,
2934 GLsizei width, GLsizei height)
2936 copy_tex_sub_image(ctx, 3, target, level, xoffset, yoffset, zoffset,
2937 x, y, width, height);
2942 _mesa_meta_CopyColorTable(struct gl_context *ctx,
2943 GLenum target, GLenum internalformat,
2944 GLint x, GLint y, GLsizei width)
2948 buf = (GLfloat *) malloc(width * 4 * sizeof(GLfloat));
2950 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyColorTable");
2955 * Read image from framebuffer (disable pixel transfer ops)
2957 _mesa_meta_begin(ctx, MESA_META_PIXEL_STORE | MESA_META_PIXEL_TRANSFER);
2958 ctx->Driver.ReadPixels(ctx, x, y, width, 1,
2959 GL_RGBA, GL_FLOAT, &ctx->Pack, buf);
2961 _mesa_ColorTable(target, internalformat, width, GL_RGBA, GL_FLOAT, buf);
2963 _mesa_meta_end(ctx);
2970 _mesa_meta_CopyColorSubTable(struct gl_context *ctx,GLenum target, GLsizei start,
2971 GLint x, GLint y, GLsizei width)
2975 buf = (GLfloat *) malloc(width * 4 * sizeof(GLfloat));
2977 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyColorSubTable");
2982 * Read image from framebuffer (disable pixel transfer ops)
2984 _mesa_meta_begin(ctx, MESA_META_PIXEL_STORE | MESA_META_PIXEL_TRANSFER);
2985 ctx->Driver.ReadPixels(ctx, x, y, width, 1,
2986 GL_RGBA, GL_FLOAT, &ctx->Pack, buf);
2988 _mesa_ColorSubTable(target, start, width, GL_RGBA, GL_FLOAT, buf);
2990 _mesa_meta_end(ctx);