diff options
| author | totonii <totonicontato@gmail.com> | 2026-11-22 08:33:44 -0300 |
|---|---|---|
| committer | totonii <totonicontato@gmail.com> | 2026-11-22 08:33:44 -0300 |
| commit | 05e2e39db96234459ab8f74cc5e364f7ba01adeb (patch) | |
| tree | 60ddae38ef0a7e74819869bf661a04a7f450561e /runtime/src | |
| parent | c2967a5e60060e4068fb992196d8f4b494a5046c (diff) | |
perf(vulkan): zero-copy DescriptorTable staging upload and uint32 wrap guard in read_slots
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/descriptor_table.h | 18 | ||||
| -rw-r--r-- | runtime/src/vulkan_bindless.h | 11 |
2 files changed, 21 insertions, 8 deletions
diff --git a/runtime/src/descriptor_table.h b/runtime/src/descriptor_table.h index 3466bc325..59f96fd4f 100644 --- a/runtime/src/descriptor_table.h +++ b/runtime/src/descriptor_table.h | |||
| @@ -207,8 +207,9 @@ public: | |||
| 207 | 207 | ||
| 208 | // `count` consecutive slots' 10 dwords each to `dest`, under one lock; false when any is out of range. | 208 | // `count` consecutive slots' 10 dwords each to `dest`, under one lock; false when any is out of range. |
| 209 | bool read_slots(std::uint32_t first, std::uint32_t count, std::uint32_t* dest) const { | 209 | bool read_slots(std::uint32_t first, std::uint32_t count, std::uint32_t* dest) const { |
| 210 | if (count > 0 && !dest) return false; | ||
| 210 | std::lock_guard lock(mutex_); | 211 | std::lock_guard lock(mutex_); |
| 211 | if (first + count > entries_.size()) return false; | 212 | if (first > entries_.size() || count > entries_.size() - first) return false; |
| 212 | for (std::uint32_t i = 0; i < count; ++i) { | 213 | for (std::uint32_t i = 0; i < count; ++i) { |
| 213 | const DescriptorEntry& entry = entries_[first + i]; | 214 | const DescriptorEntry& entry = entries_[first + i]; |
| 214 | std::memcpy(dest + i * 10, entry.key, sizeof(entry.key)); | 215 | std::memcpy(dest + i * 10, entry.key, sizeof(entry.key)); |
| @@ -218,10 +219,21 @@ public: | |||
| 218 | return true; | 219 | return true; |
| 219 | } | 220 | } |
| 220 | 221 | ||
| 222 | // Serializes directly into `dest` (must hold at least size_bytes()) without allocating a temporary vector. | ||
| 221 | void write_to_buffer(void* dest, bool clear_pad = false) const { | 223 | void write_to_buffer(void* dest, bool clear_pad = false) const { |
| 222 | if (!dest) return; | 224 | if (!dest) return; |
| 223 | const auto buf = build_gpu_buffer(clear_pad); | 225 | std::lock_guard lock(mutex_); |
| 224 | std::memcpy(dest, buf.data(), buf.size() * sizeof(std::uint32_t)); | 226 | auto* out = static_cast<std::uint8_t*>(dest); |
| 227 | std::memcpy(out, &mask_, sizeof(mask_)); | ||
| 228 | out += sizeof(mask_); | ||
| 229 | for (const auto& entry : entries_) { | ||
| 230 | std::uint32_t slot[10]; | ||
| 231 | std::memcpy(slot, entry.key, sizeof(entry.key)); | ||
| 232 | slot[8] = entry.index; | ||
| 233 | slot[9] = clear_pad ? 0 : entry.pad; | ||
| 234 | std::memcpy(out, slot, sizeof(slot)); | ||
| 235 | out += sizeof(slot); | ||
| 236 | } | ||
| 225 | } | 237 | } |
| 226 | 238 | ||
| 227 | private: | 239 | private: |
diff --git a/runtime/src/vulkan_bindless.h b/runtime/src/vulkan_bindless.h index 3bf07fa5d..c599eb7c2 100644 --- a/runtime/src/vulkan_bindless.h +++ b/runtime/src/vulkan_bindless.h | |||
| @@ -724,7 +724,7 @@ private: | |||
| 724 | full = true; | 724 | full = true; |
| 725 | grew = true; | 725 | grew = true; |
| 726 | } | 726 | } |
| 727 | const DescriptorTable::Changes changes = table_.take_changes(); | 727 | DescriptorTable::Changes changes = table_.take_changes(); |
| 728 | full = full || changes.all; | 728 | full = full || changes.all; |
| 729 | bool recorded = false; | 729 | bool recorded = false; |
| 730 | // The staging buffer is written only for buffers that were just created (first upload or growth): a later | 730 | // The staging buffer is written only for buffers that were just created (first upload or growth): a later |
| @@ -732,11 +732,11 @@ private: | |||
| 732 | // flight can be reading bytes the CPU rewrites. | 732 | // flight can be reading bytes the CPU rewrites. |
| 733 | const bool fresh = grew || !table_uploaded_; | 733 | const bool fresh = grew || !table_uploaded_; |
| 734 | const auto upload_all = [&]() { | 734 | const auto upload_all = [&]() { |
| 735 | const std::vector<std::uint32_t> words = table_.build_gpu_buffer(); | ||
| 736 | if (fresh) { | 735 | if (fresh) { |
| 737 | std::memcpy(table_buffers_.staging.mapped, words.data(), bytes); | 736 | table_.write_to_buffer(table_buffers_.staging.mapped); |
| 738 | copy_table_range(command_buffer, 0, bytes); | 737 | copy_table_range(command_buffer, 0, bytes); |
| 739 | } else { | 738 | } else { |
| 739 | const std::vector<std::uint32_t> words = table_.build_gpu_buffer(); | ||
| 740 | update_table_range(command_buffer, 0, words.data(), bytes); | 740 | update_table_range(command_buffer, 0, words.data(), bytes); |
| 741 | } | 741 | } |
| 742 | recorded = true; | 742 | recorded = true; |
| @@ -744,15 +744,16 @@ private: | |||
| 744 | if (full) { | 744 | if (full) { |
| 745 | upload_all(); | 745 | upload_all(); |
| 746 | } else if (!changes.slots.empty()) { | 746 | } else if (!changes.slots.empty()) { |
| 747 | std::vector<std::uint32_t> slots = changes.slots; | 747 | std::vector<std::uint32_t> slots = std::move(changes.slots); |
| 748 | std::sort(slots.begin(), slots.end()); | 748 | std::sort(slots.begin(), slots.end()); |
| 749 | slots.erase(std::unique(slots.begin(), slots.end()), slots.end()); | 749 | slots.erase(std::unique(slots.begin(), slots.end()), slots.end()); |
| 750 | std::vector<std::uint32_t> words; | ||
| 750 | bool staged = true; | 751 | bool staged = true; |
| 751 | for (std::size_t first = 0; staged && first < slots.size();) { | 752 | for (std::size_t first = 0; staged && first < slots.size();) { |
| 752 | std::size_t last = first; | 753 | std::size_t last = first; |
| 753 | while (last + 1 < slots.size() && slots[last + 1] == slots[last] + 1) ++last; | 754 | while (last + 1 < slots.size() && slots[last + 1] == slots[last] + 1) ++last; |
| 754 | const std::uint32_t count = std::uint32_t(last - first + 1); | 755 | const std::uint32_t count = std::uint32_t(last - first + 1); |
| 755 | std::vector<std::uint32_t> words(std::size_t(count) * 10); | 756 | words.resize(std::size_t(count) * 10); |
| 756 | staged = table_.read_slots(slots[first], count, words.data()); | 757 | staged = table_.read_slots(slots[first], count, words.data()); |
| 757 | if (staged) { | 758 | if (staged) { |
| 758 | update_table_range(command_buffer, entry_offset(slots[first]), words.data(), | 759 | update_table_range(command_buffer, entry_offset(slots[first]), words.data(), |