1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 **********************************************************/
27 #include "svga_debug.h"
29 #include "pipe/p_defines.h"
30 #include "util/u_pack_color.h"
32 #include "svga_context.h"
33 #include "svga_state.h"
34 #include "svga_surface.h"
38 * Clear the whole color buffer(s) by drawing a quad. For VGPU10 we use
39 * this when clearing integer render targets. We'll also clear the
40 * depth and/or stencil buffers if the clear_buffers mask specifies them.
43 clear_buffers_with_quad(struct svga_context
*svga
,
44 unsigned clear_buffers
,
45 const union pipe_color_union
*color
,
46 double depth
, unsigned stencil
)
48 const struct pipe_framebuffer_state
*fb
= &svga
->curr
.framebuffer
;
50 util_blitter_save_vertex_buffer_slot(svga
->blitter
, svga
->curr
.vb
);
51 util_blitter_save_vertex_elements(svga
->blitter
, (void*)svga
->curr
.velems
);
52 util_blitter_save_vertex_shader(svga
->blitter
, svga
->curr
.vs
);
53 util_blitter_save_geometry_shader(svga
->blitter
, svga
->curr
.gs
);
54 util_blitter_save_so_targets(svga
->blitter
, svga
->num_so_targets
,
55 (struct pipe_stream_output_target
**)svga
->so_targets
);
56 util_blitter_save_rasterizer(svga
->blitter
, (void*)svga
->curr
.rast
);
57 util_blitter_save_viewport(svga
->blitter
, &svga
->curr
.viewport
);
58 util_blitter_save_scissor(svga
->blitter
, &svga
->curr
.scissor
);
59 util_blitter_save_fragment_shader(svga
->blitter
, svga
->curr
.fs
);
60 util_blitter_save_blend(svga
->blitter
, (void*)svga
->curr
.blend
);
61 util_blitter_save_depth_stencil_alpha(svga
->blitter
,
62 (void*)svga
->curr
.depth
);
63 util_blitter_save_stencil_ref(svga
->blitter
, &svga
->curr
.stencil_ref
);
64 util_blitter_save_sample_mask(svga
->blitter
, svga
->curr
.sample_mask
);
66 util_blitter_clear(svga
->blitter
,
67 fb
->width
, fb
->height
,
75 * Check if any of the color buffers are integer buffers.
78 is_integer_target(struct pipe_framebuffer_state
*fb
, unsigned buffers
)
82 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
83 if ((buffers
& (PIPE_CLEAR_COLOR0
<< i
)) &&
85 util_format_is_pure_integer(fb
->cbufs
[i
]->format
)) {
94 * Check if the integer values in the clear color can be represented
95 * by floats. If so, we can use the VGPU10 ClearRenderTargetView command.
96 * Otherwise, we need to clear with a quad.
99 ints_fit_in_floats(const union pipe_color_union
*color
)
101 const int max
= 1 << 24;
102 return (color
->i
[0] <= max
&&
103 color
->i
[1] <= max
&&
104 color
->i
[2] <= max
&&
109 static enum pipe_error
110 try_clear(struct svga_context
*svga
,
112 const union pipe_color_union
*color
,
116 enum pipe_error ret
= PIPE_OK
;
117 SVGA3dRect rect
= { 0, 0, 0, 0 };
118 boolean restore_viewport
= FALSE
;
119 SVGA3dClearFlag flags
= 0;
120 struct pipe_framebuffer_state
*fb
= &svga
->curr
.framebuffer
;
121 union util_color uc
= {0};
123 ret
= svga_update_state(svga
, SVGA_STATE_HW_CLEAR
);
127 if (svga
->rebind
.flags
.rendertargets
) {
128 ret
= svga_reemit_framebuffer_bindings(svga
);
129 if (ret
!= PIPE_OK
) {
134 if (buffers
& PIPE_CLEAR_COLOR
) {
135 flags
|= SVGA3D_CLEAR_COLOR
;
136 util_pack_color(color
->f
, PIPE_FORMAT_B8G8R8A8_UNORM
, &uc
);
142 if ((buffers
& PIPE_CLEAR_DEPTHSTENCIL
) && fb
->zsbuf
) {
143 if (buffers
& PIPE_CLEAR_DEPTH
)
144 flags
|= SVGA3D_CLEAR_DEPTH
;
146 if (buffers
& PIPE_CLEAR_STENCIL
)
147 flags
|= SVGA3D_CLEAR_STENCIL
;
149 rect
.w
= MAX2(rect
.w
, fb
->zsbuf
->width
);
150 rect
.h
= MAX2(rect
.h
, fb
->zsbuf
->height
);
153 if (!svga_have_vgpu10(svga
) &&
154 !svga_rects_equal(&rect
, &svga
->state
.hw_clear
.viewport
)) {
155 restore_viewport
= TRUE
;
156 ret
= SVGA3D_SetViewport(svga
->swc
, &rect
);
161 if (svga_have_vgpu10(svga
)) {
162 if (flags
& SVGA3D_CLEAR_COLOR
) {
165 if (is_integer_target(fb
, buffers
) && !ints_fit_in_floats(color
)) {
166 clear_buffers_with_quad(svga
, buffers
, color
, depth
, stencil
);
167 /* We also cleared depth/stencil, so that's done */
168 flags
&= ~(SVGA3D_CLEAR_DEPTH
| SVGA3D_CLEAR_STENCIL
);
171 struct pipe_surface
*rtv
;
173 /* Issue VGPU10 Clear commands */
174 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
175 if ((fb
->cbufs
[i
] == NULL
) ||
176 !(buffers
& (PIPE_CLEAR_COLOR0
<< i
)))
179 rtv
= svga_validate_surface_view(svga
,
180 svga_surface(fb
->cbufs
[i
]));
182 return PIPE_ERROR_OUT_OF_MEMORY
;
184 ret
= SVGA3D_vgpu10_ClearRenderTargetView(svga
->swc
,
191 if (flags
& (SVGA3D_CLEAR_DEPTH
| SVGA3D_CLEAR_STENCIL
)) {
192 struct pipe_surface
*dsv
=
193 svga_validate_surface_view(svga
, svga_surface(fb
->zsbuf
));
195 return PIPE_ERROR_OUT_OF_MEMORY
;
197 ret
= SVGA3D_vgpu10_ClearDepthStencilView(svga
->swc
, dsv
, flags
,
198 stencil
, (float) depth
);
204 ret
= SVGA3D_ClearRect(svga
->swc
, flags
, uc
.ui
[0], (float) depth
, stencil
,
205 rect
.x
, rect
.y
, rect
.w
, rect
.h
);
210 if (restore_viewport
) {
211 ret
= SVGA3D_SetViewport(svga
->swc
, &svga
->state
.hw_clear
.viewport
);
218 * Clear the given surface to the specified value.
219 * No masking, no scissor (clear entire buffer).
222 svga_clear(struct pipe_context
*pipe
, unsigned buffers
,
223 const union pipe_color_union
*color
,
224 double depth
, unsigned stencil
)
226 struct svga_context
*svga
= svga_context( pipe
);
229 if (buffers
& PIPE_CLEAR_COLOR
) {
230 struct svga_winsys_surface
*h
= NULL
;
231 if (svga
->curr
.framebuffer
.cbufs
[0]) {
232 h
= svga_surface(svga
->curr
.framebuffer
.cbufs
[0])->handle
;
234 SVGA_DBG(DEBUG_DMA
, "clear sid %p\n", h
);
237 /* flush any queued prims (don't want them to appear after the clear!) */
238 svga_hwtnl_flush_retry(svga
);
240 ret
= try_clear( svga
, buffers
, color
, depth
, stencil
);
242 if (ret
== PIPE_ERROR_OUT_OF_MEMORY
) {
243 /* Flush command buffer and retry:
245 svga_context_flush( svga
, NULL
);
247 ret
= try_clear( svga
, buffers
, color
, depth
, stencil
);
251 * Mark target surfaces as dirty
252 * TODO Mark only cleared surfaces.
254 svga_mark_surfaces_dirty(svga
);
256 assert (ret
== PIPE_OK
);