From ce87da71e93d9eea7e9a2667e3273cab9c97667f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Daniel=20Sch=C3=BCrmann?= Date: Wed, 4 Mar 2020 16:55:13 +0100 Subject: [PATCH] nir: add pass to lower discard() to demote() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This pass is intended to work around game bugs, only! It also lowers nir_intrinsic_load_helper_invocation to nir_intrinsic_is_helper_invocation for consistency. Reviewed-by: Marek Olšák Part-of: --- src/compiler/Makefile.sources | 1 + src/compiler/nir/meson.build | 1 + src/compiler/nir/nir.h | 2 + .../nir/nir_lower_discard_to_demote.c | 69 +++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 src/compiler/nir/nir_lower_discard_to_demote.c diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources index 48c5d6e7cf2..b44b2b5f6ff 100644 --- a/src/compiler/Makefile.sources +++ b/src/compiler/Makefile.sources @@ -245,6 +245,7 @@ NIR_FILES = \ nir/nir_lower_clip_cull_distance_arrays.c \ nir/nir_lower_clip_halfz.c \ nir/nir_lower_variable_initializers.c \ + nir/nir_lower_discard_to_demote.c \ nir/nir_lower_double_ops.c \ nir/nir_lower_drawpixels.c \ nir/nir_lower_fb_read.c \ diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build index 6ffb948f049..80f2fc3d824 100644 --- a/src/compiler/nir/meson.build +++ b/src/compiler/nir/meson.build @@ -125,6 +125,7 @@ files_libnir = files( 'nir_lower_clip_cull_distance_arrays.c', 'nir_lower_clip_halfz.c', 'nir_lower_variable_initializers.c', + 'nir_lower_discard_to_demote.c', 'nir_lower_double_ops.c', 'nir_lower_drawpixels.c', 'nir_lower_fb_read.c', diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index 804e2d82519..a151f7b8e74 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -4197,6 +4197,8 @@ typedef enum { bool nir_lower_interpolation(nir_shader *shader, nir_lower_interpolation_options options); +bool nir_lower_discard_to_demote(nir_shader *shader); + bool nir_normalize_cubemap_coords(nir_shader *shader); void nir_live_ssa_defs_impl(nir_function_impl *impl); diff --git a/src/compiler/nir/nir_lower_discard_to_demote.c b/src/compiler/nir/nir_lower_discard_to_demote.c new file mode 100644 index 00000000000..cbb7da9a892 --- /dev/null +++ b/src/compiler/nir/nir_lower_discard_to_demote.c @@ -0,0 +1,69 @@ +/* + * Copyright © 2020 Valve Corporation + * + * 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 "nir.h" + +/** + * This pass is intended as workaround for game bugs to force correct + * derivatives after kill. This lowering is not valid in the general case + * as it might change the result of subgroup operations and loop behavior. + * + * discard() will be lowered as demote() and gl_HelperInvocation + * will be lowered as helperInvocationEXT(). + */ +bool +nir_lower_discard_to_demote(nir_shader *shader) +{ + if (shader->info.stage != MESA_SHADER_FRAGMENT) + return false; + + bool progress = false; + + nir_foreach_function(function, shader) { + nir_foreach_block(block, function->impl) { + nir_foreach_instr(instr, block) { + if (instr->type != nir_instr_type_intrinsic) + continue; + + nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); + switch (intrin->intrinsic) { + case nir_intrinsic_discard: + intrin->intrinsic = nir_intrinsic_demote; + break; + case nir_intrinsic_discard_if: + intrin->intrinsic = nir_intrinsic_demote_if; + break; + case nir_intrinsic_load_helper_invocation: + intrin->intrinsic = nir_intrinsic_is_helper_invocation; + break; + default: + continue; + } + progress = true; + } + } + } + + return progress; +} -- 2.30.2