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>
34 #include "main/glheader.h"
35 #include "main/macros.h"
36 #include "shader/prog_instruction.h"
38 #include "st_context.h"
39 #include "st_cb_accum.h"
40 #include "st_cb_clear.h"
41 #include "st_cb_fbo.h"
43 #include "st_program.h"
44 #include "st_public.h"
45 #include "st_mesa_to_tgsi.h"
47 #include "pipe/p_context.h"
48 #include "pipe/p_state.h"
49 #include "pipe/p_defines.h"
50 #include "pipe/p_winsys.h"
51 #include "util/u_pack_color.h"
52 #include "util/u_simple_shaders.h"
53 #include "util/u_draw_quad.h"
55 #include "cso_cache/cso_context.h"
59 st_destroy_clear(struct st_context
*st
)
61 struct pipe_context
*pipe
= st
->pipe
;
64 pipe
->delete_fs_state(pipe
, st
->clear
.fs
);
68 pipe
->delete_vs_state(pipe
, st
->clear
.vs
);
72 pipe
->winsys
->buffer_destroy(pipe
->winsys
, st
->clear
.vbuf
);
73 st
->clear
.vbuf
= NULL
;
79 is_depth_stencil_format(enum pipe_format pipeFormat
)
82 case PIPE_FORMAT_S8Z24_UNORM
:
83 case PIPE_FORMAT_Z24S8_UNORM
:
93 * Draw a screen-aligned quadrilateral.
94 * Coords are window coords with y=0=bottom. These coords will be transformed
95 * by the vertex shader and viewport transform (which will flip Y if needed).
98 draw_quad(GLcontext
*ctx
,
99 float x0
, float y0
, float x1
, float y1
, GLfloat z
,
100 const GLfloat color
[4])
102 struct st_context
*st
= ctx
->st
;
103 struct pipe_context
*pipe
= st
->pipe
;
107 if (!st
->clear
.vbuf
) {
108 st
->clear
.vbuf
= pipe
->winsys
->buffer_create(pipe
->winsys
, 32,
109 PIPE_BUFFER_USAGE_VERTEX
,
110 sizeof(st
->clear
.vertices
));
114 st
->clear
.vertices
[0][0][0] = x0
;
115 st
->clear
.vertices
[0][0][1] = y0
;
117 st
->clear
.vertices
[1][0][0] = x1
;
118 st
->clear
.vertices
[1][0][1] = y0
;
120 st
->clear
.vertices
[2][0][0] = x1
;
121 st
->clear
.vertices
[2][0][1] = y1
;
123 st
->clear
.vertices
[3][0][0] = x0
;
124 st
->clear
.vertices
[3][0][1] = y1
;
126 /* same for all verts: */
127 for (i
= 0; i
< 4; i
++) {
128 st
->clear
.vertices
[i
][0][2] = z
;
129 st
->clear
.vertices
[i
][0][3] = 1.0;
130 st
->clear
.vertices
[i
][1][0] = color
[0];
131 st
->clear
.vertices
[i
][1][1] = color
[1];
132 st
->clear
.vertices
[i
][1][2] = color
[2];
133 st
->clear
.vertices
[i
][1][3] = color
[3];
136 /* put vertex data into vbuf */
137 buf
= pipe
->winsys
->buffer_map(pipe
->winsys
, st
->clear
.vbuf
,
138 PIPE_BUFFER_USAGE_CPU_WRITE
);
139 memcpy(buf
, st
->clear
.vertices
, sizeof(st
->clear
.vertices
));
140 pipe
->winsys
->buffer_unmap(pipe
->winsys
, st
->clear
.vbuf
);
143 util_draw_vertex_buffer(pipe
, st
->clear
.vbuf
,
144 PIPE_PRIM_TRIANGLE_FAN
,
146 2); /* attribs/vert */
152 * Do glClear by drawing a quadrilateral.
153 * The vertices of the quad will be computed from the
154 * ctx->DrawBuffer->_X/Ymin/max fields.
157 clear_with_quad(GLcontext
*ctx
,
158 GLboolean color
, GLboolean depth
, GLboolean stencil
)
160 struct st_context
*st
= ctx
->st
;
161 struct pipe_context
*pipe
= st
->pipe
;
162 const GLfloat x0
= ctx
->DrawBuffer
->_Xmin
;
163 const GLfloat x1
= ctx
->DrawBuffer
->_Xmax
;
166 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
167 y0
= ctx
->DrawBuffer
->Height
- ctx
->DrawBuffer
->_Ymax
;
168 y1
= ctx
->DrawBuffer
->Height
- ctx
->DrawBuffer
->_Ymin
;
171 y0
= ctx
->DrawBuffer
->_Ymin
;
172 y1
= ctx
->DrawBuffer
->_Ymax
;
176 printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__,
177 color ? "color, " : "",
178 depth ? "depth, " : "",
179 stencil ? "stencil" : "",
184 cso_save_blend(st
->cso_context
);
185 cso_save_depth_stencil_alpha(st
->cso_context
);
186 cso_save_rasterizer(st
->cso_context
);
187 cso_save_viewport(st
->cso_context
);
189 /* blend state: RGBA masking */
191 struct pipe_blend_state blend
;
192 memset(&blend
, 0, sizeof(blend
));
193 blend
.rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
194 blend
.alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
195 blend
.rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
196 blend
.alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
198 if (ctx
->Color
.ColorMask
[0])
199 blend
.colormask
|= PIPE_MASK_R
;
200 if (ctx
->Color
.ColorMask
[1])
201 blend
.colormask
|= PIPE_MASK_G
;
202 if (ctx
->Color
.ColorMask
[2])
203 blend
.colormask
|= PIPE_MASK_B
;
204 if (ctx
->Color
.ColorMask
[3])
205 blend
.colormask
|= PIPE_MASK_A
;
206 if (st
->ctx
->Color
.DitherFlag
)
209 cso_set_blend(st
->cso_context
, &blend
);
212 /* depth_stencil state: always pass/set to ref value */
214 struct pipe_depth_stencil_alpha_state depth_stencil
;
215 memset(&depth_stencil
, 0, sizeof(depth_stencil
));
217 depth_stencil
.depth
.enabled
= 1;
218 depth_stencil
.depth
.writemask
= 1;
219 depth_stencil
.depth
.func
= PIPE_FUNC_ALWAYS
;
223 depth_stencil
.stencil
[0].enabled
= 1;
224 depth_stencil
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
225 depth_stencil
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
226 depth_stencil
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
227 depth_stencil
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
228 depth_stencil
.stencil
[0].ref_value
= ctx
->Stencil
.Clear
;
229 depth_stencil
.stencil
[0].value_mask
= 0xff;
230 depth_stencil
.stencil
[0].write_mask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
233 cso_set_depth_stencil_alpha(st
->cso_context
, &depth_stencil
);
236 /* rasterizer state: bypass clipping */
238 struct pipe_rasterizer_state raster
;
239 memset(&raster
, 0, sizeof(raster
));
240 raster
.bypass_clipping
= 1;
241 cso_set_rasterizer(st
->cso_context
, &raster
);
244 /* fragment shader state: color pass-through program */
246 st
->clear
.fs
= util_make_fragment_passthrough_shader(pipe
, &st
->clear
.frag_shader
);
248 pipe
->bind_fs_state(pipe
, st
->clear
.fs
);
251 /* vertex shader state: color/position pass-through */
253 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
254 TGSI_SEMANTIC_COLOR
};
255 const uint semantic_indexes
[] = { 0, 0 };
256 st
->clear
.vs
= util_make_vertex_passthrough_shader(pipe
, 2,
259 &st
->clear
.vert_shader
);
261 pipe
->bind_vs_state(pipe
, st
->clear
.vs
);
263 /* viewport state: identity since we're drawing in window coords */
265 struct pipe_viewport_state vp
;
270 vp
.translate
[0] = 0.0;
271 vp
.translate
[1] = 0.0;
272 vp
.translate
[2] = 0.0;
273 vp
.translate
[3] = 0.0;
274 cso_set_viewport(st
->cso_context
, &vp
);
277 /* draw quad matching scissor rect (XXX verify coord round-off) */
278 draw_quad(ctx
, x0
, y0
, x1
, y1
, ctx
->Depth
.Clear
, ctx
->Color
.ClearColor
);
280 /* Restore pipe state */
281 cso_restore_blend(st
->cso_context
);
282 cso_restore_depth_stencil_alpha(st
->cso_context
);
283 cso_restore_rasterizer(st
->cso_context
);
284 cso_restore_viewport(st
->cso_context
);
285 /* these don't go through cso yet */
286 pipe
->bind_fs_state(pipe
, st
->fp
->driver_shader
);
287 pipe
->bind_vs_state(pipe
, st
->vp
->driver_shader
);
292 * Determine if we need to clear the depth buffer by drawing a quad.
294 static INLINE GLboolean
295 check_clear_color_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
297 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
299 if (strb
->surface
->status
== PIPE_SURFACE_STATUS_UNDEFINED
)
302 if (ctx
->Scissor
.Enabled
)
305 if (!ctx
->Color
.ColorMask
[0] ||
306 !ctx
->Color
.ColorMask
[1] ||
307 !ctx
->Color
.ColorMask
[2] ||
308 !ctx
->Color
.ColorMask
[3])
315 static INLINE GLboolean
316 check_clear_depth_stencil_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
318 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
319 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
320 GLboolean maskStencil
321 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
323 if (strb
->surface
->status
== PIPE_SURFACE_STATUS_UNDEFINED
)
326 if (ctx
->Scissor
.Enabled
)
337 * Determine if we need to clear the depth buffer by drawing a quad.
339 static INLINE GLboolean
340 check_clear_depth_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
342 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
343 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
345 if (strb
->surface
->status
== PIPE_SURFACE_STATUS_UNDEFINED
)
348 if (ctx
->Scissor
.Enabled
)
352 strb
->surface
->status
== PIPE_SURFACE_STATUS_DEFINED
&&
353 ctx
->DrawBuffer
->Visual
.stencilBits
> 0)
361 * Determine if we need to clear the stencil buffer by drawing a quad.
363 static INLINE GLboolean
364 check_clear_stencil_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
366 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
367 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
368 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
369 const GLboolean maskStencil
370 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
372 if (strb
->surface
->status
== PIPE_SURFACE_STATUS_UNDEFINED
)
378 if (ctx
->Scissor
.Enabled
)
381 /* This is correct, but it is necessary to look at the depth clear
382 * value held in the surface when it comes time to issue the clear,
383 * rather than taking depth and stencil clear values from the
387 strb
->surface
->status
== PIPE_SURFACE_STATUS_DEFINED
&&
388 ctx
->DrawBuffer
->Visual
.depthBits
> 0)
397 clear_color_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
399 if (check_clear_color_with_quad( ctx
, rb
)) {
400 /* masking or scissoring */
401 clear_with_quad(ctx
, GL_TRUE
, GL_FALSE
, GL_FALSE
);
404 /* clear whole buffer w/out masking */
405 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
407 util_pack_color(ctx
->Color
.ClearColor
, strb
->surface
->format
, &clearValue
);
408 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
414 clear_depth_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
416 if (check_clear_depth_with_quad(ctx
, rb
)) {
417 /* scissoring or we have a combined depth/stencil buffer */
418 clear_with_quad(ctx
, GL_FALSE
, GL_TRUE
, GL_FALSE
);
421 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
423 /* simple clear of whole buffer */
424 uint clearValue
= util_pack_z(strb
->surface
->format
, ctx
->Depth
.Clear
);
425 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
431 clear_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
433 if (check_clear_stencil_with_quad(ctx
, rb
)) {
434 /* masking or scissoring or combined depth/stencil buffer */
435 clear_with_quad(ctx
, GL_FALSE
, GL_FALSE
, GL_TRUE
);
438 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
440 /* simple clear of whole buffer */
441 GLuint clearValue
= ctx
->Stencil
.Clear
;
443 switch (strb
->surface
->format
) {
444 case PIPE_FORMAT_S8Z24_UNORM
:
448 ; /* no-op, stencil value is in least significant bits */
451 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
457 clear_depth_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
460 if (check_clear_depth_stencil_with_quad(ctx
, rb
)) {
461 /* masking or scissoring */
462 clear_with_quad(ctx
, GL_FALSE
, GL_TRUE
, GL_TRUE
);
465 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
467 /* clear whole buffer w/out masking */
468 GLuint clearValue
= util_pack_z(strb
->surface
->format
, ctx
->Depth
.Clear
);
470 switch (strb
->surface
->format
) {
471 case PIPE_FORMAT_S8Z24_UNORM
:
472 clearValue
|= ctx
->Stencil
.Clear
<< 24;
474 case PIPE_FORMAT_Z24S8_UNORM
:
475 clearValue
|= ctx
->Stencil
.Clear
;
481 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
488 * Called via ctx->Driver.Clear()
489 * XXX: doesn't pick up the differences between front/back/left/right
490 * clears. Need to sort that out...
492 static void st_clear(GLcontext
*ctx
, GLbitfield mask
)
494 static const GLbitfield BUFFER_BITS_DS
495 = (BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
);
496 struct st_context
*st
= ctx
->st
;
497 struct gl_renderbuffer
*depthRb
498 = ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
499 struct gl_renderbuffer
*stencilRb
500 = ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
501 GLbitfield cmask
= mask
& BUFFER_BITS_COLOR
;
503 /* This makes sure the softpipe has the latest scissor, etc values */
504 st_validate_state( st
);
508 * If we're going to use clear_with_quad() for any reason, use it to
509 * clear as many other buffers as possible.
510 * As it is now, we sometimes call clear_with_quad() three times to clear
511 * color/depth/stencil individually...
516 for (b
= 0; cmask
; b
++) {
517 if (cmask
& (1 << b
)) {
518 struct gl_renderbuffer
*rb
519 = ctx
->DrawBuffer
->Attachment
[b
].Renderbuffer
;
521 clear_color_buffer(ctx
, rb
);
522 cmask
&= ~(1 << b
); /* turn off bit */
524 assert(b
< BUFFER_COUNT
);
528 if (mask
& BUFFER_BIT_ACCUM
) {
529 st_clear_accum_buffer(ctx
,
530 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
533 if ((mask
& BUFFER_BITS_DS
) == BUFFER_BITS_DS
&& depthRb
== stencilRb
) {
534 /* clearing combined depth + stencil */
535 clear_depth_stencil_buffer(ctx
, depthRb
);
538 /* separate depth/stencil clears */
539 if (mask
& BUFFER_BIT_DEPTH
) {
540 clear_depth_buffer(ctx
, depthRb
);
542 if (mask
& BUFFER_BIT_STENCIL
) {
543 clear_stencil_buffer(ctx
, stencilRb
);
549 void st_init_clear_functions(struct dd_function_table
*functions
)
551 functions
->Clear
= st_clear
;