nir: add pass to lower discard() to demote()
authorDaniel Schürmann <daniel@schuermann.dev>
Wed, 4 Mar 2020 15:55:13 +0000 (16:55 +0100)
committerMarge Bot <eric+marge@anholt.net>
Mon, 9 Mar 2020 12:29:32 +0000 (12:29 +0000)
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 <marek.olsak@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4047>

src/compiler/Makefile.sources
src/compiler/nir/meson.build
src/compiler/nir/nir.h
src/compiler/nir/nir_lower_discard_to_demote.c [new file with mode: 0644]

index 48c5d6e7cf2851845e9e0c44cb12d4ce76787fd4..b44b2b5f6ffaa7ddc48c034a58309357d0022d19 100644 (file)
@@ -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 \
index 6ffb948f0498262920b549362e3274864999c164..80f2fc3d824d3ccb70b8d58f1a01680c3739f723 100644 (file)
@@ -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',
index 804e2d825194a7e2c48419d95f3981b41cb5b5ba..a151f7b8e74e875c23e31591c565520d433460af 100644 (file)
@@ -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 (file)
index 0000000..cbb7da9
--- /dev/null
@@ -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;
+}