Path: blob/21.2-virgl/src/gallium/drivers/svga/svga_pipe_clear.c
4570 views
/**********************************************************1* Copyright 2008-2009 VMware, Inc. All rights reserved.2*3* Permission is hereby granted, free of charge, to any person4* obtaining a copy of this software and associated documentation5* files (the "Software"), to deal in the Software without6* restriction, including without limitation the rights to use, copy,7* modify, merge, publish, distribute, sublicense, and/or sell copies8* of the Software, and to permit persons to whom the Software is9* furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*23**********************************************************/2425#include "svga_cmd.h"26#include "svga_debug.h"2728#include "pipe/p_defines.h"29#include "util/u_pack_color.h"30#include "util/u_surface.h"3132#include "svga_context.h"33#include "svga_state.h"34#include "svga_surface.h"353637/**38* Saving blitter states before doing any blitter operation39*/40static void41begin_blit(struct svga_context *svga)42{43util_blitter_save_vertex_buffer_slot(svga->blitter, svga->curr.vb);44util_blitter_save_vertex_elements(svga->blitter, (void*)svga->curr.velems);45util_blitter_save_vertex_shader(svga->blitter, svga->curr.vs);46util_blitter_save_geometry_shader(svga->blitter, svga->curr.gs);47util_blitter_save_tessctrl_shader(svga->blitter, svga->curr.tcs);48util_blitter_save_tesseval_shader(svga->blitter, svga->curr.tes);49util_blitter_save_so_targets(svga->blitter, svga->num_so_targets,50(struct pipe_stream_output_target**)svga->so_targets);51util_blitter_save_rasterizer(svga->blitter, (void*)svga->curr.rast);52util_blitter_save_viewport(svga->blitter, &svga->curr.viewport[0]);53util_blitter_save_scissor(svga->blitter, &svga->curr.scissor[0]);54util_blitter_save_fragment_shader(svga->blitter, svga->curr.fs);55util_blitter_save_blend(svga->blitter, (void*)svga->curr.blend);56util_blitter_save_depth_stencil_alpha(svga->blitter,57(void*)svga->curr.depth);58util_blitter_save_stencil_ref(svga->blitter, &svga->curr.stencil_ref);59util_blitter_save_sample_mask(svga->blitter, svga->curr.sample_mask);60}616263/**64* Clear the whole color buffer(s) by drawing a quad. For VGPU10 we use65* this when clearing integer render targets. We'll also clear the66* depth and/or stencil buffers if the clear_buffers mask specifies them.67*/68static void69clear_buffers_with_quad(struct svga_context *svga,70unsigned clear_buffers,71const union pipe_color_union *color,72double depth, unsigned stencil)73{74const struct pipe_framebuffer_state *fb = &svga->curr.framebuffer;7576begin_blit(svga);77util_blitter_clear(svga->blitter,78fb->width, fb->height,791, /* num_layers */80clear_buffers, color,81depth, stencil,82util_framebuffer_get_num_samples(fb) > 1);83}848586/**87* Check if any of the color buffers are integer buffers.88*/89static boolean90is_integer_target(struct pipe_framebuffer_state *fb, unsigned buffers)91{92unsigned i;9394for (i = 0; i < fb->nr_cbufs; i++) {95if ((buffers & (PIPE_CLEAR_COLOR0 << i)) &&96fb->cbufs[i] &&97util_format_is_pure_integer(fb->cbufs[i]->format)) {98return TRUE;99}100}101return FALSE;102}103104105/**106* Check if the integer values in the clear color can be represented107* by floats. If so, we can use the VGPU10 ClearRenderTargetView command.108* Otherwise, we need to clear with a quad.109*/110static boolean111ints_fit_in_floats(const union pipe_color_union *color)112{113const int max = 1 << 24;114return (color->i[0] <= max &&115color->i[1] <= max &&116color->i[2] <= max &&117color->i[3] <= max);118}119120121static enum pipe_error122try_clear(struct svga_context *svga,123unsigned buffers,124const union pipe_color_union *color,125double depth,126unsigned stencil)127{128enum pipe_error ret = PIPE_OK;129SVGA3dRect rect = { 0, 0, 0, 0 };130boolean restore_viewport = FALSE;131SVGA3dClearFlag flags = 0;132struct pipe_framebuffer_state *fb = &svga->curr.framebuffer;133union util_color uc = {0};134135ret = svga_update_state(svga, SVGA_STATE_HW_CLEAR);136if (ret != PIPE_OK)137return ret;138139if (svga->rebind.flags.rendertargets) {140ret = svga_reemit_framebuffer_bindings(svga);141if (ret != PIPE_OK) {142return ret;143}144}145146if (buffers & PIPE_CLEAR_COLOR) {147flags |= SVGA3D_CLEAR_COLOR;148util_pack_color(color->f, PIPE_FORMAT_B8G8R8A8_UNORM, &uc);149150rect.w = fb->width;151rect.h = fb->height;152}153154if ((buffers & PIPE_CLEAR_DEPTHSTENCIL) && fb->zsbuf) {155if (buffers & PIPE_CLEAR_DEPTH)156flags |= SVGA3D_CLEAR_DEPTH;157158if (buffers & PIPE_CLEAR_STENCIL)159flags |= SVGA3D_CLEAR_STENCIL;160161rect.w = MAX2(rect.w, fb->zsbuf->width);162rect.h = MAX2(rect.h, fb->zsbuf->height);163}164165if (!svga_have_vgpu10(svga) &&166!svga_rects_equal(&rect, &svga->state.hw_clear.viewport)) {167restore_viewport = TRUE;168ret = SVGA3D_SetViewport(svga->swc, &rect);169if (ret != PIPE_OK)170return ret;171}172173if (svga_have_vgpu10(svga)) {174if (flags & SVGA3D_CLEAR_COLOR) {175unsigned i;176177if (is_integer_target(fb, buffers) && !ints_fit_in_floats(color)) {178clear_buffers_with_quad(svga, buffers, color, depth, stencil);179/* We also cleared depth/stencil, so that's done */180flags &= ~(SVGA3D_CLEAR_DEPTH | SVGA3D_CLEAR_STENCIL);181}182else {183struct pipe_surface *rtv;184185/* Issue VGPU10 Clear commands */186for (i = 0; i < fb->nr_cbufs; i++) {187if ((fb->cbufs[i] == NULL) ||188!(buffers & (PIPE_CLEAR_COLOR0 << i)))189continue;190191rtv = svga_validate_surface_view(svga,192svga_surface(fb->cbufs[i]));193if (!rtv)194return PIPE_ERROR_OUT_OF_MEMORY;195196ret = SVGA3D_vgpu10_ClearRenderTargetView(svga->swc,197rtv, color->f);198if (ret != PIPE_OK)199return ret;200}201}202}203if (flags & (SVGA3D_CLEAR_DEPTH | SVGA3D_CLEAR_STENCIL)) {204struct pipe_surface *dsv =205svga_validate_surface_view(svga, svga_surface(fb->zsbuf));206if (!dsv)207return PIPE_ERROR_OUT_OF_MEMORY;208209ret = SVGA3D_vgpu10_ClearDepthStencilView(svga->swc, dsv, flags,210stencil, (float) depth);211if (ret != PIPE_OK)212return ret;213}214}215else {216ret = SVGA3D_ClearRect(svga->swc, flags, uc.ui[0], (float) depth, stencil,217rect.x, rect.y, rect.w, rect.h);218if (ret != PIPE_OK)219return ret;220}221222if (restore_viewport) {223ret = SVGA3D_SetViewport(svga->swc, &svga->state.hw_clear.viewport);224}225226return ret;227}228229/**230* Clear the given surface to the specified value.231* No masking, no scissor (clear entire buffer).232*/233static void234svga_clear(struct pipe_context *pipe, unsigned buffers, const struct pipe_scissor_state *scissor_state,235const union pipe_color_union *color,236double depth, unsigned stencil)237{238struct svga_context *svga = svga_context( pipe );239enum pipe_error ret;240241if (buffers & PIPE_CLEAR_COLOR) {242struct svga_winsys_surface *h = NULL;243if (svga->curr.framebuffer.cbufs[0]) {244h = svga_surface(svga->curr.framebuffer.cbufs[0])->handle;245}246SVGA_DBG(DEBUG_DMA, "clear sid %p\n", h);247}248249/* flush any queued prims (don't want them to appear after the clear!) */250svga_hwtnl_flush_retry(svga);251252SVGA_RETRY_OOM(svga, ret, try_clear( svga, buffers, color, depth, stencil));253254/*255* Mark target surfaces as dirty256* TODO Mark only cleared surfaces.257*/258svga_mark_surfaces_dirty(svga);259260assert (ret == PIPE_OK);261}262263264static void265svga_clear_texture(struct pipe_context *pipe,266struct pipe_resource *res,267unsigned level,268const struct pipe_box *box,269const void *data)270{271struct svga_context *svga = svga_context(pipe);272struct svga_surface *svga_surface_dst;273struct pipe_surface tmpl;274struct pipe_surface *surface;275276memset(&tmpl, 0, sizeof(tmpl));277tmpl.format = res->format;278tmpl.u.tex.first_layer = box->z;279tmpl.u.tex.last_layer = box->z + box->depth - 1;280tmpl.u.tex.level = level;281282surface = pipe->create_surface(pipe, res, &tmpl);283if (surface == NULL) {284debug_printf("failed to create surface\n");285return;286}287svga_surface_dst = svga_surface(surface);288289union pipe_color_union color;290const struct util_format_description *desc =291util_format_description(surface->format);292293if (util_format_is_depth_or_stencil(surface->format)) {294float depth;295uint8_t stencil;296unsigned clear_flags = 0;297298/* If data is NULL, then set depthValue and stencilValue to zeros */299if (data == NULL) {300depth = 0.0;301stencil = 0;302}303else {304util_format_unpack_z_float(surface->format, &depth, data, 1);305util_format_unpack_s_8uint(surface->format, &stencil, data, 1);306}307308if (util_format_has_depth(desc)) {309clear_flags |= PIPE_CLEAR_DEPTH;310}311if (util_format_has_stencil(desc)) {312clear_flags |= PIPE_CLEAR_STENCIL;313}314315/* Setup depth stencil view */316struct pipe_surface *dsv =317svga_validate_surface_view(svga, svga_surface_dst);318319if (!dsv) {320pipe_surface_reference(&surface, NULL);321return;322}323324if (box->x == 0 && box->y == 0 && box->width == surface->width &&325box->height == surface->height) {326/* clearing whole surface, use direct VGPU10 command */327328329SVGA_RETRY(svga, SVGA3D_vgpu10_ClearDepthStencilView(svga->swc, dsv,330clear_flags,331stencil, depth));332}333else {334/* To clear subtexture use software fallback */335336util_blitter_save_framebuffer(svga->blitter,337&svga->curr.framebuffer);338begin_blit(svga);339util_blitter_clear_depth_stencil(svga->blitter,340dsv, clear_flags,341depth,stencil,342box->x, box->y,343box->width, box->height);344}345}346else {347/* non depth-stencil formats */348349if (data == NULL) {350/* If data is NULL, the texture image is filled with zeros */351color.f[0] = color.f[1] = color.f[2] = color.f[3] = 0;352}353else {354util_format_unpack_rgba(surface->format, &color, data, 1);355}356357/* Setup render target view */358struct pipe_surface *rtv =359svga_validate_surface_view(svga, svga_surface_dst);360361if (!rtv) {362pipe_surface_reference(&surface, NULL);363return;364}365366if (box->x == 0 && box->y == 0 && box->width == surface->width &&367box->height == surface->height) {368struct pipe_framebuffer_state *curr = &svga->curr.framebuffer;369370if (is_integer_target(curr, PIPE_CLEAR_COLOR) &&371!ints_fit_in_floats(&color)) {372/* To clear full texture with integer format */373clear_buffers_with_quad(svga, PIPE_CLEAR_COLOR, &color, 0.0, 0);374}375else {376/* clearing whole surface using VGPU10 command */377SVGA_RETRY(svga, SVGA3D_vgpu10_ClearRenderTargetView(svga->swc, rtv,378color.f));379}380}381else {382/* To clear subtexture use software fallback */383384/**385* util_blitter_clear_render_target doesn't support PIPE_TEXTURE_3D386* It tries to draw quad with depth 0 for PIPE_TEXTURE_3D so use387* util_clear_render_target() for PIPE_TEXTURE_3D.388*/389if (rtv->texture->target != PIPE_TEXTURE_3D &&390pipe->screen->is_format_supported(pipe->screen, rtv->format,391rtv->texture->target,392rtv->texture->nr_samples,393rtv->texture->nr_storage_samples,394PIPE_BIND_RENDER_TARGET)) {395/* clear with quad drawing */396util_blitter_save_framebuffer(svga->blitter,397&svga->curr.framebuffer);398begin_blit(svga);399util_blitter_clear_render_target(svga->blitter,400rtv,401&color,402box->x, box->y,403box->width, box->height);404}405else {406/* clear with map/write/unmap */407408/* store layer values */409unsigned first_layer = rtv->u.tex.first_layer;410unsigned last_layer = rtv->u.tex.last_layer;411unsigned box_depth = last_layer - first_layer + 1;412413for (unsigned i = 0; i < box_depth; i++) {414rtv->u.tex.first_layer = rtv->u.tex.last_layer =415first_layer + i;416util_clear_render_target(pipe, rtv, &color, box->x, box->y,417box->width, box->height);418}419/* restore layer values */420rtv->u.tex.first_layer = first_layer;421rtv->u.tex.last_layer = last_layer;422}423}424}425pipe_surface_reference(&surface, NULL);426}427428/**429* \brief Clear the whole render target using vgpu10 functionality430*431* \param svga[in] The svga context432* \param dst[in] The surface to clear433* \param color[in] Clear color434* \return PIPE_OK if all well, PIPE_ERROR_OUT_OF_MEMORY if ran out of435* command submission resources.436*/437static enum pipe_error438svga_try_clear_render_target(struct svga_context *svga,439struct pipe_surface *dst,440const union pipe_color_union *color)441{442struct pipe_surface *rtv =443svga_validate_surface_view(svga, svga_surface(dst));444445if (!rtv)446return PIPE_ERROR_OUT_OF_MEMORY;447448return SVGA3D_vgpu10_ClearRenderTargetView(svga->swc, rtv, color->f);449}450451/**452* \brief Clear part of render target using gallium blitter utilities453*454* \param svga[in] The svga context455* \param dst[in] The surface to clear456* \param color[in] Clear color457* \param dstx[in] Clear region left458* \param dsty[in] Clear region top459* \param width[in] Clear region width460* \param height[in] Clear region height461*/462static void463svga_blitter_clear_render_target(struct svga_context *svga,464struct pipe_surface *dst,465const union pipe_color_union *color,466unsigned dstx, unsigned dsty,467unsigned width, unsigned height)468{469begin_blit(svga);470util_blitter_save_framebuffer(svga->blitter, &svga->curr.framebuffer);471472util_blitter_clear_render_target(svga->blitter, dst, color,473dstx, dsty, width, height);474}475476477/**478* \brief Clear render target pipe callback479*480* \param pipe[in] The pipe context481* \param dst[in] The surface to clear482* \param color[in] Clear color483* \param dstx[in] Clear region left484* \param dsty[in] Clear region top485* \param width[in] Clear region width486* \param height[in] Clear region height487* \param render_condition_enabled[in] Whether to use conditional rendering488* to clear (if elsewhere enabled).489*/490static void491svga_clear_render_target(struct pipe_context *pipe,492struct pipe_surface *dst,493const union pipe_color_union *color,494unsigned dstx, unsigned dsty,495unsigned width, unsigned height,496bool render_condition_enabled)497{498struct svga_context *svga = svga_context( pipe );499500svga_toggle_render_condition(svga, render_condition_enabled, FALSE);501if (!svga_have_vgpu10(svga) || dstx != 0 || dsty != 0 ||502width != dst->width || height != dst->height) {503svga_blitter_clear_render_target(svga, dst, color, dstx, dsty, width,504height);505} else {506enum pipe_error ret;507508SVGA_RETRY_OOM(svga, ret, svga_try_clear_render_target(svga, dst,509color));510assert (ret == PIPE_OK);511}512svga_toggle_render_condition(svga, render_condition_enabled, TRUE);513}514515void svga_init_clear_functions(struct svga_context *svga)516{517svga->pipe.clear_render_target = svga_clear_render_target;518svga->pipe.clear_texture = svga_clear_texture;519svga->pipe.clear = svga_clear;520}521522523