Skip to content

ttpf_mips: optional native kernels (tools/ttpf_native) for detile, BC decode and block_means (PERF-TTPF-NATIVE) - #8735

Open
TG0000 wants to merge 1 commit into
ItsNotRexed:mainfrom
TG0000:perf/rust-ttpf-native
Open

TG0000 wants to merge 1 commit into
ItsNotRexed:mainfrom
TG0000:perf/rust-ttpf-native

Conversation

@TG0000

@TG0000 TG0000 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Item: PERF-TTPF-NATIVE (claimed in #8504). Same pattern as #6308 and #8447 (tools/bc7_native); does not depend on either, and touches none of tools/dds_png.py, tools/bc7_native/, tools/bcpack.py, tools/bcpack_calibration/.

What

An optional Rust crate, tools/ttpf_native (no dependencies, cdylib loaded through ctypes), with step-for-step ports of these tools/ttpf_mips.py loops:

  • the element copy of detile and detile_volume_slice (Python still builds the X/Y bit tables and row bases and passes them in);
  • the BC4, BC5, BC6H (UF16/SF16) and BC7 decoders behind decode_pixels;
  • block_means, for every BC kind (BC1-BC7).

Every integer step and every double operation is the Python one, in the same order, so the detiled bytes and the IEEE bits of every double are identical (-0.0 included). The BPTC partition/anchor tables and the BC6H header bit plans are passed in from bcpack_tables_bptc.load_bc7() and bcpack._BC6H_INFO / _bc6h_plan, not copied into Rust.

How it hooks in (small, away from the open ttpf_mips PRs)

Tests (no game data; synthetic random input only)

tests/test_ttpf_native.py (18 tests) compares exact bytes / double bits:

  • 2D mip chains for 1/2/4/8/16-byte elements (DXGI 61, 49, 28, 10, 2, BC1, BC7), 4 KiB and 64 KiB tiles, 1x1, 3x700, 1023x5, 17x13, 256x256 and 512x64, bytes and memoryview input; short allocations (the native copy refuses, so the Python result is kept);
  • 3D volume slices for every element size, 1x1x1, 5x3x7, 33x1x2, 70x40x9 and 64x64x16;
  • every BC format at 1x1, 1x37, 37x1, 2x3, 13x7 and 64x64 blocks, with every mode selector; BC6H constant-fill extremes under all 32 mode prefixes; repeated blocks (tuple sharing is never worse than Python's); bytearray/memoryview input; truncated payloads give the same result or the same exception type;
  • block_means under 5 crops × 5 block sizes × 4 grids per format, including grids that are not the decoded one; mutation after decode (set, slice, append, +=, sort); plain lists never take the native path;
  • fallbacks: a subprocess with LEONIDA_TTPF_NATIVE=0 (plain lists, same hex output as native), a missing library, a wrong ABI version, a failed self-check, and a deliberately diverging Python BC7 decoder being caught.

Run on Windows 11, AMD Ryzen (Zen 3, family 25 model 33), Python 3.12, Rust 1.100.0. CPU only; no GPU or driver involved.

  • cargo build --release --locked --offline and cargo test --release --locked --offline (9 Rust tests), 0 warnings, also with RUSTFLAGS="-D warnings"
  • python -m compileall -q make_game.py tools tests: ok
  • python -I tests/test_ttpf_native.py: 18 OK (with LEONIDA_TTPF_NATIVE=0: 16 skipped, 2 OK)
  • python -I tests/test_ttpf_mips.py: 93 OK with native, 93 OK with LEONIDA_TTPF_NATIVE=0 (this includes the existing BC1/BC4 peak-memory and shared-tuple test)
  • python -I scripts/check_no_game_data.py on every changed file: clean
  • tests/semantic not run (the translator is untouched).

CI: a new step in the same job as the BCPack calibration build. It builds and tests the crate, asserts that the library loads, then runs test_ttpf_native.py and test_ttpf_mips.py against it. It sits after the rpf9dump diagnostics step, so it does not overlap the step #8447 adds.

Benchmarks (best of 3-5, random synthetic data; "before" = same code with LEONIDA_TTPF_NATIVE=0)

Call Before After Speed-up
detile BC1 1024x1024, 4 KiB tiles 7.35 ms 0.385 ms 19x
detile BC7 1024x1024, 64 KiB tiles 7.65 ms 0.593 ms 13x
detile RGBA8 1024x1024, 4 KiB tiles 60.0 ms 2.23 ms 27x
detile_volume 256x256x32, 8-byte voxels 272 ms 16.5 ms 16x
block_means BC1 512x512 62.9 ms 5.03 ms 13x
block_means BC3 512x512 61.3 ms 5.03 ms 12x
block_means BC4 512x512 57.2 ms 4.72 ms 12x
block_means BC5 512x512 61.5 ms 5.01 ms 12x
block_means BC6H SF16 512x512 75.1 ms 13.8 ms 5.4x
block_means BC7 512x512 69.8 ms 6.60 ms 11x
decode_pixels BC7 512x512, 512 distinct blocks 386 ms 39.4 ms 10x
decode_pixels BC6H UF16 512x512, 512 distinct blocks 563 ms 45.4 ms 12x
decode_pixels BC7 512x512, every block random 370 ms 82.6 ms 4.5x
decode_pixels BC6H SF16 512x512, every block random 568 ms 114 ms 5.0x
decode_pixels BC5 512x512, every block random 127 ms 48.4 ms 2.6x
decode_pixels BC4 512x512, every block random 48.6 ms 21.5 ms 2.3x
decode_pixels BC1 512x512 (Python decoder both times) 39.6 ms 47.5 ms — (noise; same code)
_check_mip_chain BC7 512x512, 7 mips 875 ms 303 ms 2.9x
_check_mip_chain BC6H 512x512, 7 mips 976 ms 323 ms 3.0x
_check_mip_chain BC1 512x512, 7 mips 353 ms 269 ms 1.3x

Limits, stated plainly: detile and block_means reach 11x-27x because their output is bytes, or one tuple per block. decode_pixels must still build one Python tuple per pixel, about 0.1-0.3 µs each when every pixel differs, so on fully random data it caps at 2x-5x. It reaches 10x-12x when blocks repeat, as they do in real textures. The whole check chain is now dominated by predict_child_means, which this PR leaves in Python. It could use the same remembered-blocks list in a follow-up, which I'll post as a finding: in the coordination issue.

AI-assisted: written with Claude Code (Claude Opus 5.5). I reviewed and ran everything listed above.

🤖 Generated with Claude Code

… decode and block_means

A zero-dependency Rust crate, loaded through ctypes like tools/bcpack_calibration,
ports the element copy of detile / detile_volume_slice, the BC4/BC5/BC6H/BC7
decoders behind decode_pixels and block_means (all BC kinds) step for step, so the
bytes and the IEEE bits of every double are identical. The BPTC tables and BC6H
header plans are passed from bcpack_tables_bptc / bcpack, not copied.

Any call the library refuses (element outside the allocation, short payload, other
buffer types) runs the Python code, which keeps its own result or error. Each block
kind is checked against the Python decoder and means on seeded random blocks once
and stays in Python if a double differs. LEONIDA_TTPF_NATIVE=0 turns it off.

detile 13x-27x, detile_volume 16x, block_means 11x-13x, BC6H/BC7 decode_pixels
4.5x-12x (random synthetic data, Zen 3). tests/test_ttpf_native.py compares both
paths; CI builds the crate and runs it with test_ttpf_mips.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant