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
github-bot wants to merge 1 commit from github-bot/gh-1880 into main AGit
Owner

@⁠thiefyzheng on GitHub · 2026-11-25 14:01 UTC · original
thiefyzheng:fix/texel-copy-fixedwidth → main · +190 −4 · 3 files · 1 commits · open

RT-TEXEL-COPY-FIXEDWIDTH

Claimed on #1608 (#1608); implements the first of @⁠cheltyi's two findings (#1608): 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).

**@⁠thiefyzheng** on GitHub · 2026-11-25 14:01 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1880) `thiefyzheng:fix/texel-copy-fixedwidth` → `main` · +190 −4 · 3 files · 1 commits · open ## RT-TEXEL-COPY-FIXEDWIDTH Claimed on #1608 (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608); implements the first of @⁠cheltyi's two findings (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608): `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).
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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:09:46 +00:00
Author
Owner

@⁠ecox982745 on GitHub · 2026-11-25 14:17 UTC · original

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.

**@⁠ecox982745** on GitHub · 2026-11-25 14:17 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1880#issuecomment-7165045954) 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.
Author
Owner

@⁠thiefyzheng on GitHub · 2026-11-25 14:24 UTC · original

@⁠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).

**@⁠thiefyzheng** on GitHub · 2026-11-25 14:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1880#issuecomment-7165239918) @⁠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).
Author
Owner

@⁠MoonEyes on GitHub · 2026-11-25 18:16 UTC · original

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)

**@⁠MoonEyes** on GitHub · 2026-11-25 18:16 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1880#issuecomment-7170673377) 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)
Author
Owner

@⁠thiefyzheng on GitHub · 2026-11-25 19:57 UTC · original

@⁠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).

**@⁠thiefyzheng** on GitHub · 2026-11-25 19:57 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1880#issuecomment-7172989507) @⁠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).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1880/head:github-bot/gh-1880'
git switch 'github-bot/gh-1880'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1880'
git switch 'github-bot/gh-1880'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1880'
git switch 'github-bot/gh-1880'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1880'
git switch 'main'
git merge --squash 'github-bot/gh-1880'
git switch 'main'
git merge --ff-only 'github-bot/gh-1880'
git switch 'main'
git merge 'github-bot/gh-1880'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.