• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     * Twiddled (Morton order). Always allowed.
31     */
32    AIL_TILING_TWIDDLED,
33 
34    /**
35     * Twiddled (Morton order) with compression.
36     */
37    AIL_TILING_TWIDDLED_COMPRESSED,
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    /** Texture format */
74    enum pipe_format format;
75 
76    /**
77     * If tiling is LINEAR, the number of bytes between adjacent rows of
78     * elements. Otherwise, this field is zero.
79     */
80    uint32_t linear_stride_B;
81 
82    /**
83     * Stride between layers of an array texture, including a cube map. Layer i
84     * begins at offset (i * layer_stride_B) from the beginning of the texture.
85     *
86     * If depth_px = 1, the value of this field is UNDEFINED.
87     */
88    uint64_t layer_stride_B;
89 
90    /**
91     * Whether the layer stride is aligned to the page size or not. The hardware
92     * needs this flag to compute the implicit layer stride.
93     */
94    bool page_aligned_layers;
95 
96    /**
97     * Offsets of mip levels within a layer.
98     */
99    uint64_t level_offsets_B[AIL_MAX_MIP_LEVELS];
100 
101    /**
102     * For the compressed buffer, offsets of mip levels within a layer.
103     */
104    uint64_t level_offsets_compressed_B[AIL_MAX_MIP_LEVELS];
105 
106    /**
107     * If tiling is TWIDDLED, the tile size used for each mip level within a
108     * layer. Calculating tile sizes is the sole responsibility of
109     * ail_initialized_twiddled.
110     */
111    struct ail_tile tilesize_el[AIL_MAX_MIP_LEVELS];
112 
113    /**
114     * If tiling is TWIDDLED, the stride in elements used for each mip level
115     * within a layer. Calculating level strides is the sole responsibility of
116     * ail_initialized_twiddled. This is necessary because compressed pixel
117     * formats may add extra stride padding.
118     */
119    uint32_t stride_el[AIL_MAX_MIP_LEVELS];
120 
121    /* Offset of the start of the compression metadata buffer */
122    uint32_t metadata_offset_B;
123 
124    /* Stride between subsequent layers in the compression metadata buffer */
125    uint64_t compression_layer_stride_B;
126 
127    /* Size of entire texture */
128    uint64_t size_B;
129 
130    /* Must the layout support writeable images? If false, the layout MUST NOT be
131     * used as a writeable image (either PBE or image atomics).
132     */
133    bool writeable_image;
134 
135    /* Must the layout support rendering? If false, the layout MUST NOT be used
136     * for rendering, either PBE or ZLS.
137     */
138    bool renderable;
139 };
140 
141 static inline uint32_t
ail_get_linear_stride_B(const struct ail_layout * layout,ASSERTED uint8_t level)142 ail_get_linear_stride_B(const struct ail_layout *layout, ASSERTED uint8_t level)
143 {
144    assert(layout->tiling == AIL_TILING_LINEAR && "Invalid usage");
145    assert(level == 0 && "Strided linear mipmapped textures are unsupported");
146 
147    return layout->linear_stride_B;
148 }
149 
150 /*
151  * For WSI purposes, we need to associate a stride with all layouts. In the
152  * hardware, only strided linear images have an associated stride, there is no
153  * natural stride associated with twiddled images. However, various clients
154  * assert that the stride is valid for the image if it were linear (even if it
155  * is in fact not linear). In those cases, by convention we use the minimum
156  * valid such stride.
157  */
158 static inline uint32_t
ail_get_wsi_stride_B(const struct ail_layout * layout,unsigned level)159 ail_get_wsi_stride_B(const struct ail_layout *layout, unsigned level)
160 {
161    assert(level == 0 && "Mipmaps cannot be shared as WSI");
162 
163    if (layout->tiling == AIL_TILING_LINEAR)
164       return ail_get_linear_stride_B(layout, level);
165    else
166       return util_format_get_stride(layout->format, layout->width_px);
167 }
168 
169 static inline uint32_t
ail_get_layer_offset_B(const struct ail_layout * layout,unsigned z_px)170 ail_get_layer_offset_B(const struct ail_layout *layout, unsigned z_px)
171 {
172    return z_px * layout->layer_stride_B;
173 }
174 
175 static inline uint32_t
ail_get_level_offset_B(const struct ail_layout * layout,unsigned level)176 ail_get_level_offset_B(const struct ail_layout *layout, unsigned level)
177 {
178    return layout->level_offsets_B[level];
179 }
180 
181 static inline uint32_t
ail_get_layer_level_B(const struct ail_layout * layout,unsigned z_px,unsigned level)182 ail_get_layer_level_B(const struct ail_layout *layout, unsigned z_px,
183                       unsigned level)
184 {
185    return ail_get_layer_offset_B(layout, z_px) +
186           ail_get_level_offset_B(layout, level);
187 }
188 
189 static inline uint32_t
ail_get_level_size_B(const struct ail_layout * layout,unsigned level)190 ail_get_level_size_B(const struct ail_layout *layout, unsigned level)
191 {
192    if (layout->tiling == AIL_TILING_LINEAR) {
193       assert(level == 0);
194       return layout->layer_stride_B;
195    } else {
196       assert(level + 1 < ARRAY_SIZE(layout->level_offsets_B));
197       return layout->level_offsets_B[level + 1] -
198              layout->level_offsets_B[level];
199    }
200 }
201 
202 static inline uint32_t
ail_get_linear_pixel_B(const struct ail_layout * layout,ASSERTED unsigned level,uint32_t x_px,uint32_t y_px,uint32_t z_px)203 ail_get_linear_pixel_B(const struct ail_layout *layout, ASSERTED unsigned level,
204                        uint32_t x_px, uint32_t y_px, uint32_t z_px)
205 {
206    assert(level == 0 && "Strided linear mipmapped textures are unsupported");
207    assert(util_format_get_blockwidth(layout->format) == 1 &&
208           "Strided linear block formats unsupported");
209    assert(util_format_get_blockheight(layout->format) == 1 &&
210           "Strided linear block formats unsupported");
211    assert(layout->sample_count_sa == 1 &&
212           "Strided linear multisampling unsupported");
213 
214    return ail_get_layer_offset_B(layout, z_px) +
215           (y_px * ail_get_linear_stride_B(layout, level)) +
216           (x_px * util_format_get_blocksize(layout->format));
217 }
218 
219 static inline unsigned
ail_effective_width_sa(unsigned width_px,unsigned sample_count_sa)220 ail_effective_width_sa(unsigned width_px, unsigned sample_count_sa)
221 {
222    return width_px * (sample_count_sa == 4 ? 2 : 1);
223 }
224 
225 static inline unsigned
ail_effective_height_sa(unsigned height_px,unsigned sample_count_sa)226 ail_effective_height_sa(unsigned height_px, unsigned sample_count_sa)
227 {
228    return height_px * (sample_count_sa >= 2 ? 2 : 1);
229 }
230 
231 static inline unsigned
ail_metadata_width_tl(struct ail_layout * layout,unsigned level)232 ail_metadata_width_tl(struct ail_layout *layout, unsigned level)
233 {
234    unsigned px = u_minify(layout->width_px, level);
235    uint32_t sa = ail_effective_width_sa(px, layout->sample_count_sa);
236    return DIV_ROUND_UP(sa, 16);
237 }
238 
239 static inline unsigned
ail_metadata_height_tl(struct ail_layout * layout,unsigned level)240 ail_metadata_height_tl(struct ail_layout *layout, unsigned level)
241 {
242    unsigned px = u_minify(layout->height_px, level);
243    uint32_t sa = ail_effective_height_sa(px, layout->sample_count_sa);
244    return DIV_ROUND_UP(sa, 16);
245 }
246 
247 static inline bool
ail_is_compressed(const struct ail_layout * layout)248 ail_is_compressed(const struct ail_layout *layout)
249 {
250    return layout->tiling == AIL_TILING_TWIDDLED_COMPRESSED;
251 }
252 
253 /*
254  * Even when the base mip level is compressed, high levels of the miptree
255  * (smaller than 16 pixels on either axis) are not compressed as it would be
256  * pointless. This queries this case.
257  */
258 static inline bool
ail_is_level_compressed(const struct ail_layout * layout,unsigned level)259 ail_is_level_compressed(const struct ail_layout *layout, unsigned level)
260 {
261    unsigned width_sa = ALIGN(
262       ail_effective_width_sa(layout->width_px, layout->sample_count_sa), 16);
263 
264    unsigned height_sa = ALIGN(
265       ail_effective_height_sa(layout->height_px, layout->sample_count_sa), 16);
266 
267    return ail_is_compressed(layout) &&
268           u_minify(MAX2(width_sa, height_sa), level) >= 16;
269 }
270 
271 static inline bool
ail_is_level_twiddled_uncompressed(const struct ail_layout * layout,unsigned level)272 ail_is_level_twiddled_uncompressed(const struct ail_layout *layout,
273                                    unsigned level)
274 {
275    switch (layout->tiling) {
276    case AIL_TILING_TWIDDLED:
277       return true;
278    case AIL_TILING_TWIDDLED_COMPRESSED:
279       return !ail_is_level_compressed(layout, level);
280    default:
281       return false;
282    }
283 }
284 
285 void ail_make_miptree(struct ail_layout *layout);
286 
287 void ail_detile(void *_tiled, void *_linear,
288                 const struct ail_layout *tiled_layout, unsigned level,
289                 unsigned linear_pitch_B, unsigned sx_px, unsigned sy_px,
290                 unsigned width_px, unsigned height_px);
291 
292 void ail_tile(void *_tiled, void *_linear,
293               const struct ail_layout *tiled_layout, unsigned level,
294               unsigned linear_pitch_B, unsigned sx_px, unsigned sy_px,
295               unsigned width_px, unsigned height_px);
296 
297 /* Define aliases for the subset formats that are accessible in the ISA. These
298  * subsets disregard component mapping and number of components. This
299  * constitutes ABI with the compiler.
300  */
301 enum ail_isa_format {
302    AIL_ISA_FORMAT_I8 = PIPE_FORMAT_R8_UINT,
303    AIL_ISA_FORMAT_I16 = PIPE_FORMAT_R16_UINT,
304    AIL_ISA_FORMAT_I32 = PIPE_FORMAT_R32_UINT,
305    AIL_ISA_FORMAT_F16 = PIPE_FORMAT_R16_FLOAT,
306    AIL_ISA_FORMAT_U8NORM = PIPE_FORMAT_R8_UNORM,
307    AIL_ISA_FORMAT_S8NORM = PIPE_FORMAT_R8_SNORM,
308    AIL_ISA_FORMAT_U16NORM = PIPE_FORMAT_R16_UNORM,
309    AIL_ISA_FORMAT_S16NORM = PIPE_FORMAT_R16_SNORM,
310    AIL_ISA_FORMAT_RGB10A2 = PIPE_FORMAT_R10G10B10A2_UNORM,
311    AIL_ISA_FORMAT_SRGBA8 = PIPE_FORMAT_R8G8B8A8_SRGB,
312    AIL_ISA_FORMAT_RG11B10F = PIPE_FORMAT_R11G11B10_FLOAT,
313    AIL_ISA_FORMAT_RGB9E5 = PIPE_FORMAT_R9G9B9E5_FLOAT
314 };
315 
316 /*
317  * The architecture load/store instructions support masking, but packed formats
318  * are not compatible with masking. Check if a format is packed.
319  */
320 static inline bool
ail_isa_format_supports_mask(enum ail_isa_format format)321 ail_isa_format_supports_mask(enum ail_isa_format format)
322 {
323    switch (format) {
324    case AIL_ISA_FORMAT_RGB10A2:
325    case AIL_ISA_FORMAT_RG11B10F:
326    case AIL_ISA_FORMAT_RGB9E5:
327       return false;
328    default:
329       return true;
330    }
331 }
332 
333 struct ail_pixel_format_entry {
334    uint8_t channels;
335    uint8_t type;
336    bool texturable : 1;
337    enum pipe_format renderable;
338 };
339 
340 extern const struct ail_pixel_format_entry ail_pixel_format[PIPE_FORMAT_COUNT];
341 
342 static inline bool
ail_is_valid_pixel_format(enum pipe_format format)343 ail_is_valid_pixel_format(enum pipe_format format)
344 {
345    return ail_pixel_format[format].texturable;
346 }
347 
348 /* Query whether an image with the specified layout is compressible */
349 static inline bool
ail_can_compress(enum pipe_format format,unsigned w_px,unsigned h_px,unsigned sample_count_sa)350 ail_can_compress(enum pipe_format format, unsigned w_px, unsigned h_px,
351                  unsigned sample_count_sa)
352 {
353    assert(sample_count_sa == 1 || sample_count_sa == 2 || sample_count_sa == 4);
354 
355    /* We compress via the PBE, so we can only compress PBE-writeable formats. */
356    if (ail_pixel_format[format].renderable == PIPE_FORMAT_NONE &&
357        !util_format_is_depth_or_stencil(format))
358       return false;
359 
360    /* Lossy-compressed texture formats cannot be compressed */
361    assert(!util_format_is_compressed(format) &&
362           "block-compressed formats are not renderable");
363 
364    /* Small textures cannot be compressed */
365    return ail_effective_width_sa(w_px, sample_count_sa) >= 16 &&
366           ail_effective_height_sa(h_px, sample_count_sa) >= 16;
367 }
368 
369 /* AGX compression mode for a solid colour for the subtile */
370 #define AIL_COMP_SOLID 0x3
371 
372 /* AGX compression mode for an uncompessed subtile. Frustratingly, this seems to
373  * depend on the format. It is possible that modes are actual 8-bit structures
374  * with multiple fields rather than plain enumerations.
375  */
376 #define AIL_COMP_UNCOMPRESSED_1    0x1f
377 #define AIL_COMP_UNCOMPRESSED_2    0x3f
378 #define AIL_COMP_UNCOMPRESSED_4    0x7f
379 #define AIL_COMP_UNCOMPRESSED_8_16 0xff
380 
381 static inline uint8_t
ail_subtile_uncompressed_mode(enum pipe_format format)382 ail_subtile_uncompressed_mode(enum pipe_format format)
383 {
384    /* clang-format off */
385    switch (util_format_get_blocksize(format)) {
386    case  1: return AIL_COMP_UNCOMPRESSED_1;
387    case  2: return AIL_COMP_UNCOMPRESSED_2;
388    case  4: return AIL_COMP_UNCOMPRESSED_4;
389    case  8:
390    case 16: return AIL_COMP_UNCOMPRESSED_8_16;
391    default: unreachable("invalid block size");
392    }
393    /* clang-format on */
394 }
395 
396 /*
397  * Compression modes are 8-bit per 8x4 subtile, but grouped into 64-bit for all
398  * modes in a 16x16 tile. This helper replicates a subtile mode to a tile mode
399  * using a SWAR idiom.
400  */
401 static inline uint64_t
ail_tile_mode_replicated(uint8_t subtile_mode)402 ail_tile_mode_replicated(uint8_t subtile_mode)
403 {
404    return (uint64_t)subtile_mode * 0x0101010101010101ULL;
405 }
406 
407 /*
408  * Composed convenience function.
409  */
410 static inline uint64_t
ail_tile_mode_uncompressed(enum pipe_format format)411 ail_tile_mode_uncompressed(enum pipe_format format)
412 {
413    return ail_tile_mode_replicated(ail_subtile_uncompressed_mode(format));
414 }
415 
416 /*
417  * For compression, compatible formats must have the same number/size/order of
418  * channels, but may differ in data type. For example, R32_SINT is compatible
419  * with Z32_FLOAT, but not with R16G16_SINT. This is the relation given by the
420  * "channels" part of the decomposed format.
421  *
422  * This has not been exhaustively tested and might be missing some corner cases
423  * around XR formats, but is well-motivated and seems to work.
424  */
425 static inline bool
ail_formats_compatible(enum pipe_format a,enum pipe_format b)426 ail_formats_compatible(enum pipe_format a, enum pipe_format b)
427 {
428    return ail_pixel_format[a].channels == ail_pixel_format[b].channels;
429 }
430 
431 static inline bool
ail_is_view_compatible(struct ail_layout * layout,enum pipe_format view)432 ail_is_view_compatible(struct ail_layout *layout, enum pipe_format view)
433 {
434    return !ail_is_compressed(layout) ||
435           ail_formats_compatible(layout->format, view);
436 }
437 
438 /* Fake values, pending UAPI upstreaming */
439 #ifndef DRM_FORMAT_MOD_APPLE_TWIDDLED
440 #define DRM_FORMAT_MOD_APPLE_TWIDDLED (2)
441 #endif
442 #ifndef DRM_FORMAT_MOD_APPLE_TWIDDLED_COMPRESSED
443 #define DRM_FORMAT_MOD_APPLE_TWIDDLED_COMPRESSED (3)
444 #endif
445 
446 /*
447  * We generally use ail enums instead of DRM format modifiers. This helper
448  * bridges the gap.
449  */
450 static inline enum ail_tiling
ail_drm_modifier_to_tiling(uint64_t modifier)451 ail_drm_modifier_to_tiling(uint64_t modifier)
452 {
453    switch (modifier) {
454    case DRM_FORMAT_MOD_LINEAR:
455       return AIL_TILING_LINEAR;
456    case DRM_FORMAT_MOD_APPLE_TWIDDLED:
457       return AIL_TILING_TWIDDLED;
458    case DRM_FORMAT_MOD_APPLE_TWIDDLED_COMPRESSED:
459       return AIL_TILING_TWIDDLED_COMPRESSED;
460    default:
461       unreachable("Unsupported modifier");
462    }
463 }
464 
465 #ifdef __cplusplus
466 } /* extern C */
467 #endif
468