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
8 changes: 7 additions & 1 deletion docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ enough for the bundled middleware's `d3d12_xs` calls, not for the game. The laye
desktop order, but also an extra texture-creation method at slot 76, a `CreateComputePipelineState` call at
slot 62 where the PC interface it forwarded to presents `CreateStateObject` (different arity), and descriptor-heap
start handles returned in RAX at heap slots 9/10. Implement the public chain plus those measured overrides
explicitly; do not assume every slot. Device 11 adds `CreateSampler2`; command list 8 adds
explicitly; do not assume every slot. On the retail executable's higher device slots (`runtime/shims/d3d12_xs_device.cpp`),
slot 86 routes `CreatePipelineState(desc, riid, out)` to PC slot 47 (`ID3D12Device2::CreatePipelineState`),
slot 101 routes `CreateStateObject(desc, riid, out)` to PC slot 62 (`ID3D12Device5::CreateStateObject`), and
slot 108 routes `GetResourceAllocationInfo2` (accepting the console C-ABI struct return with the return buffer in
`RCX` and the device in `RDX`, expanding 56-byte `D3D12_RESOURCE_DESC` entries to `D3D12_RESOURCE_DESC1` and
normalizing Xbox layout words and flag bits on the copy) to PC
slot 68 (`ID3D12Device8::GetResourceAllocationInfo2`). Device 11 adds `CreateSampler2`; command list 8 adds
`OMSetFrontAndBackStencilRef` (public SDK).
- **Unknown IIDs and slots.** `QueryInterface` must log the IID and return `E_NOINTERFACE` if it is not in the
public chain. If the game then fails, the IID and the call site identify the Xbox-specific interface to reverse.
Expand Down
126 changes: 116 additions & 10 deletions runtime/shims/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ const char* slot_name(unsigned slot) {
case 78: return "Xbox SetFrameIntervalX";
case 79: return "Xbox ScheduleFrameEventX";
case 80: return "Xbox WaitFrameEventX";
case 86: return "Xbox CreatePipelineState -> PC CreatePipelineState (slot 47)";
case 101: return "Xbox CreateStateObject -> PC CreateStateObject (slot 62)";
case 108: return "Xbox GetResourceAllocationInfo2 -> PC GetResourceAllocationInfo2 (slot 68)";
case 133: return "Xbox CreatePlacedResourceX (a buffer on the title's pages)";
default: return "forwarded";
}
Expand Down Expand Up @@ -2548,6 +2551,97 @@ std::uint64_t proxy_slot74(std::uint64_t a1, std::uint64_t a2, std::uint64_t a3)
return hr_to_u64(S_OK);
}

// Xbox slot 86: CreatePipelineState(desc, riid, out), forwarded to PC ID3D12Device2::CreatePipelineState (PC slot 47).
HRESULT proxy_create_pipeline_state(DeviceProxy* self, const D3D12_PIPELINE_STATE_STREAM_DESC* desc, const IID* riid,
void** out) {
if (out) *out = nullptr;
if (self->slot_count <= 47) return refuse_beyond_device(86, self->slot_count); // predates ID3D12Device2
if (!riid) return E_POINTER;
using Fn = HRESULT(WINAPI*)(ID3D12Device*, const D3D12_PIPELINE_STATE_STREAM_DESC*, const IID*, void**);
return reinterpret_cast<Fn>(self->real_vtable[47])(self->real, desc, riid, out);
}

// Xbox slot 101: CreateStateObject(desc, riid, out), forwarded to PC ID3D12Device5::CreateStateObject (PC slot 62,
// displaced on Xbox by compute-PSO slot 62).
HRESULT proxy_create_state_object(DeviceProxy* self, const D3D12_STATE_OBJECT_DESC* desc, const IID* riid, void** out) {
if (out) *out = nullptr;
if (self->slot_count <= 62) return refuse_beyond_device(101, self->slot_count); // predates ID3D12Device5
if (!riid) return E_POINTER;
using Fn = HRESULT(WINAPI*)(ID3D12Device*, const D3D12_STATE_OBJECT_DESC*, const IID*, void**);
return reinterpret_cast<Fn>(self->real_vtable[62])(self->real, desc, riid, out);
}

// Xbox slot 108: GetResourceAllocationInfo2, forwarded to PC ID3D12Device8::GetResourceAllocationInfo2 (PC slot 68).
// The retail executable calls struct-returning device methods with the C ABI (return buffer in RCX, device proxy in
// RDX); a PC C++ caller passes the proxy in RCX and the return buffer in RDX. Both orders are accepted. The title's
// descriptor builder populates 56-byte D3D12_RESOURCE_DESC entries, so the input array is indexed with a 56-byte
// stride (sizeof(D3D12_RESOURCE_DESC)), each entry is expanded into a zeroed D3D12_RESOURCE_DESC1 with
// SamplerFeedbackMipRegion cleared, and Xbox layout words (e.g. 0x101, 0x105) and console flag bits above the PC
// D3D12_RESOURCE_FLAGS are normalized on the copy before the PC call.
std::uint64_t proxy_get_resource_allocation_info2(DeviceProxy* self, std::uint64_t a1, std::uint64_t a2,
std::uint64_t a3, std::uint64_t a4, std::uint64_t a5) {
DeviceProxy* proxy = nullptr;
D3D12_RESOURCE_ALLOCATION_INFO* ret = nullptr;
if (is_device_proxy(self)) {
proxy = self;
ret = reinterpret_cast<D3D12_RESOURCE_ALLOCATION_INFO*>(a1);
} else if (is_device_proxy(reinterpret_cast<const void*>(a1))) {
proxy = reinterpret_cast<DeviceProxy*>(a1);
ret = reinterpret_cast<D3D12_RESOURCE_ALLOCATION_INFO*>(self);
}
if (!ret) return 0;
if (!proxy) {
*ret = {UINT64_MAX, 0};
return reinterpret_cast<std::uint64_t>(ret);
}
if (proxy->slot_count <= 68) {
refuse_beyond_device(108, proxy->slot_count);
*ret = {UINT64_MAX, 0};
return reinterpret_cast<std::uint64_t>(ret);
}
const auto visible_mask = static_cast<UINT>(a2);
const auto count = static_cast<UINT>(a3);
const auto* raw_descs = reinterpret_cast<const D3D12_RESOURCE_DESC*>(a4);
auto* info1 = reinterpret_cast<D3D12_RESOURCE_ALLOCATION_INFO1*>(a5);
if (count > 0 && !raw_descs) { // a count with no descriptor array: refuse like the other bad-input paths
*ret = {UINT64_MAX, 0};
return reinterpret_cast<std::uint64_t>(ret);
}

std::vector<D3D12_RESOURCE_DESC1> cleaned;
const D3D12_RESOURCE_DESC1* descs_to_pass = nullptr;
if (raw_descs && count > 0) {
try {
cleaned.resize(count);
} catch (...) {
*ret = {UINT64_MAX, 0};
return reinterpret_cast<std::uint64_t>(ret);
}
for (UINT i = 0; i < count; ++i) {
D3D12_RESOURCE_DESC1 d{};
std::memcpy(&d, &raw_descs[i], sizeof(D3D12_RESOURCE_DESC));
d.SamplerFeedbackMipRegion = {};
if (static_cast<std::uint32_t>(d.Layout) >
static_cast<std::uint32_t>(D3D12_TEXTURE_LAYOUT_64KB_STANDARD_SWIZZLE)) {
d.Layout = d.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER ? D3D12_TEXTURE_LAYOUT_ROW_MAJOR
: D3D12_TEXTURE_LAYOUT_UNKNOWN;
}
// The same PC flag set as slot 133: up to RAYTRACING_ACCELERATION_STRUCTURE (0x100) and
// USE_TIGHT_ALIGNMENT (0x400), with 0x200 dropped.
constexpr std::uint32_t kPcResourceFlagsMask = 0x5FFu;
d.Flags = static_cast<D3D12_RESOURCE_FLAGS>(static_cast<std::uint32_t>(d.Flags) & kPcResourceFlagsMask);
cleaned[i] = d;
}
descs_to_pass = cleaned.data();
}

using Fn = D3D12_RESOURCE_ALLOCATION_INFO*(WINAPI*)(
ID3D12Device*, D3D12_RESOURCE_ALLOCATION_INFO*, UINT, UINT, const D3D12_RESOURCE_DESC1*,
D3D12_RESOURCE_ALLOCATION_INFO1*);
reinterpret_cast<Fn>(proxy->real_vtable[68])(proxy->real, ret, visible_mask, count, descs_to_pass, info1);
return reinterpret_cast<std::uint64_t>(ret);
}

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,
Expand Down Expand Up @@ -2707,16 +2801,28 @@ std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64
// methods. The check is before the slot-count refusal because slot 80 is outside a PC ID3D12Device11 table.
std::uint64_t routed = 0;
if (route_xbox_device_slot(self, Slot, a1, a2, a3, a4, a5, routed)) return routed;
if (Slot >= self->slot_count) return hr_to_u64(refuse_beyond_device(Slot, self->slot_count));
if constexpr (Slot == 86) {
return hr_to_u64(proxy_create_pipeline_state(
self, reinterpret_cast<const D3D12_PIPELINE_STATE_STREAM_DESC*>(a1),
reinterpret_cast<const IID*>(a2), reinterpret_cast<void**>(a3)));
} else if constexpr (Slot == 101) {
return hr_to_u64(proxy_create_state_object(
self, reinterpret_cast<const D3D12_STATE_OBJECT_DESC*>(a1),
reinterpret_cast<const IID*>(a2), reinterpret_cast<void**>(a3)));
} else if constexpr (Slot == 108) {
return proxy_get_resource_allocation_info2(self, a1, a2, a3, a4, a5);
} else {
if (Slot >= self->slot_count) return hr_to_u64(refuse_beyond_device(Slot, self->slot_count));

// 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);
// 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);
}
}
}

Expand Down Expand Up @@ -3036,7 +3142,7 @@ ID3D12Device* device_proxy_real_device(const void* device) {
bool device_slot_overridden(unsigned slot) {
if (slot <= 2 || slot == 8 || slot == 9 || slot == 14 || (slot >= 17 && slot <= 24) || slot == 25 || slot == 26 || slot == 31 || slot == 32 || slot == 34 || slot == 35 ||
slot == 36 || slot == 41 || slot == 45 || slot == 46 || slot == 50 || slot == 56 || slot == 61 || slot == 74 || slot == 62 || slot == 67 ||
slot == 70 || slot == 75 || slot == 76 || slot == 133 || slot == kCreateCommittedResource3X)
slot == 70 || slot == 75 || slot == 76 || slot == 133 || slot == kCreateCommittedResource3X || slot == 86 || slot == 101 || slot == 108)
return true;
const XboxDeviceSlotRoutes& routes = xbox_device_slot_routes();
return slot == routes.frame_interval || slot == routes.schedule_event || slot == routes.wait_event ||
Expand Down
10 changes: 9 additions & 1 deletion runtime/shims/d3d12_xs_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
// slots 78/79/80 SetFrameIntervalX, ScheduleFrameEventX, WaitFrameEventX. A per-device token state machine answers
// them before the PC vtable is indexed; WaitFrameEventX returns at once with a fresh non-zero token.
// Slot 80 is outside a PC ID3D12Device11 table, so it is checked before the beyond-table refusal.
// slot 86 CreatePipelineState(desc, riid, out) -> PC ID3D12Device2::CreatePipelineState (PC slot 47).
// slot 101 CreateStateObject(desc, riid, out) -> PC ID3D12Device5::CreateStateObject (PC slot 62, displaced on
// Xbox by compute-PSO slot 62).
// slot 108 GetResourceAllocationInfo2(ret, this, visible_mask, count, descs, info1) -> PC
// ID3D12Device8::GetResourceAllocationInfo2 (PC slot 68), accepting the console C-ABI struct return
// (return buffer in RCX, device in RDX) as well as the PC C++ order, with each 56-byte
// D3D12_RESOURCE_DESC expanded to a zero-initialized D3D12_RESOURCE_DESC1 and Xbox layout words and
// flag bits normalized on the copy.
// The slots 48/73/78/79/80 are the public HLE Freminet/ScarlettX's observation, not Microsoft documentation, and can
// be moved with LEONIDA_XS_QUEUE_X_SLOT, LEONIDA_XS_LIST_X_SLOT, LEONIDA_XS_FRAME_INTERVAL_SLOT,
// LEONIDA_XS_SCHEDULE_EVENT_SLOT and LEONIDA_XS_WAIT_EVENT_SLOT (3..127; anything else is logged once and ignored).
Expand Down Expand Up @@ -186,7 +194,7 @@ bool set_presentation_handler(void* device, const PresentationHandler* handler);
// proxies), 9 (allocator proxies), 14 (descriptor heap proxy), 17-24 (CPU descriptor handle translation), 26 (the
// console's struct-return form),
// 31/34/35/45/46/50 (proxies unwrapped), 32 and 36 (fence proxies), 48/73 and
// 78/79/80 (the Xbox slots above, at their configured indices), 62, 67, 70, 74, 76 and 127. Slot 10 is intentionally false: the
// 78/79/80 (the Xbox slots above, at their configured indices), 62, 67, 70, 74, 76, 86, 101, 108 and 127. Slot 10 is intentionally false: the
// measured 9/10 handle conversion is on the heap proxy, and device slot 9 is CreateCommandAllocator, which the proxy
// handles for the allocator proxy.
bool device_slot_overridden(unsigned slot);
Expand Down
Loading
Loading