mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 16:58:37 +00:00
The proxy read device slot 62 as (desc, riid, out), so XboxBvhBuilder's constructor, the only known caller, got E_POINTER on its first compute PSO and threw it. Recorded on 1.0.2.1: the DLL calls slot 62 with (desc, 0, 0, &IID_ID3D12PipelineState, out), the shape the prior art had documented (d3d12_vulkan.md, sections 4.8 and 7). Read the IID and the output from the fourth and fifth arguments, and log the two extra words once if they are ever non-zero. device_proxy_test now calls slot 62 in that shape. AI-assisted (Claude Code). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
592 lines
25 KiB
C++
592 lines
25 KiB
C++
// Xbox-ABI proxy around a PC ID3D12Device. See d3d12_xs_device.h for the measured slots and the limits.
|
|
//
|
|
// How forwarding works: the proxy object's first word is a vtable built once from one thunk per slot. A thunk
|
|
// receives the proxy in RCX, replaces it with the PC device pointer from offset 8, and tail-calls the PC method at
|
|
// the same index through a 15-argument integer prototype. Every PC D3D12 device method takes integer/pointer
|
|
// arguments only, so the x64 registers and stack words line up exactly; methods that return a struct keep their
|
|
// hidden return pointer in RDX, which is passed through as the first argument unchanged. The measured exceptions
|
|
// are IUnknown (0-2), slot 14 (which wraps the heap), 62 and 76, plus the heap proxy's 9/10 conversion from the
|
|
// PC hidden-pointer form to the Xbox RAX form.
|
|
#include "d3d12_xs_device.h"
|
|
|
|
#include <windows.h>
|
|
|
|
#include <atomic>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <new>
|
|
#include <utility>
|
|
|
|
namespace rt::d3d12_xs {
|
|
namespace {
|
|
|
|
constexpr unsigned kMaxSlots = 128;
|
|
static_assert(kMaxSlots > 83, "the proxy table must cover ID3D12Device14 (83 slots)");
|
|
static_assert(sizeof(void*) == 8, "the Xbox-ABI proxy is x64-only");
|
|
static_assert(sizeof(D3D12_CPU_DESCRIPTOR_HANDLE) == 8,
|
|
"the Xbox handle ABI is an 8-byte value in RAX; the heap thunk converts the PC hidden-pointer form");
|
|
static_assert(sizeof(D3D12_RESOURCE_DESC) == 56, "the measured Xbox description prefix is 56 bytes");
|
|
|
|
struct DeviceProxy {
|
|
void** vtable; // offset 0: the caller's ID3D12Device* points here
|
|
ID3D12Device* real; // offset 8: the PC device every thunk forwards to
|
|
void** real_vtable;
|
|
unsigned slot_count; // number of valid entries in real_vtable
|
|
std::atomic<unsigned long> refs;
|
|
};
|
|
|
|
constexpr unsigned kHeapSlots = 11; // IUnknown + ID3D12Object + DeviceChild + Pageable + GetDesc + 2 handles
|
|
|
|
struct HeapProxy {
|
|
void** vtable; // offset 0: the caller's ID3D12DescriptorHeap* points here
|
|
ID3D12DescriptorHeap* real;
|
|
DeviceProxy* device; // keeps the Xbox-ABI device alive; GetDevice returns it
|
|
void** real_vtable;
|
|
std::atomic<unsigned long> refs;
|
|
};
|
|
|
|
std::atomic<std::uint64_t> g_hits[kMaxSlots];
|
|
std::atomic<bool> g_first[kMaxSlots];
|
|
std::atomic<bool> g_beyond_warned[kMaxSlots];
|
|
std::atomic<std::uint64_t> g_heap_hits[kHeapSlots];
|
|
std::atomic<bool> g_heap_first[kHeapSlots];
|
|
|
|
bool trace_enabled() {
|
|
static const bool enabled = [] {
|
|
char value[8] = {};
|
|
const DWORD length = GetEnvironmentVariableA("XS_TRACE", value, sizeof(value));
|
|
return length > 0 && value[0] != '\0' && value[0] != '0';
|
|
}();
|
|
return enabled;
|
|
}
|
|
|
|
const char* slot_name(unsigned slot) {
|
|
switch (slot) {
|
|
case 0: return "QueryInterface";
|
|
case 1: return "AddRef";
|
|
case 2: return "Release";
|
|
case 9: return "CreateCommandAllocator (PC slot 9)";
|
|
case 10: return "CreateGraphicsPipelineState (PC slot 10)";
|
|
case 11: return "CreateComputePipelineState (PC slot 11)";
|
|
case 14: return "CreateDescriptorHeap";
|
|
case 16: return "CreateRootSignature";
|
|
case 25: return "GetResourceAllocationInfo";
|
|
case 27: return "CreateCommittedResource (PC slot 27)";
|
|
case 43: return "GetAdapterLuid";
|
|
case 62: return "Xbox CreateComputePipelineState";
|
|
case 76: return "Xbox resource creation";
|
|
default: return "forwarded";
|
|
}
|
|
}
|
|
|
|
void note_slot(unsigned slot) {
|
|
if (slot >= kMaxSlots) return;
|
|
g_hits[slot].fetch_add(1, std::memory_order_relaxed);
|
|
bool expected = false;
|
|
const bool first = g_first[slot].compare_exchange_strong(expected, true, std::memory_order_relaxed);
|
|
if (first) {
|
|
std::fprintf(stderr, "[rt] d3d12_xs: device slot %u (%s) first call\n", slot, slot_name(slot));
|
|
} else if (slot >= 3 && trace_enabled()) {
|
|
std::fprintf(stderr, "[rt] d3d12_xs: device slot %u (%s) call\n", slot, slot_name(slot));
|
|
}
|
|
}
|
|
|
|
const char* heap_slot_name(unsigned slot) {
|
|
switch (slot) {
|
|
case 0: return "QueryInterface";
|
|
case 1: return "AddRef";
|
|
case 2: return "Release";
|
|
case 7: return "GetDevice";
|
|
case 8: return "GetDesc";
|
|
case 9: return "GetCPUDescriptorHandleForHeapStart";
|
|
case 10: return "GetGPUDescriptorHandleForHeapStart";
|
|
default: return "forwarded";
|
|
}
|
|
}
|
|
|
|
void note_heap_slot(unsigned slot) {
|
|
if (slot >= kHeapSlots) return;
|
|
g_heap_hits[slot].fetch_add(1, std::memory_order_relaxed);
|
|
bool expected = false;
|
|
const bool first = g_heap_first[slot].compare_exchange_strong(expected, true, std::memory_order_relaxed);
|
|
if (first) {
|
|
std::fprintf(stderr, "[rt] d3d12_xs: heap slot %u (%s) first call\n", slot, heap_slot_name(slot));
|
|
} else if (slot >= 3 && trace_enabled()) {
|
|
std::fprintf(stderr, "[rt] d3d12_xs: heap slot %u (%s) call\n", slot, heap_slot_name(slot));
|
|
}
|
|
}
|
|
|
|
std::uint64_t hr_to_u64(HRESULT hr) {
|
|
return static_cast<std::uint64_t>(static_cast<std::uint32_t>(hr));
|
|
}
|
|
|
|
unsigned long proxy_add_ref(DeviceProxy* self) {
|
|
note_slot(1);
|
|
return self->refs.fetch_add(1, std::memory_order_relaxed) + 1;
|
|
}
|
|
|
|
unsigned long proxy_release(DeviceProxy* self) {
|
|
note_slot(2);
|
|
const unsigned long refs = self->refs.fetch_sub(1, std::memory_order_acq_rel) - 1;
|
|
if (refs == 0) {
|
|
ID3D12Device* real = self->real;
|
|
delete self;
|
|
if (real) real->Release();
|
|
}
|
|
return refs;
|
|
}
|
|
|
|
bool is_device_iid(const IID& iid) {
|
|
if (IsEqualIID(iid, __uuidof(ID3D12Device))) return true;
|
|
#ifdef __ID3D12Device14_INTERFACE_DEFINED__
|
|
if (IsEqualIID(iid, __uuidof(ID3D12Device14))) return true;
|
|
#endif
|
|
#ifdef __ID3D12Device13_INTERFACE_DEFINED__
|
|
if (IsEqualIID(iid, __uuidof(ID3D12Device13))) return true;
|
|
#endif
|
|
#ifdef __ID3D12Device12_INTERFACE_DEFINED__
|
|
if (IsEqualIID(iid, __uuidof(ID3D12Device12))) return true;
|
|
#endif
|
|
#ifdef __ID3D12Device11_INTERFACE_DEFINED__
|
|
if (IsEqualIID(iid, __uuidof(ID3D12Device11))) return true;
|
|
#endif
|
|
#ifdef __ID3D12Device10_INTERFACE_DEFINED__
|
|
if (IsEqualIID(iid, __uuidof(ID3D12Device10))) return true;
|
|
#endif
|
|
#ifdef __ID3D12Device9_INTERFACE_DEFINED__
|
|
if (IsEqualIID(iid, __uuidof(ID3D12Device9))) return true;
|
|
#endif
|
|
if (IsEqualIID(iid, __uuidof(ID3D12Device8))) return true;
|
|
if (IsEqualIID(iid, __uuidof(ID3D12Device7))) return true;
|
|
if (IsEqualIID(iid, __uuidof(ID3D12Device6))) return true;
|
|
if (IsEqualIID(iid, __uuidof(ID3D12Device5))) return true;
|
|
if (IsEqualIID(iid, __uuidof(ID3D12Device4))) return true;
|
|
if (IsEqualIID(iid, __uuidof(ID3D12Device3))) return true;
|
|
if (IsEqualIID(iid, __uuidof(ID3D12Device2))) return true;
|
|
if (IsEqualIID(iid, __uuidof(ID3D12Device1))) return true;
|
|
return false;
|
|
}
|
|
|
|
HRESULT proxy_query_interface(DeviceProxy* self, const IID* riid, void** out) {
|
|
note_slot(0);
|
|
if (!out) return E_POINTER;
|
|
*out = nullptr;
|
|
if (!riid) return E_POINTER;
|
|
if (IsEqualIID(*riid, __uuidof(IUnknown)) || IsEqualIID(*riid, __uuidof(ID3D12Object))) {
|
|
*out = self;
|
|
proxy_add_ref(self);
|
|
return S_OK;
|
|
}
|
|
if (!is_device_iid(*riid)) return E_NOINTERFACE;
|
|
|
|
// Only claim an interface the PC device really implements; the proxy vtable covers its slots.
|
|
void* supported = nullptr;
|
|
HRESULT hr = self->real->QueryInterface(*riid, &supported);
|
|
if (FAILED(hr)) return hr;
|
|
if (!supported) return E_NOINTERFACE;
|
|
static_cast<IUnknown*>(supported)->Release();
|
|
*out = self;
|
|
proxy_add_ref(self);
|
|
return S_OK;
|
|
}
|
|
|
|
unsigned long heap_add_ref(HeapProxy* self) {
|
|
note_heap_slot(1);
|
|
return self->refs.fetch_add(1, std::memory_order_relaxed) + 1;
|
|
}
|
|
|
|
unsigned long heap_release(HeapProxy* self) {
|
|
note_heap_slot(2);
|
|
const unsigned long refs = self->refs.fetch_sub(1, std::memory_order_acq_rel) - 1;
|
|
if (refs == 0) {
|
|
ID3D12DescriptorHeap* real = self->real;
|
|
DeviceProxy* device = self->device;
|
|
delete self;
|
|
if (real) real->Release();
|
|
if (device) proxy_release(device);
|
|
}
|
|
return refs;
|
|
}
|
|
|
|
bool is_heap_iid(const IID& iid) {
|
|
return IsEqualIID(iid, __uuidof(ID3D12DescriptorHeap)) || IsEqualIID(iid, __uuidof(ID3D12Pageable)) ||
|
|
IsEqualIID(iid, __uuidof(ID3D12DeviceChild)) || IsEqualIID(iid, __uuidof(ID3D12Object)) ||
|
|
IsEqualIID(iid, __uuidof(IUnknown));
|
|
}
|
|
|
|
HRESULT heap_query_interface(HeapProxy* self, const IID* riid, void** out) {
|
|
note_heap_slot(0);
|
|
if (!out) return E_POINTER;
|
|
*out = nullptr;
|
|
if (!riid) return E_POINTER;
|
|
if (!is_heap_iid(*riid)) return E_NOINTERFACE;
|
|
*out = self;
|
|
heap_add_ref(self);
|
|
return S_OK;
|
|
}
|
|
|
|
// ID3D12DeviceChild::GetDevice on the heap must hand back the Xbox-ABI device, not the raw PC device.
|
|
HRESULT heap_get_device(HeapProxy* self, const IID* riid, void** out) {
|
|
note_heap_slot(7);
|
|
if (!riid) return E_POINTER;
|
|
if (!out) return E_POINTER;
|
|
if (!self->device) return E_NOINTERFACE;
|
|
return proxy_query_interface(self->device, riid, out);
|
|
}
|
|
|
|
// The PC methods write through the hidden return pointer (RDX) on the measured runtime; the Xbox ABI wants the
|
|
// 8-byte value in RAX. Call with both conventions and keep whichever produced a value, so a runtime that really
|
|
// returns in RAX also works. The extra register is harmless when the callee ignores it.
|
|
D3D12_CPU_DESCRIPTOR_HANDLE heap_get_cpu_handle(HeapProxy* self) {
|
|
note_heap_slot(9);
|
|
constexpr SIZE_T kUnset = ~static_cast<SIZE_T>(0);
|
|
D3D12_CPU_DESCRIPTOR_HANDLE handle{};
|
|
handle.ptr = kUnset;
|
|
using Fn = D3D12_CPU_DESCRIPTOR_HANDLE(WINAPI*)(ID3D12DescriptorHeap*, D3D12_CPU_DESCRIPTOR_HANDLE*);
|
|
const D3D12_CPU_DESCRIPTOR_HANDLE returned =
|
|
reinterpret_cast<Fn>(self->real_vtable[9])(self->real, &handle);
|
|
return handle.ptr != kUnset ? handle : returned;
|
|
}
|
|
|
|
D3D12_GPU_DESCRIPTOR_HANDLE heap_get_gpu_handle(HeapProxy* self) {
|
|
note_heap_slot(10);
|
|
constexpr UINT64 kUnset = ~static_cast<UINT64>(0);
|
|
D3D12_GPU_DESCRIPTOR_HANDLE handle{};
|
|
handle.ptr = kUnset;
|
|
using Fn = D3D12_GPU_DESCRIPTOR_HANDLE(WINAPI*)(ID3D12DescriptorHeap*, D3D12_GPU_DESCRIPTOR_HANDLE*);
|
|
const D3D12_GPU_DESCRIPTOR_HANDLE returned =
|
|
reinterpret_cast<Fn>(self->real_vtable[10])(self->real, &handle);
|
|
return handle.ptr != kUnset ? handle : returned;
|
|
}
|
|
|
|
template <unsigned Slot>
|
|
std::uint64_t WINAPI heap_slot(HeapProxy* self, std::uint64_t a1, std::uint64_t a2, std::uint64_t a3,
|
|
std::uint64_t a4, std::uint64_t a5, std::uint64_t a6, std::uint64_t a7,
|
|
std::uint64_t a8, std::uint64_t a9, std::uint64_t a10, std::uint64_t a11,
|
|
std::uint64_t a12, std::uint64_t a13, std::uint64_t a14) {
|
|
if constexpr (Slot == 0) {
|
|
return hr_to_u64(heap_query_interface(self, reinterpret_cast<const IID*>(a1),
|
|
reinterpret_cast<void**>(a2)));
|
|
} else if constexpr (Slot == 1) {
|
|
return heap_add_ref(self);
|
|
} else if constexpr (Slot == 2) {
|
|
return heap_release(self);
|
|
} else if constexpr (Slot == 7) {
|
|
return hr_to_u64(heap_get_device(self, reinterpret_cast<const IID*>(a1),
|
|
reinterpret_cast<void**>(a2)));
|
|
} else if constexpr (Slot == 9) {
|
|
return static_cast<std::uint64_t>(heap_get_cpu_handle(self).ptr);
|
|
} else if constexpr (Slot == 10) {
|
|
return static_cast<std::uint64_t>(heap_get_gpu_handle(self).ptr);
|
|
} else {
|
|
note_heap_slot(Slot);
|
|
using ForwardFn = std::uint64_t(WINAPI*)(void*, std::uint64_t, std::uint64_t, std::uint64_t,
|
|
std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t,
|
|
std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t,
|
|
std::uint64_t, std::uint64_t, std::uint64_t);
|
|
auto fn = reinterpret_cast<ForwardFn>(self->real_vtable[Slot]);
|
|
return fn(self->real, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14);
|
|
}
|
|
}
|
|
|
|
template <std::size_t... I>
|
|
void fill_heap_vtable(void** vtable, std::index_sequence<I...>) {
|
|
((vtable[I] = reinterpret_cast<void*>(&heap_slot<static_cast<unsigned>(I)>) ), ...);
|
|
}
|
|
|
|
void** get_heap_vtable() {
|
|
static void** vtable = [] {
|
|
static void* storage[kHeapSlots];
|
|
fill_heap_vtable(storage, std::make_index_sequence<kHeapSlots>{});
|
|
return storage;
|
|
}();
|
|
return vtable;
|
|
}
|
|
|
|
HRESULT create_heap_proxy(ID3D12DescriptorHeap* real, DeviceProxy* device, REFIID riid, void** out) {
|
|
if (!real || !device) return E_POINTER;
|
|
if (!out) return E_POINTER;
|
|
*out = nullptr;
|
|
|
|
HeapProxy* proxy = new (std::nothrow) HeapProxy;
|
|
if (!proxy) return E_OUTOFMEMORY;
|
|
proxy->vtable = get_heap_vtable();
|
|
proxy->real = real;
|
|
proxy->device = device;
|
|
proxy->real_vtable = *reinterpret_cast<void***>(real);
|
|
proxy->refs.store(1, std::memory_order_relaxed);
|
|
proxy_add_ref(device); // the heap keeps its Xbox-ABI device alive for GetDevice
|
|
|
|
const HRESULT hr = heap_query_interface(proxy, &riid, out);
|
|
if (FAILED(hr)) {
|
|
// No reference escaped, and the caller still owns its references to `real` and `device`.
|
|
delete proxy;
|
|
proxy_release(device);
|
|
return hr;
|
|
}
|
|
std::fprintf(stderr, "[rt] d3d12_xs: heap proxy %p wraps PC heap %p (device proxy %p)\n",
|
|
static_cast<void*>(proxy), static_cast<void*>(real), static_cast<void*>(device));
|
|
heap_release(proxy);
|
|
return S_OK;
|
|
}
|
|
|
|
HRESULT proxy_create_descriptor_heap(DeviceProxy* self, const D3D12_DESCRIPTOR_HEAP_DESC* desc, const IID* riid,
|
|
void** out) {
|
|
note_slot(14);
|
|
if (!riid) return E_POINTER;
|
|
if (!out) return E_POINTER;
|
|
|
|
void* created = nullptr;
|
|
HRESULT hr = self->real->CreateDescriptorHeap(desc, __uuidof(ID3D12DescriptorHeap), &created);
|
|
if (FAILED(hr) || !created) return FAILED(hr) ? hr : E_FAIL;
|
|
|
|
ID3D12DescriptorHeap* heap = static_cast<ID3D12DescriptorHeap*>(created);
|
|
hr = create_heap_proxy(heap, self, *riid, out);
|
|
if (FAILED(hr)) heap->Release();
|
|
return hr;
|
|
}
|
|
|
|
// The Xbox slot 62 takes two more words between the description and the IID: (desc, w2, w3, riid, out). Measured
|
|
// on the package's XboxBvhBuilder.dll (its constructor passes w2 = w3 = 0 and IID_ID3D12PipelineState in the fourth
|
|
// argument), as the prior art had recorded. Their meaning is unknown; non-zero values are logged once.
|
|
HRESULT proxy_create_compute_pipeline_state(DeviceProxy* self, const D3D12_COMPUTE_PIPELINE_STATE_DESC* desc,
|
|
std::uint64_t w2, std::uint64_t w3, const IID* riid, void** out) {
|
|
note_slot(62);
|
|
if (w2 || w3) {
|
|
static std::atomic<bool> logged{false};
|
|
if (!logged.exchange(true, std::memory_order_relaxed))
|
|
std::fprintf(stderr, "[rt] d3d12_xs: slot 62 extra words 0x%llx 0x%llx (ignored)\n",
|
|
static_cast<unsigned long long>(w2), static_cast<unsigned long long>(w3));
|
|
}
|
|
if (!riid) return E_POINTER;
|
|
if (!out) return E_POINTER;
|
|
return self->real->CreateComputePipelineState(desc, *riid, out);
|
|
}
|
|
|
|
HRESULT proxy_create_resource(DeviceProxy* self, const D3D12_HEAP_PROPERTIES* heap_properties,
|
|
D3D12_HEAP_FLAGS heap_flags, const D3D12_RESOURCE_DESC* xbox_desc,
|
|
D3D12_RESOURCE_STATES initial_state, const D3D12_CLEAR_VALUE* clear_value,
|
|
const IID* riid, void** out) {
|
|
note_slot(76);
|
|
if (!xbox_desc) return E_POINTER;
|
|
if (!riid) return E_POINTER;
|
|
if (!out) return E_POINTER;
|
|
|
|
// The measured description is 56 bytes plus Xbox extension words; D3D12_RESOURCE_DESC is exactly 56 bytes.
|
|
// The Xbox layout word is 0x101 (tiled) or 0x1 (row-major buffer), and is not a PC layout value. Committed
|
|
// resources take ROW_MAJOR for buffers and UNKNOWN for everything else, so the word is ignored and replaced.
|
|
D3D12_RESOURCE_DESC pc_desc = *xbox_desc;
|
|
const auto xbox_layout = static_cast<std::uint32_t>(pc_desc.Layout);
|
|
pc_desc.Layout = pc_desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER ? D3D12_TEXTURE_LAYOUT_ROW_MAJOR
|
|
: D3D12_TEXTURE_LAYOUT_UNKNOWN;
|
|
|
|
static std::atomic<bool> logged{false};
|
|
if (!logged.exchange(true, std::memory_order_relaxed)) {
|
|
std::fprintf(stderr,
|
|
"[rt] d3d12_xs: slot 76 resource: dimension=%d xbox layout=0x%x -> PC layout=0x%x\n",
|
|
static_cast<int>(pc_desc.Dimension), xbox_layout, static_cast<unsigned>(pc_desc.Layout));
|
|
}
|
|
return self->real->CreateCommittedResource(heap_properties, heap_flags, &pc_desc, initial_state, clear_value,
|
|
*riid, out);
|
|
}
|
|
|
|
template <unsigned Slot>
|
|
std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64_t a2, std::uint64_t a3,
|
|
std::uint64_t a4, std::uint64_t a5, std::uint64_t a6, std::uint64_t a7,
|
|
std::uint64_t a8, std::uint64_t a9, std::uint64_t a10, std::uint64_t a11,
|
|
std::uint64_t a12, std::uint64_t a13, std::uint64_t a14) {
|
|
if constexpr (Slot == 0) {
|
|
return hr_to_u64(proxy_query_interface(self, reinterpret_cast<const IID*>(a1),
|
|
reinterpret_cast<void**>(a2)));
|
|
} else if constexpr (Slot == 1) {
|
|
return proxy_add_ref(self);
|
|
} else if constexpr (Slot == 2) {
|
|
return proxy_release(self);
|
|
} else if constexpr (Slot == 14) {
|
|
return hr_to_u64(proxy_create_descriptor_heap(
|
|
self, reinterpret_cast<const D3D12_DESCRIPTOR_HEAP_DESC*>(a1),
|
|
reinterpret_cast<const IID*>(a2), reinterpret_cast<void**>(a3)));
|
|
} else if constexpr (Slot == 62) {
|
|
return hr_to_u64(proxy_create_compute_pipeline_state(
|
|
self, reinterpret_cast<const D3D12_COMPUTE_PIPELINE_STATE_DESC*>(a1), a2, a3,
|
|
reinterpret_cast<const IID*>(a4), reinterpret_cast<void**>(a5)));
|
|
} else if constexpr (Slot == 76) {
|
|
return hr_to_u64(proxy_create_resource(
|
|
self, reinterpret_cast<const D3D12_HEAP_PROPERTIES*>(a1),
|
|
static_cast<D3D12_HEAP_FLAGS>(static_cast<std::uint32_t>(a2)),
|
|
reinterpret_cast<const D3D12_RESOURCE_DESC*>(a3),
|
|
static_cast<D3D12_RESOURCE_STATES>(static_cast<std::uint32_t>(a4)),
|
|
reinterpret_cast<const D3D12_CLEAR_VALUE*>(a5), reinterpret_cast<const IID*>(a6),
|
|
reinterpret_cast<void**>(a7)));
|
|
} else {
|
|
note_slot(Slot);
|
|
if (Slot >= self->slot_count) {
|
|
bool expected = false;
|
|
if (g_beyond_warned[Slot].compare_exchange_strong(expected, true, std::memory_order_relaxed)) {
|
|
std::fprintf(stderr,
|
|
"[rt] d3d12_xs: device slot %u (%s) is beyond the PC device vtable (%u slots); "
|
|
"returning E_NOTIMPL\n",
|
|
Slot, slot_name(Slot), self->slot_count);
|
|
}
|
|
return hr_to_u64(E_NOTIMPL);
|
|
}
|
|
|
|
// The PC vtable entry, called with the PC device as `this`. Integer/pointer arguments, the hidden
|
|
// struct-return pointer and any stack arguments keep their positions; XMM registers are untouched.
|
|
using ForwardFn = std::uint64_t(WINAPI*)(void*, std::uint64_t, std::uint64_t, std::uint64_t,
|
|
std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t,
|
|
std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t,
|
|
std::uint64_t, std::uint64_t, std::uint64_t);
|
|
auto fn = reinterpret_cast<ForwardFn>(self->real_vtable[Slot]);
|
|
return fn(self->real, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14);
|
|
}
|
|
}
|
|
|
|
template <std::size_t... I>
|
|
void fill_vtable(void** vtable, std::index_sequence<I...>) {
|
|
((vtable[I] = reinterpret_cast<void*>(&proxy_slot<static_cast<unsigned>(I)>) ), ...);
|
|
}
|
|
|
|
void** get_vtable() {
|
|
static void** vtable = [] {
|
|
static void* storage[kMaxSlots];
|
|
fill_vtable(storage, std::make_index_sequence<kMaxSlots>{});
|
|
return storage;
|
|
}();
|
|
return vtable;
|
|
}
|
|
|
|
bool supports(ID3D12Device* real, REFIID iid) {
|
|
void* found = nullptr;
|
|
const HRESULT hr = real->QueryInterface(iid, &found);
|
|
if (FAILED(hr) || !found) return false;
|
|
static_cast<IUnknown*>(found)->Release();
|
|
return true;
|
|
}
|
|
|
|
// Highest device interface the PC runtime exposes; its cumulative slot count (from d3d12.h) bounds forwarding.
|
|
// Windows SDK 10.0.26100 defines ID3D12Device through ID3D12Device14 only; there is no Device15/Device16 header.
|
|
unsigned detect_slot_count(ID3D12Device* real) {
|
|
#ifdef __ID3D12Device14_INTERFACE_DEFINED__
|
|
if (supports(real, __uuidof(ID3D12Device14))) return 83;
|
|
#endif
|
|
#ifdef __ID3D12Device13_INTERFACE_DEFINED__
|
|
if (supports(real, __uuidof(ID3D12Device13))) return 82;
|
|
#endif
|
|
#ifdef __ID3D12Device12_INTERFACE_DEFINED__
|
|
if (supports(real, __uuidof(ID3D12Device12))) return 81;
|
|
#endif
|
|
#ifdef __ID3D12Device11_INTERFACE_DEFINED__
|
|
if (supports(real, __uuidof(ID3D12Device11))) return 80;
|
|
#endif
|
|
#ifdef __ID3D12Device10_INTERFACE_DEFINED__
|
|
if (supports(real, __uuidof(ID3D12Device10))) return 79;
|
|
#endif
|
|
#ifdef __ID3D12Device9_INTERFACE_DEFINED__
|
|
if (supports(real, __uuidof(ID3D12Device9))) return 76;
|
|
#endif
|
|
if (supports(real, __uuidof(ID3D12Device8))) return 73;
|
|
if (supports(real, __uuidof(ID3D12Device7))) return 68;
|
|
if (supports(real, __uuidof(ID3D12Device6))) return 66;
|
|
if (supports(real, __uuidof(ID3D12Device5))) return 65;
|
|
if (supports(real, __uuidof(ID3D12Device4))) return 57;
|
|
if (supports(real, __uuidof(ID3D12Device3))) return 51;
|
|
if (supports(real, __uuidof(ID3D12Device2))) return 48;
|
|
if (supports(real, __uuidof(ID3D12Device1))) return 47;
|
|
return 44;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
HRESULT create_device_proxy(ID3D12Device* real, REFIID riid, void** out) {
|
|
if (!real) return E_POINTER;
|
|
if (!out) return E_POINTER;
|
|
*out = nullptr;
|
|
|
|
DeviceProxy* proxy = new (std::nothrow) DeviceProxy;
|
|
if (!proxy) return E_OUTOFMEMORY;
|
|
proxy->vtable = get_vtable();
|
|
proxy->real = real;
|
|
proxy->real_vtable = *reinterpret_cast<void***>(real);
|
|
proxy->slot_count = detect_slot_count(real);
|
|
proxy->refs.store(1, std::memory_order_relaxed);
|
|
|
|
const unsigned slot_count = proxy->slot_count;
|
|
HRESULT hr = proxy_query_interface(proxy, &riid, out);
|
|
if (FAILED(hr)) {
|
|
// No reference escaped, and the caller still owns its reference to `real`.
|
|
delete proxy;
|
|
return hr;
|
|
}
|
|
std::fprintf(stderr, "[rt] d3d12_xs: device proxy %p wraps PC device %p (%u PC vtable slots)\n",
|
|
static_cast<void*>(proxy), static_cast<void*>(real), slot_count);
|
|
proxy_release(proxy); // drop the creation reference; `out` holds the caller's reference
|
|
return S_OK;
|
|
}
|
|
|
|
bool is_device_proxy(const void* device) {
|
|
return device && *reinterpret_cast<void* const*>(device) == reinterpret_cast<void*>(get_vtable());
|
|
}
|
|
|
|
bool is_heap_proxy(const void* heap) {
|
|
return heap && *reinterpret_cast<void* const*>(heap) == reinterpret_cast<void*>(get_heap_vtable());
|
|
}
|
|
|
|
ID3D12DescriptorHeap* heap_proxy_real_heap(const void* heap) {
|
|
if (!is_heap_proxy(heap)) return nullptr;
|
|
return static_cast<const HeapProxy*>(heap)->real;
|
|
}
|
|
|
|
unsigned device_proxy_slot_count(const void* device) {
|
|
if (!is_device_proxy(device)) return 0;
|
|
return static_cast<const DeviceProxy*>(device)->slot_count;
|
|
}
|
|
|
|
ID3D12Device* device_proxy_real_device(const void* device) {
|
|
if (!is_device_proxy(device)) return nullptr;
|
|
return static_cast<const DeviceProxy*>(device)->real;
|
|
}
|
|
|
|
bool device_slot_overridden(unsigned slot) {
|
|
return slot <= 2 || slot == 14 || slot == 62 || slot == 76;
|
|
}
|
|
|
|
bool heap_slot_overridden(unsigned slot) {
|
|
return slot <= 2 || slot == 7 || slot == 9 || slot == 10;
|
|
}
|
|
|
|
std::uint64_t device_slot_hits(unsigned slot) {
|
|
return slot < kMaxSlots ? g_hits[slot].load(std::memory_order_relaxed) : 0;
|
|
}
|
|
|
|
bool device_slot_first_call_seen(unsigned slot) {
|
|
return slot < kMaxSlots && g_first[slot].load(std::memory_order_relaxed);
|
|
}
|
|
|
|
unsigned long device_proxy_ref_count(const void* device) {
|
|
if (!is_device_proxy(device)) return 0;
|
|
return static_cast<const DeviceProxy*>(device)->refs.load(std::memory_order_relaxed);
|
|
}
|
|
|
|
std::uint64_t heap_slot_hits(unsigned slot) {
|
|
return slot < kHeapSlots ? g_heap_hits[slot].load(std::memory_order_relaxed) : 0;
|
|
}
|
|
|
|
bool heap_slot_first_call_seen(unsigned slot) {
|
|
return slot < kHeapSlots && g_heap_first[slot].load(std::memory_order_relaxed);
|
|
}
|
|
|
|
void reset_device_proxy_stats() {
|
|
for (unsigned slot = 0; slot < kMaxSlots; ++slot) {
|
|
g_hits[slot].store(0, std::memory_order_relaxed);
|
|
g_first[slot].store(false, std::memory_order_relaxed);
|
|
g_beyond_warned[slot].store(false, std::memory_order_relaxed);
|
|
}
|
|
for (unsigned slot = 0; slot < kHeapSlots; ++slot) {
|
|
g_heap_hits[slot].store(0, std::memory_order_relaxed);
|
|
g_heap_first[slot].store(false, std::memory_order_relaxed);
|
|
}
|
|
}
|
|
|
|
} // namespace rt::d3d12_xs
|