diff options
| author | AlxcBTW <187763517+AlxcBTW@users.noreply.github.com> | 2026-11-22 08:11:31 +0100 |
|---|---|---|
| committer | AlxcBTW <187763517+AlxcBTW@users.noreply.github.com> | 2026-11-22 08:11:31 +0100 |
| commit | 2b7621997a0cfadc345ebee2b9a76771c5f6a534 (patch) | |
| tree | 9dfbc9c6a35eb77287f7140bb4e0c5650ca134de /runtime/src | |
| parent | 9b2042ab41b2a479909da8efd002db805bf189b0 (diff) | |
| parent | c63773d51235d72250d48699f29830ac9ddafd60 (diff) | |
Merge remote-tracking branch 'origin/main' into oob-select-v2
# Conflicts:
# tests/semantic/regen_fixtures.py
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/uav_clear.h | 20 | ||||
| -rw-r--r-- | runtime/src/vulkan_backend.h | 188 | ||||
| -rw-r--r-- | runtime/src/vulkan_replay.h | 53 |
5 files changed, 285 insertions, 33 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/uav_clear.h b/runtime/src/uav_clear.h index 6b5c53fc3..73ec54dea 100644 --- a/runtime/src/uav_clear.h +++ b/runtime/src/uav_clear.h | |||
| @@ -10,7 +10,8 @@ | |||
| 10 | // whatever the format's type: 0x3f800000 into an R32_FLOAT channel is 1.0, 0xff into an R8_UNORM one is 1.0. | 10 | // whatever the format's type: 0x3f800000 into an R32_FLOAT channel is 1.0, 0xff into an R8_UNORM one is 1.0. |
| 11 | // A raw or structured buffer view takes value 0 for every 32-bit word. | 11 | // A raw or structured buffer view takes value 0 for every 32-bit word. |
| 12 | // - Float converts to the format: UNORM and SNORM clamp to [0, 1] / [-1, 1] and round to nearest (NaN is 0); | 12 | // - Float converts to the format: UNORM and SNORM clamp to [0, 1] / [-1, 1] and round to nearest (NaN is 0); |
| 13 | // 16-bit and the packed 11/10-bit floats round to nearest even (the small floats have no sign: negative is 0). | 13 | // 16-bit and the packed 11/10-bit floats round toward zero, a finite value too large becoming the largest finite |
| 14 | // (D3D11.3 3.2.2; WARP and NVIDIA agree), and the small floats have no sign: negative is 0. | ||
| 14 | // D3D11 calls Float on an integer view invalid and D3D12 does not say; here the value is converted to the | 15 | // D3D11 calls Float on an integer view invalid and D3D12 does not say; here the value is converted to the |
| 15 | // integer (rounded toward zero, clamped, NaN is 0) so the result is defined. A raw or structured buffer view | 16 | // integer (rounded toward zero, clamped, NaN is 0) so the result is defined. A raw or structured buffer view |
| 16 | // takes value 0's bits. | 17 | // takes value 0's bits. |
| @@ -123,7 +124,7 @@ inline std::uint32_t get(const Element& e, Channel c) { | |||
| 123 | } | 124 | } |
| 124 | 125 | ||
| 125 | // A float32 as a float with `mantissa` fraction bits, `exponent` exponent bits and an optional sign bit (IEEE | 126 | // A float32 as a float with `mantissa` fraction bits, `exponent` exponent bits and an optional sign bit (IEEE |
| 126 | // half: 10, 5, signed; the packed formats: 6 or 5, 5, unsigned), rounded to nearest even. Too large is infinity. | 127 | // half: 10, 5, signed; the packed formats: 6 or 5, 5, unsigned), rounded toward zero. Too large is the largest finite; only infinity stays infinity. |
| 127 | inline std::uint32_t small_float(float value, unsigned mantissa, unsigned exponent, bool sign) { | 128 | inline std::uint32_t small_float(float value, unsigned mantissa, unsigned exponent, bool sign) { |
| 128 | const std::uint32_t f = std::bit_cast<std::uint32_t>(value); | 129 | const std::uint32_t f = std::bit_cast<std::uint32_t>(value); |
| 129 | const std::uint32_t negative = f >> 31; | 130 | const std::uint32_t negative = f >> 31; |
| @@ -132,19 +133,18 @@ inline std::uint32_t small_float(float value, unsigned mantissa, unsigned expone | |||
| 132 | if (std::isnan(value)) return sign_bit | (max_exponent << mantissa) | (1u << (mantissa - 1)); | 133 | if (std::isnan(value)) return sign_bit | (max_exponent << mantissa) | (1u << (mantissa - 1)); |
| 133 | if (!sign && negative) return 0; | 134 | if (!sign && negative) return 0; |
| 134 | const std::uint32_t magnitude = f & 0x7fffffffu; | 135 | const std::uint32_t magnitude = f & 0x7fffffffu; |
| 136 | if (magnitude == 0x7f800000u) return sign_bit | (max_exponent << mantissa); // infinity stays infinity | ||
| 137 | // D3D11.3 3.2.2: float-to-float conversion rounds toward zero, so a finite value too large is the largest finite. | ||
| 138 | const std::uint32_t largest = (max_exponent << mantissa) - 1; | ||
| 135 | const int bias = int(max_exponent >> 1); | 139 | const int bias = int(max_exponent >> 1); |
| 136 | const int e = int(magnitude >> 23) - 127 + bias; // the exponent field in the small format | 140 | const int e = int(magnitude >> 23) - 127 + bias; // the exponent field in the small format |
| 137 | if (e >= int(max_exponent)) return sign_bit | (max_exponent << mantissa); // overflow, or infinity | 141 | if (e >= int(max_exponent)) return sign_bit | largest; |
| 138 | const std::uint32_t full = (magnitude & 0x7fffffu) | 0x800000u; // 24-bit significand | 142 | const std::uint32_t full = (magnitude & 0x7fffffu) | 0x800000u; // 24-bit significand |
| 139 | // A normal result keeps `mantissa` bits; a subnormal one shifts further right. | ||
| 140 | const int shift = (e > 0 ? 0 : 1 - e) + int(23 - mantissa); | 143 | const int shift = (e > 0 ? 0 : 1 - e) + int(23 - mantissa); |
| 141 | if (shift > 24) return sign_bit; // rounds to zero | 144 | if (shift > 24) return sign_bit; |
| 142 | std::uint32_t kept = full >> shift; | 145 | const std::uint32_t kept = full >> shift; |
| 143 | const std::uint32_t rest = full & mask(unsigned(shift)); | ||
| 144 | const std::uint32_t half = 1u << (shift - 1); | ||
| 145 | if (rest > half || (rest == half && (kept & 1))) ++kept; // may carry into the exponent, which is right | ||
| 146 | const std::uint32_t biased = e > 0 ? (std::uint32_t(e) << mantissa) + (kept - (1u << mantissa)) : kept; | 146 | const std::uint32_t biased = e > 0 ? (std::uint32_t(e) << mantissa) + (kept - (1u << mantissa)) : kept; |
| 147 | return sign_bit | std::min(biased, max_exponent << mantissa); | 147 | return sign_bit | biased; |
| 148 | } | 148 | } |
| 149 | 149 | ||
| 150 | inline float from_small_float(std::uint32_t bits, unsigned mantissa, unsigned exponent, bool sign) { | 150 | inline float from_small_float(std::uint32_t bits, unsigned mantissa, unsigned exponent, bool sign) { |
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index 53c32f85a..d8b129a3d 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h | |||
| @@ -8,6 +8,7 @@ | |||
| 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_depth24.h" | 12 | #include "vulkan_depth24.h" |
| 12 | #include "vulkan_replay.h" | 13 | #include "vulkan_replay.h" |
| 13 | 14 | ||
| @@ -54,14 +55,19 @@ struct VulkanDeviceFunctions { | |||
| 54 | X(vkCmdDrawIndexedIndirectCount) X(vkCmdDispatchIndirect) X(vkGetBufferDeviceAddress) \ | 55 | X(vkCmdDrawIndexedIndirectCount) X(vkCmdDispatchIndirect) X(vkGetBufferDeviceAddress) \ |
| 55 | X(vkGetAccelerationStructureBuildSizesKHR) X(vkCreateAccelerationStructureKHR) \ | 56 | X(vkGetAccelerationStructureBuildSizesKHR) X(vkCreateAccelerationStructureKHR) \ |
| 56 | X(vkDestroyAccelerationStructureKHR) X(vkGetAccelerationStructureDeviceAddressKHR) \ | 57 | X(vkDestroyAccelerationStructureKHR) X(vkGetAccelerationStructureDeviceAddressKHR) \ |
| 57 | X(vkCmdBuildAccelerationStructuresKHR) | 58 | X(vkCmdBuildAccelerationStructuresKHR) \ |
| 59 | X(vkCreateDescriptorSetLayout) X(vkDestroyDescriptorSetLayout) X(vkCreateDescriptorPool) \ | ||
| 60 | X(vkDestroyDescriptorPool) X(vkAllocateDescriptorSets) X(vkUpdateDescriptorSets) \ | ||
| 61 | X(vkCreatePipelineLayout) X(vkDestroyPipelineLayout) X(vkCreateShaderModule) X(vkDestroyShaderModule) \ | ||
| 62 | X(vkCreateComputePipelines) X(vkDestroyPipeline) X(vkCmdBindPipeline) X(vkCmdBindDescriptorSets) \ | ||
| 63 | X(vkCmdPushConstants) X(vkCmdDispatch) | ||
| 58 | #define LEONIDA_VK_MEMBER(name) PFN_##name name = nullptr; | 64 | #define LEONIDA_VK_MEMBER(name) PFN_##name name = nullptr; |
| 59 | LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_MEMBER) | 65 | LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_MEMBER) |
| 60 | #undef LEONIDA_VK_MEMBER | 66 | #undef LEONIDA_VK_MEMBER |
| 61 | 67 | ||
| 62 | // The Vulkan 1.3 entry points may be null on older devices, the extension ones (conditional rendering) when the | 68 | // The Vulkan 1.3 entry points may be null on older devices, the extension ones (conditional rendering) when the |
| 63 | // extension is not enabled, and the query functions are optional (a list that uses queries without them is | 69 | // extension is not enabled, and the query functions are optional (a list that uses queries without them is |
| 64 | // unsupported); everything else is required. | 70 | // unsupported). The compute and descriptor functions are needed only for binary occlusion resolves. |
| 65 | bool load(PFN_vkGetDeviceProcAddr get, VkDevice device) { | 71 | bool load(PFN_vkGetDeviceProcAddr get, VkDevice device) { |
| 66 | #define LEONIDA_VK_LOAD(name) name = reinterpret_cast<PFN_##name>(get(device, #name)); | 72 | #define LEONIDA_VK_LOAD(name) name = reinterpret_cast<PFN_##name>(get(device, #name)); |
| 67 | LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_LOAD) | 73 | LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_LOAD) |
| @@ -79,6 +85,157 @@ struct VulkanDeviceFunctions { | |||
| 79 | } | 85 | } |
| 80 | }; | 86 | }; |
| 81 | 87 | ||
| 88 | // The scratch buffer belongs to one queue. Each chunk copies raw results into it, normalizes them in place, | ||
| 89 | // then transfers UINT64s to the application buffer, which needs no storage-buffer usage. | ||
| 90 | class BinaryOcclusionResolve { | ||
| 91 | public: | ||
| 92 | BinaryOcclusionResolve(const VulkanDeviceFunctions& vk, VkDevice device) : vk_(vk), device_(device) {} | ||
| 93 | BinaryOcclusionResolve(const BinaryOcclusionResolve&) = delete; | ||
| 94 | BinaryOcclusionResolve& operator=(const BinaryOcclusionResolve&) = delete; | ||
| 95 | |||
| 96 | ~BinaryOcclusionResolve() { | ||
| 97 | if (pipeline_) vk_.vkDestroyPipeline(device_, pipeline_, nullptr); | ||
| 98 | if (layout_) vk_.vkDestroyPipelineLayout(device_, layout_, nullptr); | ||
| 99 | if (pool_) vk_.vkDestroyDescriptorPool(device_, pool_, nullptr); | ||
| 100 | if (set_layout_) vk_.vkDestroyDescriptorSetLayout(device_, set_layout_, nullptr); | ||
| 101 | if (scratch_) vk_.vkDestroyBuffer(device_, scratch_, nullptr); | ||
| 102 | if (memory_) vk_.vkFreeMemory(device_, memory_, nullptr); | ||
| 103 | } | ||
| 104 | |||
| 105 | bool initialize(const VkPhysicalDeviceMemoryProperties& memory) { | ||
| 106 | if (!vk_.vkCreateDescriptorSetLayout || !vk_.vkDestroyDescriptorSetLayout || | ||
| 107 | !vk_.vkCreateDescriptorPool || !vk_.vkDestroyDescriptorPool || !vk_.vkAllocateDescriptorSets || | ||
| 108 | !vk_.vkUpdateDescriptorSets || !vk_.vkCreatePipelineLayout || !vk_.vkDestroyPipelineLayout || | ||
| 109 | !vk_.vkCreateShaderModule || !vk_.vkDestroyShaderModule || !vk_.vkCreateComputePipelines || | ||
| 110 | !vk_.vkDestroyPipeline || !vk_.vkCmdBindPipeline || !vk_.vkCmdBindDescriptorSets || | ||
| 111 | !vk_.vkCmdPushConstants || !vk_.vkCmdDispatch) | ||
| 112 | return false; | ||
| 113 | |||
| 114 | VkBufferCreateInfo buffer{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; | ||
| 115 | buffer.size = scratch_size; | ||
| 116 | buffer.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | | ||
| 117 | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; | ||
| 118 | if (vk_.vkCreateBuffer(device_, &buffer, nullptr, &scratch_) != VK_SUCCESS) return false; | ||
| 119 | VkMemoryRequirements requirements{}; | ||
| 120 | vk_.vkGetBufferMemoryRequirements(device_, scratch_, &requirements); | ||
| 121 | auto type = memory.memoryTypeCount; | ||
| 122 | for (std::uint32_t i = 0; i < memory.memoryTypeCount; ++i) { | ||
| 123 | if (!(requirements.memoryTypeBits & (1u << i))) continue; | ||
| 124 | if (type == memory.memoryTypeCount) type = i; | ||
| 125 | if (memory.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) { | ||
| 126 | type = i; | ||
| 127 | break; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | if (type == memory.memoryTypeCount) return false; | ||
| 131 | VkMemoryAllocateInfo allocation{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO}; | ||
| 132 | allocation.allocationSize = requirements.size; | ||
| 133 | allocation.memoryTypeIndex = type; | ||
| 134 | if (vk_.vkAllocateMemory(device_, &allocation, nullptr, &memory_) != VK_SUCCESS || | ||
| 135 | vk_.vkBindBufferMemory(device_, scratch_, memory_, 0) != VK_SUCCESS) | ||
| 136 | return false; | ||
| 137 | |||
| 138 | const VkDescriptorSetLayoutBinding binding{0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, | ||
| 139 | VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; | ||
| 140 | VkDescriptorSetLayoutCreateInfo set_layout{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO}; | ||
| 141 | set_layout.bindingCount = 1; | ||
| 142 | set_layout.pBindings = &binding; | ||
| 143 | if (vk_.vkCreateDescriptorSetLayout(device_, &set_layout, nullptr, &set_layout_) != VK_SUCCESS) return false; | ||
| 144 | const VkDescriptorPoolSize size{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1}; | ||
| 145 | VkDescriptorPoolCreateInfo pool{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO}; | ||
| 146 | pool.maxSets = 1; | ||
| 147 | pool.poolSizeCount = 1; | ||
| 148 | pool.pPoolSizes = &size; | ||
| 149 | if (vk_.vkCreateDescriptorPool(device_, &pool, nullptr, &pool_) != VK_SUCCESS) return false; | ||
| 150 | VkDescriptorSetAllocateInfo set{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO}; | ||
| 151 | set.descriptorPool = pool_; | ||
| 152 | set.descriptorSetCount = 1; | ||
| 153 | set.pSetLayouts = &set_layout_; | ||
| 154 | if (vk_.vkAllocateDescriptorSets(device_, &set, &set_) != VK_SUCCESS) return false; | ||
| 155 | const VkDescriptorBufferInfo storage{scratch_, 0, scratch_size}; | ||
| 156 | VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}; | ||
| 157 | write.dstSet = set_; | ||
| 158 | write.descriptorCount = 1; | ||
| 159 | write.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; | ||
| 160 | write.pBufferInfo = &storage; | ||
| 161 | vk_.vkUpdateDescriptorSets(device_, 1, &write, 0, nullptr); | ||
| 162 | |||
| 163 | const VkPushConstantRange count{VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(std::uint32_t)}; | ||
| 164 | VkPipelineLayoutCreateInfo layout{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO}; | ||
| 165 | layout.setLayoutCount = 1; | ||
| 166 | layout.pSetLayouts = &set_layout_; | ||
| 167 | layout.pushConstantRangeCount = 1; | ||
| 168 | layout.pPushConstantRanges = &count; | ||
| 169 | if (vk_.vkCreatePipelineLayout(device_, &layout, nullptr, &layout_) != VK_SUCCESS) return false; | ||
| 170 | VkShaderModuleCreateInfo module{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO}; | ||
| 171 | module.codeSize = sizeof(binary_occlusion_spirv); | ||
| 172 | module.pCode = binary_occlusion_spirv; | ||
| 173 | VkShaderModule shader = VK_NULL_HANDLE; | ||
| 174 | if (vk_.vkCreateShaderModule(device_, &module, nullptr, &shader) != VK_SUCCESS) return false; | ||
| 175 | VkComputePipelineCreateInfo pipeline{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO}; | ||
| 176 | pipeline.stage = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, nullptr, 0, | ||
| 177 | VK_SHADER_STAGE_COMPUTE_BIT, shader, "main", nullptr}; | ||
| 178 | pipeline.layout = layout_; | ||
| 179 | const VkResult result = vk_.vkCreateComputePipelines(device_, VK_NULL_HANDLE, 1, &pipeline, nullptr, &pipeline_); | ||
| 180 | vk_.vkDestroyShaderModule(device_, shader, nullptr); | ||
| 181 | return result == VK_SUCCESS; | ||
| 182 | } | ||
| 183 | |||
| 184 | void record(VkCommandBuffer cb, VkQueryPool queries, std::uint32_t first, std::uint32_t count, | ||
| 185 | VkBuffer dst, VkDeviceSize offset) const { | ||
| 186 | constexpr auto transfer = VK_PIPELINE_STAGE_TRANSFER_BIT; | ||
| 187 | constexpr auto compute = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; | ||
| 188 | vk_.vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline_); | ||
| 189 | vk_.vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE, layout_, 0, 1, &set_, 0, nullptr); | ||
| 190 | while (count) { | ||
| 191 | const std::uint32_t chunk = std::min(count, scratch_count); | ||
| 192 | const VkDeviceSize bytes = VkDeviceSize(chunk) * result_size; | ||
| 193 | // Reuse is ordered even across submissions on this queue. Each previous copy has read the scratch | ||
| 194 | // words before the query pool can write them again. | ||
| 195 | scratch_barrier(cb, transfer, transfer, VK_ACCESS_TRANSFER_READ_BIT, VK_ACCESS_TRANSFER_WRITE_BIT); | ||
| 196 | vk_.vkCmdCopyQueryPoolResults(cb, queries, first, chunk, scratch_, 0, result_size, | ||
| 197 | VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); | ||
| 198 | scratch_barrier(cb, transfer, compute, VK_ACCESS_TRANSFER_WRITE_BIT, | ||
| 199 | VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT); | ||
| 200 | vk_.vkCmdPushConstants(cb, layout_, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(chunk), &chunk); | ||
| 201 | vk_.vkCmdDispatch(cb, (chunk + group_size - 1) / group_size, 1, 1); | ||
| 202 | scratch_barrier(cb, compute, transfer, VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT); | ||
| 203 | const VkBufferCopy copy{0, offset, bytes}; | ||
| 204 | vk_.vkCmdCopyBuffer(cb, scratch_, dst, 1, ©); | ||
| 205 | first += chunk; | ||
| 206 | count -= chunk; | ||
| 207 | offset += bytes; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | private: | ||
| 212 | void scratch_barrier(VkCommandBuffer cb, VkPipelineStageFlags src, VkPipelineStageFlags dst, | ||
| 213 | VkAccessFlags src_access, VkAccessFlags dst_access) const { | ||
| 214 | VkBufferMemoryBarrier barrier{VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER}; | ||
| 215 | barrier.srcAccessMask = src_access; | ||
| 216 | barrier.dstAccessMask = dst_access; | ||
| 217 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; | ||
| 218 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; | ||
| 219 | barrier.buffer = scratch_; | ||
| 220 | barrier.size = scratch_size; | ||
| 221 | vk_.vkCmdPipelineBarrier(cb, src, dst, 0, 0, nullptr, 1, &barrier, 0, nullptr); | ||
| 222 | } | ||
| 223 | |||
| 224 | static constexpr std::uint32_t scratch_count = 1024; | ||
| 225 | static constexpr std::uint32_t group_size = 64; // local_size_x in binary_occlusion.comp | ||
| 226 | static constexpr VkDeviceSize result_size = sizeof(std::uint64_t); | ||
| 227 | static constexpr VkDeviceSize scratch_size = scratch_count * result_size; | ||
| 228 | const VulkanDeviceFunctions& vk_; | ||
| 229 | VkDevice device_; | ||
| 230 | VkBuffer scratch_ = VK_NULL_HANDLE; | ||
| 231 | VkDeviceMemory memory_ = VK_NULL_HANDLE; | ||
| 232 | VkDescriptorSetLayout set_layout_ = VK_NULL_HANDLE; | ||
| 233 | VkDescriptorPool pool_ = VK_NULL_HANDLE; | ||
| 234 | VkDescriptorSet set_ = VK_NULL_HANDLE; | ||
| 235 | VkPipelineLayout layout_ = VK_NULL_HANDLE; | ||
| 236 | VkPipeline pipeline_ = VK_NULL_HANDLE; | ||
| 237 | }; | ||
| 238 | |||
| 82 | // Per device: what the queues share. Resources are registered by their stable ID (the COM resource pointer). | 239 | // Per device: what the queues share. Resources are registered by their stable ID (the COM resource pointer). |
| 83 | class VulkanContext { | 240 | class VulkanContext { |
| 84 | public: | 241 | public: |
| @@ -209,6 +366,11 @@ public: | |||
| 209 | double timestamp_period() const { return timestamp_period_; } | 366 | double timestamp_period() const { return timestamp_period_; } |
| 210 | void set_timestamp_period(double period) { timestamp_period_ = period; } | 367 | void set_timestamp_period(double period) { timestamp_period_ = period; } |
| 211 | 368 | ||
| 369 | // Configure before queue creation, when the selected queue family supports compute. | ||
| 370 | void set_binary_occlusion_memory(const VkPhysicalDeviceMemoryProperties& memory) { | ||
| 371 | binary_occlusion_memory_ = memory; | ||
| 372 | } | ||
| 373 | |||
| 212 | // For tests and traces. | 374 | // For tests and traces. |
| 213 | VkImageLayout layout(ResourceId id, std::uint32_t subresource) { | 375 | VkImageLayout layout(ResourceId id, std::uint32_t subresource) { |
| 214 | std::lock_guard lock(mutex_); | 376 | std::lock_guard lock(mutex_); |
| @@ -320,6 +482,7 @@ private: | |||
| 320 | std::function<void(bool)> bindless_commit_; | 482 | std::function<void(bool)> bindless_commit_; |
| 321 | std::unordered_map<QueryHeapId, QueryPool> query_pools_; // element pointers stay valid across inserts | 483 | std::unordered_map<QueryHeapId, QueryPool> query_pools_; // element pointers stay valid across inserts |
| 322 | double timestamp_period_ = 0; | 484 | double timestamp_period_ = 0; |
| 485 | VkPhysicalDeviceMemoryProperties binary_occlusion_memory_{}; | ||
| 323 | ReplayFunctions replay_; | 486 | ReplayFunctions replay_; |
| 324 | DebugLabelFunctions labels_; | 487 | DebugLabelFunctions labels_; |
| 325 | }; | 488 | }; |
| @@ -415,10 +578,24 @@ public: | |||
| 415 | std::lock_guard lock(context_.mutex_); | 578 | std::lock_guard lock(context_.mutex_); |
| 416 | if (context_.depth24_) context_.depth24_->collect(); | 579 | if (context_.depth24_) context_.depth24_->collect(); |
| 417 | std::vector<PreparedList> prepared(lists.size()); | 580 | std::vector<PreparedList> prepared(lists.size()); |
| 581 | ReplayFunctions replay = context_.replay_; | ||
| 582 | if (context_.binary_occlusion_memory_.memoryTypeCount) { | ||
| 583 | replay.resolve_binary_occlusion = [this](VkCommandBuffer cb, VkQueryPool pool, std::uint32_t first, | ||
| 584 | std::uint32_t count, VkBuffer dst, VkDeviceSize offset) { | ||
| 585 | binary_occlusion_->record(cb, pool, first, count, dst, offset); | ||
| 586 | }; | ||
| 587 | } | ||
| 418 | const auto resolve = [this](ResourceId id) { return context_.resolve(id); }; | 588 | const auto resolve = [this](ResourceId id) { return context_.resolve(id); }; |
| 589 | bool binary_occlusion_needed = false; | ||
| 419 | for (std::size_t i = 0; i < lists.size(); ++i) { | 590 | for (std::size_t i = 0; i < lists.size(); ++i) { |
| 420 | const auto result = prepare(*lists[i], resolve, context_.replay_, prepared[i]); | 591 | const auto result = prepare(*lists[i], resolve, replay, prepared[i]); |
| 421 | if (result != ReplayResult::success) return result; | 592 | if (result != ReplayResult::success) return result; |
| 593 | binary_occlusion_needed |= prepared[i].binary_occlusion; | ||
| 594 | } | ||
| 595 | if (binary_occlusion_needed && !binary_occlusion_) { | ||
| 596 | auto helper = std::make_unique<BinaryOcclusionResolve>(context_.vk, context_.device()); | ||
| 597 | if (!helper->initialize(context_.binary_occlusion_memory_)) return ReplayResult::unsupported; | ||
| 598 | binary_occlusion_ = std::move(helper); | ||
| 422 | } | 599 | } |
| 423 | VkCommandBuffer command_buffer = acquire(); | 600 | VkCommandBuffer command_buffer = acquire(); |
| 424 | if (!command_buffer) return ReplayResult::unsupported; | 601 | if (!command_buffer) return ReplayResult::unsupported; |
| @@ -433,8 +610,8 @@ public: | |||
| 433 | return ReplayResult::unsupported; | 610 | return ReplayResult::unsupported; |
| 434 | } | 611 | } |
| 435 | std::uint32_t open_labels = 0; // events may span the lists of one ExecuteCommandLists | 612 | std::uint32_t open_labels = 0; // events may span the lists of one ExecuteCommandLists |
| 436 | for (const auto& list : prepared) emit(list, command_buffer, context_.replay_, &context_.tracker_, &open_labels); | 613 | for (const auto& list : prepared) emit(list, command_buffer, replay, &context_.tracker_, &open_labels); |
| 437 | close_labels(command_buffer, context_.replay_, open_labels); | 614 | close_labels(command_buffer, replay, open_labels); |
| 438 | // A fence wait on the CPU makes GPU writes visible to the host only after a barrier to the host domain: | 615 | // A fence wait on the CPU makes GPU writes visible to the host only after a barrier to the host domain: |
| 439 | // readback buffers are read through Map once the application has waited. | 616 | // readback buffers are read through Map once the application has waited. |
| 440 | VkMemoryBarrier host{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; | 617 | VkMemoryBarrier host{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; |
| @@ -547,6 +724,7 @@ private: | |||
| 547 | std::vector<std::pair<VkCommandBuffer, std::uint64_t>> buffers_; // buffer, ticket that frees it | 724 | std::vector<std::pair<VkCommandBuffer, std::uint64_t>> buffers_; // buffer, ticket that frees it |
| 548 | std::vector<VkSemaphore> waits_; | 725 | std::vector<VkSemaphore> waits_; |
| 549 | std::vector<std::uint64_t> wait_values_; | 726 | std::vector<std::uint64_t> wait_values_; |
| 727 | std::unique_ptr<BinaryOcclusionResolve> binary_occlusion_; | ||
| 550 | std::uint32_t open_labels_ = 0; // queue labels begun and not ended | 728 | std::uint32_t open_labels_ = 0; // queue labels begun and not ended |
| 551 | bool ok_ = false; | 729 | bool ok_ = false; |
| 552 | }; | 730 | }; |
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 63d5bd07d..f42be50a3 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" |
| @@ -633,6 +634,9 @@ struct ReplayFunctions { | |||
| 633 | PFN_vkCmdEndQuery end_query = nullptr; | 634 | PFN_vkCmdEndQuery end_query = nullptr; |
| 634 | PFN_vkCmdWriteTimestamp write_timestamp = nullptr; | 635 | PFN_vkCmdWriteTimestamp write_timestamp = nullptr; |
| 635 | PFN_vkCmdCopyQueryPoolResults copy_query_pool_results = nullptr; | 636 | PFN_vkCmdCopyQueryPoolResults copy_query_pool_results = nullptr; |
| 637 | // Resolves available binary-occlusion queries to 64-bit 0/1 values, outside conditional rendering. | ||
| 638 | std::function<void(VkCommandBuffer, VkQueryPool, std::uint32_t first, std::uint32_t count, | ||
| 639 | VkBuffer dst, VkDeviceSize offset)> resolve_binary_occlusion; | ||
| 636 | // VK_EXT_debug_utils, set only when the instance enabled it; debug markers are skipped without them. | 640 | // VK_EXT_debug_utils, set only when the instance enabled it; debug markers are skipped without them. |
| 637 | PFN_vkCmdInsertDebugUtilsLabelEXT insert_label = nullptr; | 641 | PFN_vkCmdInsertDebugUtilsLabelEXT insert_label = nullptr; |
| 638 | PFN_vkCmdBeginDebugUtilsLabelEXT begin_label = nullptr; | 642 | PFN_vkCmdBeginDebugUtilsLabelEXT begin_label = nullptr; |
| @@ -1234,6 +1238,7 @@ class PreparedList { | |||
| 1234 | public: | 1238 | public: |
| 1235 | const CommandList* list = nullptr; | 1239 | const CommandList* list = nullptr; |
| 1236 | std::vector<detail::Prepared> commands; | 1240 | std::vector<detail::Prepared> commands; |
| 1241 | bool binary_occlusion = false; // the queue needs its internal resolve resources | ||
| 1237 | // Copies, resolves and UAV clears that run as transfers, recorded while a predicate was set. D3D12 skips them | 1242 | // Copies, resolves and UAV clears that run as transfers, recorded while a predicate was set. D3D12 skips them |
| 1238 | // when the predicate says so, but Vulkan's conditional rendering does not cover transfer commands, so they run | 1243 | // when the predicate says so, but Vulkan's conditional rendering does not cover transfer commands, so they run |
| 1239 | // unconditionally (docs/d3d12_vulkan.md section 4.7). Counted so the caller can report it. | 1244 | // unconditionally (docs/d3d12_vulkan.md section 4.7). Counted so the caller can report it. |
| @@ -1297,6 +1302,7 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun | |||
| 1297 | const auto commands = list.commands(); | 1302 | const auto commands = list.commands(); |
| 1298 | out.list = &list; | 1303 | out.list = &list; |
| 1299 | out.commands.assign(commands.size(), {}); | 1304 | out.commands.assign(commands.size(), {}); |
| 1305 | out.binary_occlusion = false; | ||
| 1300 | const bool rendering = vk.begin_rendering && vk.end_rendering && vk.clear_attachments && vk.image_view; | 1306 | const bool rendering = vk.begin_rendering && vk.end_rendering && vk.clear_attachments && vk.image_view; |
| 1301 | const bool copies = vk.copy_buffer && vk.copy_buffer_to_image && vk.copy_image_to_buffer && vk.copy_image; | 1307 | const bool copies = vk.copy_buffer && vk.copy_buffer_to_image && vk.copy_image_to_buffer && vk.copy_image; |
| 1302 | const bool conditional = vk.begin_conditional_rendering && vk.end_conditional_rendering; | 1308 | const bool conditional = vk.begin_conditional_rendering && vk.end_conditional_rendering; |
| @@ -1541,10 +1547,17 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun | |||
| 1541 | if (const auto* resolve_queries = std::get_if<ResolveQueries>(&command)) { | 1547 | if (const auto* resolve_queries = std::get_if<ResolveQueries>(&command)) { |
| 1542 | // The destination is a buffer, as D3D12 requires, large enough for every result. | 1548 | // The destination is a buffer, as D3D12 requires, large enough for every result. |
| 1543 | if (!bind(resolve_queries->dst)) return ReplayResult::unresolved_resource; | 1549 | if (!bind(resolve_queries->dst)) return ReplayResult::unresolved_resource; |
| 1544 | if (!p.binding.buffer || | 1550 | const std::uint64_t bytes = std::uint64_t(range.count) * query_result_size(range.type); |
| 1545 | !detail::within(resolve_queries->offset, std::uint64_t(range.count) * query_result_size(range.type), | 1551 | if (!p.binding.buffer || p.binding.buffer_offset % 8 || |
| 1546 | p.binding.size)) | 1552 | !detail::within(resolve_queries->offset, bytes, p.binding.size) || |
| 1553 | !detail::within(p.binding.buffer_offset, resolve_queries->offset + bytes, | ||
| 1554 | std::numeric_limits<VkDeviceSize>::max())) | ||
| 1547 | return ReplayResult::invalid_query; | 1555 | return ReplayResult::invalid_query; |
| 1556 | if (range.type == d3d12_query::binary_occlusion) { | ||
| 1557 | // The internal dispatch would otherwise count towards the application's CSInvocations. | ||
| 1558 | if (!vk.resolve_binary_occlusion || statistics_active) return ReplayResult::unsupported; | ||
| 1559 | out.binary_occlusion = true; | ||
| 1560 | } | ||
| 1548 | } | 1561 | } |
| 1549 | } else if (const auto* ind = std::get_if<ExecuteIndirect>(&command)) { | 1562 | } else if (const auto* ind = std::get_if<ExecuteIndirect>(&command)) { |
| 1550 | if (!bind(ind->argument_buffer) || !p.binding.buffer) return ReplayResult::unresolved_resource; | 1563 | if (!bind(ind->argument_buffer) || !p.binding.buffer) return ReplayResult::unresolved_resource; |
| @@ -1595,14 +1608,13 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 1595 | const VulkanAccess transfer_dst = vulkan_access(d3d12_state::copy_dest, false); | 1608 | const VulkanAccess transfer_dst = vulkan_access(d3d12_state::copy_dest, false); |
| 1596 | // The extension is enabled: barriers into and out of PREDICATION also cover conditional rendering's read. | 1609 | // The extension is enabled: barriers into and out of PREDICATION also cover conditional rendering's read. |
| 1597 | const bool predicates = vk.begin_conditional_rendering != nullptr; | 1610 | const bool predicates = vk.begin_conditional_rendering != nullptr; |
| 1598 | bool conditional_rendering = false; // inside vkCmdBeginConditionalRenderingEXT | ||
| 1599 | VkConditionalRenderingBeginInfoEXT predicate{VK_STRUCTURE_TYPE_CONDITIONAL_RENDERING_BEGIN_INFO_EXT}; | 1611 | VkConditionalRenderingBeginInfoEXT predicate{VK_STRUCTURE_TYPE_CONDITIONAL_RENDERING_BEGIN_INFO_EXT}; |
| 1600 | // A dispatch honours conditional rendering and a copy does not (the layer runs copies unpredicated), so the | 1612 | // A dispatch honours conditional rendering and a copy does not (the layer runs copies unpredicated), so the |
| 1601 | // conversion around a copy must run whatever the predicate says: end it for the dispatch and begin it again. | 1613 | // conversion around a copy must run whatever the predicate says: end it for the dispatch and begin it again. |
| 1602 | const auto run_depth24 = [&](const Depth24Job& job) { | 1614 | const auto run_depth24 = [&](const Depth24Job& job) { |
| 1603 | if (conditional_rendering) vk.end_conditional_rendering(command_buffer); | 1615 | if (predicate.buffer) vk.end_conditional_rendering(command_buffer); |
| 1604 | vk.depth24_run(command_buffer, job); | 1616 | vk.depth24_run(command_buffer, job); |
| 1605 | if (conditional_rendering) vk.begin_conditional_rendering(command_buffer, &predicate); | 1617 | if (predicate.buffer) vk.begin_conditional_rendering(command_buffer, &predicate); |
| 1606 | }; | 1618 | }; |
| 1607 | 1619 | ||
| 1608 | for (std::size_t i = 0; i < commands.size(); ++i) { | 1620 | for (std::size_t i = 0; i < commands.size(); ++i) { |
| @@ -1610,10 +1622,9 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 1610 | const auto& p = prepared.commands[i]; | 1622 | const auto& p = prepared.commands[i]; |
| 1611 | if (const auto* predication = std::get_if<Predication>(&command)) { | 1623 | if (const auto* predication = std::get_if<Predication>(&command)) { |
| 1612 | // Conditional rendering cannot nest, so a new predicate ends the previous one first. | 1624 | // Conditional rendering cannot nest, so a new predicate ends the previous one first. |
| 1613 | if (conditional_rendering) vk.end_conditional_rendering(command_buffer); | 1625 | if (predicate.buffer) vk.end_conditional_rendering(command_buffer); |
| 1614 | conditional_rendering = predication->buffer != 0; | 1626 | predicate.buffer = p.binding.buffer; |
| 1615 | if (conditional_rendering) { | 1627 | if (predicate.buffer) { |
| 1616 | predicate.buffer = p.binding.buffer; | ||
| 1617 | predicate.offset = predication->offset; | 1628 | predicate.offset = predication->offset; |
| 1618 | // Vulkan discards the commands when the word is 0, which is EQUAL_ZERO; INVERTED gives NOT_EQUAL_ZERO. | 1629 | // Vulkan discards the commands when the word is 0, which is EQUAL_ZERO; INVERTED gives NOT_EQUAL_ZERO. |
| 1619 | predicate.flags = predication->op == d3d12_predication::not_equal_zero | 1630 | predicate.flags = predication->op == d3d12_predication::not_equal_zero |
| @@ -1988,24 +1999,30 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 1988 | } | 1999 | } |
| 1989 | heap.ended[end->index] = true; | 2000 | heap.ended[end->index] = true; |
| 1990 | } else if (const auto* resolve = std::get_if<ResolveQueries>(&command)) { | 2001 | } else if (const auto* resolve = std::get_if<ResolveQueries>(&command)) { |
| 1991 | // Runs of ended queries are copied as UINT64s, waiting for their results; D3D12 leaves a query that | 2002 | // D3D12 leaves a query that was never ended undefined. Write zeros instead of waiting forever. |
| 1992 | // was never ended undefined, and it is written as zeros here rather than waited on forever. | ||
| 1993 | const QueryPool& heap = *p.queries; | 2003 | const QueryPool& heap = *p.queries; |
| 1994 | const std::uint32_t size = query_result_size(resolve->type); | 2004 | const std::uint32_t size = query_result_size(resolve->type); |
| 2005 | const bool binary = resolve->type == d3d12_query::binary_occlusion; | ||
| 2006 | // ResolveQueryData is unconditional in D3D12, including our internal dispatch. | ||
| 2007 | if (binary && predicate.buffer) vk.end_conditional_rendering(command_buffer); | ||
| 1995 | std::uint32_t index = resolve->first; | 2008 | std::uint32_t index = resolve->first; |
| 1996 | const std::uint32_t last = resolve->first + resolve->count; | 2009 | const std::uint32_t last = resolve->first + resolve->count; |
| 1997 | while (index < last) { | 2010 | while (index < last) { |
| 1998 | const bool ended = heap.ended[index]; | 2011 | const bool ended = heap.ended[index]; |
| 1999 | std::uint32_t run = 1; | 2012 | std::uint32_t run = 1; |
| 2000 | while (index + run < last && heap.ended[index + run] == ended) ++run; | 2013 | while (index + run < last && heap.ended[index + run] == ended) ++run; |
| 2001 | const VkDeviceSize offset = resolve->offset + VkDeviceSize(index - resolve->first) * size; | 2014 | const VkDeviceSize offset = p.binding.buffer_offset + resolve->offset + |
| 2002 | if (ended) | 2015 | VkDeviceSize(index - resolve->first) * size; |
| 2016 | if (!ended) | ||
| 2017 | vk.fill_buffer(command_buffer, p.binding.buffer, offset, VkDeviceSize(run) * size, 0); | ||
| 2018 | else if (binary) | ||
| 2019 | vk.resolve_binary_occlusion(command_buffer, heap.pool, index, run, p.binding.buffer, offset); | ||
| 2020 | else | ||
| 2003 | vk.copy_query_pool_results(command_buffer, heap.pool, index, run, p.binding.buffer, offset, size, | 2021 | vk.copy_query_pool_results(command_buffer, heap.pool, index, run, p.binding.buffer, offset, size, |
| 2004 | VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); | 2022 | VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); |
| 2005 | else | ||
| 2006 | vk.fill_buffer(command_buffer, p.binding.buffer, offset, VkDeviceSize(run) * size, 0); | ||
| 2007 | index += run; | 2023 | index += run; |
| 2008 | } | 2024 | } |
| 2025 | if (binary && predicate.buffer) vk.begin_conditional_rendering(command_buffer, &predicate); | ||
| 2009 | } else if (const auto* ind = std::get_if<ExecuteIndirect>(&command)) { | 2026 | } else if (const auto* ind = std::get_if<ExecuteIndirect>(&command)) { |
| 2010 | if (ind->count_buffer) { | 2027 | if (ind->count_buffer) { |
| 2011 | if (ind->kind == IndirectKind::draw) { | 2028 | if (ind->kind == IndirectKind::draw) { |
| @@ -2081,7 +2098,7 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 2081 | } | 2098 | } |
| 2082 | } | 2099 | } |
| 2083 | // Predication does not carry over to the next command list. | 2100 | // Predication does not carry over to the next command list. |
| 2084 | if (conditional_rendering) vk.end_conditional_rendering(command_buffer); | 2101 | if (predicate.buffer) vk.end_conditional_rendering(command_buffer); |
| 2085 | if (!open_labels) close_labels(command_buffer, vk, own_labels); | 2102 | if (!open_labels) close_labels(command_buffer, vk, own_labels); |
| 2086 | } | 2103 | } |
| 2087 | 2104 | ||