Path: blob/21.2-virgl/src/gallium/drivers/vc4/vc4_blit.c
4570 views
/*1* Copyright © 2015 Broadcom2*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, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "util/format/u_format.h"24#include "util/u_surface.h"25#include "util/u_blitter.h"26#include "compiler/nir/nir_builder.h"27#include "vc4_context.h"2829static struct pipe_surface *30vc4_get_blit_surface(struct pipe_context *pctx,31struct pipe_resource *prsc, unsigned level)32{33struct pipe_surface tmpl;3435memset(&tmpl, 0, sizeof(tmpl));36tmpl.format = prsc->format;37tmpl.u.tex.level = level;38tmpl.u.tex.first_layer = 0;39tmpl.u.tex.last_layer = 0;4041return pctx->create_surface(pctx, prsc, &tmpl);42}4344static bool45is_tile_unaligned(unsigned size, unsigned tile_size)46{47return size & (tile_size - 1);48}4950static bool51vc4_tile_blit(struct pipe_context *pctx, const struct pipe_blit_info *info)52{53struct vc4_context *vc4 = vc4_context(pctx);54bool msaa = (info->src.resource->nr_samples > 1 ||55info->dst.resource->nr_samples > 1);56int tile_width = msaa ? 32 : 64;57int tile_height = msaa ? 32 : 64;5859if (util_format_is_depth_or_stencil(info->dst.resource->format))60return false;6162if (info->scissor_enable)63return false;6465if ((info->mask & PIPE_MASK_RGBA) == 0)66return false;6768if (info->dst.box.x != info->src.box.x ||69info->dst.box.y != info->src.box.y ||70info->dst.box.width != info->src.box.width ||71info->dst.box.height != info->src.box.height) {72return false;73}7475int dst_surface_width = u_minify(info->dst.resource->width0,76info->dst.level);77int dst_surface_height = u_minify(info->dst.resource->height0,78info->dst.level);79if (is_tile_unaligned(info->dst.box.x, tile_width) ||80is_tile_unaligned(info->dst.box.y, tile_height) ||81(is_tile_unaligned(info->dst.box.width, tile_width) &&82info->dst.box.x + info->dst.box.width != dst_surface_width) ||83(is_tile_unaligned(info->dst.box.height, tile_height) &&84info->dst.box.y + info->dst.box.height != dst_surface_height)) {85return false;86}8788/* VC4_PACKET_LOAD_TILE_BUFFER_GENERAL uses the89* VC4_PACKET_TILE_RENDERING_MODE_CONFIG's width (determined by our90* destination surface) to determine the stride. This may be wrong91* when reading from texture miplevels > 0, which are stored in92* POT-sized areas. For MSAA, the tile addresses are computed93* explicitly by the RCL, but still use the destination width to94* determine the stride (which could be fixed by explicitly supplying95* it in the ABI).96*/97struct vc4_resource *rsc = vc4_resource(info->src.resource);9899uint32_t stride;100101if (info->src.resource->nr_samples > 1)102stride = align(dst_surface_width, 32) * 4 * rsc->cpp;103else if (rsc->slices[info->src.level].tiling == VC4_TILING_FORMAT_T)104stride = align(dst_surface_width * rsc->cpp, 128);105else106stride = align(dst_surface_width * rsc->cpp, 16);107108if (stride != rsc->slices[info->src.level].stride)109return false;110111if (info->dst.resource->format != info->src.resource->format)112return false;113114if (false) {115fprintf(stderr, "RCL blit from %d,%d to %d,%d (%d,%d)\n",116info->src.box.x,117info->src.box.y,118info->dst.box.x,119info->dst.box.y,120info->dst.box.width,121info->dst.box.height);122}123124struct pipe_surface *dst_surf =125vc4_get_blit_surface(pctx, info->dst.resource, info->dst.level);126struct pipe_surface *src_surf =127vc4_get_blit_surface(pctx, info->src.resource, info->src.level);128129vc4_flush_jobs_reading_resource(vc4, info->src.resource);130131struct vc4_job *job = vc4_get_job(vc4, dst_surf, NULL);132pipe_surface_reference(&job->color_read, src_surf);133134/* If we're resolving from MSAA to single sample, we still need to run135* the engine in MSAA mode for the load.136*/137if (!job->msaa && info->src.resource->nr_samples > 1) {138job->msaa = true;139job->tile_width = 32;140job->tile_height = 32;141}142143job->draw_min_x = info->dst.box.x;144job->draw_min_y = info->dst.box.y;145job->draw_max_x = info->dst.box.x + info->dst.box.width;146job->draw_max_y = info->dst.box.y + info->dst.box.height;147job->draw_width = dst_surf->width;148job->draw_height = dst_surf->height;149150job->tile_width = tile_width;151job->tile_height = tile_height;152job->msaa = msaa;153job->needs_flush = true;154job->resolve |= PIPE_CLEAR_COLOR;155156vc4_job_submit(vc4, job);157158pipe_surface_reference(&dst_surf, NULL);159pipe_surface_reference(&src_surf, NULL);160161return true;162}163164void165vc4_blitter_save(struct vc4_context *vc4)166{167util_blitter_save_fragment_constant_buffer_slot(vc4->blitter,168vc4->constbuf[PIPE_SHADER_FRAGMENT].cb);169util_blitter_save_vertex_buffer_slot(vc4->blitter, vc4->vertexbuf.vb);170util_blitter_save_vertex_elements(vc4->blitter, vc4->vtx);171util_blitter_save_vertex_shader(vc4->blitter, vc4->prog.bind_vs);172util_blitter_save_rasterizer(vc4->blitter, vc4->rasterizer);173util_blitter_save_viewport(vc4->blitter, &vc4->viewport);174util_blitter_save_scissor(vc4->blitter, &vc4->scissor);175util_blitter_save_fragment_shader(vc4->blitter, vc4->prog.bind_fs);176util_blitter_save_blend(vc4->blitter, vc4->blend);177util_blitter_save_depth_stencil_alpha(vc4->blitter, vc4->zsa);178util_blitter_save_stencil_ref(vc4->blitter, &vc4->stencil_ref);179util_blitter_save_sample_mask(vc4->blitter, vc4->sample_mask);180util_blitter_save_framebuffer(vc4->blitter, &vc4->framebuffer);181util_blitter_save_fragment_sampler_states(vc4->blitter,182vc4->fragtex.num_samplers,183(void **)vc4->fragtex.samplers);184util_blitter_save_fragment_sampler_views(vc4->blitter,185vc4->fragtex.num_textures, vc4->fragtex.textures);186}187188static void *vc4_get_yuv_vs(struct pipe_context *pctx)189{190struct vc4_context *vc4 = vc4_context(pctx);191struct pipe_screen *pscreen = pctx->screen;192193if (vc4->yuv_linear_blit_vs)194return vc4->yuv_linear_blit_vs;195196const struct nir_shader_compiler_options *options =197pscreen->get_compiler_options(pscreen,198PIPE_SHADER_IR_NIR,199PIPE_SHADER_VERTEX);200201nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_VERTEX, options,202"linear_blit_vs");203204const struct glsl_type *vec4 = glsl_vec4_type();205nir_variable *pos_in = nir_variable_create(b.shader, nir_var_shader_in,206vec4, "pos");207208nir_variable *pos_out = nir_variable_create(b.shader, nir_var_shader_out,209vec4, "gl_Position");210pos_out->data.location = VARYING_SLOT_POS;211212nir_store_var(&b, pos_out, nir_load_var(&b, pos_in), 0xf);213214struct pipe_shader_state shader_tmpl = {215.type = PIPE_SHADER_IR_NIR,216.ir.nir = b.shader,217};218219vc4->yuv_linear_blit_vs = pctx->create_vs_state(pctx, &shader_tmpl);220221return vc4->yuv_linear_blit_vs;222}223224static void *vc4_get_yuv_fs(struct pipe_context *pctx, int cpp)225{226struct vc4_context *vc4 = vc4_context(pctx);227struct pipe_screen *pscreen = pctx->screen;228struct pipe_shader_state **cached_shader;229const char *name;230231if (cpp == 1) {232cached_shader = &vc4->yuv_linear_blit_fs_8bit;233name = "linear_blit_8bit_fs";234} else {235cached_shader = &vc4->yuv_linear_blit_fs_16bit;236name = "linear_blit_16bit_fs";237}238239if (*cached_shader)240return *cached_shader;241242const struct nir_shader_compiler_options *options =243pscreen->get_compiler_options(pscreen,244PIPE_SHADER_IR_NIR,245PIPE_SHADER_FRAGMENT);246247nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_FRAGMENT,248options, "%s", name);249250const struct glsl_type *vec4 = glsl_vec4_type();251const struct glsl_type *glsl_int = glsl_int_type();252253nir_variable *color_out = nir_variable_create(b.shader, nir_var_shader_out,254vec4, "f_color");255color_out->data.location = FRAG_RESULT_COLOR;256257nir_variable *pos_in = nir_variable_create(b.shader, nir_var_shader_in,258vec4, "pos");259pos_in->data.location = VARYING_SLOT_POS;260nir_ssa_def *pos = nir_load_var(&b, pos_in);261262nir_ssa_def *one = nir_imm_int(&b, 1);263nir_ssa_def *two = nir_imm_int(&b, 2);264265nir_ssa_def *x = nir_f2i32(&b, nir_channel(&b, pos, 0));266nir_ssa_def *y = nir_f2i32(&b, nir_channel(&b, pos, 1));267268nir_variable *stride_in = nir_variable_create(b.shader, nir_var_uniform,269glsl_int, "stride");270nir_ssa_def *stride = nir_load_var(&b, stride_in);271272nir_ssa_def *x_offset;273nir_ssa_def *y_offset;274if (cpp == 1) {275nir_ssa_def *intra_utile_x_offset =276nir_ishl(&b, nir_iand(&b, x, one), two);277nir_ssa_def *inter_utile_x_offset =278nir_ishl(&b, nir_iand(&b, x, nir_imm_int(&b, ~3)), one);279280x_offset = nir_iadd(&b,281intra_utile_x_offset,282inter_utile_x_offset);283y_offset = nir_imul(&b,284nir_iadd(&b,285nir_ishl(&b, y, one),286nir_ushr(&b, nir_iand(&b, x, two), one)),287stride);288} else {289x_offset = nir_ishl(&b, x, two);290y_offset = nir_imul(&b, y, stride);291}292293nir_ssa_def *load =294nir_load_ubo(&b, 1, 32, one, nir_iadd(&b, x_offset, y_offset),295.align_mul = 4,296.align_offset = 0,297.range_base = 0,298.range = ~0);299300nir_store_var(&b, color_out,301nir_unpack_unorm_4x8(&b, load),3020xf);303304struct pipe_shader_state shader_tmpl = {305.type = PIPE_SHADER_IR_NIR,306.ir.nir = b.shader,307};308309*cached_shader = pctx->create_fs_state(pctx, &shader_tmpl);310311return *cached_shader;312}313314static bool315vc4_yuv_blit(struct pipe_context *pctx, const struct pipe_blit_info *info)316{317struct vc4_context *vc4 = vc4_context(pctx);318struct vc4_resource *src = vc4_resource(info->src.resource);319struct vc4_resource *dst = vc4_resource(info->dst.resource);320bool ok;321322if (src->tiled)323return false;324if (src->base.format != PIPE_FORMAT_R8_UNORM &&325src->base.format != PIPE_FORMAT_R8G8_UNORM)326return false;327328/* YUV blits always turn raster-order to tiled */329assert(dst->base.format == src->base.format);330assert(dst->tiled);331332/* Always 1:1 and at the origin */333assert(info->src.box.x == 0 && info->dst.box.x == 0);334assert(info->src.box.y == 0 && info->dst.box.y == 0);335assert(info->src.box.width == info->dst.box.width);336assert(info->src.box.height == info->dst.box.height);337338if ((src->slices[info->src.level].offset & 3) ||339(src->slices[info->src.level].stride & 3)) {340perf_debug("YUV-blit src texture offset/stride misaligned: 0x%08x/%d\n",341src->slices[info->src.level].offset,342src->slices[info->src.level].stride);343goto fallback;344}345346vc4_blitter_save(vc4);347348/* Create a renderable surface mapping the T-tiled shadow buffer.349*/350struct pipe_surface dst_tmpl;351util_blitter_default_dst_texture(&dst_tmpl, info->dst.resource,352info->dst.level, info->dst.box.z);353dst_tmpl.format = PIPE_FORMAT_RGBA8888_UNORM;354struct pipe_surface *dst_surf =355pctx->create_surface(pctx, info->dst.resource, &dst_tmpl);356if (!dst_surf) {357fprintf(stderr, "Failed to create YUV dst surface\n");358util_blitter_unset_running_flag(vc4->blitter);359return false;360}361dst_surf->width = align(dst_surf->width, 8) / 2;362if (dst->cpp == 1)363dst_surf->height /= 2;364365/* Set the constant buffer. */366uint32_t stride = src->slices[info->src.level].stride;367struct pipe_constant_buffer cb_uniforms = {368.user_buffer = &stride,369.buffer_size = sizeof(stride),370};371pctx->set_constant_buffer(pctx, PIPE_SHADER_FRAGMENT, 0, false, &cb_uniforms);372struct pipe_constant_buffer cb_src = {373.buffer = info->src.resource,374.buffer_offset = src->slices[info->src.level].offset,375.buffer_size = (src->bo->size -376src->slices[info->src.level].offset),377};378pctx->set_constant_buffer(pctx, PIPE_SHADER_FRAGMENT, 1, false, &cb_src);379380/* Unbind the textures, to make sure we don't try to recurse into the381* shadow blit.382*/383pctx->set_sampler_views(pctx, PIPE_SHADER_FRAGMENT, 0, 0, 0, NULL);384pctx->bind_sampler_states(pctx, PIPE_SHADER_FRAGMENT, 0, 0, NULL);385386util_blitter_custom_shader(vc4->blitter, dst_surf,387vc4_get_yuv_vs(pctx),388vc4_get_yuv_fs(pctx, src->cpp));389390util_blitter_restore_textures(vc4->blitter);391util_blitter_restore_constant_buffer_state(vc4->blitter);392/* Restore cb1 (util_blitter doesn't handle this one). */393struct pipe_constant_buffer cb_disabled = { 0 };394pctx->set_constant_buffer(pctx, PIPE_SHADER_FRAGMENT, 1, false, &cb_disabled);395396pipe_surface_reference(&dst_surf, NULL);397398return true;399400fallback:401/* Do an immediate SW fallback, since the render blit path402* would just recurse.403*/404ok = util_try_blit_via_copy_region(pctx, info);405assert(ok); (void)ok;406407return true;408}409410static bool411vc4_render_blit(struct pipe_context *ctx, struct pipe_blit_info *info)412{413struct vc4_context *vc4 = vc4_context(ctx);414415if (!util_blitter_is_blit_supported(vc4->blitter, info)) {416fprintf(stderr, "blit unsupported %s -> %s\n",417util_format_short_name(info->src.resource->format),418util_format_short_name(info->dst.resource->format));419return false;420}421422/* Enable the scissor, so we get a minimal set of tiles rendered. */423if (!info->scissor_enable) {424info->scissor_enable = true;425info->scissor.minx = info->dst.box.x;426info->scissor.miny = info->dst.box.y;427info->scissor.maxx = info->dst.box.x + info->dst.box.width;428info->scissor.maxy = info->dst.box.y + info->dst.box.height;429}430431vc4_blitter_save(vc4);432util_blitter_blit(vc4->blitter, info);433434return true;435}436437/* Optimal hardware path for blitting pixels.438* Scaling, format conversion, up- and downsampling (resolve) are allowed.439*/440void441vc4_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)442{443struct pipe_blit_info info = *blit_info;444445if (vc4_yuv_blit(pctx, blit_info))446return;447448if (vc4_tile_blit(pctx, blit_info))449return;450451if (info.mask & PIPE_MASK_S) {452if (util_try_blit_via_copy_region(pctx, &info))453return;454455info.mask &= ~PIPE_MASK_S;456fprintf(stderr, "cannot blit stencil, skipping\n");457}458459if (vc4_render_blit(pctx, &info))460return;461462fprintf(stderr, "Unsupported blit\n");463}464465466