From 99176da104f8b49d3987cad24b300cd1a5f5f4c8 Mon Sep 17 00:00:00 2001 From: Moriz82 <72836413+Moriz82@users.noreply.github.com> Date: Tue, 24 Nov 2026 13:30:17 -0600 Subject: [PATCH 1/2] d3d12: resolve views of live placed textures (RT-TITLE-TEXTURE-VIEWS) A raw shader-resource view of a live image used to return a null descriptor. Match the placement and resource description for SRVs and UAVs, retain the chosen image while creating its view, and remove its entry on final release. The placement ABI is also documented by VIctory. The default and rollback GPU tests each sampled 64 expected texels from a placed view. The default accepted two writable views and rejected two controls. The rollback returned four null UAVs. xs_proxy_mock's invalid environment-slot case used slot 71 as its forwarded PC slot. Slot 71 is routed now, so the case probes slot 72. All six CTest forms of the proxy test pass. AI-assisted (Codex and Claude Code agents). Co-authored-by: Plaaasma <47513553+Plaaasma@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 --- docs/d3d12_vulkan.md | 5 ++ runtime/CMakeLists.txt | 2 + runtime/shims/d3d12_com_object.h | 5 +- runtime/shims/d3d12_vulkan_device.cpp | 94 +++++++++++++++++++++++- runtime/shims/d3d12_xs_device.cpp | 65 +++++++++++----- runtime/shims/d3d12_xs_device.h | 11 ++- runtime/test/d3d12_set0_compute_test.cpp | 93 +++++++++++++++++++++-- runtime/test/xs_proxy_mock_test.cpp | 22 ++++-- 8 files changed, 258 insertions(+), 39 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 8bf2bd38ea..53debf15a4 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -668,6 +668,11 @@ side inwards: `TEXTURE2DMS` view reads layer zero, while `TEXTURE2DMSARRAY` selects a range starting at `FirstArraySlice` (`ArraySize = UINT_MAX` selects the remaining layers). The format-support query advertises `MULTISAMPLE_LOAD` when the format can be sampled with MSAA and its binding pair is available. + - Xbox device slots 70 (SRV) and 71 (UAV) can select a live texture created by slot 133. + The address and resource description select the most recent compatible placement, including when + several live resources share an address. UAVs require writable backing. A 3D UAV can select one + complete mip. A missing placement or unsupported view gives a null descriptor. + Set `LEONIDA_PLACED_TEXTURE_UAV=0` to restore null placed UAVs. - A texture is named in its T#s by an address of its own, above 4 GiB and 256 bytes apart, that nothing dereferences; its entries leave the set and the table when it is destroyed. - `VulkanDeviceInfo::bindless` says the Vulkan device has the features this needs: `runtimeDescriptorArray`, diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 3b334c7133..e055c5a59b 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -3009,6 +3009,8 @@ if(TARGET Vulkan::Headers) target_compile_options(d3d12_set0_compute_test PRIVATE /W4 /permissive- /utf-8) add_test(NAME d3d12_set0_compute COMMAND d3d12_set0_compute_test) set_tests_properties(d3d12_set0_compute PROPERTIES SKIP_RETURN_CODE 77) + add_test(NAME d3d12_placed_texture_uav_off COMMAND d3d12_set0_compute_test) + set_tests_properties(d3d12_placed_texture_uav_off PROPERTIES SKIP_RETURN_CODE 77 ENVIRONMENT LEONIDA_PLACED_TEXTURE_UAV=0) # Bindless table churn on a direct and a compute queue: the table grows and live keys move without a # device-idle wait (D3D-314); the same shader, run twice (deferred retirement, and the old wait for timing). add_executable(d3d12_bindless_churn_test test/d3d12_bindless_churn_test.cpp shims/d3d12_xs_device.cpp diff --git a/runtime/shims/d3d12_com_object.h b/runtime/shims/d3d12_com_object.h index c7d46d775f..49133eb1b6 100644 --- a/runtime/shims/d3d12_com_object.h +++ b/runtime/shims/d3d12_com_object.h @@ -26,7 +26,7 @@ class Object : public Stubs { public: ULONG STDMETHODCALLTYPE AddRef() override { return ++refs_; } ULONG STDMETHODCALLTYPE Release() override { - const ULONG left = --refs_; + const ULONG left = drop_reference(); if (!left) delete static_cast(this); return left; } @@ -120,6 +120,9 @@ class Object : public Stubs { } protected: + // An indexed resource removes its weak entry before deleting the last reference. + ULONG drop_reference() { return --refs_; } + virtual ~Object() { for (auto& entry : data_) if (entry.object) entry.object->Release(); diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 97ed334a11..6dd04ebc2b 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -51,6 +51,7 @@ #include #include #include +#include #include #include #include @@ -556,6 +557,8 @@ std::filesystem::path shader_directory_from_file(const std::filesystem::path& co } // What a device's children need from it, kept apart from the COM class so children can hold a plain pointer. +class Resource; + struct DeviceState { VulkanDeviceInfo info; std::vector families; @@ -1064,6 +1067,10 @@ struct DeviceState { std::uint64_t address; }; std::unique_ptr bindless; + // Non-owning. The last Release removes its entry under the same lock used to acquire a view reference. + using TexturePlacements = std::multimap; + std::mutex placement_mutex; + TexturePlacements texture_placements; std::mutex descriptor_mutex; // the set and the containers below // vkDeviceWaitIdle calls made for the bindless set (none, unless LEONIDA_BINDLESS_IDLE_WAIT restores the old wait). std::atomic bindless_idle_waits{0}; @@ -1790,6 +1797,18 @@ class Resource final D3D12_HEAP_FLAGS heap_flags, VkDeviceSize placed_va) : device_(device), desc_(desc), heap_(heap), heap_flags_(heap_flags), placed_(true), va_(placed_va) {} + ULONG STDMETHODCALLTYPE Release() override { + if (!placement_entry_) return Child::Release(); + { + std::lock_guard lock(device_.placement_mutex); + const ULONG left = drop_reference(); + if (left) return left; + device_.texture_placements.erase(*placement_entry_); + } + delete this; + return 0; + } + ~Resource() override { if (accel_) { device_.context->unregister_acceleration_structure(gpu_va_); @@ -2315,7 +2334,13 @@ class Resource final // The title's own address for a texture Xbox slot 133 placed. The image is still the committed fallback, so its // pixels do not alias the title's pages yet; the address is what GetGPUVirtualAddress answers (XCL PR #168's // behavior, measured on a retail title). - void set_title_address(D3D12_GPU_VIRTUAL_ADDRESS address) { va_ = address; } + void set_title_address(D3D12_GPU_VIRTUAL_ADDRESS address) { + va_ = address; + if (address) { + std::lock_guard lock(device_.placement_mutex); + placement_entry_ = device_.texture_placements.emplace(address, this); + } + } // // The node masks read back as the one node of a single adapter (0 becomes 1), and the heap flags as the runtime @@ -2615,6 +2640,7 @@ class Resource final std::vector view_formats_; // creation-time castable list; empty means no restriction std::vector castable_formats_; // the same list as the application gave it VkFormat uint_backed_ = VK_FORMAT_UNDEFINED; + std::optional placement_entry_; std::uint64_t image_address_ = 0; // what a T# names the texture by bool rgb96_ = false; VkImageAspectFlags aspects_ = 0; @@ -5012,6 +5038,7 @@ class Device final // Every Vulkan device serves Xbox device slot 133, whichever entry made it: create_vulkan_device and the // 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_texture_view_handler(&write_placed_view); rt::d3d12_xs::set_placed_at_va_handler(&create_buffer_at_host_address); rt::d3d12_xs::set_console_descriptor_heap_handler(&rt::d3d12::create_console_descriptor_heap); state_.info = info; @@ -7136,6 +7163,71 @@ class Device final } } + static bool write_placed_view(ID3D12Device* real, bool unordered, const D3D12_RESOURCE_DESC& desc, + const std::uint32_t* words, UINT64 address, D3D12_CPU_DESCRIPTOR_HANDLE dest) { + auto* device = Device::from(real); + if (!device) return false; + static const bool allow_uav = [] { + const char* setting = std::getenv("LEONIDA_PLACED_TEXTURE_UAV"); + return !setting || std::strcmp(setting, "0") != 0; + }(); + if (unordered && !allow_uav) return false; + + auto release = [](Resource* r) { r->Release(); }; + std::unique_ptr resource(nullptr, release); + { + std::lock_guard lock(device->state_.placement_mutex); + auto [first, end] = device->state_.texture_placements.equal_range(address); + while (end != first) { + Resource* candidate = (--end)->second; + const auto& r = candidate->desc(); + if (r.Dimension != desc.Dimension || r.Width != desc.Width || r.Height != desc.Height || + r.DepthOrArraySize != desc.DepthOrArraySize || (desc.MipLevels && r.MipLevels != desc.MipLevels) || + r.SampleDesc.Count != desc.SampleDesc.Count || + (r.Format != desc.Format && !candidate->lists_castable_format(desc.Format))) continue; + if (unordered ? !(r.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS) + : (r.Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE)) continue; + candidate->AddRef(); + resource.reset(candidate); + break; + } + } + if (!resource) return false; + if (unordered) { + D3D12_UNORDERED_ACCESS_VIEW_DESC uav{}; + std::memcpy(&uav, words, sizeof(uav)); + if (uav.ViewDimension == D3D12_UAV_DIMENSION_TEXTURE3D) { + const auto& volume = uav.Texture3D; + const auto& r = resource->desc(); + if (r.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE3D || volume.MipSlice >= r.MipLevels) return false; + const UINT depth = std::max(1u, UINT(r.DepthOrArraySize) >> volume.MipSlice); + if (volume.FirstWSlice || (volume.WSize != UINT_MAX && volume.WSize != depth)) return false; + TextureView view{}; + view.type = leonida::gfx10::ImageType::image_3d; + view.format = uav.Format; + view.first_mip = volume.MipSlice; + view.mip_count = 1; + std::memset(reinterpret_cast(dest.ptr), 0, kDescriptorSize); + device->write_image_descriptor(*resource, view, dest, true); + device->state_.context->note_shader_image(resource->id()); + } else { + device->CreateUnorderedAccessView(resource.get(), nullptr, &uav, dest); + } + } else { + D3D12_SHADER_RESOURCE_VIEW_DESC srv{}; + std::memcpy(&srv, words, sizeof(srv)); + if (!srv.Shader4ComponentMapping) srv.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; + device->CreateShaderResourceView(resource.get(), &srv, dest); + } + leonida::gfx10::ImageWords key{}; + std::memcpy(key.data(), reinterpret_cast(dest.ptr), sizeof(key)); + if (!leonida::gfx10::is_image(key)) return false; + std::lock_guard lock(device->state_.descriptor_mutex); + if (!device->state_.bindless) return true; + const auto it = device->state_.image_entries.find(key); + return it != device->state_.image_entries.end() && (!unordered || it->second.storage); + } + HRESULT create_at_host_address(void* address, const D3D12_RESOURCE_DESC& desc, std::uint32_t layout, REFIID riid, void** out) { constexpr auto method = "Xbox CreatePlacedResourceX (device slot 133)"; diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index 85aece0bd2..dae22e425e 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -51,6 +51,7 @@ 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 g_placed_texture_view{nullptr}; std::atomic 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"); @@ -167,6 +168,7 @@ const char* slot_name(unsigned slot) { case 62: return "Xbox CreateComputePipelineState"; case 67: return "Xbox DeserializeComputePipelineStateX"; case 70: return "Xbox shader-resource view from raw words"; + case 71: return "Xbox unordered-access view from raw words"; case 73: return "Xbox CreateCommandListX -> PC CreateCommandList"; case 75: return "CreateCommandQueue1 -> queue proxy"; case 76: return "Xbox resource creation"; @@ -1574,24 +1576,43 @@ HRESULT proxy_create_command_queue_xbox(DeviceProxy* self, const std::uint32_t* 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); +// Slots 70/71 take a resource description and an SRV/UAV description followed by the placement address +// in dwords 10-11 (the console ABI also recorded by VIctory, victory-gpu/descriptor). The PC methods at +// these slots have different arguments. A backend without a matching image supplies a null descriptor. +void proxy_create_placed_view(DeviceProxy* self, bool unordered, std::uint64_t resource_words, + std::uint64_t view_words, std::uint64_t destination) { + const unsigned slot = unordered ? 71 : 70; + note_slot(slot); + log_xbox_route_once(slot, unordered ? "placed texture UAV" : "placed texture SRV", + resource_words, view_words, 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}); + const D3D12_CPU_DESCRIPTOR_HANDLE dest{destination}; + if (const auto handler = g_placed_texture_view.load(std::memory_order_acquire)) { + D3D12_RESOURCE_DESC resource{}; + std::uint32_t view[12]{}; + if (ReadProcessMemory(GetCurrentProcess(), reinterpret_cast(resource_words), + &resource, sizeof(resource), nullptr) && + resource.Dimension >= D3D12_RESOURCE_DIMENSION_TEXTURE1D && + resource.Dimension <= D3D12_RESOURCE_DIMENSION_TEXTURE3D && + ReadProcessMemory(GetCurrentProcess(), reinterpret_cast(view_words), + view, sizeof(view), nullptr)) { + const auto address = UINT64(view[10]) | (UINT64(view[11]) << 32); + if (address && handler(self->real, unordered, resource, view, address, dest)) return; + } + } + if (unordered) { + D3D12_UNORDERED_ACCESS_VIEW_DESC null_view{}; + null_view.Format = DXGI_FORMAT_R32_UINT; + null_view.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D; + self->real->CreateUnorderedAccessView(nullptr, nullptr, &null_view, dest); + } else { + 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, dest); + } } // Device slot 75: ID3D12Device9::CreateCommandQueue1(desc, creator_id, riid, out), called by slot index. @@ -2106,8 +2127,8 @@ 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); + } else if constexpr (Slot == 70 || Slot == 71) { + proxy_create_placed_view(self, Slot == 71, a1, a2, a3); return 0; } else if constexpr (Slot == 76) { return hr_to_u64(proxy_create_resource( @@ -2357,6 +2378,10 @@ unsigned device_proxy_slot_count(const void* device) { return static_cast(device)->slot_count; } +void set_placed_texture_view_handler(PlacedTextureViewHandler handler) { + g_placed_texture_view.store(handler, std::memory_order_release); +} + void set_placed_at_va_handler(PlacedAtVaHandler handler) { g_placed_at_va.store(handler, std::memory_order_release); } @@ -2386,7 +2411,7 @@ ID3D12Device* device_proxy_real_device(const void* device) { bool device_slot_overridden(unsigned slot) { if (slot <= 2 || slot == 8 || slot == 9 || slot == 14 || slot == 25 || slot == 26 || slot == 31 || slot == 32 || slot == 34 || slot == 35 || slot == 36 || slot == 45 || slot == 46 || slot == 50 || slot == 62 || slot == 67 || - slot == 70 || slot == 75 || slot == 76 || slot == 133 || slot == kCreateCommittedResource3X) + slot == 70 || slot == 71 || 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 b7f020bebe..c43db2887a 100644 --- a/runtime/shims/d3d12_xs_device.h +++ b/runtime/shims/d3d12_xs_device.h @@ -11,10 +11,8 @@ // 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. +// slots 70/71 (resource desc, view words, destination handle): create an SRV/UAV of the matching live +// texture placed at the address in view dwords 10-11. An unavailable image gives a null view. // 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 @@ -105,6 +103,11 @@ unsigned device_proxy_slot_count(const void* device); // The PC device behind a proxy. Null when `device` is not a proxy. ID3D12Device* device_proxy_real_device(const void* device); +// A placed texture view names its resource by address and description. False selects the null view. +using PlacedTextureViewHandler = bool (*)(ID3D12Device*, bool unordered, const D3D12_RESOURCE_DESC&, + const std::uint32_t* view, UINT64 address, D3D12_CPU_DESCRIPTOR_HANDLE); +void set_placed_texture_view_handler(PlacedTextureViewHandler handler); + // Device slot 133 (the console's CreatePlacedResourceX) makes a buffer on the title's pages at `va`. The proxy does // not create it itself: the device behind it does, through this handler, which the Vulkan layer registers // (rt::d3d12::create_buffer_at_host_address). With none registered, or a PC device behind the proxy, the slot diff --git a/runtime/test/d3d12_set0_compute_test.cpp b/runtime/test/d3d12_set0_compute_test.cpp index c2dfdfc397..670dd95955 100644 --- a/runtime/test/d3d12_set0_compute_test.cpp +++ b/runtime/test/d3d12_set0_compute_test.cpp @@ -21,10 +21,12 @@ #include "d3d12_test_shaders.h" #include "../shims/d3d12_com.h" #include "../shims/d3d12_xs_device.h" +#include "../src/gfx10_descriptors.h" #include "../src/shader_library.h" #include #include +#include #include #include #include @@ -53,6 +55,82 @@ Texel gradient(UINT x, UINT y) { // The second texture: the first one point-reflected, so no texel of it is in the same place. Texel reflected(UINT x, UINT y) { return gradient(texture_size - 1 - x, texture_size - 1 - y); } +// Exercise the console ABI: slot 133 publishes a placement and slots 70/71 receive descriptions +// plus an address, with no resource pointer. The address itself is opaque to these GPU-only tests. +HRESULT place(ID3D12Device* dev, void* address, const D3D12_RESOURCE_DESC& desc, UINT layout, ID3D12Resource** out) { + using Fn = HRESULT(WINAPI*)(ID3D12Device*, void*, const D3D12_RESOURCE_DESC*, UINT, const void*, REFIID, void**); + return reinterpret_cast(slot(dev, 133))(dev, address, &desc, layout, nullptr, IID_PPV_ARGS(out)); +} + +template +void placed_view(ID3D12Device* dev, UINT method, const D3D12_RESOURCE_DESC& desc, const View& view, + const void* address, D3D12_CPU_DESCRIPTOR_HANDLE dest) { + std::array words{}; + static_assert(sizeof(View) <= 10 * sizeof(UINT)); + std::memcpy(words.data(), &view, sizeof(view)); + std::memcpy(words.data() + 10, &address, sizeof(address)); + using Fn = void(WINAPI*)(ID3D12Device*, const D3D12_RESOURCE_DESC*, const void*, D3D12_CPU_DESCRIPTOR_HANDLE); + reinterpret_cast(slot(dev, method))(dev, &desc, words.data(), dest); +} + +int check_placed_uavs(ID3D12Device* device) { + alignas(256) std::byte address{}; + D3D12_RESOURCE_DESC array{}; + array.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + array.Width = array.Height = 8; + array.DepthOrArraySize = 6; + array.MipLevels = array.SampleDesc.Count = 1; + array.Format = DXGI_FORMAT_R32_UINT; + array.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; + auto face = array; + face.DepthOrArraySize = 1; + Ref older, newer; + EXPECT(SUCCEEDED(place(device, &address, array, 3, &older.p))); + EXPECT(SUCCEEDED(place(device, &address, face, 3, &newer.p))); + + D3D12_UNORDERED_ACCESS_VIEW_DESC view{}; + view.Format = array.Format; + view.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2DARRAY; + view.Texture2DArray.FirstArraySlice = 5; + view.Texture2DArray.ArraySize = 1; + using Words = std::array; + Words expected{}, got{}; + const D3D12_CPU_DESCRIPTOR_HANDLE dest{reinterpret_cast(got.data())}; + device->CreateUnorderedAccessView(older.p, nullptr, &view, {reinterpret_cast(expected.data())}); + EXPECT(expected != Words{}); + const char* value = std::getenv("LEONIDA_PLACED_TEXTURE_UAV"); + const bool enabled = !value || std::strcmp(value, "0") != 0; + placed_view(device, 71, array, view, &address, dest); + EXPECT(got == (enabled ? expected : Words{})); + older.reset(); + got.fill(~0u); + placed_view(device, 71, array, view, &address, dest); + EXPECT(got == Words{}); // The remaining single-layer image cannot supply layer 5. + + auto volume = face; + volume.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE3D; + volume.DepthOrArraySize = 8; + volume.MipLevels = 2; + Ref volume_target; + EXPECT(SUCCEEDED(place(device, &address, volume, 3, &volume_target.p))); + view.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE3D; + view.Texture3D = {1, 0, 4}; + placed_view(device, 71, volume, view, &address, dest); + leonida::gfx10::Image decoded{}; + if (enabled) { + EXPECT(leonida::gfx10::decode(got, decoded) && decoded.type == leonida::gfx10::ImageType::image_3d && + decoded.first_mip == 1 && decoded.mip_count == 1); + } else { + EXPECT(got == Words{}); + } + view.Texture3D.WSize = 2; + got.fill(~0u); + placed_view(device, 71, volume, view, &address, dest); + EXPECT(got == Words{}); + std::printf("placed UAVs: %u matching views, %u null controls\n", enabled ? 2 : 0, enabled ? 2 : 4); + return 0; +} + } // namespace int main() { @@ -163,10 +241,12 @@ int main() { texture_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; texture_desc.SampleDesc.Count = 1; Ref textures[2], upload, output, readback; - for (auto& texture : textures) - EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &texture_desc, - D3D12_RESOURCE_STATE_COPY_DEST, nullptr, - IID_PPV_ARGS(&texture.p)))); + EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &texture_desc, + D3D12_RESOURCE_STATE_COPY_DEST, nullptr, + IID_PPV_ARGS(&textures[0].p)))); + alignas(256) std::byte placement{}; + EXPECT(SUCCEEDED(place(device.p, &placement, texture_desc, 8, &textures[1].p))); + EXPECT(check_placed_uavs(device.p) == 0); // The upload buffer: texture A at 0, texture B at 2048 (8 rows of the 256-byte pitch). constexpr UINT64 b_offset = 2048; const auto upload_desc = buffer_desc(4096); @@ -219,8 +299,9 @@ int main() { srv.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; srv.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; srv.Texture2D = {0, 1, 0, 0.0f}; - device->CreateShaderResourceView(textures[i].p, &srv, - D3D12_CPU_DESCRIPTOR_HANDLE{SIZE_T(view_cpu + i * view_step)}); + const D3D12_CPU_DESCRIPTOR_HANDLE dest{SIZE_T(view_cpu + i * view_step)}; + if (i == 0) device->CreateShaderResourceView(textures[i].p, &srv, dest); + else placed_view(device.p, 70, texture_desc, srv, &placement, dest); } // Slot 2: bytes no view was made for. The hash table has no entry for them. std::uint32_t unknown[8]; diff --git a/runtime/test/xs_proxy_mock_test.cpp b/runtime/test/xs_proxy_mock_test.cpp index 888bedf244..0fdbe8ba04 100644 --- a/runtime/test/xs_proxy_mock_test.cpp +++ b/runtime/test/xs_proxy_mock_test.cpp @@ -1070,7 +1070,9 @@ int run_env_slots(bool valid) { 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, 81, 0, 0, 0, 0)) == S_FALSE); // the default GetFrameStatisticsX slot, unattached - EXPECT(hr_of(raw(device, 71)) == S_OK && mock->calls[71] == 1); // not an Xbox slot: forwarded + // Slot 71 is an Xbox route now (a placed texture UAV), so the probe is the next PC slot. + EXPECT(!xs::device_slot_overridden(72)); + EXPECT(hr_of(raw(device, 72)) == S_OK && mock->calls[72] == 1); // not an Xbox slot: forwarded } EXPECT(release(device) == 0 && mock->refs == 0); std::printf("%s: environment slot overrides %s\n", g_label, @@ -1162,17 +1164,17 @@ int check_resource_allocation_info() { return 0; } -// ---- 8. Device slot 70: a view from raw words, never the PC CreatePlacedResource1 ---- +// ---- 8. Device slots 70/71: raw views, including the fallback on a plain PC device ---- -int check_slot70() { +int check_placed_views() { // 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)); + EXPECT(device && xs::device_slot_overridden(70) && xs::device_slot_overridden(71)); 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::uint32_t words[14] = {0x1000, 0, 0, 0, 0, 0, 0, 0}; + const std::uint32_t view[16] = {}; 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); @@ -1183,6 +1185,12 @@ int check_slot70() { // No destination, no write. raw(device, 70, word(words), word(view), 0); EXPECT(mock->calls[70] == 0 && mock->calls[18] == 1); + raw(device, 71, word(words), word(view), destination); + EXPECT(mock->calls[71] == 0 && xs::device_slot_hits(71) == 1); + EXPECT(mock->calls[19] == 1 && mock->args[19][0] == 0 && mock->args[19][1] == 0 && + mock->args[19][2] != 0 && mock->args[19][3] == destination); + raw(device, 71, word(words), word(view), 0); + EXPECT(mock->calls[19] == 1); EXPECT(release(device) == 0 && mock->refs == 0); } return 0; @@ -1717,7 +1725,7 @@ int main(int argc, char** argv) { 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() || + check_resource_allocation_info() || check_placed_views() || check_committed_resource3_x() || check_presentation_wiring()) return 1; std::printf("xs_proxy_mock_test: PASS (%d allocations refused on purpose)\n", g_failed_news); From c4afec17ec6feaa00400482c8b5e47c5a96240b4 Mon Sep 17 00:00:00 2001 From: iFeyz <62035583+iFeyz@users.noreply.github.com> Date: Tue, 24 Nov 2026 20:22:47 +0100 Subject: [PATCH 2/2] RT-TITLE-BUFFER-VIEWS: Xbox device slots 70/71 write buffer views from a buffer description and its address DadaCap measured that slots 70 (SRV) and 71 (UAV) also make buffer views, and that the null descriptor they got left the instance-culling dispatches reading leftover memory as a V# until the device was lost (issue #14 comment 7140195321). A buffer description now reaches #1079's placed-view handler, which writes the V# of a view of Width bytes at the address (taken as a GPU address, as a CBV's BufferLocation is). The V# comes from the code CreateShaderResourceView / CreateUnorderedAccessView use for a buffer resource, factored out as write_buffer_srv_at / write_buffer_uav_at, so the view kinds and bounds are the PC methods'. A refused view or a UAV counter offset keeps the null view. d3d12_set0_compute_test compares six views with the PC methods' and checks three null controls. --- docs/d3d12_vulkan.md | 9 ++ runtime/shims/d3d12_vulkan_device.cpp | 182 ++++++++++++++--------- runtime/shims/d3d12_xs_device.cpp | 7 +- runtime/shims/d3d12_xs_device.h | 3 +- runtime/test/d3d12_set0_compute_test.cpp | 76 ++++++++++ 5 files changed, 199 insertions(+), 78 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 53debf15a4..6f6f9663e5 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -673,6 +673,15 @@ side inwards: several live resources share an address. UAVs require writable backing. A 3D UAV can select one complete mip. A missing placement or unsupported view gives a null descriptor. Set `LEONIDA_PLACED_TEXTURE_UAV=0` to restore null placed UAVs. + - With a buffer description, slots 70 and 71 write a buffer view (RT-TITLE-BUFFER-VIEWS): the buffer is the + description's `Width` bytes at the address, taken as a GPU address as a constant-buffer view's + `BufferLocation` is, and the V# is the one `CreateShaderResourceView` / `CreateUnorderedAccessView` write for a + buffer resource there (raw, structured or typed, same bounds; the shared `write_buffer_srv_at` / + `write_buffer_uav_at`). A view those refuse, or a UAV with a counter offset (no counter resource is named), + gives the null descriptor. The retail title passes typed `R32_UINT` and structured (stride 16 and 32) views of + small buffers through slot 71 with flags `0x20004`; with the null descriptor, the instance-culling dispatches + read leftover memory as a V# and the device is lost (issue #14, comment 7140195321). `d3d12_set0_compute_test` + compares six views with the PC methods' and checks three null controls. - A texture is named in its T#s by an address of its own, above 4 GiB and 256 bytes apart, that nothing dereferences; its entries leave the set and the table when it is destroyed. - `VulkanDeviceInfo::bindless` says the Vulkan device has the features this needs: `runtimeDescriptorArray`, diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 6dd04ebc2b..fd3afafd0a 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -6444,6 +6444,80 @@ class Device final std::memcpy(reinterpret_cast(destination.ptr), words.data(), sizeof(words)); } + // The V# of a shader-resource view of the buffer of `width` bytes at GPU address `base_va`: from a resource + // (CreateShaderResourceView), or from Xbox device slot 70's description and address (write_placed_view), which + // name no resource object. Raw, structured or typed; a description it refuses leaves the slot as it was. + void write_buffer_srv_at(UINT64 base_va, UINT64 width, const D3D12_SHADER_RESOURCE_VIEW_DESC* desc, + D3D12_CPU_DESCRIPTOR_HANDLE destination) { + if (!desc) { + log_once("buffer SRV requires a description; width", int(width)); + return; + } + if (desc->ViewDimension != D3D12_SRV_DIMENSION_BUFFER) { + log_once("shader-resource view dimension not supported for buffer:", desc->ViewDimension); + return; + } + if (!base_va || !desc->Buffer.NumElements || (desc->Buffer.Flags & ~D3D12_BUFFER_SRV_FLAG_RAW)) return; + leonida::gfx10::Buffer buffer{}; + if (desc->Buffer.Flags & D3D12_BUFFER_SRV_FLAG_RAW) { + if (desc->Format != DXGI_FORMAT_R32_TYPELESS || desc->Buffer.StructureByteStride != 0) return; + if (desc->Buffer.FirstElement > width / 4) return; + const UINT64 byte_off = desc->Buffer.FirstElement * 4; + const UINT64 byte_size = UINT64(desc->Buffer.NumElements) * 4; + if (byte_size > width - byte_off || byte_size > UINT32_MAX) return; + buffer.address = base_va + byte_off; + buffer.stride = 0; + buffer.num_records = std::uint32_t(byte_size); + buffer.format = leonida::gfx10::format_from_vulkan(VK_FORMAT_R32_UINT); + buffer.oob_select = leonida::gfx10::OobSelect::raw; + } else if (desc->Buffer.StructureByteStride != 0) { + if (desc->Format != DXGI_FORMAT_UNKNOWN || desc->Buffer.StructureByteStride > 16383) return; + const UINT64 stride = desc->Buffer.StructureByteStride; + if (desc->Buffer.FirstElement > width / stride) return; + const UINT64 byte_off = desc->Buffer.FirstElement * stride; + const UINT64 byte_size = UINT64(desc->Buffer.NumElements) * stride; + if (byte_size > width - byte_off) return; + buffer.address = base_va + byte_off; + buffer.stride = std::uint32_t(stride); + buffer.num_records = desc->Buffer.NumElements; + buffer.format = leonida::gfx10::format_from_vulkan(VK_FORMAT_R32_UINT); + buffer.oob_select = leonida::gfx10::OobSelect::structured; + } else { + const Format fmt = vulkan_format(desc->Format, false); + if (fmt.format == VK_FORMAT_UNDEFINED || fmt.typeless || !(fmt.aspects & VK_IMAGE_ASPECT_COLOR_BIT)) { + log_once("buffer SRV format not supported:", int(desc->Format)); + return; + } + const VkFormat named = fmt.rgb96 ? rgb96_named_format(fmt.format) : fmt.format; + const std::uint32_t code = leonida::gfx10::format_from_vulkan(named); + const leonida::BlockFormat bf = footprint_block(fmt, VK_IMAGE_ASPECT_COLOR_BIT); + if (!code || bf.block != 1 || !bf.bytes || bf.bytes > 16383) { + log_once("buffer SRV format not supported:", int(desc->Format)); + return; + } + const UINT64 stride = bf.bytes; + if (desc->Buffer.FirstElement > width / stride) return; + const UINT64 byte_off = desc->Buffer.FirstElement * stride; + const UINT64 byte_size = UINT64(desc->Buffer.NumElements) * stride; + if (byte_size > width - byte_off) return; + buffer.address = base_va + byte_off; + buffer.stride = std::uint32_t(stride); + buffer.num_records = desc->Buffer.NumElements; + buffer.format = code; + const leonida::gfx10::Swizzle channels = leonida::gfx10::format_swizzle(named); + for (unsigned i = 0; i < 4; ++i) { + const UINT from = D3D12_DECODE_SHADER_4_COMPONENT_MAPPING(i, desc->Shader4ComponentMapping); + buffer.swizzle[i] = from < 4 ? channels[from] + : from == D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_0 + ? leonida::gfx10::select_zero + : leonida::gfx10::select_one; + } + buffer.oob_select = leonida::gfx10::OobSelect::structured; + } + const leonida::gfx10::BufferWords words = leonida::gfx10::encode(buffer); + std::memcpy(reinterpret_cast(destination.ptr), words.data(), sizeof(words)); + } + // A shader-resource view of a texture is a T# in the slot (docs section 3.4): the shader loads those eight // dwords through its descriptor table and finds the image by them. A buffer SRV writes a 16-byte GFX10 V# in the // first half of the slot (raw, structured, or typed). A slot left as zeros is a null descriptor. @@ -6463,78 +6537,9 @@ class Device final log_once("shader-resource view denied by resource flags; format", int(r->desc().Format)); return; } - if (!r->image()) { - if (!desc) { - log_once("buffer SRV requires a description; width", int(r->desc().Width)); - return; - } - if (desc->ViewDimension != D3D12_SRV_DIMENSION_BUFFER) { - log_once("shader-resource view dimension not supported for buffer:", desc->ViewDimension); - return; - } - const UINT64 base_va = const_cast(r)->GetGPUVirtualAddress(); - const UINT64 width = r->desc().Width; - if (!base_va || !desc->Buffer.NumElements || (desc->Buffer.Flags & ~D3D12_BUFFER_SRV_FLAG_RAW)) return; - leonida::gfx10::Buffer buffer{}; - if (desc->Buffer.Flags & D3D12_BUFFER_SRV_FLAG_RAW) { - if (desc->Format != DXGI_FORMAT_R32_TYPELESS || desc->Buffer.StructureByteStride != 0) return; - if (desc->Buffer.FirstElement > width / 4) return; - const UINT64 byte_off = desc->Buffer.FirstElement * 4; - const UINT64 byte_size = UINT64(desc->Buffer.NumElements) * 4; - if (byte_size > width - byte_off || byte_size > UINT32_MAX) return; - buffer.address = base_va + byte_off; - buffer.stride = 0; - buffer.num_records = std::uint32_t(byte_size); - buffer.format = leonida::gfx10::format_from_vulkan(VK_FORMAT_R32_UINT); - buffer.oob_select = leonida::gfx10::OobSelect::raw; - } else if (desc->Buffer.StructureByteStride != 0) { - if (desc->Format != DXGI_FORMAT_UNKNOWN || desc->Buffer.StructureByteStride > 16383) return; - const UINT64 stride = desc->Buffer.StructureByteStride; - if (desc->Buffer.FirstElement > width / stride) return; - const UINT64 byte_off = desc->Buffer.FirstElement * stride; - const UINT64 byte_size = UINT64(desc->Buffer.NumElements) * stride; - if (byte_size > width - byte_off) return; - buffer.address = base_va + byte_off; - buffer.stride = std::uint32_t(stride); - buffer.num_records = desc->Buffer.NumElements; - buffer.format = leonida::gfx10::format_from_vulkan(VK_FORMAT_R32_UINT); - buffer.oob_select = leonida::gfx10::OobSelect::structured; - } else { - const Format fmt = vulkan_format(desc->Format, false); - if (fmt.format == VK_FORMAT_UNDEFINED || fmt.typeless || !(fmt.aspects & VK_IMAGE_ASPECT_COLOR_BIT)) { - log_once("buffer SRV format not supported:", int(desc->Format)); - return; - } - const VkFormat named = fmt.rgb96 ? rgb96_named_format(fmt.format) : fmt.format; - const std::uint32_t code = leonida::gfx10::format_from_vulkan(named); - const leonida::BlockFormat bf = footprint_block(fmt, VK_IMAGE_ASPECT_COLOR_BIT); - if (!code || bf.block != 1 || !bf.bytes || bf.bytes > 16383) { - log_once("buffer SRV format not supported:", int(desc->Format)); - return; - } - const UINT64 stride = bf.bytes; - if (desc->Buffer.FirstElement > width / stride) return; - const UINT64 byte_off = desc->Buffer.FirstElement * stride; - const UINT64 byte_size = UINT64(desc->Buffer.NumElements) * stride; - if (byte_size > width - byte_off) return; - buffer.address = base_va + byte_off; - buffer.stride = std::uint32_t(stride); - buffer.num_records = desc->Buffer.NumElements; - buffer.format = code; - const leonida::gfx10::Swizzle channels = leonida::gfx10::format_swizzle(named); - for (unsigned i = 0; i < 4; ++i) { - const UINT from = D3D12_DECODE_SHADER_4_COMPONENT_MAPPING(i, desc->Shader4ComponentMapping); - buffer.swizzle[i] = from < 4 ? channels[from] - : from == D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_0 - ? leonida::gfx10::select_zero - : leonida::gfx10::select_one; - } - buffer.oob_select = leonida::gfx10::OobSelect::structured; - } - const leonida::gfx10::BufferWords words = leonida::gfx10::encode(buffer); - std::memcpy(reinterpret_cast(destination.ptr), words.data(), sizeof(words)); - return; - } + if (!r->image()) + return write_buffer_srv_at(const_cast(r)->GetGPUVirtualAddress(), r->desc().Width, desc, + destination); TextureView view{}; if (!desc) { const bool array = r->desc().DepthOrArraySize > 1; @@ -6622,8 +6627,15 @@ class Device final // counter resource's address plus CounterOffsetInBytes. Append and consume are atomics a shader does on it. void write_buffer_uav(ID3D12Resource* resource, ID3D12Resource* counter, const D3D12_UNORDERED_ACCESS_VIEW_DESC* desc, D3D12_CPU_DESCRIPTOR_HANDLE destination) { + write_buffer_uav_at(Resource::from(resource)->desc(), resource->GetGPUVirtualAddress(), counter, desc, + destination); + } + + // The same V# for the buffer `rd` at GPU address `va`: from a resource above, or from Xbox device slot 71's + // description and address (write_placed_view), which name no resource object. + void write_buffer_uav_at(const D3D12_RESOURCE_DESC& rd, UINT64 va, ID3D12Resource* counter, + const D3D12_UNORDERED_ACCESS_VIEW_DESC* desc, D3D12_CPU_DESCRIPTOR_HANDLE destination) { namespace g = leonida::gfx10; - const D3D12_RESOURCE_DESC& rd = Resource::from(resource)->desc(); if (!(rd.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS)) { log_once("unordered-access view of a buffer created without ALLOW_UNORDERED_ACCESS; flags", int(rd.Flags)); return; @@ -6677,7 +6689,6 @@ class Device final return; } } - const UINT64 va = resource->GetGPUVirtualAddress(); const UINT64 counter_va = counter ? counter->GetGPUVirtualAddress() : 0; if (!va || (counter && !counter_va)) return; g::Buffer buffer{}; @@ -7167,6 +7178,29 @@ class Device final const std::uint32_t* words, UINT64 address, D3D12_CPU_DESCRIPTOR_HANDLE dest) { auto* device = Device::from(real); if (!device) return false; + // A buffer view (RT-TITLE-BUFFER-VIEWS): the title's buffer is the `Width` bytes at `address`, which the layer + // takes as a GPU address as it takes a constant-buffer view's BufferLocation. The V# is the one + // CreateShaderResourceView / CreateUnorderedAccessView write for a buffer resource there, with the same + // bounds; a view they refuse (or a UAV with a counter offset, which names no counter resource here) gives + // the caller's null descriptor. Measured on the retail title: typed R32_UINT and structured (stride 16, 32) + // views of small buffers through slot 71 (issue #14, comment 7140195321). + if (desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER) { + if (!desc.Width) return false; + std::memset(reinterpret_cast(dest.ptr), 0, kDescriptorSize); + if (unordered) { + D3D12_UNORDERED_ACCESS_VIEW_DESC uav{}; + std::memcpy(&uav, words, sizeof(uav)); + device->write_buffer_uav_at(desc, address, nullptr, &uav, dest); + } else { + D3D12_SHADER_RESOURCE_VIEW_DESC srv{}; + std::memcpy(&srv, words, sizeof(srv)); + if (!srv.Shader4ComponentMapping) srv.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; + device->write_buffer_srv_at(address, desc.Width, &srv, dest); + } + leonida::gfx10::BufferWords written{}; + std::memcpy(written.data(), reinterpret_cast(dest.ptr), sizeof(written)); + return written != leonida::gfx10::BufferWords{}; + } static const bool allow_uav = [] { const char* setting = std::getenv("LEONIDA_PLACED_TEXTURE_UAV"); return !setting || std::strcmp(setting, "0") != 0; diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index dae22e425e..3db21e0334 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -1578,12 +1578,13 @@ HRESULT proxy_create_command_queue_xbox(DeviceProxy* self, const std::uint32_t* // Slots 70/71 take a resource description and an SRV/UAV description followed by the placement address // in dwords 10-11 (the console ABI also recorded by VIctory, victory-gpu/descriptor). The PC methods at -// these slots have different arguments. A backend without a matching image supplies a null descriptor. +// these slots have different arguments. A backend without a matching image supplies a null descriptor. A buffer +// description goes to the same handler, which writes a buffer view of the address (RT-TITLE-BUFFER-VIEWS). void proxy_create_placed_view(DeviceProxy* self, bool unordered, std::uint64_t resource_words, std::uint64_t view_words, std::uint64_t destination) { const unsigned slot = unordered ? 71 : 70; note_slot(slot); - log_xbox_route_once(slot, unordered ? "placed texture UAV" : "placed texture SRV", + log_xbox_route_once(slot, unordered ? "placed view UAV" : "placed view SRV", resource_words, view_words, destination, 0, 0); if (!destination) return; const D3D12_CPU_DESCRIPTOR_HANDLE dest{destination}; @@ -1592,7 +1593,7 @@ void proxy_create_placed_view(DeviceProxy* self, bool unordered, std::uint64_t r std::uint32_t view[12]{}; if (ReadProcessMemory(GetCurrentProcess(), reinterpret_cast(resource_words), &resource, sizeof(resource), nullptr) && - resource.Dimension >= D3D12_RESOURCE_DIMENSION_TEXTURE1D && + resource.Dimension >= D3D12_RESOURCE_DIMENSION_BUFFER && resource.Dimension <= D3D12_RESOURCE_DIMENSION_TEXTURE3D && ReadProcessMemory(GetCurrentProcess(), reinterpret_cast(view_words), view, sizeof(view), nullptr)) { diff --git a/runtime/shims/d3d12_xs_device.h b/runtime/shims/d3d12_xs_device.h index c43db2887a..b15adc6663 100644 --- a/runtime/shims/d3d12_xs_device.h +++ b/runtime/shims/d3d12_xs_device.h @@ -103,7 +103,8 @@ unsigned device_proxy_slot_count(const void* device); // The PC device behind a proxy. Null when `device` is not a proxy. ID3D12Device* device_proxy_real_device(const void* device); -// A placed texture view names its resource by address and description. False selects the null view. +// A placed view (slots 70/71) names its resource by address and description: a texture placed by slot 133, or a +// buffer of that many bytes at that address (RT-TITLE-BUFFER-VIEWS). False selects the null view. using PlacedTextureViewHandler = bool (*)(ID3D12Device*, bool unordered, const D3D12_RESOURCE_DESC&, const std::uint32_t* view, UINT64 address, D3D12_CPU_DESCRIPTOR_HANDLE); void set_placed_texture_view_handler(PlacedTextureViewHandler handler); diff --git a/runtime/test/d3d12_set0_compute_test.cpp b/runtime/test/d3d12_set0_compute_test.cpp index 670dd95955..90d704090e 100644 --- a/runtime/test/d3d12_set0_compute_test.cpp +++ b/runtime/test/d3d12_set0_compute_test.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -131,6 +132,80 @@ int check_placed_uavs(ID3D12Device* device) { return 0; } +// RT-TITLE-BUFFER-VIEWS: slots 70/71 with a buffer description write the V# that CreateShaderResourceView / +// CreateUnorderedAccessView write for a buffer resource at the same address, the shapes the retail title passes +// (typed R32_UINT and structured stride 16 and 32 through slot 71, flags 0x20004); a view those refuse gives the null +// descriptor. +int check_placed_buffer_views(ID3D12Device* device) { + const D3D12_HEAP_PROPERTIES heap{D3D12_HEAP_TYPE_DEFAULT}; + const auto desc = buffer_desc(1024, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS); + Ref buffer; + EXPECT(SUCCEEDED(device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COMMON, + nullptr, IID_PPV_ARGS(&buffer.p)))); + const auto* address = reinterpret_cast(static_cast(buffer->GetGPUVirtualAddress())); + auto title = desc; + title.Flags = static_cast(0x20004); // as the retail title passes it: a bit beyond PC's + using Words = std::array; + unsigned matched = 0, nulls = 0; + const auto same = [&](const auto& view, bool unordered) { + Words expected{}, got{}; + got.fill(~0u); + const D3D12_CPU_DESCRIPTOR_HANDLE want{reinterpret_cast(expected.data())}; + if constexpr (std::is_same_v, D3D12_UNORDERED_ACCESS_VIEW_DESC>) + device->CreateUnorderedAccessView(buffer.p, nullptr, &view, want); + else + device->CreateShaderResourceView(buffer.p, &view, want); + placed_view(device, unordered ? 71 : 70, title, view, address, {reinterpret_cast(got.data())}); + EXPECT(expected != Words{} && got == expected); + ++matched; + return 0; + }; + const auto null = [&](const auto& view, bool unordered, const D3D12_RESOURCE_DESC& resource) { + Words got{}; + got.fill(~0u); + placed_view(device, unordered ? 71 : 70, resource, view, address, {reinterpret_cast(got.data())}); + EXPECT(got == Words{}); + ++nulls; + return 0; + }; + + D3D12_UNORDERED_ACCESS_VIEW_DESC uav{}; + uav.ViewDimension = D3D12_UAV_DIMENSION_BUFFER; + uav.Format = DXGI_FORMAT_R32_UINT; // typed + uav.Buffer = {4, 60, 0, 0, D3D12_BUFFER_UAV_FLAG_NONE}; + EXPECT(same(uav, true) == 0); + uav.Format = DXGI_FORMAT_UNKNOWN; // structured + for (UINT stride : {16u, 32u}) { + uav.Buffer = {1, 1024 / stride - 1, stride, 0, D3D12_BUFFER_UAV_FLAG_NONE}; + EXPECT(same(uav, true) == 0); + } + D3D12_SHADER_RESOURCE_VIEW_DESC srv{}; + srv.ViewDimension = D3D12_SRV_DIMENSION_BUFFER; + srv.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; + srv.Format = DXGI_FORMAT_R32_TYPELESS; // raw + srv.Buffer = {0, 256, 0, D3D12_BUFFER_SRV_FLAG_RAW}; + EXPECT(same(srv, false) == 0); + srv.Format = DXGI_FORMAT_UNKNOWN; // structured + srv.Buffer = {2, 30, 32, D3D12_BUFFER_SRV_FLAG_NONE}; + EXPECT(same(srv, false) == 0); + srv.Format = DXGI_FORMAT_R32_UINT; // typed + srv.Buffer = {0, 256, 0, D3D12_BUFFER_SRV_FLAG_NONE}; + EXPECT(same(srv, false) == 0); + + // Controls: past the buffer's end, a counter offset with no counter resource, and a UAV of a buffer whose + // description does not allow unordered access. + uav.Buffer = {1, 1024 / 16, 16, 0, D3D12_BUFFER_UAV_FLAG_NONE}; + EXPECT(null(uav, true, title) == 0); + uav.Buffer = {0, 4, 16, 4096, D3D12_BUFFER_UAV_FLAG_NONE}; + EXPECT(null(uav, true, title) == 0); + auto read_only = title; + read_only.Flags = D3D12_RESOURCE_FLAG_NONE; + uav.Buffer = {0, 4, 16, 0, D3D12_BUFFER_UAV_FLAG_NONE}; + EXPECT(null(uav, true, read_only) == 0); + std::printf("placed buffer views: %u matching views, %u null controls\n", matched, nulls); + return 0; +} + } // namespace int main() { @@ -247,6 +322,7 @@ int main() { alignas(256) std::byte placement{}; EXPECT(SUCCEEDED(place(device.p, &placement, texture_desc, 8, &textures[1].p))); EXPECT(check_placed_uavs(device.p) == 0); + EXPECT(check_placed_buffer_views(device.p) == 0); // The upload buffer: texture A at 0, texture B at 2048 (8 rows of the 256-byte pitch). constexpr UINT64 b_offset = 2048; const auto upload_desc = buffer_desc(4096);