diff --git a/runtime/shims/xg_xs.cpp b/runtime/shims/xg_xs.cpp index b45fbf689a..2d13533087 100644 --- a/runtime/shims/xg_xs.cpp +++ b/runtime/shims/xg_xs.cpp @@ -12,26 +12,38 @@ // stack structure (2 sites), slot 8 with six arguments in a loop over subresources (1 site) and slot 10 with // seven arguments (1 site). No site reads the return value of slots 2, 8 or 10. // +// Slot 10 is GetResourceTiling (its section below): it answers the PC device's 64 KiB tiling of the description, the +// numbers ID3D12Device::GetResourceTiling gives for it, so the title's mip counts are the description's. +// // The runtime has no model of the console's resource layout (runtime/src/gfx10_tiling.h covers two swizzle modes of -// one 2D level, without mips or the layout structure slot 2 fills), so nothing here computes a layout: +// one 2D level, without mips or the layout structure slot 2 fills), so nothing else here computes a layout: // - XGSuggestSwizzleMode returns E_NOTIMPL and writes nothing, so the field keeps the 0 the caller put there. // - XGCreateTextureComputer returns E_NOTIMPL, but still stores a computer in *out. The two checking callers then // fail cleanly; the two that do not check would otherwise call through a null pointer. COM says *out is null on // failure; this breaks that on purpose, because the measured callers need it. -// - Slot 0 and slot 1 count nothing (the computer is one static object). Every other slot, up to kSlots, prints its +// - Slot 0 and slot 1 count nothing (a computer lives for the run). Every other slot but 10, up to kSlots, prints its // index and caller once and returns E_NOTIMPL without writing its arguments, so a caller's own zero-filled outputs // stay zero and a stack structure the caller did not fill stays as it was. -// Hypothesis, not measured: slots 0 and 1 being AddRef and Release; the meaning of slots 2, 8 and 10; that callers +// Hypothesis, not measured: slots 0 and 1 being AddRef and Release; the meaning of slots 2 and 8; that callers // reach no slot above kSlots - 1 (the computer is also stored in an object at one site and may be called later). #include +#include #include +#include #include #include #include #include +#include +#include +#include +#include +#include +#include #include +#include "../src/resource_tiling.h" #include "shims.h" namespace { @@ -59,25 +71,119 @@ HRESULT __stdcall computer_slot(void*) { return kNotImpl; } +// A computer keeps the description it was made from. The description starts with a D3D12_RESOURCE_DESC: measured at +// the game's XGCreateTextureComputer call sites under gta6rt --run, the first 52 bytes are a dimension (3 for 2D, +// 4 for 3D), an alignment, width, height, depth or array size, mip levels, a DXGI format, a sample count, the +// console layout word in Layout and console bits in Flags; the console's own fields follow. Computers are never +// freed (Release counts nothing, as before: no site calls AddRef and one stores the computer for later), so one is +// kept per distinct description and reused. +struct Computer { + const void* const* vtable; + D3D12_RESOURCE_DESC desc; + bool has_desc; +}; + +// Bytes per element and texels per block side for the formats D3D12 tiles with the standard 64 KiB tile shapes +// (public DXGI values). Formats without a measured tile shape (12-byte texels, the depth-stencil formats with a +// stencil plane, the packed 4:2:2 and 1-bit formats) return false, as the Vulkan layer's GetResourceTiling does. +bool element_size(DXGI_FORMAT format, uint32_t& bytes, uint32_t& block) { + block = 1; + const unsigned f = static_cast(format); + if (f >= 1 && f <= 4) bytes = 16; // R32G32B32A32 + else if (f >= 9 && f <= 18) bytes = 8; // R16G16B16A16, R32G32 + else if ((f >= 23 && f <= 43) || f == 67 || (f >= 87 && f <= 93)) bytes = 4; // 32-bit, R9G9B9E5, B8G8R8A8/X8 + else if ((f >= 48 && f <= 59) || f == 85 || f == 86 || f == 115) bytes = 2; // R8G8, R16, B5G6R5, B5G5R5A1, B4G4R4A4 + else if (f >= 60 && f <= 65) bytes = 1; // R8, A8 + else if ((f >= 70 && f <= 72) || (f >= 79 && f <= 81)) bytes = 8, block = 4; // BC1, BC4 + else if ((f >= 73 && f <= 78) || (f >= 82 && f <= 84) || (f >= 94 && f <= 99)) bytes = 16, block = 4; // BC2, BC3, BC5, BC6H, BC7 + else return false; + return true; +} + +bool computer_tiling(const Computer& c, leonida::ResourceTiling& out) { + if (!c.has_desc) return false; + const D3D12_RESOURCE_DESC& d = c.desc; + if (d.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D && d.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE3D) return false; + leonida::TilingDesc t{}; + t.kind = d.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D ? leonida::TiledKind::texture3d : leonida::TiledKind::texture2d; + t.width = d.Width; + t.height = d.Height; + t.depth_or_layers = d.DepthOrArraySize; + t.samples = d.SampleDesc.Count ? d.SampleDesc.Count : 1; + if (!d.Width || !d.Height || !d.DepthOrArraySize || !element_size(d.Format, t.element_bytes, t.block)) return false; + t.mips = d.MipLevels; + if (!t.mips) { // 0 is the full chain, as in D3D12 + uint64_t largest = std::max(d.Width, d.Height); + if (t.kind == leonida::TiledKind::texture3d) largest = std::max(largest, d.DepthOrArraySize); + while (largest >> t.mips) ++t.mips; + } + return leonida::resource_tiling(t, out); +} + +// Slot 10: GetResourceTiling(total tiles, packed mip info, tile shape, subresource tiling count in/out, first, +// subresource tilings), the arguments of ID3D12Device::GetResourceTiling without the resource. Measured at the one call +// site: four out pointers to zeroed 4-byte and 12-byte locals, a count of 16, first 0 and an array; after the call the +// title reads the packed mip info's two counts (standard and packed mips) and adds them. The answer is the PC +// device's tiling of the same description (src/resource_tiling.h, as the Vulkan layer's GetResourceTiling gives it), +// not the console's: on the PC the texture is a PC resource. A description it cannot tile keeps the old answer: +// E_NOTIMPL with nothing written. +HRESULT __stdcall computer_get_resource_tiling(Computer* self, UINT* total, D3D12_PACKED_MIP_INFO* packed, + D3D12_TILE_SHAPE* shape, UINT* count, UINT first, + D3D12_SUBRESOURCE_TILING* tilings) { + leonida::ResourceTiling tiling; + if (!self || !computer_tiling(*self, tiling)) { + static std::atomic warned{false}; + warn_once(warned, "texture computer slot ", 10, _ReturnAddress()); + return kNotImpl; + } + if (total) *total = tiling.total_tiles; + if (packed) + *packed = D3D12_PACKED_MIP_INFO{static_cast(tiling.standard_mips), static_cast(tiling.packed_mips), + tiling.packed_tiles, tiling.packed_start}; + if (shape) *shape = D3D12_TILE_SHAPE{tiling.shape.width, tiling.shape.height, tiling.shape.depth}; + if (count) { + const size_t available = first < tiling.subresources.size() ? tiling.subresources.size() - first : 0; + const UINT n = tilings ? static_cast(std::min(*count, available)) : 0; + for (UINT i = 0; i < n; ++i) { + const leonida::SubresourceTiling& s = tiling.subresources[first + i]; + tilings[i] = D3D12_SUBRESOURCE_TILING{s.width, static_cast(s.height), static_cast(s.depth), s.start}; + } + *count = n; + } + return S_OK; +} + template constexpr auto make_vtable(std::index_sequence) { return std::array{ - (Slot == 0 ? reinterpret_cast(&computer_add_ref) - : Slot == 1 ? reinterpret_cast(&computer_release) - : reinterpret_cast(&computer_slot))...}; + (Slot == 0 ? reinterpret_cast(&computer_add_ref) + : Slot == 1 ? reinterpret_cast(&computer_release) + : Slot == 10 ? reinterpret_cast(&computer_get_resource_tiling) + : reinterpret_cast(&computer_slot))...}; } const auto kVtable = make_vtable(std::make_index_sequence{}); -struct Computer { - const void* const* vtable; -}; -Computer g_computer{kVtable.data()}; +Computer g_computer{kVtable.data(), {}, false}; // for a call without a description + +// One computer per distinct description, kept for the run (see Computer). +Computer* computer_for(const void* desc) { + if (!desc) return &g_computer; + D3D12_RESOURCE_DESC key{}; + std::memcpy(&key, desc, sizeof(key)); + std::string bytes(reinterpret_cast(&key), sizeof(key)); + static std::mutex mutex; + static std::unordered_map> computers; + const std::lock_guard lock(mutex); + std::unique_ptr& slot = computers[bytes]; + if (!slot) slot.reset(new (std::nothrow) Computer{kVtable.data(), key, true}); + return slot ? slot.get() : &g_computer; +} -HRESULT __stdcall XGCreateTextureComputer(const void* /*desc*/, void** out) { +HRESULT __stdcall XGCreateTextureComputer(const void* desc, void** out) { static std::atomic warned{false}; warn_once(warned, "XGCreateTextureComputer", -1, _ReturnAddress()); if (!out) return E_POINTER; - *out = &g_computer; // see the top of the file: two measured callers use *out without checking the HRESULT + *out = computer_for(desc); // see the top of the file: two measured callers use *out without checking the HRESULT return kNotImpl; } diff --git a/runtime/test/xg_xs_test.cpp b/runtime/test/xg_xs_test.cpp index 7db9b60962..7d624ae334 100644 --- a/runtime/test/xg_xs_test.cpp +++ b/runtime/test/xg_xs_test.cpp @@ -2,14 +2,17 @@ // // Both exports are resolved through rt::shims::find, so this is the table the runtime installs. The checks: both // return E_NOTIMPL, XGSuggestSwizzleMode leaves the caller's field alone, XGCreateTextureComputer still hands out a -// computer (two measured callers use it without checking the HRESULT), and the computer's slots called at the -// measured sites (1, 2, 8, 10) return without writing what their arguments point to. +// computer (two measured callers use it without checking the HRESULT), slots 2 and 8 (and slot 10 on a description it +// cannot tile) return without writing what their arguments point to, and slot 10 answers the PC tiling of a +// description it can tile (GetResourceTiling, compared with src/resource_tiling.h). #include +#include #include #include #include +#include "../src/resource_tiling.h" #include "shims.h" namespace { @@ -80,6 +83,69 @@ int main() { "slot 10 leaves the caller's zero-filled outputs as they were"); check(reinterpret_cast(vtable[31])(computer, layout) == kNotImpl, "the last slot (31) is traced too"); + // Slot 10 on a description it can tile: the first one measured at the game's call sites, 256 x 128 R8_UNORM, one + // mip, 4 KiB alignment, with the console layout word and console flag bits set. An 8-bit texel's 64 KiB tile is + // 256 x 256, so the texture is one tile and its one mip is standard (wider than half a tile). + D3D12_RESOURCE_DESC r8{}; + r8.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + r8.Alignment = 0x1000; + r8.Width = 256; + r8.Height = 128; + r8.DepthOrArraySize = 1; + r8.MipLevels = 1; + r8.Format = DXGI_FORMAT_R8_UNORM; + r8.SampleDesc.Count = 1; + r8.Layout = static_cast(0x105); + r8.Flags = static_cast(0x300000); + uint8_t console_desc[0x58]; + std::memset(console_desc, 0x5a, sizeof(console_desc)); // the console's own fields after the PC description + std::memcpy(console_desc, &r8, sizeof(r8)); + void* r8_computer = nullptr; + create(console_desc, &r8_computer); + void* const* r8_vtable = r8_computer ? *static_cast(r8_computer) : nullptr; + UINT total = 0, n = 16; + D3D12_PACKED_MIP_INFO packed{}; + D3D12_TILE_SHAPE shape{}; + D3D12_SUBRESOURCE_TILING tilings[16] = {}; + check(r8_vtable && reinterpret_cast(r8_vtable[10])(r8_computer, &total, &packed, &shape, &n, 0, tilings) == S_OK, + "slot 10 tiles a 256 x 128 R8_UNORM description"); + check(total == 1 && packed.NumStandardMips == 1 && packed.NumPackedMips == 0 && packed.NumTilesForPackedMips == 0, + "one tile, one standard mip, nothing packed"); + check(shape.WidthInTexels == 256 && shape.HeightInTexels == 256 && shape.DepthInTexels == 1, "the 8-bit tile shape"); + check(n == 1 && tilings[0].WidthInTiles == 1 && tilings[0].HeightInTiles == 1 && tilings[0].DepthInTiles == 1 && + tilings[0].StartTileIndexInOverallResource == 0, + "one subresource tiling, starting at tile 0"); + + // A full mip chain (MipLevels 0), from the second subresource on: the same numbers as src/resource_tiling.h gives. + D3D12_RESOURCE_DESC chain = r8; + chain.Width = 256; + chain.Height = 256; + chain.MipLevels = 0; + chain.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + void* chain_computer = nullptr; + create(&chain, &chain_computer); + leonida::TilingDesc expect_desc{}; + expect_desc.width = 256; + expect_desc.height = 256; + expect_desc.mips = 9; + expect_desc.element_bytes = 4; + leonida::ResourceTiling expect; + check(leonida::resource_tiling(expect_desc, expect), "the reference tiling of 256 x 256 RGBA8 with 9 mips"); + n = 16; + std::memset(tilings, 0, sizeof(tilings)); + check(reinterpret_cast(r8_vtable[10])(chain_computer, &total, &packed, &shape, &n, 2, tilings) == S_OK && + total == expect.total_tiles && packed.NumStandardMips == expect.standard_mips && + packed.NumPackedMips == expect.packed_mips && packed.NumTilesForPackedMips == expect.packed_tiles && + packed.StartTileIndexInOverallResource == expect.packed_start && shape.WidthInTexels == expect.shape.width, + "a full chain (MipLevels 0) answers the reference tiling"); + check(n == 7 && tilings[0].WidthInTiles == expect.subresources[2].width && + tilings[0].StartTileIndexInOverallResource == expect.subresources[2].start, + "the subresource tilings start at the first one asked for"); + + void* again = nullptr; + create(console_desc, &again); + check(again == r8_computer && chain_computer != r8_computer, "one computer per description, kept for the run"); + auto release = reinterpret_cast(vtable[1]); release(computer); release(computer);