Skip to content

xg_xs copy_elements: dispatch fixed element widths 1/2/4/8/16 to a compile-time-sized copy loop (RT-TEXEL-COPY-FIXEDWIDTH) - #1880

Open
thiefyzheng wants to merge 1 commit into
ProjectLeonida:mainfrom
thiefyzheng:fix/texel-copy-fixedwidth
Open

thiefyzheng wants to merge 1 commit into
ProjectLeonida:mainfrom
thiefyzheng:fix/texel-copy-fixedwidth

Conversation

@thiefyzheng

Copy link
Copy Markdown
Contributor

RT-TEXEL-COPY-FIXEDWIDTH

Claimed on #1608 (#1608 (comment)); implements the first of @cheltyi's two findings (#1608 (comment)): runtime/shims/xg_xs_layout.h's copy_elements issued a runtime-sized memcpy per texel/block — the path TitleImageUpload::write uses during runtime texture refresh. The finder's prototype (876 comparisons, ASan/UBSan clean, 4.54→0.596 ms on a synthetic 1024x1024 RGBA8 tiled texture under MinGW GCC 16.2 -O3) is the origin of this change; those numbers are the finder's, not re-measured here.

What

  • The per-element x-loop moved into copy_tiled_row<Into, Width>; copy_elements does ONE switch (bytes) per row: widths 1/2/4/8/16 get a compile-time Width, everything else takes Width = 0 — a verbatim copy of the old runtime-size loop. Address math untouched; linear textures still copy whole rows and never reach the element loop.

Verification

  • New runtime/test/texel_copy_test.cpp (ctest texel_copy, no GPU): runs copy_elements against a verbatim copy of the old loop, comparing the WHOLE destination buffer (bytes the copy must not touch are checked too). Corpus: 17 textures — R8, R16, RGBA8, RGBA16F, RGBA32F, BC1, BC3, BC5 (all five specialized widths) — tiled 4kb/64kb, linear, full mip chains with tails, arrays, 1D linear, 3D volumes; every case at 3 pointer misalignments × 3 pitch paddings; BOTH directions; and every case re-run with the width forced to 3/6/12 to prove the fallback path is unchanged.
  • g++ 20, ASan+UBSan: 306 checks, 0 failures. Guard check: breaking the width-4 path on purpose makes the test fail 27 checks.
  • MSVC /W4 /permissive- (VS 2022, winvm): texel_copy_test built; ctest -R texel_copy Passed (4.57 s).
  • NOT done here: timing (the finder's prototype numbers stand as the only measurements; frame-time benefit needs a streaming/dynamic-texture workload owner, as the finding itself says); xg_xs_test and d3d12_existing_heap_test were not run (no device on this host).

Attribution

AI-assisted (Claude Sonnet 5.5 worker implementation, contributor verification incl. the winvm MSVC build, orchestration).

…time-sized memcpy loop

copy_elements ran a runtime-sized std::memcpy per texel/block (the call is not
inlined), which dominates TitleImageUpload::write on runtime texture refresh.
The per-row element loop is now copy_tiled_row<Into, Width>, chosen by one
switch on format.bytes per row: 1/2/4/8/16 get a constant-size memcpy, every
other size takes the unchanged runtime-size loop. Addresses (row/column tables,
pitches, linear fast path) are untouched, so the bytes written are identical,
including for unaligned pointers and pitches and in both directions.

texel_copy_test compares copy_elements with the original loop over the whole
destination buffer: tiled and linear, mips and tails, arrays, volumes, widths
1/2/4/8/16 plus non-specialized 3/6/12, misaligned buffers and padded pitches.
@ecox982745

Copy link
Copy Markdown

On a Ryzen 7 7800X3D with MSVC 19.44 /O2, the once-per-row dispatch in this PR took 0.357 ms to copy a synthetic 1024×1024 RGBA8 texture into its tiled layout. The original took 2.126 ms, and #1882's per-element dispatch took 0.909 ms in the same benchmark.

CPU time per 1024×1024 copy (ms) Original 5e95fa2f4 #1880 7f9e656e3 #1882 cc4ac4101
RGBA8 into tiled, 64 KiB D 2.126 0.357 0.909
RGBA8 out of tiled 2.127 0.357 0.902
BC7 into tiled, 64 KiB S 0.111 0.052 0.095
BC7 out of tiled 0.103 0.043 0.086
Linear RGBA8 control, into / out 0.074 / 0.074 0.074 / 0.074 0.072 / 0.071

I built one executable per revision from the same benchmark source. The two PR parents have identical xg_xs_layout.h and gfx10_tiling.h. Each process used one pinned CPU thread and reused its buffers and column scratch. For each layout it ran five warm-up round trips and timed the next 15 copies in each direction, checking the final round trip byte-for-byte. The table gives the median across six process runs per build, using all six orderings of the three builds. For RGBA8 into tiled, the six run medians ranged from 0.357–0.362 ms on this PR and 0.908–0.942 ms on #1882. These are warm CPU copy timings. End-to-end texture streaming and frame times are still open.

On this PR the two CPU suites passed (xg_xs 465 checks, texel_copy 306 checks), and all three d3d12_existing_heap CTest variants passed on native Windows 11 with an RTX 4090 (616.64). I also compiled the same 306-check corpus against #1882's header and got zero failures.

Synthetic inputs. AI-assisted verification with OpenAI Codex.

@thiefyzheng

Copy link
Copy Markdown
Contributor Author

@ecox982745 — thank you; this closes the items the PR body listed as not-run. Recording for the merge decision: at 7f9e656e3 on a Ryzen 7 7800X3D / MSVC /O2, the row-level dispatch is 5.95x the original (0.357 ms vs 2.126 ms for RGBA8 1024x1024 into tiled, both directions) with linear-copy control unchanged (0.074 ms); xg_xs 465 checks and texel_copy 306 checks pass, and all three d3d12_existing_heap variants pass on native Windows 11 / RTX 4090 (616.64) — the device tests we couldn't run. Frame-time on a streaming workload remains open, as the finding said it would.

Noting your benchmark also covers #1882 (per-element dispatch, same finding): on the identical benchmark it is 0.909 ms — byte-exact as well, ours is the row-level dispatch at 0.357 ms. The claim on this finding (RT-TEXEL-COPY-FIXEDWIDTH, comment 7164508666, 13:53 UTC) and #1880 (opened 14:05) both precede #1882 (opened 14:11, no claiming: for the item); per the board's claim-before-code rules I've noted that on #1882 — maintainers' call, of course. AI-assisted (orchestrator).

@MoonEyes

Copy link
Copy Markdown
Collaborator

Second run of the device tests for #1880 at head 7f9e656e3, after @ecox982745's on an RTX 4090. One thing to report: a single failure I could not reproduce.

Config: Windows 11 (26300), RTX 4060 desktop, NVIDIA 610.88, MSVC 19.44, RelWithDebInfo, Vulkan SDK 1.4.363.0. Test targets only, no game files.

  • xg_xs (465 checks), texel_copy (306 checks), d3d12_existing_heap_no_write_watch, d3d12_existing_heap_write_watch: pass.
  • d3d12_existing_heap: failed once on the first ctest pass, then passed 23 of 23 direct reruns. The failure was in the concurrent stress section: vulkan slot-133 heap-lock faults: the write-watched half is not hot after the stress, then line 1757: !failed. That check guards the test's own validity (the watched half must stay hot), it is not a data comparison. An MSVC build was running on the machine at that moment.
  • Control: the same test built with xg_xs_layout.h from the merge base (98c2f7777) passed 20 of 20, alternated with the PR binary. I could not reproduce the failure on either side.

So: one unreproduced failure in a timing-dependent section, and no evidence either way that this PR causes it. Not covered: timing/frame benefit, full runtime build, full ctest.

AI-assisted: yes (Claude Code ran the tests)

@thiefyzheng

Copy link
Copy Markdown
Contributor Author

@MoonEyes — thank you. Recording for the merge decision: the one d3d12_existing_heap failure (write-watched half not hot after the concurrent stress, a check on the test's own validity, with an MSVC build running) did not reproduce in 23/23 reruns on the PR build or 20/20 alternating with the merge-base header, so there is no evidence it comes from this change. The device tests (xg_xs 465, texel_copy 306, both write-watch variants) pass on the RTX 4060 and, per @ecox982745, the RTX 4090. Frame-time on a streaming workload is still not measured.

AI-assisted (Claude Code).

@zmasi

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Native Windows 11 + MSVC 19.44 + RTX 3090 Vulkan Verification (7f9e656e37 + origin/main adfb3821a2)

Verdict: PASS (0 blockers, 0 new /W4 warnings; 4.7x-8.0x RGBA8 & 2.0x-2.3x BC7 speedup confirmed)

Environment & Merge Check

  • Host: Windows 11 (10.0.26200), MSVC 19.44.35228 (x64, /W4 /permissive- /utf-8 /O2 /EHsc), NVIDIA GeForce RTX 3090 (Ampere, ReBAR enabled)
  • Merge hygiene: Clean merge onto origin/main (adfb3821a2, git merge-tree --write-tree rc=0).

Red -> Green, CPU A/B Benchmark & RTX 3090 GPU Results

  • CPU Correctness (PR #1880 + origin/main):
    • texel_copy_test.exe: PASS (306 checks, 0 failures, 1.95 s, 0 new /W4 warnings).
    • xg_xs_test.exe: PASS (519 checks, 0 failures, 0.43 s).
  • Pinned-Thread CPU A/B Microbenchmark (1024x1024 mip 0, 5 warmups + median of 25 round-trips, byte-exact verified):
    • RGBA8 (kSwizzled | k64kbD): into 2.428 ms -> 0.514 ms (4.72x), out 3.164 ms -> 0.395 ms (8.01x).
    • BC7 (kSwizzled | k64kbS): into 0.125 ms -> 0.061 ms (2.05x), out 0.113 ms -> 0.049 ms (2.31x).
  • RTX 3090 GPU Neighbor Suite (d3d12_existing_heap_test.exe linked with #1880's xg_xs_layout.h):
    • d3d12_existing_heap (default): PASS (rc=0, 8/8 OpenExistingHeapFromAddress cases + concurrent write-watch stress clean).
    • d3d12_host_prefix (--host-prefix-only): PASS (rc=0, 0.89 s).
    • d3d12_mapped_plane_async (--mapped-plane-async): PASS (rc=0, 0.35 s).
    • d3d12_existing_heap_no_write_watch (LEONIDA_WRITE_WATCH=0): PASS (rc=0, 7.12 s).
    • d3d12_existing_heap_write_watch (LEONIDA_WRITE_WATCH=1): PASS (rc=0, 9.09 s).

AI-assisted.

@OreoFR

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Verified by OreoFR

  • Base: main aa918e5d9 + PR head 7f9e656e3
  • Test: ctest -R ^texel_copy$
  • Red on main: passes (the new test covers behaviour already on main)
  • Green with the PR: passes; package builds, full ctest shows no new failure
  • Verdict: NO RED (optimization; the new test also passes on main)
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

Note: shares runtime/CMakeLists.txt with the open #2566; a rebase may be needed if #2566 lands first.

AI-assisted (Claude Code, Claude Opus 5.5).

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.

5 participants