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/varray.h"
66 #include "main/viewport.h"
67 #include "program/program.h"
68 #include "swrast/swrast.h"
69 #include "drivers/common/meta.h"
72 /** Return offset in bytes of the field within a vertex struct */
73 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
77 * Flags passed to _mesa_meta_begin().
81 #define META_ALPHA_TEST 0x1
82 #define META_BLEND 0x2 /**< includes logicop */
83 #define META_COLOR_MASK 0x4
84 #define META_DEPTH_TEST 0x8
86 #define META_PIXEL_STORE 0x20
87 #define META_PIXEL_TRANSFER 0x40
88 #define META_RASTERIZATION 0x80
89 #define META_SCISSOR 0x100
90 #define META_SHADER 0x200
91 #define META_STENCIL_TEST 0x400
92 #define META_TRANSFORM 0x800 /**< modelview, projection, clip planes */
93 #define META_TEXTURE 0x1000
94 #define META_VERTEX 0x2000
95 #define META_VIEWPORT 0x4000
96 #define META_CLAMP_FRAGMENT_COLOR 0x8000
97 #define META_CLAMP_VERTEX_COLOR 0x10000
98 #define META_CONDITIONAL_RENDER 0x20000
103 * State which we may save/restore across meta ops.
104 * XXX this may be incomplete...
108 GLbitfield SavedState
; /**< bitmask of META_* flags */
110 /** META_ALPHA_TEST */
111 GLboolean AlphaEnabled
;
116 GLbitfield BlendEnabled
;
117 GLboolean ColorLogicOpEnabled
;
119 /** META_COLOR_MASK */
120 GLubyte ColorMask
[MAX_DRAW_BUFFERS
][4];
122 /** META_DEPTH_TEST */
123 struct gl_depthbuffer_attrib Depth
;
128 /** META_PIXEL_STORE */
129 struct gl_pixelstore_attrib Pack
, Unpack
;
131 /** META_PIXEL_TRANSFER */
132 GLfloat RedBias
, RedScale
;
133 GLfloat GreenBias
, GreenScale
;
134 GLfloat BlueBias
, BlueScale
;
135 GLfloat AlphaBias
, AlphaScale
;
136 GLfloat DepthBias
, DepthScale
;
137 GLboolean MapColorFlag
;
139 /** META_RASTERIZATION */
140 GLenum FrontPolygonMode
, BackPolygonMode
;
141 GLboolean PolygonOffset
;
142 GLboolean PolygonSmooth
;
143 GLboolean PolygonStipple
;
144 GLboolean PolygonCull
;
147 struct gl_scissor_attrib Scissor
;
150 GLboolean VertexProgramEnabled
;
151 struct gl_vertex_program
*VertexProgram
;
152 GLboolean FragmentProgramEnabled
;
153 struct gl_fragment_program
*FragmentProgram
;
154 struct gl_shader_program
*VertexShader
;
155 struct gl_shader_program
*GeometryShader
;
156 struct gl_shader_program
*FragmentShader
;
157 struct gl_shader_program
*ActiveShader
;
159 /** META_STENCIL_TEST */
160 struct gl_stencil_attrib Stencil
;
162 /** META_TRANSFORM */
164 GLfloat ModelviewMatrix
[16];
165 GLfloat ProjectionMatrix
[16];
166 GLfloat TextureMatrix
[16];
167 GLbitfield ClipPlanesEnabled
;
171 GLuint ClientActiveUnit
;
172 /** for unit[0] only */
173 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
174 /** mask of TEXTURE_2D_BIT, etc */
175 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
176 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
177 GLuint EnvMode
; /* unit[0] only */
180 struct gl_array_object
*ArrayObj
;
181 struct gl_buffer_object
*ArrayBufferObj
;
184 GLint ViewportX
, ViewportY
, ViewportW
, ViewportH
;
185 GLclampd DepthNear
, DepthFar
;
187 /** META_CLAMP_FRAGMENT_COLOR */
188 GLenum ClampFragmentColor
;
190 /** META_CLAMP_VERTEX_COLOR */
191 GLenum ClampVertexColor
;
193 /** META_CONDITIONAL_RENDER */
194 struct gl_query_object
*CondRenderQuery
;
195 GLenum CondRenderMode
;
197 /** Miscellaneous (always disabled) */
203 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
204 * This is currently shared by all the meta ops. But we could create a
205 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
210 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
211 GLsizei MinSize
; /**< Min texture size to allocate */
212 GLsizei MaxSize
; /**< Max possible texture size */
213 GLboolean NPOT
; /**< Non-power of two size OK? */
214 GLsizei Width
, Height
; /**< Current texture size */
216 GLfloat Sright
, Ttop
; /**< right, top texcoords */
221 * State for glBlitFramebufer()
232 * State for glClear()
242 * State for glCopyPixels()
252 * State for glDrawPixels()
258 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
259 GLuint DepthFP
; /**< Fragment program for drawing depth images */
264 * State for glBitmap()
270 struct temp_texture Tex
; /**< separate texture from other meta ops */
275 * State for _mesa_meta_generate_mipmap()
277 struct gen_mipmap_state
284 #define MAX_META_OPS_DEPTH 2
286 * All per-context meta state.
290 /** Stack of state saved during meta-ops */
291 struct save_state Save
[MAX_META_OPS_DEPTH
];
292 /** Save stack depth */
293 GLuint SaveStackDepth
;
295 struct temp_texture TempTex
;
297 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
298 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
299 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
300 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
301 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
302 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
307 * Initialize meta-ops for a context.
308 * To be called once during context creation.
311 _mesa_meta_init(struct gl_context
*ctx
)
315 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
320 * Free context meta-op state.
321 * To be called once during context destruction.
324 _mesa_meta_free(struct gl_context
*ctx
)
326 /* Note: Any textures, VBOs, etc, that we allocate should get
327 * freed by the normal context destruction code. But this would be
328 * the place to free other meta data someday.
336 * Enter meta state. This is like a light-weight version of glPushAttrib
337 * but it also resets most GL state back to default values.
339 * \param state bitmask of META_* flags indicating which attribute groups
340 * to save and reset to their defaults
343 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
345 struct save_state
*save
;
347 /* hope MAX_META_OPS_DEPTH is large enough */
348 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
350 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
351 memset(save
, 0, sizeof(*save
));
352 save
->SavedState
= state
;
354 if (state
& META_ALPHA_TEST
) {
355 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
356 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
357 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
358 if (ctx
->Color
.AlphaEnabled
)
359 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
362 if (state
& META_BLEND
) {
363 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
364 if (ctx
->Color
.BlendEnabled
) {
365 if (ctx
->Extensions
.EXT_draw_buffers2
) {
367 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
368 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
372 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
375 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
376 if (ctx
->Color
.ColorLogicOpEnabled
)
377 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
380 if (state
& META_COLOR_MASK
) {
381 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
382 sizeof(ctx
->Color
.ColorMask
));
383 if (!ctx
->Color
.ColorMask
[0][0] ||
384 !ctx
->Color
.ColorMask
[0][1] ||
385 !ctx
->Color
.ColorMask
[0][2] ||
386 !ctx
->Color
.ColorMask
[0][3])
387 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
390 if (state
& META_DEPTH_TEST
) {
391 save
->Depth
= ctx
->Depth
; /* struct copy */
393 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
396 if (state
& META_FOG
) {
397 save
->Fog
= ctx
->Fog
.Enabled
;
398 if (ctx
->Fog
.Enabled
)
399 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
402 if (state
& META_PIXEL_STORE
) {
403 save
->Pack
= ctx
->Pack
;
404 save
->Unpack
= ctx
->Unpack
;
405 ctx
->Pack
= ctx
->DefaultPacking
;
406 ctx
->Unpack
= ctx
->DefaultPacking
;
409 if (state
& META_PIXEL_TRANSFER
) {
410 save
->RedScale
= ctx
->Pixel
.RedScale
;
411 save
->RedBias
= ctx
->Pixel
.RedBias
;
412 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
413 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
414 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
415 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
416 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
417 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
418 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
419 ctx
->Pixel
.RedScale
= 1.0F
;
420 ctx
->Pixel
.RedBias
= 0.0F
;
421 ctx
->Pixel
.GreenScale
= 1.0F
;
422 ctx
->Pixel
.GreenBias
= 0.0F
;
423 ctx
->Pixel
.BlueScale
= 1.0F
;
424 ctx
->Pixel
.BlueBias
= 0.0F
;
425 ctx
->Pixel
.AlphaScale
= 1.0F
;
426 ctx
->Pixel
.AlphaBias
= 0.0F
;
427 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
429 ctx
->NewState
|=_NEW_PIXEL
;
432 if (state
& META_RASTERIZATION
) {
433 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
434 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
435 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
436 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
437 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
438 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
439 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
440 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
441 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
442 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
443 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
446 if (state
& META_SCISSOR
) {
447 save
->Scissor
= ctx
->Scissor
; /* struct copy */
448 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
451 if (state
& META_SHADER
) {
452 if (ctx
->Extensions
.ARB_vertex_program
) {
453 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
454 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
455 ctx
->VertexProgram
.Current
);
456 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
459 if (ctx
->Extensions
.ARB_fragment_program
) {
460 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
461 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
462 ctx
->FragmentProgram
.Current
);
463 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
466 if (ctx
->Extensions
.ARB_shader_objects
) {
467 _mesa_reference_shader_program(ctx
, &save
->VertexShader
,
468 ctx
->Shader
.CurrentVertexProgram
);
469 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
,
470 ctx
->Shader
.CurrentGeometryProgram
);
471 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
,
472 ctx
->Shader
.CurrentFragmentProgram
);
473 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
474 ctx
->Shader
.CurrentFragmentProgram
);
476 _mesa_UseProgramObjectARB(0);
480 if (state
& META_STENCIL_TEST
) {
481 save
->Stencil
= ctx
->Stencil
; /* struct copy */
482 if (ctx
->Stencil
.Enabled
)
483 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
484 /* NOTE: other stencil state not reset */
487 if (state
& META_TEXTURE
) {
490 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
491 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
492 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
494 /* Disable all texture units */
495 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
496 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
497 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
498 if (ctx
->Texture
.Unit
[u
].Enabled
||
499 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
500 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
501 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
502 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
503 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
504 if (ctx
->Extensions
.ARB_texture_cube_map
)
505 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
506 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
507 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
508 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
509 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
510 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
514 /* save current texture objects for unit[0] only */
515 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
516 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
517 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
520 /* set defaults for unit[0] */
521 _mesa_ActiveTextureARB(GL_TEXTURE0
);
522 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
523 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
526 if (state
& META_TRANSFORM
) {
527 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
528 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
529 16 * sizeof(GLfloat
));
530 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
531 16 * sizeof(GLfloat
));
532 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
533 16 * sizeof(GLfloat
));
534 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
535 /* set 1:1 vertex:pixel coordinate transform */
536 _mesa_ActiveTextureARB(GL_TEXTURE0
);
537 _mesa_MatrixMode(GL_TEXTURE
);
538 _mesa_LoadIdentity();
539 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
540 _mesa_MatrixMode(GL_MODELVIEW
);
541 _mesa_LoadIdentity();
542 _mesa_MatrixMode(GL_PROJECTION
);
543 _mesa_LoadIdentity();
544 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
545 0.0, ctx
->DrawBuffer
->Height
,
547 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
548 if (ctx
->Transform
.ClipPlanesEnabled
) {
550 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
551 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
556 if (state
& META_VERTEX
) {
557 /* save vertex array object state */
558 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
559 ctx
->Array
.ArrayObj
);
560 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
561 ctx
->Array
.ArrayBufferObj
);
562 /* set some default state? */
565 if (state
& META_VIEWPORT
) {
566 /* save viewport state */
567 save
->ViewportX
= ctx
->Viewport
.X
;
568 save
->ViewportY
= ctx
->Viewport
.Y
;
569 save
->ViewportW
= ctx
->Viewport
.Width
;
570 save
->ViewportH
= ctx
->Viewport
.Height
;
571 /* set viewport to match window size */
572 if (ctx
->Viewport
.X
!= 0 ||
573 ctx
->Viewport
.Y
!= 0 ||
574 ctx
->Viewport
.Width
!= ctx
->DrawBuffer
->Width
||
575 ctx
->Viewport
.Height
!= ctx
->DrawBuffer
->Height
) {
576 _mesa_set_viewport(ctx
, 0, 0,
577 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
579 /* save depth range state */
580 save
->DepthNear
= ctx
->Viewport
.Near
;
581 save
->DepthFar
= ctx
->Viewport
.Far
;
582 /* set depth range to default */
583 _mesa_DepthRange(0.0, 1.0);
586 if (state
& META_CLAMP_FRAGMENT_COLOR
) {
587 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
589 /* Generally in here we want to do clamping according to whether
590 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
591 * regardless of the internal implementation of the metaops.
593 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
)
594 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
597 if (state
& META_CLAMP_VERTEX_COLOR
) {
598 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
600 /* Generally in here we never want vertex color clamping --
601 * result clamping is only dependent on fragment clamping.
603 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
606 if (state
& META_CONDITIONAL_RENDER
) {
607 save
->CondRenderQuery
= ctx
->Query
.CondRenderQuery
;
608 save
->CondRenderMode
= ctx
->Query
.CondRenderMode
;
610 if (ctx
->Query
.CondRenderQuery
)
611 _mesa_EndConditionalRender();
616 save
->Lighting
= ctx
->Light
.Enabled
;
617 if (ctx
->Light
.Enabled
)
618 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
624 * Leave meta state. This is like a light-weight version of glPopAttrib().
627 _mesa_meta_end(struct gl_context
*ctx
)
629 struct save_state
*save
= &ctx
->Meta
->Save
[--ctx
->Meta
->SaveStackDepth
];
630 const GLbitfield state
= save
->SavedState
;
632 if (state
& META_ALPHA_TEST
) {
633 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
634 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
635 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
638 if (state
& META_BLEND
) {
639 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
640 if (ctx
->Extensions
.EXT_draw_buffers2
) {
642 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
643 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
647 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
650 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
651 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
654 if (state
& META_COLOR_MASK
) {
656 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
657 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
659 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
660 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
663 _mesa_ColorMaskIndexed(i
,
664 save
->ColorMask
[i
][0],
665 save
->ColorMask
[i
][1],
666 save
->ColorMask
[i
][2],
667 save
->ColorMask
[i
][3]);
673 if (state
& META_DEPTH_TEST
) {
674 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
675 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
676 _mesa_DepthFunc(save
->Depth
.Func
);
677 _mesa_DepthMask(save
->Depth
.Mask
);
680 if (state
& META_FOG
) {
681 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
684 if (state
& META_PIXEL_STORE
) {
685 ctx
->Pack
= save
->Pack
;
686 ctx
->Unpack
= save
->Unpack
;
689 if (state
& META_PIXEL_TRANSFER
) {
690 ctx
->Pixel
.RedScale
= save
->RedScale
;
691 ctx
->Pixel
.RedBias
= save
->RedBias
;
692 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
693 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
694 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
695 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
696 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
697 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
698 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
700 ctx
->NewState
|=_NEW_PIXEL
;
703 if (state
& META_RASTERIZATION
) {
704 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
705 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
706 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
707 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
708 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
709 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
712 if (state
& META_SCISSOR
) {
713 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
714 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
715 save
->Scissor
.Width
, save
->Scissor
.Height
);
718 if (state
& META_SHADER
) {
719 if (ctx
->Extensions
.ARB_vertex_program
) {
720 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
721 save
->VertexProgramEnabled
);
722 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
723 save
->VertexProgram
);
724 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
727 if (ctx
->Extensions
.ARB_fragment_program
) {
728 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
729 save
->FragmentProgramEnabled
);
730 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
731 save
->FragmentProgram
);
732 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
735 if (ctx
->Extensions
.ARB_vertex_shader
)
736 _mesa_use_shader_program(ctx
, GL_VERTEX_SHADER
, save
->VertexShader
);
738 if (ctx
->Extensions
.ARB_geometry_shader4
)
739 _mesa_use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
,
740 save
->GeometryShader
);
742 if (ctx
->Extensions
.ARB_fragment_shader
)
743 _mesa_use_shader_program(ctx
, GL_FRAGMENT_SHADER
,
744 save
->FragmentShader
);
746 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
750 if (state
& META_STENCIL_TEST
) {
751 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
753 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
754 _mesa_ClearStencil(stencil
->Clear
);
755 if (ctx
->Extensions
.EXT_stencil_two_side
) {
756 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
757 stencil
->TestTwoSide
);
758 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
759 ? GL_BACK
: GL_FRONT
);
762 _mesa_StencilFuncSeparate(GL_FRONT
,
763 stencil
->Function
[0],
765 stencil
->ValueMask
[0]);
766 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
767 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
768 stencil
->ZFailFunc
[0],
769 stencil
->ZPassFunc
[0]);
771 _mesa_StencilFuncSeparate(GL_BACK
,
772 stencil
->Function
[1],
774 stencil
->ValueMask
[1]);
775 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
776 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
777 stencil
->ZFailFunc
[1],
778 stencil
->ZPassFunc
[1]);
781 if (state
& META_TEXTURE
) {
784 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
786 /* restore texenv for unit[0] */
787 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
789 /* restore texture objects for unit[0] only */
790 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
791 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
792 save
->CurrentTexture
[tgt
]);
793 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
796 /* Re-enable textures, texgen */
797 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
798 if (save
->TexEnabled
[u
]) {
799 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
801 if (save
->TexEnabled
[u
] & TEXTURE_1D_BIT
)
802 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_TRUE
);
803 if (save
->TexEnabled
[u
] & TEXTURE_2D_BIT
)
804 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_TRUE
);
805 if (save
->TexEnabled
[u
] & TEXTURE_3D_BIT
)
806 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_TRUE
);
807 if (save
->TexEnabled
[u
] & TEXTURE_CUBE_BIT
)
808 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_TRUE
);
809 if (save
->TexEnabled
[u
] & TEXTURE_RECT_BIT
)
810 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_TRUE
);
813 if (save
->TexGenEnabled
[u
]) {
814 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
816 if (save
->TexGenEnabled
[u
] & S_BIT
)
817 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_TRUE
);
818 if (save
->TexGenEnabled
[u
] & T_BIT
)
819 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_TRUE
);
820 if (save
->TexGenEnabled
[u
] & R_BIT
)
821 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_TRUE
);
822 if (save
->TexGenEnabled
[u
] & Q_BIT
)
823 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_TRUE
);
827 /* restore current unit state */
828 _mesa_ActiveTextureARB(GL_TEXTURE0
+ save
->ActiveUnit
);
829 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ save
->ClientActiveUnit
);
832 if (state
& META_TRANSFORM
) {
833 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
834 _mesa_ActiveTextureARB(GL_TEXTURE0
);
835 _mesa_MatrixMode(GL_TEXTURE
);
836 _mesa_LoadMatrixf(save
->TextureMatrix
);
837 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
839 _mesa_MatrixMode(GL_MODELVIEW
);
840 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
842 _mesa_MatrixMode(GL_PROJECTION
);
843 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
845 _mesa_MatrixMode(save
->MatrixMode
);
847 if (save
->ClipPlanesEnabled
) {
849 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
850 if (save
->ClipPlanesEnabled
& (1 << i
)) {
851 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
857 if (state
& META_VERTEX
) {
858 /* restore vertex buffer object */
859 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
860 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
862 /* restore vertex array object */
863 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
864 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
867 if (state
& META_VIEWPORT
) {
868 if (save
->ViewportX
!= ctx
->Viewport
.X
||
869 save
->ViewportY
!= ctx
->Viewport
.Y
||
870 save
->ViewportW
!= ctx
->Viewport
.Width
||
871 save
->ViewportH
!= ctx
->Viewport
.Height
) {
872 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
873 save
->ViewportW
, save
->ViewportH
);
875 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
878 if (state
& META_CLAMP_FRAGMENT_COLOR
) {
879 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
882 if (state
& META_CLAMP_VERTEX_COLOR
) {
883 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
886 if (state
& META_CONDITIONAL_RENDER
) {
887 if (save
->CondRenderQuery
)
888 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
889 save
->CondRenderMode
);
893 if (save
->Lighting
) {
894 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
900 * Convert Z from a normalized value in the range [0, 1] to an object-space
901 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
902 * default/identity ortho projection results in the original Z value.
903 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
904 * value comes from the clear value or raster position.
906 static INLINE GLfloat
907 invert_z(GLfloat normZ
)
909 GLfloat objZ
= 1.0 - 2.0 * normZ
;
915 * One-time init for a temp_texture object.
916 * Choose tex target, compute max tex size, etc.
919 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
921 /* prefer texture rectangle */
922 if (ctx
->Extensions
.NV_texture_rectangle
) {
923 tex
->Target
= GL_TEXTURE_RECTANGLE
;
924 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
928 /* use 2D texture, NPOT if possible */
929 tex
->Target
= GL_TEXTURE_2D
;
930 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
931 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
933 tex
->MinSize
= 16; /* 16 x 16 at least */
934 assert(tex
->MaxSize
> 0);
936 _mesa_GenTextures(1, &tex
->TexObj
);
941 * Return pointer to temp_texture info for non-bitmap ops.
942 * This does some one-time init if needed.
944 static struct temp_texture
*
945 get_temp_texture(struct gl_context
*ctx
)
947 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
950 init_temp_texture(ctx
, tex
);
958 * Return pointer to temp_texture info for _mesa_meta_bitmap().
959 * We use a separate texture for bitmaps to reduce texture
960 * allocation/deallocation.
962 static struct temp_texture
*
963 get_bitmap_temp_texture(struct gl_context
*ctx
)
965 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
968 init_temp_texture(ctx
, tex
);
976 * Compute the width/height of texture needed to draw an image of the
977 * given size. Return a flag indicating whether the current texture
978 * can be re-used (glTexSubImage2D) or if a new texture needs to be
979 * allocated (glTexImage2D).
980 * Also, compute s/t texcoords for drawing.
982 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
985 alloc_texture(struct temp_texture
*tex
,
986 GLsizei width
, GLsizei height
, GLenum intFormat
)
988 GLboolean newTex
= GL_FALSE
;
990 ASSERT(width
<= tex
->MaxSize
);
991 ASSERT(height
<= tex
->MaxSize
);
993 if (width
> tex
->Width
||
994 height
> tex
->Height
||
995 intFormat
!= tex
->IntFormat
) {
996 /* alloc new texture (larger or different format) */
999 /* use non-power of two size */
1000 tex
->Width
= MAX2(tex
->MinSize
, width
);
1001 tex
->Height
= MAX2(tex
->MinSize
, height
);
1004 /* find power of two size */
1006 w
= h
= tex
->MinSize
;
1015 tex
->IntFormat
= intFormat
;
1020 /* compute texcoords */
1021 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1022 tex
->Sright
= (GLfloat
) width
;
1023 tex
->Ttop
= (GLfloat
) height
;
1026 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1027 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1035 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1038 setup_copypix_texture(struct temp_texture
*tex
,
1040 GLint srcX
, GLint srcY
,
1041 GLsizei width
, GLsizei height
, GLenum intFormat
,
1044 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1045 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1046 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1047 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1049 /* copy framebuffer image to texture */
1051 /* create new tex image */
1052 if (tex
->Width
== width
&& tex
->Height
== height
) {
1053 /* create new tex with framebuffer data */
1054 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1055 srcX
, srcY
, width
, height
, 0);
1058 /* create empty texture */
1059 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1060 tex
->Width
, tex
->Height
, 0,
1061 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1063 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1064 0, 0, srcX
, srcY
, width
, height
);
1068 /* replace existing tex image */
1069 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1070 0, 0, srcX
, srcY
, width
, height
);
1076 * Setup/load texture for glDrawPixels.
1079 setup_drawpix_texture(struct gl_context
*ctx
,
1080 struct temp_texture
*tex
,
1082 GLenum texIntFormat
,
1083 GLsizei width
, GLsizei height
,
1084 GLenum format
, GLenum type
,
1085 const GLvoid
*pixels
)
1087 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1088 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1089 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1090 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1092 /* copy pixel data to texture */
1094 /* create new tex image */
1095 if (tex
->Width
== width
&& tex
->Height
== height
) {
1096 /* create new tex and load image data */
1097 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1098 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1101 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1103 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1104 ctx
->Unpack
.BufferObj
);
1105 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1106 /* create empty texture */
1107 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1108 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1109 if (save_unpack_obj
!= NULL
)
1110 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
,
1111 save_unpack_obj
->Name
);
1113 _mesa_TexSubImage2D(tex
->Target
, 0,
1114 0, 0, width
, height
, format
, type
, pixels
);
1118 /* replace existing tex image */
1119 _mesa_TexSubImage2D(tex
->Target
, 0,
1120 0, 0, width
, height
, format
, type
, pixels
);
1127 * One-time init for drawing depth pixels.
1130 init_blit_depth_pixels(struct gl_context
*ctx
)
1132 static const char *program
=
1134 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1137 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1138 struct temp_texture
*tex
= get_temp_texture(ctx
);
1139 const char *texTarget
;
1141 assert(blit
->DepthFP
== 0);
1143 /* replace %s with "RECT" or "2D" */
1144 assert(strlen(program
) + 4 < sizeof(program2
));
1145 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1149 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1151 _mesa_GenPrograms(1, &blit
->DepthFP
);
1152 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1153 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1154 strlen(program2
), (const GLubyte
*) program2
);
1159 * Try to do a glBlitFramebuffer using no-copy texturing.
1160 * We can do this when the src renderbuffer is actually a texture.
1161 * But if the src buffer == dst buffer we cannot do this.
1163 * \return new buffer mask indicating the buffers left to blit using the
1167 blitframebuffer_texture(struct gl_context
*ctx
,
1168 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1169 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1170 GLbitfield mask
, GLenum filter
)
1172 if (mask
& GL_COLOR_BUFFER_BIT
) {
1173 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1174 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1175 const struct gl_renderbuffer_attachment
*drawAtt
=
1176 &drawFb
->Attachment
[drawFb
->_ColorDrawBufferIndexes
[0]];
1177 const struct gl_renderbuffer_attachment
*readAtt
=
1178 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1180 if (readAtt
&& readAtt
->Texture
) {
1181 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1182 const GLuint srcLevel
= readAtt
->TextureLevel
;
1183 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
1184 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
1185 const GLint baseLevelSave
= texObj
->BaseLevel
;
1186 const GLint maxLevelSave
= texObj
->MaxLevel
;
1187 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
1188 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
1189 const GLenum target
= texObj
->Target
;
1191 if (drawAtt
->Texture
== readAtt
->Texture
) {
1192 /* Can't use same texture as both the source and dest. We need
1193 * to handle overlapping blits and besides, some hw may not
1199 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1200 /* Can't handle other texture types at this time */
1205 printf("Blit from texture!\n");
1206 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1207 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1210 /* Prepare src texture state */
1211 _mesa_BindTexture(target
, texObj
->Name
);
1212 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
1213 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
1214 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1215 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1216 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1218 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1219 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1220 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1221 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1223 /* Prepare vertex data (the VBO was previously created and bound) */
1228 struct vertex verts
[4];
1229 GLfloat s0
, t0
, s1
, t1
;
1231 if (target
== GL_TEXTURE_2D
) {
1232 const struct gl_texture_image
*texImage
1233 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1234 s0
= srcX0
/ (float) texImage
->Width
;
1235 s1
= srcX1
/ (float) texImage
->Width
;
1236 t0
= srcY0
/ (float) texImage
->Height
;
1237 t1
= srcY1
/ (float) texImage
->Height
;
1240 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1247 verts
[0].x
= (GLfloat
) dstX0
;
1248 verts
[0].y
= (GLfloat
) dstY0
;
1249 verts
[1].x
= (GLfloat
) dstX1
;
1250 verts
[1].y
= (GLfloat
) dstY0
;
1251 verts
[2].x
= (GLfloat
) dstX1
;
1252 verts
[2].y
= (GLfloat
) dstY1
;
1253 verts
[3].x
= (GLfloat
) dstX0
;
1254 verts
[3].y
= (GLfloat
) dstY1
;
1265 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1268 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1270 /* Restore texture object state, the texture binding will
1271 * be restored by _mesa_meta_end().
1273 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
1274 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
1275 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1276 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1277 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1279 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
1280 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
1282 /* Done with color buffer */
1283 mask
&= ~GL_COLOR_BUFFER_BIT
;
1292 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1293 * of texture mapping and polygon rendering.
1296 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1297 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1298 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1299 GLbitfield mask
, GLenum filter
)
1301 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1302 struct temp_texture
*tex
= get_temp_texture(ctx
);
1303 const GLsizei maxTexSize
= tex
->MaxSize
;
1304 const GLint srcX
= MIN2(srcX0
, srcX1
);
1305 const GLint srcY
= MIN2(srcY0
, srcY1
);
1306 const GLint srcW
= abs(srcX1
- srcX0
);
1307 const GLint srcH
= abs(srcY1
- srcY0
);
1308 const GLboolean srcFlipX
= srcX1
< srcX0
;
1309 const GLboolean srcFlipY
= srcY1
< srcY0
;
1313 struct vertex verts
[4];
1316 if (srcW
> maxTexSize
|| srcH
> maxTexSize
) {
1317 /* XXX avoid this fallback */
1318 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1319 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1335 /* only scissor effects blit so save/clear all other relevant state */
1336 _mesa_meta_begin(ctx
, ~META_SCISSOR
);
1338 if (blit
->ArrayObj
== 0) {
1339 /* one-time setup */
1341 /* create vertex array object */
1342 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1343 _mesa_BindVertexArray(blit
->ArrayObj
);
1345 /* create vertex array buffer */
1346 _mesa_GenBuffersARB(1, &blit
->VBO
);
1347 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1348 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1349 NULL
, GL_DYNAMIC_DRAW_ARB
);
1351 /* setup vertex arrays */
1352 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1353 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1354 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1355 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1358 _mesa_BindVertexArray(blit
->ArrayObj
);
1359 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1362 /* Try faster, direct texture approach first */
1363 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1364 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1366 _mesa_meta_end(ctx
);
1370 /* Continue with "normal" approach which involves copying the src rect
1371 * into a temporary texture and is "blitted" by drawing a textured quad.
1374 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1376 /* vertex positions/texcoords (after texture allocation!) */
1378 verts
[0].x
= (GLfloat
) dstX0
;
1379 verts
[0].y
= (GLfloat
) dstY0
;
1380 verts
[1].x
= (GLfloat
) dstX1
;
1381 verts
[1].y
= (GLfloat
) dstY0
;
1382 verts
[2].x
= (GLfloat
) dstX1
;
1383 verts
[2].y
= (GLfloat
) dstY1
;
1384 verts
[3].x
= (GLfloat
) dstX0
;
1385 verts
[3].y
= (GLfloat
) dstY1
;
1389 verts
[1].s
= tex
->Sright
;
1391 verts
[2].s
= tex
->Sright
;
1392 verts
[2].t
= tex
->Ttop
;
1394 verts
[3].t
= tex
->Ttop
;
1396 /* upload new vertex data */
1397 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1400 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1402 if (mask
& GL_COLOR_BUFFER_BIT
) {
1403 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1405 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1406 mask
&= ~GL_COLOR_BUFFER_BIT
;
1409 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1410 GLuint
*tmp
= (GLuint
*) malloc(srcW
* srcH
* sizeof(GLuint
));
1413 init_blit_depth_pixels(ctx
);
1415 /* maybe change tex format here */
1416 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1418 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1419 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1421 setup_drawpix_texture(ctx
, tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1422 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1424 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1425 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1426 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1427 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1428 _mesa_DepthFunc(GL_ALWAYS
);
1429 _mesa_DepthMask(GL_TRUE
);
1431 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1432 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1438 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1439 /* XXX can't easily do stencil */
1442 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1444 _mesa_meta_end(ctx
);
1447 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1448 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1454 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1457 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1459 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1461 GLfloat x
, y
, z
, r
, g
, b
, a
;
1463 struct vertex verts
[4];
1464 /* save all state but scissor, pixel pack/unpack */
1465 GLbitfield metaSave
= (META_ALL
-
1468 META_CONDITIONAL_RENDER
);
1469 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1471 if (buffers
& BUFFER_BITS_COLOR
) {
1472 /* if clearing color buffers, don't save/restore colormask */
1473 metaSave
-= META_COLOR_MASK
;
1476 _mesa_meta_begin(ctx
, metaSave
);
1478 if (clear
->ArrayObj
== 0) {
1479 /* one-time setup */
1481 /* create vertex array object */
1482 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1483 _mesa_BindVertexArray(clear
->ArrayObj
);
1485 /* create vertex array buffer */
1486 _mesa_GenBuffersARB(1, &clear
->VBO
);
1487 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1489 /* setup vertex arrays */
1490 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1491 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1492 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1493 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1496 _mesa_BindVertexArray(clear
->ArrayObj
);
1497 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1500 /* GL_COLOR_BUFFER_BIT */
1501 if (buffers
& BUFFER_BITS_COLOR
) {
1502 /* leave colormask, glDrawBuffer state as-is */
1504 /* Clears never have the color clamped. */
1505 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1508 ASSERT(metaSave
& META_COLOR_MASK
);
1509 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1512 /* GL_DEPTH_BUFFER_BIT */
1513 if (buffers
& BUFFER_BIT_DEPTH
) {
1514 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1515 _mesa_DepthFunc(GL_ALWAYS
);
1516 _mesa_DepthMask(GL_TRUE
);
1519 assert(!ctx
->Depth
.Test
);
1522 /* GL_STENCIL_BUFFER_BIT */
1523 if (buffers
& BUFFER_BIT_STENCIL
) {
1524 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1525 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1526 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1527 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1528 ctx
->Stencil
.Clear
& stencilMax
,
1529 ctx
->Stencil
.WriteMask
[0]);
1532 assert(!ctx
->Stencil
.Enabled
);
1535 /* vertex positions/colors */
1537 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1538 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1539 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1540 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1541 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1558 for (i
= 0; i
< 4; i
++) {
1559 verts
[i
].r
= ctx
->Color
.ClearColorUnclamped
[0];
1560 verts
[i
].g
= ctx
->Color
.ClearColorUnclamped
[1];
1561 verts
[i
].b
= ctx
->Color
.ClearColorUnclamped
[2];
1562 verts
[i
].a
= ctx
->Color
.ClearColorUnclamped
[3];
1565 /* upload new vertex data */
1566 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1567 GL_DYNAMIC_DRAW_ARB
);
1571 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1573 _mesa_meta_end(ctx
);
1578 * Meta implementation of ctx->Driver.CopyPixels() in terms
1579 * of texture mapping and polygon rendering.
1582 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
1583 GLsizei width
, GLsizei height
,
1584 GLint dstX
, GLint dstY
, GLenum type
)
1586 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1587 struct temp_texture
*tex
= get_temp_texture(ctx
);
1589 GLfloat x
, y
, z
, s
, t
;
1591 struct vertex verts
[4];
1593 GLenum intFormat
= GL_RGBA
;
1595 if (type
!= GL_COLOR
||
1596 ctx
->_ImageTransferState
||
1598 width
> tex
->MaxSize
||
1599 height
> tex
->MaxSize
) {
1600 /* XXX avoid this fallback */
1601 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1605 /* Most GL state applies to glCopyPixels, but a there's a few things
1606 * we need to override:
1608 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1615 if (copypix
->ArrayObj
== 0) {
1616 /* one-time setup */
1618 /* create vertex array object */
1619 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
1620 _mesa_BindVertexArray(copypix
->ArrayObj
);
1622 /* create vertex array buffer */
1623 _mesa_GenBuffersARB(1, ©pix
->VBO
);
1624 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1625 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1626 NULL
, GL_DYNAMIC_DRAW_ARB
);
1628 /* setup vertex arrays */
1629 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1630 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1631 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1632 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1635 _mesa_BindVertexArray(copypix
->ArrayObj
);
1636 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1639 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
1641 /* vertex positions, texcoords (after texture allocation!) */
1643 const GLfloat dstX0
= (GLfloat
) dstX
;
1644 const GLfloat dstY0
= (GLfloat
) dstY
;
1645 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1646 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1647 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1657 verts
[1].s
= tex
->Sright
;
1662 verts
[2].s
= tex
->Sright
;
1663 verts
[2].t
= tex
->Ttop
;
1668 verts
[3].t
= tex
->Ttop
;
1670 /* upload new vertex data */
1671 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1674 /* Alloc/setup texture */
1675 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
1676 GL_RGBA
, GL_NEAREST
);
1678 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1680 /* draw textured quad */
1681 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1683 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1685 _mesa_meta_end(ctx
);
1691 * When the glDrawPixels() image size is greater than the max rectangle
1692 * texture size we use this function to break the glDrawPixels() image
1693 * into tiles which fit into the max texture size.
1696 tiled_draw_pixels(struct gl_context
*ctx
,
1698 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1699 GLenum format
, GLenum type
,
1700 const struct gl_pixelstore_attrib
*unpack
,
1701 const GLvoid
*pixels
)
1703 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
1706 if (tileUnpack
.RowLength
== 0)
1707 tileUnpack
.RowLength
= width
;
1709 for (i
= 0; i
< width
; i
+= tileSize
) {
1710 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
1711 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
1713 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
1715 for (j
= 0; j
< height
; j
+= tileSize
) {
1716 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
1717 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
1719 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
1721 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
1722 format
, type
, &tileUnpack
, pixels
);
1729 * One-time init for drawing stencil pixels.
1732 init_draw_stencil_pixels(struct gl_context
*ctx
)
1734 /* This program is run eight times, once for each stencil bit.
1735 * The stencil values to draw are found in an 8-bit alpha texture.
1736 * We read the texture/stencil value and test if bit 'b' is set.
1737 * If the bit is not set, use KIL to kill the fragment.
1738 * Finally, we use the stencil test to update the stencil buffer.
1740 * The basic algorithm for checking if a bit is set is:
1741 * if (is_odd(value / (1 << bit)))
1742 * result is one (or non-zero).
1745 * The program parameter contains three values:
1746 * parm.x = 255 / (1 << bit)
1750 static const char *program
=
1752 "PARAM parm = program.local[0]; \n"
1754 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1755 "# t = t * 255 / bit \n"
1756 "MUL t.x, t.a, parm.x; \n"
1759 "SUB t.x, t.x, t.y; \n"
1761 "MUL t.x, t.x, parm.y; \n"
1762 "# t = fract(t.x) \n"
1763 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
1764 "# t.x = (t.x == 0 ? 1 : 0) \n"
1765 "SGE t.x, -t.x, parm.z; \n"
1767 "# for debug only \n"
1768 "#MOV result.color, t.x; \n"
1770 char program2
[1000];
1771 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1772 struct temp_texture
*tex
= get_temp_texture(ctx
);
1773 const char *texTarget
;
1775 assert(drawpix
->StencilFP
== 0);
1777 /* replace %s with "RECT" or "2D" */
1778 assert(strlen(program
) + 4 < sizeof(program2
));
1779 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1783 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1785 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
1786 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1787 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1788 strlen(program2
), (const GLubyte
*) program2
);
1793 * One-time init for drawing depth pixels.
1796 init_draw_depth_pixels(struct gl_context
*ctx
)
1798 static const char *program
=
1800 "PARAM color = program.local[0]; \n"
1801 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1802 "MOV result.color, color; \n"
1805 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1806 struct temp_texture
*tex
= get_temp_texture(ctx
);
1807 const char *texTarget
;
1809 assert(drawpix
->DepthFP
== 0);
1811 /* replace %s with "RECT" or "2D" */
1812 assert(strlen(program
) + 4 < sizeof(program2
));
1813 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1817 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1819 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
1820 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1821 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1822 strlen(program2
), (const GLubyte
*) program2
);
1827 * Meta implementation of ctx->Driver.DrawPixels() in terms
1828 * of texture mapping and polygon rendering.
1831 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
1832 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1833 GLenum format
, GLenum type
,
1834 const struct gl_pixelstore_attrib
*unpack
,
1835 const GLvoid
*pixels
)
1837 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1838 struct temp_texture
*tex
= get_temp_texture(ctx
);
1839 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
1840 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
1842 GLfloat x
, y
, z
, s
, t
;
1844 struct vertex verts
[4];
1845 GLenum texIntFormat
;
1846 GLboolean fallback
, newTex
;
1847 GLbitfield metaExtraSave
= 0x0;
1851 * Determine if we can do the glDrawPixels with texture mapping.
1853 fallback
= GL_FALSE
;
1854 if (ctx
->_ImageTransferState
||
1859 if (_mesa_is_color_format(format
)) {
1860 /* use more compact format when possible */
1861 /* XXX disable special case for GL_LUMINANCE for now to work around
1862 * apparent i965 driver bug (see bug #23670).
1864 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
1865 texIntFormat
= format
;
1867 texIntFormat
= GL_RGBA
;
1869 /* If we're not supposed to clamp the resulting color, then just
1870 * promote our texture to fully float. We could do better by
1871 * just going for the matching set of channels, in floating
1874 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
1875 ctx
->Extensions
.ARB_texture_float
)
1876 texIntFormat
= GL_RGBA32F
;
1878 else if (_mesa_is_stencil_format(format
)) {
1879 if (ctx
->Extensions
.ARB_fragment_program
&&
1880 ctx
->Pixel
.IndexShift
== 0 &&
1881 ctx
->Pixel
.IndexOffset
== 0 &&
1882 type
== GL_UNSIGNED_BYTE
) {
1883 /* We'll store stencil as alpha. This only works for GLubyte
1884 * image data because of how incoming values are mapped to alpha
1887 texIntFormat
= GL_ALPHA
;
1888 metaExtraSave
= (META_COLOR_MASK
|
1897 else if (_mesa_is_depth_format(format
)) {
1898 if (ctx
->Extensions
.ARB_depth_texture
&&
1899 ctx
->Extensions
.ARB_fragment_program
) {
1900 texIntFormat
= GL_DEPTH_COMPONENT
;
1901 metaExtraSave
= (META_SHADER
);
1912 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
1913 format
, type
, unpack
, pixels
);
1918 * Check image size against max texture size, draw as tiles if needed.
1920 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
1921 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
1922 format
, type
, unpack
, pixels
);
1926 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
1927 * but a there's a few things we need to override:
1929 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1935 META_CLAMP_FRAGMENT_COLOR
|
1938 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
1940 /* vertex positions, texcoords (after texture allocation!) */
1942 const GLfloat x0
= (GLfloat
) x
;
1943 const GLfloat y0
= (GLfloat
) y
;
1944 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
1945 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
1946 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1956 verts
[1].s
= tex
->Sright
;
1961 verts
[2].s
= tex
->Sright
;
1962 verts
[2].t
= tex
->Ttop
;
1967 verts
[3].t
= tex
->Ttop
;
1970 if (drawpix
->ArrayObj
== 0) {
1971 /* one-time setup: create vertex array object */
1972 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
1974 _mesa_BindVertexArray(drawpix
->ArrayObj
);
1976 /* create vertex array buffer */
1977 _mesa_GenBuffersARB(1, &vbo
);
1978 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
1979 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1980 verts
, GL_DYNAMIC_DRAW_ARB
);
1982 /* setup vertex arrays */
1983 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1984 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1985 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1986 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1988 /* set given unpack params */
1989 ctx
->Unpack
= *unpack
;
1991 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1993 if (_mesa_is_stencil_format(format
)) {
1994 /* Drawing stencil */
1997 if (!drawpix
->StencilFP
)
1998 init_draw_stencil_pixels(ctx
);
2000 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2001 GL_ALPHA
, type
, pixels
);
2003 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2005 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2007 /* set all stencil bits to 0 */
2008 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2009 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2010 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2012 /* set stencil bits to 1 where needed */
2013 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2015 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2016 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2018 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2019 const GLuint mask
= 1 << bit
;
2020 if (mask
& origStencilMask
) {
2021 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2022 _mesa_StencilMask(mask
);
2024 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2025 255.0 / mask
, 0.5, 0.0, 0.0);
2027 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2031 else if (_mesa_is_depth_format(format
)) {
2033 if (!drawpix
->DepthFP
)
2034 init_draw_depth_pixels(ctx
);
2036 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2037 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2039 /* polygon color = current raster color */
2040 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2041 ctx
->Current
.RasterColor
);
2043 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2044 format
, type
, pixels
);
2046 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2050 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2051 format
, type
, pixels
);
2052 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2055 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2057 _mesa_DeleteBuffersARB(1, &vbo
);
2059 /* restore unpack params */
2060 ctx
->Unpack
= unpackSave
;
2062 _mesa_meta_end(ctx
);
2066 alpha_test_raster_color(struct gl_context
*ctx
)
2068 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2069 GLfloat ref
= ctx
->Color
.AlphaRef
;
2071 switch (ctx
->Color
.AlphaFunc
) {
2077 return alpha
== ref
;
2079 return alpha
<= ref
;
2083 return alpha
!= ref
;
2085 return alpha
>= ref
;
2095 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2096 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2097 * tracker would improve performance a lot.
2100 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2101 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2102 const struct gl_pixelstore_attrib
*unpack
,
2103 const GLubyte
*bitmap1
)
2105 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2106 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2107 const GLenum texIntFormat
= GL_ALPHA
;
2108 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2111 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2113 struct vertex verts
[4];
2118 * Check if swrast fallback is needed.
2120 if (ctx
->_ImageTransferState
||
2121 ctx
->FragmentProgram
._Enabled
||
2123 ctx
->Texture
._EnabledUnits
||
2124 width
> tex
->MaxSize
||
2125 height
> tex
->MaxSize
) {
2126 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2130 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2133 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2134 * but a there's a few things we need to override:
2136 _mesa_meta_begin(ctx
, (META_ALPHA_TEST
|
2138 META_RASTERIZATION
|
2145 if (bitmap
->ArrayObj
== 0) {
2146 /* one-time setup */
2148 /* create vertex array object */
2149 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2150 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2152 /* create vertex array buffer */
2153 _mesa_GenBuffersARB(1, &bitmap
->VBO
);
2154 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2155 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2156 NULL
, GL_DYNAMIC_DRAW_ARB
);
2158 /* setup vertex arrays */
2159 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2160 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2161 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2162 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2163 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2164 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2167 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2168 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2171 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2173 /* vertex positions, texcoords, colors (after texture allocation!) */
2175 const GLfloat x0
= (GLfloat
) x
;
2176 const GLfloat y0
= (GLfloat
) y
;
2177 const GLfloat x1
= (GLfloat
) (x
+ width
);
2178 const GLfloat y1
= (GLfloat
) (y
+ height
);
2179 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2190 verts
[1].s
= tex
->Sright
;
2195 verts
[2].s
= tex
->Sright
;
2196 verts
[2].t
= tex
->Ttop
;
2201 verts
[3].t
= tex
->Ttop
;
2203 for (i
= 0; i
< 4; i
++) {
2204 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2205 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2206 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2207 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2210 /* upload new vertex data */
2211 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2214 /* choose different foreground/background alpha values */
2215 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2216 bg
= (fg
> 127 ? 0 : 255);
2218 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2220 _mesa_meta_end(ctx
);
2224 bitmap8
= (GLubyte
*) malloc(width
* height
);
2226 memset(bitmap8
, bg
, width
* height
);
2227 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2228 bitmap8
, width
, fg
);
2230 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2232 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2233 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2235 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2236 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2238 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2240 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2245 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2247 _mesa_meta_end(ctx
);
2252 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2253 * software fallback. The fallback path will require that the texture
2254 * images are mapped.
2255 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2258 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
2259 struct gl_texture_object
*texObj
)
2261 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2262 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2263 struct gl_texture_image
*baseImage
;
2267 /* check for fallbacks */
2268 if (!ctx
->Extensions
.EXT_framebuffer_object
||
2269 target
== GL_TEXTURE_3D
) {
2273 srcLevel
= texObj
->BaseLevel
;
2274 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2275 if (!baseImage
|| _mesa_is_format_compressed(baseImage
->TexFormat
)) {
2280 * Test that we can actually render in the texture's format.
2283 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2284 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2286 if (target
== GL_TEXTURE_1D
) {
2287 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2288 GL_COLOR_ATTACHMENT0_EXT
,
2289 target
, texObj
->Name
, srcLevel
);
2292 /* other work is needed to enable 3D mipmap generation */
2293 else if (target
== GL_TEXTURE_3D
) {
2295 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2296 GL_COLOR_ATTACHMENT0_EXT
,
2297 target
, texObj
->Name
, srcLevel
, zoffset
);
2302 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2303 GL_COLOR_ATTACHMENT0_EXT
,
2304 target
, texObj
->Name
, srcLevel
);
2307 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2309 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2311 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2320 * Called via ctx->Driver.GenerateMipmap()
2321 * Note: texture borders and 3D texture support not yet complete.
2324 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
2325 struct gl_texture_object
*texObj
)
2327 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2329 GLfloat x
, y
, s
, t
, r
;
2331 struct vertex verts
[4];
2332 const GLuint baseLevel
= texObj
->BaseLevel
;
2333 const GLuint maxLevel
= texObj
->MaxLevel
;
2334 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
2335 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
2336 const GLint maxLevelSave
= texObj
->MaxLevel
;
2337 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
2338 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
2339 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
2340 const GLenum wrapRSave
= texObj
->Sampler
.WrapR
;
2341 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2342 const GLuint original_active_unit
= ctx
->Texture
.CurrentUnit
;
2347 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
2348 _mesa_generate_mipmap(ctx
, target
, texObj
);
2352 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
2353 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
2354 faceTarget
= target
;
2355 target
= GL_TEXTURE_CUBE_MAP
;
2358 faceTarget
= target
;
2361 _mesa_meta_begin(ctx
, META_ALL
);
2363 if (original_active_unit
!= 0)
2364 _mesa_BindTexture(target
, texObj
->Name
);
2366 if (mipmap
->ArrayObj
== 0) {
2367 /* one-time setup */
2369 /* create vertex array object */
2370 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
2371 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
2373 /* create vertex array buffer */
2374 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
2375 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2376 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2377 NULL
, GL_DYNAMIC_DRAW_ARB
);
2379 /* setup vertex arrays */
2380 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2381 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2382 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2383 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2386 _mesa_BindVertexArray(mipmap
->ArrayObj
);
2387 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2391 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2393 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2395 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
2396 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
2397 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
2398 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2399 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2400 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
2402 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2404 /* setup texcoords once (XXX what about border?) */
2405 switch (faceTarget
) {
2427 static const GLfloat st
[4][2] = {
2428 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2432 /* loop over quad verts */
2433 for (i
= 0; i
< 4; i
++) {
2434 /* Compute sc = +/-scale and tc = +/-scale.
2435 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2436 * though that can still sometimes happen with this scale factor...
2438 const GLfloat scale
= 0.9999f
;
2439 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2440 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2442 switch (faceTarget
) {
2443 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2448 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2453 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2458 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2463 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2468 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2480 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2482 /* setup vertex positions */
2493 /* upload new vertex data */
2494 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2497 /* setup projection matrix */
2498 _mesa_MatrixMode(GL_PROJECTION
);
2499 _mesa_LoadIdentity();
2500 _mesa_Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
2502 /* texture is already locked, unlock now */
2503 _mesa_unlock_texture(ctx
, texObj
);
2505 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
2506 const struct gl_texture_image
*srcImage
;
2507 const GLuint srcLevel
= dstLevel
- 1;
2508 GLsizei srcWidth
, srcHeight
, srcDepth
;
2509 GLsizei dstWidth
, dstHeight
, dstDepth
;
2512 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
2513 assert(srcImage
->Border
== 0); /* XXX we can fix this */
2515 /* src size w/out border */
2516 srcWidth
= srcImage
->Width
- 2 * border
;
2517 srcHeight
= srcImage
->Height
- 2 * border
;
2518 srcDepth
= srcImage
->Depth
- 2 * border
;
2520 /* new dst size w/ border */
2521 dstWidth
= MAX2(1, srcWidth
/ 2) + 2 * border
;
2522 dstHeight
= MAX2(1, srcHeight
/ 2) + 2 * border
;
2523 dstDepth
= MAX2(1, srcDepth
/ 2) + 2 * border
;
2525 if (dstWidth
== srcImage
->Width
&&
2526 dstHeight
== srcImage
->Height
&&
2527 dstDepth
== srcImage
->Depth
) {
2532 /* Set MaxLevel large enough to hold the new level when we allocate it */
2533 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
2535 /* Create empty dest image */
2536 if (target
== GL_TEXTURE_1D
) {
2537 _mesa_TexImage1D(target
, dstLevel
, srcImage
->InternalFormat
,
2539 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2541 else if (target
== GL_TEXTURE_3D
) {
2542 _mesa_TexImage3D(target
, dstLevel
, srcImage
->InternalFormat
,
2543 dstWidth
, dstHeight
, dstDepth
, border
,
2544 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2548 _mesa_TexImage2D(faceTarget
, dstLevel
, srcImage
->InternalFormat
,
2549 dstWidth
, dstHeight
, border
,
2550 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2552 if (target
== GL_TEXTURE_CUBE_MAP
) {
2553 /* If texturing from a cube, we need to make sure all src faces
2554 * have been defined (even if we're not sampling from them.)
2555 * Otherwise the texture object will be 'incomplete' and
2556 * texturing from it will not be allowed.
2559 for (face
= 0; face
< 6; face
++) {
2560 if (!texObj
->Image
[face
][srcLevel
] ||
2561 texObj
->Image
[face
][srcLevel
]->Width
!= srcWidth
) {
2562 _mesa_TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ face
,
2563 srcLevel
, srcImage
->InternalFormat
,
2564 srcWidth
, srcHeight
, border
,
2565 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2571 /* limit minification to src level */
2572 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
2574 /* Set to draw into the current dstLevel */
2575 if (target
== GL_TEXTURE_1D
) {
2576 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2577 GL_COLOR_ATTACHMENT0_EXT
,
2582 else if (target
== GL_TEXTURE_3D
) {
2583 GLint zoffset
= 0; /* XXX unfinished */
2584 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2585 GL_COLOR_ATTACHMENT0_EXT
,
2592 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2593 GL_COLOR_ATTACHMENT0_EXT
,
2599 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
2602 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2603 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2608 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
2609 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
2611 /* setup viewport */
2612 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
2614 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2617 _mesa_lock_texture(ctx
, texObj
); /* relock */
2619 _mesa_meta_end(ctx
);
2621 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
2622 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
2623 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
2624 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
2625 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
2626 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
2627 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, wrapRSave
);
2629 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2634 * Determine the GL data type to use for the temporary image read with
2635 * ReadPixels() and passed to Tex[Sub]Image().
2638 get_temp_image_type(struct gl_context
*ctx
, GLenum baseFormat
)
2640 switch (baseFormat
) {
2645 case GL_LUMINANCE_ALPHA
:
2647 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2648 return GL_UNSIGNED_BYTE
;
2649 else if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2650 return GL_UNSIGNED_SHORT
;
2653 case GL_DEPTH_COMPONENT
:
2654 return GL_UNSIGNED_INT
;
2655 case GL_DEPTH_STENCIL
:
2656 return GL_UNSIGNED_INT_24_8
;
2658 _mesa_problem(ctx
, "Unexpected format in get_temp_image_type()");
2665 * Helper for _mesa_meta_CopyTexImage1/2D() functions.
2666 * Have to be careful with locking and meta state for pixel transfer.
2669 copy_tex_image(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
2670 GLenum internalFormat
, GLint x
, GLint y
,
2671 GLsizei width
, GLsizei height
, GLint border
)
2673 struct gl_texture_object
*texObj
;
2674 struct gl_texture_image
*texImage
;
2675 GLenum format
, type
;
2679 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2680 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2682 /* Choose format/type for temporary image buffer */
2683 format
= _mesa_base_tex_format(ctx
, internalFormat
);
2684 type
= get_temp_image_type(ctx
, format
);
2685 bpp
= _mesa_bytes_per_pixel(format
, type
);
2687 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_image()");
2692 * Alloc image buffer (XXX could use a PBO)
2694 buf
= malloc(width
* height
* bpp
);
2696 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
2700 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2703 * Read image from framebuffer (disable pixel transfer ops)
2705 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2706 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2707 format
, type
, &ctx
->Pack
, buf
);
2708 _mesa_meta_end(ctx
);
2710 if (texImage
->Data
) {
2711 ctx
->Driver
.FreeTexImageData(ctx
, texImage
);
2714 /* The texture's format was already chosen in _mesa_CopyTexImage() */
2715 ASSERT(texImage
->TexFormat
!= MESA_FORMAT_NONE
);
2718 * Store texture data (with pixel transfer ops)
2720 _mesa_meta_begin(ctx
, META_PIXEL_STORE
);
2722 _mesa_update_state(ctx
); /* to update pixel transfer state */
2724 if (target
== GL_TEXTURE_1D
) {
2725 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
2726 width
, border
, format
, type
,
2727 buf
, &ctx
->Unpack
, texObj
, texImage
);
2730 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
2731 width
, height
, border
, format
, type
,
2732 buf
, &ctx
->Unpack
, texObj
, texImage
);
2734 _mesa_meta_end(ctx
);
2736 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
2743 _mesa_meta_CopyTexImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2744 GLenum internalFormat
, GLint x
, GLint y
,
2745 GLsizei width
, GLint border
)
2747 copy_tex_image(ctx
, 1, target
, level
, internalFormat
, x
, y
,
2753 _mesa_meta_CopyTexImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2754 GLenum internalFormat
, GLint x
, GLint y
,
2755 GLsizei width
, GLsizei height
, GLint border
)
2757 copy_tex_image(ctx
, 2, target
, level
, internalFormat
, x
, y
,
2758 width
, height
, border
);
2764 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2765 * Have to be careful with locking and meta state for pixel transfer.
2768 copy_tex_sub_image(struct gl_context
*ctx
,
2769 GLuint dims
, GLenum target
, GLint level
,
2770 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2772 GLsizei width
, GLsizei height
)
2774 struct gl_texture_object
*texObj
;
2775 struct gl_texture_image
*texImage
;
2776 GLenum format
, type
;
2780 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2781 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2783 /* Choose format/type for temporary image buffer */
2784 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
2785 type
= get_temp_image_type(ctx
, format
);
2786 bpp
= _mesa_bytes_per_pixel(format
, type
);
2788 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_sub_image()");
2793 * Alloc image buffer (XXX could use a PBO)
2795 buf
= malloc(width
* height
* bpp
);
2797 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
2801 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2804 * Read image from framebuffer (disable pixel transfer ops)
2806 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2807 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2808 format
, type
, &ctx
->Pack
, buf
);
2809 _mesa_meta_end(ctx
);
2811 _mesa_update_state(ctx
); /* to update pixel transfer state */
2814 * Store texture data (with pixel transfer ops)
2816 _mesa_meta_begin(ctx
, META_PIXEL_STORE
);
2817 if (target
== GL_TEXTURE_1D
) {
2818 ctx
->Driver
.TexSubImage1D(ctx
, target
, level
, xoffset
,
2819 width
, format
, type
, buf
,
2820 &ctx
->Unpack
, texObj
, texImage
);
2822 else if (target
== GL_TEXTURE_3D
) {
2823 ctx
->Driver
.TexSubImage3D(ctx
, target
, level
, xoffset
, yoffset
, zoffset
,
2824 width
, height
, 1, format
, type
, buf
,
2825 &ctx
->Unpack
, texObj
, texImage
);
2828 ctx
->Driver
.TexSubImage2D(ctx
, target
, level
, xoffset
, yoffset
,
2829 width
, height
, format
, type
, buf
,
2830 &ctx
->Unpack
, texObj
, texImage
);
2832 _mesa_meta_end(ctx
);
2834 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
2841 _mesa_meta_CopyTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2843 GLint x
, GLint y
, GLsizei width
)
2845 copy_tex_sub_image(ctx
, 1, target
, level
, xoffset
, 0, 0,
2851 _mesa_meta_CopyTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2852 GLint xoffset
, GLint yoffset
,
2854 GLsizei width
, GLsizei height
)
2856 copy_tex_sub_image(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2857 x
, y
, width
, height
);
2862 _mesa_meta_CopyTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2863 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2865 GLsizei width
, GLsizei height
)
2867 copy_tex_sub_image(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2868 x
, y
, width
, height
);
2873 _mesa_meta_CopyColorTable(struct gl_context
*ctx
,
2874 GLenum target
, GLenum internalformat
,
2875 GLint x
, GLint y
, GLsizei width
)
2879 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2881 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyColorTable");
2886 * Read image from framebuffer (disable pixel transfer ops)
2888 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2889 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2890 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2892 _mesa_ColorTable(target
, internalformat
, width
, GL_RGBA
, GL_FLOAT
, buf
);
2894 _mesa_meta_end(ctx
);
2901 _mesa_meta_CopyColorSubTable(struct gl_context
*ctx
,GLenum target
, GLsizei start
,
2902 GLint x
, GLint y
, GLsizei width
)
2906 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2908 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyColorSubTable");
2913 * Read image from framebuffer (disable pixel transfer ops)
2915 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2916 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2917 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2919 _mesa_ColorSubTable(target
, start
, width
, GL_RGBA
, GL_FLOAT
, buf
);
2921 _mesa_meta_end(ctx
);