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 srgbSave
= texObj
->Sampler
.sRGBDecode
;
1190 const GLenum fbo_srgb_save
= ctx
->Color
.sRGBEnabled
;
1191 const GLenum target
= texObj
->Target
;
1193 if (drawAtt
->Texture
== readAtt
->Texture
) {
1194 /* Can't use same texture as both the source and dest. We need
1195 * to handle overlapping blits and besides, some hw may not
1201 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1202 /* Can't handle other texture types at this time */
1207 printf("Blit from texture!\n");
1208 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1209 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1212 /* Prepare src texture state */
1213 _mesa_BindTexture(target
, texObj
->Name
);
1214 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
1215 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
1216 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1217 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1218 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1220 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1221 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1223 /* Always do our blits with no sRGB decode or encode.*/
1224 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1225 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
1226 GL_SKIP_DECODE_EXT
);
1228 _mesa_Disable(GL_FRAMEBUFFER_SRGB_EXT
);
1230 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1231 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1233 /* Prepare vertex data (the VBO was previously created and bound) */
1238 struct vertex verts
[4];
1239 GLfloat s0
, t0
, s1
, t1
;
1241 if (target
== GL_TEXTURE_2D
) {
1242 const struct gl_texture_image
*texImage
1243 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1244 s0
= srcX0
/ (float) texImage
->Width
;
1245 s1
= srcX1
/ (float) texImage
->Width
;
1246 t0
= srcY0
/ (float) texImage
->Height
;
1247 t1
= srcY1
/ (float) texImage
->Height
;
1250 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1257 verts
[0].x
= (GLfloat
) dstX0
;
1258 verts
[0].y
= (GLfloat
) dstY0
;
1259 verts
[1].x
= (GLfloat
) dstX1
;
1260 verts
[1].y
= (GLfloat
) dstY0
;
1261 verts
[2].x
= (GLfloat
) dstX1
;
1262 verts
[2].y
= (GLfloat
) dstY1
;
1263 verts
[3].x
= (GLfloat
) dstX0
;
1264 verts
[3].y
= (GLfloat
) dstY1
;
1275 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1278 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1280 /* Restore texture object state, the texture binding will
1281 * be restored by _mesa_meta_end().
1283 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
1284 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
1285 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1286 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1287 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1289 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
1290 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
1291 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1292 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
, srgbSave
);
1294 if (ctx
->Extensions
.EXT_texture_sRGB_decode
&& fbo_srgb_save
) {
1295 _mesa_Enable(GL_FRAMEBUFFER_SRGB_EXT
);
1298 /* Done with color buffer */
1299 mask
&= ~GL_COLOR_BUFFER_BIT
;
1308 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1309 * of texture mapping and polygon rendering.
1312 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1313 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1314 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1315 GLbitfield mask
, GLenum filter
)
1317 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1318 struct temp_texture
*tex
= get_temp_texture(ctx
);
1319 const GLsizei maxTexSize
= tex
->MaxSize
;
1320 const GLint srcX
= MIN2(srcX0
, srcX1
);
1321 const GLint srcY
= MIN2(srcY0
, srcY1
);
1322 const GLint srcW
= abs(srcX1
- srcX0
);
1323 const GLint srcH
= abs(srcY1
- srcY0
);
1324 const GLboolean srcFlipX
= srcX1
< srcX0
;
1325 const GLboolean srcFlipY
= srcY1
< srcY0
;
1329 struct vertex verts
[4];
1332 if (srcW
> maxTexSize
|| srcH
> maxTexSize
) {
1333 /* XXX avoid this fallback */
1334 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1335 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1351 /* only scissor effects blit so save/clear all other relevant state */
1352 _mesa_meta_begin(ctx
, ~META_SCISSOR
);
1354 if (blit
->ArrayObj
== 0) {
1355 /* one-time setup */
1357 /* create vertex array object */
1358 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1359 _mesa_BindVertexArray(blit
->ArrayObj
);
1361 /* create vertex array buffer */
1362 _mesa_GenBuffersARB(1, &blit
->VBO
);
1363 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1364 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1365 NULL
, GL_DYNAMIC_DRAW_ARB
);
1367 /* setup vertex arrays */
1368 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1369 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1370 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1371 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1374 _mesa_BindVertexArray(blit
->ArrayObj
);
1375 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1378 /* Try faster, direct texture approach first */
1379 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1380 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1382 _mesa_meta_end(ctx
);
1386 /* Continue with "normal" approach which involves copying the src rect
1387 * into a temporary texture and is "blitted" by drawing a textured quad.
1390 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1392 /* vertex positions/texcoords (after texture allocation!) */
1394 verts
[0].x
= (GLfloat
) dstX0
;
1395 verts
[0].y
= (GLfloat
) dstY0
;
1396 verts
[1].x
= (GLfloat
) dstX1
;
1397 verts
[1].y
= (GLfloat
) dstY0
;
1398 verts
[2].x
= (GLfloat
) dstX1
;
1399 verts
[2].y
= (GLfloat
) dstY1
;
1400 verts
[3].x
= (GLfloat
) dstX0
;
1401 verts
[3].y
= (GLfloat
) dstY1
;
1405 verts
[1].s
= tex
->Sright
;
1407 verts
[2].s
= tex
->Sright
;
1408 verts
[2].t
= tex
->Ttop
;
1410 verts
[3].t
= tex
->Ttop
;
1412 /* upload new vertex data */
1413 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1416 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1418 if (mask
& GL_COLOR_BUFFER_BIT
) {
1419 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1421 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1422 mask
&= ~GL_COLOR_BUFFER_BIT
;
1425 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1426 GLuint
*tmp
= (GLuint
*) malloc(srcW
* srcH
* sizeof(GLuint
));
1429 init_blit_depth_pixels(ctx
);
1431 /* maybe change tex format here */
1432 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1434 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1435 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1437 setup_drawpix_texture(ctx
, tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1438 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1440 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1441 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1442 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1443 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1444 _mesa_DepthFunc(GL_ALWAYS
);
1445 _mesa_DepthMask(GL_TRUE
);
1447 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1448 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1454 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1455 /* XXX can't easily do stencil */
1458 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1460 _mesa_meta_end(ctx
);
1463 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1464 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1470 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1473 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1475 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1477 GLfloat x
, y
, z
, r
, g
, b
, a
;
1479 struct vertex verts
[4];
1480 /* save all state but scissor, pixel pack/unpack */
1481 GLbitfield metaSave
= (META_ALL
-
1484 META_CONDITIONAL_RENDER
);
1485 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1487 if (buffers
& BUFFER_BITS_COLOR
) {
1488 /* if clearing color buffers, don't save/restore colormask */
1489 metaSave
-= META_COLOR_MASK
;
1492 _mesa_meta_begin(ctx
, metaSave
);
1494 if (clear
->ArrayObj
== 0) {
1495 /* one-time setup */
1497 /* create vertex array object */
1498 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1499 _mesa_BindVertexArray(clear
->ArrayObj
);
1501 /* create vertex array buffer */
1502 _mesa_GenBuffersARB(1, &clear
->VBO
);
1503 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1505 /* setup vertex arrays */
1506 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1507 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1508 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1509 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1512 _mesa_BindVertexArray(clear
->ArrayObj
);
1513 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1516 /* GL_COLOR_BUFFER_BIT */
1517 if (buffers
& BUFFER_BITS_COLOR
) {
1518 /* leave colormask, glDrawBuffer state as-is */
1520 /* Clears never have the color clamped. */
1521 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1524 ASSERT(metaSave
& META_COLOR_MASK
);
1525 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1528 /* GL_DEPTH_BUFFER_BIT */
1529 if (buffers
& BUFFER_BIT_DEPTH
) {
1530 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1531 _mesa_DepthFunc(GL_ALWAYS
);
1532 _mesa_DepthMask(GL_TRUE
);
1535 assert(!ctx
->Depth
.Test
);
1538 /* GL_STENCIL_BUFFER_BIT */
1539 if (buffers
& BUFFER_BIT_STENCIL
) {
1540 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1541 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1542 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1543 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1544 ctx
->Stencil
.Clear
& stencilMax
,
1545 ctx
->Stencil
.WriteMask
[0]);
1548 assert(!ctx
->Stencil
.Enabled
);
1551 /* vertex positions/colors */
1553 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1554 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1555 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1556 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1557 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1574 for (i
= 0; i
< 4; i
++) {
1575 verts
[i
].r
= ctx
->Color
.ClearColorUnclamped
[0];
1576 verts
[i
].g
= ctx
->Color
.ClearColorUnclamped
[1];
1577 verts
[i
].b
= ctx
->Color
.ClearColorUnclamped
[2];
1578 verts
[i
].a
= ctx
->Color
.ClearColorUnclamped
[3];
1581 /* upload new vertex data */
1582 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1583 GL_DYNAMIC_DRAW_ARB
);
1587 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1589 _mesa_meta_end(ctx
);
1594 * Meta implementation of ctx->Driver.CopyPixels() in terms
1595 * of texture mapping and polygon rendering.
1598 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
1599 GLsizei width
, GLsizei height
,
1600 GLint dstX
, GLint dstY
, GLenum type
)
1602 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1603 struct temp_texture
*tex
= get_temp_texture(ctx
);
1605 GLfloat x
, y
, z
, s
, t
;
1607 struct vertex verts
[4];
1609 GLenum intFormat
= GL_RGBA
;
1611 if (type
!= GL_COLOR
||
1612 ctx
->_ImageTransferState
||
1614 width
> tex
->MaxSize
||
1615 height
> tex
->MaxSize
) {
1616 /* XXX avoid this fallback */
1617 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1621 /* Most GL state applies to glCopyPixels, but a there's a few things
1622 * we need to override:
1624 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1631 if (copypix
->ArrayObj
== 0) {
1632 /* one-time setup */
1634 /* create vertex array object */
1635 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
1636 _mesa_BindVertexArray(copypix
->ArrayObj
);
1638 /* create vertex array buffer */
1639 _mesa_GenBuffersARB(1, ©pix
->VBO
);
1640 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1641 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1642 NULL
, GL_DYNAMIC_DRAW_ARB
);
1644 /* setup vertex arrays */
1645 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1646 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1647 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1648 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1651 _mesa_BindVertexArray(copypix
->ArrayObj
);
1652 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1655 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
1657 /* vertex positions, texcoords (after texture allocation!) */
1659 const GLfloat dstX0
= (GLfloat
) dstX
;
1660 const GLfloat dstY0
= (GLfloat
) dstY
;
1661 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1662 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1663 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1673 verts
[1].s
= tex
->Sright
;
1678 verts
[2].s
= tex
->Sright
;
1679 verts
[2].t
= tex
->Ttop
;
1684 verts
[3].t
= tex
->Ttop
;
1686 /* upload new vertex data */
1687 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1690 /* Alloc/setup texture */
1691 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
1692 GL_RGBA
, GL_NEAREST
);
1694 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1696 /* draw textured quad */
1697 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1699 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1701 _mesa_meta_end(ctx
);
1707 * When the glDrawPixels() image size is greater than the max rectangle
1708 * texture size we use this function to break the glDrawPixels() image
1709 * into tiles which fit into the max texture size.
1712 tiled_draw_pixels(struct gl_context
*ctx
,
1714 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1715 GLenum format
, GLenum type
,
1716 const struct gl_pixelstore_attrib
*unpack
,
1717 const GLvoid
*pixels
)
1719 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
1722 if (tileUnpack
.RowLength
== 0)
1723 tileUnpack
.RowLength
= width
;
1725 for (i
= 0; i
< width
; i
+= tileSize
) {
1726 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
1727 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
1729 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
1731 for (j
= 0; j
< height
; j
+= tileSize
) {
1732 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
1733 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
1735 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
1737 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
1738 format
, type
, &tileUnpack
, pixels
);
1745 * One-time init for drawing stencil pixels.
1748 init_draw_stencil_pixels(struct gl_context
*ctx
)
1750 /* This program is run eight times, once for each stencil bit.
1751 * The stencil values to draw are found in an 8-bit alpha texture.
1752 * We read the texture/stencil value and test if bit 'b' is set.
1753 * If the bit is not set, use KIL to kill the fragment.
1754 * Finally, we use the stencil test to update the stencil buffer.
1756 * The basic algorithm for checking if a bit is set is:
1757 * if (is_odd(value / (1 << bit)))
1758 * result is one (or non-zero).
1761 * The program parameter contains three values:
1762 * parm.x = 255 / (1 << bit)
1766 static const char *program
=
1768 "PARAM parm = program.local[0]; \n"
1770 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1771 "# t = t * 255 / bit \n"
1772 "MUL t.x, t.a, parm.x; \n"
1775 "SUB t.x, t.x, t.y; \n"
1777 "MUL t.x, t.x, parm.y; \n"
1778 "# t = fract(t.x) \n"
1779 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
1780 "# t.x = (t.x == 0 ? 1 : 0) \n"
1781 "SGE t.x, -t.x, parm.z; \n"
1783 "# for debug only \n"
1784 "#MOV result.color, t.x; \n"
1786 char program2
[1000];
1787 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1788 struct temp_texture
*tex
= get_temp_texture(ctx
);
1789 const char *texTarget
;
1791 assert(drawpix
->StencilFP
== 0);
1793 /* replace %s with "RECT" or "2D" */
1794 assert(strlen(program
) + 4 < sizeof(program2
));
1795 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1799 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1801 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
1802 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1803 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1804 strlen(program2
), (const GLubyte
*) program2
);
1809 * One-time init for drawing depth pixels.
1812 init_draw_depth_pixels(struct gl_context
*ctx
)
1814 static const char *program
=
1816 "PARAM color = program.local[0]; \n"
1817 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1818 "MOV result.color, color; \n"
1821 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1822 struct temp_texture
*tex
= get_temp_texture(ctx
);
1823 const char *texTarget
;
1825 assert(drawpix
->DepthFP
== 0);
1827 /* replace %s with "RECT" or "2D" */
1828 assert(strlen(program
) + 4 < sizeof(program2
));
1829 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1833 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1835 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
1836 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1837 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1838 strlen(program2
), (const GLubyte
*) program2
);
1843 * Meta implementation of ctx->Driver.DrawPixels() in terms
1844 * of texture mapping and polygon rendering.
1847 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
1848 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1849 GLenum format
, GLenum type
,
1850 const struct gl_pixelstore_attrib
*unpack
,
1851 const GLvoid
*pixels
)
1853 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1854 struct temp_texture
*tex
= get_temp_texture(ctx
);
1855 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
1856 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
1858 GLfloat x
, y
, z
, s
, t
;
1860 struct vertex verts
[4];
1861 GLenum texIntFormat
;
1862 GLboolean fallback
, newTex
;
1863 GLbitfield metaExtraSave
= 0x0;
1867 * Determine if we can do the glDrawPixels with texture mapping.
1869 fallback
= GL_FALSE
;
1870 if (ctx
->_ImageTransferState
||
1875 if (_mesa_is_color_format(format
)) {
1876 /* use more compact format when possible */
1877 /* XXX disable special case for GL_LUMINANCE for now to work around
1878 * apparent i965 driver bug (see bug #23670).
1880 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
1881 texIntFormat
= format
;
1883 texIntFormat
= GL_RGBA
;
1885 /* If we're not supposed to clamp the resulting color, then just
1886 * promote our texture to fully float. We could do better by
1887 * just going for the matching set of channels, in floating
1890 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
1891 ctx
->Extensions
.ARB_texture_float
)
1892 texIntFormat
= GL_RGBA32F
;
1894 else if (_mesa_is_stencil_format(format
)) {
1895 if (ctx
->Extensions
.ARB_fragment_program
&&
1896 ctx
->Pixel
.IndexShift
== 0 &&
1897 ctx
->Pixel
.IndexOffset
== 0 &&
1898 type
== GL_UNSIGNED_BYTE
) {
1899 /* We'll store stencil as alpha. This only works for GLubyte
1900 * image data because of how incoming values are mapped to alpha
1903 texIntFormat
= GL_ALPHA
;
1904 metaExtraSave
= (META_COLOR_MASK
|
1913 else if (_mesa_is_depth_format(format
)) {
1914 if (ctx
->Extensions
.ARB_depth_texture
&&
1915 ctx
->Extensions
.ARB_fragment_program
) {
1916 texIntFormat
= GL_DEPTH_COMPONENT
;
1917 metaExtraSave
= (META_SHADER
);
1928 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
1929 format
, type
, unpack
, pixels
);
1934 * Check image size against max texture size, draw as tiles if needed.
1936 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
1937 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
1938 format
, type
, unpack
, pixels
);
1942 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
1943 * but a there's a few things we need to override:
1945 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1951 META_CLAMP_FRAGMENT_COLOR
|
1954 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
1956 /* vertex positions, texcoords (after texture allocation!) */
1958 const GLfloat x0
= (GLfloat
) x
;
1959 const GLfloat y0
= (GLfloat
) y
;
1960 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
1961 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
1962 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1972 verts
[1].s
= tex
->Sright
;
1977 verts
[2].s
= tex
->Sright
;
1978 verts
[2].t
= tex
->Ttop
;
1983 verts
[3].t
= tex
->Ttop
;
1986 if (drawpix
->ArrayObj
== 0) {
1987 /* one-time setup: create vertex array object */
1988 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
1990 _mesa_BindVertexArray(drawpix
->ArrayObj
);
1992 /* create vertex array buffer */
1993 _mesa_GenBuffersARB(1, &vbo
);
1994 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
1995 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1996 verts
, GL_DYNAMIC_DRAW_ARB
);
1998 /* setup vertex arrays */
1999 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2000 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2001 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2002 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2004 /* set given unpack params */
2005 ctx
->Unpack
= *unpack
;
2007 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2009 if (_mesa_is_stencil_format(format
)) {
2010 /* Drawing stencil */
2013 if (!drawpix
->StencilFP
)
2014 init_draw_stencil_pixels(ctx
);
2016 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2017 GL_ALPHA
, type
, pixels
);
2019 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2021 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2023 /* set all stencil bits to 0 */
2024 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2025 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2026 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2028 /* set stencil bits to 1 where needed */
2029 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2031 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2032 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2034 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2035 const GLuint mask
= 1 << bit
;
2036 if (mask
& origStencilMask
) {
2037 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2038 _mesa_StencilMask(mask
);
2040 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2041 255.0 / mask
, 0.5, 0.0, 0.0);
2043 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2047 else if (_mesa_is_depth_format(format
)) {
2049 if (!drawpix
->DepthFP
)
2050 init_draw_depth_pixels(ctx
);
2052 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2053 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2055 /* polygon color = current raster color */
2056 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2057 ctx
->Current
.RasterColor
);
2059 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2060 format
, type
, pixels
);
2062 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2066 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2067 format
, type
, pixels
);
2068 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2071 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2073 _mesa_DeleteBuffersARB(1, &vbo
);
2075 /* restore unpack params */
2076 ctx
->Unpack
= unpackSave
;
2078 _mesa_meta_end(ctx
);
2082 alpha_test_raster_color(struct gl_context
*ctx
)
2084 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2085 GLfloat ref
= ctx
->Color
.AlphaRef
;
2087 switch (ctx
->Color
.AlphaFunc
) {
2093 return alpha
== ref
;
2095 return alpha
<= ref
;
2099 return alpha
!= ref
;
2101 return alpha
>= ref
;
2111 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2112 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2113 * tracker would improve performance a lot.
2116 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2117 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2118 const struct gl_pixelstore_attrib
*unpack
,
2119 const GLubyte
*bitmap1
)
2121 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2122 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2123 const GLenum texIntFormat
= GL_ALPHA
;
2124 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2127 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2129 struct vertex verts
[4];
2134 * Check if swrast fallback is needed.
2136 if (ctx
->_ImageTransferState
||
2137 ctx
->FragmentProgram
._Enabled
||
2139 ctx
->Texture
._EnabledUnits
||
2140 width
> tex
->MaxSize
||
2141 height
> tex
->MaxSize
) {
2142 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2146 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2149 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2150 * but a there's a few things we need to override:
2152 _mesa_meta_begin(ctx
, (META_ALPHA_TEST
|
2154 META_RASTERIZATION
|
2161 if (bitmap
->ArrayObj
== 0) {
2162 /* one-time setup */
2164 /* create vertex array object */
2165 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2166 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2168 /* create vertex array buffer */
2169 _mesa_GenBuffersARB(1, &bitmap
->VBO
);
2170 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2171 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2172 NULL
, GL_DYNAMIC_DRAW_ARB
);
2174 /* setup vertex arrays */
2175 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2176 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2177 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2178 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2179 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2180 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2183 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2184 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2187 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2189 /* vertex positions, texcoords, colors (after texture allocation!) */
2191 const GLfloat x0
= (GLfloat
) x
;
2192 const GLfloat y0
= (GLfloat
) y
;
2193 const GLfloat x1
= (GLfloat
) (x
+ width
);
2194 const GLfloat y1
= (GLfloat
) (y
+ height
);
2195 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2206 verts
[1].s
= tex
->Sright
;
2211 verts
[2].s
= tex
->Sright
;
2212 verts
[2].t
= tex
->Ttop
;
2217 verts
[3].t
= tex
->Ttop
;
2219 for (i
= 0; i
< 4; i
++) {
2220 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2221 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2222 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2223 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2226 /* upload new vertex data */
2227 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2230 /* choose different foreground/background alpha values */
2231 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2232 bg
= (fg
> 127 ? 0 : 255);
2234 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2236 _mesa_meta_end(ctx
);
2240 bitmap8
= (GLubyte
*) malloc(width
* height
);
2242 memset(bitmap8
, bg
, width
* height
);
2243 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2244 bitmap8
, width
, fg
);
2246 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2248 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2249 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2251 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2252 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2254 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2256 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2261 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2263 _mesa_meta_end(ctx
);
2268 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2269 * software fallback. The fallback path will require that the texture
2270 * images are mapped.
2271 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2274 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
2275 struct gl_texture_object
*texObj
)
2277 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2278 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2279 struct gl_texture_image
*baseImage
;
2283 /* check for fallbacks */
2284 if (!ctx
->Extensions
.EXT_framebuffer_object
||
2285 target
== GL_TEXTURE_3D
) {
2289 srcLevel
= texObj
->BaseLevel
;
2290 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2291 if (!baseImage
|| _mesa_is_format_compressed(baseImage
->TexFormat
)) {
2296 * Test that we can actually render in the texture's format.
2299 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2300 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2302 if (target
== GL_TEXTURE_1D
) {
2303 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2304 GL_COLOR_ATTACHMENT0_EXT
,
2305 target
, texObj
->Name
, srcLevel
);
2308 /* other work is needed to enable 3D mipmap generation */
2309 else if (target
== GL_TEXTURE_3D
) {
2311 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2312 GL_COLOR_ATTACHMENT0_EXT
,
2313 target
, texObj
->Name
, srcLevel
, zoffset
);
2318 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2319 GL_COLOR_ATTACHMENT0_EXT
,
2320 target
, texObj
->Name
, srcLevel
);
2323 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2325 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2327 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2336 * Called via ctx->Driver.GenerateMipmap()
2337 * Note: texture borders and 3D texture support not yet complete.
2340 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
2341 struct gl_texture_object
*texObj
)
2343 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2345 GLfloat x
, y
, s
, t
, r
;
2347 struct vertex verts
[4];
2348 const GLuint baseLevel
= texObj
->BaseLevel
;
2349 const GLuint maxLevel
= texObj
->MaxLevel
;
2350 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
2351 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
2352 const GLint maxLevelSave
= texObj
->MaxLevel
;
2353 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
2354 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
2355 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
2356 const GLenum wrapRSave
= texObj
->Sampler
.WrapR
;
2357 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2358 const GLuint original_active_unit
= ctx
->Texture
.CurrentUnit
;
2363 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
2364 _mesa_generate_mipmap(ctx
, target
, texObj
);
2368 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
2369 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
2370 faceTarget
= target
;
2371 target
= GL_TEXTURE_CUBE_MAP
;
2374 faceTarget
= target
;
2377 _mesa_meta_begin(ctx
, META_ALL
);
2379 if (original_active_unit
!= 0)
2380 _mesa_BindTexture(target
, texObj
->Name
);
2382 if (mipmap
->ArrayObj
== 0) {
2383 /* one-time setup */
2385 /* create vertex array object */
2386 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
2387 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
2389 /* create vertex array buffer */
2390 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
2391 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2392 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2393 NULL
, GL_DYNAMIC_DRAW_ARB
);
2395 /* setup vertex arrays */
2396 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2397 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2398 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2399 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2402 _mesa_BindVertexArray(mipmap
->ArrayObj
);
2403 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2407 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2409 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2411 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
2412 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
2413 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
2414 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2415 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2416 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
2418 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2420 /* setup texcoords once (XXX what about border?) */
2421 switch (faceTarget
) {
2443 static const GLfloat st
[4][2] = {
2444 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2448 /* loop over quad verts */
2449 for (i
= 0; i
< 4; i
++) {
2450 /* Compute sc = +/-scale and tc = +/-scale.
2451 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2452 * though that can still sometimes happen with this scale factor...
2454 const GLfloat scale
= 0.9999f
;
2455 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2456 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2458 switch (faceTarget
) {
2459 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2464 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2469 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2474 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2479 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2484 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2496 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2498 /* setup vertex positions */
2509 /* upload new vertex data */
2510 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2513 /* setup projection matrix */
2514 _mesa_MatrixMode(GL_PROJECTION
);
2515 _mesa_LoadIdentity();
2516 _mesa_Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
2518 /* texture is already locked, unlock now */
2519 _mesa_unlock_texture(ctx
, texObj
);
2521 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
2522 const struct gl_texture_image
*srcImage
;
2523 const GLuint srcLevel
= dstLevel
- 1;
2524 GLsizei srcWidth
, srcHeight
, srcDepth
;
2525 GLsizei dstWidth
, dstHeight
, dstDepth
;
2528 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
2529 assert(srcImage
->Border
== 0); /* XXX we can fix this */
2531 /* src size w/out border */
2532 srcWidth
= srcImage
->Width
- 2 * border
;
2533 srcHeight
= srcImage
->Height
- 2 * border
;
2534 srcDepth
= srcImage
->Depth
- 2 * border
;
2536 /* new dst size w/ border */
2537 dstWidth
= MAX2(1, srcWidth
/ 2) + 2 * border
;
2538 dstHeight
= MAX2(1, srcHeight
/ 2) + 2 * border
;
2539 dstDepth
= MAX2(1, srcDepth
/ 2) + 2 * border
;
2541 if (dstWidth
== srcImage
->Width
&&
2542 dstHeight
== srcImage
->Height
&&
2543 dstDepth
== srcImage
->Depth
) {
2548 /* Set MaxLevel large enough to hold the new level when we allocate it */
2549 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
2551 /* Create empty dest image */
2552 if (target
== GL_TEXTURE_1D
) {
2553 _mesa_TexImage1D(target
, dstLevel
, srcImage
->InternalFormat
,
2555 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2557 else if (target
== GL_TEXTURE_3D
) {
2558 _mesa_TexImage3D(target
, dstLevel
, srcImage
->InternalFormat
,
2559 dstWidth
, dstHeight
, dstDepth
, border
,
2560 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2564 _mesa_TexImage2D(faceTarget
, dstLevel
, srcImage
->InternalFormat
,
2565 dstWidth
, dstHeight
, border
,
2566 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2568 if (target
== GL_TEXTURE_CUBE_MAP
) {
2569 /* If texturing from a cube, we need to make sure all src faces
2570 * have been defined (even if we're not sampling from them.)
2571 * Otherwise the texture object will be 'incomplete' and
2572 * texturing from it will not be allowed.
2575 for (face
= 0; face
< 6; face
++) {
2576 if (!texObj
->Image
[face
][srcLevel
] ||
2577 texObj
->Image
[face
][srcLevel
]->Width
!= srcWidth
) {
2578 _mesa_TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ face
,
2579 srcLevel
, srcImage
->InternalFormat
,
2580 srcWidth
, srcHeight
, border
,
2581 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2587 /* limit minification to src level */
2588 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
2590 /* Set to draw into the current dstLevel */
2591 if (target
== GL_TEXTURE_1D
) {
2592 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2593 GL_COLOR_ATTACHMENT0_EXT
,
2598 else if (target
== GL_TEXTURE_3D
) {
2599 GLint zoffset
= 0; /* XXX unfinished */
2600 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2601 GL_COLOR_ATTACHMENT0_EXT
,
2608 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2609 GL_COLOR_ATTACHMENT0_EXT
,
2615 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
2618 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2619 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2624 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
2625 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
2627 /* setup viewport */
2628 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
2630 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2633 _mesa_lock_texture(ctx
, texObj
); /* relock */
2635 _mesa_meta_end(ctx
);
2637 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
2638 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
2639 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
2640 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
2641 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
2642 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
2643 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, wrapRSave
);
2645 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2650 * Determine the GL data type to use for the temporary image read with
2651 * ReadPixels() and passed to Tex[Sub]Image().
2654 get_temp_image_type(struct gl_context
*ctx
, GLenum baseFormat
)
2656 switch (baseFormat
) {
2661 case GL_LUMINANCE_ALPHA
:
2663 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2664 return GL_UNSIGNED_BYTE
;
2665 else if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2666 return GL_UNSIGNED_SHORT
;
2669 case GL_DEPTH_COMPONENT
:
2670 return GL_UNSIGNED_INT
;
2671 case GL_DEPTH_STENCIL
:
2672 return GL_UNSIGNED_INT_24_8
;
2674 _mesa_problem(ctx
, "Unexpected format in get_temp_image_type()");
2681 * Helper for _mesa_meta_CopyTexImage1/2D() functions.
2682 * Have to be careful with locking and meta state for pixel transfer.
2685 copy_tex_image(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
2686 GLenum internalFormat
, GLint x
, GLint y
,
2687 GLsizei width
, GLsizei height
, GLint border
)
2689 struct gl_texture_object
*texObj
;
2690 struct gl_texture_image
*texImage
;
2691 GLenum format
, type
;
2694 struct gl_renderbuffer
*read_rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
2696 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2697 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2699 /* Choose format/type for temporary image buffer */
2700 format
= _mesa_base_tex_format(ctx
, internalFormat
);
2702 if (format
== GL_LUMINANCE
&&
2703 _mesa_get_format_base_format(read_rb
->Format
) != GL_LUMINANCE
) {
2704 /* The glReadPixels() path will convert RGB to luminance by
2705 * summing R+G+B. glCopyTexImage() is supposed to behave as
2706 * glCopyPixels, which doesn't do that change, and instead
2707 * leaves it up to glTexImage which converts RGB to luminance by
2708 * just taking the R channel. To avoid glReadPixels() trashing
2709 * our data, use RGBA for our temporary image.
2714 type
= get_temp_image_type(ctx
, format
);
2715 bpp
= _mesa_bytes_per_pixel(format
, type
);
2717 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_image()");
2722 * Alloc image buffer (XXX could use a PBO)
2724 buf
= malloc(width
* height
* bpp
);
2726 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
2730 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2733 * Read image from framebuffer (disable pixel transfer ops)
2735 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2736 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2737 format
, type
, &ctx
->Pack
, buf
);
2738 _mesa_meta_end(ctx
);
2740 if (texImage
->Data
) {
2741 ctx
->Driver
.FreeTexImageData(ctx
, texImage
);
2744 /* The texture's format was already chosen in _mesa_CopyTexImage() */
2745 ASSERT(texImage
->TexFormat
!= MESA_FORMAT_NONE
);
2748 * Store texture data (with pixel transfer ops)
2750 _mesa_meta_begin(ctx
, META_PIXEL_STORE
);
2752 _mesa_update_state(ctx
); /* to update pixel transfer state */
2754 if (target
== GL_TEXTURE_1D
) {
2755 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
2756 width
, border
, format
, type
,
2757 buf
, &ctx
->Unpack
, texObj
, texImage
);
2760 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
2761 width
, height
, border
, format
, type
,
2762 buf
, &ctx
->Unpack
, texObj
, texImage
);
2764 _mesa_meta_end(ctx
);
2766 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
2773 _mesa_meta_CopyTexImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2774 GLenum internalFormat
, GLint x
, GLint y
,
2775 GLsizei width
, GLint border
)
2777 copy_tex_image(ctx
, 1, target
, level
, internalFormat
, x
, y
,
2783 _mesa_meta_CopyTexImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2784 GLenum internalFormat
, GLint x
, GLint y
,
2785 GLsizei width
, GLsizei height
, GLint border
)
2787 copy_tex_image(ctx
, 2, target
, level
, internalFormat
, x
, y
,
2788 width
, height
, border
);
2794 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2795 * Have to be careful with locking and meta state for pixel transfer.
2798 copy_tex_sub_image(struct gl_context
*ctx
,
2799 GLuint dims
, GLenum target
, GLint level
,
2800 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2802 GLsizei width
, GLsizei height
)
2804 struct gl_texture_object
*texObj
;
2805 struct gl_texture_image
*texImage
;
2806 GLenum format
, type
;
2810 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2811 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2813 /* Choose format/type for temporary image buffer */
2814 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
2815 type
= get_temp_image_type(ctx
, format
);
2816 bpp
= _mesa_bytes_per_pixel(format
, type
);
2818 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_sub_image()");
2823 * Alloc image buffer (XXX could use a PBO)
2825 buf
= malloc(width
* height
* bpp
);
2827 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
2831 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2834 * Read image from framebuffer (disable pixel transfer ops)
2836 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2837 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2838 format
, type
, &ctx
->Pack
, buf
);
2839 _mesa_meta_end(ctx
);
2841 _mesa_update_state(ctx
); /* to update pixel transfer state */
2844 * Store texture data (with pixel transfer ops)
2846 _mesa_meta_begin(ctx
, META_PIXEL_STORE
);
2847 if (target
== GL_TEXTURE_1D
) {
2848 ctx
->Driver
.TexSubImage1D(ctx
, target
, level
, xoffset
,
2849 width
, format
, type
, buf
,
2850 &ctx
->Unpack
, texObj
, texImage
);
2852 else if (target
== GL_TEXTURE_3D
) {
2853 ctx
->Driver
.TexSubImage3D(ctx
, target
, level
, xoffset
, yoffset
, zoffset
,
2854 width
, height
, 1, format
, type
, buf
,
2855 &ctx
->Unpack
, texObj
, texImage
);
2858 ctx
->Driver
.TexSubImage2D(ctx
, target
, level
, xoffset
, yoffset
,
2859 width
, height
, format
, type
, buf
,
2860 &ctx
->Unpack
, texObj
, texImage
);
2862 _mesa_meta_end(ctx
);
2864 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
2871 _mesa_meta_CopyTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2873 GLint x
, GLint y
, GLsizei width
)
2875 copy_tex_sub_image(ctx
, 1, target
, level
, xoffset
, 0, 0,
2881 _mesa_meta_CopyTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2882 GLint xoffset
, GLint yoffset
,
2884 GLsizei width
, GLsizei height
)
2886 copy_tex_sub_image(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2887 x
, y
, width
, height
);
2892 _mesa_meta_CopyTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2893 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2895 GLsizei width
, GLsizei height
)
2897 copy_tex_sub_image(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2898 x
, y
, width
, height
);
2903 _mesa_meta_CopyColorTable(struct gl_context
*ctx
,
2904 GLenum target
, GLenum internalformat
,
2905 GLint x
, GLint y
, GLsizei width
)
2909 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2911 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyColorTable");
2916 * Read image from framebuffer (disable pixel transfer ops)
2918 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2919 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2920 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2922 _mesa_ColorTable(target
, internalformat
, width
, GL_RGBA
, GL_FLOAT
, buf
);
2924 _mesa_meta_end(ctx
);
2931 _mesa_meta_CopyColorSubTable(struct gl_context
*ctx
,GLenum target
, GLsizei start
,
2932 GLint x
, GLint y
, GLsizei width
)
2936 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2938 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyColorSubTable");
2943 * Read image from framebuffer (disable pixel transfer ops)
2945 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2946 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2947 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2949 _mesa_ColorSubTable(target
, start
, width
, GL_RGBA
, GL_FLOAT
, buf
);
2951 _mesa_meta_end(ctx
);