1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * Copyright 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
31 * Keith Whitwell <keith@tungstengraphics.com>
36 #include "main/glheader.h"
37 #include "main/formats.h"
38 #include "main/macros.h"
39 #include "shader/prog_instruction.h"
40 #include "st_context.h"
42 #include "st_cb_accum.h"
43 #include "st_cb_clear.h"
44 #include "st_cb_fbo.h"
46 #include "st_program.h"
47 #include "st_public.h"
48 #include "st_mesa_to_tgsi.h"
49 #include "st_inlines.h"
51 #include "pipe/p_context.h"
52 #include "pipe/p_inlines.h"
53 #include "pipe/p_state.h"
54 #include "pipe/p_defines.h"
55 #include "util/u_pack_color.h"
56 #include "util/u_simple_shaders.h"
57 #include "util/u_draw_quad.h"
59 #include "cso_cache/cso_context.h"
63 st_init_clear(struct st_context
*st
)
65 struct pipe_context
*pipe
= st
->pipe
;
67 memset(&st
->clear
.raster
, 0, sizeof(st
->clear
.raster
));
68 st
->clear
.raster
.gl_rasterization_rules
= 1;
70 /* rasterizer state: bypass vertex shader, clipping and viewport */
71 st
->clear
.raster
.bypass_vs_clip_and_viewport
= 1;
73 /* fragment shader state: color pass-through program */
75 util_make_fragment_passthrough_shader(pipe
);
77 /* vertex shader state: color/position pass-through */
79 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
80 TGSI_SEMANTIC_COLOR
};
81 const uint semantic_indexes
[] = { 0, 0 };
82 st
->clear
.vs
= util_make_vertex_passthrough_shader(pipe
, 2,
90 st_destroy_clear(struct st_context
*st
)
93 cso_delete_fragment_shader(st
->cso_context
, st
->clear
.fs
);
97 cso_delete_vertex_shader(st
->cso_context
, st
->clear
.vs
);
100 if (st
->clear
.vbuf
) {
101 pipe_buffer_reference(&st
->clear
.vbuf
, NULL
);
102 st
->clear
.vbuf
= NULL
;
108 * Draw a screen-aligned quadrilateral.
109 * Coords are window coords with y=0=bottom. These will be passed
110 * through unmodified to the rasterizer as we have set
111 * rasterizer->bypass_vs_clip_and_viewport.
114 draw_quad(GLcontext
*ctx
,
115 float x0
, float y0
, float x1
, float y1
, GLfloat z
,
116 const GLfloat color
[4])
118 struct st_context
*st
= ctx
->st
;
119 struct pipe_context
*pipe
= st
->pipe
;
120 const GLuint max_slots
= 1024 / sizeof(st
->clear
.vertices
);
123 if (st
->clear
.vbuf_slot
>= max_slots
) {
124 pipe_buffer_reference(&st
->clear
.vbuf
, NULL
);
125 st
->clear
.vbuf_slot
= 0;
128 if (!st
->clear
.vbuf
) {
129 st
->clear
.vbuf
= pipe_buffer_create(pipe
->screen
, 32, PIPE_BUFFER_USAGE_VERTEX
,
130 max_slots
* sizeof(st
->clear
.vertices
));
134 st
->clear
.vertices
[0][0][0] = x0
;
135 st
->clear
.vertices
[0][0][1] = y0
;
137 st
->clear
.vertices
[1][0][0] = x1
;
138 st
->clear
.vertices
[1][0][1] = y0
;
140 st
->clear
.vertices
[2][0][0] = x1
;
141 st
->clear
.vertices
[2][0][1] = y1
;
143 st
->clear
.vertices
[3][0][0] = x0
;
144 st
->clear
.vertices
[3][0][1] = y1
;
146 /* same for all verts: */
147 for (i
= 0; i
< 4; i
++) {
148 st
->clear
.vertices
[i
][0][2] = z
;
149 st
->clear
.vertices
[i
][0][3] = 1.0;
150 st
->clear
.vertices
[i
][1][0] = color
[0];
151 st
->clear
.vertices
[i
][1][1] = color
[1];
152 st
->clear
.vertices
[i
][1][2] = color
[2];
153 st
->clear
.vertices
[i
][1][3] = color
[3];
156 /* put vertex data into vbuf */
157 st_no_flush_pipe_buffer_write(st
, st
->clear
.vbuf
,
158 st
->clear
.vbuf_slot
* sizeof(st
->clear
.vertices
),
159 sizeof(st
->clear
.vertices
),
163 util_draw_vertex_buffer(pipe
,
165 st
->clear
.vbuf_slot
* sizeof(st
->clear
.vertices
),
166 PIPE_PRIM_TRIANGLE_FAN
,
168 2); /* attribs/vert */
171 st
->clear
.vbuf_slot
++;
177 * Do glClear by drawing a quadrilateral.
178 * The vertices of the quad will be computed from the
179 * ctx->DrawBuffer->_X/Ymin/max fields.
182 clear_with_quad(GLcontext
*ctx
,
183 GLboolean color
, GLboolean depth
, GLboolean stencil
)
185 struct st_context
*st
= ctx
->st
;
186 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
187 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
190 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
191 y0
= (GLfloat
) (ctx
->DrawBuffer
->Height
- ctx
->DrawBuffer
->_Ymax
);
192 y1
= (GLfloat
) (ctx
->DrawBuffer
->Height
- ctx
->DrawBuffer
->_Ymin
);
195 y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
196 y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
200 printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__,
201 color ? "color, " : "",
202 depth ? "depth, " : "",
203 stencil ? "stencil" : "",
208 cso_save_blend(st
->cso_context
);
209 cso_save_depth_stencil_alpha(st
->cso_context
);
210 cso_save_rasterizer(st
->cso_context
);
211 cso_save_fragment_shader(st
->cso_context
);
212 cso_save_vertex_shader(st
->cso_context
);
214 /* blend state: RGBA masking */
216 struct pipe_blend_state blend
;
217 memset(&blend
, 0, sizeof(blend
));
218 blend
.rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
219 blend
.alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
220 blend
.rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
221 blend
.alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
223 if (ctx
->Color
.ColorMask
[0])
224 blend
.colormask
|= PIPE_MASK_R
;
225 if (ctx
->Color
.ColorMask
[1])
226 blend
.colormask
|= PIPE_MASK_G
;
227 if (ctx
->Color
.ColorMask
[2])
228 blend
.colormask
|= PIPE_MASK_B
;
229 if (ctx
->Color
.ColorMask
[3])
230 blend
.colormask
|= PIPE_MASK_A
;
231 if (st
->ctx
->Color
.DitherFlag
)
234 cso_set_blend(st
->cso_context
, &blend
);
237 /* depth_stencil state: always pass/set to ref value */
239 struct pipe_depth_stencil_alpha_state depth_stencil
;
240 memset(&depth_stencil
, 0, sizeof(depth_stencil
));
242 depth_stencil
.depth
.enabled
= 1;
243 depth_stencil
.depth
.writemask
= 1;
244 depth_stencil
.depth
.func
= PIPE_FUNC_ALWAYS
;
248 depth_stencil
.stencil
[0].enabled
= 1;
249 depth_stencil
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
250 depth_stencil
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
251 depth_stencil
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
252 depth_stencil
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
253 depth_stencil
.stencil
[0].ref_value
= ctx
->Stencil
.Clear
;
254 depth_stencil
.stencil
[0].valuemask
= 0xff;
255 depth_stencil
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
258 cso_set_depth_stencil_alpha(st
->cso_context
, &depth_stencil
);
261 cso_set_rasterizer(st
->cso_context
, &st
->clear
.raster
);
263 cso_set_fragment_shader_handle(st
->cso_context
, st
->clear
.fs
);
264 cso_set_vertex_shader_handle(st
->cso_context
, st
->clear
.vs
);
266 /* draw quad matching scissor rect (XXX verify coord round-off) */
267 draw_quad(ctx
, x0
, y0
, x1
, y1
, (GLfloat
) ctx
->Depth
.Clear
, ctx
->Color
.ClearColor
);
269 /* Restore pipe state */
270 cso_restore_blend(st
->cso_context
);
271 cso_restore_depth_stencil_alpha(st
->cso_context
);
272 cso_restore_rasterizer(st
->cso_context
);
273 cso_restore_fragment_shader(st
->cso_context
);
274 cso_restore_vertex_shader(st
->cso_context
);
279 * Determine if we need to clear the depth buffer by drawing a quad.
281 static INLINE GLboolean
282 check_clear_color_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
284 if (ctx
->Scissor
.Enabled
&&
285 (ctx
->Scissor
.X
!= 0 ||
286 ctx
->Scissor
.Y
!= 0 ||
287 ctx
->Scissor
.Width
< rb
->Width
||
288 ctx
->Scissor
.Height
< rb
->Height
))
291 if (!ctx
->Color
.ColorMask
[0] ||
292 !ctx
->Color
.ColorMask
[1] ||
293 !ctx
->Color
.ColorMask
[2] ||
294 !ctx
->Color
.ColorMask
[3])
301 static INLINE GLboolean
302 check_clear_depth_stencil_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
304 const GLuint stencilMax
= 0xff;
305 GLboolean maskStencil
306 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
308 assert(rb
->Format
== MESA_FORMAT_S8
||
309 rb
->Format
== MESA_FORMAT_Z24_S8
||
310 rb
->Format
== MESA_FORMAT_S8_Z24
);
312 if (ctx
->Scissor
.Enabled
&&
313 (ctx
->Scissor
.X
!= 0 ||
314 ctx
->Scissor
.Y
!= 0 ||
315 ctx
->Scissor
.Width
< rb
->Width
||
316 ctx
->Scissor
.Height
< rb
->Height
))
327 * Determine if we need to clear the depth buffer by drawing a quad.
329 static INLINE GLboolean
330 check_clear_depth_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
332 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
333 const GLboolean isDS
= pf_is_depth_and_stencil(strb
->surface
->format
);
335 if (ctx
->Scissor
.Enabled
&&
336 (ctx
->Scissor
.X
!= 0 ||
337 ctx
->Scissor
.Y
!= 0 ||
338 ctx
->Scissor
.Width
< rb
->Width
||
339 ctx
->Scissor
.Height
< rb
->Height
))
343 ctx
->DrawBuffer
->Visual
.stencilBits
> 0)
351 * Determine if we need to clear the stencil buffer by drawing a quad.
353 static INLINE GLboolean
354 check_clear_stencil_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
356 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
357 const GLboolean isDS
= pf_is_depth_and_stencil(strb
->surface
->format
);
358 const GLuint stencilMax
= 0xff;
359 const GLboolean maskStencil
360 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
362 assert(rb
->Format
== MESA_FORMAT_S8
||
363 rb
->Format
== MESA_FORMAT_Z24_S8
||
364 rb
->Format
== MESA_FORMAT_S8_Z24
);
369 if (ctx
->Scissor
.Enabled
&&
370 (ctx
->Scissor
.X
!= 0 ||
371 ctx
->Scissor
.Y
!= 0 ||
372 ctx
->Scissor
.Width
< rb
->Width
||
373 ctx
->Scissor
.Height
< rb
->Height
))
376 /* This is correct, but it is necessary to look at the depth clear
377 * value held in the surface when it comes time to issue the clear,
378 * rather than taking depth and stencil clear values from the
382 ctx
->DrawBuffer
->Visual
.depthBits
> 0)
390 void st_flush_clear( struct st_context
*st
)
392 /* Release vertex buffer to avoid synchronous rendering if we were
393 * to map it in the next frame.
395 pipe_buffer_reference(&st
->clear
.vbuf
, NULL
);
396 st
->clear
.vbuf_slot
= 0;
402 * Called via ctx->Driver.Clear()
403 * XXX: doesn't pick up the differences between front/back/left/right
404 * clears. Need to sort that out...
406 static void st_clear(GLcontext
*ctx
, GLbitfield mask
)
408 static const GLbitfield BUFFER_BITS_DS
409 = (BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
);
410 struct st_context
*st
= ctx
->st
;
411 struct gl_renderbuffer
*depthRb
412 = ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
413 struct gl_renderbuffer
*stencilRb
414 = ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
415 GLbitfield quad_buffers
= 0;
416 GLbitfield clear_buffers
= 0;
419 /* This makes sure the pipe has the latest scissor, etc values */
420 st_validate_state( st
);
422 if (mask
& BUFFER_BITS_COLOR
) {
423 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
424 GLuint b
= ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[i
];
426 if (mask
& (1 << b
)) {
427 struct gl_renderbuffer
*rb
428 = ctx
->DrawBuffer
->Attachment
[b
].Renderbuffer
;
429 struct st_renderbuffer
*strb
;
433 strb
= st_renderbuffer(rb
);
438 if (check_clear_color_with_quad( ctx
, rb
))
439 quad_buffers
|= PIPE_CLEAR_COLOR
;
441 clear_buffers
|= PIPE_CLEAR_COLOR
;
446 if ((mask
& BUFFER_BITS_DS
) == BUFFER_BITS_DS
&& depthRb
== stencilRb
) {
447 /* clearing combined depth + stencil */
448 struct st_renderbuffer
*strb
= st_renderbuffer(depthRb
);
451 if (check_clear_depth_stencil_with_quad(ctx
, depthRb
))
452 quad_buffers
|= PIPE_CLEAR_DEPTHSTENCIL
;
454 clear_buffers
|= PIPE_CLEAR_DEPTHSTENCIL
;
458 /* separate depth/stencil clears */
459 if (mask
& BUFFER_BIT_DEPTH
) {
460 struct st_renderbuffer
*strb
= st_renderbuffer(depthRb
);
463 if (check_clear_depth_with_quad(ctx
, depthRb
))
464 quad_buffers
|= PIPE_CLEAR_DEPTHSTENCIL
;
466 clear_buffers
|= PIPE_CLEAR_DEPTHSTENCIL
;
469 if (mask
& BUFFER_BIT_STENCIL
) {
470 struct st_renderbuffer
*strb
= st_renderbuffer(stencilRb
);
473 if (check_clear_stencil_with_quad(ctx
, stencilRb
))
474 quad_buffers
|= PIPE_CLEAR_DEPTHSTENCIL
;
476 clear_buffers
|= PIPE_CLEAR_DEPTHSTENCIL
;
482 * If we're going to use clear_with_quad() for any reason, use it for
483 * everything possible.
486 quad_buffers
|= clear_buffers
;
488 quad_buffers
& PIPE_CLEAR_COLOR
,
489 mask
& BUFFER_BIT_DEPTH
,
490 mask
& BUFFER_BIT_STENCIL
);
491 } else if (clear_buffers
)
492 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, clear_buffers
, ctx
->Color
.ClearColor
,
493 ctx
->Depth
.Clear
, ctx
->Stencil
.Clear
);
495 if (mask
& BUFFER_BIT_ACCUM
)
496 st_clear_accum_buffer(ctx
,
497 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
501 void st_init_clear_functions(struct dd_function_table
*functions
)
503 functions
->Clear
= st_clear
;