mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-19 16:20:01 -04:00
Triforce v0.3.3
This commit is contained in:
@@ -26,6 +26,8 @@ static int prepare_calls, write_calls, accepted, unprepare_calls, open_calls, cl
|
||||
static unsigned last_device, last_channels;
|
||||
static uint64_t now_ms;
|
||||
static int fail_prepare, fail_write, fail_unprepare, fail_open, fail_open6;
|
||||
static int gone_write_at, gone_unprepare, opens_fail; /* (the device gone: "no driver" 6, "invalid handle" 5) */
|
||||
static uint32_t default_key = 11; /* (Windows' default output, as au_default_key says) */
|
||||
static int queued[10];
|
||||
static WAVEHDR *known[10];
|
||||
static char last_log[512];
|
||||
@@ -52,6 +54,7 @@ static MMRESULT test_open(HWAVEOUT *out, unsigned device, const WAVEFORMATEX *f,
|
||||
CHECK(f->nBlockAlign == 2 * f->nChannels && f->nAvgBytesPerSec == 48000 * f->nBlockAlign);
|
||||
last_channels = f->nChannels;
|
||||
open_calls++;
|
||||
if (opens_fail) return 6; /* (MMSYSERR_NODRIVER: the audio service down) */
|
||||
if (fail_open || (fail_open6 && f->nChannels == 6)) return 7; /* (MMSYSERR_NOMEM; a device without 5.1: 32) */
|
||||
*out = (void *)(uintptr_t)1;
|
||||
return 0;
|
||||
@@ -69,6 +72,7 @@ static MMRESULT test_write(HWAVEOUT out, WAVEHDR *h, unsigned size) {
|
||||
const int i = slot(h);
|
||||
CHECK(!queued[i]);
|
||||
write_calls++;
|
||||
if (gone_write_at && write_calls == gone_write_at) return 6; /* (MMSYSERR_NODRIVER) */
|
||||
if (!(h->dwFlags & WHDR_PREPARED)) return 34; /* WAVERR_UNPREPARED: no completion follows. */
|
||||
if (fail_write && write_calls == 1) return 7; /* MMSYSERR_NOMEM: no buffer was queued. */
|
||||
queued[i] = 1;
|
||||
@@ -80,6 +84,7 @@ static MMRESULT test_unprepare(HWAVEOUT out, WAVEHDR *h, unsigned size) {
|
||||
(void)out; (void)size;
|
||||
CHECK(!queued[slot(h)]);
|
||||
unprepare_calls++;
|
||||
if (gone_unprepare) return 5; /* (MMSYSERR_INVALHANDLE, every call until the handle is closed) */
|
||||
if (fail_unprepare && unprepare_calls == 1) return 33; /* WAVERR_STILLPLAYING: do not reuse. */
|
||||
h->dwFlags &= ~WHDR_PREPARED;
|
||||
return 0;
|
||||
@@ -93,11 +98,12 @@ static MMRESULT test_reset(HWAVEOUT out) {
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
static MMRESULT test_close(HWAVEOUT out) { (void)out; close_calls++; return 0; }
|
||||
static MMRESULT test_close(HWAVEOUT out) { (void)out; close_calls++; gone_unprepare = 0; return 0; }
|
||||
/* (the platform helpers nn_audio.c defines before its output section) */
|
||||
static unsigned au_device_count(void) { return 2; }
|
||||
static void au_device_name(unsigned device, char *out, size_t size) { snprintf(out, size, "test device %u", device); }
|
||||
static uint64_t au_ms(void) { return now_ms; }
|
||||
static uint32_t au_default_key(void) { return default_key; }
|
||||
static void au_opened(HWAVEOUT w) { (void)w; }
|
||||
static int speakers = 2; /* (the game's speakers: 6 in the "surround" and "fallback" cases) */
|
||||
static int au_speakers(void) { return speakers; }
|
||||
@@ -158,6 +164,52 @@ int main(int argc, char **argv) {
|
||||
CHECK(accepted == 20 && prepare_calls == 20 && write_calls == 20 && !logs);
|
||||
CHECK(blocks[9][0] == 38 * 31 && blocks[9][FRAME * 6] == 39 * 31);
|
||||
CHECK(blocks[9][FRAME * 6 - 1] == (int16_t)(38 * 31 + FRAME * 6 - 1));
|
||||
} else if (!strcmp(scenario, "lost")) {
|
||||
gone_write_at = 25; /* a Bluetooth headset off: its 25th block, "no driver" */
|
||||
frames(60, 1);
|
||||
CHECK(reset_calls == 1 && close_calls == 1 && open_calls == 2 && last_device == WAVE_MAPPER);
|
||||
CHECK(accepted >= 28); /* the default took the blocks after it */
|
||||
} else if (!strcmp(scenario, "gone")) {
|
||||
frames(20, 1);
|
||||
gone_unprepare = 1; /* the audio service restarted: every call on the old handle "invalid" */
|
||||
frames(2, 1);
|
||||
CHECK(close_calls == 1 && open_calls == 2 && last_device == WAVE_MAPPER);
|
||||
const int before = accepted;
|
||||
frames(10, 1);
|
||||
CHECK(accepted == before + 5); /* (before, the same block tried again for good: no sound for the run) */
|
||||
} else if (!strcmp(scenario, "service")) {
|
||||
frames(10, 1);
|
||||
opens_fail = 1; /* the service down: no device opens */
|
||||
gone_write_at = write_calls + 1;
|
||||
frames(2, 1);
|
||||
CHECK(close_calls == 1 && !wout && open_calls == 4); /* (the first open, then the default and both devices) */
|
||||
frames(5, 1);
|
||||
CHECK(open_calls == 4); /* not again within the second */
|
||||
now_ms += 1000;
|
||||
frames(1, 1);
|
||||
CHECK(open_calls == 7 && !wout); /* a second later: all three again, the service still down */
|
||||
opens_fail = 0;
|
||||
now_ms += 1000;
|
||||
frames(1, 1);
|
||||
CHECK(wout && open_calls == 8 && last_device == WAVE_MAPPER); /* the service back: the default first */
|
||||
const int before = accepted;
|
||||
frames(4, 1);
|
||||
CHECK(accepted == before + 2);
|
||||
} else if (!strcmp(scenario, "default")) {
|
||||
frames(10, 1);
|
||||
default_key = 12; /* Windows moves its default (the headset back) */
|
||||
now_ms += 500;
|
||||
frames(1, 1);
|
||||
CHECK(open_calls == 1); /* looked at once a second */
|
||||
now_ms += 600;
|
||||
frames(1, 1);
|
||||
CHECK(close_calls == 1 && open_calls == 2 && last_device == WAVE_MAPPER && out_default_key == 12);
|
||||
now_ms += 2000;
|
||||
frames(2, 1);
|
||||
CHECK(open_calls == 2); /* the same default: not opened again */
|
||||
const int before = accepted;
|
||||
frames(4, 1);
|
||||
CHECK(accepted == before + 2);
|
||||
} else if (!strcmp(scenario, "stall")) {
|
||||
frames(20, 0); /* every block queued, none ever completes: the device takes nothing */
|
||||
now_ms += 1000;
|
||||
|
||||
92
tests/recorder_exit_test.cpp
Normal file
92
tests/recorder_exit_test.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
// RX_VK_RECORDER=thread at process exit, on Windows (tests/test_recorder_sync.py builds and runs it; skipped elsewhere).
|
||||
//
|
||||
// Built twice: with RX_EXIT_DLL, a DLL holding the real vk_work.h / vk_recorder.h (as hookshot_vk.dll does) and a fake vkCmdDraw;
|
||||
// without, an exe that loads it, has the recorder parked in idle() holding sleep_mu (the test hook RX_REC_TEST_HOOKS), and calls
|
||||
// ExitProcess, as the window's close (host/display.c) and svcExitProcess do. Windows ends the recorder thread first, then runs the
|
||||
// DLL's global destructors. A destructor that takes sleep_mu, now owned by a dead thread, either hangs (MinGW-w64 GCC and
|
||||
// clang with libstdc++; measured on Windows 11) or has the process ended on the spot, the DLL's other destructors skipped
|
||||
// (llvm-mingw's libc++, clang for the MSVC target). So: the exe must end with code 0 within the watchdog's 20 s (it ends only
|
||||
// this child), and the sentinel below, destroyed after the recorder's global, must print its line.
|
||||
#ifdef RX_EXIT_DLL
|
||||
#define RX_REC_TEST_HOOKS 1
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
extern "C" __declspec(dllimport) void* __stdcall GetStdHandle(unsigned long);
|
||||
extern "C" __declspec(dllimport) int __stdcall WriteFile(void*, const void*, unsigned long, unsigned long*, void*);
|
||||
/// (made before the recorder's global, so destroyed after it: its line shows the DLL's destructors all ran. The recorder's
|
||||
/// global is an inline variable, whose dynamic initialization is only partially ordered against this file's statics: with
|
||||
/// llvm-mingw it was made first, so this line printed before the old destructor ended the process and the broken variant
|
||||
/// passed. init_priority makes this object first on every compiler that builds the test)
|
||||
struct Sentinel {
|
||||
~Sentinel() {
|
||||
static const char line[] = "DLL detach: the global destructors ran to the end\n";
|
||||
unsigned long n;
|
||||
WriteFile(GetStdHandle((unsigned long)-11), line, sizeof line - 1, &n, nullptr);
|
||||
}
|
||||
};
|
||||
static Sentinel g_sentinel __attribute__((init_priority(101)));
|
||||
|
||||
#include "vk_work.h"
|
||||
|
||||
namespace mkwvk {
|
||||
std::atomic<uint64_t> work_epoch{0};
|
||||
std::atomic<bool> par_active{false};
|
||||
void logf(const char* fmt, ...) {
|
||||
va_list a;
|
||||
va_start(a, fmt);
|
||||
std::vfprintf(stdout, fmt, a);
|
||||
va_end(a);
|
||||
std::fputc('\n', stdout);
|
||||
}
|
||||
} // namespace mkwvk
|
||||
|
||||
static std::atomic<int> g_draws{0};
|
||||
static std::atomic<bool> g_parked{false};
|
||||
static void VKAPI_CALL fake_vkCmdDraw(VkCommandBuffer, uint32_t, uint32_t, uint32_t, uint32_t) { g_draws++; }
|
||||
namespace vkx {
|
||||
PFN_vkCmdDraw vkCmdDraw = fake_vkCmdDraw;
|
||||
} // namespace vkx
|
||||
|
||||
/// The recorder started by a packet, run, then parked for good inside idle() with sleep_mu held; 1 once it is
|
||||
extern "C" __declspec(dllexport) int park_recorder() {
|
||||
using namespace mkwvk;
|
||||
rec::g_stats_log = false;
|
||||
rec::g_test_idle_locked = [] {
|
||||
g_parked = true;
|
||||
for (;;) std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
};
|
||||
rec::set_mode(rec::THREAD);
|
||||
static rec::DrainingMutex mu;
|
||||
{
|
||||
std::lock_guard lk(mu);
|
||||
vkCmdDraw((VkCommandBuffer)(uintptr_t)0x100, 3, 1, 0, 0);
|
||||
} // (the unlock: the draw has run)
|
||||
for (int i = 0; i < 5000 && !g_parked; i++) std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
return g_parked && g_draws == 1 && rec::g_rec.th.joinable();
|
||||
}
|
||||
#else
|
||||
#include <cstdio>
|
||||
extern "C" {
|
||||
__declspec(dllimport) void* __stdcall LoadLibraryA(const char*);
|
||||
__declspec(dllimport) void* __stdcall GetProcAddress(void*, const char*);
|
||||
__declspec(dllimport) void __stdcall ExitProcess(unsigned int);
|
||||
}
|
||||
int main(int argc, char** argv) {
|
||||
if (argc < 2) return 2;
|
||||
void* m = LoadLibraryA(argv[1]);
|
||||
auto park = m ? reinterpret_cast<int (*)()>(GetProcAddress(m, "park_recorder")) : nullptr;
|
||||
if (!park) {
|
||||
std::printf("no fixture DLL\n");
|
||||
return 2;
|
||||
}
|
||||
const int ok = park();
|
||||
std::printf("recorder parked in idle() holding sleep_mu: %d; ExitProcess\n", ok);
|
||||
std::fflush(stdout);
|
||||
ExitProcess(ok ? 0 : 2);
|
||||
}
|
||||
#endif
|
||||
1760
tests/recorder_sync_test.cpp
Normal file
1760
tests/recorder_sync_test.cpp
Normal file
File diff suppressed because it is too large
Load Diff
@@ -11,7 +11,8 @@ import sys
|
||||
import tempfile
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
CASES = ("normal", "busy", "prepare", "write", "unprepare", "open", "bounded", "stall", "surround", "fallback")
|
||||
CASES = ("normal", "busy", "prepare", "write", "unprepare", "open", "bounded", "stall", "surround", "fallback",
|
||||
"lost", "gone", "service", "default")
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
@@ -39,9 +39,13 @@ def main():
|
||||
if "capture_nt() ? copy_nt : std::memcpy" not in source:
|
||||
raise SystemExit("the guarded copy does not use copy_nt")
|
||||
part = function(source, "void copy_part(const CopyPart& w)")
|
||||
retry = part[part.index("for (int64_t q = i; q < j; q++)"):]
|
||||
if "copy_fence();" not in retry[:retry.index("i = j;")]:
|
||||
raise SystemExit("copy_part does not fence after its page-by-page retry")
|
||||
# (the page-by-page retries: copy_part's plain and staged copies (the shadow staging), each fenced after its loop)
|
||||
retries = [i for i in range(len(part)) if part.startswith("for (int64_t q = a; q < b; q++)", i)]
|
||||
if len(retries) < 2:
|
||||
raise SystemExit("copy_part's page-by-page retries not found")
|
||||
for at in retries:
|
||||
if "copy_fence();" not in part[at:part.index("};", at)]:
|
||||
raise SystemExit("copy_part does not fence after its page-by-page retry")
|
||||
if platform.machine().lower() not in ("amd64", "x86_64", "x64"):
|
||||
print("SKIP: not an x86-64 host")
|
||||
return
|
||||
|
||||
144
tests/test_compressed_over_rt.py
Normal file
144
tests/test_compressed_over_rt.py
Normal file
@@ -0,0 +1,144 @@
|
||||
"""A block-compressed texture over storage render targets used is uploaded from memory (gpu/vk/vk_texture.cpp texture_touch).
|
||||
|
||||
texture_touch skips the upload of a new texture whose storage the GPU rendered into since the CPU last wrote it: the image
|
||||
already holds what the GPU drew there. The GPU cannot render into a compressed format, so for ASTC, BC and ETC textures
|
||||
that storage only means render targets used it before; skipped, a boss or a new enemy whose textures were loaded there was
|
||||
drawn with an empty texture, all blue, for its first cutscene (from Shakoquo/Mako ff479e988d). Cuts texture_touch's decision
|
||||
out of the source as it is and checks it for each kind of format, with and without RX_VK_SKIP_COMPRESSED_OVER_RT=1 (with
|
||||
gpu_owned_since stubbed: the decision alone). Then gpu_owned_since itself, cut out the same way: overlapping render-target
|
||||
ranges count their bytes once, so memory with a gap the GPU never drew is not taken as the GPU's.
|
||||
|
||||
python -I -B tests/test_compressed_over_rt.py [--cxx g++]
|
||||
"""
|
||||
import argparse
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
ENV_ON_H = ROOT / "gpu" / "vk" / "compat" / "hookshot_env.h"
|
||||
|
||||
FIXTURE = r'''#include <atomic>
|
||||
#include <initializer_list>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
enum VkFormat { VK_FORMAT_R8G8B8A8_UNORM = 37, VK_FORMAT_BC1_RGB_UNORM_BLOCK = 131, VK_FORMAT_BC7_SRGB_BLOCK = 146,
|
||||
VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK = 147, VK_FORMAT_ASTC_4x4_UNORM_BLOCK = 157,
|
||||
VK_FORMAT_ASTC_12x12_SRGB_BLOCK = 184, VK_FORMAT_D32_SFLOAT = 126 };
|
||||
struct Texture { bool uploaded = false, astc = false, bc = false; VkFormat format = VK_FORMAT_R8G8B8A8_UNORM;
|
||||
uint64_t gpu = 0x1000, guest_size = 0x4000; uint32_t id = 1, width = 64, height = 64, layers = 1; int nvn_format = 0; };
|
||||
static int logs;
|
||||
static void logf(const char *, ...) { logs++; }
|
||||
static uint32_t gpu_owned_since(uint64_t, uint64_t) { return 7; } /* (render targets took this storage) */
|
||||
static uint32_t decide(Texture *tex) {
|
||||
static const bool upload_all = false;
|
||||
DECISION
|
||||
return owned;
|
||||
}
|
||||
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL:%d %s\n", __LINE__, #x); return 1; } } while (0)
|
||||
int main(int argc, char **) {
|
||||
const bool old_rule = argc > 1; /* (RX_VK_SKIP_COMPRESSED_OVER_RT=1) */
|
||||
Texture rgba, bc, bc_kept, astc_decoded, astc_raw, etc2, depth;
|
||||
bc.format = VK_FORMAT_BC1_RGB_UNORM_BLOCK;
|
||||
bc_kept.bc = true; /* (ASTC decoded and kept as BC3) */
|
||||
astc_decoded.astc = true; /* (ASTC decoded into RGBA8: its image format says nothing) */
|
||||
astc_raw.format = VK_FORMAT_ASTC_12x12_SRGB_BLOCK;
|
||||
etc2.format = VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
|
||||
depth.format = VK_FORMAT_D32_SFLOAT;
|
||||
CHECK(decide(&rgba) == 7 && decide(&depth) == 7); /* (formats the GPU renders: skipped as before) */
|
||||
for (Texture *t : {&bc, &bc_kept, &astc_decoded, &astc_raw, &etc2}) CHECK(decide(t) == (old_rule ? 7u : 0u));
|
||||
Texture done = bc;
|
||||
done.uploaded = true;
|
||||
CHECK(decide(&done) == 0);
|
||||
CHECK(old_rule ? logs == 0 : logs > 0);
|
||||
std::printf("PASS: %s\n", old_rule ? "RX_VK_SKIP_COMPRESSED_OVER_RT=1: compressed textures skipped as before"
|
||||
: "compressed textures over render targets' storage uploaded, others skipped as before");
|
||||
return 0;
|
||||
}
|
||||
'''
|
||||
|
||||
|
||||
# (gpu_owned_since as it is in the source, over a gpu_owned of the test's own: overlapping ranges count their bytes once)
|
||||
OWNED = r'''#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <map>
|
||||
static std::map<uint64_t, std::pair<uint64_t, uint32_t>> gpu_owned;
|
||||
FUNCTION
|
||||
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL:%d %s\n", __LINE__, #x); return 1; } } while (0)
|
||||
static void set(std::initializer_list<std::pair<uint64_t, uint64_t>> r) {
|
||||
gpu_owned.clear();
|
||||
for (auto& x : r) gpu_owned[x.first] = {x.second, 5};
|
||||
}
|
||||
int main() {
|
||||
set({{0x0, 0x3000}, {0x2000, 0x4000}}); /* (overlapping, with [0x4000, 0x5000) never drawn) */
|
||||
CHECK(gpu_owned_since(0x1000, 0x4000) == 0);
|
||||
set({{0x0, 0x3000}, {0x2000, 0x6000}}); /* (overlapping, covering it) */
|
||||
CHECK(gpu_owned_since(0x1000, 0x4000) == 5);
|
||||
set({{0x1000, 0x5000}});
|
||||
CHECK(gpu_owned_since(0x1000, 0x4000) == 5);
|
||||
set({{0x1000, 0x2000}, {0x3000, 0x5000}}); /* (a gap) */
|
||||
CHECK(gpu_owned_since(0x1000, 0x4000) == 0);
|
||||
set({});
|
||||
CHECK(gpu_owned_since(0x1000, 0x4000) == 0);
|
||||
std::printf("PASS: gpu_owned_since counts overlapping render-target ranges once: a gap never drawn is not the GPU's\n");
|
||||
return 0;
|
||||
}
|
||||
'''
|
||||
|
||||
|
||||
def main():
|
||||
p = argparse.ArgumentParser(description=__doc__)
|
||||
p.add_argument("--cxx", default="g++")
|
||||
a = p.parse_args()
|
||||
src = (ROOT / "gpu" / "vk" / "vk_texture.cpp").read_text(encoding="utf-8")
|
||||
body = src[src.index("Texture* texture_touch(Texture* tex, bool for_sampling) {"):]
|
||||
start = body.index(" static const bool skip_compressed")
|
||||
end = body.index("\n", body.index(" uint32_t owned = ", start))
|
||||
decision = body[start:end]
|
||||
assert "gpu_owned_since(tex->gpu, tex->guest_size)" in decision, decision
|
||||
# (the log after it is in the block: up to its closing brace)
|
||||
tail = body.index("\n }\n", end) + len("\n }\n")
|
||||
decision = body[start:tail]
|
||||
cxx = shutil.which(a.cxx) or shutil.which("g++") or shutil.which("clang++")
|
||||
if not cxx:
|
||||
print("SKIP: no C++ compiler")
|
||||
return
|
||||
with tempfile.TemporaryDirectory(prefix="compressed-rt-") as tmp:
|
||||
tmp = Path(tmp)
|
||||
(tmp / "f.cpp").write_text(FIXTURE.replace("DECISION", decision), encoding="utf-8")
|
||||
exe = tmp / ("f.exe" if os.name == "nt" else "f")
|
||||
# (env_on from the renderer's own header; _CRT_SECURE_NO_WARNINGS: Windows' C runtime marks getenv deprecated, and
|
||||
# -Werror made that an error with clang on Windows)
|
||||
# (Triforce: this tree has no hookshot_env.h; the switch is read with getenv and atoi here, so <cstdlib> stands in)
|
||||
inc = ["-include", str(ENV_ON_H)] if ENV_ON_H.exists() else ["-include", "cstdlib"]
|
||||
subprocess.run([cxx, "-std=c++17", "-D_CRT_SECURE_NO_WARNINGS", *inc, "-Wall",
|
||||
"-Wno-unused-function", "-Werror", str(tmp / "f.cpp"), "-o", str(exe)],
|
||||
check=True)
|
||||
fn = src[src.index("uint32_t gpu_owned_since(uint64_t gpu, uint64_t n) {"):]
|
||||
fn = fn[:fn.index("\n}\n") + 3]
|
||||
(tmp / "owned.cpp").write_text(OWNED.replace("FUNCTION", fn), encoding="utf-8")
|
||||
owned = tmp / ("owned.exe" if os.name == "nt" else "owned")
|
||||
subprocess.run([cxx, "-std=c++17", "-Wall", "-Werror", str(tmp / "owned.cpp"), "-o", str(owned)], check=True)
|
||||
r = subprocess.run([str(owned)], capture_output=True, text=True)
|
||||
print(r.stdout.strip())
|
||||
if r.returncode:
|
||||
raise SystemExit("FAIL:\n" + r.stdout + r.stderr)
|
||||
for extra in ({}, {"RX_VK_SKIP_COMPRESSED_OVER_RT": "1"}):
|
||||
env = {k: v for k, v in os.environ.items() if k != "RX_VK_SKIP_COMPRESSED_OVER_RT"}
|
||||
env.update(extra)
|
||||
args = [str(exe)] + (["old"] if extra else [])
|
||||
r = subprocess.run(args, env=env, capture_output=True, text=True)
|
||||
print(r.stdout.strip())
|
||||
if r.returncode:
|
||||
raise SystemExit("FAIL:\n" + r.stdout + r.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
167
tests/test_draw_side_effects.py
Normal file
167
tests/test_draw_side_effects.py
Normal file
@@ -0,0 +1,167 @@
|
||||
"""CPU-only draw readiness decision check: inline fake worker completion, no Vulkan/shader execution.
|
||||
|
||||
The real draw decision block is extracted intact. Publication and final resource writes are CPU witnesses,
|
||||
not a claim about GPU output, timing, or a reproduced game failure.
|
||||
"""
|
||||
import argparse
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
FIXTURE = r"""
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <vector>
|
||||
#define PROF_EXACT() ((void)0)
|
||||
#define PROF_HOT(x) ((void)0)
|
||||
#define SKIP_HERE() return
|
||||
using VkPipeline = int;
|
||||
struct Desc { bool is_written = false; };
|
||||
struct Info {
|
||||
bool stores_global_memory = false;
|
||||
std::vector<Desc> storage_buffers_descriptors, image_buffer_descriptors, image_descriptors;
|
||||
};
|
||||
struct Stage { Info info; };
|
||||
int scans, early_reads, translation_waits, pipeline_waits, draws, output, st_waits, st_pending_skips;
|
||||
double st_wait_ms;
|
||||
// Instrument only fixture iteration: reject inspecting ordinary stages before acquire-ready publication.
|
||||
struct Stages {
|
||||
std::atomic<bool>* ready;
|
||||
std::vector<Stage> values;
|
||||
auto begin() const {
|
||||
if (!ready->load(std::memory_order_acquire)) ++early_reads;
|
||||
++scans;
|
||||
return values.begin();
|
||||
}
|
||||
auto end() const { return values.end(); }
|
||||
};
|
||||
struct Program {
|
||||
std::atomic<bool> ready{false};
|
||||
bool failed = false, fallback = false, fallback_task = false;
|
||||
int layout = 1, fallback_topology = 0;
|
||||
Stages stages{&ready, {}};
|
||||
};
|
||||
struct PipeEntry { std::atomic<bool> ready{false}; VkPipeline pipeline = 1; };
|
||||
struct Targets { bool persistent = true; };
|
||||
struct NvnState {};
|
||||
struct ParLock {};
|
||||
struct DrawArgs { bool indirect = true; int groups[3]{1, 1, 1}; int first_task = 0; };
|
||||
int fallback_task_groups(const Program&) { std::abort(); }
|
||||
void draw(int, NvnState&, const DrawArgs&) { std::abort(); }
|
||||
Program* current;
|
||||
PipeEntry pipe_entry;
|
||||
int effect;
|
||||
Stage shader_stage(int kind) {
|
||||
Stage s;
|
||||
if (kind == 1) s.info.stores_global_memory = true;
|
||||
if (kind == 2) s.info.storage_buffers_descriptors.push_back({true});
|
||||
if (kind == 3) s.info.image_buffer_descriptors.push_back({true});
|
||||
if (kind == 4) s.info.image_descriptors.push_back({true});
|
||||
// Nonempty read-only descriptors must still qualify for the color-only control.
|
||||
if (kind == 0) s.info.storage_buffers_descriptors.push_back({false});
|
||||
return s;
|
||||
}
|
||||
void publish() {
|
||||
// Put the write in a later stage, not stages[0].
|
||||
current->stages.values = {Stage{}, shader_stage(effect)};
|
||||
current->ready.store(true, std::memory_order_release);
|
||||
}
|
||||
void jobs_wait(const std::atomic<bool>& ready) {
|
||||
if (&ready == ¤t->ready) { ++translation_waits; publish(); }
|
||||
else { ++pipeline_waits; pipe_entry.ready.store(true, std::memory_order_release); }
|
||||
}
|
||||
bool targets_persistent(const Targets& at) { return at.persistent; }
|
||||
bool exposure_log_on() { return false; }
|
||||
void exposure_note(NvnState&, const Program&) {}
|
||||
void ensure_layout(Program&) {}
|
||||
// These cases use non-fallback programs, whose topology stays unchanged.
|
||||
int pipeline_topology(const Program&, int drawn) { return drawn; }
|
||||
PipeEntry* graphics_pipeline(const Program&, NvnState&, const Targets&, int, const unsigned char*) { return &pipe_entry; }
|
||||
void decision(const Program* cp, const Targets& at) {
|
||||
NvnState s; DrawArgs d; int cmd = 0, topo = 0; unsigned char types[8]{};
|
||||
@BLOCK@
|
||||
// Fake draw/consumer witness only: not actual GPU shader execution or resource binding.
|
||||
++draws;
|
||||
if (effect) output = 42;
|
||||
}
|
||||
int failures;
|
||||
void check(bool ok, const char* label) { if (!ok) { ++failures; std::printf("FAIL %s\n", label); } }
|
||||
void scenario(int kind, bool translated, bool pipeline_hot, bool persistent) {
|
||||
scans = early_reads = translation_waits = pipeline_waits = draws = output = st_pending_skips = 0;
|
||||
Program p; current = &p; effect = kind;
|
||||
if (translated) publish();
|
||||
pipe_entry.ready.store(pipeline_hot, std::memory_order_release);
|
||||
const bool force = std::getenv("RX_VK_PIPE_SKIP") && std::atoi(std::getenv("RX_VK_PIPE_SKIP")) == 0;
|
||||
const bool legacy = std::getenv("RX_VK_PIPE_SKIP") && std::atoi(std::getenv("RX_VK_PIPE_SKIP")) == 2;
|
||||
const bool skip_unknown = legacy && !translated && persistent;
|
||||
const bool should_draw = !skip_unknown && (pipeline_hot || force || !persistent || kind != 0);
|
||||
decision(&p, {persistent});
|
||||
char label[120];
|
||||
std::snprintf(label, sizeof label, "kind%d translated%d hot%d persistent%d force%d", kind, translated, pipeline_hot, persistent, force);
|
||||
const int before = failures;
|
||||
check(draws == int(should_draw), label);
|
||||
if (kind && should_draw) check(output == 42, "producer write absent at fake consumer");
|
||||
check(!early_reads, "metadata inspected before publication");
|
||||
check(translation_waits == int(!translated && !skip_unknown), "translation wait mode");
|
||||
if (pipeline_hot || force || !persistent || skip_unknown) check(scans == 0, "unnecessary classification");
|
||||
check(pipeline_waits == int(!pipeline_hot && should_draw), "pending pipeline must publish before draw");
|
||||
std::printf("%s %s draws%d skips%d scans%d twait%d pwait%d\n", failures == before ? "PASS" : "RED", label,
|
||||
draws, st_pending_skips, scans, translation_waits, pipeline_waits);
|
||||
}
|
||||
int main() {
|
||||
for (int kind = 0; kind <= 4; ++kind)
|
||||
for (bool translated : {false, true})
|
||||
for (bool hot : {false, true}) scenario(kind, translated, hot, true);
|
||||
scenario(1, true, false, false);
|
||||
scenario(1, false, false, false);
|
||||
scenario(0, false, false, false);
|
||||
std::printf("%d failures; fake CPU decision check only\n", failures);
|
||||
return failures ? 1 : 0;
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--cxx", default="g++")
|
||||
parser.add_argument("--source", type=Path, default=ROOT)
|
||||
args = parser.parse_args()
|
||||
src = (args.source / "gpu/vk/vk_draw.cpp").read_text(encoding="utf-8")
|
||||
try:
|
||||
start = src.index(" static const bool always_wait =", src.index("void draw(VkCommandBuffer"))
|
||||
end = src.index(" cur_program = &p;", start)
|
||||
except ValueError:
|
||||
raise SystemExit("draw readiness block changed; update this test's extraction anchors") from None
|
||||
with tempfile.TemporaryDirectory(prefix="draw-side-effects-") as tmp:
|
||||
cpp, exe = Path(tmp) / "test.cpp", Path(tmp) / "test"
|
||||
cpp.write_text(FIXTURE.replace("@BLOCK@", src[start:end]), encoding="utf-8")
|
||||
subprocess.run([args.cxx, "-std=c++20", "-Wall", "-Wextra", "-Wno-unused-variable",
|
||||
str(cpp), "-o", str(exe)], check=True, timeout=60)
|
||||
failed = False
|
||||
for mode in (None, "0", "1", "2"):
|
||||
env = os.environ.copy()
|
||||
env.pop("RX_VK_PIPE_SKIP", None)
|
||||
if mode is not None:
|
||||
env["RX_VK_PIPE_SKIP"] = mode
|
||||
result = subprocess.run([str(exe)], env=env, capture_output=True, text=True, timeout=10)
|
||||
lines = result.stdout.splitlines()
|
||||
complete = "0 failures; fake CPU decision check only" in lines and sum(l.startswith("PASS kind") for l in lines) == 23
|
||||
ok = result.returncode == 0 and complete
|
||||
print(f"RX_VK_PIPE_SKIP={mode if mode is not None else 'unset'}: {'PASS' if ok else 'FAIL'}")
|
||||
if not ok:
|
||||
failed = True
|
||||
print("fixture failed or did not finish all 23 cases\n" + result.stdout + result.stderr)
|
||||
if failed:
|
||||
raise SystemExit(1)
|
||||
print("PASS: unknown translations wait by default; mode 2 restores their old skip; translated writers wait; hot draws do not scan stages")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -49,6 +49,10 @@ bool begin(uint32_t, uint32_t) { calls += "b"; return can_draw; }
|
||||
void idle() { calls += "i"; }
|
||||
void record(VkCommandBuffer cmd, uint32_t index, uint32_t w, uint32_t h) { CHECK(cmd == 1 && index == 2 && w == 640 && h == 360); calls += "r"; }
|
||||
}
|
||||
namespace mkwvk::rec { // (RX_VK_RECORDER: the boundary before each Dear ImGui recording, logged as "d")
|
||||
enum Bound { B_DIRECT };
|
||||
void boundary(Bound) { calls += "d"; }
|
||||
}
|
||||
ui::Ctx ui_ctx(uint32_t, uint32_t) { return {}; }
|
||||
void prebake(const ui::Ctx&) { calls += "p"; }
|
||||
bool resized = false;
|
||||
@@ -69,7 +73,7 @@ int main() {
|
||||
CHECK(calls == "i");
|
||||
calls.clear(), resized = true;
|
||||
menu_record(1, 2, 640, 360);
|
||||
CHECK(calls == (can ? "bpr" : "bi"));
|
||||
CHECK(calls == (can ? "bpdr" : "bi"));
|
||||
resized = false;
|
||||
continue;
|
||||
}
|
||||
@@ -78,7 +82,7 @@ int main() {
|
||||
if (startup) want += "s";
|
||||
if (menu) want += "m";
|
||||
if (fps && !startup) want += menu ? "F" : "f";
|
||||
want += "r";
|
||||
want += "dr"; // (the recorder boundary right before the recording)
|
||||
CHECK(calls == want);
|
||||
}
|
||||
std::printf("PASS: one ImGui frame and recording for whatever shows, in order, nothing while nothing shows\n");
|
||||
|
||||
@@ -93,14 +93,20 @@ int main(int argc, char **argv) {
|
||||
hos_step_probe = strcmp(mode, "no hook") ? 0x2000 : 0; /* (the lifted code sets it when the hook is in the build) */
|
||||
realtime_step(now);
|
||||
if (!strcmp(mode, "cap 120")) {
|
||||
CHECK(near(frames(10, 1.0 / 120), 1.0 / 120) && *rate == 60 && *limit == 120); /* (the rate stays the engine's) */
|
||||
CHECK(near(frames(72000, 1.0 / 120), 1.0 / 120) && *rate == 60 && *limit == 120); /* (the rate stays the engine's) */
|
||||
CHECK(near(lifted_step(60.0f, (float)*rate), 0.5) && near(lifted_step(30.0f, (float)*rate), 0.25)); /* (half a 60th a frame) */
|
||||
CHECK(near(frames(10, 1.0 / 40), 1.0 / 40) && near(lifted_step(60.0f, (float)*rate), 1.5));
|
||||
CHECK(near(frames(10, 1.0 / 40), 1.0 / 40) && *rate == 40 && near(lifted_step(60.0f, (float)*rate), 1.5));
|
||||
CHECK(near(frames(10, 1.0 / 1000), 1.0 / 240)); /* (the shortest step) */
|
||||
CHECK(near(frames(10, 0.2), 4.0 / 60)); /* (the longest, as the rate's 15) */
|
||||
} else if (!strcmp(mode, "cap 60")) {
|
||||
CHECK(frames(10, 1.0 / 120) == 0.0f && *rate == 60 && *limit == 60);
|
||||
CHECK(frames(10, 1.0 / 40) == 0.0f && *rate == 40 && lifted_step(60.0f, (float)*rate) == 1.5f); /* (the game's own divide) */
|
||||
CHECK(frames(72000, 1.0 / 120) == 0.0f && *rate == 60 && *limit == 60);
|
||||
/* A long unpaced stretch must repay only bounded debt, including the five-frame smoothing transition. */
|
||||
int recovered = 0;
|
||||
for (int i = 0; i < 24; i++) {
|
||||
CHECK(frames(1, 1.0 / 40) == 0.0f && *rate >= 15 && *rate <= 60);
|
||||
if (*rate == 40) { recovered = 1; break; }
|
||||
}
|
||||
CHECK(recovered && lifted_step(60.0f, (float)*rate) == 1.5f);
|
||||
} else { /* (HOS_FPS_STEP=0, or a lift without the hook: the rate up to the cap, as before) */
|
||||
CHECK(frames(10, 1.0 / 120) == 0.0f && *rate == 120 && *limit == 120);
|
||||
CHECK(lifted_step(60.0f, 60.0f) == 1.0f);
|
||||
@@ -160,7 +166,7 @@ def main():
|
||||
"a second run left one divide's hook from the first")
|
||||
|
||||
vi = (ROOT / "host" / "nn_vi.c").read_text(encoding="utf-8")
|
||||
host = "\n".join(block(vi, m) for m in ("static float text_scale(", "static int fps_max(void) {", "static void realtime_step("))
|
||||
host = "\n".join(block(vi, m) for m in ("static float text_scale(", "static int fps_max(void) {", "static int realtime_rate(", "static void realtime_step("))
|
||||
fixture = FIXTURE.replace("@HOST@", host).replace("@HOOK@", "\n".join(oot_patches.STEP_HOOK.split("\n")[:2]))
|
||||
with tempfile.TemporaryDirectory(prefix="cap-step-") as tmp:
|
||||
c = Path(tmp) / "fixture.c"
|
||||
|
||||
140
tests/test_import_windows.py
Normal file
140
tests/test_import_windows.py
Normal file
@@ -0,0 +1,140 @@
|
||||
"""Run actual pool lookup/range guards/prefetch queueing with fake GPU/background dependencies.
|
||||
|
||||
No Vulkan, background import thread, SDK or game data is used.
|
||||
|
||||
python tests/test_import_windows.py [--cxx clang++]
|
||||
|
||||
(Triforce: this is the test to run. nx2/test_import_windows.py stays as v0.3.2 had it, byte for byte: every .py under nx2/ is
|
||||
part of the lift key (make_game.lift_key), so a change there makes each player's update translate the whole game again.)
|
||||
"""
|
||||
import argparse,os
|
||||
from pathlib import Path
|
||||
import subprocess,tempfile
|
||||
|
||||
RUN_ALL_ARGS = ["--cxx", "g++"]
|
||||
|
||||
ROOT=Path(__file__).resolve().parents[1]
|
||||
def block(text,marker):
|
||||
start=text.index(marker);end=text.index('{',start)+1;depth=1
|
||||
while depth:
|
||||
depth+=(text[end]=='{')-(text[end]=='}');end+=1
|
||||
return text[start:end]
|
||||
|
||||
def main():
|
||||
p=argparse.ArgumentParser(description=__doc__);p.add_argument('--cxx',default='clang++');a=p.parse_args()
|
||||
source=(ROOT/'gpu/vk/vk_memory.cpp').read_text(encoding='utf-8')
|
||||
guard=source[source.index('bool import_pool(uint64_t lo, uint64_t hi, Pool& p) {'):source.index(' p.lo = lo;',source.index('bool import_pool('))]
|
||||
lookup=block(source,'Pool* pool_lookup(uint64_t guest, uint64_t size) {')
|
||||
chunking=source[source.index('uint64_t import_chunk() {'):source.index('Pool* pool_lookup(uint64_t guest, uint64_t size) {')]
|
||||
cache=block(source,'Pool* pool_of(uint64_t guest, uint64_t size) {')
|
||||
prefetch=block(source,'void prefetch_chunks(uint64_t lo, uint64_t hi, uint64_t chunk) {')
|
||||
fixture='''#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <deque>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
#define CHECK(x) do { if(!(x)){std::fprintf(stderr,"FAIL:%d %s\\n",__LINE__,#x);std::exit(1);} }while(0)
|
||||
struct Pool { uint64_t lo=0,hi=0,range_lo=0,range_hi=0,address=0; bool prefetched=false,gpu_only=false,gpu_written=false; };
|
||||
struct GuestPool { uint64_t lo,hi; };
|
||||
std::vector<GuestPool> guest_pools;
|
||||
std::map<uint64_t,Pool> imported;
|
||||
Pool *last_pool=nullptr,*pool_cache[4096]{};
|
||||
uint64_t max_import_size=128ull<<20;
|
||||
int failures=0,imports=0,retires=0,bg_starts=0;
|
||||
int64_t st_bg_waits=0,st_prefetch_used=0,st_prefetch_sent=0,st_prefetch_dropped=0;
|
||||
thread_local const char* tl_sync_why=nullptr; // (Triforce: why a binding's import ran on its own thread, for the log line)
|
||||
struct { bool external_memory_host=true; } dev;
|
||||
std::mutex bg_mu;std::condition_variable bg_done_cv,bg_cv;std::vector<uint64_t> bg_inflight;
|
||||
std::deque<std::pair<uint64_t,uint64_t>> bg_queue;
|
||||
void bg_start() { bg_starts++; } // Recording spy: no thread or GPU import is started.
|
||||
void logf(const char*,...){} void bg_drain(){}
|
||||
void guest_pages_set(uint64_t,uint64_t,uint64_t){}
|
||||
void retire_pool(std::map<uint64_t,Pool>::iterator it) { imported.erase(it);last_pool=nullptr;std::fill(std::begin(pool_cache),std::end(pool_cache),nullptr);retires++; }
|
||||
enum { VK_SUCCESS = 0 };
|
||||
thread_local bool import_cooling = false;
|
||||
thread_local int import_result = 0;
|
||||
bool import_on_hold(uint64_t, uint64_t) { import_cooling = false; return false; }
|
||||
void import_remember_failure(uint64_t, uint64_t) {}
|
||||
'''+guard+'''
|
||||
p.lo=lo;p.hi=hi;imports++;return true;
|
||||
}
|
||||
'''+prefetch+'\n'+chunking+'\n'+lookup+'\n'+cache+'''
|
||||
int check_prefetch(int mode) {
|
||||
const uint64_t base=0x1000000000ull,chunk=16ull<<20;
|
||||
guest_pools.push_back({base,base+8*chunk});
|
||||
dev.external_memory_host=mode!=2;
|
||||
Pool seed;seed.lo=base+3*chunk;seed.hi=base+4*chunk;seed.prefetched=true;
|
||||
auto &ready=imported.emplace(seed.lo,seed).first->second;
|
||||
CHECK(pool_lookup(seed.lo+64,64)==&ready && !ready.prefetched && st_prefetch_used==1 && imports==0);
|
||||
if(mode==1) {
|
||||
CHECK(bg_queue.size()==2 && bg_starts==2 && st_prefetch_sent==2);
|
||||
CHECK(bg_queue[0].first==base+4*chunk && bg_queue[0].second==base+5*chunk);
|
||||
CHECK(bg_queue[1].first==base+2*chunk && bg_queue[1].second==base+3*chunk);
|
||||
} else CHECK(bg_queue.empty() && bg_starts==0 && st_prefetch_sent==0);
|
||||
CHECK(pool_lookup(seed.lo+128,64)==&ready && st_prefetch_used==1 && imports==0);
|
||||
Pool plain;plain.lo=base+6*chunk;plain.hi=base+7*chunk;
|
||||
auto &unprefetched=imported.emplace(plain.lo,plain).first->second;
|
||||
CHECK(pool_lookup(plain.lo+64,64)==&unprefetched && !unprefetched.prefetched && st_prefetch_used==1 && imports==0);
|
||||
CHECK(bg_starts==(mode==1?2:0) && st_prefetch_sent==(mode==1?2:0));
|
||||
auto demand=pool_lookup(base+64,64);
|
||||
CHECK(demand && !demand->prefetched && imports==1 && st_prefetch_used==1);
|
||||
CHECK(bg_queue.size()==(mode==1?3u:0u) && bg_starts==(mode==1?3:0) && st_prefetch_sent==(mode==1?3:0));
|
||||
CHECK(bg_inflight.size()==bg_queue.size() && st_prefetch_dropped==0 && st_bg_waits==0);
|
||||
std::printf("PASS: prefetched first use, reuse, unprefetched lookup, neighbour queueing (mode %d)\\n",mode);
|
||||
return 0;
|
||||
}
|
||||
int main(int argc,char **argv) {
|
||||
if(argc>1) return check_prefetch(std::atoi(argv[1]));
|
||||
const uint64_t base=0x1000000000ull,extent=0x99200000ull;
|
||||
guest_pools.push_back({base,base+extent});
|
||||
const auto chunk=uint64_t(std::atoi(std::getenv("RX_VK_IMPORT_CHUNK_MB")))<<20;
|
||||
if(!chunk) {
|
||||
CHECK(!pool_lookup(base+1234,64) && imports==0);
|
||||
max_import_size=4ull<<30;
|
||||
auto p=pool_lookup(base+1234,64);CHECK(p && p->lo==base && p->hi==base+extent);
|
||||
} else {
|
||||
const uint64_t address=base+1234,expected=address/chunk*chunk;
|
||||
auto p=pool_lookup(address,64);CHECK(p && p->lo==expected && p->hi==expected+chunk && imports==1);
|
||||
CHECK(pool_lookup(address+100,64)==p && imports==1);
|
||||
p=pool_lookup(expected+4*chunk+123,64);CHECK(p && p->lo==expected+4*chunk && p->hi==expected+5*chunk && imports==2);
|
||||
p=pool_lookup(expected+chunk-16,64);CHECK(p && p->lo==expected && p->hi==expected+2*chunk && retires==1);
|
||||
if(chunk==(16ull<<20)) {
|
||||
// a game pool across a chunk's end: its first binding imports both chunks (span_game_pools)
|
||||
const uint64_t far=base+0x40000000ull,far2=base+0x50000000ull;
|
||||
guest_pools.push_back({far+chunk-4096,far+chunk+4096});
|
||||
guest_pools.push_back({far2+chunk-4096,far2+chunk+4096});
|
||||
p=pool_lookup(far+chunk-64,32);CHECK(p && p->lo==far && p->hi==far+2*chunk);
|
||||
CHECK(pool_lookup(far+chunk+64,32)==p); // (the binding across the end: no import again)
|
||||
// a spanned import that fails (past the device's limit here) is made again without the span
|
||||
const uint64_t limit=max_import_size;max_import_size=chunk;
|
||||
p=pool_lookup(far2+chunk-64,32);CHECK(p && p->lo==far2 && p->hi==far2+chunk);
|
||||
max_import_size=limit;
|
||||
}
|
||||
}
|
||||
const int before=imports;
|
||||
CHECK(!pool_lookup(base+extent+1,1));
|
||||
CHECK(!pool_lookup(base+extent-1,2));
|
||||
CHECK(!pool_lookup(base,UINT64_MAX));
|
||||
CHECK(!pool_lookup(base,0));
|
||||
CHECK(!pool_of(base,UINT64_MAX) && !pool_of(base,0) && imports==before);
|
||||
guest_pools.push_back({UINT64_MAX-1024,UINT64_MAX});
|
||||
if(chunk) CHECK(!pool_lookup(UINT64_MAX-16,8) && imports==before);
|
||||
Pool rejected;CHECK(!import_pool(base,base,rejected) && !import_pool(base+1,base,rejected));
|
||||
CHECK(!import_pool(base,base+max_import_size+1,rejected));
|
||||
std::printf("PASS: request windows, reuse, overlap retirement, range/overflow guards, import limits (chunk %llu)\\n",(unsigned long long)chunk);
|
||||
}
|
||||
'''
|
||||
work=ROOT/'work';work.mkdir(exist_ok=True)
|
||||
with tempfile.TemporaryDirectory(prefix='import-windows-',dir=work) as temp:
|
||||
directory=Path(temp).resolve();directory.relative_to(work.resolve());cpp=directory/'fixture.cpp';exe=directory/'fixture.exe';cpp.write_text(fixture,encoding='utf-8')
|
||||
subprocess.run([a.cxx,'-std=c++20','-O2','-D_CRT_SECURE_NO_WARNINGS',str(cpp),'-o',str(exe)],check=True)
|
||||
for chunk in ('16','3','0'):
|
||||
subprocess.run([str(exe)],env=dict(os.environ,RX_VK_IMPORT_CHUNK_MB=chunk,RX_VK_PREFETCH_CHUNKS='0'),check=True)
|
||||
for mode in ('1','0','2'):
|
||||
subprocess.run([str(exe),mode],env=dict(os.environ,RX_VK_IMPORT_CHUNK_MB='16',RX_VK_PREFETCH_CHUNKS='0' if mode=='0' else '1'),check=True)
|
||||
if __name__=='__main__':main()
|
||||
46
tests/test_mirror_gpu_only.py
Normal file
46
tests/test_mirror_gpu_only.py
Normal file
@@ -0,0 +1,46 @@
|
||||
"""A pool the GPU wrote keeps its VRAM mirror (gpu/vk/vk_memory.cpp mirrors_give_back).
|
||||
|
||||
An idle mirror is given back after kIdleEpochs submissions (at once past kLongIdle, or when a wanted one does not fit), and
|
||||
made again from guest memory at the pool's next binding. What a shader wrote through a bare entry, the pointer table or a
|
||||
wide binding is never copied back (memory_note_gpu_write): it was in the old mirror alone, and the GPU went on from stale
|
||||
data. Paused 1-2 minutes in the background, the idle pools' mirrors all went, and the game came back with cyan for a few
|
||||
seconds (RX 5500M; gone with RX_VK_MIRROR_KEEP=1). Reads the source: memory_note_gpu_write marks the pools it reaches, the
|
||||
idle give-back leaves those, and a pool that takes others in keeps their mark.
|
||||
python -I -B tests/test_mirror_gpu_only.py
|
||||
"""
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def body(src, signature):
|
||||
"""the text of the function that starts with signature, up to its closing brace at column 0"""
|
||||
start = src.index(signature)
|
||||
return src[start:src.index("\n}\n", start)]
|
||||
|
||||
|
||||
def main():
|
||||
mem = (ROOT / "gpu" / "vk" / "vk_memory.cpp").read_text(encoding="utf-8")
|
||||
if "bool gpu_only = false;" not in mem[mem.index("struct Pool {"):mem.index("};", mem.index("struct Pool {"))]:
|
||||
raise SystemExit("Pool has no gpu_only mark: nothing tells which mirrors hold what guest memory never gets")
|
||||
if ".gpu_only = true;" not in body(mem, "void memory_note_gpu_write(uint64_t gpu, uint64_t size) {"):
|
||||
raise SystemExit("memory_note_gpu_write does not mark the pools it reaches: their mirrors go with the GPU's data")
|
||||
give = body(mem, "void mirrors_give_back(uint64_t want_bytes) {")
|
||||
idle = give[give.index("std::vector<Pool*> idle;"):give.index("std::sort(idle.begin()")]
|
||||
if "if (p.gpu_only || p.gpu_written)" not in idle or "else idle.push_back(&p);" not in idle:
|
||||
raise SystemExit("mirrors_give_back gives back a gpu_only pool's idle mirror: made again from guest memory, the GPU's "
|
||||
"data in it is lost (cyan for seconds after a pause in the background)")
|
||||
bind = body(mem, "BufferRef gpu_buffer_bind(uint64_t gpu, uint64_t size, bool written) {")
|
||||
if "p->gpu_written = true;" not in bind[bind.rindex("if (written) {"):]:
|
||||
raise SystemExit("gpu_buffer_bind does not mark a pool the GPU writes through its mirror: after a save reload its mirror "
|
||||
"goes and comes back without what the GPU made of it (cyan in play and over the Start menu)")
|
||||
lookup = body(mem, "Pool* pool_lookup(uint64_t guest, uint64_t size) {")
|
||||
took_in = lookup[lookup.index("for (uint64_t k : overlapped) { // (the pools it took in)"):]
|
||||
merged = took_in[:took_in.index("imported.emplace(p.lo, p)")]
|
||||
if "p.gpu_only |= o->second.gpu_only" not in merged or "p.gpu_written |= o->second.gpu_written" not in merged:
|
||||
raise SystemExit("a pool that takes others in does not keep their gpu_only and gpu_written marks")
|
||||
print("PASS: mirrors the GPU wrote past the write-back are kept")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
115
tests/test_realtime_rate.py
Normal file
115
tests/test_realtime_rate.py
Normal file
@@ -0,0 +1,115 @@
|
||||
"""Frame-time jitter must not make the real-time game clock run fast."""
|
||||
import argparse
|
||||
import subprocess
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
RUN_ALL_ARGS = ["--cc", "gcc"]
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def block(text, marker):
|
||||
start = text.index(marker)
|
||||
end = text.index("{", start) + 1
|
||||
depth = 1
|
||||
while depth:
|
||||
depth += (text[end] == "{") - (text[end] == "}")
|
||||
end += 1
|
||||
return text[start:end]
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--cc", default="gcc")
|
||||
args = parser.parse_args()
|
||||
source = (ROOT / "host/nn_vi.c").read_text(encoding="utf-8")
|
||||
if "realtime_rate(" not in block(source, "static void realtime_step("):
|
||||
raise SystemExit("the present path does not use the tested clock quantizer")
|
||||
fixture = r'''
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define CHECK(x) do { if (!(x)) { fprintf(stderr,"FAIL:%d %s\n",__LINE__,#x); exit(1); } } while(0)
|
||||
'''
|
||||
fixture += block(source, "static int realtime_rate(")
|
||||
fixture += r'''
|
||||
int main(void) {
|
||||
double carry = 0, elapsed = 0, game = 0;
|
||||
// The old clamp erased the short frame's debt and sped this clock up by ~8%.
|
||||
for (int i = 0; i < 6000; i++) {
|
||||
double dt = i % 2 ? 1.0 / 30.0 - 0.014 : 0.014;
|
||||
int fps = realtime_rate(dt * 60, 60, &carry);
|
||||
CHECK(fps >= 15 && fps <= 60);
|
||||
elapsed += dt; game += 1.0 / fps;
|
||||
}
|
||||
CHECK(fabs(game - elapsed) <= 1.0 / 60 + 1e-8);
|
||||
// Integer-rate rounding retains elapsed time at a sustained non-integer rate.
|
||||
carry = elapsed = game = 0;
|
||||
for (int i = 0; i < 6000; i++) {
|
||||
double dt = 1.0 / 47.3;
|
||||
int fps = realtime_rate(dt * 60, 60, &carry);
|
||||
elapsed += dt; game += 1.0 / fps;
|
||||
}
|
||||
CHECK(fabs(game - elapsed) < 0.001);
|
||||
// A slow frame followed by shorter ones does not leave extra game time behind.
|
||||
carry = elapsed = game = 0;
|
||||
for (int i = 0; i < 6000; i++) {
|
||||
double dt = i % 4 == 0 ? 1.0 / 30.0 : 1.0 / 90.0;
|
||||
int fps = realtime_rate(dt * 60, 60, &carry);
|
||||
elapsed += dt; game += 1.0 / fps;
|
||||
}
|
||||
CHECK(fabs(game - elapsed) <= 1.0 / 60 + 1e-8);
|
||||
// High display caps use the same debt accounting and do not force a 60 Hz step.
|
||||
carry = 0;
|
||||
CHECK(realtime_rate(0.5, 120, &carry) == 120);
|
||||
CHECK(fabs(carry) < 1e-12);
|
||||
// The default five-frame window must also preserve time for both jitter patterns.
|
||||
for (int pattern = 0; pattern < 2; pattern++) {
|
||||
double samples[5] = {0};
|
||||
int count = 0;
|
||||
carry = elapsed = game = 0;
|
||||
for (int i = 0; i < 6000; i++) {
|
||||
const double dt = pattern ? (i % 4 == 0 ? 1.0 / 30 : 1.0 / 90)
|
||||
: (i % 2 ? 1.0 / 30 - 0.014 : 0.014);
|
||||
samples[i % 5] = dt;
|
||||
if (count < 5) count++;
|
||||
double sum = 0;
|
||||
for (int j = 0; j < count; j++) sum += samples[j];
|
||||
const int fps = realtime_rate(sum / count * 60, 60, &carry);
|
||||
CHECK(fps >= 15 && fps <= 60 && carry >= -4.0);
|
||||
elapsed += dt; game += 1.0 / fps;
|
||||
}
|
||||
CHECK(fabs(game - elapsed) <= 1.0 / 30 + 1e-8);
|
||||
printf("default window pattern %d: elapsed %.9f s, game %.9f s\n", pattern, elapsed, game);
|
||||
}
|
||||
// Ten minutes of unpaced 120 Hz presents at a 60 cap must not accumulate minutes of debt.
|
||||
carry = 0;
|
||||
for (int i = 0; i < 72000; i++) {
|
||||
CHECK(realtime_rate(0.5, 60, &carry) == 60);
|
||||
CHECK(carry >= -4.0);
|
||||
}
|
||||
CHECK(carry == -4.0);
|
||||
int recovered = 0;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
if (realtime_rate(1.5, 60, &carry) == 40) { recovered = 1; break; }
|
||||
}
|
||||
CHECK(recovered);
|
||||
// A long stall drops excess time instead of scheduling an oversized catch-up.
|
||||
carry = 0;
|
||||
CHECK(realtime_rate(12.0, 60, &carry) == 15);
|
||||
CHECK(fabs(carry) < 1e-12);
|
||||
CHECK(realtime_rate(1.0, 60, &carry) == 60);
|
||||
puts("PASS: jitter preserves elapsed time; slow and high-rate steps stay bounded");
|
||||
}
|
||||
'''
|
||||
with tempfile.TemporaryDirectory(prefix="realtime-rate-") as tmp:
|
||||
c = Path(tmp) / "fixture.c"
|
||||
exe = Path(tmp) / "fixture"
|
||||
c.write_text(fixture, encoding="utf-8")
|
||||
subprocess.run([args.cc, "-std=c11", "-O2", "-Wall", "-Wextra", "-Werror", str(c), "-lm", "-o", str(exe)], check=True)
|
||||
subprocess.run([str(exe)], check=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
503
tests/test_recorder_sync.py
Normal file
503
tests/test_recorder_sync.py
Normal file
@@ -0,0 +1,503 @@
|
||||
"""RX_VK_RECORDER=sync: every vkCmd* the renderer makes through vk_work.h is packed with deep copies of its arguments, and the packed
|
||||
calls reach the driver exactly as the direct ones do. No game, no GPU, no Vulkan library.
|
||||
|
||||
python -I -B tests/test_recorder_sync.py --headers <Vulkan-Headers/include> [--cxx clang++] [--bench N]
|
||||
|
||||
1. The sources: vk_work.h has a macro for each call vk_recorder.h packs and no other, each macro calls its packer, every vkCmd* call
|
||||
in a file that includes vk_work.h is one of them, and the files that make vkCmd* calls without it are the known ones (their calls
|
||||
do not go through the recorder: a recorder thread would reorder them).
|
||||
2. tests/recorder_sync_test.cpp, built with the real vk_work.h and vk_recorder.h and a fake Vulkan that logs every call with its
|
||||
arguments dereferenced: a frame of the renderer's calls, direct vs packed (sync) vs packed and run later (deferred) while the
|
||||
caller poisons its argument memory after each call, must log the same bytes; all packed calls covered; the fallback order kept.
|
||||
Run with RX_VK_RECORDER=sync and without, so the switch is read as the renderer reads it.
|
||||
RX_VK_RECORDER=defer (step 1.5), against the fake's command buffer lifecycle: two command buffers recorded into interleaved,
|
||||
reset/begun/ended/submitted/waited for twice, a query read and a destruction, the caller's memory poisoned; a recording handed
|
||||
between two threads under the replay lock; a 2 KB soft budget; a 1 MB hard budget; the second chunk's allocation failing; a
|
||||
parallel replay running (par_active). Each must log what the direct calls log, no command outside its command buffer's recording,
|
||||
no exception out of a vkCmd*, no block lost. Run with RX_VK_RECORDER=defer (mode 2, logged once) and with RX_VK_PARALLEL=4 too
|
||||
(refused: off, logged once).
|
||||
RX_VK_RECORDER=thread (step 2): the same frame and lifecycle with every vkCmd* run by a recorder thread, the fake driver slowed
|
||||
(1 us a call) so the recorder lags, and in the ordering scenarios the recorder held (50 ms) at each point the producer must wait
|
||||
for it (a recording's end, a submit while another command buffer records, the replay lock let go, a pool reset), so a missing
|
||||
wait is caught on a loaded runner too, each fake command buffer flagged in use for each call (two calls at once on one are a
|
||||
violation), the caller's memory poisoned and each packet poisoned once run; a handoff under the lock; a submit while another
|
||||
command buffer's commands are in flight; a pool reset while recording; the lock let go, then the recording ended on another
|
||||
thread; an 8 KB ring (backpressure); a packet bigger than its room (made directly, in order; again first in an empty ring); no
|
||||
memory for the ring; teardown with work queued (stop, join, lazy restart, vkDestroyDevice); par_active; >= 1e6 packets through a
|
||||
16 KB ring, twice, hashed against the direct stream. --throw: a driver call that throws on the recorder thread ends the process
|
||||
(exit code 3, FATAL). Run with RX_VK_RECORDER=thread (mode 3, logged once) and with RX_VK_PARALLEL=4 (refused).
|
||||
3. The test has teeth: vk_recorder.h with a copy left shallow (all of them, or only the descriptor writes' image infos), with the
|
||||
fallback's drain left out, with no drain before vkEndCommandBuffer, with the replay lock let go without a drain, with a throwing
|
||||
allocation (exception safety broken), with the failure's drain left out, or with the parallel refusal left out, must fail it.
|
||||
Thread (each run with --only thread, so the recorder thread's checks alone catch it): no wait before vkEndCommandBuffer, before
|
||||
vkQueueSubmit2, at the replay lock's release, before vkResetCommandPool; no backpressure (the ring overwritten); no join at
|
||||
teardown; a packet bigger than its room written into the ring when the ring has more free; the recorder's exception swallowed
|
||||
(--throw).
|
||||
4. (Windows) the process ends cleanly with the recorder parked in idle() holding its lock: tests/recorder_exit_test.cpp, a DLL with
|
||||
the real headers and an exe that calls ExitProcess (as the window's close does); exit code 0 within 20 s (a watchdog ends only
|
||||
that child) and the DLL's last global destructor run. Broken: the old global recorder with its destructor (wake, join).
|
||||
--bench N: the packet cost per draw on the fake (fakes that do nothing), ns, median of 5; and the recorder thread's.
|
||||
--overlap F: the replay thread's time a frame, off vs thread, with a fake driver that takes time and drains every K draws.
|
||||
"""
|
||||
import argparse
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
RUN_ALL_ARGS = ["--cxx", "{cxx}", "--headers", "{vulkan_headers}"]
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
VK = ROOT / "gpu" / "vk"
|
||||
# (files that call vkCmd* without vk_work.h, and why that is right)
|
||||
UNWRAPPED_OK = {
|
||||
"vk_coopmat.cpp": "the cooperative-matrix probe at start: its own command pool, buffer, submit and fence wait",
|
||||
"vk_imgui.cpp": "Dear ImGui's overlays: vk_menu.cpp calls boundary(B_DIRECT) before each ui::record/record_to (checked below)",
|
||||
"vk_main.cpp": "Triforce's start-up screen as pictures (RX_VK_STARTUP_SHOTS, a lab switch): a command buffer of its own, before any replay",
|
||||
}
|
||||
# (files that make a boundary call (RX_REC_BOUNDARIES: begin/end/reset, submit, wait, read, destroy) without vk_work.h, and why no packet
|
||||
# can be held then: RX_VK_RECORDER=defer drains only through vk_work.h's macros and the replay lock)
|
||||
BOUNDARY_OK = {
|
||||
"vk_coopmat.cpp": "the start-up probe: before any replay, its own vkCmd* direct (nothing held)",
|
||||
"vk_main.cpp": "create_swapchain: after device_wait_idle_locked() (vkDeviceWaitIdle through vk_work.h in vk_replay.cpp: drained)",
|
||||
"vk_shader.cpp": "program_cache_borrow's and a dropped try's own modules and layouts, which no recorded command uses",
|
||||
"vk_imgui.cpp": "its own font/logo texture views, which only its direct recordings use",
|
||||
}
|
||||
# (the names a boundary has: a new one in a vk_work.h file must be in RX_REC_BOUNDARIES)
|
||||
BOUNDARY_NAME = re.compile(r"\b(vk(?:Begin|End|Reset)\w*|vkQueue\w+|vkWait\w+|vkDeviceWaitIdle|vkDestroy\w+|vkFree\w+|"
|
||||
r"vkGetQueryPoolResults|vkGetQueueCheckpointDataNV|vkGetDeviceFaultInfoEXT|vkGetSemaphoreCounterValue|"
|
||||
r"vkAcquireNextImageKHR|vma(?:Destroy|Free|Invalidate)\w*)\s*\(")
|
||||
|
||||
|
||||
def code(text):
|
||||
"""the text without // and /* */ comments (string literals in these files hold no comment markers)"""
|
||||
return re.sub(r"//[^\n]*", "", re.sub(r"/\*.*?\*/", "", text, flags=re.S))
|
||||
|
||||
|
||||
def recorder_calls(text):
|
||||
m = re.search(r"#define RX_REC_CALLS\(V, A\)(.*?)\n\s*\nnamespace", text, re.S)
|
||||
if not m:
|
||||
raise SystemExit("RX_REC_CALLS not found in vk_recorder.h")
|
||||
return re.findall(r"\b[VA]\((vkCmd\w+)\)", m.group(1))
|
||||
|
||||
|
||||
B_DIRECT_STMT = "::mkwvk::rec::boundary(::mkwvk::rec::B_DIRECT);"
|
||||
|
||||
|
||||
C_COMMENT_OR_STRING = re.compile(r'//[^\n]*|/\*.*?\*/|"(?:\\.|[^"\\\n])*"|\'(?:\\.|[^\'\\\n])*\'', re.S)
|
||||
UI_RECORD_CALL = re.compile(r"\bui\s*::\s*record(?:_to)?\s*\(")
|
||||
NGX_RECORD_CALL = re.compile(r"\bNGX_VULKAN_\w+_EXT\s*\(") # (NVIDIA NGX's create/evaluate: its own commands into cmd)
|
||||
FFX_RECORD_CALL = re.compile(r"\bl\s*\.\s*dispatch\s*\(") # (AMD FidelityFX's ffxDispatch, through its loaded pointer: its own commands into cmd)
|
||||
OPAQUE_RECORD_CALL = re.compile(UI_RECORD_CALL.pattern + "|" + NGX_RECORD_CALL.pattern + "|" + FFX_RECORD_CALL.pattern)
|
||||
B_DIRECT_BEFORE = re.compile(r"::\s*mkwvk\s*::\s*rec\s*::\s*boundary\s*\(\s*::\s*mkwvk\s*::\s*rec\s*::\s*B_DIRECT\s*\)\s*;\Z")
|
||||
|
||||
|
||||
def blank_comments(text):
|
||||
return C_COMMENT_OR_STRING.sub(lambda m: m.group(0) if m.group(0)[0] in "\"'" else re.sub(r"[^\n]", " ", m.group(0)), text)
|
||||
|
||||
|
||||
def statement_before(code, at):
|
||||
"""the code up to the end of the statement before the one containing `at` (its ';', '{' or '}')"""
|
||||
return code[:max(code.rfind(";", 0, at), code.rfind("{", 0, at), code.rfind("}", 0, at)) + 1].rstrip()
|
||||
|
||||
|
||||
def direct_unbounded(text, fname="vk_menu.cpp", call=UI_RECORD_CALL):
|
||||
"""Recordings into a command buffer by code vk_work.h does not see (Dear ImGui's ui::record/record_to in vk_menu.cpp,
|
||||
NVIDIA NGX's NGX_VULKAN_*_EXT in vk_dlss.cpp; any spacing, split over lines) whose statement before is not the
|
||||
B_DIRECT boundary. Comments are blanked first (newlines kept): a boundary in a // or /* */ comment does not count"""
|
||||
code = blank_comments(text)
|
||||
sites = list(call.finditer(code))
|
||||
if not sites:
|
||||
return ["%s: no recording call found (the B_DIRECT check needs updating)" % fname]
|
||||
return ["%s:%d: a direct recording without boundary(B_DIRECT) as the statement just before" % (fname, code.count("\n", 0, m.start()) + 1)
|
||||
for m in sites if not B_DIRECT_BEFORE.search(statement_before(code, m.start()))]
|
||||
|
||||
|
||||
def opaque_unregistered(text, fname):
|
||||
"""note_work() written out by hand marks commands vk_work.h did not see: each must follow a call this test knows
|
||||
(OPAQUE_RECORD_CALL), so a new library recording into the command buffer has to be added here, with its boundary"""
|
||||
code = blank_comments(text)
|
||||
bad = []
|
||||
for m in re.finditer(r"(?<![\w:])(?:::\s*mkwvk\s*::\s*)?note_work\s*\(\s*\)\s*;", code):
|
||||
if not OPAQUE_RECORD_CALL.search(code[max(0, m.start() - 600):m.start()]): # (in the few statements before it)
|
||||
bad.append("%s:%d: note_work() after a recording this test does not know: add it to OPAQUE_RECORD_CALL with a "
|
||||
"B_DIRECT boundary" % (fname, code.count("\n", 0, m.start()) + 1))
|
||||
return bad
|
||||
|
||||
|
||||
def check_sources():
|
||||
rec = (VK / "vk_recorder.h").read_text(encoding="utf-8")
|
||||
work = (VK / "vk_work.h").read_text(encoding="utf-8")
|
||||
calls = recorder_calls(rec)
|
||||
if len(calls) != len(set(calls)):
|
||||
raise SystemExit("RX_REC_CALLS names a call twice")
|
||||
macros = dict(re.findall(r"#define (vkCmd\w+)\(\.\.\.\) (.*)", work))
|
||||
problems = []
|
||||
if set(macros) != set(calls):
|
||||
problems.append("vk_work.h's macros and RX_REC_CALLS differ: only in vk_work.h %s, only in vk_recorder.h %s"
|
||||
% (sorted(set(macros) - set(calls)), sorted(set(calls) - set(macros))))
|
||||
for f, body in macros.items():
|
||||
if "::mkwvk::rec::rx_%s(__VA_ARGS__)" % f not in body:
|
||||
problems.append("vk_work.h: %s does not call its packer: %s" % (f, body))
|
||||
if "::vkx::" in body:
|
||||
problems.append("vk_work.h: %s calls the driver directly" % f)
|
||||
for f in calls: # (each packer defined: generated for V, written for A)
|
||||
written = "RX_REC_PACKER void pk_%s(" % f in rec and "auto& rx_%s = Route<::vkx::%s, pk_%s>::call;" % (f, f, f) in rec
|
||||
if not re.search(r"\bV\(%s\)" % f, rec) and not written:
|
||||
problems.append("vk_recorder.h: no packer for %s" % f)
|
||||
# (defer) the boundaries: RX_REC_BOUNDARIES and vk_work.h's drain-first macros agree
|
||||
m = re.search(r"#define RX_REC_BOUNDARIES\(K, M\)(.*?)\n\s*\n", rec, re.S)
|
||||
if not m:
|
||||
raise SystemExit("RX_REC_BOUNDARIES not found in vk_recorder.h")
|
||||
bounds = dict((f, k) for k, f in re.findall(r"\b([KM])\((\w+)\)", m.group(1)))
|
||||
bmacros = dict(re.findall(r"#define (\w+)\(\.\.\.\) \(::mkwvk::rec::boundary\(::mkwvk::rec::B_\w+\), (::vkx::|::)\w+\(__VA_ARGS__\)\)", work))
|
||||
for f, q in bmacros.items():
|
||||
if "#define %s(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_%s), %s%s(__VA_ARGS__))" % (f, f, q, f) not in work:
|
||||
problems.append("vk_work.h: %s's macro calls another function" % f)
|
||||
if set(bmacros) != set(bounds) or any(bmacros[f] != ("::vkx::" if k == "K" else "::") for f, k in bounds.items() if f in bmacros):
|
||||
problems.append("vk_work.h's boundary macros and RX_REC_BOUNDARIES differ: only in vk_work.h %s, only in vk_recorder.h %s"
|
||||
% (sorted(set(bmacros) - set(bounds)), sorted(set(bounds) - set(bmacros))))
|
||||
unwrapped = {}
|
||||
unbounded = {}
|
||||
routed = 0
|
||||
for path in sorted(VK.glob("*.cpp")) + sorted(VK.glob("*.h")):
|
||||
if path.name in ("vk_recorder.h", "vk_work.h", "vk_loader.h"):
|
||||
continue
|
||||
text = code(path.read_text(encoding="utf-8", errors="replace"))
|
||||
used = sorted(set(re.findall(r"\b(vkCmd\w+)\s*\(", text)))
|
||||
bused = sorted(set(BOUNDARY_NAME.findall(text)))
|
||||
if path.suffix == ".h": # (a header's calls would be changed or not by the file including it: none make any)
|
||||
if used or bused:
|
||||
problems.append("%s (a header) calls %s: move them into a .cpp that includes vk_work.h" % (path.name, used + bused))
|
||||
continue
|
||||
if '#include "vk_work.h"' in text:
|
||||
routed += 1
|
||||
bad = [c for c in used if c not in macros]
|
||||
if bad:
|
||||
problems.append("%s calls %s, which vk_work.h does not route through the recorder" % (path.name, bad))
|
||||
bad = [c for c in bused if c not in bounds]
|
||||
if bad:
|
||||
problems.append("%s calls %s, which vk_work.h does not drain before (RX_REC_BOUNDARIES)" % (path.name, bad))
|
||||
last = text.rfind("#include")
|
||||
if text.rfind('#include "vk_work.h"') != last:
|
||||
problems.append("%s: vk_work.h is not its last include" % path.name)
|
||||
else:
|
||||
if used:
|
||||
unwrapped[path.name] = used
|
||||
if bused:
|
||||
unbounded[path.name] = bused
|
||||
for name, used in unwrapped.items():
|
||||
why = UNWRAPPED_OK.get(name)
|
||||
print("not through the recorder: %s %s (%s)" % (name, ", ".join(used), why or "NOT EXPECTED"))
|
||||
if not why:
|
||||
problems.append("%s calls %s without vk_work.h: not through the recorder" % (name, used))
|
||||
for name in set(UNWRAPPED_OK) - set(unwrapped):
|
||||
print("(%s: now through the recorder, or no vkCmd* calls; its entry in UNWRAPPED_OK can go)" % name)
|
||||
for name, used in unbounded.items():
|
||||
why = BOUNDARY_OK.get(name)
|
||||
print("boundaries without a drain: %s %s (%s)" % (name, ", ".join(used), why or "NOT EXPECTED"))
|
||||
if not why:
|
||||
problems.append("%s calls %s without vk_work.h: RX_VK_RECORDER=defer would not drain before them" % (name, used))
|
||||
for name in set(BOUNDARY_OK) - set(unbounded):
|
||||
print("(%s: no boundary calls without vk_work.h now; its entry in BOUNDARY_OK can go)" % name)
|
||||
# (vk_imgui.cpp's entries above hold only if every Dear ImGui recording starts after a B_DIRECT boundary)
|
||||
menu = (VK / "vk_menu.cpp").read_text(encoding="utf-8")
|
||||
problems += direct_unbounded(menu)
|
||||
# (controls: each way of losing the last boundary must be caught: a line comment, a block comment over lines,
|
||||
# removal with the call spaced `ui::record_to (`, removal with the call split over lines)
|
||||
last = menu.rindex(B_DIRECT_STMT)
|
||||
gone = menu[:last] + menu[last + len(B_DIRECT_STMT):]
|
||||
call = gone.index("ui::record_to(", last)
|
||||
for what, broken in (("in a line comment", menu[:last] + "// " + menu[last:]),
|
||||
("in a block comment over lines", menu[:last] + "/*\n" + B_DIRECT_STMT + "\n*/" + gone[last:]),
|
||||
("removed, call spaced", gone[:call] + "ui::record_to (" + gone[call + 14:]),
|
||||
("removed, call split over lines", gone[:call] + "ui::record_to\n (" + gone[call + 14:])):
|
||||
if not direct_unbounded(broken):
|
||||
problems.append("the B_DIRECT check missed a boundary " + what)
|
||||
# (NVIDIA NGX records into cmd itself as well: each create/evaluate after a B_DIRECT boundary; control: one removed)
|
||||
dlss = (VK / "vk_dlss.cpp").read_text(encoding="utf-8")
|
||||
problems += direct_unbounded(dlss, "vk_dlss.cpp", NGX_RECORD_CALL)
|
||||
if B_DIRECT_STMT in dlss and not direct_unbounded(dlss.replace(B_DIRECT_STMT, "", 1), "vk_dlss.cpp", NGX_RECORD_CALL):
|
||||
problems.append("the B_DIRECT check missed an NGX recording without its boundary")
|
||||
# (AMD FSR 4 records into cmd through ffxDispatch: after a B_DIRECT boundary; control: that boundary removed)
|
||||
fsr4 = (VK / "vk_fsr4.cpp").read_text(encoding="utf-8")
|
||||
problems += direct_unbounded(fsr4, "vk_fsr4.cpp", FFX_RECORD_CALL)
|
||||
if not FFX_RECORD_CALL.search(blank_comments(fsr4)):
|
||||
problems.append("vk_fsr4.cpp: no ffxDispatch call (l.dispatch) found: update FFX_RECORD_CALL")
|
||||
elif not direct_unbounded(fsr4.replace(B_DIRECT_STMT, "", 1), "vk_fsr4.cpp", FFX_RECORD_CALL):
|
||||
problems.append("the B_DIRECT check missed an ffxDispatch recording without its boundary")
|
||||
# (and any other recording vk_work.h does not see must be registered: every hand-written note_work() follows a known one)
|
||||
for path in sorted(VK.glob("*.cpp")):
|
||||
problems += opaque_unregistered(path.read_text(encoding="utf-8"), path.name)
|
||||
if not opaque_unregistered("void f() {\n some_sdk_record(cmd);\n note_work();\n}\n", "control.cpp"):
|
||||
problems.append("the note_work() registry missed an unknown recording")
|
||||
if problems:
|
||||
raise SystemExit("\n".join(problems))
|
||||
print("PASS sources: %d calls packed and %d boundaries drained, each with its vk_work.h macro; every call in the %d vk_work.h "
|
||||
"files routed; no header calls either" % (len(calls), len(bounds), routed))
|
||||
return calls
|
||||
|
||||
|
||||
def test_loader():
|
||||
"""vk_loader.h without windows.h and the Win32 surface: the same declarations, on any system"""
|
||||
text = (VK / "vk_loader.h").read_text(encoding="utf-8")
|
||||
text = text.replace("#ifdef _WIN32\n#define VK_USE_PLATFORM_WIN32_KHR", "#if 0\n#define VK_USE_PLATFORM_WIN32_KHR", 1)
|
||||
text = text.replace("#include <windows.h>\n", "", 1)
|
||||
# (its windows.h switches go too: unused without windows.h, and MinGW's libstdc++ defines NOMINMAX itself (-Werror))
|
||||
text = text.replace("#define WIN32_LEAN_AND_MEAN\n#define NOMINMAX\n", "", 1)
|
||||
if "windows.h" in text or "#if 0" not in text or "#define NOMINMAX" in text:
|
||||
raise SystemExit("vk_loader.h's shape changed: update test_loader()")
|
||||
return text
|
||||
|
||||
|
||||
def build(cxx, headers, out, recorder_text, name, extra=(), work_text=None):
|
||||
d = out / name
|
||||
d.mkdir()
|
||||
(d / "vk_loader.h").write_text(test_loader(), encoding="utf-8")
|
||||
(d / "vk_work.h").write_text(work_text or (VK / "vk_work.h").read_text(encoding="utf-8"), encoding="utf-8")
|
||||
(d / "vk_recorder.h").write_text(recorder_text, encoding="utf-8")
|
||||
exe = d / "recorder_sync_test.exe"
|
||||
cmd = [cxx, "-std=c++20", "-O2", "-Wall", "-Wextra", "-Werror", "-Wno-unused-function", "-Wno-missing-field-initializers", "-DVK_NO_PROTOTYPES",
|
||||
"-D_CRT_SECURE_NO_WARNINGS",
|
||||
*extra, "-I" + str(d), "-I" + str(headers), str(ROOT / "tests" / "recorder_sync_test.cpp"), "-o", str(exe)]
|
||||
r = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
|
||||
if r.returncode:
|
||||
raise SystemExit("%s: the build failed:\n%s" % (name, r.stdout))
|
||||
return exe
|
||||
|
||||
|
||||
def run(exe, env_mode, *args, parallel=None, timeout=600):
|
||||
env = dict(os.environ)
|
||||
for k in ("RX_VK_RECORDER", "RX_VK_PARALLEL", "RX_VK_RECORDER_POISON", "RX_VK_RECORDER_RING_MB", "RX_VK_RECORDER_STATS"):
|
||||
env.pop(k, None)
|
||||
if env_mode:
|
||||
env["RX_VK_RECORDER"] = env_mode
|
||||
if parallel:
|
||||
env["RX_VK_PARALLEL"] = parallel
|
||||
try:
|
||||
return subprocess.run([str(exe), *args], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, env=env, timeout=timeout)
|
||||
except subprocess.TimeoutExpired as e: # (a hang: reported as exit code None)
|
||||
out = e.stdout.decode(errors="replace") if isinstance(e.stdout, bytes) else (e.stdout or "")
|
||||
return subprocess.CompletedProcess(e.cmd, None, out + "\nTIMEOUT after %d s" % timeout)
|
||||
|
||||
|
||||
# (the environment runs: RX_VK_RECORDER, RX_VK_PARALLEL; the mode the renderer reads; the log lines it must write, once)
|
||||
ENV_RUNS = (
|
||||
("sync", None, "env mode 1", ()),
|
||||
(None, None, "env mode 0", ()),
|
||||
("defer", None, "env mode 2", ("LOG: vk: RX_VK_RECORDER=defer:",)),
|
||||
("defer", "1", "env mode 2", ("LOG: vk: RX_VK_RECORDER=defer:",)),
|
||||
("defer", "4", "env mode 0", ("LOG: vk: RX_VK_RECORDER=defer refused: RX_VK_PARALLEL=4",)),
|
||||
("thread", None, "env mode 3", ("LOG: vk: RX_VK_RECORDER=thread: vkCmd* packed",)),
|
||||
("thread", "1", "env mode 3", ("LOG: vk: RX_VK_RECORDER=thread: vkCmd* packed",)),
|
||||
("thread", "4", "env mode 0", ("LOG: vk: RX_VK_RECORDER=thread refused: RX_VK_PARALLEL=4",)),
|
||||
)
|
||||
|
||||
|
||||
def env_run_ok(exe, env_mode, parallel, want, logs, show=True):
|
||||
r = run(exe, env_mode, parallel=parallel)
|
||||
if show:
|
||||
print("---- RX_VK_RECORDER=%s RX_VK_PARALLEL=%s\n%s" % (env_mode or "(unset)", parallel or "(unset)", r.stdout.rstrip()))
|
||||
lines = r.stdout.splitlines()
|
||||
ok = r.returncode == 0 and want in lines and "ALL PASS" in lines
|
||||
for l in logs: # (exactly once)
|
||||
ok = ok and sum(1 for x in lines if x.startswith(l)) == 1
|
||||
if not logs:
|
||||
ok = ok and not any(x.startswith(("LOG: vk: RX_VK_RECORDER=defer", "LOG: vk: RX_VK_RECORDER=thread: vkCmd* packed",
|
||||
"LOG: vk: RX_VK_RECORDER=thread refused")) for x in lines)
|
||||
return ok, r
|
||||
|
||||
|
||||
UNORDERED_END = " default: return false; // (unordered: the end of the list)"
|
||||
|
||||
|
||||
def unordered_calls(rec):
|
||||
"""the calls rec::unordered() lists (made by the producer without waiting for the recorder)"""
|
||||
m = re.search(r"constexpr bool unordered\(Bound k\) \{(.*?)\n\}", rec, re.S)
|
||||
if not m or UNORDERED_END not in m.group(1):
|
||||
raise SystemExit("rec::unordered() not found, or its shape changed")
|
||||
return re.findall(r"^ case B_(\w+): return true;", m.group(1), re.M)
|
||||
|
||||
|
||||
def unordered_too(rec, f):
|
||||
"""vk_recorder.h with f among the calls the producer makes without waiting for the recorder"""
|
||||
return broken(rec, UNORDERED_END, " case B_%s: return true;\n%s" % (f, UNORDERED_END), "unordered")
|
||||
|
||||
|
||||
def no_wait(work_h, f):
|
||||
"""vk_work.h with f's boundary macro calling the driver at once (no drain, no wait)"""
|
||||
return broken(work_h, "#define %s(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_%s), ::vkx::%s(__VA_ARGS__))" % (f, f, f),
|
||||
"#define %s(...) (::vkx::%s(__VA_ARGS__))" % (f, f), f)
|
||||
|
||||
|
||||
EXIT_LINES = ("recorder parked in idle() holding sleep_mu: 1; ExitProcess", "DLL detach: the global destructors ran to the end")
|
||||
|
||||
|
||||
def exit_test(cxx, headers, out, recorder_text, name):
|
||||
"""(Windows) recorder_exit_test.cpp's DLL and exe built with this vk_recorder.h; (ok, what it printed); None elsewhere"""
|
||||
if os.name != "nt":
|
||||
return None
|
||||
d = out / name
|
||||
d.mkdir()
|
||||
(d / "vk_loader.h").write_text(test_loader(), encoding="utf-8")
|
||||
(d / "vk_work.h").write_text((VK / "vk_work.h").read_text(encoding="utf-8"), encoding="utf-8")
|
||||
(d / "vk_recorder.h").write_text(recorder_text, encoding="utf-8")
|
||||
src = str(ROOT / "tests" / "recorder_exit_test.cpp")
|
||||
flags = ["-std=c++20", "-O2", "-Wall", "-Wextra", "-Werror", "-DVK_NO_PROTOTYPES", "-D_CRT_SECURE_NO_WARNINGS"]
|
||||
for cmd in ([cxx, *flags, "-shared", "-DRX_EXIT_DLL", "-I" + str(d), "-I" + str(headers), src, "-o", str(d / "rx_exit.dll")],
|
||||
[cxx, *flags, src, "-o", str(d / "rx_exit.exe")]):
|
||||
r = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
|
||||
if r.returncode:
|
||||
raise SystemExit("%s: the exit fixture's build failed:\n%s" % (name, r.stdout))
|
||||
try: # (the watchdog: only this child is ended)
|
||||
r = subprocess.run([str(d / "rx_exit.exe"), str(d / "rx_exit.dll")], stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
|
||||
text=True, timeout=20)
|
||||
except subprocess.TimeoutExpired:
|
||||
return False, "TIMEOUT: the process did not end within 20 s of ExitProcess"
|
||||
lines = r.stdout.splitlines()
|
||||
return r.returncode == 0 and all(l in lines for l in EXIT_LINES), "exit code %s: %s" % (r.returncode, " | ".join(lines))
|
||||
|
||||
|
||||
def broken(text, old, new, what):
|
||||
if text.count(old) != 1:
|
||||
raise SystemExit("vk_recorder.h's shape changed (%s): update the broken variants" % what)
|
||||
return text.replace(old, new)
|
||||
|
||||
|
||||
def main():
|
||||
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
p.add_argument("--cxx", default="clang++")
|
||||
p.add_argument("--headers", required=True, help="the Vulkan headers' include folder")
|
||||
p.add_argument("--bench", type=int, default=0, help="also time the packets: N draws a run")
|
||||
p.add_argument("--overlap", type=int, default=0, help="also model the overlap: F frames a run")
|
||||
a = p.parse_args()
|
||||
check_sources()
|
||||
|
||||
work = ROOT / "work"
|
||||
work.mkdir(exist_ok=True)
|
||||
out = Path(tempfile.mkdtemp(prefix="recorder-sync-", dir=work))
|
||||
rec = (VK / "vk_recorder.h").read_text(encoding="utf-8")
|
||||
exe = build(a.cxx, a.headers, out, rec, "real")
|
||||
failed = False
|
||||
for env_mode, parallel, want, logs in ENV_RUNS:
|
||||
if not env_run_ok(exe, env_mode, parallel, want, logs)[0]:
|
||||
failed = True
|
||||
if failed:
|
||||
raise SystemExit("FAIL: the packed calls differ from the direct ones, or the mode or its log line is wrong")
|
||||
ex = exit_test(a.cxx, a.headers, out, rec, "exit")
|
||||
if ex is None:
|
||||
print("---- exit with the recorder parked: SKIP (not Windows)")
|
||||
else:
|
||||
print("---- exit with the recorder parked in idle() holding its lock, then ExitProcess: %s" % ex[1])
|
||||
if not ex[0]:
|
||||
raise SystemExit("FAIL: the process does not end cleanly with the recorder parked")
|
||||
old = broken(broken(rec, "inline Recorder& g_rec = *new Recorder;", "inline Recorder g_rec;", "g_rec"),
|
||||
" // (no destructor: the recorder lives as long as the process, below)\n",
|
||||
" ~Recorder() {\n if (th.joinable()) {\n stop_req.store(true);\n wake();\n th.join();\n"
|
||||
" }\n if (base) ::operator delete(base, std::align_val_t(64));\n }\n", "dtor")
|
||||
ob = exit_test(a.cxx, a.headers, out, old, "exit_broken")
|
||||
print("---- broken: the old global recorder with its destructor (wake, join) -> %s\n %s" % ("caught" if not ob[0] else "NOT CAUGHT", ob[1]))
|
||||
if ob[0]:
|
||||
raise SystemExit("FAIL: the exit test passes the old destructor")
|
||||
r = run(exe, None, "--throw", timeout=60) # (the recorder thread's exception: loud, exit code 3)
|
||||
print("---- --throw: exit code %s\n%s" % (r.returncode, r.stdout.rstrip()))
|
||||
if r.returncode != 3 or "FATAL: RX_VK_RECORDER=thread" not in r.stdout:
|
||||
raise SystemExit("FAIL: an exception on the recorder thread did not end the process loudly")
|
||||
|
||||
work_h = (VK / "vk_work.h").read_text(encoding="utf-8")
|
||||
# (each: what is broken, vk_recorder.h, vk_work.h, the environment it runs in)
|
||||
variants = [
|
||||
("shallow copies (dup hands the caller's pointer on)",
|
||||
broken(rec, " std::memcpy(static_cast<void*>(d), p, sizeof(T) * n);\n return d;\n",
|
||||
" (void)d;\n return p;\n", "dup"), work_h, ("sync", None)),
|
||||
("descriptor writes' image infos not copied",
|
||||
broken(rec, "c[i].pImageInfo = e.dup(w[i].pImageInfo, w[i].descriptorCount);",
|
||||
"c[i].pImageInfo = w[i].pImageInfo;", "push descriptors"), work_h, ("sync", None)),
|
||||
("fallback without the drain (order)",
|
||||
broken(rec, " else drain();\n t_arena.fallbacks++;", " t_arena.fallbacks++;", "fallback"), work_h, ("sync", None)),
|
||||
("defer: no drain before vkEndCommandBuffer",
|
||||
rec, no_wait(work_h, "vkEndCommandBuffer"), (None, None)),
|
||||
("defer: the replay lock let go without a drain",
|
||||
broken(rec, " boundary(B_REPLAY_UNLOCK);\n t_prod = nullptr;", " t_prod = nullptr;", "unlock"), work_h, (None, None)),
|
||||
("defer: exception safety broken (a throwing chunk allocation)",
|
||||
broken(broken(rec, "::operator new(cap, std::align_val_t(64), std::nothrow)", "::operator new(cap, std::align_val_t(64))", "new"),
|
||||
"void* alloc(size_t n, size_t align) noexcept {", "void* alloc(size_t n, size_t align) {", "noexcept"), work_h, (None, None)),
|
||||
("defer: an allocation failure's call made before the held ones (no drain)",
|
||||
broken(rec, " if (md == DEFER) drain(); // (what is held first", " if (0) drain(); // (what is held first",
|
||||
"failure drain"), work_h, (None, None)),
|
||||
("defer: the parallel refusal left out (RX_VK_PARALLEL=4)",
|
||||
broken(rec, " if (!parallel_configured()) return e[0] == 'd' ? DEFER : THREAD;", " return e[0] == 'd' ? DEFER : THREAD;",
|
||||
"parallel"), work_h, ("defer", "4")),
|
||||
# (the recorder thread's: run with --only thread)
|
||||
("thread: no wait before vkEndCommandBuffer", rec, no_wait(work_h, "vkEndCommandBuffer"), ("--only", None)),
|
||||
("thread: no wait before vkQueueSubmit2", rec, no_wait(work_h, "vkQueueSubmit2"), ("--only", None)),
|
||||
("thread: the replay lock let go without waiting for the recorder",
|
||||
broken(rec, " boundary(B_REPLAY_UNLOCK);\n t_prod = nullptr;", " t_prod = nullptr;", "unlock"), work_h, ("--only", None)),
|
||||
("thread: a command pool reset without waiting for the recorder", rec, no_wait(work_h, "vkResetCommandPool"), ("--only", None)),
|
||||
("thread: no backpressure (the ring overwritten)",
|
||||
broken(broken(rec, " void wait_room(uint64_t need) {\n", " void wait_room(uint64_t need) {\n if (need) return;\n", "room"),
|
||||
" lim = base + off + room;", " lim = base + off + tail;", "lim"), work_h,
|
||||
("--only", None)),
|
||||
("thread: a packet bigger than its room written into the ring when it has more free (the room not the limit)",
|
||||
broken(rec, " lim = base + off + room;", " lim = base + off + std::min<uint64_t>(tail, cap - (w - r_seen));",
|
||||
"room limit"), work_h, ("--only", None)),
|
||||
("thread: no join at teardown", broken(rec, " th.join();\n ready.store(false);", " ready.store(false);", "join"), work_h,
|
||||
("--only", None)),
|
||||
("thread: the recorder thread's exception swallowed",
|
||||
broken(rec, " recorder_fatal(e.what());", " (void)e;", "fatal"), work_h, ("--throw", None)),
|
||||
# (rec::unordered(): a call that does depend on the ring added to it must fail a check)
|
||||
("thread: vkQueueSubmit2 made without waiting (added to unordered())", unordered_too(rec, "vkQueueSubmit2"), work_h, ("--only", None)),
|
||||
("thread: direct recording (Dear ImGui) without waiting (B_DIRECT added to unordered())", unordered_too(rec, "DIRECT"), work_h,
|
||||
(None, None)),
|
||||
("thread: vkDestroyBuffer made without waiting (added to unordered())", unordered_too(rec, "vkDestroyBuffer"), work_h, ("--only", None)),
|
||||
("thread: vkEndCommandBuffer made without waiting (added to unordered())", unordered_too(rec, "vkEndCommandBuffer"), work_h, ("--only", None)),
|
||||
("thread: vkGetQueryPoolResults made without waiting (added to unordered())", unordered_too(rec, "vkGetQueryPoolResults"), work_h,
|
||||
("--only", None)),
|
||||
] + [ # (the calls it lists made with a wait after all (boundary() ignoring unordered()): the check that each runs with packets
|
||||
# in flight must fail; with none listed, nothing to check)
|
||||
("thread: the calls of unordered() (%s) waiting after all" % ", ".join(unordered_calls(rec)),
|
||||
broken(rec, " else if (unordered(k)) return;\n", "", "skip"), work_h, ("--only", None))
|
||||
][:1 if unordered_calls(rec) else 0] + [
|
||||
("thread: the parallel refusal left out (RX_VK_PARALLEL=4)",
|
||||
broken(rec, " if (!parallel_configured()) return e[0] == 'd' ? DEFER : THREAD;", " return e[0] == 'd' ? DEFER : THREAD;",
|
||||
"parallel"), work_h, ("thread", "4")),
|
||||
]
|
||||
for i, (what, text, wtext, (env_mode, parallel)) in enumerate(variants):
|
||||
exe_b = build(a.cxx, a.headers, out, text, "broken%d" % i, work_text=wtext)
|
||||
if parallel:
|
||||
refused = [e for e in ENV_RUNS if e[0] == env_mode and e[1] == "4"][0]
|
||||
ok, r = env_run_ok(exe_b, env_mode, parallel, "env mode 0", refused[3], show=False)
|
||||
caught = not ok
|
||||
elif env_mode == "--only":
|
||||
r = run(exe_b, None, "--only", "thread", timeout=120)
|
||||
caught = r.returncode != 0
|
||||
elif env_mode == "--throw":
|
||||
r = run(exe_b, None, "--throw", timeout=30)
|
||||
caught = not (r.returncode == 3 and "FATAL: RX_VK_RECORDER=thread" in r.stdout)
|
||||
else:
|
||||
r = run(exe_b, env_mode)
|
||||
caught = r.returncode != 0
|
||||
lines = [l for l in r.stdout.splitlines() if l.startswith(("MISMATCH", "FAIL", "PASS sync", "PASS deferred", "env mode", "TIMEOUT",
|
||||
"the recorder"))]
|
||||
if not lines:
|
||||
lines = ["(no output: exit code %s)" % r.returncode]
|
||||
print("---- broken: %s -> %s (exit code %s)\n %s" % (what, "caught" if caught else "NOT CAUGHT", r.returncode, "\n ".join(lines[:6])))
|
||||
if not caught:
|
||||
raise SystemExit("FAIL: a broken recorder (%s) passed" % what)
|
||||
if a.bench or a.overlap: # (with the renderer's CPU flags, nx2/build.py's CPU: RX_MARCH or x86-64-v3)
|
||||
fast = build(a.cxx, a.headers, out, rec, "bench", ["-march=" + os.environ.get("RX_MARCH", "x86-64-v3"), "-ffp-contract=off"])
|
||||
if a.bench:
|
||||
r = run(fast, None, "--bench", str(a.bench))
|
||||
print(r.stdout.rstrip())
|
||||
if a.overlap:
|
||||
r = run(fast, None, "--overlap", str(a.overlap))
|
||||
print(r.stdout.rstrip())
|
||||
shutil.rmtree(out, ignore_errors=True)
|
||||
print("PASS recorder sync: identical streams, poisoned caller memory, coverage, fallback order; broken variants caught")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
90
tests/test_tex_spans_once_a_present.py
Normal file
90
tests/test_tex_spans_once_a_present.py
Normal file
@@ -0,0 +1,90 @@
|
||||
"""The texture spans' ageing (gpu/vk/vk_memory.cpp publish_tex_spans): a span unused for 1800 presents is dropped, and the
|
||||
spans are looked over for that once a present, not at each of the present's submissions. A span noted between two
|
||||
submissions of one present is still published by the next.
|
||||
|
||||
python -I -B tests/test_tex_spans_once_a_present.py [--cxx clang++]
|
||||
"""
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def block(text, marker):
|
||||
start = text.index(marker)
|
||||
end = text.index('{', start) + 1
|
||||
depth = 1
|
||||
while depth:
|
||||
depth += (text[end] == '{') - (text[end] == '}')
|
||||
end += 1
|
||||
return text[start:end]
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--cxx', default='g++')
|
||||
parser.add_argument('--source', type=Path, default=ROOT / 'gpu/vk/vk_memory.cpp')
|
||||
args = parser.parse_args()
|
||||
publish = block(args.source.read_text(encoding='utf-8'), 'void publish_tex_spans(uint64_t frame) {')
|
||||
fixture = r'''
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#define CHECK(ok) do { if (!(ok)) { std::fprintf(stderr, "failed line %d\n", __LINE__); std::exit(1); } } while (0)
|
||||
// (the spans, counting each time they are gone through from the start)
|
||||
struct Spans : std::map<uint64_t, std::pair<uint64_t, uint64_t>> {
|
||||
int walks = 0;
|
||||
iterator begin() { walks++; return std::map<uint64_t, std::pair<uint64_t, uint64_t>>::begin(); }
|
||||
};
|
||||
Spans tex_spans;
|
||||
std::vector<std::pair<uint64_t, uint64_t>> tex_merged, tex_cap_ranges, dropped;
|
||||
bool tex_spans_changed = false;
|
||||
uint64_t tex_merged_bytes = 0, cap_ranges_ver = 0;
|
||||
std::mutex shadow_mu, cap_ranges_mu;
|
||||
void shadow_drop_range(uint64_t lo, uint64_t hi) { dropped.push_back({lo, hi}); }
|
||||
''' + publish + r'''
|
||||
int main() {
|
||||
tex_spans.try_emplace(0x1000, 0x3000, 100); // (lo -> hi, the present it was last uploaded from)
|
||||
tex_spans.try_emplace(0x8000, 0x9000, 100);
|
||||
tex_spans_changed = true;
|
||||
publish_tex_spans(100); // (the present's first submission: looked over, and published)
|
||||
CHECK(tex_cap_ranges.size() == 2 && tex_merged_bytes == 0x3000 && cap_ranges_ver == 1 && !tex_spans_changed);
|
||||
const int after_first = tex_spans.walks;
|
||||
for (int i = 0; i < 19; i++) publish_tex_spans(100); // (its other submissions: nothing to do)
|
||||
CHECK(tex_spans.walks == after_first && cap_ranges_ver == 1 && dropped.empty());
|
||||
tex_spans.try_emplace(0x20000, 0x21000, 100); // (a texture uploaded between two of them)
|
||||
tex_spans_changed = true;
|
||||
publish_tex_spans(100);
|
||||
CHECK(tex_cap_ranges.size() == 3 && tex_merged_bytes == 0x4000 && cap_ranges_ver == 2 && dropped.empty());
|
||||
const int same_present = tex_spans.walks;
|
||||
publish_tex_spans(101); // (the next present: looked over again, once)
|
||||
publish_tex_spans(101);
|
||||
CHECK(tex_spans.walks == same_present + 1 && cap_ranges_ver == 2);
|
||||
tex_spans[0x8000].second = 1500; // (used again later than the others)
|
||||
publish_tex_spans(1900); // (1800 presents after 100: kept, the bound is past 1800)
|
||||
CHECK(dropped.empty() && tex_cap_ranges.size() == 3);
|
||||
publish_tex_spans(1901); // (the spans last used at 100 go, with their shadow pages; the one used at 1500 stays)
|
||||
CHECK(dropped.size() == 2 && dropped[0] == std::make_pair(uint64_t(0x1000), uint64_t(0x3000)) &&
|
||||
dropped[1] == std::make_pair(uint64_t(0x20000), uint64_t(0x21000)));
|
||||
CHECK(tex_spans.size() == 1 && tex_cap_ranges.size() == 1 && tex_cap_ranges[0].first == 0x8000 && tex_merged_bytes == 0x1000 &&
|
||||
cap_ranges_ver == 3 && !tex_spans_changed);
|
||||
std::puts("PASS: spans aged once a present, a span noted mid-present published, unused spans dropped after 1800");
|
||||
}
|
||||
'''
|
||||
with tempfile.TemporaryDirectory(prefix='tex-spans-') as tmp:
|
||||
cpp = Path(tmp) / 'fixture.cpp'
|
||||
exe = Path(tmp) / 'fixture.exe'
|
||||
cpp.write_text(fixture, encoding='utf-8')
|
||||
subprocess.run([args.cxx, '-std=c++20', '-Wall', '-Wextra', '-Werror', str(cpp), '-o', str(exe)], check=True)
|
||||
subprocess.run([str(exe)], check=True, timeout=60)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -9,6 +9,10 @@
|
||||
int main() {
|
||||
using namespace vram_compact;
|
||||
const uint64_t MB = 1ull << 20;
|
||||
// on unless RX_VK_COMPACT says 0: not set is on (the default), and 1 still is
|
||||
CHECK(enabled(nullptr));
|
||||
CHECK(enabled("1") && enabled("2"));
|
||||
CHECK(!enabled("0") && !enabled(""));
|
||||
// pressed: the driver's count over 90% of what it allows; never without an allowance
|
||||
CHECK(!pressed(9000 * MB, 10900 * MB));
|
||||
CHECK(pressed(10023 * MB, 10900 * MB)); // (the uncompacted walk's count)
|
||||
@@ -31,6 +35,63 @@ int main() {
|
||||
CHECK(held_budget(budget, 300 * MB, 10800 * MB, 0, allowed, 85) == 0); // (the excess is more than the textures hold)
|
||||
CHECK(held_budget(budget, 5600 * MB, 10023 * MB, 215 * MB, 0, 85) == budget); // (no allowance known: left alone)
|
||||
CHECK(held_budget(budget, 5600 * MB, 100 * MB, 215 * MB, allowed, 85) == budget); // (more to give back than is used)
|
||||
// the hold's pass keeps a live texture used in the last ten seconds: the eviction's own 60 presents is not enough for it
|
||||
CHECK(kHoldUnused == 600);
|
||||
CHECK(hold_keeps(0) && hold_keeps(60) && hold_keeps(599)); // (out of view for a second: a turning camera brings it back)
|
||||
CHECK(!hold_keeps(600) && !hold_keeps(3600));
|
||||
// the reserve: a line five points under the hold's, for the textures unused thirty seconds
|
||||
CHECK(reserve_pct(85) == 80 && reserve_pct(50) == 45 && reserve_pct(3) == 0);
|
||||
CHECK(kReserveUnused == 1800 && kReserveUnused > kHoldUnused);
|
||||
const uint64_t none = UINT64_MAX; // (the other rules ask nothing: the hold's two lines alone)
|
||||
CHECK(hold_target(59, 60, none, 5000 * MB, 4500 * MB) == UINT64_MAX); // (used a second ago: stays, whatever the lines ask)
|
||||
CHECK(hold_target(599, 60, none, 5000 * MB, 4500 * MB) == UINT64_MAX);
|
||||
CHECK(hold_target(600, 60, none, 5000 * MB, 4500 * MB) == 5000 * MB); // (unused ten seconds: for the hold's own line)
|
||||
CHECK(hold_target(1799, 60, none, 5000 * MB, 4500 * MB) == 5000 * MB);
|
||||
CHECK(hold_target(1800, 60, none, 5000 * MB, 4500 * MB) == 4500 * MB); // (unused thirty: down to the reserve's line)
|
||||
CHECK(hold_target(600, 60, none, 0, 4500 * MB) == UINT64_MAX); // (the hold's line not crossed: only the long unused go)
|
||||
CHECK(hold_target(1800, 60, none, 0, 4500 * MB) == 4500 * MB);
|
||||
// the other rules' part: over the budget's size, over 90% of the allowance or at the driver's limit, a texture unused for
|
||||
// the eviction's own 60 presents goes for what those rules ask, as with the compaction off; the hold's patience is for its
|
||||
// own lines
|
||||
CHECK(rule_target(5600 * MB, 6144 * MB) == UINT64_MAX); // (under the budget: they ask nothing)
|
||||
CHECK(rule_target(5600 * MB, 5600 * MB) == UINT64_MAX);
|
||||
CHECK(rule_target(5600 * MB, 5300 * MB) == 5300 * MB / 8 * 7); // (over it: cut to 7/8 of it, as ever)
|
||||
CHECK(hold_target(100, 60, 4637 * MB, 5000 * MB, 4500 * MB) == 4637 * MB); // (the count over 90%: one unused 100 presents still goes)
|
||||
CHECK(hold_target(59, 60, 4637 * MB, 5000 * MB, 4500 * MB) == UINT64_MAX); // (one used in the last 60: never)
|
||||
CHECK(hold_target(100, 120, 4637 * MB, 5000 * MB, 4500 * MB) == UINT64_MAX); // (nor before the pass's own time, whatever that is)
|
||||
CHECK(hold_target(700, 60, 5200 * MB, 5000 * MB, 4500 * MB) == 5000 * MB); // (the lowest of the lines that apply)
|
||||
CHECK(hold_target(700, 60, 4000 * MB, 5000 * MB, 4500 * MB) == 4000 * MB);
|
||||
CHECK(hold_target(2000, 60, 4000 * MB, 5000 * MB, 4500 * MB) == 4000 * MB);
|
||||
CHECK(hold_target(2000, 60, 5200 * MB, 5000 * MB, 4500 * MB) == 4500 * MB);
|
||||
CHECK(hold_target(700, 60, 4000 * MB, 0, 4500 * MB) == 4000 * MB); // (the hold's line not crossed, the rules asking)
|
||||
{ // a count at 94% of a 7,000 MB allowance (6,600 MB used, 100 of it to give back; the textures hold 2,400): the 90% rule's
|
||||
// budget is 2,200 MB and a pass of its own goes to 1,925; the hold and the reserve ask for less still, so the pass is the
|
||||
// hold's. A texture unused 100 presents goes until the 90% rule's part is covered, and no further
|
||||
const uint64_t a = 7000 * MB, total = 2400 * MB;
|
||||
const uint64_t rule = total - (6500 - 6300) * MB; // (the 90% rule: the excess over 6,300 MB off the textures)
|
||||
const uint64_t hold = held_budget(rule, total, 6600 * MB, 100 * MB, a, 85);
|
||||
const uint64_t res = held_budget(hold, total, 6600 * MB, 100 * MB, a, reserve_pct(85));
|
||||
CHECK(hold == total - (6500 - 5950) * MB && res == total - (6500 - 5600) * MB);
|
||||
const uint64_t to = hold_target(100, 60, rule_target(total, rule), hold, res);
|
||||
CHECK(to == 2200 * MB / 8 * 7);
|
||||
CHECK(!hold_met(total, 0, 0, to) && !hold_met(total, 400 * MB, 0, to) && hold_met(total, 475 * MB, 0, to));
|
||||
CHECK(hold_met(total, 100 * MB, 375 * MB, to)); // (the dead render targets count for it first, as for the other lines)
|
||||
CHECK(hold_target(700, 60, rule_target(total, rule), hold, res) == hold); // (older ones go on to the hold's line)
|
||||
CHECK(hold_target(1800, 60, rule_target(total, rule), hold, res) == res); // (and the long unused to the reserve's)
|
||||
CHECK(hold_target(100, 60, rule_target(2100 * MB, rule), hold, res) == UINT64_MAX); // (under that budget: the hold's patience again)
|
||||
}
|
||||
// a count between the two lines: the reserve asks for less than the budget, the hold for nothing
|
||||
CHECK(held_budget(budget, 5000 * MB, 9000 * MB, 200 * MB, allowed, 85) == budget);
|
||||
CHECK(held_budget(budget, 5000 * MB, 9000 * MB, 200 * MB, allowed, reserve_pct(85)) == 5000 * MB - (9000 - 200 - 8720) * MB);
|
||||
// and it is done with live textures once those chosen and the dead render targets together cover the excess
|
||||
CHECK(!hold_met(5600 * MB, 0, 0, 5000 * MB)); // (600 MB over, nothing chosen yet)
|
||||
CHECK(!hold_met(5600 * MB, 599 * MB, 0, 5000 * MB));
|
||||
CHECK(hold_met(5600 * MB, 600 * MB, 0, 5000 * MB));
|
||||
CHECK(hold_met(5600 * MB, 0, 600 * MB, 5000 * MB)); // (the dead targets alone cover it: no live texture goes)
|
||||
CHECK(hold_met(5600 * MB, 0, 2500 * MB, 5000 * MB)); // (the walk's 2.5 GB of dead targets)
|
||||
CHECK(!hold_met(5600 * MB, 100 * MB, 400 * MB, 5000 * MB)); // (100 MB still to go)
|
||||
CHECK(hold_met(5600 * MB, 200 * MB, 400 * MB, 5000 * MB));
|
||||
CHECK(hold_met(300 * MB, 200 * MB, 200 * MB, 0)); // (more chosen than the textures hold, their sum having drifted)
|
||||
std::printf("PASS: the compaction's rules\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
"""Compile and run the VRAM compaction's rules (gpu/vk/vram_compact.h; no game/GPU required), and check that vk_texture.cpp and
|
||||
vk_vma.cpp still use them: the textures in a pool of their own only with RX_VK_COMPACT, nothing of that pool freed while a pass is
|
||||
vk_vma.cpp still use them: the textures in a pool of their own unless RX_VK_COMPACT=0, nothing of that pool freed while a pass is
|
||||
open, and the budget holding the driver's count with the textures' free space alone taken off.
|
||||
|
||||
python -I -B tests/test_vram_compact.py [--cxx g++]
|
||||
@@ -31,8 +31,9 @@ def need(where, text, needles):
|
||||
def check_source():
|
||||
tex = (ROOT / "gpu/vk/vk_texture.cpp").read_text(encoding="utf-8")
|
||||
vma = (ROOT / "gpu/vk/vk_vma.cpp").read_text(encoding="utf-8")
|
||||
# (the compaction is opt-in)
|
||||
need("vk_texture.cpp: compact_on()", block(tex, "bool compact_on()"), ['getenv("RX_VK_COMPACT")'])
|
||||
# (Triforce: the switch decides when it is set; unset, the compaction is on where the memory handling is, NVIDIA cards and
|
||||
# the other makes on trial: memory_trial_on. Upstream's rule, on unless RX_VK_COMPACT=0, is vram_compact::enabled, run below)
|
||||
need("vk_texture.cpp: compact_on()", block(tex, "bool compact_on()"), ['getenv("RX_VK_COMPACT")', "memory_trial_on()"])
|
||||
# (a texture that is not a render target's goes to the textures' pool only when they are compacted, in a branch before the
|
||||
# render targets' pool and blocks, which stay as they are)
|
||||
make = block(tex, "Texture* create(const Key& key")
|
||||
@@ -50,8 +51,31 @@ def check_source():
|
||||
["vram_compact::reclaimable(block_free, pool_free)", "vram_compact::pressed(used, allowed)",
|
||||
"vram_compact::held_budget(budget, total_bytes, used, reclaim, allowed, pct)", 'getenv("RX_VK_COMPACT_PCT")',
|
||||
# (the hold lets go of the excess alone, not of an eighth of the cache; the driver's limit still asks for 7/8)
|
||||
"if (held < budget) budget = evict_to = held;", "if (budget < evict_to) evict_to = 0;",
|
||||
"total_bytes - pending_bytes <= (evict_to ? evict_to : budget / 8 * 7)"])
|
||||
"if (held < budget) budget = evict_to = held;",
|
||||
# (the hold's pass: every dead render target, and live textures only for the excess left and only long unused ones;
|
||||
# it does not stop at the first texture past the excess, which kept it from the dead targets, the youngest)
|
||||
# (and what the other rules ask on their own is kept from before the hold's lines, the 97% rule's with it, for the
|
||||
# pass's third tier: a texture unused for the eviction's own time goes for that part)
|
||||
"uint64_t rule_budget = budget;", "rule_budget = std::min(rule_budget, total_bytes > over ? total_bytes - over : 0);",
|
||||
"const uint64_t rule_to = vram_compact::rule_target(total_bytes, rule_budget);",
|
||||
# (the reserve: a lower line that sets the budget too, the hold's own line kept for the textures not long unused;
|
||||
# the driver's limit, when it asks for more, clears both)
|
||||
"hold_to = evict_to;", "vram_compact::reserve_pct(pct));", "if (reserve < budget) budget = evict_to = reserve;",
|
||||
"if (budget < evict_to) evict_to = hold_to = 0;",
|
||||
# (Triforce: the pass itself is texture_sweep here, split from texture_frame on 11-11 for the dead targets' own
|
||||
# passes; the hold's two lines go to it as arguments)
|
||||
"texture_sweep(completed, total_bytes <= budget, budget, evict_to, known, used, allowed, false, hold_to, rule_to);"])
|
||||
sweep_fn = block(tex[tex.index("void texture_frame(uint64_t completed)"):], "void texture_sweep(uint64_t completed")
|
||||
need("vk_texture.cpp: texture_sweep()", sweep_fn,
|
||||
["const bool by_hold = evict_to != 0 && !sweep;",
|
||||
"if (shifted || drawn_over(tex)) hold_dead += tex->bytes + tex->pad;",
|
||||
"if ((!sweep && !by_hold && (pending_bytes >= total_bytes || total_bytes - pending_bytes <= budget / 8 * 7)) ||",
|
||||
"const uint64_t to = vram_compact::hold_target(frame_no - last, need_unused, rule_to, hold_to, evict_to);",
|
||||
"if (to == UINT64_MAX || vram_compact::hold_met(total_bytes, hold_live, hold_dead, to)) continue;",
|
||||
"if (by_hold && !overwritten) hold_live += tex->bytes + tex->pad;"])
|
||||
# (and a live texture is judged by the hold only once it is known whether a render target drew over it)
|
||||
if not sweep_fn.index("const bool overwritten = shifted || drawn_over(tex);") < sweep_fn.index("vram_compact::hold_target(frame_no - last"):
|
||||
raise SystemExit("vk_texture.cpp: the hold's pass judges a texture before it knows whether it is a dead render target")
|
||||
need("vk_texture.cpp: compact_step()", block(tex, "void compact_step(uint64_t completed)"),
|
||||
["if (!compact_on()) return;", "vram_compact::run_from(min_free, compact.pressed)", "vram_compact::rest(compact.pressed)",
|
||||
"t != present_source", "image_compact_ignore(i);", "destroy_gen++;"])
|
||||
|
||||
Reference in New Issue
Block a user