Skip to content

perf(runtime): hash the border colour table and key replaced views by address (d3d12_vulkan_device) - #8159

Open
TG0000 wants to merge 1 commit into
ItsNotRexed:mainfrom
TG0000:perf/d3d12-vulkan-hotpath
Open

TG0000 wants to merge 1 commit into
ItsNotRexed:mainfrom
TG0000:perf/d3d12-vulkan-hotpath

Conversation

@TG0000

@TG0000 TG0000 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Item: new item, claimed in #8047 (claim). Depends on: nothing.

What

Two linear scans under DeviceState::descriptor_mutex in runtime/shims/d3d12_vulkan_device.cpp:

  • register_border_color: runs on every CreateSampler and every root-signature static sampler with a border colour other than the three named ones. It compared the colour with every entry in the table, up to 4000 entries of memcmp.
  • forget_image: runs on every texture destruction. It ran std::erase_if over replaced_images, which holds the replaced views of every live texture, to find the ones belonging to the texture being destroyed.

Both move into runtime/src/descriptor_tables.h, which uses only the standard library and no Vulkan types:

  • BorderColorTable: up to 16 colours it uses the same scan as before. Past 16 a hash index answers (FNV-1a over is_int and the 16 bytes; equality is still is_int plus memcmp, so -0.0 and 0.0 stay different and NaNs still find themselves). It still clamps a new colour to 3999 once the table is full, and still logs that.
  • ReplacedImages<ImageEntry>: up to 384 views it uses the same vector and erase_if as before. Past 384 it moves them, in order, into a std::multimap by the texture's address, so a destruction visits only that texture's views.

The thresholds are where the old code stops being faster in the benchmark below, so small tables (the common case) keep the old cost.

No behavioural change

  • Border colours are never removed or reordered, and a colour is only added when it is missing. Each colour therefore has one index, the one the scan's first match gives.
  • std::multimap keeps equal keys in insertion order (C++11 and later). The vector-to-map move inserts in vector order. So the views of an address are released in the same order erase_if released them, and the same defer_destroy calls happen in the same sequence.
  • The read side (bind_sampler) reads is_int and the 16 colour bytes as before. It now memcpys them into VkClearColorValue, with a static_assert on the size.

Tests

runtime/test/descriptor_tables_test.cpp is registered with CTest as descriptor_tables. It runs on the CPU only, against copies of the old code:

  • Fixed cases:
    • first-match indices, and the integer flag being part of the key
    • -0.0 vs 0.0, and NaN
    • filling past 4000: new colours clamp to 3999 and aren't stored, old ones still find their index
    • the switch from scan to hash at 17 colours
    • the switch from vector to multimap at 385 views, with entries from before the switch still released and released oldest first
  • Random sequences:
    • 200 border-colour sequences (up to 6000 lookups over up to 5000 colours, including near-duplicates differing in one bit or only in is_int). Every returned index, every full flag and the final table must match the old scan exactly.
    • 200 add/take sequences (up to 4000 operations over up to 500 addresses). The released entries and their order must match erase_if on every take.
  • Benchmark: old and new code on the same sequences. --bench runs larger sizes.

Measured with llvm-mingw clang++ 20261117, -O2, Windows 11, x86-64. Lower is better.

border colours in table old scan new speedup
1 1.6 ns 3.6 ns (≈2 ns fixed cost, noise next to vkCreateSampler)
4 11.1 ns 11.7 ns 1.0x
16 15.5 ns 16.8 ns 0.9x
64 29.5 ns 15.3 ns 1.9x
256 81.0 ns 15.9 ns 5.1x
1000 287.7 ns 17.0 ns 16.9x
4000 (full) 1162.9 ns 18.6 ns 62.5x
replaced views (textures × views each) old erase_if per destroy new speedup
64 (64 × 1) 32.8 ns 32.8 ns 1.0x (same code)
256 (256 × 1) 82.4 ns 66.8 ns 1.2x (same code)
1024 (1024 × 1) 289.7 ns 88.1 ns 3.3x
4096 (4096 × 1) 915.4 ns 128.7 ns 7.1x
16384 (16384 × 1) 3631.0 ns 164.6 ns 22.1x
16384 (4096 × 4) 5695.6 ns 374.6 ns 15.2x

Old: O(N) per call, holding the descriptor mutex that bind_image, bind_sampler and every descriptor write also take. New: O(1) per lookup for border colours, and O(log N + k) per destroy for replaced views, where k is that texture's own views.

What I ran:

  • clang++ -std=c++20 -O2 -Wall -Wextra -Werror runtime/test/descriptor_tables_test.cpp: all checks passed.
  • cmake -S runtime -G Ninja -DLEONIDA_BUILD_WINDOWS_RUNTIME=OFF with llvm-mingw, then a full build of that target set (150/150), then ctest -R descriptor_tables: passed.

Not compiled locally: d3d12_vulkan_device.cpp itself. There is no Windows SDK d3d12.h on this machine, and llvm-mingw's copy is too old for gen_d3d12_stubs.py. Instead I compiled the exact edited code paths (the members, register_border_color, the bind_image add, forget_image, the bind_sampler read side) in a translation unit against Vulkan-Headers, with -Wall -Wextra -Wshadow -Werror. It compiled cleanly. The MSVC build in CI is the real check for the full file. No GPU was involved: these are CPU-only data structures. The translator suite wasn't run because the translator is untouched.

AI-assisted: written with Claude Code (Claude Opus 5.5), reviewed and run by me.

🤖 Generated with Claude Code

…ews by address

register_border_color (CreateSampler and every static sampler with a custom
border colour) scanned the whole table, up to 4000 entries, under
descriptor_mutex. forget_image (every texture destruction) ran std::erase_if
over every replaced view of every live texture.

Both move into runtime/src/descriptor_tables.h, standard library only. Past
16 colours a hash index answers; past 384 replaced views they move into a
multimap by the texture's address. Below those sizes the old linear code
stays, since it is the faster there. Indices, releases and release order are
unchanged: colours are never removed or reordered, and std::multimap keeps
equal keys in insertion order.

runtime/test/descriptor_tables_test.cpp checks both against copies of the old
code on fixed cases and 400 random sequences, then times them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@KarolKurzyna

Copy link
Copy Markdown
Contributor

Independent Windows 11 MSVC + full d3d12_vulkan_device.cpp + dual-GPU review of 2a63ad03eb (PASS, AI-assisted, synthetic tests only, no game data).

Environment

  • OS / Toolchain: Windows 11 x64, MSVC 19.52.37029 (x64, Ninja, full Windows D3D12 runtime ON), CMake 4.3.1-msvc1, Vulkan loader 1.4.363, VK_LOADER_LAYERS_DISABLE=~implicit~, LEONIDA_VK_VALIDATION=1.
  • GPU 1: NVIDIA GeForce RTX 3070 Ti (driver 621.24, Vulkan 1.4.357).
  • GPU 2: Intel(R) UHD Graphics 770 (Xe-LP, driver 101.6129, Vulkan 1.3.295).

Build & Code Audit

  • Built the full runtime/shims/d3d12_vulkan_device.cpp plus descriptor_tables_test, d3d12_sampler2_test, d3d12_descriptor_test, and d3d12_bindless_churn_test cleanly on MSVC (0 new warnings).
  • Audited runtime/src/descriptor_tables.h and all border_color_table / replaced_images call sites in runtime/shims/d3d12_vulkan_device.cpp:
    • static_assert(sizeof(custom_border.customBorderColor) == sizeof(entry.bits)) passes on MSVC/Windows SDK; is_int + memcmp preserves -0.0 vs +0.0 and NaN equality, and clamping at kCapacity = 4000 matches the prior scan.
    • ReplacedImages preserves insertion order across the vector-to-std::multimap promotion at kScanMax = 384, and safely transitions back to the vector path if by_address_ drains completely.
  • python -I scripts/check_no_game_data.py runtime/src/descriptor_tables.h runtime/test/descriptor_tables_test.cpp runtime/shims/d3d12_vulkan_device.cpp runtime/CMakeLists.txt: clean.

Test & Benchmark Results

  • CPU (descriptor_tables_test.exe --bench, MSVC): PASS (0 failures):
    • Border colours: 64: 24.1 -> 12.0 ns (2.0x), 256: 65.2 -> 9.9 ns (6.6x), 1000: 220.4 -> 12.2 ns (18.0x), 4000: 865.6 -> 12.8 ns (67.5x).
    • Replaced views: 1024 x 1: 125.4 -> 96.2 ns (1.3x), 4096 x 1: 466.4 -> 129.4 ns (3.6x), 16384 x 1: 1979.7 -> 192.1 ns (10.3x), 4096 x 4: 2479.5 -> 252.6 ns (9.8x).
  • RTX 3070 Ti (LEONIDA_VK_VALIDATION=1): d3d12_sampler2_test.exe, d3d12_descriptor_test.exe, d3d12_bindless_churn_test.exe — 3/3 PASS (0 validation errors).
  • Intel UHD Graphics 770 (LEONIDA_VK_VALIDATION=1): d3d12_sampler2_test.exe, d3d12_descriptor_test.exe, d3d12_bindless_churn_test.exe — 3/3 PASS (0 validation errors).

@lohant27-netizen

Copy link
Copy Markdown

Tested 2a63ad03e on Windows 11 (10.0.26300), NVIDIA GeForce RTX 5070 (driver 32.0.16.2101, subgroup 32), MSVC with SDK 10.0.26100, Release, merged onto main 77fa308f9 together with #8180, #8159, #8152, #8138 and #8155 (no conflicts): pass.

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.

3 participants