Skip to content
Closed
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
9 changes: 9 additions & 0 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ enough for the bundled middleware's `d3d12_xs` calls, not for the game. The laye
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
`OMSetFrontAndBackStencilRef` (public SDK).
- **Derived pipeline states (device slot 55).** `CreateDerivedPipelineState(pBasePSO, n, pRecords, riid, out)`
(`runtime/shims/d3d12_xs_pso_stream.cpp`, `n` = 1..4 records of `0x14c` bytes: a u32 tag and payload at `+4`) patches a
deep copy of the base PSO's `D3D12_PIPELINE_STATE_STREAM_DESC` (stored as `ID3D12Object` private data and freed
with the PSO) and creates the derived PSO through PC slot 47 (`ID3D12Device2::CreatePipelineState`), keeping the
patched stream on the new PSO too (ready for console slot 86 via `#727` once merged). Tags 0 (`D3D12_DEPTH_STENCIL_DESC`
on `DEPTH_STENCIL` or `DEPTH_STENCIL1`), 1 (`D3D12_RASTERIZER_DESC`, `0x2c` bytes), 2 (`D3D12_BLEND_DESC`, `0x148`
bytes), 3 (`SAMPLE_MASK`) and 5 (`0x16` bytes: `StencilReadMask` and `StencilWriteMask` overlaid at payload `+0x10`/`+0x11`
of `DEPTH_STENCIL`/`DEPTH_STENCIL1`, applied in record order) are supported; any other tag, unmodelled stream
subobjects (such as `VIEW_INSTANCING`), or a base without a kept stream is refused (`E_INVALIDARG`/`E_FAIL`, null output).
- **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.
Do not invent a vtable for an unknown IID: a wrong slot is worse than a clean failure.
Expand Down
61 changes: 41 additions & 20 deletions runtime/CMakeLists.txt

Large diffs are not rendered by default.

67 changes: 66 additions & 1 deletion runtime/shims/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
// behind the device before a presenter exists, so WaitFrameEventX returns at once with a fresh non-zero token.
#include "d3d12_xs_device.h"
#include "d3d12_trace.h"
#include "d3d12_xs_pso_stream.h"
#include "../src/shader_library.h" // leonida::fspx_payload_from_parts for slot 67

#include <windows.h>
Expand All @@ -41,6 +42,7 @@
#include <new>
#include <memory>
#include <utility>
#include <vector>

namespace rt::d3d12_xs {
namespace {
Expand Down Expand Up @@ -164,6 +166,7 @@ const char* slot_name(unsigned slot) {
case 46: return "SetResidencyPriority (proxies unwrapped)";
case 48: return "Xbox CreateCommandQueueX -> queue proxy";
case 50: return "EnqueueMakeResident (proxies unwrapped)";
case 55: return "Xbox CreateDerivedPipelineState -> PC CreatePipelineState of the patched stream (slot 47)";
case 62: return "Xbox CreateComputePipelineState";
case 67: return "Xbox DeserializeComputePipelineStateX";
case 70: return "Xbox shader-resource view from raw words";
Expand Down Expand Up @@ -2048,6 +2051,63 @@ HRESULT proxy_create_committed_resource3_x(DeviceProxy* self, std::uint64_t heap
riid, out);
}

// Keeps a copy of the stream a PSO was created from, on the PSO, so slot 55 can derive from it. A stream the module
// does not model is logged once: the PSO is still returned, and slot 55 refuses it as a base.
void keep_pso_stream(IUnknown* pso, const D3D12_PIPELINE_STATE_STREAM_DESC& desc) {
ID3D12Object* object = nullptr;
if (FAILED(pso->QueryInterface(__uuidof(ID3D12Object), reinterpret_cast<void**>(&object))) || !object) return;
auto record = PsoStreamRecord::capture(desc);
static std::atomic<bool> logged{false};
if (!record || !attach_pso_record(object, std::move(record))) {
if (!logged.exchange(true, std::memory_order_relaxed))
std::fprintf(stderr, "[rt] d3d12_xs: a pipeline state stream is not kept for derivation (unmodelled subobject or allocation failure); slot 55 refuses that PSO as a base\n");
}
object->Release();
}

// The PC CreatePipelineState (PC slot 47) used by slot 55 (and shared with console slot 86): on success the stream is
// kept on the returned PSO so derived PSOs can themselves act as a base.
HRESULT create_pso_from_stream(DeviceProxy* self, const D3D12_PIPELINE_STATE_STREAM_DESC* desc, const IID* riid,
void** out) {
if (!riid) return E_POINTER;
using Fn = HRESULT(WINAPI*)(ID3D12Device*, const D3D12_PIPELINE_STATE_STREAM_DESC*, const IID*, void**);
const HRESULT hr = reinterpret_cast<Fn>(self->real_vtable[47])(self->real, desc, riid, out);
if (SUCCEEDED(hr) && out && *out && desc) keep_pso_stream(static_cast<IUnknown*>(*out), *desc);
return hr;
}

// Logs a slot 55 refusal once per reason, and returns the HRESULT.
HRESULT refuse_derived(unsigned reason, const char* why, HRESULT hr) {
static std::atomic<bool> logged[4]{};
if (reason < 4 && !logged[reason].exchange(true, std::memory_order_relaxed))
std::fprintf(stderr, "[rt] d3d12_xs: derived pipeline state (device slot 55) refused: %s\n", why);
return hr;
}

// Xbox slot 55: CreateDerivedPipelineState(pBasePSO, n, pRecords, riid, ppv). The base must be a PSO with a kept
// stream; n is 1..4 records of 0x14c bytes (d3d12_xs_pso_stream.h). The patched stream goes through PC slot 47 and
// keeps its stream on the derived PSO. Every refusal leaves *ppv null and returns a failure, never a fake success.
HRESULT proxy_create_derived_pso(DeviceProxy* self, ID3D12PipelineState* base, std::uint32_t count,
const unsigned char* records, const IID* riid, void** out) {
note_slot(55);
if (out) *out = nullptr;
if (self->slot_count <= 47) return refuse_beyond_device(55, self->slot_count); // predates ID3D12Device2
if (!out || !riid || !records) return E_POINTER;
const PsoStreamRecord* base_record = base ? find_pso_record(base) : nullptr;
if (!base_record) return refuse_derived(0, "base PSO has no kept stream", E_FAIL);
std::vector<unsigned char> patched;
switch (derive_stream(*base_record, records, count, patched)) {
case DeriveStatus::ok: break;
case DeriveStatus::unknown_tag: return refuse_derived(1, "record tag not known", E_INVALIDARG);
case DeriveStatus::missing_state: return refuse_derived(2, "base has no subobject for a record", E_INVALIDARG);
case DeriveStatus::malformed: return refuse_derived(3, "record count or base stream malformed", E_INVALIDARG);
}
D3D12_PIPELINE_STATE_STREAM_DESC desc{};
desc.SizeInBytes = patched.size();
desc.pPipelineStateSubobjectStream = patched.data();
return create_pso_from_stream(self, &desc, 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,
Expand Down Expand Up @@ -2093,6 +2153,11 @@ std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64
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 == 55) {
return hr_to_u64(proxy_create_derived_pso(
self, reinterpret_cast<ID3D12PipelineState*>(a1), static_cast<std::uint32_t>(a2),
reinterpret_cast<const unsigned char*>(a3), reinterpret_cast<const IID*>(a4),
reinterpret_cast<void**>(a5)));
} 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,
Expand Down Expand Up @@ -2385,7 +2450,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 == 25 || slot == 26 || slot == 31 || slot == 32 || slot == 34 || slot == 35 ||
slot == 36 || slot == 45 || slot == 46 || slot == 50 || slot == 62 || slot == 67 ||
slot == 36 || slot == 45 || slot == 46 || slot == 50 || slot == 55 || slot == 62 || slot == 67 ||
slot == 70 || slot == 75 || slot == 76 || slot == 133 || slot == kCreateCommittedResource3X)
return true;
const XboxDeviceSlotRoutes& routes = xbox_device_slot_routes();
Expand Down
2 changes: 1 addition & 1 deletion runtime/shims/d3d12_xs_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ bool set_presentation_handler(void* device, const PresentationHandler* handler);
// True for slots the proxy handles itself rather than forwarding verbatim: IUnknown (0-2), 8 and 75 (queue
// proxies), 9 (allocator proxies), 14, 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, 76 and 127. Slot 10 is intentionally false: the
// 78/79/80 (the Xbox slots above, at their configured indices), 55, 62, 67, 70, 76 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