diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 5f711aefa0..3ae29163e5 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2122,7 +2122,7 @@ The trace will enumerate them. | D3D12 / Xbox object | Vulkan / layer object | Notes | |---|---|---| -| `ID3D12Device` … `ID3D12Device11` | one `Device` object with the public chain plus measured Xbox overrides | The shipped BVH builder names Device11; slots 1/15/18/19/27/38 matched desktop in prior art, slot 62 is called as `CreateComputePipelineState`, slot 76 is Xbox texture creation, and heap slots 9/10 return in RAX. Unknown Xbox IIDs/slots are logged and refused. | +| `ID3D12Device` … `ID3D12Device11` | one `Device` object with the public chain plus measured Xbox overrides | The shipped BVH builder names Device11; slots 1/15/18/19/27/38 matched desktop in prior art, slot 48 is called as `(desc, riid, out)` with `IID_ID3D12CommandQueue` (PC slot 48 is `OpenExistingHeapFromAddress`; the 32-byte desc is a PC `D3D12_COMMAND_QUEUE_DESC` plus four Xbox words, logged once, and the proxy takes the slot 8 path with the PC prefix and only `DISABLE_GPU_TIMEOUT` kept), slot 62 is called as `CreateComputePipelineState`, slot 76 is Xbox texture creation, and heap slots 9/10 return in RAX. Unknown Xbox IIDs/slots are logged and refused. | | `ID3D12CommandQueue` | one leased `VkQueue` per live queue: direct queues on the graphics family, compute queues on an async-compute family and copy queues on a transfer-only (DMA) family when the GPU has them (`src/queue_families.h`), else graphics-family leases | Distinct Vulkan queues allow cross-queue waits and signals to make progress. With more than one family in use, buffers, images, the arena and staging are created `VK_SHARING_MODE_CONCURRENT` over those families, so no ownership transfers are needed. Barriers on copy and compute queues are narrowed to the stages their family has; render-target and depth clears are refused there (as in D3D12), and the D24/RGB96 copy conversions and bindless flushes stay off a transfer-only queue. Tested by `vk_queue_test`. `PresentX`, `SuspendX`, `ResumeX` are Xbox queue methods, not in the public vtable; public GDKX samples present only through them (§4.5). Call shapes known, slots **Open**. | | `ID3D12CommandAllocator` | IR arena + `VkCommandPool` | Reset only when the previous submission's fence is complete. | | `ID3D12GraphicsCommandList` … `8` | `CommandList` with IR | Replay at `ExecuteCommandLists`; bundles inlined. Supports render passes, mesh dispatch, enhanced barriers; records the GCL8 front/back stencil references (applied only once a pipeline path consumes them). `Draw` views are inlined and moved into the command stream (D3D-289), eliminating per-draw heap allocations. | @@ -2259,6 +2259,12 @@ The trace will enumerate them. unknown types and node masks above 1 get an all-zero answer, logged. The test compares `DEFAULT`, `UPLOAD` and `READBACK` byte for byte with the host runtime's answer when the host adapter has the layer's architecture (RTX 4090: identical); `d3d12_heap_properties` compares the `L1` heaps with the layer's own adapter. + The retail executable calls it (device slot 26) with the buffer for the returned struct in RCX and the device in + RDX, node mask in R8 and heap type in R9: three calls right after device creation, node mask 1 and types 1, 2 + and 3 (measured under `gta6rt --run` with a register probe in the slot's thunk). A PC C++ method has the device + in RCX and the buffer in RDX, so the Xbox-ABI proxy takes the console form when RDX holds the proxy + (`d3d12_xs_device.cpp`; `xs_proxy_mock_test`, `device_proxy_test`). Slots 25 and 43 return structs too; the + executable has not been seen calling them yet, so the proxy still forwards them in the PC form. - Residency and power (D3D-33) are no-ops that still check their arguments (`d3d12_vulkan_device.cpp`, tested in `d3d12_vulkan_device_test`). Vulkan has no residency API: memory from `vkAllocateMemory` stays usable until `vkFreeMemory`, and paging under pressure is the kernel driver's job, so every pageable object is always resident. diff --git a/docs/design.md b/docs/design.md index 6bb417340a..cec315722b 100644 --- a/docs/design.md +++ b/docs/design.md @@ -181,7 +181,9 @@ Full-scan indexes are also saved to `%LOCALAPPDATA%\RpfPreview\Indexes`, so the `CreateCommittedResource` with the Xbox `0x101`/`0x1` layout word replaced by the PC layout. Slots 9/10 are `ID3D12DescriptorHeap` methods, not device methods. The PC heap getters take a hidden return pointer (RDX), while Xbox expects the 8-byte handle in RAX, so slot 14 wraps the heap and slots 9/10 convert it. The heap holds - a reference to the device proxy and its `GetDevice` returns that same proxy. Queues, fences, command lists and + a reference to the device proxy and its `GetDevice` returns that same proxy. The retail executable calls the + struct-returning device methods with the C ABI, return buffer first: `GetCustomHeapProperties` (slot 26) is + measured as `rcx` = buffer, `rdx` = device. Slots 25 and 26 take that form, and still take the PC one. Queues, fences, command lists and `PresentX` extensions remain unimplemented. - PSO metadata is a new variant (`PSIN`, `LSCH` schema, `LENM`, `LSNS`, `LGID`, `LRSC`, `CHKS`, little-endian). CodeWalker's parser reads the container but not the new schema; dump it once the exe gives the field names. diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index 1045b86fb9..7f7040fdd9 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -5,8 +5,9 @@ // the same index through a 15-argument integer prototype. Every PC D3D12 device method takes integer/pointer // arguments only, so the x64 registers and stack words line up exactly; methods that return a struct keep their // hidden return pointer in RDX, which is passed through as the first argument unchanged. The measured exceptions -// are IUnknown (0-2), slot 14 (which wraps the heap), 62 and 76, plus the heap proxy's 9/10 conversion from the -// PC hidden-pointer form to the Xbox RAX form. Slots 8/75 (command queues), 36 (fences) and 32 (a shared fence) wrap +// are IUnknown (0-2), slot 14 (which wraps the heap), 25 and 26 (the title passes the return buffer in RCX and the +// device in RDX: see "Struct-returning device methods" below), 48 (a queue creation, where the PC has OpenExistingHeapFromAddress), 62 and 76, plus the heap proxy's 9/10 +// conversion from the PC hidden-pointer form to the Xbox RAX form. Slots 8/75 (command queues), 36 (fences) and 32 (a shared fence) wrap // what they create in queue and fence proxies built the same way, and slots 31/34/35/45/46/50 hand the real objects // behind heap, queue and fence proxies to the real device (see the command queue and fence section). // @@ -133,7 +134,8 @@ const char* slot_name(unsigned slot) { case 11: return "CreateComputePipelineState (PC slot 11)"; case 14: return "CreateDescriptorHeap"; case 16: return "CreateRootSignature"; - case 25: return "GetResourceAllocationInfo"; + case 25: return "GetResourceAllocationInfo (C or PC form)"; + case 26: return "GetCustomHeapProperties"; case 27: return "CreateCommittedResource (PC slot 27)"; case 31: return "CreateSharedHandle (proxies unwrapped)"; case 32: return "OpenSharedHandle (a fence comes back wrapped)"; @@ -147,9 +149,11 @@ const char* slot_name(unsigned slot) { case 50: return "EnqueueMakeResident (proxies unwrapped)"; case 62: return "Xbox CreateComputePipelineState"; case 67: return "Xbox DeserializeComputePipelineStateX"; + case 70: return "Xbox shader-resource view from raw words"; case 73: return "Xbox CreateCommandListX -> PC CreateCommandList"; case 75: return "CreateCommandQueue1 -> queue proxy"; case 76: return "Xbox resource creation"; + case 127: return "Xbox CreateCommittedResource3"; case 78: return "Xbox SetFrameIntervalX"; case 79: return "Xbox ScheduleFrameEventX"; case 80: return "Xbox WaitFrameEventX"; @@ -1444,6 +1448,45 @@ HRESULT proxy_create_command_list_x(DeviceProxy* self, std::uint64_t desc_word, return wrap_list(static_cast(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 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(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) { @@ -1817,6 +1860,87 @@ HRESULT proxy_create_placed_resource_x(DeviceProxy* self, void* va, const D3D12_ return handler(self->real, va, pc_desc, state, *riid, out); } +// Device slot 26, GetCustomHeapProperties, as the retail executable calls it. Measured on 1.0.2.1 under +// gta6rt --run with a register probe in this slot's thunk: three calls right after device creation, each with the +// device proxy in RDX and not in RCX, R8 = 1 and R9 = 1, 2, 3 (node mask 1; DEFAULT, UPLOAD, READBACK). So the +// buffer for the returned D3D12_HEAP_PROPERTIES is in RCX and the device in RDX. That is how x64 passes them to a +// plain function that takes the device as its first argument and returns a struct; a PC C++ method has the device +// in RCX and the buffer in RDX. The forwarder took RCX for the proxy, read the PC vtable out of the title's stack +// buffer and faulted. The buffer's address goes back in RAX, as that convention returns it; what the title does with +// RAX is not measured. +D3D12_HEAP_PROPERTIES* proxy_get_custom_heap_properties(D3D12_HEAP_PROPERTIES* out, DeviceProxy* self, + UINT node_mask, D3D12_HEAP_TYPE heap_type) { + note_slot(26); + *out = self->real->GetCustomHeapProperties(node_mask, heap_type); + return out; +} + +// 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(out_word); + if (out) *out = nullptr; // _COM_Outptr_: NULL on every failure, as slots 14/62/76 + const auto* const xbox_desc = reinterpret_cast(desc_word); + const auto* const riid = reinterpret_cast(riid_word); + if (!out || !xbox_desc || !riid) return E_POINTER; + if (self->slot_count <= kPcCreateCommittedResource3) { // the PC device predates ID3D12Device10 + static std::atomic 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(pc_desc.Layout); + const auto xbox_flags = static_cast(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(xbox_flags & kPcResourceFlagsMask); + + static std::atomic 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(pc_desc.Dimension), xbox_layout, static_cast(pc_desc.Layout), + xbox_flags, static_cast(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(self->real_vtable[kPcCreateCommittedResource3]); + return create(self->real, reinterpret_cast(heap_word), + static_cast(static_cast(heap_flags)), &pc_desc, + static_cast(layout_word), reinterpret_cast(clear_word), + reinterpret_cast(session_word), + static_cast(castable_count), reinterpret_cast(castable_formats), + riid, out); +} + template 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, @@ -1875,6 +1999,9 @@ std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64 reinterpret_cast(a2), static_cast(a3), reinterpret_cast(a5), reinterpret_cast(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(a1), @@ -1883,7 +2010,17 @@ std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64 static_cast(static_cast(a4)), reinterpret_cast(a5), reinterpret_cast(a6), reinterpret_cast(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(a1))) + return reinterpret_cast(proxy_get_custom_heap_properties( + reinterpret_cast(self), reinterpret_cast(a1), + static_cast(a2), static_cast(static_cast(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 @@ -1903,9 +2040,9 @@ std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64 } if (Slot == routes.queue_x) { log_xbox_route_once(Slot, "CreateCommandQueueX", a1, a2, a3, a4, a5); - return hr_to_u64(proxy_create_command_queue(self, reinterpret_cast(a1), - reinterpret_cast(a2), - reinterpret_cast(a3))); + return hr_to_u64(proxy_create_command_queue_xbox(self, reinterpret_cast(a1), + reinterpret_cast(a2), + reinterpret_cast(a3))); } if (Slot == routes.list_x) { log_xbox_route_once(Slot, "CreateCommandListX", a1, a2, a3, a4, a5); @@ -1930,10 +2067,55 @@ void fill_vtable(void** vtable, std::index_sequence) { ((vtable[I] = reinterpret_cast(&proxy_slot(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(a0) == vtable) // PC form: this, return buffer + return {reinterpret_cast(a0), reinterpret_cast(a1)}; + return {reinterpret_cast(a1), reinterpret_cast(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(call.ret) = call.self->real->GetResourceAllocationInfo( + static_cast(visible_mask), static_cast(count), descs); + return call.ret; +} + void** get_vtable() { static void** vtable = [] { static void* storage[kMaxSlots]; fill_vtable(storage, std::make_index_sequence{}); + storage[25] = reinterpret_cast(&proxy_get_resource_allocation_info); return storage; }(); return vtable; @@ -2070,9 +2252,9 @@ ID3D12Device* device_proxy_real_device(const void* device) { } bool device_slot_overridden(unsigned slot) { - if (slot <= 2 || slot == 8 || slot == 9 || slot == 14 || slot == 31 || slot == 32 || slot == 34 || slot == 35 || + if (slot <= 2 || slot == 8 || slot == 9 || slot == 14 || slot == 25 || slot == 26 || slot == 31 || slot == 32 || slot == 34 || slot == 35 || slot == 36 || slot == 45 || slot == 46 || slot == 50 || slot == 62 || slot == 67 || - slot == 75 || slot == 76 || slot == 133) + slot == 70 || slot == 75 || slot == 76 || slot == 133 || slot == kCreateCommittedResource3X) return true; const XboxDeviceSlotRoutes& routes = xbox_device_slot_routes(); return slot == routes.frame_interval || slot == routes.schedule_event || slot == routes.wait_event || diff --git a/runtime/shims/d3d12_xs_device.h b/runtime/shims/d3d12_xs_device.h index 20ed4afc15..7ee8809397 100644 --- a/runtime/shims/d3d12_xs_device.h +++ b/runtime/shims/d3d12_xs_device.h @@ -1,12 +1,24 @@ // Xbox-ABI proxy around a PC ID3D12Device. // // Measured Xbox device deviations used here (vtable indices, x64): +// slot 26 GetCustomHeapProperties, the PC slot, but the retail executable calls it with the buffer for the +// returned struct in RCX and the device in RDX (node mask in R8, heap type in R9). A PC C++ method has +// the device in RCX and the buffer in RDX. The thunk takes the console form when RDX holds a device +// proxy, and forwards the PC form otherwise. Slots 25 and 43 return structs too; no call of them by +// the executable has been seen yet, so they are forwarded in the PC form. // slot 62 CreateComputePipelineState(desc, w2, w3, iid, out): two words (0 from XboxBvhBuilder, meaning unknown) // before the IID. The PC slot 62 is CreateStateObject. // slot 67 CreateComputePipelineState from native code (desc, iid, out): converts the Xbox native-code desc // (+0x00 root signature, +0x28 code, +0x30 size) to D3D12_COMPUTE_PIPELINE_STATE_DESC and forwards // to PC CreateComputePipelineState (slot 11). The PC slot 67 is CreateProtectedResourceSession1. +// slot 70 (texture words, view desc, destination handle), no return value: a shader-resource view from raw words +// and a GPU address, with no resource (retail boot chain stop 14). The PC slot 70 is CreatePlacedResource1. +// The words are not measured, so the destination gets the PC null view (PC CreateShaderResourceView +// with no resource) and the call returns. // slot 76 resource creation -> PC CreateCommittedResource (PC slot 27), with the Xbox layout word ignored. +// slot 127 CreateCommittedResource3 (heap, flags, desc1, layout, clear, session, castable count/list, riid, out) -> +// PC ID3D12Device10::CreateCommittedResource3 (PC slot 76), with the Xbox layout word replaced and the +// Xbox flag bits dropped (RT-5 stop 15, docs/retail_boot_chain.md). E_NOTIMPL when the PC device lacks it. // slot 48 CreateCommandQueueX(desc, riid, out) -> the same queue proxy as slot 8. The Xbox desc's first fields are // D3D12_COMMAND_QUEUE_DESC's (public HLE). // slot 73 CreateCommandListX(desc, allocator, initialState, riid, out) -> PC CreateCommandList(0, type, allocator, @@ -18,6 +30,9 @@ // The slots 48/73/78/79/80 are the public HLE Freminet/ScarlettX's observation, not Microsoft documentation, and can // be moved with LEONIDA_XS_QUEUE_X_SLOT, LEONIDA_XS_LIST_X_SLOT, LEONIDA_XS_FRAME_INTERVAL_SLOT, // LEONIDA_XS_SCHEDULE_EVENT_SLOT and LEONIDA_XS_WAIT_EVENT_SLOT (3..127; anything else is logged once and ignored). +// slot 26 GetCustomHeapProperties, called by the retail title with the C ABI: return buffer in RCX, the device +// in RDX (the PC form is `this` in RCX, return buffer in RDX). Slot 25 (GetResourceAllocationInfo, also a +// struct wider than 8 bytes) takes the same form by the same rule. Both accept either form. // The measured slots 9/10 (GetCPU/GPUDescriptorHandleForHeapStart) are ID3D12DescriptorHeap methods, not device // methods: the PC device's 9/10 are CreateCommandAllocator/CreateGraphicsPipelineState and are forwarded unchanged. // On the PC heap those getters use the hidden return pointer in RDX; the Xbox ABI returns the 8-byte handle in @@ -118,8 +133,9 @@ using ConsoleDescriptorHeapHandler = HRESULT (*)(ID3D12Device* real, const D3D12 void set_console_descriptor_heap_handler(ConsoleDescriptorHeapHandler handler); // True for slots the proxy handles itself rather than forwarding verbatim: IUnknown (0-2), 8 and 75 (queue -// proxies), 9 (allocator proxies), 14, 31/34/35/45/46/50 (proxies unwrapped), 32 and 36 (fence proxies), 48/73 and -// 78/79/80 (the Xbox slots above, at their configured indices), 62, 67 and 76. Slot 10 is intentionally false: the +// proxies), 9 (allocator proxies), 14, 26 (the console's struct-return form), +// 31/34/35/45/46/50 (proxies unwrapped), 32 and 36 (fence proxies), 48/73 and +// 78/79/80 (the Xbox slots above, at their configured indices), 62, 67, 70, 76 and 127. Slot 10 is intentionally false: the // measured 9/10 handle conversion is on the heap proxy, and device slot 9 is CreateCommandAllocator, which the proxy // handles for the allocator proxy. bool device_slot_overridden(unsigned slot); diff --git a/runtime/test/device_proxy_test.cpp b/runtime/test/device_proxy_test.cpp index 46a853e7c3..b3b3f0fc3e 100644 --- a/runtime/test/device_proxy_test.cpp +++ b/runtime/test/device_proxy_test.cpp @@ -3,6 +3,7 @@ // It creates the proxy through the registered d3d12_xs!D3D12XboxCreateDevice shim, then calls the measured Xbox // slots exactly as the guest would (raw function pointers, not the C++ interface): // slot 14 CreateDescriptorHeap (returns a heap proxy) +// slot 25/26 GetResourceAllocationInfo/GetCustomHeapProperties in the title's C form (return buffer first) // heap 9/10 GetCPU/GetGPUDescriptorHandleForHeapStart (Xbox: 8-byte handle in RAX; the PC heap uses the // hidden return pointer in RDX, so slot 14 wraps it) // slot 62 CreateComputePipelineState @@ -199,6 +200,46 @@ int main(int argc, char** argv) { const LUID real_luid = real ? real->GetAdapterLuid() : LUID{}; check(proxy_luid.LowPart == real_luid.LowPart && proxy_luid.HighPart == real_luid.HighPart, "slot 43 forwards GetAdapterLuid (struct return)"); + // Slots 25 and 26 return a struct wider than 8 bytes. The console title calls them with the C ABI: the hidden + // return pointer first (RCX), then the device (RDX), then the arguments, and the buffer comes back in RAX + // (measured for slot 26 on the retail executable). Call them that way through raw function pointers and compare + // with the PC device's own answers. + check(rt::d3d12_xs::device_slot_overridden(25), "device slot 25 is the C-ABI GetResourceAllocationInfo override"); + check(rt::d3d12_xs::device_slot_overridden(26), "device slot 26 is the C-ABI GetCustomHeapProperties override"); + if (real) { + using HeapPropsFn = D3D12_HEAP_PROPERTIES*(WINAPI*)(D3D12_HEAP_PROPERTIES*, void*, UINT, D3D12_HEAP_TYPE); + auto get_heap_props = reinterpret_cast(proxy_vtbl[26]); + for (D3D12_HEAP_TYPE type : {D3D12_HEAP_TYPE_DEFAULT, D3D12_HEAP_TYPE_UPLOAD, D3D12_HEAP_TYPE_READBACK}) { + D3D12_HEAP_PROPERTIES got; + std::memset(&got, 0xcd, sizeof(got)); + D3D12_HEAP_PROPERTIES* returned = get_heap_props(&got, device.Get(), 1, type); + const D3D12_HEAP_PROPERTIES want = real->GetCustomHeapProperties(1, type); + check(returned == &got, "slot 26 returns the caller's buffer in RAX"); + check(std::memcmp(&got, &want, sizeof(got)) == 0, + "slot 26 (C ABI) writes the PC device's GetCustomHeapProperties into the caller's buffer"); + } + + using AllocInfoFn = D3D12_RESOURCE_ALLOCATION_INFO*(WINAPI*)(D3D12_RESOURCE_ALLOCATION_INFO*, void*, UINT, UINT, + const D3D12_RESOURCE_DESC*); + auto get_alloc_info = reinterpret_cast(proxy_vtbl[25]); + const D3D12_RESOURCE_DESC desc = buffer_desc(); + D3D12_RESOURCE_ALLOCATION_INFO got{~0ull, ~0ull}; + D3D12_RESOURCE_ALLOCATION_INFO* returned = get_alloc_info(&got, device.Get(), 0, 1, &desc); + const D3D12_RESOURCE_ALLOCATION_INFO want = real->GetResourceAllocationInfo(0, 1, &desc); + check(returned == &got, "slot 25 returns the caller's buffer in RAX"); + check(got.SizeInBytes == want.SizeInBytes && got.Alignment == want.Alignment, + "slot 25 (C ABI) writes the PC device's GetResourceAllocationInfo into the caller's buffer"); + + // The runtime's own code calls the proxy through d3d12.h (PC form: this, then the return buffer). Both + // slots still answer that form. + const D3D12_HEAP_PROPERTIES pc_props = device->GetCustomHeapProperties(1, D3D12_HEAP_TYPE_UPLOAD); + const D3D12_HEAP_PROPERTIES pc_want = real->GetCustomHeapProperties(1, D3D12_HEAP_TYPE_UPLOAD); + check(std::memcmp(&pc_props, &pc_want, sizeof(pc_props)) == 0, "slot 26 still answers the PC (C++) form"); + const D3D12_RESOURCE_ALLOCATION_INFO pc_info = device->GetResourceAllocationInfo(0, 1, &desc); + check(pc_info.SizeInBytes == want.SizeInBytes && pc_info.Alignment == want.Alignment, + "slot 25 still answers the PC (C++) form"); + } + D3D12_RESOURCE_DESC reserved_desc = buffer_desc(); reserved_desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; ComPtr reserved; @@ -206,6 +247,45 @@ int main(int argc, char** argv) { IID_PPV_ARGS(reserved.GetAddressOf())); check(SUCCEEDED(hr) && reserved != nullptr, "slot 30 forwards CreateReservedResource (stack arguments)"); + // Slot 26 the way the retail executable calls GetCustomHeapProperties: the buffer for the struct in RCX, the + // device in RDX, for the three heap types it asks about. The answer is the PC device's own, and nothing but + // the struct is written (the buffer sits in a pattern). On main this call read the PC vtable through the buffer. + check(rt::d3d12_xs::device_slot_overridden(26), "device slot 26 takes the console's struct-return form"); + using XboxGetCustomHeapPropertiesFn = + D3D12_HEAP_PROPERTIES*(WINAPI*)(D3D12_HEAP_PROPERTIES*, void*, UINT, D3D12_HEAP_TYPE); + auto get_custom_heap = reinterpret_cast(proxy_vtbl[26]); + const D3D12_HEAP_TYPE heap_types[] = {D3D12_HEAP_TYPE_DEFAULT, D3D12_HEAP_TYPE_UPLOAD, D3D12_HEAP_TYPE_READBACK}; + const auto slot26_hits_before = rt::d3d12_xs::device_slot_hits(26); + bool console_returns_buffer = true, console_matches_pc = real != nullptr, console_writes_only_struct = true; + for (const D3D12_HEAP_TYPE type : heap_types) { + struct Padded { + D3D12_HEAP_PROPERTIES value; + std::uint8_t after[64]; + } padded; + std::memset(&padded, 0xA5, sizeof(padded)); + console_returns_buffer = get_custom_heap(&padded.value, device.Get(), 1, type) == &padded.value && + console_returns_buffer; + const D3D12_HEAP_PROPERTIES want = real ? real->GetCustomHeapProperties(1, type) : D3D12_HEAP_PROPERTIES{}; + console_matches_pc = console_matches_pc && std::memcmp(&padded.value, &want, sizeof(want)) == 0; + for (const std::uint8_t byte : padded.after) + console_writes_only_struct = console_writes_only_struct && byte == 0xA5; + } + check(console_returns_buffer, "slot 26 console form returns the buffer's address"); + check(console_matches_pc, "slot 26 console form writes the PC device's DEFAULT, UPLOAD and READBACK properties"); + check(console_writes_only_struct, "slot 26 console form writes nothing past the struct"); + check(rt::d3d12_xs::device_slot_hits(26) == slot26_hits_before + 3, + "slot 26 hit counter increases by three for the console calls"); + check(rt::d3d12_xs::is_device_proxy(device.Get()) && rt::d3d12_xs::device_proxy_real_device(device.Get()) == real, + "the proxy is intact after the console-form calls"); + // A PC C++ call through the proxy (the proxy in RCX, the buffer in RDX) is still forwarded. + const D3D12_HEAP_PROPERTIES pc_form = device->GetCustomHeapProperties(1, D3D12_HEAP_TYPE_UPLOAD); + const D3D12_HEAP_PROPERTIES pc_want = real ? real->GetCustomHeapProperties(1, D3D12_HEAP_TYPE_UPLOAD) + : D3D12_HEAP_PROPERTIES{}; + check(real && std::memcmp(&pc_form, &pc_want, sizeof(pc_want)) == 0, + "slot 26 PC form still forwards GetCustomHeapProperties"); + check(rt::d3d12_xs::device_slot_hits(26) == slot26_hits_before + 4, + "slot 26 hit counter also includes the PC call"); + // Slot 14: descriptor heap through the proxy. The proxy wraps the real PC heap for the handle ABI. D3D12_DESCRIPTOR_HEAP_DESC heap_desc{}; heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; diff --git a/runtime/test/xs_proxy_mock_test.cpp b/runtime/test/xs_proxy_mock_test.cpp index 6af7ad8046..224030247c 100644 --- a/runtime/test/xs_proxy_mock_test.cpp +++ b/runtime/test/xs_proxy_mock_test.cpp @@ -7,6 +7,7 @@ // - OpenSharedHandle of a fence (as ID3D12Fence, IUnknown, Fence1), of a non-fence object, of an invalid handle, // and with a null output or IID, with the references of every object it touches; // - allocation failures in the proxy (operator new, the arrays it unwraps), injected in this executable only; +// - device slot 26 called the console's way (the struct's buffer in RCX, the device in RDX) and the PC way; // - --fail-virtualalloc / --fail-virtualprotect / --fail-veh: the guard region's setup failing, injected by // patching this executable's own import table before the first proxy table is built (one process each). // Every device, queue, fence and resource here is a mock that records the calls it gets. No game data, no Xbox DLLs. @@ -75,6 +76,8 @@ enum class Kind { device, queue, fence, resource, allocator, command_list, heap constexpr unsigned kMockSlots = 128; constexpr std::uint64_t kFenceHandle = 0x1000, kResourceHandle = 0x2000; // the handles the mock device opens constexpr std::uint64_t kCompletedValue = 7; // what a mock fence's slot 8 returns +constexpr UINT kHeapMarker = 0x26262626; // the VisibleNodeMask a mock device's slot 26 answers +constexpr UINT64 kAllocationMarker = 0x2525252500000000ull; // in the SizeInBytes a mock device's slot 25 answers // {5d1e0c4a-2b7f-4e61-9a3d-8c4b2f1e6a70}: an interface the mock queue implements that the proxy does not model. constexpr GUID kExtraIid{0x5d1e0c4a, 0x2b7f, 0x4e61, {0x9a, 0x3d, 0x8c, 0x4b, 0x2f, 0x1e, 0x6a, 0x70}}; @@ -92,6 +95,7 @@ struct Mock { unsigned calls[kMockSlots]; std::uint64_t args[kMockSlots][5]; std::uint64_t array[kMockSlots][4]; // device: the first entries of the array argument, as received + D3D12_COMMAND_QUEUE_DESC queue_desc; // device: the description of its last CreateCommandQueue (slot 8) }; Mock g_pool[256]; @@ -199,7 +203,9 @@ std::uint64_t mock_call(Mock* m, unsigned slot, const std::uint64_t* words) { const std::uint64_t a1 = words[0], a2 = words[1], a3 = words[2], a4 = words[3]; if (m->kind == Kind::device) { switch (slot) { - case 8: return static_cast(mock_create(m, Kind::queue, a2, a3)); // CreateCommandQueue + case 8: // CreateCommandQueue + if (a1) std::memcpy(&m->queue_desc, reinterpret_cast(a1), sizeof(m->queue_desc)); + return static_cast(mock_create(m, Kind::queue, a2, a3)); case 9: return static_cast(mock_create(m, Kind::allocator, a2, a3)); // CreateCmdAllocator case 14: return static_cast(mock_create(m, Kind::heap, a2, a3)); // CreateDescriptorHeap case 75: return static_cast(mock_create(m, Kind::queue, a3, a4)); // CreateCommandQueue1 @@ -210,6 +216,22 @@ std::uint64_t mock_call(Mock* m, unsigned slot, const std::uint64_t* words) { return static_cast(E_INVALIDARG); case 34: case 35: case 46: copy_array(m, slot, a2, a1); break; // (count, objects, ...) case 45: copy_array(m, slot, a1, a3); break; // (fences, values, count, ...) + case 25: { // GetResourceAllocationInfo, the PC method: (return buffer, visible mask, count, descs) + auto* out = reinterpret_cast(a1); + if (!out) break; + out->SizeInBytes = kAllocationMarker + a3; // the count, so the test sees the arguments arrive + out->Alignment = static_cast(a2); // the visible mask + return a1; + } + case 26: { // GetCustomHeapProperties, the PC method: (return buffer, node mask, heap type) + auto* out = reinterpret_cast(a1); + if (!out) break; + *out = {}; + out->Type = static_cast(static_cast(a3)); + out->CreationNodeMask = static_cast(a2); + out->VisibleNodeMask = kHeapMarker; + return a1; // a struct-returning method hands the buffer's address back in RAX + } case 50: copy_array(m, slot, a3, a2); break; // (flags, count, objects, fence, value) default: break; } @@ -263,6 +285,35 @@ HRESULT WINAPI mock_create_command_list(Mock* self, std::uint64_t node_mask, std return mock_query(child, riid, out); } +#ifdef __ID3D12Device10_INTERFACE_DEFINED__ +// PC ID3D12Device10::CreateCommittedResource3 takes ten words after `this`, so it has its own entry (PC slot 76) that +// keeps all of them: the resource description is copied because the caller's copy is a local of the proxy. +struct CommittedResource3Call { + unsigned calls; + const D3D12_HEAP_PROPERTIES* heap; + D3D12_HEAP_FLAGS heap_flags; + D3D12_RESOURCE_DESC1 desc; + std::uint32_t layout; + const D3D12_CLEAR_VALUE* clear; + const void* session; + std::uint32_t castable_count; + const DXGI_FORMAT* castable_formats; + const IID* riid; + void** out; +}; +CommittedResource3Call g_committed3; + +HRESULT WINAPI mock_create_committed_resource3(Mock* self, const D3D12_HEAP_PROPERTIES* heap, D3D12_HEAP_FLAGS flags, + const D3D12_RESOURCE_DESC1* desc, std::uint32_t layout, + const D3D12_CLEAR_VALUE* clear, const void* session, + std::uint32_t castable_count, const DXGI_FORMAT* castable_formats, + const IID* riid, void** out) { + g_committed3 = {g_committed3.calls + 1, heap, flags, *desc, layout, clear, session, castable_count, + castable_formats, riid, out}; + return mock_create(self, Kind::resource, reinterpret_cast(riid), reinterpret_cast(out)); +} +#endif + template void fill_mock_vtable(void** vtable, std::index_sequence) { ((vtable[I] = reinterpret_cast(&mock_slot(I)>)), ...); @@ -273,6 +324,9 @@ void** mock_vtable() { static const bool filled = [] { fill_mock_vtable(table, std::make_index_sequence{}); table[12] = reinterpret_cast(&mock_create_command_list); +#ifdef __ID3D12Device10_INTERFACE_DEFINED__ + table[76] = reinterpret_cast(&mock_create_committed_resource3); +#endif return true; }(); (void)filled; @@ -292,6 +346,13 @@ std::uint64_t raw(const void* object, unsigned slot, std::uint64_t a1 = 0, std:: 0, 0, 0); } +// A call with the ten words of device slot 127 (and any other slot that takes up to ten). +std::uint64_t raw10(const void* object, unsigned slot, const std::uint64_t (&w)[10]) { + void* const* vtable = *reinterpret_cast(object); + return reinterpret_cast(vtable[slot])(const_cast(object), w[0], w[1], w[2], w[3], w[4], w[5], w[6], + w[7], w[8], w[9], 0, 0, 0, 0); +} + HRESULT hr_of(std::uint64_t value) { return static_cast(static_cast(value)); } @@ -308,6 +369,8 @@ const IID kUnknownIid = __uuidof(IUnknown); const IID kResourceIid = __uuidof(ID3D12Resource); const IID kDevice1Iid = __uuidof(ID3D12Device1); const D3D12_COMMAND_QUEUE_DESC kQueueDesc{}; +// The console's CreateCommandQueueX description: the PC description's 16 bytes, then four Xbox words (slot 48). +const std::uint32_t kXboxQueueDesc[8] = {}; // A device proxy over a new mock device. The proxy takes the mock's one reference. void* make_device(Mock*& mock, int level, bool fence1, bool allocator_tag = true) { @@ -514,6 +577,30 @@ int check_levels() { return 0; } +// ---- 1b. Slot 48: the title's CreateCommandQueue with a 32-byte description ---- + +int check_slot48() { + Mock* mock = nullptr; + void* device = make_device(mock, 3, true); + EXPECT(device); + // 16-byte PC prefix, then four Xbox words; the flags carry a bit the PC does not know besides the timeout bit. + const std::uint32_t desc[8] = {D3D12_COMMAND_LIST_TYPE_COMPUTE, 100, + 0x8000u | D3D12_COMMAND_QUEUE_FLAG_DISABLE_GPU_TIMEOUT, 1, 0x11, 0x22, 0x33, 0x44}; + void* queue = reinterpret_cast(1); + EXPECT(hr_of(raw(device, 48, word(desc), word(&kQueueIid), word(&queue))) == S_OK); + EXPECT(xs::is_queue_proxy(queue) && mock->calls[8] == 1); + EXPECT(mock->queue_desc.Type == D3D12_COMMAND_LIST_TYPE_COMPUTE && mock->queue_desc.Priority == 100 && + mock->queue_desc.Flags == D3D12_COMMAND_QUEUE_FLAG_DISABLE_GPU_TIMEOUT && + mock->queue_desc.NodeMask == 1); + EXPECT(release(queue) == 0); + void* none = reinterpret_cast(1); + EXPECT(hr_of(raw(device, 48, 0, word(&kQueueIid), word(&none))) == E_INVALIDARG && !none); + EXPECT(mock->calls[8] == 1); + EXPECT(release(device) == 0); + std::printf("%s: slot 48 makes a queue proxy from a 32-byte description, PC prefix and flags only\n", g_label); + return 0; +} + // ---- 2. Fences with and without ID3D12Fence1 ---- int check_fence1() { @@ -838,9 +925,10 @@ int check_xbox_slots() { // 48 CreateCommandQueueX(desc, riid, out): the queue proxy, created through the PC slot 8 path. void* queue = nullptr; - EXPECT(hr_of(raw(device, 48, word(&kQueueDesc), word(&kQueueIid), word(&queue))) == S_OK); + EXPECT(hr_of(raw(device, 48, word(kXboxQueueDesc), word(&kQueueIid), word(&queue))) == S_OK); EXPECT(xs::is_queue_proxy(queue) && mock->calls[48] == 0 && mock->calls[8] == 1); - EXPECT(mock->args[8][0] == word(&kQueueDesc)); + // The PC call gets the description's 16-byte PC prefix, not the console's 32 bytes. + EXPECT(std::memcmp(&mock->queue_desc, kXboxQueueDesc, sizeof(mock->queue_desc)) == 0); // 73 CreateCommandListX(desc, allocator, initialState, riid, out): the type is desc's first word, the allocator // proxy is unwrapped, the node mask is 0 and the PC six-argument CreateCommandList (slot 12) is reached. @@ -916,7 +1004,7 @@ int run_env_slots(bool valid) { EXPECT(hr_of(raw(device, 97, 0, 0, 0, 0)) == S_OK && mock->calls[97] == 0); EXPECT(hr_of(raw(device, 98, 0, 0, 0, 0, word(&token))) == S_OK && token && mock->calls[98] == 0); void* queue = nullptr; - EXPECT(hr_of(raw(device, 99, word(&kQueueDesc), word(&kQueueIid), word(&queue))) == S_OK); + EXPECT(hr_of(raw(device, 99, word(kXboxQueueDesc), word(&kQueueIid), word(&queue))) == S_OK); EXPECT(xs::is_queue_proxy(queue) && mock->calls[99] == 0); const IID allocator_iid = __uuidof(ID3D12CommandAllocator); void* allocator = nullptr; @@ -937,7 +1025,7 @@ int run_env_slots(bool valid) { EXPECT(hr_of(raw(device, 78, 0, 16667, 1, 0)) == S_OK && mock->calls[78] == 0); EXPECT(hr_of(raw(device, 79, 0, 0, 0, 0)) == S_OK && mock->calls[79] == 0); EXPECT(hr_of(raw(device, 80, 0, 0, 0, 0, word(&token))) == S_OK && token && mock->calls[80] == 0); - EXPECT(hr_of(raw(device, 70)) == S_OK && mock->calls[70] == 1); // not an Xbox slot: forwarded + EXPECT(hr_of(raw(device, 71)) == S_OK && mock->calls[71] == 1); // not an Xbox slot: forwarded } EXPECT(release(device) == 0 && mock->refs == 0); std::printf("%s: environment slot overrides %s\n", g_label, @@ -945,6 +1033,116 @@ int run_env_slots(bool valid) { return 0; } +// ---- 8b. Device slot 26, GetCustomHeapProperties, the console's way and the PC's ---- + +int check_custom_heap_properties() { + Mock* mock = nullptr; + void* device = make_device(mock, 0, true); + EXPECT(device && xs::device_slot_overridden(26)); + void* const* vtable = *reinterpret_cast(device); + + // The retail executable's call: the buffer for the struct in RCX, the device in RDX, node mask and heap type in + // R8 and R9 (their upper halves are not the callee's to read). The buffer sits inside a pattern that must stay. + struct Padded { + std::uint8_t before[16]; + D3D12_HEAP_PROPERTIES value; + std::uint8_t after[64]; + } padded; + std::memset(&padded, 0xA5, sizeof(padded)); + using ConsoleFn = std::uint64_t(WINAPI*)(void*, void*, std::uint64_t, std::uint64_t); + const std::uint64_t returned = reinterpret_cast(vtable[26])( + &padded.value, device, 0xdeadbeef00000001ull, 0xdeadbeef00000002ull); + EXPECT(returned == word(&padded.value)); + // The PC method got (buffer, node mask, heap type) once, on the device behind the proxy. + EXPECT(mock->calls[26] == 1); + EXPECT(static_cast(mock->args[26][1]) == 1 && static_cast(mock->args[26][2]) == 2); + EXPECT(padded.value.Type == D3D12_HEAP_TYPE_UPLOAD && padded.value.CreationNodeMask == 1 && + padded.value.VisibleNodeMask == kHeapMarker); + for (const std::uint8_t byte : padded.before) EXPECT(byte == 0xA5); + for (const std::uint8_t byte : padded.after) EXPECT(byte == 0xA5); + // The proxy is what it was: main's forwarder took the buffer for the proxy, and a thunk that took the proxy + // for the buffer would write the struct over it. + EXPECT(xs::is_device_proxy(device) && word(xs::device_proxy_real_device(device)) == word(mock) && + xs::device_proxy_slot_count(device) == 44 && xs::device_proxy_ref_count(device) == 1); + + // A PC C++ caller: the proxy in RCX and its buffer in RDX. That call is forwarded as it is. + D3D12_HEAP_PROPERTIES pc{}; + EXPECT(raw(device, 26, word(&pc), 1, 3) == word(&pc)); + EXPECT(mock->calls[26] == 2 && mock->args[26][0] == word(&pc)); + EXPECT(pc.Type == D3D12_HEAP_TYPE_READBACK && pc.CreationNodeMask == 1 && pc.VisibleNodeMask == kHeapMarker); + + EXPECT(release(device) == 0 && mock->refs == 0); + std::printf("%s: device slot 26 with the buffer in RCX and the device in RDX reaches the PC method and writes " + "only the struct; the PC form is forwarded unchanged\n", g_label); + return 0; +} + +// ---- 8c. Device slot 25, GetResourceAllocationInfo, in the console's form and the PC's ---- + +int check_resource_allocation_info() { + Mock* mock = nullptr; + void* device = make_device(mock, 0, true); + EXPECT(device && xs::device_slot_overridden(25)); + void* const* vtable = *reinterpret_cast(device); + const D3D12_RESOURCE_DESC descs[2] = {}; + + // The console form: the buffer for the struct in RCX, the device in RDX, then the mask, the count and the + // descriptions. The buffer sits inside a pattern that must stay. + struct Padded { + std::uint8_t before[16]; + D3D12_RESOURCE_ALLOCATION_INFO value; + std::uint8_t after[64]; + } padded; + std::memset(&padded, 0xA5, sizeof(padded)); + using ConsoleFn = std::uint64_t(WINAPI*)(void*, void*, std::uint64_t, std::uint64_t, std::uint64_t); + const std::uint64_t returned = + reinterpret_cast(vtable[25])(&padded.value, device, 1, 2, word(descs)); + EXPECT(returned == word(&padded.value)); + // The PC method got (a return buffer, mask, count, descriptions) once, on the device behind the proxy. + EXPECT(mock->calls[25] == 1 && mock->args[25][1] == 1 && mock->args[25][2] == 2 && mock->args[25][3] == word(descs)); + EXPECT(padded.value.SizeInBytes == kAllocationMarker + 2 && padded.value.Alignment == 1); + for (const std::uint8_t byte : padded.before) EXPECT(byte == 0xA5); + for (const std::uint8_t byte : padded.after) EXPECT(byte == 0xA5); + EXPECT(xs::is_device_proxy(device) && word(xs::device_proxy_real_device(device)) == word(mock)); + + // A PC C++ caller: the proxy in RCX and its buffer in RDX. + D3D12_RESOURCE_ALLOCATION_INFO pc{}; + EXPECT(raw(device, 25, word(&pc), 1, 1, word(descs)) == word(&pc)); + EXPECT(mock->calls[25] == 2 && mock->args[25][2] == 1 && mock->args[25][3] == word(descs)); + EXPECT(pc.SizeInBytes == kAllocationMarker + 1 && pc.Alignment == 1); + + EXPECT(release(device) == 0 && mock->refs == 0); + std::printf("%s: device slot 25 with the buffer in RCX and the device in RDX reaches the PC method and writes " + "only the struct; the PC form reaches it too\n", g_label); + return 0; +} + +// ---- 8. Device slot 70: a view from raw words, never the PC CreatePlacedResource1 ---- + +int check_slot70() { + // Level 9 has the PC slot 70; level 0 does not (44 PC slots). Neither may reach CreatePlacedResource1. + for (const int level : {9, 0}) { + Mock* mock = nullptr; + void* device = make_device(mock, level, true); + EXPECT(device && xs::device_slot_overridden(70)); + xs::reset_device_proxy_stats(); + const std::uint32_t words[8] = {0x1000, 0, 0, 0, 0, 0, 0, 0}; + const std::uint32_t view[8] = {}; + const std::uint64_t destination = 0x7000; + raw(device, 70, word(words), word(view), destination); + EXPECT(mock->calls[70] == 0 && xs::device_slot_hits(70) == 1); + // The destination gets the PC null view: CreateShaderResourceView (slot 18) with no resource, a desc, and the + // destination handle unchanged. + EXPECT(mock->calls[18] == 1 && mock->args[18][0] == 0 && mock->args[18][1] != 0 && + mock->args[18][2] == destination); + // No destination, no write. + raw(device, 70, word(words), word(view), 0); + EXPECT(mock->calls[70] == 0 && mock->calls[18] == 1); + EXPECT(release(device) == 0 && mock->refs == 0); + } + return 0; +} + // ---- 9. The refused tail and the guard region ---- int check_tail(bool want_guarded) { @@ -1047,6 +1245,103 @@ int run_fault_mode(const char* mode) { return 2; } +// Device slot 127 is ID3D12Device10::CreateCommittedResource3 with Xbox descriptors (RT-5 stop 15). Past the PC +// vtable it used to be refused with E_NOTIMPL; the PC device must now get the resource with the Xbox layout word and +// flag bits gone and every other word as the title passed it. +int check_committed_resource3_x() { +#ifdef __ID3D12Device10_INTERFACE_DEFINED__ + constexpr std::uint32_t kXboxLayoutTiled = 0x101, kXboxFlagBit = 0x40000, kBarrierLayoutUndefined = 0xFFFFFFFFu; + D3D12_HEAP_PROPERTIES heap{}; + heap.Type = D3D12_HEAP_TYPE_DEFAULT; + D3D12_CLEAR_VALUE clear{}; + clear.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + const DXGI_FORMAT castable[2] = {DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB}; + D3D12_RESOURCE_DESC1 texture{}; + texture.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + texture.Width = 1920; + texture.Height = 1080; + texture.DepthOrArraySize = 1; + texture.MipLevels = 1; + texture.Format = DXGI_FORMAT_R8G8B8A8_TYPELESS; + texture.SampleDesc.Count = 1; + texture.Layout = static_cast(kXboxLayoutTiled); + texture.Flags = static_cast(D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | + D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS | kXboxFlagBit); + + // A PC device with ID3D12Device10: the render target comes back, as the console's call returns it. + Mock* mock = nullptr; + void* device = make_device(mock, 10, true); + EXPECT(device && xs::device_proxy_slot_count(device) == 79); + xs::reset_device_proxy_stats(); + g_committed3 = {}; + void* resource = nullptr; + std::uint64_t words[10] = {word(&heap), D3D12_HEAP_FLAG_NONE, word(&texture), kBarrierLayoutUndefined, + word(&clear), 0, 2, word(castable), word(&kResourceIid), word(&resource)}; + EXPECT(hr_of(raw10(device, 127, words)) == S_OK); + EXPECT(resource && reinterpret_cast(resource)->kind == Kind::resource); + EXPECT(g_committed3.calls == 1 && xs::device_slot_hits(127) == 1); + EXPECT(g_committed3.heap == &heap && g_committed3.clear == &clear && !g_committed3.session); + EXPECT(g_committed3.layout == kBarrierLayoutUndefined); + EXPECT(g_committed3.castable_count == 2 && g_committed3.castable_formats == castable); + EXPECT(g_committed3.riid == &kResourceIid); + EXPECT(g_committed3.desc.Layout == D3D12_TEXTURE_LAYOUT_UNKNOWN); + EXPECT(g_committed3.desc.Flags == + (D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS)); + EXPECT(g_committed3.desc.Width == 1920 && g_committed3.desc.Height == 1080 && + g_committed3.desc.Format == DXGI_FORMAT_R8G8B8A8_TYPELESS && g_committed3.desc.SampleDesc.Count == 1); + EXPECT(texture.Layout == static_cast(kXboxLayoutTiled)); // the title's description is read only + static_cast(release(resource)); + + // A buffer takes the row-major layout. + D3D12_RESOURCE_DESC1 buffer = texture; + buffer.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + buffer.Height = 1; + buffer.Format = DXGI_FORMAT_UNKNOWN; + words[2] = word(&buffer); + words[6] = 0; + words[7] = 0; + resource = nullptr; + EXPECT(hr_of(raw10(device, 127, words)) == S_OK && resource); + EXPECT(g_committed3.calls == 2 && g_committed3.desc.Layout == D3D12_TEXTURE_LAYOUT_ROW_MAJOR); + + // The PC device's own refusal reaches the title with a null object. + mock->create_result = E_OUTOFMEMORY; + resource = reinterpret_cast(1); + words[9] = word(&resource); + EXPECT(hr_of(raw10(device, 127, words)) == E_OUTOFMEMORY && !resource); + mock->create_result = S_OK; + + // A null description, IID or output is refused before the PC device is called. + const unsigned before = g_committed3.calls; + std::uint64_t bad[10]; + std::memcpy(bad, words, sizeof(bad)); + bad[2] = 0; + EXPECT(hr_of(raw10(device, 127, bad)) == E_POINTER); + std::memcpy(bad, words, sizeof(bad)); + bad[8] = 0; + EXPECT(hr_of(raw10(device, 127, bad)) == E_POINTER); + std::memcpy(bad, words, sizeof(bad)); + bad[9] = 0; + EXPECT(hr_of(raw10(device, 127, bad)) == E_POINTER); + EXPECT(g_committed3.calls == before); + EXPECT(xs::device_slot_overridden(127)); + EXPECT(release(device) == 0); + + // A PC device that stops at ID3D12Device9 has no CreateCommittedResource3: E_NOTIMPL, a null object, no PC call. + Mock* old_mock = nullptr; + void* old_device = make_device(old_mock, 9, true); + EXPECT(old_device && xs::device_proxy_slot_count(old_device) == 76); + resource = reinterpret_cast(1); + words[9] = word(&resource); + const unsigned old_before = g_committed3.calls; + EXPECT(hr_of(raw10(old_device, 127, words)) == E_NOTIMPL && !resource); + EXPECT(g_committed3.calls == old_before && old_mock->calls[76] == 0); + EXPECT(release(old_device) == 0); + std::printf("%s: device slot 127 creates the committed resource with the PC flags and layout\n", g_label); +#endif + return 0; +} + } // namespace // A failure ends the process with a message on stderr, never in a dialog a test run would wait on: abort() (an @@ -1248,8 +1543,10 @@ int main(int argc, char** argv) { return run_fault_mode(argv[1]); } if (argc != 1) return 2; - if (check_tail(true) || check_levels() || check_fence1() || check_create_failures() || check_open_shared() || - check_alloc_failures() || check_allocators() || check_xbox_slots() || check_console_command_list()) + if (check_tail(true) || check_levels() || check_slot48() || check_fence1() || check_create_failures() || check_open_shared() || + check_alloc_failures() || check_allocators() || check_xbox_slots() || check_console_command_list() || + check_custom_heap_properties() || + check_resource_allocation_info() || check_slot70() || check_committed_resource3_x()) return 1; std::printf("xs_proxy_mock_test: PASS (%d allocations refused on purpose)\n", g_failed_news); return 0;