diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f126693..b94babda 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -321,6 +321,31 @@ add_executable(test_texture_replacement tests/test_texture_replacement.cpp) target_link_libraries(test_texture_replacement PRIVATE p3p3ds_core) add_test(NAME p3p_texture_replacement COMMAND test_texture_replacement) +# Texture pack format round trip (#63): tools/texpack (Rust) writes packs from +# its fixtures, the runtime reader (C++) checks every texel. Needs cargo (the +# toolchain pinned in tools/texpack/rust-toolchain.toml); skipped without it. +add_executable(test_texture_pack_roundtrip tests/test_texture_pack_roundtrip.cpp) +target_link_libraries(test_texture_pack_roundtrip PRIVATE p3p3ds_core) +find_program(P3P_CARGO cargo) +if(P3P_CARGO) + set(_texpack_out "${CMAKE_CURRENT_BINARY_DIR}/texpack_roundtrip") + foreach(_format rgba8 auto) + add_test(NAME p3p_texpack_build_${_format} + COMMAND "${P3P_CARGO}" run --quiet --release --locked -- build + "${CMAKE_CURRENT_SOURCE_DIR}/tools/texpack/tests/fixtures" "${_texpack_out}/${_format}" --format ${_format}) + # From tools/texpack, so cargo finds its .cargo/config.toml and rust-toolchain.toml. + # One at a time: on a fresh runner both would install the pinned toolchain + # through rustup at once, and rustup's downloads race (CI failure on #64). + set_tests_properties(p3p_texpack_build_${_format} PROPERTIES + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/tools/texpack" FIXTURES_SETUP texpack_roundtrip_packs + RESOURCE_LOCK texpack_cargo) + endforeach() + add_test(NAME p3p_texpack_roundtrip COMMAND test_texture_pack_roundtrip "${_texpack_out}/rgba8" "${_texpack_out}/auto") + set_tests_properties(p3p_texpack_roundtrip PROPERTIES FIXTURES_REQUIRED texpack_roundtrip_packs) +else() + message(STATUS "cargo not found: texture pack round trip test skipped") +endif() + add_executable(test_sascore tests/test_sascore.cpp) target_link_libraries(test_sascore PRIVATE p3p3ds_core) add_test(NAME p3p_sascore COMMAND test_sascore) diff --git a/docs/CURRENT_STATE.md b/docs/CURRENT_STATE.md index baefbb7a..9a390257 100644 --- a/docs/CURRENT_STATE.md +++ b/docs/CURRENT_STATE.md @@ -2,6 +2,11 @@ This is the current source of truth, newest entries first. Evidence concerns ULUS-10512; each section says whether it was measured on the PC runner, in Azahar or on a real New 3DS. +## Texture pack format: Rust writer and C++ reader tested together (#63, 2026-11-05) + +- CTest `p3p_texpack_build_rgba8` / `p3p_texpack_build_auto` run `p3p-texpack build` on `tools/texpack/tests/fixtures` (`textures.ini` there), `p3p_texpack_roundtrip` (`tests/test_texture_pack_roundtrip.cpp`) reads both packs with the runtime's `TexturePack` and compares every texel with the fixture formulas after the format's rules (header, entry order, alignment, row order, tiling, format codes, channel packing, keep-original entry). Registered only when CMake finds `cargo` (Linux CI and local builds; not the MSYS2 Windows job). +- [VERIFIED] Passes on Windows (CTest 31/31). Three deliberate format changes each made it fail and were reverted: no row flip in `build.rs` (all RGBA8 texels differ), RGB565 green shifted in `pica.rs` (RGB565 entries differ), width read from the height field in `texture_pack.cpp` (pack rejected as invalid). + ## Texture packs replace textures on the PC; 3DS path built, untested (#23, 2026-11-05) - `core/src/ge/texture_pack.cpp`: `TexturePack` reads the New 3DS pack (`index.bin` checked: magic, version, sizes, sort order; texture data read on demand) and finds an entry in PPSSPP's lookup order; `TextureReplacer` keeps the CLUT bytes and maxSeenV state and is shared by both renderers. `SoftwareRenderer::set_texture_pack` samples the replacement with the PSP texture's normalised coordinates; PC runner `--texture-pack ` prints `[TEXPACK]`. New 3DS: `GpuRenderer::bind_texture` reads a replacement straight into a `C3D_Tex` of the pack's size and format when it uploads a texture (textures narrower than 8 texels keep the original); `platform/3ds/main.cpp` opens `sdmc:/p3p3ds/textures`, the report line `texpack:` shows count, replaced uploads, KiB and read time. diff --git a/tests/test_texture_pack_roundtrip.cpp b/tests/test_texture_pack_roundtrip.cpp new file mode 100644 index 00000000..fb19c84f --- /dev/null +++ b/tests/test_texture_pack_roundtrip.cpp @@ -0,0 +1,124 @@ +// Round trip between the two implementations of the New 3DS texture pack +// format (issue #63): `p3p-texpack build` (Rust, tools/texpack/src/build.rs) +// writes packs from tools/texpack/tests/fixtures (textures.ini there), this +// test reads them with the runtime's TexturePack (core/src/ge/texture_pack.cpp) +// and compares every texel with the formulas the fixture images were generated +// from (tools/texpack/tests/fixtures/make_fixtures.py). +// +// test_texture_pack_roundtrip +#include "p3p3ds/ge/texture_pack.hpp" + +#include +#include +#include + +using namespace p3p3ds::ge; + +namespace { +int failures = 0; +void check(bool ok, const std::string &what) { + if (!ok) { + std::printf("FAIL: %s\n", what.c_str()); + ++failures; + } +} + +struct Rgba { + std::uint32_t r, g, b, a; +}; + +// make_fixtures.py images. +Rgba all_values(std::uint32_t x, std::uint32_t y) { + const std::uint32_t v = y * 16 + x; + return {v, 255 - v, (v * 7) & 255u, (v * 13 + 5) & 255u}; +} +Rgba pattern(std::uint32_t x, std::uint32_t y) { + return {(x * 16) & 255u, (y * 32) & 255u, (x * y * 7) & 255u, (x + y) % 3 ? 255u : (x * 20) & 255u}; +} +Rgba rgb(std::uint32_t x, std::uint32_t y) { + Rgba c = all_values(x, y); + c.a = 255; // stored without alpha + return c; +} +Rgba gray(std::uint32_t x, std::uint32_t y) { + const std::uint32_t g = (x * 32 + y * 4) & 255u; + return {g, g, g, 255}; +} + +struct Image { + std::uint64_t cachekey; + std::uint32_t width, height; + Rgba (*pixel)(std::uint32_t, std::uint32_t); + std::uint8_t auto_format; // citro3d GPU_TEXCOLOR `build --format auto` picks + const char *name; +}; +const Image kImages[] = { + {1, 16, 16, all_values, 4, "all_values_16x16"}, // partial alpha -> RGBA4 + {2, 16, 8, pattern, 4, "pattern_16x8"}, // partial alpha -> RGBA4 + {3, 16, 16, rgb, 3, "rgb_16x16"}, // opaque -> RGB565 + {4, 8, 8, gray, 3, "gray_8x8"}, // opaque -> RGB565 +}; + +// What a texel becomes after encoding to `format` and decoding back: +// tex3ds rules (alpha 0 -> transparent black, channels truncated) and the +// reader's bit expansion. +Rgba through_format(Rgba c, std::uint8_t format) { + if (c.a == 0) return {0, 0, 0, 0}; + auto expand = [](std::uint32_t v, int bits) { return (v << (8 - bits)) | (v >> (2 * bits - 8 > 0 ? 2 * bits - 8 : 0)); }; + switch (format) { + case 0: return c; + case 3: return {expand(c.r >> 3, 5), expand(c.g >> 2, 6), expand(c.b >> 3, 5), 255}; + case 4: return {(c.r >> 4) * 17, (c.g >> 4) * 17, (c.b >> 4) * 17, (c.a >> 4) * 17}; + default: return {0xFFFFFFFFu, 0, 0, 0}; // never equal + } +} + +void check_pack(const char *dir, bool exact_rgba8) { + std::string error; + const auto pack = TexturePack::open(dir, error); + check(pack != nullptr, std::string(dir) + ": open (" + error + ")"); + if (!pack) return; + check(pack->size() == 5, std::string(dir) + ": 5 entries"); + check(pack->ignore_address(), std::string(dir) + ": ignoreAddress flag"); + const PackEntry *keep = pack->find({5, 5}); + check(keep && keep->width == 0, std::string(dir) + ": keep-original entry"); + for (const Image &img : kImages) { + const std::string what = std::string(dir) + " " + img.name; + const PackEntry *e = pack->find({img.cachekey, static_cast(img.cachekey)}); + check(e != nullptr, what + ": entry found"); + if (!e) continue; + const std::uint8_t format = exact_rgba8 ? 0 : img.auto_format; + check(e->width == img.width && e->height == img.height, what + ": size"); + check(e->format == format, what + ": format " + std::to_string(e->format)); + check(e->offset % 128u == 0, what + ": 128-byte aligned"); + std::vector data; + check(pack->read(*e, data), what + ": read"); + std::vector rgba; + decode_pack_texture(*e, data, rgba); + if (rgba.size() != static_cast(img.width) * img.height) continue; + int bad = 0; + for (std::uint32_t y = 0; y < img.height; ++y) + for (std::uint32_t x = 0; x < img.width; ++x) { + const Rgba want = through_format(img.pixel(x, y), format); + const std::uint32_t got = rgba[static_cast(y) * img.width + x]; + const bool same = (got & 0xFFu) == want.r && ((got >> 8) & 0xFFu) == want.g && ((got >> 16) & 0xFFu) == want.b && + (got >> 24) == want.a; + if (!same && bad++ == 0) + std::printf(" %s: texel (%u,%u) got %08x want %02x%02x%02x%02x (abgr)\n", what.c_str(), x, y, got, want.a, want.b, + want.g, want.r); + } + check(bad == 0, what + ": " + std::to_string(bad) + " texels differ"); + } +} +} // namespace + +int main(int argc, char **argv) { + if (argc != 3) { + std::printf("usage: test_texture_pack_roundtrip \n"); + return 2; + } + check_pack(argv[1], true); + check_pack(argv[2], false); + if (failures == 0) std::printf("test_texture_pack_roundtrip: all checks passed\n"); + return failures == 0 ? 0 : 1; +} diff --git a/tools/texpack/tests/fixtures/textures.ini b/tools/texpack/tests/fixtures/textures.ini new file mode 100644 index 00000000..b72d02bb --- /dev/null +++ b/tools/texpack/tests/fixtures/textures.ini @@ -0,0 +1,14 @@ +# Round-trip pack for tests/test_texture_pack_roundtrip.cpp (issue #63): +# `p3p-texpack build` converts these synthetic fixtures, the C++ runtime reads +# them back. Keys are arbitrary; the images are power-of-two sized so `build` +# does not resample them. +[options] +hash = xxh64 +ignoreAddress = true + +[hashes] +000000000000000100000001 = all_values_16x16.png +000000000000000200000002 = pattern_16x8.png +000000000000000300000003 = rgb_16x16.png +000000000000000400000004 = gray_8x8.png +000000000000000500000005 = |