Files
mandy/runtime/test/xg_xs_test.cpp
NotRexed bf87af1e96 xg_xs: XGSuggestSwizzleMode and XGCreateTextureComputer from the console layout
Ported from VIctory (GPL-3.0-or-later) victory-hle src/modules/xg.rs, on the layout of victory-formats texture.rs
that gfx10_tiling.h already carries (gfx10::ConsoleTexture): the same layout, layout-word rule and Series X/S pipe
tables the D3D12 layer detiles title textures with.

- XGSuggestSwizzleMode writes 0x100 | the suggested mode (VIctory's shortcut rule, noted).
- XGCreateTextureComputer returns a reference-counted computer. Vtable as VIctory has it: AddRef, Release (0x08),
  the layout (0x10, VIctory's LayoutOut: size, alignment, mips, planes, per-plane mips with offsets and pitches),
  the element offset (0x30), copies into (0x40) and out of (0x48) a subresource, the 64 KiB tiling (0x50); other
  slots log once and return E_NOTIMPL. A description it cannot lay out is E_INVALIDARG, but *out still gets a
  computer whose methods fail, for the two measured callers that do not check.

Before this, both returned E_NOTIMPL and the caller at game+0x979c40 took its failure path at 19.8 s into boot,
after which game+0x1e24e80 called through a null pointer.

xg_xs_test ports VIctory's xg.rs tests and adds layouts of known sizes, reference counting, every subresource of
an array and a volume mip, and a cross-check that a one-mip SW_64KB_D texture the computer writes is what
resource_detile.h detiles and tiles (every element size, BC1, BC7). resource_detile.h notes where it and the
console layout disagree (mip 0 is not at offset 0 with mips; VIctory reads 0x101 as SW_256B_S).

Tested: xg_xs_test, gfx10_tiling_test, resource_detile_test, d3d12_title_memory_test (no GPU-specific checks
changed). Not run: the game.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 00:51:48 -05:00

463 lines
23 KiB
C++

// Checks the registered xg_xs.dll shim (runtime/shims/xg_xs.cpp) through rt::shims::find, so this is the table the
// runtime installs. No game data: every texture here is a hand-made description and generated bytes.
//
// Ported from VIctory's tests in victory-hle xg.rs (element offsets follow the layout, suggestions follow the use, the
// layout and copies through the object, bad descriptions fail); the layout library's own tests (victory-formats
// texture.rs) are in gfx10_tiling_test. Added here: layouts with sizes worked out by hand, AddRef/Release counting,
// the computer a failed create still hands out, every subresource of an array, and a cross-check that a texture the
// computer lays out in SW_64KB_D is the one resource_detile.h's detile_surface_64k reads.
#include <windows.h>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <span>
#include <vector>
#include "shims.h"
#include "../src/resource_detile.h"
namespace {
struct ResourceDesc {
uint32_t dimension;
uint64_t alignment;
uint64_t width;
uint32_t height;
uint16_t depth_or_array;
uint16_t mips;
uint32_t format;
uint32_t samples;
uint32_t quality;
uint32_t layout;
uint32_t flags;
};
static_assert(sizeof(ResourceDesc) == 0x38);
struct MipOut {
uint64_t size, offset, slice_size;
uint32_t pitch_texels, pitch_bytes, alignment;
uint32_t padded[3], elements[2], texels[2];
uint32_t depth_or_array, samples, swizzle;
uint64_t bank_rotation[2];
uint32_t slice_depth, in_tail, tail_position;
};
struct PlaneOut {
uint32_t usage;
uint64_t size, slice_size, offset, alignment;
uint32_t element_bytes, standard_mips, tail_mips;
MipOut mips[15];
};
struct LayoutOut {
uint64_t size, alignment;
uint32_t mips, planes;
PlaneOut plane[4];
uint32_t dimension;
};
static_assert(sizeof(MipOut) == 0x70 && sizeof(PlaneOut) == 0x6c8 && sizeof(LayoutOut) == 0x1b40);
struct PackedMips {
uint8_t standard, packed;
uint32_t tiles, start;
};
struct SubresourceTiling {
uint32_t width;
uint16_t height, depth;
uint32_t start;
};
using CreateFn = HRESULT(__stdcall*)(const ResourceDesc*, void**);
using SuggestFn = HRESULT(__stdcall*)(const ResourceDesc*, uint32_t, uint32_t*);
using RefFn = ULONG(__stdcall*)(void*);
using LayoutFn = HRESULT(__stdcall*)(void*, LayoutOut*);
using OffsetFn = uint64_t(__stdcall*)(void*, uint32_t, uint32_t, uint64_t, uint32_t, uint32_t, uint32_t, uint32_t*);
using IntoFn = HRESULT(__stdcall*)(void*, uint8_t*, uint32_t, uint32_t, const uint8_t*, uint32_t, uint32_t);
using FromFn = HRESULT(__stdcall*)(void*, uint8_t*, uint32_t, uint32_t, uint32_t, const uint8_t*, uint32_t, uint32_t);
using TilingFn = HRESULT(__stdcall*)(void*, uint32_t*, PackedMips*, uint32_t (*)[3], uint32_t*, uint32_t,
SubresourceTiling*);
using UnusedFn = HRESULT(__stdcall*)(void*);
constexpr HRESULT kNotImpl = static_cast<HRESULT>(rt::shims::kENotImpl);
int g_checks = 0;
int g_failures = 0;
void check(bool condition, const char* what) {
++g_checks;
if (!condition) {
std::printf("FAIL %s\n", what);
++g_failures;
}
}
CreateFn g_create = nullptr;
SuggestFn g_suggest = nullptr;
ResourceDesc desc(uint64_t width, uint32_t height, uint16_t mips, uint32_t format, uint32_t layout) {
return {3, 0, width, height, 1, mips, format, 1, 0, layout, 0};
}
template <class Fn>
Fn slot(void* c, size_t index) {
return reinterpret_cast<Fn>((*static_cast<void* const* const*>(c))[index]);
}
void* create(const ResourceDesc& d) {
void* c = nullptr;
check(g_create(&d, &c) == S_OK && c, "XGCreateTextureComputer lays out the description");
return c;
}
ULONG release(void* c) { return slot<RefFn>(c, 1)(c); }
LayoutOut* layout_of(void* c, std::vector<LayoutOut>& room) {
room.assign(1, LayoutOut{});
check(slot<LayoutFn>(c, 2)(c, room.data()) == S_OK, "slot 2 (the layout) succeeds");
return room.data();
}
// Fills every subresource with its own bytes through slot 8, reads each back through slot 9, then reads them all again
// to see that no copy wrote over another subresource.
void round_trip_all(void* c, const LayoutOut& l, uint32_t slices, uint32_t element_bytes, const char* what) {
std::vector<uint8_t> tiled(size_t(l.size), 0);
std::vector<std::vector<uint8_t>> written;
const PlaneOut& p = l.plane[0];
bool ok = true;
for (uint32_t slice = 0; slice < slices; ++slice)
for (uint32_t mip = 0; mip < l.mips; ++mip) {
const MipOut& m = p.mips[mip];
const uint32_t row = m.elements[0] * element_bytes + 4; // a pitch wider than the row
std::vector<uint8_t> rows(size_t(row) * m.elements[1]);
for (size_t i = 0; i < rows.size(); ++i) rows[i] = uint8_t(i * 13 + mip * 7 + slice * 31 + (i >> 8));
const uint32_t sub = mip + slice * l.mips;
ok = ok && slot<IntoFn>(c, 8)(c, tiled.data(), 0, sub, rows.data(), row, 0) == S_OK;
std::vector<uint8_t> back(rows.size(), 0xcc);
ok = ok && slot<FromFn>(c, 9)(c, back.data(), 0, sub, 0, tiled.data(), row, 0) == S_OK;
for (uint32_t y = 0; y < m.elements[1]; ++y)
ok = ok && std::memcmp(back.data() + size_t(y) * row, rows.data() + size_t(y) * row,
size_t(m.elements[0]) * element_bytes) == 0;
written.push_back(std::move(rows));
}
size_t k = 0;
for (uint32_t slice = 0; slice < slices; ++slice)
for (uint32_t mip = 0; mip < l.mips; ++mip, ++k) {
const MipOut& m = p.mips[mip];
const uint32_t row = m.elements[0] * element_bytes + 4;
std::vector<uint8_t> back(written[k].size(), 0);
ok = ok && slot<FromFn>(c, 9)(c, back.data(), 0, mip + slice * l.mips, 0, tiled.data(), row, 0) == S_OK;
for (uint32_t y = 0; y < m.elements[1]; ++y)
ok = ok && std::memcmp(back.data() + size_t(y) * row, written[k].data() + size_t(y) * row,
size_t(m.elements[0]) * element_bytes) == 0;
}
check(ok, what);
}
void suggestions_follow_the_use() {
uint32_t out = 0;
ResourceDesc d = desc(256, 256, 1, 98, 0);
check(g_suggest(&d, 0, &out) == S_OK && out == 0x105, "suggestion: a plain texture is SW_4KB_S (0x105)");
d.flags = 1; // ALLOW_RENDER_TARGET
check(g_suggest(&d, 0, &out) == S_OK && out == 0x11b, "suggestion: a render target is SW_64KB_R_X (0x11b)");
d.flags = 2; // ALLOW_DEPTH_STENCIL
check(g_suggest(&d, 0, &out) == S_OK && out == 0x118, "suggestion: a depth target is SW_64KB_Z_X (0x118)");
d.dimension = 2;
check(g_suggest(&d, 0, &out) == S_OK && out == 0x100, "suggestion: 1D is linear (0x100)");
out = 0x1234;
check(g_suggest(nullptr, 0, &out) == E_INVALIDARG && out == 0x1234, "suggestion: no description is E_INVALIDARG");
check(g_suggest(&d, 0, nullptr) == E_INVALIDARG, "suggestion: no out pointer is E_INVALIDARG");
}
// Element offsets: linear rows of 1 KiB (128 elements of 8 bytes), 0 outside.
void element_offsets_follow_the_layout() {
void* c = create(desc(128, 4, 1, 11, 0x100));
if (!c) return;
const auto offset = slot<OffsetFn>(c, 6);
uint32_t bit = 7;
check(offset(c, 0, 0, 3, 1, 0, 0, &bit) == (128 + 3) * 8 && bit == 0, "element offset: (3, 1) of linear rows");
check(offset(c, 0, 0, 128, 0, 0, 0, nullptr) == 0, "element offset: 0 outside the texture");
check(offset(c, 1, 0, 0, 0, 0, 0, nullptr) == 0, "element offset: 0 for a plane the texture has not");
check(release(c) == 0, "element offset: the computer is released");
}
// The title's calls: layout, then a copy in and out of each mip, then the 64 KiB tiling.
void layout_and_copies_through_the_object() {
// 256x256 BC7, full chain, 64 KiB display mode.
void* c = create(desc(256, 256, 0, 98, 0x10a));
if (!c) return;
std::vector<LayoutOut> room;
const LayoutOut& l = *layout_of(c, room);
check(l.size == 131072 && l.alignment == 65536 && l.mips == 9 && l.planes == 1 && l.dimension == 3,
"layout: 256x256 BC7 in SW_64KB_D is two tiles, nine mips, one plane");
const PlaneOut& p = l.plane[0];
check(p.standard_mips == 1 && p.tail_mips == 8 && p.element_bytes == 16 && p.usage == 1,
"layout: one standard mip, eight in the tail, 16-byte elements");
check(p.mips[0].offset == 65536 && p.mips[0].pitch_bytes == 1024 && p.mips[0].pitch_texels == 256 &&
p.mips[0].swizzle == 10 && p.mips[0].size == 65536,
"layout: mip 0 is the second tile, 1 KiB rows");
check(p.mips[1].offset == 32768 && p.mips[1].in_tail == 1 && p.mips[1].swizzle == 2 && p.mips[1].alignment == 256,
"layout: mip 1 is 32 KiB into the tail, reported as SW_256B_D");
check(p.mips[8].texels[0] == 1 && p.mips[8].elements[0] == 1, "layout: mip 8 is one texel in one element");
const auto into = slot<IntoFn>(c, 8);
const auto from = slot<FromFn>(c, 9);
std::vector<uint8_t> tiled(size_t(l.size), 0);
bool ok = true;
for (uint32_t mip = 0; mip < 9; ++mip) {
const MipOut& m = p.mips[mip];
const uint32_t row = m.elements[0] * 16;
std::vector<uint8_t> rows(size_t(row) * m.elements[1]);
for (size_t i = 0; i < rows.size(); ++i) rows[i] = uint8_t(i ^ mip);
ok = ok && into(c, tiled.data(), 0, mip, rows.data(), row, 0) == S_OK;
std::vector<uint8_t> back(rows.size(), 0);
ok = ok && from(c, back.data(), 0, mip, 0, tiled.data(), row, 0) == S_OK && back == rows;
}
check(ok, "copies: every mip round-trips through slots 8 and 9");
check(into(c, tiled.data(), 0, 9, tiled.data(), 16, 0) == E_INVALIDARG,
"copies: a subresource past the array is E_INVALIDARG");
check(into(c, tiled.data(), 1, 0, tiled.data(), 16, 0) == E_INVALIDARG, "copies: a missing plane is E_INVALIDARG");
check(into(c, nullptr, 0, 0, tiled.data(), 1024, 0) == E_INVALIDARG, "copies: no tiled memory is E_INVALIDARG");
std::vector<uint8_t> small(16);
check(into(c, tiled.data(), 0, 0, small.data(), 8, 0) == E_INVALIDARG, "copies: a row pitch too small fails");
// 0x50: one tile for the tail, one for mip 0.
const auto tiling = slot<TilingFn>(c, 10);
uint32_t tiles = 0, shape[3] = {}, n = 4;
PackedMips packed{};
SubresourceTiling subs[4]{};
check(tiling(c, &tiles, &packed, &shape, &n, 0, subs) == S_OK, "tiling: a 64 KiB mode has a tiling");
check(tiles == 2 && packed.standard == 1 && packed.packed == 8 && packed.tiles == 1,
"tiling: two tiles, one standard mip, eight packed in one tile");
check(shape[0] == 256 && shape[1] == 256 && shape[2] == 1 && n == 1 && subs[0].start == 1 && subs[0].width == 1 &&
subs[0].height == 1 && subs[0].depth == 1,
"tiling: 256x256 texel tiles, mip 0 is tile 1");
check(release(c) == 0, "tiling: the computer is released");
// 4 KiB modes have no 64 KiB tiling.
c = create(desc(256, 256, 0, 98, 0x105));
if (!c) return;
check(slot<TilingFn>(c, 10)(c, &tiles, nullptr, nullptr, nullptr, 0, nullptr) == E_FAIL,
"tiling: a 4 KiB mode is E_FAIL");
check(release(c) == 0, "tiling: the 4 KiB computer is released");
}
void bad_descriptions_fail() {
void* c = nullptr;
ResourceDesc bad = desc(256, 256, 1, 0, 0x10a);
check(g_create(&bad, &c) == E_INVALIDARG, "bad: format 0 is E_INVALIDARG");
bad = desc(256, 256, 1, 28, 0x103);
check(g_create(&bad, &c) == E_INVALIDARG, "bad: swizzle mode 3 (not on GFX10) is E_INVALIDARG");
bad = desc(256, 256, 1, 28, 0x205);
check(g_create(&bad, &c) == E_INVALIDARG, "bad: a layout word other than 0, 1 or 0x1xx is E_INVALIDARG");
bad = desc(1u << 15, 1, 1, 28, 0x105);
check(g_create(&bad, &c) == E_INVALIDARG, "bad: wider than the GPU's 16384 is E_INVALIDARG");
bad = desc(256, 256, 1, 28, 0x105);
bad.dimension = 1; // a buffer
check(g_create(&bad, &c) == E_INVALIDARG, "bad: a buffer is E_INVALIDARG");
bad = desc(1u << 14, 1u << 14, 16, 28, 0x105);
check(g_create(&bad, &c) == E_INVALIDARG, "bad: 16 mips do not fit the layout structure");
check(g_create(nullptr, &c) == E_INVALIDARG, "bad: no description is E_INVALIDARG");
check(g_create(&bad, nullptr) == E_INVALIDARG, "bad: no out pointer is E_INVALIDARG");
// Two measured callers use *out without checking: a failed create still stores a computer whose methods fail.
check(c != nullptr, "bad: a failed create still stores a computer");
if (!c) return;
std::vector<LayoutOut> room(1);
std::memset(room.data(), 0xcd, sizeof(LayoutOut));
check(slot<LayoutFn>(c, 2)(c, room.data()) == E_INVALIDARG &&
reinterpret_cast<const uint8_t*>(room.data())[0] == 0xcd,
"bad: the failed computer's layout fails and writes nothing");
uint8_t buffer[64] = {};
check(slot<IntoFn>(c, 8)(c, buffer, 0, 0, buffer, 16, 0) == E_INVALIDARG, "bad: the failed computer's copy fails");
check(slot<OffsetFn>(c, 6)(c, 0, 0, 0, 0, 0, 0, nullptr) == 0, "bad: the failed computer's offsets are 0");
uint32_t tiles = 7;
check(slot<TilingFn>(c, 10)(c, &tiles, nullptr, nullptr, nullptr, 0, nullptr) == E_INVALIDARG && tiles == 7,
"bad: the failed computer's tiling fails");
check(release(c) == 1 && release(c) == 1, "bad: the failed computer counts no references");
}
// Layouts whose sizes are worked out by hand from addrlib's rules.
void known_layouts() {
std::vector<LayoutOut> room;
// 64x64 RGBA8 linear: rows aligned to 256 bytes (64 elements), 16 KiB.
if (void* c = create(desc(64, 64, 1, 28, 1))) {
const LayoutOut& l = *layout_of(c, room);
check(l.size == 16384 && l.alignment == 256 && l.plane[0].mips[0].pitch_bytes == 256,
"known: 64x64 RGBA8 linear is 16 KiB of 256-byte rows");
check(release(c) == 0, "known: linear released");
}
// 1024x1024 BC1 in SW_4KB_S, full chain: 32x16-element blocks; mips 3 to 0 take 2, 8, 32 and 128 blocks after one
// tail block holding mips 4-10.
if (void* c = create(desc(1024, 1024, 0, 71, 0x105))) {
const LayoutOut& l = *layout_of(c, room);
const PlaneOut& p = l.plane[0];
check(l.size == 4096 + 8192 + 32768 + 131072 + 524288 && l.alignment == 4096 && l.mips == 11,
"known: 1024x1024 BC1 SW_4KB_S is 684 KiB, 11 mips");
check(p.standard_mips == 4 && p.tail_mips == 7 && p.mips[3].offset == 4096 && p.mips[0].offset == 176128,
"known: BC1 mips 0-3 follow the tail, smallest first");
round_trip_all(c, l, 1, 8, "known: every BC1 mip round-trips");
check(release(c) == 0, "known: BC1 released");
}
// 1920x1080 RGBA8 render target, layout word 0: SW_64KB_R_X, 15x9 tiles of 128x128.
{
ResourceDesc d = desc(1920, 1080, 1, 28, 0);
d.flags = 1;
if (void* c = create(d)) {
const LayoutOut& l = *layout_of(c, room);
check(l.size == 135ull * 65536 && l.plane[0].mips[0].swizzle == 27 && l.plane[0].mips[0].pitch_bytes == 7680,
"known: a 1080p RGBA8 target is SW_64KB_R_X in 135 tiles");
round_trip_all(c, l, 1, 4, "known: the R_X target round-trips");
check(release(c) == 0, "known: R_X released");
}
}
// 1920x1080 D32_FLOAT_S8X24_UINT depth: two planes, 4-byte depth in 128x128 blocks, 1-byte stencil in 256x256.
{
ResourceDesc d = desc(1920, 1080, 1, 20, 0);
d.flags = 2;
if (void* c = create(d)) {
const LayoutOut& l = *layout_of(c, room);
check(l.planes == 2 && l.plane[0].usage == 7 && l.plane[1].usage == 8,
"known: depth/stencil is a depth plane and a stencil plane");
check(l.plane[0].size == 1920ull * 1152 * 4 && l.plane[1].offset == l.plane[0].size &&
l.plane[1].size == 2048ull * 1280 && l.size == l.plane[1].offset + l.plane[1].size,
"known: the stencil plane follows the depth plane");
std::vector<uint8_t> tiled(size_t(l.size)), rows(size_t(1920) * 4 * 1080);
check(slot<IntoFn>(c, 8)(c, tiled.data(), 0, 0, rows.data(), 1920 * 4, 0) == kNotImpl,
"known: Z_X has no address equation here, so its copies are E_NOTIMPL");
check(release(c) == 0, "known: depth released");
}
}
// A 3-slice 64x64 RGBA8 array in SW_4KB_S, full chain: each slice holds its chain; every subresource is its own.
{
ResourceDesc d = desc(64, 64, 0, 28, 0x105);
d.depth_or_array = 3;
if (void* c = create(d)) {
const LayoutOut& l = *layout_of(c, room);
check(l.mips == 7 && l.size == 3 * l.plane[0].slice_size && l.plane[0].mips[0].depth_or_array == 3,
"known: an array is its slices one after the other");
round_trip_all(c, l, 3, 4, "known: every subresource of the array round-trips on its own");
check(release(c) == 0, "known: array released");
}
}
// A 32x32x16 RGBA8 volume in SW_4KB_S (thick on GFX10: 8x16x8-element blocks), full chain of 6 mips.
{
ResourceDesc d = desc(32, 32, 0, 28, 0x105);
d.dimension = 4;
d.depth_or_array = 16;
if (void* c = create(d)) {
const LayoutOut& l = *layout_of(c, room);
check(l.mips == 6 && l.dimension == 4 && l.plane[0].mips[0].depth_or_array == 16,
"known: a volume's mip 0 is 16 deep");
const MipOut& m = l.plane[0].mips[1];
const uint32_t row = m.elements[0] * 4, depth = row * m.elements[1];
std::vector<uint8_t> tiled(size_t(l.size)), in(size_t(depth) * m.depth_or_array), out(in.size());
for (size_t i = 0; i < in.size(); ++i) in[i] = uint8_t(i * 5 + 1);
check(slot<IntoFn>(c, 8)(c, tiled.data(), 0, 1, in.data(), row, depth) == S_OK &&
slot<FromFn>(c, 9)(c, out.data(), 0, 1, 0, tiled.data(), row, depth) == S_OK && in == out,
"known: a volume mip's 8 depth slices round-trip");
check(release(c) == 0, "known: volume released");
}
}
// 0x101: the layout word decodes as 0x100 | mode, so it is SW_256B_S (resource_detile.h reads it as SW_64KB_D).
if (void* c = create(desc(64, 64, 1, 28, 0x101))) {
const LayoutOut& l = *layout_of(c, room);
check(l.alignment == 256 && l.plane[0].mips[0].swizzle == 1, "known: layout word 0x101 is SW_256B_S");
check(release(c) == 0, "known: 0x101 released");
}
}
void references_count() {
void* c = create(desc(16, 16, 1, 28, 0x105));
if (!c) return;
check(slot<RefFn>(c, 0)(c) == 2 && release(c) == 1 && release(c) == 0, "references: AddRef and Release count");
c = create(desc(16, 16, 1, 28, 0x105));
if (!c) return;
check(slot<UnusedFn>(c, 3)(c) == kNotImpl && slot<UnusedFn>(c, 31)(c) == kNotImpl,
"unused slots (3 and the last, 31) log once and return E_NOTIMPL");
check(release(c) == 0, "references: released");
}
// A one-mip SW_64KB_D texture the computer lays out and fills is the one resource_detile.h's detile_surface_64k reads
// (and tile_surface_64k writes): same size, same bytes, for every element size and block-compressed formats.
void detiler_agrees() {
struct Case {
uint32_t format, element_bytes, block, width, height;
};
const Case cases[] = {
{61, 1, 1, 300, 130}, {54, 2, 1, 300, 130}, {28, 4, 1, 300, 130}, {10, 8, 1, 300, 130},
{2, 16, 1, 300, 130}, {71, 8, 4, 300, 130}, {98, 16, 4, 300, 130}, {28, 4, 1, 1, 1},
{28, 4, 1, 1920, 1080}, {98, 16, 4, 256, 256},
};
for (const Case& k : cases) {
void* c = create(desc(k.width, k.height, 1, k.format, 0x10a));
if (!c) continue;
std::vector<LayoutOut> room;
const LayoutOut& l = *layout_of(c, room);
const MipOut& m = l.plane[0].mips[0];
const uint32_t row = m.elements[0] * k.element_bytes;
std::vector<uint8_t> rows(size_t(row) * m.elements[1]);
for (size_t i = 0; i < rows.size(); ++i) rows[i] = uint8_t(i * 7 + (i >> 9) + 3);
std::vector<uint8_t> tiled(size_t(l.size), 0);
check(l.size == leonida::tiled_surface_size_64k(k.width, k.height, k.element_bytes, k.block),
"cross-check: the computer's size is tiled_surface_size_64k");
check(slot<IntoFn>(c, 8)(c, tiled.data(), 0, 0, rows.data(), row, 0) == S_OK, "cross-check: slot 8 tiles");
std::vector<uint8_t> linear(rows.size(), 0xcc);
check(leonida::detile_surface_64k(std::as_bytes(std::span(tiled)), std::as_writable_bytes(std::span(linear)),
k.width, k.height, k.element_bytes, k.block) &&
linear == rows,
"cross-check: detile_surface_64k reads back what the computer wrote");
std::vector<uint8_t> retiled(tiled.size(), 0);
check(leonida::tile_surface_64k(std::as_bytes(std::span(rows)), std::as_writable_bytes(std::span(retiled)),
k.width, k.height, k.element_bytes, k.block) &&
retiled == tiled,
"cross-check: tile_surface_64k writes the bytes the computer writes");
release(c);
}
// With mips, the console puts the tail first and mip 0 last: detile_surface_64k reads mip 0 from the mip's offset
// in the layout, not from the texture's start.
void* c = create(desc(256, 256, 0, 98, 0x10a));
if (!c) return;
std::vector<LayoutOut> room;
const LayoutOut& l = *layout_of(c, room);
std::vector<uint8_t> rows(64 * 16 * 64), tiled(size_t(l.size), 0), linear(rows.size());
for (size_t i = 0; i < rows.size(); ++i) rows[i] = uint8_t(i * 11 + 5);
check(slot<IntoFn>(c, 8)(c, tiled.data(), 0, 0, rows.data(), 64 * 16, 0) == S_OK, "cross-check: mip 0 tiles");
const auto span = std::as_bytes(std::span(tiled));
check(leonida::detile_surface_64k(span.subspan(size_t(l.plane[0].mips[0].offset)),
std::as_writable_bytes(std::span(linear)), 256, 256, 16, 4) &&
linear == rows,
"cross-check: with mips, mip 0 is detiled from its layout offset (64 KiB in)");
check(leonida::detile_surface_64k(span, std::as_writable_bytes(std::span(linear)), 256, 256, 16, 4) &&
linear != rows,
"cross-check: with mips, the texture's start is the tail, not mip 0");
release(c);
}
} // namespace
int main() {
g_create = reinterpret_cast<CreateFn>(rt::shims::find("xg_xs.dll", "XGCreateTextureComputer"));
g_suggest = reinterpret_cast<SuggestFn>(rt::shims::find("xg_xs.dll", "XGSuggestSwizzleMode"));
check(g_create != nullptr && g_suggest != nullptr, "both exports are registered");
check(rt::shims::owns_dll("xg_xs.dll"), "the registry owns xg_xs.dll");
if (!g_create || !g_suggest) return 1;
const struct {
const char* name;
void (*run)();
} tests[] = {
{"suggestions_follow_the_use", suggestions_follow_the_use},
{"element_offsets_follow_the_layout", element_offsets_follow_the_layout},
{"layout_and_copies_through_the_object", layout_and_copies_through_the_object},
{"bad_descriptions_fail", bad_descriptions_fail},
{"known_layouts", known_layouts},
{"references_count", references_count},
{"detiler_agrees", detiler_agrees},
};
for (const auto& t : tests) {
const int before = g_failures;
t.run();
std::printf("%s %s\n", g_failures == before ? "PASS" : "FAIL", t.name);
}
std::printf("%d checks, %d failures\n", g_checks, g_failures);
return g_failures ? 1 : 0;
}