bab61780610b6a3baac7097c97792fe3134748ac
[mesa.git] / src / gallium / drivers / svga / svga_pipe_clear.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
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:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
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
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "svga_cmd.h"
27 #include "svga_debug.h"
28
29 #include "pipe/p_defines.h"
30 #include "util/u_pack_color.h"
31
32 #include "svga_context.h"
33 #include "svga_state.h"
34 #include "svga_surface.h"
35
36
37 /**
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.
41 */
42 static void
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)
47 {
48 const struct pipe_framebuffer_state *fb = &svga->curr.framebuffer;
49
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);
65
66 util_blitter_clear(svga->blitter,
67 fb->width, fb->height,
68 1, /* num_layers */
69 clear_buffers, color,
70 depth, stencil);
71 }
72
73
74 /**
75 * Check if any of the color buffers are integer buffers.
76 */
77 static boolean
78 is_integer_target(struct pipe_framebuffer_state *fb, unsigned buffers)
79 {
80 unsigned i;
81
82 for (i = 0; i < fb->nr_cbufs; i++) {
83 if ((buffers & (PIPE_CLEAR_COLOR0 << i)) &&
84 fb->cbufs[i] &&
85 util_format_is_pure_integer(fb->cbufs[i]->format)) {
86 return TRUE;
87 }
88 }
89 return FALSE;
90 }
91
92
93 /**
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.
97 */
98 static boolean
99 ints_fit_in_floats(const union pipe_color_union *color)
100 {
101 const int max = 1 << 24;
102 return (color->i[0] <= max &&
103 color->i[1] <= max &&
104 color->i[2] <= max &&
105 color->i[3] <= max);
106 }
107
108
109 static enum pipe_error
110 try_clear(struct svga_context *svga,
111 unsigned buffers,
112 const union pipe_color_union *color,
113 double depth,
114 unsigned stencil)
115 {
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};
122
123 ret = svga_update_state(svga, SVGA_STATE_HW_CLEAR);
124 if (ret != PIPE_OK)
125 return ret;
126
127 if (svga->rebind.flags.rendertargets) {
128 ret = svga_reemit_framebuffer_bindings(svga);
129 if (ret != PIPE_OK) {
130 return ret;
131 }
132 }
133
134 if (buffers & PIPE_CLEAR_COLOR) {
135 flags |= SVGA3D_CLEAR_COLOR;
136 util_pack_color(color->f, PIPE_FORMAT_B8G8R8A8_UNORM, &uc);
137
138 rect.w = fb->width;
139 rect.h = fb->height;
140 }
141
142 if ((buffers & PIPE_CLEAR_DEPTHSTENCIL) && fb->zsbuf) {
143 if (buffers & PIPE_CLEAR_DEPTH)
144 flags |= SVGA3D_CLEAR_DEPTH;
145
146 if (buffers & PIPE_CLEAR_STENCIL)
147 flags |= SVGA3D_CLEAR_STENCIL;
148
149 rect.w = MAX2(rect.w, fb->zsbuf->width);
150 rect.h = MAX2(rect.h, fb->zsbuf->height);
151 }
152
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);
157 if (ret != PIPE_OK)
158 return ret;
159 }
160
161 if (svga_have_vgpu10(svga)) {
162 if (flags & SVGA3D_CLEAR_COLOR) {
163 unsigned i;
164
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);
169 }
170 else {
171 struct pipe_surface *rtv;
172
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)))
177 continue;
178
179 rtv = svga_validate_surface_view(svga,
180 svga_surface(fb->cbufs[i]));
181 if (rtv == NULL)
182 return PIPE_ERROR_OUT_OF_MEMORY;
183
184 ret = SVGA3D_vgpu10_ClearRenderTargetView(svga->swc,
185 rtv, color->f);
186 if (ret != PIPE_OK)
187 return ret;
188 }
189 }
190 }
191 if (flags & (SVGA3D_CLEAR_DEPTH | SVGA3D_CLEAR_STENCIL)) {
192 struct pipe_surface *dsv =
193 svga_validate_surface_view(svga, svga_surface(fb->zsbuf));
194 if (dsv == NULL)
195 return PIPE_ERROR_OUT_OF_MEMORY;
196
197 ret = SVGA3D_vgpu10_ClearDepthStencilView(svga->swc, dsv, flags,
198 stencil, (float) depth);
199 if (ret != PIPE_OK)
200 return ret;
201 }
202 }
203 else {
204 ret = SVGA3D_ClearRect(svga->swc, flags, uc.ui[0], (float) depth, stencil,
205 rect.x, rect.y, rect.w, rect.h);
206 if (ret != PIPE_OK)
207 return ret;
208 }
209
210 if (restore_viewport) {
211 ret = SVGA3D_SetViewport(svga->swc, &svga->state.hw_clear.viewport);
212 }
213
214 return ret;
215 }
216
217 /**
218 * Clear the given surface to the specified value.
219 * No masking, no scissor (clear entire buffer).
220 */
221 void
222 svga_clear(struct pipe_context *pipe, unsigned buffers,
223 const union pipe_color_union *color,
224 double depth, unsigned stencil)
225 {
226 struct svga_context *svga = svga_context( pipe );
227 enum pipe_error ret;
228
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;
233 }
234 SVGA_DBG(DEBUG_DMA, "clear sid %p\n", h);
235 }
236
237 /* flush any queued prims (don't want them to appear after the clear!) */
238 svga_hwtnl_flush_retry(svga);
239
240 ret = try_clear( svga, buffers, color, depth, stencil );
241
242 if (ret == PIPE_ERROR_OUT_OF_MEMORY) {
243 /* Flush command buffer and retry:
244 */
245 svga_context_flush( svga, NULL );
246
247 ret = try_clear( svga, buffers, color, depth, stencil );
248 }
249
250 /*
251 * Mark target surfaces as dirty
252 * TODO Mark only cleared surfaces.
253 */
254 svga_mark_surfaces_dirty(svga);
255
256 assert (ret == PIPE_OK);
257 }