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"
37 #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_inlines.h"
49 #include "pipe/p_state.h"
50 #include "pipe/p_defines.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_init_clear(struct st_context
*st
)
61 struct pipe_context
*pipe
= st
->pipe
;
63 /* rasterizer state: bypass clipping */
64 memset(&st
->clear
.raster
, 0, sizeof(st
->clear
.raster
));
65 st
->clear
.raster
.gl_rasterization_rules
= 1;
66 st
->clear
.raster
.bypass_clipping
= 1;
68 /* viewport state: identity since we're drawing in window coords */
69 st
->clear
.viewport
.scale
[0] = 1.0;
70 st
->clear
.viewport
.scale
[1] = 1.0;
71 st
->clear
.viewport
.scale
[2] = 1.0;
72 st
->clear
.viewport
.scale
[3] = 1.0;
73 st
->clear
.viewport
.translate
[0] = 0.0;
74 st
->clear
.viewport
.translate
[1] = 0.0;
75 st
->clear
.viewport
.translate
[2] = 0.0;
76 st
->clear
.viewport
.translate
[3] = 0.0;
78 /* fragment shader state: color pass-through program */
80 util_make_fragment_passthrough_shader(pipe
, &st
->clear
.frag_shader
);
82 /* vertex shader state: color/position pass-through */
84 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
85 TGSI_SEMANTIC_COLOR
};
86 const uint semantic_indexes
[] = { 0, 0 };
87 st
->clear
.vs
= util_make_vertex_passthrough_shader(pipe
, 2,
90 &st
->clear
.vert_shader
);
96 st_destroy_clear(struct st_context
*st
)
98 if (st
->clear
.vert_shader
.tokens
) {
99 util_free_shader(&st
->clear
.vert_shader
);
100 st
->clear
.vert_shader
.tokens
= NULL
;
103 if (st
->clear
.frag_shader
.tokens
) {
104 util_free_shader(&st
->clear
.frag_shader
);
105 st
->clear
.frag_shader
.tokens
= NULL
;
109 cso_delete_fragment_shader(st
->cso_context
, st
->clear
.fs
);
113 cso_delete_vertex_shader(st
->cso_context
, st
->clear
.vs
);
116 if (st
->clear
.vbuf
) {
117 pipe_buffer_reference(&st
->clear
.vbuf
, NULL
);
118 st
->clear
.vbuf
= NULL
;
124 is_depth_stencil_format(enum pipe_format pipeFormat
)
126 switch (pipeFormat
) {
127 case PIPE_FORMAT_S8Z24_UNORM
:
128 case PIPE_FORMAT_Z24S8_UNORM
:
138 * Draw a screen-aligned quadrilateral.
139 * Coords are window coords with y=0=bottom. These coords will be transformed
140 * by the vertex shader and viewport transform (which will flip Y if needed).
143 draw_quad(GLcontext
*ctx
,
144 float x0
, float y0
, float x1
, float y1
, GLfloat z
,
145 const GLfloat color
[4])
147 struct st_context
*st
= ctx
->st
;
148 struct pipe_context
*pipe
= st
->pipe
;
149 const GLuint max_slots
= 1024 / sizeof(st
->clear
.vertices
);
152 if (st
->clear
.vbuf_slot
>= max_slots
) {
153 pipe_buffer_reference(&st
->clear
.vbuf
, NULL
);
154 st
->clear
.vbuf_slot
= 0;
157 if (!st
->clear
.vbuf
) {
158 st
->clear
.vbuf
= pipe_buffer_create(pipe
->screen
, 32, PIPE_BUFFER_USAGE_VERTEX
,
159 max_slots
* sizeof(st
->clear
.vertices
));
163 st
->clear
.vertices
[0][0][0] = x0
;
164 st
->clear
.vertices
[0][0][1] = y0
;
166 st
->clear
.vertices
[1][0][0] = x1
;
167 st
->clear
.vertices
[1][0][1] = y0
;
169 st
->clear
.vertices
[2][0][0] = x1
;
170 st
->clear
.vertices
[2][0][1] = y1
;
172 st
->clear
.vertices
[3][0][0] = x0
;
173 st
->clear
.vertices
[3][0][1] = y1
;
175 /* same for all verts: */
176 for (i
= 0; i
< 4; i
++) {
177 st
->clear
.vertices
[i
][0][2] = z
;
178 st
->clear
.vertices
[i
][0][3] = 1.0;
179 st
->clear
.vertices
[i
][1][0] = color
[0];
180 st
->clear
.vertices
[i
][1][1] = color
[1];
181 st
->clear
.vertices
[i
][1][2] = color
[2];
182 st
->clear
.vertices
[i
][1][3] = color
[3];
185 /* put vertex data into vbuf */
186 pipe_buffer_write(pipe
->screen
, st
->clear
.vbuf
,
187 st
->clear
.vbuf_slot
* sizeof(st
->clear
.vertices
),
188 sizeof(st
->clear
.vertices
),
192 util_draw_vertex_buffer(pipe
,
194 st
->clear
.vbuf_slot
* sizeof(st
->clear
.vertices
),
195 PIPE_PRIM_TRIANGLE_FAN
,
197 2); /* attribs/vert */
200 st
->clear
.vbuf_slot
++;
206 * Do glClear by drawing a quadrilateral.
207 * The vertices of the quad will be computed from the
208 * ctx->DrawBuffer->_X/Ymin/max fields.
211 clear_with_quad(GLcontext
*ctx
,
212 GLboolean color
, GLboolean depth
, GLboolean stencil
)
214 struct st_context
*st
= ctx
->st
;
215 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
216 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
219 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
220 y0
= (GLfloat
) (ctx
->DrawBuffer
->Height
- ctx
->DrawBuffer
->_Ymax
);
221 y1
= (GLfloat
) (ctx
->DrawBuffer
->Height
- ctx
->DrawBuffer
->_Ymin
);
224 y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
225 y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
229 printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__,
230 color ? "color, " : "",
231 depth ? "depth, " : "",
232 stencil ? "stencil" : "",
237 cso_save_blend(st
->cso_context
);
238 cso_save_depth_stencil_alpha(st
->cso_context
);
239 cso_save_rasterizer(st
->cso_context
);
240 cso_save_viewport(st
->cso_context
);
241 cso_save_fragment_shader(st
->cso_context
);
242 cso_save_vertex_shader(st
->cso_context
);
244 /* blend state: RGBA masking */
246 struct pipe_blend_state blend
;
247 memset(&blend
, 0, sizeof(blend
));
248 blend
.rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
249 blend
.alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
250 blend
.rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
251 blend
.alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
253 if (ctx
->Color
.ColorMask
[0])
254 blend
.colormask
|= PIPE_MASK_R
;
255 if (ctx
->Color
.ColorMask
[1])
256 blend
.colormask
|= PIPE_MASK_G
;
257 if (ctx
->Color
.ColorMask
[2])
258 blend
.colormask
|= PIPE_MASK_B
;
259 if (ctx
->Color
.ColorMask
[3])
260 blend
.colormask
|= PIPE_MASK_A
;
261 if (st
->ctx
->Color
.DitherFlag
)
264 cso_set_blend(st
->cso_context
, &blend
);
267 /* depth_stencil state: always pass/set to ref value */
269 struct pipe_depth_stencil_alpha_state depth_stencil
;
270 memset(&depth_stencil
, 0, sizeof(depth_stencil
));
272 depth_stencil
.depth
.enabled
= 1;
273 depth_stencil
.depth
.writemask
= 1;
274 depth_stencil
.depth
.func
= PIPE_FUNC_ALWAYS
;
278 depth_stencil
.stencil
[0].enabled
= 1;
279 depth_stencil
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
280 depth_stencil
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
281 depth_stencil
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
282 depth_stencil
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
283 depth_stencil
.stencil
[0].ref_value
= ctx
->Stencil
.Clear
;
284 depth_stencil
.stencil
[0].valuemask
= 0xff;
285 depth_stencil
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
288 cso_set_depth_stencil_alpha(st
->cso_context
, &depth_stencil
);
291 cso_set_rasterizer(st
->cso_context
, &st
->clear
.raster
);
292 cso_set_viewport(st
->cso_context
, &st
->clear
.viewport
);
294 cso_set_fragment_shader_handle(st
->cso_context
, st
->clear
.fs
);
295 cso_set_vertex_shader_handle(st
->cso_context
, st
->clear
.vs
);
297 /* draw quad matching scissor rect (XXX verify coord round-off) */
298 draw_quad(ctx
, x0
, y0
, x1
, y1
, (GLfloat
) ctx
->Depth
.Clear
, ctx
->Color
.ClearColor
);
300 /* Restore pipe state */
301 cso_restore_blend(st
->cso_context
);
302 cso_restore_depth_stencil_alpha(st
->cso_context
);
303 cso_restore_rasterizer(st
->cso_context
);
304 cso_restore_viewport(st
->cso_context
);
305 cso_restore_fragment_shader(st
->cso_context
);
306 cso_restore_vertex_shader(st
->cso_context
);
311 * Determine if we need to clear the depth buffer by drawing a quad.
313 static INLINE GLboolean
314 check_clear_color_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
316 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
318 if (strb
->surface
->status
== PIPE_SURFACE_STATUS_UNDEFINED
)
321 if (ctx
->Scissor
.Enabled
)
324 if (!ctx
->Color
.ColorMask
[0] ||
325 !ctx
->Color
.ColorMask
[1] ||
326 !ctx
->Color
.ColorMask
[2] ||
327 !ctx
->Color
.ColorMask
[3])
334 static INLINE GLboolean
335 check_clear_depth_stencil_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
337 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
338 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
339 GLboolean maskStencil
340 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
342 if (strb
->surface
->status
== PIPE_SURFACE_STATUS_UNDEFINED
)
345 if (ctx
->Scissor
.Enabled
)
356 * Determine if we need to clear the depth buffer by drawing a quad.
358 static INLINE GLboolean
359 check_clear_depth_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
361 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
362 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
364 if (strb
->surface
->status
== PIPE_SURFACE_STATUS_UNDEFINED
)
367 if (ctx
->Scissor
.Enabled
)
371 strb
->surface
->status
== PIPE_SURFACE_STATUS_DEFINED
&&
372 ctx
->DrawBuffer
->Visual
.stencilBits
> 0)
380 * Determine if we need to clear the stencil buffer by drawing a quad.
382 static INLINE GLboolean
383 check_clear_stencil_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
385 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
386 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
387 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
388 const GLboolean maskStencil
389 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
391 if (strb
->surface
->status
== PIPE_SURFACE_STATUS_UNDEFINED
)
397 if (ctx
->Scissor
.Enabled
)
400 /* This is correct, but it is necessary to look at the depth clear
401 * value held in the surface when it comes time to issue the clear,
402 * rather than taking depth and stencil clear values from the
406 strb
->surface
->status
== PIPE_SURFACE_STATUS_DEFINED
&&
407 ctx
->DrawBuffer
->Visual
.depthBits
> 0)
416 clear_color_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
418 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
423 if (check_clear_color_with_quad( ctx
, rb
)) {
424 /* masking or scissoring */
425 clear_with_quad(ctx
, GL_TRUE
, GL_FALSE
, GL_FALSE
);
428 /* clear whole buffer w/out masking */
430 /* NOTE: we always pass the clear color as PIPE_FORMAT_A8R8G8B8_UNORM
433 util_pack_color(ctx
->Color
.ClearColor
, PIPE_FORMAT_A8R8G8B8_UNORM
, &clearValue
);
434 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
440 clear_depth_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
442 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
447 if (check_clear_depth_with_quad(ctx
, rb
)) {
448 /* scissoring or we have a combined depth/stencil buffer */
449 clear_with_quad(ctx
, GL_FALSE
, GL_TRUE
, GL_FALSE
);
452 /* simple clear of whole buffer */
453 uint clearValue
= util_pack_z(strb
->surface
->format
, ctx
->Depth
.Clear
);
454 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
460 clear_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
462 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
467 if (check_clear_stencil_with_quad(ctx
, rb
)) {
468 /* masking or scissoring or combined depth/stencil buffer */
469 clear_with_quad(ctx
, GL_FALSE
, GL_FALSE
, GL_TRUE
);
472 /* simple clear of whole buffer */
473 GLuint clearValue
= ctx
->Stencil
.Clear
;
475 switch (strb
->surface
->format
) {
476 case PIPE_FORMAT_S8Z24_UNORM
:
480 ; /* no-op, stencil value is in least significant bits */
483 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
489 clear_depth_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
491 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
496 if (check_clear_depth_stencil_with_quad(ctx
, rb
)) {
497 /* masking or scissoring */
498 clear_with_quad(ctx
, GL_FALSE
, GL_TRUE
, GL_TRUE
);
501 /* clear whole buffer w/out masking */
502 GLuint clearValue
= util_pack_z(strb
->surface
->format
, ctx
->Depth
.Clear
);
504 switch (strb
->surface
->format
) {
505 case PIPE_FORMAT_S8Z24_UNORM
:
506 clearValue
|= ctx
->Stencil
.Clear
<< 24;
508 case PIPE_FORMAT_Z24S8_UNORM
:
509 clearValue
|= ctx
->Stencil
.Clear
;
515 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
520 void st_flush_clear( struct st_context
*st
)
522 /* Release vertex buffer to avoid synchronous rendering if we were
523 * to map it in the next frame.
525 pipe_buffer_reference(&st
->clear
.vbuf
, NULL
);
526 st
->clear
.vbuf_slot
= 0;
532 * Called via ctx->Driver.Clear()
533 * XXX: doesn't pick up the differences between front/back/left/right
534 * clears. Need to sort that out...
536 static void st_clear(GLcontext
*ctx
, GLbitfield mask
)
538 static const GLbitfield BUFFER_BITS_DS
539 = (BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
);
540 struct st_context
*st
= ctx
->st
;
541 struct gl_renderbuffer
*depthRb
542 = ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
543 struct gl_renderbuffer
*stencilRb
544 = ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
545 GLbitfield cmask
= mask
& BUFFER_BITS_COLOR
;
547 /* This makes sure the softpipe has the latest scissor, etc values */
548 st_validate_state( st
);
552 * If we're going to use clear_with_quad() for any reason, use it to
553 * clear as many other buffers as possible.
554 * As it is now, we sometimes call clear_with_quad() three times to clear
555 * color/depth/stencil individually...
560 for (b
= 0; cmask
; b
++) {
561 if (cmask
& (1 << b
)) {
562 struct gl_renderbuffer
*rb
563 = ctx
->DrawBuffer
->Attachment
[b
].Renderbuffer
;
565 clear_color_buffer(ctx
, rb
);
566 cmask
&= ~(1 << b
); /* turn off bit */
568 assert(b
< BUFFER_COUNT
);
572 if (mask
& BUFFER_BIT_ACCUM
) {
573 st_clear_accum_buffer(ctx
,
574 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
577 if ((mask
& BUFFER_BITS_DS
) == BUFFER_BITS_DS
&& depthRb
== stencilRb
) {
578 /* clearing combined depth + stencil */
579 clear_depth_stencil_buffer(ctx
, depthRb
);
582 /* separate depth/stencil clears */
583 if (mask
& BUFFER_BIT_DEPTH
) {
584 clear_depth_buffer(ctx
, depthRb
);
586 if (mask
& BUFFER_BIT_STENCIL
) {
587 clear_stencil_buffer(ctx
, stencilRb
);
593 void st_init_clear_functions(struct dd_function_table
*functions
)
595 functions
->Clear
= st_clear
;