Path: blob/21.2-virgl/src/freedreno/ir3/ir3_nir_lower_tex_prefetch.c
4565 views
/*1* Copyright © 2019 Igalia S.L.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, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "ir3_nir.h"2425/**26* A pass which detects tex instructions which are candidate to be executed27* prior to FS shader start, and change them to nir_texop_tex_prefetch.28*/2930static int31coord_offset(nir_ssa_def *ssa)32{33nir_instr *parent_instr = ssa->parent_instr;3435/* The coordinate of a texture sampling instruction eligible for36* pre-fetch is either going to be a load_interpolated_input/37* load_input, or a vec2 assembling non-swizzled components of38* a load_interpolated_input/load_input (due to varying packing)39*/4041if (parent_instr->type == nir_instr_type_alu) {42nir_alu_instr *alu = nir_instr_as_alu(parent_instr);4344if (alu->op != nir_op_vec2)45return -1;4647if (!alu->src[0].src.is_ssa)48return -1;4950int base_offset =51coord_offset(alu->src[0].src.ssa) + alu->src[0].swizzle[0];5253/* NOTE it might be possible to support more than 2D? */54for (int i = 1; i < 2; i++) {55if (!alu->src[i].src.is_ssa)56return -1;5758int nth_offset =59coord_offset(alu->src[i].src.ssa) + alu->src[i].swizzle[0];6061if (nth_offset != (base_offset + i))62return -1;63}6465return base_offset;66}6768if (parent_instr->type != nir_instr_type_intrinsic)69return -1;7071nir_intrinsic_instr *input = nir_instr_as_intrinsic(parent_instr);7273if (input->intrinsic != nir_intrinsic_load_interpolated_input)74return -1;7576/* limit to load_barycentric_pixel, other interpolation modes don't seem77* to be supported:78*/79if (!input->src[0].is_ssa)80return -1;8182nir_intrinsic_instr *interp =83nir_instr_as_intrinsic(input->src[0].ssa->parent_instr);8485if (interp->intrinsic != nir_intrinsic_load_barycentric_pixel)86return -1;8788/* we also need a const input offset: */89if (!nir_src_is_const(input->src[1]))90return -1;9192unsigned base = nir_src_as_uint(input->src[1]) + nir_intrinsic_base(input);93unsigned comp = nir_intrinsic_component(input);9495return (4 * base) + comp;96}9798int99ir3_nir_coord_offset(nir_ssa_def *ssa)100{101102assert(ssa->num_components == 2);103return coord_offset(ssa);104}105106static bool107has_src(nir_tex_instr *tex, nir_tex_src_type type)108{109return nir_tex_instr_src_index(tex, type) >= 0;110}111112static bool113ok_bindless_src(nir_tex_instr *tex, nir_tex_src_type type)114{115int idx = nir_tex_instr_src_index(tex, type);116assert(idx >= 0);117nir_intrinsic_instr *bindless = ir3_bindless_resource(tex->src[idx].src);118119/* TODO from SP_FS_BINDLESS_PREFETCH[n] it looks like this limit should120* be 1<<8 ?121*/122return nir_src_is_const(bindless->src[0]) &&123(nir_src_as_uint(bindless->src[0]) < (1 << 16));124}125126/**127* Check that we will be able to encode the tex/samp parameters128* successfully. These limits are based on the layout of129* SP_FS_PREFETCH[n] and SP_FS_BINDLESS_PREFETCH[n], so at some130* point (if those regs changes) they may become generation131* specific.132*/133static bool134ok_tex_samp(nir_tex_instr *tex)135{136if (has_src(tex, nir_tex_src_texture_handle)) {137/* bindless case: */138139assert(has_src(tex, nir_tex_src_sampler_handle));140141return ok_bindless_src(tex, nir_tex_src_texture_handle) &&142ok_bindless_src(tex, nir_tex_src_sampler_handle);143} else {144assert(!has_src(tex, nir_tex_src_texture_offset));145assert(!has_src(tex, nir_tex_src_sampler_offset));146147return (tex->texture_index <= 0x1f) && (tex->sampler_index <= 0xf);148}149}150151static bool152lower_tex_prefetch_block(nir_block *block)153{154bool progress = false;155156nir_foreach_instr_safe (instr, block) {157if (instr->type != nir_instr_type_tex)158continue;159160nir_tex_instr *tex = nir_instr_as_tex(instr);161if (tex->op != nir_texop_tex)162continue;163164if (has_src(tex, nir_tex_src_bias) || has_src(tex, nir_tex_src_lod) ||165has_src(tex, nir_tex_src_comparator) ||166has_src(tex, nir_tex_src_projector) ||167has_src(tex, nir_tex_src_offset) || has_src(tex, nir_tex_src_ddx) ||168has_src(tex, nir_tex_src_ddy) || has_src(tex, nir_tex_src_ms_index) ||169has_src(tex, nir_tex_src_texture_offset) ||170has_src(tex, nir_tex_src_sampler_offset))171continue;172173/* only prefetch for simple 2d tex fetch case */174if (tex->sampler_dim != GLSL_SAMPLER_DIM_2D || tex->is_array)175continue;176177if (!ok_tex_samp(tex))178continue;179180int idx = nir_tex_instr_src_index(tex, nir_tex_src_coord);181/* First source should be the sampling coordinate. */182nir_tex_src *coord = &tex->src[idx];183debug_assert(coord->src.is_ssa);184185if (ir3_nir_coord_offset(coord->src.ssa) >= 0) {186tex->op = nir_texop_tex_prefetch;187188progress |= true;189}190}191192return progress;193}194195static bool196lower_tex_prefetch_func(nir_function_impl *impl)197{198/* Only instructions in the the outer-most block are considered199* eligible for pre-dispatch, because they need to be move-able200* to the beginning of the shader to avoid locking down the201* register holding the pre-fetched result for too long.202*/203nir_block *block = nir_start_block(impl);204if (!block)205return false;206207bool progress = lower_tex_prefetch_block(block);208209if (progress) {210nir_metadata_preserve(impl,211nir_metadata_block_index | nir_metadata_dominance);212}213214return progress;215}216217bool218ir3_nir_lower_tex_prefetch(nir_shader *shader)219{220bool progress = false;221222assert(shader->info.stage == MESA_SHADER_FRAGMENT);223224nir_foreach_function (function, shader) {225/* Only texture sampling instructions inside the main function226* are eligible for pre-dispatch.227*/228if (!function->impl || !function->is_entrypoint)229continue;230231progress |= lower_tex_prefetch_func(function->impl);232}233234return progress;235}236237238