// The runtime's shader lookup (src/shader_library.h) over a translated corpus from the player's own copy. // // LEONIDA_SHADERS=/shaders/spirv the translator's output (index.json and the .spv modules) // LEONIDA_SHADER_DXBC=/shaders/dxbc optional; the carved DXBC containers, default /../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 #include #include #include #include #include #include namespace { namespace fs = std::filesystem; std::string env(const char* name) { const char* value = std::getenv(name); return value ? value : ""; } std::vector read_file(const fs::path& path) { std::ifstream file(path, std::ios::binary); return {std::istreambuf_iterator(file), std::istreambuf_iterator()}; } std::uint32_t u32(std::span 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 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> all_programs(std::span container) { std::vector> 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 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 /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 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; }