Skip to content

vulkan_bindless: pending writes in flat per-binding arrays, free index by memchr (no allocation per queued descriptor) - #8180

Open
TG0000 wants to merge 1 commit into
ItsNotRexed:mainfrom
TG0000:perf/vulkan-bindless-fast
Open

TG0000 wants to merge 1 commit into
ItsNotRexed:mainfrom
TG0000:perf/vulkan-bindless-fast

Conversation

@TG0000

@TG0000 TG0000 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Item: new item claimed in #8047 (claim), a CPU hot path in runtime/src/vulkan_bindless.h.

What

BindlessSet does this work every time a texture is streamed in or out, or a view changes:

  • queue() kept its one-write-per-slot map in a std::unordered_map<uint64_t, size_t>. Every descriptor it queued (up to five per add_image: sampled, three storage, r32ui) allocated a heap node, and flush() freed them all with clear(). The map is now one std::vector<uint32_t> per binding (pending_at_, 1 + the place in pending_, 0 when the slot has no pending write). create_layout() sizes each to the binding's count, and flush() zeroes only the entries pending_ used. An index past the count, which BindlessSet never queues, grows the vector, so even that case behaves as before.
  • flush()'s in-place VkWriteDescriptorSet array was a new std::vector on every flush. It is now a member that is reused.
  • allocate() looked for the first free index one byte at a time. It now calls memchr for the first zero byte, which returns the same index, marks it used and returns 0 when there is none.

There is no behaviour change. The order of writes stays first-queued, the last info queued for a slot still wins, and the indices returned are the same.

Proof of equivalence

runtime/test/bindless_queue_test.cpp is a new CPU-only ctest (bindless_queue, about 0.6 s, no GPU). It has three parts:

  1. The whole class, through a fake Vulkan device. The test drives the real BindlessSet with random operations: add, update and remove images in all six dimensions, with and without storage views and MS classes; add and remove samplers; insert and erase keys, so both the in-place and the replace-the-generation flush() paths run. It covers 30 configurations and seeds, 60 frames × 120 operations each. It checks every returned index against the old allocation steps, free_images() against the model, and that no in-place flush writes a slot twice. It also prints an FNV digest of every vkUpdateDescriptorSets element and every returned index. I built the same test file against the origin/main header (-DLEONIDA_BINDLESS_HEADER=...), and the digest is identical (94e517cb5ed9a136) for the old and new header.
  2. Copies of the old queue/clear and allocate, compared with the new ones on fixed cases and random inputs: 200 rounds × 4 flushes of up to 4000 queues, and 300 rounds of used arrays up to 70 000 entries with random ranges. They must give the same pending list and the same index and array state each time.
  3. A short benchmark. --bench runs a longer one. A global operator new counter gives the allocation counts.

Measurements

Windows 11, AMD Radeon RX 6800 machine (CPU-only test), llvm-mingw clang 20261117, -O2 (RelWithDebInfo), --bench, 3 runs:

old (origin/main header) new
BindlessSet frame: 256 adds with storage views + 256 updates + 2 flushes, 65 536 2D images 7/8 full 206 / 222 / 411 µs 48 / 54 / 93 µs 4.3× / 4.1× / 4.4×
same frame, default 4096 2D images 192 / 203 / 379 µs 46 / 58 / 75 µs 4.2× / 3.5× / 5.0×
heap allocations per frame 2811 (2852 at 4096) 0
extracted queue: 512 images × 5 writes + clear 156.6 µs, 2015 allocs 22.4 µs, 0 allocs 7.0×
extracted allocate, 65 536 indices, free ones in the upper half 16.9 µs 3.1 µs 5.5×

The third run of each row was slower for both headers alike (machine load). The speedup holds within each run.

Tested

  • cmake -S runtime -B runtime/build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLEONIDA_BUILD_WINDOWS_RUNTIME=OFF with llvm-mingw clang. Everything builds without warnings.
  • ctest -j8: 65 of 68 pass and 2 are skipped (shader_library_corpus, vk_sync_validation). The one failure is vk_queue_query_sync, at line 468: progress_loss_ok ("progress counter device loss NOT propagated") on the RX 6800. Its sources do not include vulkan_bindless.h (checked with ninja -t deps), so this change cannot affect it.
  • bindless_sizing and the new bindless_queue pass.
  • I did not run tests/semantic, because this does not touch the translator.

It does not touch the lines of #4517, #4672 or #2756 (the other open PRs on this file), or gfx10_descriptors.h, vulkan_backend.h or image_view_cache.h.

AI-assisted (Claude Code). I reviewed the change and checked the measurements above on this machine.

🤖 Generated with Claude Code

…x by memchr

BindlessSet::queue() kept its one-write-per-slot map in a std::unordered_map:
a heap node for every descriptor queued (up to five per add_image), all freed
again by flush(). The map is now one position array per binding, sized to the
binding's count in create_layout(); flush() zeroes only the entries it used.
flush()'s in-place VkWriteDescriptorSet array is a reused member.

allocate() looked for the first free index one byte at a time; memchr finds
the same first zero byte.

No behaviour change: bindless_queue_test drives BindlessSet through a fake
Vulkan device and digests every descriptor write and returned index; the
digest is the same built against the previous header. It also compares copies
of the old queue and allocate with the new ones on fixed and random inputs.

Per streamed frame (256 adds with storage views, 256 updates, two flushes):
206 -> 48 us and 2811 -> 0 heap allocations.

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

Copy link
Copy Markdown

Tested 2ef5052cb 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.

2 participants