1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
38 #include "st_context.h"
39 #include "st_cb_clear.h"
40 #include "st_public.h"
41 #include "pipe/p_context.h"
42 #include "pipe/p_defines.h"
47 * Draw a screen-aligned quadrilateral.
48 * Coords are window coords.
51 draw_quad(GLcontext
*ctx
,
52 float x0
, float y0
, float x1
, float y1
, GLfloat z
,
53 const GLfloat color
[4])
55 static const GLuint attribs
[2] = {
59 GLfloat verts
[4][2][4]; /* four verts, two attribs, XYZW */
75 /* same for all verts: */
76 for (i
= 0; i
< 4; i
++) {
79 verts
[i
][1][0] = color
[0];
80 verts
[i
][1][1] = color
[1];
81 verts
[i
][1][2] = color
[2];
82 verts
[i
][1][3] = color
[3];
85 ctx
->st
->pipe
->draw_vertices(ctx
->st
->pipe
, GL_QUADS
,
86 4, (GLfloat
*) verts
, 2, attribs
);
92 * Do glClear by drawing a quadrilateral.
95 clear_with_quad(GLcontext
*ctx
,
96 GLboolean color
, GLboolean depth
, GLboolean stencil
)
98 struct st_context
*st
= ctx
->st
;
99 struct pipe_blend_state blend
;
100 struct pipe_depth_state depth_test
;
101 struct pipe_stencil_state stencil_test
;
103 /* depth state: always pass */
104 memset(&depth_test
, 0, sizeof(depth
));
106 depth_test
.enabled
= 1;
107 depth_test
.writemask
= 1;
108 depth_test
.func
= PIPE_FUNC_ALWAYS
;
110 st
->pipe
->set_depth_state(st
->pipe
, &depth_test
);
112 /* stencil state: always set to ref value */
113 memset(&stencil_test
, 0, sizeof(stencil
));
115 stencil_test
.front_enabled
= 1;
116 stencil_test
.front_func
= PIPE_FUNC_ALWAYS
;
117 stencil_test
.front_fail_op
= PIPE_STENCIL_OP_REPLACE
;
118 stencil_test
.front_zpass_op
= PIPE_STENCIL_OP_REPLACE
;
119 stencil_test
.front_zfail_op
= PIPE_STENCIL_OP_REPLACE
;
120 stencil_test
.ref_value
[0] = ctx
->Stencil
.Clear
;
121 stencil_test
.value_mask
[0] = 0xff;
122 stencil_test
.write_mask
[0] = ctx
->Stencil
.WriteMask
[0];
124 st
->pipe
->set_stencil_state(st
->pipe
, &stencil_test
);
126 /* blend state: RGBA masking */
127 memset(&blend
, 0, sizeof(blend
));
129 if (ctx
->Color
.ColorMask
[0])
130 blend
.colormask
|= PIPE_MASK_R
;
131 if (ctx
->Color
.ColorMask
[1])
132 blend
.colormask
|= PIPE_MASK_G
;
133 if (ctx
->Color
.ColorMask
[2])
134 blend
.colormask
|= PIPE_MASK_B
;
135 if (ctx
->Color
.ColorMask
[3])
136 blend
.colormask
|= PIPE_MASK_A
;
137 if (st
->ctx
->Color
.DitherFlag
)
140 st
->pipe
->set_blend_state(st
->pipe
, &blend
);
143 ctx
->Scissor
.X
, ctx
->Scissor
.Y
,
144 ctx
->Scissor
.X
+ ctx
->Scissor
.Width
,
145 ctx
->Scissor
.Y
+ ctx
->Scissor
.Height
,
146 ctx
->Depth
.Clear
, ctx
->Color
.ClearColor
);
148 /* Restore GL state */
149 st
->pipe
->set_blend_state(st
->pipe
, &st
->state
.blend
);
150 st
->pipe
->set_depth_state(st
->pipe
, &st
->state
.depth
);
151 st
->pipe
->set_stencil_state(st
->pipe
, &st
->state
.stencil
);
153 st_invalidate_state(ctx, _NEW_COLOR | _NEW_DEPTH | _NEW_STENCIL);
160 * Called via ctx->Driver.Clear()
161 * XXX: doesn't pick up the differences between front/back/left/right
162 * clears. Need to sort that out...
164 static void st_clear(GLcontext
*ctx
, GLbitfield mask
)
166 struct st_context
*st
= ctx
->st
;
167 GLboolean color
= (mask
& BUFFER_BITS_COLOR
) ? GL_TRUE
: GL_FALSE
;
168 GLboolean depth
= (mask
& BUFFER_BIT_DEPTH
) ? GL_TRUE
: GL_FALSE
;
169 GLboolean stencil
= (mask
& BUFFER_BIT_STENCIL
) ? GL_TRUE
: GL_FALSE
;
170 GLboolean accum
= (mask
& BUFFER_BIT_ACCUM
) ? GL_TRUE
: GL_FALSE
;
172 GLboolean maskColor
, maskStencil
;
173 GLboolean fullscreen
= !ctx
->Scissor
.Enabled
;
174 GLuint stencilMax
= stencil
? (1 << ctx
->DrawBuffer
->_StencilBuffer
->StencilBits
) : 0;
176 /* This makes sure the softpipe has the latest scissor, etc values */
177 st_validate_state( st
);
179 maskColor
= color
&& st
->state
.blend
.colormask
!= PIPE_MASK_RGBA
;
180 maskStencil
= stencil
&& ctx
->Stencil
.WriteMask
[0] != stencilMax
;
182 if (fullscreen
&& !maskColor
&& !maskStencil
) {
183 /* pipe->clear() should clear a particular surface, so that we
184 * can iterate over render buffers at this level and clear the
185 * ones GL is asking for.
187 * Will probably need something like pipe->clear_z_stencil() to
188 * cope with the special case of paired and unpaired z/stencil
189 * buffers, though could perhaps deal with them explicitly at
192 st
->pipe
->clear(st
->pipe
, color
, depth
, stencil
);
194 /* And here we would do a clear on whatever surface we are using
195 * to implement accum buffers:
200 clear_with_quad(ctx
, color
, depth
, stencil
);
205 void st_init_cb_clear( struct st_context
*st
)
207 struct dd_function_table
*functions
= &st
->ctx
->Driver
;
209 functions
->Clear
= st_clear
;
213 void st_destroy_cb_clear( struct st_context
*st
)