runtime: speed up uav_clear format lookup and channel pack/unpack with unit tests (RT-UAV-CLEAR-FAST-PACK-AND-UNIT-TESTS) #2009

Merged
IlyassRS3 merged 1 commit from upstream-pr/2009 into main 2026-11-29 14:26:43 +00:00
IlyassRS3 commented 2026-11-25 18:01:21 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T18:01:21Z.


Claim: RT-UAV-CLEAR-FAST-PACK-AND-UNIT-TESTS (claim comment #7170281757 on #1608).

Problem

In runtime/src/uav_clear.h:

  1. layout(VkFormat) linearly scanned all 45 Layout entries on every ClearUnorderedAccessViewUint / ClearUnorderedAccessViewFloat call (up to 45 comparisons for R8_* formats).
  2. detail::put() and detail::get() packed and unpacked every channel one bit at a time (for (unsigned i = 0; i < c.bits; ++i) — up to 128 loop iterations per element for R32G32B32A32_*) without guarding out-of-bounds (offset, bits) pairs.
  3. There was no portable CPU-only unit test exercising uav_clear.h independently of a live Vulkan/D3D12 device.

Fix

  • Replace the 45-entry linear scan in layout(VkFormat) with a constexpr switch verified at compile time by a static_assert over layouts[].
  • Replace bit-by-bit loops in detail::put() / detail::get() with bounds-checked byte-aligned (8/16/32-bit) fast paths and a 64-bit word shift/mask path for packed sub-byte channels (10:10:10:2, 11:11:10, 5:6:5, 1:5:5:5, 4:4:4:4).
  • Move the unorm scale computation inside case Kind::unorm: in encode().
  • Add a portable Vulkan-headers-only CTest uav_clear_unit (runtime/test/uav_clear_unit_test.cpp) verifying all 45 formats, exhaustive bit-exact parity of put()/get() against a bit-by-bit reference across widths 1..32, bounds guards, NaN/Inf/edge float conversions, cross-format decodes, and raw_element().

Benchmark (clang++ -std=c++20 -O2, bit-exact A/B across all 45 formats)

  • 45-format uint encode() + decode() sweep (500,000 iterations): 981.25 ms -> 388.01 ms (2.53x faster)
  • Hot 4-format mix (R32G32B32A32_UINT, R8G8B8A8_UNORM, A2B10G10R10_UINT_PACK32, R8_SINT, 5M calls): 237.19 ms -> 83.14 ms (2.85x faster)
  • Bit-exact parity verified on all 45 formats for both uint and float encode() + decode().

Verification

  • AI-assisted: yes (Antigravity agent by Google DeepMind).
  • GPUs tested: none (host is macOS arm64; no retail files).
  • Ran runtime/test/uav_clear_unit_test.cpp cleanly under:
    • clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror
    • clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror
    • clang++ -std=c++20 -O2 -Wall -Wextra -Werror
  • Ran python3 -I scripts/check_no_game_data.py runtime/src/uav_clear.h runtime/test/uav_clear_unit_test.cpp runtime/CMakeLists.txt and python3 -I tests/test_text_write_line_endings.py: all passed.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5496509712 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2009). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T18:01:21Z. --- Claim: `RT-UAV-CLEAR-FAST-PACK-AND-UNIT-TESTS` (claim comment `#7170281757` on #1608). ### Problem In `runtime/src/uav_clear.h`: 1. `layout(VkFormat)` linearly scanned all 45 `Layout` entries on every `ClearUnorderedAccessViewUint` / `ClearUnorderedAccessViewFloat` call (up to 45 comparisons for `R8_*` formats). 2. `detail::put()` and `detail::get()` packed and unpacked every channel one bit at a time (`for (unsigned i = 0; i < c.bits; ++i)` — up to 128 loop iterations per element for `R32G32B32A32_*`) without guarding out-of-bounds `(offset, bits)` pairs. 3. There was no portable CPU-only unit test exercising `uav_clear.h` independently of a live Vulkan/D3D12 device. ### Fix - Replace the 45-entry linear scan in `layout(VkFormat)` with a `constexpr switch` verified at compile time by a `static_assert` over `layouts[]`. - Replace bit-by-bit loops in `detail::put()` / `detail::get()` with bounds-checked byte-aligned (8/16/32-bit) fast paths and a 64-bit word shift/mask path for packed sub-byte channels (`10:10:10:2`, `11:11:10`, `5:6:5`, `1:5:5:5`, `4:4:4:4`). - Move the `unorm` scale computation inside `case Kind::unorm:` in `encode()`. - Add a portable Vulkan-headers-only CTest `uav_clear_unit` (`runtime/test/uav_clear_unit_test.cpp`) verifying all 45 formats, exhaustive bit-exact parity of `put()`/`get()` against a bit-by-bit reference across widths `1..32`, bounds guards, NaN/Inf/edge float conversions, cross-format decodes, and `raw_element()`. ### Benchmark (`clang++ -std=c++20 -O2`, bit-exact A/B across all 45 formats) - 45-format uint `encode()` + `decode()` sweep (500,000 iterations): `981.25 ms` -> `388.01 ms` (**2.53x faster**) - Hot 4-format mix (`R32G32B32A32_UINT`, `R8G8B8A8_UNORM`, `A2B10G10R10_UINT_PACK32`, `R8_SINT`, 5M calls): `237.19 ms` -> `83.14 ms` (**2.85x faster**) - Bit-exact parity verified on all 45 formats for both uint and float `encode()` + `decode()`. ### Verification - **AI-assisted**: yes (Antigravity agent by Google DeepMind). - **GPUs tested**: none (host is macOS arm64; no retail files). - Ran `runtime/test/uav_clear_unit_test.cpp` cleanly under: - `clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror` - `clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror` - `clang++ -std=c++20 -O2 -Wall -Wextra -Werror` - Ran `python3 -I scripts/check_no_game_data.py runtime/src/uav_clear.h runtime/test/uav_clear_unit_test.cpp runtime/CMakeLists.txt` and `python3 -I tests/test_text_write_line_endings.py`: all passed.
copilot-pull-request-reviewer[bot] commented 2026-11-25 18:01:32 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T18:01:32Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6062530788 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2009#pullrequestreview-6062530788). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T18:01:32Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
zmasi commented 2026-11-28 20:40:04 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T20:40:04Z.


#2009 | head f1332737d4 (merged cleanly with main 1bc055e6c2) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(uav_clear_unit|d3d12_uav_clear|d3d12_short_uav_clear|replay_uav_clear_offset)$" --output-on-failure (LEONIDA_VK_VALIDATION=1) + 200,000 × 45-format uint encode+decode A/B sweep | red & baseline on main: uav_clear_unit fails at line 116: CHECK failed: b == 0 (out-of-bounds detail::put lacked bounds guards) and runs the 45-format sweep in 728.81 ms | green on PR+main: 4/4 CTests pass (uav_clear_unit 306.80 ms — 2.38x faster on MSVC RelWithDebInfo with bit-exact parity across all 45 formats, d3d12_uav_clear, d3d12_short_uav_clear, and replay_uav_clear_offset) on RTX 3090 with 0 Khronos validation errors; check_no_game_data.py 0 violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7245452478 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2009#issuecomment-7245452478). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T20:40:04Z. --- #2009 | head f1332737d4a8c039851a255a7ebe5e4f6abd3103 (merged cleanly with main 1bc055e6c2dedaa0de23ec79d8c4007c1dff1fb3) | `ctest --test-dir runtime/build -C RelWithDebInfo -R "^(uav_clear_unit|d3d12_uav_clear|d3d12_short_uav_clear|replay_uav_clear_offset)$" --output-on-failure` (`LEONIDA_VK_VALIDATION=1`) + `200,000` × 45-format uint `encode`+`decode` A/B sweep | red & baseline on main: `uav_clear_unit` fails at `line 116: CHECK failed: b == 0` (out-of-bounds `detail::put` lacked bounds guards) and runs the 45-format sweep in `728.81 ms` | green on PR+main: 4/4 CTests pass (`uav_clear_unit` `306.80 ms` — **2.38x faster** on MSVC `RelWithDebInfo` with bit-exact parity across all 45 formats, `d3d12_uav_clear`, `d3d12_short_uav_clear`, and `replay_uav_clear_offset`) on RTX 3090 with `0` Khronos validation errors; `check_no_game_data.py` `0` violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes
OreoFR commented 2026-11-29 02:39:40 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-29T02:39:40Z.


Verified by OreoFR

  • Base: main aa918e5d9 + PR head f1332737d
  • Test: ctest -R ^uav_clear_unit$
  • Red on main: fails at line 94: put() writes into the element for malformed channels (0 bits, 33 bits, offset past 128 bits)
  • Green with the PR: passes; package builds, full ctest shows no new failure
  • Verdict: READY
  • 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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7249567334 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2009#issuecomment-7249567334). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-29T02:39:40Z. --- **Verified by OreoFR** - Base: main `aa918e5d9` + PR head `f1332737d` - Test: `ctest -R ^uav_clear_unit$` - Red on main: fails at line 94: `put()` writes into the element for malformed channels (0 bits, 33 bits, offset past 128 bits) - Green with the PR: passes; package builds, full ctest shows no new failure - Verdict: READY - 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).
MoonEyes commented 2026-11-29 14:26:46 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-29T14:26:46Z.


Merged. Tested by OreoFR (RTX 4070 Laptop 8 GB, NVIDIA 621.40, Windows 11) on this head: ctest -R ^uav_clear_unit$ red on main (line 94), green with the PR. AI-assisted: yes.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7257308646 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2009#issuecomment-7257308646). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-29T14:26:46Z. --- Merged. Tested by OreoFR (RTX 4070 Laptop 8 GB, NVIDIA 621.40, Windows 11) on this head: ctest -R ^uav_clear_unit$ red on main (line 94), green with the PR. AI-assisted: yes.
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!2009
No description provided.