Repository navigation
feat(runtime): optimize resource detile loops with bit-shifts and typed moves - #9429
Cryptoteep wants to merge 1 commit into
Conversation
…ed moves Replaces per-texel integer divisions and modulos (x / tile_w, x % tile_w, y / tile_h, y % tile_h) in detile_typed and tile_typed with power-of-two bit shifts (>> shift_w, >> shift_h) and bitwise masks (& mask_w, & mask_h). Tile dimensions (tile_w, tile_h) are compile-time powers of two (64, 128, 256) for 64 KiB GFX10 swizzle tiles. Replaces std::memcpy per texel with typed unaligned Element<Bpe> assignments, and advances linear row offsets incrementally. Eliminates millions of 64-bit integer division instructions and function call overhead during texture detiling and surface readback on x86-64 CPUs. Verified with runtime/test/resource_detile_test.cpp (all round-trip and validation checks pass bit-exact, 0 game data).
|
Please correct the claim of more than 33 million integer division instructions for a 4096 x 4096 surface. I checked the optimized x64 code with MSVC 19.51.36260. Both the parent (6312c05) and the PR (07d62a6) emit zero div or idiv instructions across all ten tile and detile specializations (1, 2, 4, 8 and 16 bytes per element). For example, the parent already uses shr by 7 and masks with 127 for 4-byte elements. At 16 bytes, the parent already emits unaligned XMM moves for the fixed-size copy. To reproduce one specialization, put this in probe.cpp: At each revision, run this from an x64 MSVC shell: Inspect detile_typed<4> in probe.asm. The divisors are compile-time constants on the parent. The existing resource_detile_test passes at both revisions. A before/after timing would establish whether the offset or copy changes improve throughput. AI-assisted. |
|
Independent MSVC /O2 measurement of 07d62a6 against 6312c05. Ryzen 7 9800X3D, Windows 11 build 26300, MSVC 19.51.36248 (/O2 /EHsc /std:c++20 /DNDEBUG). Same synthetic four-byte elements on both revisions. Seven interleaved processes per revision and surface, seven samples per process. The table gives the median of the process medians.
All round trips passed and parent and head tiled outputs compared byte-for-byte equal at all three sizes. The strongest timing difference was 4096×64 tiling: process medians ranged from 152–197 µs on parent and 202–225 µs on head. At 4096×2160 the ranges overlap for both directions. On this compiler the optimized parent four-byte inner loops already used shifts and masks, and the fixed-size memcpy compiled down to a dword move. I found no divide instruction there. This CPU/compiler combination therefore does not reproduce the proposed speedup. Other element sizes and toolchains were outside this measurement. No game files. AI-assisted with Codex. Claim: #9401 (comment) |
|
Tested on AMD Radeon RX 7700 XT (RDNA3), AMD Windows driver 32.0.32029.2010, Windows 11, MSVC 14.51 Release, at 07d62a6. Full No regression. The only failures are the same 5 as on AI-assisted (Claude Code). |
|
Windows check of The bit-shift/typed-move rewrite is bit-identical to the reference on MSVC as well; only correctness was checked, not speed. |
|
Tested
3/3 passed in 2.94 s, no warnings from the changed header. These compare the detiled output against the expected patterns, so the bit-shift and typed-move rewrite produces the same bytes as before on this build. AI-assisted. |
|
Tested
Three things to look at:
|
Summary
x / tile_w,x % tile_w,y / tile_h,y % tile_h) indetile_typedandtile_typed(runtime/src/resource_detile.h) with compile-time power-of-two bit shifts (>> shift_w,>> shift_h) and bitwise masks (& mask_w,& mask_h).TileShapedimensions for 64 KiB GFX10 swizzle tiles (SW_64KB_D) are always compile-time powers of two (64, 128, 256 for 16, 8, 4, 2, 1 BPE).std::memcpy(&dst, &src, Bpe)per pixel with typedElement<Bpe>unaligned struct copy, and incrementslinear_off += Bpelinearly instead of calculating row index on every inner iteration.Performance & Hardware Motivation
idivinstructions incur ~15-20 cycles of execution latency.Verification
runtime/test/resource_detile_test.cppunder MSVC (cl /EHsc /std:c++20 /O2): "resource detile: all 64 KiB GFX10 swizzle round-trip and validation checks passed".python -I scripts/check_no_game_data.py runtime/src/resource_detile.h(0 game data).<bit>,std::countr_zero), no GPU or game data required.