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/readpix.h"
47 #include "main/scissor.h"
48 #include "main/shaders.h"
49 #include "main/stencil.h"
50 #include "main/texobj.h"
51 #include "main/texenv.h"
52 #include "main/teximage.h"
53 #include "main/texparam.h"
54 #include "main/texstate.h"
55 #include "main/varray.h"
56 #include "main/viewport.h"
57 #include "shader/program.h"
58 #include "shader/arbprogram.h"
59 #include "swrast/swrast.h"
60 #include "drivers/common/meta.h"
64 * State which we may save/restore across meta ops.
65 * XXX this may be incomplete...
69 GLbitfield SavedState
; /**< bitmask of META_* flags */
71 /** META_ALPHA_TEST */
72 GLboolean AlphaEnabled
;
75 GLboolean BlendEnabled
;
76 GLboolean ColorLogicOpEnabled
;
78 /** META_COLOR_MASK */
81 /** META_DEPTH_TEST */
82 struct gl_depthbuffer_attrib Depth
;
87 /** META_PIXEL_STORE */
88 struct gl_pixelstore_attrib Pack
, Unpack
;
90 /** META_RASTERIZATION */
91 GLenum FrontPolygonMode
, BackPolygonMode
;
92 GLboolean PolygonOffset
;
93 GLboolean PolygonSmooth
;
94 GLboolean PolygonStipple
;
95 GLboolean PolygonCull
;
98 struct gl_scissor_attrib Scissor
;
101 GLboolean VertexProgramEnabled
;
102 struct gl_vertex_program
*VertexProgram
;
103 GLboolean FragmentProgramEnabled
;
104 struct gl_fragment_program
*FragmentProgram
;
107 /** META_STENCIL_TEST */
108 struct gl_stencil_attrib Stencil
;
110 /** META_TRANSFORM */
112 GLfloat ModelviewMatrix
[16];
113 GLfloat ProjectionMatrix
[16];
114 GLfloat TextureMatrix
[16];
115 GLbitfield ClipPlanesEnabled
;
119 GLuint ClientActiveUnit
;
120 /** for unit[0] only */
121 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
122 /** mask of TEXTURE_2D_BIT, etc */
123 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
124 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
125 GLuint EnvMode
; /* unit[0] only */
128 struct gl_array_object
*ArrayObj
;
129 struct gl_buffer_object
*ArrayBufferObj
;
132 GLint ViewportX
, ViewportY
, ViewportW
, ViewportH
;
133 GLclampd DepthNear
, DepthFar
;
135 /** Miscellaneous (always disabled) */
141 * State for glBlitFramebufer()
152 * State for glClear()
162 * State for glCopyPixels()
172 * State for glDrawPixels()
179 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
180 GLuint DepthFP
; /**< Fragment program for drawing depth images */
185 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
186 * This is currently shared by all the meta ops. But we could create a
187 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
192 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
193 GLsizei MaxSize
; /**< Max possible texture size */
194 GLboolean NPOT
; /**< Non-power of two size OK? */
195 GLsizei Width
, Height
; /**< Current texture size */
197 GLfloat Sright
, Ttop
; /**< right, top texcoords */
202 * All per-context meta state.
206 struct save_state Save
; /**< state saved during meta-ops */
208 struct temp_texture TempTex
;
210 struct blit_state Blit
; /**< For _mesa_meta_blit_framebuffer() */
211 struct clear_state Clear
; /**< For _mesa_meta_clear() */
212 struct copypix_state CopyPix
; /**< For _mesa_meta_copy_pixels() */
213 struct drawpix_state DrawPix
; /**< For _mesa_meta_draw_pixels() */
215 /* other possible meta-ops:
223 * Initialize meta-ops for a context.
224 * To be called once during context creation.
227 _mesa_meta_init(GLcontext
*ctx
)
231 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
236 * Free context meta-op state.
237 * To be called once during context destruction.
240 _mesa_meta_free(GLcontext
*ctx
)
242 struct gl_meta_state
*meta
= ctx
->Meta
;
244 if (_mesa_get_current_context()) {
245 /* if there's no current context, these textures, buffers, etc should
246 * still get freed by _mesa_free_context_data().
249 _mesa_DeleteTextures(1, &meta
->TempTex
.TexObj
);
251 /* glBlitFramebuffer */
252 _mesa_DeleteBuffersARB(1, & meta
->Blit
.VBO
);
253 _mesa_DeleteVertexArraysAPPLE(1, &meta
->Blit
.ArrayObj
);
254 _mesa_DeletePrograms(1, &meta
->Blit
.DepthFP
);
257 _mesa_DeleteBuffersARB(1, & meta
->Clear
.VBO
);
258 _mesa_DeleteVertexArraysAPPLE(1, &meta
->Clear
.ArrayObj
);
261 _mesa_DeleteBuffersARB(1, & meta
->CopyPix
.VBO
);
262 _mesa_DeleteVertexArraysAPPLE(1, &meta
->CopyPix
.ArrayObj
);
265 _mesa_DeleteBuffersARB(1, & meta
->DrawPix
.VBO
);
266 _mesa_DeleteVertexArraysAPPLE(1, &meta
->DrawPix
.ArrayObj
);
267 _mesa_DeletePrograms(1, &meta
->DrawPix
.DepthFP
);
268 _mesa_DeletePrograms(1, &meta
->DrawPix
.StencilFP
);
271 _mesa_free(ctx
->Meta
);
277 * Enter meta state. This is like a light-weight version of glPushAttrib
278 * but it also resets most GL state back to default values.
280 * \param state bitmask of META_* flags indicating which attribute groups
281 * to save and reset to their defaults
284 _mesa_meta_begin(GLcontext
*ctx
, GLbitfield state
)
286 struct save_state
*save
= &ctx
->Meta
->Save
;
288 save
->SavedState
= state
;
290 if (state
& META_ALPHA_TEST
) {
291 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
292 if (ctx
->Color
.AlphaEnabled
)
293 _mesa_Disable(GL_ALPHA_TEST
);
296 if (state
& META_BLEND
) {
297 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
298 if (ctx
->Color
.BlendEnabled
)
299 _mesa_Disable(GL_BLEND
);
300 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
301 if (ctx
->Color
.ColorLogicOpEnabled
)
302 _mesa_Disable(GL_COLOR_LOGIC_OP
);
305 if (state
& META_COLOR_MASK
) {
306 COPY_4V(save
->ColorMask
, ctx
->Color
.ColorMask
);
307 if (!ctx
->Color
.ColorMask
[0] ||
308 !ctx
->Color
.ColorMask
[1] ||
309 !ctx
->Color
.ColorMask
[2] ||
310 !ctx
->Color
.ColorMask
[3])
311 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
314 if (state
& META_DEPTH_TEST
) {
315 save
->Depth
= ctx
->Depth
; /* struct copy */
317 _mesa_Disable(GL_DEPTH_TEST
);
320 if (state
& META_FOG
) {
321 save
->Fog
= ctx
->Fog
.Enabled
;
322 if (ctx
->Fog
.Enabled
)
323 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
326 if (state
& META_PIXEL_STORE
) {
327 save
->Pack
= ctx
->Pack
;
328 save
->Unpack
= ctx
->Unpack
;
329 ctx
->Pack
= ctx
->DefaultPacking
;
330 ctx
->Unpack
= ctx
->DefaultPacking
;
333 if (state
& META_RASTERIZATION
) {
334 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
335 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
336 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
337 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
338 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
339 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
340 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
341 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
342 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
343 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
344 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
347 if (state
& META_SCISSOR
) {
348 save
->Scissor
= ctx
->Scissor
; /* struct copy */
351 if (state
& META_SHADER
) {
352 if (ctx
->Extensions
.ARB_vertex_program
) {
353 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
354 save
->VertexProgram
= ctx
->VertexProgram
.Current
;
355 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
358 if (ctx
->Extensions
.ARB_fragment_program
) {
359 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
360 save
->FragmentProgram
= ctx
->FragmentProgram
.Current
;
361 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
364 if (ctx
->Extensions
.ARB_shader_objects
) {
365 save
->Shader
= ctx
->Shader
.CurrentProgram
?
366 ctx
->Shader
.CurrentProgram
->Name
: 0;
367 _mesa_UseProgramObjectARB(0);
371 if (state
& META_STENCIL_TEST
) {
372 save
->Stencil
= ctx
->Stencil
; /* struct copy */
373 if (ctx
->Stencil
.Enabled
)
374 _mesa_Disable(GL_STENCIL_TEST
);
375 /* NOTE: other stencil state not reset */
378 if (state
& META_TEXTURE
) {
381 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
382 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
383 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
385 /* Disable all texture units */
386 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
387 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
388 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
389 if (ctx
->Texture
.Unit
[u
].Enabled
||
390 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
391 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
392 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
393 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
394 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
395 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
396 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
397 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
398 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
399 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
400 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
404 /* save current texture objects for unit[0] only */
405 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
406 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
407 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
410 /* set defaults for unit[0] */
411 _mesa_ActiveTextureARB(GL_TEXTURE0
);
412 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
413 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
416 if (state
& META_TRANSFORM
) {
417 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
418 _mesa_memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
419 16 * sizeof(GLfloat
));
420 _mesa_memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
421 16 * sizeof(GLfloat
));
422 _mesa_memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
423 16 * sizeof(GLfloat
));
424 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
425 /* set 1:1 vertex:pixel coordinate transform */
426 _mesa_ActiveTextureARB(GL_TEXTURE0
);
427 _mesa_MatrixMode(GL_TEXTURE
);
428 _mesa_LoadIdentity();
429 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
430 _mesa_MatrixMode(GL_MODELVIEW
);
431 _mesa_LoadIdentity();
432 _mesa_MatrixMode(GL_PROJECTION
);
433 _mesa_LoadIdentity();
434 _mesa_Ortho(0.0F
, ctx
->DrawBuffer
->Width
,
435 0.0F
, ctx
->DrawBuffer
->Height
,
437 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
438 if (ctx
->Transform
.ClipPlanesEnabled
) {
440 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
441 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
446 if (state
& META_VERTEX
) {
447 /* save vertex array object state */
448 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
449 ctx
->Array
.ArrayObj
);
450 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
451 ctx
->Array
.ArrayBufferObj
);
452 /* set some default state? */
455 if (state
& META_VIEWPORT
) {
456 /* save viewport state */
457 save
->ViewportX
= ctx
->Viewport
.X
;
458 save
->ViewportY
= ctx
->Viewport
.Y
;
459 save
->ViewportW
= ctx
->Viewport
.Width
;
460 save
->ViewportH
= ctx
->Viewport
.Height
;
461 /* set viewport to match window size */
462 if (ctx
->Viewport
.X
!= 0 ||
463 ctx
->Viewport
.Y
!= 0 ||
464 ctx
->Viewport
.Width
!= ctx
->DrawBuffer
->Width
||
465 ctx
->Viewport
.Height
!= ctx
->DrawBuffer
->Height
) {
466 _mesa_set_viewport(ctx
, 0, 0,
467 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
469 /* save depth range state */
470 save
->DepthNear
= ctx
->Viewport
.Near
;
471 save
->DepthFar
= ctx
->Viewport
.Far
;
472 /* set depth range to default */
473 _mesa_DepthRange(0.0, 1.0);
478 save
->Lighting
= ctx
->Light
.Enabled
;
479 if (ctx
->Light
.Enabled
)
480 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
486 * Leave meta state. This is like a light-weight version of glPopAttrib().
489 _mesa_meta_end(GLcontext
*ctx
)
491 struct save_state
*save
= &ctx
->Meta
->Save
;
492 const GLbitfield state
= save
->SavedState
;
494 if (state
& META_ALPHA_TEST
) {
495 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
496 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
499 if (state
& META_BLEND
) {
500 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
)
501 _mesa_set_enable(ctx
, GL_BLEND
, save
->BlendEnabled
);
502 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
503 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
506 if (state
& META_COLOR_MASK
) {
507 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
, save
->ColorMask
))
508 _mesa_ColorMask(save
->ColorMask
[0], save
->ColorMask
[1],
509 save
->ColorMask
[2], save
->ColorMask
[3]);
512 if (state
& META_DEPTH_TEST
) {
513 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
514 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
515 _mesa_DepthFunc(save
->Depth
.Func
);
516 _mesa_DepthMask(save
->Depth
.Mask
);
519 if (state
& META_FOG
) {
520 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
523 if (state
& META_PIXEL_STORE
) {
524 ctx
->Pack
= save
->Pack
;
525 ctx
->Unpack
= save
->Unpack
;
528 if (state
& META_RASTERIZATION
) {
529 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
530 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
531 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
532 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
533 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
534 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
537 if (state
& META_SCISSOR
) {
538 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
539 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
540 save
->Scissor
.Width
, save
->Scissor
.Height
);
543 if (state
& META_SHADER
) {
544 if (ctx
->Extensions
.ARB_vertex_program
) {
545 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
546 save
->VertexProgramEnabled
);
547 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
548 save
->VertexProgram
);
551 if (ctx
->Extensions
.ARB_fragment_program
) {
552 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
553 save
->FragmentProgramEnabled
);
554 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
555 save
->FragmentProgram
);
558 if (ctx
->Extensions
.ARB_shader_objects
) {
559 _mesa_UseProgramObjectARB(save
->Shader
);
563 if (state
& META_STENCIL_TEST
) {
564 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
566 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
567 _mesa_ClearStencil(stencil
->Clear
);
568 if (ctx
->Extensions
.EXT_stencil_two_side
) {
569 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
570 stencil
->TestTwoSide
);
571 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
572 ? GL_BACK
: GL_FRONT
);
575 _mesa_StencilFuncSeparate(GL_FRONT
,
576 stencil
->Function
[0],
578 stencil
->ValueMask
[0]);
579 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
580 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
581 stencil
->ZFailFunc
[0],
582 stencil
->ZPassFunc
[0]);
584 _mesa_StencilFuncSeparate(GL_BACK
,
585 stencil
->Function
[1],
587 stencil
->ValueMask
[1]);
588 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
589 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
590 stencil
->ZFailFunc
[1],
591 stencil
->ZPassFunc
[1]);
594 if (state
& META_TEXTURE
) {
597 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
599 /* restore texenv for unit[0] */
600 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
602 /* restore texture objects for unit[0] only */
603 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
604 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
605 save
->CurrentTexture
[tgt
]);
608 /* Re-enable textures, texgen */
609 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
610 if (save
->TexEnabled
[u
]) {
611 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
613 if (save
->TexEnabled
[u
] & TEXTURE_1D_BIT
)
614 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_TRUE
);
615 if (save
->TexEnabled
[u
] & TEXTURE_2D_BIT
)
616 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_TRUE
);
617 if (save
->TexEnabled
[u
] & TEXTURE_3D_BIT
)
618 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_TRUE
);
619 if (save
->TexEnabled
[u
] & TEXTURE_CUBE_BIT
)
620 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_TRUE
);
621 if (save
->TexEnabled
[u
] & TEXTURE_RECT_BIT
)
622 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_TRUE
);
625 if (save
->TexGenEnabled
[u
]) {
626 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
628 if (save
->TexGenEnabled
[u
] & S_BIT
)
629 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_TRUE
);
630 if (save
->TexGenEnabled
[u
] & T_BIT
)
631 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_TRUE
);
632 if (save
->TexGenEnabled
[u
] & R_BIT
)
633 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_TRUE
);
634 if (save
->TexGenEnabled
[u
] & Q_BIT
)
635 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_TRUE
);
639 /* restore current unit state */
640 _mesa_ActiveTextureARB(GL_TEXTURE0
+ save
->ActiveUnit
);
641 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ save
->ClientActiveUnit
);
644 if (state
& META_TRANSFORM
) {
645 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
646 _mesa_ActiveTextureARB(GL_TEXTURE0
);
647 _mesa_MatrixMode(GL_TEXTURE
);
648 _mesa_LoadMatrixf(save
->TextureMatrix
);
649 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
651 _mesa_MatrixMode(GL_MODELVIEW
);
652 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
654 _mesa_MatrixMode(GL_PROJECTION
);
655 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
657 _mesa_MatrixMode(save
->MatrixMode
);
659 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
660 if (save
->ClipPlanesEnabled
) {
662 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
663 if (save
->ClipPlanesEnabled
& (1 << i
)) {
664 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
670 if (state
& META_VERTEX
) {
671 /* restore vertex buffer object */
672 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
673 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
675 /* restore vertex array object */
676 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
677 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
680 if (state
& META_VIEWPORT
) {
681 if (save
->ViewportX
!= ctx
->Viewport
.X
||
682 save
->ViewportY
!= ctx
->Viewport
.Y
||
683 save
->ViewportW
!= ctx
->Viewport
.Width
||
684 save
->ViewportH
!= ctx
->Viewport
.Height
) {
685 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
686 save
->ViewportW
, save
->ViewportH
);
688 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
692 if (save
->Lighting
) {
693 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
696 _mesa_set_enable(ctx
, GL_FOG
, GL_TRUE
);
702 * Return pointer to temp_texture info. This does some one-time init
705 static struct temp_texture
*
706 get_temp_texture(GLcontext
*ctx
)
708 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
711 /* do one-time init */
713 /* prefer texture rectangle */
714 if (ctx
->Extensions
.NV_texture_rectangle
) {
715 tex
->Target
= GL_TEXTURE_RECTANGLE
;
716 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
720 /* use 2D texture, NPOT if possible */
721 tex
->Target
= GL_TEXTURE_2D
;
722 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
723 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
725 assert(tex
->MaxSize
> 0);
727 _mesa_GenTextures(1, &tex
->TexObj
);
728 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
736 * Compute the width/height of texture needed to draw an image of the
737 * given size. Return a flag indicating whether the current texture
738 * can be re-used (glTexSubImage2D) or if a new texture needs to be
739 * allocated (glTexImage2D).
740 * Also, compute s/t texcoords for drawing.
742 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
745 alloc_texture(struct temp_texture
*tex
,
746 GLsizei width
, GLsizei height
, GLenum intFormat
)
748 GLboolean newTex
= GL_FALSE
;
750 if (width
> tex
->Width
||
751 height
> tex
->Height
||
752 intFormat
!= tex
->IntFormat
) {
753 /* alloc new texture (larger or different format) */
756 /* use non-power of two size */
758 tex
->Height
= height
;
761 /* find power of two size */
772 tex
->IntFormat
= intFormat
;
777 /* compute texcoords */
778 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
779 tex
->Sright
= (GLfloat
) width
;
780 tex
->Ttop
= (GLfloat
) height
;
783 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
784 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
792 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
795 setup_copypix_texture(struct temp_texture
*tex
,
797 GLint srcX
, GLint srcY
,
798 GLsizei width
, GLsizei height
, GLenum intFormat
,
801 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
802 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
803 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
804 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
806 /* copy framebuffer image to texture */
808 /* create new tex image */
809 if (tex
->Width
== width
&& tex
->Height
== height
) {
810 /* create new tex with framebuffer data */
811 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
812 srcX
, srcY
, width
, height
, 0);
815 /* create empty texture */
816 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
817 tex
->Width
, tex
->Height
, 0,
818 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
820 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
821 0, 0, srcX
, srcY
, width
, height
);
825 /* replace existing tex image */
826 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
827 0, 0, srcX
, srcY
, width
, height
);
833 * Setup/load texture for glDrawPixels.
836 setup_drawpix_texture(struct temp_texture
*tex
,
839 GLsizei width
, GLsizei height
,
840 GLenum format
, GLenum type
,
841 const GLvoid
*pixels
)
843 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
844 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
845 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
846 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
848 /* copy pixel data to texture */
850 /* create new tex image */
851 if (tex
->Width
== width
&& tex
->Height
== height
) {
852 /* create new tex and load image data */
853 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
854 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
857 /* create empty texture */
858 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
859 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
861 _mesa_TexSubImage2D(tex
->Target
, 0,
862 0, 0, width
, height
, format
, type
, pixels
);
866 /* replace existing tex image */
867 _mesa_TexSubImage2D(tex
->Target
, 0,
868 0, 0, width
, height
, format
, type
, pixels
);
875 * One-time init for drawing depth pixels.
878 init_blit_depth_pixels(GLcontext
*ctx
)
880 static const char *program
=
882 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
885 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
886 struct temp_texture
*tex
= get_temp_texture(ctx
);
887 const char *texTarget
;
889 assert(blit
->DepthFP
== 0);
891 /* replace %s with "RECT" or "2D" */
892 assert(strlen(program
) + 4 < sizeof(program2
));
893 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
897 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
899 _mesa_GenPrograms(1, &blit
->DepthFP
);
900 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
901 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
902 strlen(program2
), (const GLubyte
*) program2
);
907 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
908 * of texture mapping and polygon rendering.
911 _mesa_meta_blit_framebuffer(GLcontext
*ctx
,
912 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
913 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
914 GLbitfield mask
, GLenum filter
)
916 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
917 struct temp_texture
*tex
= get_temp_texture(ctx
);
918 const GLsizei maxTexSize
= tex
->MaxSize
;
919 const GLint srcX
= MIN2(srcX0
, srcX1
);
920 const GLint srcY
= MIN2(srcY0
, srcY1
);
921 const GLint srcW
= abs(srcX1
- srcX0
);
922 const GLint srcH
= abs(srcY1
- srcY0
);
923 const GLboolean srcFlipX
= srcX1
< srcX0
;
924 const GLboolean srcFlipY
= srcY1
< srcY0
;
925 GLfloat verts
[4][4]; /* four verts of X,Y,S,T */
928 if (srcW
> maxTexSize
|| srcH
> maxTexSize
) {
929 /* XXX avoid this fallback */
930 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
931 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
947 /* only scissor effects blit so save/clear all other relevant state */
948 _mesa_meta_begin(ctx
, ~META_SCISSOR
);
950 if (blit
->ArrayObj
== 0) {
953 /* create vertex array object */
954 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
955 _mesa_BindVertexArray(blit
->ArrayObj
);
957 /* create vertex array buffer */
958 _mesa_GenBuffersARB(1, &blit
->VBO
);
959 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
960 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
961 NULL
, GL_DYNAMIC_DRAW_ARB
);
963 /* setup vertex arrays */
964 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(verts
[0]),
965 (void *) (0 * sizeof(GLfloat
)));
966 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(verts
[0]),
967 (void *) (2 * sizeof(GLfloat
)));
968 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
969 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
972 _mesa_BindVertexArray(blit
->ArrayObj
);
973 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
976 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
978 /* vertex positions/texcoords (after texture allocation!) */
980 verts
[0][0] = (GLfloat
) dstX0
;
981 verts
[0][1] = (GLfloat
) dstY0
;
982 verts
[1][0] = (GLfloat
) dstX1
;
983 verts
[1][1] = (GLfloat
) dstY0
;
984 verts
[2][0] = (GLfloat
) dstX1
;
985 verts
[2][1] = (GLfloat
) dstY1
;
986 verts
[3][0] = (GLfloat
) dstX0
;
987 verts
[3][1] = (GLfloat
) dstY1
;
991 verts
[1][2] = tex
->Sright
;
993 verts
[2][2] = tex
->Sright
;
994 verts
[2][3] = tex
->Ttop
;
996 verts
[3][3] = tex
->Ttop
;
998 /* upload new vertex data */
999 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1002 _mesa_Enable(tex
->Target
);
1004 if (mask
& GL_COLOR_BUFFER_BIT
) {
1005 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1007 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1008 mask
&= ~GL_COLOR_BUFFER_BIT
;
1011 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1012 GLuint
*tmp
= (GLuint
*) _mesa_malloc(srcW
* srcH
* sizeof(GLuint
));
1015 init_blit_depth_pixels(ctx
);
1017 /* maybe change tex format here */
1018 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1020 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1021 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1023 setup_drawpix_texture(tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1024 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1026 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1027 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1028 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1029 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1030 _mesa_DepthFunc(GL_ALWAYS
);
1031 _mesa_DepthMask(GL_TRUE
);
1033 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1034 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1040 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1041 /* XXX can't easily do stencil */
1044 _mesa_Disable(tex
->Target
);
1046 _mesa_meta_end(ctx
);
1049 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1050 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1056 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1059 _mesa_meta_clear(GLcontext
*ctx
, GLbitfield buffers
)
1061 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1062 GLfloat verts
[4][7]; /* four verts of X,Y,Z,R,G,B,A */
1063 /* save all state but scissor, pixel pack/unpack */
1064 GLbitfield metaSave
= META_ALL
- META_SCISSOR
- META_PIXEL_STORE
;
1066 if (buffers
& BUFFER_BITS_COLOR
) {
1067 /* if clearing color buffers, don't save/restore colormask */
1068 metaSave
-= META_COLOR_MASK
;
1071 _mesa_meta_begin(ctx
, metaSave
);
1073 if (clear
->ArrayObj
== 0) {
1074 /* one-time setup */
1076 /* create vertex array object */
1077 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1078 _mesa_BindVertexArray(clear
->ArrayObj
);
1080 /* create vertex array buffer */
1081 _mesa_GenBuffersARB(1, &clear
->VBO
);
1082 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1083 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1084 NULL
, GL_DYNAMIC_DRAW_ARB
);
1086 /* setup vertex arrays */
1087 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(verts
[0]),
1088 (void *) (0 * sizeof(GLfloat
)));
1089 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(verts
[0]),
1090 (void *) (3 * sizeof(GLfloat
)));
1091 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1092 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1095 _mesa_BindVertexArray(clear
->ArrayObj
);
1096 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1099 /* GL_COLOR_BUFFER_BIT */
1100 if (buffers
& BUFFER_BITS_COLOR
) {
1101 /* leave colormask, glDrawBuffer state as-is */
1104 ASSERT(metaSave
& META_COLOR_MASK
);
1105 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1108 /* GL_DEPTH_BUFFER_BIT */
1109 if (buffers
& BUFFER_BIT_DEPTH
) {
1110 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1111 _mesa_DepthFunc(GL_ALWAYS
);
1112 _mesa_DepthMask(GL_TRUE
);
1115 assert(!ctx
->Depth
.Test
);
1118 /* GL_STENCIL_BUFFER_BIT */
1119 if (buffers
& BUFFER_BIT_STENCIL
) {
1120 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1121 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1122 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1123 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1124 ctx
->Stencil
.Clear
& 0x7fffffff,
1125 ctx
->Stencil
.WriteMask
[0]);
1128 assert(!ctx
->Stencil
.Enabled
);
1131 /* vertex positions/colors */
1133 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1134 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1135 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1136 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1137 const GLfloat z
= 1.0 - 2.0 * ctx
->Depth
.Clear
;
1154 for (i
= 0; i
< 4; i
++) {
1155 COPY_4FV(&verts
[i
][3], ctx
->Color
.ClearColor
);
1158 /* upload new vertex data */
1159 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1163 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1165 _mesa_meta_end(ctx
);
1170 * Meta implementation of ctx->Driver.CopyPixels() in terms
1171 * of texture mapping and polygon rendering.
1174 _mesa_meta_copy_pixels(GLcontext
*ctx
, GLint srcX
, GLint srcY
,
1175 GLsizei width
, GLsizei height
,
1176 GLint dstX
, GLint dstY
, GLenum type
)
1178 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1179 struct temp_texture
*tex
= get_temp_texture(ctx
);
1180 GLfloat verts
[4][5]; /* four verts of X,Y,Z,S,T */
1182 GLenum intFormat
= GL_RGBA
;
1184 if (type
!= GL_COLOR
||
1185 ctx
->_ImageTransferState
||
1187 width
> tex
->MaxSize
||
1188 height
> tex
->MaxSize
) {
1189 /* XXX avoid this fallback */
1190 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1194 /* Most GL state applies to glCopyPixels, but a there's a few things
1195 * we need to override:
1197 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1204 if (copypix
->ArrayObj
== 0) {
1205 /* one-time setup */
1207 /* create vertex array object */
1208 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
1209 _mesa_BindVertexArray(copypix
->ArrayObj
);
1211 /* create vertex array buffer */
1212 _mesa_GenBuffersARB(1, ©pix
->VBO
);
1213 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1214 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1215 NULL
, GL_DYNAMIC_DRAW_ARB
);
1217 /* setup vertex arrays */
1218 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(verts
[0]),
1219 (void *) (0 * sizeof(GLfloat
)));
1220 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(verts
[0]),
1221 (void *) (3 * sizeof(GLfloat
)));
1222 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1223 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1226 _mesa_BindVertexArray(copypix
->ArrayObj
);
1227 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1230 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
1232 /* vertex positions, texcoords (after texture allocation!) */
1234 const GLfloat dstX0
= (GLfloat
) dstX
;
1235 const GLfloat dstY0
= (GLfloat
) dstY
;
1236 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1237 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1238 const GLfloat z
= ctx
->Current
.RasterPos
[2];
1240 verts
[0][0] = dstX0
;
1241 verts
[0][1] = dstY0
;
1245 verts
[1][0] = dstX1
;
1246 verts
[1][1] = dstY0
;
1248 verts
[1][3] = tex
->Sright
;
1250 verts
[2][0] = dstX1
;
1251 verts
[2][1] = dstY1
;
1253 verts
[2][3] = tex
->Sright
;
1254 verts
[2][4] = tex
->Ttop
;
1255 verts
[3][0] = dstX0
;
1256 verts
[3][1] = dstY1
;
1259 verts
[3][4] = tex
->Ttop
;
1261 /* upload new vertex data */
1262 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1265 /* Alloc/setup texture */
1266 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
1267 GL_RGBA
, GL_NEAREST
);
1269 _mesa_Enable(tex
->Target
);
1271 /* draw textured quad */
1272 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1274 _mesa_Disable(tex
->Target
);
1276 _mesa_meta_end(ctx
);
1282 * When the glDrawPixels() image size is greater than the max rectangle
1283 * texture size we use this function to break the glDrawPixels() image
1284 * into tiles which fit into the max texture size.
1287 tiled_draw_pixels(GLcontext
*ctx
,
1289 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1290 GLenum format
, GLenum type
,
1291 const struct gl_pixelstore_attrib
*unpack
,
1292 const GLvoid
*pixels
)
1294 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
1297 if (tileUnpack
.RowLength
== 0)
1298 tileUnpack
.RowLength
= width
;
1300 for (i
= 0; i
< width
; i
+= tileSize
) {
1301 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
1302 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
1304 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
1306 for (j
= 0; j
< height
; j
+= tileSize
) {
1307 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
1308 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
1310 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
1312 _mesa_meta_draw_pixels(ctx
, tileX
, tileY
,
1313 tileWidth
, tileHeight
,
1314 format
, type
, &tileUnpack
, pixels
);
1321 * One-time init for drawing stencil pixels.
1324 init_draw_stencil_pixels(GLcontext
*ctx
)
1326 /* This program is run eight times, once for each stencil bit.
1327 * The stencil values to draw are found in an 8-bit alpha texture.
1328 * We read the texture/stencil value and test if bit 'b' is set.
1329 * If the bit is not set, use KIL to kill the fragment.
1330 * Finally, we use the stencil test to update the stencil buffer.
1332 * The basic algorithm for checking if a bit is set is:
1333 * if (is_odd(value / (1 << bit)))
1334 * result is one (or non-zero).
1337 * The program parameter contains three values:
1338 * parm.x = 255 / (1 << bit)
1342 static const char *program
=
1344 "PARAM parm = program.local[0]; \n"
1346 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1347 "# t = t * 255 / bit \n"
1348 "MUL t.x, t.a, parm.x; \n"
1351 "SUB t.x, t.x, t.y; \n"
1353 "MUL t.x, t.x, parm.y; \n"
1354 "# t = fract(t.x) \n"
1355 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
1356 "# t.x = (t.x == 0 ? 1 : 0) \n"
1357 "SGE t.x, -t.x, parm.z; \n"
1359 "# for debug only \n"
1360 "#MOV result.color, t.x; \n"
1362 char program2
[1000];
1363 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1364 struct temp_texture
*tex
= get_temp_texture(ctx
);
1365 const char *texTarget
;
1367 assert(drawpix
->StencilFP
== 0);
1369 /* replace %s with "RECT" or "2D" */
1370 assert(strlen(program
) + 4 < sizeof(program2
));
1371 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1375 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1377 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
1378 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1379 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1380 strlen(program2
), (const GLubyte
*) program2
);
1385 * One-time init for drawing depth pixels.
1388 init_draw_depth_pixels(GLcontext
*ctx
)
1390 static const char *program
=
1392 "PARAM color = program.local[0]; \n"
1393 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1394 "MOV result.color, color; \n"
1397 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1398 struct temp_texture
*tex
= get_temp_texture(ctx
);
1399 const char *texTarget
;
1401 assert(drawpix
->DepthFP
== 0);
1403 /* replace %s with "RECT" or "2D" */
1404 assert(strlen(program
) + 4 < sizeof(program2
));
1405 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1409 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1411 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
1412 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1413 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1414 strlen(program2
), (const GLubyte
*) program2
);
1419 * Meta implementation of ctx->Driver.DrawPixels() in terms
1420 * of texture mapping and polygon rendering.
1423 _mesa_meta_draw_pixels(GLcontext
*ctx
,
1424 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1425 GLenum format
, GLenum type
,
1426 const struct gl_pixelstore_attrib
*unpack
,
1427 const GLvoid
*pixels
)
1429 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1430 struct temp_texture
*tex
= get_temp_texture(ctx
);
1431 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
1432 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
1433 GLfloat verts
[4][5]; /* four verts of X,Y,Z,S,T */
1434 GLenum texIntFormat
;
1435 GLboolean fallback
, newTex
;
1436 GLbitfield metaExtraSave
= 0x0;
1439 * Determine if we can do the glDrawPixels with texture mapping.
1441 fallback
= GL_FALSE
;
1442 if (ctx
->_ImageTransferState
||
1447 if (_mesa_is_color_format(format
)) {
1448 /* use more compact format when possible */
1449 if (format
== GL_LUMINANCE
|| format
== GL_LUMINANCE_ALPHA
)
1450 texIntFormat
= format
;
1452 texIntFormat
= GL_RGBA
;
1454 else if (_mesa_is_stencil_format(format
)) {
1455 if (ctx
->Extensions
.ARB_fragment_program
&&
1456 type
== GL_UNSIGNED_BYTE
) {
1457 /* We'll store stencil as alpha. This only works for GLubyte
1458 * image data because of how incoming values are mapped to alpha
1461 texIntFormat
= GL_ALPHA
;
1462 metaExtraSave
= (META_COLOR_MASK
|
1471 else if (_mesa_is_depth_format(format
)) {
1472 if (ctx
->Extensions
.ARB_depth_texture
&&
1473 ctx
->Extensions
.ARB_fragment_program
) {
1474 texIntFormat
= GL_DEPTH_COMPONENT
;
1475 metaExtraSave
= (META_SHADER
);
1486 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
1487 format
, type
, unpack
, pixels
);
1492 * Check image size against max texture size, draw as tiles if needed.
1494 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
1495 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
1496 format
, type
, unpack
, pixels
);
1500 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
1501 * but a there's a few things we need to override:
1503 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1511 if (drawpix
->ArrayObj
== 0) {
1512 /* one-time setup */
1514 /* create vertex array object */
1515 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
1516 _mesa_BindVertexArray(drawpix
->ArrayObj
);
1518 /* create vertex array buffer */
1519 _mesa_GenBuffersARB(1, &drawpix
->VBO
);
1520 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, drawpix
->VBO
);
1521 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1522 NULL
, GL_DYNAMIC_DRAW_ARB
);
1524 /* setup vertex arrays */
1525 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(verts
[0]),
1526 (void *) (0 * sizeof(GLfloat
)));
1527 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(verts
[0]),
1528 (void *) (3 * sizeof(GLfloat
)));
1529 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1530 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1533 _mesa_BindVertexArray(drawpix
->ArrayObj
);
1534 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, drawpix
->VBO
);
1537 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
1539 /* vertex positions, texcoords (after texture allocation!) */
1541 const GLfloat x0
= (GLfloat
) x
;
1542 const GLfloat y0
= (GLfloat
) y
;
1543 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
1544 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
1545 const GLfloat z
= ctx
->Current
.RasterPos
[2];
1555 verts
[1][3] = tex
->Sright
;
1560 verts
[2][3] = tex
->Sright
;
1561 verts
[2][4] = tex
->Ttop
;
1566 verts
[3][4] = tex
->Ttop
;
1568 /* upload new vertex data */
1569 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1572 /* set given unpack params */
1573 ctx
->Unpack
= *unpack
;
1575 _mesa_Enable(tex
->Target
);
1577 if (_mesa_is_stencil_format(format
)) {
1578 /* Drawing stencil */
1581 if (!drawpix
->StencilFP
)
1582 init_draw_stencil_pixels(ctx
);
1584 setup_drawpix_texture(tex
, newTex
, texIntFormat
, width
, height
,
1585 GL_ALPHA
, type
, pixels
);
1587 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1589 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1590 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
1592 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1593 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1595 for (bit
= 0; bit
< ctx
->Visual
.stencilBits
; bit
++) {
1596 const GLuint mask
= 1 << bit
;
1597 if (mask
& origStencilMask
) {
1598 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
1599 _mesa_StencilMask(mask
);
1601 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
1602 255.0 / mask
, 0.5, 0.0, 0.0);
1604 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1608 else if (_mesa_is_depth_format(format
)) {
1610 if (!drawpix
->DepthFP
)
1611 init_draw_depth_pixels(ctx
);
1613 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1614 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1616 /* polygon color = current raster color */
1617 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
1618 ctx
->Current
.RasterColor
);
1620 setup_drawpix_texture(tex
, newTex
, texIntFormat
, width
, height
,
1621 format
, type
, pixels
);
1623 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1627 setup_drawpix_texture(tex
, newTex
, texIntFormat
, width
, height
,
1628 format
, type
, pixels
);
1629 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1632 _mesa_Disable(tex
->Target
);
1634 /* restore unpack params */
1635 ctx
->Unpack
= unpackSave
;
1637 _mesa_meta_end(ctx
);