Mesa (main): nir: Add a nir_sysvals_to_varyings() helper

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Thu Oct 7 20:13:56 UTC 2021


Module: Mesa
Branch: main
Commit: 56251f924de1d3a70f800613e2a18704a51b54c7
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=56251f924de1d3a70f800613e2a18704a51b54c7

Author: Boris Brezillon <boris.brezillon at collabora.com>
Date:   Mon Sep 27 14:20:20 2021 +0200

nir: Add a nir_sysvals_to_varyings() helper

Allow backends to turn some sysvals into input varyings so the frontend
(in our case spirv_to_nir()) doesn't have to bother selecting which
one is expected.

Signed-off-by: Boris Brezillon <boris.brezillon at collabora.com>
Reviewed-by: Jason Ekstrand <jason at jlekstrand.net>
Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin at intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13017>

---

 src/compiler/nir/meson.build                     |  1 +
 src/compiler/nir/nir.h                           | 10 ++++
 src/compiler/nir/nir_lower_sysvals_to_varyings.c | 72 ++++++++++++++++++++++++
 3 files changed, 83 insertions(+)

diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build
index 339b37200e2..414bb3e9054 100644
--- a/src/compiler/nir/meson.build
+++ b/src/compiler/nir/meson.build
@@ -207,6 +207,7 @@ files_libnir = files(
   'nir_lower_bit_size.c',
   'nir_lower_ubo_vec4.c',
   'nir_lower_uniforms_to_ubo.c',
+  'nir_lower_sysvals_to_varyings.c',
   'nir_metadata.c',
   'nir_move_vec_src_uses_to_dest.c',
   'nir_normalize_cubemap_coords.c',
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
index 5d076c75487..2bc17548d4a 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -5048,6 +5048,16 @@ typedef struct nir_lower_compute_system_values_options {
 bool nir_lower_compute_system_values(nir_shader *shader,
                                      const nir_lower_compute_system_values_options *options);
 
+struct nir_lower_sysvals_to_varyings_options {
+   bool frag_coord:1;
+   bool front_face:1;
+   bool point_coord:1;
+};
+
+bool
+nir_lower_sysvals_to_varyings(nir_shader *shader,
+                              const struct nir_lower_sysvals_to_varyings_options *options);
+
 enum PACKED nir_lower_tex_packing {
    /** No packing */
    nir_lower_tex_packing_none = 0,
diff --git a/src/compiler/nir/nir_lower_sysvals_to_varyings.c b/src/compiler/nir/nir_lower_sysvals_to_varyings.c
new file mode 100644
index 00000000000..6422e9cb92f
--- /dev/null
+++ b/src/compiler/nir/nir_lower_sysvals_to_varyings.c
@@ -0,0 +1,72 @@
+/*
+ * Copyright © 2021 Collabora Ltd.
+ *
+ * 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"
+
+/*
+ * spirv_to_nir() creates system values for some builtin inputs, but
+ * backends might want to have those inputs exposed as varyings. This
+ * lowering pass allows backends to convert system values to input
+ * varyings and should be called just after spirv_to_nir() when needed.
+ */
+
+bool
+nir_lower_sysvals_to_varyings(nir_shader *shader,
+                              const struct nir_lower_sysvals_to_varyings_options *options)
+{
+   bool progress = false;
+
+   nir_foreach_variable_with_modes(var, shader, nir_var_system_value) {
+      switch (var->data.location) {
+#define SYSVAL_TO_VARYING(opt, sysval, varying) \
+        case SYSTEM_VALUE_ ## sysval: \
+           if (options->opt) { \
+              var->data.mode = nir_var_shader_in; \
+              var->data.location = VARYING_SLOT_ ## varying; \
+              progress = true; \
+           } \
+           break
+
+      SYSVAL_TO_VARYING(frag_coord, FRAG_COORD, POS);
+      SYSVAL_TO_VARYING(point_coord, POINT_COORD, PNTC);
+      SYSVAL_TO_VARYING(front_face, FRONT_FACE, FACE);
+
+#undef SYSVAL_TO_VARYING
+
+      default:
+         break;
+      }
+   }
+
+   if (progress)
+      nir_fixup_deref_modes(shader);
+
+   /* Nothing this does actually changes anything tracked by metadata.
+    * If we ever made this pass more complicated, we might need to care
+    * more about metadata.
+    */
+   nir_shader_preserve_all_metadata(shader);
+
+   return progress;
+}



More information about the mesa-commit mailing list