Debian Codegraph @

src/asahi/layout/layout.h

raw
1 /*
2 * Copyright 2022 Alyssa Rosenzweig
3 * SPDX-License-Identifier: MIT
4 *
5 */
6
7 #pragma once
8
9 #include "drm-uapi/drm_fourcc.h"
10 #include "util/format/u_format.h"
11 #include "util/macros.h"
12 #include "util/u_math.h"
13
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17
18 #define AIL_CACHELINE 0x80
19 #define AIL_PAGESIZE 0x4000
20 #define AIL_MAX_MIP_LEVELS 16
21
22 enum ail_tiling {
23 /**
24 * Strided linear (raster order). Only allowed for 1D or 2D, without
25 * mipmapping, multisampling, block-compression, or arrays.
26 */
27 AIL_TILING_LINEAR,
28
29 /**
30 * GPU-tiled. Always allowed.
31 */
32 AIL_TILING_GPU,
33
34 /**
35 * Fully twiddled.
36 */
37 AIL_TILING_TWIDDLED,
38 };
39
40 /*
41 * Represents the dimensions of a single tile. Used to describe tiled layouts.
42 * Width and height are in units of elements, not pixels, to model compressed
43 * textures corrects.
44 *
45 * Invariant: width_el and height_el are powers of two.
46 */
47 struct ail_tile {
48 unsigned width_el, height_el;
49 };
50
51 /*
52 * An AGX image layout.
53 */
54 struct ail_layout {
55 /** Width, height, and depth in pixels at level 0 */
56 uint32_t width_px, height_px, depth_px;
57
58 /** Number of samples per pixel. 1 if multisampling is disabled. */
59 uint8_t sample_count_sa;
60
61 /** Number of miplevels. 1 if no mipmapping is used. */
62 uint8_t levels;
63
64 /** Should this image be mipmapped along the Z-axis in addition to the X- and
65 * Y-axes? This should be set for API-level 3D images, but not 2D arrays or
66 * cubes.
67 */
68 bool mipmapped_z;
69
70 /** Tiling mode used */
71 enum ail_tiling tiling;
72
73 /** Whether compression is used. Requires a non-linear layout. */
74 bool compressed;
75
76 /** Texture format */
77 enum pipe_format format;
78
79 /**
80 * If tiling is LINEAR, the number of bytes between adjacent rows of
81 * elements. Otherwise, this field is zero.
82 */
83 uint32_t linear_stride_B;
84
85 /**
86 * Stride between layers of an array texture, including a cube map. Layer i
87 * begins at offset (i * layer_stride_B) from the beginning of the texture.
88 *
89 * If depth_px = 1, the value of this field is UNDEFINED.
90 */
91 uint64_t layer_stride_B;
92
93 /**
94 * Whether the layer stride is aligned to the page size or not. The hardware
95 * needs this flag to compute the implicit layer stride.
96 */
97 bool page_aligned_layers;
98
99 /**
100 * Offsets of mip levels within a layer.
101 */
102 uint64_t level_offsets_B[AIL_MAX_MIP_LEVELS];
103
104 /**
105 * For the compressed buffer, offsets of mip levels within a layer.
106 */
107 uint64_t level_offsets_compressed_B[AIL_MAX_MIP_LEVELS];
108
109 /**
110 * If tiling is nonlinear, the tile size used for each mip level within a
111 * layer. Calculating tile sizes is the sole responsibility of
112 * ail_initialized_twiddled.
113 */
114 struct ail_tile tilesize_el[AIL_MAX_MIP_LEVELS];
115
116 /**
117 * If tiling is nonlinear, the stride in elements used for each mip level
118 * within a layer. Calculating level strides is the sole responsibility of
119 * ail_initialized_twiddled. This is necessary because compressed pixel
120 * formats may add extra stride padding.
121 */
122 uint32_t stride_el[AIL_MAX_MIP_LEVELS];
123
124 /**
125 * If the image is bound sparsely, the first LOD/level of the miptail.
126 * This corresponds to Vulkan imageMipTailFirstLod.
127 */
128 uint32_t mip_tail_first_lod;
129
130 /**
131 * If the image is bound sparsely, the layer stride of the miptail.
132 * Conceptually, this corresponds to Vulkan imageMipTailStride. However, it
133 * is not actually used for imageMipTailStride due to complications with
134 * standard sparse block sizes.
135 */
136 uint32_t mip_tail_stride;
137
138 /* Offset of the start of the compression metadata buffer */
139 uint32_t metadata_offset_B;
140
141 /* Stride between subsequent layers in the compression metadata buffer */
142 uint64_t compression_layer_stride_B;
143
144 /* Size of entire texture */
145 uint64_t size_B;
146
147 /* Size of the sparse page table required to address this image. This is
148 * calculated for all images (whether sparse is used or not), since the
149 * hardware has few requirements imposed on sparse.
150 *
151 * This size does not contribute to size_B. In a sparse image, size_B is the
152 * logical size of the image (if it were completely resident), whereas
153 * sparse_table_size_B is the physical size of the page table required to
154 * address that logical image.
155 */
156 uint64_t sparse_table_size_B;
157
158 /* Number of sparse folios required to cover a single layer of an image. This
159 * is proportional to sparse_table_size_B but split out for convenience.
160 */
161 uint32_t sparse_folios_per_layer;
162
163 /* Must the layout support writeable images? If false, the layout MUST NOT be
164 * used as a writeable image (either PBE or image atomics).
165 */
166 bool writeable_image;
167
168 /* Must the layout support rendering? If false, the layout MUST NOT be used
169 * for rendering, either PBE or ZLS.
170 */
171 bool renderable;
172 };
173
174 static inline uint32_t
175 ail_get_linear_stride_B(const struct ail_layout *layout, ASSERTED uint8_t level)
176 {
177 assert(layout->tiling == AIL_TILING_LINEAR && "Invalid usage");
178 assert(level == 0 && "Strided linear mipmapped textures are unsupported");
179
180 return layout->linear_stride_B;
181 }
182
183 /*
184 * For WSI purposes, we need to associate a stride with all layouts. In the
185 * hardware, only strided linear images have an associated stride, there is no
186 * natural stride associated with nonlinear images. However, various clients
187 * assert that the stride is valid for the image if it were linear (even if it
188 * is in fact not linear). In those cases, by convention we use the minimum
189 * valid such stride.
190 */
191 static inline uint32_t
192 ail_get_wsi_stride_B(const struct ail_layout *layout, unsigned level)
193 {
194 assert(level == 0 && "Mipmaps cannot be shared as WSI");
195
196 if (layout->tiling == AIL_TILING_LINEAR)
197 return ail_get_linear_stride_B(layout, level);
198 else
199 return util_format_get_stride(layout->format, layout->width_px);
200 }
201
202 static inline uint32_t
203 ail_get_layer_offset_B(const struct ail_layout *layout, unsigned z_px)
204 {
205 return z_px * layout->layer_stride_B;
206 }
207
208 static inline uint32_t
209 ail_get_level_offset_B(const struct ail_layout *layout, unsigned level)
210 {
211 return layout->level_offsets_B[level];
212 }
213
214 static inline uint32_t
215 ail_get_layer_level_B(const struct ail_layout *layout, unsigned z_px,
216 unsigned level)
217 {
218 return ail_get_layer_offset_B(layout, z_px) +
219 ail_get_level_offset_B(layout, level);
220 }
221
222 static inline uint32_t
223 ail_get_level_size_B(const struct ail_layout *layout, unsigned level)
224 {
225 if (layout->tiling == AIL_TILING_LINEAR) {
226 assert(level == 0);
227 return layout->layer_stride_B;
228 } else {
229 assert(level + 1 < ARRAY_SIZE(layout->level_offsets_B));
230 return layout->level_offsets_B[level + 1] -
231 layout->level_offsets_B[level];
232 }
233 }
234
235 static inline uint32_t
236 ail_get_linear_pixel_B(const struct ail_layout *layout, ASSERTED unsigned level,
237 uint32_t x_px, uint32_t y_px, uint32_t z_px)
238 {
239 assert(level == 0 && "Strided linear mipmapped textures are unsupported");
240 assert(util_format_get_blockwidth(layout->format) == 1 &&
241 "Strided linear block formats unsupported");
242 assert(util_format_get_blockheight(layout->format) == 1 &&
243 "Strided linear block formats unsupported");
244 assert(layout->sample_count_sa == 1 &&
245 "Strided linear multisampling unsupported");
246
247 return ail_get_layer_offset_B(layout, z_px) +
248 (y_px * ail_get_linear_stride_B(layout, level)) +
249 (x_px * util_format_get_blocksize(layout->format));
250 }
251
252 static inline uint32_t
253 ail_space_bits(unsigned x)
254 {
255 uint32_t accum = 0;
256
257 /* With fully twiddled images, we can have up to 16384x16384 tiles,
258 * justifying stopping the loop at 14.
259 */
260 assert(x < (1 << 14) && "offset must be inside the tile");
261
262 for (unsigned i = 0; i < 14; ++i) {
263 accum |= (x & (1 << i)) << i;
264 }
265
266 return accum;
267 }
268
269 #define MOD_POT(x, y) (x) & ((y) - 1)
270
271 static inline uint32_t
272 ail_get_blocksize_B(const struct ail_layout *layout)
273 {
274 return util_format_get_blocksize(layout->format);
275 }
276
277 static inline uint32_t
278 ail_get_twiddled_block_B(const struct ail_layout *layout, unsigned level,
279 uint32_t x_px, uint32_t y_px, uint32_t z_px)
280 {
281 assert(layout->tiling == AIL_TILING_GPU);
282 assert(level < layout->levels);
283
284 unsigned x_el = util_format_get_nblocksx(layout->format, x_px);
285 unsigned y_el = util_format_get_nblocksy(layout->format, y_px);
286
287 struct ail_tile tile_size = layout->tilesize_el[level];
288 assert((x_el % tile_size.width_el) == 0 && "must be aligned");
289 assert((y_el % tile_size.height_el) == 0 && "must be aligned");
290
291 unsigned stride_el = layout->stride_el[level];
292 unsigned tile_area_el = tile_size.width_el * tile_size.height_el;
293 unsigned tiles_per_row = DIV_ROUND_UP(stride_el, tile_size.width_el);
294
295 unsigned y_rowtile = y_el / tile_size.height_el;
296 unsigned y_tile = y_rowtile * tiles_per_row;
297
298 unsigned tile_idx = y_tile + (x_el / tile_size.width_el);
299 unsigned tile_offset_el = tile_idx * tile_area_el;
300 unsigned tile_offset_B = tile_offset_el * ail_get_blocksize_B(layout);
301
302 return ail_get_layer_level_B(layout, z_px, level) + tile_offset_B;
303 }
304
305 static inline unsigned
306 ail_effective_width_sa(unsigned width_px, unsigned sample_count_sa)
307 {
308 return width_px * (sample_count_sa == 4 ? 2 : 1);
309 }
310
311 static inline unsigned
312 ail_effective_height_sa(unsigned height_px, unsigned sample_count_sa)
313 {
314 return height_px * (sample_count_sa >= 2 ? 2 : 1);
315 }
316
317 static inline unsigned
318 ail_metadata_width_tl(struct ail_layout *layout, unsigned level)
319 {
320 unsigned px = u_minify(layout->width_px, level);
321 uint32_t sa = ail_effective_width_sa(px, layout->sample_count_sa);
322 return DIV_ROUND_UP(sa, 16);
323 }
324
325 static inline unsigned
326 ail_metadata_height_tl(struct ail_layout *layout, unsigned level)
327 {
328 unsigned px = u_minify(layout->height_px, level);
329 uint32_t sa = ail_effective_height_sa(px, layout->sample_count_sa);
330 return DIV_ROUND_UP(sa, 16);
331 }
332
333 /*
334 * Even when the base mip level is compressed, high levels of the miptree
335 * (smaller than 16 pixels on either axis) are not compressed as it would be
336 * pointless. This queries this case.
337 */
338 static inline bool
339 ail_is_level_allocated_compressed(const struct ail_layout *layout,
340 unsigned level)
341 {
342 unsigned width_sa = align(
343 ail_effective_width_sa(layout->width_px, layout->sample_count_sa), 16);
344
345 unsigned height_sa = align(
346 ail_effective_height_sa(layout->height_px, layout->sample_count_sa), 16);
347
348 return layout->compressed &&
349 u_minify(MAX2(width_sa, height_sa), level) >= 16;
350 }
351
352 static inline bool
353 ail_is_level_logically_compressed(const struct ail_layout *layout,
354 unsigned level)
355 {
356 unsigned width_sa =
357 ail_effective_width_sa(layout->width_px, layout->sample_count_sa);
358
359 unsigned height_sa =
360 ail_effective_height_sa(layout->height_px, layout->sample_count_sa);
361
362 return layout->compressed &&
363 u_minify(MIN2(width_sa, height_sa), level) >= 16;
364 }
365
366 static inline bool
367 ail_is_level_twiddled_uncompressed(const struct ail_layout *layout,
368 unsigned level)
369 {
370 if (layout->compressed) {
371 return !ail_is_level_logically_compressed(layout, level);
372 } else {
373 return layout->tiling != AIL_TILING_LINEAR;
374 }
375 }
376
377 struct ail_tile ail_get_max_tile_size(unsigned blocksize_B);
378
379 void ail_make_miptree(struct ail_layout *layout);
380
381 void ail_detile(void *_tiled, void *_linear,
382 const struct ail_layout *tiled_layout, unsigned level,
383 unsigned linear_pitch_B, unsigned sx_px, unsigned sy_px,
384 unsigned width_px, unsigned height_px);
385
386 void ail_tile(void *_tiled, void *_linear,
387 const struct ail_layout *tiled_layout, unsigned level,
388 unsigned linear_pitch_B, unsigned sx_px, unsigned sy_px,
389 unsigned width_px, unsigned height_px);
390
391 /* Define aliases for the subset formats that are accessible in the ISA. These
392 * subsets disregard component mapping and number of components. This
393 * constitutes ABI with the compiler.
394 */
395 enum ail_isa_format {
396 AIL_ISA_FORMAT_I8 = PIPE_FORMAT_R8_UINT,
397 AIL_ISA_FORMAT_I16 = PIPE_FORMAT_R16_UINT,
398 AIL_ISA_FORMAT_I32 = PIPE_FORMAT_R32_UINT,
399 AIL_ISA_FORMAT_F16 = PIPE_FORMAT_R16_FLOAT,
400 AIL_ISA_FORMAT_U8NORM = PIPE_FORMAT_R8_UNORM,
401 AIL_ISA_FORMAT_S8NORM = PIPE_FORMAT_R8_SNORM,
402 AIL_ISA_FORMAT_U16NORM = PIPE_FORMAT_R16_UNORM,
403 AIL_ISA_FORMAT_S16NORM = PIPE_FORMAT_R16_SNORM,
404 AIL_ISA_FORMAT_RGB10A2 = PIPE_FORMAT_R10G10B10A2_UNORM,
405 AIL_ISA_FORMAT_SRGBA8 = PIPE_FORMAT_R8G8B8A8_SRGB,
406 AIL_ISA_FORMAT_RG11B10F = PIPE_FORMAT_R11G11B10_FLOAT,
407 AIL_ISA_FORMAT_RGB9E5 = PIPE_FORMAT_R9G9B9E5_FLOAT
408 };
409
410 /*
411 * The architecture load/store instructions support masking, but packed formats
412 * are not compatible with masking. Check if a format is packed.
413 */
414 static inline bool
415 ail_isa_format_supports_mask(enum ail_isa_format format)
416 {
417 switch (format) {
418 case AIL_ISA_FORMAT_RGB10A2:
419 case AIL_ISA_FORMAT_RG11B10F:
420 case AIL_ISA_FORMAT_RGB9E5:
421 return false;
422 default:
423 return true;
424 }
425 }
426
427 struct ail_pixel_format_entry {
428 uint8_t channels;
429 uint8_t type;
430 bool texturable : 1;
431 enum pipe_format renderable;
432 };
433
434 extern const struct ail_pixel_format_entry ail_pixel_format[PIPE_FORMAT_COUNT];
435
436 static inline bool
437 ail_is_valid_pixel_format(enum pipe_format format)
438 {
439 return ail_pixel_format[format].texturable;
440 }
441
442 /* Query whether an image with the specified layout is compressible */
443 static inline bool
444 ail_can_compress(enum pipe_format format, unsigned w_px, unsigned h_px,
445 unsigned sample_count_sa)
446 {
447 assert(sample_count_sa == 1 || sample_count_sa == 2 || sample_count_sa == 4);
448
449 /* We compress via the PBE, so we can only compress PBE-writeable formats. */
450 if (ail_pixel_format[format].renderable == PIPE_FORMAT_NONE &&
451 !util_format_is_depth_or_stencil(format))
452 return false;
453
454 /* Lossy-compressed texture formats cannot be compressed */
455 assert(!util_format_is_compressed(format) &&
456 "block-compressed formats are not renderable");
457
458 /* Small textures cannot be compressed */
459 return ail_effective_width_sa(w_px, sample_count_sa) >= 16 &&
460 ail_effective_height_sa(h_px, sample_count_sa) >= 16;
461 }
462
463 /* AGX compression mode for an uncompessed subtile. Frustratingly, this seems to
464 * depend on the format. It is possible that modes are actual 8-bit structures
465 * with multiple fields rather than plain enumerations.
466 */
467 #define AIL_COMP_UNCOMPRESSED_1 0x1f
468 #define AIL_COMP_UNCOMPRESSED_2 0x3f
469 #define AIL_COMP_UNCOMPRESSED_4 0x7f
470 #define AIL_COMP_UNCOMPRESSED_8_16 0xff
471
472 /* AGX compression mode for a solid colour for the subtile. */
473 #define AIL_COMP_SOLID_1 0x60
474 #define AIL_COMP_SOLID_2 0x01
475 #define AIL_COMP_SOLID_4 0x03
476 #define AIL_COMP_SOLID_8_16 0x07
477
478 static inline uint8_t
479 ail_subtile_uncompressed_mode(enum pipe_format format)
480 {
481 /* clang-format off */
482 switch (util_format_get_blocksize(format)) {
483 case 1: return AIL_COMP_UNCOMPRESSED_1;
484 case 2: return AIL_COMP_UNCOMPRESSED_2;
485 case 4: return AIL_COMP_UNCOMPRESSED_4;
486 case 8:
487 case 16: return AIL_COMP_UNCOMPRESSED_8_16;
488 default: UNREACHABLE("invalid block size");
489 }
490 /* clang-format on */
491 }
492
493 static inline uint8_t
494 ail_subtile_solid_mode(enum pipe_format format)
495 {
496 /* clang-format off */
497 switch (util_format_get_blocksize(format)) {
498 case 1: return AIL_COMP_SOLID_1;
499 case 2: return AIL_COMP_SOLID_2;
500 case 4: return AIL_COMP_SOLID_4;
501 case 8:
502 case 16: return AIL_COMP_SOLID_8_16;
503 default: UNREACHABLE("invalid block size");
504 }
505 /* clang-format on */
506 }
507
508 /*
509 * Compression modes are 8-bit per 8x4 subtile, but grouped into 64-bit for all
510 * modes in a 16x16 tile. This helper replicates a subtile mode to a tile mode
511 * using a SWAR idiom.
512 */
513 static inline uint64_t
514 ail_tile_mode_replicated(uint8_t subtile_mode)
515 {
516 return (uint64_t)subtile_mode * 0x0101010101010101ULL;
517 }
518
519 /*
520 * Composed convenience function.
521 */
522 static inline uint64_t
523 ail_tile_mode_uncompressed(enum pipe_format format)
524 {
525 return ail_tile_mode_replicated(ail_subtile_uncompressed_mode(format));
526 }
527
528 static inline uint64_t
529 ail_tile_mode_solid(enum pipe_format format)
530 {
531 return ail_tile_mode_replicated(ail_subtile_solid_mode(format));
532 }
533
534 /*
535 * For compression, compatible formats must have the same number/size/order of
536 * channels, but may differ in data type. For example, R32_SINT is compatible
537 * with Z32_FLOAT, but not with R16G16_SINT. This is the relation given by the
538 * "channels" part of the decomposed format.
539 *
540 * This has not been exhaustively tested and might be missing some corner cases
541 * around XR formats, but is well-motivated and seems to work.
542 */
543 static inline bool
544 ail_formats_compatible(enum pipe_format a, enum pipe_format b)
545 {
546 return ail_pixel_format[a].channels == ail_pixel_format[b].channels;
547 }
548
549 static inline bool
550 ail_is_view_compatible(struct ail_layout *layout, enum pipe_format view)
551 {
552 return !layout->compressed || ail_formats_compatible(layout->format, view);
553 }
554
555 /*
556 * We generally use ail enums instead of DRM format modifiers. These helpers
557 * bridges the gap.
558 */
559 static inline enum ail_tiling
560 ail_drm_modifier_to_tiling(uint64_t modifier)
561 {
562 switch (modifier) {
563 case DRM_FORMAT_MOD_LINEAR:
564 return AIL_TILING_LINEAR;
565 case DRM_FORMAT_MOD_APPLE_GPU_TILED:
566 case DRM_FORMAT_MOD_APPLE_GPU_TILED_COMPRESSED:
567 return AIL_TILING_GPU;
568 default:
569 UNREACHABLE("Unsupported modifier");
570 }
571 }
572
573 static inline bool
574 ail_is_drm_modifier_compressed(uint64_t modifier)
575 {
576 switch (modifier) {
577 case DRM_FORMAT_MOD_LINEAR:
578 case DRM_FORMAT_MOD_APPLE_GPU_TILED:
579 return false;
580 case DRM_FORMAT_MOD_APPLE_GPU_TILED_COMPRESSED:
581 return true;
582 default:
583 UNREACHABLE("Unsupported modifier");
584 }
585 }
586
587 /*
588 * Constants for sparse page tables. See docs/drivers/asahi.rst.
589 */
590 #define AIL_SPARSE_ELSIZE_B 4
591 #define AIL_PAGES_PER_FOLIO 256
592 #define AIL_IMAGE_SIZE_PER_FOLIO_B (AIL_PAGESIZE * AIL_PAGES_PER_FOLIO)
593 #define AIL_FOLIO_SIZE_EL (AIL_PAGES_PER_FOLIO * 2)
594 #define AIL_FOLIO_SIZE_B (AIL_FOLIO_SIZE_EL * AIL_SPARSE_ELSIZE_B)
595
596 static inline unsigned
597 ail_bytes_to_pages(unsigned x_B)
598 {
599 assert((x_B % AIL_PAGESIZE) == 0);
600 return x_B / AIL_PAGESIZE;
601 }
602
603 /*
604 * Map a logical page of the image (i.e. page = logical address / page_size) to
605 * a word-index into the sparse table.
606 */
607 static inline unsigned
608 ail_page_to_sparse_index_el(const struct ail_layout *layout, unsigned z,
609 unsigned page)
610 {
611 unsigned z_base = z * layout->sparse_folios_per_layer;
612 unsigned folio = page / AIL_PAGES_PER_FOLIO;
613 unsigned index_in_folio = page % AIL_PAGES_PER_FOLIO;
614 unsigned idx = ((z_base + folio) * AIL_FOLIO_SIZE_EL) + index_in_folio;
615
616 assert((idx * AIL_SPARSE_ELSIZE_B) < layout->sparse_table_size_B);
617 return idx;
618 }
619
620 #ifdef __cplusplus
621 } /* extern C */
622 #endif