nir: Use nir_shader_instructions_pass in nir_lower_input_attachments

This simplifies things a bit and also fixes metadata handling.

Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16482>
This commit is contained in:
Jason Ekstrand 2022-05-12 14:34:59 -05:00 committed by Marge Bot
parent a170448a18
commit 98cc4c3a20
1 changed files with 53 additions and 59 deletions

View File

@ -77,7 +77,7 @@ load_layer_id(nir_builder *b, const nir_input_attachment_options *options)
}
static bool
try_lower_input_load(nir_function_impl *impl, nir_intrinsic_instr *load,
try_lower_input_load(nir_builder *b, nir_intrinsic_instr *load,
const nir_input_attachment_options *options)
{
nir_deref_instr *deref = nir_src_as_deref(load->src[0]);
@ -90,20 +90,18 @@ try_lower_input_load(nir_function_impl *impl, nir_intrinsic_instr *load,
const bool multisampled = (image_dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
nir_builder b;
nir_builder_init(&b, impl);
b.cursor = nir_instr_remove(&load->instr);
b->cursor = nir_instr_remove(&load->instr);
nir_ssa_def *frag_coord = load_frag_coord(&b, options);
frag_coord = nir_f2i32(&b, frag_coord);
nir_ssa_def *offset = nir_ssa_for_src(&b, load->src[1], 2);
nir_ssa_def *pos = nir_iadd(&b, frag_coord, offset);
nir_ssa_def *frag_coord = load_frag_coord(b, options);
frag_coord = nir_f2i32(b, frag_coord);
nir_ssa_def *offset = nir_ssa_for_src(b, load->src[1], 2);
nir_ssa_def *pos = nir_iadd(b, frag_coord, offset);
nir_ssa_def *layer = load_layer_id(&b, options);
nir_ssa_def *layer = load_layer_id(b, options);
nir_ssa_def *coord =
nir_vec3(&b, nir_channel(&b, pos, 0), nir_channel(&b, pos, 1), layer);
nir_vec3(b, nir_channel(b, pos, 0), nir_channel(b, pos, 1), layer);
nir_tex_instr *tex = nir_tex_instr_create(b.shader, 3 + multisampled);
nir_tex_instr *tex = nir_tex_instr_create(b->shader, 3 + multisampled);
tex->op = nir_texop_txf;
tex->sampler_dim = image_dim;
@ -125,7 +123,7 @@ try_lower_input_load(nir_function_impl *impl, nir_intrinsic_instr *load,
tex->coord_components = 3;
tex->src[2].src_type = nir_tex_src_lod;
tex->src[2].src = nir_src_for_ssa(nir_imm_int(&b, 0));
tex->src[2].src = nir_src_for_ssa(nir_imm_int(b, 0));
if (image_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) {
tex->op = nir_texop_txf_ms;
@ -136,13 +134,13 @@ try_lower_input_load(nir_function_impl *impl, nir_intrinsic_instr *load,
tex->texture_non_uniform = nir_intrinsic_access(load) & ACCESS_NON_UNIFORM;
nir_ssa_dest_init(&tex->instr, &tex->dest, nir_tex_instr_dest_size(tex), 32, NULL);
nir_builder_instr_insert(&b, &tex->instr);
nir_builder_instr_insert(b, &tex->instr);
if (tex->is_sparse) {
unsigned load_result_size = load->dest.ssa.num_components - 1;
nir_component_mask_t load_result_mask = nir_component_mask(load_result_size);
nir_ssa_def *res = nir_channels(
&b, &tex->dest.ssa, load_result_mask | 0x10);
b, &tex->dest.ssa, load_result_mask | 0x10);
nir_ssa_def_rewrite_uses(&load->dest.ssa, res);
} else {
@ -154,7 +152,7 @@ try_lower_input_load(nir_function_impl *impl, nir_intrinsic_instr *load,
}
static bool
try_lower_input_texop(nir_function_impl *impl, nir_tex_instr *tex,
try_lower_input_texop(nir_builder *b, nir_tex_instr *tex,
const nir_input_attachment_options *options)
{
nir_deref_instr *deref = nir_src_as_deref(tex->src[0].src);
@ -162,16 +160,14 @@ try_lower_input_texop(nir_function_impl *impl, nir_tex_instr *tex,
if (glsl_get_sampler_dim(deref->type) != GLSL_SAMPLER_DIM_SUBPASS_MS)
return false;
nir_builder b;
nir_builder_init(&b, impl);
b.cursor = nir_before_instr(&tex->instr);
b->cursor = nir_before_instr(&tex->instr);
nir_ssa_def *frag_coord = load_frag_coord(&b, options);
frag_coord = nir_f2i32(&b, frag_coord);
nir_ssa_def *frag_coord = load_frag_coord(b, options);
frag_coord = nir_f2i32(b, frag_coord);
nir_ssa_def *layer = load_layer_id(&b, options);
nir_ssa_def *coord = nir_vec3(&b, nir_channel(&b, frag_coord, 0),
nir_channel(&b, frag_coord, 1), layer);
nir_ssa_def *layer = load_layer_id(b, options);
nir_ssa_def *coord = nir_vec3(b, nir_channel(b, frag_coord, 0),
nir_channel(b, frag_coord, 1), layer);
tex->coord_components = 3;
@ -180,46 +176,44 @@ try_lower_input_texop(nir_function_impl *impl, nir_tex_instr *tex,
return true;
}
static bool
lower_input_attachments_instr(nir_builder *b, nir_instr *instr, void *_data)
{
const nir_input_attachment_options *options = _data;
switch (instr->type) {
case nir_instr_type_tex: {
nir_tex_instr *tex = nir_instr_as_tex(instr);
if (tex->op == nir_texop_fragment_mask_fetch_amd ||
tex->op == nir_texop_fragment_fetch_amd)
return try_lower_input_texop(b, tex, options);
return false;
}
case nir_instr_type_intrinsic: {
nir_intrinsic_instr *load = nir_instr_as_intrinsic(instr);
if (load->intrinsic == nir_intrinsic_image_deref_load ||
load->intrinsic == nir_intrinsic_image_deref_sparse_load)
return try_lower_input_load(b, load, options);
return false;
}
default:
return false;
}
}
bool
nir_lower_input_attachments(nir_shader *shader,
const nir_input_attachment_options *options)
{
assert(shader->info.stage == MESA_SHADER_FRAGMENT);
bool progress = false;
nir_foreach_function(function, shader) {
if (!function->impl)
continue;
nir_foreach_block(block, function->impl) {
nir_foreach_instr_safe(instr, block) {
switch (instr->type) {
case nir_instr_type_tex: {
nir_tex_instr *tex = nir_instr_as_tex(instr);
if (tex->op == nir_texop_fragment_mask_fetch_amd ||
tex->op == nir_texop_fragment_fetch_amd) {
progress |= try_lower_input_texop(function->impl, tex,
options);
}
break;
}
case nir_instr_type_intrinsic: {
nir_intrinsic_instr *load = nir_instr_as_intrinsic(instr);
if (load->intrinsic == nir_intrinsic_image_deref_load ||
load->intrinsic == nir_intrinsic_image_deref_sparse_load) {
progress |= try_lower_input_load(function->impl, load,
options);
}
break;
}
default:
break;
}
}
}
}
return progress;
return nir_shader_instructions_pass(shader, lower_input_attachments_instr,
nir_metadata_block_index |
nir_metadata_dominance,
(void *)options);
}