Files
Triforce-Source/tests/test_readback_rec_end.cpp
ruckus7382sfgsgdf 822ee913bc Triforce v0.9.0
Character tab, faster renderer defaults, HD rumble shipped, and the 11-30 bug pass: ending softlock below 60 fps, adult first-night lock, Intel Arc robust buffers, adult map, randomizer load order.
2026-11-30 20:55:14 -06:00

445 lines
23 KiB
C++

// tests/test_readback_rec_end.py: gpu/vk/wb_rec_wait.h's readback wait (Parts, readback_wait, rec_end_on) against a made-up GPU.
// A recording made in parts writes back at each part's end, under its one value; a readback waits for that value on the
// recordings' timeline. The old wait named the first part; the rule waits for the last.
#include "../gpu/vk/wb_rec_wait.h"
#include <cstdint>
#include <cstdio>
#include <random>
#include <vector>
#define CHECK(name, condition) do { if (!(condition)) { std::fprintf(stderr, "FAIL %s at line %d\n", name, __LINE__); return 1; } } while (0)
namespace wr = mkwvk::wb_rec_wait;
// One recording of value V made in parts while a readback waits: parts[i] is part i's number on the recordings' timeline, the last
// one ends the recording. `sent` parts are submitted when the read comes; the replay submits the next one each time the reader
// waits, flushes or slices, `step` parts at a time (0: the replay is stuck, the recording never ends). The GPU runs every part
// submitted, so a wait for a submitted part comes back with it done; a wait for one never submitted is a hang (counted).
struct Gpu {
uint64_t value = 100;
std::vector<uint64_t> parts;
size_t sent = 1;
int step = 1;
uint64_t rec_done = 0; // (the recordings' timeline)
bool timeline_at_v = false; // (the submissions' timeline at the value: only once the recording's last part is done)
int timeline_after = -1; // (slices until the submissions' timeline reaches the value with the recording stuck; -1 never)
int rec_wait_error_at = -1; // (the n-th recording wait gives an error)
int rec_waits = 0, flushes = 0, slices = 0, hangs = 0;
wr::Parts table;
uint64_t first_noted = 0; // (value_rec: the first part, emplace)
uint64_t range = 0; // ((11-28) the part whose write-back last covered the read's range, 0: none known)
int range_part = -1; // ((11-29) the part (its index) whose submit makes the range known: its write-back copied it)
void submit_up_to(size_t n) {
for (; sent < n && sent < parts.size(); sent++) note(sent);
}
void note(size_t i) {
table.submitted(value, parts[i], i + 1 == parts.size());
if (!first_noted) first_noted = parts[i];
if ((int)i == range_part) range = parts[i];
}
void begin() {
for (size_t i = 0; i < sent; i++) note(i);
}
void replay_goes_on() { submit_up_to(sent + (size_t)step); }
bool ended() const { return sent == parts.size(); }
// the calls readback_wait makes
uint64_t first_part() { return first_noted; }
wr::Parts::Entry look() { return table.of(value); }
uint64_t range_seq() { return range; }
int rec_wait(uint64_t seq) {
rec_waits++;
if (rec_wait_error_at >= 0 && rec_waits > rec_wait_error_at) return 2;
if (seq > parts[sent - 1]) { // (never submitted: the wait would never come back)
hangs++;
return 2;
}
if (seq > rec_done) rec_done = seq;
if (ended() && rec_done >= parts.back()) timeline_at_v = true;
replay_goes_on();
return 0;
}
void flush() {
flushes++;
replay_goes_on();
}
int slice() {
slices++;
if (timeline_after >= 0 && slices >= timeline_after) { // (the replay ended it and the GPU ran it meanwhile)
submit_up_to(parts.size());
rec_done = parts.back();
timeline_at_v = true;
}
if (timeline_at_v) return 0;
replay_goes_on();
return 1;
}
};
int main() {
// the switch: on unset or empty; set, it is its number (atoi, as the renderer's other switches: a word reads as 0, off)
CHECK("unset: on", wr::rec_end_on(nullptr));
CHECK("empty: on", wr::rec_end_on(""));
CHECK("0: off", !wr::rec_end_on("0"));
CHECK("00: off", !wr::rec_end_on("00"));
CHECK("1: on", wr::rec_end_on("1"));
CHECK("2: on", wr::rec_end_on("2"));
CHECK("a word: off", !wr::rec_end_on("off"));
// ((Triforce, 11-28) the modes: unset, the CPU-written ranges; 0 off; 1 every readback (0.8.5); another number, 2
CHECK("mode unset: 2", wr::rec_end_mode(nullptr) == wr::kRecEndCpuWritten);
CHECK("mode empty: 2", wr::rec_end_mode("") == wr::kRecEndCpuWritten);
CHECK("mode 0: off", wr::rec_end_mode("0") == wr::kRecEndOff);
CHECK("mode a word: off", wr::rec_end_mode("off") == wr::kRecEndOff);
CHECK("mode 1: every readback", wr::rec_end_mode("1") == wr::kRecEndAll);
CHECK("mode 2", wr::rec_end_mode("2") == wr::kRecEndCpuWritten);
CHECK("mode 7: 2", wr::rec_end_mode("7") == wr::kRecEndCpuWritten);
CHECK("mode 2, the CPU writes it: the rule", wr::rec_end_applies(wr::kRecEndCpuWritten, true));
CHECK("mode 2, the CPU only reads it: the first part", !wr::rec_end_applies(wr::kRecEndCpuWritten, false));
CHECK("mode 1: the rule either way", wr::rec_end_applies(wr::kRecEndAll, false) && wr::rec_end_applies(wr::kRecEndAll, true));
CHECK("mode 0: never", !wr::rec_end_applies(wr::kRecEndOff, true));
// ((11-29) mode 3: mode 2's rule for the CPU-written ranges, and the read-only ranges at their part (range_applies))
CHECK("mode 3", wr::rec_end_mode("3") == wr::kRecEndRange && wr::rec_end_on("3"));
// ((11-29, the owner's decision) the reader's default where the parts end at the game's render passes: unset or empty alone
CHECK("unset with the pass-end parts: 3", wr::rec_end_mode(nullptr, wr::kRecEndRange) == wr::kRecEndRange
&& wr::rec_end_mode("", wr::kRecEndRange) == wr::kRecEndRange);
CHECK("set with the pass-end parts: as set", wr::rec_end_mode("2", wr::kRecEndRange) == wr::kRecEndCpuWritten
&& wr::rec_end_mode("0", wr::kRecEndRange) == wr::kRecEndOff
&& wr::rec_end_mode("1", wr::kRecEndRange) == wr::kRecEndAll
&& wr::rec_end_mode("7", wr::kRecEndRange) == wr::kRecEndCpuWritten);
CHECK("mode 3, the CPU writes it: the rule", wr::rec_end_applies(wr::kRecEndRange, true) && !wr::range_applies(wr::kRecEndRange, true));
CHECK("mode 3, the CPU only reads it: its range's part",
!wr::rec_end_applies(wr::kRecEndRange, false) && wr::range_applies(wr::kRecEndRange, false));
CHECK("the range's part in mode 3 alone", !wr::range_applies(wr::kRecEndCpuWritten, false) && !wr::range_applies(wr::kRecEndAll, false)
&& !wr::range_applies(wr::kRecEndOff, false));
// the parts' table: the newest part and whether it ended the recording; an older note changes nothing; a later recording
// under the same value takes the entry over from its first part; nothing for no value or no number; the oldest past the cap
wr::Parts t;
CHECK("not known", t.of(7).seq == 0 && !t.of(7).ended);
t.submitted(7, 10, false);
CHECK("a part", t.of(7).seq == 10 && !t.of(7).ended);
t.submitted(7, 11, false), t.submitted(7, 12, true);
CHECK("its end", t.of(7).seq == 12 && t.of(7).ended);
t.submitted(7, 11, false);
CHECK("an older note after a newer one: nothing", t.of(7).seq == 12 && t.of(7).ended);
t.submitted(7, 20, false);
CHECK("a later recording under the value: from its first part, going on", t.of(7).seq == 20 && !t.of(7).ended);
t.submitted(0, 30, true), t.submitted(8, 0, true);
CHECK("no value or no number: nothing noted", t.by_value.size() == 1);
t.cap = 3;
for (uint64_t v = 40; v < 44; v++) t.submitted(v, 100 + v, true);
CHECK("cap held", t.by_value.size() == 3 && t.of(7).seq == 0 && t.of(40).seq == 0 && t.of(43).seq == 143);
// the meter's case: a recording in three parts (10, 11, 12), the read after the first one's submit. The old wait stops at
// the first part, with the last two (and the write-back they make) still to come: the read got the CPU's 0
{
Gpu g;
g.parts = {10, 11, 12};
g.begin();
const wr::ReadbackWait r = wr::readback_wait(false, g);
CHECK("old: known", r.known && r.ok);
CHECK("old: ends at the first part, before the last", g.rec_done == 10 && g.rec_done < g.parts.back());
}
{
Gpu g;
g.parts = {10, 11, 12};
g.begin();
const wr::ReadbackWait r = wr::readback_wait(true, g);
CHECK("rule: known and done", r.known && r.ok);
CHECK("rule: its recording went on past the part first found", r.went_on);
CHECK("rule: ends with the last part done", g.rec_done == 12);
CHECK("rule: no wait for a part never submitted", g.hangs == 0);
}
// an ended recording: its last part, one wait, as the old wait's one
{
Gpu g;
g.parts = {10, 11, 12};
g.sent = 3;
g.begin();
const wr::ReadbackWait r = wr::readback_wait(true, g);
CHECK("ended: one wait for the last part", r.known && r.ok && !r.went_on && g.rec_waits == 1 && g.rec_done == 12 && g.slices == 0);
}
// a recording in one part: the same as the old wait
{
Gpu g;
g.parts = {10};
g.begin();
const wr::ReadbackWait r = wr::readback_wait(true, g);
CHECK("one part: one wait", r.known && r.ok && !r.went_on && g.rec_waits == 1 && g.rec_done == 10);
}
// not known at all (not recorded yet): the caller's fallback, nothing waited
{
Gpu g;
g.parts = {10, 11};
const wr::ReadbackWait r = wr::readback_wait(true, g);
const wr::ReadbackWait r0 = wr::readback_wait(false, g);
CHECK("not known: the fallback's", !r.known && !r0.known && g.rec_waits == 0 && g.slices == 0);
}
// the replay stuck with the recording going on: slices of the submissions' timeline, ended once it reaches the value
{
Gpu g;
g.parts = {10, 11, 12};
g.step = 0;
g.timeline_after = 5;
g.begin();
const wr::ReadbackWait r = wr::readback_wait(true, g);
CHECK("stuck: ended by the timeline", r.known && r.ok && r.went_on && r.slices == 5 && g.timeline_at_v);
}
// an error (a lost device) ends the wait, not done
{
Gpu g;
g.parts = {10, 11, 12};
g.rec_wait_error_at = 1;
g.begin();
const wr::ReadbackWait r = wr::readback_wait(true, g);
CHECK("error: not done", r.known && !r.ok && g.rec_waits == 2);
}
// ((11-28) Start next frame early: the tail began after part 11 (the scene's last), part 12 the tail. A tail that passed its
// verdict wrote nothing the game reads: the read stops at the scene's part, never waits for the tail. The table's notes)
{
wr::Parts t2;
t2.submitted(9, 10, false), t2.submitted(9, 11, false);
t2.tail_began(9);
CHECK("tail began: the scene's part noted, not passed yet", t2.of(9).scene_part == 11 && t2.of(9).scene_seq == 0);
t2.submitted(9, 12, true);
t2.tail_passed(9);
CHECK("tail passed: the scene's part is the readbacks' end", t2.of(9).scene_seq == 11 && t2.of(9).seq == 12);
t2.tail_passed(5);
CHECK("a pass for an unknown value: nothing", t2.of(5).seq == 0);
wr::Parts t3;
t3.submitted(4, 30, false), t3.tail_passed(4);
CHECK("passed with no tail begun: nothing (the last part as before)", t3.of(4).scene_seq == 0);
}
{ // the tail passed before the read: one wait, for the scene's part; the tail (12) is not waited for
Gpu g;
g.parts = {10, 11, 12};
g.sent = 3;
g.begin();
g.table.tail_began(g.value); // (noted after part 11's submit in the replay: here the entry's newest is 12, so set it)
g.table.by_value[g.value].scene_part = 11;
g.table.tail_passed(g.value);
g.rec_done = 0;
const wr::ReadbackWait r = wr::readback_wait(true, g);
CHECK("tail passed: known and done", r.known && r.ok);
CHECK("tail passed: one wait, for the scene's part", g.rec_waits == 1 && g.rec_done == 11 && !r.went_on);
}
{ // the read during the tail, before its verdict: waits for the scene's part, then stops once the tail is judged and passed
Gpu g;
g.parts = {10, 11, 12};
g.sent = 2;
g.step = 0; // (the tail's part not submitted while the reader waits)
g.begin();
g.table.tail_began(g.value); // (after part 11, the newest submitted)
struct Judged : Gpu {
int left = 2;
void flush() {
Gpu::flush();
if (--left == 0) table.tail_passed(value); // (the verdict comes while the reader slices)
}
} j;
static_cast<Gpu&>(j) = g;
const wr::ReadbackWait r = wr::readback_wait(true, j);
CHECK("judged during the wait: known and done", r.known && r.ok);
CHECK("judged during the wait: ends at the scene's part, the tail never waited for", j.rec_done == 11 && j.hangs == 0);
}
{ // a tail that failed its verdict (no pass noted): the last part, as the rule was
Gpu g;
g.parts = {10, 11, 12};
g.sent = 2;
g.begin();
g.table.tail_began(g.value);
const wr::ReadbackWait r = wr::readback_wait(true, g);
CHECK("tail failed: the last part", r.known && r.ok && g.rec_done == 12);
}
// ((11-28) the tail begun, its verdict not in yet (the worker threads' reads at the owner's save): the read's range was copied
// back by a scene part (10): one wait, for that part; the tail (12) never waited for, never submitted while the read waits)
{
Gpu g;
g.parts = {10, 11, 12};
g.sent = 2;
g.step = 0;
g.begin();
g.table.tail_began(g.value); // (after part 11)
g.range = 10;
const wr::ReadbackWait r = wr::readback_wait(true, g);
CHECK("range in a scene part: known and done", r.known && r.ok && !r.went_on);
CHECK("range in a scene part: one wait, for its part", g.rec_waits == 1 && g.rec_done == 10 && g.hangs == 0 && g.slices == 0);
}
{ // the same read with no scene part known to have covered its range: the tail's end, as before (here the timeline ends it)
Gpu g;
g.parts = {10, 11, 12};
g.sent = 2;
g.step = 1;
g.begin();
g.table.tail_began(g.value);
const wr::ReadbackWait r = wr::readback_wait(true, g);
CHECK("range not in a scene part: the last part", r.known && r.ok && g.rec_done == 12);
}
{ // a range the tail itself copied back (its part after the scene's): the last part
Gpu g;
g.parts = {10, 11, 12};
g.sent = 2;
g.step = 1;
g.begin();
g.table.tail_began(g.value);
g.range = 12;
const wr::ReadbackWait r = wr::readback_wait(true, g);
CHECK("range in the tail: the last part", r.known && r.ok && g.rec_done == 12);
}
{ // the read during the scene (the tail not begun): as the rule was, the last part; then, once the tail begins with its
// range a scene part's, it stops at that part
Gpu g;
g.parts = {10, 11, 12};
g.sent = 1;
g.step = 1;
g.begin();
g.range = 10;
const wr::ReadbackWait r = wr::readback_wait(true, g);
CHECK("no tail: the last part", r.known && r.ok && g.rec_done == 12);
}
// 20,000 random recordings: 1 to 8 parts, the read after 1 to all of them are submitted, the replay going on 0 to 3 parts at
// each call (0: stuck, the timeline reaching the value after some slices). The rule never ends before the last part is done
// (or the timeline at the value), never waits for a part not submitted, and never takes more waits than parts plus one; the
// old wait ends at the first part whenever the read came before the recording ended
std::mt19937_64 rng(20261127);
int old_early = 0, cases_going_on = 0;
for (int k = 0; k < 20000; k++) {
Gpu g, g0;
const size_t n = 1 + rng() % 8;
uint64_t s = 1000 + (uint64_t)k * 16;
for (size_t i = 0; i < n; i++) g.parts.push_back(s++);
g.sent = 1 + rng() % n;
g.step = (int)(rng() % 4);
if (g.step == 0) g.timeline_after = 1 + (int)(rng() % 20);
g0 = g;
g.begin(), g0.begin();
const bool going_on = g.sent < n;
cases_going_on += going_on;
const wr::ReadbackWait r = wr::readback_wait(true, g);
CHECK("random: known and done", r.known && r.ok);
CHECK("random: never before the last part is done", g.rec_done == g.parts.back() || g.timeline_at_v);
CHECK("random: never a wait for a part not submitted", g.hangs == 0);
CHECK("random: went on exactly when the read came before the end", r.went_on == going_on);
CHECK("random: waits bounded", g.rec_waits <= (int)n + 1);
const wr::ReadbackWait r0 = wr::readback_wait(false, g0);
CHECK("random old: known", r0.known && r0.ok && g0.rec_waits == 1);
if (going_on && g0.rec_done < g0.parts.back()) old_early++;
}
CHECK("random: the reads before a recording's end were many", cases_going_on > 10000);
CHECK("random: the old wait ended early at every one of them", old_early == cases_going_on);
std::printf("readback rec end: %d of 20000 reads came before their recording's end; the old wait ended early at all %d\n",
cases_going_on, old_early);
// ((11-29) RX_VK_RB_REC_END=3, readback_range_wait: a range the CPU only reads waits for the part that copied it back. The
// flicker's case: parts end at the game's render passes, the first after ~32 draws; the read comes after the first part's
// submit, its range copied back by the second. The first part's wait read it a frame late (the exposure moved for 15 presents)
{
Gpu g, g0;
g.parts = {10, 11, 12};
g.range_part = 1;
g0 = g;
g.begin(), g0.begin();
const wr::ReadbackWait r = wr::readback_range_wait(g);
CHECK("range: known and done", r.known && r.ok);
CHECK("range: ends at the part that copied it back, not the first, not the last", g.rec_done == 11 && g.hangs == 0);
const wr::ReadbackWait r0 = wr::readback_wait(false, g0);
CHECK("range: the first part's wait ended before it (the frame-late read)", r0.known && g0.rec_done == 10);
}
{ // the range's part submitted already: one wait, for it, nothing sliced
Gpu g;
g.parts = {10, 11, 12};
g.sent = 2;
g.range_part = 0;
g.begin();
const wr::ReadbackWait r = wr::readback_range_wait(g);
CHECK("range known: one wait, for its part", r.known && r.ok && !r.went_on && g.rec_waits == 1 && g.rec_done == 10 && g.slices == 0);
}
{ // no part copies the range back: the recording's end, as the last-part rule
Gpu g;
g.parts = {10, 11, 12};
g.begin();
const wr::ReadbackWait r = wr::readback_range_wait(g);
CHECK("range never copied: the last part", r.known && r.ok && r.went_on && g.rec_done == 12 && g.hangs == 0);
}
{ // an ended recording whose range no part noted (its runs dropped once done): one wait, for the last part
Gpu g;
g.parts = {10, 11, 12};
g.sent = 3;
g.begin();
const wr::ReadbackWait r = wr::readback_range_wait(g);
CHECK("ended, range not noted: one wait", r.known && r.ok && !r.went_on && g.rec_waits == 1 && g.rec_done == 12);
}
{ // the parts not known, the first part is (value_rec): that, as before
Gpu g;
g.parts = {10, 11};
g.first_noted = 10;
const wr::ReadbackWait r = wr::readback_range_wait(g);
CHECK("parts not known: the first part", r.known && r.ok && g.rec_waits == 1 && g.rec_done == 10);
Gpu n;
n.parts = {10, 11};
const wr::ReadbackWait rn = wr::readback_range_wait(n);
CHECK("not known at all: the fallback's", !rn.known && n.rec_waits == 0);
}
{ // the replay stuck before the range's part: slices of the submissions' timeline, ended once it reaches the value
Gpu g;
g.parts = {10, 11, 12};
g.range_part = 2;
g.step = 0;
g.timeline_after = 4;
g.begin();
const wr::ReadbackWait r = wr::readback_range_wait(g);
CHECK("range, stuck: ended by the timeline", r.known && r.ok && r.went_on && r.slices == 4 && g.timeline_at_v && g.hangs == 0);
}
{ // an error ends it, not done
Gpu g;
g.parts = {10, 11, 12};
g.range_part = 1;
g.rec_wait_error_at = 0;
g.begin();
const wr::ReadbackWait r = wr::readback_range_wait(g);
CHECK("range, error: not done", r.known && !r.ok && g.rec_waits == 1);
}
// 20,000 random recordings, the range copied back by one of the parts or by none: never back before the range's part is done
// (none: the last part), never past it but for the timeline, never a wait for a part not submitted, one wait when its part was
// submitted before the read; the first part's wait read it early whenever a later part copied it
int range_later = 0, first_early = 0;
for (int k = 0; k < 20000; k++) {
Gpu g, g0;
const size_t n = 1 + rng() % 8;
uint64_t s = 5000000 + (uint64_t)k * 16;
for (size_t i = 0; i < n; i++) g.parts.push_back(s++);
g.sent = 1 + rng() % n;
g.step = (int)(rng() % 4);
if (g.step == 0) g.timeline_after = 1 + (int)(rng() % 20);
g.range_part = (int)(rng() % (n + 1)) - 1;
g0 = g;
g.begin(), g0.begin();
const size_t sent0 = g.sent;
const wr::ReadbackWait r = wr::readback_range_wait(g);
CHECK("range random: known and done", r.known && r.ok);
CHECK("range random: never a wait for a part not submitted", g.hangs == 0);
CHECK("range random: waits bounded", g.rec_waits <= (int)n + 1);
const uint64_t want = g.range_part < 0 ? g.parts.back() : g.parts[(size_t)g.range_part];
CHECK("range random: never before the range's part is done", g.rec_done >= want || g.timeline_at_v);
CHECK("range random: never past it but for the timeline", g.rec_done == want || g.timeline_at_v);
if (g.range_part >= 0 && (size_t)g.range_part < sent0)
CHECK("range random: its part submitted before the read: one wait", g.rec_waits == 1 && !r.went_on && g.slices == 0);
const wr::ReadbackWait r0 = wr::readback_wait(false, g0);
CHECK("range random old: known", r0.known && r0.ok);
if (g.range_part > 0) {
range_later++;
if (g0.rec_done < want) first_early++;
}
}
CHECK("range random: ranges a later part copied were many", range_later > 5000);
CHECK("range random: the first part's wait read every one of them early", first_early == range_later);
std::printf("readback range wait: %d of 20000 ranges were copied back after the first part; the first part's wait read all %d early\n",
range_later, first_early);
return 0;
}