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/arrayobj.h"
38 #include "main/blend.h"
39 #include "main/bufferobj.h"
40 #include "main/depth.h"
41 #include "main/enable.h"
42 #include "main/image.h"
43 #include "main/macros.h"
44 #include "main/matrix.h"
45 #include "main/polygon.h"
46 #include "main/scissor.h"
47 #include "main/shaders.h"
48 #include "main/stencil.h"
49 #include "main/texobj.h"
50 #include "main/texenv.h"
51 #include "main/teximage.h"
52 #include "main/texparam.h"
53 #include "main/texstate.h"
54 #include "main/varray.h"
55 #include "main/viewport.h"
56 #include "shader/program.h"
57 #include "shader/arbprogram.h"
58 #include "swrast/swrast.h"
59 #include "drivers/common/meta.h"
63 * State which we may save/restore across meta ops.
64 * XXX this may be incomplete...
68 GLbitfield SavedState
; /**< bitmask of META_* flags */
70 /** META_ALPHA_TEST */
71 GLboolean AlphaEnabled
;
74 GLboolean BlendEnabled
;
75 GLboolean ColorLogicOpEnabled
;
77 /** META_COLOR_MASK */
80 /** META_DEPTH_TEST */
81 struct gl_depthbuffer_attrib Depth
;
86 /** META_PIXELSTORE */
89 /** META_RASTERIZATION */
90 GLenum FrontPolygonMode
, BackPolygonMode
;
91 GLboolean PolygonOffset
;
92 GLboolean PolygonSmooth
;
93 GLboolean PolygonStipple
;
94 GLboolean PolygonCull
;
97 struct gl_scissor_attrib Scissor
;
100 GLboolean VertexProgramEnabled
;
101 struct gl_vertex_program
*VertexProgram
;
102 GLboolean FragmentProgramEnabled
;
103 struct gl_fragment_program
*FragmentProgram
;
106 /** META_STENCIL_TEST */
107 struct gl_stencil_attrib Stencil
;
109 /** META_TRANSFORM */
111 GLfloat ModelviewMatrix
[16];
112 GLfloat ProjectionMatrix
[16];
113 GLfloat TextureMatrix
[16];
114 GLbitfield ClipPlanesEnabled
;
118 GLuint ClientActiveUnit
;
119 /** for unit[0] only */
120 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
121 /** mask of TEXTURE_2D_BIT, etc */
122 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
123 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
124 GLuint EnvMode
; /* unit[0] only */
127 struct gl_array_object
*ArrayObj
;
128 struct gl_buffer_object
*ArrayBufferObj
;
131 GLint ViewportX
, ViewportY
, ViewportW
, ViewportH
;
132 GLclampd DepthNear
, DepthFar
;
134 /** Miscellaneous (always disabled) */
140 * State for glBlitFramebufer()
146 GLfloat verts
[4][4]; /**< four verts of X,Y,S,T */
151 * State for glClear()
157 GLfloat verts
[4][7]; /**< four verts of X,Y,Z,R,G,B,A */
162 * State for glCopyPixels()
168 GLfloat verts
[4][5]; /**< four verts of X,Y,Z,S,T */
173 * State for glDrawPixels()
179 GLfloat verts
[4][5]; /**< four verts of X,Y,Z,S,T */
181 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
182 GLuint DepthFP
; /**< Fragment program for drawing depth images */
187 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
188 * This is currently shared by all the meta ops. But we could create a
189 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
194 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
195 GLsizei MaxSize
; /**< Max possible texture size */
196 GLboolean NPOT
; /**< Non-power of two size OK? */
197 GLsizei Width
, Height
; /**< Current texture size */
199 GLfloat Sright
, Ttop
; /**< right, top texcoords */
204 * All per-context meta state.
208 struct save_state Save
; /**< state saved during meta-ops */
210 struct temp_texture TempTex
;
212 struct blit_state Blit
; /**< For _mesa_meta_blit_framebuffer() */
213 struct clear_state Clear
; /**< For _mesa_meta_clear() */
214 struct copypix_state CopyPix
; /**< For _mesa_meta_copy_pixels() */
215 struct drawpix_state DrawPix
; /**< For _mesa_meta_draw_pixels() */
217 /* other possible meta-ops:
225 * Initialize meta-ops for a context.
226 * To be called once during context creation.
229 _mesa_meta_init(GLcontext
*ctx
)
233 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
238 * Free context meta-op state.
239 * To be called once during context destruction.
242 _mesa_meta_free(GLcontext
*ctx
)
244 struct gl_meta_state
*meta
= ctx
->Meta
;
246 if (_mesa_get_current_context()) {
247 /* if there's no current context, these textures, buffers, etc should
248 * still get freed by _mesa_free_context_data().
251 if (meta
->TempTex
.TexObj
) {
252 _mesa_DeleteTextures(1, &meta
->TempTex
.TexObj
);
255 if (meta
->Blit
.VBO
) {
256 _mesa_DeleteBuffersARB(1, & meta
->Blit
.VBO
);
257 _mesa_DeleteVertexArraysAPPLE(1, &meta
->Blit
.ArrayObj
);
260 if (meta
->Clear
.VBO
) {
261 _mesa_DeleteBuffersARB(1, & meta
->Clear
.VBO
);
262 _mesa_DeleteVertexArraysAPPLE(1, &meta
->Clear
.ArrayObj
);
265 if (meta
->CopyPix
.VBO
) {
266 _mesa_DeleteBuffersARB(1, & meta
->CopyPix
.VBO
);
267 _mesa_DeleteVertexArraysAPPLE(1, &meta
->CopyPix
.ArrayObj
);
270 if (meta
->DrawPix
.VBO
) {
271 _mesa_DeleteBuffersARB(1, & meta
->DrawPix
.VBO
);
272 _mesa_DeleteVertexArraysAPPLE(1, &meta
->DrawPix
.ArrayObj
);
276 _mesa_free(ctx
->Meta
);
282 * Enter meta state. This is like a light-weight version of glPushAttrib
283 * but it also resets most GL state back to default values.
285 * \param state bitmask of META_* flags indicating which attribute groups
286 * to save and reset to their defaults
289 _mesa_meta_begin(GLcontext
*ctx
, GLbitfield state
)
291 struct save_state
*save
= &ctx
->Meta
->Save
;
293 save
->SavedState
= state
;
295 if (state
& META_ALPHA_TEST
) {
296 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
297 if (ctx
->Color
.AlphaEnabled
)
298 _mesa_Disable(GL_ALPHA_TEST
);
301 if (state
& META_BLEND
) {
302 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
303 if (ctx
->Color
.BlendEnabled
)
304 _mesa_Disable(GL_BLEND
);
305 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
306 if (ctx
->Color
.ColorLogicOpEnabled
)
307 _mesa_Disable(GL_COLOR_LOGIC_OP
);
310 if (state
& META_COLOR_MASK
) {
311 COPY_4V(save
->ColorMask
, ctx
->Color
.ColorMask
);
312 if (!ctx
->Color
.ColorMask
[0] ||
313 !ctx
->Color
.ColorMask
[1] ||
314 !ctx
->Color
.ColorMask
[2] ||
315 !ctx
->Color
.ColorMask
[3])
316 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
319 if (state
& META_DEPTH_TEST
) {
320 save
->Depth
= ctx
->Depth
; /* struct copy */
322 _mesa_Disable(GL_DEPTH_TEST
);
325 if (state
& META_FOG
) {
326 save
->Fog
= ctx
->Fog
.Enabled
;
327 if (ctx
->Fog
.Enabled
)
328 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
331 if (state
& META_RASTERIZATION
) {
332 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
333 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
334 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
335 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
336 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
337 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
338 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
339 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
340 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
341 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
342 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
345 if (state
& META_SCISSOR
) {
346 save
->Scissor
= ctx
->Scissor
; /* struct copy */
349 if (state
& META_SHADER
) {
350 if (ctx
->Extensions
.ARB_vertex_program
) {
351 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
352 save
->VertexProgram
= ctx
->VertexProgram
.Current
;
353 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
356 if (ctx
->Extensions
.ARB_fragment_program
) {
357 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
358 save
->FragmentProgram
= ctx
->FragmentProgram
.Current
;
359 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
362 if (ctx
->Extensions
.ARB_shader_objects
) {
363 save
->Shader
= ctx
->Shader
.CurrentProgram
?
364 ctx
->Shader
.CurrentProgram
->Name
: 0;
365 _mesa_UseProgramObjectARB(0);
369 if (state
& META_STENCIL_TEST
) {
370 save
->Stencil
= ctx
->Stencil
; /* struct copy */
371 if (ctx
->Stencil
.Enabled
)
372 _mesa_Disable(GL_STENCIL_TEST
);
373 /* NOTE: other stencil state not reset */
376 if (state
& META_TEXTURE
) {
379 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
380 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
381 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
383 /* Disable all texture units */
384 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
385 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
386 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
387 if (ctx
->Texture
.Unit
[u
].Enabled
||
388 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
389 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
390 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
391 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
392 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
393 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
394 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
395 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
396 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
397 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
398 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
402 /* save current texture objects for unit[0] only */
403 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
404 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
405 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
408 /* set defaults for unit[0] */
409 _mesa_ActiveTextureARB(GL_TEXTURE0
);
410 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
411 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
414 if (state
& META_TRANSFORM
) {
415 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
416 _mesa_memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
417 16 * sizeof(GLfloat
));
418 _mesa_memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
419 16 * sizeof(GLfloat
));
420 _mesa_memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
421 16 * sizeof(GLfloat
));
422 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
423 /* set 1:1 vertex:pixel coordinate transform */
424 _mesa_ActiveTextureARB(GL_TEXTURE0
);
425 _mesa_MatrixMode(GL_TEXTURE
);
426 _mesa_LoadIdentity();
427 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
428 _mesa_MatrixMode(GL_MODELVIEW
);
429 _mesa_LoadIdentity();
430 _mesa_MatrixMode(GL_PROJECTION
);
431 _mesa_LoadIdentity();
432 _mesa_Ortho(0.0F
, ctx
->DrawBuffer
->Width
,
433 0.0F
, ctx
->DrawBuffer
->Height
,
435 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
436 if (ctx
->Transform
.ClipPlanesEnabled
) {
438 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
439 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
444 if (state
& META_VERTEX
) {
445 /* save vertex array object state */
446 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
447 ctx
->Array
.ArrayObj
);
448 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
449 ctx
->Array
.ArrayBufferObj
);
450 /* set some default state? */
453 if (state
& META_VIEWPORT
) {
454 /* save viewport state */
455 save
->ViewportX
= ctx
->Viewport
.X
;
456 save
->ViewportY
= ctx
->Viewport
.Y
;
457 save
->ViewportW
= ctx
->Viewport
.Width
;
458 save
->ViewportH
= ctx
->Viewport
.Height
;
459 /* set viewport to match window size */
460 if (ctx
->Viewport
.X
!= 0 ||
461 ctx
->Viewport
.Y
!= 0 ||
462 ctx
->Viewport
.Width
!= ctx
->DrawBuffer
->Width
||
463 ctx
->Viewport
.Height
!= ctx
->DrawBuffer
->Height
) {
464 _mesa_set_viewport(ctx
, 0, 0,
465 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
467 /* save depth range state */
468 save
->DepthNear
= ctx
->Viewport
.Near
;
469 save
->DepthFar
= ctx
->Viewport
.Far
;
470 /* set depth range to default */
471 _mesa_DepthRange(0.0, 1.0);
476 save
->Lighting
= ctx
->Light
.Enabled
;
477 if (ctx
->Light
.Enabled
)
478 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
484 * Leave meta state. This is like a light-weight version of glPopAttrib().
487 _mesa_meta_end(GLcontext
*ctx
)
489 struct save_state
*save
= &ctx
->Meta
->Save
;
490 const GLbitfield state
= save
->SavedState
;
492 if (state
& META_ALPHA_TEST
) {
493 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
494 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
497 if (state
& META_BLEND
) {
498 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
)
499 _mesa_set_enable(ctx
, GL_BLEND
, save
->BlendEnabled
);
500 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
501 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
504 if (state
& META_COLOR_MASK
) {
505 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
, save
->ColorMask
))
506 _mesa_ColorMask(save
->ColorMask
[0], save
->ColorMask
[1],
507 save
->ColorMask
[2], save
->ColorMask
[3]);
510 if (state
& META_DEPTH_TEST
) {
511 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
512 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
513 _mesa_DepthFunc(save
->Depth
.Func
);
514 _mesa_DepthMask(save
->Depth
.Mask
);
517 if (state
& META_FOG
) {
518 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
521 if (state
& META_RASTERIZATION
) {
522 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
523 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
524 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
525 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
526 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
527 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
530 if (state
& META_SCISSOR
) {
531 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
532 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
533 save
->Scissor
.Width
, save
->Scissor
.Height
);
536 if (state
& META_SHADER
) {
537 if (ctx
->Extensions
.ARB_vertex_program
) {
538 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
539 save
->VertexProgramEnabled
);
540 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
541 save
->VertexProgram
);
544 if (ctx
->Extensions
.ARB_fragment_program
) {
545 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
546 save
->FragmentProgramEnabled
);
547 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
548 save
->FragmentProgram
);
551 if (ctx
->Extensions
.ARB_shader_objects
) {
552 _mesa_UseProgramObjectARB(save
->Shader
);
556 if (state
& META_STENCIL_TEST
) {
557 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
559 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
560 _mesa_ClearStencil(stencil
->Clear
);
561 if (ctx
->Extensions
.EXT_stencil_two_side
) {
562 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
563 stencil
->TestTwoSide
);
564 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
565 ? GL_BACK
: GL_FRONT
);
568 _mesa_StencilFuncSeparate(GL_FRONT
,
569 stencil
->Function
[0],
571 stencil
->ValueMask
[0]);
572 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
573 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
574 stencil
->ZFailFunc
[0],
575 stencil
->ZPassFunc
[0]);
577 _mesa_StencilFuncSeparate(GL_BACK
,
578 stencil
->Function
[1],
580 stencil
->ValueMask
[1]);
581 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
582 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
583 stencil
->ZFailFunc
[1],
584 stencil
->ZPassFunc
[1]);
587 if (state
& META_TEXTURE
) {
590 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
592 /* restore texenv for unit[0] */
593 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
595 /* restore texture objects for unit[0] only */
596 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
597 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
598 save
->CurrentTexture
[tgt
]);
601 /* Re-enable textures, texgen */
602 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
603 if (save
->TexEnabled
[u
]) {
604 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
606 if (save
->TexEnabled
[u
] & TEXTURE_1D_BIT
)
607 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_TRUE
);
608 if (save
->TexEnabled
[u
] & TEXTURE_2D_BIT
)
609 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_TRUE
);
610 if (save
->TexEnabled
[u
] & TEXTURE_3D_BIT
)
611 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_TRUE
);
612 if (save
->TexEnabled
[u
] & TEXTURE_CUBE_BIT
)
613 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_TRUE
);
614 if (save
->TexEnabled
[u
] & TEXTURE_RECT_BIT
)
615 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_TRUE
);
618 if (save
->TexGenEnabled
[u
]) {
619 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
621 if (save
->TexGenEnabled
[u
] & S_BIT
)
622 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_TRUE
);
623 if (save
->TexGenEnabled
[u
] & T_BIT
)
624 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_TRUE
);
625 if (save
->TexGenEnabled
[u
] & R_BIT
)
626 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_TRUE
);
627 if (save
->TexGenEnabled
[u
] & Q_BIT
)
628 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_TRUE
);
632 /* restore current unit state */
633 _mesa_ActiveTextureARB(GL_TEXTURE0
+ save
->ActiveUnit
);
634 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ save
->ClientActiveUnit
);
637 if (state
& META_TRANSFORM
) {
638 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
639 _mesa_ActiveTextureARB(GL_TEXTURE0
);
640 _mesa_MatrixMode(GL_TEXTURE
);
641 _mesa_LoadMatrixf(save
->TextureMatrix
);
642 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
644 _mesa_MatrixMode(GL_MODELVIEW
);
645 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
647 _mesa_MatrixMode(GL_PROJECTION
);
648 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
650 _mesa_MatrixMode(save
->MatrixMode
);
652 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
653 if (save
->ClipPlanesEnabled
) {
655 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
656 if (save
->ClipPlanesEnabled
& (1 << i
)) {
657 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
663 if (state
& META_VERTEX
) {
664 /* restore vertex buffer object */
665 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
666 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
668 /* restore vertex array object */
669 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
670 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
673 if (state
& META_VIEWPORT
) {
674 if (save
->ViewportX
!= ctx
->Viewport
.X
||
675 save
->ViewportY
!= ctx
->Viewport
.Y
||
676 save
->ViewportW
!= ctx
->Viewport
.Width
||
677 save
->ViewportH
!= ctx
->Viewport
.Height
) {
678 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
679 save
->ViewportW
, save
->ViewportH
);
681 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
685 if (save
->Lighting
) {
686 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
689 _mesa_set_enable(ctx
, GL_FOG
, GL_TRUE
);
695 * Return pointer to temp_texture info. This does some one-time init
698 static struct temp_texture
*
699 get_temp_texture(GLcontext
*ctx
)
701 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
704 /* do one-time init */
706 /* prefer texture rectangle */
707 if (ctx
->Extensions
.NV_texture_rectangle
) {
708 tex
->Target
= GL_TEXTURE_RECTANGLE
;
709 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
713 /* use 2D texture, NPOT if possible */
714 tex
->Target
= GL_TEXTURE_2D
;
715 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
716 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
718 assert(tex
->MaxSize
> 0);
720 _mesa_GenTextures(1, &tex
->TexObj
);
721 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
729 * Compute the width/height of texture needed to draw an image of the
730 * given size. Return a flag indicating whether the current texture
731 * can be re-used (glTexSubImage2D) or if a new texture needs to be
732 * allocated (glTexImage2D).
733 * Also, compute s/t texcoords for drawing.
735 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
738 alloc_texture(struct temp_texture
*tex
,
739 GLsizei width
, GLsizei height
, GLenum intFormat
)
741 GLboolean newTex
= GL_FALSE
;
743 if (1|| width
> tex
->Width
||
744 height
> tex
->Height
||
745 intFormat
!= tex
->IntFormat
) {
746 /* alloc new texture (larger or different format) */
749 /* use non-power of two size */
751 tex
->Height
= height
;
754 /* find power of two size */
765 tex
->IntFormat
= intFormat
;
770 /* compute texcoords */
771 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
772 tex
->Sright
= (GLfloat
) width
;
773 tex
->Ttop
= (GLfloat
) height
;
776 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
777 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
785 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
788 setup_copypix_texture(struct temp_texture
*tex
,
790 GLint srcX
, GLint srcY
,
791 GLsizei width
, GLsizei height
, GLenum intFormat
,
794 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
795 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
796 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
797 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
799 /* copy framebuffer image to texture */
801 /* create new tex image */
802 if (tex
->Width
== width
&& tex
->Height
== height
) {
803 /* create new tex with framebuffer data */
804 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
805 srcX
, srcY
, width
, height
, 0);
808 /* create empty texture */
809 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
810 tex
->Width
, tex
->Height
, 0,
811 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
813 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
814 0, 0, srcX
, srcY
, width
, height
);
818 /* replace existing tex image */
819 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
820 0, 0, srcX
, srcY
, width
, height
);
826 * Setup/load texture for glDrawPixels.
829 setup_drawpix_texture(struct temp_texture
*tex
,
832 GLsizei width
, GLsizei height
,
833 GLenum format
, GLenum type
,
834 const GLvoid
*pixels
)
836 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
837 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
838 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
839 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
841 /* copy pixel data to texture */
843 /* create new tex image */
844 if (tex
->Width
== width
&& tex
->Height
== height
) {
845 /* create new tex and load image data */
846 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
847 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
850 /* create empty texture */
851 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
852 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
854 _mesa_TexSubImage2D(tex
->Target
, 0,
855 0, 0, width
, height
, format
, type
, pixels
);
859 /* replace existing tex image */
860 _mesa_TexSubImage2D(tex
->Target
, 0,
861 0, 0, width
, height
, format
, type
, pixels
);
868 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
869 * of texture mapping and polygon rendering.
872 _mesa_meta_blit_framebuffer(GLcontext
*ctx
,
873 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
874 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
875 GLbitfield mask
, GLenum filter
)
877 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
878 struct temp_texture
*tex
= get_temp_texture(ctx
);
879 const GLsizei maxTexSize
= tex
->MaxSize
;
880 const GLint srcX
= MIN2(srcX0
, srcX1
);
881 const GLint srcY
= MIN2(srcY0
, srcY1
);
882 const GLint srcW
= abs(srcX1
- srcX0
);
883 const GLint srcH
= abs(srcY1
- srcY0
);
884 const GLboolean srcFlipX
= srcX1
< srcX0
;
885 const GLboolean srcFlipY
= srcY1
< srcY0
;
888 if (srcW
> maxTexSize
|| srcH
> maxTexSize
) {
889 /* XXX avoid this fallback */
890 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
891 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
907 /* only scissor effects blit so save/clear all other relevant state */
908 _mesa_meta_begin(ctx
, ~META_SCISSOR
);
910 if (blit
->ArrayObj
== 0) {
913 /* create vertex array object */
914 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
915 _mesa_BindVertexArray(blit
->ArrayObj
);
917 /* create vertex array buffer */
918 _mesa_GenBuffersARB(1, &blit
->VBO
);
919 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
920 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(blit
->verts
),
921 blit
->verts
, GL_STREAM_DRAW_ARB
);
923 /* setup vertex arrays */
924 _mesa_VertexPointer(2, GL_FLOAT
, 4 * sizeof(GLfloat
),
925 (void*) (0 * sizeof(GLfloat
)));
926 _mesa_TexCoordPointer(2, GL_FLOAT
, 4 * sizeof(GLfloat
),
927 (void *) (2 * sizeof(GLfloat
)));
928 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
929 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
932 _mesa_BindVertexArray(blit
->ArrayObj
);
933 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
936 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
938 /* vertex positions/texcoords (after texture allocation!) */
940 blit
->verts
[0][0] = (GLfloat
) dstX0
;
941 blit
->verts
[0][1] = (GLfloat
) dstY0
;
942 blit
->verts
[1][0] = (GLfloat
) dstX1
;
943 blit
->verts
[1][1] = (GLfloat
) dstY0
;
944 blit
->verts
[2][0] = (GLfloat
) dstX1
;
945 blit
->verts
[2][1] = (GLfloat
) dstY1
;
946 blit
->verts
[3][0] = (GLfloat
) dstX0
;
947 blit
->verts
[3][1] = (GLfloat
) dstY1
;
949 blit
->verts
[0][2] = 0.0F
;
950 blit
->verts
[0][3] = 0.0F
;
951 blit
->verts
[1][2] = tex
->Sright
;
952 blit
->verts
[1][3] = 0.0F
;
953 blit
->verts
[2][2] = tex
->Sright
;
954 blit
->verts
[2][3] = tex
->Ttop
;
955 blit
->verts
[3][2] = 0.0F
;
956 blit
->verts
[3][3] = tex
->Ttop
;
958 /* upload new vertex data */
959 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0,
960 sizeof(blit
->verts
), blit
->verts
);
963 _mesa_Enable(tex
->Target
);
965 if (mask
& GL_COLOR_BUFFER_BIT
) {
966 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
968 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
969 mask
&= ~GL_COLOR_BUFFER_BIT
;
971 if (mask
& GL_DEPTH_BUFFER_BIT
) {
972 /* XXX todo (need fragment shader) */
974 if (mask
& GL_STENCIL_BUFFER_BIT
) {
975 /* XXX can't easily do stencil */
978 _mesa_Disable(tex
->Target
);
982 /* XXX, TO-DO: try to handle these cases above! */
983 if (mask
& (GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
)) {
984 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
985 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
991 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
994 _mesa_meta_clear(GLcontext
*ctx
, GLbitfield buffers
)
996 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
997 GLbitfield metaSave
= META_ALL
- META_SCISSOR
; /* all but scissor */
999 if (buffers
& BUFFER_BITS_COLOR
) {
1000 /* if clearing color buffers, don't save/restore colormask */
1001 metaSave
-= META_COLOR_MASK
;
1004 _mesa_meta_begin(ctx
, metaSave
);
1006 if (clear
->ArrayObj
== 0) {
1007 /* one-time setup */
1009 /* create vertex array object */
1010 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1011 _mesa_BindVertexArray(clear
->ArrayObj
);
1013 /* create vertex array buffer */
1014 _mesa_GenBuffersARB(1, &clear
->VBO
);
1015 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1016 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(clear
->verts
),
1017 clear
->verts
, GL_STREAM_DRAW_ARB
);
1019 /* setup vertex arrays */
1020 _mesa_VertexPointer(3, GL_FLOAT
, 7 * sizeof(GLfloat
), (void *) 0);
1021 _mesa_ColorPointer(4, GL_FLOAT
, 7 * sizeof(GLfloat
),
1022 (void *) (3 * sizeof(GLfloat
)));
1023 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1024 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1027 _mesa_BindVertexArray(clear
->ArrayObj
);
1028 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1031 /* GL_COLOR_BUFFER_BIT */
1032 if (buffers
& BUFFER_BITS_COLOR
) {
1033 /* leave colormask, glDrawBuffer state as-is */
1036 ASSERT(metaSave
& META_COLOR_MASK
);
1037 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1040 /* GL_DEPTH_BUFFER_BIT */
1041 if (buffers
& BUFFER_BIT_DEPTH
) {
1042 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1043 _mesa_DepthFunc(GL_ALWAYS
);
1044 _mesa_DepthMask(GL_TRUE
);
1047 assert(!ctx
->Depth
.Test
);
1050 /* GL_STENCIL_BUFFER_BIT */
1051 if (buffers
& BUFFER_BIT_STENCIL
) {
1052 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1053 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1054 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1055 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1056 ctx
->Stencil
.Clear
& 0x7fffffff,
1057 ctx
->Stencil
.WriteMask
[0]);
1060 assert(!ctx
->Stencil
.Enabled
);
1063 /* vertex positions/colors */
1065 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1066 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1067 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1068 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1069 const GLfloat z
= 1.0 - 2.0 * ctx
->Depth
.Clear
;
1072 clear
->verts
[0][0] = x0
;
1073 clear
->verts
[0][1] = y0
;
1074 clear
->verts
[0][2] = z
;
1075 clear
->verts
[1][0] = x1
;
1076 clear
->verts
[1][1] = y0
;
1077 clear
->verts
[1][2] = z
;
1078 clear
->verts
[2][0] = x1
;
1079 clear
->verts
[2][1] = y1
;
1080 clear
->verts
[2][2] = z
;
1081 clear
->verts
[3][0] = x0
;
1082 clear
->verts
[3][1] = y1
;
1083 clear
->verts
[3][2] = z
;
1086 for (i
= 0; i
< 4; i
++) {
1087 COPY_4FV(&clear
->verts
[i
][3], ctx
->Color
.ClearColor
);
1090 /* upload new vertex data */
1091 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0,
1092 sizeof(clear
->verts
), clear
->verts
);
1096 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1098 _mesa_meta_end(ctx
);
1103 * Meta implementation of ctx->Driver.CopyPixels() in terms
1104 * of texture mapping and polygon rendering.
1107 _mesa_meta_copy_pixels(GLcontext
*ctx
, GLint srcX
, GLint srcY
,
1108 GLsizei width
, GLsizei height
,
1109 GLint dstX
, GLint dstY
, GLenum type
)
1111 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1112 struct temp_texture
*tex
= get_temp_texture(ctx
);
1114 GLenum intFormat
= GL_RGBA
;
1116 if (type
!= GL_COLOR
||
1117 ctx
->_ImageTransferState
||
1119 width
> tex
->MaxSize
||
1120 height
> tex
->MaxSize
) {
1121 /* XXX avoid this fallback */
1122 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1126 /* Most GL state applies to glCopyPixels, but a there's a few things
1127 * we need to override:
1129 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1136 if (copypix
->ArrayObj
== 0) {
1137 /* one-time setup */
1139 /* create vertex array object */
1140 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
1141 _mesa_BindVertexArray(copypix
->ArrayObj
);
1143 /* create vertex array buffer */
1144 _mesa_GenBuffersARB(1, ©pix
->VBO
);
1145 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1146 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(copypix
->verts
),
1147 copypix
->verts
, GL_STREAM_DRAW_ARB
);
1149 /* setup vertex arrays */
1150 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(copypix
->verts
[0]),
1151 (void*) (0 * sizeof(GLfloat
)));
1152 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(copypix
->verts
[0]),
1153 (void *) (3 * sizeof(GLfloat
)));
1154 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1155 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1158 _mesa_BindVertexArray(copypix
->ArrayObj
);
1159 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1162 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
1164 /* vertex positions, texcoords (after texture allocation!) */
1166 const GLfloat dstX0
= (GLfloat
) dstX
;
1167 const GLfloat dstY0
= (GLfloat
) dstY
;
1168 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1169 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1170 const GLfloat z
= ctx
->Current
.RasterPos
[2];
1172 copypix
->verts
[0][0] = dstX0
;
1173 copypix
->verts
[0][1] = dstY0
;
1174 copypix
->verts
[0][2] = z
;
1175 copypix
->verts
[0][3] = 0.0F
;
1176 copypix
->verts
[0][4] = 0.0F
;
1177 copypix
->verts
[1][0] = dstX1
;
1178 copypix
->verts
[1][1] = dstY0
;
1179 copypix
->verts
[1][2] = z
;
1180 copypix
->verts
[1][3] = tex
->Sright
;
1181 copypix
->verts
[1][4] = 0.0F
;
1182 copypix
->verts
[2][0] = dstX1
;
1183 copypix
->verts
[2][1] = dstY1
;
1184 copypix
->verts
[2][2] = z
;
1185 copypix
->verts
[2][3] = tex
->Sright
;
1186 copypix
->verts
[2][4] = tex
->Ttop
;
1187 copypix
->verts
[3][0] = dstX0
;
1188 copypix
->verts
[3][1] = dstY1
;
1189 copypix
->verts
[3][2] = z
;
1190 copypix
->verts
[3][3] = 0.0F
;
1191 copypix
->verts
[3][4] = tex
->Ttop
;
1193 /* upload new vertex data */
1194 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0,
1195 sizeof(copypix
->verts
), copypix
->verts
);
1198 /* Alloc/setup texture */
1199 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
1200 GL_RGBA
, GL_NEAREST
);
1202 _mesa_Enable(tex
->Target
);
1204 /* draw textured quad */
1205 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1207 _mesa_Disable(tex
->Target
);
1209 _mesa_meta_end(ctx
);
1215 * When the glDrawPixels() image size is greater than the max rectangle
1216 * texture size we use this function to break the glDrawPixels() image
1217 * into tiles which fit into the max texture size.
1220 tiled_draw_pixels(GLcontext
*ctx
,
1222 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1223 GLenum format
, GLenum type
,
1224 const struct gl_pixelstore_attrib
*unpack
,
1225 const GLvoid
*pixels
)
1227 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
1230 if (tileUnpack
.RowLength
== 0)
1231 tileUnpack
.RowLength
= width
;
1233 for (i
= 0; i
< width
; i
+= tileSize
) {
1234 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
1235 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
1237 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
1239 for (j
= 0; j
< height
; j
+= tileSize
) {
1240 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
1241 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
1243 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
1245 _mesa_meta_draw_pixels(ctx
, tileX
, tileY
,
1246 tileWidth
, tileHeight
,
1247 format
, type
, &tileUnpack
, pixels
);
1254 * One-time init for drawing stencil pixels.
1257 init_draw_stencil_pixels(GLcontext
*ctx
)
1259 /* This program is run eight times, once for each stencil bit.
1260 * The stencil values to draw are found in an 8-bit alpha texture.
1261 * We read the texture/stencil value and test if bit 'b' is set.
1262 * If the bit is not set, use KIL to kill the fragment.
1263 * Finally, we use the stencil test to update the stencil buffer.
1265 * The basic algorithm for checking if a bit is set is:
1266 * if (is_odd(value / (1 << bit)))
1267 * result is one (or non-zero).
1270 * The program parameter contains three values:
1271 * parm.x = 255 / (1 << bit)
1275 static const char *program
=
1277 "PARAM parm = program.local[0]; \n"
1279 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1280 "# t = t * 255 / bit \n"
1281 "MUL t.x, t.a, parm.x; \n"
1284 "SUB t.x, t.x, t.y; \n"
1286 "MUL t.x, t.x, parm.y; \n"
1287 "# t = fract(t.x) \n"
1288 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
1289 "# t.x = (t.x == 0 ? 1 : 0) \n"
1290 "SGE t.x, -t.x, parm.z; \n"
1292 "# for debug only \n"
1293 "#MOV result.color, t.x; \n"
1295 char program2
[1000];
1296 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1297 struct temp_texture
*tex
= get_temp_texture(ctx
);
1298 const char *texTarget
;
1300 assert(drawpix
->StencilFP
== 0);
1302 /* replace %s with "RECT" or "2D" */
1303 assert(strlen(program
) + 4 < sizeof(program2
));
1304 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1308 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1310 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
1311 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1312 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1313 strlen(program2
), (const GLubyte
*) program2
);
1318 * One-time init for drawing depth pixels.
1321 init_draw_depth_pixels(GLcontext
*ctx
)
1323 static const char *program
=
1325 "PARAM color = program.local[0]; \n"
1326 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1327 "MOV result.color, color; \n"
1330 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1331 struct temp_texture
*tex
= get_temp_texture(ctx
);
1332 const char *texTarget
;
1334 assert(drawpix
->DepthFP
== 0);
1336 /* replace %s with "RECT" or "2D" */
1337 assert(strlen(program
) + 4 < sizeof(program2
));
1338 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1342 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1344 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
1345 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1346 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1347 strlen(program2
), (const GLubyte
*) program2
);
1352 * Meta implementation of ctx->Driver.DrawPixels() in terms
1353 * of texture mapping and polygon rendering.
1356 _mesa_meta_draw_pixels(GLcontext
*ctx
,
1357 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1358 GLenum format
, GLenum type
,
1359 const struct gl_pixelstore_attrib
*unpack
,
1360 const GLvoid
*pixels
)
1362 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1363 struct temp_texture
*tex
= get_temp_texture(ctx
);
1364 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
1365 GLenum texIntFormat
;
1366 GLboolean fallback
, newTex
;
1367 GLbitfield metaExtraSave
= 0x0;
1370 * Determine if we can do the glDrawPixels with texture mapping.
1372 fallback
= GL_FALSE
;
1373 if (ctx
->_ImageTransferState
||
1378 if (_mesa_is_color_format(format
)) {
1379 /* use more compact format when possible */
1380 if (format
== GL_LUMINANCE
|| format
== GL_LUMINANCE_ALPHA
)
1381 texIntFormat
= format
;
1383 texIntFormat
= GL_RGBA
;
1385 else if (_mesa_is_stencil_format(format
)) {
1386 if (ctx
->Extensions
.ARB_fragment_program
&&
1387 type
== GL_UNSIGNED_BYTE
) {
1388 /* We'll store stencil as alpha. This only works for GLubyte
1389 * image data because of how incoming values are mapped to alpha
1392 texIntFormat
= GL_ALPHA
;
1393 metaExtraSave
= (META_COLOR_MASK
|
1402 else if (_mesa_is_depth_format(format
)) {
1403 if (ctx
->Extensions
.ARB_depth_texture
&&
1404 ctx
->Extensions
.ARB_fragment_program
) {
1405 texIntFormat
= GL_DEPTH_COMPONENT
;
1406 metaExtraSave
= (META_SHADER
);
1417 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
1418 format
, type
, unpack
, pixels
);
1423 * Check image size against max texture size, draw as tiles if needed.
1425 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
1426 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
1427 format
, type
, unpack
, pixels
);
1431 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
1432 * but a there's a few things we need to override:
1434 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1442 if (drawpix
->ArrayObj
== 0) {
1443 /* one-time setup */
1445 /* create vertex array object */
1446 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
1447 _mesa_BindVertexArray(drawpix
->ArrayObj
);
1449 /* create vertex array buffer */
1450 _mesa_GenBuffersARB(1, &drawpix
->VBO
);
1451 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, drawpix
->VBO
);
1452 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(drawpix
->verts
),
1453 drawpix
->verts
, GL_STREAM_DRAW_ARB
);
1455 /* setup vertex arrays */
1456 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(drawpix
->verts
[0]),
1457 (void*) (0 * sizeof(GLfloat
)));
1458 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(drawpix
->verts
[0]),
1459 (void *) (3 * sizeof(GLfloat
)));
1460 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1461 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1464 _mesa_BindVertexArray(drawpix
->ArrayObj
);
1465 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, drawpix
->VBO
);
1468 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
1470 /* vertex positions, texcoords (after texture allocation!) */
1472 const GLfloat x0
= (GLfloat
) x
;
1473 const GLfloat y0
= (GLfloat
) y
;
1474 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
1475 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
1476 const GLfloat z
= ctx
->Current
.RasterPos
[2];
1478 drawpix
->verts
[0][0] = x0
;
1479 drawpix
->verts
[0][1] = y0
;
1480 drawpix
->verts
[0][2] = z
;
1481 drawpix
->verts
[0][3] = 0.0F
;
1482 drawpix
->verts
[0][4] = 0.0F
;
1483 drawpix
->verts
[1][0] = x1
;
1484 drawpix
->verts
[1][1] = y0
;
1485 drawpix
->verts
[1][2] = z
;
1486 drawpix
->verts
[1][3] = tex
->Sright
;
1487 drawpix
->verts
[1][4] = 0.0F
;
1488 drawpix
->verts
[2][0] = x1
;
1489 drawpix
->verts
[2][1] = y1
;
1490 drawpix
->verts
[2][2] = z
;
1491 drawpix
->verts
[2][3] = tex
->Sright
;
1492 drawpix
->verts
[2][4] = tex
->Ttop
;
1493 drawpix
->verts
[3][0] = x0
;
1494 drawpix
->verts
[3][1] = y1
;
1495 drawpix
->verts
[3][2] = z
;
1496 drawpix
->verts
[3][3] = 0.0F
;
1497 drawpix
->verts
[3][4] = tex
->Ttop
;
1499 /* upload new vertex data */
1500 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0,
1501 sizeof(drawpix
->verts
), drawpix
->verts
);
1504 /* set given unpack params */
1505 ctx
->Unpack
= *unpack
;
1507 _mesa_Enable(tex
->Target
);
1509 if (_mesa_is_stencil_format(format
)) {
1510 /* Drawing stencil */
1513 if (!drawpix
->StencilFP
)
1514 init_draw_stencil_pixels(ctx
);
1516 setup_drawpix_texture(tex
, newTex
, texIntFormat
, width
, height
,
1517 GL_ALPHA
, type
, pixels
);
1519 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1521 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1522 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
1524 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1525 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1527 for (bit
= 0; bit
< ctx
->Visual
.stencilBits
; bit
++) {
1528 const GLuint mask
= 1 << bit
;
1530 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
1531 _mesa_StencilMask(mask
);
1533 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
1534 255.0 / mask
, 0.5, 0.0, 0.0);
1536 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1539 else if (_mesa_is_depth_format(format
)) {
1541 if (!drawpix
->DepthFP
)
1542 init_draw_depth_pixels(ctx
);
1544 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1545 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1547 /* polygon color = current raster color */
1548 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
1549 ctx
->Current
.RasterColor
);
1551 setup_drawpix_texture(tex
, newTex
, texIntFormat
, width
, height
,
1552 format
, type
, pixels
);
1554 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1558 setup_drawpix_texture(tex
, newTex
, texIntFormat
, width
, height
,
1559 format
, type
, pixels
);
1560 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1563 _mesa_Disable(tex
->Target
);
1565 /* restore unpack params */
1566 ctx
->Unpack
= unpackSave
;
1568 _mesa_meta_end(ctx
);