upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authortedin7 <tommasoderosa94@gmail.com>2026-11-23 17:26:57 +0100
committerMumogg <104529023+Mumogg@users.noreply.github.com>2026-11-23 22:14:29 -0600
commitf1a68eeffbf280b2e2c86f439ebd674da026d82e (patch)
treec829b577f3dbd5a1ed7c53c2e786f604c5c89a7d /runtime
parent584ef36fa82ca00e19a4a03104b1b0d6ce295bd4 (diff)
d3d12_xs: console render target sizes follow each resource's alignment, slot 56 offsets, full mip chains, rounded-up mip extents, B5G5R5A1, saturating sums
(cherry picked from commit 359390833f62378a5d1d2c0f9a6cea48fccbcb53)
Diffstat (limited to 'runtime')
-rw-r--r--runtime/shims/d3d12_xs_alloc_size.h81
-rw-r--r--runtime/shims/d3d12_xs_device.cpp73
-rw-r--r--runtime/test/xs_proxy_mock_test.cpp155
3 files changed, 252 insertions, 57 deletions
diff --git a/runtime/shims/d3d12_xs_alloc_size.h b/runtime/shims/d3d12_xs_alloc_size.h
index 8caead8d7..3321c4ec8 100644
--- a/runtime/shims/d3d12_xs_alloc_size.h
+++ b/runtime/shims/d3d12_xs_alloc_size.h
@@ -1,4 +1,4 @@
1// Console allocation sizes for render targets, for the Xbox-ABI device proxy's slot 25 (GetResourceAllocationInfo). 1// Console allocation sizes for render targets, for the Xbox-ABI device proxy's slots 25 and 56 (GetResourceAllocationInfo and GetResourceAllocationInfo1).
2// 2//
3// Why: the retail title checks a fixed memory budget against what its first full-screen render targets used and asserts 3// Why: the retail title checks a fixed memory budget against what its first full-screen render targets used and asserts
4// when more than 2 MiB is left over (docs/retail_boot_chain.md, stop 17, #6982). The PC device sizes a 2D render target 4// when more than 2 MiB is left over (docs/retail_boot_chain.md, stop 17, #6982). The PC device sizes a 2D render target
@@ -10,7 +10,9 @@
10// What this is: the tile shapes are the RDNA2 SW_64KB_D shapes of runtime/src/gfx10_tiling.h (Mesa addrlib patterns, 10// What this is: the tile shapes are the RDNA2 SW_64KB_D shapes of runtime/src/gfx10_tiling.h (Mesa addrlib patterns,
11// validated against real textures in docs/textures.md). That a console render target uses these shapes and no extra 11// validated against real textures in docs/textures.md). That a console render target uses these shapes and no extra
12// (DCC, CMask) memory in its allocation size is an assumption: the title's own sizes are not measured. The rule only 12// (DCC, CMask) memory in its allocation size is an assumption: the title's own sizes are not measured. The rule only
13// ever makes a size larger than the PC device's own answer, never smaller. 13// ever makes a size larger than the PC device's own answer, never smaller. The mip tail follows AMD's addrlib
14// (gfx10, 2D): a mip extent is rounded up (ShiftCeil), and the tail starts at the first mip that fits in half a tile's
15// width and a whole tile's height; the tail is one tile.
14#pragma once 16#pragma once
15 17
16#include "../src/gfx10_tiling.h" 18#include "../src/gfx10_tiling.h"
@@ -19,6 +21,7 @@
19#include <d3d12.h> 21#include <d3d12.h>
20 22
21#include <algorithm> 23#include <algorithm>
24#include <bit>
22#include <cstddef> 25#include <cstddef>
23#include <cstdint> 26#include <cstdint>
24 27
@@ -36,6 +39,7 @@ inline std::size_t render_target_element_bytes(DXGI_FORMAT format) {
36 case DXGI_FORMAT_R8G8_SNORM: case DXGI_FORMAT_R8G8_SINT: case DXGI_FORMAT_R16_TYPELESS: 39 case DXGI_FORMAT_R8G8_SNORM: case DXGI_FORMAT_R8G8_SINT: case DXGI_FORMAT_R16_TYPELESS:
37 case DXGI_FORMAT_R16_FLOAT: case DXGI_FORMAT_R16_UNORM: case DXGI_FORMAT_R16_UINT: 40 case DXGI_FORMAT_R16_FLOAT: case DXGI_FORMAT_R16_UNORM: case DXGI_FORMAT_R16_UINT:
38 case DXGI_FORMAT_R16_SNORM: case DXGI_FORMAT_R16_SINT: case DXGI_FORMAT_B5G6R5_UNORM: 41 case DXGI_FORMAT_R16_SNORM: case DXGI_FORMAT_R16_SINT: case DXGI_FORMAT_B5G6R5_UNORM:
42 case DXGI_FORMAT_B5G5R5A1_UNORM:
39 return 2; 43 return 2;
40 case DXGI_FORMAT_R10G10B10A2_TYPELESS: case DXGI_FORMAT_R10G10B10A2_UNORM: 44 case DXGI_FORMAT_R10G10B10A2_TYPELESS: case DXGI_FORMAT_R10G10B10A2_UNORM:
41 case DXGI_FORMAT_R10G10B10A2_UINT: case DXGI_FORMAT_R11G11B10_FLOAT: 45 case DXGI_FORMAT_R10G10B10A2_UINT: case DXGI_FORMAT_R11G11B10_FLOAT:
@@ -66,7 +70,9 @@ inline std::size_t render_target_element_bytes(DXGI_FORMAT format) {
66 70
67// The console's allocation size for a single-sampled 2D render target, or 0 when the rule does not apply (not a 2D 71// The console's allocation size for a single-sampled 2D render target, or 0 when the rule does not apply (not a 2D
68// texture, not a render target, more than one sample, an unlisted format, an empty extent, or a size that overflows). 72// texture, not a render target, more than one sample, an unlisted format, an empty extent, or a size that overflows).
69// Mips whose extent fits in one tile share the tail tile; every other mip is padded to whole tiles. Slices add up. 73// MipLevels 0 is the full chain, as on the PC device. A mip extent is rounded up. Mips before the tail are padded to
74// whole tiles; the first mip that fits in half a tile's width and the whole tile height, and every mip after it, share
75// one tail tile. Slices add up.
70inline UINT64 console_render_target_size(const D3D12_RESOURCE_DESC& desc) { 76inline UINT64 console_render_target_size(const D3D12_RESOURCE_DESC& desc) {
71 if (desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D) return 0; 77 if (desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D) return 0;
72 if (!(desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET)) return 0; 78 if (!(desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET)) return 0;
@@ -74,19 +80,20 @@ inline UINT64 console_render_target_size(const D3D12_RESOURCE_DESC& desc) {
74 const std::size_t element_bytes = render_target_element_bytes(desc.Format); 80 const std::size_t element_bytes = render_target_element_bytes(desc.Format);
75 if (!element_bytes || !desc.Width || !desc.Height || !desc.DepthOrArraySize) return 0; 81 if (!element_bytes || !desc.Width || !desc.Height || !desc.DepthOrArraySize) return 0;
76 82
77 const UINT mips = desc.MipLevels ? desc.MipLevels : 1; 83 const UINT full_chain = static_cast<UINT>(std::bit_width(std::max<UINT64>(desc.Width, desc.Height)));
84 const UINT mips = desc.MipLevels ? std::min<UINT>(desc.MipLevels, full_chain) : full_chain;
78 UINT64 slice = 0; 85 UINT64 slice = 0;
79 bool tail = false; 86 bool tail = false;
80 for (UINT mip = 0; mip < mips && mip < 32; ++mip) { 87 for (UINT mip = 0; mip < mips; ++mip) {
81 const UINT64 width = std::max<UINT64>(desc.Width >> mip, 1); 88 const UINT64 width = std::max<UINT64>((desc.Width + (UINT64{1} << mip) - 1) >> mip, 1);
82 const UINT64 height = std::max<UINT>(desc.Height >> mip, 1); 89 const UINT64 height = std::max<UINT64>((UINT64{desc.Height} + (UINT64{1} << mip) - 1) >> mip, 1);
83 leonida::gfx10::TiledSurface2D surface; 90 leonida::gfx10::TiledSurface2D surface;
84 if (width > UINT32_MAX || 91 if (width > UINT32_MAX ||
85 !leonida::gfx10::TiledSurface2D::make(leonida::gfx10::TileMode::sw_64kb_d, element_bytes, 92 !leonida::gfx10::TiledSurface2D::make(leonida::gfx10::TileMode::sw_64kb_d, element_bytes,
86 static_cast<std::size_t>(width), static_cast<std::size_t>(height), 93 static_cast<std::size_t>(width), static_cast<std::size_t>(height),
87 surface)) 94 surface))
88 return 0; 95 return 0;
89 if (width <= surface.tile_width() && height <= surface.tile_height()) { 96 if (width <= surface.tile_width() / 2 && height <= surface.tile_height()) {
90 tail = true; 97 tail = true;
91 break; 98 break;
92 } 99 }
@@ -98,20 +105,52 @@ inline UINT64 console_render_target_size(const D3D12_RESOURCE_DESC& desc) {
98 return slice * slices; 105 return slice * slices;
99} 106}
100 107
101// What GetResourceAllocationInfo adds to the PC device's answer: for every description with a console size larger than 108// Rounds up to a multiple of `to`, stopping at the last multiple below 2^64 instead of wrapping (as the PC device's
102// the PC device's size for it alone, the difference. pc_size_of(i) is the PC device's size for description i alone. 109// totals do).
103// A description the PC device refuses (UINT64_MAX) or the rule does not cover adds nothing. 110inline UINT64 align_up_saturating(UINT64 value, UINT64 to) {
104template <class PcSizeOf> 111 return value > UINT64_MAX - (to - 1) ? UINT64_MAX / to * to : (value + to - 1) / to * to;
105UINT64 console_render_target_extra(const D3D12_RESOURCE_DESC* descs, UINT count, PcSizeOf pc_size_of) { 112}
106 UINT64 extra = 0; 113
107 for (UINT i = 0; descs && i < count; ++i) { 114// Lays the descriptions out as the PC device does (one after another, each at its own alignment, the total rounded up
108 const UINT64 console = console_render_target_size(descs[i]); 115// to the largest alignment), with a render target's size taken as its console size when that is larger than the PC
109 if (!console) continue; 116// size. pc_one(i) is the PC device's answer for description i alone. Sizes are rounded to each description's own
110 const UINT64 pc = pc_size_of(i); 117// alignment, so a multisampled neighbour at 4 MiB keeps its place, and sums saturate instead of wrapping. When
111 if (pc == UINT64_MAX || pc >= console) continue; 118// `each` is given it receives every description's offset, alignment and size.
112 extra += console - pc; 119//
120// Returns false, and writes nothing, when no description is a console render target that grows, or when the PC
121// device refuses one: the PC answer then stands. A true answer is never below what the PC device gave for the same
122// descriptions laid out in that way.
123template <class PcOne>
124bool console_placement(const D3D12_RESOURCE_DESC* descs, UINT count, PcOne pc_one,
125 D3D12_RESOURCE_ALLOCATION_INFO& total, D3D12_RESOURCE_ALLOCATION_INFO1* each) {
126 if (!descs) return false;
127 bool any_target = false;
128 for (UINT i = 0; i < count && !any_target; ++i) any_target = console_render_target_size(descs[i]) != 0;
129 if (!any_target) return false;
130 // Two passes when `each` is wanted: the first decides, the second writes, so a refusal or a list that does not
131 // grow leaves the array as the PC device filled it.
132 for (const bool write : {false, true}) {
133 if (write && !each) break;
134 bool grows = false;
135 UINT64 end = 0, alignment = 1;
136 for (UINT i = 0; i < count; ++i) {
137 const D3D12_RESOURCE_ALLOCATION_INFO one = pc_one(i);
138 if (one.SizeInBytes == UINT64_MAX || !one.Alignment) return false;
139 UINT64 size = one.SizeInBytes;
140 const UINT64 console = console_render_target_size(descs[i]);
141 if (console > size) {
142 size = align_up_saturating(console, one.Alignment);
143 grows = true;
144 }
145 const UINT64 offset = align_up_saturating(end, one.Alignment);
146 if (write) each[i] = {offset, one.Alignment, size};
147 end = size > UINT64_MAX - offset ? UINT64_MAX : offset + size;
148 alignment = std::max(alignment, one.Alignment);
149 }
150 if (!grows) return false;
151 total = {align_up_saturating(end, alignment), alignment};
113 } 152 }
114 return extra; 153 return true;
115} 154}
116 155
117} // namespace rt::d3d12_xs 156} // namespace rt::d3d12_xs
diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp
index 43cbaf7b1..cd3953fa6 100644
--- a/runtime/shims/d3d12_xs_device.cpp
+++ b/runtime/shims/d3d12_xs_device.cpp
@@ -33,6 +33,7 @@
33#include <dxgi1_4.h> 33#include <dxgi1_4.h>
34 34
35#include <atomic> 35#include <atomic>
36#include <functional>
36#include <cerrno> 37#include <cerrno>
37#include <cstddef> 38#include <cstddef>
38#include <cstdint> 39#include <cstdint>
@@ -237,6 +238,7 @@ const char* slot_name(unsigned slot) {
237 case 46: return "SetResidencyPriority (proxies unwrapped)"; 238 case 46: return "SetResidencyPriority (proxies unwrapped)";
238 case 48: return "Xbox CreateCommandQueueX -> queue proxy"; 239 case 48: return "Xbox CreateCommandQueueX -> queue proxy";
239 case 50: return "EnqueueMakeResident (proxies unwrapped)"; 240 case 50: return "EnqueueMakeResident (proxies unwrapped)";
241 case 56: return "GetResourceAllocationInfo1 (C or PC form)";
240 case 62: return "Xbox CreateComputePipelineState"; 242 case 62: return "Xbox CreateComputePipelineState";
241 case 67: return "Xbox DeserializeComputePipelineStateX"; 243 case 67: return "Xbox DeserializeComputePipelineStateX";
242 case 70: return "Xbox shader-resource view from raw words"; 244 case 70: return "Xbox shader-resource view from raw words";
@@ -2373,8 +2375,24 @@ StructReturnCall struct_return_call(std::uint64_t a0, std::uint64_t a1) {
2373 return {reinterpret_cast<DeviceProxy*>(a1), reinterpret_cast<void*>(a0)}; // C form: return buffer, this 2375 return {reinterpret_cast<DeviceProxy*>(a1), reinterpret_cast<void*>(a0)}; // C form: return buffer, this
2374} 2376}
2375 2377
2376// The size the PC device saturates a total at (docs/d3d12_vulkan.md, GetResourceAllocationInfo). 2378// The console size of a list of descriptions (d3d12_xs_alloc_size.h): the PC device's answer with every console render
2377constexpr UINT64 kSizeSaturated = 0xFFFFFFFFFFFF0000ull; 2379// target laid out at its console size, each at its own alignment. `info` is the PC answer and is replaced only when
2380// the console layout is larger. `each` is the per-description array of slot 56, or null.
2381void apply_console_placement(const D3D12_RESOURCE_DESC* descs, UINT count, D3D12_RESOURCE_ALLOCATION_INFO& info,
2382 D3D12_RESOURCE_ALLOCATION_INFO1* each,
2383 const std::function<D3D12_RESOURCE_ALLOCATION_INFO(UINT)>& pc_one, unsigned slot) {
2384 if (info.SizeInBytes == UINT64_MAX) return;
2385 D3D12_RESOURCE_ALLOCATION_INFO console{};
2386 if (!console_placement(descs, count, pc_one, console, each)) return;
2387 static std::atomic<bool> logged{false};
2388 if (!logged.exchange(true, std::memory_order_relaxed))
2389 std::fprintf(stderr,
2390 "[rt] d3d12_xs: slot %u: render targets sized as console tiles, %llu B for %llu B on %u "
2391 "description(s)\n",
2392 slot, static_cast<unsigned long long>(console.SizeInBytes),
2393 static_cast<unsigned long long>(info.SizeInBytes), static_cast<unsigned>(count));
2394 if (console.SizeInBytes >= info.SizeInBytes) info = console;
2395}
2378 2396
2379void* WINAPI proxy_get_resource_allocation_info(std::uint64_t a0, std::uint64_t a1, std::uint64_t visible_mask, 2397void* WINAPI proxy_get_resource_allocation_info(std::uint64_t a0, std::uint64_t a1, std::uint64_t visible_mask,
2380 std::uint64_t count, const D3D12_RESOURCE_DESC* descs) { 2398 std::uint64_t count, const D3D12_RESOURCE_DESC* descs) {
@@ -2394,34 +2412,45 @@ void* WINAPI proxy_get_resource_allocation_info(std::uint64_t a0, std::uint64_t
2394 for (UINT i = 0; i < desc_count; ++i) clean[i] = pc_resource_desc(descs[i]); 2412 for (UINT i = 0; i < desc_count; ++i) clean[i] = pc_resource_desc(descs[i]);
2395 descs = clean.get(); 2413 descs = clean.get();
2396 } 2414 }
2397 D3D12_RESOURCE_ALLOCATION_INFO info = 2415 const UINT mask = static_cast<UINT>(visible_mask);
2398 real->GetResourceAllocationInfo(static_cast<UINT>(visible_mask), desc_count, descs); 2416 D3D12_RESOURCE_ALLOCATION_INFO info = real->GetResourceAllocationInfo(mask, desc_count, descs);
2399 // A render target takes whole console tiles, which the PC device's size does not (d3d12_xs_alloc_size.h). The 2417 apply_console_placement(descs, desc_count, info, nullptr,
2400 // total grows by what each render target lacks on its own, so the placement of the others stays as the PC lays it. 2418 [&](UINT i) { return real->GetResourceAllocationInfo(mask, 1, &descs[i]); }, 25);
2401 if (info.SizeInBytes != UINT64_MAX) {
2402 const UINT64 extra = console_render_target_extra(descs, static_cast<UINT>(count), [&](UINT i) {
2403 return real->GetResourceAllocationInfo(static_cast<UINT>(visible_mask), 1, &descs[i]).SizeInBytes;
2404 });
2405 if (extra) {
2406 static std::atomic<bool> logged{false};
2407 if (!logged.exchange(true, std::memory_order_relaxed))
2408 std::fprintf(stderr,
2409 "[rt] d3d12_xs: slot 25: render target sizes padded to console tiles, +%llu B on "
2410 "%llu B for %u description(s)\n",
2411 static_cast<unsigned long long>(extra),
2412 static_cast<unsigned long long>(info.SizeInBytes), static_cast<unsigned>(count));
2413 info.SizeInBytes = extra > kSizeSaturated - info.SizeInBytes ? kSizeSaturated : info.SizeInBytes + extra;
2414 }
2415 }
2416 *static_cast<D3D12_RESOURCE_ALLOCATION_INFO*>(call.ret) = info; 2419 *static_cast<D3D12_RESOURCE_ALLOCATION_INFO*>(call.ret) = info;
2417 return call.ret; 2420 return call.ret;
2418} 2421}
2419 2422
2423// Slot 56, ID3D12Device4::GetResourceAllocationInfo1: the same answer, and the per-description offsets follow the
2424// padded sizes, so a title that places its resources at those offsets does not overlap them. The call is forwarded
2425// as it came; the console layout then replaces the total and the offsets.
2426void* WINAPI proxy_get_resource_allocation_info1(std::uint64_t a0, std::uint64_t a1, std::uint64_t visible_mask,
2427 std::uint64_t count, const D3D12_RESOURCE_DESC* descs,
2428 D3D12_RESOURCE_ALLOCATION_INFO1* each) {
2429 const StructReturnCall call = struct_return_call(a0, a1);
2430 DeviceProxy* const self = call.self;
2431 note_slot(56);
2432 if (56 >= self->slot_count) {
2433 return reinterpret_cast<void*>(hr_to_u64(refuse_beyond_device(56, self->slot_count)));
2434 }
2435 using Fn = void*(WINAPI*)(void*, void*, UINT, UINT, const D3D12_RESOURCE_DESC*, D3D12_RESOURCE_ALLOCATION_INFO1*);
2436 const UINT n = static_cast<UINT>(count);
2437 void* const returned = reinterpret_cast<Fn>(self->real_vtable[56])(
2438 self->real, call.ret, static_cast<UINT>(visible_mask), n, descs, each);
2439 if (each) {
2440 auto& info = *static_cast<D3D12_RESOURCE_ALLOCATION_INFO*>(call.ret);
2441 apply_console_placement(descs, n, info, each, [&](UINT i) {
2442 return D3D12_RESOURCE_ALLOCATION_INFO{each[i].SizeInBytes, each[i].Alignment};
2443 }, 56);
2444 }
2445 return returned;
2446}
2447
2420void** get_vtable() { 2448void** get_vtable() {
2421 static void** vtable = [] { 2449 static void** vtable = [] {
2422 static void* storage[kMaxSlots]; 2450 static void* storage[kMaxSlots];
2423 fill_vtable(storage, std::make_index_sequence<kMaxSlots>{}); 2451 fill_vtable(storage, std::make_index_sequence<kMaxSlots>{});
2424 storage[25] = reinterpret_cast<void*>(&proxy_get_resource_allocation_info); 2452 storage[25] = reinterpret_cast<void*>(&proxy_get_resource_allocation_info);
2453 storage[56] = reinterpret_cast<void*>(&proxy_get_resource_allocation_info1);
2425 return storage; 2454 return storage;
2426 }(); 2455 }();
2427 return vtable; 2456 return vtable;
@@ -2578,7 +2607,7 @@ ID3D12Device* device_proxy_real_device(const void* device) {
2578 2607
2579bool device_slot_overridden(unsigned slot) { 2608bool device_slot_overridden(unsigned slot) {
2580 if (slot <= 2 || slot == 8 || slot == 9 || slot == 14 || (slot >= 17 && slot <= 24) || slot == 25 || slot == 26 || slot == 31 || slot == 32 || slot == 34 || slot == 35 || 2609 if (slot <= 2 || slot == 8 || slot == 9 || slot == 14 || (slot >= 17 && slot <= 24) || slot == 25 || slot == 26 || slot == 31 || slot == 32 || slot == 34 || slot == 35 ||
2581 slot == 36 || slot == 45 || slot == 46 || slot == 50 || slot == 62 || slot == 67 || 2610 slot == 36 || slot == 45 || slot == 46 || slot == 50 || slot == 56 || slot == 62 || slot == 67 ||
2582 slot == 70 || slot == 75 || slot == 76 || slot == 133 || slot == kCreateCommittedResource3X) 2611 slot == 70 || slot == 75 || slot == 76 || slot == 133 || slot == kCreateCommittedResource3X)
2583 return true; 2612 return true;
2584 const XboxDeviceSlotRoutes& routes = xbox_device_slot_routes(); 2613 const XboxDeviceSlotRoutes& routes = xbox_device_slot_routes();
diff --git a/runtime/test/xs_proxy_mock_test.cpp b/runtime/test/xs_proxy_mock_test.cpp
index d0e837ef0..6bae19ac7 100644
--- a/runtime/test/xs_proxy_mock_test.cpp
+++ b/runtime/test/xs_proxy_mock_test.cpp
@@ -16,6 +16,8 @@
16#include <d3d12.h> 16#include <d3d12.h>
17#include <windows.h> 17#include <windows.h>
18 18
19#include <algorithm>
20
19#include <crtdbg.h> 21#include <crtdbg.h>
20 22
21#include <atomic> 23#include <atomic>
@@ -200,33 +202,55 @@ void copy_array(Mock* m, unsigned slot, std::uint64_t objects, std::uint64_t cou
200 for (std::uint32_t i = 0; in && i < static_cast<std::uint32_t>(count) && i < 4; ++i) m->array[slot][i] = in[i]; 202 for (std::uint32_t i = 0; in && i < static_cast<std::uint32_t>(count) && i < 4; ++i) m->array[slot][i] = in[i];
201} 203}
202 204
203// The PC device's GetResourceAllocationInfo, as a driver that packs a surface with no tile padding answers it: a buffer 205// GetResourceAllocationInfo as a PC driver without console tile padding answers it. A buffer is its width, a texture
204// its width, a texture width x height x slices x element bytes (4 for RGBA8, 8 for RGBA16F), each rounded up to 206// width x height x slices x element bytes (2 for B5G5R5A1, 4 for RGBA8, 8 for RGBA16F, 16 for RGBA32F; mips are not
205// 64 KiB, summed over the descriptions; a texture with no width is refused. The struct is written to the hidden return 207// counted, and one resource is at most 4 GiB, the way a driver caps an allocation), each rounded up to the
206// pointer (RDX of a C++ method call), which comes back in RAX. 208// resource's alignment: 64 KiB, or 4 MiB with more than one sample. A texture with no width is refused. Resources are
209// laid out one after another, each at its own alignment, and the total is rounded up to the largest alignment, as the
210// Vulkan layer's place() does. The struct is written to the hidden return pointer (RDX of a C++ method call), which
211// comes back in RAX. `each` (slot 56) gets every resource's offset, alignment and size.
212std::uint64_t mock_align(std::uint64_t value, std::uint64_t to) {
213 return value > UINT64_MAX - (to - 1) ? UINT64_MAX / to * to : (value + to - 1) / to * to;
214}
215
216D3D12_RESOURCE_ALLOCATION_INFO mock_one_allocation(const D3D12_RESOURCE_DESC& d) {
217 if (d.Width == 0) return {UINT64_MAX, 65536};
218 if (d.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER) return {mock_align(d.Width, 65536), 65536};
219 std::uint64_t element = 4;
220 if (d.Format == DXGI_FORMAT_B5G5R5A1_UNORM) element = 2;
221 if (d.Format == DXGI_FORMAT_R16G16B16A16_FLOAT) element = 8;
222 if (d.Format == DXGI_FORMAT_R32G32B32A32_FLOAT) element = 16;
223 const std::uint64_t samples = d.SampleDesc.Count ? d.SampleDesc.Count : 1;
224 const std::uint64_t alignment = samples > 1 ? 4194304 : 65536;
225 const std::uint64_t bytes = std::min<std::uint64_t>(
226 d.Width * d.Height * d.DepthOrArraySize * element * samples, std::uint64_t{4} << 30);
227 return {mock_align(bytes, alignment), alignment};
228}
229
207D3D12_RESOURCE_DESC g_seen_descs[4]; // the first descriptions the mock device's slot 25 was given 230D3D12_RESOURCE_DESC g_seen_descs[4]; // the first descriptions the mock device's slot 25 was given
208std::uint32_t g_seen_desc_count = 0; 231std::uint32_t g_seen_desc_count = 0;
209std::uint32_t g_seen_calls = 0; // slot 25 calls since the test cleared it; only the first is recorded 232std::uint32_t g_seen_calls = 0; // slot 25 calls since the test cleared it; only the first is recorded
210 233
211std::uint64_t mock_allocation_info(std::uint64_t ret, std::uint64_t count, std::uint64_t descs_word) { 234std::uint64_t mock_allocation_info(std::uint64_t ret, std::uint64_t count, std::uint64_t descs_word,
235 D3D12_RESOURCE_ALLOCATION_INFO1* each = nullptr) {
212 const auto* descs = reinterpret_cast<const D3D12_RESOURCE_DESC*>(descs_word); 236 const auto* descs = reinterpret_cast<const D3D12_RESOURCE_DESC*>(descs_word);
213 if (g_seen_calls++ == 0) { 237 if (g_seen_calls++ == 0) {
214 g_seen_desc_count = static_cast<std::uint32_t>(count); 238 g_seen_desc_count = static_cast<std::uint32_t>(count);
215 for (std::uint32_t i = 0; descs && i < g_seen_desc_count && i < std::size(g_seen_descs); ++i) 239 for (std::uint32_t i = 0; descs && i < g_seen_desc_count && i < std::size(g_seen_descs); ++i)
216 g_seen_descs[i] = descs[i]; 240 g_seen_descs[i] = descs[i];
217 } 241 }
218 std::uint64_t total = 0; 242 D3D12_RESOURCE_ALLOCATION_INFO total{0, 1};
219 bool refused = false; 243 bool refused = false;
220 for (std::uint32_t i = 0; descs && i < static_cast<std::uint32_t>(count); ++i) { 244 for (std::uint32_t i = 0; descs && i < static_cast<std::uint32_t>(count); ++i) {
221 const D3D12_RESOURCE_DESC& d = descs[i]; 245 const D3D12_RESOURCE_ALLOCATION_INFO one = mock_one_allocation(descs[i]);
222 std::uint64_t bytes = d.Width; 246 refused = refused || one.SizeInBytes == UINT64_MAX;
223 if (d.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER) 247 const std::uint64_t offset = mock_align(total.SizeInBytes, one.Alignment);
224 bytes = d.Width * d.Height * d.DepthOrArraySize * (d.Format == DXGI_FORMAT_R16G16B16A16_FLOAT ? 8 : 4); 248 if (each) each[i] = {offset, one.Alignment, one.SizeInBytes};
225 refused = refused || d.Width == 0; 249 total.SizeInBytes = one.SizeInBytes > UINT64_MAX - offset ? UINT64_MAX : offset + one.SizeInBytes;
226 total += (bytes + 65535) / 65536 * 65536; 250 total.Alignment = std::max(total.Alignment, one.Alignment);
227 } 251 }
228 auto* out = reinterpret_cast<D3D12_RESOURCE_ALLOCATION_INFO*>(ret); 252 auto* out = reinterpret_cast<D3D12_RESOURCE_ALLOCATION_INFO*>(ret);
229 *out = {refused ? UINT64_MAX : total, 65536}; 253 *out = {refused ? UINT64_MAX : mock_align(total.SizeInBytes, total.Alignment), total.Alignment};
230 return ret; 254 return ret;
231} 255}
232 256
@@ -252,6 +276,8 @@ std::uint64_t mock_call(Mock* m, unsigned slot, const std::uint64_t* words) {
252 if (a1 == kResourceHandle) return static_cast<std::uint32_t>(mock_create(m, Kind::resource, a2, a3)); 276 if (a1 == kResourceHandle) return static_cast<std::uint32_t>(mock_create(m, Kind::resource, a2, a3));
253 return static_cast<std::uint32_t>(E_INVALIDARG); 277 return static_cast<std::uint32_t>(E_INVALIDARG);
254 case 25: return mock_allocation_info(a1, a3, a4); // GetResourceAllocationInfo (mask a2 is unused) 278 case 25: return mock_allocation_info(a1, a3, a4); // GetResourceAllocationInfo (mask a2 is unused)
279 case 56: // GetResourceAllocationInfo1: (return buffer, mask, count, descs, per-description array)
280 return mock_allocation_info(a1, a3, a4, reinterpret_cast<D3D12_RESOURCE_ALLOCATION_INFO1*>(words[4]));
255 case 34: case 35: case 46: copy_array(m, slot, a2, a1); break; // (count, objects, ...) 281 case 34: case 35: case 46: copy_array(m, slot, a2, a1); break; // (count, objects, ...)
256 case 45: copy_array(m, slot, a1, a3); break; // (fences, values, count, ...) 282 case 45: copy_array(m, slot, a1, a3); break; // (fences, values, count, ...)
257 case 26: { // GetCustomHeapProperties, the PC method: (return buffer, node mask, heap type) 283 case 26: { // GetCustomHeapProperties, the PC method: (return buffer, node mask, heap type)
@@ -1280,6 +1306,106 @@ int check_render_target_sizes() {
1280 return 0; 1306 return 0;
1281} 1307}
1282 1308
1309// ---- 11. Console render target layout: alignment, per-description offsets, mips, formats, overflow ----
1310
1311int check_render_target_layout() {
1312 Mock* mock = nullptr;
1313 void* device = make_device(mock, 9, true);
1314 EXPECT(device);
1315 void* const* vtable = *reinterpret_cast<void* const* const*>(device);
1316 // The retail executable's calls: the buffer for the struct in RCX, the device in RDX.
1317 using InfoFn = D3D12_RESOURCE_ALLOCATION_INFO*(WINAPI*)(D3D12_RESOURCE_ALLOCATION_INFO*, void*, UINT, UINT,
1318 const D3D12_RESOURCE_DESC*);
1319 using Info1Fn = D3D12_RESOURCE_ALLOCATION_INFO*(WINAPI*)(D3D12_RESOURCE_ALLOCATION_INFO*, void*, UINT, UINT,
1320 const D3D12_RESOURCE_DESC*,
1321 D3D12_RESOURCE_ALLOCATION_INFO1*);
1322 const auto get_info = reinterpret_cast<InfoFn>(vtable[25]);
1323 const auto get_info1 = reinterpret_cast<Info1Fn>(vtable[56]);
1324 auto ask = [&](const D3D12_RESOURCE_DESC* descs, UINT count) {
1325 D3D12_RESOURCE_ALLOCATION_INFO info{0xDEAD, 0xBEEF};
1326 if (get_info(&info, device, 0, count, descs) != &info) info = {0, 0};
1327 return info;
1328 };
1329 auto ask_one = [&](const D3D12_RESOURCE_DESC& d) { return ask(&d, 1); };
1330 auto target = [](std::uint64_t w, UINT h, DXGI_FORMAT f, UINT slices, UINT mips) {
1331 D3D12_RESOURCE_DESC d = target_desc(w, h, f, slices);
1332 d.MipLevels = static_cast<UINT16>(mips);
1333 return d;
1334 };
1335
1336 // A four-sample 1 x 1 target is 4 MiB at 4 MiB alignment. Next to a 1080p target that grows to 8,847,360 B, the
1337 // two need 4 MiB + 8,847,360 B = 13,041,664 B placed, and the total is a whole number of 4 MiB: 16 MiB, in
1338 // either order. (The PC device says 12 MiB for them; adding the 512 KiB the padding takes would say 12.5 MiB.)
1339 const D3D12_RESOURCE_DESC hd = target_desc(1920, 1080, DXGI_FORMAT_R8G8B8A8_UNORM);
1340 D3D12_RESOURCE_DESC msaa = target_desc(1, 1, DXGI_FORMAT_R8G8B8A8_UNORM);
1341 msaa.SampleDesc.Count = 4;
1342 const D3D12_RESOURCE_DESC msaa_first[] = {msaa, hd};
1343 const D3D12_RESOURCE_DESC msaa_last[] = {hd, msaa};
1344 D3D12_RESOURCE_ALLOCATION_INFO info = ask(msaa_first, 2);
1345 EXPECT(info.SizeInBytes == 16777216 && info.Alignment == 4194304);
1346 info = ask(msaa_last, 2);
1347 EXPECT(info.SizeInBytes == 16777216 && info.Alignment == 4194304);
1348 EXPECT(ask(&msaa, 1).SizeInBytes == 4194304); // multisampled targets are left as the PC device sizes them
1349
1350 // GetResourceAllocationInfo1: the offsets follow the padded sizes, in the console form and the PC form.
1351 const D3D12_RESOURCE_DESC two[] = {hd, hd};
1352 D3D12_RESOURCE_ALLOCATION_INFO1 each[2]{};
1353 D3D12_RESOURCE_ALLOCATION_INFO total{};
1354 EXPECT(get_info1(&total, device, 0, 2, two, each) == &total);
1355 EXPECT(total.SizeInBytes == 17694720 && total.Alignment == 65536);
1356 EXPECT(each[0].Offset == 0 && each[0].SizeInBytes == 8847360 && each[0].Alignment == 65536);
1357 EXPECT(each[1].Offset == 8847360 && each[1].SizeInBytes == 8847360 && each[1].Alignment == 65536);
1358 D3D12_RESOURCE_ALLOCATION_INFO1 mixed[2]{};
1359 D3D12_RESOURCE_ALLOCATION_INFO pc{};
1360 EXPECT(raw(device, 56, word(&pc), 0, 2, word(msaa_first), word(mixed)) == word(&pc));
1361 EXPECT(pc.SizeInBytes == 16777216 && pc.Alignment == 4194304);
1362 EXPECT(mixed[0].Offset == 0 && mixed[0].SizeInBytes == 4194304 && mixed[0].Alignment == 4194304);
1363 EXPECT(mixed[1].Offset == 4194304 && mixed[1].SizeInBytes == 8847360 && mixed[1].Alignment == 65536);
1364 // Nothing to pad: the PC device's answer stands, per-description array included.
1365 D3D12_RESOURCE_DESC buffer{};
1366 buffer.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
1367 buffer.Width = 100000;
1368 buffer.Height = 1;
1369 buffer.DepthOrArraySize = 1;
1370 buffer.MipLevels = 1;
1371 buffer.SampleDesc.Count = 1;
1372 buffer.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
1373 const D3D12_RESOURCE_DESC buffers[] = {buffer, buffer};
1374 D3D12_RESOURCE_ALLOCATION_INFO1 plain[2]{};
1375 EXPECT(get_info1(&total, device, 0, 2, buffers, plain) == &total);
1376 EXPECT(total.SizeInBytes == 262144 && plain[1].Offset == 131072 && plain[1].SizeInBytes == 131072);
1377
1378 // Mips: 0 is the full chain. 256 x 128 RGBA8 takes two tiles for mip 0, one for mip 1 (128 x 64 is wider than half a
1379 // tile) and one tail tile for the rest.
1380 EXPECT(ask_one(target(256, 128, DXGI_FORMAT_R8G8B8A8_UNORM, 1, 0)).SizeInBytes ==
1381 4 * 65536ull);
1382 // 128 x 32 with two mips and two slices: mip 0 is a whole tile, mip 1 (64 x 16) is the tail tile, per slice.
1383 EXPECT(ask_one(target(128, 32, DXGI_FORMAT_R8G8B8A8_UNORM, 2, 2)).SizeInBytes ==
1384 4 * 65536ull);
1385 // 257 x 32 with two mips: mip 0 is 3 tiles across, mip 1 is 129 x 16 (an odd extent rounds up): 2 tiles.
1386 EXPECT(ask_one(target(257, 32, DXGI_FORMAT_R8G8B8A8_UNORM, 1, 2)).SizeInBytes ==
1387 5 * 65536ull);
1388 // 2-byte B5G5R5A1 is a 256 x 128 tile: 257 x 64 takes two.
1389 EXPECT(ask_one(target(257, 64, DXGI_FORMAT_B5G5R5A1_UNORM, 1, 1)).SizeInBytes ==
1390 2 * 65536ull);
1391 // 16-byte RGBA32F is a 64 x 64 tile: 65 x 32 takes two.
1392 EXPECT(ask_one(target(65, 32, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 1)).SizeInBytes ==
1393 2 * 65536ull);
1394
1395 // Sums saturate where the PC device's do. A 2^26 x 2^26 RGBA32F target of 200 slices is 200 x 2^56 B (the PC mock
1396 // caps it at 4 GiB); two of them are past 2^64.
1397 const D3D12_RESOURCE_DESC huge = target(1ull << 26, 1u << 26, DXGI_FORMAT_R32G32B32A32_FLOAT, 200, 1);
1398 EXPECT(ask(&huge, 1).SizeInBytes == 200ull << 56);
1399 const D3D12_RESOURCE_DESC huge_two[] = {huge, huge};
1400 info = ask(huge_two, 2);
1401 EXPECT(info.SizeInBytes == 0xFFFFFFFFFFFF0000ull && info.Alignment == 65536);
1402
1403 EXPECT(release(device) == 0 && mock->refs == 0);
1404 std::printf("%s: render targets are laid out at their own alignments, with per-description offsets, mip chains "
1405 "and saturating sums\n", g_label);
1406 return 0;
1407}
1408
1283 1409
1284// ---- 7b. Device slot 25 hands the PC device clean descriptions and leaves the title's array alone ---- 1410// ---- 7b. Device slot 25 hands the PC device clean descriptions and leaves the title's array alone ----
1285 1411
@@ -1863,7 +1989,8 @@ int main(int argc, char** argv) {
1863 check_resource_allocation_info() || check_slot70() || check_committed_resource3_x() || 1989 check_resource_allocation_info() || check_slot70() || check_committed_resource3_x() ||
1864 check_descriptor_heap_hooks() || 1990 check_descriptor_heap_hooks() ||
1865 check_render_target_sizes() || 1991 check_render_target_sizes() ||
1866 check_allocation_descriptions_cleaned()) 1992 check_allocation_descriptions_cleaned() ||
1993 check_render_target_layout())
1867 return 1; 1994 return 1;
1868 std::printf("xs_proxy_mock_test: PASS (%d allocations refused on purpose)\n", g_failed_news); 1995 std::printf("xs_proxy_mock_test: PASS (%d allocations refused on purpose)\n", g_failed_news);
1869 return 0; 1996 return 0;