Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions runtime/shims/d3d12_xs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <windows.h>
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
196 changes: 180 additions & 16 deletions runtime/shims/d3d12_xs_rootsig.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "d3d12_xs_rootsig.h"

#include <cstring>
#include <new>

namespace rt::d3d12_xs {
namespace {
Expand Down Expand Up @@ -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<uint8_t>& out) {
out.clear();
if (!data || size < 36 || size - 20 > 0x1FFFFFFFu) return false;
namespace {

const uint8_t* bytes = static_cast<const uint8_t*>(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;
Expand All @@ -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<size_t>(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);
Expand All @@ -160,22 +159,53 @@ 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<size_t>(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<size_t>(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 (!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<uint8_t>& out) {
out.clear();
const uint8_t* bytes = static_cast<const uint8_t*>(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<uint8_t>& out) {
out.clear();
if (!data) return false;

const uint8_t* bytes = static_cast<const uint8_t*>(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;
Expand All @@ -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<uint8_t> 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<ID3DBlob*>(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<uint8_t> bytes_;
ULONG refs_ = 1;
};

HRESULT make_blob(std::vector<uint8_t> 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<uint8_t> 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<uint32_t>(desc.Desc_1_0.Flags); break;
case D3D_ROOT_SIGNATURE_VERSION_1_1: flags = static_cast<uint32_t>(desc.Desc_1_1.Flags); break;
case D3D_ROOT_SIGNATURE_VERSION_1_2: flags = static_cast<uint32_t>(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<D3D12_ROOT_SIGNATURE_FLAGS>(kept);
break;
case D3D_ROOT_SIGNATURE_VERSION_1_1:
stripped.Desc_1_1.Flags = static_cast<D3D12_ROOT_SIGNATURE_FLAGS>(kept);
break;
default:
stripped.Desc_1_2.Flags = static_cast<D3D12_ROOT_SIGNATURE_FLAGS>(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<uint32_t>(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<D3D12_ROOT_SIGNATURE_FLAGS>(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
27 changes: 27 additions & 0 deletions runtime/shims/d3d12_xs_rootsig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t>(D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) |
static_cast<uint32_t>(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.
Expand Down Expand Up @@ -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
Loading