75507fc1752bb6b915fcc75631a3afbee7fb6ffa
[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 #include "util/u_surface.h"
32
33 #include "svga_context.h"
34 #include "svga_state.h"
35 #include "svga_surface.h"
36
37
38 /**
39 * Saving blitter states before doing any blitter operation
40 */
41 static void
42 begin_blit(struct svga_context *svga)
43 {
44 util_blitter_save_vertex_buffer_slot(svga->blitter, svga->curr.vb);
45 util_blitter_save_vertex_elements(svga->blitter, (void*)svga->curr.velems);
46 util_blitter_save_vertex_shader(svga->blitter, svga->curr.vs);
47 util_blitter_save_geometry_shader(svga->blitter, svga->curr.gs);
48 util_blitter_save_so_targets(svga->blitter, svga->num_so_targets,
49 (struct pipe_stream_output_target**)svga->so_targets);
50 util_blitter_save_rasterizer(svga->blitter, (void*)svga->curr.rast);
51 util_blitter_save_viewport(svga->blitter, &svga->curr.viewport);
52 util_blitter_save_scissor(svga->blitter, &svga->curr.scissor);
53 util_blitter_save_fragment_shader(svga->blitter, svga->curr.fs);
54 util_blitter_save_blend(svga->blitter, (void*)svga->curr.blend);
55 util_blitter_save_depth_stencil_alpha(svga->blitter,
56 (void*)svga->curr.depth);
57 util_blitter_save_stencil_ref(svga->blitter, &svga->curr.stencil_ref);
58 util_blitter_save_sample_mask(svga->blitter, svga->curr.sample_mask);
59 }
60
61
62 /**
63 * Clear the whole color buffer(s) by drawing a quad. For VGPU10 we use
64 * this when clearing integer render targets. We'll also clear the
65 * depth and/or stencil buffers if the clear_buffers mask specifies them.
66 */
67 static void
68 clear_buffers_with_quad(struct svga_context *svga,
69 unsigned clear_buffers,
70 const union pipe_color_union *color,
71 double depth, unsigned stencil)
72 {
73 const struct pipe_framebuffer_state *fb = &svga->curr.framebuffer;
74
75 begin_blit(svga);
76 util_blitter_clear(svga->blitter,
77 fb->width, fb->height,
78 1, /* num_layers */
79 clear_buffers, color,
80 depth, stencil,
81 util_framebuffer_get_num_samples(fb) > 1);
82 }
83
84
85 /**
86 * Check if any of the color buffers are integer buffers.
87 */
88 static boolean
89 is_integer_target(struct pipe_framebuffer_state *fb, unsigned buffers)
90 {
91 unsigned i;
92
93 for (i = 0; i < fb->nr_cbufs; i++) {
94 if ((buffers & (PIPE_CLEAR_COLOR0 << i)) &&
95 fb->cbufs[i] &&
96 util_format_is_pure_integer(fb->cbufs[i]->format)) {
97 return TRUE;
98 }
99 }
100 return FALSE;
101 }
102
103
104 /**
105 * Check if the integer values in the clear color can be represented
106 * by floats. If so, we can use the VGPU10 ClearRenderTargetView command.
107 * Otherwise, we need to clear with a quad.
108 */
109 static boolean
110 ints_fit_in_floats(const union pipe_color_union *color)
111 {
112 const int max = 1 << 24;
113 return (color->i[0] <= max &&
114 color->i[1] <= max &&
115 color->i[2] <= max &&
116 color->i[3] <= max);
117 }
118
119
120 static enum pipe_error
121 try_clear(struct svga_context *svga,
122 unsigned buffers,
123 const union pipe_color_union *color,
124 double depth,
125 unsigned stencil)
126 {
127 enum pipe_error ret = PIPE_OK;
128 SVGA3dRect rect = { 0, 0, 0, 0 };
129 boolean restore_viewport = FALSE;
130 SVGA3dClearFlag flags = 0;
131 struct pipe_framebuffer_state *fb = &svga->curr.framebuffer;
132 union util_color uc = {0};
133
134 ret = svga_update_state(svga, SVGA_STATE_HW_CLEAR);
135 if (ret != PIPE_OK)
136 return ret;
137
138 if (svga->rebind.flags.rendertargets) {
139 ret = svga_reemit_framebuffer_bindings(svga);
140 if (ret != PIPE_OK) {
141 return ret;
142 }
143 }
144
145 if (buffers & PIPE_CLEAR_COLOR) {
146 flags |= SVGA3D_CLEAR_COLOR;
147 util_pack_color(color->f, PIPE_FORMAT_B8G8R8A8_UNORM, &uc);
148
149 rect.w = fb->width;
150 rect.h = fb->height;
151 }
152
153 if ((buffers & PIPE_CLEAR_DEPTHSTENCIL) && fb->zsbuf) {
154 if (buffers & PIPE_CLEAR_DEPTH)
155 flags |= SVGA3D_CLEAR_DEPTH;
156
157 if (buffers & PIPE_CLEAR_STENCIL)
158 flags |= SVGA3D_CLEAR_STENCIL;
159
160 rect.w = MAX2(rect.w, fb->zsbuf->width);
161 rect.h = MAX2(rect.h, fb->zsbuf->height);
162 }
163
164 if (!svga_have_vgpu10(svga) &&
165 !svga_rects_equal(&rect, &svga->state.hw_clear.viewport)) {
166 restore_viewport = TRUE;
167 ret = SVGA3D_SetViewport(svga->swc, &rect);
168 if (ret != PIPE_OK)
169 return ret;
170 }
171
172 if (svga_have_vgpu10(svga)) {
173 if (flags & SVGA3D_CLEAR_COLOR) {
174 unsigned i;
175
176 if (is_integer_target(fb, buffers) && !ints_fit_in_floats(color)) {
177 clear_buffers_with_quad(svga, buffers, color, depth, stencil);
178 /* We also cleared depth/stencil, so that's done */
179 flags &= ~(SVGA3D_CLEAR_DEPTH | SVGA3D_CLEAR_STENCIL);
180 }
181 else {
182 struct pipe_surface *rtv;
183
184 /* Issue VGPU10 Clear commands */
185 for (i = 0; i < fb->nr_cbufs; i++) {
186 if ((fb->cbufs[i] == NULL) ||
187 !(buffers & (PIPE_CLEAR_COLOR0 << i)))
188 continue;
189
190 rtv = svga_validate_surface_view(svga,
191 svga_surface(fb->cbufs[i]));
192 if (!rtv)
193 return PIPE_ERROR_OUT_OF_MEMORY;
194
195 ret = SVGA3D_vgpu10_ClearRenderTargetView(svga->swc,
196 rtv, color->f);
197 if (ret != PIPE_OK)
198 return ret;
199 }
200 }
201 }
202 if (flags & (SVGA3D_CLEAR_DEPTH | SVGA3D_CLEAR_STENCIL)) {
203 struct pipe_surface *dsv =
204 svga_validate_surface_view(svga, svga_surface(fb->zsbuf));
205 if (!dsv)
206 return PIPE_ERROR_OUT_OF_MEMORY;
207
208 ret = SVGA3D_vgpu10_ClearDepthStencilView(svga->swc, dsv, flags,
209 stencil, (float) depth);
210 if (ret != PIPE_OK)
211 return ret;
212 }
213 }
214 else {
215 ret = SVGA3D_ClearRect(svga->swc, flags, uc.ui[0], (float) depth, stencil,
216 rect.x, rect.y, rect.w, rect.h);
217 if (ret != PIPE_OK)
218 return ret;
219 }
220
221 if (restore_viewport) {
222 ret = SVGA3D_SetViewport(svga->swc, &svga->state.hw_clear.viewport);
223 }
224
225 return ret;
226 }
227
228 /**
229 * Clear the given surface to the specified value.
230 * No masking, no scissor (clear entire buffer).
231 */
232 static void
233 svga_clear(struct pipe_context *pipe, unsigned buffers,
234 const union pipe_color_union *color,
235 double depth, unsigned stencil)
236 {
237 struct svga_context *svga = svga_context( pipe );
238 enum pipe_error ret;
239
240 if (buffers & PIPE_CLEAR_COLOR) {
241 struct svga_winsys_surface *h = NULL;
242 if (svga->curr.framebuffer.cbufs[0]) {
243 h = svga_surface(svga->curr.framebuffer.cbufs[0])->handle;
244 }
245 SVGA_DBG(DEBUG_DMA, "clear sid %p\n", h);
246 }
247
248 /* flush any queued prims (don't want them to appear after the clear!) */
249 svga_hwtnl_flush_retry(svga);
250
251 ret = try_clear( svga, buffers, color, depth, stencil );
252
253 if (ret == PIPE_ERROR_OUT_OF_MEMORY) {
254 /* Flush command buffer and retry:
255 */
256 svga_context_flush( svga, NULL );
257
258 ret = try_clear( svga, buffers, color, depth, stencil );
259 }
260
261 /*
262 * Mark target surfaces as dirty
263 * TODO Mark only cleared surfaces.
264 */
265 svga_mark_surfaces_dirty(svga);
266
267 assert (ret == PIPE_OK);
268 }
269
270
271 static void
272 svga_clear_texture(struct pipe_context *pipe,
273 struct pipe_resource *res,
274 unsigned level,
275 const struct pipe_box *box,
276 const void *data)
277 {
278 struct svga_context *svga = svga_context(pipe);
279 struct svga_surface *svga_surface_dst;
280 enum pipe_error ret;
281 struct pipe_surface tmpl;
282 struct pipe_surface *surface;
283
284 memset(&tmpl, 0, sizeof(tmpl));
285 tmpl.format = res->format;
286 tmpl.u.tex.first_layer = box->z;
287 tmpl.u.tex.last_layer = box->z + box->depth - 1;
288 tmpl.u.tex.level = level;
289
290 surface = pipe->create_surface(pipe, res, &tmpl);
291 if (surface == NULL) {
292 debug_printf("failed to create surface\n");
293 return;
294 }
295 svga_surface_dst = svga_surface(surface);
296
297 union pipe_color_union color;
298 const struct util_format_description *desc =
299 util_format_description(surface->format);
300
301 if (util_format_is_depth_or_stencil(surface->format)) {
302 float depth;
303 uint8_t stencil;
304 unsigned clear_flags = 0;
305
306 /* If data is NULL, then set depthValue and stencilValue to zeros */
307 if (data == NULL) {
308 depth = 0.0;
309 stencil = 0;
310 }
311 else {
312 util_format_unpack_z_float(surface->format, &depth, data, 1);
313 util_format_unpack_s_8uint(surface->format, &stencil, data, 1);
314 }
315
316 if (util_format_has_depth(desc)) {
317 clear_flags |= PIPE_CLEAR_DEPTH;
318 }
319 if (util_format_has_stencil(desc)) {
320 clear_flags |= PIPE_CLEAR_STENCIL;
321 }
322
323 /* Setup depth stencil view */
324 struct pipe_surface *dsv =
325 svga_validate_surface_view(svga, svga_surface_dst);
326
327 if (!dsv) {
328 pipe_surface_reference(&surface, NULL);
329 return;
330 }
331
332 if (box->x == 0 && box->y == 0 && box->width == surface->width &&
333 box->height == surface->height) {
334 /* clearing whole surface, use direct VGPU10 command */
335
336
337 ret = SVGA3D_vgpu10_ClearDepthStencilView(svga->swc, dsv,
338 clear_flags,
339 stencil, depth);
340 if (ret != PIPE_OK) {
341 /* flush and try again */
342 svga_context_flush(svga, NULL);
343 ret = SVGA3D_vgpu10_ClearDepthStencilView(svga->swc, dsv,
344 clear_flags,
345 stencil, depth);
346 assert(ret == PIPE_OK);
347 }
348 }
349 else {
350 /* To clear subtexture use software fallback */
351
352 util_blitter_save_framebuffer(svga->blitter,
353 &svga->curr.framebuffer);
354 begin_blit(svga);
355 util_blitter_clear_depth_stencil(svga->blitter,
356 dsv, clear_flags,
357 depth,stencil,
358 box->x, box->y,
359 box->width, box->height);
360 }
361 }
362 else {
363 /* non depth-stencil formats */
364
365 if (data == NULL) {
366 /* If data is NULL, the texture image is filled with zeros */
367 color.f[0] = color.f[1] = color.f[2] = color.f[3] = 0;
368 }
369 else {
370 util_format_unpack_rgba(surface->format, color.ui, data, 1);
371 }
372
373 /* Setup render target view */
374 struct pipe_surface *rtv =
375 svga_validate_surface_view(svga, svga_surface_dst);
376
377 if (!rtv) {
378 pipe_surface_reference(&surface, NULL);
379 return;
380 }
381
382 if (box->x == 0 && box->y == 0 && box->width == surface->width &&
383 box->height == surface->height) {
384 struct pipe_framebuffer_state *curr = &svga->curr.framebuffer;
385
386 if (is_integer_target(curr, PIPE_CLEAR_COLOR) &&
387 !ints_fit_in_floats(&color)) {
388 /* To clear full texture with integer format */
389 clear_buffers_with_quad(svga, PIPE_CLEAR_COLOR, &color, 0.0, 0);
390 }
391 else {
392 /* clearing whole surface using VGPU10 command */
393 ret = SVGA3D_vgpu10_ClearRenderTargetView(svga->swc, rtv,
394 color.f);
395 if (ret != PIPE_OK) {
396 svga_context_flush(svga,NULL);
397 ret = SVGA3D_vgpu10_ClearRenderTargetView(svga->swc, rtv,
398 color.f);
399 assert(ret == PIPE_OK);
400 }
401 }
402 }
403 else {
404 /* To clear subtexture use software fallback */
405
406 /**
407 * util_blitter_clear_render_target doesn't support PIPE_TEXTURE_3D
408 * It tries to draw quad with depth 0 for PIPE_TEXTURE_3D so use
409 * util_clear_render_target() for PIPE_TEXTURE_3D.
410 */
411 if (rtv->texture->target != PIPE_TEXTURE_3D &&
412 pipe->screen->is_format_supported(pipe->screen, rtv->format,
413 rtv->texture->target,
414 rtv->texture->nr_samples,
415 rtv->texture->nr_storage_samples,
416 PIPE_BIND_RENDER_TARGET)) {
417 /* clear with quad drawing */
418 util_blitter_save_framebuffer(svga->blitter,
419 &svga->curr.framebuffer);
420 begin_blit(svga);
421 util_blitter_clear_render_target(svga->blitter,
422 rtv,
423 &color,
424 box->x, box->y,
425 box->width, box->height);
426 }
427 else {
428 /* clear with map/write/unmap */
429
430 /* store layer values */
431 unsigned first_layer = rtv->u.tex.first_layer;
432 unsigned last_layer = rtv->u.tex.last_layer;
433 unsigned box_depth = last_layer - first_layer + 1;
434
435 for (unsigned i = 0; i < box_depth; i++) {
436 rtv->u.tex.first_layer = rtv->u.tex.last_layer =
437 first_layer + i;
438 util_clear_render_target(pipe, rtv, &color, box->x, box->y,
439 box->width, box->height);
440 }
441 /* restore layer values */
442 rtv->u.tex.first_layer = first_layer;
443 rtv->u.tex.last_layer = last_layer;
444 }
445 }
446 }
447 pipe_surface_reference(&surface, NULL);
448 }
449
450 /**
451 * \brief Clear the whole render target using vgpu10 functionality
452 *
453 * \param svga[in] The svga context
454 * \param dst[in] The surface to clear
455 * \param color[in] Clear color
456 * \return PIPE_OK if all well, PIPE_ERROR_OUT_OF_MEMORY if ran out of
457 * command submission resources.
458 */
459 static enum pipe_error
460 svga_try_clear_render_target(struct svga_context *svga,
461 struct pipe_surface *dst,
462 const union pipe_color_union *color)
463 {
464 struct pipe_surface *rtv =
465 svga_validate_surface_view(svga, svga_surface(dst));
466
467 if (!rtv)
468 return PIPE_ERROR_OUT_OF_MEMORY;
469
470 return SVGA3D_vgpu10_ClearRenderTargetView(svga->swc, rtv, color->f);
471 }
472
473 /**
474 * \brief Clear part of render target using gallium blitter utilities
475 *
476 * \param svga[in] The svga context
477 * \param dst[in] The surface to clear
478 * \param color[in] Clear color
479 * \param dstx[in] Clear region left
480 * \param dsty[in] Clear region top
481 * \param width[in] Clear region width
482 * \param height[in] Clear region height
483 */
484 static void
485 svga_blitter_clear_render_target(struct svga_context *svga,
486 struct pipe_surface *dst,
487 const union pipe_color_union *color,
488 unsigned dstx, unsigned dsty,
489 unsigned width, unsigned height)
490 {
491 begin_blit(svga);
492 util_blitter_save_framebuffer(svga->blitter, &svga->curr.framebuffer);
493
494 util_blitter_clear_render_target(svga->blitter, dst, color,
495 dstx, dsty, width, height);
496 }
497
498
499 /**
500 * \brief Clear render target pipe callback
501 *
502 * \param pipe[in] The pipe context
503 * \param dst[in] The surface to clear
504 * \param color[in] Clear color
505 * \param dstx[in] Clear region left
506 * \param dsty[in] Clear region top
507 * \param width[in] Clear region width
508 * \param height[in] Clear region height
509 * \param render_condition_enabled[in] Whether to use conditional rendering
510 * to clear (if elsewhere enabled).
511 */
512 static void
513 svga_clear_render_target(struct pipe_context *pipe,
514 struct pipe_surface *dst,
515 const union pipe_color_union *color,
516 unsigned dstx, unsigned dsty,
517 unsigned width, unsigned height,
518 bool render_condition_enabled)
519 {
520 struct svga_context *svga = svga_context( pipe );
521
522 svga_toggle_render_condition(svga, render_condition_enabled, FALSE);
523 if (!svga_have_vgpu10(svga) || dstx != 0 || dsty != 0 ||
524 width != dst->width || height != dst->height) {
525 svga_blitter_clear_render_target(svga, dst, color, dstx, dsty, width,
526 height);
527 } else {
528 enum pipe_error ret;
529
530 ret = svga_try_clear_render_target(svga, dst, color);
531 if (ret == PIPE_ERROR_OUT_OF_MEMORY) {
532 svga_context_flush( svga, NULL );
533 ret = svga_try_clear_render_target(svga, dst, color);
534 }
535
536 assert (ret == PIPE_OK);
537 }
538 svga_toggle_render_condition(svga, render_condition_enabled, TRUE);
539 }
540
541 void svga_init_clear_functions(struct svga_context *svga)
542 {
543 svga->pipe.clear_render_target = svga_clear_render_target;
544 svga->pipe.clear_texture = svga_clear_texture;
545 svga->pipe.clear = svga_clear;
546 }