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_clear.h"
45 #include "st_cb_fbo.h"
46 #include "st_format.h"
47 #include "st_program.h"
49 #include "pipe/p_context.h"
50 #include "pipe/p_shader_tokens.h"
51 #include "pipe/p_state.h"
52 #include "pipe/p_defines.h"
53 #include "util/u_format.h"
54 #include "util/u_framebuffer.h"
55 #include "util/u_inlines.h"
56 #include "util/u_simple_shaders.h"
57 #include "util/u_draw_quad.h"
58 #include "util/u_upload_mgr.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 * Draw a screen-aligned quadrilateral.
172 * Coords are clip coords with y=0=bottom.
175 draw_quad(struct st_context
*st
,
176 float x0
, float y0
, float x1
, float y1
, GLfloat z
,
177 unsigned num_instances
,
178 const union pipe_color_union
*color
)
180 struct cso_context
*cso
= st
->cso_context
;
181 struct pipe_vertex_buffer vb
= {0};
183 float (*vertices
)[2][4]; /**< vertex pos + color */
185 vb
.stride
= 8 * sizeof(float);
187 if (u_upload_alloc(st
->uploader
, 0, 4 * sizeof(vertices
[0]),
188 &vb
.buffer_offset
, &vb
.buffer
,
189 (void **) &vertices
) != PIPE_OK
) {
193 /* Convert Z from [0,1] to [-1,1] range */
197 vertices
[0][0][0] = x0
;
198 vertices
[0][0][1] = y0
;
200 vertices
[1][0][0] = x1
;
201 vertices
[1][0][1] = y0
;
203 vertices
[2][0][0] = x1
;
204 vertices
[2][0][1] = y1
;
206 vertices
[3][0][0] = x0
;
207 vertices
[3][0][1] = y1
;
209 /* same for all verts: */
210 for (i
= 0; i
< 4; i
++) {
211 vertices
[i
][0][2] = z
;
212 vertices
[i
][0][3] = 1.0;
213 vertices
[i
][1][0] = color
->f
[0];
214 vertices
[i
][1][1] = color
->f
[1];
215 vertices
[i
][1][2] = color
->f
[2];
216 vertices
[i
][1][3] = color
->f
[3];
219 u_upload_unmap(st
->uploader
);
222 cso_set_vertex_buffers(cso
, cso_get_aux_vertex_buffer_slot(cso
), 1, &vb
);
223 cso_draw_arrays_instanced(cso
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4,
225 pipe_resource_reference(&vb
.buffer
, NULL
);
231 * Do glClear by drawing a quadrilateral.
232 * The vertices of the quad will be computed from the
233 * ctx->DrawBuffer->_X/Ymin/max fields.
236 clear_with_quad(struct gl_context
*ctx
, unsigned clear_buffers
)
238 struct st_context
*st
= st_context(ctx
);
239 const struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
240 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
241 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
242 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
/ fb_width
* 2.0f
- 1.0f
;
243 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
/ fb_width
* 2.0f
- 1.0f
;
244 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
/ fb_height
* 2.0f
- 1.0f
;
245 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
/ fb_height
* 2.0f
- 1.0f
;
246 unsigned num_layers
=
247 util_framebuffer_get_num_layers(&st
->state
.framebuffer
);
250 printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__,
251 color ? "color, " : "",
252 depth ? "depth, " : "",
253 stencil ? "stencil" : "",
258 cso_save_blend(st
->cso_context
);
259 cso_save_stencil_ref(st
->cso_context
);
260 cso_save_depth_stencil_alpha(st
->cso_context
);
261 cso_save_rasterizer(st
->cso_context
);
262 cso_save_sample_mask(st
->cso_context
);
263 cso_save_min_samples(st
->cso_context
);
264 cso_save_viewport(st
->cso_context
);
265 cso_save_fragment_shader(st
->cso_context
);
266 cso_save_stream_outputs(st
->cso_context
);
267 cso_save_vertex_shader(st
->cso_context
);
268 cso_save_geometry_shader(st
->cso_context
);
269 cso_save_vertex_elements(st
->cso_context
);
270 cso_save_aux_vertex_buffer_slot(st
->cso_context
);
272 /* blend state: RGBA masking */
274 struct pipe_blend_state blend
;
275 memset(&blend
, 0, sizeof(blend
));
276 if (clear_buffers
& PIPE_CLEAR_COLOR
) {
277 int num_buffers
= ctx
->Extensions
.EXT_draw_buffers2
?
278 ctx
->DrawBuffer
->_NumColorDrawBuffers
: 1;
281 blend
.independent_blend_enable
= num_buffers
> 1;
283 for (i
= 0; i
< num_buffers
; i
++) {
284 if (!(clear_buffers
& (PIPE_CLEAR_COLOR0
<< i
)))
287 if (ctx
->Color
.ColorMask
[i
][0])
288 blend
.rt
[i
].colormask
|= PIPE_MASK_R
;
289 if (ctx
->Color
.ColorMask
[i
][1])
290 blend
.rt
[i
].colormask
|= PIPE_MASK_G
;
291 if (ctx
->Color
.ColorMask
[i
][2])
292 blend
.rt
[i
].colormask
|= PIPE_MASK_B
;
293 if (ctx
->Color
.ColorMask
[i
][3])
294 blend
.rt
[i
].colormask
|= PIPE_MASK_A
;
297 if (st
->ctx
->Color
.DitherFlag
)
300 cso_set_blend(st
->cso_context
, &blend
);
303 /* depth_stencil state: always pass/set to ref value */
305 struct pipe_depth_stencil_alpha_state depth_stencil
;
306 memset(&depth_stencil
, 0, sizeof(depth_stencil
));
307 if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
308 depth_stencil
.depth
.enabled
= 1;
309 depth_stencil
.depth
.writemask
= 1;
310 depth_stencil
.depth
.func
= PIPE_FUNC_ALWAYS
;
313 if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
314 struct pipe_stencil_ref stencil_ref
;
315 memset(&stencil_ref
, 0, sizeof(stencil_ref
));
316 depth_stencil
.stencil
[0].enabled
= 1;
317 depth_stencil
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
318 depth_stencil
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
319 depth_stencil
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
320 depth_stencil
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
321 depth_stencil
.stencil
[0].valuemask
= 0xff;
322 depth_stencil
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
323 stencil_ref
.ref_value
[0] = ctx
->Stencil
.Clear
;
324 cso_set_stencil_ref(st
->cso_context
, &stencil_ref
);
327 cso_set_depth_stencil_alpha(st
->cso_context
, &depth_stencil
);
330 cso_set_vertex_elements(st
->cso_context
, 2, st
->velems_util_draw
);
331 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, NULL
);
332 cso_set_sample_mask(st
->cso_context
, ~0);
333 cso_set_min_samples(st
->cso_context
, 1);
334 cso_set_rasterizer(st
->cso_context
, &st
->clear
.raster
);
336 /* viewport state: viewport matching window dims */
338 const GLboolean invert
= (st_fb_orientation(fb
) == Y_0_TOP
);
339 struct pipe_viewport_state vp
;
340 vp
.scale
[0] = 0.5f
* fb_width
;
341 vp
.scale
[1] = fb_height
* (invert
? -0.5f
: 0.5f
);
343 vp
.translate
[0] = 0.5f
* fb_width
;
344 vp
.translate
[1] = 0.5f
* fb_height
;
345 vp
.translate
[2] = 0.5f
;
346 cso_set_viewport(st
->cso_context
, &vp
);
349 set_fragment_shader(st
);
352 set_vertex_shader_layered(st
);
354 set_vertex_shader(st
);
356 /* We can't translate the clear color to the colorbuffer format,
357 * because different colorbuffers may have different formats.
360 /* draw quad matching scissor rect */
361 draw_quad(st
, x0
, y0
, x1
, y1
, (GLfloat
) ctx
->Depth
.Clear
, num_layers
,
362 (union pipe_color_union
*)&ctx
->Color
.ClearColor
);
364 /* Restore pipe state */
365 cso_restore_blend(st
->cso_context
);
366 cso_restore_stencil_ref(st
->cso_context
);
367 cso_restore_depth_stencil_alpha(st
->cso_context
);
368 cso_restore_rasterizer(st
->cso_context
);
369 cso_restore_sample_mask(st
->cso_context
);
370 cso_restore_min_samples(st
->cso_context
);
371 cso_restore_viewport(st
->cso_context
);
372 cso_restore_fragment_shader(st
->cso_context
);
373 cso_restore_vertex_shader(st
->cso_context
);
374 cso_restore_geometry_shader(st
->cso_context
);
375 cso_restore_vertex_elements(st
->cso_context
);
376 cso_restore_aux_vertex_buffer_slot(st
->cso_context
);
377 cso_restore_stream_outputs(st
->cso_context
);
382 * Return if the scissor must be enabled during the clear.
384 static inline GLboolean
385 is_scissor_enabled(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
387 return (ctx
->Scissor
.EnableFlags
& 1) &&
388 (ctx
->Scissor
.ScissorArray
[0].X
> 0 ||
389 ctx
->Scissor
.ScissorArray
[0].Y
> 0 ||
390 (unsigned) ctx
->Scissor
.ScissorArray
[0].Width
< rb
->Width
||
391 (unsigned) ctx
->Scissor
.ScissorArray
[0].Height
< rb
->Height
);
396 * Return if all of the color channels are masked.
398 static inline GLboolean
399 is_color_disabled(struct gl_context
*ctx
, int i
)
401 return !ctx
->Color
.ColorMask
[i
][0] &&
402 !ctx
->Color
.ColorMask
[i
][1] &&
403 !ctx
->Color
.ColorMask
[i
][2] &&
404 !ctx
->Color
.ColorMask
[i
][3];
409 * Return if any of the color channels are masked.
411 static inline GLboolean
412 is_color_masked(struct gl_context
*ctx
, int i
)
414 return !ctx
->Color
.ColorMask
[i
][0] ||
415 !ctx
->Color
.ColorMask
[i
][1] ||
416 !ctx
->Color
.ColorMask
[i
][2] ||
417 !ctx
->Color
.ColorMask
[i
][3];
422 * Return if all of the stencil bits are masked.
424 static inline GLboolean
425 is_stencil_disabled(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
427 const GLuint stencilMax
= 0xff;
429 assert(_mesa_get_format_bits(rb
->Format
, GL_STENCIL_BITS
) > 0);
430 return (ctx
->Stencil
.WriteMask
[0] & stencilMax
) == 0;
435 * Return if any of the stencil bits are masked.
437 static inline GLboolean
438 is_stencil_masked(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
440 const GLuint stencilMax
= 0xff;
442 assert(_mesa_get_format_bits(rb
->Format
, GL_STENCIL_BITS
) > 0);
443 return (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
448 * Called via ctx->Driver.Clear()
451 st_Clear(struct gl_context
*ctx
, GLbitfield mask
)
453 struct st_context
*st
= st_context(ctx
);
454 struct gl_renderbuffer
*depthRb
455 = ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
456 struct gl_renderbuffer
*stencilRb
457 = ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
458 GLbitfield quad_buffers
= 0x0;
459 GLbitfield clear_buffers
= 0x0;
462 /* This makes sure the pipe has the latest scissor, etc values */
463 st_validate_state( st
);
465 if (mask
& BUFFER_BITS_COLOR
) {
466 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
467 GLint b
= ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[i
];
469 if (b
>= 0 && mask
& (1 << b
)) {
470 struct gl_renderbuffer
*rb
471 = ctx
->DrawBuffer
->Attachment
[b
].Renderbuffer
;
472 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
473 int colormask_index
= ctx
->Extensions
.EXT_draw_buffers2
? i
: 0;
475 if (!strb
|| !strb
->surface
)
478 if (is_color_disabled(ctx
, colormask_index
))
481 if (is_scissor_enabled(ctx
, rb
) ||
482 is_color_masked(ctx
, colormask_index
))
483 quad_buffers
|= PIPE_CLEAR_COLOR0
<< i
;
485 clear_buffers
|= PIPE_CLEAR_COLOR0
<< i
;
490 if (mask
& BUFFER_BIT_DEPTH
) {
491 struct st_renderbuffer
*strb
= st_renderbuffer(depthRb
);
493 if (strb
->surface
&& ctx
->Depth
.Mask
) {
494 if (is_scissor_enabled(ctx
, depthRb
))
495 quad_buffers
|= PIPE_CLEAR_DEPTH
;
497 clear_buffers
|= PIPE_CLEAR_DEPTH
;
500 if (mask
& BUFFER_BIT_STENCIL
) {
501 struct st_renderbuffer
*strb
= st_renderbuffer(stencilRb
);
503 if (strb
->surface
&& !is_stencil_disabled(ctx
, stencilRb
)) {
504 if (is_scissor_enabled(ctx
, stencilRb
) ||
505 is_stencil_masked(ctx
, stencilRb
))
506 quad_buffers
|= PIPE_CLEAR_STENCIL
;
508 clear_buffers
|= PIPE_CLEAR_STENCIL
;
512 /* Always clear depth and stencil together.
513 * This can only happen when the stencil writemask is not a full mask.
515 if (quad_buffers
& PIPE_CLEAR_DEPTHSTENCIL
&&
516 clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) {
517 quad_buffers
|= clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
;
518 clear_buffers
&= ~PIPE_CLEAR_DEPTHSTENCIL
;
521 /* Only use quad-based clearing for the renderbuffers which cannot
522 * use pipe->clear. We want to always use pipe->clear for the other
523 * renderbuffers, because it's likely to be faster.
526 clear_with_quad(ctx
, quad_buffers
);
529 /* We can't translate the clear color to the colorbuffer format,
530 * because different colorbuffers may have different formats.
532 st
->pipe
->clear(st
->pipe
, clear_buffers
,
533 (union pipe_color_union
*)&ctx
->Color
.ClearColor
,
534 ctx
->Depth
.Clear
, ctx
->Stencil
.Clear
);
536 if (mask
& BUFFER_BIT_ACCUM
)
537 _mesa_clear_accum_buffer(ctx
);
542 st_init_clear_functions(struct dd_function_table
*functions
)
544 functions
->Clear
= st_Clear
;