From a330747f237b7ce1d2cc82a531ef5d2220ee9c20 Mon Sep 17 00:00:00 2001 From: robot00f Date: Tue, 24 Nov 2026 19:16:06 -0300 Subject: [PATCH] d3d12_xs: keep the heap-directly-indexed flags on a host that refuses to serialize them --- docs/d3d12_vulkan.md | 11 + runtime/shims/d3d12_xs.cpp | 10 +- runtime/shims/d3d12_xs_rootsig.cpp | 196 ++++++++++++++++-- runtime/shims/d3d12_xs_rootsig.h | 27 +++ runtime/test/d3d12_test_shaders.h | 51 ++++- runtime/test/d3d12_xs_root_signature_test.cpp | 58 ++++++ runtime/test/root_sig_test.cpp | 31 +++ 7 files changed, 363 insertions(+), 21 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 3cd9d6d5e8..3638ccdff0 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -1879,6 +1879,17 @@ UINT border colour or non-normalized coordinates) with a log line, since 1.1 can once with an empty 1.2 description. `root_sig_test` checks the rewrite field by field and through the host deserializer. +**Heap-directly-indexed flags on older runtimes.** The same in-box `d3d12.dll` refuses +`D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED` and `..._SAMPLER_HEAP_DIRECTLY_INDEXED` in the +serializer at every descriptor version, with `E_INVALIDARG` and "Unsupported bit-flag set (root signature flags c00)" +(measured on 10.0.19041.4355; 1.0 and 1.1 both fail). Its *deserializer* and its devices take a blob that carries +them: a blob serialized without the bits, patched back in and re-hashed deserializes with `Flags = 0xc00` and creates +a WARP root signature. So the `d3d12_xs` serializer, when the host refuses and the description sets either bit, +serializes again with the bits cleared and puts them back into the produced `RTS0` chunk, recomputing the container +hash. A host that takes them (Windows 11) gets one call and its own blob back. `d3d12_xs_root_signature_test` covers +both descriptor versions and checks the flags through the host deserializer and a device; the test-side +`d3d12test::serialize_root_signature` helper does the same retry for tests that serialize directly. + Things that can invalidate this simple model, all **Open**: - **Shader visibility.** The layer starts with one global block in root-parameter order and treats visibility as diff --git a/runtime/shims/d3d12_xs.cpp b/runtime/shims/d3d12_xs.cpp index 8e54c3fd56..61b6c99596 100644 --- a/runtime/shims/d3d12_xs.cpp +++ b/runtime/shims/d3d12_xs.cpp @@ -2,7 +2,9 @@ // The SDK types describe the Windows side. The measured guest ABI differs in the root-signature flag word: Xbox // sets bits in the high byte (observed 0x03000000) and the PC serializer rejects them, so descriptors are copied // with that byte cleared before forwarding. On a host that serializes only up to 1.1 (Windows 10's built-in -// d3d12.dll), version 1.2 descriptions are rewritten as 1.1 when no static sampler uses 1.2's flags. +// d3d12.dll), version 1.2 descriptions are rewritten as 1.1 when no static sampler uses 1.2's flags, and the same +// host refuses the two heap-directly-indexed flags at every version, so they are serialized without them and put +// back into the blob (d3d12_xs_rootsig.h). // D3D12XboxCreateDevice returns an Xbox-ABI proxy (d3d12_xs_device.h) over the PC device; only the measured slots // are overridden, the rest follow the PC vtable. #include @@ -84,9 +86,9 @@ HRESULT WINAPI serialize_root_signature(const D3D12_VERSIONED_ROOT_SIGNATURE_DES std::fprintf(stderr, "[rt] d3d12_xs: the host d3d12.dll serializes root signatures only up to 1.1; version 1.2 " "descriptions without static sampler flags are serialized as 1.1\n"); - return fn(&v11, blob, error); + return rt::d3d12_xs::serialize_versioned_keeping_heap_flags(fn, v11, blob, error); } - return fn(&cleared, blob, error); + return rt::d3d12_xs::serialize_versioned_keeping_heap_flags(fn, cleared, blob, error); } // The package DLLs import the versioned serializer; the legacy entry point is kept because it is trivial to forward @@ -100,7 +102,7 @@ HRESULT WINAPI serialize_root_signature_legacy(const D3D12_ROOT_SIGNATURE_DESC* D3D12_ROOT_SIGNATURE_DESC cleared{}; rt::d3d12_xs::copy_root_signature_desc_clear_xbox_flags(*desc, cleared); - return fn(&cleared, version, blob, error); + return rt::d3d12_xs::serialize_legacy_keeping_heap_flags(fn, cleared, version, blob, error); } HRESULT WINAPI create_root_signature_deserializer(const void* data, SIZE_T size, REFIID iid, void** out) { diff --git a/runtime/shims/d3d12_xs_rootsig.cpp b/runtime/shims/d3d12_xs_rootsig.cpp index a49cf04253..ec96abcedf 100644 --- a/runtime/shims/d3d12_xs_rootsig.cpp +++ b/runtime/shims/d3d12_xs_rootsig.cpp @@ -1,6 +1,7 @@ #include "d3d12_xs_rootsig.h" #include +#include namespace rt::d3d12_xs { namespace { @@ -129,13 +130,15 @@ void copy_root_signature_desc_clear_xbox_flags(const D3D12_ROOT_SIGNATURE_DESC& out.Flags = clear_high_byte(in.Flags); } -bool copy_rts0_blob_clear_xbox_flags(const void* data, size_t size, std::vector& out) { - out.clear(); - if (!data || size < 36 || size - 20 > 0x1FFFFFFFu) return false; +namespace { - const uint8_t* bytes = static_cast(data); - // The host deserializer only consumes the DXBC container the serializer - // produces, so a non-DXBC blob is forwarded unchanged (no rewrite). +// Offset of the RTS0 chunk in a DXBC container, after checking the container hash and every chunk-table entry. +// A blob whose hash does not match is refused (fail closed, so a corrupted blob reaches the host unchanged); +// an all-zero hash is accepted and reported in `zero_hash` so a rewrite keeps it zeroed, as the host does. +bool find_rts0(const uint8_t* bytes, size_t size, uint32_t& rts0_off, bool& zero_hash) { + if (!bytes || size < 36 || size - 20 > 0x1FFFFFFFu) return false; + // The host deserializer only consumes the DXBC container the serializer produces, so a non-DXBC blob is + // forwarded unchanged (no rewrite). if (std::memcmp(bytes, "DXBC", 4) != 0) return false; uint32_t total_size = 0; @@ -146,12 +149,8 @@ bool copy_rts0_blob_clear_xbox_flags(const void* data, size_t size, std::vector< std::memcpy(&chunk_count, bytes + 28, sizeof(chunk_count)); if (chunk_count == 0 || chunk_count > 16 || static_cast(chunk_count) * 4 > size - 32) return false; - // Verify the incoming container hash before rewriting: a corrupted blob must reach the host deserializer - // unchanged so it is rejected, rather than getting a freshly forged valid hash. Host d3d12.dll accepts a - // matching DXBC checksum or an all-zero one and rejects any other with E_INVALIDARG, so an all-zero checksum - // is accepted here too and stays zero in the rewritten blob. constexpr uint8_t kZeroHash[16] = {}; - const bool zero_hash = std::memcmp(bytes + 4, kZeroHash, sizeof(kZeroHash)) == 0; + zero_hash = std::memcmp(bytes + 4, kZeroHash, sizeof(kZeroHash)) == 0; if (!zero_hash) { uint32_t expected_hash[4]; dxbc_checksum(bytes, size, expected_hash); @@ -160,8 +159,7 @@ bool copy_rts0_blob_clear_xbox_flags(const void* data, size_t size, std::vector< // Fail closed on any malformed chunk-table entry (5c3487a94). const size_t min_chunk_off = 32 + static_cast(chunk_count) * 4; - bool found_rts0 = false; - uint32_t rts0_off = 0; + bool found = false; for (uint32_t i = 0; i < chunk_count; ++i) { uint32_t off = 0; std::memcpy(&off, bytes + 32 + static_cast(i) * 4, sizeof(off)); @@ -169,13 +167,45 @@ bool copy_rts0_blob_clear_xbox_flags(const void* data, size_t size, std::vector< uint32_t chunk_size = 0; std::memcpy(&chunk_size, bytes + off + 4, sizeof(chunk_size)); if (chunk_size > size - off - 8) return false; - if (!found_rts0 && std::memcmp(bytes + off, "RTS0", 4) == 0) { + if (!found && std::memcmp(bytes + off, "RTS0", 4) == 0) { if (chunk_size < 24) return false; - found_rts0 = true; + found = true; rts0_off = off; } } - if (!found_rts0) return false; + return found; +} + +// Rewrite the RTS0 flags dword of `blob` to `flags`, re-signing the container when it carried a hash. Used to put +// back the bits the host serializer refused; unlike copy_rts0_blob_clear_xbox_flags it does not require them to +// differ from what the blob already has. +bool rewrite_rts0_flags(const void* data, size_t size, uint32_t flags, std::vector& out) { + out.clear(); + const uint8_t* bytes = static_cast(data); + uint32_t rts0_off = 0; + bool zero_hash = false; + if (!find_rts0(bytes, size, rts0_off, zero_hash)) return false; + + out.assign(bytes, bytes + size); + std::memcpy(out.data() + rts0_off + 8 + 20, &flags, sizeof(flags)); + if (!zero_hash) { + uint32_t hash[4]; + dxbc_checksum(out.data(), out.size(), hash); + std::memcpy(out.data() + 4, hash, sizeof(hash)); + } + return true; +} + +} // namespace + +bool copy_rts0_blob_clear_xbox_flags(const void* data, size_t size, std::vector& out) { + out.clear(); + if (!data) return false; + + const uint8_t* bytes = static_cast(data); + uint32_t rts0_off = 0; + bool zero_hash = false; + if (!find_rts0(bytes, size, rts0_off, zero_hash)) return false; const uint8_t* payload = bytes + rts0_off + 8; uint32_t rts_version = 0; @@ -200,4 +230,138 @@ bool copy_rts0_blob_clear_xbox_flags(const void* data, size_t size, std::vector< return true; } +namespace { + +class RewrittenBlob final : public ID3DBlob { +public: + explicit RewrittenBlob(std::vector bytes) : bytes_(std::move(bytes)) {} + HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** out) override { + if (!out) return E_POINTER; + if (riid == __uuidof(IUnknown) || riid == __uuidof(ID3D10Blob)) { + AddRef(); + *out = static_cast(this); + return S_OK; + } + *out = nullptr; + return E_NOINTERFACE; + } + ULONG STDMETHODCALLTYPE AddRef() override { return ++refs_; } + ULONG STDMETHODCALLTYPE Release() override { + const ULONG left = --refs_; + if (!left) delete this; + return left; + } + LPVOID STDMETHODCALLTYPE GetBufferPointer() override { return bytes_.data(); } + SIZE_T STDMETHODCALLTYPE GetBufferSize() override { return bytes_.size(); } + +private: + std::vector bytes_; + ULONG refs_ = 1; +}; + +HRESULT make_blob(std::vector bytes, ID3DBlob** out) { + if (!out) return E_POINTER; + *out = new (std::nothrow) RewrittenBlob(std::move(bytes)); + return *out ? S_OK : E_OUTOFMEMORY; +} + +// Hands `blob` and `error` to the caller, releasing a blob it did not ask for. +void give(ID3DBlob** slot, ID3DBlob* blob) { + if (slot) + *slot = blob; + else if (blob) + blob->Release(); +} + +// Shared retry: `first` is the failed attempt on `flags`, `error` the host's error blob. Calls `fn` again without +// the directly-indexed bits, then restores them. `call` is `fn(&stripped, &plain, &plain_error)`'s result. +HRESULT finish_retry(bool retried, HRESULT hr, uint32_t flags, ID3DBlob* first, ID3DBlob* first_error, + ID3DBlob* plain, ID3DBlob* plain_error, ID3DBlob** blob, ID3DBlob** error) { + if (!retried) { + give(blob, first); + give(error, first_error); + return hr; + } + if (first) first->Release(); + if (first_error) first_error->Release(); + if (FAILED(hr) || !plain) { + give(blob, plain); + give(error, plain_error); + return FAILED(hr) ? hr : S_FALSE; + } + std::vector patched; + if (!rewrite_rts0_flags(plain->GetBufferPointer(), plain->GetBufferSize(), flags, patched)) { + give(blob, plain); + give(error, plain_error); + return S_FALSE; // a usable blob, but the flags are lost + } + ID3DBlob* rewritten = nullptr; + const HRESULT made = make_blob(std::move(patched), &rewritten); + plain->Release(); + if (FAILED(made)) { + give(blob, nullptr); + give(error, plain_error); + return made; + } + give(blob, rewritten); + give(error, plain_error); + return S_OK; +} + +} // namespace + +HRESULT serialize_versioned_keeping_heap_flags(VersionedSerializeFn fn, const D3D12_VERSIONED_ROOT_SIGNATURE_DESC& desc, + ID3DBlob** blob, ID3DBlob** error) { + if (!fn) return E_NOTIMPL; + uint32_t flags = 0; + switch (desc.Version) { + case D3D_ROOT_SIGNATURE_VERSION_1_0: flags = static_cast(desc.Desc_1_0.Flags); break; + case D3D_ROOT_SIGNATURE_VERSION_1_1: flags = static_cast(desc.Desc_1_1.Flags); break; + case D3D_ROOT_SIGNATURE_VERSION_1_2: flags = static_cast(desc.Desc_1_2.Flags); break; + default: break; + } + + ID3DBlob* first = nullptr; + ID3DBlob* first_error = nullptr; + const HRESULT hr = fn(&desc, &first, &first_error); + const bool retry = FAILED(hr) && (flags & kDirectlyIndexedFlags) != 0; + if (!retry) return finish_retry(false, hr, flags, first, first_error, nullptr, nullptr, blob, error); + + D3D12_VERSIONED_ROOT_SIGNATURE_DESC stripped = desc; + const uint32_t kept = flags & ~kDirectlyIndexedFlags; + switch (stripped.Version) { + case D3D_ROOT_SIGNATURE_VERSION_1_0: + stripped.Desc_1_0.Flags = static_cast(kept); + break; + case D3D_ROOT_SIGNATURE_VERSION_1_1: + stripped.Desc_1_1.Flags = static_cast(kept); + break; + default: + stripped.Desc_1_2.Flags = static_cast(kept); + break; + } + ID3DBlob* plain = nullptr; + ID3DBlob* plain_error = nullptr; + const HRESULT again = fn(&stripped, &plain, &plain_error); + return finish_retry(true, again, flags, first, first_error, plain, plain_error, blob, error); +} + +HRESULT serialize_legacy_keeping_heap_flags(LegacySerializeFn fn, const D3D12_ROOT_SIGNATURE_DESC& desc, + D3D_ROOT_SIGNATURE_VERSION version, ID3DBlob** blob, ID3DBlob** error) { + if (!fn) return E_NOTIMPL; + const uint32_t flags = static_cast(desc.Flags); + ID3DBlob* first = nullptr; + ID3DBlob* first_error = nullptr; + const HRESULT hr = fn(&desc, version, &first, &first_error); + const bool retry = FAILED(hr) && (flags & kDirectlyIndexedFlags) != 0; + if (!retry) return finish_retry(false, hr, flags, first, first_error, nullptr, nullptr, blob, error); + + D3D12_ROOT_SIGNATURE_DESC stripped = desc; + stripped.Flags = static_cast(flags & ~kDirectlyIndexedFlags); + ID3DBlob* plain = nullptr; + ID3DBlob* plain_error = nullptr; + const HRESULT again = fn(&stripped, version, &plain, &plain_error); + return finish_retry(true, again, flags, first, first_error, plain, plain_error, blob, error); +} + } // namespace rt::d3d12_xs diff --git a/runtime/shims/d3d12_xs_rootsig.h b/runtime/shims/d3d12_xs_rootsig.h index 9f742ea35a..f706af61ea 100644 --- a/runtime/shims/d3d12_xs_rootsig.h +++ b/runtime/shims/d3d12_xs_rootsig.h @@ -13,6 +13,14 @@ namespace rt::d3d12_xs { constexpr uint32_t kXboxRootSignatureFlagHighByte = 0xFF000000u; +// The two flags that let a shader index a descriptor heap directly. The in-box d3d12.dll of Windows 10 (measured on +// 10.0.19041.4355) refuses either of them in the serializer, at every descriptor version, with E_INVALIDARG +// ("Unsupported bit-flag set"), although its deserializer and its devices take a blob that carries them. The wrappers +// below serialize again without these bits and put them back into the produced RTS0 chunk. +constexpr uint32_t kDirectlyIndexedFlags = + static_cast(D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) | + static_cast(D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED); + // Always copies `in` to `out`. Returns true for the known 1.0/1.1/1.2 descriptor versions and clears the high flag // byte in the copy. Returns false for an unknown version, leaving the copy byte-for-byte identical to `in` so the // caller can forward it unchanged and log the unsupported version. @@ -46,4 +54,23 @@ bool copy_rts0_blob_clear_xbox_flags(const void* data, size_t size, std::vector< // `size < 20`, or the hashed payload exceeds the 32-bit bit-count limit. void dxbc_checksum(const uint8_t* blob, size_t size, uint32_t out[4]); +using VersionedSerializeFn = HRESULT(WINAPI*)(const D3D12_VERSIONED_ROOT_SIGNATURE_DESC*, ID3DBlob**, ID3DBlob**); +using LegacySerializeFn = HRESULT(WINAPI*)(const D3D12_ROOT_SIGNATURE_DESC*, D3D_ROOT_SIGNATURE_VERSION, ID3DBlob**, + ID3DBlob**); + +// Serialize through `fn` (the host entry point), cooperating with a host that refuses kDirectlyIndexedFlags: when the +// first call fails and the description carries any of those bits, it calls `fn` again with them cleared and, if that +// works, restores them in the produced RTS0 chunk and recomputes the container hash. A host that takes the flags +// itself (Windows 11) gets one call and its own blob back unchanged. +// +// Returns S_OK with a blob that carries the flags, S_FALSE when the retry worked but the blob could not be patched +// (usable, flags lost), or the host's failure from the retry. `blob`/`error` may be null and receive what a direct +// call would. +HRESULT serialize_versioned_keeping_heap_flags(VersionedSerializeFn fn, const D3D12_VERSIONED_ROOT_SIGNATURE_DESC& desc, + ID3DBlob** blob, ID3DBlob** error); + +// The same for the legacy D3D12SerializeRootSignature entry point and its version-1.0 descriptor. +HRESULT serialize_legacy_keeping_heap_flags(LegacySerializeFn fn, const D3D12_ROOT_SIGNATURE_DESC& desc, + D3D_ROOT_SIGNATURE_VERSION version, ID3DBlob** blob, ID3DBlob** error); + } // namespace rt::d3d12_xs diff --git a/runtime/test/d3d12_test_shaders.h b/runtime/test/d3d12_test_shaders.h index d778d1bc69..3c9b43a3cf 100644 --- a/runtime/test/d3d12_test_shaders.h +++ b/runtime/test/d3d12_test_shaders.h @@ -8,11 +8,14 @@ #include #include +#include #include #include #include #include +#include "../shims/d3d12_xs_rootsig.h" + namespace d3d12test { inline void put32(std::vector& out, std::uint32_t v) { @@ -69,6 +72,33 @@ inline std::vector shader_container(const std::vector(desc.Desc_1_0.Flags); + case D3D_ROOT_SIGNATURE_VERSION_1_1: return static_cast(desc.Desc_1_1.Flags); + case D3D_ROOT_SIGNATURE_VERSION_1_2: return static_cast(desc.Desc_1_2.Flags); + default: return 0; + } +} + +inline void set_root_signature_flags(D3D12_VERSIONED_ROOT_SIGNATURE_DESC& desc, std::uint32_t flags) { + switch (desc.Version) { + case D3D_ROOT_SIGNATURE_VERSION_1_0: + desc.Desc_1_0.Flags = static_cast(flags); + break; + case D3D_ROOT_SIGNATURE_VERSION_1_1: + desc.Desc_1_1.Flags = static_cast(flags); + break; + default: + desc.Desc_1_2.Flags = static_cast(flags); + break; + } +} + +// The in-box d3d12.dll of Windows 10 refuses the two heap-directly-indexed root-signature flags at every descriptor +// version ("Unsupported bit-flag set") although its deserializer and devices take a blob that carries them. Retry +// without them and put them back, like the shim does. The DXBC hash is zeroed rather than recomputed: the host +// accepts an all-zero checksum, and the device these blobs go to here parses the container itself. inline std::vector serialize_root_signature(const D3D12_VERSIONED_ROOT_SIGNATURE_DESC& desc) { const HMODULE d3d12 = LoadLibraryExA("d3d12.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); const auto fn = d3d12 ? reinterpret_cast( @@ -76,9 +106,28 @@ inline std::vector serialize_root_signature(const D3D12_VERSIONED_ : nullptr; std::vector out; ID3DBlob* blob = nullptr; - if (fn && SUCCEEDED(fn(&desc, &blob, nullptr)) && blob) { + if (!fn) return out; + + const std::uint32_t flags = root_signature_flags(desc); + HRESULT hr = fn(&desc, &blob, nullptr); + bool retried = false; + if (FAILED(hr) && blob == nullptr && (flags & rt::d3d12_xs::kDirectlyIndexedFlags)) { + D3D12_VERSIONED_ROOT_SIGNATURE_DESC stripped = desc; + set_root_signature_flags(stripped, flags & ~rt::d3d12_xs::kDirectlyIndexedFlags); + hr = fn(&stripped, &blob, nullptr); + retried = SUCCEEDED(hr) && blob != nullptr; + } + if (SUCCEEDED(hr) && blob) { const auto* p = static_cast(blob->GetBufferPointer()); out.assign(p, p + blob->GetBufferSize()); + if (retried) { + for (std::size_t i = 32; i + 8 <= out.size(); ++i) { + if (std::memcmp(out.data() + i, "RTS0", 4) != 0) continue; + std::memcpy(out.data() + i + 8 + 20, &flags, sizeof(flags)); + std::memset(out.data() + 4, 0, 16); + break; + } + } blob->Release(); } return out; diff --git a/runtime/test/d3d12_xs_root_signature_test.cpp b/runtime/test/d3d12_xs_root_signature_test.cpp index aee02246f0..a39ca1c157 100644 --- a/runtime/test/d3d12_xs_root_signature_test.cpp +++ b/runtime/test/d3d12_xs_root_signature_test.cpp @@ -268,6 +268,62 @@ void test_version_1_2(SerializeFn serialize, DeserializeFn deserialize, ID3D12De std::puts("PASS root signature 1.2 serialization, versioned reflection, and legacy comparison"); } +void check_directly_indexed_flags(SerializeFn serialize, DeserializeFn deserialize, ID3D12Device& device, + D3D_ROOT_SIGNATURE_VERSION version) { + D3D12_ROOT_PARAMETER constants{}; + constants.ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; + constants.Constants = {0, 0, 2}; + D3D12_ROOT_PARAMETER1 constants1{}; + constants1.ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; + constants1.Constants = {0, 0, 2}; + const auto flags = static_cast(D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) | + static_cast(D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED); + D3D12_VERSIONED_ROOT_SIGNATURE_DESC desc{}; + desc.Version = version; + if (version == D3D_ROOT_SIGNATURE_VERSION_1_0) + desc.Desc_1_0 = {1, &constants, 0, nullptr, static_cast(flags)}; + else + desc.Desc_1_1 = {1, &constants1, 0, nullptr, static_cast(flags)}; + + // The in-box serializer of Windows 10 refuses these bits; Windows 11 takes them. Either way the shim must hand + // back a blob that still carries them. + ComPtr direct, direct_error, shim, shim_error; + const HRESULT direct_hr = D3D12SerializeVersionedRootSignature(&desc, &direct, &direct_error); + const HRESULT shim_hr = serialize(&desc, &shim, &shim_error); + require_success(shim_hr, "serialize directly indexed flags via shim"); + require(shim != nullptr && shim_error == nullptr, "shim returns a blob and no error"); + if (SUCCEEDED(direct_hr)) { + // A host that takes the flags itself gets its own blob back unchanged. + require(direct != nullptr && direct->GetBufferSize() == shim->GetBufferSize() && + std::memcmp(direct->GetBufferPointer(), shim->GetBufferPointer(), shim->GetBufferSize()) == 0, + "a host that takes the flags gets its blob back"); + std::printf("PASS host accepts directly indexed flags (version %d)\n", static_cast(version)); + } else { + require(direct_error != nullptr, "the refusing host reports why"); + std::printf("PASS host refuses directly indexed flags (version %d, 0x%08lX); shim retried\n", + static_cast(version), static_cast(direct_hr)); + } + + const void* data = shim->GetBufferPointer(); + const SIZE_T size = shim->GetBufferSize(); + ComPtr layout; + require_success(D3D12CreateRootSignatureDeserializer(data, size, IID_PPV_ARGS(&layout)), + "deserialize directly indexed blob"); + require((static_cast(layout->GetRootSignatureDesc()->Flags) & flags) == flags, + "directly indexed flags survive in the blob"); + + ComPtr shim_layout; + require_success(deserialize(data, size, IID_PPV_ARGS(&shim_layout)), "deserialize directly indexed via shim"); + require((static_cast(shim_layout->GetRootSignatureDesc()->Flags) & flags) == flags, + "directly indexed flags survive the shim deserializer"); + + ComPtr signature; + require_success(device.CreateRootSignature(0, data, size, IID_PPV_ARGS(&signature)), + "create WARP directly indexed root signature"); + require(signature != nullptr, "WARP returned a directly indexed root signature"); + std::puts("PASS directly indexed flags survive serialization and are accepted by the device"); +} + } // namespace int main() { @@ -292,6 +348,8 @@ int main() { test_version_1_0(serialize, deserialize, *device.Get()); test_version_1_1(serialize, deserialize, *device.Get()); test_version_1_2(serialize, deserialize, *device.Get()); + check_directly_indexed_flags(serialize, deserialize, *device.Get(), D3D_ROOT_SIGNATURE_VERSION_1_0); + check_directly_indexed_flags(serialize, deserialize, *device.Get(), D3D_ROOT_SIGNATURE_VERSION_1_1); } catch (const std::exception& error) { std::fprintf(stderr, "FAIL %s\n", error.what()); return 1; diff --git a/runtime/test/root_sig_test.cpp b/runtime/test/root_sig_test.cpp index ac65075eed..78857fd661 100644 --- a/runtime/test/root_sig_test.cpp +++ b/runtime/test/root_sig_test.cpp @@ -269,6 +269,37 @@ void test_legacy_entry_point() { Blobs blobs; HRESULT hr = D3D12SerializeRootSignature(&cleared, D3D_ROOT_SIGNATURE_VERSION_1_0, &blobs.blob, &blobs.error); check(SUCCEEDED(hr), "legacy copy with cleared flags is accepted by the PC serializer"); + + // The directly indexed bits meet a host that refuses them even at flags 0: the shim retries without them and + // puts them back in the blob. Reached through the registry, since the name above is the host import. + using LegacyFn = HRESULT(WINAPI*)(const D3D12_ROOT_SIGNATURE_DESC*, D3D_ROOT_SIGNATURE_VERSION, ID3DBlob**, + ID3DBlob**); + const auto legacy = + reinterpret_cast(rt::shims::find("d3d12_xs.dll", "D3D12SerializeRootSignature")); + check(legacy != nullptr, "the legacy shim is reachable"); + D3D12_ROOT_SIGNATURE_DESC indexed = make_legacy_desc(rt::d3d12_xs::kDirectlyIndexedFlags); + Blobs retried; + hr = legacy ? legacy(&indexed, D3D_ROOT_SIGNATURE_VERSION_1_0, &retried.blob, &retried.error) : E_FAIL; + check(SUCCEEDED(hr) && retried.blob != nullptr, "legacy shim serializes with the directly indexed flags"); + check(indexed.Flags == static_cast(rt::d3d12_xs::kDirectlyIndexedFlags), + "legacy original keeps the directly indexed flags"); + if (retried.blob) { + const uint8_t* bytes = static_cast(retried.blob->GetBufferPointer()); + const SIZE_T size = retried.blob->GetBufferSize(); + uint32_t chunk_count = 0; + std::memcpy(&chunk_count, bytes + 28, sizeof(chunk_count)); + bool kept = false; + for (uint32_t i = 0; i < chunk_count; ++i) { + uint32_t off = 0; + std::memcpy(&off, bytes + 32 + i * 4, sizeof(off)); + if (off > size || size - off < 8 + 24) continue; + if (std::memcmp(bytes + off, "RTS0", 4) != 0) continue; + uint32_t flags = 0; + std::memcpy(&flags, bytes + off + 8 + 20, sizeof(flags)); + kept = flags == rt::d3d12_xs::kDirectlyIndexedFlags; + } + check(kept, "the directly indexed flags are back in the legacy RTS0 chunk"); + } } void test_preserves_low_bits_and_unknown_versions() {