upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/root_signature_oracle_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/test/root_signature_oracle_test.cpp')
-rw-r--r--runtime/test/root_signature_oracle_test.cpp120
1 files changed, 120 insertions, 0 deletions
diff --git a/runtime/test/root_signature_oracle_test.cpp b/runtime/test/root_signature_oracle_test.cpp
new file mode 100644
index 000000000..d60dfae3a
--- /dev/null
+++ b/runtime/test/root_signature_oracle_test.cpp
@@ -0,0 +1,120 @@
1// Windows oracle for the RTS0 parser hypothesis (needs a real PC d3d12.lib).
2//
3// Builds synthetic versioned root-signature descriptors by hand (no game
4// data), serializes each with Microsoft's D3D12SerializeVersionedRootSignature,
5// and requires rt::rootsig::parse_rts0 to recover the same model. If the
6// hypothesis layout in root_signature.cpp ever disagrees with the real
7// serializer, this fails loudly here instead of corrupting draws silently.
8// NOT portable: runs on Windows CI only, like root_sig_test.cpp.
9#include <windows.h>
10#include <d3d12.h>
11
12#include <cstdint>
13#include <cstdio>
14#include <vector>
15
16#include "root_signature.h"
17
18namespace {
19
20int g_failures = 0;
21
22void check(bool condition, const char* what) {
23 std::printf("%s %s\n", condition ? "PASS" : "FAIL", what);
24 if (!condition) ++g_failures;
25}
26
27struct Blobs {
28 ID3DBlob* blob = nullptr;
29 ID3DBlob* error = nullptr;
30 ~Blobs() {
31 if (blob) blob->Release();
32 if (error) error->Release();
33 }
34};
35
36D3D12_VERSIONED_ROOT_SIGNATURE_DESC make_desc(
37 const D3D12_ROOT_PARAMETER1* params, uint32_t nparams) {
38 D3D12_VERSIONED_ROOT_SIGNATURE_DESC desc{};
39 desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_1;
40 desc.Desc_1_1.NumParameters = nparams;
41 desc.Desc_1_1.pParameters = params;
42 desc.Desc_1_1.NumStaticSamplers = 0;
43 desc.Desc_1_1.pStaticSamplers = nullptr;
44 desc.Desc_1_1.Flags = D3D12_ROOT_SIGNATURE_FLAG_NONE;
45 return desc;
46}
47
48bool parse_blob(ID3DBlob* blob, rt::rootsig::RootSignature& model) {
49 const auto* bytes = static_cast<const uint8_t*>(blob->GetBufferPointer());
50 const size_t size = blob->GetBufferSize();
51 // Diagnostics first: if the layout hypothesis is wrong, the CI log shows
52 // the real bytes instead of just a crash or a bare failure.
53 std::printf("oracle blob: %zu bytes: ", size);
54 for (size_t i = 0; i < 32 && i < size; ++i) {
55 std::printf("%02x", bytes[i]);
56 }
57 std::printf("\n");
58 std::fflush(stdout);
59 const rt::rootsig::DecodeResult d = rt::rootsig::parse_rts0(bytes, size);
60 if (!d.ok) {
61 std::printf("oracle parse failed: %s\n", d.error.c_str());
62 return false;
63 }
64 model = d.sig;
65 return true;
66}
67
68} // namespace
69
70int main() {
71 using namespace rt::rootsig;
72
73 // 4 constants (VS-visible) + 1 CBV: parses back to the same model and
74 // packs to 6 dwords, exactly like the portable packer says for 1.1.
75 D3D12_ROOT_PARAMETER1 params[2]{};
76 params[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
77 params[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX;
78 params[0].Constants.ShaderRegister = 0;
79 params[0].Constants.RegisterSpace = 0;
80 params[0].Constants.Num32BitValues = 4;
81 params[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
82 params[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
83 params[1].Descriptor.ShaderRegister = 2;
84 params[1].Descriptor.RegisterSpace = 0;
85 params[1].Descriptor.Flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE;
86
87 const D3D12_VERSIONED_ROOT_SIGNATURE_DESC desc = make_desc(params, 2);
88 Blobs blobs;
89 const HRESULT hr =
90 D3D12SerializeVersionedRootSignature(&desc, &blobs.blob, &blobs.error);
91 check(hr == S_OK, "Microsoft serializer accepts the synthetic desc");
92 if (hr != S_OK) {
93 if (blobs.error != nullptr) {
94 std::printf("serializer error: %s\n",
95 static_cast<const char*>(blobs.error->GetBufferPointer()));
96 }
97 std::printf("%d FAILURES\n", g_failures + 1);
98 return 1;
99 }
100
101 RootSignature model;
102 check(parse_blob(blobs.blob, model), "oracle blob parses");
103 if (g_failures != 0) {
104 // Never index into a model that failed to parse (that segfaulted CI).
105 std::printf("%d FAILURES\n", g_failures);
106 return 1;
107 }
108 check(model.params.size() == 2, "oracle model has 2 params");
109 check(model.params[0].type == ParamType::Constants &&
110 model.params[0].num_values == 4 &&
111 model.params[0].visibility == Visibility::Vertex,
112 "oracle constants match");
113 check(model.params[1].type == ParamType::Descriptor,
114 "oracle CBV matches");
115 const PackResult packed = pack(model);
116 check(packed.ok && packed.dword_count == 6, "oracle model packs to 6 dwords");
117
118 if (g_failures != 0) std::printf("%d FAILURES\n", g_failures);
119 return g_failures == 0 ? 0 : 1;
120}