diff options
| author | gabrielsteglich <63128571+gabrielsteglich@users.noreply.github.com> | 2026-11-22 01:13:30 -0300 |
|---|---|---|
| committer | gabrielsteglich <63128571+gabrielsteglich@users.noreply.github.com> | 2026-11-22 01:13:30 -0300 |
| commit | cce77b65ef0bab7bfcad431d0fc96c9c122b67c6 (patch) | |
| tree | bd74f63a7a6424fb43192da8b03ce104c514fb0e /runtime/src | |
| parent | 4a36bb19979b611f525d027f92e0fefad1626546 (diff) | |
Normalize binary occlusion results in Vulkan replay
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/binary_occlusion.comp | 13 | ||||
| -rw-r--r-- | runtime/src/binary_occlusion_spirv.h | 44 | ||||
| -rw-r--r-- | runtime/src/vulkan_backend.h | 186 | ||||
| -rw-r--r-- | runtime/src/vulkan_replay.h | 57 |
4 files changed, 276 insertions, 24 deletions
diff --git a/runtime/src/binary_occlusion.comp b/runtime/src/binary_occlusion.comp new file mode 100644 index 000000000..30f43ebc6 --- /dev/null +++ b/runtime/src/binary_occlusion.comp | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | #version 450 | ||
| 2 | |||
| 3 | layout(local_size_x = 64) in; | ||
| 4 | layout(set = 0, binding = 0, std430) buffer QueryResults { uvec2 results[]; }; | ||
| 5 | layout(push_constant) uniform ResolveRange { uint count; }; | ||
| 6 | |||
| 7 | void main() { | ||
| 8 | uint index = gl_GlobalInvocationID.x; | ||
| 9 | if (index < count) { | ||
| 10 | uvec2 raw = results[index]; | ||
| 11 | results[index] = uvec2(uint((raw.x | raw.y) != 0u), 0u); | ||
| 12 | } | ||
| 13 | } | ||
diff --git a/runtime/src/binary_occlusion_spirv.h b/runtime/src/binary_occlusion_spirv.h new file mode 100644 index 000000000..163bf5723 --- /dev/null +++ b/runtime/src/binary_occlusion_spirv.h | |||
| @@ -0,0 +1,44 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <cstdint> | ||
| 4 | |||
| 5 | namespace leonida { | ||
| 6 | |||
| 7 | // From binary_occlusion.comp. Regenerate with glslangValidator -V --target-env vulkan1.0 -Os -g0, | ||
| 8 | // then encode the resulting little-endian SPIR-V as uint32_t words. | ||
| 9 | // Embedded so the runtime can build with Vulkan headers alone. | ||
| 10 | inline constexpr std::uint32_t binary_occlusion_spirv[] = { | ||
| 11 | 0x07230203, 0x00010000, 0x0008000b, 0x00000035, 0x00000000, 0x00020011, 0x00000001, 0x0006000b, | ||
| 12 | 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, 0x00000000, 0x0003000e, 0x00000000, 0x00000001, | ||
| 13 | 0x0006000f, 0x00000005, 0x00000004, 0x6e69616d, 0x00000000, 0x0000000b, 0x00060010, 0x00000004, | ||
| 14 | 0x00000011, 0x00000040, 0x00000001, 0x00000001, 0x00040047, 0x0000000b, 0x0000000b, 0x0000001c, | ||
| 15 | 0x00030047, 0x00000011, 0x00000002, 0x00050048, 0x00000011, 0x00000000, 0x00000023, 0x00000000, | ||
| 16 | 0x00040047, 0x00000020, 0x00000006, 0x00000008, 0x00030047, 0x00000021, 0x00000003, 0x00050048, | ||
| 17 | 0x00000021, 0x00000000, 0x00000023, 0x00000000, 0x00040047, 0x00000023, 0x00000021, 0x00000000, | ||
| 18 | 0x00040047, 0x00000023, 0x00000022, 0x00000000, 0x00040047, 0x00000034, 0x0000000b, 0x00000019, | ||
| 19 | 0x00020013, 0x00000002, 0x00030021, 0x00000003, 0x00000002, 0x00040015, 0x00000006, 0x00000020, | ||
| 20 | 0x00000000, 0x00040017, 0x00000009, 0x00000006, 0x00000003, 0x00040020, 0x0000000a, 0x00000001, | ||
| 21 | 0x00000009, 0x0004003b, 0x0000000a, 0x0000000b, 0x00000001, 0x0004002b, 0x00000006, 0x0000000c, | ||
| 22 | 0x00000000, 0x00040020, 0x0000000d, 0x00000001, 0x00000006, 0x0003001e, 0x00000011, 0x00000006, | ||
| 23 | 0x00040020, 0x00000012, 0x00000009, 0x00000011, 0x0004003b, 0x00000012, 0x00000013, 0x00000009, | ||
| 24 | 0x00040015, 0x00000014, 0x00000020, 0x00000001, 0x0004002b, 0x00000014, 0x00000015, 0x00000000, | ||
| 25 | 0x00040020, 0x00000016, 0x00000009, 0x00000006, 0x00020014, 0x00000019, 0x00040017, 0x0000001d, | ||
| 26 | 0x00000006, 0x00000002, 0x0003001d, 0x00000020, 0x0000001d, 0x0003001e, 0x00000021, 0x00000020, | ||
| 27 | 0x00040020, 0x00000022, 0x00000002, 0x00000021, 0x0004003b, 0x00000022, 0x00000023, 0x00000002, | ||
| 28 | 0x00040020, 0x00000025, 0x00000002, 0x0000001d, 0x0004002b, 0x00000006, 0x0000002b, 0x00000001, | ||
| 29 | 0x0004002b, 0x00000006, 0x00000033, 0x00000040, 0x0006002c, 0x00000009, 0x00000034, 0x00000033, | ||
| 30 | 0x0000002b, 0x0000002b, 0x00050036, 0x00000002, 0x00000004, 0x00000000, 0x00000003, 0x000200f8, | ||
| 31 | 0x00000005, 0x00050041, 0x0000000d, 0x0000000e, 0x0000000b, 0x0000000c, 0x0004003d, 0x00000006, | ||
| 32 | 0x0000000f, 0x0000000e, 0x00050041, 0x00000016, 0x00000017, 0x00000013, 0x00000015, 0x0004003d, | ||
| 33 | 0x00000006, 0x00000018, 0x00000017, 0x000500b0, 0x00000019, 0x0000001a, 0x0000000f, 0x00000018, | ||
| 34 | 0x000300f7, 0x0000001c, 0x00000000, 0x000400fa, 0x0000001a, 0x0000001b, 0x0000001c, 0x000200f8, | ||
| 35 | 0x0000001b, 0x00060041, 0x00000025, 0x00000026, 0x00000023, 0x00000015, 0x0000000f, 0x0004003d, | ||
| 36 | 0x0000001d, 0x00000027, 0x00000026, 0x00050051, 0x00000006, 0x0000002a, 0x00000027, 0x00000000, | ||
| 37 | 0x00050051, 0x00000006, 0x0000002d, 0x00000027, 0x00000001, 0x000500c5, 0x00000006, 0x0000002e, | ||
| 38 | 0x0000002a, 0x0000002d, 0x000500ab, 0x00000019, 0x0000002f, 0x0000002e, 0x0000000c, 0x000600a9, | ||
| 39 | 0x00000006, 0x00000030, 0x0000002f, 0x0000002b, 0x0000000c, 0x00050050, 0x0000001d, 0x00000031, | ||
| 40 | 0x00000030, 0x0000000c, 0x0003003e, 0x00000026, 0x00000031, 0x000200f9, 0x0000001c, 0x000200f8, | ||
| 41 | 0x0000001c, 0x000100fd, 0x00010038, | ||
| 42 | }; | ||
| 43 | |||
| 44 | } // namespace leonida | ||
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index 5c7ee7f7b..5ab8d5d46 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h | |||
| @@ -8,12 +8,14 @@ | |||
| 8 | #ifndef VK_NO_PROTOTYPES | 8 | #ifndef VK_NO_PROTOTYPES |
| 9 | #define VK_NO_PROTOTYPES | 9 | #define VK_NO_PROTOTYPES |
| 10 | #endif | 10 | #endif |
| 11 | #include "binary_occlusion_spirv.h" | ||
| 11 | #include "vulkan_replay.h" | 12 | #include "vulkan_replay.h" |
| 12 | 13 | ||
| 13 | #include <cstdint> | 14 | #include <cstdint> |
| 14 | #include <cstdio> | 15 | #include <cstdio> |
| 15 | #include <functional> | 16 | #include <functional> |
| 16 | #include <map> | 17 | #include <map> |
| 18 | #include <memory> | ||
| 17 | #include <mutex> | 19 | #include <mutex> |
| 18 | #include <span> | 20 | #include <span> |
| 19 | #include <tuple> | 21 | #include <tuple> |
| @@ -36,14 +38,18 @@ struct VulkanDeviceFunctions { | |||
| 36 | X(vkCmdResolveImage) X(vkCmdFillBuffer) X(vkCmdUpdateBuffer) X(vkCreateSampler) X(vkDestroySampler) \ | 38 | X(vkCmdResolveImage) X(vkCmdFillBuffer) X(vkCmdUpdateBuffer) X(vkCreateSampler) X(vkDestroySampler) \ |
| 37 | X(vkCmdBeginConditionalRenderingEXT) X(vkCmdEndConditionalRenderingEXT) X(vkCreateQueryPool) \ | 39 | X(vkCmdBeginConditionalRenderingEXT) X(vkCmdEndConditionalRenderingEXT) X(vkCreateQueryPool) \ |
| 38 | X(vkDestroyQueryPool) X(vkCmdResetQueryPool) X(vkCmdBeginQuery) X(vkCmdEndQuery) X(vkCmdWriteTimestamp) \ | 40 | X(vkDestroyQueryPool) X(vkCmdResetQueryPool) X(vkCmdBeginQuery) X(vkCmdEndQuery) X(vkCmdWriteTimestamp) \ |
| 39 | X(vkCmdCopyQueryPoolResults) | 41 | X(vkCmdCopyQueryPoolResults) X(vkCreateDescriptorSetLayout) X(vkDestroyDescriptorSetLayout) \ |
| 42 | X(vkCreateDescriptorPool) X(vkDestroyDescriptorPool) X(vkAllocateDescriptorSets) X(vkUpdateDescriptorSets) \ | ||
| 43 | X(vkCreatePipelineLayout) X(vkDestroyPipelineLayout) X(vkCreateShaderModule) X(vkDestroyShaderModule) \ | ||
| 44 | X(vkCreateComputePipelines) X(vkDestroyPipeline) X(vkCmdBindPipeline) X(vkCmdBindDescriptorSets) \ | ||
| 45 | X(vkCmdPushConstants) X(vkCmdDispatch) | ||
| 40 | #define LEONIDA_VK_MEMBER(name) PFN_##name name = nullptr; | 46 | #define LEONIDA_VK_MEMBER(name) PFN_##name name = nullptr; |
| 41 | LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_MEMBER) | 47 | LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_MEMBER) |
| 42 | #undef LEONIDA_VK_MEMBER | 48 | #undef LEONIDA_VK_MEMBER |
| 43 | 49 | ||
| 44 | // The Vulkan 1.3 entry points may be null on older devices, the extension ones (conditional rendering) when the | 50 | // The Vulkan 1.3 entry points may be null on older devices, the extension ones (conditional rendering) when the |
| 45 | // extension is not enabled, and the query functions are optional (a list that uses queries without them is | 51 | // extension is not enabled, and the query functions are optional (a list that uses queries without them is |
| 46 | // unsupported); everything else is required. | 52 | // unsupported). The compute and descriptor functions are needed only for binary occlusion resolves. |
| 47 | bool load(PFN_vkGetDeviceProcAddr get, VkDevice device) { | 53 | bool load(PFN_vkGetDeviceProcAddr get, VkDevice device) { |
| 48 | #define LEONIDA_VK_LOAD(name) name = reinterpret_cast<PFN_##name>(get(device, #name)); | 54 | #define LEONIDA_VK_LOAD(name) name = reinterpret_cast<PFN_##name>(get(device, #name)); |
| 49 | LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_LOAD) | 55 | LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_LOAD) |
| @@ -61,6 +67,157 @@ struct VulkanDeviceFunctions { | |||
| 61 | } | 67 | } |
| 62 | }; | 68 | }; |
| 63 | 69 | ||
| 70 | // The scratch buffer belongs to one queue. Each chunk copies raw results into it, normalizes them in place, | ||
| 71 | // then transfers UINT64s to the application buffer, which needs no storage-buffer usage. | ||
| 72 | class BinaryOcclusionResolve { | ||
| 73 | public: | ||
| 74 | BinaryOcclusionResolve(const VulkanDeviceFunctions& vk, VkDevice device) : vk_(vk), device_(device) {} | ||
| 75 | BinaryOcclusionResolve(const BinaryOcclusionResolve&) = delete; | ||
| 76 | BinaryOcclusionResolve& operator=(const BinaryOcclusionResolve&) = delete; | ||
| 77 | |||
| 78 | ~BinaryOcclusionResolve() { | ||
| 79 | if (pipeline_) vk_.vkDestroyPipeline(device_, pipeline_, nullptr); | ||
| 80 | if (layout_) vk_.vkDestroyPipelineLayout(device_, layout_, nullptr); | ||
| 81 | if (pool_) vk_.vkDestroyDescriptorPool(device_, pool_, nullptr); | ||
| 82 | if (set_layout_) vk_.vkDestroyDescriptorSetLayout(device_, set_layout_, nullptr); | ||
| 83 | if (scratch_) vk_.vkDestroyBuffer(device_, scratch_, nullptr); | ||
| 84 | if (memory_) vk_.vkFreeMemory(device_, memory_, nullptr); | ||
| 85 | } | ||
| 86 | |||
| 87 | bool initialize(const VkPhysicalDeviceMemoryProperties& memory) { | ||
| 88 | if (!vk_.vkCreateDescriptorSetLayout || !vk_.vkDestroyDescriptorSetLayout || | ||
| 89 | !vk_.vkCreateDescriptorPool || !vk_.vkDestroyDescriptorPool || !vk_.vkAllocateDescriptorSets || | ||
| 90 | !vk_.vkUpdateDescriptorSets || !vk_.vkCreatePipelineLayout || !vk_.vkDestroyPipelineLayout || | ||
| 91 | !vk_.vkCreateShaderModule || !vk_.vkDestroyShaderModule || !vk_.vkCreateComputePipelines || | ||
| 92 | !vk_.vkDestroyPipeline || !vk_.vkCmdBindPipeline || !vk_.vkCmdBindDescriptorSets || | ||
| 93 | !vk_.vkCmdPushConstants || !vk_.vkCmdDispatch) | ||
| 94 | return false; | ||
| 95 | |||
| 96 | VkBufferCreateInfo buffer{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; | ||
| 97 | buffer.size = scratch_size; | ||
| 98 | buffer.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | | ||
| 99 | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; | ||
| 100 | if (vk_.vkCreateBuffer(device_, &buffer, nullptr, &scratch_) != VK_SUCCESS) return false; | ||
| 101 | VkMemoryRequirements requirements{}; | ||
| 102 | vk_.vkGetBufferMemoryRequirements(device_, scratch_, &requirements); | ||
| 103 | auto type = memory.memoryTypeCount; | ||
| 104 | for (std::uint32_t i = 0; i < memory.memoryTypeCount; ++i) { | ||
| 105 | if (!(requirements.memoryTypeBits & (1u << i))) continue; | ||
| 106 | if (type == memory.memoryTypeCount) type = i; | ||
| 107 | if (memory.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) { | ||
| 108 | type = i; | ||
| 109 | break; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | if (type == memory.memoryTypeCount) return false; | ||
| 113 | VkMemoryAllocateInfo allocation{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO}; | ||
| 114 | allocation.allocationSize = requirements.size; | ||
| 115 | allocation.memoryTypeIndex = type; | ||
| 116 | if (vk_.vkAllocateMemory(device_, &allocation, nullptr, &memory_) != VK_SUCCESS || | ||
| 117 | vk_.vkBindBufferMemory(device_, scratch_, memory_, 0) != VK_SUCCESS) | ||
| 118 | return false; | ||
| 119 | |||
| 120 | const VkDescriptorSetLayoutBinding binding{0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, | ||
| 121 | VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; | ||
| 122 | VkDescriptorSetLayoutCreateInfo set_layout{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO}; | ||
| 123 | set_layout.bindingCount = 1; | ||
| 124 | set_layout.pBindings = &binding; | ||
| 125 | if (vk_.vkCreateDescriptorSetLayout(device_, &set_layout, nullptr, &set_layout_) != VK_SUCCESS) return false; | ||
| 126 | const VkDescriptorPoolSize size{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1}; | ||
| 127 | VkDescriptorPoolCreateInfo pool{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO}; | ||
| 128 | pool.maxSets = 1; | ||
| 129 | pool.poolSizeCount = 1; | ||
| 130 | pool.pPoolSizes = &size; | ||
| 131 | if (vk_.vkCreateDescriptorPool(device_, &pool, nullptr, &pool_) != VK_SUCCESS) return false; | ||
| 132 | VkDescriptorSetAllocateInfo set{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO}; | ||
| 133 | set.descriptorPool = pool_; | ||
| 134 | set.descriptorSetCount = 1; | ||
| 135 | set.pSetLayouts = &set_layout_; | ||
| 136 | if (vk_.vkAllocateDescriptorSets(device_, &set, &set_) != VK_SUCCESS) return false; | ||
| 137 | const VkDescriptorBufferInfo storage{scratch_, 0, scratch_size}; | ||
| 138 | VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}; | ||
| 139 | write.dstSet = set_; | ||
| 140 | write.descriptorCount = 1; | ||
| 141 | write.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; | ||
| 142 | write.pBufferInfo = &storage; | ||
| 143 | vk_.vkUpdateDescriptorSets(device_, 1, &write, 0, nullptr); | ||
| 144 | |||
| 145 | const VkPushConstantRange count{VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(std::uint32_t)}; | ||
| 146 | VkPipelineLayoutCreateInfo layout{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO}; | ||
| 147 | layout.setLayoutCount = 1; | ||
| 148 | layout.pSetLayouts = &set_layout_; | ||
| 149 | layout.pushConstantRangeCount = 1; | ||
| 150 | layout.pPushConstantRanges = &count; | ||
| 151 | if (vk_.vkCreatePipelineLayout(device_, &layout, nullptr, &layout_) != VK_SUCCESS) return false; | ||
| 152 | VkShaderModuleCreateInfo module{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO}; | ||
| 153 | module.codeSize = sizeof(binary_occlusion_spirv); | ||
| 154 | module.pCode = binary_occlusion_spirv; | ||
| 155 | VkShaderModule shader = VK_NULL_HANDLE; | ||
| 156 | if (vk_.vkCreateShaderModule(device_, &module, nullptr, &shader) != VK_SUCCESS) return false; | ||
| 157 | VkComputePipelineCreateInfo pipeline{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO}; | ||
| 158 | pipeline.stage = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, nullptr, 0, | ||
| 159 | VK_SHADER_STAGE_COMPUTE_BIT, shader, "main", nullptr}; | ||
| 160 | pipeline.layout = layout_; | ||
| 161 | const VkResult result = vk_.vkCreateComputePipelines(device_, VK_NULL_HANDLE, 1, &pipeline, nullptr, &pipeline_); | ||
| 162 | vk_.vkDestroyShaderModule(device_, shader, nullptr); | ||
| 163 | return result == VK_SUCCESS; | ||
| 164 | } | ||
| 165 | |||
| 166 | void record(VkCommandBuffer cb, VkQueryPool queries, std::uint32_t first, std::uint32_t count, | ||
| 167 | VkBuffer dst, VkDeviceSize offset) const { | ||
| 168 | constexpr auto transfer = VK_PIPELINE_STAGE_TRANSFER_BIT; | ||
| 169 | constexpr auto compute = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; | ||
| 170 | vk_.vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline_); | ||
| 171 | vk_.vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE, layout_, 0, 1, &set_, 0, nullptr); | ||
| 172 | while (count) { | ||
| 173 | const std::uint32_t chunk = std::min(count, scratch_count); | ||
| 174 | const VkDeviceSize bytes = VkDeviceSize(chunk) * result_size; | ||
| 175 | // Reuse is ordered even across submissions on this queue. Each previous copy has read the scratch | ||
| 176 | // words before the query pool can write them again. | ||
| 177 | scratch_barrier(cb, transfer, transfer, VK_ACCESS_TRANSFER_READ_BIT, VK_ACCESS_TRANSFER_WRITE_BIT); | ||
| 178 | vk_.vkCmdCopyQueryPoolResults(cb, queries, first, chunk, scratch_, 0, result_size, | ||
| 179 | VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); | ||
| 180 | scratch_barrier(cb, transfer, compute, VK_ACCESS_TRANSFER_WRITE_BIT, | ||
| 181 | VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT); | ||
| 182 | vk_.vkCmdPushConstants(cb, layout_, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(chunk), &chunk); | ||
| 183 | vk_.vkCmdDispatch(cb, (chunk + group_size - 1) / group_size, 1, 1); | ||
| 184 | scratch_barrier(cb, compute, transfer, VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT); | ||
| 185 | const VkBufferCopy copy{0, offset, bytes}; | ||
| 186 | vk_.vkCmdCopyBuffer(cb, scratch_, dst, 1, ©); | ||
| 187 | first += chunk; | ||
| 188 | count -= chunk; | ||
| 189 | offset += bytes; | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | private: | ||
| 194 | void scratch_barrier(VkCommandBuffer cb, VkPipelineStageFlags src, VkPipelineStageFlags dst, | ||
| 195 | VkAccessFlags src_access, VkAccessFlags dst_access) const { | ||
| 196 | VkBufferMemoryBarrier barrier{VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER}; | ||
| 197 | barrier.srcAccessMask = src_access; | ||
| 198 | barrier.dstAccessMask = dst_access; | ||
| 199 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; | ||
| 200 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; | ||
| 201 | barrier.buffer = scratch_; | ||
| 202 | barrier.size = scratch_size; | ||
| 203 | vk_.vkCmdPipelineBarrier(cb, src, dst, 0, 0, nullptr, 1, &barrier, 0, nullptr); | ||
| 204 | } | ||
| 205 | |||
| 206 | static constexpr std::uint32_t scratch_count = 1024; | ||
| 207 | static constexpr std::uint32_t group_size = 64; // local_size_x in binary_occlusion.comp | ||
| 208 | static constexpr VkDeviceSize result_size = sizeof(std::uint64_t); | ||
| 209 | static constexpr VkDeviceSize scratch_size = scratch_count * result_size; | ||
| 210 | const VulkanDeviceFunctions& vk_; | ||
| 211 | VkDevice device_; | ||
| 212 | VkBuffer scratch_ = VK_NULL_HANDLE; | ||
| 213 | VkDeviceMemory memory_ = VK_NULL_HANDLE; | ||
| 214 | VkDescriptorSetLayout set_layout_ = VK_NULL_HANDLE; | ||
| 215 | VkDescriptorPool pool_ = VK_NULL_HANDLE; | ||
| 216 | VkDescriptorSet set_ = VK_NULL_HANDLE; | ||
| 217 | VkPipelineLayout layout_ = VK_NULL_HANDLE; | ||
| 218 | VkPipeline pipeline_ = VK_NULL_HANDLE; | ||
| 219 | }; | ||
| 220 | |||
| 64 | // Per device: what the queues share. Resources are registered by their stable ID (the COM resource pointer). | 221 | // Per device: what the queues share. Resources are registered by their stable ID (the COM resource pointer). |
| 65 | class VulkanContext { | 222 | class VulkanContext { |
| 66 | public: | 223 | public: |
| @@ -167,6 +324,11 @@ public: | |||
| 167 | double timestamp_period() const { return timestamp_period_; } | 324 | double timestamp_period() const { return timestamp_period_; } |
| 168 | void set_timestamp_period(double period) { timestamp_period_ = period; } | 325 | void set_timestamp_period(double period) { timestamp_period_ = period; } |
| 169 | 326 | ||
| 327 | // Configure before queue creation, when the selected queue family supports compute. | ||
| 328 | void set_binary_occlusion_memory(const VkPhysicalDeviceMemoryProperties& memory) { | ||
| 329 | binary_occlusion_memory_ = memory; | ||
| 330 | } | ||
| 331 | |||
| 170 | // For tests and traces. | 332 | // For tests and traces. |
| 171 | VkImageLayout layout(ResourceId id, std::uint32_t subresource) { | 333 | VkImageLayout layout(ResourceId id, std::uint32_t subresource) { |
| 172 | std::lock_guard lock(mutex_); | 334 | std::lock_guard lock(mutex_); |
| @@ -234,6 +396,7 @@ private: | |||
| 234 | std::function<void(bool)> bindless_commit_; | 396 | std::function<void(bool)> bindless_commit_; |
| 235 | std::unordered_map<QueryHeapId, QueryPool> query_pools_; // element pointers stay valid across inserts | 397 | std::unordered_map<QueryHeapId, QueryPool> query_pools_; // element pointers stay valid across inserts |
| 236 | double timestamp_period_ = 0; | 398 | double timestamp_period_ = 0; |
| 399 | VkPhysicalDeviceMemoryProperties binary_occlusion_memory_{}; | ||
| 237 | ReplayFunctions replay_; | 400 | ReplayFunctions replay_; |
| 238 | }; | 401 | }; |
| 239 | 402 | ||
| @@ -322,10 +485,24 @@ public: | |||
| 322 | ReplayResult execute(std::span<const CommandList* const> lists) { | 485 | ReplayResult execute(std::span<const CommandList* const> lists) { |
| 323 | std::lock_guard lock(context_.mutex_); | 486 | std::lock_guard lock(context_.mutex_); |
| 324 | std::vector<PreparedList> prepared(lists.size()); | 487 | std::vector<PreparedList> prepared(lists.size()); |
| 488 | ReplayFunctions replay = context_.replay_; | ||
| 489 | if (context_.binary_occlusion_memory_.memoryTypeCount) { | ||
| 490 | replay.resolve_binary_occlusion = [this](VkCommandBuffer cb, VkQueryPool pool, std::uint32_t first, | ||
| 491 | std::uint32_t count, VkBuffer dst, VkDeviceSize offset) { | ||
| 492 | binary_occlusion_->record(cb, pool, first, count, dst, offset); | ||
| 493 | }; | ||
| 494 | } | ||
| 325 | const auto resolve = [this](ResourceId id) { return context_.resolve(id); }; | 495 | const auto resolve = [this](ResourceId id) { return context_.resolve(id); }; |
| 496 | bool binary_occlusion_needed = false; | ||
| 326 | for (std::size_t i = 0; i < lists.size(); ++i) { | 497 | for (std::size_t i = 0; i < lists.size(); ++i) { |
| 327 | const auto result = prepare(*lists[i], resolve, context_.replay_, prepared[i]); | 498 | const auto result = prepare(*lists[i], resolve, replay, prepared[i]); |
| 328 | if (result != ReplayResult::success) return result; | 499 | if (result != ReplayResult::success) return result; |
| 500 | binary_occlusion_needed |= prepared[i].binary_occlusion; | ||
| 501 | } | ||
| 502 | if (binary_occlusion_needed && !binary_occlusion_) { | ||
| 503 | auto helper = std::make_unique<BinaryOcclusionResolve>(context_.vk, context_.device()); | ||
| 504 | if (!helper->initialize(context_.binary_occlusion_memory_)) return ReplayResult::unsupported; | ||
| 505 | binary_occlusion_ = std::move(helper); | ||
| 329 | } | 506 | } |
| 330 | VkCommandBuffer command_buffer = acquire(); | 507 | VkCommandBuffer command_buffer = acquire(); |
| 331 | if (!command_buffer) return ReplayResult::unsupported; | 508 | if (!command_buffer) return ReplayResult::unsupported; |
| @@ -339,7 +516,7 @@ public: | |||
| 339 | if (context_.bindless_commit_) context_.bindless_commit_(submitted); | 516 | if (context_.bindless_commit_) context_.bindless_commit_(submitted); |
| 340 | return ReplayResult::unsupported; | 517 | return ReplayResult::unsupported; |
| 341 | } | 518 | } |
| 342 | for (const auto& list : prepared) emit(list, command_buffer, context_.replay_, &context_.tracker_); | 519 | for (const auto& list : prepared) emit(list, command_buffer, replay, &context_.tracker_); |
| 343 | // A fence wait on the CPU makes GPU writes visible to the host only after a barrier to the host domain: | 520 | // A fence wait on the CPU makes GPU writes visible to the host only after a barrier to the host domain: |
| 344 | // readback buffers are read through Map once the application has waited. | 521 | // readback buffers are read through Map once the application has waited. |
| 345 | VkMemoryBarrier host{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; | 522 | VkMemoryBarrier host{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; |
| @@ -431,6 +608,7 @@ private: | |||
| 431 | std::vector<std::pair<VkCommandBuffer, std::uint64_t>> buffers_; // buffer, ticket that frees it | 608 | std::vector<std::pair<VkCommandBuffer, std::uint64_t>> buffers_; // buffer, ticket that frees it |
| 432 | std::vector<VkSemaphore> waits_; | 609 | std::vector<VkSemaphore> waits_; |
| 433 | std::vector<std::uint64_t> wait_values_; | 610 | std::vector<std::uint64_t> wait_values_; |
| 611 | std::unique_ptr<BinaryOcclusionResolve> binary_occlusion_; | ||
| 434 | bool ok_ = false; | 612 | bool ok_ = false; |
| 435 | }; | 613 | }; |
| 436 | 614 | ||
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 8fdd790cb..f8c7cd7be 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h | |||
| @@ -22,7 +22,8 @@ | |||
| 22 | // - enhanced barriers need pipeline_barrier2 (synchronization2, Vulkan 1.3); | 22 | // - enhanced barriers need pipeline_barrier2 (synchronization2, Vulkan 1.3); |
| 23 | // - unordered-access clears need fill_buffer, update_buffer and copy_buffer for buffers; texture views cleared with | 23 | // - unordered-access clears need fill_buffer, update_buffer and copy_buffer for buffers; texture views cleared with |
| 24 | // rectangles need color_attachment usage on the image (ResourceBinding::color_attachment); | 24 | // rectangles need color_attachment usage on the image (ResourceBinding::color_attachment); |
| 25 | // - query commands need the query functions, fill_buffer and query_pool; resolve destinations have TRANSFER_DST usage. | 25 | // - query commands need the query functions, fill_buffer and query_pool; resolve destinations have TRANSFER_DST |
| 26 | // usage and binary occlusion resolves need resolve_binary_occlusion. | ||
| 26 | // A list that needs a missing function fails with ReplayResult::unsupported before anything is written. | 27 | // A list that needs a missing function fails with ReplayResult::unsupported before anything is written. |
| 27 | 28 | ||
| 28 | #include "command_list.h" | 29 | #include "command_list.h" |
| @@ -611,6 +612,9 @@ struct ReplayFunctions { | |||
| 611 | PFN_vkCmdEndQuery end_query = nullptr; | 612 | PFN_vkCmdEndQuery end_query = nullptr; |
| 612 | PFN_vkCmdWriteTimestamp write_timestamp = nullptr; | 613 | PFN_vkCmdWriteTimestamp write_timestamp = nullptr; |
| 613 | PFN_vkCmdCopyQueryPoolResults copy_query_pool_results = nullptr; | 614 | PFN_vkCmdCopyQueryPoolResults copy_query_pool_results = nullptr; |
| 615 | // Resolves available binary-occlusion queries to 64-bit 0/1 values, outside conditional rendering. | ||
| 616 | std::function<void(VkCommandBuffer, VkQueryPool, std::uint32_t first, std::uint32_t count, | ||
| 617 | VkBuffer dst, VkDeviceSize offset)> resolve_binary_occlusion; | ||
| 614 | // Returns a 2D-array view of the range (caller-owned, alive until execution completes); rectangle clears. | 618 | // Returns a 2D-array view of the range (caller-owned, alive until execution completes); rectangle clears. |
| 615 | std::function<VkImageView(const ResourceBinding&, const VkImageSubresourceRange&)> image_view; | 619 | std::function<VkImageView(const ResourceBinding&, const VkImageSubresourceRange&)> image_view; |
| 616 | // Returns the query heap with that ID, or nullptr; it stays valid while the lists that use it run. | 620 | // Returns the query heap with that ID, or nullptr; it stays valid while the lists that use it run. |
| @@ -1189,6 +1193,7 @@ class PreparedList { | |||
| 1189 | public: | 1193 | public: |
| 1190 | const CommandList* list = nullptr; | 1194 | const CommandList* list = nullptr; |
| 1191 | std::vector<detail::Prepared> commands; | 1195 | std::vector<detail::Prepared> commands; |
| 1196 | bool binary_occlusion = false; // the queue needs its internal resolve resources | ||
| 1192 | // Copies, resolves and UAV clears that run as transfers, recorded while a predicate was set. D3D12 skips them | 1197 | // Copies, resolves and UAV clears that run as transfers, recorded while a predicate was set. D3D12 skips them |
| 1193 | // when the predicate says so, but Vulkan's conditional rendering does not cover transfer commands, so they run | 1198 | // when the predicate says so, but Vulkan's conditional rendering does not cover transfer commands, so they run |
| 1194 | // unconditionally (docs/d3d12_vulkan.md section 4.7). Counted so the caller can report it. | 1199 | // unconditionally (docs/d3d12_vulkan.md section 4.7). Counted so the caller can report it. |
| @@ -1204,6 +1209,7 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun | |||
| 1204 | const auto commands = list.commands(); | 1209 | const auto commands = list.commands(); |
| 1205 | out.list = &list; | 1210 | out.list = &list; |
| 1206 | out.commands.assign(commands.size(), {}); | 1211 | out.commands.assign(commands.size(), {}); |
| 1212 | out.binary_occlusion = false; | ||
| 1207 | const bool rendering = vk.begin_rendering && vk.end_rendering && vk.clear_attachments && vk.image_view; | 1213 | const bool rendering = vk.begin_rendering && vk.end_rendering && vk.clear_attachments && vk.image_view; |
| 1208 | const bool copies = vk.copy_buffer && vk.copy_buffer_to_image && vk.copy_image_to_buffer && vk.copy_image; | 1214 | const bool copies = vk.copy_buffer && vk.copy_buffer_to_image && vk.copy_image_to_buffer && vk.copy_image; |
| 1209 | const bool conditional = vk.begin_conditional_rendering && vk.end_conditional_rendering; | 1215 | const bool conditional = vk.begin_conditional_rendering && vk.end_conditional_rendering; |
| @@ -1425,10 +1431,17 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun | |||
| 1425 | if (const auto* resolve_queries = std::get_if<ResolveQueries>(&command)) { | 1431 | if (const auto* resolve_queries = std::get_if<ResolveQueries>(&command)) { |
| 1426 | // The destination is a buffer, as D3D12 requires, large enough for every result. | 1432 | // The destination is a buffer, as D3D12 requires, large enough for every result. |
| 1427 | if (!bind(resolve_queries->dst)) return ReplayResult::unresolved_resource; | 1433 | if (!bind(resolve_queries->dst)) return ReplayResult::unresolved_resource; |
| 1428 | if (!p.binding.buffer || | 1434 | const std::uint64_t bytes = std::uint64_t(range.count) * query_result_size(range.type); |
| 1429 | !detail::within(resolve_queries->offset, std::uint64_t(range.count) * query_result_size(range.type), | 1435 | if (!p.binding.buffer || p.binding.buffer_offset % 8 || |
| 1430 | p.binding.size)) | 1436 | !detail::within(resolve_queries->offset, bytes, p.binding.size) || |
| 1437 | !detail::within(p.binding.buffer_offset, resolve_queries->offset + bytes, | ||
| 1438 | std::numeric_limits<VkDeviceSize>::max())) | ||
| 1431 | return ReplayResult::invalid_query; | 1439 | return ReplayResult::invalid_query; |
| 1440 | if (range.type == d3d12_query::binary_occlusion) { | ||
| 1441 | // The internal dispatch would otherwise count towards the application's CSInvocations. | ||
| 1442 | if (!vk.resolve_binary_occlusion || statistics_active) return ReplayResult::unsupported; | ||
| 1443 | out.binary_occlusion = true; | ||
| 1444 | } | ||
| 1432 | } | 1445 | } |
| 1433 | } | 1446 | } |
| 1434 | } | 1447 | } |
| @@ -1441,24 +1454,22 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 1441 | const auto commands = prepared.list->commands(); | 1454 | const auto commands = prepared.list->commands(); |
| 1442 | const bool separate = vk.separate_depth_stencil_layouts; | 1455 | const bool separate = vk.separate_depth_stencil_layouts; |
| 1443 | const VulkanAccess transfer_dst = vulkan_access(d3d12_state::copy_dest, false); | 1456 | const VulkanAccess transfer_dst = vulkan_access(d3d12_state::copy_dest, false); |
| 1444 | bool conditional_rendering = false; // inside vkCmdBeginConditionalRenderingEXT | 1457 | VkConditionalRenderingBeginInfoEXT predicate{VK_STRUCTURE_TYPE_CONDITIONAL_RENDERING_BEGIN_INFO_EXT}; |
| 1445 | 1458 | ||
| 1446 | for (std::size_t i = 0; i < commands.size(); ++i) { | 1459 | for (std::size_t i = 0; i < commands.size(); ++i) { |
| 1447 | const auto& command = commands[i]; | 1460 | const auto& command = commands[i]; |
| 1448 | const auto& p = prepared.commands[i]; | 1461 | const auto& p = prepared.commands[i]; |
| 1449 | if (const auto* predication = std::get_if<Predication>(&command)) { | 1462 | if (const auto* predication = std::get_if<Predication>(&command)) { |
| 1450 | // Conditional rendering cannot nest, so a new predicate ends the previous one first. | 1463 | // Conditional rendering cannot nest, so a new predicate ends the previous one first. |
| 1451 | if (conditional_rendering) vk.end_conditional_rendering(command_buffer); | 1464 | if (predicate.buffer) vk.end_conditional_rendering(command_buffer); |
| 1452 | conditional_rendering = predication->buffer != 0; | 1465 | predicate.buffer = p.binding.buffer; |
| 1453 | if (conditional_rendering) { | 1466 | if (predicate.buffer) { |
| 1454 | VkConditionalRenderingBeginInfoEXT begin{VK_STRUCTURE_TYPE_CONDITIONAL_RENDERING_BEGIN_INFO_EXT}; | 1467 | predicate.offset = predication->offset; |
| 1455 | begin.buffer = p.binding.buffer; | ||
| 1456 | begin.offset = predication->offset; | ||
| 1457 | // Vulkan discards the commands when the word is 0, which is EQUAL_ZERO; INVERTED gives NOT_EQUAL_ZERO. | 1468 | // Vulkan discards the commands when the word is 0, which is EQUAL_ZERO; INVERTED gives NOT_EQUAL_ZERO. |
| 1458 | begin.flags = predication->op == d3d12_predication::not_equal_zero | 1469 | predicate.flags = predication->op == d3d12_predication::not_equal_zero |
| 1459 | ? VK_CONDITIONAL_RENDERING_INVERTED_BIT_EXT | 1470 | ? VK_CONDITIONAL_RENDERING_INVERTED_BIT_EXT |
| 1460 | : 0; | 1471 | : 0; |
| 1461 | vk.begin_conditional_rendering(command_buffer, &begin); | 1472 | vk.begin_conditional_rendering(command_buffer, &predicate); |
| 1462 | } | 1473 | } |
| 1463 | continue; | 1474 | continue; |
| 1464 | } | 1475 | } |
| @@ -1769,28 +1780,34 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 1769 | } | 1780 | } |
| 1770 | heap.ended[end->index] = true; | 1781 | heap.ended[end->index] = true; |
| 1771 | } else if (const auto* resolve = std::get_if<ResolveQueries>(&command)) { | 1782 | } else if (const auto* resolve = std::get_if<ResolveQueries>(&command)) { |
| 1772 | // Runs of ended queries are copied as UINT64s, waiting for their results; D3D12 leaves a query that | 1783 | // D3D12 leaves a query that was never ended undefined. Write zeros instead of waiting forever. |
| 1773 | // was never ended undefined, and it is written as zeros here rather than waited on forever. | ||
| 1774 | const QueryPool& heap = *p.queries; | 1784 | const QueryPool& heap = *p.queries; |
| 1775 | const std::uint32_t size = query_result_size(resolve->type); | 1785 | const std::uint32_t size = query_result_size(resolve->type); |
| 1786 | const bool binary = resolve->type == d3d12_query::binary_occlusion; | ||
| 1787 | // ResolveQueryData is unconditional in D3D12, including our internal dispatch. | ||
| 1788 | if (binary && predicate.buffer) vk.end_conditional_rendering(command_buffer); | ||
| 1776 | std::uint32_t index = resolve->first; | 1789 | std::uint32_t index = resolve->first; |
| 1777 | const std::uint32_t last = resolve->first + resolve->count; | 1790 | const std::uint32_t last = resolve->first + resolve->count; |
| 1778 | while (index < last) { | 1791 | while (index < last) { |
| 1779 | const bool ended = heap.ended[index]; | 1792 | const bool ended = heap.ended[index]; |
| 1780 | std::uint32_t run = 1; | 1793 | std::uint32_t run = 1; |
| 1781 | while (index + run < last && heap.ended[index + run] == ended) ++run; | 1794 | while (index + run < last && heap.ended[index + run] == ended) ++run; |
| 1782 | const VkDeviceSize offset = resolve->offset + VkDeviceSize(index - resolve->first) * size; | 1795 | const VkDeviceSize offset = p.binding.buffer_offset + resolve->offset + |
| 1783 | if (ended) | 1796 | VkDeviceSize(index - resolve->first) * size; |
| 1797 | if (!ended) | ||
| 1798 | vk.fill_buffer(command_buffer, p.binding.buffer, offset, VkDeviceSize(run) * size, 0); | ||
| 1799 | else if (binary) | ||
| 1800 | vk.resolve_binary_occlusion(command_buffer, heap.pool, index, run, p.binding.buffer, offset); | ||
| 1801 | else | ||
| 1784 | vk.copy_query_pool_results(command_buffer, heap.pool, index, run, p.binding.buffer, offset, size, | 1802 | vk.copy_query_pool_results(command_buffer, heap.pool, index, run, p.binding.buffer, offset, size, |
| 1785 | VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); | 1803 | VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); |
| 1786 | else | ||
| 1787 | vk.fill_buffer(command_buffer, p.binding.buffer, offset, VkDeviceSize(run) * size, 0); | ||
| 1788 | index += run; | 1804 | index += run; |
| 1789 | } | 1805 | } |
| 1806 | if (binary && predicate.buffer) vk.begin_conditional_rendering(command_buffer, &predicate); | ||
| 1790 | } | 1807 | } |
| 1791 | } | 1808 | } |
| 1792 | // Predication does not carry over to the next command list. | 1809 | // Predication does not carry over to the next command list. |
| 1793 | if (conditional_rendering) vk.end_conditional_rendering(command_buffer); | 1810 | if (predicate.buffer) vk.end_conditional_rendering(command_buffer); |
| 1794 | } | 1811 | } |
| 1795 | 1812 | ||
| 1796 | // prepare + emit for one list. A failed replay leaves the command buffer and the tracker untouched. | 1813 | // prepare + emit for one list. A failed replay leaves the command buffer and the tracker untouched. |