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