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 @@ -2122,7 +2122,7 @@ The trace will enumerate them.

| D3D12 / Xbox object | Vulkan / layer object | Notes |
|---|---|---|
| `ID3D12Device` … `ID3D12Device11` | one `Device` object with the public chain plus measured Xbox overrides | The shipped BVH builder names Device11; slots 1/15/18/19/27/38 matched desktop in prior art, slot 62 is called as `CreateComputePipelineState`, slot 76 is Xbox texture creation, and heap slots 9/10 return in RAX. Unknown Xbox IIDs/slots are logged and refused. |
| `ID3D12Device` … `ID3D12Device11` | one `Device` object with the public chain plus measured Xbox overrides | The shipped BVH builder names Device11; slots 1/15/18/19/27/38 matched desktop in prior art, slot 48 is called as `(desc, riid, out)` with `IID_ID3D12CommandQueue` (PC slot 48 is `OpenExistingHeapFromAddress`; the 32-byte desc is a PC `D3D12_COMMAND_QUEUE_DESC` plus four Xbox words, logged once, and the proxy takes the slot 8 path with the PC prefix and only `DISABLE_GPU_TIMEOUT` kept), slot 62 is called as `CreateComputePipelineState`, slot 76 is Xbox texture creation, and heap slots 9/10 return in RAX. Unknown Xbox IIDs/slots are logged and refused. |
| `ID3D12CommandQueue` | one leased `VkQueue` per live queue: direct queues on the graphics family, compute queues on an async-compute family and copy queues on a transfer-only (DMA) family when the GPU has them (`src/queue_families.h`), else graphics-family leases | Distinct Vulkan queues allow cross-queue waits and signals to make progress. With more than one family in use, buffers, images, the arena and staging are created `VK_SHARING_MODE_CONCURRENT` over those families, so no ownership transfers are needed. Barriers on copy and compute queues are narrowed to the stages their family has; render-target and depth clears are refused there (as in D3D12), and the D24/RGB96 copy conversions and bindless flushes stay off a transfer-only queue. Tested by `vk_queue_test`. `PresentX`, `SuspendX`, `ResumeX` are Xbox queue methods, not in the public vtable; public GDKX samples present only through them (§4.5). Call shapes known, slots **Open**. |
| `ID3D12CommandAllocator` | IR arena + `VkCommandPool` | Reset only when the previous submission's fence is complete. |
| `ID3D12GraphicsCommandList` … `8` | `CommandList` with IR | Replay at `ExecuteCommandLists`; bundles inlined. Supports render passes, mesh dispatch, enhanced barriers; records the GCL8 front/back stencil references (applied only once a pipeline path consumes them). `Draw` views are inlined and moved into the command stream (D3D-289), eliminating per-draw heap allocations. |
Expand Down Expand Up @@ -2259,6 +2259,12 @@ The trace will enumerate them.
unknown types and node masks above 1 get an all-zero answer, logged. The test compares `DEFAULT`, `UPLOAD` and
`READBACK` byte for byte with the host runtime's answer when the host adapter has the layer's architecture (RTX
4090: identical); `d3d12_heap_properties` compares the `L1` heaps with the layer's own adapter.
The retail executable calls it (device slot 26) with the buffer for the returned struct in RCX and the device in
RDX, node mask in R8 and heap type in R9: three calls right after device creation, node mask 1 and types 1, 2
and 3 (measured under `gta6rt --run` with a register probe in the slot's thunk). A PC C++ method has the device
in RCX and the buffer in RDX, so the Xbox-ABI proxy takes the console form when RDX holds the proxy
(`d3d12_xs_device.cpp`; `xs_proxy_mock_test`, `device_proxy_test`). Slots 25 and 43 return structs too; the
executable has not been seen calling them yet, so the proxy still forwards them in the PC form.
- Residency and power (D3D-33) are no-ops that still check their arguments (`d3d12_vulkan_device.cpp`, tested in
`d3d12_vulkan_device_test`). Vulkan has no residency API: memory from `vkAllocateMemory` stays usable until
`vkFreeMemory`, and paging under pressure is the kernel driver's job, so every pageable object is always resident.
Expand Down
4 changes: 3 additions & 1 deletion docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ Full-scan indexes are also saved to `%LOCALAPPDATA%\RpfPreview\Indexes`, so the
`CreateCommittedResource` with the Xbox `0x101`/`0x1` layout word replaced by the PC layout. Slots 9/10 are
`ID3D12DescriptorHeap` methods, not device methods. The PC heap getters take a hidden return pointer (RDX),
while Xbox expects the 8-byte handle in RAX, so slot 14 wraps the heap and slots 9/10 convert it. The heap holds
a reference to the device proxy and its `GetDevice` returns that same proxy. Queues, fences, command lists and
a reference to the device proxy and its `GetDevice` returns that same proxy. The retail executable calls the
struct-returning device methods with the C ABI, return buffer first: `GetCustomHeapProperties` (slot 26) is
measured as `rcx` = buffer, `rdx` = device. Slots 25 and 26 take that form, and still take the PC one. Queues, fences, command lists and
`PresentX` extensions remain unimplemented.
- PSO metadata is a new variant (`PSIN`, `LSCH` schema, `LENM`, `LSNS`, `LGID`, `LRSC`, `CHKS`, little-endian).
CodeWalker's parser reads the container but not the new schema; dump it once the exe gives the field names.
Expand Down
129 changes: 121 additions & 8 deletions runtime/shims/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
// 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. Slots 8/75 (command queues), 36 (fences) and 32 (a shared fence) wrap
// are IUnknown (0-2), slot 14 (which wraps the heap), 25 and 26 (the title passes the return buffer in RCX and the
// device in RDX: see "Struct-returning device methods" below), 48 (a queue creation, where the PC has OpenExistingHeapFromAddress), 62 and 76, plus the heap proxy's 9/10
// conversion from the PC hidden-pointer form to the Xbox RAX form. Slots 8/75 (command queues), 36 (fences) and 32 (a shared fence) wrap
// what they create in queue and fence proxies built the same way, and slots 31/34/35/45/46/50 hand the real objects
// behind heap, queue and fence proxies to the real device (see the command queue and fence section).
//
Expand Down Expand Up @@ -133,7 +134,8 @@ const char* slot_name(unsigned slot) {
case 11: return "CreateComputePipelineState (PC slot 11)";
case 14: return "CreateDescriptorHeap";
case 16: return "CreateRootSignature";
case 25: return "GetResourceAllocationInfo";
case 25: return "GetResourceAllocationInfo (C or PC form)";
case 26: return "GetCustomHeapProperties";
case 27: return "CreateCommittedResource (PC slot 27)";
case 31: return "CreateSharedHandle (proxies unwrapped)";
case 32: return "OpenSharedHandle (a fence comes back wrapped)";
Expand All @@ -147,6 +149,7 @@ const char* slot_name(unsigned slot) {
case 50: return "EnqueueMakeResident (proxies unwrapped)";
case 62: return "Xbox CreateComputePipelineState";
case 67: return "Xbox DeserializeComputePipelineStateX";
case 70: return "Xbox shader-resource view from raw words";
case 73: return "Xbox CreateCommandListX -> PC CreateCommandList";
case 75: return "CreateCommandQueue1 -> queue proxy";
case 76: return "Xbox resource creation";
Expand Down Expand Up @@ -1444,6 +1447,45 @@ HRESULT proxy_create_command_list_x(DeviceProxy* self, std::uint64_t desc_word,
return wrap_list(static_cast<IUnknown*>(created), self, riid, out);
}

// Device slot 48, measured on the retail title: (desc, riid, out) with riid = IID_ID3D12CommandQueue. On the PC the
// slot is OpenExistingHeapFromAddress, so forwarding it by number fails. desc is a PC D3D12_COMMAND_QUEUE_DESC
// (16 bytes) followed by four more 32-bit Xbox words of unknown meaning, logged once. Only the PC prefix is used and
// only the flag the PC knows is kept; the rest is the slot 8 path.
HRESULT proxy_create_command_queue_xbox(DeviceProxy* self, const std::uint32_t* desc, const IID* riid, void** out) {
if (out) *out = nullptr;
if (!desc) return E_INVALIDARG;
static_assert(sizeof(D3D12_COMMAND_QUEUE_DESC) == 16, "the PC prefix of the Xbox description is 16 bytes");
D3D12_COMMAND_QUEUE_DESC pc{};
std::memcpy(&pc, desc, sizeof(pc));
pc.Flags &= D3D12_COMMAND_QUEUE_FLAG_DISABLE_GPU_TIMEOUT;
static std::atomic<bool> logged{false};
if (!logged.exchange(true, std::memory_order_relaxed))
std::fprintf(stderr,
"[rt] d3d12_xs: slot 48 queue: flags=0x%x extra words=0x%x 0x%x 0x%x 0x%x\n",
static_cast<unsigned>(desc[2]), desc[4], desc[5], desc[6], desc[7]);
return proxy_create_command_queue(self, &pc, riid, out);
}

// Xbox device slot 70: (texture_words, view_desc, destination), no return value. The retail executable's SRV writer
// makes a view from raw descriptions and a GPU address with no resource object (docs/retail_boot_chain.md, stop 14,
// measured by call shape only). The PC slot 70 is ID3D12Device8::CreatePlacedResource1, which would take the
// destination handle for a resource description and write through it. The layout of the raw words is not measured,
// so nothing reads them: the destination gets the PC's null shader-resource view, which a shader reads as zeros, and
// the call returns. A view that reaches the GPU address needs that layout.
void proxy_create_srv_from_raw_x(DeviceProxy* self, std::uint64_t texture_words, std::uint64_t view_desc,
std::uint64_t destination) {
note_slot(70);
log_xbox_route_once(70, "CreateShaderResourceView from raw words (null view)", texture_words, view_desc,
destination, 0, 0);
if (!destination) return;
D3D12_SHADER_RESOURCE_VIEW_DESC null_view{};
null_view.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
null_view.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
null_view.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
null_view.Texture2D.MipLevels = 1;
self->real->CreateShaderResourceView(nullptr, &null_view, D3D12_CPU_DESCRIPTOR_HANDLE{destination});
}

// Device slot 75: ID3D12Device9::CreateCommandQueue1(desc, creator_id, riid, out), called by slot index.
HRESULT proxy_create_command_queue1(DeviceProxy* self, std::uint64_t desc, std::uint64_t creator_id,
const IID* riid, void** out) {
Expand Down Expand Up @@ -1817,6 +1859,21 @@ HRESULT proxy_create_placed_resource_x(DeviceProxy* self, void* va, const D3D12_
return handler(self->real, va, pc_desc, state, *riid, out);
}

// Device slot 26, GetCustomHeapProperties, as the retail executable calls it. Measured on 1.0.2.1 under
// gta6rt --run with a register probe in this slot's thunk: three calls right after device creation, each with the
// device proxy in RDX and not in RCX, R8 = 1 and R9 = 1, 2, 3 (node mask 1; DEFAULT, UPLOAD, READBACK). So the
// buffer for the returned D3D12_HEAP_PROPERTIES is in RCX and the device in RDX. That is how x64 passes them to a
// plain function that takes the device as its first argument and returns a struct; a PC C++ method has the device
// in RCX and the buffer in RDX. The forwarder took RCX for the proxy, read the PC vtable out of the title's stack
// buffer and faulted. The buffer's address goes back in RAX, as that convention returns it; what the title does with
// RAX is not measured.
D3D12_HEAP_PROPERTIES* proxy_get_custom_heap_properties(D3D12_HEAP_PROPERTIES* out, DeviceProxy* self,
UINT node_mask, D3D12_HEAP_TYPE heap_type) {
note_slot(26);
*out = self->real->GetCustomHeapProperties(node_mask, heap_type);
return 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,
Expand Down Expand Up @@ -1875,6 +1932,9 @@ std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64
reinterpret_cast<const D3D12_RESOURCE_DESC*>(a2),
static_cast<std::uint32_t>(a3), reinterpret_cast<const IID*>(a5),
reinterpret_cast<void**>(a6)));
} else if constexpr (Slot == 70) {
proxy_create_srv_from_raw_x(self, a1, a2, a3);
return 0;
} else if constexpr (Slot == 76) {
return hr_to_u64(proxy_create_resource(
self, reinterpret_cast<const D3D12_HEAP_PROPERTIES*>(a1),
Expand All @@ -1884,6 +1944,14 @@ std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64
reinterpret_cast<const D3D12_CLEAR_VALUE*>(a5), reinterpret_cast<const IID*>(a6),
reinterpret_cast<void**>(a7)));
} else {
if constexpr (Slot == 26) {
// The console form has the device in RDX. A PC C++ caller has the proxy in RCX and its return buffer in
// RDX; that call goes to the forwarder below, as before.
if (is_device_proxy(reinterpret_cast<const void*>(a1)))
return reinterpret_cast<std::uint64_t>(proxy_get_custom_heap_properties(
reinterpret_cast<D3D12_HEAP_PROPERTIES*>(self), reinterpret_cast<DeviceProxy*>(a1),
static_cast<UINT>(a2), static_cast<D3D12_HEAP_TYPE>(static_cast<std::uint32_t>(a3))));
}
note_slot(Slot);
// Xbox device slots, before the PC vtable is indexed: the public HLE's 78/79/80 would otherwise reach
// CreateReservedResource2/CreateSampler2 or fall past an 80-slot table, and 48/73 would reach unrelated PC
Expand All @@ -1903,9 +1971,9 @@ std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64
}
if (Slot == routes.queue_x) {
log_xbox_route_once(Slot, "CreateCommandQueueX", a1, a2, a3, a4, a5);
return hr_to_u64(proxy_create_command_queue(self, reinterpret_cast<const D3D12_COMMAND_QUEUE_DESC*>(a1),
reinterpret_cast<const IID*>(a2),
reinterpret_cast<void**>(a3)));
return hr_to_u64(proxy_create_command_queue_xbox(self, reinterpret_cast<const std::uint32_t*>(a1),
reinterpret_cast<const IID*>(a2),
reinterpret_cast<void**>(a3)));
}
if (Slot == routes.list_x) {
log_xbox_route_once(Slot, "CreateCommandListX", a1, a2, a3, a4, a5);
Expand All @@ -1930,10 +1998,55 @@ void fill_vtable(void** vtable, std::index_sequence<I...>) {
((vtable[I] = reinterpret_cast<void*>(&proxy_slot<static_cast<unsigned>(I)>) ), ...);
}

// ---- Struct-returning device methods: the console title calls them with the C ABI ----
//
// The PC vtable (MSVC C++) takes `this` in RCX and, for a struct return, the hidden return pointer in RDX. The console
// title does not. Measured with the retail game_scarlett_master.exe 1.0.2.1 under gta6rt --run: its first
// GetCustomHeapProperties call (device slot 26) is `rcx = return buffer on the caller's stack, rdx = the device,
// r8 = node mask 1, r9 = heap type` (DEFAULT, then UPLOAD), and it reads the result from that buffer. That is the C
// convention, where a struct wider than 8 bytes comes back through a hidden *first* argument (an 8-byte one comes back
// in RAX, which is what the heap proxy's slots 9/10 already measured). The generic forwarder took the return buffer
// for the device and crashed on its vtable.
//
// The runtime's own code and tests still call the proxy through d3d12.h, in the PC form, so these thunks accept both,
// as the heap proxy's 9/10 do: whichever of the first two arguments is the device proxy (its first word is the proxy
// vtable) is `this`, and the other is the return buffer. A return buffer never starts with that vtable pointer: the
// C-form caller hands over uninitialised memory to be written, and the PC-form caller is this runtime. The buffer is
// returned in RAX, as both conventions require.
// slot 25 GetResourceAllocationInfo -> D3D12_RESOURCE_ALLOCATION_INFO (16 bytes): same rule as slot 26 (inferred,
// no call site measured yet)
// slot 26 GetCustomHeapProperties -> D3D12_HEAP_PROPERTIES (20 bytes): measured; it has its own thunk above
// (proxy_get_custom_heap_properties), not this one
// Not changed: slot 43 GetAdapterLuid (an 8-byte LUID; by the same rule the title would expect it in RAX, but no call
// site is measured yet, so it still forwards in the PC form).
void** get_vtable();

struct StructReturnCall {
DeviceProxy* self;
void* ret;
};

StructReturnCall struct_return_call(std::uint64_t a0, std::uint64_t a1) {
void** const vtable = get_vtable();
if (a0 && *reinterpret_cast<void***>(a0) == vtable) // PC form: this, return buffer
return {reinterpret_cast<DeviceProxy*>(a0), reinterpret_cast<void*>(a1)};
return {reinterpret_cast<DeviceProxy*>(a1), reinterpret_cast<void*>(a0)}; // C form: return buffer, this
}

void* WINAPI proxy_get_resource_allocation_info(std::uint64_t a0, std::uint64_t a1, std::uint64_t visible_mask,
std::uint64_t count, const D3D12_RESOURCE_DESC* descs) {
note_slot(25);
const StructReturnCall call = struct_return_call(a0, a1);
*static_cast<D3D12_RESOURCE_ALLOCATION_INFO*>(call.ret) = call.self->real->GetResourceAllocationInfo(
static_cast<UINT>(visible_mask), static_cast<UINT>(count), descs);
return call.ret;
}

void** get_vtable() {
static void** vtable = [] {
static void* storage[kMaxSlots];
fill_vtable(storage, std::make_index_sequence<kMaxSlots>{});
storage[25] = reinterpret_cast<void*>(&proxy_get_resource_allocation_info);
return storage;
}();
return vtable;
Expand Down Expand Up @@ -2070,9 +2183,9 @@ ID3D12Device* device_proxy_real_device(const void* device) {
}

bool device_slot_overridden(unsigned slot) {
if (slot <= 2 || slot == 8 || slot == 9 || slot == 14 || slot == 31 || slot == 32 || slot == 34 || slot == 35 ||
if (slot <= 2 || slot == 8 || slot == 9 || slot == 14 || slot == 25 || slot == 26 || slot == 31 || slot == 32 || slot == 34 || slot == 35 ||
slot == 36 || slot == 45 || slot == 46 || slot == 50 || slot == 62 || slot == 67 ||
slot == 75 || slot == 76 || slot == 133)
slot == 70 || slot == 75 || slot == 76 || slot == 133)
return true;
const XboxDeviceSlotRoutes& routes = xbox_device_slot_routes();
return slot == routes.frame_interval || slot == routes.schedule_event || slot == routes.wait_event ||
Expand Down
Loading
Loading