From b9be1f1f20d42d736f64ce95fc9240483958e1fd Mon Sep 17 00:00:00 2001 From: Caterina Shablia Date: Thu, 14 Nov 2024 22:46:18 +0000 Subject: [PATCH] nir: introduce instance_index system value The semantics of this newly introduced system value match Vulkan's InstanceIndex exactly, and are equivalent to instance_id + base_instance. Some hardware, such as Mali Valhall or later, only provides instance id offset by base_instance. Introducing a new system value to represent this, rather than handling the mismatch when lowering to BIR lets us use NIR to eliminate redundant arithmetic that would follow from mismatched semantics, e.g. instance_id could be lowered to instance_index - base_instance, so expressions such as instance_id + base_instance would be optimized to a simple instance_index. Reviewed-by: Alyssa Rosenzweig Reviewed-by: Boris Brezillon Part-of: --- src/compiler/nir/nir_divergence_analysis.c | 1 + src/compiler/nir/nir_gather_info.c | 1 + src/compiler/nir/nir_intrinsics.py | 1 + src/compiler/nir/nir_opt_peephole_select.c | 1 + 4 files changed, 4 insertions(+) diff --git a/src/compiler/nir/nir_divergence_analysis.c b/src/compiler/nir/nir_divergence_analysis.c index a8fc33bc98e..d40486c724b 100644 --- a/src/compiler/nir/nir_divergence_analysis.c +++ b/src/compiler/nir/nir_divergence_analysis.c @@ -452,6 +452,7 @@ visit_intrinsic(nir_intrinsic_instr *instr, struct divergence_state *state) is_divergent |= !(options & nir_divergence_single_prim_per_subgroup); break; case nir_intrinsic_load_instance_id: + case nir_intrinsic_load_instance_index: is_divergent = !state->vertex_divergence; break; case nir_intrinsic_load_primitive_id: diff --git a/src/compiler/nir/nir_gather_info.c b/src/compiler/nir/nir_gather_info.c index a8621ab4a3f..301b06889d0 100644 --- a/src/compiler/nir/nir_gather_info.c +++ b/src/compiler/nir/nir_gather_info.c @@ -658,6 +658,7 @@ gather_intrinsic_info(nir_intrinsic_instr *instr, nir_shader *shader, case nir_intrinsic_load_subgroup_id: case nir_intrinsic_load_vertex_id: case nir_intrinsic_load_instance_id: + case nir_intrinsic_load_instance_index: case nir_intrinsic_load_vertex_id_zero_base: case nir_intrinsic_load_base_vertex: case nir_intrinsic_load_first_vertex: diff --git a/src/compiler/nir/nir_intrinsics.py b/src/compiler/nir/nir_intrinsics.py index 06fa6ecceef..c668e17f6f4 100644 --- a/src/compiler/nir/nir_intrinsics.py +++ b/src/compiler/nir/nir_intrinsics.py @@ -891,6 +891,7 @@ system_value("first_vertex", 1) system_value("is_indexed_draw", 1) system_value("base_vertex", 1) system_value("instance_id", 1) +system_value("instance_index", 1) system_value("base_instance", 1) system_value("draw_id", 1) system_value("sample_id", 1) diff --git a/src/compiler/nir/nir_opt_peephole_select.c b/src/compiler/nir/nir_opt_peephole_select.c index 202f84a18ff..946835c1beb 100644 --- a/src/compiler/nir/nir_opt_peephole_select.c +++ b/src/compiler/nir/nir_opt_peephole_select.c @@ -166,6 +166,7 @@ block_check_for_allowed_instrs(nir_block *block, unsigned *count, case nir_intrinsic_load_first_vertex: case nir_intrinsic_load_base_instance: case nir_intrinsic_load_instance_id: + case nir_intrinsic_load_instance_index: case nir_intrinsic_load_draw_id: case nir_intrinsic_load_num_workgroups: case nir_intrinsic_load_workgroup_id: