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
13 changes: 10 additions & 3 deletions docs/d3d12x-shim.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,12 @@ B5G6R5, B5G5R5A1 or B4G4R4A4 take a substitute format; see [gpu-resources.md](gp
**Allocation info (slots 25, 108).** A texture desc in a console layout (0x100 + swizzle mode), or with layout
UNKNOWN and none of ALLOW_RENDER_TARGET, ALLOW_DEPTH_STENCIL, ALLOW_UNORDERED_ACCESS, gets the XG layout's size and
alignment ([xg.md](xg.md); UNKNOWN takes the suggested mode), as the console reports it, not the desktop driver's
(`ConsoleAllocationSizes`; other descs, or a query mixing them, keep the desktop answer). The game sizes streamed
(`ConsoleAllocationSizes`). So does a single-sample render target, depth target or UAV texture with layout UNKNOWN:
the game places those in its 2.2 GiB graphics heap at the size slot 108 answers (0x140abfad0), while the shim backs
them with desktop textures of their own. AMD's driver (RX 7800 XT) adds its compression metadata, 0x1330000 bytes for
a 2560x1440 depth target where XG gives 0xb40000; with the desktop sizes the heap held 2050 MB of its 2199 MB after
boot and the story load stopped on a 46 MiB request (the allocator's fatal path, 0x142219650). Buffers, multisampled
textures, or a query mixing them keep the desktop answer. The game sizes streamed
textures without size fields in their header this way (0x1451fb8f3 through 0x140343e80, desc built without a
layout), stores the size in 4 KiB units in its streaming table (0x149c3e1d0, 0x38-byte entries, words +0x18 offsets
and +0x20 sizes) and reads exactly that many bytes from the archive (0x1443c4850); the archive's compressed chunks
Expand Down Expand Up @@ -408,8 +413,10 @@ in a boot split; the elements past a cut read 0 until such a buffer is one deskt
**Committed resources (slot 127).** Created in the state of the initial barrier layout (buffers COMMON), heap flags
0 (the game passes 8). On the console D3D12.X takes their memory from the title's XMemAlloc hook, and the game checks
it: around its two 4K render targets (0x14247ad10) it pushes a hook for D3D allocations with page-size field 2 that
hands out a 64 MiB region, and breaks into the debugger unless all but 2 MiB of it was used. The shim allocates the
desktop size with attributes 0x11808000 (allocator 128, page size 2, 64 KiB alignment) and frees it with the resource.
hands out a 64 MiB region, and breaks into the debugger unless all but 2 MiB of it was used. The shim allocates a
texture's XG console size (a desktop driver pads: an RDNA3 card asks 0x2010000 bytes per 4K target, and two overrun the
region) and a buffer's desktop size, with attributes 0x11808000 (allocator 128, page size 2, 64 KiB alignment), and
frees it with the resource.

**Root signatures.** `D3D12SerializeVersionedRootSignature` drops the flag bits above 0xFFF (`XboxBvhBuilder.dll`
passes 0x3000000) and logs the serializer's message when it still fails.
Expand Down
48 changes: 44 additions & 4 deletions src/FiveStar.Loader/Shims/D3D12X/ConsoleAllocationSizes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ internal static unsafe class ConsoleAllocationSizes
/// <summary>ALLOW_RENDER_TARGET, ALLOW_DEPTH_STENCIL and ALLOW_UNORDERED_ACCESS.</summary>
private const int TargetFlags = 0x7;

/// <summary>DXGI_SAMPLE_DESC.Count of a texture that is not multisampled.</summary>
private const uint SingleSample = 1;

/// <summary>
/// Replaces <paramref name="result"/> (and <paramref name="infos"/>, when given) with the console sizes when every
/// desc is a texture in a console layout; false, leaving them as they are, otherwise.
/// desc is a texture the console sizes with XG (<see cref="IsConsoleSized"/>); false, leaving them as they are,
/// otherwise.
/// </summary>
public static bool TryApply(ResourceDescription* descriptions, uint count, ResourceAllocationInfo* result, ResourceAllocationInfo1* infos)
{
if (count == 0 || !AllConsoleTextures(descriptions, count))
if (count == 0 || !AllConsoleSized(descriptions, count))
{
return false;
}
Expand Down Expand Up @@ -53,6 +57,25 @@ public static bool TryApply(ResourceDescription* descriptions, uint count, Resou
return true;
}

/// <summary>
/// The console size of a texture the game creates committed, render and depth targets included (XG lays those out
/// with the mode it suggests); false for buffers and multisampled textures, which keep the desktop size.
/// </summary>
public static bool TryCommittedTextureSize(ResourceDescription* description, out ulong size)
{
size = 0;

if (description->Dimension == ResourceDescription.BufferDimension || description->SampleCount > SingleSample)
{
return false;
}

XgLayoutBuilder.Build(*(XgResourceDesc*)description, out var surface);
size = surface.Size;

return true;
}

/// <summary>
/// Whether a desc is a texture the console lays out with XG: its layout field carries a swizzle mode, or it is
/// UNKNOWN on a texture that is neither render target, depth target nor UAV (XG suggests the mode; the game sizes
Expand All @@ -68,11 +91,28 @@ public static bool IsConsoleTexture(in ResourceDescription description)
return (uint)description.Layout >= XgSwizzleModes.ModeBase || (description.Layout == UnknownLayout && (description.Flags & TargetFlags) == 0);
}

private static bool AllConsoleTextures(ResourceDescription* descriptions, uint count)
/// <summary>
/// Whether the console reports XG's size for a desc: a texture in a console layout, or a single-sample render
/// target, depth target or UAV texture without a layout. The game places those in its 2.2 GiB graphics heap at the
/// size slot 108 answers (0x140abfad0), while the shim backs them with desktop textures of their own, so the
/// desktop driver's size only takes heap: AMD's (RX 7800 XT) adds its compression metadata, 0x1330000 bytes for a
/// 2560x1440 depth target where XG gives 0xb40000, and the story load ran the heap out at a 46 MiB request.
/// </summary>
public static bool IsConsoleSized(in ResourceDescription description)
{
if (IsConsoleTexture(description))
{
return true;
}

return description.Dimension != ResourceDescription.BufferDimension && description.Layout == UnknownLayout && description.SampleCount <= SingleSample;
}

private static bool AllConsoleSized(ResourceDescription* descriptions, uint count)
{
for (uint i = 0; i < count; i++)
{
if (!IsConsoleTexture(descriptions[i]))
if (!IsConsoleSized(descriptions[i]))
{
return false;
}
Expand Down
18 changes: 15 additions & 3 deletions src/FiveStar.Loader/Shims/D3D12X/XboxCommittedResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ namespace FiveStar.Loader.Shims.D3D12X;
/// Device slot 127, the committed resources. On the console D3D12.X takes their memory from the title's XMemAlloc
/// hook, and the game counts on it: around its render targets (0x14247ad10) it pushes a hook that hands out a 64 MiB
/// region for D3D allocations with page-size field 2, then breaks into the debugger unless all but 2 MiB of it went
/// to the resources. The shim makes the same allocation, of the desktop resource's size, and frees it with the
/// resource; the desktop resource itself lives in desktop memory.
/// to the resources. The shim makes the same allocation, of the console's size for a texture (a desktop driver adds
/// metadata of its own: 0x2010000 bytes for a 3840x2160 RGBA8 target on an RDNA3 card, and two of those overrun the
/// region) and of the desktop size for a buffer, and frees it with the resource; the desktop resource itself lives in
/// desktop memory.
/// </summary>
internal sealed unsafe class XboxCommittedResources
{
Expand Down Expand Up @@ -68,7 +70,7 @@ public int Create(nint device, void* properties, int heapFlags, ResourceDescript
if (!HResult.Failed(hr) && resource != null && *resource != 0)
{
var wrapper = (XboxResourceObject*)*resource;
wrapper->TitleMemory = _hooks.Allocate((nuint)AllocationSize(device, &desktop), ResourceAttributes);
wrapper->TitleMemory = _hooks.Allocate((nuint)TitleAllocationSize(device, description, &desktop), ResourceAttributes);
ResourceStates.Record(wrapper, desktop.Dimension, state, desktop.Flags);
}

Expand Down Expand Up @@ -110,6 +112,16 @@ public static uint ReleaseResource(XboxResourceObject* self)
}
}

private static ulong TitleAllocationSize(nint device, ResourceDescription* console, ResourceDescription* desktop)
{
if (ConsoleAllocationSizes.TryCommittedTextureSize(console, out ulong size))
{
return size;
}

return AllocationSize(device, desktop);
}

private static ulong AllocationSize(nint device, ResourceDescription* description)
{
ResourceAllocationInfo info;
Expand Down
45 changes: 45 additions & 0 deletions tests/FiveStar.Loader.Tests/ConsoleAllocationSizesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ public sealed unsafe class ConsoleAllocationSizesTests
private const int Rgba16Unorm = 11;
private const int ConsoleFlags = 0x300000;
private const int AllowRenderTarget = 1;
private const int AllowDepthStencil = 2;
private const int AllowUnorderedAccess = 4;
private const int D16S8 = 118;
private const uint Msaa4 = 4;
private const ulong DepthTargetXgBytes = 0xb40000;
private const int Rgba8Unorm = 28;
private const ulong RenderTargetRegion = 64ul << 20;
private const ulong RegionSlack = 2ul << 20;

/// <summary>A 64 KiB block holds 128x128 four-byte elements (docs/xg.md); 3840x2160 takes 30 x 17 of them.</summary>
private const ulong LargeBlockBytes = 0x10000;
private const ulong Blocks4kTarget = 30 * 17;

[Fact]
public void A_console_layout_texture_gets_its_xg_size_and_a_buffer_keeps_the_desktop_answer()
Expand Down Expand Up @@ -46,6 +58,39 @@ public void A_streamed_texture_without_a_layout_gets_the_suggested_4_kb_blocks()
Assert.Equal(0x4000ul, result.Size);

desc.Flags |= AllowRenderTarget;
Assert.True(ConsoleAllocationSizes.TryApply(&desc, 1, &result, null));
Assert.True(ConsoleAllocationSizes.TryCommittedTextureSize(&desc, out ulong targetSize));
Assert.Equal(targetSize, result.Size);

desc.SampleCount = Msaa4;
Assert.False(ConsoleAllocationSizes.TryApply(&desc, 1, &result, null));
}

[Fact]
public void A_depth_target_gets_its_xg_size_and_not_the_desktop_metadata()
{
// The 2560x1440 D16S8 depth targets the game places in its graphics heap: an RX 7800 XT answers 0x1330000
// bytes, XG 0xb40000 (the heap ran out at the story load with the desktop sizes).
var desc = new ResourceDescription { Dimension = Texture2D, Width = 2560, Height = 1440, DepthOrArraySize = 1, MipLevels = 1, Format = D16S8, SampleCount = 1, Flags = ConsoleFlags | AllowDepthStencil | AllowUnorderedAccess };
ResourceAllocationInfo result = default;

Assert.True(ConsoleAllocationSizes.TryApply(&desc, 1, &result, null));
Assert.Equal(DepthTargetXgBytes, result.Size);
}

[Fact]
public void Two_4k_render_targets_fill_the_region_the_game_hands_out_for_them()
{
// 0x14247ad10 creates two 3840x2160 RGBA8 targets from a 64 MiB region and breaks unless they fill all but
// 2 MiB of it; an RDNA3 desktop driver asks 0x2010000 bytes for each, which overruns it.

var desc = new ResourceDescription { Dimension = Texture2D, Width = 3840, Height = 2160, DepthOrArraySize = 1, MipLevels = 1, Format = Rgba8Unorm, SampleCount = 1, Flags = ConsoleFlags | AllowRenderTarget };

Assert.True(ConsoleAllocationSizes.TryCommittedTextureSize(&desc, out ulong size));
Assert.Equal(Blocks4kTarget * LargeBlockBytes, size);
Assert.InRange(2 * size, RenderTargetRegion - RegionSlack, RenderTargetRegion);

desc.Dimension = ResourceDescription.BufferDimension;
Assert.False(ConsoleAllocationSizes.TryCommittedTextureSize(&desc, out _));
}
}