diff options
| author | jsthope <68657498+jsthope@users.noreply.github.com> | 2026-11-22 19:00:08 +0100 |
|---|---|---|
| committer | jsthope <68657498+jsthope@users.noreply.github.com> | 2026-11-22 19:17:52 +0100 |
| commit | df1ae87efe2045dedeca9ad4ff1a211eba2631b0 (patch) | |
| tree | 721eb26c9a4eb5f937054f17a169a713ab971ca3 /runtime/src | |
| parent | 454ae0637c9ed45131e031e71cc7d912e0b24ac8 (diff) | |
runtime: buffer_at finds a buffer by binary search, not a scan of every resource
VulkanContext::buffer_at walked every registered resource, images included,
for each vertex-buffer slot, index buffer and WriteBufferImmediate of every
draw. It now asks a BufferVaIndex of the addressable buffers (no image, a
VkBuffer, a VA and bytes: what the scan accepted), kept in step with
resources_ by add_resource, add_sparse_buffer and remove_resource; a
re-registered ID drops its old range first.
Where placed buffers alias, the scan returned whichever matched first in
unordered_map order; the index returns the range that starts closest below
the VA. docs/d3d12_vulkan.md drops the "Lookups" known gap.
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/vulkan_backend.h | 44 |
1 files changed, 32 insertions, 12 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index 140d7234b..4d005e998 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h | |||
| @@ -10,6 +10,7 @@ | |||
| 10 | #define VK_NO_PROTOTYPES | 10 | #define VK_NO_PROTOTYPES |
| 11 | #endif | 11 | #endif |
| 12 | #include "binary_occlusion_spirv.h" | 12 | #include "binary_occlusion_spirv.h" |
| 13 | #include "buffer_va_index.h" | ||
| 13 | #include "occlusion_sum_spirv.h" | 14 | #include "occlusion_sum_spirv.h" |
| 14 | #include "retire_tickets.h" | 15 | #include "retire_tickets.h" |
| 15 | #include "statistics_sum_spirv.h" | 16 | #include "statistics_sum_spirv.h" |
| @@ -818,18 +819,19 @@ public: | |||
| 818 | const auto it = graphics_pipelines_.find(id); | 819 | const auto it = graphics_pipelines_.find(id); |
| 819 | return it == graphics_pipelines_.end() ? nullptr : &it->second; | 820 | return it == graphics_pipelines_.end() ? nullptr : &it->second; |
| 820 | }; | 821 | }; |
| 821 | // Called with resource_mutex_ held. A linear search: fine for the first frames, an interval map when it shows. | 822 | // Called with resource_mutex_ held. A binary search of the buffers' VA ranges (buffer_va_index.h), kept in step |
| 823 | // with resources_ by set_resource_locked and remove_resource. | ||
| 822 | // A range that runs past its buffer is cut at the buffer's end (`size` is updated): D3D12 reads past a view's | 824 | // A range that runs past its buffer is cut at the buffer's end (`size` is updated): D3D12 reads past a view's |
| 823 | // buffer as out of bounds, which is no reason to drop the submission. | 825 | // buffer as out of bounds, which is no reason to drop the submission. |
| 824 | replay_.buffer_at = [this](std::uint64_t va, std::uint64_t& size, VkBuffer& buffer, VkDeviceSize& offset) { | 826 | replay_.buffer_at = [this](std::uint64_t va, std::uint64_t& size, VkBuffer& buffer, VkDeviceSize& offset) { |
| 825 | for (const auto& [id, b] : resources_) { | 827 | BufferVaIndex<ResourceId>::Hit hit{}; |
| 826 | if (b.image || !b.buffer || !b.va || va < b.va || va - b.va >= b.size) continue; | 828 | if (!buffer_vas_.find(va, hit)) return false; |
| 827 | buffer = b.buffer; | 829 | const ResourceBinding* b = resolve(hit.id); |
| 828 | offset = b.buffer_offset + (va - b.va); | 830 | if (!b) return false; |
| 829 | size = std::min<std::uint64_t>(size, b.size - (va - b.va)); | 831 | buffer = b->buffer; |
| 830 | return true; | 832 | offset = b->buffer_offset + (va - b->va); |
| 831 | } | 833 | size = std::min<std::uint64_t>(size, b->size - (va - b->va)); |
| 832 | return false; | 834 | return true; |
| 833 | }; | 835 | }; |
| 834 | if (xdxr && loaded_) { | 836 | if (xdxr && loaded_) { |
| 835 | xdxr_ = std::make_unique<XdxrBuilder>(get, device); | 837 | xdxr_ = std::make_unique<XdxrBuilder>(get, device); |
| @@ -927,11 +929,11 @@ public: | |||
| 927 | // The image must already be in that layout, e.g. transitioned from UNDEFINED at creation. | 929 | // The image must already be in that layout, e.g. transitioned from UNDEFINED at creation. |
| 928 | void add_resource(ResourceId id, const ResourceBinding& binding) { | 930 | void add_resource(ResourceId id, const ResourceBinding& binding) { |
| 929 | std::unique_lock lock(resource_mutex_); | 931 | std::unique_lock lock(resource_mutex_); |
| 930 | resources_[id] = binding; | 932 | set_resource_locked(id, binding); |
| 931 | } | 933 | } |
| 932 | void add_resource(ResourceId id, const ResourceBinding& binding, VkImageLayout current_layout) { | 934 | void add_resource(ResourceId id, const ResourceBinding& binding, VkImageLayout current_layout) { |
| 933 | std::unique_lock rlock(resource_mutex_); | 935 | std::unique_lock rlock(resource_mutex_); |
| 934 | resources_[id] = binding; | 936 | set_resource_locked(id, binding); |
| 935 | if (binding.image) { | 937 | if (binding.image) { |
| 936 | std::lock_guard tlock(tracker_mutex_); | 938 | std::lock_guard tlock(tracker_mutex_); |
| 937 | tracker_.track(id, binding, current_layout); | 939 | tracker_.track(id, binding, current_layout); |
| @@ -949,7 +951,7 @@ public: | |||
| 949 | 951 | ||
| 950 | void add_sparse_buffer(ResourceId id, const ResourceBinding& binding, std::shared_ptr<SparseBuffer> buffer) { | 952 | void add_sparse_buffer(ResourceId id, const ResourceBinding& binding, std::shared_ptr<SparseBuffer> buffer) { |
| 951 | std::unique_lock lock(resource_mutex_); | 953 | std::unique_lock lock(resource_mutex_); |
| 952 | resources_[id] = binding; | 954 | set_resource_locked(id, binding); |
| 953 | sparse_buffers_.emplace(id, std::move(buffer)); | 955 | sparse_buffers_.emplace(id, std::move(buffer)); |
| 954 | } | 956 | } |
| 955 | 957 | ||
| @@ -960,6 +962,7 @@ public: | |||
| 960 | const auto it = resources_.find(id); | 962 | const auto it = resources_.find(id); |
| 961 | if (it == resources_.end()) return; | 963 | if (it == resources_.end()) return; |
| 962 | img = it->second.image; | 964 | img = it->second.image; |
| 965 | unindex_buffer(id, it->second); | ||
| 963 | resources_.erase(it); | 966 | resources_.erase(it); |
| 964 | sparse_buffers_.erase(id); | 967 | sparse_buffers_.erase(id); |
| 965 | } | 968 | } |
| @@ -1227,6 +1230,22 @@ private: | |||
| 1227 | friend class VulkanQueue; | 1230 | friend class VulkanQueue; |
| 1228 | friend class VulkanPresenter; | 1231 | friend class VulkanPresenter; |
| 1229 | 1232 | ||
| 1233 | // buffer_at finds a buffer by VA: one with no image, a VkBuffer, a VA and bytes (what the linear search it replaced | ||
| 1234 | // accepted). Both are called with resource_mutex_ held uniquely. | ||
| 1235 | static bool addressable(const ResourceBinding& b) { return !b.image && b.buffer && b.va && b.size; } | ||
| 1236 | void unindex_buffer(ResourceId id, const ResourceBinding& b) { | ||
| 1237 | if (addressable(b)) buffer_vas_.erase(b.va, id); | ||
| 1238 | } | ||
| 1239 | // resources_[id] = binding, with the VA index kept in step (a re-registered ID drops its old range first). | ||
| 1240 | void set_resource_locked(ResourceId id, const ResourceBinding& binding) { | ||
| 1241 | auto [it, inserted] = resources_.try_emplace(id, binding); | ||
| 1242 | if (!inserted) { | ||
| 1243 | unindex_buffer(id, it->second); | ||
| 1244 | it->second = binding; | ||
| 1245 | } | ||
| 1246 | if (addressable(binding)) buffer_vas_.insert(binding.va, binding.size, id); | ||
| 1247 | } | ||
| 1248 | |||
| 1230 | const ResourceBinding* resolve(ResourceId id) const { | 1249 | const ResourceBinding* resolve(ResourceId id) const { |
| 1231 | const auto it = resources_.find(id); | 1250 | const auto it = resources_.find(id); |
| 1232 | return it == resources_.end() ? nullptr : &it->second; | 1251 | return it == resources_.end() ? nullptr : &it->second; |
| @@ -1269,6 +1288,7 @@ private: | |||
| 1269 | DeferredDeletionQueue deletion_queue_; | 1288 | DeferredDeletionQueue deletion_queue_; |
| 1270 | std::unique_ptr<FenceWaitDispatcher> fence_dispatcher_; | 1289 | std::unique_ptr<FenceWaitDispatcher> fence_dispatcher_; |
| 1271 | std::unordered_map<ResourceId, ResourceBinding> resources_; | 1290 | std::unordered_map<ResourceId, ResourceBinding> resources_; |
| 1291 | BufferVaIndex<ResourceId> buffer_vas_; // the addressable buffers of resources_, by VA range (buffer_at) | ||
| 1272 | std::unordered_map<ResourceId, std::shared_ptr<SparseBuffer>> sparse_buffers_; | 1292 | std::unordered_map<ResourceId, std::shared_ptr<SparseBuffer>> sparse_buffers_; |
| 1273 | std::unordered_map<std::uintptr_t, ComputePipeline> pipelines_; | 1293 | std::unordered_map<std::uintptr_t, ComputePipeline> pipelines_; |
| 1274 | std::unordered_map<std::uintptr_t, GraphicsPipeline> graphics_pipelines_; | 1294 | std::unordered_map<std::uintptr_t, GraphicsPipeline> graphics_pipelines_; |