mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 20:58:36 +00:00
shader_library_corpus_test runs src/shader_library.h over the work folder make_game.py builds from the player's copy (LEONIDA_SHADERS=<work>/shaders/spirv, containers from <work>/shaders/dxbc or LEONIDA_SHADER_DXBC), as the D3D12 layer does with a PSO's bytecode: inflate the FSSM program, hash it and look it up under the stage the DXIL program header names. Every container must find its own index.json entry and a readable .spv, every entry must be reached, and in containers with more than one 0x0D payload no earlier payload may be the indexed program. Without LEONIDA_SHADERS it prints SKIP and exits 77 (SKIP_RETURN_CODE). On the 1.0.2.1 corpus: 26,503 containers, all resolve, 195 with more than one program. With fssm_program switched to the first payload it fails (391 failures, 26,308 of 26,503 entries reached). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
160 lines
7.1 KiB
C++
160 lines
7.1 KiB
C++
// The runtime's shader lookup (src/shader_library.h) over a translated corpus from the player's own copy.
|
|
//
|
|
// LEONIDA_SHADERS=<work>/shaders/spirv the translator's output (index.json and the .spv modules)
|
|
// LEONIDA_SHADER_DXBC=<work>/shaders/dxbc optional; the carved DXBC containers, default <spirv>/../dxbc
|
|
//
|
|
// For every container the test does what d3d12_vulkan_device.cpp does with a PSO's bytecode: inflate the FSSM program
|
|
// from the DXIL/XDAT/XBCH entry, hash it and look it up under the container's stage. The stage comes from the DXIL
|
|
// part's program header, not from index.json. Every container has to resolve to its own entry (the one named after
|
|
// the file, or one of its `~N` duplicates) with a readable SPIR-V module, and every index entry has to be reached by
|
|
// a container. Containers with more than one 0x0D payload are counted, and their first payload must not stand in for
|
|
// the last: that is the rule fssm_program follows, so a regression to the first payload fails here.
|
|
//
|
|
// Without LEONIDA_SHADERS it prints SKIP and exits 77 (SKIP_RETURN_CODE). It reads the work folder in place, writes
|
|
// nothing, and prints counts; on a failure it names at most a few shaders, which stay on your machine.
|
|
#include "../src/shader_library.h"
|
|
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <set>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
std::string env(const char* name) {
|
|
const char* value = std::getenv(name);
|
|
return value ? value : "";
|
|
}
|
|
|
|
std::vector<std::uint8_t> read_file(const fs::path& path) {
|
|
std::ifstream file(path, std::ios::binary);
|
|
return {std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()};
|
|
}
|
|
|
|
std::uint32_t u32(std::span<const std::uint8_t> data, std::size_t at) {
|
|
if (at + 4 > data.size()) return 0;
|
|
std::uint32_t v;
|
|
std::memcpy(&v, data.data() + at, 4);
|
|
return v;
|
|
}
|
|
|
|
// The stage the DXIL program header names (DxilProgramHeader::ProgramVersion, kind in bits 31:16; D3D12's
|
|
// D3D12_SHADER_VERSION_TYPE), in the translator's spelling. Empty for a kind the corpus has no translation for.
|
|
std::string dxil_stage(std::span<const std::uint8_t> container) {
|
|
const auto dxil = leonida::dxbc_part(container, "DXIL");
|
|
if (dxil.size() < 4) return {};
|
|
switch (u32(dxil, 0) >> 16) {
|
|
case 0: return "ps";
|
|
case 1: return "vs";
|
|
case 2: return "gs";
|
|
case 5: return "cs";
|
|
case 13: return "ms";
|
|
default: return {};
|
|
}
|
|
}
|
|
|
|
// Every 0x0D (XBCH) payload of the container's XDAT table, in table order, inflated. fssm_program keeps the last.
|
|
std::vector<std::vector<std::uint8_t>> all_programs(std::span<const std::uint8_t> container) {
|
|
std::vector<std::vector<std::uint8_t>> out;
|
|
const auto dxil = leonida::dxbc_part(container, "DXIL");
|
|
std::size_t xdat = 0;
|
|
while (xdat + 4 <= dxil.size() && std::memcmp(dxil.data() + xdat, "XDAT", 4) != 0) ++xdat;
|
|
const std::uint32_t count = u32(dxil, xdat + 4);
|
|
for (std::uint32_t i = 0; i < count; ++i) {
|
|
const std::size_t entry = xdat + 12 + 12 * std::size_t(i);
|
|
if (entry + 12 > dxil.size()) break;
|
|
const std::uint32_t tag = u32(dxil, entry), offset = u32(dxil, entry + 4), size = u32(dxil, entry + 8);
|
|
if (tag != 0x0d || size < 24 || std::size_t(offset) + size > dxil.size()) continue;
|
|
const auto blob = dxil.subspan(offset, size);
|
|
std::vector<std::uint8_t> program;
|
|
if (std::memcmp(blob.data(), "XBCH", 4) == 0 && leonida::zlib_inflate(blob.subspan(24), program, u32(blob, 16)))
|
|
out.push_back(std::move(program));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
bool own_entry(const leonida::ShaderEntry& entry, const std::string& stem) {
|
|
return entry.name == stem || entry.name.rfind(stem + "~", 0) == 0;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
const std::string spirv_dir = env("LEONIDA_SHADERS");
|
|
if (spirv_dir.empty()) {
|
|
std::printf("SKIP (no translated corpus: set LEONIDA_SHADERS to <work>/shaders/spirv)\n");
|
|
return 77;
|
|
}
|
|
std::string dxbc_dir = env("LEONIDA_SHADER_DXBC");
|
|
if (dxbc_dir.empty()) dxbc_dir = (fs::path(spirv_dir) / ".." / "dxbc").lexically_normal().string();
|
|
|
|
int failures = 0, named = 0;
|
|
const auto fail = [&](const std::string& what, const std::string& shader) {
|
|
++failures;
|
|
if (named++ < 8) std::fprintf(stderr, "FAIL %s: %s\n", what.c_str(), shader.c_str());
|
|
};
|
|
|
|
leonida::ShaderLibrary library;
|
|
std::string error;
|
|
if (!library.load(spirv_dir, &error)) {
|
|
std::fprintf(stderr, "FAIL loading %s: %s\n", spirv_dir.c_str(), error.c_str());
|
|
return 1;
|
|
}
|
|
std::error_code ec;
|
|
if (!fs::is_directory(dxbc_dir, ec)) {
|
|
std::fprintf(stderr, "FAIL no DXBC folder %s (set LEONIDA_SHADER_DXBC)\n", dxbc_dir.c_str());
|
|
return 1;
|
|
}
|
|
|
|
std::size_t containers = 0, multi = 0;
|
|
std::set<std::string> reached;
|
|
for (const auto& item : fs::directory_iterator(dxbc_dir)) {
|
|
if (!item.is_regular_file() || item.path().extension() != ".dxbc") continue;
|
|
++containers;
|
|
const std::string stem = item.path().stem().string();
|
|
const auto bytes = read_file(item.path());
|
|
const std::string stage = dxil_stage(bytes);
|
|
if (stage.empty()) {
|
|
fail("no shader stage in the DXIL program header", stem);
|
|
continue;
|
|
}
|
|
const auto program = leonida::fssm_program(bytes);
|
|
if (program.size() < 4 || std::memcmp(program.data(), "FSSM", 4) != 0) {
|
|
fail("no FSSM program", stem);
|
|
continue;
|
|
}
|
|
bool found = false;
|
|
for (const leonida::ShaderEntry* entry : library.candidates(stage, leonida::sha256_hex(program))) {
|
|
if (!own_entry(*entry, stem)) continue;
|
|
found = true;
|
|
reached.insert(entry->name);
|
|
if (library.spirv(*entry).empty()) fail("no SPIR-V module for", entry->name);
|
|
}
|
|
if (!found) fail("not found under its " + stage + " stage", stem);
|
|
|
|
// The 0x0D payloads that differ from the last: none of them may be what the index has for this container.
|
|
const auto programs = all_programs(bytes);
|
|
if (programs.empty() || programs.back() != program) fail("fssm_program is not the last 0x0D payload", stem);
|
|
bool differs = false;
|
|
for (std::size_t i = 0; i + 1 < programs.size(); ++i) {
|
|
if (programs[i] == program) continue;
|
|
differs = true;
|
|
for (const leonida::ShaderEntry* entry : library.candidates(stage, leonida::sha256_hex(programs[i])))
|
|
if (own_entry(*entry, stem)) fail("an earlier 0x0D payload is the indexed program", stem);
|
|
}
|
|
multi += differs;
|
|
}
|
|
|
|
if (containers == 0) fail("no .dxbc containers in", dxbc_dir);
|
|
if (reached.size() != library.size())
|
|
fail(std::to_string(library.size() - reached.size()) + " index entries reached by no container", spirv_dir);
|
|
std::printf("%zu containers, %zu index entries, %zu reached; %zu containers with more than one program; "
|
|
"%d failures\n", containers, library.size(), reached.size(), multi, failures);
|
|
return failures ? 1 : 0;
|
|
}
|