Mesa (main): pan/bi: Add transform feedback lowering pass

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Sat Jun 4 15:11:11 UTC 2022


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

Author: Alyssa Rosenzweig <alyssa at collabora.com>
Date:   Thu Jun  2 10:50:54 2022 -0400

pan/bi: Add transform feedback lowering pass

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 at collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/15720>

---

 src/panfrost/bifrost/bi_lower_xfb.c    | 96 ++++++++++++++++++++++++++++++++++
 src/panfrost/bifrost/bifrost_compile.c |  8 +++
 src/panfrost/bifrost/bifrost_nir.h     |  2 +
 src/panfrost/bifrost/meson.build       |  1 +
 4 files changed, 107 insertions(+)

diff --git a/src/panfrost/bifrost/bi_lower_xfb.c b/src/panfrost/bifrost/bi_lower_xfb.c
new file mode 100644
index 00000000000..35daeb3a55c
--- /dev/null
+++ b/src/panfrost/bifrost/bi_lower_xfb.c
@@ -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);
+}
+
diff --git a/src/panfrost/bifrost/bifrost_compile.c b/src/panfrost/bifrost/bifrost_compile.c
index cb5e368cd11..74dc3132f3f 100644
--- a/src/panfrost/bifrost/bifrost_compile.c
+++ b/src/panfrost/bifrost/bifrost_compile.c
@@ -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;
diff --git a/src/panfrost/bifrost/bifrost_nir.h b/src/panfrost/bifrost/bifrost_nir.h
index d6244637dce..b94ba6eaf38 100644
--- a/src/panfrost/bifrost/bifrost_nir.h
+++ b/src/panfrost/bifrost/bifrost_nir.h
@@ -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);
diff --git a/src/panfrost/bifrost/meson.build b/src/panfrost/bifrost/meson.build
index e66fea341b3..f485961366d 100644
--- a/src/panfrost/bifrost/meson.build
+++ b/src/panfrost/bifrost/meson.build
@@ -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',



More information about the mesa-commit mailing list