Skip to content
Closed
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
20 changes: 13 additions & 7 deletions docs/runtime_imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,19 @@ policy "other" (none is counted as returning an `HRESULT`), and they come from s

The game executable imports two functions from `xg_xs.dll`, the console's texture layout library:
`XGSuggestSwizzleMode` (2 call sites, in the helper that fills a texture description) and `XGCreateTextureComputer`
(4 call sites, which use slots 1, 2, 8 and 10 of the returned object's vtable). The runtime has no model of the console
resource layout, so `runtime/shims/xg_xs.cpp` computes none: both return `E_NOTIMPL` and write no layout. Two of the
four `XGCreateTextureComputer` callers ignore the HRESULT and call through the object at once, so the shim still stores
a static object in the out pointer; its slot 1 (read as `Release`) does nothing, and every other slot up to 31 logs its
index and caller once and returns `E_NOTIMPL` without writing its arguments. The call shapes and the reasoning are at
the top of the file; `xg_xs_test` covers them. Textures whose layout the game asks for are therefore not laid out yet.
The table above was measured before this shim and still lists these two as unimplemented; they are served now.
(4 call sites, which use slots 1, 2, 8 and 10 of the returned object's vtable). `runtime/shims/xg_xs.cpp` answers them
from a model of the console's texture layouts, `runtime/src/console_texture_layout.h` (AMD addrlib's GFX10 rules for
the linear, 256 B, 4 KiB and 64 KiB modes: block shapes, mip tails, the micro tile patterns, a separate stencil plane),
ported with the computer's method table and layout structure from Marvisak/VIctory (GPL-3.0, `xg.rs` and `texture.rs`
at `d2a4bae1f`). `XGSuggestSwizzleMode` writes `0x100 | mode` (linear for 1D, `64KB_Z_X` for depth, `64KB_R_X` for
render targets, else `4KB_S`); the computer fills the layout (slot 2), gives an element's offset (slot 6), copies a
subresource into (slot 8) and out of (slot 9) tiled memory, and reports the 64 KiB tiling (slot 10). Not modelled:
compression metadata planes, and addressing or copies for the pipe/bank XOR modes and MSAA (`E_NOTIMPL`). A description
it cannot lay out still gets a computer in the out pointer with `E_INVALIDARG`, because two of the four callers ignore
the HRESULT and call through the object at once; that object answers every slot with `E_NOTIMPL` and writes nothing.
`xg_xs_test` covers the model (hand-checked addresses, mip tails, a bijection and round trip over every element) and the
shim's slots. The table above was measured before this shim and still lists these two as unimplemented; they are served
now.

Only two of these DLLs have a shim in the repository (`xmem.dll` and `d3d12_xs.dll`), and they are not complete:
they hold 8 of the 30 unimplemented imports. The other 22 are in five DLLs (`xg_xs.dll`, `XCRDAPI.dll`, `chad.dll`,
Expand Down
13 changes: 13 additions & 0 deletions runtime/shims/console_texture_size.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

// The console's size for a texture (xg_xs.cpp), for the D3D12 device proxy's GetResourceAllocationInfo. A pointer, so
// that the proxy does not link the xg_xs shim; null in a process without it.
#include <cstdint>

namespace rt::xg {

// `desc`: the title's 56-byte D3D12_RESOURCE_DESC. 0 when the shim cannot lay it out; else its bytes, and `alignment`.
using TextureSizeFn = std::uint64_t (*)(const void* desc, std::uint64_t& alignment);
inline TextureSizeFn texture_size_fn = nullptr;

} // namespace rt::xg
31 changes: 31 additions & 0 deletions runtime/shims/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
// for a trace that disagrees. The frame-event calls answer from a per-device token counter: there is no vblank source
// behind the device before a presenter exists, so WaitFrameEventX returns at once with a fresh non-zero token.
#include "d3d12_xs_device.h"
#include "console_texture_size.h"
#include "d3d12_trace.h"
#include "d3d12_xs_alloc_size.h"
#include "d3d12_xs_desc_clean.h"
Expand Down Expand Up @@ -2693,11 +2694,41 @@ void apply_console_placement(const D3D12_RESOURCE_DESC* descs, UINT count, D3D12
if (console.SizeInBytes >= info.SizeInBytes) info = console;
}

// The console's answer for textures it lays out itself: those whose layout is a swizzle mode (0x100 | mode) or left to
// the runtime (0, the mode XGSuggestSwizzleMode gives). The retail title sizes its streamed textures with slot 25 and
// computes offsets and lengths inside its texture archives from the answers, so a PC GPU's sizes send it to the wrong
// bytes (it asserted at game_scarlett_master.exe+0x4ffd879, 1.0.2.1). Every description must be such a texture, else
// `info` is left alone: render targets, depth, buffers and shapes xg_xs cannot lay out keep the PC answer.
bool console_texture_sizes(const D3D12_RESOURCE_DESC* descs, UINT count, D3D12_RESOURCE_ALLOCATION_INFO& info) {
if (!descs || !count || !rt::xg::texture_size_fn) return false;
UINT64 end = 0, alignment = 1;
for (UINT i = 0; i < count; ++i) {
const D3D12_RESOURCE_DESC& d = descs[i];
const UINT layout = static_cast<UINT>(d.Layout);
if (d.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER || (layout != 0 && (layout & ~0xffu) != 0x100) ||
(d.Flags & (D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)))
return false;
std::uint64_t own = 0;
const std::uint64_t size = rt::xg::texture_size_fn(&d, own);
if (!size || !own) return false;
end = (end + own - 1) / own * own + size;
alignment = (std::max)(alignment, own);
}
info.SizeInBytes = (end + alignment - 1) / alignment * alignment;
info.Alignment = alignment;
return true;
}

void* WINAPI proxy_get_resource_allocation_info(std::uint64_t a0, std::uint64_t a1, std::uint64_t visible_mask,
std::uint64_t count, const D3D12_RESOURCE_DESC* descs) {
note_slot(25);
const StructReturnCall call = struct_return_call(a0, a1);
ID3D12Device* const real = call.self->real;
// Streamed textures (console_texture_sizes) get the console's own sizes, in the console form only, as the render
// targets below: a PC-form caller compares with the PC device.
if (call.console_abi &&
console_texture_sizes(descs, static_cast<UINT>(count), *static_cast<D3D12_RESOURCE_ALLOCATION_INFO*>(call.ret)))
return call.ret;
// Normalize console descriptors in a copy. PC calls can also pass the Xbox tiled layout explicitly. Otherwise
// their flags and layout reach the host unchanged so the host can validate them.
const UINT desc_count = static_cast<UINT>(count);
Expand Down
Loading
Loading