upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/vulkan_predicate.h
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/src/vulkan_predicate.h')
-rw-r--r--runtime/src/vulkan_predicate.h183
1 files changed, 183 insertions, 0 deletions
diff --git a/runtime/src/vulkan_predicate.h b/runtime/src/vulkan_predicate.h
new file mode 100644
index 000000000..cedb8af81
--- /dev/null
+++ b/runtime/src/vulkan_predicate.h
@@ -0,0 +1,183 @@
1// GPU snapshot of D3D12's 64-bit predicate. No host readback and no per-predicate queue wait.
2#pragma once
3#ifndef VK_NO_PROTOTYPES
4#define VK_NO_PROTOTYPES
5#endif
6#include <vulkan/vulkan.h>
7#include <cstdint>
8#include <memory>
9#include <initializer_list>
10#include "predicate_reduce_spv.h"
11
12namespace leonida {
13struct PredicateCapture {
14 VkBuffer source = VK_NULL_HANDLE;
15 VkDeviceSize source_offset = 0;
16 VkBuffer scratch = VK_NULL_HANDLE;
17 VkDeviceSize offset = 0; // two input words, then the reduced condition
18 VkDescriptorSet set = VK_NULL_HANDLE;
19};
20
21class PredicateReducer {
22public:
23 PredicateReducer(PFN_vkGetDeviceProcAddr get, VkDevice device, VkPhysicalDeviceMemoryProperties memory,
24 VkDeviceSize max_range) : get_(get), device_(device), memory_(memory), max_range_(max_range) {}
25 ~PredicateReducer() {
26 if (pipeline_) fn<PFN_vkDestroyPipeline>("vkDestroyPipeline")(device_, pipeline_, nullptr);
27 if (layout_) fn<PFN_vkDestroyPipelineLayout>("vkDestroyPipelineLayout")(device_, layout_, nullptr);
28 if (set_layout_) fn<PFN_vkDestroyDescriptorSetLayout>("vkDestroyDescriptorSetLayout")(device_, set_layout_, nullptr);
29 }
30 PredicateReducer(const PredicateReducer&) = delete;
31 PredicateReducer& operator=(const PredicateReducer&) = delete;
32
33 // Owned by a command-buffer slot until its timeline ticket completes (or a failed recording is reset).
34 struct Batch {
35 PredicateReducer& owner;
36 VkBuffer buffer = VK_NULL_HANDLE;
37 VkDeviceMemory memory = VK_NULL_HANDLE;
38 VkDescriptorPool pool = VK_NULL_HANDLE;
39 VkDescriptorSet set = VK_NULL_HANDLE;
40 VkDeviceSize bytes = 0;
41 std::uint32_t count = 0, next = 0;
42 explicit Batch(PredicateReducer& r) : owner(r) {}
43 ~Batch() {
44 if (pool) owner.fn<PFN_vkDestroyDescriptorPool>("vkDestroyDescriptorPool")(owner.device_, pool, nullptr);
45 if (buffer) owner.fn<PFN_vkDestroyBuffer>("vkDestroyBuffer")(owner.device_, buffer, nullptr);
46 if (memory) owner.fn<PFN_vkFreeMemory>("vkFreeMemory")(owner.device_, memory, nullptr);
47 }
48 Batch(const Batch&) = delete;
49 Batch& operator=(const Batch&) = delete;
50 bool prepare(VkBuffer source, VkDeviceSize offset, PredicateCapture& out) {
51 if (next >= count || !source) return false;
52 out = {source, offset, buffer, VkDeviceSize(next++) * 16, set};
53 return true;
54 }
55 };
56
57 std::unique_ptr<Batch> batch(std::uint64_t count) {
58 // One descriptor starts at offset zero; a push constant selects each aligned 16-byte slot.
59 if (!count || count > max_range_ / 16 || count > UINT32_MAX / 4 || !make_pipeline()) return {};
60 auto b = std::make_unique<Batch>(*this);
61 b->count = static_cast<std::uint32_t>(count);
62 b->bytes = count * 16;
63 VkBufferCreateInfo buffer{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
64 buffer.size = b->bytes;
65 buffer.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
66 VK_BUFFER_USAGE_CONDITIONAL_RENDERING_BIT_EXT;
67 if (fn<PFN_vkCreateBuffer>("vkCreateBuffer")(device_, &buffer, nullptr, &b->buffer) != VK_SUCCESS) return {};
68 VkMemoryRequirements requirements{};
69 fn<PFN_vkGetBufferMemoryRequirements>("vkGetBufferMemoryRequirements")(device_, b->buffer, &requirements);
70 std::uint32_t type = UINT32_MAX;
71 for (std::uint32_t i = 0; i < memory_.memoryTypeCount; ++i) {
72 if (!(requirements.memoryTypeBits & (1u << i))) continue;
73 if (type == UINT32_MAX) type = i;
74 if (memory_.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) { type = i; break; }
75 }
76 if (type == UINT32_MAX) return {};
77 VkMemoryAllocateInfo allocation{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
78 allocation.allocationSize = requirements.size;
79 allocation.memoryTypeIndex = type;
80 if (fn<PFN_vkAllocateMemory>("vkAllocateMemory")(device_, &allocation, nullptr, &b->memory) != VK_SUCCESS ||
81 fn<PFN_vkBindBufferMemory>("vkBindBufferMemory")(device_, b->buffer, b->memory, 0) != VK_SUCCESS) return {};
82 const VkDescriptorPoolSize size{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1};
83 VkDescriptorPoolCreateInfo pool{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO};
84 pool.maxSets = 1; pool.poolSizeCount = 1; pool.pPoolSizes = &size;
85 if (fn<PFN_vkCreateDescriptorPool>("vkCreateDescriptorPool")(device_, &pool, nullptr, &b->pool) != VK_SUCCESS) return {};
86 VkDescriptorSetAllocateInfo sets{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO};
87 sets.descriptorPool = b->pool; sets.descriptorSetCount = 1; sets.pSetLayouts = &set_layout_;
88 if (fn<PFN_vkAllocateDescriptorSets>("vkAllocateDescriptorSets")(device_, &sets, &b->set) != VK_SUCCESS) return {};
89 const VkDescriptorBufferInfo info{b->buffer, 0, b->bytes};
90 VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
91 write.dstSet = b->set; write.dstBinding = 0; write.descriptorCount = 1;
92 write.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; write.pBufferInfo = &info;
93 fn<PFN_vkUpdateDescriptorSets>("vkUpdateDescriptorSets")(device_, 1, &write, 0, nullptr);
94 return b;
95 }
96
97 void capture(VkCommandBuffer cb, const PredicateCapture& p) const {
98 // End an earlier conditional scope before entering this helper: dispatch is itself conditional.
99 // The descriptor spans the arena, so order all prior accesses before reusing it for another slot.
100 VkMemoryBarrier before{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
101 before.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT | VK_ACCESS_MEMORY_READ_BIT;
102 before.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT;
103 const auto barrier = fn<PFN_vkCmdPipelineBarrier>("vkCmdPipelineBarrier");
104 barrier(cb, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 1, &before, 0, nullptr, 0, nullptr);
105 const VkBufferCopy copy{p.source_offset, p.offset, 8};
106 fn<PFN_vkCmdCopyBuffer>("vkCmdCopyBuffer")(cb, p.source, p.scratch, 1, &copy);
107 // The application's PREDICATION state does not describe this internal transfer read. Preserve snapshot
108 // semantics when it subsequently transitions and overwrites the source in the same command list.
109 VkBufferMemoryBarrier source_done{VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER};
110 source_done.srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
111 source_done.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
112 source_done.srcQueueFamilyIndex = source_done.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
113 source_done.buffer = p.source; source_done.offset = p.source_offset; source_done.size = 8;
114 barrier(cb, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
115 0, 0, nullptr, 1, &source_done, 0, nullptr);
116 VkMemoryBarrier compute{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
117 compute.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
118 compute.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT;
119 barrier(cb, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 1, &compute, 0, nullptr, 0, nullptr);
120 fn<PFN_vkCmdBindPipeline>("vkCmdBindPipeline")(cb, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline_);
121 fn<PFN_vkCmdBindDescriptorSets>("vkCmdBindDescriptorSets")(cb, VK_PIPELINE_BIND_POINT_COMPUTE, layout_, 0, 1, &p.set, 0, nullptr);
122 const std::uint32_t index = static_cast<std::uint32_t>(p.offset / 4);
123 fn<PFN_vkCmdPushConstants>("vkCmdPushConstants")(cb, layout_, VK_SHADER_STAGE_COMPUTE_BIT, 0, 4, &index);
124 fn<PFN_vkCmdDispatch>("vkCmdDispatch")(cb, 1, 1, 1);
125 VkMemoryBarrier condition{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
126 condition.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
127 condition.dstAccessMask = VK_ACCESS_CONDITIONAL_RENDERING_READ_BIT_EXT;
128 barrier(cb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_CONDITIONAL_RENDERING_BIT_EXT,
129 0, 1, &condition, 0, nullptr, 0, nullptr);
130 }
131
132private:
133 template<class T> T fn(const char* name) const { return reinterpret_cast<T>(get_(device_, name)); }
134 bool make_pipeline() {
135 if (pipeline_) return true;
136 // No entry-point assumptions for partial/mock devices: fail before allocating or recording anything.
137 for (const char* name : {"vkCreateBuffer", "vkDestroyBuffer", "vkGetBufferMemoryRequirements", "vkAllocateMemory",
138 "vkFreeMemory", "vkBindBufferMemory", "vkCreateDescriptorPool", "vkDestroyDescriptorPool",
139 "vkAllocateDescriptorSets", "vkUpdateDescriptorSets", "vkCreateShaderModule", "vkDestroyShaderModule",
140 "vkCreateDescriptorSetLayout", "vkDestroyDescriptorSetLayout", "vkCreatePipelineLayout",
141 "vkDestroyPipelineLayout", "vkCreateComputePipelines", "vkDestroyPipeline", "vkCmdCopyBuffer",
142 "vkCmdPipelineBarrier", "vkCmdBindPipeline", "vkCmdBindDescriptorSets", "vkCmdPushConstants", "vkCmdDispatch"})
143 if (!get_(device_, name)) return false;
144 VkDescriptorSetLayout set = VK_NULL_HANDLE;
145 VkPipelineLayout layout = VK_NULL_HANDLE;
146 VkShaderModule shader = VK_NULL_HANDLE;
147 VkPipeline pipeline = VK_NULL_HANDLE;
148 const VkDescriptorSetLayoutBinding binding{0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_COMPUTE_BIT, nullptr};
149 VkDescriptorSetLayoutCreateInfo set_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
150 set_info.bindingCount = 1; set_info.pBindings = &binding;
151 bool ok = fn<PFN_vkCreateDescriptorSetLayout>("vkCreateDescriptorSetLayout")(device_, &set_info, nullptr, &set) == VK_SUCCESS;
152 const VkPushConstantRange push{VK_SHADER_STAGE_COMPUTE_BIT, 0, 4};
153 VkPipelineLayoutCreateInfo layout_info{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO};
154 layout_info.setLayoutCount = 1; layout_info.pSetLayouts = &set;
155 layout_info.pushConstantRangeCount = 1; layout_info.pPushConstantRanges = &push;
156 ok = ok && fn<PFN_vkCreatePipelineLayout>("vkCreatePipelineLayout")(device_, &layout_info, nullptr, &layout) == VK_SUCCESS;
157 VkShaderModuleCreateInfo module{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO};
158 module.codeSize = sizeof(predicate_reduce_spirv); module.pCode = predicate_reduce_spirv;
159 ok = ok && fn<PFN_vkCreateShaderModule>("vkCreateShaderModule")(device_, &module, nullptr, &shader) == VK_SUCCESS;
160 VkComputePipelineCreateInfo info{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO};
161 info.stage = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO};
162 info.stage.stage = VK_SHADER_STAGE_COMPUTE_BIT; info.stage.module = shader; info.stage.pName = "main";
163 info.layout = layout;
164 ok = ok && fn<PFN_vkCreateComputePipelines>("vkCreateComputePipelines")(device_, VK_NULL_HANDLE, 1, &info, nullptr, &pipeline) == VK_SUCCESS;
165 if (shader) fn<PFN_vkDestroyShaderModule>("vkDestroyShaderModule")(device_, shader, nullptr);
166 if (!ok) {
167 if (pipeline) fn<PFN_vkDestroyPipeline>("vkDestroyPipeline")(device_, pipeline, nullptr);
168 if (layout) fn<PFN_vkDestroyPipelineLayout>("vkDestroyPipelineLayout")(device_, layout, nullptr);
169 if (set) fn<PFN_vkDestroyDescriptorSetLayout>("vkDestroyDescriptorSetLayout")(device_, set, nullptr);
170 return false;
171 }
172 set_layout_ = set; layout_ = layout; pipeline_ = pipeline;
173 return true;
174 }
175 PFN_vkGetDeviceProcAddr get_;
176 VkDevice device_;
177 VkPhysicalDeviceMemoryProperties memory_;
178 VkDeviceSize max_range_;
179 VkDescriptorSetLayout set_layout_ = VK_NULL_HANDLE;
180 VkPipelineLayout layout_ = VK_NULL_HANDLE;
181 VkPipeline pipeline_ = VK_NULL_HANDLE;
182};
183} // namespace leonida