diff --git a/src/compiler/nir/nir_inline_uniforms.c b/src/compiler/nir/nir_inline_uniforms.c index 691721a95a4..0b33600ed8c 100644 --- a/src/compiler/nir/nir_inline_uniforms.c +++ b/src/compiler/nir/nir_inline_uniforms.c @@ -380,6 +380,86 @@ nir_find_inlinable_uniforms(nir_shader *shader) shader->info.num_inlinable_uniforms = num_offsets[0]; } +typedef struct { + unsigned num_uniforms; + const uint32_t *uniform_values; + const uint16_t *uniform_dw_offsets; +} nir_inline_uniforms_state; + +static bool +nir_inline_uniforms_impl(nir_builder *b, nir_intrinsic_instr *intr, void *state) +{ + const nir_inline_uniforms_state *s = state; + + /* Only replace UBO 0 with constant offsets. */ + if (intr->intrinsic == nir_intrinsic_load_ubo && + nir_src_is_const(intr->src[0]) && + nir_src_as_uint(intr->src[0]) == 0 && + nir_src_is_const(intr->src[1]) && + /* TODO: Can't handle other bit sizes for now. */ + intr->def.bit_size == 32) { + int num_components = intr->def.num_components; + uint32_t offset = nir_src_as_uint(intr->src[1]) / 4; + + if (num_components == 1) { + /* Just replace the uniform load to constant load. */ + for (unsigned i = 0; i < s->num_uniforms; i++) { + if (offset == s->uniform_dw_offsets[i]) { + b->cursor = nir_before_instr(&intr->instr); + nir_def *def = nir_imm_int(b, s->uniform_values[i]); + nir_def_replace(&intr->def, def); + return true; + } + } + } else { + /* Lower vector uniform load to scalar and replace each + * found component load with constant load. + */ + uint32_t max_offset = offset + num_components; + nir_def *components[NIR_MAX_VEC_COMPONENTS] = { 0 }; + bool found = false; + + b->cursor = nir_before_instr(&intr->instr); + + /* Find component to replace. */ + for (unsigned i = 0; i < s->num_uniforms; i++) { + uint32_t uni_offset = s->uniform_dw_offsets[i]; + if (uni_offset >= offset && uni_offset < max_offset) { + int index = uni_offset - offset; + components[index] = nir_imm_int(b, s->uniform_values[i]); + found = true; + } + } + + if (!found) + return false; + + /* Create per-component uniform load. */ + for (unsigned i = 0; i < num_components; i++) { + if (!components[i]) { + uint32_t scalar_offset = (offset + i) * 4; + components[i] = nir_load_ubo(b, 1, intr->def.bit_size, + intr->src[0].ssa, + nir_imm_int(b, scalar_offset)); + nir_intrinsic_instr *load = + nir_instr_as_intrinsic(components[i]->parent_instr); + nir_intrinsic_set_align(load, NIR_ALIGN_MUL_MAX, scalar_offset); + nir_intrinsic_set_range_base(load, scalar_offset); + nir_intrinsic_set_range(load, 4); + } + } + + /* Replace the original uniform load. */ + nir_def_replace(&intr->def, + nir_vec(b, components, num_components)); + + return true; + } + } + + return false; +} + bool nir_inline_uniforms(nir_shader *shader, unsigned num_uniforms, const uint32_t *uniform_values, @@ -388,83 +468,12 @@ nir_inline_uniforms(nir_shader *shader, unsigned num_uniforms, if (!num_uniforms) return false; - nir_foreach_function_impl(impl, shader) { - nir_builder b = nir_builder_create(impl); - nir_foreach_block(block, impl) { - nir_foreach_instr_safe(instr, block) { - if (instr->type != nir_instr_type_intrinsic) - continue; + nir_inline_uniforms_state state = { + .num_uniforms = num_uniforms, + .uniform_values = uniform_values, + .uniform_dw_offsets = uniform_dw_offsets, + }; - nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); - - /* Only replace UBO 0 with constant offsets. */ - if (intr->intrinsic == nir_intrinsic_load_ubo && - nir_src_is_const(intr->src[0]) && - nir_src_as_uint(intr->src[0]) == 0 && - nir_src_is_const(intr->src[1]) && - /* TODO: Can't handle other bit sizes for now. */ - intr->def.bit_size == 32) { - int num_components = intr->def.num_components; - uint32_t offset = nir_src_as_uint(intr->src[1]) / 4; - - if (num_components == 1) { - /* Just replace the uniform load to constant load. */ - for (unsigned i = 0; i < num_uniforms; i++) { - if (offset == uniform_dw_offsets[i]) { - b.cursor = nir_before_instr(&intr->instr); - nir_def *def = nir_imm_int(&b, uniform_values[i]); - nir_def_replace(&intr->def, def); - break; - } - } - } else { - /* Lower vector uniform load to scalar and replace each - * found component load with constant load. - */ - uint32_t max_offset = offset + num_components; - nir_def *components[NIR_MAX_VEC_COMPONENTS] = { 0 }; - bool found = false; - - b.cursor = nir_before_instr(&intr->instr); - - /* Find component to replace. */ - for (unsigned i = 0; i < num_uniforms; i++) { - uint32_t uni_offset = uniform_dw_offsets[i]; - if (uni_offset >= offset && uni_offset < max_offset) { - int index = uni_offset - offset; - components[index] = nir_imm_int(&b, uniform_values[i]); - found = true; - } - } - - if (!found) - continue; - - /* Create per-component uniform load. */ - for (unsigned i = 0; i < num_components; i++) { - if (!components[i]) { - uint32_t scalar_offset = (offset + i) * 4; - components[i] = nir_load_ubo(&b, 1, intr->def.bit_size, - intr->src[0].ssa, - nir_imm_int(&b, scalar_offset)); - nir_intrinsic_instr *load = - nir_instr_as_intrinsic(components[i]->parent_instr); - nir_intrinsic_set_align(load, NIR_ALIGN_MUL_MAX, scalar_offset); - nir_intrinsic_set_range_base(load, scalar_offset); - nir_intrinsic_set_range(load, 4); - } - } - - /* Replace the original uniform load. */ - nir_def_replace(&intr->def, - nir_vec(&b, components, num_components)); - } - } - } - - nir_progress(true, impl, nir_metadata_control_flow); - } - } - - return true; + return nir_shader_intrinsics_pass(shader, nir_inline_uniforms_impl, + nir_metadata_control_flow, &state); }