nir: add pass to lower load_interpolated_input
authorRob Clark <robdclark@gmail.com>
Wed, 3 Apr 2019 23:29:36 +0000 (19:29 -0400)
committerJason Ekstrand <jason@jlekstrand.net>
Tue, 2 Jul 2019 16:15:25 +0000 (16:15 +0000)
Signed-off-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Matt Turner <mattst88@gmail.com>
src/compiler/Makefile.sources
src/compiler/nir/meson.build
src/compiler/nir/nir.h
src/compiler/nir/nir_intrinsics.py
src/compiler/nir/nir_lower_interpolation.c [new file with mode: 0644]
src/compiler/nir/nir_lower_io.c

index c94ec24b2e6027d94a8cc1cddc43616408f11ef0..213c04840e72ea37f45856c2d582382329d723b2 100644 (file)
@@ -257,6 +257,7 @@ NIR_FILES = \
        nir/nir_lower_idiv.c \
        nir/nir_lower_indirect_derefs.c \
        nir/nir_lower_int64.c \
+       nir/nir_lower_interpolation.c \
        nir/nir_lower_int_to_float.c \
        nir/nir_lower_io.c \
        nir/nir_lower_io_arrays_to_elements.c \
index 268acfeb2e63ffceb7e38ca3366e120d57d0388b..75dbd2809d0f63e898140eb130a924c0dc0e5b99 100644 (file)
@@ -135,6 +135,7 @@ files_libnir = files(
   'nir_lower_idiv.c',
   'nir_lower_indirect_derefs.c',
   'nir_lower_int64.c',
+  'nir_lower_interpolation.c',
   'nir_lower_int_to_float.c',
   'nir_lower_io.c',
   'nir_lower_io_arrays_to_elements.c',
index 56b5c996af1efdef4453422d4d07237b53a474ba..700174ca02a4ed6e8ceda481ca1491ef90f37d9e 100644 (file)
@@ -3570,6 +3570,17 @@ bool nir_lower_doubles(nir_shader *shader, const nir_shader *softfp64,
                        nir_lower_doubles_options options);
 bool nir_lower_pack(nir_shader *shader);
 
+typedef enum {
+   nir_lower_interpolation_at_sample = (1 << 1),
+   nir_lower_interpolation_at_offset = (1 << 2),
+   nir_lower_interpolation_centroid  = (1 << 3),
+   nir_lower_interpolation_pixel     = (1 << 4),
+   nir_lower_interpolation_sample    = (1 << 5),
+} nir_lower_interpolation_options;
+
+bool nir_lower_interpolation(nir_shader *shader,
+                             nir_lower_interpolation_options options);
+
 bool nir_normalize_cubemap_coords(nir_shader *shader);
 
 void nir_live_ssa_defs_impl(nir_function_impl *impl);
index c9d0a236c5a72c169d4932a25ab62c16f5507c3d..24bb09908050e110ff367ce605320160e372f77c 100644 (file)
@@ -630,6 +630,19 @@ intrinsic("load_sample_pos_from_id", src_comp=[1], dest_comp=2,
 # Loads what I believe is the primitive size, for scaling ij to pixel size:
 intrinsic("load_size_ir3", dest_comp=1, flags=[CAN_ELIMINATE, CAN_REORDER])
 
+# Fragment shader input interpolation delta intrinsic.
+#
+# For hw where fragment shader input interpolation is handled in shader, the
+# load_fs_input_interp deltas intrinsics can be used to load the input deltas
+# used for interpolation as follows:
+#
+#    vec3 iid = load_fs_input_interp_deltas(varying_slot)
+#    vec2 bary = load_barycentric_*(...)
+#    float result = iid.x + iid.y * bary.y + iid.z * bary.x
+
+intrinsic("load_fs_input_interp_deltas", src_comp=[1], dest_comp=3,
+          indices=[BASE, COMPONENT], flags=[CAN_ELIMINATE, CAN_REORDER])
+
 # Load operations pull data from some piece of GPU memory.  All load
 # operations operate in terms of offsets into some piece of theoretical
 # memory.  Loads from externally visible memory (UBO and SSBO) simply take a
diff --git a/src/compiler/nir/nir_lower_interpolation.c b/src/compiler/nir/nir_lower_interpolation.c
new file mode 100644 (file)
index 0000000..b4edfb9
--- /dev/null
@@ -0,0 +1,166 @@
+/*
+ * Copyright © 2019 Google, Inc.
+ *
+ * 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.
+ */
+
+/*
+ * This lower pass lowers load_interpolated_input for various interpolation
+ * modes (as configured via nir_lower_interpolation_options bitmask) into
+ * load_attribute_deltas plus alu instructions:
+ *
+ *    vec3 ad = load_attribute_deltas(varying_slot)
+ *    float result = ad.x + ad.y * j + ad.z * i
+ *
+ */
+
+#include "nir.h"
+#include "nir_builder.h"
+
+static bool
+nir_lower_interpolation_block(nir_block *block, nir_builder *b,
+                              nir_lower_interpolation_options options)
+{
+   bool progress = false;
+
+   nir_foreach_instr_safe(instr, block) {
+      if (instr->type != nir_instr_type_intrinsic)
+         continue;
+
+      nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
+
+      if (intr->intrinsic != nir_intrinsic_load_interpolated_input)
+         continue;
+
+      assert(intr->dest.is_ssa);
+      assert(intr->src[0].is_ssa);
+      assert(intr->src[1].is_ssa);
+
+      nir_intrinsic_instr *bary_intrinsic =
+         nir_instr_as_intrinsic(intr->src[0].ssa->parent_instr);
+
+      /* Leave VARYING_SLOT_POS alone */
+      if (nir_intrinsic_base(intr) == VARYING_SLOT_POS)
+         continue;
+
+      const enum glsl_interp_mode interp_mode =
+         nir_intrinsic_interp_mode(bary_intrinsic);
+
+      /* We need actual interpolation modes by the time we get here */
+      assert(interp_mode != INTERP_MODE_NONE);
+
+      /* Only lower for inputs that need interpolation */
+      if (interp_mode != INTERP_MODE_SMOOTH &&
+          interp_mode != INTERP_MODE_NOPERSPECTIVE)
+         continue;
+
+      nir_intrinsic_op op = bary_intrinsic->intrinsic;
+
+      switch (op) {
+      case nir_intrinsic_load_barycentric_at_sample:
+         if (options & nir_lower_interpolation_at_sample)
+            break;
+         continue;
+      case nir_intrinsic_load_barycentric_at_offset:
+         if (options & nir_lower_interpolation_at_offset)
+            break;
+         continue;
+      case nir_intrinsic_load_barycentric_centroid:
+         if (options & nir_lower_interpolation_centroid)
+            break;
+         continue;
+      case nir_intrinsic_load_barycentric_pixel:
+         if (options & nir_lower_interpolation_pixel)
+            break;
+         continue;
+      case nir_intrinsic_load_barycentric_sample:
+         if (options & nir_lower_interpolation_sample)
+            break;
+         continue;
+      default:
+         continue;
+      }
+
+      b->cursor = nir_before_instr(instr);
+
+      nir_ssa_def *comps[NIR_MAX_VEC_COMPONENTS];
+      for (int i = 0; i < intr->num_components; i++) {
+         nir_intrinsic_instr *load_iid =
+            nir_intrinsic_instr_create(b->shader,
+                                       nir_intrinsic_load_fs_input_interp_deltas);
+         load_iid->src[0] = nir_src_for_ssa(intr->src[1].ssa);
+         nir_ssa_dest_init(&load_iid->instr, &load_iid->dest,
+                           3, 32, NULL);
+         nir_intrinsic_set_base(load_iid, nir_intrinsic_base(intr));
+         nir_intrinsic_set_component(load_iid,
+                                     nir_intrinsic_component(intr) + i);
+         nir_builder_instr_insert(b, &load_iid->instr);
+
+         nir_ssa_def *iid = &load_iid->dest.ssa;
+         nir_ssa_def *bary = intr->src[0].ssa;
+         nir_ssa_def *val;
+
+         val = nir_ffma(b, nir_channel(b, bary, 1),
+                           nir_channel(b, iid, 1),
+                           nir_channel(b, iid, 0));
+         val = nir_ffma(b, nir_channel(b, bary, 0),
+                           nir_channel(b, iid, 2),
+                           val);
+
+         comps[i] = val;
+      }
+      nir_ssa_def *vec = nir_vec(b, comps, intr->num_components);
+      nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(vec));
+
+      progress = true;
+   }
+
+   return progress;
+}
+
+static bool
+nir_lower_interpolation_impl(nir_function_impl *impl,
+                             nir_lower_interpolation_options options)
+{
+   bool progress = false;
+   nir_builder builder;
+   nir_builder_init(&builder, impl);
+
+   nir_foreach_block(block, impl) {
+      progress |= nir_lower_interpolation_block(block, &builder, options);
+   }
+
+   nir_metadata_preserve(impl, nir_metadata_block_index |
+                               nir_metadata_dominance);
+   return progress;
+}
+
+bool
+nir_lower_interpolation(nir_shader *shader, nir_lower_interpolation_options options)
+{
+   bool progress = false;
+
+   nir_foreach_function(function, shader) {
+      if (function->impl)
+         progress |= nir_lower_interpolation_impl(function->impl, options);
+   }
+
+   return progress;
+}
index c54303b271ef42019a05a551216ef38c1f9c89f9..a9b3b95ef496dbffab9a9da8e5fa6dc805e67ba6 100644 (file)
@@ -1203,6 +1203,7 @@ nir_get_io_offset_src(nir_intrinsic_instr *instr)
    case nir_intrinsic_load_uniform:
    case nir_intrinsic_load_global:
    case nir_intrinsic_load_scratch:
+   case nir_intrinsic_load_fs_input_interp_deltas:
       return &instr->src[0];
    case nir_intrinsic_load_ubo:
    case nir_intrinsic_load_ssbo: