From d3ada77a6af63f33082ddee76738116a340485be Mon Sep 17 00:00:00 2001 From: Emma Anholt Date: Mon, 28 Jul 2025 16:47:19 -0700 Subject: [PATCH] nir: Move ST's force-persample-shading NIR pass to shared code. This is about to grow a little. Part-of: --- src/compiler/nir/meson.build | 1 + src/compiler/nir/nir.h | 1 + src/compiler/nir/nir_lower_sample_shading.c | 49 +++++++++++++++++++++ src/mesa/state_tracker/st_program.c | 16 +------ 4 files changed, 52 insertions(+), 15 deletions(-) create mode 100644 src/compiler/nir/nir_lower_sample_shading.c diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build index 4989e94675b..7b00033d9ca 100644 --- a/src/compiler/nir/meson.build +++ b/src/compiler/nir/meson.build @@ -215,6 +215,7 @@ else 'nir_lower_returns.c', 'nir_lower_robust_access.c', 'nir_lower_samplers.c', + 'nir_lower_sample_shading.c', 'nir_lower_scratch.c', 'nir_lower_scratch_to_var.c', 'nir_lower_shader_calls.c', diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index a6b3aace2bf..7c8a996fa5d 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -5417,6 +5417,7 @@ bool nir_lower_uniforms_to_ubo(nir_shader *shader, bool dword_packed, bool load_ bool nir_lower_is_helper_invocation(nir_shader *shader); bool nir_lower_single_sampled(nir_shader *shader); +bool nir_lower_sample_shading(nir_shader *shader); bool nir_lower_atomics(nir_shader *shader, nir_instr_filter_cb filter); diff --git a/src/compiler/nir/nir_lower_sample_shading.c b/src/compiler/nir/nir_lower_sample_shading.c new file mode 100644 index 00000000000..7983a664b15 --- /dev/null +++ b/src/compiler/nir/nir_lower_sample_shading.c @@ -0,0 +1,49 @@ +/* + * Copyright © 2025 Igalia SL + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "compiler/nir/nir.h" +#include "compiler/nir/nir_builder.h" + +static bool +force_persample_shading(struct nir_builder *b, nir_intrinsic_instr *intr, + void *data) +{ + if (intr->intrinsic == nir_intrinsic_load_barycentric_pixel || + intr->intrinsic == nir_intrinsic_load_barycentric_centroid) { + intr->intrinsic = nir_intrinsic_load_barycentric_sample; + return true; + } + + return false; +} + +/** Lowering to set up interpolation for sample shading. */ +bool +nir_lower_sample_shading(nir_shader *nir) +{ + assert(nir->info.stage == MESA_SHADER_FRAGMENT); + assert(nir->info.fs.uses_sample_shading); + + return nir_shader_intrinsics_pass(nir, force_persample_shading, + nir_metadata_all, NULL); +} diff --git a/src/mesa/state_tracker/st_program.c b/src/mesa/state_tracker/st_program.c index 1117e0abc94..fae306c7d9d 100644 --- a/src/mesa/state_tracker/st_program.c +++ b/src/mesa/state_tracker/st_program.c @@ -693,19 +693,6 @@ lower_ucp(struct st_context *st, } } -static bool -force_persample_shading(struct nir_builder *b, nir_intrinsic_instr *intr, - void *data) -{ - if (intr->intrinsic == nir_intrinsic_load_barycentric_pixel || - intr->intrinsic == nir_intrinsic_load_barycentric_centroid) { - intr->intrinsic = nir_intrinsic_load_barycentric_sample; - return true; - } - - return false; -} - static int xfb_compare_dst_offset(const void *a, const void *b) { @@ -1075,8 +1062,6 @@ st_create_fp_variant(struct st_context *st, if (key->persample_shading) { nir_shader *shader = state.ir.nir; - nir_shader_intrinsics_pass(shader, force_persample_shading, - nir_metadata_all, NULL); /* In addition to requiring per-sample interpolation, sample shading * changes the behaviour of gl_SampleMaskIn, so we need per-sample shading @@ -1084,6 +1069,7 @@ st_create_fp_variant(struct st_context *st, * uses_sample_shading won't be set by glsl_to_nir. We need to do so here. */ shader->info.fs.uses_sample_shading = true; + nir_lower_sample_shading(shader); finalize = true; }