Debian Codegraph @

src/asahi/lib/agx_tilebuffer.c

raw
1 /*
2 * Copyright 2022 Alyssa Rosenzweig
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "agx_tilebuffer.h"
7 #include <assert.h>
8 #include "util/format/u_format.h"
9 #include "layout.h"
10
11 /* Maximum number of bytes per tile on G13G. This may change in future versions
12 * of the architecture.
13 */
14 #define MAX_BYTES_PER_TILE (32768 - 1)
15
16 /* Maximum bytes per sample in the tilebuffer. Greater allocations require
17 * spilling render targets to memory.
18 */
19 #define MAX_BYTES_PER_SAMPLE (64)
20
21 /* Minimum tile size in pixels, architectural. */
22 #define MIN_TILE_SIZE_PX (16 * 16)
23
24 /* Select the largest tile size that fits */
25 static uint16_t
26 agx_select_tile_size(unsigned px_size_B)
27 {
28 assert(px_size_B <= (MAX_BYTES_PER_TILE / MIN_TILE_SIZE_PX));
29
30 return ((px_size_B * 32 * 32) <= MAX_BYTES_PER_TILE) ? (32 * 32)
31 : ((px_size_B * 32 * 16) <= MAX_BYTES_PER_TILE) ? (32 * 16)
32 : MIN_TILE_SIZE_PX;
33 }
34
35 static inline unsigned
36 format_align_B(enum pipe_format format)
37 {
38 /* For some reason util_format_get_blocksize(NONE) = 1 */
39 enum pipe_format phys = ail_pixel_format[format].renderable;
40 return (format != PIPE_FORMAT_NONE) ? util_format_get_blocksize(phys) : 0;
41 }
42
43 struct agx_tilebuffer_layout
44 agx_build_tilebuffer_layout(const enum pipe_format *formats, uint8_t nr_cbufs,
45 uint8_t nr_samples, bool layered)
46 {
47 struct agx_tilebuffer_layout tib = {
48 .nr_samples = nr_samples,
49 .layered = layered,
50 };
51
52 uint32_t offset_B = 0;
53 uint8_t order[] = {0, 1, 2, 3, 4, 5, 6, 7};
54
55 /* Sort render targets in descending order of alignment, eliminating padding
56 * and giving the optimal order of render targets. We use insertion sort
57 * because it is simple, stable, fast for small n, and free for n=1.
58 */
59 for (int i = 1; i < nr_cbufs; ++i) {
60 for (int j = i; j > 0 && format_align_B(formats[order[j - 1]]) <
61 format_align_B(formats[order[j]]);
62 --j) {
63 SWAP(order[j], order[j - 1]);
64 }
65 }
66
67 for (unsigned i = 0; i < nr_cbufs; ++i) {
68 unsigned rt = order[i];
69 enum pipe_format format = formats[rt];
70 tib.logical_format[rt] = format;
71
72 assert(util_is_aligned(offset_B, MAX2(format_align_B(formats[rt]), 1)) &&
73 "loop invariant ensured by the sort");
74
75 unsigned size_B = format_align_B(format);
76 enum pipe_format phys = ail_pixel_format[format].renderable;
77 if (util_format_get_nr_components(phys) == 1) {
78 size_B *= util_format_get_nr_components(format);
79 }
80
81 /* If allocating this render target would exceed any tilebuffer limits, we
82 * need to spill it to memory. Otherwise, allocate it to the tilebuffer.
83 */
84 unsigned new_offset_B = offset_B + size_B;
85 bool fits = (new_offset_B <= MAX_BYTES_PER_SAMPLE) &&
86 (ALIGN_POT(new_offset_B, 8) * MIN_TILE_SIZE_PX *
87 nr_samples) <= MAX_BYTES_PER_TILE;
88 if (fits) {
89 tib._offset_B[rt] = offset_B;
90 offset_B = new_offset_B;
91 } else {
92 tib.spilled[rt] = true;
93 }
94 }
95
96 assert(offset_B <= MAX_BYTES_PER_SAMPLE && "loop invariant");
97
98 /* Multisampling needs a nonempty allocation.
99 * XXX: Check this against hw
100 */
101 if (nr_samples > 1)
102 offset_B = MAX2(offset_B, 1);
103
104 tib.sample_size_B = align(offset_B, 8);
105 tib.tile_size = agx_select_tile_size(tib.sample_size_B * nr_samples);
106
107 agx_tilebuffer_pack_usc(&tib);
108 return tib;
109 }
110
111 /*
112 * With attachmentless rendering in Vulkan, the sample count may not known until
113 * draw-time. It's convenient to construct an agx_tilebuffer_layout anyway when
114 * beginning rendering, updating the sample count later. This helper allows the
115 * driver to set the sample count in a partial agx_tilebuffer_layout.
116 *
117 * When doing so, we need to rebuild entirely since e.g. tile size might change.
118 */
119 void
120 agx_tilebuffer_set_samples(struct agx_tilebuffer_layout *tib,
121 unsigned nr_samples)
122 {
123 assert(tib->nr_samples == 0 && "must not be initialized");
124
125 *tib = agx_build_tilebuffer_layout(tib->logical_format,
126 ARRAY_SIZE(tib->logical_format),
127 nr_samples, tib->layered);
128 }
129
130 enum pipe_format
131 agx_tilebuffer_physical_format(struct agx_tilebuffer_layout *tib, unsigned rt)
132 {
133 return ail_pixel_format[tib->logical_format[rt]].renderable;
134 }
135
136 bool
137 agx_tilebuffer_supports_mask(struct agx_tilebuffer_layout *tib, unsigned rt)
138 {
139 /* We don't bother support masking with spilled render targets. This might be
140 * optimized in the future but spilling is so rare anyway it's not worth it.
141 */
142 if (tib->spilled[rt])
143 return false;
144
145 enum pipe_format fmt = agx_tilebuffer_physical_format(tib, rt);
146 return ail_isa_format_supports_mask((enum ail_isa_format)fmt);
147 }
148
149 uint32_t
150 agx_tilebuffer_total_size(struct agx_tilebuffer_layout *tib)
151 {
152 return tib->sample_size_B * tib->nr_samples * tib->tile_size;
153 }
154
155 void
156 agx_tilebuffer_pack_usc(struct agx_tilebuffer_layout *tib)
157 {
158 agx_pack(&tib->usc, USC_SHARED, cfg) {
159 if (tib->nr_samples > 0) {
160 cfg.uses_shared_memory = true;
161 cfg.sample_stride_in_8_bytes = tib->sample_size_B / 8;
162 cfg.sample_count = tib->nr_samples;
163 cfg.bytes_per_threadgroup = agx_tilebuffer_total_size(tib);
164
165 if (tib->tile_size == 32 * 32)
166 cfg.layout = AGX_SHARED_LAYOUT_32X32;
167 else if (tib->tile_size == 32 * 16)
168 cfg.layout = AGX_SHARED_LAYOUT_32X16;
169 else if (tib->tile_size == 16 * 16)
170 cfg.layout = AGX_SHARED_LAYOUT_16X16;
171 else
172 UNREACHABLE("Invalid tile size");
173 } else {
174 cfg.layout = AGX_SHARED_LAYOUT_VERTEX_COMPUTE;
175 cfg.bytes_per_threadgroup = 65536;
176 }
177 }
178 }