You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
perf(vulkan): zero-copy DescriptorTable staging upload and uint32 wrap guard in read_slots - #1464
Eliminates redundant heap allocations and double-copies during bindless descriptor-table uploads and fixes a uint32_t addition wrap-around in DescriptorTable::read_slots():
Previously allocated a full temporary std::vector<std::uint32_t> via build_gpu_buffer() and memcpy'd it into dest. Now serializes directly into dest under mutex_ using alignment-safe memcpy into a stack-local 10-dword slot buffer (zero heap allocation, safe for any void* alignment).
Fixes if (first + count > entries_.size()) which could wrap around in 32-bit unsigned arithmetic (e.g., first = UINT32_MAX, count = 1 wrapped to 0 <= entries_.size()), and rejects count > 0 && !dest.
Avoid redundant allocations in BindlessSet::flush() (runtime/src/vulkan_bindless.h):
On fresh staging uploads (grew || !table_uploaded_), writes directly into table_buffers_.staging.mapped via table_.write_to_buffer(...) instead of allocating a full-table std::vector and copying twice.
On incremental slot updates, moves changes.slots instead of copying it and reuses a single std::vector<std::uint32_t> words across contiguous runs instead of allocating a new vector per run.
Unit tests (runtime/test/descriptor_table_test.cpp):
Adds regression tests for read_slotsuint32_t wrap-around (UINT32_MAX, 1 and UINT32_MAX - 2, 4), null-dest handling, and byte-for-byte parity of write_to_buffer vs build_gpu_buffer for both clear_pad modes.
Tested 05e2e39 merged onto main (bb2e184) on AMD Radeon RX 9070 (RDNA4), AMD 26.11.2, Windows 11 Pro 26300, MSVC 14.44: pass.
descriptor_table_test: all tests passed (0 failures), including the new read_slots wrap and null-destination cases.
run_gpu_tests.py --filter ps_tex.asm with LEONIDA_VK_VALIDATION=1: ps_tex bindless: 24 draws read back, 0 bad pixels, largest error 0. That drives BindlessSet::flush() through both the fresh staging upload and the incremental-slot paths this changes, with the 65536-image set this GPU sizes to.
Tested 05e2e39 on RTX 3070 (driver 610.88), Windows 11 26300, MSVC (VS 2022), Windows SDK 10.0.26100: pass. This is the NVIDIA side, to go with @Dom-in's RX 9070 run.
ctest: 79/79, 0 build warnings.
descriptor_table_test, d3d12_vulkan_device_test, d3d12_com_test and vk_command_replay_test with LEONIDA_VK_VALIDATION=1: pass, 0 validation errors.
Negative control: descriptor_table_test built against main's descriptor_table.h fails at the first new check (!table.read_slots(UINT32_MAX, 1, read)), so the test does catch the wrap.
One thing from reading it, not a bug today: upload_table sizes the staging buffer from bytes = table_.size_bytes() and later calls write_to_buffer, which writes the table's size at that moment, with no destination size. Before, the memcpy copied exactly bytes from a snapshot, so a table that grew in between couldn't overrun the buffer. That's fine now, because every insert/erase and the flush hold descriptor_mutex (d3d12_vulkan_device.cpp 581, 718, 1803). But a write_to_buffer(void* dest, std::size_t dest_bytes, bool clear_pad) that refuses when size_bytes() > dest_bytes would keep a future caller from writing past a mapped buffer.
Tested on Windows 11 with two GPUs on one machine (runtime built from this PR's head, MSVC 14.51): pass. ctest: 79/79 on the RTX 4080 SUPER, including the descriptor-table staging tests. The Intel UHD 770 failures are known and unrelated: d3d12_descriptor (min-LOD), d3d12_copy_xfmt, and d3d12_present (reported in #1380).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Eliminates redundant heap allocations and double-copies during bindless descriptor-table uploads and fixes a
uint32_taddition wrap-around inDescriptorTable::read_slots():DescriptorTable::write_to_buffer()(runtime/src/descriptor_table.h):std::vector<std::uint32_t>viabuild_gpu_buffer()andmemcpy'd it intodest. Now serializes directly intodestundermutex_using alignment-safememcpyinto a stack-local 10-dword slot buffer (zero heap allocation, safe for anyvoid*alignment).DescriptorTable::read_slots()(runtime/src/descriptor_table.h):if (first + count > entries_.size())which could wrap around in 32-bit unsigned arithmetic (e.g.,first = UINT32_MAX, count = 1wrapped to0 <= entries_.size()), and rejectscount > 0 && !dest.BindlessSet::flush()(runtime/src/vulkan_bindless.h):grew || !table_uploaded_), writes directly intotable_buffers_.staging.mappedviatable_.write_to_buffer(...)instead of allocating a full-tablestd::vectorand copying twice.changes.slotsinstead of copying it and reuses a singlestd::vector<std::uint32_t> wordsacross contiguous runs instead of allocating a new vector per run.runtime/test/descriptor_table_test.cpp):read_slotsuint32_twrap-around (UINT32_MAX, 1andUINT32_MAX - 2, 4), null-dest handling, and byte-for-byte parity ofwrite_to_buffervsbuild_gpu_bufferfor bothclear_padmodes.Validation (NVIDIA GeForce RTX 4070 Ti,
LEONIDA_VK_VALIDATION=1— Core + Synchronization Validation)RelWithDebInfoandDebug: 0 errors, 0 new warnings.ctest -C RelWithDebInfo -E d3d12_clear_test: 79/79 passed (0 validation errors).ctest -C Debug -E d3d12_clear_test: 79/79 passed (0 validation errors).descriptor_table,bindless_sizing,d3d12_multithread,d3d12_vulkan_device,d3d12_descriptor,gfx10_descriptors,vk_descriptor_bench_selftest): 7/7 passed in bothRelWithDebInfoandDebug(0 validation errors).