Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,20 @@ 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.
- 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`,
Expand Down
2 changes: 2 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion runtime/shims/d3d12_com_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Derived*>(this);
return left;
}
Expand Down Expand Up @@ -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();
Expand Down
276 changes: 201 additions & 75 deletions runtime/shims/d3d12_vulkan_device.cpp

Large diffs are not rendered by default.

66 changes: 46 additions & 20 deletions runtime/shims/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<PlacedTextureViewHandler> g_placed_texture_view{nullptr};
std::atomic<ConsoleDescriptorHeapHandler> g_console_descriptor_heap{nullptr};
static_assert(kMaxSlots > 83, "the proxy table must cover ID3D12Device14 (83 slots)");
static_assert(sizeof(void*) == 8, "the Xbox-ABI proxy is x64-only");
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -1574,24 +1576,44 @@ 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. 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 view UAV" : "placed view 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<const void*>(resource_words),
&resource, sizeof(resource), nullptr) &&
resource.Dimension >= D3D12_RESOURCE_DIMENSION_BUFFER &&
resource.Dimension <= D3D12_RESOURCE_DIMENSION_TEXTURE3D &&
ReadProcessMemory(GetCurrentProcess(), reinterpret_cast<const void*>(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.
Expand Down Expand Up @@ -2106,8 +2128,8 @@ std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64
reinterpret_cast<const D3D12_RESOURCE_DESC*>(a2),
static_cast<std::uint32_t>(a3), reinterpret_cast<const IID*>(a5),
reinterpret_cast<void**>(a6)));
} else if constexpr (Slot == 70) {
proxy_create_srv_from_raw_x(self, a1, a2, a3);
} 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(
Expand Down Expand Up @@ -2357,6 +2379,10 @@ unsigned device_proxy_slot_count(const void* device) {
return static_cast<const DeviceProxy*>(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);
}
Expand Down Expand Up @@ -2386,7 +2412,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 ||
Expand Down
12 changes: 8 additions & 4 deletions runtime/shims/d3d12_xs_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -105,6 +103,12 @@ 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 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);

// 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
Expand Down
Loading
Loading