Mesa (master): nir: add lowering pass for helperInvocationEXT()

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Mon Apr 19 17:41:36 UTC 2021


Module: Mesa
Branch: master
Commit: f17b41ab4f017b562fdf7c388200e68c22105331
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=f17b41ab4f017b562fdf7c388200e68c22105331

Author: Danylo Piliaiev <dpiliaiev at igalia.com>
Date:   Fri Mar  5 19:58:25 2021 +0200

nir: add lowering pass for helperInvocationEXT()

Some hardware doesn't have a way to check if invocation was demoted,
in such case we have to track it ourselves.
OpIsHelperInvocationEXT is specified as:

 "An invocation is currently a helper invocation if it was originally
  invoked as a helper invocation or if it has been demoted to a helper
  invocation by OpDemoteToHelperInvocationEXT."

Therefore we:
- Set gl_IsHelperInvocationEXT = gl_HelperInvocation
- Add "gl_IsHelperInvocationEXT = true" right before each demote
- Add "gl_IsHelperInvocationEXT = gl_IsHelperInvocationEXT || condition"
  right before each demote_if

Signed-off-by: Danylo Piliaiev <dpiliaiev at igalia.com>
Reviewed-by: Samuel Iglesias Gonsálvez <siglesias at igalia.com>
Reviewed-by: Eric Anholt <eric at anholt.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/9460>

---

 src/compiler/Makefile.sources                     |   1 +
 src/compiler/nir/meson.build                      |   1 +
 src/compiler/nir/nir.h                            |   2 +
 src/compiler/nir/nir_lower_is_helper_invocation.c | 126 ++++++++++++++++++++++
 4 files changed, 130 insertions(+)

diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources
index 66777c7213e..a3a04f88c23 100644
--- a/src/compiler/Makefile.sources
+++ b/src/compiler/Makefile.sources
@@ -278,6 +278,7 @@ NIR_FILES = \
 	nir/nir_lower_io_to_temporaries.c \
 	nir/nir_lower_io_to_scalar.c \
 	nir/nir_lower_io_to_vector.c \
+	nir/nir_lower_is_helper_invocation.c \
 	nir/nir_lower_multiview.c \
 	nir/nir_lower_mediump.c \
 	nir/nir_lower_memcpy.c \
diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build
index d17bbe504a5..1fcd4fc6a22 100644
--- a/src/compiler/nir/meson.build
+++ b/src/compiler/nir/meson.build
@@ -167,6 +167,7 @@ files_libnir = files(
   'nir_lower_io_to_temporaries.c',
   'nir_lower_io_to_scalar.c',
   'nir_lower_io_to_vector.c',
+  'nir_lower_is_helper_invocation.c',
   'nir_lower_multiview.c',
   'nir_lower_mediump.c',
   'nir_lower_memcpy.c',
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
index a0a646610db..cbe5ab94f3b 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -4633,6 +4633,8 @@ bool nir_lower_fragcoord_wtrans(nir_shader *shader);
 void nir_lower_viewport_transform(nir_shader *shader);
 bool nir_lower_uniforms_to_ubo(nir_shader *shader, bool dword_packed, bool load_vec4);
 
+bool nir_lower_is_helper_invocation(nir_shader *shader);
+
 typedef struct nir_lower_subgroups_options {
    uint8_t subgroup_size;
    uint8_t ballot_bit_size;
diff --git a/src/compiler/nir/nir_lower_is_helper_invocation.c b/src/compiler/nir/nir_lower_is_helper_invocation.c
new file mode 100644
index 00000000000..38263d5c2ec
--- /dev/null
+++ b/src/compiler/nir/nir_lower_is_helper_invocation.c
@@ -0,0 +1,126 @@
+/*
+ * Copyright © 2021 Igalia S.L.
+ *
+ * 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"
+#include "nir_builder.h"
+
+/* Some hardware doesn't have a way to check if invocation was demoted,
+ * in such case we have to track it ourselves.
+ * OpIsHelperInvocationEXT is specified as:
+ *
+ *  "An invocation is currently a helper invocation if it was originally
+ *   invoked as a helper invocation or if it has been demoted to a helper
+ *   invocation by OpDemoteToHelperInvocationEXT."
+ *
+ * Therefore we:
+ * - Set gl_IsHelperInvocationEXT = gl_HelperInvocation
+ * - Add "gl_IsHelperInvocationEXT = true" right before each demote
+ * - Add "gl_IsHelperInvocationEXT = gl_IsHelperInvocationEXT || condition"
+ *   right before each demote_if
+ */
+
+static bool
+nir_lower_load_and_store_is_helper(nir_builder *b, nir_instr *instr, void *data)
+{
+   if (instr->type != nir_instr_type_intrinsic)
+      return false;
+
+   nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
+   nir_deref_instr *is_helper_deref = (nir_deref_instr*) data;
+
+   switch (intrin->intrinsic) {
+   case nir_intrinsic_demote: {
+      b->cursor = nir_before_instr(instr);
+      nir_store_deref(b, is_helper_deref, nir_imm_bool(b, true), 1);
+      return true;
+   }
+   case nir_intrinsic_demote_if: {
+      b->cursor = nir_before_instr(instr);
+      nir_ssa_def *current_is_helper = nir_load_deref(b, is_helper_deref);
+      nir_ssa_def *updated_is_helper = nir_ior(b, current_is_helper, intrin->src[0].ssa);
+      nir_store_deref(b, is_helper_deref, updated_is_helper, 1);
+      return true;
+   }
+   case nir_intrinsic_is_helper_invocation: {
+      b->cursor = nir_before_instr(instr);
+      nir_ssa_def *is_helper = nir_load_deref(b, is_helper_deref);
+      nir_ssa_def_rewrite_uses(&intrin->dest.ssa, is_helper);
+      nir_instr_remove_v(instr);
+      return true;
+   }
+   default:
+      return false;
+   }
+}
+
+static bool
+has_is_helper_invocation(nir_shader *shader)
+{
+   nir_foreach_function(function, shader) {
+      if (!function->impl)
+         continue;
+
+      nir_foreach_block_safe(block, function->impl) {
+         nir_foreach_instr_safe(instr, block) {
+            if (instr->type != nir_instr_type_intrinsic)
+               continue;
+
+            nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
+            if (intrin->intrinsic == nir_intrinsic_is_helper_invocation)
+               return true;
+         }
+      }
+   }
+
+   return false;
+}
+
+bool
+nir_lower_is_helper_invocation(nir_shader *shader)
+{
+   if (shader->info.stage != MESA_SHADER_FRAGMENT)
+      return false;
+
+   if (!has_is_helper_invocation(shader))
+      return false;
+
+   nir_function_impl *entrypoint = nir_shader_get_entrypoint(shader);
+
+   nir_builder b;
+   nir_builder_init(&b, entrypoint);
+   b.cursor = nir_before_cf_list(&entrypoint->body);
+
+   nir_variable *is_helper = nir_local_variable_create(entrypoint,
+                                          glsl_bool_type(),
+                                          "gl_IsHelperInvocationEXT");
+
+   nir_ssa_def *started_as_helper = nir_load_helper_invocation(&b, 32);
+
+   nir_deref_instr *is_helper_deref = nir_build_deref_var(&b, is_helper);
+   nir_store_deref(&b, is_helper_deref, started_as_helper, 1);
+
+   return nir_shader_instructions_pass(shader,
+                                       nir_lower_load_and_store_is_helper,
+                                       nir_metadata_all,
+                                       is_helper_deref);
+}



More information about the mesa-commit mailing list