Skip to content
Merged
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
29 changes: 27 additions & 2 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -2263,6 +2263,30 @@ The trace will enumerate them.
Intel Raptor Lake-S (101.7092) and RTX 5060 Laptop (621.24) hosts wrap, two 2^63 buffers to 0, and WARP on that
PC still saturates. A wrapped total could make a caller allocate less than one of its resources, so the layer
keeps saturating, and the test accepts a host that wraps.
Behind the Xbox-ABI proxy (device slots 25 and 56) a 2D render target of a listed colour format with one sample is
padded to whole console tiles: the size is the sum over its mips (MipLevels 0 is the full chain, an odd extent
rounds up), each mip rounded up to the RDNA2 SW_64KB_D tile shape (`runtime/src/gfx10_tiling.h`: 256 x 128 pixels
for 2-byte elements, 128 x 128 for 4-byte, 128 x 64 for 8-byte, 64 x 64 for 16-byte), with the first mip that fits
in half a tile's width and a whole tile's height, and the mips after it, sharing one tail tile, times the array
size. A 1920 x 1080 RGBA8 target is 135 tiles, 8,847,360 bytes, where the PC answer is 8,323,072. The proxy lays
the descriptions out as the PC device does (each at its own alignment, the total rounded up to the largest
alignment, sums saturating) with each render target at the larger of its console size and its PC size; a 4 MiB
aligned multisampled neighbour therefore keeps its place (a 1 x 1 four-sample target and a 1080p target need 16 MiB,
not 12.5). Slot 56 reports the same offsets. The PC layout stands when no description grows
(`d3d12_xs_alloc_size.h`; `xs_proxy_mock_test`). **Assumption**: the
retail title's own sizes are not measured (#6982, stop 17), so that its allocation is these shapes and no DCC or
CMask memory is open until they are.
The descriptions the title passes to slot 25 carry console values (layout word `0x101`/`0x1`, flag bits above the PC
ones). For console-form calls or descriptors explicitly using layout `0x101`, the proxy strips Xbox-only flags and
translates layout `0x101` to ROW_MAJOR for buffers or UNKNOWN for textures. Other PC-form descriptors reach the host
unchanged for validation (`d3d12_xs_desc_clean.h`; #6982 section 3.7).
A committed resource made through slots 76 and 127 also allocates that size through the title's
`__XMemAllocationHooks__`, as the console runtime's `XMemAlloc` for the resource does, and frees it when the
resource is destroyed (a lease kept with `SetPrivateDataInterface`; the bytes are not used). The retail title
budgets its first full-screen render targets with such a hook: it bump-allocates the allocations whose XALLOC
page-size field is 2 out of a range it reserved and asserts that less than 2 MiB is left (RT-5 stop 17). The
lease's attribute word (allocator ID 0x80, page-size field 2, object type 0) is an **assumption**; the measured
hook reads only the page-size field.
- `GetAdapterLuid` (implemented): `VkPhysicalDeviceIDProperties::deviceLUID`, which on Windows is the DXGI
adapter's LUID; `d3d12_fence_timing_test` finds it with `IDXGIFactory4::EnumAdapterByLuid` (RTX 4090, driver
621.14). A driver that reports `deviceLUIDValid = VK_FALSE` gets a zero LUID, logged.
Expand All @@ -2279,8 +2303,9 @@ The trace will enumerate them.
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.
(`d3d12_xs_device.cpp`; `xs_proxy_mock_test`, `device_proxy_test`). Slot 43 returns a struct too and the
proxy still forwards it in the PC form: no call of slot 43 has been seen. Slots 25 and 56 take the same form by the
same rule (reported later in the boot, #6982; not measured), as #6973 first did for slot 25.
- 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.
Expand Down
11 changes: 11 additions & 0 deletions runtime/shims/d3d12_xs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,23 @@ HRESULT WINAPI create_versioned_root_signature_deserializer(const void* data, SI
return fn(data, size, iid, out);
}

void* xmem_descriptor_heap_alloc(std::size_t byte_size, std::uint64_t attributes) {
const rt::shims::XMemAllocationHooks* hooks = rt::shims::xmem_allocation_hooks();
return (hooks && hooks->alloc) ? hooks->alloc(byte_size, attributes) : nullptr;
}

void xmem_descriptor_heap_free(void* ptr, std::uint64_t attributes) {
const rt::shims::XMemAllocationHooks* hooks = rt::shims::xmem_allocation_hooks();
if (hooks && hooks->free) hooks->free(ptr, attributes);
}

// Measured shape: D3D12XboxCreateDevice(adapter, params, riid, out), where params is a 24-word block that is only
// logged and not used to create the device. The struct's total size is unknown, so the log reads 24 x 32-bit words
// (96 bytes) and no more. The PC D3D12CreateDevice needs a minimum feature level instead: create_host_device asks for
// 11_0, the least restrictive one (BL-2; 12_0 refused an adapter that tops out at 11_x), and the device still has its
// highest level.
HRESULT WINAPI D3D12XboxCreateDevice(IUnknown* adapter, const void* params, const IID* riid, void** out) {
rt::d3d12_xs::set_default_descriptor_heap_allocator(&xmem_descriptor_heap_alloc, &xmem_descriptor_heap_free);
constexpr auto method = "D3D12XboxCreateDevice";
using rt::d3d12_trace::result;
if (!out) return result(method, E_POINTER);
Expand Down
156 changes: 156 additions & 0 deletions runtime/shims/d3d12_xs_alloc_size.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Console allocation sizes for render targets, for the Xbox-ABI device proxy's slots 25 and 56 (GetResourceAllocationInfo and GetResourceAllocationInfo1).
//
// Why: the retail title checks a fixed memory budget against what its first full-screen render targets used and asserts
// when more than 2 MiB is left over (docs/retail_boot_chain.md, stop 17, #6982). The PC device sizes a 2D render target
// from the Vulkan driver's memory requirements, which is about width x height x element bytes. The console lays a
// render target out in 64 KiB tiles (the RDNA2 SW_64KB layouts), so its allocation is the surface padded to whole
// tiles: for 4-byte elements a tile is 128 x 128 pixels, and 1080 rows take nine tile rows, not 8.4. The PC size is
// smaller by that padding, summed over every full-screen target.
//
// What this is: the tile shapes are the RDNA2 SW_64KB_D shapes of runtime/src/gfx10_tiling.h (Mesa addrlib patterns,
// validated against real textures in docs/textures.md). That a console render target uses these shapes and no extra
// (DCC, CMask) memory in its allocation size is an assumption: the title's own sizes are not measured. The rule only
// ever makes a size larger than the PC device's own answer, never smaller. The mip tail follows AMD's addrlib
// (gfx10, 2D): a mip extent is rounded up (ShiftCeil), and the tail starts at the first mip that fits in half a tile's
// width and a whole tile's height; the tail is one tile.
#pragma once

#include "../src/gfx10_tiling.h"

#include <windows.h>
#include <d3d12.h>

#include <algorithm>
#include <bit>
#include <cstddef>
#include <cstdint>

namespace rt::d3d12_xs {

constexpr UINT64 kConsoleTileBytes = 64 * 1024; // one SW_64KB tile

// Bytes per element of the colour formats a render target takes, or 0 for any other format (the PC size then stays).
inline std::size_t render_target_element_bytes(DXGI_FORMAT format) {
switch (format) {
case DXGI_FORMAT_R8_TYPELESS: case DXGI_FORMAT_R8_UNORM: case DXGI_FORMAT_R8_UINT:
case DXGI_FORMAT_R8_SNORM: case DXGI_FORMAT_R8_SINT: case DXGI_FORMAT_A8_UNORM:
return 1;
case DXGI_FORMAT_R8G8_TYPELESS: case DXGI_FORMAT_R8G8_UNORM: case DXGI_FORMAT_R8G8_UINT:
case DXGI_FORMAT_R8G8_SNORM: case DXGI_FORMAT_R8G8_SINT: case DXGI_FORMAT_R16_TYPELESS:
case DXGI_FORMAT_R16_FLOAT: case DXGI_FORMAT_R16_UNORM: case DXGI_FORMAT_R16_UINT:
case DXGI_FORMAT_R16_SNORM: case DXGI_FORMAT_R16_SINT: case DXGI_FORMAT_B5G6R5_UNORM:
case DXGI_FORMAT_B5G5R5A1_UNORM:
return 2;
case DXGI_FORMAT_R10G10B10A2_TYPELESS: case DXGI_FORMAT_R10G10B10A2_UNORM:
case DXGI_FORMAT_R10G10B10A2_UINT: case DXGI_FORMAT_R11G11B10_FLOAT:
case DXGI_FORMAT_R8G8B8A8_TYPELESS: case DXGI_FORMAT_R8G8B8A8_UNORM:
case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB: case DXGI_FORMAT_R8G8B8A8_UINT:
case DXGI_FORMAT_R8G8B8A8_SNORM: case DXGI_FORMAT_R8G8B8A8_SINT:
case DXGI_FORMAT_B8G8R8A8_TYPELESS: case DXGI_FORMAT_B8G8R8A8_UNORM:
case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB: case DXGI_FORMAT_B8G8R8X8_TYPELESS:
case DXGI_FORMAT_B8G8R8X8_UNORM: case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
case DXGI_FORMAT_R16G16_TYPELESS: case DXGI_FORMAT_R16G16_FLOAT: case DXGI_FORMAT_R16G16_UNORM:
case DXGI_FORMAT_R16G16_UINT: case DXGI_FORMAT_R16G16_SNORM: case DXGI_FORMAT_R16G16_SINT:
case DXGI_FORMAT_R32_TYPELESS: case DXGI_FORMAT_R32_FLOAT: case DXGI_FORMAT_R32_UINT:
case DXGI_FORMAT_R32_SINT:
return 4;
case DXGI_FORMAT_R16G16B16A16_TYPELESS: case DXGI_FORMAT_R16G16B16A16_FLOAT:
case DXGI_FORMAT_R16G16B16A16_UNORM: case DXGI_FORMAT_R16G16B16A16_UINT:
case DXGI_FORMAT_R16G16B16A16_SNORM: case DXGI_FORMAT_R16G16B16A16_SINT:
case DXGI_FORMAT_R32G32_TYPELESS: case DXGI_FORMAT_R32G32_FLOAT: case DXGI_FORMAT_R32G32_UINT:
case DXGI_FORMAT_R32G32_SINT:
return 8;
case DXGI_FORMAT_R32G32B32A32_TYPELESS: case DXGI_FORMAT_R32G32B32A32_FLOAT:
case DXGI_FORMAT_R32G32B32A32_UINT: case DXGI_FORMAT_R32G32B32A32_SINT:
return 16;
default:
return 0;
}
}

// The console's allocation size for a single-sampled 2D render target, or 0 when the rule does not apply (not a 2D
// texture, not a render target, more than one sample, an unlisted format, an empty extent, or a size that overflows).
// MipLevels 0 is the full chain, as on the PC device. A mip extent is rounded up. Mips before the tail are padded to
// whole tiles; the first mip that fits in half a tile's width and the whole tile height, and every mip after it, share
// one tail tile. Slices add up.
inline UINT64 console_render_target_size(const D3D12_RESOURCE_DESC& desc) {
if (desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D) return 0;
if (!(desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET)) return 0;
if (desc.SampleDesc.Count > 1) return 0;
const std::size_t element_bytes = render_target_element_bytes(desc.Format);
if (!element_bytes || !desc.Width || !desc.Height || !desc.DepthOrArraySize) return 0;

const UINT full_chain = static_cast<UINT>(std::bit_width(std::max<UINT64>(desc.Width, desc.Height)));
const UINT mips = desc.MipLevels ? std::min<UINT>(desc.MipLevels, full_chain) : full_chain;
UINT64 slice = 0;
bool tail = false;
for (UINT mip = 0; mip < mips; ++mip) {
const UINT64 width = std::max<UINT64>((desc.Width + (UINT64{1} << mip) - 1) >> mip, 1);
const UINT64 height = std::max<UINT64>((UINT64{desc.Height} + (UINT64{1} << mip) - 1) >> mip, 1);
leonida::gfx10::TiledSurface2D surface;
if (width > UINT32_MAX ||
!leonida::gfx10::TiledSurface2D::make(leonida::gfx10::TileMode::sw_64kb_d, element_bytes,
static_cast<std::size_t>(width), static_cast<std::size_t>(height),
surface))
return 0;
if (width <= surface.tile_width() / 2 && height <= surface.tile_height()) {
tail = true;
break;
}
slice += surface.tiled_size();
}
if (tail) slice += kConsoleTileBytes;
const UINT64 slices = desc.DepthOrArraySize;
if (slice > UINT64_MAX / slices) return 0;
return slice * slices;
}

// Rounds up to a multiple of `to`, stopping at the last multiple below 2^64 instead of wrapping (as the PC device's
// totals do).
inline UINT64 align_up_saturating(UINT64 value, UINT64 to) {
return value > UINT64_MAX - (to - 1) ? UINT64_MAX / to * to : (value + to - 1) / to * to;
}

// Lays the descriptions out as the PC device does (one after another, each at its own alignment, the total rounded up
// to the largest alignment), with a render target's size taken as its console size when that is larger than the PC
// size. pc_one(i) is the PC device's answer for description i alone. Sizes are rounded to each description's own
// alignment, so a multisampled neighbour at 4 MiB keeps its place, and sums saturate instead of wrapping. When
// `each` is given it receives every description's offset, alignment and size.
//
// Returns false, and writes nothing, when no description is a console render target that grows, or when the PC
// device refuses one: the PC answer then stands. A true answer is never below what the PC device gave for the same
// descriptions laid out in that way.
template <class PcOne>
bool console_placement(const D3D12_RESOURCE_DESC* descs, UINT count, PcOne pc_one,
D3D12_RESOURCE_ALLOCATION_INFO& total, D3D12_RESOURCE_ALLOCATION_INFO1* each) {
if (!descs) return false;
bool any_target = false;
for (UINT i = 0; i < count && !any_target; ++i) any_target = console_render_target_size(descs[i]) != 0;
if (!any_target) return false;
// Two passes when `each` is wanted: the first decides, the second writes, so a refusal or a list that does not
// grow leaves the array as the PC device filled it.
for (const bool write : {false, true}) {
if (write && !each) break;
bool grows = false;
UINT64 end = 0, alignment = 1;
for (UINT i = 0; i < count; ++i) {
const D3D12_RESOURCE_ALLOCATION_INFO one = pc_one(i);
if (one.SizeInBytes == UINT64_MAX || !one.Alignment) return false;
UINT64 size = one.SizeInBytes;
const UINT64 console = console_render_target_size(descs[i]);
if (console > size) {
size = align_up_saturating(console, one.Alignment);
grows = true;
}
const UINT64 offset = align_up_saturating(end, one.Alignment);
if (write) each[i] = {offset, one.Alignment, size};
end = size > UINT64_MAX - offset ? UINT64_MAX : offset + size;
alignment = std::max(alignment, one.Alignment);
}
if (!grows) return false;
total = {align_up_saturating(end, alignment), alignment};
}
return true;
}

} // namespace rt::d3d12_xs
38 changes: 38 additions & 0 deletions runtime/shims/d3d12_xs_desc_clean.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// The PC form of a resource description the retail title builds for the console, for the Xbox-ABI device proxy.
//
// The title's D3D12_RESOURCE_DESC carries console values the PC device cannot take: the layout word is 0x101 (tiled)
// or 0x1 (row-major buffer), and the flags have bits above the PC ones set (docs/retail_boot_chain.md, #6982). Slot 76
// replaces the layout word the same way before it forwards to CreateCommittedResource; slot 25 and the slot 127
// stand-in need the same cleaning. The title's own description is never written: a caller gets a copy.
#pragma once

#include <windows.h>
#include <d3d12.h>

#include <cstdint>

namespace rt::d3d12_xs {

// The D3D12_RESOURCE_FLAGS bits the PC runtime defines; every other bit is a console one.
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;

// Not constexpr: 0x101 is outside the unfixed enum's value range, which clang-cl rejects in a constant expression.
inline const auto kXboxTiledResourceLayout = static_cast<D3D12_TEXTURE_LAYOUT>(0x101);

// Strip Xbox-only flags and translate the Xbox tiled layout. Preserve all other layouts so the host can validate them.
inline D3D12_RESOURCE_DESC pc_resource_desc(const D3D12_RESOURCE_DESC& xbox) {
D3D12_RESOURCE_DESC pc = xbox;
if (pc.Layout == kXboxTiledResourceLayout) {
pc.Layout = pc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER ? D3D12_TEXTURE_LAYOUT_ROW_MAJOR
: D3D12_TEXTURE_LAYOUT_UNKNOWN;
}
pc.Flags = static_cast<D3D12_RESOURCE_FLAGS>(static_cast<std::uint32_t>(xbox.Flags) & kPcResourceFlagsMask);
return pc;
}

} // namespace rt::d3d12_xs
Loading
Loading