Debian Codegraph @

src/asahi/layout/layout.c

raw
1 /*
2 * Copyright 2022 Alyssa Rosenzweig
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "layout.h"
7 #include "util/format/u_format.h"
8 #include "util/u_math.h"
9
10 static void
11 ail_initialize_linear(struct ail_layout *layout)
12 {
13 /* Select the optimal stride if none is forced */
14 if (layout->linear_stride_B == 0) {
15 uint32_t minimum_stride_B =
16 util_format_get_stride(layout->format, layout->width_px);
17
18 layout->linear_stride_B = ALIGN_POT(minimum_stride_B, AIL_CACHELINE);
19 }
20
21 assert((layout->linear_stride_B % 16) == 0 && "Strides must be aligned");
22
23 /* Layer stride must be cache line aligned to pack linear 2D arrays */
24 layout->layer_stride_B = align64(
25 (uint64_t)layout->linear_stride_B * layout->height_px, AIL_CACHELINE);
26
27 layout->size_B =
28 layout->level_offsets_B[0] + (layout->layer_stride_B * layout->depth_px);
29 }
30
31 /*
32 * Get the maximum tile size possible for a given block size. This satisfy
33 * width * height * blocksize = 16384 = page size, so each tile is one page.
34 */
35 struct ail_tile
36 ail_get_max_tile_size(unsigned blocksize_B)
37 {
38 /* clang-format off */
39 switch (blocksize_B) {
40 case 1: return (struct ail_tile) { 128, 128 };
41 case 2: return (struct ail_tile) { 128, 64 };
42 case 4: return (struct ail_tile) { 64, 64 };
43 case 8: return (struct ail_tile) { 64, 32 };
44 case 16: return (struct ail_tile) { 32, 32 };
45 case 32: return (struct ail_tile) { 32, 16 };
46 case 64: return (struct ail_tile) { 16, 16 };
47 default: UNREACHABLE("Invalid blocksize");
48 }
49 /* clang-format on */
50 }
51
52 /*
53 * Calculate the number of bytes in a block. This must take both block
54 * dimensions and multisampling into account.
55 */
56 static uint32_t
57 ail_get_block_size_B(struct ail_layout *layout)
58 {
59 ASSERTED const struct util_format_description *desc =
60 util_format_description(layout->format);
61
62 assert(((layout->sample_count_sa == 1) ||
63 (desc->block.width == 1 && desc->block.height == 1)) &&
64 "multisampling and block-compression are mutually-exclusive");
65
66 return util_format_get_blocksize(layout->format) * layout->sample_count_sa;
67 }
68
69 static void
70 ail_initialize_gpu_tiled(struct ail_layout *layout)
71 {
72 unsigned offset_B = 0;
73 unsigned blocksize_B = ail_get_block_size_B(layout);
74 unsigned w_el = util_format_get_nblocksx(layout->format, layout->width_px);
75 unsigned h_el = util_format_get_nblocksy(layout->format, layout->height_px);
76 unsigned bw_px = util_format_get_blockwidth(layout->format);
77 unsigned bh_px = util_format_get_blockheight(layout->format);
78 bool compressed = util_format_is_compressed(layout->format);
79
80 /* Calculate the tile size used for the large miptree, and the dimensions of
81 * level 0 given that tile size.
82 */
83 struct ail_tile tilesize_el = ail_get_max_tile_size(blocksize_B);
84 unsigned stx_tiles = DIV_ROUND_UP(w_el, tilesize_el.width_el);
85 unsigned sty_tiles = DIV_ROUND_UP(h_el, tilesize_el.height_el);
86 unsigned sarea_tiles = stx_tiles * sty_tiles;
87
88 /* Calculate which level the small power-of-two miptree begins at. The
89 * power-of-two miptree is used when either the width or the height is
90 * smaller than a single large tile.
91 */
92 unsigned pot_level = 0;
93 unsigned pot_w_px = bw_px * w_el;
94 unsigned pot_h_px = bh_px * h_el;
95 do {
96 unsigned pot_w_el = util_format_get_nblocksx(layout->format, pot_w_px);
97 unsigned pot_h_el = util_format_get_nblocksy(layout->format, pot_h_px);
98 if (pot_w_el < tilesize_el.width_el || pot_h_el < tilesize_el.height_el)
99 break;
100 pot_w_px = u_minify(pot_w_px, 1);
101 pot_h_px = u_minify(pot_h_px, 1);
102 pot_level++;
103 } while (1);
104
105 /* First allocate the large miptree. All tiles in the large miptree are of
106 * size tilesize_el and have their dimensions given by stx/sty/sarea.
107 */
108 for (unsigned l = 0; l < MIN2(pot_level, layout->levels); ++l) {
109 unsigned tiles = (sarea_tiles >> (2 * l));
110
111 bool pad_left = (stx_tiles & BITFIELD_MASK(l));
112 bool pad_bottom = (sty_tiles & BITFIELD_MASK(l));
113 bool pad_corner = pad_left && pad_bottom;
114
115 if (pad_left)
116 tiles += (sty_tiles >> l);
117
118 if (pad_bottom)
119 tiles += (stx_tiles >> l);
120
121 if (pad_corner)
122 tiles += 1;
123
124 unsigned size_el = tiles * tilesize_el.width_el * tilesize_el.height_el;
125 layout->level_offsets_B[l] = offset_B;
126 offset_B = ALIGN_POT(offset_B + (blocksize_B * size_el), AIL_CACHELINE);
127
128 layout->stride_el[l] = util_format_get_nblocksx(
129 layout->format, u_minify(layout->width_px, l));
130
131 /* Compressed textures pad the stride in this case */
132 if (compressed && pad_left)
133 layout->stride_el[l]++;
134
135 layout->tilesize_el[l] = tilesize_el;
136 }
137
138 /* Then begin the POT miptree. Note that we round up to a power-of-two
139 * outside the loop. That ensures correct handling of cases like 33x33
140 * images, where the round-down error of right-shifting could cause incorrect
141 * tile size calculations.
142 */
143 unsigned potw_el, poth_el;
144 if (compressed) {
145 /* Compressed formats round then minify instead of minifying then rounding
146 */
147 potw_el = u_minify(util_next_power_of_two(w_el), pot_level);
148 poth_el = u_minify(util_next_power_of_two(h_el), pot_level);
149 } else {
150 potw_el = util_next_power_of_two(u_minify(w_el, pot_level));
151 poth_el = util_next_power_of_two(u_minify(h_el, pot_level));
152 }
153
154 /* Finally we allocate the POT miptree, starting at level pot_level. Each
155 * level uses the largest power-of-two tile that fits the level.
156 */
157 for (unsigned l = pot_level; l < layout->levels; ++l) {
158 unsigned size_el = potw_el * poth_el;
159 layout->level_offsets_B[l] = offset_B;
160 offset_B = ALIGN_POT(offset_B + (blocksize_B * size_el), AIL_CACHELINE);
161
162 /* The tilesize is based on the true mipmap level size, not the POT
163 * rounded size, except for compressed textures */
164 unsigned tilesize_el;
165 if (compressed)
166 tilesize_el = util_next_power_of_two(MIN2(potw_el, poth_el));
167 else
168 tilesize_el = util_next_power_of_two(u_minify(MIN2(w_el, h_el), l));
169 layout->tilesize_el[l] = (struct ail_tile){tilesize_el, tilesize_el};
170 layout->stride_el[l] = util_format_get_nblocksx(
171 layout->format, u_minify(layout->width_px, l));
172
173 potw_el = u_minify(potw_el, 1);
174 poth_el = u_minify(poth_el, 1);
175 }
176
177 /* Add the end offset so we can easily recover the size of a level */
178 assert(layout->levels < ARRAY_SIZE(layout->level_offsets_B));
179 layout->level_offsets_B[layout->levels] = offset_B;
180
181 /* Determine the start of the miptail. From that level on, we can no longer
182 * precisely bind at page granularity.
183 */
184 layout->mip_tail_first_lod = MIN2(pot_level, layout->levels);
185
186 /* Determine the stride of the miptail. Sparse arrayed images inherently
187 * require page-aligned layers to be able to bind individual layers.
188 */
189 unsigned tail_offset_B = layout->level_offsets_B[layout->mip_tail_first_lod];
190 layout->mip_tail_stride = align(offset_B - tail_offset_B, AIL_PAGESIZE);
191
192 /* Align layer size if we have mipmaps and one miptree is larger than one
193 * page */
194 layout->page_aligned_layers = layout->levels != 1 && offset_B > AIL_PAGESIZE;
195
196 /* Single-layer images are not padded unless they are Z/S */
197 bool zs = util_format_is_depth_or_stencil(layout->format);
198 if (layout->depth_px == 1 && !zs)
199 layout->page_aligned_layers = false;
200
201 /* For writable images, we require page-aligned layers. This appears to be
202 * required for PBE stores, including block stores for colour rendering.
203 * Likewise, we specify the ZLS layer stride in pages, so we need
204 * page-aligned layers for renderable depth/stencil targets.
205 */
206 layout->page_aligned_layers |= layout->writeable_image;
207 layout->page_aligned_layers |= layout->renderable && layout->depth_px > 1;
208
209 if (layout->page_aligned_layers)
210 layout->layer_stride_B = ALIGN_POT(offset_B, AIL_PAGESIZE);
211 else
212 layout->layer_stride_B = offset_B;
213
214 layout->size_B = (uint64_t)layout->layer_stride_B * layout->depth_px;
215 }
216
217 static void
218 ail_initialize_twiddled(struct ail_layout *layout)
219 {
220 unsigned offset_B = 0;
221 unsigned blocksize_B = ail_get_block_size_B(layout);
222 unsigned w_el = util_format_get_nblocksx(layout->format, layout->width_px);
223 unsigned h_el = util_format_get_nblocksy(layout->format, layout->height_px);
224 bool compressed = util_format_is_compressed(layout->format);
225
226 for (unsigned l = 0; l < layout->levels; ++l) {
227 unsigned alloc_w_el = u_minify(util_next_power_of_two(w_el), l);
228 unsigned alloc_h_el = u_minify(util_next_power_of_two(h_el), l);
229 unsigned logical_w_el = util_next_power_of_two(u_minify(w_el, l));
230 unsigned logical_h_el = util_next_power_of_two(u_minify(h_el, l));
231
232 /* AGX: Even the exceptions have exceptions */
233 if (compressed) {
234 logical_w_el = alloc_w_el;
235 logical_h_el = alloc_h_el;
236 }
237
238 unsigned size_el = alloc_w_el * alloc_h_el;
239 layout->level_offsets_B[l] = offset_B;
240 offset_B = ALIGN_POT(offset_B + (blocksize_B * size_el), AIL_CACHELINE);
241
242 unsigned minor_el = MIN2(logical_w_el, logical_h_el);
243 layout->stride_el[l] = logical_w_el;
244 layout->tilesize_el[l] = (struct ail_tile){minor_el, minor_el};
245 }
246
247 /* Add the end offset so we can easily recover the size of a level */
248 assert(layout->levels < ARRAY_SIZE(layout->level_offsets_B));
249 layout->level_offsets_B[layout->levels] = offset_B;
250
251 /* Determine the start of the miptail. From that level on, we can no longer
252 * precisely bind at page granularity.
253 */
254 /* XXX TODO */
255 layout->mip_tail_first_lod = 0;
256
257 /* Determine the stride of the miptail. Sparse arrayed images inherently
258 * require page-aligned layers to be able to bind individual layers.
259 */
260 unsigned tail_offset_B = layout->level_offsets_B[layout->mip_tail_first_lod];
261 layout->mip_tail_stride = align(offset_B - tail_offset_B, AIL_PAGESIZE);
262
263 layout->page_aligned_layers = true;
264 layout->layer_stride_B = ALIGN_POT(offset_B, AIL_PAGESIZE);
265 layout->size_B = (uint64_t)layout->layer_stride_B * layout->depth_px;
266 }
267
268 static void
269 ail_initialize_compression(struct ail_layout *layout)
270 {
271 assert(!util_format_is_compressed(layout->format) &&
272 "Compressed pixel formats not supported");
273 assert(util_format_get_blockwidth(layout->format) == 1);
274 assert(util_format_get_blockheight(layout->format) == 1);
275
276 unsigned width_sa =
277 ail_effective_width_sa(layout->width_px, layout->sample_count_sa);
278 unsigned height_sa =
279 ail_effective_height_sa(layout->height_px, layout->sample_count_sa);
280
281 assert(width_sa >= 16 && "Small textures are never compressed");
282 assert(height_sa >= 16 && "Small textures are never compressed");
283
284 layout->metadata_offset_B = layout->size_B;
285
286 width_sa = ALIGN_POT(width_sa, 16);
287 height_sa = ALIGN_POT(height_sa, 16);
288
289 unsigned compbuf_B = 0;
290
291 for (unsigned l = 0;
292 l < layout->levels && ail_is_level_allocated_compressed(layout, l);
293 ++l) {
294
295 layout->level_offsets_compressed_B[l] = compbuf_B;
296
297 /* The metadata buffer contains 8 bytes per 16x16 compression tile.
298 * Addressing is fully twiddled, so both width and height are padded to
299 * powers-of-two.
300 */
301 unsigned w_tl = DIV_ROUND_UP(util_next_power_of_two(width_sa), 16);
302 unsigned h_tl = DIV_ROUND_UP(util_next_power_of_two(height_sa), 16);
303 unsigned B_per_tl_2 = 8;
304
305 compbuf_B += ALIGN_POT(w_tl * h_tl * B_per_tl_2, AIL_CACHELINE);
306
307 width_sa = DIV_ROUND_UP(width_sa, 2);
308 height_sa = DIV_ROUND_UP(height_sa, 2);
309 }
310
311 layout->compression_layer_stride_B = compbuf_B;
312 layout->size_B +=
313 (uint64_t)(layout->compression_layer_stride_B * layout->depth_px);
314 }
315
316 static void
317 ail_initialize_sparse_table(struct ail_layout *layout)
318 {
319 layout->sparse_folios_per_layer =
320 DIV_ROUND_UP(layout->layer_stride_B, AIL_IMAGE_SIZE_PER_FOLIO_B);
321
322 unsigned folios = layout->sparse_folios_per_layer * layout->depth_px;
323 layout->sparse_table_size_B = folios * AIL_FOLIO_SIZE_B;
324 }
325
326 void
327 ail_make_miptree(struct ail_layout *layout)
328 {
329 assert(layout->width_px >= 1 && "Invalid dimensions");
330 assert(layout->height_px >= 1 && "Invalid dimensions");
331 assert(layout->depth_px >= 1 && "Invalid dimensions");
332
333 if (layout->tiling == AIL_TILING_LINEAR) {
334 assert(layout->levels == 1 && "Invalid linear layout");
335 assert(layout->sample_count_sa == 1 &&
336 "Multisampled linear layouts not supported");
337 assert(util_format_get_blockwidth(layout->format) == 1 &&
338 "Strided linear block formats unsupported");
339 assert(util_format_get_blockheight(layout->format) == 1 &&
340 "Strided linear block formats unsupported");
341 } else {
342 assert(layout->linear_stride_B == 0 && "Invalid nonlinear layout");
343 assert(layout->levels >= 1 && "Invalid dimensions");
344 assert(layout->sample_count_sa >= 1 && "Invalid sample count");
345 assert(layout->level_offsets_B[0] == 0 && "Invalid offset");
346 }
347
348 assert(!(layout->writeable_image && layout->compressed) &&
349 "Writeable images must not be compressed");
350
351 /* Hardware strides are based on the maximum number of levels, so always
352 * allocate them all.
353 */
354 if (layout->levels > 1) {
355 unsigned major_axis_px = MAX2(layout->width_px, layout->height_px);
356
357 if (layout->mipmapped_z)
358 major_axis_px = MAX2(major_axis_px, layout->depth_px);
359
360 layout->levels = util_logbase2(major_axis_px) + 1;
361 }
362
363 assert(util_format_get_blockdepth(layout->format) == 1 &&
364 "Deep formats unsupported");
365
366 switch (layout->tiling) {
367 case AIL_TILING_LINEAR:
368 ail_initialize_linear(layout);
369 break;
370 case AIL_TILING_GPU:
371 ail_initialize_gpu_tiled(layout);
372 break;
373 case AIL_TILING_TWIDDLED:
374 ail_initialize_twiddled(layout);
375 break;
376 default:
377 UNREACHABLE("Unsupported tiling");
378 }
379
380 if (layout->compressed) {
381 ail_initialize_compression(layout);
382 }
383
384 ail_initialize_sparse_table(layout);
385
386 layout->size_B = ALIGN_POT(layout->size_B, AIL_CACHELINE);
387 assert(layout->size_B > 0 && "Invalid dimensions");
388 }