pan/bi: Add transform feedback lowering pass
authorAlyssa Rosenzweig <alyssa@collabora.com>
Thu, 2 Jun 2022 14:50:54 +0000 (10:50 -0400)
committerMarge Bot <emma+marge@anholt.net>
Sat, 4 Jun 2022 14:35:56 +0000 (14:35 +0000)
Add a simple NIR-based implementation of transform feedback, appropriate for
OpenGL ES 3.1 class hardware (compute but no geometry or tessellation shaders).
Stores to varyings that will be captured are replaced by stores to transform
feedback buffers and some addressing math. This allows implementing the semantic
of transform feedback in a compute-like stage.

Signed-off-by: Alyssa Rosenzweig <alyssa@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/15720>

src/panfrost/bifrost/bi_lower_xfb.c [new file with mode: 0644]
src/panfrost/bifrost/bifrost_compile.c
src/panfrost/bifrost/bifrost_nir.h
src/panfrost/bifrost/meson.build

diff --git a/src/panfrost/bifrost/bi_lower_xfb.c b/src/panfrost/bifrost/bi_lower_xfb.c
new file mode 100644 (file)
index 0000000..35daeb3
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2022 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 "bifrost_nir.h"
+
+static void
+lower_xfb_output(nir_builder *b, nir_intrinsic_instr *intr,
+                 unsigned start_component, unsigned num_components,
+                 unsigned buffer, unsigned offset_words)
+{
+        assert(buffer < MAX_XFB_BUFFERS);
+        assert(nir_intrinsic_component(intr) == 0); // TODO
+
+        /* Transform feedback info in units of words, convert to bytes. */
+        uint16_t stride = b->shader->info.xfb_stride[buffer] * 4;
+        assert(stride != 0);
+
+        uint16_t offset = offset_words * 4;
+
+        nir_ssa_def *index = nir_iadd(b,
+                nir_imul(b, nir_load_instance_id(b),
+                            nir_load_num_vertices(b)),
+                nir_load_vertex_id_zero_base(b));
+
+        nir_ssa_def *buf = nir_load_xfb_address(b, 64, .base = buffer);
+        nir_ssa_def *addr =
+                nir_iadd(b, buf, nir_u2u64(b,
+                                    nir_iadd_imm(b,
+                                                 nir_imul_imm(b, index, stride),
+                                                 offset)));
+
+        assert(intr->src[0].is_ssa && "must lower XFB before lowering SSA");
+        nir_ssa_def *src = intr->src[0].ssa;
+        nir_ssa_def *value = nir_channels(b, src, BITFIELD_MASK(num_components) << start_component);
+        nir_store_global(b, addr, 4, value, BITFIELD_MASK(num_components));
+}
+
+static bool
+lower_xfb(nir_builder *b, nir_instr *instr, UNUSED void *data)
+{
+        if (instr->type != nir_instr_type_intrinsic)
+                return false;
+
+        nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
+        if (intr->intrinsic != nir_intrinsic_store_output)
+                return false;
+
+        bool progress = false;
+
+        b->cursor = nir_before_instr(&intr->instr);
+
+        for (unsigned i = 0; i < 2; ++i) {
+                nir_io_xfb xfb = i ? nir_intrinsic_io_xfb2(intr) : nir_intrinsic_io_xfb(intr);
+                for (unsigned j = 0; j < 2; ++j) {
+                        if (!xfb.out[j].num_components) continue;
+
+                        lower_xfb_output(b, intr, i*2 + j,
+                                                     xfb.out[j].num_components,
+                                                     xfb.out[j].buffer,
+                                                     xfb.out[j].offset);
+                        progress = true;
+                }
+        }
+
+        nir_instr_remove(instr);
+        return progress;
+}
+
+bool
+bifrost_nir_lower_xfb(nir_shader *nir)
+{
+        return nir_shader_instructions_pass(nir, lower_xfb,
+                                            nir_metadata_block_index |
+                                            nir_metadata_dominance, NULL);
+}
+
index cb5e368..74dc313 100644 (file)
@@ -4845,6 +4845,13 @@ bi_finalize_nir(nir_shader *nir, unsigned gpu_id, bool is_blend)
                                 NULL);
         }
 
+        if (nir->xfb_info != NULL && nir->info.has_transform_feedback_varyings) {
+                NIR_PASS_V(nir, nir_io_add_const_offset_to_base,
+                           nir_var_shader_in | nir_var_shader_out);
+                NIR_PASS_V(nir, nir_io_add_intrinsic_xfb_info);
+                NIR_PASS_V(nir, bifrost_nir_lower_xfb);
+        }
+
         bi_optimize_nir(nir, gpu_id, is_blend);
 }
 
@@ -5156,6 +5163,7 @@ bi_compile_variant(nir_shader *nir,
         }
 
         if (idvs == BI_IDVS_POSITION &&
+            !nir->info.internal &&
             nir->info.outputs_written & BITFIELD_BIT(VARYING_SLOT_PSIZ)) {
                 /* Find the psiz write */
                 bi_instr *write = NULL;
index d624463..b94ba6e 100644 (file)
@@ -23,5 +23,7 @@
 
 #include <stdbool.h>
 #include "nir.h"
+#include "nir_builder.h"
 
 bool bifrost_nir_lower_algebraic_late(nir_shader *shader);
+bool bifrost_nir_lower_xfb(nir_shader *shader);
index e66fea3..f485961 100644 (file)
@@ -29,6 +29,7 @@ libpanfrost_bifrost_files = files(
   'bi_liveness.c',
   'bi_lower_divergent_indirects.c',
   'bi_lower_swizzle.c',
+  'bi_lower_xfb.c',
   'bi_print.c',
   'bi_opt_constant_fold.c',
   'bi_opt_copy_prop.c',