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/depth.h"
44 #include "main/enable.h"
45 #include "main/fbobject.h"
46 #include "main/formats.h"
47 #include "main/image.h"
48 #include "main/macros.h"
49 #include "main/matrix.h"
50 #include "main/mipmap.h"
52 #include "main/polygon.h"
53 #include "main/readpix.h"
54 #include "main/scissor.h"
55 #include "main/shaderapi.h"
56 #include "main/shaderobj.h"
57 #include "main/state.h"
58 #include "main/stencil.h"
59 #include "main/texobj.h"
60 #include "main/texenv.h"
61 #include "main/teximage.h"
62 #include "main/texparam.h"
63 #include "main/texstate.h"
64 #include "main/varray.h"
65 #include "main/viewport.h"
66 #include "program/program.h"
67 #include "swrast/swrast.h"
68 #include "drivers/common/meta.h"
71 /** Return offset in bytes of the field within a vertex struct */
72 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
76 * Flags passed to _mesa_meta_begin().
80 #define META_ALPHA_TEST 0x1
81 #define META_BLEND 0x2 /**< includes logicop */
82 #define META_COLOR_MASK 0x4
83 #define META_DEPTH_TEST 0x8
85 #define META_PIXEL_STORE 0x20
86 #define META_PIXEL_TRANSFER 0x40
87 #define META_RASTERIZATION 0x80
88 #define META_SCISSOR 0x100
89 #define META_SHADER 0x200
90 #define META_STENCIL_TEST 0x400
91 #define META_TRANSFORM 0x800 /**< modelview, projection, clip planes */
92 #define META_TEXTURE 0x1000
93 #define META_VERTEX 0x2000
94 #define META_VIEWPORT 0x4000
99 * State which we may save/restore across meta ops.
100 * XXX this may be incomplete...
104 GLbitfield SavedState
; /**< bitmask of META_* flags */
106 /** META_ALPHA_TEST */
107 GLboolean AlphaEnabled
;
112 GLbitfield BlendEnabled
;
113 GLboolean ColorLogicOpEnabled
;
115 /** META_COLOR_MASK */
116 GLubyte ColorMask
[MAX_DRAW_BUFFERS
][4];
118 /** META_DEPTH_TEST */
119 struct gl_depthbuffer_attrib Depth
;
124 /** META_PIXEL_STORE */
125 struct gl_pixelstore_attrib Pack
, Unpack
;
127 /** META_PIXEL_TRANSFER */
128 GLfloat RedBias
, RedScale
;
129 GLfloat GreenBias
, GreenScale
;
130 GLfloat BlueBias
, BlueScale
;
131 GLfloat AlphaBias
, AlphaScale
;
132 GLfloat DepthBias
, DepthScale
;
133 GLboolean MapColorFlag
;
135 /** META_RASTERIZATION */
136 GLenum FrontPolygonMode
, BackPolygonMode
;
137 GLboolean PolygonOffset
;
138 GLboolean PolygonSmooth
;
139 GLboolean PolygonStipple
;
140 GLboolean PolygonCull
;
143 struct gl_scissor_attrib Scissor
;
146 GLboolean VertexProgramEnabled
;
147 struct gl_vertex_program
*VertexProgram
;
148 GLboolean FragmentProgramEnabled
;
149 struct gl_fragment_program
*FragmentProgram
;
150 struct gl_shader_program
*VertexShader
;
151 struct gl_shader_program
*GeometryShader
;
152 struct gl_shader_program
*FragmentShader
;
153 struct gl_shader_program
*ActiveShader
;
155 /** META_STENCIL_TEST */
156 struct gl_stencil_attrib Stencil
;
158 /** META_TRANSFORM */
160 GLfloat ModelviewMatrix
[16];
161 GLfloat ProjectionMatrix
[16];
162 GLfloat TextureMatrix
[16];
163 GLbitfield ClipPlanesEnabled
;
167 GLuint ClientActiveUnit
;
168 /** for unit[0] only */
169 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
170 /** mask of TEXTURE_2D_BIT, etc */
171 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
172 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
173 GLuint EnvMode
; /* unit[0] only */
176 struct gl_array_object
*ArrayObj
;
177 struct gl_buffer_object
*ArrayBufferObj
;
180 GLint ViewportX
, ViewportY
, ViewportW
, ViewportH
;
181 GLclampd DepthNear
, DepthFar
;
183 /** Miscellaneous (always disabled) */
189 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
190 * This is currently shared by all the meta ops. But we could create a
191 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
196 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
197 GLsizei MinSize
; /**< Min texture size to allocate */
198 GLsizei MaxSize
; /**< Max possible texture size */
199 GLboolean NPOT
; /**< Non-power of two size OK? */
200 GLsizei Width
, Height
; /**< Current texture size */
202 GLfloat Sright
, Ttop
; /**< right, top texcoords */
207 * State for glBlitFramebufer()
218 * State for glClear()
228 * State for glCopyPixels()
238 * State for glDrawPixels()
244 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
245 GLuint DepthFP
; /**< Fragment program for drawing depth images */
250 * State for glBitmap()
256 struct temp_texture Tex
; /**< separate texture from other meta ops */
261 * State for _mesa_meta_generate_mipmap()
263 struct gen_mipmap_state
270 #define MAX_META_OPS_DEPTH 2
272 * All per-context meta state.
276 /** Stack of state saved during meta-ops */
277 struct save_state Save
[MAX_META_OPS_DEPTH
];
278 /** Save stack depth */
279 GLuint SaveStackDepth
;
281 struct temp_texture TempTex
;
283 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
284 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
285 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
286 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
287 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
288 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
293 * Initialize meta-ops for a context.
294 * To be called once during context creation.
297 _mesa_meta_init(struct gl_context
*ctx
)
301 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
306 * Free context meta-op state.
307 * To be called once during context destruction.
310 _mesa_meta_free(struct gl_context
*ctx
)
312 /* Note: Any textures, VBOs, etc, that we allocate should get
313 * freed by the normal context destruction code. But this would be
314 * the place to free other meta data someday.
322 * Enter meta state. This is like a light-weight version of glPushAttrib
323 * but it also resets most GL state back to default values.
325 * \param state bitmask of META_* flags indicating which attribute groups
326 * to save and reset to their defaults
329 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
331 struct save_state
*save
;
333 /* hope MAX_META_OPS_DEPTH is large enough */
334 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
336 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
337 memset(save
, 0, sizeof(*save
));
338 save
->SavedState
= state
;
340 if (state
& META_ALPHA_TEST
) {
341 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
342 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
343 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
344 if (ctx
->Color
.AlphaEnabled
)
345 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
348 if (state
& META_BLEND
) {
349 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
350 if (ctx
->Color
.BlendEnabled
) {
351 if (ctx
->Extensions
.EXT_draw_buffers2
) {
353 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
354 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
358 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
361 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
362 if (ctx
->Color
.ColorLogicOpEnabled
)
363 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
366 if (state
& META_COLOR_MASK
) {
367 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
368 sizeof(ctx
->Color
.ColorMask
));
369 if (!ctx
->Color
.ColorMask
[0][0] ||
370 !ctx
->Color
.ColorMask
[0][1] ||
371 !ctx
->Color
.ColorMask
[0][2] ||
372 !ctx
->Color
.ColorMask
[0][3])
373 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
376 if (state
& META_DEPTH_TEST
) {
377 save
->Depth
= ctx
->Depth
; /* struct copy */
379 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
382 if (state
& META_FOG
) {
383 save
->Fog
= ctx
->Fog
.Enabled
;
384 if (ctx
->Fog
.Enabled
)
385 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
388 if (state
& META_PIXEL_STORE
) {
389 save
->Pack
= ctx
->Pack
;
390 save
->Unpack
= ctx
->Unpack
;
391 ctx
->Pack
= ctx
->DefaultPacking
;
392 ctx
->Unpack
= ctx
->DefaultPacking
;
395 if (state
& META_PIXEL_TRANSFER
) {
396 save
->RedScale
= ctx
->Pixel
.RedScale
;
397 save
->RedBias
= ctx
->Pixel
.RedBias
;
398 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
399 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
400 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
401 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
402 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
403 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
404 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
405 ctx
->Pixel
.RedScale
= 1.0F
;
406 ctx
->Pixel
.RedBias
= 0.0F
;
407 ctx
->Pixel
.GreenScale
= 1.0F
;
408 ctx
->Pixel
.GreenBias
= 0.0F
;
409 ctx
->Pixel
.BlueScale
= 1.0F
;
410 ctx
->Pixel
.BlueBias
= 0.0F
;
411 ctx
->Pixel
.AlphaScale
= 1.0F
;
412 ctx
->Pixel
.AlphaBias
= 0.0F
;
413 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
415 ctx
->NewState
|=_NEW_PIXEL
;
418 if (state
& META_RASTERIZATION
) {
419 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
420 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
421 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
422 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
423 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
424 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
425 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
426 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
427 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
428 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
429 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
432 if (state
& META_SCISSOR
) {
433 save
->Scissor
= ctx
->Scissor
; /* struct copy */
434 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
437 if (state
& META_SHADER
) {
438 if (ctx
->Extensions
.ARB_vertex_program
) {
439 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
440 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
441 ctx
->VertexProgram
.Current
);
442 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
445 if (ctx
->Extensions
.ARB_fragment_program
) {
446 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
447 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
448 ctx
->FragmentProgram
.Current
);
449 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
452 if (ctx
->Extensions
.ARB_shader_objects
) {
453 _mesa_reference_shader_program(ctx
, &save
->VertexShader
,
454 ctx
->Shader
.CurrentVertexProgram
);
455 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
,
456 ctx
->Shader
.CurrentGeometryProgram
);
457 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
,
458 ctx
->Shader
.CurrentFragmentProgram
);
459 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
460 ctx
->Shader
.CurrentFragmentProgram
);
462 _mesa_UseProgramObjectARB(0);
466 if (state
& META_STENCIL_TEST
) {
467 save
->Stencil
= ctx
->Stencil
; /* struct copy */
468 if (ctx
->Stencil
.Enabled
)
469 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
470 /* NOTE: other stencil state not reset */
473 if (state
& META_TEXTURE
) {
476 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
477 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
478 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
480 /* Disable all texture units */
481 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
482 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
483 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
484 if (ctx
->Texture
.Unit
[u
].Enabled
||
485 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
486 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
487 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
488 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
489 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
490 if (ctx
->Extensions
.ARB_texture_cube_map
)
491 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
492 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
493 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
494 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
495 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
496 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
500 /* save current texture objects for unit[0] only */
501 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
502 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
503 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
506 /* set defaults for unit[0] */
507 _mesa_ActiveTextureARB(GL_TEXTURE0
);
508 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
509 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
512 if (state
& META_TRANSFORM
) {
513 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
514 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
515 16 * sizeof(GLfloat
));
516 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
517 16 * sizeof(GLfloat
));
518 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
519 16 * sizeof(GLfloat
));
520 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
521 /* set 1:1 vertex:pixel coordinate transform */
522 _mesa_ActiveTextureARB(GL_TEXTURE0
);
523 _mesa_MatrixMode(GL_TEXTURE
);
524 _mesa_LoadIdentity();
525 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
526 _mesa_MatrixMode(GL_MODELVIEW
);
527 _mesa_LoadIdentity();
528 _mesa_MatrixMode(GL_PROJECTION
);
529 _mesa_LoadIdentity();
530 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
531 0.0, ctx
->DrawBuffer
->Height
,
533 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
534 if (ctx
->Transform
.ClipPlanesEnabled
) {
536 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
537 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
542 if (state
& META_VERTEX
) {
543 /* save vertex array object state */
544 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
545 ctx
->Array
.ArrayObj
);
546 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
547 ctx
->Array
.ArrayBufferObj
);
548 /* set some default state? */
551 if (state
& META_VIEWPORT
) {
552 /* save viewport state */
553 save
->ViewportX
= ctx
->Viewport
.X
;
554 save
->ViewportY
= ctx
->Viewport
.Y
;
555 save
->ViewportW
= ctx
->Viewport
.Width
;
556 save
->ViewportH
= ctx
->Viewport
.Height
;
557 /* set viewport to match window size */
558 if (ctx
->Viewport
.X
!= 0 ||
559 ctx
->Viewport
.Y
!= 0 ||
560 ctx
->Viewport
.Width
!= ctx
->DrawBuffer
->Width
||
561 ctx
->Viewport
.Height
!= ctx
->DrawBuffer
->Height
) {
562 _mesa_set_viewport(ctx
, 0, 0,
563 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
565 /* save depth range state */
566 save
->DepthNear
= ctx
->Viewport
.Near
;
567 save
->DepthFar
= ctx
->Viewport
.Far
;
568 /* set depth range to default */
569 _mesa_DepthRange(0.0, 1.0);
574 save
->Lighting
= ctx
->Light
.Enabled
;
575 if (ctx
->Light
.Enabled
)
576 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
582 * Leave meta state. This is like a light-weight version of glPopAttrib().
585 _mesa_meta_end(struct gl_context
*ctx
)
587 struct save_state
*save
= &ctx
->Meta
->Save
[--ctx
->Meta
->SaveStackDepth
];
588 const GLbitfield state
= save
->SavedState
;
590 if (state
& META_ALPHA_TEST
) {
591 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
592 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
593 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
596 if (state
& META_BLEND
) {
597 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
598 if (ctx
->Extensions
.EXT_draw_buffers2
) {
600 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
601 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
605 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
608 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
609 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
612 if (state
& META_COLOR_MASK
) {
614 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
615 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
617 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
618 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
621 _mesa_ColorMaskIndexed(i
,
622 save
->ColorMask
[i
][0],
623 save
->ColorMask
[i
][1],
624 save
->ColorMask
[i
][2],
625 save
->ColorMask
[i
][3]);
631 if (state
& META_DEPTH_TEST
) {
632 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
633 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
634 _mesa_DepthFunc(save
->Depth
.Func
);
635 _mesa_DepthMask(save
->Depth
.Mask
);
638 if (state
& META_FOG
) {
639 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
642 if (state
& META_PIXEL_STORE
) {
643 ctx
->Pack
= save
->Pack
;
644 ctx
->Unpack
= save
->Unpack
;
647 if (state
& META_PIXEL_TRANSFER
) {
648 ctx
->Pixel
.RedScale
= save
->RedScale
;
649 ctx
->Pixel
.RedBias
= save
->RedBias
;
650 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
651 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
652 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
653 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
654 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
655 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
656 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
658 ctx
->NewState
|=_NEW_PIXEL
;
661 if (state
& META_RASTERIZATION
) {
662 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
663 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
664 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
665 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
666 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
667 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
670 if (state
& META_SCISSOR
) {
671 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
672 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
673 save
->Scissor
.Width
, save
->Scissor
.Height
);
676 if (state
& META_SHADER
) {
677 if (ctx
->Extensions
.ARB_vertex_program
) {
678 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
679 save
->VertexProgramEnabled
);
680 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
681 save
->VertexProgram
);
682 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
685 if (ctx
->Extensions
.ARB_fragment_program
) {
686 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
687 save
->FragmentProgramEnabled
);
688 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
689 save
->FragmentProgram
);
690 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
693 if (ctx
->Extensions
.ARB_vertex_shader
)
694 _mesa_use_shader_program(ctx
, GL_VERTEX_SHADER
, save
->VertexShader
);
696 if (ctx
->Extensions
.ARB_geometry_shader4
)
697 _mesa_use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
,
698 save
->GeometryShader
);
700 if (ctx
->Extensions
.ARB_fragment_shader
)
701 _mesa_use_shader_program(ctx
, GL_FRAGMENT_SHADER
,
702 save
->FragmentShader
);
704 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
708 if (state
& META_STENCIL_TEST
) {
709 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
711 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
712 _mesa_ClearStencil(stencil
->Clear
);
713 if (ctx
->Extensions
.EXT_stencil_two_side
) {
714 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
715 stencil
->TestTwoSide
);
716 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
717 ? GL_BACK
: GL_FRONT
);
720 _mesa_StencilFuncSeparate(GL_FRONT
,
721 stencil
->Function
[0],
723 stencil
->ValueMask
[0]);
724 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
725 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
726 stencil
->ZFailFunc
[0],
727 stencil
->ZPassFunc
[0]);
729 _mesa_StencilFuncSeparate(GL_BACK
,
730 stencil
->Function
[1],
732 stencil
->ValueMask
[1]);
733 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
734 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
735 stencil
->ZFailFunc
[1],
736 stencil
->ZPassFunc
[1]);
739 if (state
& META_TEXTURE
) {
742 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
744 /* restore texenv for unit[0] */
745 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
747 /* restore texture objects for unit[0] only */
748 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
749 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
750 save
->CurrentTexture
[tgt
]);
751 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
754 /* Re-enable textures, texgen */
755 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
756 if (save
->TexEnabled
[u
]) {
757 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
759 if (save
->TexEnabled
[u
] & TEXTURE_1D_BIT
)
760 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_TRUE
);
761 if (save
->TexEnabled
[u
] & TEXTURE_2D_BIT
)
762 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_TRUE
);
763 if (save
->TexEnabled
[u
] & TEXTURE_3D_BIT
)
764 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_TRUE
);
765 if (save
->TexEnabled
[u
] & TEXTURE_CUBE_BIT
)
766 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_TRUE
);
767 if (save
->TexEnabled
[u
] & TEXTURE_RECT_BIT
)
768 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_TRUE
);
771 if (save
->TexGenEnabled
[u
]) {
772 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
774 if (save
->TexGenEnabled
[u
] & S_BIT
)
775 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_TRUE
);
776 if (save
->TexGenEnabled
[u
] & T_BIT
)
777 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_TRUE
);
778 if (save
->TexGenEnabled
[u
] & R_BIT
)
779 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_TRUE
);
780 if (save
->TexGenEnabled
[u
] & Q_BIT
)
781 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_TRUE
);
785 /* restore current unit state */
786 _mesa_ActiveTextureARB(GL_TEXTURE0
+ save
->ActiveUnit
);
787 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ save
->ClientActiveUnit
);
790 if (state
& META_TRANSFORM
) {
791 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
792 _mesa_ActiveTextureARB(GL_TEXTURE0
);
793 _mesa_MatrixMode(GL_TEXTURE
);
794 _mesa_LoadMatrixf(save
->TextureMatrix
);
795 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
797 _mesa_MatrixMode(GL_MODELVIEW
);
798 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
800 _mesa_MatrixMode(GL_PROJECTION
);
801 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
803 _mesa_MatrixMode(save
->MatrixMode
);
805 if (save
->ClipPlanesEnabled
) {
807 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
808 if (save
->ClipPlanesEnabled
& (1 << i
)) {
809 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
815 if (state
& META_VERTEX
) {
816 /* restore vertex buffer object */
817 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
818 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
820 /* restore vertex array object */
821 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
822 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
825 if (state
& META_VIEWPORT
) {
826 if (save
->ViewportX
!= ctx
->Viewport
.X
||
827 save
->ViewportY
!= ctx
->Viewport
.Y
||
828 save
->ViewportW
!= ctx
->Viewport
.Width
||
829 save
->ViewportH
!= ctx
->Viewport
.Height
) {
830 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
831 save
->ViewportW
, save
->ViewportH
);
833 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
837 if (save
->Lighting
) {
838 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
844 * Convert Z from a normalized value in the range [0, 1] to an object-space
845 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
846 * default/identity ortho projection results in the original Z value.
847 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
848 * value comes from the clear value or raster position.
850 static INLINE GLfloat
851 invert_z(GLfloat normZ
)
853 GLfloat objZ
= 1.0 - 2.0 * normZ
;
859 * One-time init for a temp_texture object.
860 * Choose tex target, compute max tex size, etc.
863 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
865 /* prefer texture rectangle */
866 if (ctx
->Extensions
.NV_texture_rectangle
) {
867 tex
->Target
= GL_TEXTURE_RECTANGLE
;
868 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
872 /* use 2D texture, NPOT if possible */
873 tex
->Target
= GL_TEXTURE_2D
;
874 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
875 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
877 tex
->MinSize
= 16; /* 16 x 16 at least */
878 assert(tex
->MaxSize
> 0);
880 _mesa_GenTextures(1, &tex
->TexObj
);
885 * Return pointer to temp_texture info for non-bitmap ops.
886 * This does some one-time init if needed.
888 static struct temp_texture
*
889 get_temp_texture(struct gl_context
*ctx
)
891 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
894 init_temp_texture(ctx
, tex
);
902 * Return pointer to temp_texture info for _mesa_meta_bitmap().
903 * We use a separate texture for bitmaps to reduce texture
904 * allocation/deallocation.
906 static struct temp_texture
*
907 get_bitmap_temp_texture(struct gl_context
*ctx
)
909 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
912 init_temp_texture(ctx
, tex
);
920 * Compute the width/height of texture needed to draw an image of the
921 * given size. Return a flag indicating whether the current texture
922 * can be re-used (glTexSubImage2D) or if a new texture needs to be
923 * allocated (glTexImage2D).
924 * Also, compute s/t texcoords for drawing.
926 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
929 alloc_texture(struct temp_texture
*tex
,
930 GLsizei width
, GLsizei height
, GLenum intFormat
)
932 GLboolean newTex
= GL_FALSE
;
934 ASSERT(width
<= tex
->MaxSize
);
935 ASSERT(height
<= tex
->MaxSize
);
937 if (width
> tex
->Width
||
938 height
> tex
->Height
||
939 intFormat
!= tex
->IntFormat
) {
940 /* alloc new texture (larger or different format) */
943 /* use non-power of two size */
944 tex
->Width
= MAX2(tex
->MinSize
, width
);
945 tex
->Height
= MAX2(tex
->MinSize
, height
);
948 /* find power of two size */
950 w
= h
= tex
->MinSize
;
959 tex
->IntFormat
= intFormat
;
964 /* compute texcoords */
965 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
966 tex
->Sright
= (GLfloat
) width
;
967 tex
->Ttop
= (GLfloat
) height
;
970 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
971 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
979 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
982 setup_copypix_texture(struct temp_texture
*tex
,
984 GLint srcX
, GLint srcY
,
985 GLsizei width
, GLsizei height
, GLenum intFormat
,
988 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
989 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
990 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
991 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
993 /* copy framebuffer image to texture */
995 /* create new tex image */
996 if (tex
->Width
== width
&& tex
->Height
== height
) {
997 /* create new tex with framebuffer data */
998 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
999 srcX
, srcY
, width
, height
, 0);
1002 /* create empty texture */
1003 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1004 tex
->Width
, tex
->Height
, 0,
1005 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1007 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1008 0, 0, srcX
, srcY
, width
, height
);
1012 /* replace existing tex image */
1013 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1014 0, 0, srcX
, srcY
, width
, height
);
1020 * Setup/load texture for glDrawPixels.
1023 setup_drawpix_texture(struct gl_context
*ctx
,
1024 struct temp_texture
*tex
,
1026 GLenum texIntFormat
,
1027 GLsizei width
, GLsizei height
,
1028 GLenum format
, GLenum type
,
1029 const GLvoid
*pixels
)
1031 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1032 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1033 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1034 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1036 /* copy pixel data to texture */
1038 /* create new tex image */
1039 if (tex
->Width
== width
&& tex
->Height
== height
) {
1040 /* create new tex and load image data */
1041 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1042 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1045 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1047 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1048 ctx
->Unpack
.BufferObj
);
1049 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1050 /* create empty texture */
1051 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1052 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1053 if (save_unpack_obj
!= NULL
)
1054 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
,
1055 save_unpack_obj
->Name
);
1057 _mesa_TexSubImage2D(tex
->Target
, 0,
1058 0, 0, width
, height
, format
, type
, pixels
);
1062 /* replace existing tex image */
1063 _mesa_TexSubImage2D(tex
->Target
, 0,
1064 0, 0, width
, height
, format
, type
, pixels
);
1071 * One-time init for drawing depth pixels.
1074 init_blit_depth_pixels(struct gl_context
*ctx
)
1076 static const char *program
=
1078 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1081 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1082 struct temp_texture
*tex
= get_temp_texture(ctx
);
1083 const char *texTarget
;
1085 assert(blit
->DepthFP
== 0);
1087 /* replace %s with "RECT" or "2D" */
1088 assert(strlen(program
) + 4 < sizeof(program2
));
1089 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1093 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1095 _mesa_GenPrograms(1, &blit
->DepthFP
);
1096 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1097 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1098 strlen(program2
), (const GLubyte
*) program2
);
1103 * Try to do a glBlitFramebuffer using no-copy texturing.
1104 * We can do this when the src renderbuffer is actually a texture.
1105 * But if the src buffer == dst buffer we cannot do this.
1107 * \return new buffer mask indicating the buffers left to blit using the
1111 blitframebuffer_texture(struct gl_context
*ctx
,
1112 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1113 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1114 GLbitfield mask
, GLenum filter
)
1116 if (mask
& GL_COLOR_BUFFER_BIT
) {
1117 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1118 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1119 const struct gl_renderbuffer_attachment
*drawAtt
=
1120 &drawFb
->Attachment
[drawFb
->_ColorDrawBufferIndexes
[0]];
1121 const struct gl_renderbuffer_attachment
*readAtt
=
1122 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1124 if (readAtt
&& readAtt
->Texture
) {
1125 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1126 const GLuint srcLevel
= readAtt
->TextureLevel
;
1127 const GLenum minFilterSave
= texObj
->MinFilter
;
1128 const GLenum magFilterSave
= texObj
->MagFilter
;
1129 const GLint baseLevelSave
= texObj
->BaseLevel
;
1130 const GLint maxLevelSave
= texObj
->MaxLevel
;
1131 const GLenum wrapSSave
= texObj
->WrapS
;
1132 const GLenum wrapTSave
= texObj
->WrapT
;
1133 const GLenum target
= texObj
->Target
;
1135 if (drawAtt
->Texture
== readAtt
->Texture
) {
1136 /* Can't use same texture as both the source and dest. We need
1137 * to handle overlapping blits and besides, some hw may not
1143 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1144 /* Can't handle other texture types at this time */
1149 printf("Blit from texture!\n");
1150 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1151 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1154 /* Prepare src texture state */
1155 _mesa_BindTexture(target
, texObj
->Name
);
1156 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
1157 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
1158 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1159 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1160 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1162 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1163 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1164 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1165 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1167 /* Prepare vertex data (the VBO was previously created and bound) */
1172 struct vertex verts
[4];
1173 GLfloat s0
, t0
, s1
, t1
;
1175 if (target
== GL_TEXTURE_2D
) {
1176 const struct gl_texture_image
*texImage
1177 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1178 s0
= srcX0
/ (float) texImage
->Width
;
1179 s1
= srcX1
/ (float) texImage
->Width
;
1180 t0
= srcY0
/ (float) texImage
->Height
;
1181 t1
= srcY1
/ (float) texImage
->Height
;
1184 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1191 verts
[0].x
= (GLfloat
) dstX0
;
1192 verts
[0].y
= (GLfloat
) dstY0
;
1193 verts
[1].x
= (GLfloat
) dstX1
;
1194 verts
[1].y
= (GLfloat
) dstY0
;
1195 verts
[2].x
= (GLfloat
) dstX1
;
1196 verts
[2].y
= (GLfloat
) dstY1
;
1197 verts
[3].x
= (GLfloat
) dstX0
;
1198 verts
[3].y
= (GLfloat
) dstY1
;
1209 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1212 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1214 /* Restore texture object state, the texture binding will
1215 * be restored by _mesa_meta_end().
1217 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
1218 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
1219 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1220 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1221 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1223 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
1224 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
1226 /* Done with color buffer */
1227 mask
&= ~GL_COLOR_BUFFER_BIT
;
1236 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1237 * of texture mapping and polygon rendering.
1240 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1241 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1242 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1243 GLbitfield mask
, GLenum filter
)
1245 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1246 struct temp_texture
*tex
= get_temp_texture(ctx
);
1247 const GLsizei maxTexSize
= tex
->MaxSize
;
1248 const GLint srcX
= MIN2(srcX0
, srcX1
);
1249 const GLint srcY
= MIN2(srcY0
, srcY1
);
1250 const GLint srcW
= abs(srcX1
- srcX0
);
1251 const GLint srcH
= abs(srcY1
- srcY0
);
1252 const GLboolean srcFlipX
= srcX1
< srcX0
;
1253 const GLboolean srcFlipY
= srcY1
< srcY0
;
1257 struct vertex verts
[4];
1260 if (srcW
> maxTexSize
|| srcH
> maxTexSize
) {
1261 /* XXX avoid this fallback */
1262 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1263 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1279 /* only scissor effects blit so save/clear all other relevant state */
1280 _mesa_meta_begin(ctx
, ~META_SCISSOR
);
1282 if (blit
->ArrayObj
== 0) {
1283 /* one-time setup */
1285 /* create vertex array object */
1286 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1287 _mesa_BindVertexArray(blit
->ArrayObj
);
1289 /* create vertex array buffer */
1290 _mesa_GenBuffersARB(1, &blit
->VBO
);
1291 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1292 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1293 NULL
, GL_DYNAMIC_DRAW_ARB
);
1295 /* setup vertex arrays */
1296 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1297 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1298 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1299 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1302 _mesa_BindVertexArray(blit
->ArrayObj
);
1303 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1306 /* Try faster, direct texture approach first */
1307 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1308 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1310 _mesa_meta_end(ctx
);
1314 /* Continue with "normal" approach which involves copying the src rect
1315 * into a temporary texture and is "blitted" by drawing a textured quad.
1318 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1320 /* vertex positions/texcoords (after texture allocation!) */
1322 verts
[0].x
= (GLfloat
) dstX0
;
1323 verts
[0].y
= (GLfloat
) dstY0
;
1324 verts
[1].x
= (GLfloat
) dstX1
;
1325 verts
[1].y
= (GLfloat
) dstY0
;
1326 verts
[2].x
= (GLfloat
) dstX1
;
1327 verts
[2].y
= (GLfloat
) dstY1
;
1328 verts
[3].x
= (GLfloat
) dstX0
;
1329 verts
[3].y
= (GLfloat
) dstY1
;
1333 verts
[1].s
= tex
->Sright
;
1335 verts
[2].s
= tex
->Sright
;
1336 verts
[2].t
= tex
->Ttop
;
1338 verts
[3].t
= tex
->Ttop
;
1340 /* upload new vertex data */
1341 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1344 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1346 if (mask
& GL_COLOR_BUFFER_BIT
) {
1347 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1349 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1350 mask
&= ~GL_COLOR_BUFFER_BIT
;
1353 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1354 GLuint
*tmp
= (GLuint
*) malloc(srcW
* srcH
* sizeof(GLuint
));
1357 init_blit_depth_pixels(ctx
);
1359 /* maybe change tex format here */
1360 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1362 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1363 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1365 setup_drawpix_texture(ctx
, tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1366 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1368 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1369 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1370 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1371 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1372 _mesa_DepthFunc(GL_ALWAYS
);
1373 _mesa_DepthMask(GL_TRUE
);
1375 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1376 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1382 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1383 /* XXX can't easily do stencil */
1386 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1388 _mesa_meta_end(ctx
);
1391 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1392 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1398 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1401 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1403 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1405 GLfloat x
, y
, z
, r
, g
, b
, a
;
1407 struct vertex verts
[4];
1408 /* save all state but scissor, pixel pack/unpack */
1409 GLbitfield metaSave
= META_ALL
- META_SCISSOR
- META_PIXEL_STORE
;
1410 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1412 if (buffers
& BUFFER_BITS_COLOR
) {
1413 /* if clearing color buffers, don't save/restore colormask */
1414 metaSave
-= META_COLOR_MASK
;
1417 _mesa_meta_begin(ctx
, metaSave
);
1419 if (clear
->ArrayObj
== 0) {
1420 /* one-time setup */
1422 /* create vertex array object */
1423 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1424 _mesa_BindVertexArray(clear
->ArrayObj
);
1426 /* create vertex array buffer */
1427 _mesa_GenBuffersARB(1, &clear
->VBO
);
1428 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1430 /* setup vertex arrays */
1431 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1432 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1433 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1434 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1437 _mesa_BindVertexArray(clear
->ArrayObj
);
1438 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1441 /* GL_COLOR_BUFFER_BIT */
1442 if (buffers
& BUFFER_BITS_COLOR
) {
1443 /* leave colormask, glDrawBuffer state as-is */
1446 ASSERT(metaSave
& META_COLOR_MASK
);
1447 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1450 /* GL_DEPTH_BUFFER_BIT */
1451 if (buffers
& BUFFER_BIT_DEPTH
) {
1452 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1453 _mesa_DepthFunc(GL_ALWAYS
);
1454 _mesa_DepthMask(GL_TRUE
);
1457 assert(!ctx
->Depth
.Test
);
1460 /* GL_STENCIL_BUFFER_BIT */
1461 if (buffers
& BUFFER_BIT_STENCIL
) {
1462 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1463 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1464 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1465 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1466 ctx
->Stencil
.Clear
& stencilMax
,
1467 ctx
->Stencil
.WriteMask
[0]);
1470 assert(!ctx
->Stencil
.Enabled
);
1473 /* vertex positions/colors */
1475 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1476 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1477 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1478 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1479 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1496 for (i
= 0; i
< 4; i
++) {
1497 verts
[i
].r
= ctx
->Color
.ClearColor
[0];
1498 verts
[i
].g
= ctx
->Color
.ClearColor
[1];
1499 verts
[i
].b
= ctx
->Color
.ClearColor
[2];
1500 verts
[i
].a
= ctx
->Color
.ClearColor
[3];
1503 /* upload new vertex data */
1504 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1505 GL_DYNAMIC_DRAW_ARB
);
1509 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1511 _mesa_meta_end(ctx
);
1516 * Meta implementation of ctx->Driver.CopyPixels() in terms
1517 * of texture mapping and polygon rendering.
1520 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
1521 GLsizei width
, GLsizei height
,
1522 GLint dstX
, GLint dstY
, GLenum type
)
1524 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1525 struct temp_texture
*tex
= get_temp_texture(ctx
);
1527 GLfloat x
, y
, z
, s
, t
;
1529 struct vertex verts
[4];
1531 GLenum intFormat
= GL_RGBA
;
1533 if (type
!= GL_COLOR
||
1534 ctx
->_ImageTransferState
||
1536 width
> tex
->MaxSize
||
1537 height
> tex
->MaxSize
) {
1538 /* XXX avoid this fallback */
1539 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1543 /* Most GL state applies to glCopyPixels, but a there's a few things
1544 * we need to override:
1546 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1553 if (copypix
->ArrayObj
== 0) {
1554 /* one-time setup */
1556 /* create vertex array object */
1557 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
1558 _mesa_BindVertexArray(copypix
->ArrayObj
);
1560 /* create vertex array buffer */
1561 _mesa_GenBuffersARB(1, ©pix
->VBO
);
1562 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1563 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1564 NULL
, GL_DYNAMIC_DRAW_ARB
);
1566 /* setup vertex arrays */
1567 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1568 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1569 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1570 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1573 _mesa_BindVertexArray(copypix
->ArrayObj
);
1574 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1577 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
1579 /* vertex positions, texcoords (after texture allocation!) */
1581 const GLfloat dstX0
= (GLfloat
) dstX
;
1582 const GLfloat dstY0
= (GLfloat
) dstY
;
1583 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1584 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1585 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1595 verts
[1].s
= tex
->Sright
;
1600 verts
[2].s
= tex
->Sright
;
1601 verts
[2].t
= tex
->Ttop
;
1606 verts
[3].t
= tex
->Ttop
;
1608 /* upload new vertex data */
1609 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1612 /* Alloc/setup texture */
1613 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
1614 GL_RGBA
, GL_NEAREST
);
1616 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1618 /* draw textured quad */
1619 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1621 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1623 _mesa_meta_end(ctx
);
1629 * When the glDrawPixels() image size is greater than the max rectangle
1630 * texture size we use this function to break the glDrawPixels() image
1631 * into tiles which fit into the max texture size.
1634 tiled_draw_pixels(struct gl_context
*ctx
,
1636 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1637 GLenum format
, GLenum type
,
1638 const struct gl_pixelstore_attrib
*unpack
,
1639 const GLvoid
*pixels
)
1641 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
1644 if (tileUnpack
.RowLength
== 0)
1645 tileUnpack
.RowLength
= width
;
1647 for (i
= 0; i
< width
; i
+= tileSize
) {
1648 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
1649 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
1651 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
1653 for (j
= 0; j
< height
; j
+= tileSize
) {
1654 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
1655 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
1657 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
1659 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
1660 format
, type
, &tileUnpack
, pixels
);
1667 * One-time init for drawing stencil pixels.
1670 init_draw_stencil_pixels(struct gl_context
*ctx
)
1672 /* This program is run eight times, once for each stencil bit.
1673 * The stencil values to draw are found in an 8-bit alpha texture.
1674 * We read the texture/stencil value and test if bit 'b' is set.
1675 * If the bit is not set, use KIL to kill the fragment.
1676 * Finally, we use the stencil test to update the stencil buffer.
1678 * The basic algorithm for checking if a bit is set is:
1679 * if (is_odd(value / (1 << bit)))
1680 * result is one (or non-zero).
1683 * The program parameter contains three values:
1684 * parm.x = 255 / (1 << bit)
1688 static const char *program
=
1690 "PARAM parm = program.local[0]; \n"
1692 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1693 "# t = t * 255 / bit \n"
1694 "MUL t.x, t.a, parm.x; \n"
1697 "SUB t.x, t.x, t.y; \n"
1699 "MUL t.x, t.x, parm.y; \n"
1700 "# t = fract(t.x) \n"
1701 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
1702 "# t.x = (t.x == 0 ? 1 : 0) \n"
1703 "SGE t.x, -t.x, parm.z; \n"
1705 "# for debug only \n"
1706 "#MOV result.color, t.x; \n"
1708 char program2
[1000];
1709 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1710 struct temp_texture
*tex
= get_temp_texture(ctx
);
1711 const char *texTarget
;
1713 assert(drawpix
->StencilFP
== 0);
1715 /* replace %s with "RECT" or "2D" */
1716 assert(strlen(program
) + 4 < sizeof(program2
));
1717 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1721 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1723 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
1724 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1725 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1726 strlen(program2
), (const GLubyte
*) program2
);
1731 * One-time init for drawing depth pixels.
1734 init_draw_depth_pixels(struct gl_context
*ctx
)
1736 static const char *program
=
1738 "PARAM color = program.local[0]; \n"
1739 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1740 "MOV result.color, color; \n"
1743 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1744 struct temp_texture
*tex
= get_temp_texture(ctx
);
1745 const char *texTarget
;
1747 assert(drawpix
->DepthFP
== 0);
1749 /* replace %s with "RECT" or "2D" */
1750 assert(strlen(program
) + 4 < sizeof(program2
));
1751 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1755 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1757 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
1758 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1759 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1760 strlen(program2
), (const GLubyte
*) program2
);
1765 * Meta implementation of ctx->Driver.DrawPixels() in terms
1766 * of texture mapping and polygon rendering.
1769 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
1770 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1771 GLenum format
, GLenum type
,
1772 const struct gl_pixelstore_attrib
*unpack
,
1773 const GLvoid
*pixels
)
1775 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1776 struct temp_texture
*tex
= get_temp_texture(ctx
);
1777 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
1778 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
1780 GLfloat x
, y
, z
, s
, t
;
1782 struct vertex verts
[4];
1783 GLenum texIntFormat
;
1784 GLboolean fallback
, newTex
;
1785 GLbitfield metaExtraSave
= 0x0;
1789 * Determine if we can do the glDrawPixels with texture mapping.
1791 fallback
= GL_FALSE
;
1792 if (ctx
->_ImageTransferState
||
1797 if (_mesa_is_color_format(format
)) {
1798 /* use more compact format when possible */
1799 /* XXX disable special case for GL_LUMINANCE for now to work around
1800 * apparent i965 driver bug (see bug #23670).
1802 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
1803 texIntFormat
= format
;
1805 texIntFormat
= GL_RGBA
;
1807 else if (_mesa_is_stencil_format(format
)) {
1808 if (ctx
->Extensions
.ARB_fragment_program
&&
1809 ctx
->Pixel
.IndexShift
== 0 &&
1810 ctx
->Pixel
.IndexOffset
== 0 &&
1811 type
== GL_UNSIGNED_BYTE
) {
1812 /* We'll store stencil as alpha. This only works for GLubyte
1813 * image data because of how incoming values are mapped to alpha
1816 texIntFormat
= GL_ALPHA
;
1817 metaExtraSave
= (META_COLOR_MASK
|
1826 else if (_mesa_is_depth_format(format
)) {
1827 if (ctx
->Extensions
.ARB_depth_texture
&&
1828 ctx
->Extensions
.ARB_fragment_program
) {
1829 texIntFormat
= GL_DEPTH_COMPONENT
;
1830 metaExtraSave
= (META_SHADER
);
1841 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
1842 format
, type
, unpack
, pixels
);
1847 * Check image size against max texture size, draw as tiles if needed.
1849 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
1850 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
1851 format
, type
, unpack
, pixels
);
1855 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
1856 * but a there's a few things we need to override:
1858 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1866 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
1868 /* vertex positions, texcoords (after texture allocation!) */
1870 const GLfloat x0
= (GLfloat
) x
;
1871 const GLfloat y0
= (GLfloat
) y
;
1872 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
1873 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
1874 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1884 verts
[1].s
= tex
->Sright
;
1889 verts
[2].s
= tex
->Sright
;
1890 verts
[2].t
= tex
->Ttop
;
1895 verts
[3].t
= tex
->Ttop
;
1898 if (drawpix
->ArrayObj
== 0) {
1899 /* one-time setup: create vertex array object */
1900 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
1902 _mesa_BindVertexArray(drawpix
->ArrayObj
);
1904 /* create vertex array buffer */
1905 _mesa_GenBuffersARB(1, &vbo
);
1906 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
1907 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1908 verts
, GL_DYNAMIC_DRAW_ARB
);
1910 /* setup vertex arrays */
1911 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1912 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1913 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1914 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1916 /* set given unpack params */
1917 ctx
->Unpack
= *unpack
;
1919 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1921 if (_mesa_is_stencil_format(format
)) {
1922 /* Drawing stencil */
1925 if (!drawpix
->StencilFP
)
1926 init_draw_stencil_pixels(ctx
);
1928 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1929 GL_ALPHA
, type
, pixels
);
1931 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1933 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1935 /* set all stencil bits to 0 */
1936 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1937 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
1938 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1940 /* set stencil bits to 1 where needed */
1941 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
1943 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1944 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1946 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
1947 const GLuint mask
= 1 << bit
;
1948 if (mask
& origStencilMask
) {
1949 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
1950 _mesa_StencilMask(mask
);
1952 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
1953 255.0 / mask
, 0.5, 0.0, 0.0);
1955 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1959 else if (_mesa_is_depth_format(format
)) {
1961 if (!drawpix
->DepthFP
)
1962 init_draw_depth_pixels(ctx
);
1964 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1965 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1967 /* polygon color = current raster color */
1968 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
1969 ctx
->Current
.RasterColor
);
1971 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1972 format
, type
, pixels
);
1974 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1978 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1979 format
, type
, pixels
);
1980 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1983 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1985 _mesa_DeleteBuffersARB(1, &vbo
);
1987 /* restore unpack params */
1988 ctx
->Unpack
= unpackSave
;
1990 _mesa_meta_end(ctx
);
1994 alpha_test_raster_color(struct gl_context
*ctx
)
1996 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
1997 GLfloat ref
= ctx
->Color
.AlphaRef
;
1999 switch (ctx
->Color
.AlphaFunc
) {
2005 return alpha
== ref
;
2007 return alpha
<= ref
;
2011 return alpha
!= ref
;
2013 return alpha
>= ref
;
2023 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2024 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2025 * tracker would improve performance a lot.
2028 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2029 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2030 const struct gl_pixelstore_attrib
*unpack
,
2031 const GLubyte
*bitmap1
)
2033 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2034 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2035 const GLenum texIntFormat
= GL_ALPHA
;
2036 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2039 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2041 struct vertex verts
[4];
2046 * Check if swrast fallback is needed.
2048 if (ctx
->_ImageTransferState
||
2049 ctx
->FragmentProgram
._Enabled
||
2051 ctx
->Texture
._EnabledUnits
||
2052 width
> tex
->MaxSize
||
2053 height
> tex
->MaxSize
) {
2054 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2058 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2061 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2062 * but a there's a few things we need to override:
2064 _mesa_meta_begin(ctx
, (META_ALPHA_TEST
|
2066 META_RASTERIZATION
|
2073 if (bitmap
->ArrayObj
== 0) {
2074 /* one-time setup */
2076 /* create vertex array object */
2077 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2078 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2080 /* create vertex array buffer */
2081 _mesa_GenBuffersARB(1, &bitmap
->VBO
);
2082 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2083 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2084 NULL
, GL_DYNAMIC_DRAW_ARB
);
2086 /* setup vertex arrays */
2087 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2088 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2089 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2090 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2091 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2092 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2095 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2096 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2099 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2101 /* vertex positions, texcoords, colors (after texture allocation!) */
2103 const GLfloat x0
= (GLfloat
) x
;
2104 const GLfloat y0
= (GLfloat
) y
;
2105 const GLfloat x1
= (GLfloat
) (x
+ width
);
2106 const GLfloat y1
= (GLfloat
) (y
+ height
);
2107 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2118 verts
[1].s
= tex
->Sright
;
2123 verts
[2].s
= tex
->Sright
;
2124 verts
[2].t
= tex
->Ttop
;
2129 verts
[3].t
= tex
->Ttop
;
2131 for (i
= 0; i
< 4; i
++) {
2132 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2133 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2134 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2135 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2138 /* upload new vertex data */
2139 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2142 /* choose different foreground/background alpha values */
2143 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2144 bg
= (fg
> 127 ? 0 : 255);
2146 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2148 _mesa_meta_end(ctx
);
2152 bitmap8
= (GLubyte
*) malloc(width
* height
);
2154 memset(bitmap8
, bg
, width
* height
);
2155 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2156 bitmap8
, width
, fg
);
2158 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2160 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2161 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2163 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2164 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2166 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2168 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2173 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2175 _mesa_meta_end(ctx
);
2180 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2181 * software fallback. The fallback path will require that the texture
2182 * images are mapped.
2183 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2186 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
2187 struct gl_texture_object
*texObj
)
2189 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2190 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2191 struct gl_texture_image
*baseImage
;
2195 /* check for fallbacks */
2196 if (!ctx
->Extensions
.EXT_framebuffer_object
||
2197 target
== GL_TEXTURE_3D
) {
2201 srcLevel
= texObj
->BaseLevel
;
2202 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2203 if (!baseImage
|| _mesa_is_format_compressed(baseImage
->TexFormat
)) {
2208 * Test that we can actually render in the texture's format.
2211 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2212 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2214 if (target
== GL_TEXTURE_1D
) {
2215 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2216 GL_COLOR_ATTACHMENT0_EXT
,
2217 target
, texObj
->Name
, srcLevel
);
2220 /* other work is needed to enable 3D mipmap generation */
2221 else if (target
== GL_TEXTURE_3D
) {
2223 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2224 GL_COLOR_ATTACHMENT0_EXT
,
2225 target
, texObj
->Name
, srcLevel
, zoffset
);
2230 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2231 GL_COLOR_ATTACHMENT0_EXT
,
2232 target
, texObj
->Name
, srcLevel
);
2235 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2237 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2239 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2248 * Called via ctx->Driver.GenerateMipmap()
2249 * Note: texture borders and 3D texture support not yet complete.
2252 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
2253 struct gl_texture_object
*texObj
)
2255 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2257 GLfloat x
, y
, s
, t
, r
;
2259 struct vertex verts
[4];
2260 const GLuint baseLevel
= texObj
->BaseLevel
;
2261 const GLuint maxLevel
= texObj
->MaxLevel
;
2262 const GLenum minFilterSave
= texObj
->MinFilter
;
2263 const GLenum magFilterSave
= texObj
->MagFilter
;
2264 const GLint maxLevelSave
= texObj
->MaxLevel
;
2265 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
2266 const GLenum wrapSSave
= texObj
->WrapS
;
2267 const GLenum wrapTSave
= texObj
->WrapT
;
2268 const GLenum wrapRSave
= texObj
->WrapR
;
2269 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2270 const GLuint original_active_unit
= ctx
->Texture
.CurrentUnit
;
2275 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
2276 _mesa_generate_mipmap(ctx
, target
, texObj
);
2280 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
2281 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
2282 faceTarget
= target
;
2283 target
= GL_TEXTURE_CUBE_MAP
;
2286 faceTarget
= target
;
2289 _mesa_meta_begin(ctx
, META_ALL
);
2291 if (original_active_unit
!= 0)
2292 _mesa_BindTexture(target
, texObj
->Name
);
2294 if (mipmap
->ArrayObj
== 0) {
2295 /* one-time setup */
2297 /* create vertex array object */
2298 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
2299 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
2301 /* create vertex array buffer */
2302 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
2303 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2304 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2305 NULL
, GL_DYNAMIC_DRAW_ARB
);
2307 /* setup vertex arrays */
2308 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2309 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2310 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2311 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2314 _mesa_BindVertexArray(mipmap
->ArrayObj
);
2315 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2319 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2321 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2323 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
2324 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
2325 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
2326 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2327 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2328 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
2330 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2332 /* setup texcoords once (XXX what about border?) */
2333 switch (faceTarget
) {
2355 static const GLfloat st
[4][2] = {
2356 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2360 /* loop over quad verts */
2361 for (i
= 0; i
< 4; i
++) {
2362 /* Compute sc = +/-scale and tc = +/-scale.
2363 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2364 * though that can still sometimes happen with this scale factor...
2366 const GLfloat scale
= 0.9999f
;
2367 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2368 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2370 switch (faceTarget
) {
2371 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2376 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2381 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2386 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2391 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2396 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2408 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2410 /* setup vertex positions */
2421 /* upload new vertex data */
2422 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2425 /* setup projection matrix */
2426 _mesa_MatrixMode(GL_PROJECTION
);
2427 _mesa_LoadIdentity();
2428 _mesa_Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
2430 /* texture is already locked, unlock now */
2431 _mesa_unlock_texture(ctx
, texObj
);
2433 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
2434 const struct gl_texture_image
*srcImage
;
2435 const GLuint srcLevel
= dstLevel
- 1;
2436 GLsizei srcWidth
, srcHeight
, srcDepth
;
2437 GLsizei dstWidth
, dstHeight
, dstDepth
;
2440 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
2441 assert(srcImage
->Border
== 0); /* XXX we can fix this */
2443 /* src size w/out border */
2444 srcWidth
= srcImage
->Width
- 2 * border
;
2445 srcHeight
= srcImage
->Height
- 2 * border
;
2446 srcDepth
= srcImage
->Depth
- 2 * border
;
2448 /* new dst size w/ border */
2449 dstWidth
= MAX2(1, srcWidth
/ 2) + 2 * border
;
2450 dstHeight
= MAX2(1, srcHeight
/ 2) + 2 * border
;
2451 dstDepth
= MAX2(1, srcDepth
/ 2) + 2 * border
;
2453 if (dstWidth
== srcImage
->Width
&&
2454 dstHeight
== srcImage
->Height
&&
2455 dstDepth
== srcImage
->Depth
) {
2460 /* Set MaxLevel large enough to hold the new level when we allocate it */
2461 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
2463 /* Create empty dest image */
2464 if (target
== GL_TEXTURE_1D
) {
2465 _mesa_TexImage1D(target
, dstLevel
, srcImage
->InternalFormat
,
2467 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2469 else if (target
== GL_TEXTURE_3D
) {
2470 _mesa_TexImage3D(target
, dstLevel
, srcImage
->InternalFormat
,
2471 dstWidth
, dstHeight
, dstDepth
, border
,
2472 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2476 _mesa_TexImage2D(faceTarget
, dstLevel
, srcImage
->InternalFormat
,
2477 dstWidth
, dstHeight
, border
,
2478 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2480 if (target
== GL_TEXTURE_CUBE_MAP
) {
2481 /* If texturing from a cube, we need to make sure all src faces
2482 * have been defined (even if we're not sampling from them.)
2483 * Otherwise the texture object will be 'incomplete' and
2484 * texturing from it will not be allowed.
2487 for (face
= 0; face
< 6; face
++) {
2488 if (!texObj
->Image
[face
][srcLevel
] ||
2489 texObj
->Image
[face
][srcLevel
]->Width
!= srcWidth
) {
2490 _mesa_TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ face
,
2491 srcLevel
, srcImage
->InternalFormat
,
2492 srcWidth
, srcHeight
, border
,
2493 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2499 /* limit minification to src level */
2500 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
2502 /* Set to draw into the current dstLevel */
2503 if (target
== GL_TEXTURE_1D
) {
2504 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2505 GL_COLOR_ATTACHMENT0_EXT
,
2510 else if (target
== GL_TEXTURE_3D
) {
2511 GLint zoffset
= 0; /* XXX unfinished */
2512 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2513 GL_COLOR_ATTACHMENT0_EXT
,
2520 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2521 GL_COLOR_ATTACHMENT0_EXT
,
2527 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
2530 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2531 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2536 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
2537 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
2539 /* setup viewport */
2540 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
2542 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2545 _mesa_lock_texture(ctx
, texObj
); /* relock */
2547 _mesa_meta_end(ctx
);
2549 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
2550 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
2551 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
2552 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
2553 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
2554 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
2555 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, wrapRSave
);
2557 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2562 * Determine the GL data type to use for the temporary image read with
2563 * ReadPixels() and passed to Tex[Sub]Image().
2566 get_temp_image_type(struct gl_context
*ctx
, GLenum baseFormat
)
2568 switch (baseFormat
) {
2573 case GL_LUMINANCE_ALPHA
:
2575 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2576 return GL_UNSIGNED_BYTE
;
2577 else if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2578 return GL_UNSIGNED_SHORT
;
2581 case GL_DEPTH_COMPONENT
:
2582 return GL_UNSIGNED_INT
;
2583 case GL_DEPTH_STENCIL
:
2584 return GL_UNSIGNED_INT_24_8
;
2586 _mesa_problem(ctx
, "Unexpected format in get_temp_image_type()");
2593 * Helper for _mesa_meta_CopyTexImage1/2D() functions.
2594 * Have to be careful with locking and meta state for pixel transfer.
2597 copy_tex_image(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
2598 GLenum internalFormat
, GLint x
, GLint y
,
2599 GLsizei width
, GLsizei height
, GLint border
)
2601 struct gl_texture_object
*texObj
;
2602 struct gl_texture_image
*texImage
;
2603 GLenum format
, type
;
2607 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2608 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2610 /* Choose format/type for temporary image buffer */
2611 format
= _mesa_base_tex_format(ctx
, internalFormat
);
2612 type
= get_temp_image_type(ctx
, format
);
2613 bpp
= _mesa_bytes_per_pixel(format
, type
);
2615 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_image()");
2620 * Alloc image buffer (XXX could use a PBO)
2622 buf
= malloc(width
* height
* bpp
);
2624 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
2628 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2631 * Read image from framebuffer (disable pixel transfer ops)
2633 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2634 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2635 format
, type
, &ctx
->Pack
, buf
);
2636 _mesa_meta_end(ctx
);
2638 if (texImage
->Data
) {
2639 ctx
->Driver
.FreeTexImageData(ctx
, texImage
);
2642 /* The texture's format was already chosen in _mesa_CopyTexImage() */
2643 ASSERT(texImage
->TexFormat
!= MESA_FORMAT_NONE
);
2646 * Store texture data (with pixel transfer ops)
2648 _mesa_meta_begin(ctx
, META_PIXEL_STORE
);
2650 _mesa_update_state(ctx
); /* to update pixel transfer state */
2652 if (target
== GL_TEXTURE_1D
) {
2653 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
2654 width
, border
, format
, type
,
2655 buf
, &ctx
->Unpack
, texObj
, texImage
);
2658 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
2659 width
, height
, border
, format
, type
,
2660 buf
, &ctx
->Unpack
, texObj
, texImage
);
2662 _mesa_meta_end(ctx
);
2664 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
2671 _mesa_meta_CopyTexImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2672 GLenum internalFormat
, GLint x
, GLint y
,
2673 GLsizei width
, GLint border
)
2675 copy_tex_image(ctx
, 1, target
, level
, internalFormat
, x
, y
,
2681 _mesa_meta_CopyTexImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2682 GLenum internalFormat
, GLint x
, GLint y
,
2683 GLsizei width
, GLsizei height
, GLint border
)
2685 copy_tex_image(ctx
, 2, target
, level
, internalFormat
, x
, y
,
2686 width
, height
, border
);
2692 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2693 * Have to be careful with locking and meta state for pixel transfer.
2696 copy_tex_sub_image(struct gl_context
*ctx
,
2697 GLuint dims
, GLenum target
, GLint level
,
2698 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2700 GLsizei width
, GLsizei height
)
2702 struct gl_texture_object
*texObj
;
2703 struct gl_texture_image
*texImage
;
2704 GLenum format
, type
;
2708 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2709 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2711 /* Choose format/type for temporary image buffer */
2712 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
2713 type
= get_temp_image_type(ctx
, format
);
2714 bpp
= _mesa_bytes_per_pixel(format
, type
);
2716 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_sub_image()");
2721 * Alloc image buffer (XXX could use a PBO)
2723 buf
= malloc(width
* height
* bpp
);
2725 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
2729 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2732 * Read image from framebuffer (disable pixel transfer ops)
2734 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2735 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2736 format
, type
, &ctx
->Pack
, buf
);
2737 _mesa_meta_end(ctx
);
2739 _mesa_update_state(ctx
); /* to update pixel transfer state */
2742 * Store texture data (with pixel transfer ops)
2744 _mesa_meta_begin(ctx
, META_PIXEL_STORE
);
2745 if (target
== GL_TEXTURE_1D
) {
2746 ctx
->Driver
.TexSubImage1D(ctx
, target
, level
, xoffset
,
2747 width
, format
, type
, buf
,
2748 &ctx
->Unpack
, texObj
, texImage
);
2750 else if (target
== GL_TEXTURE_3D
) {
2751 ctx
->Driver
.TexSubImage3D(ctx
, target
, level
, xoffset
, yoffset
, zoffset
,
2752 width
, height
, 1, format
, type
, buf
,
2753 &ctx
->Unpack
, texObj
, texImage
);
2756 ctx
->Driver
.TexSubImage2D(ctx
, target
, level
, xoffset
, yoffset
,
2757 width
, height
, format
, type
, buf
,
2758 &ctx
->Unpack
, texObj
, texImage
);
2760 _mesa_meta_end(ctx
);
2762 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
2769 _mesa_meta_CopyTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2771 GLint x
, GLint y
, GLsizei width
)
2773 copy_tex_sub_image(ctx
, 1, target
, level
, xoffset
, 0, 0,
2779 _mesa_meta_CopyTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2780 GLint xoffset
, GLint yoffset
,
2782 GLsizei width
, GLsizei height
)
2784 copy_tex_sub_image(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2785 x
, y
, width
, height
);
2790 _mesa_meta_CopyTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2791 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2793 GLsizei width
, GLsizei height
)
2795 copy_tex_sub_image(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2796 x
, y
, width
, height
);
2801 _mesa_meta_CopyColorTable(struct gl_context
*ctx
,
2802 GLenum target
, GLenum internalformat
,
2803 GLint x
, GLint y
, GLsizei width
)
2807 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2809 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyColorTable");
2814 * Read image from framebuffer (disable pixel transfer ops)
2816 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2817 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2818 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2820 _mesa_ColorTable(target
, internalformat
, width
, GL_RGBA
, GL_FLOAT
, buf
);
2822 _mesa_meta_end(ctx
);
2829 _mesa_meta_CopyColorSubTable(struct gl_context
*ctx
,GLenum target
, GLsizei start
,
2830 GLint x
, GLint y
, GLsizei width
)
2834 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2836 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyColorSubTable");
2841 * Read image from framebuffer (disable pixel transfer ops)
2843 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2844 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2845 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2847 _mesa_ColorSubTable(target
, start
, width
, GL_RGBA
, GL_FLOAT
, buf
);
2849 _mesa_meta_end(ctx
);