diff options
| author | totonii <totonicontato@gmail.com> | 2026-11-23 16:54:12 -0300 |
|---|---|---|
| committer | totonii <totonicontato@gmail.com> | 2026-11-23 16:54:12 -0300 |
| commit | 0c2effa27fea08d4df7c3ab40eef821864d9a1fa (patch) | |
| tree | cf7a8096e9fcec790822c39e3e31e1af9a8b0682 /runtime/src | |
| parent | 867d922f09b86583859796a174df89555fb32da8 (diff) | |
fix(vulkan): use memory_type_order and validate stride/alignment in IndirectDispatchCounter
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/vulkan_indirect_dispatch_count.h | 35 |
1 files changed, 18 insertions, 17 deletions
diff --git a/runtime/src/vulkan_indirect_dispatch_count.h b/runtime/src/vulkan_indirect_dispatch_count.h index 225f7c1fc..510a4dcad 100644 --- a/runtime/src/vulkan_indirect_dispatch_count.h +++ b/runtime/src/vulkan_indirect_dispatch_count.h | |||
| @@ -12,6 +12,7 @@ | |||
| 12 | #include <memory> | 12 | #include <memory> |
| 13 | #include <vector> | 13 | #include <vector> |
| 14 | #include "indirect_dispatch_count_spv.h" | 14 | #include "indirect_dispatch_count_spv.h" |
| 15 | #include "vulkan_memory.h" | ||
| 15 | 16 | ||
| 16 | namespace leonida { | 17 | namespace leonida { |
| 17 | 18 | ||
| @@ -59,9 +60,10 @@ public: | |||
| 59 | VkDeviceSize count_offset, std::uint32_t stride, std::uint32_t max_count, | 60 | VkDeviceSize count_offset, std::uint32_t stride, std::uint32_t max_count, |
| 60 | CountedDispatch& out) { | 61 | CountedDispatch& out) { |
| 61 | out = {}; | 62 | out = {}; |
| 62 | if (!arguments || !count || !stride || stride % 4 || !max_count || | ||
| 63 | !owner.limits_.maxComputeWorkGroupCount[0] || !owner.make_pipeline()) return false; | ||
| 64 | constexpr VkDeviceSize record_bytes = 3 * sizeof(std::uint32_t); | 63 | constexpr VkDeviceSize record_bytes = 3 * sizeof(std::uint32_t); |
| 64 | if (!arguments || !count || stride < record_bytes || stride % 4 || | ||
| 65 | (argument_offset % 4) != 0 || (count_offset % 4) != 0 || !max_count || | ||
| 66 | !owner.limits_.maxComputeWorkGroupCount[0] || !owner.make_pipeline()) return false; | ||
| 65 | if (VkDeviceSize(max_count) > std::numeric_limits<VkDeviceSize>::max() / record_bytes) | 67 | if (VkDeviceSize(max_count) > std::numeric_limits<VkDeviceSize>::max() / record_bytes) |
| 66 | return false; | 68 | return false; |
| 67 | const VkDeviceSize bytes = VkDeviceSize(max_count) * record_bytes; | 69 | const VkDeviceSize bytes = VkDeviceSize(max_count) * record_bytes; |
| @@ -77,22 +79,20 @@ public: | |||
| 77 | VkMemoryRequirements requirements{}; | 79 | VkMemoryRequirements requirements{}; |
| 78 | owner.fn<PFN_vkGetBufferMemoryRequirements>("vkGetBufferMemoryRequirements")( | 80 | owner.fn<PFN_vkGetBufferMemoryRequirements>("vkGetBufferMemoryRequirements")( |
| 79 | owner.device_, a.buffer, &requirements); | 81 | owner.device_, a.buffer, &requirements); |
| 80 | std::uint32_t memory_type = UINT32_MAX; | 82 | const auto order = memory_type_order(owner.memory_, requirements.memoryTypeBits, 0, |
| 81 | for (std::uint32_t i = 0; i < owner.memory_.memoryTypeCount; ++i) { | 83 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, requirements.size); |
| 82 | if (!(requirements.memoryTypeBits & (1u << i))) continue; | 84 | if (order.empty()) return false; |
| 83 | if (memory_type == UINT32_MAX) memory_type = i; | ||
| 84 | if (owner.memory_.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) { | ||
| 85 | memory_type = i; | ||
| 86 | break; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | if (memory_type == UINT32_MAX) return false; | ||
| 90 | VkMemoryAllocateFlagsInfo flags{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO}; | 85 | VkMemoryAllocateFlagsInfo flags{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO}; |
| 91 | flags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT; | 86 | flags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT; |
| 92 | VkMemoryAllocateInfo allocation{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, &flags}; | 87 | VkMemoryAllocateInfo allocation{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, &flags}; |
| 93 | allocation.allocationSize = requirements.size; | 88 | allocation.allocationSize = requirements.size; |
| 94 | allocation.memoryTypeIndex = memory_type; | 89 | const auto alloc_memory = owner.fn<PFN_vkAllocateMemory>("vkAllocateMemory"); |
| 95 | if (owner.fn<PFN_vkAllocateMemory>("vkAllocateMemory")(owner.device_, &allocation, nullptr, &a.memory) != VK_SUCCESS || | 90 | for (const std::uint32_t memory_type : order) { |
| 91 | allocation.memoryTypeIndex = memory_type; | ||
| 92 | if (alloc_memory(owner.device_, &allocation, nullptr, &a.memory) == VK_SUCCESS) break; | ||
| 93 | a.memory = VK_NULL_HANDLE; | ||
| 94 | } | ||
| 95 | if (!a.memory || | ||
| 96 | owner.fn<PFN_vkBindBufferMemory>("vkBindBufferMemory")(owner.device_, a.buffer, a.memory, 0) != VK_SUCCESS) | 96 | owner.fn<PFN_vkBindBufferMemory>("vkBindBufferMemory")(owner.device_, a.buffer, a.memory, 0) != VK_SUCCESS) |
| 97 | return false; | 97 | return false; |
| 98 | const auto address = owner.fn<PFN_vkGetBufferDeviceAddress>("vkGetBufferDeviceAddress"); | 98 | const auto address = owner.fn<PFN_vkGetBufferDeviceAddress>("vkGetBufferDeviceAddress"); |
| @@ -129,8 +129,9 @@ public: | |||
| 129 | static_assert(sizeof(Push) == 40); | 129 | static_assert(sizeof(Push) == 40); |
| 130 | const VkDeviceSize dispatch_limit = VkDeviceSize(limits_.maxComputeWorkGroupCount[0]) * 64; | 130 | const VkDeviceSize dispatch_limit = VkDeviceSize(limits_.maxComputeWorkGroupCount[0]) * 64; |
| 131 | // Bound per-dispatch indexing to 32 bits, even when the D3D12 stride is very large. | 131 | // Bound per-dispatch indexing to 32 bits, even when the D3D12 stride is very large. |
| 132 | // The shader loads three consecutive dwords, so leave room for the final +2. | 132 | // The shader loads and stores three consecutive dwords per record, so leave room for the final +2. |
| 133 | const VkDeviceSize index_limit = (UINT32_MAX - 2u) / (job.stride / 4); | 133 | const std::uint32_t index_stride_words = std::max<std::uint32_t>(3u, job.stride / 4); |
| 134 | const VkDeviceSize index_limit = (UINT32_MAX - 2u) / index_stride_words; | ||
| 134 | const VkDeviceSize chunk_limit = std::min(dispatch_limit, index_limit + 1); | 135 | const VkDeviceSize chunk_limit = std::min(dispatch_limit, index_limit + 1); |
| 135 | for (VkDeviceSize first = 0; first < job.max_count;) { | 136 | for (VkDeviceSize first = 0; first < job.max_count;) { |
| 136 | const std::uint32_t count = std::uint32_t(std::min<VkDeviceSize>(chunk_limit, job.max_count - first)); | 137 | const std::uint32_t count = std::uint32_t(std::min<VkDeviceSize>(chunk_limit, job.max_count - first)); |
| @@ -138,7 +139,7 @@ public: | |||
| 138 | job.stride / 4, std::uint32_t(first), count}; | 139 | job.stride / 4, std::uint32_t(first), count}; |
| 139 | fn<PFN_vkCmdPushConstants>("vkCmdPushConstants")(cb, layout_, VK_SHADER_STAGE_COMPUTE_BIT, | 140 | fn<PFN_vkCmdPushConstants>("vkCmdPushConstants")(cb, layout_, VK_SHADER_STAGE_COMPUTE_BIT, |
| 140 | 0, sizeof(push), &push); | 141 | 0, sizeof(push), &push); |
| 141 | fn<PFN_vkCmdDispatch>("vkCmdDispatch")(cb, (count + 63u) / 64u, 1, 1); | 142 | fn<PFN_vkCmdDispatch>("vkCmdDispatch")(cb, (count - 1u) / 64u + 1u, 1, 1); |
| 142 | first += count; | 143 | first += count; |
| 143 | } | 144 | } |
| 144 | VkMemoryBarrier after{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; | 145 | VkMemoryBarrier after{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; |