From 1c88ed61943a009cd87f09dca0ada6128cdca77b Mon Sep 17 00:00:00 2001 From: Timothy Arceri Date: Wed, 11 Sep 2024 14:56:53 +1000 Subject: [PATCH] glsl: add lower_derivatives_without_layout() helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Acked-by: Marek Olšák Part-of: --- src/compiler/glsl/gl_nir_linker.c | 39 +++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/compiler/glsl/gl_nir_linker.c b/src/compiler/glsl/gl_nir_linker.c index 2f60828ccb2..d4e83107c64 100644 --- a/src/compiler/glsl/gl_nir_linker.c +++ b/src/compiler/glsl/gl_nir_linker.c @@ -339,6 +339,43 @@ validate_geometry_shader_emissions(const struct gl_constants *consts, } } +/* For derivatives in compute shaders, GLSL_NV_compute_shader_derivatives + * states: + * + * If neither layout qualifier is specified, derivatives in compute + * shaders return zero, which is consistent with the handling of built-in + * texture functions like texture() in GLSL 4.50 compute shaders. + */ +static void +lower_derivatives_without_layout(nir_builder *b) +{ + if (b->shader->info.stage != MESA_SHADER_COMPUTE || + b->shader->info.derivative_group != DERIVATIVE_GROUP_NONE) + return; + + nir_foreach_function_impl(impl, b->shader) { + nir_foreach_block(block, impl) { + nir_foreach_instr_safe(instr, block) { + if (instr->type == nir_instr_type_intrinsic) { + nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); + nir_intrinsic_op op = intrin->intrinsic; + if (op != nir_intrinsic_ddx && op != nir_intrinsic_ddx_fine && op != nir_intrinsic_ddx_coarse && + op != nir_intrinsic_ddy && op != nir_intrinsic_ddy_fine && op != nir_intrinsic_ddy_coarse) + continue; + + nir_def *def = &intrin->def; + b->cursor = nir_before_instr(instr); + nir_def *zero = nir_imm_zero(b, def->num_components, + def->bit_size); + nir_def_replace(def, zero); + } else { + continue; + } + } + } + } +} + /** * Generate a string describing the mode of a variable */ @@ -2844,6 +2881,8 @@ link_intrastage_shaders(void *mem_ctx, */ nir_validate_shader(linked->Program->nir, "post shader stage combine"); + lower_derivatives_without_layout(&b); + /* Set the linked source BLAKE3. */ if (num_shaders == 1) { memcpy(linked->linked_source_blake3, shader_list[0]->compiled_source_blake3,