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>
37 #include "st_context.h"
38 #include "st_cb_clear.h"
39 #include "st_public.h"
40 #include "pipe/p_context.h"
41 #include "pipe/p_defines.h"
47 color_value(GLuint pipeFormat
, const GLfloat color
[4])
51 UNCLAMPED_FLOAT_TO_UBYTE(r
, color
[0]);
52 UNCLAMPED_FLOAT_TO_UBYTE(g
, color
[1]);
53 UNCLAMPED_FLOAT_TO_UBYTE(b
, color
[2]);
54 UNCLAMPED_FLOAT_TO_UBYTE(a
, color
[3]);
57 case PIPE_FORMAT_U_R8_G8_B8_A8
:
58 return (r
<< 24) | (g
<< 16) | (b
<< 8) | a
;
59 case PIPE_FORMAT_U_A8_R8_G8_B8
:
60 return (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
61 case PIPE_FORMAT_U_R5_G6_B5
:
62 return ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | (b
>> 3);
70 depth_value(GLuint pipeFormat
, GLfloat value
)
74 case PIPE_FORMAT_U_Z16
:
75 val
= (GLuint
) (value
* 0xffffff);
77 case PIPE_FORMAT_U_Z32
:
78 val
= (GLuint
) (value
* 0xffffffff);
80 case PIPE_FORMAT_S8_Z24
:
81 /*case PIPE_FORMAT_Z24_S8:*/
82 val
= (GLuint
) (value
* 0xffffff);
92 is_depth_stencil_format(GLuint pipeFormat
)
95 case PIPE_FORMAT_S8_Z24
:
96 /*case PIPE_FORMAT_Z24_S8:*/
105 * Draw a screen-aligned quadrilateral.
106 * Coords are window coords.
109 draw_quad(GLcontext
*ctx
,
110 float x0
, float y0
, float x1
, float y1
, GLfloat z
,
111 const GLfloat color
[4])
113 static const GLuint attribs
[2] = {
117 GLfloat verts
[4][2][4]; /* four verts, two attribs, XYZW */
133 /* same for all verts: */
134 for (i
= 0; i
< 4; i
++) {
136 verts
[i
][0][3] = 1.0;
137 verts
[i
][1][0] = color
[0];
138 verts
[i
][1][1] = color
[1];
139 verts
[i
][1][2] = color
[2];
140 verts
[i
][1][3] = color
[3];
143 ctx
->st
->pipe
->draw_vertices(ctx
->st
->pipe
, GL_QUADS
,
144 4, (GLfloat
*) verts
, 2, attribs
);
150 * Do glClear by drawing a quadrilateral.
153 clear_with_quad(GLcontext
*ctx
, GLuint x0
, GLuint y0
,
154 GLuint x1
, GLuint y1
,
155 GLboolean color
, GLboolean depth
, GLboolean stencil
)
157 struct st_context
*st
= ctx
->st
;
158 struct pipe_alpha_test_state alpha_test
;
159 struct pipe_blend_state blend
;
160 struct pipe_depth_state depth_test
;
161 struct pipe_stencil_state stencil_test
;
162 struct pipe_setup_state setup
;
164 /* alpha state: disabled */
165 memset(&alpha_test
, 0, sizeof(alpha_test
));
166 st
->pipe
->set_alpha_test_state(st
->pipe
, &alpha_test
);
168 /* blend state: RGBA masking */
169 memset(&blend
, 0, sizeof(blend
));
171 if (ctx
->Color
.ColorMask
[0])
172 blend
.colormask
|= PIPE_MASK_R
;
173 if (ctx
->Color
.ColorMask
[1])
174 blend
.colormask
|= PIPE_MASK_G
;
175 if (ctx
->Color
.ColorMask
[2])
176 blend
.colormask
|= PIPE_MASK_B
;
177 if (ctx
->Color
.ColorMask
[3])
178 blend
.colormask
|= PIPE_MASK_A
;
179 if (st
->ctx
->Color
.DitherFlag
)
182 st
->pipe
->set_blend_state(st
->pipe
, &blend
);
184 /* depth state: always pass */
185 memset(&depth_test
, 0, sizeof(depth_test
));
187 depth_test
.enabled
= 1;
188 depth_test
.writemask
= 1;
189 depth_test
.func
= PIPE_FUNC_ALWAYS
;
191 st
->pipe
->set_depth_state(st
->pipe
, &depth_test
);
193 /* setup state: nothing */
194 memset(&setup
, 0, sizeof(setup
));
195 if (ctx
->Scissor
.Enabled
)
197 st
->pipe
->set_setup_state(st
->pipe
, &setup
);
199 /* stencil state: always set to ref value */
200 memset(&stencil_test
, 0, sizeof(stencil_test
));
202 stencil_test
.front_enabled
= 1;
203 stencil_test
.front_func
= PIPE_FUNC_ALWAYS
;
204 stencil_test
.front_fail_op
= PIPE_STENCIL_OP_REPLACE
;
205 stencil_test
.front_zpass_op
= PIPE_STENCIL_OP_REPLACE
;
206 stencil_test
.front_zfail_op
= PIPE_STENCIL_OP_REPLACE
;
207 stencil_test
.ref_value
[0] = ctx
->Stencil
.Clear
;
208 stencil_test
.value_mask
[0] = 0xff;
209 stencil_test
.write_mask
[0] = ctx
->Stencil
.WriteMask
[0] & 0xff;
211 st
->pipe
->set_stencil_state(st
->pipe
, &stencil_test
);
213 /* draw quad matching scissor rect (XXX verify coord round-off) */
214 draw_quad(ctx
, x0
, y0
, x1
, y1
, ctx
->Depth
.Clear
, ctx
->Color
.ClearColor
);
216 /* Restore pipe state */
217 st
->pipe
->set_alpha_test_state(st
->pipe
, &st
->state
.alpha_test
);
218 st
->pipe
->set_blend_state(st
->pipe
, &st
->state
.blend
);
219 st
->pipe
->set_depth_state(st
->pipe
, &st
->state
.depth
);
220 st
->pipe
->set_setup_state(st
->pipe
, &st
->state
.setup
);
221 st
->pipe
->set_stencil_state(st
->pipe
, &st
->state
.stencil
);
223 st_invalidate_state(ctx, _NEW_COLOR | _NEW_DEPTH | _NEW_STENCIL);
229 clear_color_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
231 if (ctx
->Color
.ColorMask
[0] &&
232 ctx
->Color
.ColorMask
[1] &&
233 ctx
->Color
.ColorMask
[2] &&
234 ctx
->Color
.ColorMask
[3] &&
235 !ctx
->Scissor
.Enabled
)
237 /* clear whole buffer w/out masking */
239 = color_value(rb
->surface
->format
, ctx
->Color
.ClearColor
);
240 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, rb
->surface
, clearValue
);
243 /* masking or scissoring */
245 ctx
->DrawBuffer
->_Xmin
,
246 ctx
->DrawBuffer
->_Xmin
,
247 ctx
->DrawBuffer
->_Xmax
,
248 ctx
->DrawBuffer
->_Ymax
,
249 GL_TRUE
, GL_FALSE
, GL_FALSE
);
255 clear_accum_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
257 if (!ctx
->Scissor
.Enabled
) {
258 /* clear whole buffer w/out masking */
260 = color_value(rb
->surface
->format
, ctx
->Accum
.ClearColor
);
261 /* Note that clearValue is 32 bits but the accum buffer will
262 * typically be 64bpp...
264 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, rb
->surface
, clearValue
);
268 /* XXX point framebuffer.cbufs[0] at the accum buffer */
270 ctx
->DrawBuffer
->_Xmin
,
271 ctx
->DrawBuffer
->_Xmin
,
272 ctx
->DrawBuffer
->_Xmax
,
273 ctx
->DrawBuffer
->_Ymax
,
274 GL_TRUE
, GL_FALSE
, GL_FALSE
);
280 clear_depth_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
282 if (!ctx
->Scissor
.Enabled
&&
283 !is_depth_stencil_format(rb
->surface
->format
)) {
284 /* clear whole depth buffer w/out masking */
285 GLuint clearValue
= depth_value(rb
->surface
->format
, ctx
->Depth
.Clear
);
286 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, rb
->surface
, clearValue
);
289 /* masking or scissoring or combined z/stencil buffer */
291 ctx
->DrawBuffer
->_Xmin
,
292 ctx
->DrawBuffer
->_Xmin
,
293 ctx
->DrawBuffer
->_Xmax
,
294 ctx
->DrawBuffer
->_Ymax
,
295 GL_FALSE
, GL_TRUE
, GL_FALSE
);
301 clear_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
303 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
304 GLboolean maskStencil
= ctx
->Stencil
.WriteMask
[0] != stencilMax
;
306 if (!maskStencil
&& !ctx
->Scissor
.Enabled
&&
307 !is_depth_stencil_format(rb
->surface
->format
)) {
308 /* clear whole stencil buffer w/out masking */
309 GLuint clearValue
= ctx
->Stencil
.Clear
;
310 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, rb
->surface
, clearValue
);
313 /* masking or scissoring */
315 ctx
->DrawBuffer
->_Xmin
,
316 ctx
->DrawBuffer
->_Xmin
,
317 ctx
->DrawBuffer
->_Xmax
,
318 ctx
->DrawBuffer
->_Ymax
,
319 GL_FALSE
, GL_FALSE
, GL_TRUE
);
325 clear_depth_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
327 const GLuint stencilMax
= 1 << rb
->StencilBits
;
328 GLboolean maskStencil
= ctx
->Stencil
.WriteMask
[0] != stencilMax
;
330 assert(is_depth_stencil_format(rb
->surface
->format
));
332 if (!maskStencil
&& !ctx
->Scissor
.Enabled
) {
333 /* clear whole buffer w/out masking */
334 GLuint clearValue
= depth_value(rb
->surface
->format
, ctx
->Depth
.Clear
);
336 switch (rb
->surface
->format
) {
337 case PIPE_FORMAT_S8_Z24
:
338 clearValue
|= ctx
->Stencil
.Clear
<< 24;
341 case PIPE_FORMAT_Z24_S8
:
342 clearValue
= (clearValue
<< 8) | clearVal
;
349 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, rb
->surface
, clearValue
);
352 /* masking or scissoring */
354 ctx
->DrawBuffer
->_Xmin
,
355 ctx
->DrawBuffer
->_Xmin
,
356 ctx
->DrawBuffer
->_Xmax
,
357 ctx
->DrawBuffer
->_Ymax
,
358 GL_FALSE
, GL_TRUE
, GL_TRUE
);
365 * Called via ctx->Driver.Clear()
366 * XXX: doesn't pick up the differences between front/back/left/right
367 * clears. Need to sort that out...
369 static void st_clear(GLcontext
*ctx
, GLbitfield mask
)
371 static const GLbitfield BUFFER_BITS_DS
372 = (BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
);
373 struct st_context
*st
= ctx
->st
;
374 struct gl_renderbuffer
*depthRb
375 = ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
376 struct gl_renderbuffer
*stencilRb
377 = ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
379 /* This makes sure the softpipe has the latest scissor, etc values */
380 st_validate_state( st
);
384 * If we're going to use clear_with_quad() for any reason, use it to
385 * clear as many other buffers as possible.
386 * As it is now, we sometimes call clear_with_quad() three times to clear
387 * color/depth/stencil individually...
390 if (mask
& BUFFER_BITS_COLOR
) {
392 for (b
= 0; b
< BUFFER_COUNT
; b
++) {
393 if (BUFFER_BITS_COLOR
& mask
& (1 << b
)) {
394 clear_color_buffer(ctx
,
395 ctx
->DrawBuffer
->Attachment
[b
].Renderbuffer
);
400 if (mask
& BUFFER_BIT_ACCUM
) {
401 clear_accum_buffer(ctx
,
402 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
405 if ((mask
& BUFFER_BITS_DS
) == BUFFER_BITS_DS
&& depthRb
== stencilRb
) {
406 /* clearing combined depth + stencil */
407 clear_depth_stencil_buffer(ctx
, depthRb
);
410 /* separate depth/stencil clears */
411 if (mask
& BUFFER_BIT_DEPTH
) {
412 clear_depth_buffer(ctx
, depthRb
);
414 if (mask
& BUFFER_BIT_STENCIL
) {
415 clear_stencil_buffer(ctx
, stencilRb
);
421 void st_init_cb_clear( struct st_context
*st
)
423 struct dd_function_table
*functions
= &st
->ctx
->Driver
;
425 functions
->Clear
= st_clear
;
429 void st_destroy_cb_clear( struct st_context
*st
)