From 04be5a8910bfeedfbda350c252248a8d5c5e2d66 Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Fri, 27 Jun 2025 10:12:49 -0400 Subject: [PATCH] nir/lower_system_values: simplify load_helper_invocation lowering The two backends (ir3, dxil) using the lowering have info->fs.uses_sample_shading matching set when sample shading is used -- the VK drivers pass the rasterization state flag into the compiler, while freedreno and d3d12 have caps->force_persample_interp so the frontend creates a shader variant with info->fs.uses_sample_shading set. This means that we can drop the sample-id SHL/AND in the pixel-rate shading case, which in turn means that drivers don't need to have a load_sample_id() that doesn't trigger sample-rate shading (which Adreno doesn't have). Signed-off-by: Alyssa Rosenzweig Part-of: --- src/compiler/nir/nir_lower_system_values.c | 13 ++++++++----- src/compiler/nir/nir_shader_compiler_options.h | 9 ++++++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/compiler/nir/nir_lower_system_values.c b/src/compiler/nir/nir_lower_system_values.c index b73135aff46..5e23019a81a 100644 --- a/src/compiler/nir/nir_lower_system_values.c +++ b/src/compiler/nir/nir_lower_system_values.c @@ -347,11 +347,14 @@ lower_system_value_instr(nir_builder *b, nir_instr *instr, void *_state) nir_def * nir_build_lowered_load_helper_invocation(nir_builder *b) { - nir_def *tmp; - tmp = nir_ishl(b, nir_imm_int(b, 1), - nir_load_sample_id_no_per_sample(b)); - tmp = nir_iand(b, nir_load_sample_mask_in(b), tmp); - return nir_inot(b, nir_i2b(b, tmp)); + nir_def *mask = nir_load_sample_mask_in(b); + + if (b->shader->info.fs.uses_sample_shading) { + nir_def *id = nir_load_sample_id(b); + mask = nir_iand(b, mask, nir_ishl(b, nir_imm_int(b, 1), id)); + } + + return nir_ieq_imm(b, mask, 0); } bool diff --git a/src/compiler/nir/nir_shader_compiler_options.h b/src/compiler/nir/nir_shader_compiler_options.h index 525fcd02a45..f7d6ef07184 100644 --- a/src/compiler/nir/nir_shader_compiler_options.h +++ b/src/compiler/nir/nir_shader_compiler_options.h @@ -370,7 +370,14 @@ typedef struct nir_shader_compiler_options { /** * If enabled, gl_HelperInvocation will be lowered as: * - * !((1 << sample_id) & sample_mask_in)) + * - non-sample-shading: sample_mask_in == 0. + * - sample shading: !((1 << sample_id) & sample_mask_in)) + * + * For this to be correct, it requires that fs.uses_sample_shading is set to + * true when sample shading is enabled. This means that you need shader + * variants to set the flag when Vulkan's + * VkPipelineMultisampleStateCreateInfo->sampleShadingEnable or GL's + * glMinSampleshading() are enabled. * * This depends on some possibly hw implementation details, which may * not be true for all hw. In particular that the FS is only executed