1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
#include <forstio/test/suite.hpp>
#include "../c++/capacity.hpp"
namespace {
using namespace kel::lbm::swarm;
/**
* The packing arithmetic is small enough that the interesting thing to test is
* not that it computes, but that the ceilings it implies are the ones we think
* they are. These are compile time assertions on purpose: they are statements
* about which descriptors are usable, and the answer should not depend on
* running the test on a particular device.
*/
SAW_TEST("Packing / planes and idle slots per descriptor"){
// D1Q3, D2Q5, D2Q9, D3Q19, D3Q27.
static_assert(packing_math::planes(3u) == 1u, "D1Q3 needs one texture");
static_assert(packing_math::planes(5u) == 2u, "D2Q5 needs two textures");
static_assert(packing_math::planes(9u) == 3u, "D2Q9 needs three textures");
static_assert(packing_math::planes(19u) == 5u, "D3Q19 needs five textures");
static_assert(packing_math::planes(27u) == 7u, "D3Q27 needs seven textures");
static_assert(packing_math::idle_slots(3u) == 1u, "D1Q3 wastes one slot");
static_assert(packing_math::idle_slots(5u) == 3u, "D2Q5 wastes three slots");
static_assert(packing_math::idle_slots(9u) == 3u, "D2Q9 wastes three slots");
static_assert(packing_math::idle_slots(19u) == 1u, "D3Q19 wastes one slot");
static_assert(packing_math::idle_slots(27u) == 1u, "D3Q27 wastes one slot");
SAW_EXPECT(true, "static asserts carry this one");
}
SAW_TEST("Packing / slots are a partition of the texels"){
// planes*4 is exactly Q plus the waste, for every Q. Cheap, but it is the
// invariant every slot table depends on.
for(uint64_t q = 1u; q <= 64u; ++q){
const uint64_t total = packing_math::planes(q) * packing_math::slots_per_texel;
SAW_EXPECT(total == q + packing_math::idle_slots(q),
"planes*4 must be Q plus the idle slots");
SAW_EXPECT(packing_math::idle_slots(q) < packing_math::slots_per_texel,
"Waste below a full texel, or a plane was over allocated");
}
}
/**
* The limit that decides whether a descriptor is usable at all. WebGL2
* guarantees only four colour attachments, so the 3D descriptors cannot write
* their whole distribution in one pass on a conformant minimum spec device.
* Asserting it here means we learn it from a test rather than from a browser.
*/
SAW_TEST("Packing / single pass only reaches Q <= 16 at guaranteed limits"){
constexpr uint64_t mrt = webgl2_guaranteed::max_draw_buffers;
static_assert(packing_math::fits_single_pass(9u, mrt),
"D2Q9 must fit one pass on any conformant WebGL2");
static_assert(packing_math::fits_single_pass(5u, mrt), "D2Q5 must fit");
static_assert(packing_math::fits_single_pass(3u, mrt), "D1Q3 must fit");
static_assert(not packing_math::fits_single_pass(19u, mrt),
"D3Q19 must not claim a single pass at guaranteed limits");
static_assert(not packing_math::fits_single_pass(27u, mrt),
"D3Q27 must not claim a single pass at guaranteed limits");
static_assert(packing_math::passes(19u, mrt) == 2u, "D3Q19 needs two passes");
static_assert(packing_math::passes(27u, mrt) == 2u, "D3Q27 needs two passes");
// Sixteen is the boundary: four planes, four attachments.
static_assert(packing_math::fits_single_pass(16u, mrt), "Q=16 is the last that fits");
static_assert(not packing_math::fits_single_pass(17u, mrt), "Q=17 must not fit");
SAW_EXPECT(packing_math::passes(9u, 0u) == 0u,
"A device reporting no draw buffers must not divide by zero");
}
SAW_TEST("Packing / stuffing aux fields into idle slots"){
// D2Q9 has three idle slots, so a lone info flag rides along free...
static_assert(packing_math::aux_planes(9u, 1u, true) == 0u,
"One aux scalar must fit D2Q9's idle slots");
// ...but the four fields a PSM run wants do not.
static_assert(packing_math::aux_planes(9u, 4u, true) == 1u,
"Four aux scalars must not fit three idle slots");
// Unstuffed always pays for its own plane.
static_assert(packing_math::aux_planes(9u, 1u, false) == 1u,
"Unstuffed aux must take a plane of its own");
// D3Q19 and D3Q27 have exactly one idle slot, so info is free there too.
static_assert(packing_math::aux_planes(19u, 1u, true) == 0u, "D3Q19 stuffs info");
static_assert(packing_math::aux_planes(27u, 1u, true) == 0u, "D3Q27 stuffs info");
SAW_EXPECT(true, "static asserts carry this one");
}
SAW_TEST("Packing / bytes per cell"){
// D2Q9, double buffered, info stuffed: 2*3 textures * 16 bytes.
static_assert(packing_math::bytes_per_cell(9u, 2u, 1u, true) == 96u,
"D2Q9 stuffed must be 96 bytes per cell");
// Unstuffed adds a plane: 2*3 + 1 textures.
static_assert(packing_math::bytes_per_cell(9u, 2u, 1u, false) == 112u,
"D2Q9 unstuffed must be 112 bytes per cell");
static_assert(packing_math::bytes_per_cell(19u, 2u, 1u, true) == 160u,
"D3Q19 stuffed must be 160 bytes per cell");
static_assert(packing_math::bytes_per_cell(27u, 2u, 1u, true) == 224u,
"D3Q27 stuffed must be 224 bytes per cell");
SAW_EXPECT(true, "static asserts carry this one");
}
SAW_TEST("Capacity / integer square root is exact"){
static_assert(isqrt(0u) == 0u, "isqrt(0)");
static_assert(isqrt(1u) == 1u, "isqrt(1)");
static_assert(isqrt(2u) == 1u, "isqrt(2)");
static_assert(isqrt(3u) == 1u, "isqrt(3)");
static_assert(isqrt(4u) == 2u, "isqrt(4)");
// The property, not just the samples: isqrt(n)^2 <= n < (isqrt(n)+1)^2.
for(uint64_t n = 0u; n < 4096u; ++n){
const uint64_t r = isqrt(n);
SAW_EXPECT(r * r <= n, "isqrt must not overshoot");
SAW_EXPECT((r + 1u) * (r + 1u) > n, "isqrt must not undershoot");
}
// Exact squares near the top of the range, where a float sqrt would drift.
const uint64_t big = 3037000499u;
SAW_EXPECT(isqrt(big * big) == big, "isqrt must stay exact for large squares");
SAW_EXPECT(isqrt(big * big - 1u) == big - 1u, "isqrt must round down");
}
SAW_TEST("Capacity / the texture bound is the squared usable edge"){
// Ghost eats a ring off each side, so the edge is S - 2G.
static_assert(capacity_math::texture_bound_cells_2d(2048u, 0u) == 2048u * 2048u,
"No ghost means the full edge");
static_assert(capacity_math::texture_bound_cells_2d(2048u, 1u) == 2046u * 2046u,
"A one cell ring costs two off the edge");
// A device whose texture dimension cannot even hold the ring has no room.
static_assert(capacity_math::texture_bound_cells_2d(2u, 1u) == 0u,
"An edge consumed entirely by ghost leaves nothing");
SAW_EXPECT(true, "static asserts carry this one");
}
/**
* The headline number: how much lattice a worker can actually hold. The point
* of the test is the relationship between the two bounds, since on real
* hardware the texture bound is roughly a hundred times looser than memory and
* a capacity model that used it would be badly wrong.
*/
SAW_TEST("Capacity / memory binds before texture size on real devices"){
constexpr uint64_t bpc = packing_math::bytes_per_cell(9u, 2u, 1u, true);
static_assert(bpc == 96u, "D2Q9 stuffed");
// A typical desktop: 16384 max texture, 512MB of budget.
constexpr uint64_t desktop_tex = 16384u;
constexpr uint64_t budget = 512ull * 1024ull * 1024ull;
constexpr uint64_t by_texture = capacity_math::texture_bound_cells_2d(desktop_tex, 0u);
constexpr uint64_t by_memory = capacity_math::cells_from_bytes(budget, bpc);
static_assert(by_memory < by_texture,
"Memory has to be the binding constraint on a typical desktop");
static_assert(by_texture / by_memory > 40u,
"The texture bound should be looser by a wide margin, not a hair");
constexpr uint64_t cells = capacity_math::max_cells_2d(budget, bpc, desktop_tex, 0u);
static_assert(cells == by_memory, "max_cells must take the tighter bound");
SAW_EXPECT(cells * bpc <= budget, "The answer has to fit the budget it came from");
}
SAW_TEST("Capacity / minimum spec can be texture bound instead"){
constexpr uint64_t bpc = packing_math::bytes_per_cell(9u, 2u, 1u, true);
// Guaranteed limits with a generous budget: now the texture dimension is
// what runs out first, which is a different thing to report upstream.
constexpr uint64_t tex = webgl2_guaranteed::max_texture_size;
constexpr uint64_t budget = 4ull * 1024ull * 1024ull * 1024ull;
constexpr uint64_t by_texture = capacity_math::texture_bound_cells_2d(tex, 1u);
constexpr uint64_t by_memory = capacity_math::cells_from_bytes(budget, bpc);
static_assert(by_texture < by_memory, "Texture size must bind at minimum spec");
static_assert(capacity_math::max_cells_2d(budget, bpc, tex, 1u) == by_texture,
"max_cells must take the tighter bound here too");
SAW_EXPECT(true, "static asserts carry this one");
}
SAW_TEST("Capacity / a fitted domain always fits"){
constexpr uint64_t bpc = packing_math::bytes_per_cell(9u, 2u, 1u, true);
// Sweep budgets and aspect ratios; the invariant is that the answer never
// exceeds the cell count it was given, and never exceeds the texture edge.
const uint64_t aspects[][2] = {{1u,1u}, {4u,1u}, {1u,4u}, {16u,9u}, {2u,1u}};
const uint64_t tex = 16384u;
const uint64_t ghost = 1u;
const uint64_t edge = tex - 2u * ghost;
for(uint64_t mb = 1u; mb <= 2048u; mb *= 2u){
const uint64_t budget = mb * 1024ull * 1024ull;
const uint64_t cells = capacity_math::max_cells_2d(budget, bpc, tex, ghost);
for(const auto& a : aspects){
const auto dom = capacity_math::fit_domain_2d(cells, a[0], a[1], tex, ghost);
SAW_EXPECT(dom.cells() <= cells,
"A fitted domain must not exceed the measured capacity");
SAW_EXPECT(dom.nx <= edge and dom.ny <= edge,
"A fitted domain must not exceed the texture edge");
SAW_EXPECT(dom.nx > 0u and dom.ny > 0u,
"A non zero budget must yield a non empty domain");
SAW_EXPECT(dom.cells() * bpc <= budget,
"The domain must fit the byte budget it was derived from");
}
}
}
SAW_TEST("Capacity / fitting respects the requested aspect"){
// 4:1 on a square-ish budget should come out roughly four times as wide as
// it is tall. Exactness is not the claim -- flooring twice costs a little --
// but the shape has to be recognisably what was asked for.
const auto dom = capacity_math::fit_domain_2d(1024u * 256u, 4u, 1u, 16384u, 0u);
SAW_EXPECT(dom.nx > dom.ny, "A 4:1 request must be wider than tall");
const uint64_t ratio_x100 = (dom.nx * 100u) / dom.ny;
SAW_EXPECT(ratio_x100 >= 380u and ratio_x100 <= 420u,
"A 4:1 request must land near 4:1");
// Degenerate inputs are answered, not asserted on.
const auto zero = capacity_math::fit_domain_2d(0u, 4u, 1u, 16384u, 0u);
SAW_EXPECT(zero.cells() == 0u, "No capacity means no domain");
const auto bad = capacity_math::fit_domain_2d(1024u, 0u, 1u, 16384u, 0u);
SAW_EXPECT(bad.cells() == 0u, "A degenerate aspect must not divide by zero");
}
}
|