diff --git a/runtime/shims/acphal.cpp b/runtime/shims/acphal.cpp index 553f57f207..98ada97b0b 100644 --- a/runtime/shims/acphal.cpp +++ b/runtime/shims/acphal.cpp @@ -141,7 +141,7 @@ struct SHAPE_XMA_CONTEXT { uint32_t reserved1 : 6; uint32_t loopStartOffset : 26; uint32_t loopEndOffset : 6; - uint32_t reserved[10]; + uint32_t reserved[11]; }; static_assert(sizeof(SHAPE_XMA_CONTEXT) == 64, "SHAPE_XMA_CONTEXT is 64 bytes per GDK"); @@ -162,7 +162,7 @@ struct SHAPE_PCM_CONTEXT { uint32_t reserved6 : 15; uint32_t format : 2; uint32_t reserved7 : 14; - uint32_t reserved[1]; + uint32_t reserved[2]; }; static_assert(sizeof(SHAPE_PCM_CONTEXT) == 32, "SHAPE_PCM_CONTEXT is 32 bytes per GDK"); diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index a6911d0f4e..92ae184de5 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -5008,6 +5008,7 @@ class Device final // standalone device gta6rt --run uses both come through here (found in review: registering only in // create_vulkan_device left the retail path without it). rt::d3d12_xs::set_placed_at_va_handler(&create_buffer_at_host_address); + rt::d3d12_xs::set_console_descriptor_heap_handler(&create_console_descriptor_heap); state_.info = info; queue_users_.resize(info.command_queues.size(), 0); compute_users_.resize(info.compute_queues.size(), 0); @@ -5499,6 +5500,7 @@ class Device final leonida::VulkanContext& context() { return *state_.context; } const leonida::BindlessSet* bindless() const { return state_.bindless.get(); } const DeviceState& state() const { return state_; } + DeviceState& state() { return state_; } IUnknown* holder() { return identity(); } bool bind_sampler(const leonida::gfx10::SamplerWords& words) { return state_.bind_sampler(words); } void own(const OwnedRuntime& owned) { owned_ = owned; } @@ -9999,7 +10001,7 @@ HRESULT create_console_descriptor_heap(ID3D12Device* device, const D3D12_DESCRIP if (!d) return E_INVALIDARG; auto* heap = new DescriptorHeap(d->state(), *desc, console_address, console_size); - heap->attach_device(d->identity()); + heap->attach_device(d->holder()); if (!heap->ok()) { heap->Release(); return E_OUTOFMEMORY; diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index 187406e3d8..5631eb8be5 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -1579,6 +1579,8 @@ static bool readable_range(const void* ptr, std::size_t bytes) { return true; } +static std::atomic g_console_descriptor_heap{nullptr}; + HRESULT proxy_create_descriptor_heap(DeviceProxy* self, const D3D12_DESCRIPTOR_HEAP_DESC* desc, const IID* riid, void** out) { note_slot(14); @@ -1599,13 +1601,21 @@ HRESULT proxy_create_descriptor_heap(DeviceProxy* self, const D3D12_DESCRIPTOR_H 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. Only take this path when a - // non-zero console VA is present; normal PC 16-byte descs never enter it. - if (address != 0) { - console_extended = true; - hr = rt::d3d12::create_console_descriptor_heap( - self->real, desc, static_cast(address), size, - __uuidof(ID3D12DescriptorHeap), &created); + // Console-extended slot 14 (issue #313): address/size follow the PC desc. Require a plausible aligned + // canonical VA and a matching non-zero heap byte size so normal stack-allocated 16-byte PC descs with + // adjacent stack data never enter this path by accident. + const std::uint64_t min_size = std::uint64_t(desc->NumDescriptors) * 16ull; + const std::uint64_t max_size = std::uint64_t(desc->NumDescriptors) * 128ull; + const bool plausible_console_va = address >= 0x10000ull && address < 0x0000800000000000ull && + (address & 0xffull) == 0; + const bool plausible_size = desc->NumDescriptors > 0 && size >= min_size && size <= max_size && + (size & 0xfull) == 0; + if (plausible_console_va && plausible_size) { + if (const auto handler = g_console_descriptor_heap.load(std::memory_order_acquire)) { + console_extended = true; + hr = handler(self->real, desc, static_cast(address), size, + __uuidof(ID3D12DescriptorHeap), &created); + } } } } @@ -2041,6 +2051,10 @@ 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(device)->real; diff --git a/runtime/shims/d3d12_xs_device.h b/runtime/shims/d3d12_xs_device.h index ac0437215d..68d659b6dc 100644 --- a/runtime/shims/d3d12_xs_device.h +++ b/runtime/shims/d3d12_xs_device.h @@ -108,6 +108,14 @@ void set_placed_at_va_handler(PlacedAtVaHandler handler); using PlacedAtVaPagesHook = bool (*)(const void* va, std::size_t bytes); void set_placed_at_va_pages_hook(PlacedAtVaPagesHook hook); +// Device slot 14 with a console-extended descriptor heap desc (address/size after the PC 16-byte desc, issue #313) +// delegates creation to this handler, which the Vulkan layer registers (rt::d3d12::create_console_descriptor_heap). +// With none registered (e.g. standalone proxy tests without the Vulkan layer), slot 14 falls back to the PC path. +using ConsoleDescriptorHeapHandler = HRESULT (*)(ID3D12Device* real, const D3D12_DESCRIPTOR_HEAP_DESC* desc, + D3D12_GPU_VIRTUAL_ADDRESS console_address, + std::uint64_t console_size, REFIID riid, void** out); +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 diff --git a/runtime/test/d3d12_console_descriptor_heap_test.cpp b/runtime/test/d3d12_console_descriptor_heap_test.cpp index 5f694b5a2a..4f3b2677c3 100644 --- a/runtime/test/d3d12_console_descriptor_heap_test.cpp +++ b/runtime/test/d3d12_console_descriptor_heap_test.cpp @@ -24,6 +24,7 @@ struct Ref { return &p; } T* get() const { return p; } + T* operator->() const { return p; } }; bool check_console_addressed_heap(ID3D12Device* device) { @@ -64,13 +65,13 @@ bool check_console_addressed_heap(ID3D12Device* device) { } bool check_xs_proxy_slot14(ID3D12Device* real_device) { - void* proxy_raw = nullptr; - if (FAILED(rt::d3d12_xs::create_device_proxy(real_device, IID_PPV_ARGS(&proxy_raw))) || !proxy_raw) { + Ref proxy; + real_device->AddRef(); + if (FAILED(rt::d3d12_xs::create_device_proxy(real_device, IID_PPV_ARGS(proxy.put()))) || !proxy.get()) { + real_device->Release(); std::fprintf(stderr, "create_device_proxy failed\n"); return false; } - Ref proxy; - proxy.p = static_cast(proxy_raw); struct ConsoleDescriptorHeapDesc { D3D12_DESCRIPTOR_HEAP_DESC desc{}; @@ -90,10 +91,13 @@ bool check_xs_proxy_slot14(ID3D12Device* real_device) { static_cast(hr)); return false; } - const auto cpu = heap->GetCPUDescriptorHandleForHeapStart(); - if (cpu.ptr != static_cast(console.address)) { + // Heap proxy slot 9 uses the Xbox ABI (returns the 8-byte handle in RAX, not via hidden RDX pointer). + auto** vtbl = *reinterpret_cast(heap.get()); + using XboxGetHandleFn = std::uint64_t(STDMETHODCALLTYPE*)(void*); + const std::uint64_t cpu = reinterpret_cast(vtbl[9])(heap.get()); + if (cpu != console.address) { std::fprintf(stderr, "proxy CPU heap start mismatch: got 0x%llx want 0x%llx\n", - static_cast(cpu.ptr), + static_cast(cpu), static_cast(console.address)); return false; }