diff options
Diffstat (limited to 'runtime/test/root_signature_test.cpp')
| -rw-r--r-- | runtime/test/root_signature_test.cpp | 221 |
1 files changed, 221 insertions, 0 deletions
diff --git a/runtime/test/root_signature_test.cpp b/runtime/test/root_signature_test.cpp new file mode 100644 index 000000000..92ef8347f --- /dev/null +++ b/runtime/test/root_signature_test.cpp | |||
| @@ -0,0 +1,221 @@ | |||
| 1 | // Portable tests for runtime/src/root_signature.{h,cpp} (no windows.h). | ||
| 2 | // | ||
| 3 | // Own main + PASS/FAIL lines like the other runtime tests; returns nonzero | ||
| 4 | // on any failure so CTest gates on it. Everything here is hand-built: no | ||
| 5 | // game data, no DXBC, no serializer needed. | ||
| 6 | #include <cstdint> | ||
| 7 | #include <cstdio> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "root_signature.h" | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | int g_failures = 0; | ||
| 15 | |||
| 16 | void check(bool condition, const char* what) { | ||
| 17 | std::printf("%s %s\n", condition ? "PASS" : "FAIL", what); | ||
| 18 | if (!condition) ++g_failures; | ||
| 19 | } | ||
| 20 | |||
| 21 | rt::rootsig::RootParameter consts(uint32_t n) { | ||
| 22 | rt::rootsig::RootParameter p; | ||
| 23 | p.type = rt::rootsig::ParamType::Constants; | ||
| 24 | p.num_values = n; | ||
| 25 | return p; | ||
| 26 | } | ||
| 27 | |||
| 28 | rt::rootsig::RootParameter view() { | ||
| 29 | rt::rootsig::RootParameter p; | ||
| 30 | p.type = rt::rootsig::ParamType::Descriptor; | ||
| 31 | return p; | ||
| 32 | } | ||
| 33 | |||
| 34 | rt::rootsig::RootParameter table() { | ||
| 35 | rt::rootsig::RootParameter p; | ||
| 36 | p.type = rt::rootsig::ParamType::Table; | ||
| 37 | p.ranges.push_back({0, 8}); | ||
| 38 | return p; | ||
| 39 | } | ||
| 40 | |||
| 41 | void put_u32(std::vector<uint8_t>& out, uint32_t v) { | ||
| 42 | out.push_back(static_cast<uint8_t>(v)); | ||
| 43 | out.push_back(static_cast<uint8_t>(v >> 8)); | ||
| 44 | out.push_back(static_cast<uint8_t>(v >> 16)); | ||
| 45 | out.push_back(static_cast<uint8_t>(v >> 24)); | ||
| 46 | } | ||
| 47 | |||
| 48 | // Hand-built RTS0 payload in the offset-table layout the maintainer | ||
| 49 | // described: header, parameter table, then payloads at their offsets. | ||
| 50 | std::vector<uint8_t> build_payload() { | ||
| 51 | std::vector<uint8_t> p; | ||
| 52 | put_u32(p, 2); // version 1.1 | ||
| 53 | put_u32(p, 2); // 2 params | ||
| 54 | put_u32(p, 24); // params table offset | ||
| 55 | put_u32(p, 0); // 0 samplers | ||
| 56 | put_u32(p, 0); // samplers offset | ||
| 57 | put_u32(p, 0x03000000u); // Xbox flags | ||
| 58 | // entry 0: constants at 48; entry 1: CBV descriptor at 60. | ||
| 59 | put_u32(p, 1); | ||
| 60 | put_u32(p, 0); | ||
| 61 | put_u32(p, 48); | ||
| 62 | put_u32(p, 2); | ||
| 63 | put_u32(p, 4); | ||
| 64 | put_u32(p, 60); | ||
| 65 | put_u32(p, 7); // register (ignored by packer) | ||
| 66 | put_u32(p, 0); // space | ||
| 67 | put_u32(p, 3); // 3 values | ||
| 68 | put_u32(p, 0); // register | ||
| 69 | put_u32(p, 0); // space | ||
| 70 | put_u32(p, 0); // 1.1 descriptor flags | ||
| 71 | return p; | ||
| 72 | } | ||
| 73 | |||
| 74 | } // namespace | ||
| 75 | |||
| 76 | int main() { | ||
| 77 | using namespace rt::rootsig; | ||
| 78 | |||
| 79 | // Mixed signature: 4 constants + CBV + table + 1 static sampler. | ||
| 80 | { | ||
| 81 | RootSignature sig; | ||
| 82 | sig.params = {consts(4), view(), table()}; | ||
| 83 | StaticSampler s; | ||
| 84 | s.shader_register = 3; | ||
| 85 | sig.static_samplers = {s}; | ||
| 86 | const PackResult r = pack(sig); | ||
| 87 | check(r.ok, "mixed signature packs"); | ||
| 88 | check(r.dword_count == 8, "mixed signature uses 8 dwords"); | ||
| 89 | check(r.d3d12_cost == 4 + 2 + 1, "mixed signature costs 7"); | ||
| 90 | check(r.provenance.size() == 8 && r.provenance[0].param_index == 0 && | ||
| 91 | r.provenance[0].sub_index == 0 && r.provenance[4].param_index == 1 && | ||
| 92 | r.provenance[4].sub_index == 0 && r.provenance[5].sub_index == 1 && | ||
| 93 | r.provenance[6].param_index == 2, | ||
| 94 | "provenance tracks param and sub-dword"); | ||
| 95 | } | ||
| 96 | |||
| 97 | // Exactly 32 dwords still fits. | ||
| 98 | { | ||
| 99 | RootSignature sig; | ||
| 100 | for (int i = 0; i < 16; ++i) sig.params.push_back(view()); | ||
| 101 | const PackResult r = pack(sig); | ||
| 102 | check(r.ok && r.dword_count == 32, "16 views fill 32 dwords"); | ||
| 103 | } | ||
| 104 | |||
| 105 | // 34 dwords: spill is refused loudly, never truncated. | ||
| 106 | { | ||
| 107 | RootSignature sig; | ||
| 108 | for (int i = 0; i < 17; ++i) sig.params.push_back(view()); | ||
| 109 | const PackResult r = pack(sig); | ||
| 110 | check(!r.ok && r.error.find("spill") != std::string::npos, | ||
| 111 | "17 views refused as spill"); | ||
| 112 | } | ||
| 113 | |||
| 114 | // 65 constants exceed the 64-dword D3D12 cost limit. | ||
| 115 | { | ||
| 116 | RootSignature sig; | ||
| 117 | sig.params = {consts(65)}; | ||
| 118 | const PackResult r = pack(sig); | ||
| 119 | check(!r.ok && r.error.find("64-dword") != std::string::npos, | ||
| 120 | "65 constants refused by cost limit"); | ||
| 121 | } | ||
| 122 | |||
| 123 | // Canonical codec round-trips byte-exactly, sampler included. | ||
| 124 | { | ||
| 125 | RootSignature sig; | ||
| 126 | sig.version = Version::V1_0; | ||
| 127 | sig.params = {consts(2), table()}; | ||
| 128 | sig.flags = 0x03000000u; // Xbox high byte survives the model | ||
| 129 | StaticSampler s; | ||
| 130 | s.filter = 21; | ||
| 131 | s.mip_lod_bias = 0.5f; | ||
| 132 | sig.static_samplers = {s}; | ||
| 133 | const std::vector<uint8_t> bytes = encode_canonical(sig); | ||
| 134 | const DecodeResult d = decode_canonical(bytes.data(), bytes.size()); | ||
| 135 | check(d.ok, "canonical round-trip decodes"); | ||
| 136 | check(d.sig.params.size() == 2 && d.sig.params[0].num_values == 2 && | ||
| 137 | d.sig.params[1].ranges.size() == 1 && | ||
| 138 | d.sig.static_samplers.size() == 1 && d.sig.flags == 0x03000000u && | ||
| 139 | d.sig.static_samplers[0].filter == 21, | ||
| 140 | "canonical round-trip preserves model"); | ||
| 141 | } | ||
| 142 | |||
| 143 | // Canonical decoder controls: bad magic, truncation, trailing bytes. | ||
| 144 | { | ||
| 145 | const std::vector<uint8_t> bad = {0, 1, 2, 3}; | ||
| 146 | check(!decode_canonical(bad.data(), bad.size()).ok, "bad magic rejected"); | ||
| 147 | RootSignature sig; | ||
| 148 | sig.params = {consts(1)}; | ||
| 149 | const std::vector<uint8_t> bytes = encode_canonical(sig); | ||
| 150 | check(!decode_canonical(bytes.data(), bytes.size() - 1).ok, | ||
| 151 | "truncated canonical rejected"); | ||
| 152 | std::vector<uint8_t> trailed = bytes; | ||
| 153 | trailed.push_back(0); | ||
| 154 | check(!decode_canonical(trailed.data(), trailed.size()).ok, | ||
| 155 | "trailing bytes rejected"); | ||
| 156 | } | ||
| 157 | |||
| 158 | // RTS0 hypothesis: a hand-built blob per the documented layout parses. | ||
| 159 | // Hand-built RTS0 payload in the offset-table layout the maintainer | ||
| 160 | // described: header, parameter table, then payloads at their offsets. | ||
| 161 | { | ||
| 162 | const std::vector<uint8_t> payload = build_payload(); | ||
| 163 | const DecodeResult d = parse_rts0(payload.data(), payload.size()); | ||
| 164 | check(d.ok, "hand-built RTS0 parses"); | ||
| 165 | check(d.sig.params.size() == 2 && d.sig.params[0].num_values == 3 && | ||
| 166 | d.sig.flags == 0x03000000u, | ||
| 167 | "RTS0 model matches hand-built blob"); | ||
| 168 | const PackResult r = pack(d.sig); | ||
| 169 | check(r.ok && r.dword_count == 3 + 2, "parsed RTS0 packs to 5 dwords"); | ||
| 170 | } | ||
| 171 | |||
| 172 | // RTS0 controls: unknown version, wild offsets and truncation fail loudly. | ||
| 173 | { | ||
| 174 | std::vector<uint8_t> blob; | ||
| 175 | put_u32(blob, 9); // unknown version | ||
| 176 | put_u32(blob, 0); | ||
| 177 | put_u32(blob, 0); | ||
| 178 | put_u32(blob, 0); | ||
| 179 | put_u32(blob, 0); | ||
| 180 | put_u32(blob, 0); | ||
| 181 | const DecodeResult d = parse_rts0(blob.data(), blob.size()); | ||
| 182 | check(!d.ok, "unknown RTS0 version rejected"); | ||
| 183 | std::vector<uint8_t> cut = {1, 0}; | ||
| 184 | check(!parse_rts0(cut.data(), cut.size()).ok, "truncated RTS0 rejected"); | ||
| 185 | std::vector<uint8_t> wild = build_payload(); | ||
| 186 | // Corrupt the params offset to point past the end. | ||
| 187 | wild[8] = 0xFF; | ||
| 188 | wild[9] = 0xFF; | ||
| 189 | wild[10] = 0xFF; | ||
| 190 | wild[11] = 0xFF; | ||
| 191 | check(!parse_rts0(wild.data(), wild.size()).ok, "wild offset rejected"); | ||
| 192 | } | ||
| 193 | |||
| 194 | // DXBC container walk: a hand-built container with one RTS0 chunk parses | ||
| 195 | // through the chunk table (same path the MS serializer blob takes). | ||
| 196 | { | ||
| 197 | const std::vector<uint8_t> payload = build_payload(); | ||
| 198 | std::vector<uint8_t> blob; | ||
| 199 | blob.insert(blob.end(), {'D', 'X', 'B', 'C'}); | ||
| 200 | blob.insert(blob.end(), 16, 0); // hash | ||
| 201 | put_u32(blob, 1); // version | ||
| 202 | put_u32(blob, 0); // file size (patched below) | ||
| 203 | put_u32(blob, 1); // 1 chunk | ||
| 204 | put_u32(blob, 36); // chunk offset: 4 magic + 16 hash + 4 ver + 4 size + 4 count + 4 offset | ||
| 205 | blob.insert(blob.end(), {'R', 'T', 'S', '0'}); | ||
| 206 | put_u32(blob, static_cast<uint32_t>(payload.size())); | ||
| 207 | blob.insert(blob.end(), payload.begin(), payload.end()); | ||
| 208 | const uint32_t total = static_cast<uint32_t>(blob.size()); | ||
| 209 | blob[24] = static_cast<uint8_t>(total); | ||
| 210 | blob[25] = static_cast<uint8_t>(total >> 8); | ||
| 211 | blob[26] = static_cast<uint8_t>(total >> 16); | ||
| 212 | blob[27] = static_cast<uint8_t>(total >> 24); | ||
| 213 | const DecodeResult d = parse_rts0(blob.data(), blob.size()); | ||
| 214 | check(d.ok, "DXBC container walk finds RTS0"); | ||
| 215 | check(d.sig.params.size() == 2 && d.sig.params[0].num_values == 3, | ||
| 216 | "walked RTS0 model matches payload"); | ||
| 217 | } | ||
| 218 | |||
| 219 | if (g_failures != 0) std::printf("%d FAILURES\n", g_failures); | ||
| 220 | return g_failures == 0 ? 0 : 1; | ||
| 221 | } | ||