Skip to content

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

Open
IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:perf/rt-uav-clear-fast-pack-and-tests
Open

IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:perf/rt-uav-clear-fast-pack-and-tests

Conversation

@IlyassRS3

Copy link
Copy Markdown
Contributor

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 AI balanced review requested due to automatic review settings November 25, 2026 18:01

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

#2009 | head f133273 (merged cleanly with main 1bc055e) | 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

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants