mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 22:28:38 +00:00
Slot 127 is past the PC device vtable and was refused with E_NOTIMPL. It has the argument list of ID3D12Device10::CreateCommittedResource3; the proxy now calls the PC device's with the Xbox layout word and flag bits cleaned as slot 76 does. Measured in #6982.
(cherry picked from commit 8d622049ae)
2425 lines
126 KiB
C++
2425 lines
126 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), 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).
|
|
//
|
|
// A thunk declares 14 argument words, but the title passes only as many as the method takes, so the caller's frame
|
|
// may end right after the four register homes. The compiler owns a function's incoming stack-argument slots and may
|
|
// write them, a tail call included. Every function a thunk hands its words to therefore takes them in the same
|
|
// positions (slot 8: desc, riid, out as a1-a3; slot 50: a1-a5; and so on). A tail call then writes back only the
|
|
// values it read from those slots. A helper that moved a word to another stack position would overwrite the title's
|
|
// frame. xs_proxy_mock_test's mock hit this in an optimized build, and its thunks now pass their words by pointer.
|
|
//
|
|
// The public HLE Freminet/ScarlettX reports five more Xbox device slots (a third-party observation, not a Microsoft
|
|
// ABI); the generic thunk routes them before it forwards anything: 48 CreateCommandQueueX, 73 CreateCommandListX,
|
|
// 78 SetFrameIntervalX, 79 ScheduleFrameEventX and 80 WaitFrameEventX. The slots can be moved with LEONIDA_XS_*_SLOT
|
|
// for a trace that disagrees. The frame-event calls answer from a per-device token counter: there is no vblank source
|
|
// 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 "../src/shader_library.h" // leonida::fspx_payload_from_parts for slot 67
|
|
|
|
#include <windows.h>
|
|
#include <dxgi1_4.h>
|
|
|
|
#include <atomic>
|
|
#include <cerrno>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <iterator>
|
|
#include <new>
|
|
#include <memory>
|
|
#include <utility>
|
|
|
|
namespace rt::d3d12_xs {
|
|
namespace {
|
|
|
|
// The retail title calls device slots well past ID3D12Device14 (slot 133 at least); every slot past the PC device's
|
|
// vtable goes through the generic thunk and is refused with E_NOTIMPL, so the table must outlast the highest one.
|
|
constexpr unsigned kMaxSlots = 192;
|
|
static_assert(kMaxSlots > 133, "the proxy table must cover the slots the retail title calls (up to 133)");
|
|
|
|
// Set by the Vulkan device behind the proxy; slot 14 uses it for a console-extended CreateDescriptorHeap.
|
|
std::atomic<ConsoleDescriptorHeapHandler> g_console_descriptor_heap{nullptr};
|
|
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;
|
|
// The device's IDXGIDevice1 (see "IDXGIDevice1 on the device" below): a title's IDXGIDevice1* points here.
|
|
void** dxgi_vtable;
|
|
DxgiAdapterProvider adapter_provider;
|
|
std::atomic<int> gpu_thread_priority;
|
|
std::atomic<unsigned> max_frame_latency;
|
|
// The first-frame subset of SetFrameIntervalX/ScheduleFrameEventX/WaitFrameEventX. Tokens were never measured;
|
|
// they start at 1 and only have to be fresh and non-zero for the boot sequence. Waiting returns at once: there is
|
|
// no vblank source until a presenter exists, and a bounded zero wait cannot stall the first frame.
|
|
std::atomic<std::uint32_t> frame_interval_us{0};
|
|
std::atomic<std::uint32_t> frame_latency{1};
|
|
std::atomic<bool> frame_event_scheduled{false};
|
|
std::atomic<std::uint64_t> frame_next_token{1};
|
|
};
|
|
|
|
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];
|
|
// The hit counters are for tests. They count from the first call of a *_slot_hits reader or of
|
|
// reset_device_proxy_stats, so the calls of a title, which reads no counter, write no shared counter.
|
|
std::atomic<bool> g_count_hits{false};
|
|
|
|
// For the readers and the reset. The flag is written one time only.
|
|
void start_hit_counts() {
|
|
if (!g_count_hits.load(std::memory_order_relaxed)) g_count_hits.store(true, std::memory_order_relaxed);
|
|
}
|
|
|
|
void count_hit(std::atomic<std::uint64_t>& hits) {
|
|
if (g_count_hits.load(std::memory_order_relaxed)) hits.fetch_add(1, std::memory_order_relaxed);
|
|
}
|
|
|
|
// True for the first call of a slot only. The flag is read first: a compare-exchange takes the cache line for
|
|
// writing also when it fails, and the flags of all slots share one or two lines.
|
|
bool first_call(std::atomic<bool>& seen) {
|
|
if (seen.load(std::memory_order_relaxed)) return false;
|
|
bool expected = false;
|
|
return seen.compare_exchange_strong(expected, true, std::memory_order_relaxed);
|
|
}
|
|
|
|
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 8: return "CreateCommandQueue -> queue proxy";
|
|
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 (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)";
|
|
case 34: return "MakeResident (proxies unwrapped)";
|
|
case 35: return "Evict (proxies unwrapped)";
|
|
case 36: return "CreateFence -> fence proxy";
|
|
case 43: return "GetAdapterLuid";
|
|
case 45: return "SetEventOnMultipleFenceCompletion (fence proxies unwrapped)";
|
|
case 46: return "SetResidencyPriority (proxies unwrapped)";
|
|
case 48: return "Xbox CreateCommandQueueX -> queue proxy";
|
|
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";
|
|
case 127: return "Xbox CreateCommittedResource3";
|
|
case 78: return "Xbox SetFrameIntervalX";
|
|
case 79: return "Xbox ScheduleFrameEventX";
|
|
case 80: return "Xbox WaitFrameEventX";
|
|
case 133: return "Xbox CreatePlacedResourceX (a buffer on the title's pages)";
|
|
default: return "forwarded";
|
|
}
|
|
}
|
|
|
|
void note_slot(unsigned slot) {
|
|
if (slot >= kMaxSlots) return;
|
|
count_hit(g_hits[slot]);
|
|
const bool first = first_call(g_first[slot]);
|
|
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));
|
|
}
|
|
}
|
|
|
|
// Xbox device slots (public HLE Freminet/ScarlettX, an observation, not Microsoft documentation). The defaults are
|
|
// the HLE's; an environment variable that names another slot in 3..127 replaces one (a whole-table move for a trace
|
|
// that disagrees). A value outside that range is logged once and ignored. The table is read once, on the first
|
|
// generic slot call, so a test that wants other slots sets the variables before it creates a device.
|
|
struct XboxDeviceSlotRoutes {
|
|
unsigned frame_interval = 78; // SetFrameIntervalX(output, interval_us, latency, flags)
|
|
unsigned schedule_event = 79; // ScheduleFrameEventX(event, offset_us, reserved, flags)
|
|
unsigned wait_event = 80; // WaitFrameEventX(event, timeout_ms, reserved, flags, &token)
|
|
unsigned queue_x = 48; // CreateCommandQueueX(desc, riid, out)
|
|
unsigned list_x = 73; // CreateCommandListX(desc, allocator, initial_state, riid, out)
|
|
};
|
|
|
|
unsigned env_device_slot(const char* name, unsigned fallback) {
|
|
char value[32] = {};
|
|
const DWORD length = GetEnvironmentVariableA(name, value, sizeof(value));
|
|
if (length == 0) return fallback;
|
|
if (length >= sizeof(value)) {
|
|
std::fprintf(stderr, "[rt] d3d12_xs: %s is too long for a device slot; the default is kept\n", name);
|
|
return fallback;
|
|
}
|
|
errno = 0;
|
|
char* end = nullptr;
|
|
const unsigned long slot = std::strtoul(value, &end, 0);
|
|
if (errno != 0 || end == value || *end != '\0' || slot < 3 || slot > 127) {
|
|
std::fprintf(stderr, "[rt] d3d12_xs: %s=%s is not a device slot in 3..127; the default is kept\n", name,
|
|
value);
|
|
return fallback;
|
|
}
|
|
return static_cast<unsigned>(slot);
|
|
}
|
|
|
|
const XboxDeviceSlotRoutes& xbox_device_slot_routes() {
|
|
static const XboxDeviceSlotRoutes routes = [] {
|
|
XboxDeviceSlotRoutes r;
|
|
const unsigned frame_interval = env_device_slot("LEONIDA_XS_FRAME_INTERVAL_SLOT", r.frame_interval);
|
|
const unsigned schedule_event = env_device_slot("LEONIDA_XS_SCHEDULE_EVENT_SLOT", r.schedule_event);
|
|
const unsigned wait_event = env_device_slot("LEONIDA_XS_WAIT_EVENT_SLOT", r.wait_event);
|
|
const unsigned queue_x = env_device_slot("LEONIDA_XS_QUEUE_X_SLOT", r.queue_x);
|
|
const unsigned list_x = env_device_slot("LEONIDA_XS_LIST_X_SLOT", r.list_x);
|
|
const bool moved = frame_interval != r.frame_interval || schedule_event != r.schedule_event ||
|
|
wait_event != r.wait_event || queue_x != r.queue_x || list_x != r.list_x;
|
|
r.frame_interval = frame_interval;
|
|
r.schedule_event = schedule_event;
|
|
r.wait_event = wait_event;
|
|
r.queue_x = queue_x;
|
|
r.list_x = list_x;
|
|
if (moved)
|
|
std::fprintf(stderr,
|
|
"[rt] d3d12_xs: Xbox device slots: SetFrameIntervalX=%u ScheduleFrameEventX=%u "
|
|
"WaitFrameEventX=%u CreateCommandQueueX=%u CreateCommandListX=%u\n",
|
|
r.frame_interval, r.schedule_event, r.wait_event, r.queue_x, r.list_x);
|
|
return r;
|
|
}();
|
|
return routes;
|
|
}
|
|
|
|
// The public HLE call shapes. SetFrameIntervalX's output and flags are unused while there is no output or pacing;
|
|
// ScheduleFrameEventX's reserved event pointer is a3 in the console call and a4 is its flags. The logged words are the
|
|
// caller's, so a trace names the slot even when an override moved it.
|
|
void log_xbox_route_once(unsigned slot, const char* name, std::uint64_t a1, std::uint64_t a2, std::uint64_t a3,
|
|
std::uint64_t a4, std::uint64_t a5) {
|
|
static std::atomic<bool> logged[kMaxSlots];
|
|
if (slot >= kMaxSlots) return;
|
|
bool expected = false;
|
|
if (!logged[slot].compare_exchange_strong(expected, true, std::memory_order_relaxed)) return;
|
|
std::fprintf(stderr,
|
|
"[rt] d3d12_xs: device slot %u routed to %s (args 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n", slot,
|
|
name, static_cast<unsigned long long>(a1), static_cast<unsigned long long>(a2),
|
|
static_cast<unsigned long long>(a3), static_cast<unsigned long long>(a4),
|
|
static_cast<unsigned long long>(a5));
|
|
}
|
|
|
|
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;
|
|
count_hit(g_heap_hits[slot]);
|
|
const bool first = first_call(g_heap_first[slot]);
|
|
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 (IsEqualIID(*riid, __uuidof(IDXGIObject)) || IsEqualIID(*riid, __uuidof(IDXGIDevice)) ||
|
|
IsEqualIID(*riid, __uuidof(IDXGIDevice1))) {
|
|
*out = &self->dxgi_vtable;
|
|
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;
|
|
}
|
|
|
|
// ---- IDXGIDevice1 on the device ---------------------------------------------------------------------------------
|
|
//
|
|
// A GDKX title finds its output through the device: QueryInterface for IDXGIDevice1, GetAdapter, EnumOutputs(0),
|
|
// and hands that output to SetFrameIntervalX (docs/d3d12_vulkan.md section 4.5, from the public samples). A PC
|
|
// ID3D12Device has no IDXGIDevice, so the proxy answers it itself: a second vtable pointer inside the proxy object,
|
|
// with the proxy's reference count and QueryInterface, so the two pointers are one COM object. Only IDXGIDevice and
|
|
// IDXGIDevice1 are answered (what the samples ask for). The 14 slots are the public dxgi.h order; that the console
|
|
// keeps it is the same assumption as for the queue and the fence.
|
|
//
|
|
// GetAdapter and GetParent hand out the adapter of the device behind the proxy. The provider says how: the default
|
|
// asks the host's DXGI for the adapter with the device's LUID (the host backend); set_device_adapter_provider puts
|
|
// the Vulkan layer's own adapter there, so that backend never loads the host's dxgi.dll for it.
|
|
constexpr unsigned kDxgiDeviceSlots = 14; // IUnknown 3, IDXGIObject 4, IDXGIDevice 5, IDXGIDevice1 2
|
|
|
|
DeviceProxy* dxgi_owner(void* self) {
|
|
return reinterpret_cast<DeviceProxy*>(static_cast<char*>(self) - offsetof(DeviceProxy, dxgi_vtable));
|
|
}
|
|
|
|
HRESULT host_adapter(ID3D12Device* real, REFIID riid, void** out) {
|
|
using CreateFactoryFn = HRESULT(WINAPI*)(REFIID, void**);
|
|
static const CreateFactoryFn create_factory = [] {
|
|
const HMODULE dxgi = LoadLibraryExA("dxgi.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
|
|
return dxgi ? reinterpret_cast<CreateFactoryFn>(
|
|
reinterpret_cast<void*>(GetProcAddress(dxgi, "CreateDXGIFactory1")))
|
|
: nullptr;
|
|
}();
|
|
if (!create_factory) return DXGI_ERROR_NOT_FOUND;
|
|
IDXGIFactory4* factory = nullptr;
|
|
HRESULT hr = create_factory(__uuidof(IDXGIFactory4), reinterpret_cast<void**>(&factory));
|
|
if (FAILED(hr) || !factory) return FAILED(hr) ? hr : DXGI_ERROR_NOT_FOUND;
|
|
hr = factory->EnumAdapterByLuid(real->GetAdapterLuid(), riid, out);
|
|
factory->Release();
|
|
return hr;
|
|
}
|
|
|
|
HRESULT dxgi_adapter(DeviceProxy* self, REFIID riid, void** out) {
|
|
if (!out) return E_POINTER;
|
|
*out = nullptr;
|
|
const DxgiAdapterProvider provider = self->adapter_provider ? self->adapter_provider : &host_adapter;
|
|
const HRESULT hr = provider(self->real, riid, out);
|
|
if (FAILED(hr)) *out = nullptr;
|
|
return hr;
|
|
}
|
|
|
|
void note_dxgi(const char* method) {
|
|
d3d12_trace::record(method);
|
|
}
|
|
|
|
HRESULT WINAPI dxgi_query_interface(void* self, const IID* riid, void** out) {
|
|
return proxy_query_interface(dxgi_owner(self), riid, out);
|
|
}
|
|
ULONG WINAPI dxgi_add_ref(void* self) { return proxy_add_ref(dxgi_owner(self)); }
|
|
ULONG WINAPI dxgi_release(void* self) { return proxy_release(dxgi_owner(self)); }
|
|
|
|
// IDXGIObject's private data is the device's: ID3D12Object has the same three methods with the same arguments.
|
|
HRESULT WINAPI dxgi_set_private_data(void* self, const GUID* name, UINT size, const void* data) {
|
|
if (!name) return E_INVALIDARG;
|
|
return dxgi_owner(self)->real->SetPrivateData(*name, size, data);
|
|
}
|
|
HRESULT WINAPI dxgi_set_private_data_interface(void* self, const GUID* name, const IUnknown* object) {
|
|
if (!name) return E_INVALIDARG;
|
|
return dxgi_owner(self)->real->SetPrivateDataInterface(*name, object);
|
|
}
|
|
HRESULT WINAPI dxgi_get_private_data(void* self, const GUID* name, UINT* size, void* data) {
|
|
if (!name) return E_INVALIDARG;
|
|
return dxgi_owner(self)->real->GetPrivateData(*name, size, data);
|
|
}
|
|
HRESULT WINAPI dxgi_get_parent(void* self, const IID* riid, void** out) {
|
|
note_dxgi("XboxIDXGIDevice::GetParent");
|
|
if (!riid) return E_POINTER;
|
|
return dxgi_adapter(dxgi_owner(self), *riid, out);
|
|
}
|
|
HRESULT WINAPI dxgi_get_adapter(void* self, IDXGIAdapter** out) {
|
|
note_dxgi("XboxIDXGIDevice::GetAdapter");
|
|
return dxgi_adapter(dxgi_owner(self), __uuidof(IDXGIAdapter), reinterpret_cast<void**>(out));
|
|
}
|
|
// CreateSurface and QueryResourceResidency are D3D10/11-era methods no D3D12 title has a use for; what the console
|
|
// answers is not measured, so they are refused and say so once.
|
|
HRESULT WINAPI dxgi_create_surface(void*, const DXGI_SURFACE_DESC*, UINT, DXGI_USAGE, const DXGI_SHARED_RESOURCE*,
|
|
IDXGISurface** out) {
|
|
static std::atomic<bool> logged{false};
|
|
if (!logged.exchange(true, std::memory_order_relaxed))
|
|
std::fprintf(stderr, "[rt] d3d12_xs: IDXGIDevice::CreateSurface is not implemented; returning E_NOTIMPL\n");
|
|
if (out) *out = nullptr;
|
|
return E_NOTIMPL;
|
|
}
|
|
HRESULT WINAPI dxgi_query_resource_residency(void*, IUnknown* const*, DXGI_RESIDENCY*, UINT) {
|
|
static std::atomic<bool> logged{false};
|
|
if (!logged.exchange(true, std::memory_order_relaxed))
|
|
std::fprintf(stderr,
|
|
"[rt] d3d12_xs: IDXGIDevice::QueryResourceResidency is not implemented; returning E_NOTIMPL\n");
|
|
return E_NOTIMPL;
|
|
}
|
|
// The priority and the frame latency are kept and read back; nothing acts on them (the Xbox presenter's latency is
|
|
// SetFrameIntervalX's). The ranges are the documented ones: -7..7, and 1..16 with 0 for the default of 3.
|
|
HRESULT WINAPI dxgi_set_gpu_thread_priority(void* self, INT priority) {
|
|
if (priority < -7 || priority > 7) return E_INVALIDARG;
|
|
dxgi_owner(self)->gpu_thread_priority.store(priority, std::memory_order_relaxed);
|
|
return S_OK;
|
|
}
|
|
HRESULT WINAPI dxgi_get_gpu_thread_priority(void* self, INT* priority) {
|
|
if (!priority) return E_POINTER;
|
|
*priority = dxgi_owner(self)->gpu_thread_priority.load(std::memory_order_relaxed);
|
|
return S_OK;
|
|
}
|
|
HRESULT WINAPI dxgi_set_maximum_frame_latency(void* self, UINT latency) {
|
|
if (latency > 16) return DXGI_ERROR_INVALID_CALL;
|
|
dxgi_owner(self)->max_frame_latency.store(latency ? latency : 3, std::memory_order_relaxed);
|
|
return S_OK;
|
|
}
|
|
HRESULT WINAPI dxgi_get_maximum_frame_latency(void* self, UINT* latency) {
|
|
if (!latency) return E_POINTER;
|
|
*latency = dxgi_owner(self)->max_frame_latency.load(std::memory_order_relaxed);
|
|
return S_OK;
|
|
}
|
|
|
|
void** get_dxgi_vtable() {
|
|
static void* table[kDxgiDeviceSlots] = {
|
|
reinterpret_cast<void*>(&dxgi_query_interface), // 0
|
|
reinterpret_cast<void*>(&dxgi_add_ref), // 1
|
|
reinterpret_cast<void*>(&dxgi_release), // 2
|
|
reinterpret_cast<void*>(&dxgi_set_private_data), // 3 IDXGIObject
|
|
reinterpret_cast<void*>(&dxgi_set_private_data_interface),
|
|
reinterpret_cast<void*>(&dxgi_get_private_data),
|
|
reinterpret_cast<void*>(&dxgi_get_parent),
|
|
reinterpret_cast<void*>(&dxgi_get_adapter), // 7 IDXGIDevice
|
|
reinterpret_cast<void*>(&dxgi_create_surface),
|
|
reinterpret_cast<void*>(&dxgi_query_resource_residency),
|
|
reinterpret_cast<void*>(&dxgi_set_gpu_thread_priority),
|
|
reinterpret_cast<void*>(&dxgi_get_gpu_thread_priority),
|
|
reinterpret_cast<void*>(&dxgi_set_maximum_frame_latency), // 12 IDXGIDevice1
|
|
reinterpret_cast<void*>(&dxgi_get_maximum_frame_latency),
|
|
};
|
|
return table;
|
|
}
|
|
|
|
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 (!out) return E_POINTER;
|
|
*out = nullptr;
|
|
if (!riid) 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;
|
|
}
|
|
if (!d3d12_trace::enabled())
|
|
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;
|
|
}
|
|
|
|
// ---- Command queue and fence proxies --------------------------------------------------------------------------
|
|
//
|
|
// Device slots 8 (CreateCommandQueue), 75 (CreateCommandQueue1), 36 (CreateFence) and 32 (OpenSharedHandle, for a
|
|
// fence) wrap what they create, so a title never holds a PC or Vulkan-layer queue or fence. The proxies forward
|
|
// the public chain in PC order, exactly like the device proxy, and a slot past the PC vtable is logged and refused
|
|
// with E_NOTIMPL. PresentX, SuspendX and ResumeX are queue methods past the public chain
|
|
// (docs/d3d12_command_objects.md §5.3). Their slots are not known and nothing is guessed: the first run of the
|
|
// executable names them in the log.
|
|
//
|
|
// Slot numbers are those of the Windows SDK 10.0.26100 d3d12.h: ID3D12CommandQueue has 19, ID3D12Fence 11 and
|
|
// ID3D12Fence1 12. That the console keeps these public slots, and keeps device slots 8/32/36/75, is an assumption
|
|
// the shipped binaries cannot test (§3.5: the package has no queue or fence client). Every queue and fence method
|
|
// takes integer and pointer arguments only; GetDesc returns its struct through the hidden pointer in RDX, which is
|
|
// forwarded unchanged (no Xbox variant is measured, §6).
|
|
//
|
|
// The refused range is the proxy's own table: 128 queue slots and 64 fence slots, every one a thunk. The table sits
|
|
// at the end of the first page of a 64 KiB reservation whose other pages are never committed, so a slot index past
|
|
// the table makes the vtable read itself fault, at an address that names the slot (a vectored handler logs it and
|
|
// lets the fault continue), instead of jumping through whatever memory follows. A title whose vtable read lands
|
|
// further away than that reservation is not covered.
|
|
//
|
|
// Objects cross back into the real runtime wherever a device or queue method takes them, and the real object is
|
|
// passed there: queue Signal/Wait (14/15) take a fence; device CreateSharedHandle (31) a device child,
|
|
// MakeResident/Evict (34/35), SetResidencyPriority (46) and EnqueueMakeResident (50) pageables, and
|
|
// SetEventOnMultipleFenceCompletion (45) and EnqueueMakeResident fences. The heap proxies of slot 14 are pageables
|
|
// and device children too and are unwrapped in the same places. Command lists are not wrapped yet, so
|
|
// ExecuteCommandLists passes them through, and a heap proxy still reaches the real runtime through the list's
|
|
// SetDescriptorHeaps. A command-list proxy (docs/d3d12_command_objects.md §3.5a) adds its vtable to
|
|
// proxy_real_object, which every unwrap goes through, and a queue slot 10 branch in child_slot that unwraps the list
|
|
// array (UnwrappedArray) before forwarding, and unwraps heap proxies in its own SetDescriptorHeaps.
|
|
//
|
|
// QueryInterface answers only the interfaces the proxy models (the base chain and its own interface, Fence1 when
|
|
// the real fence has it), like the device and heap proxies, so the COM identity holds and no real object leaks to
|
|
// the title. Leonida's own code reaches the real object through queue_proxy_real_queue / fence_proxy_real_fence /
|
|
// allocator_proxy_real_allocator.
|
|
//
|
|
// Command allocators (device slot 9) are the third kind: their public chain is 9 slots (Reset at 8). Slot 9 wraps
|
|
// only an allocator that answers kLeonidaCommandAllocatorTag, because a list takes the title's allocator in
|
|
// CreateCommandList and Reset and only the Vulkan layer's lists resolve a proxy there (CommandAllocator::from);
|
|
// a PC runtime allocator is handed out unchanged until a command-list proxy unwraps it at Reset.
|
|
|
|
enum class ChildKind { queue, fence, allocator };
|
|
|
|
constexpr unsigned kQueueSlots = 128; // the table: 19 public slots plus a refused tail
|
|
constexpr unsigned kFenceSlots = 64; // the table: 12 public slots plus a refused tail
|
|
constexpr unsigned kQueuePublicSlots = 19; // ID3D12CommandQueue
|
|
constexpr unsigned kFencePublicSlots = 11; // ID3D12Fence
|
|
constexpr unsigned kFence1PublicSlots = 12; // ID3D12Fence1
|
|
constexpr unsigned kAllocatorSlots = 64; // the table: 9 public slots plus a refused tail
|
|
constexpr unsigned kAllocatorPublicSlots = 9; // ID3D12CommandAllocator: the base chain and Reset (slot 8)
|
|
constexpr SIZE_T kTableRegion = 64 * 1024; // one allocation granule: the table page plus 15 never-committed pages
|
|
static_assert(kQueueSlots > kQueuePublicSlots && kFenceSlots > kFence1PublicSlots &&
|
|
kAllocatorSlots > kAllocatorPublicSlots,
|
|
"the tables need a tail");
|
|
static_assert(kQueueSlots * sizeof(void*) <= 4096 && kFenceSlots * sizeof(void*) <= 4096 &&
|
|
kAllocatorSlots * sizeof(void*) <= 4096,
|
|
"a table must fit in the first page of its reservation");
|
|
|
|
constexpr unsigned kQueueSignal = 14, kQueueWait = 15; // (ID3D12Fence*, UINT64)
|
|
|
|
struct ChildProxy {
|
|
void** vtable; // offset 0: the caller's ID3D12CommandQueue* / ID3D12Fence* points here
|
|
IUnknown* real; // offset 8: the PC or Vulkan-layer object every thunk forwards to
|
|
DeviceProxy* device; // keeps the Xbox-ABI device alive; GetDevice returns it
|
|
void** real_vtable;
|
|
unsigned slot_count; // public slots the real object implements; the rest of the table is refused
|
|
std::atomic<unsigned long> refs;
|
|
};
|
|
|
|
template <ChildKind K>
|
|
struct ChildStats {
|
|
static constexpr unsigned kSlots = K == ChildKind::queue ? kQueueSlots
|
|
: K == ChildKind::fence ? kFenceSlots
|
|
: kAllocatorSlots;
|
|
static inline std::atomic<std::uint64_t> hits[kSlots];
|
|
static inline std::atomic<bool> first[kSlots];
|
|
static inline std::atomic<bool> beyond_warned[kSlots];
|
|
static inline std::atomic<std::uint64_t> overruns{0};
|
|
// Where the table and its reservation are: written once, before the table is handed out.
|
|
static inline std::atomic<std::uintptr_t> table_begin{0}, table_end{0}, region_end{0};
|
|
};
|
|
|
|
template <ChildKind K>
|
|
constexpr const char* child_kind_name() {
|
|
return K == ChildKind::queue ? "queue" : K == ChildKind::fence ? "fence" : "allocator";
|
|
}
|
|
|
|
template <ChildKind K>
|
|
const char* child_public_name(unsigned slot) {
|
|
static constexpr const char* object[] = {"QueryInterface", "AddRef", "Release", "GetPrivateData",
|
|
"SetPrivateData", "SetPrivateDataInterface", "SetName", "GetDevice"};
|
|
static constexpr const char* queue[] = {"UpdateTileMappings", "CopyTileMappings", "ExecuteCommandLists",
|
|
"SetMarker", "BeginEvent", "EndEvent", "Signal", "Wait",
|
|
"GetTimestampFrequency", "GetClockCalibration", "GetDesc"};
|
|
static constexpr const char* fence[] = {"GetCompletedValue", "SetEventOnCompletion", "Signal",
|
|
"GetCreationFlags"};
|
|
if (slot < 8) return object[slot];
|
|
if constexpr (K == ChildKind::queue) {
|
|
if (slot < kQueuePublicSlots) return queue[slot - 8];
|
|
} else if constexpr (K == ChildKind::fence) {
|
|
if (slot < kFence1PublicSlots) return fence[slot - 8];
|
|
} else {
|
|
if (slot == 8) return "Reset";
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
// Fixed names for the trace channel (d3d12_trace::record takes fixed strings): one per table slot, built once, in
|
|
// the trace grammar of runtime/README.md and check_d3d12_trace.py (an identifier with `::`, no spaces):
|
|
// XboxID3D12CommandQueue::Signal, and XboxID3D12CommandQueue::Slot40 for a slot past the public chain.
|
|
template <ChildKind K>
|
|
const char* child_trace_name(unsigned slot) {
|
|
static const auto names = [] {
|
|
static char storage[ChildStats<K>::kSlots][64];
|
|
const char* iface = K == ChildKind::queue ? "XboxID3D12CommandQueue"
|
|
: K == ChildKind::fence ? "XboxID3D12Fence"
|
|
: "XboxID3D12CommandAllocator";
|
|
for (unsigned s = 0; s < ChildStats<K>::kSlots; ++s) {
|
|
const char* method = child_public_name<K>(s);
|
|
if (method)
|
|
std::snprintf(storage[s], sizeof(storage[s]), "%s::%s", iface, method);
|
|
else
|
|
std::snprintf(storage[s], sizeof(storage[s]), "%s::Slot%u", iface, s);
|
|
}
|
|
return &storage;
|
|
}();
|
|
return (*names)[slot < ChildStats<K>::kSlots ? slot : 0];
|
|
}
|
|
|
|
template <ChildKind K>
|
|
void note_child_slot(unsigned slot) {
|
|
using S = ChildStats<K>;
|
|
if (slot >= S::kSlots) return;
|
|
count_hit(S::hits[slot]);
|
|
const bool first = first_call(S::first[slot]);
|
|
const char* method = child_public_name<K>(slot);
|
|
if (first) {
|
|
std::fprintf(stderr, "[rt] d3d12_xs: %s slot %u (%s) first call\n", child_kind_name<K>(), slot,
|
|
method ? method : "past the public chain");
|
|
} else if (slot >= 3 && trace_enabled()) {
|
|
std::fprintf(stderr, "[rt] d3d12_xs: %s slot %u (%s) call\n", child_kind_name<K>(), slot,
|
|
method ? method : "past the public chain");
|
|
}
|
|
}
|
|
|
|
// The counters reset_device_proxy_stats clears for one kind: per-slot hits, first-call and beyond-table flags, and
|
|
// the faulted reads past the table.
|
|
template <ChildKind K>
|
|
void reset_child_stats() {
|
|
using S = ChildStats<K>;
|
|
for (unsigned slot = 0; slot < S::kSlots; ++slot) {
|
|
S::hits[slot].store(0, std::memory_order_relaxed);
|
|
S::first[slot].store(false, std::memory_order_relaxed);
|
|
S::beyond_warned[slot].store(false, std::memory_order_relaxed);
|
|
}
|
|
S::overruns.store(0, std::memory_order_relaxed);
|
|
}
|
|
|
|
// Frees a proxy whose last reference is gone, with the real object and the device reference it held.
|
|
void destroy_child(ChildProxy* self) {
|
|
IUnknown* real = self->real;
|
|
DeviceProxy* device = self->device;
|
|
delete self;
|
|
if (real) real->Release();
|
|
if (device) proxy_release(device);
|
|
}
|
|
|
|
template <ChildKind K>
|
|
unsigned long child_add_ref(ChildProxy* self) {
|
|
note_child_slot<K>(1);
|
|
return self->refs.fetch_add(1, std::memory_order_relaxed) + 1;
|
|
}
|
|
|
|
template <ChildKind K>
|
|
unsigned long child_release(ChildProxy* self) {
|
|
note_child_slot<K>(2);
|
|
const unsigned long refs = self->refs.fetch_sub(1, std::memory_order_acq_rel) - 1;
|
|
if (refs == 0) destroy_child(self);
|
|
return refs;
|
|
}
|
|
|
|
// The interfaces the proxy answers: the base chain and the object's own interface.
|
|
template <ChildKind K>
|
|
bool is_child_iid(const IID& iid) {
|
|
if (IsEqualIID(iid, __uuidof(IUnknown)) || IsEqualIID(iid, __uuidof(ID3D12Object)) ||
|
|
IsEqualIID(iid, __uuidof(ID3D12DeviceChild)) || IsEqualIID(iid, __uuidof(ID3D12Pageable)))
|
|
return true;
|
|
if constexpr (K == ChildKind::queue) {
|
|
return IsEqualIID(iid, __uuidof(ID3D12CommandQueue));
|
|
} else if constexpr (K == ChildKind::fence) {
|
|
return IsEqualIID(iid, __uuidof(ID3D12Fence)) || IsEqualIID(iid, __uuidof(ID3D12Fence1));
|
|
} else {
|
|
return IsEqualIID(iid, __uuidof(ID3D12CommandAllocator));
|
|
}
|
|
}
|
|
|
|
bool supports_iid(IUnknown* 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;
|
|
}
|
|
|
|
// An interface the real object implements but the proxy does not model is refused where the PC runtime would answer:
|
|
// logged once per interface (the first 32 of them), so a title that stops after such a refusal shows what it asked
|
|
// for. Also reached from wrap_child, for the interface a title creates a queue or fence as.
|
|
void log_refused_iid(const char* kind, const IID& iid) {
|
|
static SRWLOCK lock = SRWLOCK_INIT;
|
|
static IID seen[32];
|
|
static unsigned count = 0;
|
|
AcquireSRWLockExclusive(&lock);
|
|
bool known = false;
|
|
for (unsigned i = 0; i < count && !known; ++i) known = IsEqualIID(seen[i], iid) != 0;
|
|
const bool log = !known && count < std::size(seen);
|
|
if (log) seen[count++] = iid;
|
|
ReleaseSRWLockExclusive(&lock);
|
|
if (!log) return;
|
|
std::fprintf(stderr,
|
|
"[rt] d3d12_xs: %s proxy refuses {%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}, which the real "
|
|
"object implements; E_NOINTERFACE\n",
|
|
kind, iid.Data1, iid.Data2, iid.Data3, iid.Data4[0], iid.Data4[1], iid.Data4[2], iid.Data4[3],
|
|
iid.Data4[4], iid.Data4[5], iid.Data4[6], iid.Data4[7]);
|
|
}
|
|
|
|
template <ChildKind K>
|
|
HRESULT child_query_interface(ChildProxy* self, const IID* riid, void** out) {
|
|
note_child_slot<K>(0);
|
|
if (!out) return E_POINTER;
|
|
*out = nullptr;
|
|
if (!riid) return E_POINTER;
|
|
// Only what the proxy models, and of that only what the real object implements (ID3D12Fence1 is optional).
|
|
if (!is_child_iid<K>(*riid)) {
|
|
if (supports_iid(self->real, *riid)) log_refused_iid(child_kind_name<K>(), *riid);
|
|
return E_NOINTERFACE;
|
|
}
|
|
if (!supports_iid(self->real, *riid)) return E_NOINTERFACE;
|
|
*out = self;
|
|
child_add_ref<K>(self);
|
|
return S_OK;
|
|
}
|
|
|
|
// ID3D12DeviceChild::GetDevice hands back the Xbox-ABI device, not the real one. Without an output it is the real
|
|
// object's own answer (what the PC runtime returns for a null pointer).
|
|
template <ChildKind K>
|
|
HRESULT child_get_device(ChildProxy* self, const IID* riid, void** out) {
|
|
note_child_slot<K>(7);
|
|
if (out) *out = nullptr; // _COM_Outptr_: NULL on every failure (bc59ef46)
|
|
if (!riid) return E_POINTER;
|
|
if (!out) {
|
|
using Fn = HRESULT(WINAPI*)(IUnknown*, const IID*, void**);
|
|
return reinterpret_cast<Fn>(self->real_vtable[7])(self->real, riid, nullptr);
|
|
}
|
|
if (!self->device) return E_NOINTERFACE;
|
|
return proxy_query_interface(self->device, riid, out);
|
|
}
|
|
|
|
void** get_heap_vtable();
|
|
void** get_list_vtable();
|
|
|
|
template <ChildKind K>
|
|
void** get_child_vtable();
|
|
|
|
template <ChildKind K>
|
|
bool is_child_proxy(const void* object) {
|
|
return object && *reinterpret_cast<void* const*>(object) == reinterpret_cast<void*>(get_child_vtable<K>());
|
|
}
|
|
|
|
constexpr unsigned kListSlots = 160;
|
|
constexpr unsigned kConsoleSetDescriptorHeaps = 32;
|
|
constexpr unsigned kConsoleExecuteBundle = 31;
|
|
constexpr unsigned kConsoleBarrier = 146;
|
|
|
|
struct ListProxy {
|
|
void** vtable; // +0x00
|
|
ID3D12GraphicsCommandList* real; // +0x08
|
|
DeviceProxy* device; // +0x10: keeps the Xbox-ABI device alive; GetDevice returns it
|
|
// +0x18..: the console object body a title's inline writers read. +0x18 holds the write cursor, +0x28 and
|
|
// +0x30 the record limits; the rest is room for the measured flags, header table and pipeline record.
|
|
alignas(8) std::uint8_t console_fields[0x600];
|
|
std::atomic<unsigned long> refs; // past the console body
|
|
unsigned pc_slot_count; // slots the real list implements; mapped slots past it are refused
|
|
|
|
void pin_cursor_at_limit() {
|
|
const std::uint64_t at_limit = static_cast<std::uint64_t>(
|
|
reinterpret_cast<std::uintptr_t>(this) + 0x18 + 0x500);
|
|
for (const size_t offset : {0x18, 0x28, 0x30})
|
|
std::memcpy(reinterpret_cast<std::uint8_t*>(this) + offset, &at_limit, sizeof(at_limit));
|
|
}
|
|
};
|
|
static_assert(offsetof(ListProxy, console_fields) == 0x18, "the console fields start at +0x18");
|
|
|
|
// The real object behind a heap, queue, fence or list proxy, or null when `object` is none of them.
|
|
IUnknown* proxy_real_object(const void* object) {
|
|
if (!object) return nullptr;
|
|
void* const vtable = *reinterpret_cast<void* const*>(object);
|
|
if (vtable == reinterpret_cast<void*>(get_heap_vtable())) return static_cast<const HeapProxy*>(object)->real;
|
|
if (vtable == reinterpret_cast<void*>(get_child_vtable<ChildKind::queue>()) ||
|
|
vtable == reinterpret_cast<void*>(get_child_vtable<ChildKind::fence>()) ||
|
|
vtable == reinterpret_cast<void*>(get_child_vtable<ChildKind::allocator>()))
|
|
return static_cast<const ChildProxy*>(object)->real;
|
|
if (vtable == reinterpret_cast<void*>(get_list_vtable()))
|
|
return static_cast<const ListProxy*>(object)->real;
|
|
return nullptr;
|
|
}
|
|
|
|
// `object` with a proxy replaced by its real object; anything else (a real object, null) unchanged.
|
|
inline std::uint64_t unwrap_object(std::uint64_t object) {
|
|
IUnknown* real = proxy_real_object(reinterpret_cast<const void*>(object));
|
|
return real ? reinterpret_cast<std::uint64_t>(real) : object;
|
|
}
|
|
|
|
// A copy of an array of interface pointers with every proxy replaced by its real object. Nothing here throws, since
|
|
// an exception must not leave a thunk for the title's code: up to 16 entries are kept inline (no allocation, which
|
|
// also suits a per-frame ExecuteCommandLists), a longer array is allocated with new (std::nothrow).
|
|
class UnwrappedArray {
|
|
public:
|
|
// Copies `count` entries (a UINT in every PC prototype) of `objects`. False on allocation failure.
|
|
bool fill(std::uint64_t objects, std::uint64_t count) {
|
|
const auto n = static_cast<std::uint32_t>(count);
|
|
if (!objects || !n) return true;
|
|
std::uint64_t* copy = inline_;
|
|
if (n > std::size(inline_)) {
|
|
heap_.reset(new (std::nothrow) std::uint64_t[n]);
|
|
if (!heap_) return false;
|
|
copy = heap_.get();
|
|
}
|
|
const auto* in = reinterpret_cast<const std::uint64_t*>(objects);
|
|
for (std::uint32_t i = 0; i < n; ++i) copy[i] = unwrap_object(in[i]);
|
|
data_ = copy;
|
|
return true;
|
|
}
|
|
// The copy to pass on, or 0 when there was no array (the caller's argument then goes on unchanged).
|
|
std::uint64_t data() const { return reinterpret_cast<std::uint64_t>(data_); }
|
|
|
|
private:
|
|
std::uint64_t inline_[16];
|
|
std::unique_ptr<std::uint64_t[]> heap_;
|
|
std::uint64_t* data_ = nullptr;
|
|
};
|
|
|
|
struct ListStats {
|
|
static inline std::atomic<std::uint64_t> hits[kListSlots];
|
|
static inline std::atomic<bool> first[kListSlots];
|
|
};
|
|
|
|
inline void note_list_slot(unsigned slot, std::uint64_t a1 = 0, std::uint64_t a2 = 0, std::uint64_t a3 = 0, std::uint64_t a4 = 0) {
|
|
if (slot >= kListSlots) return;
|
|
ListStats::hits[slot].fetch_add(1, std::memory_order_relaxed);
|
|
bool expected = false;
|
|
if (ListStats::first[slot].compare_exchange_strong(expected, true, std::memory_order_relaxed))
|
|
std::fprintf(stderr, "[rt] d3d12_xs: list slot %u first call (args 0x%llx 0x%llx 0x%llx 0x%llx)\n", slot,
|
|
static_cast<unsigned long long>(a1), static_cast<unsigned long long>(a2),
|
|
static_cast<unsigned long long>(a3), static_cast<unsigned long long>(a4));
|
|
}
|
|
|
|
inline bool is_list_iid(const IID& iid) {
|
|
return IsEqualIID(iid, __uuidof(IUnknown)) || IsEqualIID(iid, __uuidof(ID3D12Object)) ||
|
|
IsEqualIID(iid, __uuidof(ID3D12DeviceChild)) || IsEqualIID(iid, __uuidof(ID3D12CommandList)) ||
|
|
IsEqualIID(iid, __uuidof(ID3D12GraphicsCommandList)) ||
|
|
IsEqualIID(iid, __uuidof(ID3D12GraphicsCommandList1)) ||
|
|
IsEqualIID(iid, __uuidof(ID3D12GraphicsCommandList2)) ||
|
|
IsEqualIID(iid, __uuidof(ID3D12GraphicsCommandList3)) ||
|
|
IsEqualIID(iid, __uuidof(ID3D12GraphicsCommandList4)) ||
|
|
IsEqualIID(iid, __uuidof(ID3D12GraphicsCommandList5)) ||
|
|
IsEqualIID(iid, __uuidof(ID3D12GraphicsCommandList6)) ||
|
|
IsEqualIID(iid, __uuidof(ID3D12GraphicsCommandList7)) ||
|
|
IsEqualIID(iid, __uuidof(ID3D12GraphicsCommandList8));
|
|
}
|
|
|
|
inline unsigned long list_add_ref(ListProxy* self) { return self->refs.fetch_add(1, std::memory_order_relaxed) + 1; }
|
|
|
|
inline void destroy_list(ListProxy* self) {
|
|
IUnknown* real = self->real;
|
|
DeviceProxy* device = self->device;
|
|
delete self;
|
|
if (real) real->Release();
|
|
if (device) proxy_release(device);
|
|
}
|
|
|
|
inline unsigned long list_release(ListProxy* self) {
|
|
const unsigned long refs = self->refs.fetch_sub(1, std::memory_order_acq_rel) - 1;
|
|
if (refs == 0) destroy_list(self);
|
|
return refs;
|
|
}
|
|
|
|
inline HRESULT list_query_interface(ListProxy* self, const IID* riid, void** out) {
|
|
note_list_slot(0, reinterpret_cast<std::uint64_t>(riid), reinterpret_cast<std::uint64_t>(out), 0, 0);
|
|
if (!out) return E_POINTER;
|
|
*out = nullptr;
|
|
if (!riid) return E_POINTER;
|
|
if (!is_list_iid(*riid)) {
|
|
if (supports_iid(self->real, *riid)) log_refused_iid("list", *riid);
|
|
return E_NOINTERFACE;
|
|
}
|
|
if (!supports_iid(self->real, *riid)) return E_NOINTERFACE;
|
|
*out = self;
|
|
list_add_ref(self);
|
|
return S_OK;
|
|
}
|
|
|
|
inline unsigned detect_list_slot_count(IUnknown* real) {
|
|
if (supports_iid(real, __uuidof(ID3D12GraphicsCommandList8))) return 82;
|
|
if (supports_iid(real, __uuidof(ID3D12GraphicsCommandList7))) return 81;
|
|
if (supports_iid(real, __uuidof(ID3D12GraphicsCommandList6))) return 80;
|
|
if (supports_iid(real, __uuidof(ID3D12GraphicsCommandList5))) return 79;
|
|
if (supports_iid(real, __uuidof(ID3D12GraphicsCommandList4))) return 77;
|
|
if (supports_iid(real, __uuidof(ID3D12GraphicsCommandList3))) return 68;
|
|
if (supports_iid(real, __uuidof(ID3D12GraphicsCommandList2))) return 67;
|
|
if (supports_iid(real, __uuidof(ID3D12GraphicsCommandList1))) return 66;
|
|
if (supports_iid(real, __uuidof(ID3D12GraphicsCommandList))) return 60;
|
|
return 9;
|
|
}
|
|
|
|
template <unsigned Slot>
|
|
std::uint64_t WINAPI list_slot(ListProxy* 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(list_query_interface(self, reinterpret_cast<const IID*>(a1),
|
|
reinterpret_cast<void**>(a2)));
|
|
} else if constexpr (Slot == 1) {
|
|
return list_add_ref(self);
|
|
} else if constexpr (Slot == 2) {
|
|
return list_release(self);
|
|
} else if constexpr (Slot == 7) { // GetDevice
|
|
note_list_slot(7, a1, a2, 0, 0);
|
|
if (a2) *reinterpret_cast<void**>(a2) = nullptr;
|
|
if (!a1) return hr_to_u64(E_POINTER);
|
|
if (!a2) {
|
|
using Fn = HRESULT(WINAPI*)(IUnknown*, const IID*, void**);
|
|
return hr_to_u64(reinterpret_cast<Fn>((*reinterpret_cast<void***>(self->real))[7])(
|
|
self->real, reinterpret_cast<const IID*>(a1), nullptr));
|
|
}
|
|
return hr_to_u64(proxy_query_interface(self->device, reinterpret_cast<const IID*>(a1),
|
|
reinterpret_cast<void**>(a2)));
|
|
} else {
|
|
note_list_slot(Slot, a1, a2, a3, a4);
|
|
constexpr unsigned pc = console_to_pc_list_slot(Slot);
|
|
if constexpr (pc == kListNotForwarded) {
|
|
if constexpr (Slot == 148) {
|
|
static std::atomic<bool> logged{false};
|
|
if (!logged.exchange(true, std::memory_order_relaxed))
|
|
std::fprintf(stderr,
|
|
"[rt] d3d12_xs: console list slot 148 AllocateUploadMemory stub called (size %llu, align %llu)\n",
|
|
static_cast<unsigned long long>(a1), static_cast<unsigned long long>(a2));
|
|
}
|
|
return 0;
|
|
} else {
|
|
if (pc >= self->pc_slot_count) {
|
|
static std::atomic<bool> beyond_warned[kListSlots];
|
|
bool expected = false;
|
|
if (beyond_warned[Slot].compare_exchange_strong(expected, true, std::memory_order_relaxed))
|
|
std::fprintf(stderr,
|
|
"[rt] d3d12_xs: list slot %u maps to PC slot %u, beyond the real list's %u "
|
|
"slots; returning E_NOTIMPL\n",
|
|
Slot, pc, self->pc_slot_count);
|
|
return hr_to_u64(E_NOTIMPL);
|
|
}
|
|
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** const real_vtable = *reinterpret_cast<void***>(self->real);
|
|
UnwrappedArray heaps;
|
|
if constexpr (Slot == 10) {
|
|
a1 = unwrap_object(a1);
|
|
self->pin_cursor_at_limit();
|
|
} else if constexpr (Slot == kConsoleExecuteBundle) {
|
|
a1 = unwrap_object(a1);
|
|
} else if constexpr (Slot == kConsoleSetDescriptorHeaps) {
|
|
if (!heaps.fill(a2, a1)) return 0;
|
|
if (heaps.data()) a2 = heaps.data();
|
|
}
|
|
return reinterpret_cast<ForwardFn>(real_vtable[pc])(self->real, a1, a2, a3, a4, a5, a6, a7, a8, a9,
|
|
a10, a11, a12, a13, a14);
|
|
}
|
|
}
|
|
}
|
|
|
|
template <std::size_t... I>
|
|
void fill_list_vtable(void** vtable, std::index_sequence<I...>) {
|
|
((vtable[I] = reinterpret_cast<void*>(&list_slot<static_cast<unsigned>(I)>)), ...);
|
|
}
|
|
|
|
void** get_list_vtable() {
|
|
static void** vtable = [] {
|
|
static void* storage[kListSlots];
|
|
fill_list_vtable(storage, std::make_index_sequence<kListSlots>{});
|
|
return storage;
|
|
}();
|
|
return vtable;
|
|
}
|
|
|
|
HRESULT wrap_list(IUnknown* created, DeviceProxy* device, const IID* riid, void** out) {
|
|
if (out) *out = nullptr;
|
|
if (!created) return E_FAIL;
|
|
auto* proxy = new (std::nothrow) ListProxy();
|
|
if (!proxy) {
|
|
created->Release();
|
|
return E_OUTOFMEMORY;
|
|
}
|
|
proxy->vtable = get_list_vtable();
|
|
proxy->real = static_cast<ID3D12GraphicsCommandList*>(created);
|
|
proxy->device = device;
|
|
proxy->refs.store(1, std::memory_order_relaxed);
|
|
proxy->pc_slot_count = detect_list_slot_count(created);
|
|
proxy->pin_cursor_at_limit();
|
|
proxy_add_ref(device);
|
|
|
|
const HRESULT hr = list_query_interface(proxy, riid, out);
|
|
if (FAILED(hr)) {
|
|
destroy_list(proxy);
|
|
return hr;
|
|
}
|
|
static std::atomic<bool> logged{false};
|
|
if (!d3d12_trace::enabled() && (!logged.exchange(true, std::memory_order_relaxed) || trace_enabled()))
|
|
std::fprintf(stderr, "[rt] d3d12_xs: list proxy %p wraps %p (%u PC vtable slots)\n",
|
|
static_cast<void*>(proxy), static_cast<void*>(created), proxy->pc_slot_count);
|
|
proxy->refs.fetch_sub(1, std::memory_order_relaxed);
|
|
return S_OK;
|
|
}
|
|
|
|
// The vtable read for a slot past a table faults in the reservation behind it; name the slot. Only reads the
|
|
// exception record and writes with WriteFile, so it is safe whatever the faulting thread holds.
|
|
template <ChildKind K>
|
|
void report_table_overrun(std::uintptr_t address) {
|
|
using S = ChildStats<K>;
|
|
const std::uintptr_t begin = S::table_begin.load(std::memory_order_acquire);
|
|
const std::uintptr_t end = S::table_end.load(std::memory_order_acquire);
|
|
const std::uintptr_t limit = S::region_end.load(std::memory_order_acquire);
|
|
if (!end || address < end || address >= limit) return;
|
|
S::overruns.fetch_add(1, std::memory_order_relaxed);
|
|
char line[160];
|
|
unsigned long long slot = (address - begin) / sizeof(void*);
|
|
char digits[24];
|
|
int d = 0;
|
|
do {
|
|
digits[d++] = static_cast<char>('0' + slot % 10);
|
|
slot /= 10;
|
|
} while (slot && d < 23);
|
|
int n = 0;
|
|
const auto put = [&](const char* text) {
|
|
while (*text && n < static_cast<int>(sizeof(line)) - 1) line[n++] = *text++;
|
|
};
|
|
put("[rt] d3d12_xs: ");
|
|
put(child_kind_name<K>());
|
|
put(" slot ");
|
|
while (d > 0 && n < static_cast<int>(sizeof(line)) - 1) line[n++] = digits[--d];
|
|
put(" is past the proxy table; the vtable read faults\n");
|
|
DWORD written = 0;
|
|
WriteFile(GetStdHandle(STD_ERROR_HANDLE), line, static_cast<DWORD>(n), &written, nullptr);
|
|
}
|
|
|
|
LONG CALLBACK table_overrun_handler(EXCEPTION_POINTERS* info) {
|
|
const EXCEPTION_RECORD* record = info ? info->ExceptionRecord : nullptr;
|
|
if (record && record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION && record->NumberParameters >= 2) {
|
|
const auto address = static_cast<std::uintptr_t>(record->ExceptionInformation[1]);
|
|
report_table_overrun<ChildKind::queue>(address);
|
|
report_table_overrun<ChildKind::fence>(address);
|
|
report_table_overrun<ChildKind::allocator>(address);
|
|
}
|
|
return EXCEPTION_CONTINUE_SEARCH; // diagnostics only: the fault goes on to the title's handlers or the crash
|
|
}
|
|
|
|
template <ChildKind K, unsigned Slot>
|
|
std::uint64_t WINAPI child_slot(ChildProxy* 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) {
|
|
d3d12_trace::record(child_trace_name<K>(Slot));
|
|
return hr_to_u64(child_query_interface<K>(self, reinterpret_cast<const IID*>(a1),
|
|
reinterpret_cast<void**>(a2)));
|
|
} else if constexpr (Slot == 1) {
|
|
return child_add_ref<K>(self);
|
|
} else if constexpr (Slot == 2) {
|
|
return child_release<K>(self);
|
|
} else if constexpr (Slot == 7) {
|
|
d3d12_trace::record(child_trace_name<K>(Slot));
|
|
return hr_to_u64(child_get_device<K>(self, reinterpret_cast<const IID*>(a1),
|
|
reinterpret_cast<void**>(a2)));
|
|
} else {
|
|
note_child_slot<K>(Slot);
|
|
if (Slot >= self->slot_count) {
|
|
bool expected = false;
|
|
if (ChildStats<K>::beyond_warned[Slot].compare_exchange_strong(expected, true,
|
|
std::memory_order_relaxed)) {
|
|
std::fprintf(stderr,
|
|
"[rt] d3d12_xs: %s slot %u is beyond the PC %s vtable (%u slots); returning E_NOTIMPL "
|
|
"(args 0x%llx 0x%llx 0x%llx 0x%llx)\n",
|
|
child_kind_name<K>(), Slot, child_kind_name<K>(), self->slot_count,
|
|
static_cast<unsigned long long>(a1), static_cast<unsigned long long>(a2),
|
|
static_cast<unsigned long long>(a3), static_cast<unsigned long long>(a4));
|
|
}
|
|
d3d12_trace::record(child_trace_name<K>(Slot), "unsupported");
|
|
return hr_to_u64(E_NOTIMPL);
|
|
}
|
|
d3d12_trace::record(child_trace_name<K>(Slot)); // "called": the real object's result is not inspected
|
|
if constexpr (K == ChildKind::queue && (Slot == kQueueSignal || Slot == kQueueWait)) a1 = unwrap_object(a1);
|
|
UnwrappedArray unwrapped_lists;
|
|
if constexpr (K == ChildKind::queue && Slot == 10) {
|
|
if (!unwrapped_lists.fill(a2, a1)) return hr_to_u64(E_OUTOFMEMORY);
|
|
if (unwrapped_lists.data()) a2 = unwrapped_lists.data();
|
|
}
|
|
|
|
// The PC vtable entry with the real object as `this`; integer/pointer arguments, the hidden struct-return
|
|
// pointer and stack arguments keep their positions, as in the device proxy.
|
|
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 <ChildKind K, std::size_t... I>
|
|
void fill_child_vtable(void** vtable, std::index_sequence<I...>) {
|
|
((vtable[I] = reinterpret_cast<void*>(&child_slot<K, static_cast<unsigned>(I)>)), ...);
|
|
}
|
|
|
|
// True when the fault logger is installed (once per process; a failure is logged once).
|
|
bool install_table_overrun_handler() {
|
|
static const bool installed = [] {
|
|
if (AddVectoredExceptionHandler(0, &table_overrun_handler)) return true;
|
|
std::fprintf(stderr, "[rt] d3d12_xs: AddVectoredExceptionHandler failed (error %lu); a slot past a queue or "
|
|
"fence proxy table faults without being named\n", GetLastError());
|
|
return false;
|
|
}();
|
|
return installed;
|
|
}
|
|
|
|
template <ChildKind K>
|
|
void** get_child_vtable() {
|
|
static void** vtable = [] {
|
|
using S = ChildStats<K>;
|
|
constexpr std::size_t bytes = S::kSlots * sizeof(void*);
|
|
SYSTEM_INFO system{};
|
|
GetSystemInfo(&system);
|
|
const SIZE_T page = system.dwPageSize;
|
|
if (void* base = page >= bytes ? VirtualAlloc(nullptr, kTableRegion, MEM_RESERVE, PAGE_NOACCESS) : nullptr) {
|
|
if (VirtualAlloc(base, page, MEM_COMMIT, PAGE_READWRITE)) {
|
|
auto* table = reinterpret_cast<void**>(static_cast<char*>(base) + page - bytes);
|
|
fill_child_vtable<K>(table, std::make_index_sequence<S::kSlots>{});
|
|
DWORD old = 0;
|
|
if (!VirtualProtect(base, page, PAGE_READONLY, &old)) // the table works, but stays writable
|
|
std::fprintf(stderr, "[rt] d3d12_xs: %s proxy table stays writable (VirtualProtect error %lu)\n",
|
|
child_kind_name<K>(), GetLastError());
|
|
S::table_begin.store(reinterpret_cast<std::uintptr_t>(table), std::memory_order_release);
|
|
S::region_end.store(reinterpret_cast<std::uintptr_t>(base) + kTableRegion, std::memory_order_release);
|
|
// table_end is what "guarded" means (command_object_tables_guarded, the fault logger): set only when
|
|
// a fault past the table will be named. Without the logger the region still faults, unnamed.
|
|
if (install_table_overrun_handler())
|
|
S::table_end.store(reinterpret_cast<std::uintptr_t>(base) + page, std::memory_order_release);
|
|
return table;
|
|
}
|
|
VirtualFree(base, 0, MEM_RELEASE);
|
|
}
|
|
// No reservation: the table still works, but a slot past it is not caught.
|
|
std::fprintf(stderr, "[rt] d3d12_xs: %s proxy table without a guard region\n", child_kind_name<K>());
|
|
static void* storage[S::kSlots];
|
|
fill_child_vtable<K>(storage, std::make_index_sequence<S::kSlots>{});
|
|
return storage;
|
|
}();
|
|
return vtable;
|
|
}
|
|
|
|
// Wraps `created` (one reference, owned by the proxy on success, released on failure) and returns `riid` from the
|
|
// proxy through `out`.
|
|
template <ChildKind K>
|
|
HRESULT wrap_child(IUnknown* created, DeviceProxy* device, const IID* riid, void** out) {
|
|
if (!created) return E_FAIL;
|
|
ChildProxy* proxy = new (std::nothrow) ChildProxy;
|
|
if (!proxy) {
|
|
created->Release();
|
|
return E_OUTOFMEMORY;
|
|
}
|
|
proxy->vtable = get_child_vtable<K>();
|
|
proxy->real = created;
|
|
proxy->device = device;
|
|
proxy->real_vtable = *reinterpret_cast<void***>(created);
|
|
if constexpr (K == ChildKind::queue) {
|
|
proxy->slot_count = kQueuePublicSlots;
|
|
} else if constexpr (K == ChildKind::fence) {
|
|
proxy->slot_count = supports_iid(created, __uuidof(ID3D12Fence1)) ? kFence1PublicSlots : kFencePublicSlots;
|
|
} else {
|
|
proxy->slot_count = kAllocatorPublicSlots;
|
|
}
|
|
proxy->refs.store(1, std::memory_order_relaxed);
|
|
proxy_add_ref(device); // the object keeps its Xbox-ABI device alive for GetDevice
|
|
|
|
const HRESULT hr = child_query_interface<K>(proxy, riid, out);
|
|
if (FAILED(hr)) {
|
|
destroy_child(proxy); // no reference escaped
|
|
return hr;
|
|
}
|
|
// Titles create fences at run time: the first proxy of each kind is logged, every one only with XS_TRACE.
|
|
static std::atomic<bool> logged{false};
|
|
if (!d3d12_trace::enabled() && (!logged.exchange(true, std::memory_order_relaxed) || trace_enabled()))
|
|
std::fprintf(stderr, "[rt] d3d12_xs: %s proxy %p wraps %p (device proxy %p, %u public slots)\n",
|
|
child_kind_name<K>(), static_cast<void*>(proxy), static_cast<void*>(created),
|
|
static_cast<void*>(device), proxy->slot_count);
|
|
proxy->refs.fetch_sub(1, std::memory_order_relaxed); // drop the creation reference; `out` holds the caller's
|
|
return S_OK;
|
|
}
|
|
|
|
// Calls the real device's slot `Slot` with these five words in the positions the thunk received them (see "A thunk
|
|
// declares 14 argument words" at the top: no word may move to another stack position on the way).
|
|
template <unsigned Slot>
|
|
std::uint64_t forward_device(DeviceProxy* self, std::uint64_t a1, std::uint64_t a2, std::uint64_t a3,
|
|
std::uint64_t a4, std::uint64_t a5) {
|
|
using Fn = std::uint64_t(WINAPI*)(void*, std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t,
|
|
std::uint64_t);
|
|
return reinterpret_cast<Fn>(self->real_vtable[Slot])(self->real, a1, a2, a3, a4, a5);
|
|
}
|
|
|
|
// A device slot past the PC device's vtable (the real device predates the interface): E_NOTIMPL, logged once per
|
|
// slot. The generic thunk and the slots the proxy handles itself (31/34/35/45/46/50, 75) log the same line.
|
|
HRESULT refuse_beyond_device(unsigned slot, unsigned slot_count) {
|
|
bool expected = false;
|
|
if (slot < kMaxSlots && 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), slot_count);
|
|
return E_NOTIMPL;
|
|
}
|
|
|
|
// Device slot 8: CreateCommandQueue(desc, riid, out). Without an output it is the device's own capability answer
|
|
// (S_FALSE for a valid description on the PC), as #558 does for slot 14.
|
|
HRESULT proxy_create_command_queue(DeviceProxy* self, const D3D12_COMMAND_QUEUE_DESC* desc, const IID* riid,
|
|
void** out) {
|
|
note_slot(8);
|
|
if (out) *out = nullptr; // _COM_Outptr_: NULL on every failure, as slots 14/62/76
|
|
if (!riid) return E_POINTER;
|
|
// The caller's null output goes to the real device on purpose: its answer (S_FALSE, E_UNEXPECTED, ...) is the
|
|
// one to give, although the SDK annotates the parameter as required.
|
|
#pragma warning(suppress : 6387)
|
|
if (!out) return self->real->CreateCommandQueue(desc, *riid, nullptr);
|
|
void* created = nullptr;
|
|
const HRESULT hr = self->real->CreateCommandQueue(desc, __uuidof(ID3D12CommandQueue), &created);
|
|
if (FAILED(hr)) return hr;
|
|
return wrap_child<ChildKind::queue>(static_cast<IUnknown*>(created), self, riid, out);
|
|
}
|
|
|
|
// Xbox device slot 78 (public HLE): SetFrameIntervalX(output, interval_us, latency, flags). The output and flags are
|
|
// not used before a presenter exists. The period is a vblank period in microseconds in the public samples (16667 for
|
|
// 60 Hz), and latency is the allowed frames in flight. S_OK even for 0: a boot must not abort here, and nothing paces.
|
|
HRESULT proxy_set_frame_interval_x(DeviceProxy* self, std::uint64_t output, std::uint64_t interval_us,
|
|
std::uint64_t latency, std::uint64_t flags) {
|
|
(void)output;
|
|
(void)flags;
|
|
self->frame_interval_us.store(static_cast<std::uint32_t>(interval_us), std::memory_order_relaxed);
|
|
self->frame_latency.store(static_cast<std::uint32_t>(latency ? latency : 1), std::memory_order_relaxed);
|
|
return S_OK;
|
|
}
|
|
|
|
// Xbox device slot 79 (public HLE): ScheduleFrameEventX(event, offset_us, pEvent, flags); our pacer has only the origin.
|
|
HRESULT proxy_schedule_frame_event_x(DeviceProxy* self, std::uint64_t event, std::uint64_t offset_us,
|
|
std::uint64_t flags) {
|
|
(void)event;
|
|
(void)offset_us;
|
|
(void)flags;
|
|
self->frame_event_scheduled.store(true, std::memory_order_relaxed);
|
|
return S_OK;
|
|
}
|
|
|
|
// Xbox device slot 80 (public HLE): WaitFrameEventX(event, timeout_ms, pEvent, flags, &token). It returns at once with
|
|
// a fresh non-zero token (the first-frame choice): there is no vblank source behind the device before a presenter, and
|
|
// a zero wait cannot stall the title. A null token pointer is E_POINTER, as a _Out_ parameter.
|
|
HRESULT proxy_wait_frame_event_x(DeviceProxy* self, std::uint64_t event, std::uint64_t timeout_ms,
|
|
std::uint64_t flags, std::uint64_t token_word) {
|
|
(void)event;
|
|
(void)timeout_ms;
|
|
(void)flags;
|
|
auto* token = reinterpret_cast<std::uint64_t*>(token_word);
|
|
if (!token) return E_POINTER;
|
|
std::uint64_t value = self->frame_next_token.fetch_add(1, std::memory_order_relaxed);
|
|
if (value == 0) value = self->frame_next_token.fetch_add(1, std::memory_order_relaxed); // skip the null token
|
|
*token = value;
|
|
return S_OK;
|
|
}
|
|
|
|
// Xbox device slot 73 (public HLE): CreateCommandListX(desc, allocator, initial_state, riid, out). The console call is
|
|
// not the PC six-argument form: the type is the first word of D3D12XBOX_COMMAND_LIST_DESC and there is no node mask
|
|
// (the HLE passes 0). The second desc word is logged, not interpreted: its public layout is not established.
|
|
HRESULT proxy_create_command_list_x(DeviceProxy* self, std::uint64_t desc_word, std::uint64_t allocator_word,
|
|
std::uint64_t initial_state_word, const IID* riid, void** out) {
|
|
if (out) *out = nullptr;
|
|
if (!desc_word || !allocator_word) return E_INVALIDARG;
|
|
if (!riid) return E_POINTER;
|
|
if (!out) return E_POINTER;
|
|
|
|
std::uint32_t words[2] = {};
|
|
std::memcpy(words, reinterpret_cast<const void*>(desc_word), sizeof(words));
|
|
auto type = static_cast<D3D12_COMMAND_LIST_TYPE>(words[0]);
|
|
if (type != D3D12_COMMAND_LIST_TYPE_DIRECT && type != D3D12_COMMAND_LIST_TYPE_BUNDLE &&
|
|
type != D3D12_COMMAND_LIST_TYPE_COMPUTE && type != D3D12_COMMAND_LIST_TYPE_COPY)
|
|
type = D3D12_COMMAND_LIST_TYPE_DIRECT; // as the public HLE does
|
|
|
|
static std::atomic<bool> logged{false};
|
|
if (first_call(logged))
|
|
std::fprintf(stderr,
|
|
"[rt] d3d12_xs: slot 73 CreateCommandListX desc words 0x%08x 0x%08x -> node mask 0, type %d\n",
|
|
words[0], words[1], static_cast<int>(type));
|
|
|
|
// Leonida wraps its Vulkan allocators; a PC allocator comes back unwrapped and is passed as it is. Pipeline states
|
|
// are not wrapped by this proxy, so initial_state is already the real object (a proxy there is passed through too).
|
|
auto* allocator = allocator_proxy_real_allocator(reinterpret_cast<void*>(allocator_word));
|
|
if (!allocator) allocator = reinterpret_cast<ID3D12CommandAllocator*>(allocator_word);
|
|
auto* initial_state = reinterpret_cast<ID3D12PipelineState*>(initial_state_word);
|
|
void* created = nullptr;
|
|
const HRESULT hr = self->real->CreateCommandList(0, type, allocator, initial_state,
|
|
*riid, &created);
|
|
if (FAILED(hr)) return hr;
|
|
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) {
|
|
note_slot(75);
|
|
if (out) *out = nullptr; // as slot 8
|
|
if (self->slot_count <= 75) return refuse_beyond_device(75, self->slot_count); // predates ID3D12Device9
|
|
if (!riid) return E_POINTER;
|
|
using Fn = HRESULT(WINAPI*)(ID3D12Device*, std::uint64_t, std::uint64_t, const IID*, void**);
|
|
const auto create = reinterpret_cast<Fn>(self->real_vtable[75]);
|
|
if (!out) return create(self->real, desc, creator_id, riid, nullptr);
|
|
void* created = nullptr;
|
|
const HRESULT hr = create(self->real, desc, creator_id, &__uuidof(ID3D12CommandQueue), &created);
|
|
if (FAILED(hr)) return hr;
|
|
return wrap_child<ChildKind::queue>(static_cast<IUnknown*>(created), self, riid, out);
|
|
}
|
|
|
|
// Device slot 36: CreateFence(initial_value, flags, riid, out).
|
|
HRESULT proxy_create_fence(DeviceProxy* self, std::uint64_t initial_value, std::uint64_t flags, const IID* riid,
|
|
void** out) {
|
|
note_slot(36);
|
|
if (out) *out = nullptr; // as slot 8
|
|
if (!riid) return E_POINTER;
|
|
const auto fence_flags = static_cast<D3D12_FENCE_FLAGS>(static_cast<std::uint32_t>(flags));
|
|
#pragma warning(suppress : 6387) // the caller's null output, on purpose (see slot 8)
|
|
if (!out) return self->real->CreateFence(initial_value, fence_flags, *riid, nullptr);
|
|
void* created = nullptr;
|
|
const HRESULT hr = self->real->CreateFence(initial_value, fence_flags, __uuidof(ID3D12Fence), &created);
|
|
if (FAILED(hr)) return hr;
|
|
return wrap_child<ChildKind::fence>(static_cast<IUnknown*>(created), self, riid, out);
|
|
}
|
|
|
|
// True when `allocator` is one whose consumers resolve allocator proxies: Leonida's own Vulkan-layer allocator,
|
|
// whose lists (CreateCommandList, Reset) find the real allocator through allocator_proxy_real_allocator. A PC
|
|
// runtime allocator does not answer the tag, and its lists would take a proxy for their own allocator.
|
|
bool resolves_allocator_proxies(IUnknown* allocator) {
|
|
void* found = nullptr;
|
|
if (FAILED(allocator->QueryInterface(kLeonidaCommandAllocatorTag, &found)) || !found) return false;
|
|
static_cast<IUnknown*>(found)->Release(); // the implementation pointer starts with its IUnknown chain
|
|
return true;
|
|
}
|
|
|
|
// Device slot 9: CreateCommandAllocator(type, riid, out). An allocator proxy where the list side resolves it (see
|
|
// resolves_allocator_proxies); any other allocator goes back unchanged, logged once, so the host runtime's lists
|
|
// never see a proxy. Null output and null IID as slot 8.
|
|
HRESULT proxy_create_command_allocator(DeviceProxy* self, std::uint64_t type, const IID* riid, void** out) {
|
|
note_slot(9);
|
|
if (out) *out = nullptr; // as slot 8
|
|
if (!riid) return E_POINTER;
|
|
const auto list_type = static_cast<D3D12_COMMAND_LIST_TYPE>(static_cast<std::uint32_t>(type));
|
|
#pragma warning(suppress : 6387) // the caller's null output, on purpose (see slot 8)
|
|
if (!out) return self->real->CreateCommandAllocator(list_type, *riid, nullptr);
|
|
void* created = nullptr;
|
|
const HRESULT hr = self->real->CreateCommandAllocator(list_type, __uuidof(ID3D12CommandAllocator), &created);
|
|
if (FAILED(hr)) return hr;
|
|
if (!created) return E_FAIL;
|
|
auto* allocator = static_cast<IUnknown*>(created);
|
|
if (!resolves_allocator_proxies(allocator)) {
|
|
static std::atomic<bool> logged{false};
|
|
if (!logged.exchange(true, std::memory_order_relaxed))
|
|
std::fprintf(stderr, "[rt] d3d12_xs: allocators of this device are not wrapped: its command lists "
|
|
"cannot resolve an allocator proxy\n");
|
|
const HRESULT asked = allocator->QueryInterface(*riid, out);
|
|
allocator->Release();
|
|
return asked;
|
|
}
|
|
return wrap_child<ChildKind::allocator>(allocator, self, riid, out);
|
|
}
|
|
|
|
// Device slot 32: OpenSharedHandle(handle, riid, out). The object is opened as asked; when it is a fence (whatever
|
|
// interface was asked for, IUnknown included) the title gets a fence proxy, anything else (heaps, resources) is
|
|
// returned unchanged.
|
|
HRESULT proxy_open_shared_handle(DeviceProxy* self, std::uint64_t handle, const IID* riid, void** out) {
|
|
note_slot(32);
|
|
if (out) *out = nullptr; // as slot 8
|
|
if (!riid) return E_POINTER;
|
|
const auto shared = reinterpret_cast<HANDLE>(handle);
|
|
if (!out) return self->real->OpenSharedHandle(shared, *riid, nullptr);
|
|
void* opened = nullptr;
|
|
HRESULT hr = self->real->OpenSharedHandle(shared, *riid, &opened);
|
|
if (FAILED(hr) || !opened) return FAILED(hr) ? hr : E_FAIL;
|
|
void* fence = nullptr;
|
|
if (FAILED(static_cast<IUnknown*>(opened)->QueryInterface(__uuidof(ID3D12Fence), &fence)) || !fence) {
|
|
*out = opened; // not a fence
|
|
return hr;
|
|
}
|
|
static_cast<IUnknown*>(opened)->Release();
|
|
return wrap_child<ChildKind::fence>(static_cast<IUnknown*>(fence), self, riid, out);
|
|
}
|
|
|
|
// Device slots that take heap, queue or fence objects: the real runtime gets the real objects.
|
|
// 31 CreateSharedHandle(object, attributes, access, name, handle)
|
|
// 34 MakeResident(count, objects) 35 Evict(count, objects)
|
|
// 45 SetEventOnMultipleFenceCompletion(fences, values, count, flags, event) (ID3D12Device1)
|
|
// 46 SetResidencyPriority(count, objects, priorities) (ID3D12Device1)
|
|
// 50 EnqueueMakeResident(flags, count, objects, fence, value) (ID3D12Device3)
|
|
template <unsigned Slot>
|
|
HRESULT proxy_unwrapping_call(DeviceProxy* self, std::uint64_t a1, std::uint64_t a2, std::uint64_t a3,
|
|
std::uint64_t a4, std::uint64_t a5) {
|
|
note_slot(Slot);
|
|
if (Slot >= self->slot_count) return refuse_beyond_device(Slot, self->slot_count); // predates the interface
|
|
UnwrappedArray objects;
|
|
std::uint64_t r1 = a1, r2 = a2, r3 = a3, r4 = a4;
|
|
if constexpr (Slot == 31) {
|
|
r1 = unwrap_object(a1);
|
|
} else if constexpr (Slot == 34 || Slot == 35 || Slot == 46) {
|
|
if (!objects.fill(a2, a1)) return E_OUTOFMEMORY;
|
|
if (objects.data()) r2 = objects.data();
|
|
} else if constexpr (Slot == 45) {
|
|
if (!objects.fill(a1, a3)) return E_OUTOFMEMORY;
|
|
if (objects.data()) r1 = objects.data();
|
|
} else if constexpr (Slot == 50) {
|
|
if (!objects.fill(a3, a2)) return E_OUTOFMEMORY;
|
|
if (objects.data()) r3 = objects.data();
|
|
r4 = unwrap_object(a4);
|
|
}
|
|
// r1-r4 differ from a1-a4 only in a position the method takes, so the title's frame has that word.
|
|
return static_cast<HRESULT>(static_cast<std::uint32_t>(forward_device<Slot>(self, r1, r2, r3, r4, a5)));
|
|
}
|
|
|
|
// A console descriptor heap carries a size as well as an address, and that size is 32 bytes per descriptor. A PC
|
|
// desc's trailing words are whatever the caller holds next; they do not encode NumDescriptors * 32, so this
|
|
// separates the two layouts without guessing anything about the address itself.
|
|
static bool console_heap_range_plausible(const D3D12_DESCRIPTOR_HEAP_DESC* desc, std::uint64_t address,
|
|
std::uint64_t size) {
|
|
constexpr std::uint64_t kConsoleDescriptorSize = 32;
|
|
if (address == 0 || size == 0) return false;
|
|
if (desc->NumDescriptors == 0 || desc->NumDescriptors > (0xFFFFFFFFull / kConsoleDescriptorSize)) return false;
|
|
if (size != static_cast<std::uint64_t>(desc->NumDescriptors) * kConsoleDescriptorSize) return false;
|
|
return true;
|
|
}
|
|
|
|
static bool readable_range(const void* ptr, std::size_t bytes) {
|
|
if (!ptr || !bytes) return false;
|
|
const auto* p = static_cast<const std::uint8_t*>(ptr);
|
|
std::size_t remaining = bytes;
|
|
while (remaining) {
|
|
MEMORY_BASIC_INFORMATION mbi{};
|
|
if (!VirtualQuery(p, &mbi, sizeof(mbi)) || mbi.State != MEM_COMMIT) return false;
|
|
const DWORD protect = mbi.Protect & 0xffu;
|
|
if (protect == PAGE_NOACCESS || protect == PAGE_GUARD || protect == PAGE_EXECUTE) return false;
|
|
const auto base = reinterpret_cast<std::uintptr_t>(mbi.BaseAddress);
|
|
const auto addr = reinterpret_cast<std::uintptr_t>(p);
|
|
const std::size_t avail = static_cast<std::size_t>(base + mbi.RegionSize - addr);
|
|
if (avail >= remaining) return true;
|
|
remaining -= avail;
|
|
p += avail;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
HRESULT proxy_create_descriptor_heap(DeviceProxy* self, const D3D12_DESCRIPTOR_HEAP_DESC* desc, const IID* riid,
|
|
void** out) {
|
|
note_slot(14);
|
|
if (out) *out = nullptr; // _COM_Outptr_: NULL on every failure, as the PC device does
|
|
if (!riid) return E_POINTER;
|
|
// No output: a capability check, answered by the device (S_FALSE, as on the PC).
|
|
if (!out) return self->real->CreateDescriptorHeap(desc, *riid, nullptr);
|
|
|
|
void* created = nullptr;
|
|
HRESULT hr = E_INVALIDARG;
|
|
bool console_extended = false;
|
|
if (desc && desc->Type <= D3D12_DESCRIPTOR_HEAP_TYPE_DSV &&
|
|
(desc->Flags & ~D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE) == 0) {
|
|
const auto* bytes = reinterpret_cast<const std::uint8_t*>(desc);
|
|
const void* tail = bytes + sizeof(D3D12_DESCRIPTOR_HEAP_DESC);
|
|
if (readable_range(tail, sizeof(std::uint64_t) * 2)) {
|
|
std::uint64_t address = 0;
|
|
std::uint64_t size = 0;
|
|
std::memcpy(&address, tail, sizeof(address));
|
|
std::memcpy(&size, bytes + sizeof(D3D12_DESCRIPTOR_HEAP_DESC) + sizeof(address), sizeof(size));
|
|
// Console-extended slot 14 (issue #313): address/size follow the PC desc. A non-zero word is not evidence
|
|
// of that layout - the bytes after a PC desc are whatever the caller holds next, and they are almost
|
|
// always readable and non-zero. The size has to agree with the descriptor count for the path to be taken.
|
|
if (console_heap_range_plausible(desc, address, size)) {
|
|
const ConsoleDescriptorHeapHandler handler =
|
|
g_console_descriptor_heap.load(std::memory_order_acquire);
|
|
if (handler) {
|
|
console_extended = true;
|
|
hr = handler(self->real, desc, static_cast<D3D12_GPU_VIRTUAL_ADDRESS>(address), size,
|
|
__uuidof(ID3D12DescriptorHeap), &created);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (!console_extended)
|
|
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 (!out) return E_POINTER;
|
|
*out = nullptr;
|
|
if (!riid) return E_POINTER;
|
|
return self->real->CreateComputePipelineState(desc, *riid, out);
|
|
}
|
|
|
|
// The Xbox slot 67 creates a compute pipeline from native GPU code: (desc, riid, out), named
|
|
// DeserializeComputePipelineStateX in amd_fidelityfx_upscaler_xbox.dll's messages (call at RVA 0x56d9). The desc
|
|
// passes the root signature at +0x00, followed by pointer/size pairs for the shader's FSPX entries (docs/fspx.md):
|
|
// kind 0 (dispatch state) at +0x08/+0x10, kind 1 at +0x18/+0x20, kind 6 (the RDNA2 program) at +0x28/+0x30, and
|
|
// kind 7 (records) at +0x38/+0x40. Translates the console description to a standard D3D12_COMPUTE_PIPELINE_STATE_DESC
|
|
// with CS bytecode pointing at kind 6 and routes it to CreateComputePipelineState on the PC device (slot 11), which
|
|
// on the Vulkan device resolves through the translator. The PC slot 67 is CreateProtectedResourceSession1.
|
|
HRESULT proxy_create_compute_pipeline_from_native_code(DeviceProxy* self, const void* desc,
|
|
const IID* riid, void** out) {
|
|
note_slot(67);
|
|
if (!out) return E_POINTER;
|
|
*out = nullptr;
|
|
if (!desc) return E_INVALIDARG;
|
|
if (!riid) return E_POINTER;
|
|
|
|
// FSPX description layout (docs/fspx.md §How the DLL reads it):
|
|
struct XboxNativeComputePipelineStateDesc {
|
|
ID3D12RootSignature* root_signature; // +0x00
|
|
const void* kind0; // +0x08: dispatch state
|
|
std::uint64_t kind0_size; // +0x10: 28 bytes
|
|
const void* kind1; // +0x18: unknown
|
|
std::uint64_t kind1_size; // +0x20: 20 bytes
|
|
const void* kind6; // +0x28: RDNA2 program code
|
|
std::uint64_t kind6_size; // +0x30: program size in bytes
|
|
const void* kind7; // +0x38: resource/hash records
|
|
std::uint64_t kind7_size; // +0x40: records size in bytes
|
|
};
|
|
const auto* xbox_desc = static_cast<const XboxNativeComputePipelineStateDesc*>(desc);
|
|
|
|
D3D12_COMPUTE_PIPELINE_STATE_DESC pc_desc{};
|
|
pc_desc.pRootSignature = xbox_desc->root_signature;
|
|
pc_desc.CS.pShaderBytecode = xbox_desc->kind6;
|
|
pc_desc.CS.BytecodeLength = static_cast<SIZE_T>(xbox_desc->kind6_size);
|
|
// With kind 0 (the DLL always passes it: its reader refuses a payload without one), the four pairs go on as one
|
|
// FSPX payload, so the device matches the program by its kind-6 and kind-0 keys (docs/fspx.md, "Build and run-time
|
|
// lookup"): two programs with the same code and different dispatch state stay apart. Every pair is checked before
|
|
// it is read (leonida::fspx_payload_from_parts); an inconsistent one refuses the call. Without kind 0 the kind-6
|
|
// bytes go on alone, as before.
|
|
std::vector<std::uint8_t> payload;
|
|
if (xbox_desc->kind0 || xbox_desc->kind0_size) {
|
|
const leonida::FspxPart parts[] = {{0, xbox_desc->kind0, xbox_desc->kind0_size},
|
|
{1, xbox_desc->kind1, xbox_desc->kind1_size},
|
|
{6, xbox_desc->kind6, xbox_desc->kind6_size},
|
|
{7, xbox_desc->kind7, xbox_desc->kind7_size}};
|
|
payload = leonida::fspx_payload_from_parts(parts);
|
|
if (payload.empty()) {
|
|
static std::atomic<bool> logged{false};
|
|
if (!logged.exchange(true, std::memory_order_relaxed))
|
|
std::fprintf(stderr,
|
|
"[rt] d3d12_xs: slot 67 refused: a pointer/size pair is inconsistent or too large "
|
|
"(kind0 %llu B, kind1 %llu B, kind6 %llu B, kind7 %llu B)\n",
|
|
static_cast<unsigned long long>(xbox_desc->kind0_size),
|
|
static_cast<unsigned long long>(xbox_desc->kind1_size),
|
|
static_cast<unsigned long long>(xbox_desc->kind6_size),
|
|
static_cast<unsigned long long>(xbox_desc->kind7_size));
|
|
return E_INVALIDARG;
|
|
}
|
|
pc_desc.CS.pShaderBytecode = payload.data();
|
|
pc_desc.CS.BytecodeLength = payload.size();
|
|
}
|
|
|
|
pc_desc.NodeMask = 0;
|
|
pc_desc.CachedPSO = {nullptr, 0};
|
|
pc_desc.Flags = D3D12_PIPELINE_STATE_FLAG_NONE;
|
|
|
|
// Every failed call is logged with its number: the DLL creates its pipelines one after the other, and only the
|
|
// first call of a slot is announced, so a miss on a later program would otherwise read as the first call's.
|
|
static std::atomic<std::uint64_t> calls{0};
|
|
const std::uint64_t call = calls.fetch_add(1, std::memory_order_relaxed) + 1;
|
|
const HRESULT hr = self->real->CreateComputePipelineState(&pc_desc, *riid, out);
|
|
if (FAILED(hr))
|
|
std::fprintf(stderr,
|
|
"[rt] d3d12_xs: slot 67 call %llu failed with 0x%08lx (kind 0 %llu B, kind 1 %llu B, kind 6 %llu B, "
|
|
"kind 7 %llu B)\n",
|
|
static_cast<unsigned long long>(call), static_cast<unsigned long>(hr),
|
|
static_cast<unsigned long long>(xbox_desc->kind0_size),
|
|
static_cast<unsigned long long>(xbox_desc->kind1_size),
|
|
static_cast<unsigned long long>(xbox_desc->kind6_size),
|
|
static_cast<unsigned long long>(xbox_desc->kind7_size));
|
|
return hr;
|
|
}
|
|
|
|
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 (!out) return E_POINTER;
|
|
*out = nullptr;
|
|
if (!xbox_desc) return E_POINTER;
|
|
if (!riid) 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);
|
|
}
|
|
|
|
// Device slot 133 is the console's CreatePlacedResourceX: (va, desc, state, clear, riid, out), measured on the retail
|
|
// executable (docs/retail_boot_chain.md, section 3.4). `va` is the start of a physical-page range the title has just
|
|
// mapped, `desc` a 56-byte description with Xbox bits above the PC resource flags and an Xbox layout word, and
|
|
// `state` is 0xFFFFFFFF, which is no PC state. The resource is a buffer on those pages, made by the handler the
|
|
// device behind the proxy registered (set_placed_at_va_handler), with the PC flags kept and the layout made
|
|
// ROW_MAJOR, as slot 76 does.
|
|
std::atomic<PlacedAtVaHandler> g_placed_at_va{nullptr};
|
|
std::atomic<PlacedAtVaPagesHook> g_placed_at_va_pages{nullptr};
|
|
|
|
HRESULT proxy_create_placed_resource_x(DeviceProxy* self, void* va, const D3D12_RESOURCE_DESC* xbox_desc,
|
|
std::uint32_t state, const IID* riid, void** out) {
|
|
note_slot(133);
|
|
if (out) *out = nullptr;
|
|
// Without a handler (a PC device behind the proxy, or none registered) the slot refuses like any slot past the
|
|
// PC vtable, whatever its arguments.
|
|
const PlacedAtVaHandler handler = g_placed_at_va.load(std::memory_order_acquire);
|
|
if (!handler) return E_NOTIMPL;
|
|
if (!out || !xbox_desc || !riid) return E_POINTER;
|
|
// An integer mask: a D3D12_RESOURCE_FLAGS constant past the SDK's last enumerator is not a constant expression
|
|
// for clang-cl with SDK 10.0.26100 (found in review). The cast is applied to the masked value at run time.
|
|
// The PC flags up to RAYTRACING_ACCELERATION_STRUCTURE (0x100) and USE_TIGHT_ALIGNMENT (0x400); 0x200 is
|
|
// dropped, as committed_resource_refusal says the host refuses it (found in review).
|
|
constexpr std::uint32_t kPcResourceFlags = 0x5ff;
|
|
D3D12_RESOURCE_DESC pc_desc = *xbox_desc;
|
|
const auto xbox_flags = static_cast<std::uint32_t>(pc_desc.Flags);
|
|
pc_desc.Flags = static_cast<D3D12_RESOURCE_FLAGS>(xbox_flags & kPcResourceFlags);
|
|
// The Xbox layout word (0x101 tiled, 0x1 row-major buffer) is no PC layout: ROW_MAJOR for a buffer and UNKNOWN
|
|
// for everything else, as slot 76 does.
|
|
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 133 CreatePlacedResourceX: va=%p dimension=%d width=%llu xbox flags=0x%x "
|
|
"state=0x%x -> a buffer on the title's pages\n",
|
|
va, static_cast<int>(pc_desc.Dimension), static_cast<unsigned long long>(pc_desc.Width),
|
|
xbox_flags, state);
|
|
}
|
|
// The buffer's pages are committed before the handler walks them (set_placed_at_va_pages_hook).
|
|
const PlacedAtVaPagesHook pages = g_placed_at_va_pages.load(std::memory_order_acquire);
|
|
if (pages && pc_desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER && pc_desc.Width &&
|
|
!pages(va, static_cast<std::size_t>(pc_desc.Width))) {
|
|
static std::atomic<bool> refused{false};
|
|
if (!refused.exchange(true, std::memory_order_relaxed)) {
|
|
std::fprintf(stderr, "[rt] d3d12_xs: slot 133: committing the pages of va=%p width=%llu failed (error %lu)\n",
|
|
va, static_cast<unsigned long long>(pc_desc.Width), GetLastError());
|
|
}
|
|
}
|
|
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;
|
|
}
|
|
|
|
// Xbox device slot 127 (RT-5 stop 15, measured by Pipodi in #6982): the argument list of ID3D12Device10::
|
|
// CreateCommittedResource3, (heap properties, heap flags, desc1, initial layout, clear value, protected session,
|
|
// castable format count, castable formats, riid, out), with the Xbox layout word in desc1.Layout and Xbox bits above
|
|
// the PC flags in desc1.Flags. The slot is past the PC device's vtable, so the generic thunk could only refuse it. The
|
|
// PC device creates the resource through its own CreateCommittedResource3 (PC slot 76) with the layout word replaced
|
|
// as slot 76 does and the flags limited to the PC bits. The initial layout, clear value, session and castable list go
|
|
// on as the title passed them; 0xFFFFFFFF is D3D12_BARRIER_LAYOUT_UNDEFINED, the value the console passes for the
|
|
// state of slot 133 as well. The words take the positions the thunk received them in (see the top of the file).
|
|
constexpr unsigned kCreateCommittedResource3X = 127;
|
|
constexpr unsigned kPcCreateCommittedResource3 = 76; // ID3D12Device10, the PC device's 77th method
|
|
constexpr std::uint32_t kPcResourceFlagsMask =
|
|
D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL |
|
|
D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS | D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE |
|
|
D3D12_RESOURCE_FLAG_ALLOW_CROSS_ADAPTER | D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS |
|
|
D3D12_RESOURCE_FLAG_VIDEO_DECODE_REFERENCE_ONLY | D3D12_RESOURCE_FLAG_VIDEO_ENCODE_REFERENCE_ONLY |
|
|
D3D12_RESOURCE_FLAG_RAYTRACING_ACCELERATION_STRUCTURE;
|
|
|
|
HRESULT proxy_create_committed_resource3_x(DeviceProxy* self, std::uint64_t heap_word, std::uint64_t heap_flags,
|
|
std::uint64_t desc_word, std::uint64_t layout_word,
|
|
std::uint64_t clear_word, std::uint64_t session_word,
|
|
std::uint64_t castable_count, std::uint64_t castable_formats,
|
|
std::uint64_t riid_word, std::uint64_t out_word) {
|
|
note_slot(kCreateCommittedResource3X);
|
|
auto* const out = reinterpret_cast<void**>(out_word);
|
|
if (out) *out = nullptr; // _COM_Outptr_: NULL on every failure, as slots 14/62/76
|
|
const auto* const xbox_desc = reinterpret_cast<const D3D12_RESOURCE_DESC1*>(desc_word);
|
|
const auto* const riid = reinterpret_cast<const IID*>(riid_word);
|
|
if (!out || !xbox_desc || !riid) return E_POINTER;
|
|
if (self->slot_count <= kPcCreateCommittedResource3) { // the PC device predates ID3D12Device10
|
|
static std::atomic<bool> warned{false};
|
|
if (first_call(warned))
|
|
std::fprintf(stderr,
|
|
"[rt] d3d12_xs: device slot %u (%s) needs ID3D12Device10 and the PC device has %u slots; "
|
|
"returning E_NOTIMPL\n",
|
|
kCreateCommittedResource3X, slot_name(kCreateCommittedResource3X), self->slot_count);
|
|
return E_NOTIMPL;
|
|
}
|
|
|
|
D3D12_RESOURCE_DESC1 pc_desc = *xbox_desc;
|
|
const auto xbox_layout = static_cast<std::uint32_t>(pc_desc.Layout);
|
|
const auto xbox_flags = static_cast<std::uint32_t>(pc_desc.Flags);
|
|
pc_desc.Layout = pc_desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER ? D3D12_TEXTURE_LAYOUT_ROW_MAJOR
|
|
: D3D12_TEXTURE_LAYOUT_UNKNOWN;
|
|
pc_desc.Flags = static_cast<D3D12_RESOURCE_FLAGS>(xbox_flags & kPcResourceFlagsMask);
|
|
|
|
static std::atomic<bool> logged{false};
|
|
if (first_call(logged))
|
|
std::fprintf(stderr,
|
|
"[rt] d3d12_xs: slot 127 resource: dimension=%d xbox layout=0x%x -> PC layout=0x%x, xbox flags "
|
|
"0x%x -> PC flags 0x%x\n",
|
|
static_cast<int>(pc_desc.Dimension), xbox_layout, static_cast<unsigned>(pc_desc.Layout),
|
|
xbox_flags, static_cast<unsigned>(pc_desc.Flags));
|
|
|
|
using Fn = HRESULT(WINAPI*)(ID3D12Device*, const D3D12_HEAP_PROPERTIES*, D3D12_HEAP_FLAGS,
|
|
const D3D12_RESOURCE_DESC1*, std::uint32_t, const D3D12_CLEAR_VALUE*,
|
|
ID3D12ProtectedResourceSession*, std::uint32_t, const DXGI_FORMAT*, const IID*,
|
|
void**);
|
|
const auto create = reinterpret_cast<Fn>(self->real_vtable[kPcCreateCommittedResource3]);
|
|
return create(self->real, reinterpret_cast<const D3D12_HEAP_PROPERTIES*>(heap_word),
|
|
static_cast<D3D12_HEAP_FLAGS>(static_cast<std::uint32_t>(heap_flags)), &pc_desc,
|
|
static_cast<std::uint32_t>(layout_word), reinterpret_cast<const D3D12_CLEAR_VALUE*>(clear_word),
|
|
reinterpret_cast<ID3D12ProtectedResourceSession*>(session_word),
|
|
static_cast<std::uint32_t>(castable_count), reinterpret_cast<const DXGI_FORMAT*>(castable_formats),
|
|
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 == 8) {
|
|
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)));
|
|
} else if constexpr (Slot == 9) {
|
|
return hr_to_u64(proxy_create_command_allocator(self, a1, reinterpret_cast<const IID*>(a2),
|
|
reinterpret_cast<void**>(a3)));
|
|
} else if constexpr (Slot == 12) {
|
|
note_slot(12);
|
|
a3 = unwrap_object(a3);
|
|
if (Slot >= self->slot_count) return hr_to_u64(refuse_beyond_device(12, self->slot_count));
|
|
using CreateListFn = HRESULT(WINAPI*)(ID3D12Device*, UINT, D3D12_COMMAND_LIST_TYPE, ID3D12CommandAllocator*,
|
|
ID3D12PipelineState*, const IID*, void**);
|
|
auto fn = reinterpret_cast<CreateListFn>(self->real_vtable[12]);
|
|
return hr_to_u64(fn(self->real, static_cast<UINT>(a1), static_cast<D3D12_COMMAND_LIST_TYPE>(a2),
|
|
reinterpret_cast<ID3D12CommandAllocator*>(a3),
|
|
reinterpret_cast<ID3D12PipelineState*>(a4),
|
|
reinterpret_cast<const IID*>(a5),
|
|
reinterpret_cast<void**>(a6)));
|
|
} else if constexpr (Slot == 36) {
|
|
return hr_to_u64(proxy_create_fence(self, a1, a2, reinterpret_cast<const IID*>(a3),
|
|
reinterpret_cast<void**>(a4)));
|
|
} else if constexpr (Slot == 32) {
|
|
return hr_to_u64(proxy_open_shared_handle(self, a1, reinterpret_cast<const IID*>(a2),
|
|
reinterpret_cast<void**>(a3)));
|
|
} else if constexpr (Slot == 31 || Slot == 34 || Slot == 35 || Slot == 45 || Slot == 46 || Slot == 50) {
|
|
return hr_to_u64(proxy_unwrapping_call<Slot>(self, a1, a2, a3, a4, a5));
|
|
} else if constexpr (Slot == 75) {
|
|
return hr_to_u64(proxy_create_command_queue1(self, a1, a2, reinterpret_cast<const IID*>(a3),
|
|
reinterpret_cast<void**>(a4)));
|
|
} 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 == 67) {
|
|
return hr_to_u64(proxy_create_compute_pipeline_from_native_code(
|
|
self, reinterpret_cast<const void*>(a1), reinterpret_cast<const IID*>(a2),
|
|
reinterpret_cast<void**>(a3)));
|
|
} else if constexpr (Slot == 133) {
|
|
return hr_to_u64(proxy_create_placed_resource_x(self, reinterpret_cast<void*>(a1),
|
|
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),
|
|
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 if constexpr (Slot == kCreateCommittedResource3X) {
|
|
return hr_to_u64(proxy_create_committed_resource3_x(self, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10));
|
|
} 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
|
|
// methods. The check is before the slot-count refusal because slot 80 is outside a PC ID3D12Device11 table.
|
|
const XboxDeviceSlotRoutes& routes = xbox_device_slot_routes();
|
|
if (Slot == routes.frame_interval) {
|
|
log_xbox_route_once(Slot, "SetFrameIntervalX", a1, a2, a3, a4, a5);
|
|
return hr_to_u64(proxy_set_frame_interval_x(self, a1, a2, a3, a4));
|
|
}
|
|
if (Slot == routes.schedule_event) {
|
|
log_xbox_route_once(Slot, "ScheduleFrameEventX", a1, a2, a3, a4, a5);
|
|
return hr_to_u64(proxy_schedule_frame_event_x(self, a1, a2, a4));
|
|
}
|
|
if (Slot == routes.wait_event) {
|
|
log_xbox_route_once(Slot, "WaitFrameEventX", a1, a2, a3, a4, a5);
|
|
return hr_to_u64(proxy_wait_frame_event_x(self, a1, a2, a4, a5));
|
|
}
|
|
if (Slot == routes.queue_x) {
|
|
log_xbox_route_once(Slot, "CreateCommandQueueX", a1, a2, a3, a4, a5);
|
|
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);
|
|
return hr_to_u64(proxy_create_command_list_x(self, a1, a2, a3, reinterpret_cast<const IID*>(a4),
|
|
reinterpret_cast<void**>(a5)));
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
|
|
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)>) ), ...);
|
|
}
|
|
|
|
// ---- 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;
|
|
}
|
|
|
|
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);
|
|
proxy->dxgi_vtable = get_dxgi_vtable();
|
|
proxy->adapter_provider = nullptr;
|
|
proxy->gpu_thread_priority.store(0, std::memory_order_relaxed);
|
|
proxy->max_frame_latency.store(3, 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;
|
|
}
|
|
if (!d3d12_trace::enabled())
|
|
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());
|
|
}
|
|
|
|
void* device_proxy_from_dxgi_device(void* dxgi_device) {
|
|
if (!dxgi_device || *static_cast<void***>(dxgi_device) != get_dxgi_vtable()) return nullptr;
|
|
return dxgi_owner(dxgi_device);
|
|
}
|
|
|
|
void* device_proxy_primary(void* device) {
|
|
if (is_device_proxy(device)) return device;
|
|
return device_proxy_from_dxgi_device(device);
|
|
}
|
|
|
|
bool set_device_adapter_provider(void* device, DxgiAdapterProvider provider) {
|
|
auto* proxy = static_cast<DeviceProxy*>(device_proxy_primary(device));
|
|
if (!proxy) {
|
|
// Not installing one silently would leave the host's adapter in its place.
|
|
std::fprintf(stderr, "[rt] d3d12_xs: set_device_adapter_provider: %p is not a device proxy; no provider set\n",
|
|
device);
|
|
return false;
|
|
}
|
|
proxy->adapter_provider = provider;
|
|
return true;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
void set_placed_at_va_handler(PlacedAtVaHandler handler) {
|
|
g_placed_at_va.store(handler, std::memory_order_release);
|
|
}
|
|
|
|
void set_placed_at_va_pages_hook(PlacedAtVaPagesHook hook) {
|
|
g_placed_at_va_pages.store(hook, std::memory_order_release);
|
|
}
|
|
|
|
void set_console_descriptor_heap_handler(ConsoleDescriptorHeapHandler handler) {
|
|
g_console_descriptor_heap.store(handler, std::memory_order_release);
|
|
}
|
|
|
|
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) {
|
|
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 == 70 || slot == 75 || slot == 76 || slot == 133 || slot == kCreateCommittedResource3X)
|
|
return true;
|
|
const XboxDeviceSlotRoutes& routes = xbox_device_slot_routes();
|
|
return slot == routes.frame_interval || slot == routes.schedule_event || slot == routes.wait_event ||
|
|
slot == routes.queue_x || slot == routes.list_x;
|
|
}
|
|
|
|
bool is_queue_proxy(const void* queue) { return is_child_proxy<ChildKind::queue>(queue); }
|
|
bool is_fence_proxy(const void* fence) { return is_child_proxy<ChildKind::fence>(fence); }
|
|
bool is_allocator_proxy(const void* allocator) { return is_child_proxy<ChildKind::allocator>(allocator); }
|
|
|
|
ID3D12CommandAllocator* allocator_proxy_real_allocator(const void* allocator) {
|
|
if (!is_allocator_proxy(allocator)) return nullptr;
|
|
return reinterpret_cast<ID3D12CommandAllocator*>(static_cast<const ChildProxy*>(allocator)->real);
|
|
}
|
|
|
|
ID3D12CommandQueue* queue_proxy_real_queue(const void* queue) {
|
|
if (!is_queue_proxy(queue)) return nullptr;
|
|
return reinterpret_cast<ID3D12CommandQueue*>(static_cast<const ChildProxy*>(queue)->real);
|
|
}
|
|
|
|
ID3D12Fence* fence_proxy_real_fence(const void* fence) {
|
|
if (!is_fence_proxy(fence)) return nullptr;
|
|
return reinterpret_cast<ID3D12Fence*>(static_cast<const ChildProxy*>(fence)->real);
|
|
}
|
|
|
|
unsigned queue_proxy_slot_count(const void* queue) {
|
|
return is_queue_proxy(queue) ? static_cast<const ChildProxy*>(queue)->slot_count : 0;
|
|
}
|
|
|
|
unsigned fence_proxy_slot_count(const void* fence) {
|
|
return is_fence_proxy(fence) ? static_cast<const ChildProxy*>(fence)->slot_count : 0;
|
|
}
|
|
|
|
unsigned queue_proxy_table_size() { return kQueueSlots; }
|
|
unsigned fence_proxy_table_size() { return kFenceSlots; }
|
|
unsigned allocator_proxy_table_size() { return kAllocatorSlots; }
|
|
unsigned allocator_proxy_slot_count(const void* allocator) {
|
|
return is_allocator_proxy(allocator) ? static_cast<const ChildProxy*>(allocator)->slot_count : 0;
|
|
}
|
|
|
|
unsigned long command_object_proxy_ref_count(const void* object) {
|
|
if (!is_queue_proxy(object) && !is_fence_proxy(object) && !is_allocator_proxy(object)) return 0;
|
|
return static_cast<const ChildProxy*>(object)->refs.load(std::memory_order_relaxed);
|
|
}
|
|
|
|
std::uint64_t queue_slot_hits(unsigned slot) {
|
|
using S = ChildStats<ChildKind::queue>;
|
|
start_hit_counts();
|
|
return slot < S::kSlots ? S::hits[slot].load(std::memory_order_relaxed) : 0;
|
|
}
|
|
|
|
bool queue_slot_first_call_seen(unsigned slot) {
|
|
using S = ChildStats<ChildKind::queue>;
|
|
return slot < S::kSlots && S::first[slot].load(std::memory_order_relaxed);
|
|
}
|
|
|
|
std::uint64_t fence_slot_hits(unsigned slot) {
|
|
using S = ChildStats<ChildKind::fence>;
|
|
start_hit_counts();
|
|
return slot < S::kSlots ? S::hits[slot].load(std::memory_order_relaxed) : 0;
|
|
}
|
|
|
|
bool fence_slot_first_call_seen(unsigned slot) {
|
|
using S = ChildStats<ChildKind::fence>;
|
|
return slot < S::kSlots && S::first[slot].load(std::memory_order_relaxed);
|
|
}
|
|
|
|
std::uint64_t queue_table_overruns() {
|
|
return ChildStats<ChildKind::queue>::overruns.load(std::memory_order_relaxed);
|
|
}
|
|
|
|
std::uint64_t fence_table_overruns() {
|
|
return ChildStats<ChildKind::fence>::overruns.load(std::memory_order_relaxed);
|
|
}
|
|
|
|
bool command_object_tables_guarded() {
|
|
get_child_vtable<ChildKind::queue>();
|
|
get_child_vtable<ChildKind::fence>();
|
|
get_child_vtable<ChildKind::allocator>();
|
|
return ChildStats<ChildKind::queue>::table_end.load(std::memory_order_acquire) &&
|
|
ChildStats<ChildKind::fence>::table_end.load(std::memory_order_acquire) &&
|
|
ChildStats<ChildKind::allocator>::table_end.load(std::memory_order_acquire);
|
|
}
|
|
|
|
std::uint64_t allocator_slot_hits(unsigned slot) {
|
|
using S = ChildStats<ChildKind::allocator>;
|
|
start_hit_counts();
|
|
return slot < S::kSlots ? S::hits[slot].load(std::memory_order_relaxed) : 0;
|
|
}
|
|
|
|
bool allocator_slot_first_call_seen(unsigned slot) {
|
|
using S = ChildStats<ChildKind::allocator>;
|
|
return slot < S::kSlots && S::first[slot].load(std::memory_order_relaxed);
|
|
}
|
|
|
|
std::uint64_t allocator_table_overruns() {
|
|
return ChildStats<ChildKind::allocator>::overruns.load(std::memory_order_relaxed);
|
|
}
|
|
|
|
bool heap_slot_overridden(unsigned slot) {
|
|
return slot <= 2 || slot == 7 || slot == 9 || slot == 10;
|
|
}
|
|
|
|
std::uint64_t device_slot_hits(unsigned slot) {
|
|
start_hit_counts();
|
|
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) {
|
|
start_hit_counts();
|
|
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);
|
|
}
|
|
|
|
bool is_command_list_proxy(const void* list) {
|
|
return list && *reinterpret_cast<void* const*>(list) == reinterpret_cast<void*>(get_list_vtable());
|
|
}
|
|
|
|
ID3D12GraphicsCommandList* command_list_proxy_real_list(const void* list) {
|
|
if (!is_command_list_proxy(list)) return nullptr;
|
|
return static_cast<const ListProxy*>(list)->real;
|
|
}
|
|
|
|
unsigned command_list_proxy_table_size() { return kListSlots; }
|
|
|
|
std::uint64_t list_slot_hits(unsigned slot) {
|
|
start_hit_counts();
|
|
return slot < kListSlots ? ListStats::hits[slot].load(std::memory_order_relaxed) : 0;
|
|
}
|
|
|
|
bool list_slot_first_call_seen(unsigned slot) {
|
|
return slot < kListSlots && ListStats::first[slot].load(std::memory_order_relaxed);
|
|
}
|
|
|
|
void reset_device_proxy_stats() {
|
|
start_hit_counts();
|
|
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);
|
|
}
|
|
for (unsigned slot = 0; slot < kListSlots; ++slot) {
|
|
ListStats::hits[slot].store(0, std::memory_order_relaxed);
|
|
ListStats::first[slot].store(false, std::memory_order_relaxed);
|
|
}
|
|
reset_child_stats<ChildKind::queue>();
|
|
reset_child_stats<ChildKind::fence>();
|
|
reset_child_stats<ChildKind::allocator>();
|
|
}
|
|
|
|
} // namespace rt::d3d12_xs
|