From baa301d5f783d3bf35aef7a5a1dd5bf57ffb5bbb Mon Sep 17 00:00:00 2001 From: Timothy Arceri Date: Tue, 23 Jul 2024 13:07:34 +1000 Subject: [PATCH] glsl: make use of new tex src deref intrinsic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The bindless spec has no language requiring functions params to be defined as bindless so we need to be able to look at the values being passed to functions to decide if they are bindless or not. This intrinsic allows us to wait until function inlining is complete to make this assessment. Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/11535 Acked-by: Marek Olšák Part-of: (cherry picked from commit a629d829dc8d8fa9ac8c8b22da130a19e61d40a6) --- .pick_status.json | 2 +- src/compiler/glsl/gl_nir_linker.c | 53 +++++++++++++++++++++++++++++-- src/compiler/glsl/glsl_to_nir.cpp | 18 +++-------- 3 files changed, 57 insertions(+), 16 deletions(-) diff --git a/.pick_status.json b/.pick_status.json index fb2b6d08a52..56998feef63 100644 --- a/.pick_status.json +++ b/.pick_status.json @@ -13814,7 +13814,7 @@ "description": "glsl: make use of new tex src deref intrinsic", "nominated": false, "nomination_type": 3, - "resolution": 4, + "resolution": 1, "main_sha": null, "because_sha": null, "notes": null diff --git a/src/compiler/glsl/gl_nir_linker.c b/src/compiler/glsl/gl_nir_linker.c index df0f70e7fd2..9a585857671 100644 --- a/src/compiler/glsl/gl_nir_linker.c +++ b/src/compiler/glsl/gl_nir_linker.c @@ -133,6 +133,15 @@ gl_nir_opts(nir_shader *nir) NIR_PASS(_, nir, nir_lower_var_copies); } +static void +replace_tex_src(nir_tex_src *dst, nir_tex_src_type src_type, nir_def *src_def, + nir_instr *src_parent) +{ + *dst = nir_tex_src_for_ssa(src_type, src_def); + nir_src_set_parent_instr(&dst->src, src_parent); + list_addtail(&dst->src.use_link, &dst->src.ssa->uses); +} + void gl_nir_inline_functions(nir_shader *shader) { @@ -145,14 +154,54 @@ gl_nir_inline_functions(nir_shader *shader) NIR_PASS(_, shader, nir_inline_functions); NIR_PASS(_, shader, nir_opt_deref); - nir_validate_shader(shader, "after function inlining and return lowering"); - /* We set func->is_entrypoint after nir_function_create if the function * is named "main", so we can use nir_remove_non_entrypoints() for this. * Now that we have inlined everything remove all of the functions except * func->is_entrypoint. */ nir_remove_non_entrypoints(shader); + + /* Now that functions have been inlined remove deref_texture_src intrinisic + * as we can now see if the texture source is bindless or not. + */ + nir_function_impl *impl = nir_shader_get_entrypoint(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_tex) { + nir_tex_instr *intr = nir_instr_as_tex(instr); + + b.cursor = nir_before_instr(instr); + + if (intr->src[0].src_type == nir_tex_src_sampler_deref_intrinsic) { + assert(intr->src[1].src_type == nir_tex_src_texture_deref_intrinsic); + nir_intrinsic_instr *intrin = + nir_instr_as_intrinsic(intr->src[0].src.ssa->parent_instr); + nir_deref_instr *deref = + nir_instr_as_deref(intrin->src[0].ssa->parent_instr); + + /* check for bindless handles */ + if (!nir_deref_mode_is(deref, nir_var_uniform) || + nir_deref_instr_get_variable(deref)->data.bindless) { + nir_def *load = nir_load_deref(&b, deref); + replace_tex_src(&intr->src[0], nir_tex_src_texture_handle, + load, instr); + replace_tex_src(&intr->src[1], nir_tex_src_sampler_handle, + load, instr); + } else { + replace_tex_src(&intr->src[0], nir_tex_src_texture_deref, + &deref->def, instr); + replace_tex_src(&intr->src[1], nir_tex_src_sampler_deref, + &deref->def, instr); + } + nir_instr_remove(&intrin->instr); + } + } + } + } + + nir_validate_shader(shader, "after function inlining and return lowering"); } struct emit_vertex_state { diff --git a/src/compiler/glsl/glsl_to_nir.cpp b/src/compiler/glsl/glsl_to_nir.cpp index b79565bf371..f37451ab039 100644 --- a/src/compiler/glsl/glsl_to_nir.cpp +++ b/src/compiler/glsl/glsl_to_nir.cpp @@ -2395,20 +2395,12 @@ nir_visitor::visit(ir_texture *ir) instr->is_sparse = ir->is_sparse; nir_deref_instr *sampler_deref = evaluate_deref(ir->sampler); + nir_def *tex_intrin = nir_deref_texture_src(&b, 32, &sampler_deref->def); - /* check for bindless handles */ - if (!nir_deref_mode_is(sampler_deref, nir_var_uniform) || - (nir_deref_instr_get_variable(sampler_deref) && - nir_deref_instr_get_variable(sampler_deref)->data.bindless)) { - nir_def *load = nir_load_deref(&b, sampler_deref); - instr->src[0] = nir_tex_src_for_ssa(nir_tex_src_texture_handle, load); - instr->src[1] = nir_tex_src_for_ssa(nir_tex_src_sampler_handle, load); - } else { - instr->src[0] = nir_tex_src_for_ssa(nir_tex_src_texture_deref, - &sampler_deref->def); - instr->src[1] = nir_tex_src_for_ssa(nir_tex_src_sampler_deref, - &sampler_deref->def); - } + instr->src[0] = nir_tex_src_for_ssa(nir_tex_src_sampler_deref_intrinsic, + tex_intrin); + instr->src[1] = nir_tex_src_for_ssa(nir_tex_src_texture_deref_intrinsic, + tex_intrin); unsigned src_number = 2;