1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 <keithw@vmware.com>
36 #include "main/glheader.h"
37 #include "main/accum.h"
38 #include "main/formats.h"
39 #include "main/macros.h"
40 #include "main/glformats.h"
41 #include "program/prog_instruction.h"
42 #include "st_context.h"
44 #include "st_cb_bitmap.h"
45 #include "st_cb_clear.h"
46 #include "st_cb_fbo.h"
48 #include "st_format.h"
49 #include "st_program.h"
51 #include "pipe/p_context.h"
52 #include "pipe/p_shader_tokens.h"
53 #include "pipe/p_state.h"
54 #include "pipe/p_defines.h"
55 #include "util/u_format.h"
56 #include "util/u_framebuffer.h"
57 #include "util/u_inlines.h"
58 #include "util/u_simple_shaders.h"
60 #include "cso_cache/cso_context.h"
64 * Do per-context initialization for glClear.
67 st_init_clear(struct st_context
*st
)
69 memset(&st
->clear
, 0, sizeof(st
->clear
));
71 st
->clear
.raster
.half_pixel_center
= 1;
72 st
->clear
.raster
.bottom_edge_rule
= 1;
73 st
->clear
.raster
.depth_clip
= 1;
78 * Free per-context state for glClear.
81 st_destroy_clear(struct st_context
*st
)
84 cso_delete_fragment_shader(st
->cso_context
, st
->clear
.fs
);
88 cso_delete_vertex_shader(st
->cso_context
, st
->clear
.vs
);
91 if (st
->clear
.vs_layered
) {
92 cso_delete_vertex_shader(st
->cso_context
, st
->clear
.vs_layered
);
93 st
->clear
.vs_layered
= NULL
;
95 if (st
->clear
.gs_layered
) {
96 cso_delete_geometry_shader(st
->cso_context
, st
->clear
.gs_layered
);
97 st
->clear
.gs_layered
= NULL
;
103 * Helper function to set the fragment shaders.
106 set_fragment_shader(struct st_context
*st
)
110 util_make_fragment_passthrough_shader(st
->pipe
, TGSI_SEMANTIC_GENERIC
,
111 TGSI_INTERPOLATE_CONSTANT
,
114 cso_set_fragment_shader_handle(st
->cso_context
, st
->clear
.fs
);
119 * Helper function to set the vertex shader.
122 set_vertex_shader(struct st_context
*st
)
124 /* vertex shader - still required to provide the linkage between
125 * fragment shader input semantics and vertex_element/buffers.
129 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
130 TGSI_SEMANTIC_GENERIC
};
131 const uint semantic_indexes
[] = { 0, 0 };
132 st
->clear
.vs
= util_make_vertex_passthrough_shader(st
->pipe
, 2,
138 cso_set_vertex_shader_handle(st
->cso_context
, st
->clear
.vs
);
139 cso_set_geometry_shader_handle(st
->cso_context
, NULL
);
144 set_vertex_shader_layered(struct st_context
*st
)
146 struct pipe_context
*pipe
= st
->pipe
;
148 if (!pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_INSTANCEID
)) {
149 assert(!"Got layered clear, but VS instancing is unsupported");
150 set_vertex_shader(st
);
154 if (!st
->clear
.vs_layered
) {
156 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_VS_LAYER_VIEWPORT
);
158 st
->clear
.vs_layered
= util_make_layered_clear_vertex_shader(pipe
);
160 st
->clear
.vs_layered
= util_make_layered_clear_helper_vertex_shader(pipe
);
161 st
->clear
.gs_layered
= util_make_layered_clear_geometry_shader(pipe
);
165 cso_set_vertex_shader_handle(st
->cso_context
, st
->clear
.vs_layered
);
166 cso_set_geometry_shader_handle(st
->cso_context
, st
->clear
.gs_layered
);
171 * Do glClear by drawing a quadrilateral.
172 * The vertices of the quad will be computed from the
173 * ctx->DrawBuffer->_X/Ymin/max fields.
176 clear_with_quad(struct gl_context
*ctx
, unsigned clear_buffers
)
178 struct st_context
*st
= st_context(ctx
);
179 const struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
180 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
181 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
182 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
/ fb_width
* 2.0f
- 1.0f
;
183 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
/ fb_width
* 2.0f
- 1.0f
;
184 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
/ fb_height
* 2.0f
- 1.0f
;
185 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
/ fb_height
* 2.0f
- 1.0f
;
186 unsigned num_layers
=
187 util_framebuffer_get_num_layers(&st
->state
.framebuffer
);
190 printf("%s %s%s%s %f,%f %f,%f\n", __func__,
191 color ? "color, " : "",
192 depth ? "depth, " : "",
193 stencil ? "stencil" : "",
198 cso_save_blend(st
->cso_context
);
199 cso_save_stencil_ref(st
->cso_context
);
200 cso_save_depth_stencil_alpha(st
->cso_context
);
201 cso_save_rasterizer(st
->cso_context
);
202 cso_save_sample_mask(st
->cso_context
);
203 cso_save_min_samples(st
->cso_context
);
204 cso_save_viewport(st
->cso_context
);
205 cso_save_fragment_shader(st
->cso_context
);
206 cso_save_stream_outputs(st
->cso_context
);
207 cso_save_vertex_shader(st
->cso_context
);
208 cso_save_tessctrl_shader(st
->cso_context
);
209 cso_save_tesseval_shader(st
->cso_context
);
210 cso_save_geometry_shader(st
->cso_context
);
211 cso_save_vertex_elements(st
->cso_context
);
212 cso_save_aux_vertex_buffer_slot(st
->cso_context
);
214 /* blend state: RGBA masking */
216 struct pipe_blend_state blend
;
217 memset(&blend
, 0, sizeof(blend
));
218 if (clear_buffers
& PIPE_CLEAR_COLOR
) {
219 int num_buffers
= ctx
->Extensions
.EXT_draw_buffers2
?
220 ctx
->DrawBuffer
->_NumColorDrawBuffers
: 1;
223 blend
.independent_blend_enable
= num_buffers
> 1;
225 for (i
= 0; i
< num_buffers
; i
++) {
226 if (!(clear_buffers
& (PIPE_CLEAR_COLOR0
<< i
)))
229 if (ctx
->Color
.ColorMask
[i
][0])
230 blend
.rt
[i
].colormask
|= PIPE_MASK_R
;
231 if (ctx
->Color
.ColorMask
[i
][1])
232 blend
.rt
[i
].colormask
|= PIPE_MASK_G
;
233 if (ctx
->Color
.ColorMask
[i
][2])
234 blend
.rt
[i
].colormask
|= PIPE_MASK_B
;
235 if (ctx
->Color
.ColorMask
[i
][3])
236 blend
.rt
[i
].colormask
|= PIPE_MASK_A
;
239 if (st
->ctx
->Color
.DitherFlag
)
242 cso_set_blend(st
->cso_context
, &blend
);
245 /* depth_stencil state: always pass/set to ref value */
247 struct pipe_depth_stencil_alpha_state depth_stencil
;
248 memset(&depth_stencil
, 0, sizeof(depth_stencil
));
249 if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
250 depth_stencil
.depth
.enabled
= 1;
251 depth_stencil
.depth
.writemask
= 1;
252 depth_stencil
.depth
.func
= PIPE_FUNC_ALWAYS
;
255 if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
256 struct pipe_stencil_ref stencil_ref
;
257 memset(&stencil_ref
, 0, sizeof(stencil_ref
));
258 depth_stencil
.stencil
[0].enabled
= 1;
259 depth_stencil
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
260 depth_stencil
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
261 depth_stencil
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
262 depth_stencil
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
263 depth_stencil
.stencil
[0].valuemask
= 0xff;
264 depth_stencil
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
265 stencil_ref
.ref_value
[0] = ctx
->Stencil
.Clear
;
266 cso_set_stencil_ref(st
->cso_context
, &stencil_ref
);
269 cso_set_depth_stencil_alpha(st
->cso_context
, &depth_stencil
);
272 cso_set_vertex_elements(st
->cso_context
, 2, st
->util_velems
);
273 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, NULL
);
274 cso_set_sample_mask(st
->cso_context
, ~0);
275 cso_set_min_samples(st
->cso_context
, 1);
276 cso_set_rasterizer(st
->cso_context
, &st
->clear
.raster
);
278 /* viewport state: viewport matching window dims */
280 const GLboolean invert
= (st_fb_orientation(fb
) == Y_0_TOP
);
281 struct pipe_viewport_state vp
;
282 vp
.scale
[0] = 0.5f
* fb_width
;
283 vp
.scale
[1] = fb_height
* (invert
? -0.5f
: 0.5f
);
285 vp
.translate
[0] = 0.5f
* fb_width
;
286 vp
.translate
[1] = 0.5f
* fb_height
;
287 vp
.translate
[2] = 0.5f
;
288 cso_set_viewport(st
->cso_context
, &vp
);
291 set_fragment_shader(st
);
292 cso_set_tessctrl_shader_handle(st
->cso_context
, NULL
);
293 cso_set_tesseval_shader_handle(st
->cso_context
, NULL
);
296 set_vertex_shader_layered(st
);
298 set_vertex_shader(st
);
300 /* draw quad matching scissor rect.
302 * Note: if we're only clearing depth/stencil we still setup vertices
303 * with color, but they'll be ignored.
305 * We can't translate the clear color to the colorbuffer format,
306 * because different colorbuffers may have different formats.
308 if (!st_draw_quad(st
, x0
, y0
, x1
, y1
,
309 ctx
->Depth
.Clear
* 2.0f
- 1.0f
,
310 0.0f
, 0.0f
, 0.0f
, 0.0f
,
311 (const float *) &ctx
->Color
.ClearColor
.f
,
313 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClear");
316 /* Restore pipe state */
317 cso_restore_blend(st
->cso_context
);
318 cso_restore_stencil_ref(st
->cso_context
);
319 cso_restore_depth_stencil_alpha(st
->cso_context
);
320 cso_restore_rasterizer(st
->cso_context
);
321 cso_restore_sample_mask(st
->cso_context
);
322 cso_restore_min_samples(st
->cso_context
);
323 cso_restore_viewport(st
->cso_context
);
324 cso_restore_fragment_shader(st
->cso_context
);
325 cso_restore_vertex_shader(st
->cso_context
);
326 cso_restore_tessctrl_shader(st
->cso_context
);
327 cso_restore_tesseval_shader(st
->cso_context
);
328 cso_restore_geometry_shader(st
->cso_context
);
329 cso_restore_vertex_elements(st
->cso_context
);
330 cso_restore_aux_vertex_buffer_slot(st
->cso_context
);
331 cso_restore_stream_outputs(st
->cso_context
);
336 * Return if the scissor must be enabled during the clear.
338 static inline GLboolean
339 is_scissor_enabled(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
341 return (ctx
->Scissor
.EnableFlags
& 1) &&
342 (ctx
->Scissor
.ScissorArray
[0].X
> 0 ||
343 ctx
->Scissor
.ScissorArray
[0].Y
> 0 ||
344 (unsigned) ctx
->Scissor
.ScissorArray
[0].Width
< rb
->Width
||
345 (unsigned) ctx
->Scissor
.ScissorArray
[0].Height
< rb
->Height
);
350 * Return if all of the color channels are masked.
352 static inline GLboolean
353 is_color_disabled(struct gl_context
*ctx
, int i
)
355 return !ctx
->Color
.ColorMask
[i
][0] &&
356 !ctx
->Color
.ColorMask
[i
][1] &&
357 !ctx
->Color
.ColorMask
[i
][2] &&
358 !ctx
->Color
.ColorMask
[i
][3];
363 * Return if any of the color channels are masked.
365 static inline GLboolean
366 is_color_masked(struct gl_context
*ctx
, int i
)
368 return !ctx
->Color
.ColorMask
[i
][0] ||
369 !ctx
->Color
.ColorMask
[i
][1] ||
370 !ctx
->Color
.ColorMask
[i
][2] ||
371 !ctx
->Color
.ColorMask
[i
][3];
376 * Return if all of the stencil bits are masked.
378 static inline GLboolean
379 is_stencil_disabled(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
381 const GLuint stencilMax
= 0xff;
383 assert(_mesa_get_format_bits(rb
->Format
, GL_STENCIL_BITS
) > 0);
384 return (ctx
->Stencil
.WriteMask
[0] & stencilMax
) == 0;
389 * Return if any of the stencil bits are masked.
391 static inline GLboolean
392 is_stencil_masked(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
394 const GLuint stencilMax
= 0xff;
396 assert(_mesa_get_format_bits(rb
->Format
, GL_STENCIL_BITS
) > 0);
397 return (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
402 * Called via ctx->Driver.Clear()
405 st_Clear(struct gl_context
*ctx
, GLbitfield mask
)
407 struct st_context
*st
= st_context(ctx
);
408 struct gl_renderbuffer
*depthRb
409 = ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
410 struct gl_renderbuffer
*stencilRb
411 = ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
412 GLbitfield quad_buffers
= 0x0;
413 GLbitfield clear_buffers
= 0x0;
416 st_flush_bitmap_cache(st
);
418 /* This makes sure the pipe has the latest scissor, etc values */
419 st_validate_state( st
, ST_PIPELINE_RENDER
);
421 if (mask
& BUFFER_BITS_COLOR
) {
422 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
423 GLint b
= ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[i
];
425 if (b
>= 0 && mask
& (1 << b
)) {
426 struct gl_renderbuffer
*rb
427 = ctx
->DrawBuffer
->Attachment
[b
].Renderbuffer
;
428 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
429 int colormask_index
= ctx
->Extensions
.EXT_draw_buffers2
? i
: 0;
431 if (!strb
|| !strb
->surface
)
434 if (is_color_disabled(ctx
, colormask_index
))
437 if (is_scissor_enabled(ctx
, rb
) ||
438 is_color_masked(ctx
, colormask_index
))
439 quad_buffers
|= PIPE_CLEAR_COLOR0
<< i
;
441 clear_buffers
|= PIPE_CLEAR_COLOR0
<< i
;
446 if (mask
& BUFFER_BIT_DEPTH
) {
447 struct st_renderbuffer
*strb
= st_renderbuffer(depthRb
);
449 if (strb
->surface
&& ctx
->Depth
.Mask
) {
450 if (is_scissor_enabled(ctx
, depthRb
))
451 quad_buffers
|= PIPE_CLEAR_DEPTH
;
453 clear_buffers
|= PIPE_CLEAR_DEPTH
;
456 if (mask
& BUFFER_BIT_STENCIL
) {
457 struct st_renderbuffer
*strb
= st_renderbuffer(stencilRb
);
459 if (strb
->surface
&& !is_stencil_disabled(ctx
, stencilRb
)) {
460 if (is_scissor_enabled(ctx
, stencilRb
) ||
461 is_stencil_masked(ctx
, stencilRb
))
462 quad_buffers
|= PIPE_CLEAR_STENCIL
;
464 clear_buffers
|= PIPE_CLEAR_STENCIL
;
468 /* Always clear depth and stencil together.
469 * This can only happen when the stencil writemask is not a full mask.
471 if (quad_buffers
& PIPE_CLEAR_DEPTHSTENCIL
&&
472 clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) {
473 quad_buffers
|= clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
;
474 clear_buffers
&= ~PIPE_CLEAR_DEPTHSTENCIL
;
477 /* Only use quad-based clearing for the renderbuffers which cannot
478 * use pipe->clear. We want to always use pipe->clear for the other
479 * renderbuffers, because it's likely to be faster.
482 clear_with_quad(ctx
, quad_buffers
);
485 /* We can't translate the clear color to the colorbuffer format,
486 * because different colorbuffers may have different formats.
488 st
->pipe
->clear(st
->pipe
, clear_buffers
,
489 (union pipe_color_union
*)&ctx
->Color
.ClearColor
,
490 ctx
->Depth
.Clear
, ctx
->Stencil
.Clear
);
492 if (mask
& BUFFER_BIT_ACCUM
)
493 _mesa_clear_accum_buffer(ctx
);
498 st_init_clear_functions(struct dd_function_table
*functions
)
500 functions
->Clear
= st_Clear
;