Path: blob/21.2-virgl/src/gallium/drivers/v3d/v3d_blit.c
4570 views
/*1* Copyright © 2015-2017 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 "v3d_context.h"28#include "broadcom/common/v3d_tiling.h"2930void31v3d_blitter_save(struct v3d_context *v3d)32{33util_blitter_save_fragment_constant_buffer_slot(v3d->blitter,34v3d->constbuf[PIPE_SHADER_FRAGMENT].cb);35util_blitter_save_vertex_buffer_slot(v3d->blitter, v3d->vertexbuf.vb);36util_blitter_save_vertex_elements(v3d->blitter, v3d->vtx);37util_blitter_save_vertex_shader(v3d->blitter, v3d->prog.bind_vs);38util_blitter_save_geometry_shader(v3d->blitter, v3d->prog.bind_gs);39util_blitter_save_so_targets(v3d->blitter, v3d->streamout.num_targets,40v3d->streamout.targets);41util_blitter_save_rasterizer(v3d->blitter, v3d->rasterizer);42util_blitter_save_viewport(v3d->blitter, &v3d->viewport);43util_blitter_save_scissor(v3d->blitter, &v3d->scissor);44util_blitter_save_fragment_shader(v3d->blitter, v3d->prog.bind_fs);45util_blitter_save_blend(v3d->blitter, v3d->blend);46util_blitter_save_depth_stencil_alpha(v3d->blitter, v3d->zsa);47util_blitter_save_stencil_ref(v3d->blitter, &v3d->stencil_ref);48util_blitter_save_sample_mask(v3d->blitter, v3d->sample_mask);49util_blitter_save_framebuffer(v3d->blitter, &v3d->framebuffer);50util_blitter_save_fragment_sampler_states(v3d->blitter,51v3d->tex[PIPE_SHADER_FRAGMENT].num_samplers,52(void **)v3d->tex[PIPE_SHADER_FRAGMENT].samplers);53util_blitter_save_fragment_sampler_views(v3d->blitter,54v3d->tex[PIPE_SHADER_FRAGMENT].num_textures,55v3d->tex[PIPE_SHADER_FRAGMENT].textures);56util_blitter_save_so_targets(v3d->blitter, v3d->streamout.num_targets,57v3d->streamout.targets);58}5960static void61v3d_render_blit(struct pipe_context *ctx, struct pipe_blit_info *info)62{63struct v3d_context *v3d = v3d_context(ctx);64struct v3d_resource *src = v3d_resource(info->src.resource);65struct pipe_resource *tiled = NULL;6667if (!info->mask)68return;6970if (!src->tiled) {71struct pipe_box box = {72.x = 0,73.y = 0,74.width = u_minify(info->src.resource->width0,75info->src.level),76.height = u_minify(info->src.resource->height0,77info->src.level),78.depth = 1,79};80struct pipe_resource tmpl = {81.target = info->src.resource->target,82.format = info->src.resource->format,83.width0 = box.width,84.height0 = box.height,85.depth0 = 1,86.array_size = 1,87};88tiled = ctx->screen->resource_create(ctx->screen, &tmpl);89if (!tiled) {90fprintf(stderr, "Failed to create tiled blit temp\n");91return;92}93ctx->resource_copy_region(ctx,94tiled, 0,950, 0, 0,96info->src.resource, info->src.level,97&box);98info->src.level = 0;99info->src.resource = tiled;100}101102if (!util_blitter_is_blit_supported(v3d->blitter, info)) {103fprintf(stderr, "blit unsupported %s -> %s\n",104util_format_short_name(info->src.resource->format),105util_format_short_name(info->dst.resource->format));106return;107}108109v3d_blitter_save(v3d);110util_blitter_blit(v3d->blitter, info);111112pipe_resource_reference(&tiled, NULL);113info->mask = 0;114}115116/* Implement stencil blits by reinterpreting the stencil data as an RGBA8888117* or R8 texture.118*/119static void120v3d_stencil_blit(struct pipe_context *ctx, struct pipe_blit_info *info)121{122struct v3d_context *v3d = v3d_context(ctx);123struct v3d_resource *src = v3d_resource(info->src.resource);124struct v3d_resource *dst = v3d_resource(info->dst.resource);125enum pipe_format src_format, dst_format;126127if ((info->mask & PIPE_MASK_S) == 0)128return;129130if (src->separate_stencil) {131src = src->separate_stencil;132src_format = PIPE_FORMAT_R8_UINT;133} else {134src_format = PIPE_FORMAT_RGBA8888_UINT;135}136137if (dst->separate_stencil) {138dst = dst->separate_stencil;139dst_format = PIPE_FORMAT_R8_UINT;140} else {141dst_format = PIPE_FORMAT_RGBA8888_UINT;142}143144/* Initialize the surface. */145struct pipe_surface dst_tmpl = {146.u.tex = {147.level = info->dst.level,148.first_layer = info->dst.box.z,149.last_layer = info->dst.box.z,150},151.format = dst_format,152};153struct pipe_surface *dst_surf =154ctx->create_surface(ctx, &dst->base, &dst_tmpl);155156/* Initialize the sampler view. */157struct pipe_sampler_view src_tmpl = {158.target = src->base.target,159.format = src_format,160.u.tex = {161.first_level = info->src.level,162.last_level = info->src.level,163.first_layer = 0,164.last_layer = (PIPE_TEXTURE_3D ?165u_minify(src->base.depth0,166info->src.level) - 1 :167src->base.array_size - 1),168},169.swizzle_r = PIPE_SWIZZLE_X,170.swizzle_g = PIPE_SWIZZLE_Y,171.swizzle_b = PIPE_SWIZZLE_Z,172.swizzle_a = PIPE_SWIZZLE_W,173};174struct pipe_sampler_view *src_view =175ctx->create_sampler_view(ctx, &src->base, &src_tmpl);176177v3d_blitter_save(v3d);178util_blitter_blit_generic(v3d->blitter, dst_surf, &info->dst.box,179src_view, &info->src.box,180src->base.width0, src->base.height0,181PIPE_MASK_R,182PIPE_TEX_FILTER_NEAREST,183info->scissor_enable ? &info->scissor : NULL,184info->alpha_blend);185186pipe_surface_reference(&dst_surf, NULL);187pipe_sampler_view_reference(&src_view, NULL);188189info->mask &= ~PIPE_MASK_S;190}191192/* Disable level 0 write, just write following mipmaps */193#define V3D_TFU_IOA_DIMTW (1 << 0)194#define V3D_TFU_IOA_FORMAT_SHIFT 3195#define V3D_TFU_IOA_FORMAT_LINEARTILE 3196#define V3D_TFU_IOA_FORMAT_UBLINEAR_1_COLUMN 4197#define V3D_TFU_IOA_FORMAT_UBLINEAR_2_COLUMN 5198#define V3D_TFU_IOA_FORMAT_UIF_NO_XOR 6199#define V3D_TFU_IOA_FORMAT_UIF_XOR 7200201#define V3D_TFU_ICFG_NUMMM_SHIFT 5202#define V3D_TFU_ICFG_TTYPE_SHIFT 9203204#define V3D_TFU_ICFG_OPAD_SHIFT 22205206#define V3D_TFU_ICFG_FORMAT_SHIFT 18207#define V3D_TFU_ICFG_FORMAT_RASTER 0208#define V3D_TFU_ICFG_FORMAT_SAND_128 1209#define V3D_TFU_ICFG_FORMAT_SAND_256 2210#define V3D_TFU_ICFG_FORMAT_LINEARTILE 11211#define V3D_TFU_ICFG_FORMAT_UBLINEAR_1_COLUMN 12212#define V3D_TFU_ICFG_FORMAT_UBLINEAR_2_COLUMN 13213#define V3D_TFU_ICFG_FORMAT_UIF_NO_XOR 14214#define V3D_TFU_ICFG_FORMAT_UIF_XOR 15215216static bool217v3d_tfu(struct pipe_context *pctx,218struct pipe_resource *pdst,219struct pipe_resource *psrc,220unsigned int src_level,221unsigned int base_level,222unsigned int last_level,223unsigned int src_layer,224unsigned int dst_layer,225bool for_mipmap)226{227struct v3d_context *v3d = v3d_context(pctx);228struct v3d_screen *screen = v3d->screen;229struct v3d_resource *src = v3d_resource(psrc);230struct v3d_resource *dst = v3d_resource(pdst);231struct v3d_resource_slice *src_base_slice = &src->slices[src_level];232struct v3d_resource_slice *dst_base_slice = &dst->slices[base_level];233int msaa_scale = pdst->nr_samples > 1 ? 2 : 1;234int width = u_minify(pdst->width0, base_level) * msaa_scale;235int height = u_minify(pdst->height0, base_level) * msaa_scale;236enum pipe_format pformat;237238if (psrc->format != pdst->format)239return false;240if (psrc->nr_samples != pdst->nr_samples)241return false;242243if (pdst->target != PIPE_TEXTURE_2D || psrc->target != PIPE_TEXTURE_2D)244return false;245246/* Can't write to raster. */247if (dst_base_slice->tiling == V3D_TILING_RASTER)248return false;249250/* When using TFU for blit, we are doing exact copies (both input and251* output format must be the same, no scaling, etc), so there is no252* pixel format conversions. Thus we can rewrite the format to use one253* that is TFU compatible based on its texel size.254*/255if (for_mipmap) {256pformat = pdst->format;257} else {258switch (dst->cpp) {259case 16: pformat = PIPE_FORMAT_R32G32B32A32_FLOAT; break;260case 8: pformat = PIPE_FORMAT_R16G16B16A16_FLOAT; break;261case 4: pformat = PIPE_FORMAT_R32_FLOAT; break;262case 2: pformat = PIPE_FORMAT_R16_FLOAT; break;263case 1: pformat = PIPE_FORMAT_R8_UNORM; break;264default: unreachable("unsupported format bit-size"); break;265};266}267268uint32_t tex_format = v3d_get_tex_format(&screen->devinfo, pformat);269270if (!v3d_tfu_supports_tex_format(&screen->devinfo, tex_format, for_mipmap)) {271assert(for_mipmap);272return false;273}274275v3d_flush_jobs_writing_resource(v3d, psrc, V3D_FLUSH_DEFAULT, false);276v3d_flush_jobs_reading_resource(v3d, pdst, V3D_FLUSH_DEFAULT, false);277278struct drm_v3d_submit_tfu tfu = {279.ios = (height << 16) | width,280.bo_handles = {281dst->bo->handle,282src != dst ? src->bo->handle : 0283},284.in_sync = v3d->out_sync,285.out_sync = v3d->out_sync,286};287uint32_t src_offset = (src->bo->offset +288v3d_layer_offset(psrc, src_level, src_layer));289tfu.iia |= src_offset;290if (src_base_slice->tiling == V3D_TILING_RASTER) {291tfu.icfg |= (V3D_TFU_ICFG_FORMAT_RASTER <<292V3D_TFU_ICFG_FORMAT_SHIFT);293} else {294tfu.icfg |= ((V3D_TFU_ICFG_FORMAT_LINEARTILE +295(src_base_slice->tiling - V3D_TILING_LINEARTILE)) <<296V3D_TFU_ICFG_FORMAT_SHIFT);297}298299uint32_t dst_offset = (dst->bo->offset +300v3d_layer_offset(pdst, base_level, dst_layer));301tfu.ioa |= dst_offset;302if (last_level != base_level)303tfu.ioa |= V3D_TFU_IOA_DIMTW;304tfu.ioa |= ((V3D_TFU_IOA_FORMAT_LINEARTILE +305(dst_base_slice->tiling - V3D_TILING_LINEARTILE)) <<306V3D_TFU_IOA_FORMAT_SHIFT);307308tfu.icfg |= tex_format << V3D_TFU_ICFG_TTYPE_SHIFT;309tfu.icfg |= (last_level - base_level) << V3D_TFU_ICFG_NUMMM_SHIFT;310311switch (src_base_slice->tiling) {312case V3D_TILING_UIF_NO_XOR:313case V3D_TILING_UIF_XOR:314tfu.iis |= (src_base_slice->padded_height /315(2 * v3d_utile_height(src->cpp)));316break;317case V3D_TILING_RASTER:318tfu.iis |= src_base_slice->stride / src->cpp;319break;320case V3D_TILING_LINEARTILE:321case V3D_TILING_UBLINEAR_1_COLUMN:322case V3D_TILING_UBLINEAR_2_COLUMN:323break;324}325326/* If we're writing level 0 (!IOA_DIMTW), then we need to supply the327* OPAD field for the destination (how many extra UIF blocks beyond328* those necessary to cover the height). When filling mipmaps, the329* miplevel 1+ tiling state is inferred.330*/331if (dst_base_slice->tiling == V3D_TILING_UIF_NO_XOR ||332dst_base_slice->tiling == V3D_TILING_UIF_XOR) {333int uif_block_h = 2 * v3d_utile_height(dst->cpp);334int implicit_padded_height = align(height, uif_block_h);335336tfu.icfg |= (((dst_base_slice->padded_height -337implicit_padded_height) / uif_block_h) <<338V3D_TFU_ICFG_OPAD_SHIFT);339}340341int ret = v3d_ioctl(screen->fd, DRM_IOCTL_V3D_SUBMIT_TFU, &tfu);342if (ret != 0) {343fprintf(stderr, "Failed to submit TFU job: %d\n", ret);344return false;345}346347dst->writes++;348349return true;350}351352bool353v3d_generate_mipmap(struct pipe_context *pctx,354struct pipe_resource *prsc,355enum pipe_format format,356unsigned int base_level,357unsigned int last_level,358unsigned int first_layer,359unsigned int last_layer)360{361if (format != prsc->format)362return false;363364/* We could maybe support looping over layers for array textures, but365* we definitely don't support 3D.366*/367if (first_layer != last_layer)368return false;369370return v3d_tfu(pctx,371prsc, prsc,372base_level,373base_level, last_level,374first_layer, first_layer,375true);376}377378static void379v3d_tfu_blit(struct pipe_context *pctx, struct pipe_blit_info *info)380{381int dst_width = u_minify(info->dst.resource->width0, info->dst.level);382int dst_height = u_minify(info->dst.resource->height0, info->dst.level);383384if ((info->mask & PIPE_MASK_RGBA) == 0)385return;386387if (info->scissor_enable ||388info->dst.box.x != 0 ||389info->dst.box.y != 0 ||390info->dst.box.width != dst_width ||391info->dst.box.height != dst_height ||392info->src.box.x != 0 ||393info->src.box.y != 0 ||394info->src.box.width != info->dst.box.width ||395info->src.box.height != info->dst.box.height) {396return;397}398399if (info->dst.format != info->src.format)400return;401402if (v3d_tfu(pctx, info->dst.resource, info->src.resource,403info->src.level,404info->dst.level, info->dst.level,405info->src.box.z, info->dst.box.z,406false)) {407info->mask &= ~PIPE_MASK_RGBA;408}409}410411static struct pipe_surface *412v3d_get_blit_surface(struct pipe_context *pctx,413struct pipe_resource *prsc,414unsigned level,415int16_t layer)416{417struct pipe_surface tmpl;418419tmpl.format = prsc->format;420tmpl.u.tex.level = level;421tmpl.u.tex.first_layer = layer;422tmpl.u.tex.last_layer = layer;423424return pctx->create_surface(pctx, prsc, &tmpl);425}426427static bool428is_tile_unaligned(unsigned size, unsigned tile_size)429{430return size & (tile_size - 1);431}432433static void434v3d_tlb_blit(struct pipe_context *pctx, struct pipe_blit_info *info)435{436struct v3d_context *v3d = v3d_context(pctx);437struct v3d_screen *screen = v3d->screen;438439if (screen->devinfo.ver < 40 || !info->mask)440return;441442bool is_color_blit = info->mask & PIPE_MASK_RGBA;443bool is_depth_blit = info->mask & PIPE_MASK_Z;444bool is_stencil_blit = info->mask & PIPE_MASK_S;445446/* We should receive either a depth/stencil blit, or color blit, but447* not both.448*/449assert ((is_color_blit && !is_depth_blit && !is_stencil_blit) ||450(!is_color_blit && (is_depth_blit || is_stencil_blit)));451452if (info->scissor_enable)453return;454455if (info->src.box.x != info->dst.box.x ||456info->src.box.y != info->dst.box.y ||457info->src.box.width != info->dst.box.width ||458info->src.box.height != info->dst.box.height)459return;460461if (is_color_blit &&462util_format_is_depth_or_stencil(info->dst.resource->format))463return;464465if (!v3d_rt_format_supported(&screen->devinfo, info->src.resource->format))466return;467468if (v3d_get_rt_format(&screen->devinfo, info->src.resource->format) !=469v3d_get_rt_format(&screen->devinfo, info->dst.resource->format))470return;471472bool msaa = (info->src.resource->nr_samples > 1 ||473info->dst.resource->nr_samples > 1);474bool is_msaa_resolve = (info->src.resource->nr_samples > 1 &&475info->dst.resource->nr_samples < 2);476477if (is_msaa_resolve &&478!v3d_format_supports_tlb_msaa_resolve(&screen->devinfo, info->src.resource->format))479return;480481v3d_flush_jobs_writing_resource(v3d, info->src.resource, V3D_FLUSH_DEFAULT, false);482483struct pipe_surface *dst_surf =484v3d_get_blit_surface(pctx, info->dst.resource, info->dst.level, info->dst.box.z);485struct pipe_surface *src_surf =486v3d_get_blit_surface(pctx, info->src.resource, info->src.level, info->src.box.z);487488struct pipe_surface *surfaces[V3D_MAX_DRAW_BUFFERS] = { 0 };489if (is_color_blit)490surfaces[0] = dst_surf;491492uint32_t tile_width, tile_height, max_bpp;493v3d_get_tile_buffer_size(msaa, is_color_blit ? 1 : 0, surfaces, src_surf, &tile_width, &tile_height, &max_bpp);494495int dst_surface_width = u_minify(info->dst.resource->width0,496info->dst.level);497int dst_surface_height = u_minify(info->dst.resource->height0,498info->dst.level);499if (is_tile_unaligned(info->dst.box.x, tile_width) ||500is_tile_unaligned(info->dst.box.y, tile_height) ||501(is_tile_unaligned(info->dst.box.width, tile_width) &&502info->dst.box.x + info->dst.box.width != dst_surface_width) ||503(is_tile_unaligned(info->dst.box.height, tile_height) &&504info->dst.box.y + info->dst.box.height != dst_surface_height)) {505pipe_surface_reference(&dst_surf, NULL);506pipe_surface_reference(&src_surf, NULL);507return;508}509510struct v3d_job *job = v3d_get_job(v3d,511is_color_blit ? 1u : 0u,512surfaces,513is_color_blit ? NULL : dst_surf,514src_surf);515job->msaa = msaa;516job->tile_width = tile_width;517job->tile_height = tile_height;518job->internal_bpp = max_bpp;519job->draw_min_x = info->dst.box.x;520job->draw_min_y = info->dst.box.y;521job->draw_max_x = info->dst.box.x + info->dst.box.width;522job->draw_max_y = info->dst.box.y + info->dst.box.height;523job->scissor.disabled = false;524525/* The simulator complains if we do a TLB load from a source with a526* stride that is smaller than the destination's, so we program the527* 'frame region' to match the smallest dimensions of the two surfaces.528* This should be fine because we only get here if the src and dst boxes529* match, so we know the blit involves the same tiles on both surfaces.530*/531job->draw_width = MIN2(dst_surf->width, src_surf->width);532job->draw_height = MIN2(dst_surf->height, src_surf->height);533job->draw_tiles_x = DIV_ROUND_UP(job->draw_width,534job->tile_width);535job->draw_tiles_y = DIV_ROUND_UP(job->draw_height,536job->tile_height);537538job->needs_flush = true;539job->num_layers = info->dst.box.depth;540541job->store = 0;542if (is_color_blit) {543job->store |= PIPE_CLEAR_COLOR0;544info->mask &= ~PIPE_MASK_RGBA;545}546if (is_depth_blit) {547job->store |= PIPE_CLEAR_DEPTH;548info->mask &= ~PIPE_MASK_Z;549}550if (is_stencil_blit){551job->store |= PIPE_CLEAR_STENCIL;552info->mask &= ~PIPE_MASK_S;553}554555v3d41_start_binning(v3d, job);556557v3d_job_submit(v3d, job);558559pipe_surface_reference(&dst_surf, NULL);560pipe_surface_reference(&src_surf, NULL);561}562563/**564* Creates the VS of the custom blit shader to convert YUV plane from565* the NV12 format with BROADCOM_SAND_COL128 modifier to UIF tiled format.566* This vertex shader is mostly a pass-through VS.567*/568static void *569v3d_get_sand8_vs(struct pipe_context *pctx)570{571struct v3d_context *v3d = v3d_context(pctx);572struct pipe_screen *pscreen = pctx->screen;573574if (v3d->sand8_blit_vs)575return v3d->sand8_blit_vs;576577const struct nir_shader_compiler_options *options =578pscreen->get_compiler_options(pscreen,579PIPE_SHADER_IR_NIR,580PIPE_SHADER_VERTEX);581582nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_VERTEX,583options,584"sand8_blit_vs");585586const struct glsl_type *vec4 = glsl_vec4_type();587nir_variable *pos_in = nir_variable_create(b.shader,588nir_var_shader_in,589vec4, "pos");590591nir_variable *pos_out = nir_variable_create(b.shader,592nir_var_shader_out,593vec4, "gl_Position");594pos_out->data.location = VARYING_SLOT_POS;595nir_store_var(&b, pos_out, nir_load_var(&b, pos_in), 0xf);596597struct pipe_shader_state shader_tmpl = {598.type = PIPE_SHADER_IR_NIR,599.ir.nir = b.shader,600};601602v3d->sand8_blit_vs = pctx->create_vs_state(pctx, &shader_tmpl);603604return v3d->sand8_blit_vs;605}606/**607* Creates the FS of the custom blit shader to convert YUV plane from608* the NV12 format with BROADCOM_SAND_COL128 modifier to UIF tiled format.609* The result texture is equivalent to a chroma (cpp=2) or luma (cpp=1)610* plane for a NV12 format without the SAND modifier.611*/612static void *613v3d_get_sand8_fs(struct pipe_context *pctx, int cpp)614{615struct v3d_context *v3d = v3d_context(pctx);616struct pipe_screen *pscreen = pctx->screen;617struct pipe_shader_state **cached_shader;618const char *name;619620if (cpp == 1) {621cached_shader = &v3d->sand8_blit_fs_luma;622name = "sand8_blit_fs_luma";623} else {624cached_shader = &v3d->sand8_blit_fs_chroma;625name = "sand8_blit_fs_chroma";626}627628if (*cached_shader)629return *cached_shader;630631const struct nir_shader_compiler_options *options =632pscreen->get_compiler_options(pscreen,633PIPE_SHADER_IR_NIR,634PIPE_SHADER_FRAGMENT);635636nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_FRAGMENT,637options, "%s", name);638const struct glsl_type *vec4 = glsl_vec4_type();639640const struct glsl_type *glsl_int = glsl_int_type();641642nir_variable *color_out =643nir_variable_create(b.shader, nir_var_shader_out,644vec4, "f_color");645color_out->data.location = FRAG_RESULT_COLOR;646647nir_variable *pos_in =648nir_variable_create(b.shader, nir_var_shader_in, vec4, "pos");649pos_in->data.location = VARYING_SLOT_POS;650nir_ssa_def *pos = nir_load_var(&b, pos_in);651652nir_ssa_def *zero = nir_imm_int(&b, 0);653nir_ssa_def *one = nir_imm_int(&b, 1);654nir_ssa_def *two = nir_imm_int(&b, 2);655nir_ssa_def *six = nir_imm_int(&b, 6);656nir_ssa_def *seven = nir_imm_int(&b, 7);657nir_ssa_def *eight = nir_imm_int(&b, 8);658659nir_ssa_def *x = nir_f2i32(&b, nir_channel(&b, pos, 0));660nir_ssa_def *y = nir_f2i32(&b, nir_channel(&b, pos, 1));661662nir_variable *stride_in =663nir_variable_create(b.shader, nir_var_uniform, glsl_int,664"sand8_stride");665nir_ssa_def *stride =666nir_load_uniform(&b, 1, 32, zero,667.base = stride_in->data.driver_location,668.range = 1,669.dest_type = nir_type_int32);670671nir_ssa_def *x_offset;672nir_ssa_def *y_offset;673674/* UIF tiled format is composed by UIF blocks, Each block has675* four 64 byte microtiles. Inside each microtile pixels are stored676* in raster format. But microtiles have different dimensions677* based in the bits per pixel of the image.678*679* 8bpp microtile dimensions are 8x8680* 16bpp microtile dimensions are 8x4681* 32bpp microtile dimensions are 4x4682*683* As we are reading and writing with 32bpp to optimize684* the number of texture operations during the blit. We need685* to adjust the offsets were we read and write as data will686* be later read using 8bpp (luma) and 16bpp (chroma).687*688* For chroma 8x4 16bpp raster order is compatible with 4x4689* 32bpp. In both layouts each line has 8*2 == 4*4 == 16 bytes.690* But luma 8x8 8bpp raster order is not compatible691* with 4x4 32bpp. 8bpp has 8 bytes per line, and 32bpp has692* 16 bytes per line. So if we read a 8bpp texture that was693* written as 32bpp texture. Bytes would be misplaced.694*695* inter/intra_utile_x_offests takes care of mapping the offsets696* between microtiles to deal with this issue for luma planes.697*/698if (cpp == 1) {699nir_ssa_def *intra_utile_x_offset =700nir_ishl(&b, nir_iand_imm(&b, x, 1), two);701nir_ssa_def *inter_utile_x_offset =702nir_ishl(&b, nir_iand_imm(&b, x, 60), one);703nir_ssa_def *stripe_offset=704nir_ishl(&b,nir_imul(&b,nir_ishr_imm(&b, x, 6),705stride),706seven);707708x_offset = nir_iadd(&b, stripe_offset,709nir_iadd(&b, intra_utile_x_offset,710inter_utile_x_offset));711y_offset = nir_iadd(&b,712nir_ishl(&b, nir_iand_imm(&b, x, 2), six),713nir_ishl(&b, y, eight));714} else {715nir_ssa_def *stripe_offset=716nir_ishl(&b,nir_imul(&b,nir_ishr_imm(&b, x, 5),717stride),718seven);719x_offset = nir_iadd(&b, stripe_offset,720nir_ishl(&b, nir_iand_imm(&b, x, 31), two));721y_offset = nir_ishl(&b, y, seven);722}723nir_ssa_def *ubo_offset = nir_iadd(&b, x_offset, y_offset);724nir_ssa_def *load =725nir_load_ubo(&b, 1, 32, one, ubo_offset,726.align_mul = 4,727.align_offset = 0,728.range_base = 0,729.range = ~0);730731nir_ssa_def *output = nir_unpack_unorm_4x8(&b, load);732733nir_store_var(&b, color_out,734output,7350xF);736737struct pipe_shader_state shader_tmpl = {738.type = PIPE_SHADER_IR_NIR,739.ir.nir = b.shader,740};741742*cached_shader = pctx->create_fs_state(pctx, &shader_tmpl);743744return *cached_shader;745}746747/**748* Turns NV12 with SAND8 format modifier from raster-order with interleaved749* luma and chroma 128-byte-wide-columns to tiled format for luma and chroma.750*751* This implementation is based on vc4_yuv_blit.752*/753static void754v3d_sand8_blit(struct pipe_context *pctx, struct pipe_blit_info *info)755{756struct v3d_context *v3d = v3d_context(pctx);757struct v3d_resource *src = v3d_resource(info->src.resource);758ASSERTED struct v3d_resource *dst = v3d_resource(info->dst.resource);759760if (!src->sand_col128_stride)761return;762if (src->tiled)763return;764if (src->base.format != PIPE_FORMAT_R8_UNORM &&765src->base.format != PIPE_FORMAT_R8G8_UNORM)766return;767if (!(info->mask & PIPE_MASK_RGBA))768return;769770assert(dst->base.format == src->base.format);771assert(dst->tiled);772773assert(info->src.box.x == 0 && info->dst.box.x == 0);774assert(info->src.box.y == 0 && info->dst.box.y == 0);775assert(info->src.box.width == info->dst.box.width);776assert(info->src.box.height == info->dst.box.height);777778v3d_blitter_save(v3d);779780struct pipe_surface dst_tmpl;781util_blitter_default_dst_texture(&dst_tmpl, info->dst.resource,782info->dst.level, info->dst.box.z);783/* Although the src textures are cpp=1 or cpp=2, the dst texture784* uses a cpp=4 dst texture. So, all read/write texture ops will785* be done using 32-bit read and writes.786*/787dst_tmpl.format = PIPE_FORMAT_R8G8B8A8_UNORM;788struct pipe_surface *dst_surf =789pctx->create_surface(pctx, info->dst.resource, &dst_tmpl);790if (!dst_surf) {791fprintf(stderr, "Failed to create YUV dst surface\n");792util_blitter_unset_running_flag(v3d->blitter);793return;794}795796uint32_t sand8_stride = src->sand_col128_stride;797798/* Adjust the dimensions of dst luma/chroma to match src799* size now we are using a cpp=4 format. Next dimension take into800* account the UIF microtile layouts.801*/802dst_surf->width = align(dst_surf->width, 8) / 2;803if (src->cpp == 1)804dst_surf->height /= 2;805806/* Set the constant buffer. */807struct pipe_constant_buffer cb_uniforms = {808.user_buffer = &sand8_stride,809.buffer_size = sizeof(sand8_stride),810};811812pctx->set_constant_buffer(pctx, PIPE_SHADER_FRAGMENT, 0, false,813&cb_uniforms);814struct pipe_constant_buffer cb_src = {815.buffer = info->src.resource,816.buffer_offset = src->slices[info->src.level].offset,817.buffer_size = (src->bo->size -818src->slices[info->src.level].offset),819};820pctx->set_constant_buffer(pctx, PIPE_SHADER_FRAGMENT, 2, false,821&cb_src);822/* Unbind the textures, to make sure we don't try to recurse into the823* shadow blit.824*/825pctx->set_sampler_views(pctx, PIPE_SHADER_FRAGMENT, 0, 0, 0, NULL);826pctx->bind_sampler_states(pctx, PIPE_SHADER_FRAGMENT, 0, 0, NULL);827828util_blitter_custom_shader(v3d->blitter, dst_surf,829v3d_get_sand8_vs(pctx),830v3d_get_sand8_fs(pctx, src->cpp));831832util_blitter_restore_textures(v3d->blitter);833util_blitter_restore_constant_buffer_state(v3d->blitter);834835/* Restore cb1 (util_blitter doesn't handle this one). */836struct pipe_constant_buffer cb_disabled = { 0 };837pctx->set_constant_buffer(pctx, PIPE_SHADER_FRAGMENT, 1, false,838&cb_disabled);839840pipe_surface_reference(&dst_surf, NULL);841842info->mask &= ~PIPE_MASK_RGBA;843return;844}845846847/* Optimal hardware path for blitting pixels.848* Scaling, format conversion, up- and downsampling (resolve) are allowed.849*/850void851v3d_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)852{853struct v3d_context *v3d = v3d_context(pctx);854struct pipe_blit_info info = *blit_info;855856v3d_sand8_blit(pctx, &info);857858v3d_tfu_blit(pctx, &info);859860v3d_tlb_blit(pctx, &info);861862v3d_stencil_blit(pctx, &info);863864v3d_render_blit(pctx, &info);865866/* Flush our blit jobs immediately. They're unlikely to get reused by867* normal drawing or other blits, and without flushing we can easily868* run into unexpected OOMs when blits are used for a large series of869* texture uploads before using the textures.870*/871v3d_flush_jobs_writing_resource(v3d, info.dst.resource,872V3D_FLUSH_DEFAULT, false);873}874875876