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

Open
github-bot wants to merge 1 commit from github-bot/gh-2009 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 18:01 UTC · original
IlyassRS3:perf/rt-uav-clear-fast-pack-and-tests → main · +282 −11 · 3 files · 1 commits · open

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.
**@⁠IlyassRS3** on GitHub · 2026-11-25 18:01 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2009) `IlyassRS3:perf/rt-uav-clear-fast-pack-and-tests` → `main` · +282 −11 · 3 files · 1 commits · open 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:14:40 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 18:01 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 18:01 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2009#pullrequestreview-6062530788) · **reviewed** 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.
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/2009/head:github-bot/gh-2009'
git switch 'github-bot/gh-2009'

Merge

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