Triforce v0.3.6

This commit is contained in:
ruckus7382sfgsgdf
2026-11-14 00:33:23 -06:00
parent 2f1e350320
commit a02bcb21a9
106 changed files with 7683 additions and 1422 deletions

View File

@@ -87,6 +87,9 @@ FILE* disk_idx = nullptr;
std::mutex disk_mu;
std::unordered_map<uint64_t, std::vector<DiskRecord>> disk_records;
std::atomic<int64_t> st_cache_hits{0};
// (local, RenoDX port: vk_renodx.h's, which make_module asks for a fragment module's replacement. None here: every module
// is the cache's own, so the counts below are of what the loader made)
const std::vector<uint32_t>* renodx_replacement(uint32_t) { return nullptr; }
#define PROF_EXACT() // (vk_prof.h's exact-timing scope, since #989: nothing to time here)
#include "cache_functions.inc"

View File

@@ -0,0 +1,110 @@
// Fixture of tests/test_registration_host_copies.py: gpu/vk/vk_bank.cpp's descriptor_ok, cut from the source, over a host that
// answers a GPU address as host/nvdrv.c's hos_gpu_ptr does with the native NVN layer (host/nvn_native.c's nvn_gpu_guest, cut
// from the source too) and a process whose memory VirtualQuery tells from a table. No pointer is read: the cases ask
// which memory a registration would be copied from.
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <string>
/* CONSTANTS */
// ---- the host: the guest window, and what it takes for a GPU address
struct nvn_vmap { uint64_t gpu, size; const uint64_t* page; };
static nvn_vmap pool; // (one virtual pool, in the case that maps it)
static const nvn_vmap* vmap_find(uint64_t gpu) {
return pool.size && gpu >= pool.gpu && gpu < pool.gpu + pool.size ? &pool : nullptr;
}
/* NVN_GPU_GUEST */
static uint8_t* guest_base() { return (uint8_t*)(uintptr_t)A64_HOST_BASE; }
static uint64_t guest_size() { return HOS_GUEST_SPACE; }
static uint8_t* gpu_ptr(uint64_t va, uint64_t* avail) { // (hos_gpu_ptr, the native NVN layer's branch)
const uint64_t g = nvn_gpu_guest(va);
if (!g || g >= guest_size()) return nullptr;
*avail = va < guest_size() ? guest_size() - g : VPAGE - g % VPAGE;
return guest_base() + g;
}
static const struct Host {
uint8_t* (*guest_base)();
uint64_t (*guest_size)();
uint8_t* (*gpu_ptr)(uint64_t, uint64_t*);
} host_table{guest_base, guest_size, gpu_ptr}, *host = &host_table;
// ---- the process's own memory, as VirtualQuery tells it
enum : uint32_t {
MEM_COMMIT = 0x1000, MEM_FREE = 0x10000,
PAGE_NOACCESS = 0x1, PAGE_READONLY = 0x2, PAGE_READWRITE = 0x4, PAGE_WRITECOPY = 0x8, PAGE_GUARD = 0x100,
};
struct MEMORY_BASIC_INFORMATION {
void* BaseAddress;
size_t RegionSize;
uint32_t State, Protect;
};
static struct Region { uintptr_t lo, hi; uint32_t protect; } region; // (one committed region, in the cases that have it)
static size_t VirtualQuery(const void* p, MEMORY_BASIC_INFORMATION* mi, size_t size) {
if (size < sizeof *mi) return 0;
const uintptr_t page = (uintptr_t)p & ~(uintptr_t)4095;
const bool in = (uintptr_t)p >= region.lo && (uintptr_t)p < region.hi;
*mi = {(void*)page, in ? region.hi - page : 4096, in ? MEM_COMMIT : MEM_FREE, in ? region.protect : PAGE_NOACCESS};
return sizeof *mi;
}
static unsigned refusals;
static void logf(const char* f, ...) {
refusals += std::strstr(f, "refused") != nullptr;
}
/* DESCRIPTOR_OK */
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "%s: failed: %s\n", name.c_str(), #x); return 1; } } while (0)
int main(int argc, char** argv) {
const std::string name = argc > 1 ? argv[1] : "";
const size_t n = 24; // (a registration's size: no case depends on it but "short")
const auto at = [](uint64_t a) { return (const void*)(uintptr_t)a; };
const auto commit = [](uint64_t lo, uint64_t size, uint32_t protect) { region = {(uintptr_t)lo, (uintptr_t)(lo + size), protect}; };
// (the address the handler committed while a Windows run handed over its kept calls: issue 1117's log)
const uint64_t heap_low = 0x9873eec;
if (name == "guest") { // the NVN layer's pointers: a guest object's host pointer, up to the window's last bytes
CHECK(descriptor_ok(guest_base() + 0x1040000000ull, n) == guest_base() + 0x1040000000ull);
CHECK(descriptor_ok(guest_base() + guest_size() - n, n) == guest_base() + guest_size() - n);
CHECK(descriptor_ok(guest_base() + guest_size() - n + 1, n) == nullptr);
} else if (name == "copy-in-guest-address-range") {
// a host copy whose address the host would take for a guest address: the copy is read, not guest memory there
CHECK(nvn_gpu_guest(heap_low) == heap_low);
commit(0x9870000, 0x20000, PAGE_READWRITE);
CHECK(descriptor_ok(at(heap_low), n) == at(heap_low));
CHECK(refusals == 0);
} else if (name == "copy-at-virtual-pool-address") {
// the same above 1 TB, where a heap address can equal a mapped virtual pool's GPU address
static const uint64_t pages[1] = {0x1040000000ull};
pool = {0x1e200000000ull, VPAGE, pages};
CHECK(nvn_gpu_guest(0x1e200000040ull) == 0x1040000040ull);
commit(0x1e200000000ull, 0x10000, PAGE_READWRITE);
CHECK(descriptor_ok(at(0x1e200000040ull), n) == at(0x1e200000040ull));
} else if (name == "copy-above-the-window") { // glibc's and a high-entropy Windows heap: no GPU address
CHECK(nvn_gpu_guest(0x7f1234567000ull) == 0);
commit(0x7f1234560000ull, 0x10000, PAGE_READWRITE);
CHECK(descriptor_ok(at(0x7f1234567000ull), n) == at(0x7f1234567000ull));
commit(0x7f1234560000ull, 0x10000, PAGE_READONLY);
CHECK(descriptor_ok(at(0x7f1234567000ull), n) == at(0x7f1234567000ull));
} else if (name == "nothing-there") { // an address no memory is at is refused, also where a guest address could be
CHECK(descriptor_ok(at(heap_low), n) == nullptr);
CHECK(descriptor_ok(at(0x7f1234567000ull), n) == nullptr);
CHECK(descriptor_ok(nullptr, n) == nullptr);
CHECK(refusals == 2);
} else if (name == "not-readable") {
commit(0x9870000, 0x20000, PAGE_READWRITE | PAGE_GUARD);
CHECK(descriptor_ok(at(heap_low), n) == nullptr);
commit(0x9870000, 0x20000, PAGE_NOACCESS);
CHECK(descriptor_ok(at(heap_low), n) == nullptr);
} else if (name == "short") { // the region ends inside the registration
commit(0x9870000, 0x4000, PAGE_READWRITE);
CHECK(descriptor_ok(at(0x9874000 - n), n) == at(0x9874000 - n));
CHECK(descriptor_ok(at(0x9874000 - n + 1), n) == nullptr);
} else {
std::fprintf(stderr, "unknown case %s\n", name.c_str());
return 2;
}
return 0;
}

View File

@@ -33,6 +33,7 @@ constexpr uint32_t VK_DRIVER_ID_AMD_PROPRIETARY = 1;
struct {
uint32_t sg32_stages = 0, subgroup_size = 32, subgroup_max = 32, driver_id = 0;
struct { struct { uint64_t minStorageBufferOffsetAlignment = 16; } limits; } props;
struct { bool gather_offset = false; } fixes; // (vk_vendor.h: make_profile reads the gather offset from it)
} dev;
using Shader::Stage;

View File

@@ -26,6 +26,60 @@ int main() {
CHECK("32-bit, aligned", at == 0x10008 && first == 0);
da::index_start(0x10004, 1, &at, &first);
CHECK("16-bit, aligned", at == 0x10004 && first == 0);
// (a set's joint and weight count in Push::formats: Link's four, five or six bones a vertex, two, and rigid parts)
const uint32_t five = da::count_code(4) << da::kCountShift | da::count_code(1) << (da::kCountShift + 2);
CHECK("four, then one", da::set_count(five, 0) == 4 && da::set_count(five, 1) == 1);
const uint32_t two = da::kByteJoints | da::count_code(2) << da::kCountShift;
CHECK("two, 8-bit joints", da::set_count(two, 0) == 2 && (two & da::kByteJoints));
CHECK("the count bits clear of the others",
!((da::count_code(2) << da::kCountShift | da::count_code(2) << (da::kCountShift + 2)) &
(da::kHalfPosition | da::kByteJoints | da::kByteJoints << 1 | da::kRigid)));
CHECK("no other counts", da::count_code(3) == 3 && da::set_count(3u << da::kCountShift, 0) == 0);
// capture motion: an indexed draw's vertices are its least index to its greatest (the restart index none of them)
uint32_t lo = 0, hi = 0;
const uint16_t strip[] = {7, 3, 9, 0xffff, 4, 12};
CHECK("16-bit, restart on", da::index_range(strip, 6, 1, true, &lo, &hi) && lo == 3 && hi == 12);
CHECK("16-bit, restart off: all ones is a vertex", da::index_range(strip, 6, 1, false, &lo, &hi) && lo == 3 && hi == 0xffff);
const uint8_t bytes[] = {200, 5, 255, 17};
CHECK("8-bit", da::index_range(bytes, 4, 0, true, &lo, &hi) && lo == 5 && hi == 200);
const uint32_t words[] = {100000, 99999, 0xffffffffu};
CHECK("32-bit", da::index_range(words, 3, 2, true, &lo, &hi) && lo == 99999 && hi == 100000);
const uint16_t cuts[] = {0xffff, 0xffff};
CHECK("only restarts: none", !da::index_range(cuts, 2, 1, true, &lo, &hi) && !da::index_range(strip, 0, 1, true, &lo, &hi));
CHECK("no such index type", !da::index_range(strip, 6, 3, true, &lo, &hi));
// its slots: from base vertex + least index, as many as the indices span; a vertex array's from its first, count of them
uint32_t n = 0;
CHECK("indexed", da::capture_span(1, 6, 1000, 3, 12, 1u << 20, &first, &n) && first == 1003 && n == 10);
CHECK("a vertex array", da::capture_span(da::kNoIndices, 36, 250, 0, 0, 1u << 20, &first, &n) && first == 250 && n == 36);
CHECK("too many", !da::capture_span(2, 3, 0, 0, 1u << 20, 1u << 20, &first, &n));
CHECK("nothing", !da::capture_span(da::kNoIndices, 0, 0, 0, 0, 1u << 20, &first, &n) &&
!da::capture_span(1, 6, 0, 5, 4, 1u << 20, &first, &n));
CHECK("a negative base vertex", da::capture_span(1, 3, -4, 6, 9, 64, &first, &n) && first == 2 && n == 4);
// a vertex's slot, as the stages take it: VertexIndex - first, unsigned, below the count
uint32_t slot = 0;
CHECK("the first and the last", da::capture_slot(1003, 1003, 10, &slot) && slot == 0 && da::capture_slot(1012, 1003, 10, &slot) &&
slot == 9);
CHECK("past the last", !da::capture_slot(1013, 1003, 10, &slot));
CHECK("before the first wraps past the count", !da::capture_slot(1002, 1003, 10, &slot));
CHECK("index 9 with base vertex -4: the last slot", da::capture_slot(-4 + 9, (uint32_t)(-4 + 6), 4, &slot) && slot == 3);
// the frame before's position without its jitter: the jitter is the projection's, x + jitter * w
float p[4] = {0.5f + 0.01f * 2.0f, -0.25f - 0.02f * 2.0f, 0.7f, 2.0f};
const float jitter[2] = {0.01f, -0.02f};
da::unjitter_position(p, jitter);
CHECK("unjittered", p[0] > 0.4999f && p[0] < 0.5001f && p[1] > -0.2501f && p[1] < -0.2499f && p[2] == 0.7f && p[3] == 2.0f);
CHECK("the push constants", sizeof(da::CapturePush) == 48 && sizeof(da::CapturePush) <= sizeof(da::Push));
// a draw's own camera's jitter: the projection's third column, negated; none from what is not a perspective projection
float proj[16] = {1.5f, 0, -0.0004f, 0, 0, 2.6f, 0.0007f, 0, 0, 0, -1.0002f, -0.2f, 0, 0, -1, 0};
float own[2] = {9, 9};
CHECK("a jittered projection", da::camera_jitter(proj, own) && own[0] == 0.0004f && own[1] == -0.0007f);
proj[2] = proj[6] = 0.0f;
CHECK("one without jitter", da::camera_jitter(proj, own) && own[0] == 0.0f && own[1] == 0.0f);
proj[14] = 0.0f, proj[15] = 1.0f;
CHECK("an orthographic one: not taken", !da::camera_jitter(proj, own));
proj[14] = -1.0f, proj[15] = 0.0f, proj[2] = 0.5f;
CHECK("an off-centre one: not a jitter", !da::camera_jitter(proj, own));
CHECK("the cameras' buffer holds its words", da::kJitterBeforeWord + 2 <= da::kHeadWords && da::kToleranceWord < da::kJitterBeforeWord);
std::puts("dlss_actor: ok");
return 0;
}

View File

@@ -1,6 +1,8 @@
"""NVIDIA DLSS's actor motion (gpu/vk/vk_dlss.cpp, gpu/vk/dlss_actor.h): the CPU side's draw names and index addresses
(tests/test_dlss_actor.cpp), the push constants of shaders/dlss_actor.vert and .frag against dlss_actor::Push, and the
SPIR-V headers of the DLSS shaders made from their sources as they are now (when glslangValidator is here). No game or GPU.
"""The temporal upscaler's own motion (gpu/vk/vk_temporal.cpp, gpu/vk/dlss_actor.h): the CPU side's draw names, index
addresses, index ranges and capture slots (tests/test_dlss_actor.cpp), the push constants of shaders/dlss_actor.vert against
dlss_actor::Push and of shaders/dlss_capture.vert against dlss_actor::CapturePush, dlss_actor.frag reading its tolerance from
the cameras' buffer (the two vertex stages push their own), and the SPIR-V headers of the DLSS shaders made from their sources
as they are now (when glslangValidator is here). No game or GPU.
python -I -B tests/test_dlss_actor.py [--cxx g++]
"""
@@ -25,7 +27,7 @@ def fields(text, start, end):
decl = decl.strip()
if not decl:
continue
names = re.sub(r"^\s*(uint64_t|uint32_t|int32_t|uint|int)\s+", "", decl)
names = re.sub(r"^\s*(uint64_t|uint32_t|int32_t|uint|int|float|vec2)\s+", "", decl)
out += [n.strip() for n in names.split(",")]
return out
@@ -38,25 +40,51 @@ def main():
want = fields(header, "struct Push {", "};")
if len(want) != 16:
raise SystemExit("dlss_actor::Push: %d fields read: %s" % (len(want), want))
for name in ("dlss_actor.vert", "dlss_actor.frag"):
got = fields((SHADERS / name).read_text(encoding="utf-8"), "uniform P {", "} pc;")
if got != want:
raise SystemExit("%s: its push constants %s are not dlss_actor::Push's %s" % (name, got, want))
got = fields((SHADERS / "dlss_actor.vert").read_text(encoding="utf-8"), "uniform P {", "} pc;")
if got != want:
raise SystemExit("dlss_actor.vert: its push constants %s are not dlss_actor::Push's %s" % (got, want))
want_capture = fields(header, "struct CapturePush {", "};")
capture = (SHADERS / "dlss_capture.vert").read_text(encoding="utf-8")
got = fields(capture, "uniform P {", "} pc;")
want_capture = [n.replace("[2]", "") for n in want_capture] # (float x[2] there is vec2 x here)
if got != want_capture or len(want_capture) != 8:
raise SystemExit("dlss_capture.vert: its push constants %s are not dlss_actor::CapturePush's %s" % (got, want_capture))
# (the cameras' buffer's words, as dlss_actor.h names them)
words = {n: int(v) for n, v in re.findall(r"(k\w+Word) = (\d+)", header)}
if words.get("kToleranceWord") != 66 or words.get("kJitterBeforeWord") != 68:
raise SystemExit("dlss_actor.h: the tolerance and the frame before's jitter are not words 66 and 68: %s" % words)
frag = (SHADERS / "dlss_actor.frag").read_text(encoding="utf-8")
if "push_constant" in frag or "data.w[66]" not in frag:
raise SystemExit("dlss_actor.frag: its tolerance is to be the cameras' buffer's word 66, not a push constant")
if "pc.jitter_before * then.w" not in capture or "jitter = pc.jitter_now" not in capture:
raise SystemExit("dlss_capture.vert: the draw's own jitters are to be used (pushed), not the scene's")
if "flat in vec2 jitter" not in frag:
raise SystemExit("dlss_actor.frag: this frame's jitter is to come from the vertex stage (the draw's own camera's)")
chain = (ROOT / "gpu" / "vk" / "vk_temporal.cpp").read_text(encoding="utf-8")
if "using ActorPush = dlss_actor::Push;" not in chain or "dlss_actor::index_start(" not in chain:
raise SystemExit("vk_temporal.cpp does not use dlss_actor.h")
for use in ("dlss_actor::CapturePush", "dlss_actor::index_range(", "dlss_actor::capture_span(", "dlss_actor::kToleranceWord",
"dlss_actor::kJitterBeforeWord", "dlss_actor::camera_jitter("):
if use not in chain:
raise SystemExit("vk_temporal.cpp does not use %s" % use)
glslang = shutil.which("glslangValidator")
with tempfile.TemporaryDirectory(prefix="dlss-actor-") as tmp:
if glslang:
for src in ("dlss_actor.vert", "dlss_actor.frag", "dlss_prep.comp", "dlss_out.comp"):
var = src.replace(".", "_").upper() + "_SPV"
out = Path(tmp) / (src.replace(".", "_") + "_spv.h")
subprocess.run([glslang, "-V", "--target-env", "vulkan1.3", "--vn", var, "-o", str(out), str(SHADERS / src)],
check=True, capture_output=True)
# (the two passes' -DHDR variants, for an RGBA16F scene: the same sources)
for src, hdr in (("dlss_actor.vert", False), ("dlss_actor.frag", False), ("dlss_capture.vert", False),
("dlss_prep.comp", False),
("dlss_out.comp", False), ("dlss_merge.comp", False), ("dlss_prep.comp", True),
("dlss_out.comp", True)):
stem = src.replace(".", "_hdr_" if hdr else "_")
var = stem.upper() + "_SPV"
out = Path(tmp) / (stem + "_spv.h")
define = ["-DHDR"] if hdr else []
subprocess.run([glslang, "-V", "--target-env", "vulkan1.3"] + define + ["--vn", var, "-o", str(out),
str(SHADERS / src)], check=True, capture_output=True)
words = lambda t: re.findall(r"0x[0-9a-f]{8}", t)
if words(out.read_text()) != words((SHADERS / out.name).read_text()):
raise SystemExit("shaders/%s is not made from %s as it is now (glslangValidator -V --target-env vulkan1.3 "
"--vn %s -o %s %s)" % (out.name, src, var, out.name, src))
"%s--vn %s -o %s %s)" % (out.name, src, "-DHDR " if hdr else "", var, out.name, src))
else:
print("glslangValidator not found: the SPIR-V headers not checked")
exe = Path(tmp) / "dlss-actor"

View File

@@ -84,6 +84,7 @@ 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; }
@WRITES_MEMORY@
void decision(const Program* cp, const Targets& at) {
NvnState s; DrawArgs d; int cmd = 0, topo = 0; unsigned char types[8]{};
@BLOCK@
@@ -135,13 +136,16 @@ def main():
args = parser.parse_args()
src = (args.source / "gpu/vk/vk_draw.cpp").read_text(encoding="utf-8")
try:
helper_start = src.index("static bool writes_memory(const Program& p)")
helper_end = src.index("enum class FallbackRole", helper_start)
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
fixture = FIXTURE.replace("@WRITES_MEMORY@", src[helper_start:helper_end]).replace("@BLOCK@", src[start:end])
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")
cpp.write_text(fixture, 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

View File

@@ -0,0 +1,104 @@
"""A mesh fallback's depth-only draws are held while the colour draw of the same culled list waits for its build
(gpu/vk/vk_draw.cpp): the draw roles, RX_VK_FALLBACK_DEPTH_HOLD=0, the hold's 120-frame release and draw()'s order."""
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 <cstdint>
#include <cstdio>
#include <cstdlib>
#include <mutex>
#include <unordered_map>
#include <vector>
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; };
struct Program { uint32_t mask = 0; std::atomic<int> compute_state{0}; std::vector<Stage> stages; };
struct NvnState { struct { bool dw = false; } ds; };
struct Targets { int n = 0; const void* color[1] = {}; const void* depth = nullptr; };
struct DrawArgs { uint64_t indirect = 0; };
uint32_t target_write_mask(const Program& p, const NvnState&, int) { return p.mask; }
static uint64_t frame = 10;
uint64_t texture_frame_no() { return frame; }
@HELPERS@
#define CHECK(c) if (!(c)) return std::printf("FAIL line %d: %s\n", __LINE__, #c), 1
int main(int argc, char**) {
using R = FallbackRole;
int t = 0;
Program colour{0xf}, depth{0};
NvnState writes{{true}}, tests{{false}};
Targets both{1, {&t}, &t}, depth_only{0, {}, &t};
DrawArgs listed{0x1000}, bare{0};
if (argc > 1) { CHECK(fallback_role_of(depth, writes, both, listed) == R::None); return 0; } // switch off
CHECK(fallback_role_of(colour, tests, both, listed) == R::Colour);
CHECK(fallback_role_of(depth, writes, both, listed) == R::DepthOnly);
CHECK(fallback_role_of(depth, writes, depth_only, listed) == R::DepthOnly);
CHECK(fallback_role_of(depth, tests, both, listed) == R::None);
CHECK(fallback_role_of(depth, writes, both, bare) == R::None);
Program writer;
writer.stages.resize(1);
writer.stages[0].info.stores_global_memory = true;
CHECK(writes_memory(writer) && fallback_role_of(writer, writes, both, listed) == R::None);
writer.stages[0].info = {};
writer.stages[0].info.storage_buffers_descriptors.push_back({true});
CHECK(writes_memory(writer) && fallback_role_of(writer, writes, both, listed) == R::None);
writer.stages[0].info = {};
writer.stages[0].info.image_buffer_descriptors.push_back({true});
CHECK(writes_memory(writer) && fallback_role_of(writer, writes, both, listed) == R::None);
writer.stages[0].info = {};
writer.stages[0].info.image_descriptors.push_back({true});
CHECK(writes_memory(writer) && fallback_role_of(writer, writes, both, listed) == R::None);
CHECK(!fallback_list_held(0x1000));
fallback_list_hold(0x1000, true);
CHECK(fallback_list_held(0x1000) && !fallback_list_held(0x2000));
frame = 130; CHECK(fallback_list_held(0x1000));
frame = 131; CHECK(!fallback_list_held(0x1000));
fallback_list_hold(0x1000, true);
fallback_list_hold(0x1000, false);
CHECK(!fallback_list_held(0x1000));
return 0;
}
"""
STEPS = [ # draw(): hold depth, then hold colour only for a failed record whose compute build is still running
"if (fallback_role == FallbackRole::DepthOnly && fallback_list_held(d.indirect)) SKIP_HERE();",
"if (p.fallback && d.mesh && !fallback_record(cmd, p, d)) {",
"if (fallback_role == FallbackRole::Colour && p.compute_state.load(std::memory_order_acquire) == 1)",
"fallback_list_hold(d.indirect, true);",
"if (fallback_role == FallbackRole::Colour) fallback_list_hold(d.indirect, false);",
]
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="g++")
cxx = parser.parse_args().cxx
src = (ROOT / "gpu/vk/vk_draw.cpp").read_text(encoding="utf-8")
draw = src.index("void draw(VkCommandBuffer cmd, NvnState& s, const DrawArgs& d) {")
helpers = src[src.index("static bool writes_memory"):draw]
at = draw
for step in STEPS:
at = src.index(step, at) # (ValueError: missing, or out of order)
with tempfile.TemporaryDirectory(prefix="depth-hold-") as tmp:
cpp, exe = Path(tmp) / "t.cpp", Path(tmp) / "t.exe"
cpp.write_text(FIXTURE.replace("@HELPERS@", helpers), encoding="utf-8")
subprocess.run([cxx, "-std=c++20", "-Wall", "-Wno-unused-function", str(cpp), "-o", str(exe)], check=True)
env = {k: v for k, v in os.environ.items() if k != "RX_VK_FALLBACK_DEPTH_HOLD"}
subprocess.run([str(exe)], check=True, timeout=10, env=env)
subprocess.run([str(exe), "off"], check=True, timeout=10, env=dict(env, RX_VK_FALLBACK_DEPTH_HOLD="0"))
print("PASS: fallback depth hold")
if __name__ == "__main__":
main()

188
tests/test_handle_table.py Normal file
View File

@@ -0,0 +1,188 @@
# SPDX-License-Identifier: GPL-3.0-or-later
"""A texture handle read from a table of records in a loop gets one texture per element.
Ocarina of Time's water and wet floors light each pixel with the lights of its screen tile (clustered lighting). A
projected light's shadow map handle is a word of the light's record, read at `table + index * 96 + 96`, with the index
from the tile's light list. No handle program can follow that for a whole draw, and the translation used to fall back
to the first constant word on the handle's path, the light buffer's address: every light then sampled the dummy and
its light stopped at the tile's edges, the squares on the water (#2509).
The texture pass now builds such a handle as a table: the element index becomes `HandleOp::Index` in the handle
program, the texture an array the shader indexes by it, and the draw evaluates the program once per element.
This check compiles the production `HandleOp` enum and `EvaluateHandleProgram` with a synthetic storage buffer and runs
the program the pass builds for the water's record address, in both forms the game's shaders use (a plain multiply,
and an XMAD pair's 16-bit halves of one). It also reads the pass and the draw for the parts that make the table:
the index search, the array's budget counted once, the element passed to the handle, and the bounded storage reads.
python -I -B tests/test_handle_table.py [--cxx c++]
"""
import argparse
import os
from pathlib import Path
import subprocess
import tempfile
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
def block(text, start_marker, end_marker):
start = text.index(start_marker)
end = text.index(end_marker, start)
return text[start:end]
def function(text, signature):
# (the definition: a declaration of the same name, ended by ';' before any '{', is skipped)
start = text.index(signature)
while text.index(';', start) < text.index('{', start):
start = text.index(signature, start + 1)
brace = text.index('{', start)
depth = 1
end = brace + 1
while depth:
depth += (text[end] == '{') - (text[end] == '}')
end += 1
return text[start:end]
def check_sources(root):
passes = (root / 'gpu/vk/eden/shader_recompiler/ir_opt/texture_pass.cpp').read_text(encoding='utf-8')
shader = (root / 'gpu/vk/vk_shader.cpp').read_text(encoding='utf-8')
draw = (root / 'gpu/vk/vk_draw.cpp').read_text(encoding='utf-8')
info = (root / 'gpu/vk/eden/shader_recompiler/shader_info.h').read_text(encoding='utf-8')
ops = block(info, 'enum class HandleOp', '};')
names = [line.strip().split(',')[0].split()[0] for line in ops.splitlines()[1:] if line.strip() and not line.strip().startswith('//')]
# (programs are kept in hookshot_programs.bin as raw words: an operation added anywhere but at the end renumbers
# the ones after it)
assert names[-1] == 'Index', f'HandleOp::Index must be the last operation, not {names[-1]}'
make = function(passes, 'TextureInst MakeInst(')
table = make.index('FindHandleTableIndex(')
legacy = make.index('IR::BreadthFirstSearch(')
assert table < legacy, 'the table is not tried before the search through loads'
assert 'RX_VK_HANDLE_TABLE' in make, 'the switch back to the search is gone'
assert 'g_index_operand = ' in make and 'g_index_operand = nullptr' in make, 'the index is not reset after the build'
assert 'ShiftLeftLogical(' in make and 'DESCRIPTOR_SIZE_SHIFT' in make, \
"the shader's array index is not the element index as a descriptor offset"
node = function(passes, 'bool BuildHandleNode(')
assert 'inst == g_index_operand' in node and 'Op::Index' in node, 'the index is not built as HandleOp::Index'
assert 'CompositeExtractU32x4' in node, 'a word of a 128-bit load is not a handle'
count = function(passes, 'u32 DynamicSampledTextureArrayCount(')
assert 'handle_program' in count, \
"a table's samples are counted once each: the water's 69 cut its array to 14 elements"
of = function(draw, 'template <class D>\nuint32_t handle_of(')
assert 'stage_handle(s, st, d.handle_program, k)' in of, 'element k does not read record k'
stage = function(shader, 'uint32_t stage_handle(')
assert 'element' in stage and 'd.cbuf_offset + 8' in stage, \
"a table's elements past the storage buffer's bound size are read"
for kind in ('compute', 'vertex', 'fragment', 'task'):
assert f'note_fallbacks(p, "{kind}"' in shader, f'the {kind} stage does not log the handles not followed'
return info
def run_programs(info, cxx):
enum = block(info, 'enum class HandleOp', '};') + '};'
evaluate = function(info, 'template <typename ReadCbuf, typename ReadStorage>\nstd::optional<u32> EvaluateHandleProgram(')
source = r'''
#include <array>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <optional>
#include <span>
#include <vector>
using u32 = std::uint32_t;
using s32 = std::int32_t;
namespace Shader {
''' + enum + '\n' + evaluate + r'''
}
using Shader::HandleOp;
// A light buffer at storage binding 1: 96-byte records after a 96-byte head, word 0 of record i its shadow map
// handle (the water's layout at Zora's River: handles 0x1a902a3c and 0x1a904290 for lights 12 and 13)
static std::vector<u32> lights(4096 / 4);
static u32 cbuf(u32 index, u32 offset) { return index == 0 && offset == 1312 ? 0x3b75a040u : 0; }
static u32 storage(u32 binding, u32 offset) {
if (binding != 1 || offset + 4 > lights.size() * 4) return 0;
return lights[offset / 4];
}
static std::vector<u32> op(std::vector<u32> code, HandleOp o) { code.push_back(u32(o)); return code; }
int main() {
for (u32 i = 0; i < 32; i++) lights[(96 + i * 96) / 4] = 0x1a900000u + 0x100u * i;
// Plain multiply: c0[1312] + index * 96 + 96 - (c0[1312] & ~15)
std::vector<u32> plain{u32(HandleOp::Cbuf), 0, 1312, u32(HandleOp::Index), u32(HandleOp::Immediate), 96};
plain = op(plain, HandleOp::Mul);
plain = op(plain, HandleOp::Add);
plain.insert(plain.end(), {u32(HandleOp::Immediate), 96});
plain = op(plain, HandleOp::Add);
plain.insert(plain.end(), {u32(HandleOp::Cbuf), 0, 1312, u32(HandleOp::Immediate), 0xfffffff0u});
plain = op(plain, HandleOp::And);
plain = op(plain, HandleOp::Sub);
plain.insert(plain.end(), {u32(HandleOp::Storage), 1});
// XMAD pair: (index & 0xffff) * 96 + (((index >> 16) & 0xffff) * 96 << 16) + 96
std::vector<u32> xmad{u32(HandleOp::Index), u32(HandleOp::Immediate), 0xffff};
xmad = op(xmad, HandleOp::And);
xmad.insert(xmad.end(), {u32(HandleOp::Immediate), 96});
xmad = op(xmad, HandleOp::Mul);
xmad.insert(xmad.end(), {u32(HandleOp::Index), u32(HandleOp::Immediate), 16});
xmad = op(xmad, HandleOp::Shr);
xmad.insert(xmad.end(), {u32(HandleOp::Immediate), 0xffff});
xmad = op(xmad, HandleOp::And);
xmad.insert(xmad.end(), {u32(HandleOp::Immediate), 96});
xmad = op(xmad, HandleOp::Mul);
xmad.insert(xmad.end(), {u32(HandleOp::Immediate), 16});
xmad = op(xmad, HandleOp::Shl);
xmad = op(xmad, HandleOp::Add);
xmad.insert(xmad.end(), {u32(HandleOp::Immediate), 96});
xmad = op(xmad, HandleOp::Add);
xmad.insert(xmad.end(), {u32(HandleOp::Storage), 1});
int failures = 0;
for (const auto* code : {&plain, &xmad}) {
for (u32 k = 0; k < 32; k++) {
const auto h = Shader::EvaluateHandleProgram(std::span<const u32>(*code), cbuf, storage, k);
const u32 want = 0x1a900000u + 0x100u * k;
if (!h || *h != want) {
std::cerr << (code == &plain ? "plain" : "xmad") << " element " << k << ": " << std::hex
<< (h ? *h : 0xdeadu) << " != " << want << "\n";
failures++;
}
}
}
// Without an element, a program evaluates as before: element 0
const auto zero = Shader::EvaluateHandleProgram(std::span<const u32>(plain), cbuf, storage);
if (!zero || *zero != 0x1a900000u) { std::cerr << "default element is not 0\n"; failures++; }
if (failures) return 1;
std::cout << "ok\n";
return 0;
}
'''
with tempfile.TemporaryDirectory() as tmp:
src = os.path.join(tmp, 'handle_table.cpp')
exe = os.path.join(tmp, 'handle_table.exe' if os.name == 'nt' else 'handle_table')
with open(src, 'w', encoding='utf-8') as f:
f.write(source)
built = subprocess.run([cxx, '-std=c++20', '-O1', src, '-o', exe], capture_output=True, text=True)
if built.returncode:
raise SystemExit('compile failed:\n' + built.stderr[-4000:])
ran = subprocess.run([exe], capture_output=True, text=True)
if ran.returncode:
raise SystemExit('handle programs wrong:\n' + ran.stderr[-4000:])
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument('--source-root', type=Path, default=Path(__file__).resolve().parents[1])
p.add_argument('--cxx', default='c++')
a = p.parse_args()
info = check_sources(a.source_root)
run_programs(info, a.cxx)
print('PASS: a handle read from a table of records in a loop gets one texture per element')
if __name__ == '__main__':
main()

View File

@@ -139,7 +139,7 @@ def main():
"push_constants(cmd, p, nullptr, d.first_task);" not in draw or \
"LoadWord(ctx, BufferWordAt(ctx, base, ctx.Const(1U)))" not in isbe:
raise SystemExit("a task draw loses its direct or GPU-provided starting workgroup")
if "if (p.fallback && d.mesh && !fallback_record(cmd, p, d)) SKIP_HERE();" not in draw:
if "if (p.fallback && d.mesh && !fallback_record(cmd, p, d)) {" not in draw:
raise SystemExit("the fallback dispatches do not run before the pass")
if "vkCmdDrawIndirect(cmd, fallback_draw_records, 0, 1, sizeof(VkDrawIndirectCommand));" not in draw:
raise SystemExit("the fallback is not drawn from the records' header as an indirect draw")

View File

@@ -40,7 +40,7 @@ static uint32_t handles[3]{};
static uint8_t answers[4]{0,2,2,2};
uint32_t driver_bank_word(const NvnState&, int, int, uint32_t offset) { return handles[offset/8]; }
uint32_t stage_cbuf_word(const NvnState&, const Stage&, uint32_t, uint32_t offset) { return handles[offset/8]; }
uint32_t stage_handle(const NvnState&, const Stage&, uint32_t) { std::abort(); }
uint32_t stage_handle(const NvnState&, const Stage&, uint32_t, uint32_t) { std::abort(); }
uint8_t texture_answer(uint32_t h) { return h ? answers[h] : 0xff; }
uint32_t texture_slot_version(int id) { return 10+id; }
#include MATCHER_FUNCTIONS

View File

@@ -41,7 +41,10 @@ class CacheModuleCleanupTests(unittest.TestCase):
header = (ROOT / 'gpu/vk/vk_shader.h').read_text(encoding='utf-8')
structs = '\n'.join(block(header, marker) + ';' for marker in ('struct Stage {', 'struct Program {'))
structs += '\n' + block(source, 'struct DiskRecord {') + ';'
functions = '\n'.join(block(source, marker) for marker in (
# (local, RenoDX port: make_module names each module by its CRC-32 and asks vk_renodx.h for a replacement, which the
# test's stub never has. An older --source has no spirv_crc32)
crc = ('uint32_t spirv_crc32(',) if 'uint32_t spirv_crc32(' in source else ()
functions = '\n'.join(block(source, marker) for marker in crc + (
'VkShaderModule make_module(', 'uint64_t answers_hash(', 'bool answers_fit(', 'void idx_put('))
serialization = source[source.index('void put_bytes('):source.index('void program_cache_save(')]
functions += '\n' + serialization + block(source, 'void program_cache_save(')

View File

@@ -0,0 +1,72 @@
"""A texture or sampler registration kept from before the renderer started is copied from the host's copy of it.
host/rx_backend.c keeps the registrations the game makes before the window's thread starts the renderer and hands them
over as pointers to its own copies; gpu/vk/vk_bank.cpp's descriptor_ok says which memory a registration is copied from.
It used to ask the host whether the pointer was a GPU address first. With the native NVN layer every address from 16 MB
to 512 GB is one (host/nvn_native.c nvn_gpu_guest), so a copy on a Windows heap there was read from untouched guest
memory instead: all zero (issue 1117: sampler 258 filtered nearest, texture slot 0x102 had no image).
Runs descriptor_ok, cut from the source, against nvn_gpu_guest, cut from the source, with the guest window's real
bounds."""
import argparse
from pathlib import Path
import re
import subprocess
import tempfile
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
ROOT = Path(__file__).resolve().parents[1]
CASES = ["guest", "copy-in-guest-address-range", "copy-at-virtual-pool-address", "copy-above-the-window", "nothing-there",
"not-readable", "short"]
def block(source, marker):
start = source.index(marker)
end = source.index("{", start) + 1
depth = 1
while depth:
depth += (source[end] == "{") - (source[end] == "}")
end += 1
return source[start:end]
def define(source, name):
"""The literal a header gives a name (HOS_SPACE_END has a second, web-only definition that is a variable)"""
found = re.findall(r"^#define %s (\(?[0-9][0-9A-Za-z<> ]*\)?)" % name, source, re.M)
assert len(found) == 1, (name, found)
return "#define %s %s" % (name, found[0])
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("--source", type=Path, default=ROOT)
p.add_argument("--cxx", default="g++")
a = p.parse_args()
read = lambda path: (a.source / path).read_text(encoding="utf-8")
bank, native, hos, a64 = (read(path) for path in ["gpu/vk/vk_bank.cpp", "host/nvn_native.c", "host/hos.h",
"third_party/a64-kit/rt/a64.h"])
constants = "\n".join([define(hos, "HOS_SYSTEM_BASE"), define(hos, "HOS_SPACE_END"), define(hos, "HOS_GUEST_SPACE"),
define(native, "VBASE"), define(native, "VPAGE"), define(a64, "A64_HOST_BASE")])
# (the registrations' entries all go through descriptor_ok: one that copied from its pointer unchecked would not)
replay = read("gpu/vk/vk_replay.cpp")
for source, marker in [(bank, "void register_texture(int id"), (bank, "void register_sampler(int id"),
(replay, "void register_texture_queued(int id"), (replay, "void register_sampler_queued(int id")]:
assert "descriptor_ok(" in block(source, marker), marker
fixture = (ROOT / "tests/registration_host_copies_test.cpp").read_text(encoding="utf-8")
for marker, text in [("/* CONSTANTS */", constants), ("/* NVN_GPU_GUEST */", block(native, "uint64_t nvn_gpu_guest(uint64_t gpu) {")),
("/* DESCRIPTOR_OK */", block(bank, "const void* descriptor_ok(const void* p, size_t n) {"))]:
assert fixture.count(marker) == 1, marker
fixture = fixture.replace(marker, text)
with tempfile.TemporaryDirectory(prefix="registration-host-copies-") as temp:
cpp, exe = Path(temp) / "fixture.cpp", Path(temp) / "fixture.exe"
cpp.write_text(fixture, encoding="utf-8")
subprocess.run([a.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", str(cpp), "-o", str(exe)], check=True)
for case in CASES:
subprocess.run([str(exe), case], check=True, timeout=20)
print(f"{len(CASES)} registration pointer cases passed")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,113 @@
"""scripts/renodx_shaders.py: its pinned tables, the SHA-256 checks and when the shaders are compiled again, without the
network or the Slang compiler; and gpu/vk/vk_renodx.cpp's two halves (with RenoDX's shaders, and the stubs without).
Run with: python -I tests/test_renodx_shaders.py
"""
import hashlib
import importlib.util
import io
import os
from pathlib import Path
import re
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
spec = importlib.util.spec_from_file_location("renodx_shaders_under_test", ROOT / "scripts" / "renodx_shaders.py")
renodx = importlib.util.module_from_spec(spec)
spec.loader.exec_module(renodx)
def digest(data):
return hashlib.sha256(data).hexdigest()
class Opener:
"""urlopen over a dict of url -> bytes; records what was asked for"""
def __init__(self, contents):
self.contents, self.asked = contents, []
def __call__(self, url, timeout=None):
if timeout != renodx.TIMEOUT:
raise AssertionError("urlopen without the timeout (%r)" % (timeout,))
self.asked.append(url)
return io.BytesIO(self.contents[url])
class TableTests(unittest.TestCase):
def test_pinned(self):
self.assertRegex(renodx.COMMIT, r"^[0-9a-f]{40}$")
self.assertIn(renodx.COMMIT, renodx.BASE)
for path, sha in renodx.FILES:
self.assertRegex(sha, r"^[0-9a-f]{64}$", path)
self.assertFalse(path.startswith("/") or ".." in path.split("/"), path)
for platform, (url, sha, compiler) in renodx.SLANG.items():
self.assertRegex(sha, r"^[0-9a-f]{64}$", platform)
self.assertIn(renodx.SLANG_VERSION, url)
self.assertTrue(url.startswith("https://github.com/shader-slang/slang/releases/download/"), url)
self.assertTrue(compiler.startswith("bin/slangc"), compiler)
def test_every_shader_is_fetched(self):
paths = {p for p, _ in renodx.FILES}
self.assertIn("LICENSE", paths)
for _, name, kind, _ in renodx.SHADERS:
self.assertIn(renodx.GAME + "/" + name, paths)
self.assertIn(kind, (0, 1, 2))
self.assertEqual(len({crc for crc, *_ in renodx.SHADERS}), len(renodx.SHADERS))
class FetchTests(unittest.TestCase):
def test_wrong_file_is_not_kept(self):
with tempfile.TemporaryDirectory() as tmp:
dst = os.path.join(tmp, "a", "file")
with self.assertRaises(ValueError):
renodx.fetch_one("u", digest(b"expected"), dst, Opener({"u": b"something else"}))
self.assertFalse(os.path.exists(dst))
self.assertFalse(os.path.exists(dst + ".part"))
def test_a_file_already_there_is_not_asked_for(self):
with tempfile.TemporaryDirectory() as tmp:
dst = os.path.join(tmp, "file")
opener = Opener({"u": b"data"})
self.assertTrue(renodx.fetch_one("u", digest(b"data"), dst, opener))
self.assertFalse(renodx.fetch_one("u", digest(b"data"), dst, opener))
self.assertEqual(opener.asked, ["u"])
def test_a_current_header_downloads_and_compiles_nothing(self):
with tempfile.TemporaryDirectory() as tmp:
self.assertFalse(renodx.current(tmp))
with open(renodx.header(tmp), "w", encoding="utf-8") as f:
f.write("// renodx_shaders.py 0000000000000000\n") # (another script's: made again)
self.assertFalse(renodx.current(tmp))
with open(renodx.header(tmp), "w", encoding="utf-8") as f:
f.write(renodx.stamp() + "\n")
self.assertTrue(renodx.current(tmp))
opener = Opener({})
self.assertEqual(renodx.prepare(tmp, "windows", opener), renodx.header(tmp))
self.assertEqual(opener.asked, [])
class SourceTests(unittest.TestCase):
def test_the_renderer_builds_without_the_shaders(self):
source = (ROOT / "gpu" / "vk" / "vk_renodx.cpp").read_text(encoding="utf-8")
with_shaders, stubs = source.split("#else", 1)
self.assertIn('#ifdef HOOKSHOT_RENODX', with_shaders)
self.assertLess(with_shaders.index("#ifdef HOOKSHOT_RENODX"), with_shaders.index('#include "renodx_spv.h"'))
header = (ROOT / "gpu" / "vk" / "vk_renodx.h").read_text(encoding="utf-8")
declared = set(re.findall(r"^(?:[\w:<>*&]+ )+\*?(renodx_\w+)\(", header, re.M))
self.assertTrue(declared)
for name in declared: # (every function of the header is in both halves)
self.assertIn(name + "(", with_shaders, name)
self.assertIn(name + "(", stubs, name)
self.assertIn("bool renodx_built() { return false; }", stubs)
def test_the_compiled_shaders_are_never_committed(self):
ignored = (ROOT / ".gitignore").read_text(encoding="utf-8").split("\n")
self.assertIn(renodx.HEADER, ignored)
self.assertFalse((ROOT / "gpu" / "vk" / renodx.HEADER).exists(),
"gpu/vk/renodx_spv.h is in the tree: the build takes HOOKSHOT_RENODX's, and this one would be included first")
if __name__ == "__main__":
unittest.main()

View File

@@ -39,6 +39,9 @@ static const char *fixture_env(const char *name) {
return override;
}
#define getenv fixture_env
/* (local, RenoDX port: host/nn_vi.c's, whether the scene's colour target is RGBA16F for the run) */
static int scene_rgba16f;
int hos_scene_rgba16f(void) { return scene_rgba16f; }
''' + body + r'''
#define WIDE(w, h) ((uint64_t)(h) << 32 | (w))
static const struct {
@@ -74,6 +77,13 @@ int main(void) {
failed = 1;
}
}
/* (local, RenoDX port) an RGBA16F scene: 128 MiB more, but not over an explicit HOS_RT_POOL_MB */
scene_rgba16f = 1;
output_width = 2560; output_height = 1440; hos_wide_out = 0;
override = NULL;
if (hos_rt_pool_size(305 * MIB) != (512 + 128) * MIB) { fprintf(stderr, "RGBA16F scene: not 640 MiB\n"); failed = 1; }
override = "256";
if (hos_rt_pool_size(305 * MIB) != 256 * MIB) { fprintf(stderr, "RGBA16F scene: the override not kept\n"); failed = 1; }
return failed;
}
'''
@@ -87,7 +97,8 @@ int main(void) {
source.write_text(fixture)
subprocess.run([compiler, "-std=c11", "-O2", str(source), "-o", str(binary)], check=True)
subprocess.run([str(binary)], check=True, timeout=5)
print("PASS: 512 MiB floor, 4K/ultrawide growth and explicit overrides, the 5K output's 900 MiB (14 cases)")
print("PASS: 512 MiB floor, 4K/ultrawide growth and explicit overrides, the 5K output's 900 MiB (14 cases), "
"an RGBA16F scene's 128 MiB more")
if __name__ == "__main__":

View File

@@ -110,6 +110,7 @@ class ShaderStampCacheTests(unittest.TestCase):
"NGX_SDK": ngx, # ("": no NVIDIA NGX SDK, vk_dlss.cpp built without DLSS, as by default)
"NGX_MSVC_RT": "vk_ngx_msvc_rt.cpp",
"FSR": fsr, # (HOOKSHOT_FSR=1: vk_fsr4.cpp built with AMD FSR 4)
"RENODX": "", # (no HOOKSHOT_RENODX: vk_renodx.cpp built without RenoDX's shaders, as by default)
}
with mock.patch.dict(os.environ, {"RX_RELEASE": "1"} if release else {}, clear=True), warnings.catch_warnings():
# The production key/header helpers use short-lived open(...).read() expressions.

View File

@@ -0,0 +1,42 @@
"""The GPUs whose task + mesh draws take the compute fallback by default (gpu/vk/vk_main.cpp, issue 996).
On AMD's Windows driver the native task + mesh path timed out the driver on Navi 22 (#1747) and on Navi 21: an RX 6800 XT lost
the device within minutes at Hyrule Castle's entrance, and played 25 minutes there with none through the fallback. This checks
that both chips are in the default, only for AMD and only on Windows, and that RX_VK_TASK_MESH_FALLBACK still decides over it.
python -I -B tests/test_task_mesh_fallback_devices.py
"""
from pathlib import Path
import re
ROOT = Path(__file__).resolve().parents[1]
DEVICES = {
0x73BF: "Navi 21 (RX 6800, 6800 XT, 6900 XT)",
0x73AF: "Navi 21 (RX 6900 XT, XTXH)",
0x73A5: "Navi 21 (RX 6950 XT)",
0x73DF: "Navi 22 (RX 6700 XT)",
}
def main():
main_cpp = (ROOT / "gpu/vk/vk_main.cpp").read_text(encoding="utf-8")
at = main_cpp.index("bool task_mesh_fallback = false;")
block = main_cpp[at:main_cpp.index("dev.task_mesh_fallback =", at)]
default = re.search(r"#ifdef _WIN32\n(.*?)#endif\n", block, re.S)
assert default, "the default is no longer Windows-only"
line = re.search(r"task_mesh_fallback = (.*?);", default.group(1), re.S)
assert line, "no default for task_mesh_fallback"
cond = line.group(1)
assert "dev.props.vendorID == 0x1002" in cond, "the default is not limited to AMD"
ids = {int(x, 16) for x in re.findall(r"dev\.props\.deviceID == (0x[0-9a-fA-F]+)", cond)}
for device, name in DEVICES.items():
assert device in ids, "%s (0x%04x) is not in the default" % (name, device)
assert ids == set(DEVICES), "unexpected devices in the default: %s" % sorted(hex(i) for i in ids - set(DEVICES))
# (the environment variable is read after the default, so it decides for any GPU)
override = block.index('getenv("RX_VK_TASK_MESH_FALLBACK")')
assert override > default.end(), "RX_VK_TASK_MESH_FALLBACK is read before the default and is overwritten by it"
print("PASS: task + mesh fallback by default on Windows for AMD's %s" % ", ".join(DEVICES.values()))
if __name__ == "__main__":
main()

View File

@@ -62,7 +62,7 @@ int recorded_reads=0,live_reads=0,vm_reads=0;
void ub_read(uint64_t addr,void* out,size_t n) { ++recorded_reads; std::memcpy(out,recorded+addr-4096,n); }
struct Host { void gpu_read(uint64_t addr,void* out,size_t n) { ++live_reads; std::memcpy(out,live_bytes+addr-4096,n); } } host_object;
Host* host=&host_object;
uint32_t stage_handle(const NvnState&,const Stage&,uint32_t program) { ++vm_reads; return 0xABC000U+program; }
uint32_t stage_handle(const NvnState&,const Stage&,uint32_t program,uint32_t element=0) { ++vm_reads; return 0xABC000U+program+0x1000U*element; }
'''
checks = r'''
int main() {
@@ -102,7 +102,7 @@ int main() {
st.nvn_stage=-1; all(3,0,0,"invalid user stage"); st.nvn_stage=2;
t={}; i={}; t.handle_program=i.handle_program=3;
const int before=recorded_reads+live_reads;
eq(handle_of(s,st,t,9),0xABC003,"VM array bypass");
eq(handle_of(s,st,t,9),0xABC003+0x9000,"VM array element 9 (a handle table: its record)");
eq(descriptor_handle(s,st,t),0xABC003,"VM texture bypass");
eq(descriptor_handle(s,st,i),0xABC003,"VM image bypass");
eq(vm_reads,3,"VM call count"); eq(recorded_reads+live_reads,before,"VM no ordinary reads");

171
tests/test_vendor_fixes.py Normal file
View File

@@ -0,0 +1,171 @@
"""Which GPUs get which vendor workaround (gpu/vk/vk_vendor.h), on made-up devices, and where the renderer uses them.
vendor_fixes() is compiled with the Vulkan headers and run on the properties of GPUs players have: NVIDIA's driver on
Windows and Linux (and NVK on the same cards), AMD's Windows driver in its two branches (RDNA 1/2: "AMD proprietary
shader compiler"; RDNA 3/4: LLPC), RADV, AMDVLK, Intel's Windows driver on an integrated GPU and on an Arc card, ANV,
llvmpipe. Each fix must be on for the GPUs it is for and off for every other, and NVIDIA's driver must get none: that is
what keeps the renderer and the translated shaders on NVIDIA as they were. The RX_VK_ switches force each one either way.
Then the source: the device's fixes are made from its properties at device creation, and the translator is told
fetch_bounds, nan_bits and the gather offset (and the program cache's stamp tells the three apart).
python -I -B tests/test_vendor_fixes.py --headers <Vulkan-Headers/include> [--cxx g++]
"""
import argparse
from pathlib import Path
import subprocess
import tempfile
RUN_ALL_ARGS = ["--cxx", "{cxx}", "--headers", "{vulkan_headers}"]
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu" / "vk"
# name, vendor, driver id, device type, driverInfo -> fetch_bounds, nan_bits, gather_offset
DEVICES = [
("RTX 3090, NVIDIA's Linux driver", 0x10DE, "NVIDIA_PROPRIETARY", "DISCRETE_GPU", "580.105.08", "000"),
("RTX 4070 laptop, NVIDIA's Windows driver", 0x10DE, "NVIDIA_PROPRIETARY", "DISCRETE_GPU", "581.57", "000"),
("GTX 1650, NVK", 0x10DE, "MESA_NVK", "DISCRETE_GPU", "Mesa 26.0.1", "000"),
("RX 6700 XT, AMD's Windows driver (RDNA 2 branch)", 0x1002, "AMD_PROPRIETARY", "DISCRETE_GPU",
"26.10.1 (AMD proprietary shader compiler)", "111"),
("RX 5700, AMD's Windows driver (RDNA 1 branch)", 0x1002, "AMD_PROPRIETARY", "DISCRETE_GPU",
"25.9.1 (AMD proprietary shader compiler)", "111"),
("RX 9070 XT, AMD's Windows driver (LLPC)", 0x1002, "AMD_PROPRIETARY", "DISCRETE_GPU", "26.10.1 (LLPC)", "101"),
("RX 7600, AMD's Windows driver, no driverInfo", 0x1002, "AMD_PROPRIETARY", "DISCRETE_GPU", "", "101"),
("AMD's driver naming another shader compiler", 0x1002, "AMD_PROPRIETARY", "DISCRETE_GPU", "26.10.1 (LLPC shader compiler)",
"101"),
("RX 580, RADV", 0x1002, "MESA_RADV", "DISCRETE_GPU", "Mesa 26.2.5", "101"),
("Steam Deck, RADV", 0x1002, "MESA_RADV", "INTEGRATED_GPU", "Mesa 25.1.0", "101"),
("RX 6800, AMDVLK", 0x1002, "AMD_OPEN_SOURCE", "DISCRETE_GPU", "2025.Q2.1 (LLPC)", "101"),
("UHD Graphics, Intel's Windows driver", 0x8086, "INTEL_PROPRIETARY_WINDOWS", "INTEGRATED_GPU", "101.7088", "101"),
("Iris Xe, Intel's Windows driver", 0x8086, "INTEL_PROPRIETARY_WINDOWS", "INTEGRATED_GPU", "101.7092", "101"),
("Arc B580, Intel's Windows driver", 0x8086, "INTEL_PROPRIETARY_WINDOWS", "DISCRETE_GPU", "101.8132", "101"),
("Iris Xe, ANV", 0x8086, "INTEL_OPEN_SOURCE_MESA", "INTEGRATED_GPU", "Mesa 26.0.1", "101"),
("llvmpipe", 0x10005, "MESA_LLVMPIPE", "CPU", "Mesa 26.0.1 (LLVM 20.1.8)", "100"),
]
FIELDS = ['fetch_bounds', 'nan_bits', 'gather_offset']
FIXTURE = r"""
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include "vk_vendor.h"
using namespace mkwvk;
static std::map<std::string, std::string> env;
static const char* get(const char* name) {
auto it = env.find(name);
return it == env.end() ? nullptr : it->second.c_str();
}
static void print(const char* what, const VendorFixes& x) {
std::printf("%s %d%d%d\n", what, x.fetch_bounds, x.nan_bits, x.gather_offset);
}
int main() {
struct D { const char* name; uint32_t vendor; VkDriverId driver; VkPhysicalDeviceType type; const char* info; };
const D devices[] = {
@@DEVICES@@
};
for (const D& d : devices) {
VendorFacts f;
f.vendor_id = d.vendor, f.driver_id = d.driver, f.device_type = d.type, f.driver_info = d.info;
VendorFixes x = vendor_fixes(f);
print(d.name, x);
vendor_fixes_env(x, get); // (no switch set: as it was)
print(d.name, x);
const char* names[] = {"RX_VK_FETCH_BOUNDS", "RX_VK_NAN_BITS", "RX_VK_GATHER_OFFSET"};
for (const char* n : names)
for (const char* v : {"0", "1", ""}) {
env.clear();
env[n] = v;
VendorFixes y = vendor_fixes(f);
vendor_fixes_env(y, get);
std::printf("%s=%s ", n, v);
print(d.name, y);
}
env.clear();
}
return 0;
}
"""
def cut(text, marker):
at = text.index(marker)
end = text.index("{", at) + 1
depth = 1
while depth:
depth += (text[end] == "{") - (text[end] == "}")
end += 1
return text[at:end]
def check_source():
main = (VK / "vk_main.cpp").read_text(encoding="utf-8")
at = main.index("dev.fixes = vendor_fixes(facts);")
for text in ("vendor_fixes_env(dev.fixes,",):
if main.find(text, at) < 0:
raise SystemExit("FAIL: vk_main.cpp: %s not after the fixes are made" % text)
if main.index("dev.fixes = vendor_fixes(facts);") > main.index("vkCreateDevice(dev.physical, &ci"):
raise SystemExit("FAIL: the fixes are made after the device")
for field in ("vendor_id = p2.properties.vendorID", "driver_id = drv.driverID", "device_type = p2.properties.deviceType",
"driver_info = drv.driverInfo"):
if "facts." + field not in main:
raise SystemExit("FAIL: vk_main.cpp does not fill facts.%s" % field)
coop = (VK / "vk_coopmat.cpp").read_text(encoding="utf-8")
for text in ("Shader::Backend::SPIRV::fetch_bounds = dev.fixes.fetch_bounds;", "Shader::Backend::SPIRV::nan_bits = dev.fixes.nan_bits;",
"Shader::Backend::SPIRV::fetch_bounds_buffers = dev.fixes.fetch_bounds && !dev.features.robustBufferAccess;"):
if text not in cut(coop, "void coopmat_init() {"):
raise SystemExit("FAIL: coopmat_init does not set %s" % text)
shader = (VK / "vk_shader.cpp").read_text(encoding="utf-8")
if "p.need_gather_subpixel_offset = dev.fixes.gather_offset;" not in cut(shader, "Shader::Profile make_profile() {"):
raise SystemExit("FAIL: make_profile does not set the gather offset")
stamp = cut(shader, "uint64_t disk_stamp() {") # (Triforce: the stamp's function has this name here)
for text in ("fetch_bounds ? 16u : 0u", "nan_bits ? 32u : 0u", "profile().need_gather_subpixel_offset ? 64u : 0u",
"fetch_bounds_buffers ? 128u : 0u"):
if text not in stamp:
raise SystemExit("FAIL: the program cache's stamp lacks %s" % text)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--headers", type=Path, required=True)
parser.add_argument("--cxx", default="g++")
args = parser.parse_args()
check_source()
rows = ",\n".join(' {"%s", 0x%X, VK_DRIVER_ID_%s, VK_PHYSICAL_DEVICE_TYPE_%s, "%s"}' % (n, v, d, t, i)
for n, v, d, t, i, _ in DEVICES)
with tempfile.TemporaryDirectory(prefix="vendor-fixes-") as tmp:
tmp = Path(tmp)
cpp, exe = tmp / "fixture.cpp", tmp / "fixture"
cpp.write_text(FIXTURE.replace("@@DEVICES@@", rows), encoding="utf-8")
r = subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Werror", "-I" + str(VK), "-I" + str(args.headers),
str(cpp), "-o", str(exe)], capture_output=True, text=True, timeout=300)
if r.returncode:
print(r.stdout + r.stderr)
raise SystemExit("the fixture did not build")
out = subprocess.run([str(exe)], capture_output=True, text=True, timeout=30, check=True).stdout.splitlines()
per_device = 2 + len(FIELDS) * 3
if len(out) != per_device * len(DEVICES):
raise SystemExit("FAIL: %d lines" % len(out))
failures = []
for k, (name, _, _, _, _, want) in enumerate(DEVICES):
lines = out[k * per_device:(k + 1) * per_device]
for line in lines[:2]:
if line.split()[-1] != want:
failures.append("%s: %s, expected %s" % (name, line.split()[-1], want))
for i, line in enumerate(lines[2:]):
switch, value = ["RX_VK_FETCH_BOUNDS", "RX_VK_NAN_BITS", "RX_VK_GATHER_OFFSET"][i // 3], ["0", "1", ""][i % 3]
got = list(line.split()[-1])
expect = list(want)
if value:
field = i // 3
expect[field] = "1" if value == "1" else "0"
if got != expect:
failures.append("%s with %s=%s: %s, expected %s" % (name, switch, value, "".join(got), "".join(expect)))
if failures:
raise SystemExit("FAIL:\n " + "\n ".join(failures))
print("ok: %d devices, each fix on only where it is for, none on NVIDIA's driver; every switch forces its fix both ways"
% len(DEVICES))
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,273 @@
"""The translation's vendor fixes (gpu/vk/vk_vendor.h): out-of-bounds texel fetches answered with zero, NaN tests on bits.
Both are cut out of the translator (FetchInBounds in backend/spirv/emit_spirv_image.cpp, IsNan in
emit_spirv_floating_point.cpp) into a Sirit fixture: a compute shader that fetches from an image of every type a fetch
reads (texel buffer, 1D, 1D array, 2D, 2D rect, 2D array, 3D, 2D multisampled) through FetchInBounds, gathers from a 2D,
2D rect, 2D array and cube texture through ImageGatherSubpixelOffset, and tests a 16-, 32- and 64-bit float with IsNan,
made once with the fixes off (fetch_bounds, nan_bits false: what NVIDIA gets) and once
on. Both must pass spirv-val. Off, IsNan is OpIsNan alone, as it was; on, there is no OpIsNan left, and every fetch's
coordinates and level are checked against the image's size and levels. The fetch path with fetch_bounds off is checked
to be the translator's old one, word for word. Nothing runs on a GPU.
python -I -B tests/test_vendor_translation.py --headers <SPIRV-Headers/include> [--cxx g++] [--spirv-val spirv-val]
"""
import argparse
from pathlib import Path
import struct
import subprocess
import tempfile
RUN_ALL_ARGS = ["--cxx", "{cxx}", "--headers", "{spirv_headers}", "--spirv-val", "{spirv_val}"]
# (spirv-val: SPIRV-Tools comes with the Linux runner's packages, not on Windows)
RUN_ALL_SKIP = {"windows": "needs spirv-val: checked on Linux"}
ROOT = Path(__file__).resolve().parents[1]
SPIRV = ROOT / "gpu" / "vk" / "eden" / "shader_recompiler" / "backend" / "spirv"
def block(text, marker, start=0):
at = text.index(marker, start)
end = text.index("{", at) + 1
depth = 1
while depth:
depth += (text[end] == "{") - (text[end] == "}")
end += 1
return at, end
FIXTURE = r"""
#include <array>
#include <cstdint>
#include <cstdio>
#include <vector>
#include <sirit/sirit.h>
using u32 = std::uint32_t;
using Sirit::Id;
bool nan_bits = false, fetch_bounds = false, fetch_bounds_buffers = false, gather_offset = false;
enum class TextureType { Color1D, ColorArray1D, Color2D, ColorArray2D, Color3D, ColorCube, ColorArrayCube, Buffer, Color2DRect };
namespace IR { struct TextureInstInfo { TextureType type; }; }
struct EmitContext : Sirit::Module {
using Sirit::Module::Module;
Id U1{}, u32_zero_value{}, f32_zero_value{};
std::array<Id, 5> U32{}, F32{};
Id Const(u32 v) { return Constant(U32[1], v); }
Id Const(float v) { return Constant(F32[1], v); }
};
@@ISNAN@@
@@FETCH@@
@@GATHER@@
struct Case { TextureType type; spv::Dim dim; bool arrayed, ms, lod; u32 dims; };
int main(int argc, char** argv) {
if (argc < 3) return 2;
nan_bits = fetch_bounds = gather_offset = argv[1][0] != '0';
fetch_bounds_buffers = argv[1][0] == '2'; // (robustBufferAccess2 off: texel buffers checked too)
EmitContext ctx{0x00010600};
ctx.AddCapability(spv::Capability::Shader);
ctx.AddCapability(spv::Capability::ImageQuery);
ctx.AddCapability(spv::Capability::Sampled1D);
ctx.AddCapability(spv::Capability::SampledBuffer);
ctx.AddCapability(spv::Capability::Float16);
ctx.AddCapability(spv::Capability::Float64);
ctx.SetMemoryModel(spv::AddressingModel::Logical, spv::MemoryModel::GLSL450);
ctx.U1 = ctx.TypeBool();
const Id u32t{ctx.TypeInt(32, false)}, f32t{ctx.TypeFloat(32)}, f16t{ctx.TypeFloat(16)}, f64t{ctx.TypeFloat(64)};
ctx.U32[1] = u32t, ctx.F32[1] = f32t;
for (int i = 2; i <= 4; i++) ctx.U32[i] = ctx.TypeVector(u32t, i), ctx.F32[i] = ctx.TypeVector(f32t, i);
ctx.u32_zero_value = ctx.Const(0u);
ctx.f32_zero_value = ctx.Const(0.0f);
// (the inputs, from a storage buffer: words the validator cannot fold)
const Id words{ctx.TypeRuntimeArray(u32t)};
ctx.Decorate(words, spv::Decoration::ArrayStride, 4u);
const Id block_type{ctx.TypeStruct(words)};
ctx.Decorate(block_type, spv::Decoration::Block);
ctx.MemberDecorate(block_type, 0, spv::Decoration::Offset, 0u);
const Id buffer{ctx.AddGlobalVariable(ctx.TypePointer(spv::StorageClass::StorageBuffer, block_type), spv::StorageClass::StorageBuffer)};
ctx.Decorate(buffer, spv::Decoration::DescriptorSet, 0u);
ctx.Decorate(buffer, spv::Decoration::Binding, 0u);
const Id word_ptr{ctx.TypePointer(spv::StorageClass::StorageBuffer, u32t)};
const std::vector<Case> cases{
{TextureType::Buffer, spv::Dim::Buffer, false, false, false, 1}, {TextureType::Color1D, spv::Dim::Dim1D, false, false, true, 1},
{TextureType::ColorArray1D, spv::Dim::Dim1D, true, false, true, 2}, {TextureType::Color2D, spv::Dim::Dim2D, false, false, true, 2},
{TextureType::Color2DRect, spv::Dim::Dim2D, false, false, true, 2}, {TextureType::ColorArray2D, spv::Dim::Dim2D, true, false, true, 3},
{TextureType::Color3D, spv::Dim::Dim3D, false, false, true, 3}, {TextureType::Color2D, spv::Dim::Dim2D, false, true, false, 2},
};
std::vector<Id> images, interface{buffer};
for (size_t i = 0; i < cases.size(); i++) {
const Case& c = cases[i];
const Id image_type{ctx.TypeImage(f32t, c.dim, 0, c.arrayed, c.ms, 1, spv::ImageFormat::Unknown)};
const Id var{ctx.AddGlobalVariable(ctx.TypePointer(spv::StorageClass::UniformConstant, image_type), spv::StorageClass::UniformConstant)};
ctx.Decorate(var, spv::Decoration::DescriptorSet, 0u);
ctx.Decorate(var, spv::Decoration::Binding, u32(1 + i));
images.push_back(var);
interface.push_back(var);
}
// (gathers through ImageGatherSubpixelOffset: 2D, 2D rect, 2D array, cube; the profile's gather offset is on with the fixes)
struct G { TextureType type; spv::Dim dim; bool arrayed; u32 dims; };
const std::vector<G> gathers{{TextureType::Color2D, spv::Dim::Dim2D, false, 2}, {TextureType::Color2DRect, spv::Dim::Dim2D, false, 2},
{TextureType::ColorArray2D, spv::Dim::Dim2D, true, 3}, {TextureType::ColorCube, spv::Dim::Cube, false, 3}};
std::vector<Id> gather_vars;
for (size_t i = 0; i < gathers.size(); i++) {
const Id image_type{ctx.TypeImage(f32t, gathers[i].dim, 0, gathers[i].arrayed, false, 1, spv::ImageFormat::Unknown)};
const Id var{ctx.AddGlobalVariable(ctx.TypePointer(spv::StorageClass::UniformConstant, ctx.TypeSampledImage(image_type)),
spv::StorageClass::UniformConstant)};
ctx.Decorate(var, spv::Decoration::DescriptorSet, 0u);
ctx.Decorate(var, spv::Decoration::Binding, u32(1 + cases.size() + i));
gather_vars.push_back(var);
interface.push_back(var);
}
const Id voidt{ctx.TypeVoid()};
const Id main{ctx.OpFunction(voidt, spv::FunctionControlMask::MaskNone, ctx.TypeFunction(voidt))};
ctx.AddLabel();
u32 next = 0;
const auto input = [&] { return ctx.OpLoad(u32t, ctx.OpAccessChain(word_ptr, buffer, ctx.u32_zero_value, ctx.Const(next++))); };
const auto output = [&](Id value) { ctx.OpStore(ctx.OpAccessChain(word_ptr, buffer, ctx.u32_zero_value, ctx.Const(next++)), value); };
for (size_t i = 0; i < cases.size(); i++) {
const Case& c = cases[i];
const Id image_type{ctx.TypeImage(f32t, c.dim, 0, c.arrayed, c.ms, 1, spv::ImageFormat::Unknown)};
const Id image{ctx.OpLoad(image_type, images[i])};
Id coords{input()};
if (c.dims > 1) {
std::vector<Id> parts{coords};
for (u32 k = 1; k < c.dims; k++) parts.push_back(input());
coords = ctx.OpCompositeConstruct(ctx.U32[c.dims], parts);
}
Id lod{c.lod ? input() : Id{}};
const Id sample{c.ms ? input() : Id{}};
Id in_bounds{};
Id sample_id{sample};
if (fetch_bounds) in_bounds = FetchInBounds(ctx, IR::TextureInstInfo{c.type}, image, coords, lod, sample_id);
Id color{};
if (c.ms) color = ctx.OpImageFetch(ctx.F32[4], image, coords, spv::ImageOperandsMask::Sample, sample_id);
else if (c.lod) color = ctx.OpImageFetch(ctx.F32[4], image, coords, spv::ImageOperandsMask::Lod, lod);
else color = ctx.OpImageFetch(ctx.F32[4], image, coords);
if (Sirit::ValidId(in_bounds)) color = ctx.OpSelect(ctx.F32[4], in_bounds, color, ctx.ConstantNull(ctx.F32[4]));
output(ctx.OpBitcast(u32t, ctx.OpCompositeExtract(f32t, color, 0u)));
}
for (size_t i = 0; i < gathers.size(); i++) {
const G& g = gathers[i];
const Id image_type{ctx.TypeImage(f32t, g.dim, 0, g.arrayed, false, 1, spv::ImageFormat::Unknown)};
const Id sampled{ctx.OpLoad(ctx.TypeSampledImage(image_type), gather_vars[i])};
std::vector<Id> parts;
for (u32 k = 0; k < g.dims; k++) parts.push_back(ctx.OpBitcast(f32t, input()));
Id coords{ctx.OpCompositeConstruct(ctx.F32[g.dims], parts)};
if (gather_offset) coords = ImageGatherSubpixelOffset(ctx, IR::TextureInstInfo{g.type}, ctx.OpImage(image_type, sampled), coords);
const Id texels{ctx.OpImageGather(ctx.F32[4], sampled, coords, ctx.u32_zero_value)};
output(ctx.OpBitcast(u32t, ctx.OpCompositeExtract(f32t, texels, 0u)));
}
const Id f32v{ctx.OpBitcast(f32t, input())};
const Id one{ctx.Const(1u)};
output(ctx.OpSelect(u32t, IsNan(ctx, ctx.OpFConvert(f16t, f32v), 16), one, ctx.u32_zero_value));
output(ctx.OpSelect(u32t, IsNan(ctx, f32v, 32), one, ctx.u32_zero_value));
output(ctx.OpSelect(u32t, IsNan(ctx, ctx.OpFConvert(f64t, f32v), 64), one, ctx.u32_zero_value));
ctx.OpReturn();
ctx.OpFunctionEnd();
ctx.AddEntryPoint(spv::ExecutionModel::GLCompute, main, "main", interface);
ctx.AddExecutionMode(main, spv::ExecutionMode::LocalSize, 1u, 1u, 1u);
std::vector<u32> code{ctx.Assemble()};
FILE* f = std::fopen(argv[2], "wb");
if (!f) return 3;
std::fwrite(code.data(), 4, code.size(), f);
std::fclose(f);
return 0;
}
"""
OP_IS_NAN, OP_IMAGE_FETCH, OP_SELECT, OP_QUERY_SIZE_LOD, OP_QUERY_SIZE, OP_QUERY_LEVELS = 156, 95, 169, 103, 104, 106
OP_U_GREATER_THAN, OP_BITCAST, OP_QUERY_SAMPLES = 172, 124, 107
# (the fetch path with fetch_bounds off: the translator's before the fix, word for word)
OLD_FETCH = """ const ImageOperands operands(lod, ms);
const Id color{Emit(&EmitContext::OpImageSparseFetch, &EmitContext::OpImageFetch, ctx, inst,
result_type, TextureImage(ctx, info, index), coords,
operands.MaskOptional(), operands.Span())};
return is_integer ? ctx.OpBitcast(ctx.F32[4], color) : color;
}"""
def opcodes(path):
data = path.read_bytes()
words = struct.unpack("<%dI" % (len(data) // 4), data)
i, ops = 5, []
while i < len(words):
ops.append(words[i] & 0xFFFF)
i += words[i] >> 16
return ops
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--headers", type=Path, required=True)
parser.add_argument("--cxx", default="g++")
parser.add_argument("--spirv-val", default="spirv-val")
parser.add_argument("--keep", type=Path, help="a folder to keep the two modules in (fixes0.spv, fixes1.spv)")
args = parser.parse_args()
image = (SPIRV / "emit_spirv_image.cpp").read_text(encoding="utf-8")
fp = (SPIRV / "emit_spirv_floating_point.cpp").read_text(encoding="utf-8")
fetch_at, fetch_end = block(image, "Id FetchInBounds(")
nan_at, nan_end = block(fp, "Id IsNan(EmitContext& ctx, Id value, u32 bits)")
gather_at, gather_end = block(image, "Id ImageGatherSubpixelOffset(")
emit_at, emit_end = block(image, "Id EmitImageFetch(")
emit = image[emit_at:emit_end]
guard_at, guard_end = block(emit, "if (fetch_bounds) {")
if not emit[guard_end:].lstrip("\n").startswith(OLD_FETCH):
raise SystemExit("FAIL: EmitImageFetch's path with fetch_bounds off is not the old one")
for name in ("EmitFPIsNan16", "EmitFPIsNan32", "EmitFPIsNan64"):
at, end = block(fp, "Id %s(" % name)
if "return IsNan(ctx, value, %s);" % name[-2:] not in fp[at:end]:
raise SystemExit("FAIL: %s does not go through IsNan" % name)
get_set = (SPIRV / "emit_spirv_context_get_set.cpp").read_text(encoding="utf-8")
convert = (SPIRV / "emit_spirv_convert.cpp").read_text(encoding="utf-8")
if "IsNan(ctx, value, 32)" not in get_set[slice(*block(get_set, "void EmitSetFragColor("))]:
raise SystemExit("FAIL: the colour outputs' NaN guard does not go through IsNan")
if "IsNan(ctx, result, 16)" not in convert[slice(*block(convert, "Id EmitConvertF16F32("))]:
raise SystemExit("FAIL: EmitConvertF16F32 does not go through IsNan")
fixture = FIXTURE.replace("@@ISNAN@@", fp[nan_at:nan_end]).replace("@@FETCH@@", image[fetch_at:fetch_end]).replace(
"@@GATHER@@", image[gather_at:gather_end])
sirit = ROOT / "gpu" / "vk" / "sirit"
with tempfile.TemporaryDirectory(prefix="vendor-translation-") as tmp:
tmp = Path(tmp)
cpp, exe = tmp / "fixture.cpp", tmp / "fixture"
cpp.write_text(fixture, encoding="utf-8")
r = subprocess.run([args.cxx, "-std=c++20", "-O1", "-I" + str(sirit / "include"), "-I" + str(sirit / "src"),
"-I" + str(args.headers), str(cpp), *map(str, sorted((sirit / "src").rglob("*.cpp"))), "-o", str(exe)],
capture_output=True, text=True, timeout=600)
if r.returncode:
print(r.stdout + r.stderr)
raise SystemExit("the fixture did not build")
ops = {}
for on in ("0", "1", "2"):
spv = tmp / ("fixes%s.spv" % on)
r = subprocess.run([str(exe), on, str(spv)], capture_output=True, text=True, timeout=30)
if r.returncode:
raise SystemExit("FAIL: the fixture stopped (%d): %s" % (r.returncode, r.stdout + r.stderr))
v = subprocess.run([args.spirv_val, "--target-env", "vulkan1.3", str(spv)], capture_output=True, text=True, timeout=60)
if v.returncode:
raise SystemExit("FAIL: spirv-val, fixes %s:\n%s" % (["off", "on", "on, texel buffers too"][int(on)], v.stdout + v.stderr))
ops[on] = opcodes(spv)
if args.keep:
args.keep.mkdir(parents=True, exist_ok=True)
(args.keep / spv.name).write_bytes(spv.read_bytes())
off, on, buffers = ops["0"], ops["1"], ops["2"]
# (robustBufferAccess2 off: the texel buffer's fetch checked too, by its size: one more size query than with it on)
if buffers.count(OP_QUERY_SIZE) != on.count(OP_QUERY_SIZE) + 1:
raise SystemExit("FAIL: with robustBufferAccess2 off the texel buffer's fetch is not checked")
if off.count(OP_IS_NAN) != 3 or OP_U_GREATER_THAN in off or OP_QUERY_LEVELS in off or OP_QUERY_SIZE_LOD in off:
raise SystemExit("FAIL: with the fixes off the shader is not what it was (OpIsNan alone, no bounds)")
if on.count(OP_IS_NAN) or on.count(OP_U_GREATER_THAN) < 3:
raise SystemExit("FAIL: with nan_bits a NaN test is still OpIsNan")
# (6 fetches with a level: each its levels and its size at that level; the multisampled image: its size; the texel
# buffer none (robustBufferAccess2 answers it); 3 gathers nudged by their size (2D, rect, array), the cube's not)
if OP_QUERY_SIZE_LOD in off:
raise SystemExit("FAIL: with the fixes off a gather is nudged")
if on.count(OP_QUERY_LEVELS) != 6 or on.count(OP_QUERY_SIZE_LOD) != 6 + 3 or on.count(OP_QUERY_SIZE) != 1:
raise SystemExit("FAIL: with fetch_bounds not every fetch is checked (levels %d, sizes %d + %d)" % (
on.count(OP_QUERY_LEVELS), on.count(OP_QUERY_SIZE_LOD), on.count(OP_QUERY_SIZE)))
if on.count(OP_QUERY_SAMPLES) != 1 or OP_QUERY_SAMPLES in off:
raise SystemExit("FAIL: with fetch_bounds the multisampled fetch's sample is not checked")
if on.count(OP_IMAGE_FETCH) != 8 or off.count(OP_IMAGE_FETCH) != 8:
raise SystemExit("FAIL: fetches went missing")
print("ok: fixes off: 3 OpIsNan, 8 plain fetches, 4 plain gathers; on: NaN tests on bits, 7 fetches checked (not the texel "
"buffer), 3 gathers nudged (not the cube); both valid SPIR-V")
if __name__ == "__main__":
main()