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

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

📋 Pull Request Information

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

Base: main ← Head: perf/rt-uav-clear-fast-pack-and-tests


📝 Commits (1)

  • f133273 runtime: speed up uav_clear format lookup and channel pack/unpack with unit tests

📊 Changes

3 files changed (+282 additions, -11 deletions)

View changed files

📝 runtime/CMakeLists.txt (+5 -0)
📝 runtime/src/uav_clear.h (+107 -11)
➕ runtime/test/uav_clear_unit_test.cpp (+170 -0)

📄 Description

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.

🔄 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/2009 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `perf/rt-uav-clear-fast-pack-and-tests` --- ### 📝 Commits (1) - [`f133273`](https://github.com/ubiquitarianism/leonida/commit/f1332737d4a8c039851a255a7ebe5e4f6abd3103) runtime: speed up uav_clear format lookup and channel pack/unpack with unit tests ### 📊 Changes **3 files changed** (+282 additions, -11 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+5 -0) 📝 `runtime/src/uav_clear.h` (+107 -11) ➕ `runtime/test/uav_clear_unit_test.cpp` (+170 -0) </details> ### 📄 Description 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. --- <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#2018
No description provided.