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"
51 #include "main/polygon.h"
52 #include "main/readpix.h"
53 #include "main/scissor.h"
54 #include "main/shaderapi.h"
55 #include "main/shaderobj.h"
56 #include "main/state.h"
57 #include "main/stencil.h"
58 #include "main/texobj.h"
59 #include "main/texenv.h"
60 #include "main/teximage.h"
61 #include "main/texparam.h"
62 #include "main/texstate.h"
63 #include "main/varray.h"
64 #include "main/viewport.h"
65 #include "program/program.h"
66 #include "swrast/swrast.h"
67 #include "drivers/common/meta.h"
70 /** Return offset in bytes of the field within a vertex struct */
71 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
75 * Flags passed to _mesa_meta_begin().
79 #define META_ALPHA_TEST 0x1
80 #define META_BLEND 0x2 /**< includes logicop */
81 #define META_COLOR_MASK 0x4
82 #define META_DEPTH_TEST 0x8
84 #define META_PIXEL_STORE 0x20
85 #define META_PIXEL_TRANSFER 0x40
86 #define META_RASTERIZATION 0x80
87 #define META_SCISSOR 0x100
88 #define META_SHADER 0x200
89 #define META_STENCIL_TEST 0x400
90 #define META_TRANSFORM 0x800 /**< modelview, projection, clip planes */
91 #define META_TEXTURE 0x1000
92 #define META_VERTEX 0x2000
93 #define META_VIEWPORT 0x4000
98 * State which we may save/restore across meta ops.
99 * XXX this may be incomplete...
103 GLbitfield SavedState
; /**< bitmask of META_* flags */
105 /** META_ALPHA_TEST */
106 GLboolean AlphaEnabled
;
111 GLbitfield BlendEnabled
;
112 GLboolean ColorLogicOpEnabled
;
114 /** META_COLOR_MASK */
115 GLubyte ColorMask
[MAX_DRAW_BUFFERS
][4];
117 /** META_DEPTH_TEST */
118 struct gl_depthbuffer_attrib Depth
;
123 /** META_PIXEL_STORE */
124 struct gl_pixelstore_attrib Pack
, Unpack
;
126 /** META_PIXEL_TRANSFER */
127 GLfloat RedBias
, RedScale
;
128 GLfloat GreenBias
, GreenScale
;
129 GLfloat BlueBias
, BlueScale
;
130 GLfloat AlphaBias
, AlphaScale
;
131 GLfloat DepthBias
, DepthScale
;
132 GLboolean MapColorFlag
;
134 /** META_RASTERIZATION */
135 GLenum FrontPolygonMode
, BackPolygonMode
;
136 GLboolean PolygonOffset
;
137 GLboolean PolygonSmooth
;
138 GLboolean PolygonStipple
;
139 GLboolean PolygonCull
;
142 struct gl_scissor_attrib Scissor
;
145 GLboolean VertexProgramEnabled
;
146 struct gl_vertex_program
*VertexProgram
;
147 GLboolean FragmentProgramEnabled
;
148 struct gl_fragment_program
*FragmentProgram
;
149 struct gl_shader_program
*VertexShader
;
150 struct gl_shader_program
*GeometryShader
;
151 struct gl_shader_program
*FragmentShader
;
152 struct gl_shader_program
*ActiveShader
;
154 /** META_STENCIL_TEST */
155 struct gl_stencil_attrib Stencil
;
157 /** META_TRANSFORM */
159 GLfloat ModelviewMatrix
[16];
160 GLfloat ProjectionMatrix
[16];
161 GLfloat TextureMatrix
[16];
162 GLbitfield ClipPlanesEnabled
;
166 GLuint ClientActiveUnit
;
167 /** for unit[0] only */
168 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
169 /** mask of TEXTURE_2D_BIT, etc */
170 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
171 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
172 GLuint EnvMode
; /* unit[0] only */
175 struct gl_array_object
*ArrayObj
;
176 struct gl_buffer_object
*ArrayBufferObj
;
179 GLint ViewportX
, ViewportY
, ViewportW
, ViewportH
;
180 GLclampd DepthNear
, DepthFar
;
182 /** Miscellaneous (always disabled) */
188 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
189 * This is currently shared by all the meta ops. But we could create a
190 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
195 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
196 GLsizei MinSize
; /**< Min texture size to allocate */
197 GLsizei MaxSize
; /**< Max possible texture size */
198 GLboolean NPOT
; /**< Non-power of two size OK? */
199 GLsizei Width
, Height
; /**< Current texture size */
201 GLfloat Sright
, Ttop
; /**< right, top texcoords */
206 * State for glBlitFramebufer()
217 * State for glClear()
227 * State for glCopyPixels()
237 * State for glDrawPixels()
243 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
244 GLuint DepthFP
; /**< Fragment program for drawing depth images */
249 * State for glBitmap()
255 struct temp_texture Tex
; /**< separate texture from other meta ops */
260 * State for _mesa_meta_generate_mipmap()
262 struct gen_mipmap_state
269 #define MAX_META_OPS_DEPTH 2
271 * All per-context meta state.
275 /** Stack of state saved during meta-ops */
276 struct save_state Save
[MAX_META_OPS_DEPTH
];
277 /** Save stack depth */
278 GLuint SaveStackDepth
;
280 struct temp_texture TempTex
;
282 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
283 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
284 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
285 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
286 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
287 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
292 * Initialize meta-ops for a context.
293 * To be called once during context creation.
296 _mesa_meta_init(struct gl_context
*ctx
)
300 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
305 * Free context meta-op state.
306 * To be called once during context destruction.
309 _mesa_meta_free(struct gl_context
*ctx
)
311 /* Note: Any textures, VBOs, etc, that we allocate should get
312 * freed by the normal context destruction code. But this would be
313 * the place to free other meta data someday.
321 * Enter meta state. This is like a light-weight version of glPushAttrib
322 * but it also resets most GL state back to default values.
324 * \param state bitmask of META_* flags indicating which attribute groups
325 * to save and reset to their defaults
328 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
330 struct save_state
*save
;
332 /* hope MAX_META_OPS_DEPTH is large enough */
333 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
335 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
336 memset(save
, 0, sizeof(*save
));
337 save
->SavedState
= state
;
339 if (state
& META_ALPHA_TEST
) {
340 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
341 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
342 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
343 if (ctx
->Color
.AlphaEnabled
)
344 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
347 if (state
& META_BLEND
) {
348 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
349 if (ctx
->Color
.BlendEnabled
) {
350 if (ctx
->Extensions
.EXT_draw_buffers2
) {
352 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
353 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
357 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
360 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
361 if (ctx
->Color
.ColorLogicOpEnabled
)
362 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
365 if (state
& META_COLOR_MASK
) {
366 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
367 sizeof(ctx
->Color
.ColorMask
));
368 if (!ctx
->Color
.ColorMask
[0][0] ||
369 !ctx
->Color
.ColorMask
[0][1] ||
370 !ctx
->Color
.ColorMask
[0][2] ||
371 !ctx
->Color
.ColorMask
[0][3])
372 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
375 if (state
& META_DEPTH_TEST
) {
376 save
->Depth
= ctx
->Depth
; /* struct copy */
378 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
381 if (state
& META_FOG
) {
382 save
->Fog
= ctx
->Fog
.Enabled
;
383 if (ctx
->Fog
.Enabled
)
384 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
387 if (state
& META_PIXEL_STORE
) {
388 save
->Pack
= ctx
->Pack
;
389 save
->Unpack
= ctx
->Unpack
;
390 ctx
->Pack
= ctx
->DefaultPacking
;
391 ctx
->Unpack
= ctx
->DefaultPacking
;
394 if (state
& META_PIXEL_TRANSFER
) {
395 save
->RedScale
= ctx
->Pixel
.RedScale
;
396 save
->RedBias
= ctx
->Pixel
.RedBias
;
397 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
398 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
399 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
400 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
401 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
402 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
403 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
404 ctx
->Pixel
.RedScale
= 1.0F
;
405 ctx
->Pixel
.RedBias
= 0.0F
;
406 ctx
->Pixel
.GreenScale
= 1.0F
;
407 ctx
->Pixel
.GreenBias
= 0.0F
;
408 ctx
->Pixel
.BlueScale
= 1.0F
;
409 ctx
->Pixel
.BlueBias
= 0.0F
;
410 ctx
->Pixel
.AlphaScale
= 1.0F
;
411 ctx
->Pixel
.AlphaBias
= 0.0F
;
412 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
414 ctx
->NewState
|=_NEW_PIXEL
;
417 if (state
& META_RASTERIZATION
) {
418 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
419 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
420 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
421 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
422 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
423 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
424 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
425 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
426 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
427 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
428 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
431 if (state
& META_SCISSOR
) {
432 save
->Scissor
= ctx
->Scissor
; /* struct copy */
433 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
436 if (state
& META_SHADER
) {
437 if (ctx
->Extensions
.ARB_vertex_program
) {
438 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
439 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
440 ctx
->VertexProgram
.Current
);
441 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
444 if (ctx
->Extensions
.ARB_fragment_program
) {
445 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
446 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
447 ctx
->FragmentProgram
.Current
);
448 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
451 if (ctx
->Extensions
.ARB_shader_objects
) {
452 _mesa_reference_shader_program(ctx
, &save
->VertexShader
,
453 ctx
->Shader
.CurrentVertexProgram
);
454 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
,
455 ctx
->Shader
.CurrentGeometryProgram
);
456 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
,
457 ctx
->Shader
.CurrentFragmentProgram
);
458 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
459 ctx
->Shader
.CurrentFragmentProgram
);
461 _mesa_UseProgramObjectARB(0);
465 if (state
& META_STENCIL_TEST
) {
466 save
->Stencil
= ctx
->Stencil
; /* struct copy */
467 if (ctx
->Stencil
.Enabled
)
468 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
469 /* NOTE: other stencil state not reset */
472 if (state
& META_TEXTURE
) {
475 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
476 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
477 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
479 /* Disable all texture units */
480 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
481 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
482 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
483 if (ctx
->Texture
.Unit
[u
].Enabled
||
484 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
485 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
486 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
487 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
488 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
489 if (ctx
->Extensions
.ARB_texture_cube_map
)
490 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
491 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
492 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
493 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
494 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
495 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
499 /* save current texture objects for unit[0] only */
500 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
501 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
502 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
505 /* set defaults for unit[0] */
506 _mesa_ActiveTextureARB(GL_TEXTURE0
);
507 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
508 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
511 if (state
& META_TRANSFORM
) {
512 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
513 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
514 16 * sizeof(GLfloat
));
515 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
516 16 * sizeof(GLfloat
));
517 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
518 16 * sizeof(GLfloat
));
519 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
520 /* set 1:1 vertex:pixel coordinate transform */
521 _mesa_ActiveTextureARB(GL_TEXTURE0
);
522 _mesa_MatrixMode(GL_TEXTURE
);
523 _mesa_LoadIdentity();
524 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
525 _mesa_MatrixMode(GL_MODELVIEW
);
526 _mesa_LoadIdentity();
527 _mesa_MatrixMode(GL_PROJECTION
);
528 _mesa_LoadIdentity();
529 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
530 0.0, ctx
->DrawBuffer
->Height
,
532 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
533 if (ctx
->Transform
.ClipPlanesEnabled
) {
535 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
536 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
541 if (state
& META_VERTEX
) {
542 /* save vertex array object state */
543 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
544 ctx
->Array
.ArrayObj
);
545 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
546 ctx
->Array
.ArrayBufferObj
);
547 /* set some default state? */
550 if (state
& META_VIEWPORT
) {
551 /* save viewport state */
552 save
->ViewportX
= ctx
->Viewport
.X
;
553 save
->ViewportY
= ctx
->Viewport
.Y
;
554 save
->ViewportW
= ctx
->Viewport
.Width
;
555 save
->ViewportH
= ctx
->Viewport
.Height
;
556 /* set viewport to match window size */
557 if (ctx
->Viewport
.X
!= 0 ||
558 ctx
->Viewport
.Y
!= 0 ||
559 ctx
->Viewport
.Width
!= ctx
->DrawBuffer
->Width
||
560 ctx
->Viewport
.Height
!= ctx
->DrawBuffer
->Height
) {
561 _mesa_set_viewport(ctx
, 0, 0,
562 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
564 /* save depth range state */
565 save
->DepthNear
= ctx
->Viewport
.Near
;
566 save
->DepthFar
= ctx
->Viewport
.Far
;
567 /* set depth range to default */
568 _mesa_DepthRange(0.0, 1.0);
573 save
->Lighting
= ctx
->Light
.Enabled
;
574 if (ctx
->Light
.Enabled
)
575 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
581 * Leave meta state. This is like a light-weight version of glPopAttrib().
584 _mesa_meta_end(struct gl_context
*ctx
)
586 struct save_state
*save
= &ctx
->Meta
->Save
[--ctx
->Meta
->SaveStackDepth
];
587 const GLbitfield state
= save
->SavedState
;
589 if (state
& META_ALPHA_TEST
) {
590 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
591 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
592 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
595 if (state
& META_BLEND
) {
596 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
597 if (ctx
->Extensions
.EXT_draw_buffers2
) {
599 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
600 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
604 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
607 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
608 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
611 if (state
& META_COLOR_MASK
) {
613 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
614 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
616 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
617 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
620 _mesa_ColorMaskIndexed(i
,
621 save
->ColorMask
[i
][0],
622 save
->ColorMask
[i
][1],
623 save
->ColorMask
[i
][2],
624 save
->ColorMask
[i
][3]);
630 if (state
& META_DEPTH_TEST
) {
631 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
632 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
633 _mesa_DepthFunc(save
->Depth
.Func
);
634 _mesa_DepthMask(save
->Depth
.Mask
);
637 if (state
& META_FOG
) {
638 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
641 if (state
& META_PIXEL_STORE
) {
642 ctx
->Pack
= save
->Pack
;
643 ctx
->Unpack
= save
->Unpack
;
646 if (state
& META_PIXEL_TRANSFER
) {
647 ctx
->Pixel
.RedScale
= save
->RedScale
;
648 ctx
->Pixel
.RedBias
= save
->RedBias
;
649 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
650 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
651 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
652 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
653 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
654 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
655 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
657 ctx
->NewState
|=_NEW_PIXEL
;
660 if (state
& META_RASTERIZATION
) {
661 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
662 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
663 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
664 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
665 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
666 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
669 if (state
& META_SCISSOR
) {
670 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
671 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
672 save
->Scissor
.Width
, save
->Scissor
.Height
);
675 if (state
& META_SHADER
) {
676 if (ctx
->Extensions
.ARB_vertex_program
) {
677 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
678 save
->VertexProgramEnabled
);
679 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
680 save
->VertexProgram
);
681 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
684 if (ctx
->Extensions
.ARB_fragment_program
) {
685 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
686 save
->FragmentProgramEnabled
);
687 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
688 save
->FragmentProgram
);
689 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
692 if (ctx
->Extensions
.ARB_vertex_shader
)
693 _mesa_use_shader_program(ctx
, GL_VERTEX_SHADER
, save
->VertexShader
);
695 if (ctx
->Extensions
.ARB_geometry_shader4
)
696 _mesa_use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
,
697 save
->GeometryShader
);
699 if (ctx
->Extensions
.ARB_fragment_shader
)
700 _mesa_use_shader_program(ctx
, GL_FRAGMENT_SHADER
,
701 save
->FragmentShader
);
703 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
707 if (state
& META_STENCIL_TEST
) {
708 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
710 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
711 _mesa_ClearStencil(stencil
->Clear
);
712 if (ctx
->Extensions
.EXT_stencil_two_side
) {
713 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
714 stencil
->TestTwoSide
);
715 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
716 ? GL_BACK
: GL_FRONT
);
719 _mesa_StencilFuncSeparate(GL_FRONT
,
720 stencil
->Function
[0],
722 stencil
->ValueMask
[0]);
723 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
724 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
725 stencil
->ZFailFunc
[0],
726 stencil
->ZPassFunc
[0]);
728 _mesa_StencilFuncSeparate(GL_BACK
,
729 stencil
->Function
[1],
731 stencil
->ValueMask
[1]);
732 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
733 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
734 stencil
->ZFailFunc
[1],
735 stencil
->ZPassFunc
[1]);
738 if (state
& META_TEXTURE
) {
741 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
743 /* restore texenv for unit[0] */
744 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
746 /* restore texture objects for unit[0] only */
747 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
748 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
749 save
->CurrentTexture
[tgt
]);
750 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
753 /* Re-enable textures, texgen */
754 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
755 if (save
->TexEnabled
[u
]) {
756 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
758 if (save
->TexEnabled
[u
] & TEXTURE_1D_BIT
)
759 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_TRUE
);
760 if (save
->TexEnabled
[u
] & TEXTURE_2D_BIT
)
761 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_TRUE
);
762 if (save
->TexEnabled
[u
] & TEXTURE_3D_BIT
)
763 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_TRUE
);
764 if (save
->TexEnabled
[u
] & TEXTURE_CUBE_BIT
)
765 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_TRUE
);
766 if (save
->TexEnabled
[u
] & TEXTURE_RECT_BIT
)
767 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_TRUE
);
770 if (save
->TexGenEnabled
[u
]) {
771 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
773 if (save
->TexGenEnabled
[u
] & S_BIT
)
774 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_TRUE
);
775 if (save
->TexGenEnabled
[u
] & T_BIT
)
776 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_TRUE
);
777 if (save
->TexGenEnabled
[u
] & R_BIT
)
778 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_TRUE
);
779 if (save
->TexGenEnabled
[u
] & Q_BIT
)
780 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_TRUE
);
784 /* restore current unit state */
785 _mesa_ActiveTextureARB(GL_TEXTURE0
+ save
->ActiveUnit
);
786 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ save
->ClientActiveUnit
);
789 if (state
& META_TRANSFORM
) {
790 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
791 _mesa_ActiveTextureARB(GL_TEXTURE0
);
792 _mesa_MatrixMode(GL_TEXTURE
);
793 _mesa_LoadMatrixf(save
->TextureMatrix
);
794 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
796 _mesa_MatrixMode(GL_MODELVIEW
);
797 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
799 _mesa_MatrixMode(GL_PROJECTION
);
800 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
802 _mesa_MatrixMode(save
->MatrixMode
);
804 if (save
->ClipPlanesEnabled
) {
806 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
807 if (save
->ClipPlanesEnabled
& (1 << i
)) {
808 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
814 if (state
& META_VERTEX
) {
815 /* restore vertex buffer object */
816 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
817 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
819 /* restore vertex array object */
820 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
821 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
824 if (state
& META_VIEWPORT
) {
825 if (save
->ViewportX
!= ctx
->Viewport
.X
||
826 save
->ViewportY
!= ctx
->Viewport
.Y
||
827 save
->ViewportW
!= ctx
->Viewport
.Width
||
828 save
->ViewportH
!= ctx
->Viewport
.Height
) {
829 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
830 save
->ViewportW
, save
->ViewportH
);
832 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
836 if (save
->Lighting
) {
837 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
843 * Convert Z from a normalized value in the range [0, 1] to an object-space
844 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
845 * default/identity ortho projection results in the original Z value.
846 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
847 * value comes from the clear value or raster position.
849 static INLINE GLfloat
850 invert_z(GLfloat normZ
)
852 GLfloat objZ
= 1.0 - 2.0 * normZ
;
858 * One-time init for a temp_texture object.
859 * Choose tex target, compute max tex size, etc.
862 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
864 /* prefer texture rectangle */
865 if (ctx
->Extensions
.NV_texture_rectangle
) {
866 tex
->Target
= GL_TEXTURE_RECTANGLE
;
867 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
871 /* use 2D texture, NPOT if possible */
872 tex
->Target
= GL_TEXTURE_2D
;
873 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
874 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
876 tex
->MinSize
= 16; /* 16 x 16 at least */
877 assert(tex
->MaxSize
> 0);
879 _mesa_GenTextures(1, &tex
->TexObj
);
884 * Return pointer to temp_texture info for non-bitmap ops.
885 * This does some one-time init if needed.
887 static struct temp_texture
*
888 get_temp_texture(struct gl_context
*ctx
)
890 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
893 init_temp_texture(ctx
, tex
);
901 * Return pointer to temp_texture info for _mesa_meta_bitmap().
902 * We use a separate texture for bitmaps to reduce texture
903 * allocation/deallocation.
905 static struct temp_texture
*
906 get_bitmap_temp_texture(struct gl_context
*ctx
)
908 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
911 init_temp_texture(ctx
, tex
);
919 * Compute the width/height of texture needed to draw an image of the
920 * given size. Return a flag indicating whether the current texture
921 * can be re-used (glTexSubImage2D) or if a new texture needs to be
922 * allocated (glTexImage2D).
923 * Also, compute s/t texcoords for drawing.
925 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
928 alloc_texture(struct temp_texture
*tex
,
929 GLsizei width
, GLsizei height
, GLenum intFormat
)
931 GLboolean newTex
= GL_FALSE
;
933 ASSERT(width
<= tex
->MaxSize
);
934 ASSERT(height
<= tex
->MaxSize
);
936 if (width
> tex
->Width
||
937 height
> tex
->Height
||
938 intFormat
!= tex
->IntFormat
) {
939 /* alloc new texture (larger or different format) */
942 /* use non-power of two size */
943 tex
->Width
= MAX2(tex
->MinSize
, width
);
944 tex
->Height
= MAX2(tex
->MinSize
, height
);
947 /* find power of two size */
949 w
= h
= tex
->MinSize
;
958 tex
->IntFormat
= intFormat
;
963 /* compute texcoords */
964 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
965 tex
->Sright
= (GLfloat
) width
;
966 tex
->Ttop
= (GLfloat
) height
;
969 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
970 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
978 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
981 setup_copypix_texture(struct temp_texture
*tex
,
983 GLint srcX
, GLint srcY
,
984 GLsizei width
, GLsizei height
, GLenum intFormat
,
987 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
988 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
989 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
990 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
992 /* copy framebuffer image to texture */
994 /* create new tex image */
995 if (tex
->Width
== width
&& tex
->Height
== height
) {
996 /* create new tex with framebuffer data */
997 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
998 srcX
, srcY
, width
, height
, 0);
1001 /* create empty texture */
1002 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1003 tex
->Width
, tex
->Height
, 0,
1004 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1006 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1007 0, 0, srcX
, srcY
, width
, height
);
1011 /* replace existing tex image */
1012 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1013 0, 0, srcX
, srcY
, width
, height
);
1019 * Setup/load texture for glDrawPixels.
1022 setup_drawpix_texture(struct gl_context
*ctx
,
1023 struct temp_texture
*tex
,
1025 GLenum texIntFormat
,
1026 GLsizei width
, GLsizei height
,
1027 GLenum format
, GLenum type
,
1028 const GLvoid
*pixels
)
1030 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1031 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1032 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1033 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1035 /* copy pixel data to texture */
1037 /* create new tex image */
1038 if (tex
->Width
== width
&& tex
->Height
== height
) {
1039 /* create new tex and load image data */
1040 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1041 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1044 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1046 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1047 ctx
->Unpack
.BufferObj
);
1048 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1049 /* create empty texture */
1050 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1051 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1052 if (save_unpack_obj
!= NULL
)
1053 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
,
1054 save_unpack_obj
->Name
);
1056 _mesa_TexSubImage2D(tex
->Target
, 0,
1057 0, 0, width
, height
, format
, type
, pixels
);
1061 /* replace existing tex image */
1062 _mesa_TexSubImage2D(tex
->Target
, 0,
1063 0, 0, width
, height
, format
, type
, pixels
);
1070 * One-time init for drawing depth pixels.
1073 init_blit_depth_pixels(struct gl_context
*ctx
)
1075 static const char *program
=
1077 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1080 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1081 struct temp_texture
*tex
= get_temp_texture(ctx
);
1082 const char *texTarget
;
1084 assert(blit
->DepthFP
== 0);
1086 /* replace %s with "RECT" or "2D" */
1087 assert(strlen(program
) + 4 < sizeof(program2
));
1088 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1092 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1094 _mesa_GenPrograms(1, &blit
->DepthFP
);
1095 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1096 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1097 strlen(program2
), (const GLubyte
*) program2
);
1102 * Try to do a glBlitFramebuffer using no-copy texturing.
1103 * We can do this when the src renderbuffer is actually a texture.
1104 * But if the src buffer == dst buffer we cannot do this.
1106 * \return new buffer mask indicating the buffers left to blit using the
1110 blitframebuffer_texture(struct gl_context
*ctx
,
1111 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1112 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1113 GLbitfield mask
, GLenum filter
)
1115 if (mask
& GL_COLOR_BUFFER_BIT
) {
1116 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1117 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1118 const struct gl_renderbuffer_attachment
*drawAtt
=
1119 &drawFb
->Attachment
[drawFb
->_ColorDrawBufferIndexes
[0]];
1120 const struct gl_renderbuffer_attachment
*readAtt
=
1121 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1123 if (readAtt
&& readAtt
->Texture
) {
1124 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1125 const GLuint srcLevel
= readAtt
->TextureLevel
;
1126 const GLenum minFilterSave
= texObj
->MinFilter
;
1127 const GLenum magFilterSave
= texObj
->MagFilter
;
1128 const GLint baseLevelSave
= texObj
->BaseLevel
;
1129 const GLint maxLevelSave
= texObj
->MaxLevel
;
1130 const GLenum wrapSSave
= texObj
->WrapS
;
1131 const GLenum wrapTSave
= texObj
->WrapT
;
1132 const GLenum target
= texObj
->Target
;
1134 if (drawAtt
->Texture
== readAtt
->Texture
) {
1135 /* Can't use same texture as both the source and dest. We need
1136 * to handle overlapping blits and besides, some hw may not
1142 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1143 /* Can't handle other texture types at this time */
1148 printf("Blit from texture!\n");
1149 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1150 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1153 /* Prepare src texture state */
1154 _mesa_BindTexture(target
, texObj
->Name
);
1155 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
1156 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
1157 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1158 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1159 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1161 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1162 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1163 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1164 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1166 /* Prepare vertex data (the VBO was previously created and bound) */
1171 struct vertex verts
[4];
1172 GLfloat s0
, t0
, s1
, t1
;
1174 if (target
== GL_TEXTURE_2D
) {
1175 const struct gl_texture_image
*texImage
1176 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1177 s0
= srcX0
/ (float) texImage
->Width
;
1178 s1
= srcX1
/ (float) texImage
->Width
;
1179 t0
= srcY0
/ (float) texImage
->Height
;
1180 t1
= srcY1
/ (float) texImage
->Height
;
1183 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1190 verts
[0].x
= (GLfloat
) dstX0
;
1191 verts
[0].y
= (GLfloat
) dstY0
;
1192 verts
[1].x
= (GLfloat
) dstX1
;
1193 verts
[1].y
= (GLfloat
) dstY0
;
1194 verts
[2].x
= (GLfloat
) dstX1
;
1195 verts
[2].y
= (GLfloat
) dstY1
;
1196 verts
[3].x
= (GLfloat
) dstX0
;
1197 verts
[3].y
= (GLfloat
) dstY1
;
1208 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1211 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1213 /* Restore texture object state, the texture binding will
1214 * be restored by _mesa_meta_end().
1216 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
1217 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
1218 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1219 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1220 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1222 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
1223 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
1225 /* Done with color buffer */
1226 mask
&= ~GL_COLOR_BUFFER_BIT
;
1235 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1236 * of texture mapping and polygon rendering.
1239 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1240 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1241 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1242 GLbitfield mask
, GLenum filter
)
1244 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1245 struct temp_texture
*tex
= get_temp_texture(ctx
);
1246 const GLsizei maxTexSize
= tex
->MaxSize
;
1247 const GLint srcX
= MIN2(srcX0
, srcX1
);
1248 const GLint srcY
= MIN2(srcY0
, srcY1
);
1249 const GLint srcW
= abs(srcX1
- srcX0
);
1250 const GLint srcH
= abs(srcY1
- srcY0
);
1251 const GLboolean srcFlipX
= srcX1
< srcX0
;
1252 const GLboolean srcFlipY
= srcY1
< srcY0
;
1256 struct vertex verts
[4];
1259 if (srcW
> maxTexSize
|| srcH
> maxTexSize
) {
1260 /* XXX avoid this fallback */
1261 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1262 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1278 /* only scissor effects blit so save/clear all other relevant state */
1279 _mesa_meta_begin(ctx
, ~META_SCISSOR
);
1281 if (blit
->ArrayObj
== 0) {
1282 /* one-time setup */
1284 /* create vertex array object */
1285 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1286 _mesa_BindVertexArray(blit
->ArrayObj
);
1288 /* create vertex array buffer */
1289 _mesa_GenBuffersARB(1, &blit
->VBO
);
1290 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1291 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1292 NULL
, GL_DYNAMIC_DRAW_ARB
);
1294 /* setup vertex arrays */
1295 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1296 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1297 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1298 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1301 _mesa_BindVertexArray(blit
->ArrayObj
);
1302 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1305 /* Try faster, direct texture approach first */
1306 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1307 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1309 _mesa_meta_end(ctx
);
1313 /* Continue with "normal" approach which involves copying the src rect
1314 * into a temporary texture and is "blitted" by drawing a textured quad.
1317 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1319 /* vertex positions/texcoords (after texture allocation!) */
1321 verts
[0].x
= (GLfloat
) dstX0
;
1322 verts
[0].y
= (GLfloat
) dstY0
;
1323 verts
[1].x
= (GLfloat
) dstX1
;
1324 verts
[1].y
= (GLfloat
) dstY0
;
1325 verts
[2].x
= (GLfloat
) dstX1
;
1326 verts
[2].y
= (GLfloat
) dstY1
;
1327 verts
[3].x
= (GLfloat
) dstX0
;
1328 verts
[3].y
= (GLfloat
) dstY1
;
1332 verts
[1].s
= tex
->Sright
;
1334 verts
[2].s
= tex
->Sright
;
1335 verts
[2].t
= tex
->Ttop
;
1337 verts
[3].t
= tex
->Ttop
;
1339 /* upload new vertex data */
1340 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1343 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1345 if (mask
& GL_COLOR_BUFFER_BIT
) {
1346 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1348 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1349 mask
&= ~GL_COLOR_BUFFER_BIT
;
1352 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1353 GLuint
*tmp
= (GLuint
*) malloc(srcW
* srcH
* sizeof(GLuint
));
1356 init_blit_depth_pixels(ctx
);
1358 /* maybe change tex format here */
1359 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1361 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1362 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1364 setup_drawpix_texture(ctx
, tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1365 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1367 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1368 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1369 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1370 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1371 _mesa_DepthFunc(GL_ALWAYS
);
1372 _mesa_DepthMask(GL_TRUE
);
1374 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1375 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1381 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1382 /* XXX can't easily do stencil */
1385 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1387 _mesa_meta_end(ctx
);
1390 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1391 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1397 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1400 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1402 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1404 GLfloat x
, y
, z
, r
, g
, b
, a
;
1406 struct vertex verts
[4];
1407 /* save all state but scissor, pixel pack/unpack */
1408 GLbitfield metaSave
= META_ALL
- META_SCISSOR
- META_PIXEL_STORE
;
1409 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1411 if (buffers
& BUFFER_BITS_COLOR
) {
1412 /* if clearing color buffers, don't save/restore colormask */
1413 metaSave
-= META_COLOR_MASK
;
1416 _mesa_meta_begin(ctx
, metaSave
);
1418 if (clear
->ArrayObj
== 0) {
1419 /* one-time setup */
1421 /* create vertex array object */
1422 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1423 _mesa_BindVertexArray(clear
->ArrayObj
);
1425 /* create vertex array buffer */
1426 _mesa_GenBuffersARB(1, &clear
->VBO
);
1427 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1429 /* setup vertex arrays */
1430 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1431 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1432 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1433 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1436 _mesa_BindVertexArray(clear
->ArrayObj
);
1437 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1440 /* GL_COLOR_BUFFER_BIT */
1441 if (buffers
& BUFFER_BITS_COLOR
) {
1442 /* leave colormask, glDrawBuffer state as-is */
1445 ASSERT(metaSave
& META_COLOR_MASK
);
1446 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1449 /* GL_DEPTH_BUFFER_BIT */
1450 if (buffers
& BUFFER_BIT_DEPTH
) {
1451 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1452 _mesa_DepthFunc(GL_ALWAYS
);
1453 _mesa_DepthMask(GL_TRUE
);
1456 assert(!ctx
->Depth
.Test
);
1459 /* GL_STENCIL_BUFFER_BIT */
1460 if (buffers
& BUFFER_BIT_STENCIL
) {
1461 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1462 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1463 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1464 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1465 ctx
->Stencil
.Clear
& stencilMax
,
1466 ctx
->Stencil
.WriteMask
[0]);
1469 assert(!ctx
->Stencil
.Enabled
);
1472 /* vertex positions/colors */
1474 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1475 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1476 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1477 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1478 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1495 for (i
= 0; i
< 4; i
++) {
1496 verts
[i
].r
= ctx
->Color
.ClearColor
[0];
1497 verts
[i
].g
= ctx
->Color
.ClearColor
[1];
1498 verts
[i
].b
= ctx
->Color
.ClearColor
[2];
1499 verts
[i
].a
= ctx
->Color
.ClearColor
[3];
1502 /* upload new vertex data */
1503 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1504 GL_DYNAMIC_DRAW_ARB
);
1508 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1510 _mesa_meta_end(ctx
);
1515 * Meta implementation of ctx->Driver.CopyPixels() in terms
1516 * of texture mapping and polygon rendering.
1519 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
1520 GLsizei width
, GLsizei height
,
1521 GLint dstX
, GLint dstY
, GLenum type
)
1523 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1524 struct temp_texture
*tex
= get_temp_texture(ctx
);
1526 GLfloat x
, y
, z
, s
, t
;
1528 struct vertex verts
[4];
1530 GLenum intFormat
= GL_RGBA
;
1532 if (type
!= GL_COLOR
||
1533 ctx
->_ImageTransferState
||
1535 width
> tex
->MaxSize
||
1536 height
> tex
->MaxSize
) {
1537 /* XXX avoid this fallback */
1538 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1542 /* Most GL state applies to glCopyPixels, but a there's a few things
1543 * we need to override:
1545 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1552 if (copypix
->ArrayObj
== 0) {
1553 /* one-time setup */
1555 /* create vertex array object */
1556 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
1557 _mesa_BindVertexArray(copypix
->ArrayObj
);
1559 /* create vertex array buffer */
1560 _mesa_GenBuffersARB(1, ©pix
->VBO
);
1561 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1562 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1563 NULL
, GL_DYNAMIC_DRAW_ARB
);
1565 /* setup vertex arrays */
1566 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1567 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1568 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1569 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1572 _mesa_BindVertexArray(copypix
->ArrayObj
);
1573 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1576 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
1578 /* vertex positions, texcoords (after texture allocation!) */
1580 const GLfloat dstX0
= (GLfloat
) dstX
;
1581 const GLfloat dstY0
= (GLfloat
) dstY
;
1582 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1583 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1584 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1594 verts
[1].s
= tex
->Sright
;
1599 verts
[2].s
= tex
->Sright
;
1600 verts
[2].t
= tex
->Ttop
;
1605 verts
[3].t
= tex
->Ttop
;
1607 /* upload new vertex data */
1608 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1611 /* Alloc/setup texture */
1612 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
1613 GL_RGBA
, GL_NEAREST
);
1615 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1617 /* draw textured quad */
1618 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1620 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1622 _mesa_meta_end(ctx
);
1628 * When the glDrawPixels() image size is greater than the max rectangle
1629 * texture size we use this function to break the glDrawPixels() image
1630 * into tiles which fit into the max texture size.
1633 tiled_draw_pixels(struct gl_context
*ctx
,
1635 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1636 GLenum format
, GLenum type
,
1637 const struct gl_pixelstore_attrib
*unpack
,
1638 const GLvoid
*pixels
)
1640 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
1643 if (tileUnpack
.RowLength
== 0)
1644 tileUnpack
.RowLength
= width
;
1646 for (i
= 0; i
< width
; i
+= tileSize
) {
1647 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
1648 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
1650 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
1652 for (j
= 0; j
< height
; j
+= tileSize
) {
1653 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
1654 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
1656 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
1658 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
1659 format
, type
, &tileUnpack
, pixels
);
1666 * One-time init for drawing stencil pixels.
1669 init_draw_stencil_pixels(struct gl_context
*ctx
)
1671 /* This program is run eight times, once for each stencil bit.
1672 * The stencil values to draw are found in an 8-bit alpha texture.
1673 * We read the texture/stencil value and test if bit 'b' is set.
1674 * If the bit is not set, use KIL to kill the fragment.
1675 * Finally, we use the stencil test to update the stencil buffer.
1677 * The basic algorithm for checking if a bit is set is:
1678 * if (is_odd(value / (1 << bit)))
1679 * result is one (or non-zero).
1682 * The program parameter contains three values:
1683 * parm.x = 255 / (1 << bit)
1687 static const char *program
=
1689 "PARAM parm = program.local[0]; \n"
1691 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1692 "# t = t * 255 / bit \n"
1693 "MUL t.x, t.a, parm.x; \n"
1696 "SUB t.x, t.x, t.y; \n"
1698 "MUL t.x, t.x, parm.y; \n"
1699 "# t = fract(t.x) \n"
1700 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
1701 "# t.x = (t.x == 0 ? 1 : 0) \n"
1702 "SGE t.x, -t.x, parm.z; \n"
1704 "# for debug only \n"
1705 "#MOV result.color, t.x; \n"
1707 char program2
[1000];
1708 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1709 struct temp_texture
*tex
= get_temp_texture(ctx
);
1710 const char *texTarget
;
1712 assert(drawpix
->StencilFP
== 0);
1714 /* replace %s with "RECT" or "2D" */
1715 assert(strlen(program
) + 4 < sizeof(program2
));
1716 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1720 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1722 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
1723 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1724 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1725 strlen(program2
), (const GLubyte
*) program2
);
1730 * One-time init for drawing depth pixels.
1733 init_draw_depth_pixels(struct gl_context
*ctx
)
1735 static const char *program
=
1737 "PARAM color = program.local[0]; \n"
1738 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1739 "MOV result.color, color; \n"
1742 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1743 struct temp_texture
*tex
= get_temp_texture(ctx
);
1744 const char *texTarget
;
1746 assert(drawpix
->DepthFP
== 0);
1748 /* replace %s with "RECT" or "2D" */
1749 assert(strlen(program
) + 4 < sizeof(program2
));
1750 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1754 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1756 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
1757 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1758 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1759 strlen(program2
), (const GLubyte
*) program2
);
1764 * Meta implementation of ctx->Driver.DrawPixels() in terms
1765 * of texture mapping and polygon rendering.
1768 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
1769 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1770 GLenum format
, GLenum type
,
1771 const struct gl_pixelstore_attrib
*unpack
,
1772 const GLvoid
*pixels
)
1774 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1775 struct temp_texture
*tex
= get_temp_texture(ctx
);
1776 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
1777 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
1779 GLfloat x
, y
, z
, s
, t
;
1781 struct vertex verts
[4];
1782 GLenum texIntFormat
;
1783 GLboolean fallback
, newTex
;
1784 GLbitfield metaExtraSave
= 0x0;
1788 * Determine if we can do the glDrawPixels with texture mapping.
1790 fallback
= GL_FALSE
;
1791 if (ctx
->_ImageTransferState
||
1796 if (_mesa_is_color_format(format
)) {
1797 /* use more compact format when possible */
1798 /* XXX disable special case for GL_LUMINANCE for now to work around
1799 * apparent i965 driver bug (see bug #23670).
1801 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
1802 texIntFormat
= format
;
1804 texIntFormat
= GL_RGBA
;
1806 else if (_mesa_is_stencil_format(format
)) {
1807 if (ctx
->Extensions
.ARB_fragment_program
&&
1808 ctx
->Pixel
.IndexShift
== 0 &&
1809 ctx
->Pixel
.IndexOffset
== 0 &&
1810 type
== GL_UNSIGNED_BYTE
) {
1811 /* We'll store stencil as alpha. This only works for GLubyte
1812 * image data because of how incoming values are mapped to alpha
1815 texIntFormat
= GL_ALPHA
;
1816 metaExtraSave
= (META_COLOR_MASK
|
1825 else if (_mesa_is_depth_format(format
)) {
1826 if (ctx
->Extensions
.ARB_depth_texture
&&
1827 ctx
->Extensions
.ARB_fragment_program
) {
1828 texIntFormat
= GL_DEPTH_COMPONENT
;
1829 metaExtraSave
= (META_SHADER
);
1840 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
1841 format
, type
, unpack
, pixels
);
1846 * Check image size against max texture size, draw as tiles if needed.
1848 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
1849 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
1850 format
, type
, unpack
, pixels
);
1854 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
1855 * but a there's a few things we need to override:
1857 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1865 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
1867 /* vertex positions, texcoords (after texture allocation!) */
1869 const GLfloat x0
= (GLfloat
) x
;
1870 const GLfloat y0
= (GLfloat
) y
;
1871 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
1872 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
1873 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1883 verts
[1].s
= tex
->Sright
;
1888 verts
[2].s
= tex
->Sright
;
1889 verts
[2].t
= tex
->Ttop
;
1894 verts
[3].t
= tex
->Ttop
;
1897 if (drawpix
->ArrayObj
== 0) {
1898 /* one-time setup: create vertex array object */
1899 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
1901 _mesa_BindVertexArray(drawpix
->ArrayObj
);
1903 /* create vertex array buffer */
1904 _mesa_GenBuffersARB(1, &vbo
);
1905 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
1906 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1907 verts
, GL_DYNAMIC_DRAW_ARB
);
1909 /* setup vertex arrays */
1910 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1911 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1912 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1913 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1915 /* set given unpack params */
1916 ctx
->Unpack
= *unpack
;
1918 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1920 if (_mesa_is_stencil_format(format
)) {
1921 /* Drawing stencil */
1924 if (!drawpix
->StencilFP
)
1925 init_draw_stencil_pixels(ctx
);
1927 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1928 GL_ALPHA
, type
, pixels
);
1930 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1932 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1934 /* set all stencil bits to 0 */
1935 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1936 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
1937 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1939 /* set stencil bits to 1 where needed */
1940 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
1942 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1943 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1945 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
1946 const GLuint mask
= 1 << bit
;
1947 if (mask
& origStencilMask
) {
1948 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
1949 _mesa_StencilMask(mask
);
1951 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
1952 255.0 / mask
, 0.5, 0.0, 0.0);
1954 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1958 else if (_mesa_is_depth_format(format
)) {
1960 if (!drawpix
->DepthFP
)
1961 init_draw_depth_pixels(ctx
);
1963 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1964 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1966 /* polygon color = current raster color */
1967 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
1968 ctx
->Current
.RasterColor
);
1970 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1971 format
, type
, pixels
);
1973 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1977 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1978 format
, type
, pixels
);
1979 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1982 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1984 _mesa_DeleteBuffersARB(1, &vbo
);
1986 /* restore unpack params */
1987 ctx
->Unpack
= unpackSave
;
1989 _mesa_meta_end(ctx
);
1993 alpha_test_raster_color(struct gl_context
*ctx
)
1995 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
1996 GLfloat ref
= ctx
->Color
.AlphaRef
;
1998 switch (ctx
->Color
.AlphaFunc
) {
2004 return alpha
== ref
;
2006 return alpha
<= ref
;
2010 return alpha
!= ref
;
2012 return alpha
>= ref
;
2022 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2023 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2024 * tracker would improve performance a lot.
2027 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2028 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2029 const struct gl_pixelstore_attrib
*unpack
,
2030 const GLubyte
*bitmap1
)
2032 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2033 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2034 const GLenum texIntFormat
= GL_ALPHA
;
2035 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2038 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2040 struct vertex verts
[4];
2045 * Check if swrast fallback is needed.
2047 if (ctx
->_ImageTransferState
||
2048 ctx
->FragmentProgram
._Enabled
||
2050 ctx
->Texture
._EnabledUnits
||
2051 width
> tex
->MaxSize
||
2052 height
> tex
->MaxSize
) {
2053 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2057 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2060 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2061 * but a there's a few things we need to override:
2063 _mesa_meta_begin(ctx
, (META_ALPHA_TEST
|
2065 META_RASTERIZATION
|
2072 if (bitmap
->ArrayObj
== 0) {
2073 /* one-time setup */
2075 /* create vertex array object */
2076 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2077 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2079 /* create vertex array buffer */
2080 _mesa_GenBuffersARB(1, &bitmap
->VBO
);
2081 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2082 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2083 NULL
, GL_DYNAMIC_DRAW_ARB
);
2085 /* setup vertex arrays */
2086 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2087 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2088 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2089 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2090 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2091 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2094 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2095 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2098 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2100 /* vertex positions, texcoords, colors (after texture allocation!) */
2102 const GLfloat x0
= (GLfloat
) x
;
2103 const GLfloat y0
= (GLfloat
) y
;
2104 const GLfloat x1
= (GLfloat
) (x
+ width
);
2105 const GLfloat y1
= (GLfloat
) (y
+ height
);
2106 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2117 verts
[1].s
= tex
->Sright
;
2122 verts
[2].s
= tex
->Sright
;
2123 verts
[2].t
= tex
->Ttop
;
2128 verts
[3].t
= tex
->Ttop
;
2130 for (i
= 0; i
< 4; i
++) {
2131 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2132 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2133 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2134 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2137 /* upload new vertex data */
2138 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2141 /* choose different foreground/background alpha values */
2142 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2143 bg
= (fg
> 127 ? 0 : 255);
2145 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2147 _mesa_meta_end(ctx
);
2151 bitmap8
= (GLubyte
*) malloc(width
* height
);
2153 memset(bitmap8
, bg
, width
* height
);
2154 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2155 bitmap8
, width
, fg
);
2157 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2159 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2160 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2162 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2163 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2165 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2167 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2172 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2174 _mesa_meta_end(ctx
);
2179 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2180 * software fallback. The fallback path will require that the texture
2181 * images are mapped.
2182 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2185 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
2186 struct gl_texture_object
*texObj
)
2188 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2189 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2190 struct gl_texture_image
*baseImage
;
2194 /* check for fallbacks */
2195 if (!ctx
->Extensions
.EXT_framebuffer_object
||
2196 target
== GL_TEXTURE_3D
) {
2200 srcLevel
= texObj
->BaseLevel
;
2201 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2202 if (!baseImage
|| _mesa_is_format_compressed(baseImage
->TexFormat
)) {
2207 * Test that we can actually render in the texture's format.
2210 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2211 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2213 if (target
== GL_TEXTURE_1D
) {
2214 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2215 GL_COLOR_ATTACHMENT0_EXT
,
2216 target
, texObj
->Name
, srcLevel
);
2219 /* other work is needed to enable 3D mipmap generation */
2220 else if (target
== GL_TEXTURE_3D
) {
2222 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2223 GL_COLOR_ATTACHMENT0_EXT
,
2224 target
, texObj
->Name
, srcLevel
, zoffset
);
2229 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2230 GL_COLOR_ATTACHMENT0_EXT
,
2231 target
, texObj
->Name
, srcLevel
);
2234 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2236 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2238 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2247 * Called via ctx->Driver.GenerateMipmap()
2248 * Note: texture borders and 3D texture support not yet complete.
2251 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
2252 struct gl_texture_object
*texObj
)
2254 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2256 GLfloat x
, y
, s
, t
, r
;
2258 struct vertex verts
[4];
2259 const GLuint baseLevel
= texObj
->BaseLevel
;
2260 const GLuint maxLevel
= texObj
->MaxLevel
;
2261 const GLenum minFilterSave
= texObj
->MinFilter
;
2262 const GLenum magFilterSave
= texObj
->MagFilter
;
2263 const GLint maxLevelSave
= texObj
->MaxLevel
;
2264 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
2265 const GLenum wrapSSave
= texObj
->WrapS
;
2266 const GLenum wrapTSave
= texObj
->WrapT
;
2267 const GLenum wrapRSave
= texObj
->WrapR
;
2268 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2269 const GLuint original_active_unit
= ctx
->Texture
.CurrentUnit
;
2274 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
2275 _mesa_generate_mipmap(ctx
, target
, texObj
);
2279 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
2280 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
2281 faceTarget
= target
;
2282 target
= GL_TEXTURE_CUBE_MAP
;
2285 faceTarget
= target
;
2288 _mesa_meta_begin(ctx
, META_ALL
);
2290 if (original_active_unit
!= 0)
2291 _mesa_BindTexture(target
, texObj
->Name
);
2293 if (mipmap
->ArrayObj
== 0) {
2294 /* one-time setup */
2296 /* create vertex array object */
2297 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
2298 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
2300 /* create vertex array buffer */
2301 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
2302 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2303 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2304 NULL
, GL_DYNAMIC_DRAW_ARB
);
2306 /* setup vertex arrays */
2307 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2308 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2309 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2310 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2313 _mesa_BindVertexArray(mipmap
->ArrayObj
);
2314 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2318 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2320 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2322 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
2323 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
2324 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
2325 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2326 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2327 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
2329 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2331 /* setup texcoords once (XXX what about border?) */
2332 switch (faceTarget
) {
2354 static const GLfloat st
[4][2] = {
2355 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2359 /* loop over quad verts */
2360 for (i
= 0; i
< 4; i
++) {
2361 /* Compute sc = +/-scale and tc = +/-scale.
2362 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2363 * though that can still sometimes happen with this scale factor...
2365 const GLfloat scale
= 0.9999f
;
2366 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2367 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2369 switch (faceTarget
) {
2370 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2375 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2380 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2385 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2390 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2395 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2407 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2409 /* setup vertex positions */
2420 /* upload new vertex data */
2421 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2424 /* setup projection matrix */
2425 _mesa_MatrixMode(GL_PROJECTION
);
2426 _mesa_LoadIdentity();
2427 _mesa_Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
2429 /* texture is already locked, unlock now */
2430 _mesa_unlock_texture(ctx
, texObj
);
2432 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
2433 const struct gl_texture_image
*srcImage
;
2434 const GLuint srcLevel
= dstLevel
- 1;
2435 GLsizei srcWidth
, srcHeight
, srcDepth
;
2436 GLsizei dstWidth
, dstHeight
, dstDepth
;
2439 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
2440 assert(srcImage
->Border
== 0); /* XXX we can fix this */
2442 /* src size w/out border */
2443 srcWidth
= srcImage
->Width
- 2 * border
;
2444 srcHeight
= srcImage
->Height
- 2 * border
;
2445 srcDepth
= srcImage
->Depth
- 2 * border
;
2447 /* new dst size w/ border */
2448 dstWidth
= MAX2(1, srcWidth
/ 2) + 2 * border
;
2449 dstHeight
= MAX2(1, srcHeight
/ 2) + 2 * border
;
2450 dstDepth
= MAX2(1, srcDepth
/ 2) + 2 * border
;
2452 if (dstWidth
== srcImage
->Width
&&
2453 dstHeight
== srcImage
->Height
&&
2454 dstDepth
== srcImage
->Depth
) {
2459 /* Set MaxLevel large enough to hold the new level when we allocate it */
2460 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
2462 /* Create empty dest image */
2463 if (target
== GL_TEXTURE_1D
) {
2464 _mesa_TexImage1D(target
, dstLevel
, srcImage
->InternalFormat
,
2466 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2468 else if (target
== GL_TEXTURE_3D
) {
2469 _mesa_TexImage3D(target
, dstLevel
, srcImage
->InternalFormat
,
2470 dstWidth
, dstHeight
, dstDepth
, border
,
2471 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2475 _mesa_TexImage2D(faceTarget
, dstLevel
, srcImage
->InternalFormat
,
2476 dstWidth
, dstHeight
, border
,
2477 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2479 if (target
== GL_TEXTURE_CUBE_MAP
) {
2480 /* If texturing from a cube, we need to make sure all src faces
2481 * have been defined (even if we're not sampling from them.)
2482 * Otherwise the texture object will be 'incomplete' and
2483 * texturing from it will not be allowed.
2486 for (face
= 0; face
< 6; face
++) {
2487 if (!texObj
->Image
[face
][srcLevel
] ||
2488 texObj
->Image
[face
][srcLevel
]->Width
!= srcWidth
) {
2489 _mesa_TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ face
,
2490 srcLevel
, srcImage
->InternalFormat
,
2491 srcWidth
, srcHeight
, border
,
2492 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2498 /* limit minification to src level */
2499 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
2501 /* Set to draw into the current dstLevel */
2502 if (target
== GL_TEXTURE_1D
) {
2503 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2504 GL_COLOR_ATTACHMENT0_EXT
,
2509 else if (target
== GL_TEXTURE_3D
) {
2510 GLint zoffset
= 0; /* XXX unfinished */
2511 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2512 GL_COLOR_ATTACHMENT0_EXT
,
2519 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2520 GL_COLOR_ATTACHMENT0_EXT
,
2526 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
2529 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2530 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2535 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
2536 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
2538 /* setup viewport */
2539 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
2541 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2544 _mesa_lock_texture(ctx
, texObj
); /* relock */
2546 _mesa_meta_end(ctx
);
2548 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
2549 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
2550 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
2551 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
2552 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
2553 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
2554 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, wrapRSave
);
2556 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2561 * Determine the GL data type to use for the temporary image read with
2562 * ReadPixels() and passed to Tex[Sub]Image().
2565 get_temp_image_type(struct gl_context
*ctx
, GLenum baseFormat
)
2567 switch (baseFormat
) {
2572 case GL_LUMINANCE_ALPHA
:
2574 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2575 return GL_UNSIGNED_BYTE
;
2576 else if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2577 return GL_UNSIGNED_SHORT
;
2580 case GL_DEPTH_COMPONENT
:
2581 return GL_UNSIGNED_INT
;
2582 case GL_DEPTH_STENCIL
:
2583 return GL_UNSIGNED_INT_24_8
;
2585 _mesa_problem(ctx
, "Unexpected format in get_temp_image_type()");
2592 * Helper for _mesa_meta_CopyTexImage1/2D() functions.
2593 * Have to be careful with locking and meta state for pixel transfer.
2596 copy_tex_image(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
2597 GLenum internalFormat
, GLint x
, GLint y
,
2598 GLsizei width
, GLsizei height
, GLint border
)
2600 struct gl_texture_object
*texObj
;
2601 struct gl_texture_image
*texImage
;
2602 GLenum format
, type
;
2606 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2607 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2609 /* Choose format/type for temporary image buffer */
2610 format
= _mesa_base_tex_format(ctx
, internalFormat
);
2611 type
= get_temp_image_type(ctx
, format
);
2612 bpp
= _mesa_bytes_per_pixel(format
, type
);
2614 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_image()");
2619 * Alloc image buffer (XXX could use a PBO)
2621 buf
= malloc(width
* height
* bpp
);
2623 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
2627 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2630 * Read image from framebuffer (disable pixel transfer ops)
2632 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2633 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2634 format
, type
, &ctx
->Pack
, buf
);
2635 _mesa_meta_end(ctx
);
2637 if (texImage
->Data
) {
2638 ctx
->Driver
.FreeTexImageData(ctx
, texImage
);
2641 /* The texture's format was already chosen in _mesa_CopyTexImage() */
2642 ASSERT(texImage
->TexFormat
!= MESA_FORMAT_NONE
);
2645 * Store texture data (with pixel transfer ops)
2647 _mesa_meta_begin(ctx
, META_PIXEL_STORE
);
2649 _mesa_update_state(ctx
); /* to update pixel transfer state */
2651 if (target
== GL_TEXTURE_1D
) {
2652 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
2653 width
, border
, format
, type
,
2654 buf
, &ctx
->Unpack
, texObj
, texImage
);
2657 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
2658 width
, height
, border
, format
, type
,
2659 buf
, &ctx
->Unpack
, texObj
, texImage
);
2661 _mesa_meta_end(ctx
);
2663 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
2670 _mesa_meta_CopyTexImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2671 GLenum internalFormat
, GLint x
, GLint y
,
2672 GLsizei width
, GLint border
)
2674 copy_tex_image(ctx
, 1, target
, level
, internalFormat
, x
, y
,
2680 _mesa_meta_CopyTexImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2681 GLenum internalFormat
, GLint x
, GLint y
,
2682 GLsizei width
, GLsizei height
, GLint border
)
2684 copy_tex_image(ctx
, 2, target
, level
, internalFormat
, x
, y
,
2685 width
, height
, border
);
2691 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2692 * Have to be careful with locking and meta state for pixel transfer.
2695 copy_tex_sub_image(struct gl_context
*ctx
,
2696 GLuint dims
, GLenum target
, GLint level
,
2697 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2699 GLsizei width
, GLsizei height
)
2701 struct gl_texture_object
*texObj
;
2702 struct gl_texture_image
*texImage
;
2703 GLenum format
, type
;
2707 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2708 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2710 /* Choose format/type for temporary image buffer */
2711 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
2712 type
= get_temp_image_type(ctx
, format
);
2713 bpp
= _mesa_bytes_per_pixel(format
, type
);
2715 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_sub_image()");
2720 * Alloc image buffer (XXX could use a PBO)
2722 buf
= malloc(width
* height
* bpp
);
2724 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
2728 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2731 * Read image from framebuffer (disable pixel transfer ops)
2733 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2734 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2735 format
, type
, &ctx
->Pack
, buf
);
2736 _mesa_meta_end(ctx
);
2738 _mesa_update_state(ctx
); /* to update pixel transfer state */
2741 * Store texture data (with pixel transfer ops)
2743 _mesa_meta_begin(ctx
, META_PIXEL_STORE
);
2744 if (target
== GL_TEXTURE_1D
) {
2745 ctx
->Driver
.TexSubImage1D(ctx
, target
, level
, xoffset
,
2746 width
, format
, type
, buf
,
2747 &ctx
->Unpack
, texObj
, texImage
);
2749 else if (target
== GL_TEXTURE_3D
) {
2750 ctx
->Driver
.TexSubImage3D(ctx
, target
, level
, xoffset
, yoffset
, zoffset
,
2751 width
, height
, 1, format
, type
, buf
,
2752 &ctx
->Unpack
, texObj
, texImage
);
2755 ctx
->Driver
.TexSubImage2D(ctx
, target
, level
, xoffset
, yoffset
,
2756 width
, height
, format
, type
, buf
,
2757 &ctx
->Unpack
, texObj
, texImage
);
2759 _mesa_meta_end(ctx
);
2761 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
2768 _mesa_meta_CopyTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2770 GLint x
, GLint y
, GLsizei width
)
2772 copy_tex_sub_image(ctx
, 1, target
, level
, xoffset
, 0, 0,
2778 _mesa_meta_CopyTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2779 GLint xoffset
, GLint yoffset
,
2781 GLsizei width
, GLsizei height
)
2783 copy_tex_sub_image(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2784 x
, y
, width
, height
);
2789 _mesa_meta_CopyTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2790 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2792 GLsizei width
, GLsizei height
)
2794 copy_tex_sub_image(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2795 x
, y
, width
, height
);
2800 _mesa_meta_CopyColorTable(struct gl_context
*ctx
,
2801 GLenum target
, GLenum internalformat
,
2802 GLint x
, GLint y
, GLsizei width
)
2806 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2808 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyColorTable");
2813 * Read image from framebuffer (disable pixel transfer ops)
2815 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2816 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2817 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2819 _mesa_ColorTable(target
, internalformat
, width
, GL_RGBA
, GL_FLOAT
, buf
);
2821 _mesa_meta_end(ctx
);
2828 _mesa_meta_CopyColorSubTable(struct gl_context
*ctx
,GLenum target
, GLsizei start
,
2829 GLint x
, GLint y
, GLsizei width
)
2833 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2835 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyColorSubTable");
2840 * Read image from framebuffer (disable pixel transfer ops)
2842 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2843 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2844 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2846 _mesa_ColorSubTable(target
, start
, width
, GL_RGBA
, GL_FLOAT
, buf
);
2848 _mesa_meta_end(ctx
);