xref: /aosp_15_r20/external/mesa3d/src/asahi/layout/tiling.cc (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright 2021 Alyssa Rosenzweig
3  * Copyright 2019 Collabora, Ltd.
4  * SPDX-License-Identifier: MIT
5  */
6 
7 #include <assert.h>
8 #include <stdbool.h>
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include "util/macros.h"
13 #include "layout.h"
14 
15 /* Z-order with rectangular (NxN or 2NxN) tiles, at most 128x128:
16  *
17  * 	[y6][x6][y5][x5][y4][x4]y3][x3][y2][x2][y1][x1][y0][x0]
18  *
19  * Efficient tiling algorithm described in
20  * https://fgiesen.wordpress.com/2011/01/17/texture-tiling-and-swizzling/ but
21  * for posterity, we split into X and Y parts, and are faced with the problem
22  * of incrementing:
23  *
24  * 	0 [x6] 0 [x5] 0 [x4] 0 [x3] 0 [x2] 0 [x1] 0 [x0]
25  *
26  * To do so, we fill in the "holes" with 1's by adding the bitwise inverse of
27  * the mask of bits we care about
28  *
29  * 	0 [x6] 0 [x5] 0 [x4] 0 [x3] 0 [x2] 0 [x1] 0 [x0]
30  *    + 1  0   1  0   1  0   1  0   1  0   1  0   1  0
31  *    ------------------------------------------------
32  * 	1 [x6] 1 [x5] 1 [x4] 1 [x3] 1 [x2] 1 [x1] 1 [x0]
33  *
34  * Then when we add one, the holes are passed over by forcing carry bits high.
35  * Finally, we need to zero out the holes, by ANDing with the mask of bits we
36  * care about. In total, we get the expression (X + ~mask + 1) & mask, and
37  * applying the two's complement identity, we are left with (X - mask) & mask
38  */
39 
40 #define MOD_POT(x, y) (x) & ((y)-1)
41 
42 typedef struct {
43    uint64_t lo;
44    uint64_t hi;
45 } __attribute__((packed)) ail_uint128_t;
46 
47 static uint32_t
ail_space_bits(unsigned x)48 ail_space_bits(unsigned x)
49 {
50    assert(x < 128 && "offset must be inside the tile");
51 
52    return ((x & 1) << 0) | ((x & 2) << 1) | ((x & 4) << 2) | ((x & 8) << 3) |
53           ((x & 16) << 4) | ((x & 32) << 5) | ((x & 64) << 6);
54 }
55 
56 /*
57  * Given a power-of-two block width/height, construct the mask of "X" bits. This
58  * is found by restricting the full mask of alternating 0s and 1s to only cover
59  * the bottom 2 * log2(dim) bits. That's the same as modding by dim^2.
60  */
61 static uint32_t
ail_space_mask(unsigned x)62 ail_space_mask(unsigned x)
63 {
64    assert(util_is_power_of_two_nonzero(x));
65 
66    return MOD_POT(0x55555555, x * x);
67 }
68 
69 template <typename T, bool is_store>
70 void
memcpy_small(void * _tiled,void * _linear,const struct ail_layout * tiled_layout,unsigned level,unsigned linear_pitch_B,unsigned sx_px,unsigned sy_px,unsigned swidth_px,unsigned sheight_px)71 memcpy_small(void *_tiled, void *_linear, const struct ail_layout *tiled_layout,
72              unsigned level, unsigned linear_pitch_B, unsigned sx_px,
73              unsigned sy_px, unsigned swidth_px, unsigned sheight_px)
74 {
75    enum pipe_format format = tiled_layout->format;
76    unsigned linear_pitch_el = linear_pitch_B / sizeof(T);
77    unsigned stride_el = tiled_layout->stride_el[level];
78    unsigned sx_el = util_format_get_nblocksx(format, sx_px);
79    unsigned sy_el = util_format_get_nblocksy(format, sy_px);
80    unsigned swidth_el = util_format_get_nblocksx(format, swidth_px);
81    unsigned sheight_el = util_format_get_nblocksy(format, sheight_px);
82    unsigned sx_end_el = sx_el + swidth_el;
83    unsigned sy_end_el = sy_el + sheight_el;
84 
85    struct ail_tile tile_size = tiled_layout->tilesize_el[level];
86    unsigned tile_area_el = tile_size.width_el * tile_size.height_el;
87    unsigned tiles_per_row = DIV_ROUND_UP(stride_el, tile_size.width_el);
88    unsigned y_offs_el = ail_space_bits(MOD_POT(sy_el, tile_size.height_el))
89                         << 1;
90    unsigned x_offs_start_el =
91       ail_space_bits(MOD_POT(sx_el, tile_size.width_el));
92    unsigned space_mask_x = ail_space_mask(tile_size.width_el);
93    unsigned space_mask_y = ail_space_mask(tile_size.height_el) << 1;
94    unsigned log2_tile_width_el = util_logbase2(tile_size.width_el);
95    unsigned log2_tile_height_el = util_logbase2(tile_size.height_el);
96 
97    T *linear = (T *)_linear;
98    T *tiled = (T *)_tiled;
99 
100    for (unsigned y_el = sy_el; y_el < sy_end_el; ++y_el) {
101       unsigned y_rowtile = y_el >> log2_tile_height_el;
102       unsigned y_tile = y_rowtile * tiles_per_row;
103       unsigned x_offs_el = x_offs_start_el;
104 
105       T *linear_row = linear;
106 
107       for (unsigned x_el = sx_el; x_el < sx_end_el; ++x_el) {
108          unsigned tile_idx = (y_tile + (x_el >> log2_tile_width_el));
109          unsigned tile_offset_el = tile_idx * tile_area_el;
110 
111          T *ptiled = &tiled[tile_offset_el + y_offs_el + x_offs_el];
112          T *plinear = (linear_row++);
113 
114          if (is_store)
115             *ptiled = *plinear;
116          else
117             *plinear = *ptiled;
118 
119          x_offs_el = (x_offs_el - space_mask_x) & space_mask_x;
120       }
121 
122       y_offs_el = (y_offs_el - space_mask_y) & space_mask_y;
123       linear += linear_pitch_el;
124    }
125 }
126 
127 #define TILED_UNALIGNED_TYPES(blocksize_B, store)                              \
128    if (blocksize_B == 1) {                                                     \
129       memcpy_small<uint8_t, store>(_tiled, _linear, tiled_layout, level,       \
130                                    linear_pitch_B, sx_px, sy_px, swidth_px,    \
131                                    sheight_px);                                \
132    } else if (blocksize_B == 2) {                                              \
133       memcpy_small<uint16_t, store>(_tiled, _linear, tiled_layout, level,      \
134                                     linear_pitch_B, sx_px, sy_px, swidth_px,   \
135                                     sheight_px);                               \
136    } else if (blocksize_B == 4) {                                              \
137       memcpy_small<uint32_t, store>(_tiled, _linear, tiled_layout, level,      \
138                                     linear_pitch_B, sx_px, sy_px, swidth_px,   \
139                                     sheight_px);                               \
140    } else if (blocksize_B == 8) {                                              \
141       memcpy_small<uint64_t, store>(_tiled, _linear, tiled_layout, level,      \
142                                     linear_pitch_B, sx_px, sy_px, swidth_px,   \
143                                     sheight_px);                               \
144    } else if (blocksize_B == 16) {                                             \
145       memcpy_small<ail_uint128_t, store>(_tiled, _linear, tiled_layout, level, \
146                                          linear_pitch_B, sx_px, sy_px,         \
147                                          swidth_px, sheight_px);               \
148    } else {                                                                    \
149       unreachable("Invalid block size");                                       \
150    }
151 
152 void
ail_detile(void * _tiled,void * _linear,const struct ail_layout * tiled_layout,unsigned level,unsigned linear_pitch_B,unsigned sx_px,unsigned sy_px,unsigned swidth_px,unsigned sheight_px)153 ail_detile(void *_tiled, void *_linear, const struct ail_layout *tiled_layout,
154            unsigned level, unsigned linear_pitch_B, unsigned sx_px,
155            unsigned sy_px, unsigned swidth_px, unsigned sheight_px)
156 {
157    unsigned width_px = u_minify(tiled_layout->width_px, level);
158    unsigned height_px = u_minify(tiled_layout->height_px, level);
159    unsigned blocksize_B = util_format_get_blocksize(tiled_layout->format);
160 
161    assert(level < tiled_layout->levels && "Mip level out of bounds");
162    assert(ail_is_level_twiddled_uncompressed(tiled_layout, level) &&
163           "Invalid usage");
164    assert((sx_px + swidth_px) <= width_px && "Invalid usage");
165    assert((sy_px + sheight_px) <= height_px && "Invalid usage");
166 
167    TILED_UNALIGNED_TYPES(blocksize_B, false);
168 }
169 
170 void
ail_tile(void * _tiled,void * _linear,const struct ail_layout * tiled_layout,unsigned level,unsigned linear_pitch_B,unsigned sx_px,unsigned sy_px,unsigned swidth_px,unsigned sheight_px)171 ail_tile(void *_tiled, void *_linear, const struct ail_layout *tiled_layout,
172          unsigned level, unsigned linear_pitch_B, unsigned sx_px,
173          unsigned sy_px, unsigned swidth_px, unsigned sheight_px)
174 {
175    unsigned width_px = u_minify(tiled_layout->width_px, level);
176    unsigned height_px = u_minify(tiled_layout->height_px, level);
177    unsigned blocksize_B = util_format_get_blocksize(tiled_layout->format);
178 
179    assert(level < tiled_layout->levels && "Mip level out of bounds");
180    assert(ail_is_level_twiddled_uncompressed(tiled_layout, level) &&
181           "Invalid usage");
182    assert((sx_px + swidth_px) <= width_px && "Invalid usage");
183    assert((sy_px + sheight_px) <= height_px && "Invalid usage");
184 
185    TILED_UNALIGNED_TYPES(blocksize_B, true);
186 }
187