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 struct pipe_context
*pipe
= st
->pipe
;
100 if (st
->clear
.vert_shader
.tokens
) {
101 FREE((void *) st
->clear
.vert_shader
.tokens
);
102 st
->clear
.vert_shader
.tokens
= NULL
;
105 if (st
->clear
.frag_shader
.tokens
) {
106 FREE((void *) st
->clear
.frag_shader
.tokens
);
107 st
->clear
.frag_shader
.tokens
= NULL
;
111 cso_delete_fragment_shader(st
->cso_context
, st
->clear
.fs
);
115 cso_delete_vertex_shader(st
->cso_context
, st
->clear
.vs
);
118 if (st
->clear
.vbuf
) {
119 pipe_buffer_destroy(pipe
, st
->clear
.vbuf
);
120 st
->clear
.vbuf
= NULL
;
126 is_depth_stencil_format(enum pipe_format pipeFormat
)
128 switch (pipeFormat
) {
129 case PIPE_FORMAT_S8Z24_UNORM
:
130 case PIPE_FORMAT_Z24S8_UNORM
:
140 * Draw a screen-aligned quadrilateral.
141 * Coords are window coords with y=0=bottom. These coords will be transformed
142 * by the vertex shader and viewport transform (which will flip Y if needed).
145 draw_quad(GLcontext
*ctx
,
146 float x0
, float y0
, float x1
, float y1
, GLfloat z
,
147 const GLfloat color
[4])
149 struct st_context
*st
= ctx
->st
;
150 struct pipe_context
*pipe
= st
->pipe
;
154 if (!st
->clear
.vbuf
) {
155 st
->clear
.vbuf
= pipe_buffer_create(pipe
, 32, PIPE_BUFFER_USAGE_VERTEX
,
156 sizeof(st
->clear
.vertices
));
160 st
->clear
.vertices
[0][0][0] = x0
;
161 st
->clear
.vertices
[0][0][1] = y0
;
163 st
->clear
.vertices
[1][0][0] = x1
;
164 st
->clear
.vertices
[1][0][1] = y0
;
166 st
->clear
.vertices
[2][0][0] = x1
;
167 st
->clear
.vertices
[2][0][1] = y1
;
169 st
->clear
.vertices
[3][0][0] = x0
;
170 st
->clear
.vertices
[3][0][1] = y1
;
172 /* same for all verts: */
173 for (i
= 0; i
< 4; i
++) {
174 st
->clear
.vertices
[i
][0][2] = z
;
175 st
->clear
.vertices
[i
][0][3] = 1.0;
176 st
->clear
.vertices
[i
][1][0] = color
[0];
177 st
->clear
.vertices
[i
][1][1] = color
[1];
178 st
->clear
.vertices
[i
][1][2] = color
[2];
179 st
->clear
.vertices
[i
][1][3] = color
[3];
182 /* put vertex data into vbuf */
183 buf
= pipe_buffer_map(pipe
, st
->clear
.vbuf
, PIPE_BUFFER_USAGE_CPU_WRITE
);
184 memcpy(buf
, st
->clear
.vertices
, sizeof(st
->clear
.vertices
));
185 pipe_buffer_unmap(pipe
, st
->clear
.vbuf
);
188 util_draw_vertex_buffer(pipe
, st
->clear
.vbuf
,
189 PIPE_PRIM_TRIANGLE_FAN
,
191 2); /* attribs/vert */
197 * Do glClear by drawing a quadrilateral.
198 * The vertices of the quad will be computed from the
199 * ctx->DrawBuffer->_X/Ymin/max fields.
202 clear_with_quad(GLcontext
*ctx
,
203 GLboolean color
, GLboolean depth
, GLboolean stencil
)
205 struct st_context
*st
= ctx
->st
;
206 const GLfloat x0
= ctx
->DrawBuffer
->_Xmin
;
207 const GLfloat x1
= ctx
->DrawBuffer
->_Xmax
;
210 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
211 y0
= ctx
->DrawBuffer
->Height
- ctx
->DrawBuffer
->_Ymax
;
212 y1
= ctx
->DrawBuffer
->Height
- ctx
->DrawBuffer
->_Ymin
;
215 y0
= ctx
->DrawBuffer
->_Ymin
;
216 y1
= ctx
->DrawBuffer
->_Ymax
;
220 printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__,
221 color ? "color, " : "",
222 depth ? "depth, " : "",
223 stencil ? "stencil" : "",
228 cso_save_blend(st
->cso_context
);
229 cso_save_depth_stencil_alpha(st
->cso_context
);
230 cso_save_rasterizer(st
->cso_context
);
231 cso_save_viewport(st
->cso_context
);
232 cso_save_fragment_shader(st
->cso_context
);
233 cso_save_vertex_shader(st
->cso_context
);
235 /* blend state: RGBA masking */
237 struct pipe_blend_state blend
;
238 memset(&blend
, 0, sizeof(blend
));
239 blend
.rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
240 blend
.alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
241 blend
.rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
242 blend
.alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
244 if (ctx
->Color
.ColorMask
[0])
245 blend
.colormask
|= PIPE_MASK_R
;
246 if (ctx
->Color
.ColorMask
[1])
247 blend
.colormask
|= PIPE_MASK_G
;
248 if (ctx
->Color
.ColorMask
[2])
249 blend
.colormask
|= PIPE_MASK_B
;
250 if (ctx
->Color
.ColorMask
[3])
251 blend
.colormask
|= PIPE_MASK_A
;
252 if (st
->ctx
->Color
.DitherFlag
)
255 cso_set_blend(st
->cso_context
, &blend
);
258 /* depth_stencil state: always pass/set to ref value */
260 struct pipe_depth_stencil_alpha_state depth_stencil
;
261 memset(&depth_stencil
, 0, sizeof(depth_stencil
));
263 depth_stencil
.depth
.enabled
= 1;
264 depth_stencil
.depth
.writemask
= 1;
265 depth_stencil
.depth
.func
= PIPE_FUNC_ALWAYS
;
269 depth_stencil
.stencil
[0].enabled
= 1;
270 depth_stencil
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
271 depth_stencil
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
272 depth_stencil
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
273 depth_stencil
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
274 depth_stencil
.stencil
[0].ref_value
= ctx
->Stencil
.Clear
;
275 depth_stencil
.stencil
[0].value_mask
= 0xff;
276 depth_stencil
.stencil
[0].write_mask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
279 cso_set_depth_stencil_alpha(st
->cso_context
, &depth_stencil
);
282 cso_set_rasterizer(st
->cso_context
, &st
->clear
.raster
);
283 cso_set_viewport(st
->cso_context
, &st
->clear
.viewport
);
285 cso_set_fragment_shader_handle(st
->cso_context
, st
->clear
.fs
);
286 cso_set_vertex_shader_handle(st
->cso_context
, st
->clear
.vs
);
288 /* draw quad matching scissor rect (XXX verify coord round-off) */
289 draw_quad(ctx
, x0
, y0
, x1
, y1
, ctx
->Depth
.Clear
, ctx
->Color
.ClearColor
);
291 /* Restore pipe state */
292 cso_restore_blend(st
->cso_context
);
293 cso_restore_depth_stencil_alpha(st
->cso_context
);
294 cso_restore_rasterizer(st
->cso_context
);
295 cso_restore_viewport(st
->cso_context
);
296 cso_restore_fragment_shader(st
->cso_context
);
297 cso_restore_vertex_shader(st
->cso_context
);
302 * Determine if we need to clear the depth buffer by drawing a quad.
304 static INLINE GLboolean
305 check_clear_color_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
307 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
309 if (strb
->surface
->status
== PIPE_SURFACE_STATUS_UNDEFINED
)
312 if (ctx
->Scissor
.Enabled
)
315 if (!ctx
->Color
.ColorMask
[0] ||
316 !ctx
->Color
.ColorMask
[1] ||
317 !ctx
->Color
.ColorMask
[2] ||
318 !ctx
->Color
.ColorMask
[3])
325 static INLINE GLboolean
326 check_clear_depth_stencil_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
328 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
329 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
330 GLboolean maskStencil
331 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
333 if (strb
->surface
->status
== PIPE_SURFACE_STATUS_UNDEFINED
)
336 if (ctx
->Scissor
.Enabled
)
347 * Determine if we need to clear the depth buffer by drawing a quad.
349 static INLINE GLboolean
350 check_clear_depth_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
352 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
353 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
355 if (strb
->surface
->status
== PIPE_SURFACE_STATUS_UNDEFINED
)
358 if (ctx
->Scissor
.Enabled
)
362 strb
->surface
->status
== PIPE_SURFACE_STATUS_DEFINED
&&
363 ctx
->DrawBuffer
->Visual
.stencilBits
> 0)
371 * Determine if we need to clear the stencil buffer by drawing a quad.
373 static INLINE GLboolean
374 check_clear_stencil_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
376 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
377 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
378 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
379 const GLboolean maskStencil
380 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
382 if (strb
->surface
->status
== PIPE_SURFACE_STATUS_UNDEFINED
)
388 if (ctx
->Scissor
.Enabled
)
391 /* This is correct, but it is necessary to look at the depth clear
392 * value held in the surface when it comes time to issue the clear,
393 * rather than taking depth and stencil clear values from the
397 strb
->surface
->status
== PIPE_SURFACE_STATUS_DEFINED
&&
398 ctx
->DrawBuffer
->Visual
.depthBits
> 0)
407 clear_color_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
409 if (check_clear_color_with_quad( ctx
, rb
)) {
410 /* masking or scissoring */
411 clear_with_quad(ctx
, GL_TRUE
, GL_FALSE
, GL_FALSE
);
414 /* clear whole buffer w/out masking */
415 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
417 util_pack_color(ctx
->Color
.ClearColor
, PIPE_FORMAT_A8R8G8B8_UNORM
, &clearValue
);
418 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
424 clear_depth_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
426 if (check_clear_depth_with_quad(ctx
, rb
)) {
427 /* scissoring or we have a combined depth/stencil buffer */
428 clear_with_quad(ctx
, GL_FALSE
, GL_TRUE
, GL_FALSE
);
431 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
433 /* simple clear of whole buffer */
434 uint clearValue
= util_pack_z(strb
->surface
->format
, ctx
->Depth
.Clear
);
435 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
441 clear_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
443 if (check_clear_stencil_with_quad(ctx
, rb
)) {
444 /* masking or scissoring or combined depth/stencil buffer */
445 clear_with_quad(ctx
, GL_FALSE
, GL_FALSE
, GL_TRUE
);
448 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
450 /* simple clear of whole buffer */
451 GLuint clearValue
= ctx
->Stencil
.Clear
;
453 switch (strb
->surface
->format
) {
454 case PIPE_FORMAT_S8Z24_UNORM
:
458 ; /* no-op, stencil value is in least significant bits */
461 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
467 clear_depth_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
470 if (check_clear_depth_stencil_with_quad(ctx
, rb
)) {
471 /* masking or scissoring */
472 clear_with_quad(ctx
, GL_FALSE
, GL_TRUE
, GL_TRUE
);
475 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
477 /* clear whole buffer w/out masking */
478 GLuint clearValue
= util_pack_z(strb
->surface
->format
, ctx
->Depth
.Clear
);
480 switch (strb
->surface
->format
) {
481 case PIPE_FORMAT_S8Z24_UNORM
:
482 clearValue
|= ctx
->Stencil
.Clear
<< 24;
484 case PIPE_FORMAT_Z24S8_UNORM
:
485 clearValue
|= ctx
->Stencil
.Clear
;
491 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
498 * Called via ctx->Driver.Clear()
499 * XXX: doesn't pick up the differences between front/back/left/right
500 * clears. Need to sort that out...
502 static void st_clear(GLcontext
*ctx
, GLbitfield mask
)
504 static const GLbitfield BUFFER_BITS_DS
505 = (BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
);
506 struct st_context
*st
= ctx
->st
;
507 struct gl_renderbuffer
*depthRb
508 = ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
509 struct gl_renderbuffer
*stencilRb
510 = ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
511 GLbitfield cmask
= mask
& BUFFER_BITS_COLOR
;
513 /* This makes sure the softpipe has the latest scissor, etc values */
514 st_validate_state( st
);
518 * If we're going to use clear_with_quad() for any reason, use it to
519 * clear as many other buffers as possible.
520 * As it is now, we sometimes call clear_with_quad() three times to clear
521 * color/depth/stencil individually...
526 for (b
= 0; cmask
; b
++) {
527 if (cmask
& (1 << b
)) {
528 struct gl_renderbuffer
*rb
529 = ctx
->DrawBuffer
->Attachment
[b
].Renderbuffer
;
531 clear_color_buffer(ctx
, rb
);
532 cmask
&= ~(1 << b
); /* turn off bit */
534 assert(b
< BUFFER_COUNT
);
538 if (mask
& BUFFER_BIT_ACCUM
) {
539 st_clear_accum_buffer(ctx
,
540 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
543 if ((mask
& BUFFER_BITS_DS
) == BUFFER_BITS_DS
&& depthRb
== stencilRb
) {
544 /* clearing combined depth + stencil */
545 clear_depth_stencil_buffer(ctx
, depthRb
);
548 /* separate depth/stencil clears */
549 if (mask
& BUFFER_BIT_DEPTH
) {
550 clear_depth_buffer(ctx
, depthRb
);
552 if (mask
& BUFFER_BIT_STENCIL
) {
553 clear_stencil_buffer(ctx
, stencilRb
);
559 void st_init_clear_functions(struct dd_function_table
*functions
)
561 functions
->Clear
= st_clear
;