Path: blob/21.2-virgl/src/gallium/drivers/zink/nir_lower_dynamic_bo_access.c
4570 views
/*1* Copyright © 2020 Mike Blumenkrantz2*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*22* Authors:23* Mike Blumenkrantz <[email protected]>24*/2526#include "nir.h"27#include "nir_builder.h"2829bool nir_lower_dynamic_bo_access(nir_shader *shader);30/**31* This pass converts dynamic UBO/SSBO block indices to constant indices by generating32* conditional chains which reduce to single values.33*34* This is needed by anything which intends to convert GLSL-like shaders to SPIRV,35* as SPIRV requires explicit load points for UBO/SSBO variables and has no instruction for36* loading based on an offset in the underlying driver's binding table37*/383940/* generate a single ssa value which conditionally selects the right value that41* was previously loaded by the load_ubo conditional chain42*/43static nir_ssa_def *44recursive_generate_bo_ssa_def(nir_builder *b, nir_intrinsic_instr *instr, nir_ssa_def *index, unsigned start, unsigned end)45{46if (start == end - 1) {47nir_intrinsic_instr *new_instr = nir_intrinsic_instr_create(b->shader, instr->intrinsic);48new_instr->src[0] = nir_src_for_ssa(nir_imm_int(b, start));49for (unsigned i = 0; i < nir_intrinsic_infos[instr->intrinsic].num_srcs; i++) {50if (i)51nir_src_copy(&new_instr->src[i], &instr->src[i], &new_instr->instr);52}53if (instr->intrinsic != nir_intrinsic_load_ubo_vec4) {54nir_intrinsic_set_align(new_instr, nir_intrinsic_align_mul(instr), nir_intrinsic_align_offset(instr));55if (instr->intrinsic != nir_intrinsic_load_ssbo)56nir_intrinsic_set_range(new_instr, nir_intrinsic_range(instr));57}58new_instr->num_components = instr->num_components;59nir_ssa_dest_init(&new_instr->instr, &new_instr->dest,60nir_dest_num_components(instr->dest),61nir_dest_bit_size(instr->dest), NULL);62nir_builder_instr_insert(b, &new_instr->instr);63return &new_instr->dest.ssa;64}6566unsigned mid = start + (end - start) / 2;67return nir_build_alu(b, nir_op_bcsel, nir_build_alu(b, nir_op_ilt, index, nir_imm_int(b, mid), NULL, NULL),68recursive_generate_bo_ssa_def(b, instr, index, start, mid),69recursive_generate_bo_ssa_def(b, instr, index, mid, end),70NULL71);72}7374static void75generate_store_ssbo_ssa_def(nir_builder *b, nir_intrinsic_instr *instr, nir_ssa_def *index, unsigned start, unsigned end)76{77if (start == end - 1) {78nir_intrinsic_instr *new_instr = nir_instr_as_intrinsic(nir_instr_clone(b->shader, &instr->instr));79new_instr->src[1] = nir_src_for_ssa(nir_imm_int(b, start));80nir_builder_instr_insert(b, &new_instr->instr);81} else {82int mid = start + (end - start) / 2;83nir_ssa_def *mid_idx = nir_imm_int(b, mid);84nir_push_if(b, nir_ilt(b, index, mid_idx));85generate_store_ssbo_ssa_def(b, instr, index, start, mid);86nir_push_else(b, NULL);87generate_store_ssbo_ssa_def(b, instr, index, mid, end);88nir_pop_if(b, NULL);89}90}9192static bool93lower_dynamic_bo_access_instr(nir_intrinsic_instr *instr, nir_builder *b)94{95if (instr->intrinsic != nir_intrinsic_load_ubo &&96instr->intrinsic != nir_intrinsic_load_ubo_vec4 &&97instr->intrinsic != nir_intrinsic_get_ssbo_size &&98instr->intrinsic != nir_intrinsic_load_ssbo &&99instr->intrinsic != nir_intrinsic_store_ssbo)100return false;101/* block index src is 1 for this op */102unsigned block_idx = instr->intrinsic == nir_intrinsic_store_ssbo;103if (nir_src_is_const(instr->src[block_idx]))104return false;105b->cursor = nir_after_instr(&instr->instr);106bool ssbo_mode = instr->intrinsic != nir_intrinsic_load_ubo && instr->intrinsic != nir_intrinsic_load_ubo_vec4;107unsigned first_idx = UINT_MAX, last_idx;108if (ssbo_mode) {109nir_foreach_variable_with_modes(var, b->shader, nir_var_mem_ssbo)110first_idx = MIN2(first_idx, var->data.driver_location);111last_idx = first_idx + b->shader->info.num_ssbos;112} else {113/* skip 0 index if uniform_0 is one we created previously */114first_idx = !b->shader->info.first_ubo_is_default_ubo;115last_idx = first_idx + b->shader->info.num_ubos;116}117118if (instr->intrinsic != nir_intrinsic_store_ssbo) {119/* now create the composite dest with a bcsel chain based on the original value */120nir_ssa_def *new_dest = recursive_generate_bo_ssa_def(b, instr,121instr->src[block_idx].ssa,122first_idx, last_idx);123124/* now use the composite dest in all cases where the original dest (from the dynamic index)125* was used and remove the dynamically-indexed load_*bo instruction126*/127nir_ssa_def_rewrite_uses_after(&instr->dest.ssa, new_dest,128&instr->instr);129} else130generate_store_ssbo_ssa_def(b, instr, instr->src[block_idx].ssa, first_idx, last_idx);131nir_instr_remove(&instr->instr);132133return true;134}135136bool137nir_lower_dynamic_bo_access(nir_shader *shader)138{139bool progress = false;140141nir_foreach_function(function, shader) {142if (function->impl) {143nir_builder builder;144nir_builder_init(&builder, function->impl);145nir_foreach_block(block, function->impl) {146nir_foreach_instr_safe(instr, block) {147if (instr->type == nir_instr_type_intrinsic)148progress |= lower_dynamic_bo_access_instr(149nir_instr_as_intrinsic(instr),150&builder);151}152}153154nir_metadata_preserve(function->impl, nir_metadata_dominance);155}156}157158return progress;159}160161162