From 09a192a7078472e1ef5a1adf45fa72f7b0c31856 Mon Sep 17 00:00:00 2001
From: Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Date: Fri, 29 Jan 2021 15:44:01 -0500
Subject: [PATCH] WIP - pan_samples.c

---
 src/gallium/drivers/panfrost/pan_cmdstream.c |  59 ++++-----
 src/panfrost/lib/pan_device.h                |   6 +
 src/panfrost/lib/pan_props.c                 |   3 +
 src/panfrost/lib/pan_samples.c               | 129 +++++++++++++++++++
 4 files changed, 163 insertions(+), 34 deletions(-)
 create mode 100644 src/panfrost/lib/pan_samples.c

diff --git a/src/gallium/drivers/panfrost/pan_cmdstream.c b/src/gallium/drivers/panfrost/pan_cmdstream.c
index d3ef6ffcb0e..c909266cf1d 100644
--- a/src/gallium/drivers/panfrost/pan_cmdstream.c
+++ b/src/gallium/drivers/panfrost/pan_cmdstream.c
@@ -2109,44 +2109,35 @@ panfrost_emit_vertex_tiler_jobs(struct panfrost_batch *batch,
 }
 
 /* TODO: stop hardcoding this */
+
+/* in 1/8s */
+#define SAMPLE(x, y) \
+        ((x) * (256 / 8)), \
+        ((y) * (256 / 8))
+
 mali_ptr
 panfrost_emit_sample_locations(struct panfrost_batch *batch)
 {
-        uint16_t locations[] = {
-            128, 128,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            0, 256,
-            128, 128,
+        uint16_t single_sample[] = {
+                SAMPLE(4, 4)
+        };
+
+        uint16_t rotated_grid_4[] = {
+                SAMPLE(3, 1),
+                SAMPLE(7, 3),
+                SAMPLE(1, 5),
+                SAMPLE(5, 7),
         };
 
+        unsigned count = util_framebuffer_get_num_samples(&batch->key);
+        assert(count == 1 || count == 4);
+        uint16_t *samples = (count == 1) ? single_sample : rotated_grid_4;
+
+        uint16_t locations[33 * 2] = { 0 };
+
+        memcpy(locations, samples, count * sizeof(locations[0]) * 2);
+        locations[(32 * 2) + 0] = 128;
+        locations[(32 * 2) + 1] = 128;
+
         return panfrost_pool_upload_aligned(&batch->pool, locations, sizeof(locations), 64);
 }
diff --git a/src/panfrost/lib/pan_device.h b/src/panfrost/lib/pan_device.h
index 3d16c5b3472..dccbcd3bfa7 100644
--- a/src/panfrost/lib/pan_device.h
+++ b/src/panfrost/lib/pan_device.h
@@ -151,6 +151,12 @@ struct panfrost_device {
          * costly per-context allocation. */
 
         struct panfrost_bo *tiler_heap;
+
+        /* Sample positions are preloaded into a write-once constant buffer,
+         * such that they can be referenced fore free later. Needed
+         * unconditionally on Bifrost, and useful for sharing with Midgard */
+
+        struct panfrost_bo *sample_positions;
 };
 
 void
diff --git a/src/panfrost/lib/pan_props.c b/src/panfrost/lib/pan_props.c
index 334c36e6872..80ecd606e2d 100644
--- a/src/panfrost/lib/pan_props.c
+++ b/src/panfrost/lib/pan_props.c
@@ -247,6 +247,9 @@ panfrost_open_device(void *memctx, int fd, struct panfrost_device *dev)
 
         dev->tiler_heap = panfrost_bo_create(dev, 4096 * 4096,
                         PAN_BO_INVISIBLE | PAN_BO_GROWABLE);
+
+        /* Done once on init */
+        panfrost_upload_sample_positions(dev);
 }
 
 void
diff --git a/src/panfrost/lib/pan_samples.c b/src/panfrost/lib/pan_samples.c
new file mode 100644
index 00000000000..934a7bf4856
--- /dev/null
+++ b/src/panfrost/lib/pan_samples.c
@@ -0,0 +1,129 @@
+/*
+ * Copyright (C) 2019 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 "pan_device.h"
+
+struct mali_sample_position {
+        uint16_t x, y;
+} __attribute__((packed));
+
+struct mali_sample_positions {
+        struct mali_sample_position positions[32];
+        struct mali_sample_position center;
+        struct mali_sample_position padding[64 - (32 + 1)];
+} __attribute__((packed));
+
+/* Arguments specified in 1/16s of a grid, centered at the origin */
+
+#define SAMPLE16(x, y) { \
+        (((x) + 8) * (256 / 16)), \
+        (((y) + 8) * (256 / 16)) \
+}
+
+#define SAMPLE8(x, y) SAMPLE16((x) * 2, (y) * 2)
+#define SAMPLE4(x, y) SAMPLE16((x) * 4, (y) * 4)
+
+const struct mali_sample_positions sample_position_lut[] = {
+        [MALI_SAMPLE_PATTERN_SINGLE_SAMPLED] = {
+                .positions = { 
+                        SAMPLE(0, 0)
+                },
+                .center = SAMPLE(0, 0)
+        },
+
+        [MALI_SAMPLE_PATTERN_ORDERED_4X_GRID] = {
+                .positions = { 
+                        SAMPLE4(-1, -1),
+                        SAMPLE4( 1, -1),
+                        SAMPLE4(-1,  1),
+                        SAMPLE4( 1,  1),
+                },
+                .center = SAMPLE(0, 0)
+        },
+
+        [MALI_SAMPLE_PATTERN_ROTATED_4X_GRID] = {
+                .positions = { 
+                        SAMPLE8(-1, -3),
+                        SAMPLE8( 3, -1),
+                        SAMPLE8(-3,  1),
+                        SAMPLE8( 1,  3),
+                },
+                .center = SAMPLE(0, 0)
+        },
+
+        [MALI_SAMPLE_PATTERN_D3D_8X_GRID] = {
+                .positions = { 
+                        SAMPLE16( 1, -3),
+                        SAMPLE16(-1,  3),
+                        SAMPLE16( 5,  1),
+                        SAMPLE16(-3,  5),
+                        SAMPLE16(-5,  5),
+                        SAMPLE16(-7, -1),
+                        SAMPLE16( 3,  7),
+                        SAMPLE16( 7,  -7),
+                },
+                .center = SAMPLE(0, 0)
+        },
+
+        [MALI_SAMPLE_PATTERN_D3D_16X_GRID] = {
+                .positions = { 
+                        SAMPLE16( 1,  1),
+                        SAMPLE16(-1, -3),
+                        SAMPLE16(-3,  2),
+                        SAMPLE16( 4, -1),
+                        SAMPLE16(-5, -2),
+                        SAMPLE16( 2,  5),
+                        SAMPLE16( 5,  3),
+                        SAMPLE16( 3, -5),
+                        SAMPLE16(-2,  6),
+                        SAMPLE16( 0,  7),
+                        SAMPLE16(-4, -6),
+                        SAMPLE16(-6,  4),
+                        SAMPLE16(-8,  0),
+                        SAMPLE16( 7, -4),
+                        SAMPLE16( 6,  7),
+                        SAMPLE16(-7, -8),
+
+                },
+                .center = SAMPLE(0, 0)
+        }
+};
+
+mali_ptr
+panfrost_sample_positions(struct panfrost_device *dev,
+                enum mali_sample_pattern pattern)
+{
+        assert(pattern < ARRAY_SIZE(sample_position_lut));
+        unsigned offset = (pattern * sizeof(mali_sample_positions));
+        return dev->sample_positions.gpu + offset;
+}
+
+void
+panfrost_upload_sample_positions(struct panfrost_device *dev)
+{
+        STATIC_ASSERT(sizeof(sample_position_lut) < 4096);
+        dev->sample_positions = panfrost_bo_create(dev, 4096);
+
+        memcpy(dev->sample_positions.cpu, sample_position_lut,
+                        sizeof(sample_position_lut));
+}
-- 
2.27.0

