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"
58 /* XXX for testing draw module vertex passthrough: */
59 /* XXX this hack is broken now */
60 #define TEST_DRAW_PASSTHROUGH 0
64 st_destroy_clear(struct st_context
*st
)
66 struct pipe_context
*pipe
= st
->pipe
;
69 pipe
->delete_fs_state(pipe
, st
->clear
.fs
);
73 pipe
->delete_vs_state(pipe
, st
->clear
.vs
);
77 pipe
->winsys
->buffer_destroy(pipe
->winsys
, st
->clear
.vbuf
);
78 st
->clear
.vbuf
= NULL
;
84 is_depth_stencil_format(enum pipe_format pipeFormat
)
87 case PIPE_FORMAT_S8Z24_UNORM
:
88 case PIPE_FORMAT_Z24S8_UNORM
:
98 * Draw a screen-aligned quadrilateral.
99 * Coords are window coords with y=0=bottom. These coords will be transformed
100 * by the vertex shader and viewport transform (which will flip Y if needed).
103 draw_quad(GLcontext
*ctx
,
104 float x0
, float y0
, float x1
, float y1
, GLfloat z
,
105 const GLfloat color
[4])
107 struct st_context
*st
= ctx
->st
;
108 struct pipe_context
*pipe
= st
->pipe
;
112 if (!st
->clear
.vbuf
) {
113 st
->clear
.vbuf
= pipe
->winsys
->buffer_create(pipe
->winsys
, 32,
114 PIPE_BUFFER_USAGE_VERTEX
,
115 sizeof(st
->clear
.vertices
));
119 st
->clear
.vertices
[0][0][0] = x0
;
120 st
->clear
.vertices
[0][0][1] = y0
;
122 st
->clear
.vertices
[1][0][0] = x1
;
123 st
->clear
.vertices
[1][0][1] = y0
;
125 st
->clear
.vertices
[2][0][0] = x1
;
126 st
->clear
.vertices
[2][0][1] = y1
;
128 st
->clear
.vertices
[3][0][0] = x0
;
129 st
->clear
.vertices
[3][0][1] = y1
;
131 /* same for all verts: */
132 for (i
= 0; i
< 4; i
++) {
133 st
->clear
.vertices
[i
][0][2] = z
;
134 st
->clear
.vertices
[i
][0][3] = 1.0;
135 st
->clear
.vertices
[i
][1][0] = color
[0];
136 st
->clear
.vertices
[i
][1][1] = color
[1];
137 st
->clear
.vertices
[i
][1][2] = color
[2];
138 st
->clear
.vertices
[i
][1][3] = color
[3];
141 /* put vertex data into vbuf */
142 buf
= pipe
->winsys
->buffer_map(pipe
->winsys
, st
->clear
.vbuf
,
143 PIPE_BUFFER_USAGE_CPU_WRITE
);
144 memcpy(buf
, st
->clear
.vertices
, sizeof(st
->clear
.vertices
));
145 pipe
->winsys
->buffer_unmap(pipe
->winsys
, st
->clear
.vbuf
);
148 util_draw_vertex_buffer(pipe
, st
->clear
.vbuf
,
149 PIPE_PRIM_TRIANGLE_FAN
,
151 2); /* attribs/vert */
157 * Do glClear by drawing a quadrilateral.
158 * The vertices of the quad will be computed from the
159 * ctx->DrawBuffer->_X/Ymin/max fields.
162 clear_with_quad(GLcontext
*ctx
,
163 GLboolean color
, GLboolean depth
, GLboolean stencil
)
165 struct st_context
*st
= ctx
->st
;
166 struct pipe_context
*pipe
= st
->pipe
;
167 const GLfloat x0
= ctx
->DrawBuffer
->_Xmin
;
168 const GLfloat x1
= ctx
->DrawBuffer
->_Xmax
;
171 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
172 y0
= ctx
->DrawBuffer
->Height
- ctx
->DrawBuffer
->_Ymax
;
173 y1
= ctx
->DrawBuffer
->Height
- ctx
->DrawBuffer
->_Ymin
;
176 y0
= ctx
->DrawBuffer
->_Ymin
;
177 y1
= ctx
->DrawBuffer
->_Ymax
;
181 printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__,
182 color ? "color, " : "",
183 depth ? "depth, " : "",
184 stencil ? "stencil" : "",
189 cso_save_blend(st
->cso_context
);
190 cso_save_depth_stencil_alpha(st
->cso_context
);
191 cso_save_rasterizer(st
->cso_context
);
192 cso_save_viewport(st
->cso_context
);
194 /* blend state: RGBA masking */
196 struct pipe_blend_state blend
;
197 memset(&blend
, 0, sizeof(blend
));
198 blend
.rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
199 blend
.alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
200 blend
.rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
201 blend
.alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
203 if (ctx
->Color
.ColorMask
[0])
204 blend
.colormask
|= PIPE_MASK_R
;
205 if (ctx
->Color
.ColorMask
[1])
206 blend
.colormask
|= PIPE_MASK_G
;
207 if (ctx
->Color
.ColorMask
[2])
208 blend
.colormask
|= PIPE_MASK_B
;
209 if (ctx
->Color
.ColorMask
[3])
210 blend
.colormask
|= PIPE_MASK_A
;
211 if (st
->ctx
->Color
.DitherFlag
)
214 cso_set_blend(st
->cso_context
, &blend
);
217 /* depth_stencil state: always pass/set to ref value */
219 struct pipe_depth_stencil_alpha_state depth_stencil
;
220 memset(&depth_stencil
, 0, sizeof(depth_stencil
));
222 depth_stencil
.depth
.enabled
= 1;
223 depth_stencil
.depth
.writemask
= 1;
224 depth_stencil
.depth
.func
= PIPE_FUNC_ALWAYS
;
228 depth_stencil
.stencil
[0].enabled
= 1;
229 depth_stencil
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
230 depth_stencil
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
231 depth_stencil
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
232 depth_stencil
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
233 depth_stencil
.stencil
[0].ref_value
= ctx
->Stencil
.Clear
;
234 depth_stencil
.stencil
[0].value_mask
= 0xff;
235 depth_stencil
.stencil
[0].write_mask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
238 cso_set_depth_stencil_alpha(st
->cso_context
, &depth_stencil
);
241 /* rasterizer state: bypass clipping */
243 struct pipe_rasterizer_state raster
;
244 memset(&raster
, 0, sizeof(raster
));
245 raster
.bypass_clipping
= 1;
246 #if TEST_DRAW_PASSTHROUGH
247 raster
.bypass_vs
= 1;
249 cso_set_rasterizer(st
->cso_context
, &raster
);
252 /* fragment shader state: color pass-through program */
254 st
->clear
.fs
= util_make_fragment_passthrough_shader(pipe
);
256 pipe
->bind_fs_state(pipe
, st
->clear
.fs
);
259 #if !TEST_DRAW_PASSTHROUGH
260 /* vertex shader state: color/position pass-through */
262 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
263 TGSI_SEMANTIC_COLOR
};
264 const uint semantic_indexes
[] = { 0, 0 };
265 st
->clear
.vs
= util_make_vertex_passthrough_shader(pipe
, 2,
269 pipe
->bind_vs_state(pipe
, st
->clear
.vs
);
272 #if !TEST_DRAW_PASSTHROUGH
273 /* viewport state: viewport matching window dims */
275 const float width
= ctx
->DrawBuffer
->Width
;
276 const float height
= ctx
->DrawBuffer
->Height
;
277 struct pipe_viewport_state vp
;
278 vp
.scale
[0] = 0.5 * width
;
279 vp
.scale
[1] = -0.5 * height
;
282 vp
.translate
[0] = 0.5 * width
;
283 vp
.translate
[1] = 0.5 * height
;
284 vp
.translate
[2] = 0.0;
285 vp
.translate
[3] = 0.0;
286 cso_set_viewport(st
->cso_context
, &vp
);
290 /* draw quad matching scissor rect (XXX verify coord round-off) */
291 draw_quad(ctx
, x0
, y0
, x1
, y1
, ctx
->Depth
.Clear
, ctx
->Color
.ClearColor
);
293 /* Restore pipe state */
294 cso_restore_blend(st
->cso_context
);
295 cso_restore_depth_stencil_alpha(st
->cso_context
);
296 cso_restore_rasterizer(st
->cso_context
);
297 cso_restore_viewport(st
->cso_context
);
298 /* these don't go through cso yet */
299 pipe
->bind_fs_state(pipe
, st
->fp
->driver_shader
);
300 pipe
->bind_vs_state(pipe
, st
->vp
->driver_shader
);
305 * Determine if we need to clear the depth buffer by drawing a quad.
307 static INLINE GLboolean
308 check_clear_color_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
310 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
312 if (strb
->surface
->status
== PIPE_SURFACE_STATUS_UNDEFINED
)
315 if (ctx
->Scissor
.Enabled
)
318 if (!ctx
->Color
.ColorMask
[0] ||
319 !ctx
->Color
.ColorMask
[1] ||
320 !ctx
->Color
.ColorMask
[2] ||
321 !ctx
->Color
.ColorMask
[3])
328 static INLINE GLboolean
329 check_clear_depth_stencil_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
331 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
332 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
333 GLboolean maskStencil
334 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
336 if (strb
->surface
->status
== PIPE_SURFACE_STATUS_UNDEFINED
)
339 if (ctx
->Scissor
.Enabled
)
350 * Determine if we need to clear the depth buffer by drawing a quad.
352 static INLINE GLboolean
353 check_clear_depth_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
355 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
356 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
358 if (strb
->surface
->status
== PIPE_SURFACE_STATUS_UNDEFINED
)
361 if (ctx
->Scissor
.Enabled
)
365 strb
->surface
->status
== PIPE_SURFACE_STATUS_DEFINED
&&
366 ctx
->DrawBuffer
->Visual
.stencilBits
> 0)
374 * Determine if we need to clear the stencil buffer by drawing a quad.
376 static INLINE GLboolean
377 check_clear_stencil_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
379 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
380 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
381 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
382 const GLboolean maskStencil
383 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
385 if (strb
->surface
->status
== PIPE_SURFACE_STATUS_UNDEFINED
)
391 if (ctx
->Scissor
.Enabled
)
394 /* This is correct, but it is necessary to look at the depth clear
395 * value held in the surface when it comes time to issue the clear,
396 * rather than taking depth and stencil clear values from the
400 strb
->surface
->status
== PIPE_SURFACE_STATUS_DEFINED
&&
401 ctx
->DrawBuffer
->Visual
.depthBits
> 0)
410 clear_color_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
412 if (check_clear_color_with_quad( ctx
, rb
)) {
413 /* masking or scissoring */
414 clear_with_quad(ctx
, GL_TRUE
, GL_FALSE
, GL_FALSE
);
417 /* clear whole buffer w/out masking */
418 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
420 util_pack_color(ctx
->Color
.ClearColor
, strb
->surface
->format
, &clearValue
);
421 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
427 clear_depth_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
429 if (check_clear_depth_with_quad(ctx
, rb
)) {
430 /* scissoring or we have a combined depth/stencil buffer */
431 clear_with_quad(ctx
, GL_FALSE
, GL_TRUE
, GL_FALSE
);
434 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
436 /* simple clear of whole buffer */
437 uint clearValue
= util_pack_z(strb
->surface
->format
, ctx
->Depth
.Clear
);
438 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
444 clear_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
446 if (check_clear_stencil_with_quad(ctx
, rb
)) {
447 /* masking or scissoring or combined depth/stencil buffer */
448 clear_with_quad(ctx
, GL_FALSE
, GL_FALSE
, GL_TRUE
);
451 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
453 /* simple clear of whole buffer */
454 GLuint clearValue
= ctx
->Stencil
.Clear
;
456 switch (strb
->surface
->format
) {
457 case PIPE_FORMAT_S8Z24_UNORM
:
461 ; /* no-op, stencil value is in least significant bits */
464 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
470 clear_depth_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
473 if (check_clear_depth_stencil_with_quad(ctx
, rb
)) {
474 /* masking or scissoring */
475 clear_with_quad(ctx
, GL_FALSE
, GL_TRUE
, GL_TRUE
);
478 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
480 /* clear whole buffer w/out masking */
481 GLuint clearValue
= util_pack_z(strb
->surface
->format
, ctx
->Depth
.Clear
);
483 switch (strb
->surface
->format
) {
484 case PIPE_FORMAT_S8Z24_UNORM
:
485 clearValue
|= ctx
->Stencil
.Clear
<< 24;
487 case PIPE_FORMAT_Z24S8_UNORM
:
488 clearValue
|= ctx
->Stencil
.Clear
;
494 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
501 * Called via ctx->Driver.Clear()
502 * XXX: doesn't pick up the differences between front/back/left/right
503 * clears. Need to sort that out...
505 static void st_clear(GLcontext
*ctx
, GLbitfield mask
)
507 static const GLbitfield BUFFER_BITS_DS
508 = (BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
);
509 struct st_context
*st
= ctx
->st
;
510 struct gl_renderbuffer
*depthRb
511 = ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
512 struct gl_renderbuffer
*stencilRb
513 = ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
514 GLbitfield cmask
= mask
& BUFFER_BITS_COLOR
;
516 /* This makes sure the softpipe has the latest scissor, etc values */
517 st_validate_state( st
);
521 * If we're going to use clear_with_quad() for any reason, use it to
522 * clear as many other buffers as possible.
523 * As it is now, we sometimes call clear_with_quad() three times to clear
524 * color/depth/stencil individually...
529 for (b
= 0; cmask
; b
++) {
530 if (cmask
& (1 << b
)) {
531 struct gl_renderbuffer
*rb
532 = ctx
->DrawBuffer
->Attachment
[b
].Renderbuffer
;
534 clear_color_buffer(ctx
, rb
);
535 cmask
&= ~(1 << b
); /* turn off bit */
537 assert(b
< BUFFER_COUNT
);
541 if (mask
& BUFFER_BIT_ACCUM
) {
542 st_clear_accum_buffer(ctx
,
543 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
546 if ((mask
& BUFFER_BITS_DS
) == BUFFER_BITS_DS
&& depthRb
== stencilRb
) {
547 /* clearing combined depth + stencil */
548 clear_depth_stencil_buffer(ctx
, depthRb
);
551 /* separate depth/stencil clears */
552 if (mask
& BUFFER_BIT_DEPTH
) {
553 clear_depth_buffer(ctx
, depthRb
);
555 if (mask
& BUFFER_BIT_STENCIL
) {
556 clear_stencil_buffer(ctx
, stencilRb
);
562 void st_init_clear_functions(struct dd_function_table
*functions
)
564 functions
->Clear
= st_clear
;