Path: blob/21.2-virgl/src/gallium/drivers/freedreno/freedreno_blitter.c
4570 views
/*1* Copyright (C) 2017 Rob Clark <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22* Authors:23* Rob Clark <[email protected]>24*/2526#include "util/u_blitter.h"27#include "util/u_surface.h"2829#include "freedreno_blitter.h"30#include "freedreno_context.h"31#include "freedreno_fence.h"32#include "freedreno_resource.h"3334/* generic blit using u_blitter.. slightly modified version of util_blitter_blit35* which also handles PIPE_BUFFER:36*/3738static void39default_dst_texture(struct pipe_surface *dst_templ, struct pipe_resource *dst,40unsigned dstlevel, unsigned dstz)41{42memset(dst_templ, 0, sizeof(*dst_templ));43dst_templ->u.tex.level = dstlevel;44dst_templ->u.tex.first_layer = dstz;45dst_templ->u.tex.last_layer = dstz;46}4748static void49default_src_texture(struct pipe_sampler_view *src_templ,50struct pipe_resource *src, unsigned srclevel)51{52bool cube_as_2darray =53src->screen->get_param(src->screen, PIPE_CAP_SAMPLER_VIEW_TARGET);5455memset(src_templ, 0, sizeof(*src_templ));5657if (cube_as_2darray && (src->target == PIPE_TEXTURE_CUBE ||58src->target == PIPE_TEXTURE_CUBE_ARRAY))59src_templ->target = PIPE_TEXTURE_2D_ARRAY;60else61src_templ->target = src->target;6263if (src->target == PIPE_BUFFER) {64src_templ->target = PIPE_TEXTURE_1D;65}66src_templ->u.tex.first_level = srclevel;67src_templ->u.tex.last_level = srclevel;68src_templ->u.tex.first_layer = 0;69src_templ->u.tex.last_layer = src->target == PIPE_TEXTURE_3D70? u_minify(src->depth0, srclevel) - 171: (unsigned)(src->array_size - 1);72src_templ->swizzle_r = PIPE_SWIZZLE_X;73src_templ->swizzle_g = PIPE_SWIZZLE_Y;74src_templ->swizzle_b = PIPE_SWIZZLE_Z;75src_templ->swizzle_a = PIPE_SWIZZLE_W;76}7778static void79fd_blitter_pipe_begin(struct fd_context *ctx, bool render_cond) assert_dt80{81util_blitter_save_vertex_buffer_slot(ctx->blitter, ctx->vtx.vertexbuf.vb);82util_blitter_save_vertex_elements(ctx->blitter, ctx->vtx.vtx);83util_blitter_save_vertex_shader(ctx->blitter, ctx->prog.vs);84util_blitter_save_tessctrl_shader(ctx->blitter, ctx->prog.hs);85util_blitter_save_tesseval_shader(ctx->blitter, ctx->prog.ds);86util_blitter_save_geometry_shader(ctx->blitter, ctx->prog.gs);87util_blitter_save_so_targets(ctx->blitter, ctx->streamout.num_targets,88ctx->streamout.targets);89util_blitter_save_rasterizer(ctx->blitter, ctx->rasterizer);90util_blitter_save_viewport(ctx->blitter, &ctx->viewport);91util_blitter_save_scissor(ctx->blitter, &ctx->scissor);92util_blitter_save_fragment_shader(ctx->blitter, ctx->prog.fs);93util_blitter_save_blend(ctx->blitter, ctx->blend);94util_blitter_save_depth_stencil_alpha(ctx->blitter, ctx->zsa);95util_blitter_save_stencil_ref(ctx->blitter, &ctx->stencil_ref);96util_blitter_save_sample_mask(ctx->blitter, ctx->sample_mask);97util_blitter_save_framebuffer(ctx->blitter, &ctx->framebuffer);98util_blitter_save_fragment_sampler_states(99ctx->blitter, ctx->tex[PIPE_SHADER_FRAGMENT].num_samplers,100(void **)ctx->tex[PIPE_SHADER_FRAGMENT].samplers);101util_blitter_save_fragment_sampler_views(102ctx->blitter, ctx->tex[PIPE_SHADER_FRAGMENT].num_textures,103ctx->tex[PIPE_SHADER_FRAGMENT].textures);104if (!render_cond)105util_blitter_save_render_condition(ctx->blitter, ctx->cond_query,106ctx->cond_cond, ctx->cond_mode);107108if (ctx->batch)109fd_batch_update_queries(ctx->batch);110}111112static void113fd_blitter_pipe_end(struct fd_context *ctx) assert_dt114{115}116117bool118fd_blitter_blit(struct fd_context *ctx, const struct pipe_blit_info *info)119{120struct pipe_context *pctx = &ctx->base;121struct pipe_resource *dst = info->dst.resource;122struct pipe_resource *src = info->src.resource;123struct pipe_context *pipe = &ctx->base;124struct pipe_surface *dst_view, dst_templ;125struct pipe_sampler_view src_templ, *src_view;126127/* If the blit is updating the whole contents of the resource,128* invalidate it so we don't trigger any unnecessary tile loads in the 3D129* path.130*/131if (util_blit_covers_whole_resource(info))132pctx->invalidate_resource(pctx, info->dst.resource);133134/* The blit format may not match the resource format in this path, so135* we need to validate that we can use the src/dst resource with the136* requested format (and uncompress if necessary). Normally this would137* happen in ->set_sampler_view(), ->set_framebuffer_state(), etc. But138* that would cause recursion back into u_blitter, which ends in tears.139*140* To avoid recursion, this needs to be done before util_blitter_save_*()141*/142if (ctx->validate_format) {143ctx->validate_format(ctx, fd_resource(dst), info->dst.format);144ctx->validate_format(ctx, fd_resource(src), info->src.format);145}146147if (src == dst)148pipe->flush(pipe, NULL, 0);149150DBG_BLIT(info, NULL);151152fd_blitter_pipe_begin(ctx, info->render_condition_enable);153154/* Initialize the surface. */155default_dst_texture(&dst_templ, dst, info->dst.level, info->dst.box.z);156dst_templ.format = info->dst.format;157dst_view = pipe->create_surface(pipe, dst, &dst_templ);158159/* Initialize the sampler view. */160default_src_texture(&src_templ, src, info->src.level);161src_templ.format = info->src.format;162src_view = pipe->create_sampler_view(pipe, src, &src_templ);163164/* Copy. */165util_blitter_blit_generic(166ctx->blitter, dst_view, &info->dst.box, src_view, &info->src.box,167src->width0, src->height0, info->mask, info->filter,168info->scissor_enable ? &info->scissor : NULL, info->alpha_blend);169170pipe_surface_reference(&dst_view, NULL);171pipe_sampler_view_reference(&src_view, NULL);172173fd_blitter_pipe_end(ctx);174175/* While this shouldn't technically be necessary, it is required for176* dEQP-GLES31.functional.stencil_texturing.format.stencil_index8_cube and177* 2d_array to pass.178*/179fd_bc_flush_writer(ctx, fd_resource(info->dst.resource));180181/* The fallback blitter must never fail: */182return true;183}184185/* Generic clear implementation (partially) using u_blitter: */186void187fd_blitter_clear(struct pipe_context *pctx, unsigned buffers,188const union pipe_color_union *color, double depth,189unsigned stencil)190{191struct fd_context *ctx = fd_context(pctx);192struct pipe_framebuffer_state *pfb = &ctx->batch->framebuffer;193struct blitter_context *blitter = ctx->blitter;194195/* Note: don't use discard=true, if there was something to196* discard, that would have been already handled in fd_clear().197*/198fd_blitter_pipe_begin(ctx, false);199200util_blitter_save_fragment_constant_buffer_slot(201ctx->blitter, ctx->constbuf[PIPE_SHADER_FRAGMENT].cb);202203util_blitter_common_clear_setup(blitter, pfb->width, pfb->height, buffers,204NULL, NULL);205206struct pipe_stencil_ref sr = {.ref_value = {stencil & 0xff}};207pctx->set_stencil_ref(pctx, sr);208209struct pipe_constant_buffer cb = {210.buffer_size = 16,211.user_buffer = &color->ui,212};213pctx->set_constant_buffer(pctx, PIPE_SHADER_FRAGMENT, 0, false, &cb);214215unsigned rs_idx = pfb->samples > 1 ? 1 : 0;216if (!ctx->clear_rs_state[rs_idx]) {217const struct pipe_rasterizer_state tmpl = {218.cull_face = PIPE_FACE_NONE,219.half_pixel_center = 1,220.bottom_edge_rule = 1,221.flatshade = 1,222.depth_clip_near = 1,223.depth_clip_far = 1,224.multisample = pfb->samples > 1,225};226ctx->clear_rs_state[rs_idx] = pctx->create_rasterizer_state(pctx, &tmpl);227}228pctx->bind_rasterizer_state(pctx, ctx->clear_rs_state[rs_idx]);229230struct pipe_viewport_state vp = {231.scale = {0.5f * pfb->width, -0.5f * pfb->height, depth},232.translate = {0.5f * pfb->width, 0.5f * pfb->height, 0.0f},233};234pctx->set_viewport_states(pctx, 0, 1, &vp);235236pctx->bind_vertex_elements_state(pctx, ctx->solid_vbuf_state.vtx);237pctx->set_vertex_buffers(pctx, blitter->vb_slot, 1, 0, false,238&ctx->solid_vbuf_state.vertexbuf.vb[0]);239pctx->set_stream_output_targets(pctx, 0, NULL, NULL);240241if (pfb->layers > 1)242pctx->bind_vs_state(pctx, ctx->solid_layered_prog.vs);243else244pctx->bind_vs_state(pctx, ctx->solid_prog.vs);245246pctx->bind_fs_state(pctx, ctx->solid_prog.fs);247248/* Clear geom/tess shaders, lest the draw emit code think we are249* trying to use use them:250*/251pctx->bind_gs_state(pctx, NULL);252pctx->bind_tcs_state(pctx, NULL);253pctx->bind_tes_state(pctx, NULL);254255struct pipe_draw_info info = {256.mode = PIPE_PRIM_MAX, /* maps to DI_PT_RECTLIST */257.index_bounds_valid = true,258.max_index = 1,259.instance_count = MAX2(1, pfb->layers),260};261struct pipe_draw_start_count_bias draw = {262.count = 2,263};264265pctx->draw_vbo(pctx, &info, 0, NULL, &draw, 1);266267/* We expect that this should not have triggered a change in pfb: */268assert(util_framebuffer_state_equal(pfb, &ctx->framebuffer));269270util_blitter_restore_constant_buffer_state(blitter);271util_blitter_restore_vertex_states(blitter);272util_blitter_restore_fragment_states(blitter);273util_blitter_restore_textures(blitter);274util_blitter_restore_fb_state(blitter);275util_blitter_restore_render_cond(blitter);276util_blitter_unset_running_flag(blitter);277278fd_blitter_pipe_end(ctx);279}280281/**282* Optimal hardware path for blitting pixels.283* Scaling, format conversion, up- and downsampling (resolve) are allowed.284*/285bool286fd_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)287{288struct fd_context *ctx = fd_context(pctx);289struct pipe_blit_info info = *blit_info;290291if (info.render_condition_enable && !fd_render_condition_check(pctx))292return true;293294if (ctx->blit && ctx->blit(ctx, &info))295return true;296297if (info.mask & PIPE_MASK_S) {298DBG("cannot blit stencil, skipping");299info.mask &= ~PIPE_MASK_S;300}301302if (!util_blitter_is_blit_supported(ctx->blitter, &info)) {303DBG("blit unsupported %s -> %s",304util_format_short_name(info.src.resource->format),305util_format_short_name(info.dst.resource->format));306return false;307}308309return fd_blitter_blit(ctx, &info);310}311312/**313* _copy_region using pipe (3d engine)314*/315static bool316fd_blitter_pipe_copy_region(struct fd_context *ctx, struct pipe_resource *dst,317unsigned dst_level, unsigned dstx, unsigned dsty,318unsigned dstz, struct pipe_resource *src,319unsigned src_level,320const struct pipe_box *src_box) assert_dt321{322/* not until we allow rendertargets to be buffers */323if (dst->target == PIPE_BUFFER || src->target == PIPE_BUFFER)324return false;325326if (!util_blitter_is_copy_supported(ctx->blitter, dst, src))327return false;328329if (src == dst) {330struct pipe_context *pctx = &ctx->base;331pctx->flush(pctx, NULL, 0);332}333334/* TODO we could invalidate if dst box covers dst level fully. */335fd_blitter_pipe_begin(ctx, false);336util_blitter_copy_texture(ctx->blitter, dst, dst_level, dstx, dsty, dstz,337src, src_level, src_box);338fd_blitter_pipe_end(ctx);339340return true;341}342343/**344* Copy a block of pixels from one resource to another.345* The resource must be of the same format.346*/347void348fd_resource_copy_region(struct pipe_context *pctx, struct pipe_resource *dst,349unsigned dst_level, unsigned dstx, unsigned dsty,350unsigned dstz, struct pipe_resource *src,351unsigned src_level, const struct pipe_box *src_box)352{353struct fd_context *ctx = fd_context(pctx);354355/* The blitter path handles compressed formats only if src and dst format356* match, in other cases just fall back to sw:357*/358if ((src->format != dst->format) &&359(util_format_is_compressed(src->format) ||360util_format_is_compressed(dst->format))) {361perf_debug_ctx(ctx, "copy_region falls back to sw for {%"PRSC_FMT"} to {%"PRSC_FMT"}",362PRSC_ARGS(src), PRSC_ARGS(dst));363goto fallback;364}365366if (ctx->blit) {367struct pipe_blit_info info;368369memset(&info, 0, sizeof info);370info.dst.resource = dst;371info.dst.level = dst_level;372info.dst.box.x = dstx;373info.dst.box.y = dsty;374info.dst.box.z = dstz;375info.dst.box.width = src_box->width;376info.dst.box.height = src_box->height;377assert(info.dst.box.width >= 0);378assert(info.dst.box.height >= 0);379info.dst.box.depth = 1;380info.dst.format = dst->format;381info.src.resource = src;382info.src.level = src_level;383info.src.box = *src_box;384info.src.format = src->format;385info.mask = util_format_get_mask(src->format);386info.filter = PIPE_TEX_FILTER_NEAREST;387info.scissor_enable = 0;388389if (ctx->blit(ctx, &info))390return;391}392393/* try blit on 3d pipe: */394if (fd_blitter_pipe_copy_region(ctx, dst, dst_level, dstx, dsty, dstz, src,395src_level, src_box))396return;397398/* else fallback to pure sw: */399fallback:400util_resource_copy_region(pctx, dst, dst_level, dstx, dsty, dstz, src,401src_level, src_box);402}403404405