#include "../gpu/vk/dlss_actor.h" #include #include #include #define CHECK(name, condition) do { if (!(condition)) { std::fprintf(stderr, "FAIL %s at line %d\n", name, __LINE__); return 1; } } while (0) int main() { namespace da = dlss_actor; // names: the same draw keeps its name, another draw of the same model is the next one, another model another name const uint64_t a0 = da::key(0x1000, 0x2000, 9702, 0, 0xabc, 0), a1 = da::key(0x1000, 0x2000, 9702, 0, 0xabc, 1); CHECK("the same draw, the same name", a0 == da::key(0x1000, 0x2000, 9702, 0, 0xabc, 0)); CHECK("the second draw of a model is another", a0 != a1 && da::same_model(a0, a1)); CHECK("another model", !da::same_model(a0, da::key(0x1000, 0x2000, 9702, 3, 0xabc, 0)) && !da::same_model(a0, da::key(0x1040, 0x2000, 9702, 0, 0xabc, 0)) && !da::same_model(a0, da::key(0x1000, 0x2000, 9702, 0, 0xabd, 0))); CHECK("many draws of one model", da::same_model(a0, da::key(0x1000, 0x2000, 9702, 0, 0xabc, 900))); // where a draw's indices start for the shader, which reads 32-bit words: the address 4-aligned down, the first index in it uint64_t at = 0; uint32_t first = 0; da::index_start(0x10002, 1, &at, &first); CHECK("16-bit at +2: the second half of the word", at == 0x10000 && first == 1); da::index_start(0x10003, 0, &at, &first); CHECK("8-bit at +3: the fourth byte", at == 0x10000 && first == 3); da::index_start(0x10008, 2, &at, &first); CHECK("32-bit, aligned", at == 0x10008 && first == 0); da::index_start(0x10004, 1, &at, &first); CHECK("16-bit, aligned", at == 0x10004 && first == 0); // (a set's joint and weight count in Push::formats: Link's four, five or six bones a vertex, two, and rigid parts) const uint32_t five = da::count_code(4) << da::kCountShift | da::count_code(1) << (da::kCountShift + 2); CHECK("four, then one", da::set_count(five, 0) == 4 && da::set_count(five, 1) == 1); const uint32_t two = da::kByteJoints | da::count_code(2) << da::kCountShift; CHECK("two, 8-bit joints", da::set_count(two, 0) == 2 && (two & da::kByteJoints)); CHECK("the count bits clear of the others", !((da::count_code(2) << da::kCountShift | da::count_code(2) << (da::kCountShift + 2)) & (da::kHalfPosition | da::kByteJoints | da::kByteJoints << 1 | da::kRigid))); CHECK("no other counts", da::count_code(3) == 3 && da::set_count(3u << da::kCountShift, 0) == 0); // capture motion: an indexed draw's vertices are its least index to its greatest (the restart index none of them) uint32_t lo = 0, hi = 0; const uint16_t strip[] = {7, 3, 9, 0xffff, 4, 12}; CHECK("16-bit, restart on", da::index_range(strip, 6, 1, true, &lo, &hi) && lo == 3 && hi == 12); CHECK("16-bit, restart off: all ones is a vertex", da::index_range(strip, 6, 1, false, &lo, &hi) && lo == 3 && hi == 0xffff); const uint8_t bytes[] = {200, 5, 255, 17}; CHECK("8-bit", da::index_range(bytes, 4, 0, true, &lo, &hi) && lo == 5 && hi == 200); const uint32_t words[] = {100000, 99999, 0xffffffffu}; CHECK("32-bit", da::index_range(words, 3, 2, true, &lo, &hi) && lo == 99999 && hi == 100000); const uint16_t cuts[] = {0xffff, 0xffff}; CHECK("only restarts: none", !da::index_range(cuts, 2, 1, true, &lo, &hi) && !da::index_range(strip, 0, 1, true, &lo, &hi)); CHECK("no such index type", !da::index_range(strip, 6, 3, true, &lo, &hi)); // its slots: from base vertex + least index, as many as the indices span; a vertex array's from its first, count of them uint32_t n = 0; CHECK("indexed", da::capture_span(1, 6, 1000, 3, 12, 1u << 20, &first, &n) && first == 1003 && n == 10); CHECK("a vertex array", da::capture_span(da::kNoIndices, 36, 250, 0, 0, 1u << 20, &first, &n) && first == 250 && n == 36); CHECK("too many", !da::capture_span(2, 3, 0, 0, 1u << 20, 1u << 20, &first, &n)); CHECK("nothing", !da::capture_span(da::kNoIndices, 0, 0, 0, 0, 1u << 20, &first, &n) && !da::capture_span(1, 6, 0, 5, 4, 1u << 20, &first, &n)); CHECK("a negative base vertex", da::capture_span(1, 3, -4, 6, 9, 64, &first, &n) && first == 2 && n == 4); // a vertex's slot, as the stages take it: VertexIndex - first, unsigned, below the count uint32_t slot = 0; CHECK("the first and the last", da::capture_slot(1003, 1003, 10, &slot) && slot == 0 && da::capture_slot(1012, 1003, 10, &slot) && slot == 9); CHECK("past the last", !da::capture_slot(1013, 1003, 10, &slot)); CHECK("before the first wraps past the count", !da::capture_slot(1002, 1003, 10, &slot)); CHECK("index 9 with base vertex -4: the last slot", da::capture_slot(-4 + 9, (uint32_t)(-4 + 6), 4, &slot) && slot == 3); // the frame before's position without its jitter: the jitter is the projection's, x + jitter * w float p[4] = {0.5f + 0.01f * 2.0f, -0.25f - 0.02f * 2.0f, 0.7f, 2.0f}; const float jitter[2] = {0.01f, -0.02f}; da::unjitter_position(p, jitter); CHECK("unjittered", p[0] > 0.4999f && p[0] < 0.5001f && p[1] > -0.2501f && p[1] < -0.2499f && p[2] == 0.7f && p[3] == 2.0f); CHECK("the push constants", sizeof(da::CapturePush) == 48 && sizeof(da::CapturePush) <= sizeof(da::Push)); // a draw's own camera's jitter: the projection's third column, negated; none from what is not a perspective projection float proj[16] = {1.5f, 0, -0.0004f, 0, 0, 2.6f, 0.0007f, 0, 0, 0, -1.0002f, -0.2f, 0, 0, -1, 0}; float own[2] = {9, 9}; CHECK("a jittered projection", da::camera_jitter(proj, own) && own[0] == 0.0004f && own[1] == -0.0007f); proj[2] = proj[6] = 0.0f; CHECK("one without jitter", da::camera_jitter(proj, own) && own[0] == 0.0f && own[1] == 0.0f); proj[14] = 0.0f, proj[15] = 1.0f; CHECK("an orthographic one: not taken", !da::camera_jitter(proj, own)); proj[14] = -1.0f, proj[15] = 0.0f, proj[2] = 0.5f; CHECK("an off-centre one: not a jitter", !da::camera_jitter(proj, own)); CHECK("the cameras' buffer holds its words", da::kJitterBeforeWord + 2 <= da::kHeadWords && da::kToleranceWord < da::kJitterBeforeWord); // copies of a model pair with the frame before's copies by place, all at once, the nearest pairs first, within a reach using P = da::Placed; const auto same = [](const std::vector& a, std::vector b) { return a == b; }; // (the four Gossip Stones: 2.3 units apart in a row, still) const std::vector

stones = {{{39.4f, 102.8f, -11.9f}, 0}, {{39.4f, 102.8f, -9.6f}, 1}, {{39.4f, 102.8f, -7.3f}, 2}, {{39.4f, 102.8f, -5.0f}, 3}}; CHECK("still copies in the same order: themselves", same(da::pair_by_place(stones, stones, 1.0f), {0, 1, 2, 3})); const std::vector

turned = {stones[2], stones[0], stones[3], stones[1]}; CHECK("still copies drawn in another order: themselves", same(da::pair_by_place(turned, stones, 1.0f), {2, 0, 3, 1})); // (run C, 19.58 s: the first stone comes into view; the frame before had the other three only) const std::vector

three = {{{39.4f, 102.8f, -9.6f}, 0}, {{39.4f, 102.8f, -7.3f}, 1}, {{39.4f, 102.8f, -5.0f}, 2}}; CHECK("a copy new in view takes no neighbour's place", same(da::pair_by_place(stones, three, 1.0f), {-1, 0, 1, 2})); CHECK("... nor with a reach wider than the row", same(da::pair_by_place(stones, three, 100.0f), {-1, 0, 1, 2})); // (a stone drawn by the other model in the frame before: this model's only copy then is another stone) const std::vector

lone_now = {stones[3]}, lone_then = {stones[1]}; CHECK("a lone copy further than the reach: none", same(da::pair_by_place(lone_now, lone_then, 1.0f), {-1})); const std::vector

moved = {{{39.9f, 102.8f, -5.2f}, 0}}; CHECK("a lone copy that moved a little: it", same(da::pair_by_place(moved, lone_now, 1.0f), {0})); // (two of one model passing each other, drawn in the other order than the frame before) const std::vector

walkers_then = {{{0.1f, 0, 0}, 0}, {{0.4f, 0, 0}, 1}}, walkers_now = {{{0.5f, 0, 0}, 0}, {{0.0f, 0, 0}, 1}}; CHECK("two passing each other: each itself", same(da::pair_by_place(walkers_now, walkers_then, 1.0f), {1, 0})); // (two of one model 0.6 apart, both moving 0.35 and drawn in the same order: the first's new place is nearest the // second's old one, and nearest first alone would swap them. By draw order 0.35 each, against 0.25 and 0.95) const std::vector

pair_then = {{{0.0f, 0, 0}, 0}, {{0.6f, 0, 0}, 1}}, pair_now = {{{0.35f, 0, 0}, 0}, {{0.95f, 0, 0}, 1}}; CHECK("two moving fast for their spacing, in draw order: each itself", same(da::pair_by_place(pair_now, pair_then, 1.0f), {0, 1})); // (0.9 apart, both moving 0.5: nearest first pairs only one, the first's new place with the second's old one at 0.4, // and leaves the other 1.4 away, past the reach. Draw order pairs both at 0.5: more pairs win over a smaller total) const std::vector

wide_then = {{{0.0f, 0, 0}, 0}, {{0.9f, 0, 0}, 1}}, wide_now = {{{0.5f, 0, 0}, 0}, {{1.4f, 0, 0}, 1}}; CHECK("draw order pairing more copies wins over nearest first's smaller total", same(da::pair_by_place(wide_now, wide_then, 1.0f), {0, 1})); // (a stone changes model: the frame before's copies of this model were the first two stones, this frame's are the second // and third. Within the reach actor_motion keeps at the most (2), the second stone is itself and the third has none; with // a reach past the stones' 2.3, draw order pairs two to nearest first's one and moves both toward their neighbour) const std::vector

row_then = {stones[0], stones[1]}, row_now = {{{39.4f, 102.8f, -9.6f}, 0}, {{39.4f, 102.8f, -7.3f}, 1}}; CHECK("a row whose model changed, within a reach of 2: itself and none", same(da::pair_by_place(row_now, row_then, 2.0f), {1, -1})); CHECK("... and past the stones' spacing, each takes its neighbour (why the reach is capped)", same(da::pair_by_place(row_now, row_then, 3.3f), {0, 1})); // (the same geometry drawn twice for one object: both copies at one place, each keeps its own n-th) const std::vector

twice = {{{5, 5, 5}, 0}, {{5, 5, 5}, 1}}, twice_swapped = {{{5, 5, 5}, 1}, {{5, 5, 5}, 0}}; CHECK("a tie at one place: the same n-th", same(da::pair_by_place(twice, twice, 1.0f), {0, 1}) && same(da::pair_by_place(twice, twice_swapped, 1.0f), {1, 0})); const std::vector

once = {{{5, 5, 5}, 0}}; CHECK("a tie at one place, its own n-th gone: the other", same(da::pair_by_place(once, {twice[1]}, 1.0f), {0})); const std::vector

lost = {{{std::nanf(""), 0, 0}, 0}}; CHECK("a place that is not finite: none", same(da::pair_by_place(lost, once, 1.0f), {-1})); CHECK("none then, none now", da::pair_by_place(stones, {}, 1.0f) == std::vector(4, -1) && da::pair_by_place({}, stones, 1.0f).empty()); // a draw's place: its origin plus its bones' mean translation (copies at one origin, placed by their bones, differ) const auto pose_of = [](float ox, float oy, float oz, std::vector> moves) { std::vector p(16 + moves.size() * 48, 0); const float o[4] = {ox, oy, oz, 0.0f}; std::memcpy(p.data(), o, sizeof o); for (size_t b = 0; b < moves.size(); b++) { const float m[12] = {1, 0, 0, moves[b][0], 0, 1, 0, moves[b][1], 0, 0, 1, moves[b][2]}; std::memcpy(p.data() + 16 + b * 48, m, sizeof m); } return p; }; float place[3]; const auto by_origin = pose_of(100, 0, 5, {{0, 0, 0}, {0, 2, 0}}); da::place_of(by_origin.data(), by_origin.data() + 16, 2, place); CHECK("placed by its origin", place[0] == 100 && place[1] == 1 && place[2] == 5); const auto by_bones = pose_of(0, 0, 0, {{300, 0, 7}, {300, 4, 7}}); da::place_of(by_bones.data(), by_bones.data() + 16, 2, place); CHECK("placed by its bones, at a shared origin", place[0] == 300 && place[1] == 2 && place[2] == 7); auto bad = pose_of(10, 0, 0, {{2, 0, 0}, {0, 0, 0}}); const float nan = std::nanf(""); std::memcpy(bad.data() + 16 + 48 + 3 * 4, &nan, 4); da::place_of(bad.data(), bad.data() + 16, 2, place); CHECK("a bone that is not finite is left out", place[0] == 12 && place[1] == 0); da::place_of(by_origin.data(), by_origin.data() + 16, 0, place); CHECK("no bones: the origin", place[0] == 100 && place[1] == 0 && place[2] == 5); std::puts("dlss_actor: ok"); return 0; }