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/state.h"
56 #include "main/stencil.h"
57 #include "main/texobj.h"
58 #include "main/texenv.h"
59 #include "main/teximage.h"
60 #include "main/texparam.h"
61 #include "main/texstate.h"
62 #include "main/varray.h"
63 #include "main/viewport.h"
64 #include "program/program.h"
65 #include "swrast/swrast.h"
66 #include "drivers/common/meta.h"
69 /** Return offset in bytes of the field within a vertex struct */
70 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
74 * Flags passed to _mesa_meta_begin().
78 #define META_ALPHA_TEST 0x1
79 #define META_BLEND 0x2 /**< includes logicop */
80 #define META_COLOR_MASK 0x4
81 #define META_DEPTH_TEST 0x8
83 #define META_PIXEL_STORE 0x20
84 #define META_PIXEL_TRANSFER 0x40
85 #define META_RASTERIZATION 0x80
86 #define META_SCISSOR 0x100
87 #define META_SHADER 0x200
88 #define META_STENCIL_TEST 0x400
89 #define META_TRANSFORM 0x800 /**< modelview, projection, clip planes */
90 #define META_TEXTURE 0x1000
91 #define META_VERTEX 0x2000
92 #define META_VIEWPORT 0x4000
97 * State which we may save/restore across meta ops.
98 * XXX this may be incomplete...
102 GLbitfield SavedState
; /**< bitmask of META_* flags */
104 /** META_ALPHA_TEST */
105 GLboolean AlphaEnabled
;
108 GLbitfield BlendEnabled
;
109 GLboolean ColorLogicOpEnabled
;
111 /** META_COLOR_MASK */
112 GLubyte ColorMask
[MAX_DRAW_BUFFERS
][4];
114 /** META_DEPTH_TEST */
115 struct gl_depthbuffer_attrib Depth
;
120 /** META_PIXEL_STORE */
121 struct gl_pixelstore_attrib Pack
, Unpack
;
123 /** META_PIXEL_TRANSFER */
124 GLfloat RedBias
, RedScale
;
125 GLfloat GreenBias
, GreenScale
;
126 GLfloat BlueBias
, BlueScale
;
127 GLfloat AlphaBias
, AlphaScale
;
128 GLfloat DepthBias
, DepthScale
;
129 GLboolean MapColorFlag
;
131 /** META_RASTERIZATION */
132 GLenum FrontPolygonMode
, BackPolygonMode
;
133 GLboolean PolygonOffset
;
134 GLboolean PolygonSmooth
;
135 GLboolean PolygonStipple
;
136 GLboolean PolygonCull
;
139 struct gl_scissor_attrib Scissor
;
142 GLboolean VertexProgramEnabled
;
143 struct gl_vertex_program
*VertexProgram
;
144 GLboolean FragmentProgramEnabled
;
145 struct gl_fragment_program
*FragmentProgram
;
148 /** META_STENCIL_TEST */
149 struct gl_stencil_attrib Stencil
;
151 /** META_TRANSFORM */
153 GLfloat ModelviewMatrix
[16];
154 GLfloat ProjectionMatrix
[16];
155 GLfloat TextureMatrix
[16];
156 GLbitfield ClipPlanesEnabled
;
160 GLuint ClientActiveUnit
;
161 /** for unit[0] only */
162 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
163 /** mask of TEXTURE_2D_BIT, etc */
164 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
165 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
166 GLuint EnvMode
; /* unit[0] only */
169 struct gl_array_object
*ArrayObj
;
170 struct gl_buffer_object
*ArrayBufferObj
;
173 GLint ViewportX
, ViewportY
, ViewportW
, ViewportH
;
174 GLclampd DepthNear
, DepthFar
;
176 /** Miscellaneous (always disabled) */
182 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
183 * This is currently shared by all the meta ops. But we could create a
184 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
189 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
190 GLsizei MinSize
; /**< Min texture size to allocate */
191 GLsizei MaxSize
; /**< Max possible texture size */
192 GLboolean NPOT
; /**< Non-power of two size OK? */
193 GLsizei Width
, Height
; /**< Current texture size */
195 GLfloat Sright
, Ttop
; /**< right, top texcoords */
200 * State for glBlitFramebufer()
211 * State for glClear()
221 * State for glCopyPixels()
231 * State for glDrawPixels()
237 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
238 GLuint DepthFP
; /**< Fragment program for drawing depth images */
243 * State for glBitmap()
249 struct temp_texture Tex
; /**< separate texture from other meta ops */
254 * State for _mesa_meta_generate_mipmap()
256 struct gen_mipmap_state
265 * All per-context meta state.
269 struct save_state Save
; /**< state saved during meta-ops */
271 struct temp_texture TempTex
;
273 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
274 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
275 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
276 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
277 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
278 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
283 * Initialize meta-ops for a context.
284 * To be called once during context creation.
287 _mesa_meta_init(GLcontext
*ctx
)
291 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
296 * Free context meta-op state.
297 * To be called once during context destruction.
300 _mesa_meta_free(GLcontext
*ctx
)
302 /* Note: Any textures, VBOs, etc, that we allocate should get
303 * freed by the normal context destruction code. But this would be
304 * the place to free other meta data someday.
312 * Enter meta state. This is like a light-weight version of glPushAttrib
313 * but it also resets most GL state back to default values.
315 * \param state bitmask of META_* flags indicating which attribute groups
316 * to save and reset to their defaults
319 _mesa_meta_begin(GLcontext
*ctx
, GLbitfield state
)
321 struct save_state
*save
= &ctx
->Meta
->Save
;
323 save
->SavedState
= state
;
325 if (state
& META_ALPHA_TEST
) {
326 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
327 if (ctx
->Color
.AlphaEnabled
)
328 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
331 if (state
& META_BLEND
) {
332 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
333 if (ctx
->Color
.BlendEnabled
) {
334 if (ctx
->Extensions
.EXT_draw_buffers2
) {
336 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
337 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
341 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
344 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
345 if (ctx
->Color
.ColorLogicOpEnabled
)
346 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
349 if (state
& META_COLOR_MASK
) {
350 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
351 sizeof(ctx
->Color
.ColorMask
));
352 if (!ctx
->Color
.ColorMask
[0][0] ||
353 !ctx
->Color
.ColorMask
[0][1] ||
354 !ctx
->Color
.ColorMask
[0][2] ||
355 !ctx
->Color
.ColorMask
[0][3])
356 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
359 if (state
& META_DEPTH_TEST
) {
360 save
->Depth
= ctx
->Depth
; /* struct copy */
362 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
365 if (state
& META_FOG
) {
366 save
->Fog
= ctx
->Fog
.Enabled
;
367 if (ctx
->Fog
.Enabled
)
368 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
371 if (state
& META_PIXEL_STORE
) {
372 save
->Pack
= ctx
->Pack
;
373 save
->Unpack
= ctx
->Unpack
;
374 ctx
->Pack
= ctx
->DefaultPacking
;
375 ctx
->Unpack
= ctx
->DefaultPacking
;
378 if (state
& META_PIXEL_TRANSFER
) {
379 save
->RedScale
= ctx
->Pixel
.RedScale
;
380 save
->RedBias
= ctx
->Pixel
.RedBias
;
381 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
382 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
383 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
384 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
385 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
386 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
387 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
388 ctx
->Pixel
.RedScale
= 1.0F
;
389 ctx
->Pixel
.RedBias
= 0.0F
;
390 ctx
->Pixel
.GreenScale
= 1.0F
;
391 ctx
->Pixel
.GreenBias
= 0.0F
;
392 ctx
->Pixel
.BlueScale
= 1.0F
;
393 ctx
->Pixel
.BlueBias
= 0.0F
;
394 ctx
->Pixel
.AlphaScale
= 1.0F
;
395 ctx
->Pixel
.AlphaBias
= 0.0F
;
396 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
398 ctx
->NewState
|=_NEW_PIXEL
;
401 if (state
& META_RASTERIZATION
) {
402 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
403 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
404 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
405 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
406 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
407 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
408 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
409 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
410 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
411 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
412 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
415 if (state
& META_SCISSOR
) {
416 save
->Scissor
= ctx
->Scissor
; /* struct copy */
417 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
420 if (state
& META_SHADER
) {
421 if (ctx
->Extensions
.ARB_vertex_program
) {
422 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
423 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
424 ctx
->VertexProgram
.Current
);
425 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
428 if (ctx
->Extensions
.ARB_fragment_program
) {
429 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
430 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
431 ctx
->FragmentProgram
.Current
);
432 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
435 if (ctx
->Extensions
.ARB_shader_objects
) {
436 save
->Shader
= ctx
->Shader
.CurrentProgram
?
437 ctx
->Shader
.CurrentProgram
->Name
: 0;
438 _mesa_UseProgramObjectARB(0);
442 if (state
& META_STENCIL_TEST
) {
443 save
->Stencil
= ctx
->Stencil
; /* struct copy */
444 if (ctx
->Stencil
.Enabled
)
445 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
446 /* NOTE: other stencil state not reset */
449 if (state
& META_TEXTURE
) {
452 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
453 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
454 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
456 /* Disable all texture units */
457 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
458 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
459 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
460 if (ctx
->Texture
.Unit
[u
].Enabled
||
461 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
462 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
463 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
464 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
465 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
466 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
467 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
468 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
469 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
470 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
471 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
475 /* save current texture objects for unit[0] only */
476 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
477 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
478 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
481 /* set defaults for unit[0] */
482 _mesa_ActiveTextureARB(GL_TEXTURE0
);
483 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
484 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
487 if (state
& META_TRANSFORM
) {
488 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
489 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
490 16 * sizeof(GLfloat
));
491 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
492 16 * sizeof(GLfloat
));
493 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
494 16 * sizeof(GLfloat
));
495 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
496 /* set 1:1 vertex:pixel coordinate transform */
497 _mesa_ActiveTextureARB(GL_TEXTURE0
);
498 _mesa_MatrixMode(GL_TEXTURE
);
499 _mesa_LoadIdentity();
500 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
501 _mesa_MatrixMode(GL_MODELVIEW
);
502 _mesa_LoadIdentity();
503 _mesa_MatrixMode(GL_PROJECTION
);
504 _mesa_LoadIdentity();
505 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
506 0.0, ctx
->DrawBuffer
->Height
,
508 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
509 if (ctx
->Transform
.ClipPlanesEnabled
) {
511 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
512 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
517 if (state
& META_VERTEX
) {
518 /* save vertex array object state */
519 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
520 ctx
->Array
.ArrayObj
);
521 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
522 ctx
->Array
.ArrayBufferObj
);
523 /* set some default state? */
526 if (state
& META_VIEWPORT
) {
527 /* save viewport state */
528 save
->ViewportX
= ctx
->Viewport
.X
;
529 save
->ViewportY
= ctx
->Viewport
.Y
;
530 save
->ViewportW
= ctx
->Viewport
.Width
;
531 save
->ViewportH
= ctx
->Viewport
.Height
;
532 /* set viewport to match window size */
533 if (ctx
->Viewport
.X
!= 0 ||
534 ctx
->Viewport
.Y
!= 0 ||
535 ctx
->Viewport
.Width
!= ctx
->DrawBuffer
->Width
||
536 ctx
->Viewport
.Height
!= ctx
->DrawBuffer
->Height
) {
537 _mesa_set_viewport(ctx
, 0, 0,
538 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
540 /* save depth range state */
541 save
->DepthNear
= ctx
->Viewport
.Near
;
542 save
->DepthFar
= ctx
->Viewport
.Far
;
543 /* set depth range to default */
544 _mesa_DepthRange(0.0, 1.0);
549 save
->Lighting
= ctx
->Light
.Enabled
;
550 if (ctx
->Light
.Enabled
)
551 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
557 * Leave meta state. This is like a light-weight version of glPopAttrib().
560 _mesa_meta_end(GLcontext
*ctx
)
562 struct save_state
*save
= &ctx
->Meta
->Save
;
563 const GLbitfield state
= save
->SavedState
;
565 if (state
& META_ALPHA_TEST
) {
566 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
567 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
570 if (state
& META_BLEND
) {
571 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
572 if (ctx
->Extensions
.EXT_draw_buffers2
) {
574 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
575 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
579 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
582 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
583 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
586 if (state
& META_COLOR_MASK
) {
588 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
589 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
591 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
592 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
595 _mesa_ColorMaskIndexed(i
,
596 save
->ColorMask
[i
][0],
597 save
->ColorMask
[i
][1],
598 save
->ColorMask
[i
][2],
599 save
->ColorMask
[i
][3]);
605 if (state
& META_DEPTH_TEST
) {
606 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
607 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
608 _mesa_DepthFunc(save
->Depth
.Func
);
609 _mesa_DepthMask(save
->Depth
.Mask
);
612 if (state
& META_FOG
) {
613 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
616 if (state
& META_PIXEL_STORE
) {
617 ctx
->Pack
= save
->Pack
;
618 ctx
->Unpack
= save
->Unpack
;
621 if (state
& META_PIXEL_TRANSFER
) {
622 ctx
->Pixel
.RedScale
= save
->RedScale
;
623 ctx
->Pixel
.RedBias
= save
->RedBias
;
624 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
625 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
626 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
627 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
628 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
629 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
630 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
632 ctx
->NewState
|=_NEW_PIXEL
;
635 if (state
& META_RASTERIZATION
) {
636 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
637 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
638 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
639 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
640 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
641 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
644 if (state
& META_SCISSOR
) {
645 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
646 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
647 save
->Scissor
.Width
, save
->Scissor
.Height
);
650 if (state
& META_SHADER
) {
651 if (ctx
->Extensions
.ARB_vertex_program
) {
652 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
653 save
->VertexProgramEnabled
);
654 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
655 save
->VertexProgram
);
656 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
659 if (ctx
->Extensions
.ARB_fragment_program
) {
660 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
661 save
->FragmentProgramEnabled
);
662 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
663 save
->FragmentProgram
);
664 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
667 if (ctx
->Extensions
.ARB_shader_objects
) {
668 _mesa_UseProgramObjectARB(save
->Shader
);
672 if (state
& META_STENCIL_TEST
) {
673 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
675 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
676 _mesa_ClearStencil(stencil
->Clear
);
677 if (ctx
->Extensions
.EXT_stencil_two_side
) {
678 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
679 stencil
->TestTwoSide
);
680 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
681 ? GL_BACK
: GL_FRONT
);
684 _mesa_StencilFuncSeparate(GL_FRONT
,
685 stencil
->Function
[0],
687 stencil
->ValueMask
[0]);
688 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
689 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
690 stencil
->ZFailFunc
[0],
691 stencil
->ZPassFunc
[0]);
693 _mesa_StencilFuncSeparate(GL_BACK
,
694 stencil
->Function
[1],
696 stencil
->ValueMask
[1]);
697 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
698 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
699 stencil
->ZFailFunc
[1],
700 stencil
->ZPassFunc
[1]);
703 if (state
& META_TEXTURE
) {
706 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
708 /* restore texenv for unit[0] */
709 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
711 /* restore texture objects for unit[0] only */
712 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
713 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
714 save
->CurrentTexture
[tgt
]);
715 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
718 /* Re-enable textures, texgen */
719 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
720 if (save
->TexEnabled
[u
]) {
721 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
723 if (save
->TexEnabled
[u
] & TEXTURE_1D_BIT
)
724 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_TRUE
);
725 if (save
->TexEnabled
[u
] & TEXTURE_2D_BIT
)
726 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_TRUE
);
727 if (save
->TexEnabled
[u
] & TEXTURE_3D_BIT
)
728 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_TRUE
);
729 if (save
->TexEnabled
[u
] & TEXTURE_CUBE_BIT
)
730 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_TRUE
);
731 if (save
->TexEnabled
[u
] & TEXTURE_RECT_BIT
)
732 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_TRUE
);
735 if (save
->TexGenEnabled
[u
]) {
736 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
738 if (save
->TexGenEnabled
[u
] & S_BIT
)
739 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_TRUE
);
740 if (save
->TexGenEnabled
[u
] & T_BIT
)
741 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_TRUE
);
742 if (save
->TexGenEnabled
[u
] & R_BIT
)
743 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_TRUE
);
744 if (save
->TexGenEnabled
[u
] & Q_BIT
)
745 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_TRUE
);
749 /* restore current unit state */
750 _mesa_ActiveTextureARB(GL_TEXTURE0
+ save
->ActiveUnit
);
751 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ save
->ClientActiveUnit
);
754 if (state
& META_TRANSFORM
) {
755 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
756 _mesa_ActiveTextureARB(GL_TEXTURE0
);
757 _mesa_MatrixMode(GL_TEXTURE
);
758 _mesa_LoadMatrixf(save
->TextureMatrix
);
759 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
761 _mesa_MatrixMode(GL_MODELVIEW
);
762 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
764 _mesa_MatrixMode(GL_PROJECTION
);
765 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
767 _mesa_MatrixMode(save
->MatrixMode
);
769 if (save
->ClipPlanesEnabled
) {
771 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
772 if (save
->ClipPlanesEnabled
& (1 << i
)) {
773 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
779 if (state
& META_VERTEX
) {
780 /* restore vertex buffer object */
781 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
782 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
784 /* restore vertex array object */
785 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
786 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
789 if (state
& META_VIEWPORT
) {
790 if (save
->ViewportX
!= ctx
->Viewport
.X
||
791 save
->ViewportY
!= ctx
->Viewport
.Y
||
792 save
->ViewportW
!= ctx
->Viewport
.Width
||
793 save
->ViewportH
!= ctx
->Viewport
.Height
) {
794 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
795 save
->ViewportW
, save
->ViewportH
);
797 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
801 if (save
->Lighting
) {
802 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
808 * Convert Z from a normalized value in the range [0, 1] to an object-space
809 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
810 * default/identity ortho projection results in the original Z value.
811 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
812 * value comes from the clear value or raster position.
814 static INLINE GLfloat
815 invert_z(GLfloat normZ
)
817 GLfloat objZ
= 1.0 - 2.0 * normZ
;
823 * One-time init for a temp_texture object.
824 * Choose tex target, compute max tex size, etc.
827 init_temp_texture(GLcontext
*ctx
, struct temp_texture
*tex
)
829 /* prefer texture rectangle */
830 if (ctx
->Extensions
.NV_texture_rectangle
) {
831 tex
->Target
= GL_TEXTURE_RECTANGLE
;
832 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
836 /* use 2D texture, NPOT if possible */
837 tex
->Target
= GL_TEXTURE_2D
;
838 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
839 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
841 tex
->MinSize
= 16; /* 16 x 16 at least */
842 assert(tex
->MaxSize
> 0);
844 _mesa_GenTextures(1, &tex
->TexObj
);
849 * Return pointer to temp_texture info for non-bitmap ops.
850 * This does some one-time init if needed.
852 static struct temp_texture
*
853 get_temp_texture(GLcontext
*ctx
)
855 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
858 init_temp_texture(ctx
, tex
);
866 * Return pointer to temp_texture info for _mesa_meta_bitmap().
867 * We use a separate texture for bitmaps to reduce texture
868 * allocation/deallocation.
870 static struct temp_texture
*
871 get_bitmap_temp_texture(GLcontext
*ctx
)
873 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
876 init_temp_texture(ctx
, tex
);
884 * Compute the width/height of texture needed to draw an image of the
885 * given size. Return a flag indicating whether the current texture
886 * can be re-used (glTexSubImage2D) or if a new texture needs to be
887 * allocated (glTexImage2D).
888 * Also, compute s/t texcoords for drawing.
890 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
893 alloc_texture(struct temp_texture
*tex
,
894 GLsizei width
, GLsizei height
, GLenum intFormat
)
896 GLboolean newTex
= GL_FALSE
;
898 ASSERT(width
<= tex
->MaxSize
);
899 ASSERT(height
<= tex
->MaxSize
);
901 if (width
> tex
->Width
||
902 height
> tex
->Height
||
903 intFormat
!= tex
->IntFormat
) {
904 /* alloc new texture (larger or different format) */
907 /* use non-power of two size */
908 tex
->Width
= MAX2(tex
->MinSize
, width
);
909 tex
->Height
= MAX2(tex
->MinSize
, height
);
912 /* find power of two size */
914 w
= h
= tex
->MinSize
;
923 tex
->IntFormat
= intFormat
;
928 /* compute texcoords */
929 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
930 tex
->Sright
= (GLfloat
) width
;
931 tex
->Ttop
= (GLfloat
) height
;
934 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
935 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
943 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
946 setup_copypix_texture(struct temp_texture
*tex
,
948 GLint srcX
, GLint srcY
,
949 GLsizei width
, GLsizei height
, GLenum intFormat
,
952 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
953 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
954 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
955 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
957 /* copy framebuffer image to texture */
959 /* create new tex image */
960 if (tex
->Width
== width
&& tex
->Height
== height
) {
961 /* create new tex with framebuffer data */
962 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
963 srcX
, srcY
, width
, height
, 0);
966 /* create empty texture */
967 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
968 tex
->Width
, tex
->Height
, 0,
969 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
971 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
972 0, 0, srcX
, srcY
, width
, height
);
976 /* replace existing tex image */
977 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
978 0, 0, srcX
, srcY
, width
, height
);
984 * Setup/load texture for glDrawPixels.
987 setup_drawpix_texture(GLcontext
*ctx
,
988 struct temp_texture
*tex
,
991 GLsizei width
, GLsizei height
,
992 GLenum format
, GLenum type
,
993 const GLvoid
*pixels
)
995 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
996 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
997 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
998 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1000 /* copy pixel data to texture */
1002 /* create new tex image */
1003 if (tex
->Width
== width
&& tex
->Height
== height
) {
1004 /* create new tex and load image data */
1005 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1006 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1009 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1011 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1012 ctx
->Unpack
.BufferObj
);
1013 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1014 /* create empty texture */
1015 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1016 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1017 if (save_unpack_obj
!= NULL
)
1018 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
,
1019 save_unpack_obj
->Name
);
1021 _mesa_TexSubImage2D(tex
->Target
, 0,
1022 0, 0, width
, height
, format
, type
, pixels
);
1026 /* replace existing tex image */
1027 _mesa_TexSubImage2D(tex
->Target
, 0,
1028 0, 0, width
, height
, format
, type
, pixels
);
1035 * One-time init for drawing depth pixels.
1038 init_blit_depth_pixels(GLcontext
*ctx
)
1040 static const char *program
=
1042 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1045 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1046 struct temp_texture
*tex
= get_temp_texture(ctx
);
1047 const char *texTarget
;
1049 assert(blit
->DepthFP
== 0);
1051 /* replace %s with "RECT" or "2D" */
1052 assert(strlen(program
) + 4 < sizeof(program2
));
1053 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1057 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1059 _mesa_GenPrograms(1, &blit
->DepthFP
);
1060 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1061 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1062 strlen(program2
), (const GLubyte
*) program2
);
1067 * Try to do a glBlitFramebuffer using no-copy texturing.
1068 * We can do this when the src renderbuffer is actually a texture.
1069 * But if the src buffer == dst buffer we cannot do this.
1071 * \return new buffer mask indicating the buffers left to blit using the
1075 blitframebuffer_texture(GLcontext
*ctx
,
1076 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1077 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1078 GLbitfield mask
, GLenum filter
)
1080 if (mask
& GL_COLOR_BUFFER_BIT
) {
1081 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1082 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1083 const struct gl_renderbuffer_attachment
*drawAtt
=
1084 &drawFb
->Attachment
[drawFb
->_ColorDrawBufferIndexes
[0]];
1085 const struct gl_renderbuffer_attachment
*readAtt
=
1086 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1088 if (readAtt
&& readAtt
->Texture
) {
1089 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1090 const GLuint srcLevel
= readAtt
->TextureLevel
;
1091 const GLenum minFilterSave
= texObj
->MinFilter
;
1092 const GLenum magFilterSave
= texObj
->MagFilter
;
1093 const GLint baseLevelSave
= texObj
->BaseLevel
;
1094 const GLint maxLevelSave
= texObj
->MaxLevel
;
1095 const GLenum wrapSSave
= texObj
->WrapS
;
1096 const GLenum wrapTSave
= texObj
->WrapT
;
1097 const GLenum target
= texObj
->Target
;
1099 if (drawAtt
->Texture
== readAtt
->Texture
) {
1100 /* Can't use same texture as both the source and dest. We need
1101 * to handle overlapping blits and besides, some hw may not
1107 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1108 /* Can't handle other texture types at this time */
1113 printf("Blit from texture!\n");
1114 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1115 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1118 /* Prepare src texture state */
1119 _mesa_BindTexture(target
, texObj
->Name
);
1120 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
1121 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
1122 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1123 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1124 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1126 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1127 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1128 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1129 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1131 /* Prepare vertex data (the VBO was previously created and bound) */
1136 struct vertex verts
[4];
1137 GLfloat s0
, t0
, s1
, t1
;
1139 if (target
== GL_TEXTURE_2D
) {
1140 const struct gl_texture_image
*texImage
1141 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1142 s0
= srcX0
/ (float) texImage
->Width
;
1143 s1
= srcX1
/ (float) texImage
->Width
;
1144 t0
= srcY0
/ (float) texImage
->Height
;
1145 t1
= srcY1
/ (float) texImage
->Height
;
1148 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1155 verts
[0].x
= (GLfloat
) dstX0
;
1156 verts
[0].y
= (GLfloat
) dstY0
;
1157 verts
[1].x
= (GLfloat
) dstX1
;
1158 verts
[1].y
= (GLfloat
) dstY0
;
1159 verts
[2].x
= (GLfloat
) dstX1
;
1160 verts
[2].y
= (GLfloat
) dstY1
;
1161 verts
[3].x
= (GLfloat
) dstX0
;
1162 verts
[3].y
= (GLfloat
) dstY1
;
1173 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1176 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1178 /* Restore texture object state, the texture binding will
1179 * be restored by _mesa_meta_end().
1181 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
1182 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
1183 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1184 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1185 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1187 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
1188 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
1190 /* Done with color buffer */
1191 mask
&= ~GL_COLOR_BUFFER_BIT
;
1200 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1201 * of texture mapping and polygon rendering.
1204 _mesa_meta_BlitFramebuffer(GLcontext
*ctx
,
1205 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1206 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1207 GLbitfield mask
, GLenum filter
)
1209 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1210 struct temp_texture
*tex
= get_temp_texture(ctx
);
1211 const GLsizei maxTexSize
= tex
->MaxSize
;
1212 const GLint srcX
= MIN2(srcX0
, srcX1
);
1213 const GLint srcY
= MIN2(srcY0
, srcY1
);
1214 const GLint srcW
= abs(srcX1
- srcX0
);
1215 const GLint srcH
= abs(srcY1
- srcY0
);
1216 const GLboolean srcFlipX
= srcX1
< srcX0
;
1217 const GLboolean srcFlipY
= srcY1
< srcY0
;
1221 struct vertex verts
[4];
1224 if (srcW
> maxTexSize
|| srcH
> maxTexSize
) {
1225 /* XXX avoid this fallback */
1226 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1227 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1243 /* only scissor effects blit so save/clear all other relevant state */
1244 _mesa_meta_begin(ctx
, ~META_SCISSOR
);
1246 if (blit
->ArrayObj
== 0) {
1247 /* one-time setup */
1249 /* create vertex array object */
1250 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1251 _mesa_BindVertexArray(blit
->ArrayObj
);
1253 /* create vertex array buffer */
1254 _mesa_GenBuffersARB(1, &blit
->VBO
);
1255 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1256 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1257 NULL
, GL_DYNAMIC_DRAW_ARB
);
1259 /* setup vertex arrays */
1260 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1261 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1262 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1263 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1266 _mesa_BindVertexArray(blit
->ArrayObj
);
1267 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1270 /* Try faster, direct texture approach first */
1271 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1272 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1274 _mesa_meta_end(ctx
);
1278 /* Continue with "normal" approach which involves copying the src rect
1279 * into a temporary texture and is "blitted" by drawing a textured quad.
1282 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1284 /* vertex positions/texcoords (after texture allocation!) */
1286 verts
[0].x
= (GLfloat
) dstX0
;
1287 verts
[0].y
= (GLfloat
) dstY0
;
1288 verts
[1].x
= (GLfloat
) dstX1
;
1289 verts
[1].y
= (GLfloat
) dstY0
;
1290 verts
[2].x
= (GLfloat
) dstX1
;
1291 verts
[2].y
= (GLfloat
) dstY1
;
1292 verts
[3].x
= (GLfloat
) dstX0
;
1293 verts
[3].y
= (GLfloat
) dstY1
;
1297 verts
[1].s
= tex
->Sright
;
1299 verts
[2].s
= tex
->Sright
;
1300 verts
[2].t
= tex
->Ttop
;
1302 verts
[3].t
= tex
->Ttop
;
1304 /* upload new vertex data */
1305 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1308 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1310 if (mask
& GL_COLOR_BUFFER_BIT
) {
1311 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1313 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1314 mask
&= ~GL_COLOR_BUFFER_BIT
;
1317 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1318 GLuint
*tmp
= (GLuint
*) malloc(srcW
* srcH
* sizeof(GLuint
));
1321 init_blit_depth_pixels(ctx
);
1323 /* maybe change tex format here */
1324 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1326 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1327 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1329 setup_drawpix_texture(ctx
, tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1330 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1332 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1333 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1334 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1335 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1336 _mesa_DepthFunc(GL_ALWAYS
);
1337 _mesa_DepthMask(GL_TRUE
);
1339 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1340 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1346 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1347 /* XXX can't easily do stencil */
1350 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1352 _mesa_meta_end(ctx
);
1355 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1356 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1362 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1365 _mesa_meta_Clear(GLcontext
*ctx
, GLbitfield buffers
)
1367 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1369 GLfloat x
, y
, z
, r
, g
, b
, a
;
1371 struct vertex verts
[4];
1372 /* save all state but scissor, pixel pack/unpack */
1373 GLbitfield metaSave
= META_ALL
- META_SCISSOR
- META_PIXEL_STORE
;
1375 if (buffers
& BUFFER_BITS_COLOR
) {
1376 /* if clearing color buffers, don't save/restore colormask */
1377 metaSave
-= META_COLOR_MASK
;
1380 _mesa_meta_begin(ctx
, metaSave
);
1382 if (clear
->ArrayObj
== 0) {
1383 /* one-time setup */
1385 /* create vertex array object */
1386 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1387 _mesa_BindVertexArray(clear
->ArrayObj
);
1389 /* create vertex array buffer */
1390 _mesa_GenBuffersARB(1, &clear
->VBO
);
1391 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1393 /* setup vertex arrays */
1394 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1395 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1396 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1397 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1400 _mesa_BindVertexArray(clear
->ArrayObj
);
1401 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1404 /* GL_COLOR_BUFFER_BIT */
1405 if (buffers
& BUFFER_BITS_COLOR
) {
1406 /* leave colormask, glDrawBuffer state as-is */
1409 ASSERT(metaSave
& META_COLOR_MASK
);
1410 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1413 /* GL_DEPTH_BUFFER_BIT */
1414 if (buffers
& BUFFER_BIT_DEPTH
) {
1415 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1416 _mesa_DepthFunc(GL_ALWAYS
);
1417 _mesa_DepthMask(GL_TRUE
);
1420 assert(!ctx
->Depth
.Test
);
1423 /* GL_STENCIL_BUFFER_BIT */
1424 if (buffers
& BUFFER_BIT_STENCIL
) {
1425 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1426 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1427 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1428 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1429 ctx
->Stencil
.Clear
& 0x7fffffff,
1430 ctx
->Stencil
.WriteMask
[0]);
1433 assert(!ctx
->Stencil
.Enabled
);
1436 /* vertex positions/colors */
1438 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1439 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1440 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1441 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1442 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1459 for (i
= 0; i
< 4; i
++) {
1460 verts
[i
].r
= ctx
->Color
.ClearColor
[0];
1461 verts
[i
].g
= ctx
->Color
.ClearColor
[1];
1462 verts
[i
].b
= ctx
->Color
.ClearColor
[2];
1463 verts
[i
].a
= ctx
->Color
.ClearColor
[3];
1466 /* upload new vertex data */
1467 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1468 GL_DYNAMIC_DRAW_ARB
);
1472 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1474 _mesa_meta_end(ctx
);
1479 * Meta implementation of ctx->Driver.CopyPixels() in terms
1480 * of texture mapping and polygon rendering.
1483 _mesa_meta_CopyPixels(GLcontext
*ctx
, GLint srcX
, GLint srcY
,
1484 GLsizei width
, GLsizei height
,
1485 GLint dstX
, GLint dstY
, GLenum type
)
1487 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1488 struct temp_texture
*tex
= get_temp_texture(ctx
);
1490 GLfloat x
, y
, z
, s
, t
;
1492 struct vertex verts
[4];
1494 GLenum intFormat
= GL_RGBA
;
1496 if (type
!= GL_COLOR
||
1497 ctx
->_ImageTransferState
||
1499 width
> tex
->MaxSize
||
1500 height
> tex
->MaxSize
) {
1501 /* XXX avoid this fallback */
1502 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1506 /* Most GL state applies to glCopyPixels, but a there's a few things
1507 * we need to override:
1509 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1516 if (copypix
->ArrayObj
== 0) {
1517 /* one-time setup */
1519 /* create vertex array object */
1520 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
1521 _mesa_BindVertexArray(copypix
->ArrayObj
);
1523 /* create vertex array buffer */
1524 _mesa_GenBuffersARB(1, ©pix
->VBO
);
1525 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1526 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1527 NULL
, GL_DYNAMIC_DRAW_ARB
);
1529 /* setup vertex arrays */
1530 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1531 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1532 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1533 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1536 _mesa_BindVertexArray(copypix
->ArrayObj
);
1537 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1540 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
1542 /* vertex positions, texcoords (after texture allocation!) */
1544 const GLfloat dstX0
= (GLfloat
) dstX
;
1545 const GLfloat dstY0
= (GLfloat
) dstY
;
1546 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1547 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1548 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1558 verts
[1].s
= tex
->Sright
;
1563 verts
[2].s
= tex
->Sright
;
1564 verts
[2].t
= tex
->Ttop
;
1569 verts
[3].t
= tex
->Ttop
;
1571 /* upload new vertex data */
1572 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1575 /* Alloc/setup texture */
1576 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
1577 GL_RGBA
, GL_NEAREST
);
1579 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1581 /* draw textured quad */
1582 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1584 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1586 _mesa_meta_end(ctx
);
1592 * When the glDrawPixels() image size is greater than the max rectangle
1593 * texture size we use this function to break the glDrawPixels() image
1594 * into tiles which fit into the max texture size.
1597 tiled_draw_pixels(GLcontext
*ctx
,
1599 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1600 GLenum format
, GLenum type
,
1601 const struct gl_pixelstore_attrib
*unpack
,
1602 const GLvoid
*pixels
)
1604 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
1607 if (tileUnpack
.RowLength
== 0)
1608 tileUnpack
.RowLength
= width
;
1610 for (i
= 0; i
< width
; i
+= tileSize
) {
1611 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
1612 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
1614 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
1616 for (j
= 0; j
< height
; j
+= tileSize
) {
1617 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
1618 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
1620 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
1622 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
1623 format
, type
, &tileUnpack
, pixels
);
1630 * One-time init for drawing stencil pixels.
1633 init_draw_stencil_pixels(GLcontext
*ctx
)
1635 /* This program is run eight times, once for each stencil bit.
1636 * The stencil values to draw are found in an 8-bit alpha texture.
1637 * We read the texture/stencil value and test if bit 'b' is set.
1638 * If the bit is not set, use KIL to kill the fragment.
1639 * Finally, we use the stencil test to update the stencil buffer.
1641 * The basic algorithm for checking if a bit is set is:
1642 * if (is_odd(value / (1 << bit)))
1643 * result is one (or non-zero).
1646 * The program parameter contains three values:
1647 * parm.x = 255 / (1 << bit)
1651 static const char *program
=
1653 "PARAM parm = program.local[0]; \n"
1655 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1656 "# t = t * 255 / bit \n"
1657 "MUL t.x, t.a, parm.x; \n"
1660 "SUB t.x, t.x, t.y; \n"
1662 "MUL t.x, t.x, parm.y; \n"
1663 "# t = fract(t.x) \n"
1664 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
1665 "# t.x = (t.x == 0 ? 1 : 0) \n"
1666 "SGE t.x, -t.x, parm.z; \n"
1668 "# for debug only \n"
1669 "#MOV result.color, t.x; \n"
1671 char program2
[1000];
1672 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1673 struct temp_texture
*tex
= get_temp_texture(ctx
);
1674 const char *texTarget
;
1676 assert(drawpix
->StencilFP
== 0);
1678 /* replace %s with "RECT" or "2D" */
1679 assert(strlen(program
) + 4 < sizeof(program2
));
1680 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1684 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1686 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
1687 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1688 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1689 strlen(program2
), (const GLubyte
*) program2
);
1694 * One-time init for drawing depth pixels.
1697 init_draw_depth_pixels(GLcontext
*ctx
)
1699 static const char *program
=
1701 "PARAM color = program.local[0]; \n"
1702 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1703 "MOV result.color, color; \n"
1706 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1707 struct temp_texture
*tex
= get_temp_texture(ctx
);
1708 const char *texTarget
;
1710 assert(drawpix
->DepthFP
== 0);
1712 /* replace %s with "RECT" or "2D" */
1713 assert(strlen(program
) + 4 < sizeof(program2
));
1714 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1718 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1720 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
1721 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1722 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1723 strlen(program2
), (const GLubyte
*) program2
);
1728 * Meta implementation of ctx->Driver.DrawPixels() in terms
1729 * of texture mapping and polygon rendering.
1732 _mesa_meta_DrawPixels(GLcontext
*ctx
,
1733 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1734 GLenum format
, GLenum type
,
1735 const struct gl_pixelstore_attrib
*unpack
,
1736 const GLvoid
*pixels
)
1738 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1739 struct temp_texture
*tex
= get_temp_texture(ctx
);
1740 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
1741 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
1743 GLfloat x
, y
, z
, s
, t
;
1745 struct vertex verts
[4];
1746 GLenum texIntFormat
;
1747 GLboolean fallback
, newTex
;
1748 GLbitfield metaExtraSave
= 0x0;
1752 * Determine if we can do the glDrawPixels with texture mapping.
1754 fallback
= GL_FALSE
;
1755 if (ctx
->_ImageTransferState
||
1760 if (_mesa_is_color_format(format
)) {
1761 /* use more compact format when possible */
1762 /* XXX disable special case for GL_LUMINANCE for now to work around
1763 * apparent i965 driver bug (see bug #23670).
1765 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
1766 texIntFormat
= format
;
1768 texIntFormat
= GL_RGBA
;
1770 else if (_mesa_is_stencil_format(format
)) {
1771 if (ctx
->Extensions
.ARB_fragment_program
&&
1772 ctx
->Pixel
.IndexShift
== 0 &&
1773 ctx
->Pixel
.IndexOffset
== 0 &&
1774 type
== GL_UNSIGNED_BYTE
) {
1775 /* We'll store stencil as alpha. This only works for GLubyte
1776 * image data because of how incoming values are mapped to alpha
1779 texIntFormat
= GL_ALPHA
;
1780 metaExtraSave
= (META_COLOR_MASK
|
1789 else if (_mesa_is_depth_format(format
)) {
1790 if (ctx
->Extensions
.ARB_depth_texture
&&
1791 ctx
->Extensions
.ARB_fragment_program
) {
1792 texIntFormat
= GL_DEPTH_COMPONENT
;
1793 metaExtraSave
= (META_SHADER
);
1804 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
1805 format
, type
, unpack
, pixels
);
1810 * Check image size against max texture size, draw as tiles if needed.
1812 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
1813 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
1814 format
, type
, unpack
, pixels
);
1818 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
1819 * but a there's a few things we need to override:
1821 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1829 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
1831 /* vertex positions, texcoords (after texture allocation!) */
1833 const GLfloat x0
= (GLfloat
) x
;
1834 const GLfloat y0
= (GLfloat
) y
;
1835 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
1836 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
1837 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1847 verts
[1].s
= tex
->Sright
;
1852 verts
[2].s
= tex
->Sright
;
1853 verts
[2].t
= tex
->Ttop
;
1858 verts
[3].t
= tex
->Ttop
;
1861 if (drawpix
->ArrayObj
== 0) {
1862 /* one-time setup: create vertex array object */
1863 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
1865 _mesa_BindVertexArray(drawpix
->ArrayObj
);
1867 /* create vertex array buffer */
1868 _mesa_GenBuffersARB(1, &vbo
);
1869 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
1870 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1871 verts
, GL_DYNAMIC_DRAW_ARB
);
1873 /* setup vertex arrays */
1874 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1875 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1876 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1877 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1879 /* set given unpack params */
1880 ctx
->Unpack
= *unpack
;
1882 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1884 if (_mesa_is_stencil_format(format
)) {
1885 /* Drawing stencil */
1888 if (!drawpix
->StencilFP
)
1889 init_draw_stencil_pixels(ctx
);
1891 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1892 GL_ALPHA
, type
, pixels
);
1894 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1896 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1898 /* set all stencil bits to 0 */
1899 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1900 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
1901 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1903 /* set stencil bits to 1 where needed */
1904 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
1906 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1907 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1909 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
1910 const GLuint mask
= 1 << bit
;
1911 if (mask
& origStencilMask
) {
1912 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
1913 _mesa_StencilMask(mask
);
1915 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
1916 255.0 / mask
, 0.5, 0.0, 0.0);
1918 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1922 else if (_mesa_is_depth_format(format
)) {
1924 if (!drawpix
->DepthFP
)
1925 init_draw_depth_pixels(ctx
);
1927 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1928 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1930 /* polygon color = current raster color */
1931 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
1932 ctx
->Current
.RasterColor
);
1934 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1935 format
, type
, pixels
);
1937 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1941 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1942 format
, type
, pixels
);
1943 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1946 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1948 _mesa_DeleteBuffersARB(1, &vbo
);
1950 /* restore unpack params */
1951 ctx
->Unpack
= unpackSave
;
1953 _mesa_meta_end(ctx
);
1958 * Do glBitmap with a alpha texture quad. Use the alpha test to
1959 * cull the 'off' bits. If alpha test is already enabled, fall back
1960 * to swrast (should be a rare case).
1961 * A bitmap cache as in the gallium/mesa state tracker would
1962 * improve performance a lot.
1965 _mesa_meta_Bitmap(GLcontext
*ctx
,
1966 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1967 const struct gl_pixelstore_attrib
*unpack
,
1968 const GLubyte
*bitmap1
)
1970 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
1971 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
1972 const GLenum texIntFormat
= GL_ALPHA
;
1973 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
1975 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
1977 struct vertex verts
[4];
1982 * Check if swrast fallback is needed.
1984 if (ctx
->_ImageTransferState
||
1985 ctx
->Color
.AlphaEnabled
||
1987 ctx
->Texture
._EnabledUnits
||
1988 width
> tex
->MaxSize
||
1989 height
> tex
->MaxSize
) {
1990 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
1994 /* Most GL state applies to glBitmap (like blending, stencil, etc),
1995 * but a there's a few things we need to override:
1997 _mesa_meta_begin(ctx
, (META_ALPHA_TEST
|
1999 META_RASTERIZATION
|
2006 if (bitmap
->ArrayObj
== 0) {
2007 /* one-time setup */
2009 /* create vertex array object */
2010 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2011 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2013 /* create vertex array buffer */
2014 _mesa_GenBuffersARB(1, &bitmap
->VBO
);
2015 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2016 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2017 NULL
, GL_DYNAMIC_DRAW_ARB
);
2019 /* setup vertex arrays */
2020 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2021 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2022 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2023 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2024 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2025 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2028 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2029 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2032 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2034 /* vertex positions, texcoords, colors (after texture allocation!) */
2036 const GLfloat x0
= (GLfloat
) x
;
2037 const GLfloat y0
= (GLfloat
) y
;
2038 const GLfloat x1
= (GLfloat
) (x
+ width
);
2039 const GLfloat y1
= (GLfloat
) (y
+ height
);
2040 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2051 verts
[1].s
= tex
->Sright
;
2056 verts
[2].s
= tex
->Sright
;
2057 verts
[2].t
= tex
->Ttop
;
2062 verts
[3].t
= tex
->Ttop
;
2064 for (i
= 0; i
< 4; i
++) {
2065 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2066 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2067 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2068 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2071 /* upload new vertex data */
2072 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2075 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2077 _mesa_meta_end(ctx
);
2081 bitmap8
= (GLubyte
*) calloc(1, width
* height
);
2083 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2084 bitmap8
, width
, 0xff);
2086 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2088 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2089 _mesa_AlphaFunc(GL_GREATER
, 0.0);
2091 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2092 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2094 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2096 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2101 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2103 _mesa_meta_end(ctx
);
2108 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2109 * software fallback. The fallback path will require that the texture
2110 * images are mapped.
2111 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2114 _mesa_meta_check_generate_mipmap_fallback(GLcontext
*ctx
, GLenum target
,
2115 struct gl_texture_object
*texObj
)
2117 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2118 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2119 struct gl_texture_image
*baseImage
;
2123 /* check for fallbacks */
2124 if (!ctx
->Extensions
.EXT_framebuffer_object
||
2125 target
== GL_TEXTURE_3D
) {
2129 srcLevel
= texObj
->BaseLevel
;
2130 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2131 if (!baseImage
|| _mesa_is_format_compressed(baseImage
->TexFormat
)) {
2136 * Test that we can actually render in the texture's format.
2139 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2140 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2142 if (target
== GL_TEXTURE_1D
) {
2143 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2144 GL_COLOR_ATTACHMENT0_EXT
,
2145 target
, texObj
->Name
, srcLevel
);
2148 /* other work is needed to enable 3D mipmap generation */
2149 else if (target
== GL_TEXTURE_3D
) {
2151 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2152 GL_COLOR_ATTACHMENT0_EXT
,
2153 target
, texObj
->Name
, srcLevel
, zoffset
);
2158 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2159 GL_COLOR_ATTACHMENT0_EXT
,
2160 target
, texObj
->Name
, srcLevel
);
2163 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2165 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2167 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2176 * Called via ctx->Driver.GenerateMipmap()
2177 * Note: texture borders and 3D texture support not yet complete.
2180 _mesa_meta_GenerateMipmap(GLcontext
*ctx
, GLenum target
,
2181 struct gl_texture_object
*texObj
)
2183 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2185 GLfloat x
, y
, s
, t
, r
;
2187 struct vertex verts
[4];
2188 const GLuint baseLevel
= texObj
->BaseLevel
;
2189 const GLuint maxLevel
= texObj
->MaxLevel
;
2190 const GLenum minFilterSave
= texObj
->MinFilter
;
2191 const GLenum magFilterSave
= texObj
->MagFilter
;
2192 const GLint baseLevelSave
= texObj
->BaseLevel
;
2193 const GLint maxLevelSave
= texObj
->MaxLevel
;
2194 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
2195 const GLenum wrapSSave
= texObj
->WrapS
;
2196 const GLenum wrapTSave
= texObj
->WrapT
;
2197 const GLenum wrapRSave
= texObj
->WrapR
;
2198 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2199 const GLuint original_active_unit
= ctx
->Texture
.CurrentUnit
;
2204 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
2205 _mesa_generate_mipmap(ctx
, target
, texObj
);
2209 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
2210 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
2211 faceTarget
= target
;
2212 target
= GL_TEXTURE_CUBE_MAP
;
2215 faceTarget
= target
;
2218 _mesa_meta_begin(ctx
, META_ALL
);
2220 if (original_active_unit
!= 0)
2221 _mesa_BindTexture(target
, texObj
->Name
);
2223 if (mipmap
->ArrayObj
== 0) {
2224 /* one-time setup */
2226 /* create vertex array object */
2227 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
2228 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
2230 /* create vertex array buffer */
2231 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
2232 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2233 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2234 NULL
, GL_DYNAMIC_DRAW_ARB
);
2236 /* setup vertex arrays */
2237 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2238 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2239 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2240 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2243 _mesa_BindVertexArray(mipmap
->ArrayObj
);
2244 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2248 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2250 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2252 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
2253 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
2254 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
2255 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2256 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2257 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
2259 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2261 /* setup texcoords once (XXX what about border?) */
2262 switch (faceTarget
) {
2284 static const GLfloat st
[4][2] = {
2285 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2289 /* loop over quad verts */
2290 for (i
= 0; i
< 4; i
++) {
2291 /* Compute sc = +/-scale and tc = +/-scale.
2292 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2293 * though that can still sometimes happen with this scale factor...
2295 const GLfloat scale
= 0.9999f
;
2296 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2297 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2299 switch (faceTarget
) {
2300 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2305 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2310 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2315 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2320 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2325 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2337 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2339 /* setup vertex positions */
2350 /* upload new vertex data */
2351 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2354 /* setup projection matrix */
2355 _mesa_MatrixMode(GL_PROJECTION
);
2356 _mesa_LoadIdentity();
2357 _mesa_Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
2359 /* texture is already locked, unlock now */
2360 _mesa_unlock_texture(ctx
, texObj
);
2362 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
2363 const struct gl_texture_image
*srcImage
;
2364 const GLuint srcLevel
= dstLevel
- 1;
2365 GLsizei srcWidth
, srcHeight
, srcDepth
;
2366 GLsizei dstWidth
, dstHeight
, dstDepth
;
2369 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
2370 assert(srcImage
->Border
== 0); /* XXX we can fix this */
2372 /* src size w/out border */
2373 srcWidth
= srcImage
->Width
- 2 * border
;
2374 srcHeight
= srcImage
->Height
- 2 * border
;
2375 srcDepth
= srcImage
->Depth
- 2 * border
;
2377 /* new dst size w/ border */
2378 dstWidth
= MAX2(1, srcWidth
/ 2) + 2 * border
;
2379 dstHeight
= MAX2(1, srcHeight
/ 2) + 2 * border
;
2380 dstDepth
= MAX2(1, srcDepth
/ 2) + 2 * border
;
2382 if (dstWidth
== srcImage
->Width
&&
2383 dstHeight
== srcImage
->Height
&&
2384 dstDepth
== srcImage
->Depth
) {
2389 /* Set MaxLevel large enough to hold the new level when we allocate it */
2390 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
2392 /* Create empty dest image */
2393 if (target
== GL_TEXTURE_1D
) {
2394 _mesa_TexImage1D(target
, dstLevel
, srcImage
->InternalFormat
,
2396 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2398 else if (target
== GL_TEXTURE_3D
) {
2399 _mesa_TexImage3D(target
, dstLevel
, srcImage
->InternalFormat
,
2400 dstWidth
, dstHeight
, dstDepth
, border
,
2401 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2405 _mesa_TexImage2D(faceTarget
, dstLevel
, srcImage
->InternalFormat
,
2406 dstWidth
, dstHeight
, border
,
2407 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2409 if (target
== GL_TEXTURE_CUBE_MAP
) {
2410 /* If texturing from a cube, we need to make sure all src faces
2411 * have been defined (even if we're not sampling from them.)
2412 * Otherwise the texture object will be 'incomplete' and
2413 * texturing from it will not be allowed.
2416 for (face
= 0; face
< 6; face
++) {
2417 if (!texObj
->Image
[face
][srcLevel
] ||
2418 texObj
->Image
[face
][srcLevel
]->Width
!= srcWidth
) {
2419 _mesa_TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ face
,
2420 srcLevel
, srcImage
->InternalFormat
,
2421 srcWidth
, srcHeight
, border
,
2422 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2428 /* limit sampling to src level */
2429 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
2430 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
2432 /* Set to draw into the current dstLevel */
2433 if (target
== GL_TEXTURE_1D
) {
2434 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2435 GL_COLOR_ATTACHMENT0_EXT
,
2440 else if (target
== GL_TEXTURE_3D
) {
2441 GLint zoffset
= 0; /* XXX unfinished */
2442 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2443 GL_COLOR_ATTACHMENT0_EXT
,
2450 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2451 GL_COLOR_ATTACHMENT0_EXT
,
2457 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
2460 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2461 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2466 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
2467 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
2469 /* setup viewport */
2470 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
2472 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2475 _mesa_lock_texture(ctx
, texObj
); /* relock */
2477 _mesa_meta_end(ctx
);
2479 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
2480 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
2481 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
2482 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
2483 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
2484 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
2485 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
2486 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, wrapRSave
);
2488 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2493 * Determine the GL data type to use for the temporary image read with
2494 * ReadPixels() and passed to Tex[Sub]Image().
2497 get_temp_image_type(GLcontext
*ctx
, GLenum baseFormat
)
2499 switch (baseFormat
) {
2504 case GL_LUMINANCE_ALPHA
:
2506 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2507 return GL_UNSIGNED_BYTE
;
2508 else if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2509 return GL_UNSIGNED_SHORT
;
2512 case GL_DEPTH_COMPONENT
:
2513 return GL_UNSIGNED_INT
;
2514 case GL_DEPTH_STENCIL
:
2515 return GL_UNSIGNED_INT_24_8
;
2517 _mesa_problem(ctx
, "Unexpected format in get_temp_image_type()");
2524 * Helper for _mesa_meta_CopyTexImage1/2D() functions.
2525 * Have to be careful with locking and meta state for pixel transfer.
2528 copy_tex_image(GLcontext
*ctx
, GLuint dims
, GLenum target
, GLint level
,
2529 GLenum internalFormat
, GLint x
, GLint y
,
2530 GLsizei width
, GLsizei height
, GLint border
)
2532 struct gl_texture_object
*texObj
;
2533 struct gl_texture_image
*texImage
;
2534 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2535 GLenum format
, type
;
2539 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2540 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2542 format
= _mesa_base_tex_format(ctx
, internalFormat
);
2543 type
= get_temp_image_type(ctx
, format
);
2544 bpp
= _mesa_bytes_per_pixel(format
, type
);
2546 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_image()");
2551 * Alloc image buffer (XXX could use a PBO)
2553 buf
= malloc(width
* height
* bpp
);
2555 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
2559 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2562 * Read image from framebuffer (disable pixel transfer ops)
2564 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2565 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2566 format
, type
, &ctx
->Pack
, buf
);
2567 _mesa_meta_end(ctx
);
2569 if (texImage
->Data
) {
2570 ctx
->Driver
.FreeTexImageData(ctx
, texImage
);
2573 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2574 postConvWidth
, postConvHeight
, 1,
2575 border
, internalFormat
);
2577 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2578 internalFormat
, GL_NONE
, GL_NONE
);
2581 * Store texture data (with pixel transfer ops)
2583 _mesa_meta_begin(ctx
, META_PIXEL_STORE
);
2585 _mesa_update_state(ctx
); /* to update pixel transfer state */
2587 if (target
== GL_TEXTURE_1D
) {
2588 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
2589 width
, border
, format
, type
,
2590 buf
, &ctx
->Unpack
, texObj
, texImage
);
2593 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
2594 width
, height
, border
, format
, type
,
2595 buf
, &ctx
->Unpack
, texObj
, texImage
);
2597 _mesa_meta_end(ctx
);
2599 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
2606 _mesa_meta_CopyTexImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
2607 GLenum internalFormat
, GLint x
, GLint y
,
2608 GLsizei width
, GLint border
)
2610 copy_tex_image(ctx
, 1, target
, level
, internalFormat
, x
, y
,
2616 _mesa_meta_CopyTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
2617 GLenum internalFormat
, GLint x
, GLint y
,
2618 GLsizei width
, GLsizei height
, GLint border
)
2620 copy_tex_image(ctx
, 2, target
, level
, internalFormat
, x
, y
,
2621 width
, height
, border
);
2627 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2628 * Have to be careful with locking and meta state for pixel transfer.
2631 copy_tex_sub_image(GLcontext
*ctx
, GLuint dims
, GLenum target
, GLint level
,
2632 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2634 GLsizei width
, GLsizei height
)
2636 struct gl_texture_object
*texObj
;
2637 struct gl_texture_image
*texImage
;
2638 GLenum format
, type
;
2642 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2643 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2645 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
2646 type
= get_temp_image_type(ctx
, format
);
2647 bpp
= _mesa_bytes_per_pixel(format
, type
);
2649 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_sub_image()");
2654 * Alloc image buffer (XXX could use a PBO)
2656 buf
= malloc(width
* height
* bpp
);
2658 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
2662 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2665 * Read image from framebuffer (disable pixel transfer ops)
2667 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2668 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2669 format
, type
, &ctx
->Pack
, buf
);
2670 _mesa_meta_end(ctx
);
2672 _mesa_update_state(ctx
); /* to update pixel transfer state */
2675 * Store texture data (with pixel transfer ops)
2677 _mesa_meta_begin(ctx
, META_PIXEL_STORE
);
2678 if (target
== GL_TEXTURE_1D
) {
2679 ctx
->Driver
.TexSubImage1D(ctx
, target
, level
, xoffset
,
2680 width
, format
, type
, buf
,
2681 &ctx
->Unpack
, texObj
, texImage
);
2683 else if (target
== GL_TEXTURE_3D
) {
2684 ctx
->Driver
.TexSubImage3D(ctx
, target
, level
, xoffset
, yoffset
, zoffset
,
2685 width
, height
, 1, format
, type
, buf
,
2686 &ctx
->Unpack
, texObj
, texImage
);
2689 ctx
->Driver
.TexSubImage2D(ctx
, target
, level
, xoffset
, yoffset
,
2690 width
, height
, format
, type
, buf
,
2691 &ctx
->Unpack
, texObj
, texImage
);
2693 _mesa_meta_end(ctx
);
2695 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
2702 _mesa_meta_CopyTexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
2704 GLint x
, GLint y
, GLsizei width
)
2706 copy_tex_sub_image(ctx
, 1, target
, level
, xoffset
, 0, 0,
2712 _mesa_meta_CopyTexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
2713 GLint xoffset
, GLint yoffset
,
2715 GLsizei width
, GLsizei height
)
2717 copy_tex_sub_image(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2718 x
, y
, width
, height
);
2723 _mesa_meta_CopyTexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
2724 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2726 GLsizei width
, GLsizei height
)
2728 copy_tex_sub_image(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2729 x
, y
, width
, height
);
2734 _mesa_meta_CopyColorTable(GLcontext
*ctx
,
2735 GLenum target
, GLenum internalformat
,
2736 GLint x
, GLint y
, GLsizei width
)
2740 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2742 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyColorTable");
2747 * Read image from framebuffer (disable pixel transfer ops)
2749 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2750 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2751 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2753 _mesa_ColorTable(target
, internalformat
, width
, GL_RGBA
, GL_FLOAT
, buf
);
2755 _mesa_meta_end(ctx
);
2762 _mesa_meta_CopyColorSubTable(GLcontext
*ctx
,GLenum target
, GLsizei start
,
2763 GLint x
, GLint y
, GLsizei width
)
2767 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2769 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyColorSubTable");
2774 * Read image from framebuffer (disable pixel transfer ops)
2776 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2777 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2778 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2780 _mesa_ColorSubTable(target
, start
, width
, GL_RGBA
, GL_FLOAT
, buf
);
2782 _mesa_meta_end(ctx
);