6aa74d5b69aba724374d96a1a654db6b5f7b8be8
[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 desc->unpack_z_float(&depth, 0, data, 0, 1, 1);
313 desc->unpack_s_8uint(&stencil, 0, data, 0, 1, 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 if (util_format_is_pure_sint(surface->format)) {
371 /* signed integer */
372 desc->unpack_rgba_sint(color.i, 0, data, 0, 1, 1);
373 }
374 else if (util_format_is_pure_uint(surface->format)) {
375 /* unsigned integer */
376 desc->unpack_rgba_uint(color.ui, 0, data, 0, 1, 1);
377 }
378 else {
379 /* floating point */
380 desc->unpack_rgba_float(color.f, 0, data, 0, 1, 1);
381 }
382 }
383
384 /* Setup render target view */
385 struct pipe_surface *rtv =
386 svga_validate_surface_view(svga, svga_surface_dst);
387
388 if (!rtv) {
389 pipe_surface_reference(&surface, NULL);
390 return;
391 }
392
393 if (box->x == 0 && box->y == 0 && box->width == surface->width &&
394 box->height == surface->height) {
395 struct pipe_framebuffer_state *curr = &svga->curr.framebuffer;
396
397 if (is_integer_target(curr, PIPE_CLEAR_COLOR) &&
398 !ints_fit_in_floats(&color)) {
399 /* To clear full texture with integer format */
400 clear_buffers_with_quad(svga, PIPE_CLEAR_COLOR, &color, 0.0, 0);
401 }
402 else {
403 /* clearing whole surface using VGPU10 command */
404 ret = SVGA3D_vgpu10_ClearRenderTargetView(svga->swc, rtv,
405 color.f);
406 if (ret != PIPE_OK) {
407 svga_context_flush(svga,NULL);
408 ret = SVGA3D_vgpu10_ClearRenderTargetView(svga->swc, rtv,
409 color.f);
410 assert(ret == PIPE_OK);
411 }
412 }
413 }
414 else {
415 /* To clear subtexture use software fallback */
416
417 /**
418 * util_blitter_clear_render_target doesn't support PIPE_TEXTURE_3D
419 * It tries to draw quad with depth 0 for PIPE_TEXTURE_3D so use
420 * util_clear_render_target() for PIPE_TEXTURE_3D.
421 */
422 if (rtv->texture->target != PIPE_TEXTURE_3D &&
423 pipe->screen->is_format_supported(pipe->screen, rtv->format,
424 rtv->texture->target,
425 rtv->texture->nr_samples,
426 rtv->texture->nr_storage_samples,
427 PIPE_BIND_RENDER_TARGET)) {
428 /* clear with quad drawing */
429 util_blitter_save_framebuffer(svga->blitter,
430 &svga->curr.framebuffer);
431 begin_blit(svga);
432 util_blitter_clear_render_target(svga->blitter,
433 rtv,
434 &color,
435 box->x, box->y,
436 box->width, box->height);
437 }
438 else {
439 /* clear with map/write/unmap */
440
441 /* store layer values */
442 unsigned first_layer = rtv->u.tex.first_layer;
443 unsigned last_layer = rtv->u.tex.last_layer;
444 unsigned box_depth = last_layer - first_layer + 1;
445
446 for (unsigned i = 0; i < box_depth; i++) {
447 rtv->u.tex.first_layer = rtv->u.tex.last_layer =
448 first_layer + i;
449 util_clear_render_target(pipe, rtv, &color, box->x, box->y,
450 box->width, box->height);
451 }
452 /* restore layer values */
453 rtv->u.tex.first_layer = first_layer;
454 rtv->u.tex.last_layer = last_layer;
455 }
456 }
457 }
458 pipe_surface_reference(&surface, NULL);
459 }
460
461 /**
462 * \brief Clear the whole render target using vgpu10 functionality
463 *
464 * \param svga[in] The svga context
465 * \param dst[in] The surface to clear
466 * \param color[in] Clear color
467 * \return PIPE_OK if all well, PIPE_ERROR_OUT_OF_MEMORY if ran out of
468 * command submission resources.
469 */
470 static enum pipe_error
471 svga_try_clear_render_target(struct svga_context *svga,
472 struct pipe_surface *dst,
473 const union pipe_color_union *color)
474 {
475 struct pipe_surface *rtv =
476 svga_validate_surface_view(svga, svga_surface(dst));
477
478 if (!rtv)
479 return PIPE_ERROR_OUT_OF_MEMORY;
480
481 return SVGA3D_vgpu10_ClearRenderTargetView(svga->swc, rtv, color->f);
482 }
483
484 /**
485 * \brief Clear part of render target using gallium blitter utilities
486 *
487 * \param svga[in] The svga context
488 * \param dst[in] The surface to clear
489 * \param color[in] Clear color
490 * \param dstx[in] Clear region left
491 * \param dsty[in] Clear region top
492 * \param width[in] Clear region width
493 * \param height[in] Clear region height
494 */
495 static void
496 svga_blitter_clear_render_target(struct svga_context *svga,
497 struct pipe_surface *dst,
498 const union pipe_color_union *color,
499 unsigned dstx, unsigned dsty,
500 unsigned width, unsigned height)
501 {
502 begin_blit(svga);
503 util_blitter_save_framebuffer(svga->blitter, &svga->curr.framebuffer);
504
505 util_blitter_clear_render_target(svga->blitter, dst, color,
506 dstx, dsty, width, height);
507 }
508
509
510 /**
511 * \brief Clear render target pipe callback
512 *
513 * \param pipe[in] The pipe context
514 * \param dst[in] The surface to clear
515 * \param color[in] Clear color
516 * \param dstx[in] Clear region left
517 * \param dsty[in] Clear region top
518 * \param width[in] Clear region width
519 * \param height[in] Clear region height
520 * \param render_condition_enabled[in] Whether to use conditional rendering
521 * to clear (if elsewhere enabled).
522 */
523 static void
524 svga_clear_render_target(struct pipe_context *pipe,
525 struct pipe_surface *dst,
526 const union pipe_color_union *color,
527 unsigned dstx, unsigned dsty,
528 unsigned width, unsigned height,
529 bool render_condition_enabled)
530 {
531 struct svga_context *svga = svga_context( pipe );
532
533 svga_toggle_render_condition(svga, render_condition_enabled, FALSE);
534 if (!svga_have_vgpu10(svga) || dstx != 0 || dsty != 0 ||
535 width != dst->width || height != dst->height) {
536 svga_blitter_clear_render_target(svga, dst, color, dstx, dsty, width,
537 height);
538 } else {
539 enum pipe_error ret;
540
541 ret = svga_try_clear_render_target(svga, dst, color);
542 if (ret == PIPE_ERROR_OUT_OF_MEMORY) {
543 svga_context_flush( svga, NULL );
544 ret = svga_try_clear_render_target(svga, dst, color);
545 }
546
547 assert (ret == PIPE_OK);
548 }
549 svga_toggle_render_condition(svga, render_condition_enabled, TRUE);
550 }
551
552 void svga_init_clear_functions(struct svga_context *svga)
553 {
554 svga->pipe.clear_render_target = svga_clear_render_target;
555 svga->pipe.clear_texture = svga_clear_texture;
556 svga->pipe.clear = svga_clear;
557 }