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

Open
opened 2026-11-26 21:19:19 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1880
Author: @thiefyzheng
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/texel-copy-fixedwidth


📝 Commits (1)

  • 7f9e656 copy_elements: dispatch fixed element widths 1/2/4/8/16 to a compile-time-sized memcpy loop

📊 Changes

3 files changed (+190 additions, -4 deletions)

View changed files

📝 runtime/CMakeLists.txt (+7 -0)
📝 runtime/shims/xg_xs_layout.h (+26 -4)
➕ runtime/test/texel_copy_test.cpp (+157 -0)

📄 Description

RT-TEXEL-COPY-FIXEDWIDTH

Claimed on #1608 (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7164508666); implements the first of @cheltyi's two findings (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7164354183): 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).


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1880 **Author:** [@thiefyzheng](https://github.com/thiefyzheng) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/texel-copy-fixedwidth` --- ### 📝 Commits (1) - [`7f9e656`](https://github.com/ubiquitarianism/leonida/commit/7f9e656e37c5a9599df110f3407fabc69d63ce19) copy_elements: dispatch fixed element widths 1/2/4/8/16 to a compile-time-sized memcpy loop ### 📊 Changes **3 files changed** (+190 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+7 -0) 📝 `runtime/shims/xg_xs_layout.h` (+26 -4) ➕ `runtime/test/texel_copy_test.cpp` (+157 -0) </details> ### 📄 Description ## RT-TEXEL-COPY-FIXEDWIDTH Claimed on #1608 (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7164508666); implements the first of @cheltyi's two findings (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7164354183): `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). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1893
No description provided.