xref: /aosp_15_r20/external/mesa3d/src/asahi/layout/tests/test-generated.cpp (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright 2022 Alyssa Rosenzweig
3  * Copyright 2022 Asahi Lina
4  * SPDX-License-Identifier: MIT
5  */
6 
7 #include <gtest/gtest.h>
8 #include "layout.h"
9 
10 /*
11  * Test texture layouts with test cases extracted from texture structure dumps
12  * produced by Metal.
13  *
14  * The extracted test cases are stored in separate files which do not get
15  * clang-formatted. They do still get parsed as C code, though. They may be
16  * sorted with `sort -t"," -k1,1 -k2,5n | uniq`.
17  */
18 struct sizetest {
19    enum pipe_format format;
20    uint32_t width, height, depth;
21    uint8_t levels;
22    uint32_t size;
23 };
24 
25 struct miptest {
26    enum pipe_format format;
27    uint32_t width, height;
28    uint8_t levels;
29    uint32_t offsets[16];
30 };
31 
32 struct msaatest {
33    enum pipe_format format;
34    uint32_t width, height, depth;
35    uint8_t levels;
36    uint8_t samples;
37    bool is_compressed;
38    uint32_t meta_offset;
39    uint32_t size;
40 };
41 
42 static struct sizetest comptests[] = {
43 #include "comp-twiddled.txt"
44 };
45 
46 static struct sizetest sizetests[] = {
47 #include "uncomp-twiddled.txt"
48 };
49 
50 static struct miptest miptests[] = {
51 #include "miptree.txt"
52 };
53 
54 static struct msaatest msaatests[] = {
55 #include "msaa.txt"
56 };
57 
TEST(Generated,CompTwiddled)58 TEST(Generated, CompTwiddled)
59 {
60    for (unsigned i = 0; i < ARRAY_SIZE(comptests); ++i) {
61       struct sizetest test = comptests[i];
62 
63       struct ail_layout layout = {
64          .width_px = test.width,
65          .height_px = test.height,
66          .depth_px = test.depth,
67          .sample_count_sa = 1,
68          .levels = test.levels,
69          .tiling = AIL_TILING_TWIDDLED_COMPRESSED,
70          .format = test.format,
71       };
72 
73       ail_make_miptree(&layout);
74 
75       EXPECT_EQ(layout.size_B, test.size)
76          << test.width << "x" << test.height << "x" << test.depth << " "
77          << (int)test.levels << "L " << util_format_short_name(test.format)
78          << " compressed texture has wrong allocation size, off by "
79          << ((int)layout.size_B - (int)test.size);
80    }
81 }
82 
TEST(Generated,UncompTwiddled)83 TEST(Generated, UncompTwiddled)
84 {
85    for (unsigned i = 0; i < ARRAY_SIZE(sizetests); ++i) {
86       struct sizetest test = sizetests[i];
87 
88       struct ail_layout layout = {
89          .width_px = test.width,
90          .height_px = test.height,
91          .depth_px = test.depth,
92          .sample_count_sa = 1,
93          .levels = test.levels,
94          .tiling = AIL_TILING_TWIDDLED,
95          .format = test.format,
96       };
97 
98       ail_make_miptree(&layout);
99 
100       EXPECT_EQ(layout.size_B, test.size)
101          << test.width << "x" << test.height << "x" << test.depth << " "
102          << (int)test.levels << "L " << util_format_short_name(test.format)
103          << " uncompressed texture has wrong allocation size, off by "
104          << ((int)layout.size_B - (int)test.size);
105    }
106 }
107 
TEST(Generated,Miptree2D)108 TEST(Generated, Miptree2D)
109 {
110    for (unsigned i = 0; i < ARRAY_SIZE(miptests); ++i) {
111       struct miptest test = miptests[i];
112 
113       struct ail_layout layout = {
114          .width_px = test.width,
115          .height_px = test.height,
116          .depth_px = 1,
117          .sample_count_sa = 1,
118          .levels = test.levels,
119          .tiling = AIL_TILING_TWIDDLED,
120          .format = test.format,
121       };
122 
123       ail_make_miptree(&layout);
124 
125       for (unsigned l = 0; l < test.levels; ++l) {
126          EXPECT_EQ(ail_get_level_offset_B(&layout, l), test.offsets[l])
127             << test.width << "x" << test.height << " "
128             << util_format_short_name(test.format)
129             << " texture has wrong offset at level " << l << ", off by "
130             << test.offsets[l] - ail_get_level_offset_B(&layout, l);
131       }
132    }
133 }
134 
TEST(Generated,MSAA)135 TEST(Generated, MSAA)
136 {
137    for (unsigned i = 0; i < ARRAY_SIZE(msaatests); ++i) {
138       struct msaatest test = msaatests[i];
139 
140       struct ail_layout layout = {
141          .width_px = test.width,
142          .height_px = test.height,
143          .depth_px = test.depth,
144          .sample_count_sa = test.samples,
145          .levels = test.levels,
146          .tiling = test.is_compressed ? AIL_TILING_TWIDDLED_COMPRESSED
147                                       : AIL_TILING_TWIDDLED,
148          .format = test.format,
149       };
150 
151       ail_make_miptree(&layout);
152 
153       EXPECT_EQ(layout.size_B, test.size)
154          << test.width << "x" << test.height << "x" << test.depth << " "
155          << (int)test.levels << "L " << (int)test.samples << "S "
156          << util_format_short_name(test.format)
157          << (test.is_compressed ? " " : " un")
158          << "compressed texture has wrong allocation size, off by "
159          << ((int)layout.size_B - (int)test.size);
160 
161       if (test.is_compressed) {
162          EXPECT_EQ(layout.metadata_offset_B, test.meta_offset)
163             << test.width << "x" << test.height << "x" << test.depth << " "
164             << (int)test.levels << "L " << (int)test.samples << "S "
165             << util_format_short_name(test.format)
166             << "compressed texture has wrong metadata offset size, off by "
167             << ((int)layout.metadata_offset_B - (int)test.meta_offset);
168       }
169    }
170 }
171