diff --git a/runtime/shims/d3d12_xs.cpp b/runtime/shims/d3d12_xs.cpp index 8e54c3fd56..6ad6d112db 100644 --- a/runtime/shims/d3d12_xs.cpp +++ b/runtime/shims/d3d12_xs.cpp @@ -53,8 +53,89 @@ bool host_serializes_1_2(SerializeFn fn) { return supported; } +HRESULT serialize_root_signature_unlogged(const D3D12_VERSIONED_ROOT_SIGNATURE_DESC* desc, ID3DBlob** blob, + ID3DBlob** error); + +// A refused description is reported with the host's message (the first few): the title treats a failed serialization +// as a program that cannot be linked and leaves a null in its pass table. HRESULT WINAPI serialize_root_signature(const D3D12_VERSIONED_ROOT_SIGNATURE_DESC* desc, ID3DBlob** blob, ID3DBlob** error) { + ID3DBlob* local_error = nullptr; + ID3DBlob** error_out = error ? error : &local_error; + const HRESULT hr = serialize_root_signature_unlogged(desc, blob, error_out); + if (FAILED(hr)) { + static std::atomic reported{0}; + if (reported.fetch_add(1, std::memory_order_relaxed) < 8) { + const char* text = *error_out ? static_cast((*error_out)->GetBufferPointer()) : nullptr; + std::fprintf(stderr, + "[rt] d3d12_xs: D3D12SerializeVersionedRootSignature (version %d, %u parameter(s), flags " + "0x%x) failed 0x%08lx: %.300s\n", + desc ? static_cast(desc->Version) : -1, + desc && desc->Version == D3D_ROOT_SIGNATURE_VERSION_1_1 ? desc->Desc_1_1.NumParameters : 0u, + desc && desc->Version == D3D_ROOT_SIGNATURE_VERSION_1_1 + ? static_cast(desc->Desc_1_1.Flags) + : 0u, + static_cast(hr), text ? text : "(no message)"); + } + } + if (local_error) local_error->Release(); + return hr; +} + +D3D12_ROOT_SIGNATURE_FLAGS* versioned_flags(D3D12_VERSIONED_ROOT_SIGNATURE_DESC& desc) { + switch (desc.Version) { + case D3D_ROOT_SIGNATURE_VERSION_1_0: return &desc.Desc_1_0.Flags; + case D3D_ROOT_SIGNATURE_VERSION_1_1: return &desc.Desc_1_1.Flags; + case D3D_ROOT_SIGNATURE_VERSION_1_2: return &desc.Desc_1_2.Flags; + default: return nullptr; + } +} + +// Root-signature flags the host serializer refuses, asked once per bit with an otherwise empty 1.1 description. +// Windows 10's built-in d3d12.dll predates shader model 6.6 and refuses CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED (0x400) +// and SAMPLER_HEAP_DIRECTLY_INDEXED (0x800) with "Unsupported bit-flag set" (measured, boot 9); the title's programs +// use both, and a failed serialization leaves a null program in its pass table (the crash at +0xb5a5f8). +std::uint32_t host_refused_flags(SerializeFn fn) { + static const std::uint32_t refused = [fn] { + std::uint32_t mask = 0; + for (const std::uint32_t bit : {0x100u, 0x200u, 0x400u, 0x800u}) { + D3D12_VERSIONED_ROOT_SIGNATURE_DESC probe{}; + probe.Version = D3D_ROOT_SIGNATURE_VERSION_1_1; + probe.Desc_1_1.Flags = static_cast(bit); + ID3DBlob* blob = nullptr; + ID3DBlob* error = nullptr; + if (FAILED(fn(&probe, &blob, &error))) mask |= bit; + if (blob) blob->Release(); + if (error) error->Release(); + } + if (mask) + std::fprintf(stderr, + "[rt] d3d12_xs: the host d3d12.dll refuses root signature flags 0x%x; they are serialized " + "without and written back into the blob\n", + mask); + return mask; + }(); + return refused; +} + +// Serializes with the refused flags taken out, then puts them back into the RTS0 chunk of the result. +HRESULT serialize_moving_flags(SerializeFn fn, D3D12_VERSIONED_ROOT_SIGNATURE_DESC& desc, ID3DBlob** blob, + ID3DBlob** error) { + D3D12_ROOT_SIGNATURE_FLAGS* flags = versioned_flags(desc); + const std::uint32_t moved = flags ? (static_cast(*flags) & host_refused_flags(fn)) : 0; + if (moved) *flags = static_cast(static_cast(*flags) & ~moved); + const HRESULT hr = fn(&desc, blob, error); + if (SUCCEEDED(hr) && moved && blob && *blob && + !rt::d3d12_xs::set_rts0_flag_bits((*blob)->GetBufferPointer(), (*blob)->GetBufferSize(), moved)) { + (*blob)->Release(); + *blob = nullptr; + return E_FAIL; + } + return hr; +} + +HRESULT serialize_root_signature_unlogged(const D3D12_VERSIONED_ROOT_SIGNATURE_DESC* desc, ID3DBlob** blob, + ID3DBlob** error) { static auto fn = reinterpret_cast(host("D3D12SerializeVersionedRootSignature")); if (!fn) return E_NOTIMPL; if (!desc) return fn(desc, blob, error); @@ -84,9 +165,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 serialize_moving_flags(fn, v11, blob, error); } - return fn(&cleared, blob, error); + return serialize_moving_flags(fn, cleared, blob, error); } // The package DLLs import the versioned serializer; the legacy entry point is kept because it is trivial to forward diff --git a/runtime/shims/d3d12_xs_rootsig.cpp b/runtime/shims/d3d12_xs_rootsig.cpp index a49cf04253..ce9c7aeebf 100644 --- a/runtime/shims/d3d12_xs_rootsig.cpp +++ b/runtime/shims/d3d12_xs_rootsig.cpp @@ -200,4 +200,39 @@ bool copy_rts0_blob_clear_xbox_flags(const void* data, size_t size, std::vector< return true; } +bool set_rts0_flag_bits(void* data, size_t size, uint32_t bits) { + if (!data || size < 36 || size - 20 > 0x1FFFFFFFu) return false; + auto* bytes = static_cast(data); + if (std::memcmp(bytes, "DXBC", 4) != 0) return false; + uint32_t total_size = 0; + std::memcpy(&total_size, bytes + 24, sizeof(total_size)); + if (total_size != size) return false; + uint32_t chunk_count = 0; + 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; + const size_t min_chunk_off = 32 + static_cast(chunk_count) * 4; + for (uint32_t i = 0; i < chunk_count; ++i) { + uint32_t off = 0; + std::memcpy(&off, bytes + 32 + static_cast(i) * 4, sizeof(off)); + if (off < min_chunk_off || off > size || size - off < 8) return false; + uint32_t chunk_size = 0; + std::memcpy(&chunk_size, bytes + off + 4, sizeof(chunk_size)); + if (chunk_size > size - off - 8) return false; + if (std::memcmp(bytes + off, "RTS0", 4) != 0) continue; + if (chunk_size < 24) return false; + uint32_t flags = 0; + std::memcpy(&flags, bytes + off + 8 + 20, sizeof(flags)); + flags |= bits; + std::memcpy(bytes + off + 8 + 20, &flags, sizeof(flags)); + constexpr uint8_t kZeroHash[16] = {}; + if (std::memcmp(bytes + 4, kZeroHash, sizeof(kZeroHash)) != 0) { + uint32_t hash[4]; + dxbc_checksum(bytes, size, hash); + std::memcpy(bytes + 4, hash, sizeof(hash)); + } + return true; + } + return false; +} + } // namespace rt::d3d12_xs diff --git a/runtime/shims/d3d12_xs_rootsig.h b/runtime/shims/d3d12_xs_rootsig.h index 9f742ea35a..efdbe06ea3 100644 --- a/runtime/shims/d3d12_xs_rootsig.h +++ b/runtime/shims/d3d12_xs_rootsig.h @@ -42,6 +42,13 @@ void copy_root_signature_desc_clear_xbox_flags(const D3D12_ROOT_SIGNATURE_DESC& // non-zero incoming DXBC hash that does not match the input bytes. bool copy_rts0_blob_clear_xbox_flags(const void* data, size_t size, std::vector& out); +// Sets `bits` in the RTS0 flags of a serialized root signature in place and re-signs the container (an all-zero hash +// stays zero). The serializer path uses it for flags the host d3d12.dll does not know yet (Windows 10's predates the +// shader model 6.6 heap-directly-indexed flags 0x400/0x800): the description is serialized without them and they are +// put back, so CreateRootSignature still sees them. Returns false, leaving the blob unchanged, when it is not a +// well-formed DXBC container with an RTS0 chunk. +bool set_rts0_flag_bits(void* data, size_t size, uint32_t bits); + // The DXBC container hash of `blob` (bytes 20..size), as stored at bytes 4..19. Zeroes `out` when `blob` is null, // `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]); diff --git a/runtime/test/d3d12_xs_root_signature_test.cpp b/runtime/test/d3d12_xs_root_signature_test.cpp index aee02246f0..ce36e2b5c8 100644 --- a/runtime/test/d3d12_xs_root_signature_test.cpp +++ b/runtime/test/d3d12_xs_root_signature_test.cpp @@ -8,6 +8,7 @@ #include #include +#include "d3d12_xs_rootsig.h" #include "shims.h" namespace { @@ -270,6 +271,45 @@ void test_version_1_2(SerializeFn serialize, DeserializeFn deserialize, ID3D12De } // namespace +// The shader model 6.6 heap-directly-indexed flags (0x400, 0x800), which Windows 10's d3d12.dll refuses: the shim +// serializes them anyway (taken out for the host, written back into RTS0), so the blob's flags word carries them and +// its container hash is valid. +void test_heap_directly_indexed_flags(SerializeFn serialize) { + D3D12_ROOT_PARAMETER1 constants{}; + constants.ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; + constants.Constants.Num32BitValues = 4; + constants.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL; + D3D12_VERSIONED_ROOT_SIGNATURE_DESC desc{}; + desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_1; + desc.Desc_1_1.NumParameters = 1; + desc.Desc_1_1.pParameters = &constants; + desc.Desc_1_1.Flags = static_cast(0x400 | 0x800 | 0x1); + ComPtr blob, error; + require_success(serialize(&desc, &blob, &error), "serialize heap-directly-indexed flags via shim"); + const auto* bytes = static_cast(blob->GetBufferPointer()); + const SIZE_T size = blob->GetBufferSize(); + require(size >= 36 && std::memcmp(bytes, "DXBC", 4) == 0, "heap-indexed blob is a DXBC container"); + std::uint32_t count = 0; + std::memcpy(&count, bytes + 28, 4); + bool found = false; + for (std::uint32_t i = 0; i < count; ++i) { + std::uint32_t off = 0; + std::memcpy(&off, bytes + 32 + 4 * i, 4); + if (std::memcmp(bytes + off, "RTS0", 4) != 0) continue; + std::uint32_t flags = 0; + std::memcpy(&flags, bytes + off + 8 + 20, 4); + require(flags == (0x400u | 0x800u | 0x1u), "RTS0 flags keep 0x400 | 0x800"); + found = true; + } + require(found, "heap-indexed blob has an RTS0 chunk"); + std::uint32_t hash[4]; + rt::d3d12_xs::dxbc_checksum(bytes, size, hash); + constexpr std::uint8_t kZero[16] = {}; + require(std::memcmp(bytes + 4, kZero, 16) == 0 || std::memcmp(bytes + 4, hash, 16) == 0, + "heap-indexed blob hash is valid"); + std::printf("heap-directly-indexed flags serialized\n"); +} + int main() { try { const auto serialize = reinterpret_cast( @@ -277,6 +317,7 @@ int main() { const auto deserialize = reinterpret_cast( rt::shims::find("d3d12_xs.dll", "D3D12CreateRootSignatureDeserializer")); require(serialize && deserialize, "resolve both root signature imports from the shim registry"); + test_heap_directly_indexed_flags(serialize); ComPtr factory; ComPtr adapter;