Debian Codegraph @

src/asahi/layout/tests/test-twiddling.cpp

raw
1 /*
2 * Copyright 2024 Alyssa Rosenzweig
3 * Copyright 2022 Collabora, Ltd.
4 * SPDX-License-Identifier: MIT
5 *
6 */
7
8 #include "layout.h"
9
10 #include "util/format/u_format.h"
11 #include <gtest/gtest.h>
12
13 /*
14 * Reference tiling algorithm, written for clarity rather than performance. See
15 * docs/drivers/asahi.rst for details on the format.
16 */
17
18 static unsigned
19 z_order(unsigned x, unsigned y)
20 {
21 unsigned out = 0;
22
23 for (unsigned i = 0; i < 8; ++i) {
24 unsigned bit = (1 << (2 * i));
25
26 if (x & (1 << i))
27 out |= bit;
28
29 if (y & (1 << i))
30 out |= bit << 1;
31 }
32
33 return out;
34 }
35
36 /* x/y are in blocks */
37 static unsigned
38 tiled_offset_el(struct ail_layout *layout, unsigned level, unsigned x_el,
39 unsigned y_el)
40 {
41 unsigned x_tl = x_el / layout->tilesize_el[level].width_el;
42 unsigned y_tl = y_el / layout->tilesize_el[level].height_el;
43
44 unsigned offs_x_el = x_el % layout->tilesize_el[level].width_el;
45 unsigned offs_y_el = y_el % layout->tilesize_el[level].height_el;
46
47 unsigned offs_in_tile_el = z_order(offs_x_el, offs_y_el);
48
49 unsigned offs_row_el =
50 y_tl *
51 align(layout->stride_el[level], layout->tilesize_el[level].width_el) *
52 layout->tilesize_el[level].height_el;
53
54 unsigned offs_col_el = x_tl * layout->tilesize_el[level].width_el *
55 layout->tilesize_el[level].height_el;
56
57 return offs_row_el + offs_col_el + offs_in_tile_el;
58 }
59
60 static unsigned
61 linear_offset_B(unsigned x_el, unsigned y_el, unsigned stride_B,
62 unsigned blocksize_B)
63 {
64 return (stride_B * y_el) + (x_el * blocksize_B);
65 }
66
67 static void
68 ref_access_tiled(uint8_t *tiled, uint8_t *linear, struct ail_layout *layout,
69 unsigned region_x_px, unsigned region_y_px, unsigned w_px,
70 unsigned h_px, uint32_t linear_stride_B, bool dst_is_tiled)
71 {
72 unsigned blocksize_B = util_format_get_blocksize(layout->format);
73
74 unsigned w_el = util_format_get_nblocksx(layout->format, w_px);
75 unsigned h_el = util_format_get_nblocksy(layout->format, h_px);
76
77 unsigned region_x_el = util_format_get_nblocksx(layout->format, region_x_px);
78 unsigned region_y_el = util_format_get_nblocksy(layout->format, region_y_px);
79
80 for (unsigned linear_y_el = 0; linear_y_el < h_el; ++linear_y_el) {
81 for (unsigned linear_x_el = 0; linear_x_el < w_el; ++linear_x_el) {
82 unsigned tiled_x_el = region_x_el + linear_x_el;
83 unsigned tiled_y_el = region_y_el + linear_y_el;
84
85 uint8_t *linear_ptr =
86 linear + linear_offset_B(linear_x_el, linear_y_el, linear_stride_B,
87 blocksize_B);
88
89 unsigned twiddled_offs_B =
90 blocksize_B * tiled_offset_el(layout, 0, tiled_x_el, tiled_y_el);
91
92 /* While we're at it, check that ail_get_twiddled_block_B does the
93 * right thing where it is defined (at tile boundaries). We can't
94 * otherwise use it here as it is part of the routine under test.
95 */
96 if ((tiled_x_el % layout->tilesize_el[0].width_el) == 0 &&
97 (tiled_y_el % layout->tilesize_el[0].height_el) == 0) {
98
99 unsigned tiled_x_px =
100 tiled_x_el * util_format_get_blockwidth(layout->format);
101
102 unsigned tiled_y_px =
103 tiled_y_el * util_format_get_blockwidth(layout->format);
104
105 EXPECT_EQ(
106 ail_get_twiddled_block_B(layout, 0, tiled_x_px, tiled_y_px, 0),
107 twiddled_offs_B);
108 }
109
110 uint8_t *tiled_ptr = tiled + twiddled_offs_B;
111
112 if (dst_is_tiled) {
113 memcpy(tiled_ptr, linear_ptr, blocksize_B);
114 } else {
115 memcpy(linear_ptr, tiled_ptr, blocksize_B);
116 }
117 }
118 }
119 }
120
121 /*
122 * Helper to build test cases for tiled texture access. This test suite compares
123 * the above reference tiling algorithm to the optimized algorithm used in
124 * production.
125 */
126 static void
127 test(unsigned width, unsigned height, unsigned rx, unsigned ry, unsigned rw,
128 unsigned rh, unsigned linear_stride, enum pipe_format format, bool store)
129 {
130 unsigned bpp = util_format_get_blocksize(format);
131 struct ail_layout layout = {
132 .width_px = width,
133 .height_px = height,
134 .depth_px = 1,
135 .sample_count_sa = 1,
136 .levels = 1,
137 .tiling = AIL_TILING_GPU,
138 .format = format,
139 };
140
141 ail_make_miptree(&layout);
142
143 uint8_t *tiled = (uint8_t *)calloc(bpp, layout.size_B);
144 uint8_t *linear = (uint8_t *)calloc(bpp, rh * linear_stride);
145 uint8_t *ref =
146 (uint8_t *)calloc(bpp, store ? layout.size_B : (rh * linear_stride));
147
148 if (store) {
149 for (unsigned i = 0; i < bpp * rh * linear_stride; ++i) {
150 linear[i] = (i & 0xFF);
151 }
152
153 ail_tile(tiled, linear, &layout, 0, linear_stride, rx, ry, rw, rh);
154 ref_access_tiled(ref, linear, &layout, rx, ry, rw, rh, linear_stride,
155 true);
156
157 EXPECT_EQ(memcmp(ref, tiled, layout.size_B), 0);
158 } else {
159 for (unsigned i = 0; i < layout.size_B; ++i) {
160 tiled[i] = (i & 0xFF);
161 }
162
163 ail_detile(tiled, linear, &layout, 0, linear_stride, rx, ry, rw, rh);
164 ref_access_tiled(tiled, ref, &layout, rx, ry, rw, rh, linear_stride,
165 false);
166
167 EXPECT_EQ(memcmp(ref, linear, bpp * rh * linear_stride), 0);
168 }
169
170 free(ref);
171 free(tiled);
172 free(linear);
173 }
174
175 static void
176 test_ldst(unsigned width, unsigned height, unsigned rx, unsigned ry,
177 unsigned rw, unsigned rh, unsigned linear_stride,
178 enum pipe_format format)
179 {
180 test(width, height, rx, ry, rw, rh, linear_stride, format, true);
181 test(width, height, rx, ry, rw, rh, linear_stride, format, false);
182 }
183
184 TEST(Twiddling, RegularFormats)
185 {
186 test_ldst(233, 173, 0, 0, 233, 173, 233, PIPE_FORMAT_R8_UINT);
187 test_ldst(233, 173, 0, 0, 233, 173, 233 * 2, PIPE_FORMAT_R8G8_UINT);
188 test_ldst(233, 173, 0, 0, 233, 173, 233 * 4, PIPE_FORMAT_R32_UINT);
189 test_ldst(173, 143, 0, 0, 173, 143, 173 * 8, PIPE_FORMAT_R32G32_UINT);
190 test_ldst(133, 143, 0, 0, 133, 143, 133 * 16, PIPE_FORMAT_R32G32B32A32_UINT);
191 }
192
193 TEST(Twiddling, UnpackedStrides)
194 {
195 test_ldst(213, 17, 0, 0, 213, 17, 369 * 1, PIPE_FORMAT_R8_SINT);
196 test_ldst(213, 17, 0, 0, 213, 17, 369 * 2, PIPE_FORMAT_R8G8_SINT);
197 test_ldst(213, 17, 0, 0, 213, 17, 369 * 4, PIPE_FORMAT_R32_SINT);
198 test_ldst(213, 17, 0, 0, 213, 17, 369 * 8, PIPE_FORMAT_R32G32_SINT);
199 test_ldst(213, 17, 0, 0, 213, 17, 369 * 16, PIPE_FORMAT_R32G32B32A32_SINT);
200 }
201
202 TEST(Twiddling, PartialAccess)
203 {
204 test_ldst(283, 171, 3, 1, 131, 7, 369 * 1, PIPE_FORMAT_R8_UNORM);
205 test_ldst(283, 171, 3, 1, 131, 7, 369 * 2, PIPE_FORMAT_R8G8_UNORM);
206 test_ldst(283, 171, 3, 1, 131, 7, 369 * 4, PIPE_FORMAT_R32_UNORM);
207 test_ldst(283, 171, 3, 1, 131, 7, 369 * 8, PIPE_FORMAT_R32G32_UNORM);
208 test_ldst(283, 171, 3, 1, 131, 7, 369 * 16, PIPE_FORMAT_R32G32B32A32_UNORM);
209 }
210
211 TEST(Twiddling, ETC)
212 {
213 /* Block alignment assumed */
214 test_ldst(32, 32, 0, 0, 32, 32, 512, PIPE_FORMAT_ETC1_RGB8);
215 test_ldst(32, 256, 0, 0, 32, 256, 512, PIPE_FORMAT_ETC2_RGB8A1);
216 test_ldst(32, 512, 0, 0, 32, 512, 512, PIPE_FORMAT_ETC2_RG11_SNORM);
217 }
218
219 TEST(Twiddling, PartialETC)
220 {
221 /* Block alignment assumed */
222 test_ldst(32, 32, 4, 8, 16, 12, 512, PIPE_FORMAT_ETC1_RGB8);
223 test_ldst(32, 32, 4, 8, 16, 12, 512, PIPE_FORMAT_ETC2_RGB8A1);
224 test_ldst(32, 32, 4, 8, 16, 12, 512, PIPE_FORMAT_ETC2_RG11_SNORM);
225 }
226
227 TEST(Twiddling, DXT)
228 {
229 /* Block alignment assumed */
230 test_ldst(32, 32, 0, 0, 32, 32, 512, PIPE_FORMAT_DXT1_RGB);
231 test_ldst(32, 32, 0, 0, 32, 32, 512, PIPE_FORMAT_DXT3_RGBA);
232 test_ldst(32, 32, 0, 0, 32, 32, 512, PIPE_FORMAT_DXT5_RGBA);
233 }
234
235 TEST(Twiddling, PartialDXT)
236 {
237 /* Block alignment assumed */
238 test_ldst(32, 32, 4, 8, 16, 12, 512, PIPE_FORMAT_DXT1_RGB);
239 test_ldst(32, 32, 4, 8, 16, 12, 512, PIPE_FORMAT_DXT3_RGBA);
240 test_ldst(32, 32, 4, 8, 16, 12, 512, PIPE_FORMAT_DXT5_RGBA);
241 }
242
243 TEST(Twiddling, ASTC)
244 {
245 /* Block alignment assumed */
246 test_ldst(40, 40, 0, 0, 40, 40, 512, PIPE_FORMAT_ASTC_4x4);
247 test_ldst(50, 40, 0, 0, 50, 40, 512, PIPE_FORMAT_ASTC_5x4);
248 test_ldst(50, 50, 0, 0, 50, 50, 512, PIPE_FORMAT_ASTC_5x5);
249 }
250
251 TEST(Twiddling, PartialASTC)
252 {
253 /* Block alignment assumed */
254 test_ldst(40, 40, 4, 4, 16, 8, 512, PIPE_FORMAT_ASTC_4x4);
255 test_ldst(50, 40, 5, 4, 10, 8, 512, PIPE_FORMAT_ASTC_5x4);
256 test_ldst(50, 50, 5, 5, 10, 10, 512, PIPE_FORMAT_ASTC_5x5);
257 }