diff options
| author | ChatGPT (AI-assisted) <chatgpt@local.invalid> | 2026-11-22 15:34:34 -0500 |
|---|---|---|
| committer | ChatGPT (AI-assisted) <chatgpt@local.invalid> | 2026-11-22 15:34:34 -0500 |
| commit | c262fb7b3c2e3e5c30279eed7bca0e85148e202f (patch) | |
| tree | ed25d1bb58466c12766dcdb68379587f01b278af /runtime/src | |
| parent | f847748cc76bd3777f9c070c4034eb2fc1c04e64 (diff) | |
Runtime: gate counted indirect compute dispatches on GPU
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/indirect_dispatch_count.comp | 34 | ||||
| -rw-r--r-- | runtime/src/indirect_dispatch_count_spv.h | 84 | ||||
| -rw-r--r-- | runtime/src/vulkan_backend.h | 26 | ||||
| -rw-r--r-- | runtime/src/vulkan_indirect_dispatch_count.h | 199 | ||||
| -rw-r--r-- | runtime/src/vulkan_replay.h | 42 |
5 files changed, 377 insertions, 8 deletions
diff --git a/runtime/src/indirect_dispatch_count.comp b/runtime/src/indirect_dispatch_count.comp new file mode 100644 index 000000000..9cbc371d9 --- /dev/null +++ b/runtime/src/indirect_dispatch_count.comp | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | // Clamp a GPU-written D3D12 ExecuteIndirect DISPATCH count without reading it on the CPU. | ||
| 2 | // Each record is a VkDispatchIndirectCommand (three uints); disabled records are zero. | ||
| 3 | #version 460 | ||
| 4 | #extension GL_EXT_buffer_reference : require | ||
| 5 | layout(local_size_x = 64) in; | ||
| 6 | layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer WordsIn { | ||
| 7 | uint data[]; | ||
| 8 | }; | ||
| 9 | layout(buffer_reference, std430, buffer_reference_align = 4) buffer WordsOut { | ||
| 10 | uint data[]; | ||
| 11 | }; | ||
| 12 | layout(push_constant) uniform Params { | ||
| 13 | WordsIn arguments; | ||
| 14 | WordsIn count; | ||
| 15 | WordsOut output_words; | ||
| 16 | uint stride_words; | ||
| 17 | uint first_record; | ||
| 18 | uint records; | ||
| 19 | } p; | ||
| 20 | void main() { | ||
| 21 | const uint local_record = gl_GlobalInvocationID.x; | ||
| 22 | if (local_record >= p.records) return; | ||
| 23 | const uint target = local_record * 3u; | ||
| 24 | if (p.first_record + local_record < p.count.data[0]) { | ||
| 25 | const uint source = local_record * p.stride_words; | ||
| 26 | p.output_words.data[target] = p.arguments.data[source]; | ||
| 27 | p.output_words.data[target + 1u] = p.arguments.data[source + 1u]; | ||
| 28 | p.output_words.data[target + 2u] = p.arguments.data[source + 2u]; | ||
| 29 | } else { | ||
| 30 | p.output_words.data[target] = 0u; | ||
| 31 | p.output_words.data[target + 1u] = 0u; | ||
| 32 | p.output_words.data[target + 2u] = 0u; | ||
| 33 | } | ||
| 34 | } | ||
diff --git a/runtime/src/indirect_dispatch_count_spv.h b/runtime/src/indirect_dispatch_count_spv.h new file mode 100644 index 000000000..9cf7bbae5 --- /dev/null +++ b/runtime/src/indirect_dispatch_count_spv.h | |||
| @@ -0,0 +1,84 @@ | |||
| 1 | // Generated with glslangValidator -V --target-env vulkan1.2 -Os --spirv-val indirect_dispatch_count.comp | ||
| 2 | #pragma once | ||
| 3 | #include <cstdint> | ||
| 4 | namespace leonida { | ||
| 5 | inline constexpr std::uint32_t indirect_dispatch_count_spirv[] = { | ||
| 6 | 0x07230203u, 0x00010500u, 0x0008000bu, 0x00000074u, 0x00000000u, 0x00020011u, 0x00000001u, 0x00020011u, | ||
| 7 | 0x000014e3u, 0x0006000bu, 0x00000001u, 0x4c534c47u, 0x6474732eu, 0x3035342eu, 0x00000000u, 0x0003000eu, | ||
| 8 | 0x000014e4u, 0x00000001u, 0x0007000fu, 0x00000005u, 0x00000004u, 0x6e69616du, 0x00000000u, 0x0000000bu, | ||
| 9 | 0x00000019u, 0x00060010u, 0x00000004u, 0x00000011u, 0x00000040u, 0x00000001u, 0x00000001u, 0x00030003u, | ||
| 10 | 0x00000002u, 0x000001ccu, 0x00070004u, 0x455f4c47u, 0x625f5458u, 0x65666675u, 0x65725f72u, 0x65726566u, | ||
| 11 | 0x0065636eu, 0x00040005u, 0x00000004u, 0x6e69616du, 0x00000000u, 0x00080005u, 0x0000000bu, 0x475f6c67u, | ||
| 12 | 0x61626f6cu, 0x766e496cu, 0x7461636fu, 0x496e6f69u, 0x00000044u, 0x00040005u, 0x00000013u, 0x61726150u, | ||
| 13 | 0x0000736du, 0x00060006u, 0x00000013u, 0x00000000u, 0x75677261u, 0x746e656du, 0x00000073u, 0x00050006u, | ||
| 14 | 0x00000013u, 0x00000001u, 0x6e756f63u, 0x00000074u, 0x00070006u, 0x00000013u, 0x00000002u, 0x7074756fu, | ||
| 15 | 0x775f7475u, 0x7364726fu, 0x00000000u, 0x00070006u, 0x00000013u, 0x00000003u, 0x69727473u, 0x775f6564u, | ||
| 16 | 0x7364726fu, 0x00000000u, 0x00070006u, 0x00000013u, 0x00000004u, 0x73726966u, 0x65725f74u, 0x64726f63u, | ||
| 17 | 0x00000000u, 0x00050006u, 0x00000013u, 0x00000005u, 0x6f636572u, 0x00736472u, 0x00040005u, 0x00000015u, | ||
| 18 | 0x64726f57u, 0x006e4973u, 0x00050006u, 0x00000015u, 0x00000000u, 0x61746164u, 0x00000000u, 0x00050005u, | ||
| 19 | 0x00000017u, 0x64726f57u, 0x74754f73u, 0x00000000u, 0x00050006u, 0x00000017u, 0x00000000u, 0x61746164u, | ||
| 20 | 0x00000000u, 0x00030005u, 0x00000019u, 0x00000070u, 0x00040047u, 0x0000000bu, 0x0000000bu, 0x0000001cu, | ||
| 21 | 0x00030047u, 0x00000013u, 0x00000002u, 0x00050048u, 0x00000013u, 0x00000000u, 0x00000023u, 0x00000000u, | ||
| 22 | 0x00050048u, 0x00000013u, 0x00000001u, 0x00000023u, 0x00000008u, 0x00050048u, 0x00000013u, 0x00000002u, | ||
| 23 | 0x00000023u, 0x00000010u, 0x00050048u, 0x00000013u, 0x00000003u, 0x00000023u, 0x00000018u, 0x00050048u, | ||
| 24 | 0x00000013u, 0x00000004u, 0x00000023u, 0x0000001cu, 0x00050048u, 0x00000013u, 0x00000005u, 0x00000023u, | ||
| 25 | 0x00000020u, 0x00040047u, 0x00000014u, 0x00000006u, 0x00000004u, 0x00030047u, 0x00000015u, 0x00000002u, | ||
| 26 | 0x00040048u, 0x00000015u, 0x00000000u, 0x00000018u, 0x00050048u, 0x00000015u, 0x00000000u, 0x00000023u, | ||
| 27 | 0x00000000u, 0x00040047u, 0x00000016u, 0x00000006u, 0x00000004u, 0x00030047u, 0x00000017u, 0x00000002u, | ||
| 28 | 0x00050048u, 0x00000017u, 0x00000000u, 0x00000023u, 0x00000000u, 0x00040047u, 0x00000071u, 0x0000000bu, | ||
| 29 | 0x00000019u, 0x00020013u, 0x00000002u, 0x00030021u, 0x00000003u, 0x00000002u, 0x00040015u, 0x00000006u, | ||
| 30 | 0x00000020u, 0x00000000u, 0x00040017u, 0x00000009u, 0x00000006u, 0x00000003u, 0x00040020u, 0x0000000au, | ||
| 31 | 0x00000001u, 0x00000009u, 0x0004003bu, 0x0000000au, 0x0000000bu, 0x00000001u, 0x0004002bu, 0x00000006u, | ||
| 32 | 0x0000000cu, 0x00000000u, 0x00040020u, 0x0000000du, 0x00000001u, 0x00000006u, 0x00030027u, 0x00000011u, | ||
| 33 | 0x000014e5u, 0x00030027u, 0x00000012u, 0x000014e5u, 0x0008001eu, 0x00000013u, 0x00000011u, 0x00000011u, | ||
| 34 | 0x00000012u, 0x00000006u, 0x00000006u, 0x00000006u, 0x0003001du, 0x00000014u, 0x00000006u, 0x0003001eu, | ||
| 35 | 0x00000015u, 0x00000014u, 0x00040020u, 0x00000011u, 0x000014e5u, 0x00000015u, 0x0003001du, 0x00000016u, | ||
| 36 | 0x00000006u, 0x0003001eu, 0x00000017u, 0x00000016u, 0x00040020u, 0x00000012u, 0x000014e5u, 0x00000017u, | ||
| 37 | 0x00040020u, 0x00000018u, 0x00000009u, 0x00000013u, 0x0004003bu, 0x00000018u, 0x00000019u, 0x00000009u, | ||
| 38 | 0x00040015u, 0x0000001au, 0x00000020u, 0x00000001u, 0x0004002bu, 0x0000001au, 0x0000001bu, 0x00000005u, | ||
| 39 | 0x00040020u, 0x0000001cu, 0x00000009u, 0x00000006u, 0x00020014u, 0x0000001fu, 0x0004002bu, 0x00000006u, | ||
| 40 | 0x00000026u, 0x00000003u, 0x0004002bu, 0x0000001au, 0x00000028u, 0x00000004u, 0x0004002bu, 0x0000001au, | ||
| 41 | 0x0000002du, 0x00000001u, 0x00040020u, 0x0000002eu, 0x00000009u, 0x00000011u, 0x0004002bu, 0x0000001au, | ||
| 42 | 0x00000031u, 0x00000000u, 0x00040020u, 0x00000032u, 0x000014e5u, 0x00000006u, 0x0004002bu, 0x0000001au, | ||
| 43 | 0x0000003au, 0x00000003u, 0x0004002bu, 0x0000001au, 0x0000003eu, 0x00000002u, 0x00040020u, 0x0000003fu, | ||
| 44 | 0x00000009u, 0x00000012u, 0x0004002bu, 0x00000006u, 0x0000004cu, 0x00000001u, 0x0004002bu, 0x00000006u, | ||
| 45 | 0x00000058u, 0x00000002u, 0x0004002bu, 0x00000006u, 0x00000070u, 0x00000040u, 0x0006002cu, 0x00000009u, | ||
| 46 | 0x00000071u, 0x00000070u, 0x0000004cu, 0x0000004cu, 0x00050036u, 0x00000002u, 0x00000004u, 0x00000000u, | ||
| 47 | 0x00000003u, 0x000200f8u, 0x00000005u, 0x000300f7u, 0x00000072u, 0x00000000u, 0x000300fbu, 0x0000000cu, | ||
| 48 | 0x00000073u, 0x000200f8u, 0x00000073u, 0x00050041u, 0x0000000du, 0x0000000eu, 0x0000000bu, 0x0000000cu, | ||
| 49 | 0x0004003du, 0x00000006u, 0x0000000fu, 0x0000000eu, 0x00050041u, 0x0000001cu, 0x0000001du, 0x00000019u, | ||
| 50 | 0x0000001bu, 0x0004003du, 0x00000006u, 0x0000001eu, 0x0000001du, 0x000500aeu, 0x0000001fu, 0x00000020u, | ||
| 51 | 0x0000000fu, 0x0000001eu, 0x000300f7u, 0x00000022u, 0x00000000u, 0x000400fau, 0x00000020u, 0x00000021u, | ||
| 52 | 0x00000022u, 0x000200f8u, 0x00000021u, 0x000200f9u, 0x00000072u, 0x000200f8u, 0x00000022u, 0x00050084u, | ||
| 53 | 0x00000006u, 0x00000027u, 0x0000000fu, 0x00000026u, 0x00050041u, 0x0000001cu, 0x00000029u, 0x00000019u, | ||
| 54 | 0x00000028u, 0x0004003du, 0x00000006u, 0x0000002au, 0x00000029u, 0x00050080u, 0x00000006u, 0x0000002cu, | ||
| 55 | 0x0000002au, 0x0000000fu, 0x00050041u, 0x0000002eu, 0x0000002fu, 0x00000019u, 0x0000002du, 0x0004003du, | ||
| 56 | 0x00000011u, 0x00000030u, 0x0000002fu, 0x00060041u, 0x00000032u, 0x00000033u, 0x00000030u, 0x00000031u, | ||
| 57 | 0x00000031u, 0x0006003du, 0x00000006u, 0x00000034u, 0x00000033u, 0x00000002u, 0x00000004u, 0x000500b0u, | ||
| 58 | 0x0000001fu, 0x00000035u, 0x0000002cu, 0x00000034u, 0x000300f7u, 0x00000037u, 0x00000000u, 0x000400fau, | ||
| 59 | 0x00000035u, 0x00000036u, 0x00000061u, 0x000200f8u, 0x00000036u, 0x00050041u, 0x0000001cu, 0x0000003bu, | ||
| 60 | 0x00000019u, 0x0000003au, 0x0004003du, 0x00000006u, 0x0000003cu, 0x0000003bu, 0x00050084u, 0x00000006u, | ||
| 61 | 0x0000003du, 0x0000000fu, 0x0000003cu, 0x00050041u, 0x0000003fu, 0x00000040u, 0x00000019u, 0x0000003eu, | ||
| 62 | 0x0004003du, 0x00000012u, 0x00000041u, 0x00000040u, 0x00050041u, 0x0000002eu, 0x00000043u, 0x00000019u, | ||
| 63 | 0x00000031u, 0x0004003du, 0x00000011u, 0x00000044u, 0x00000043u, 0x00060041u, 0x00000032u, 0x00000046u, | ||
| 64 | 0x00000044u, 0x00000031u, 0x0000003du, 0x0006003du, 0x00000006u, 0x00000047u, 0x00000046u, 0x00000002u, | ||
| 65 | 0x00000004u, 0x00060041u, 0x00000032u, 0x00000048u, 0x00000041u, 0x00000031u, 0x00000027u, 0x0005003eu, | ||
| 66 | 0x00000048u, 0x00000047u, 0x00000002u, 0x00000004u, 0x00050080u, 0x00000006u, 0x0000004du, 0x00000027u, | ||
| 67 | 0x0000004cu, 0x00050080u, 0x00000006u, 0x00000051u, 0x0000003du, 0x0000004cu, 0x00060041u, 0x00000032u, | ||
| 68 | 0x00000052u, 0x00000044u, 0x00000031u, 0x00000051u, 0x0006003du, 0x00000006u, 0x00000053u, 0x00000052u, | ||
| 69 | 0x00000002u, 0x00000004u, 0x00060041u, 0x00000032u, 0x00000054u, 0x00000041u, 0x00000031u, 0x0000004du, | ||
| 70 | 0x0005003eu, 0x00000054u, 0x00000053u, 0x00000002u, 0x00000004u, 0x00050080u, 0x00000006u, 0x00000059u, | ||
| 71 | 0x00000027u, 0x00000058u, 0x00050080u, 0x00000006u, 0x0000005du, 0x0000003du, 0x00000058u, 0x00060041u, | ||
| 72 | 0x00000032u, 0x0000005eu, 0x00000044u, 0x00000031u, 0x0000005du, 0x0006003du, 0x00000006u, 0x0000005fu, | ||
| 73 | 0x0000005eu, 0x00000002u, 0x00000004u, 0x00060041u, 0x00000032u, 0x00000060u, 0x00000041u, 0x00000031u, | ||
| 74 | 0x00000059u, 0x0005003eu, 0x00000060u, 0x0000005fu, 0x00000002u, 0x00000004u, 0x000200f9u, 0x00000037u, | ||
| 75 | 0x000200f8u, 0x00000061u, 0x00050041u, 0x0000003fu, 0x00000062u, 0x00000019u, 0x0000003eu, 0x0004003du, | ||
| 76 | 0x00000012u, 0x00000063u, 0x00000062u, 0x00060041u, 0x00000032u, 0x00000065u, 0x00000063u, 0x00000031u, | ||
| 77 | 0x00000027u, 0x0005003eu, 0x00000065u, 0x0000000cu, 0x00000002u, 0x00000004u, 0x00050080u, 0x00000006u, | ||
| 78 | 0x00000069u, 0x00000027u, 0x0000004cu, 0x00060041u, 0x00000032u, 0x0000006au, 0x00000063u, 0x00000031u, | ||
| 79 | 0x00000069u, 0x0005003eu, 0x0000006au, 0x0000000cu, 0x00000002u, 0x00000004u, 0x00050080u, 0x00000006u, | ||
| 80 | 0x0000006eu, 0x00000027u, 0x00000058u, 0x00060041u, 0x00000032u, 0x0000006fu, 0x00000063u, 0x00000031u, | ||
| 81 | 0x0000006eu, 0x0005003eu, 0x0000006fu, 0x0000000cu, 0x00000002u, 0x00000004u, 0x000200f9u, 0x00000037u, | ||
| 82 | 0x000200f8u, 0x00000037u, 0x000200f9u, 0x00000072u, 0x000200f8u, 0x00000072u, 0x000100fdu, 0x00010038u, | ||
| 83 | }; | ||
| 84 | } // namespace leonida | ||
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index 1be77a0ca..562e528a7 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h | |||
| @@ -15,6 +15,7 @@ | |||
| 15 | #include "statistics_sum_spirv.h" | 15 | #include "statistics_sum_spirv.h" |
| 16 | #include "vulkan13_features.h" | 16 | #include "vulkan13_features.h" |
| 17 | #include "vulkan_depth24.h" | 17 | #include "vulkan_depth24.h" |
| 18 | #include "vulkan_indirect_dispatch_count.h" | ||
| 18 | #include "vulkan_scratch.h" | 19 | #include "vulkan_scratch.h" |
| 19 | #include "vulkan_replay.h" | 20 | #include "vulkan_replay.h" |
| 20 | #include "queue_families.h" | 21 | #include "queue_families.h" |
| @@ -941,6 +942,13 @@ public: | |||
| 941 | std::unique_lock lock(resource_mutex_); | 942 | std::unique_lock lock(resource_mutex_); |
| 942 | predicated_copier_ = std::make_unique<PredicatedBufferCopier>(get, device_, limits); | 943 | predicated_copier_ = std::make_unique<PredicatedBufferCopier>(get, device_, limits); |
| 943 | } | 944 | } |
| 945 | // Counts for DISPATCH-only ExecuteIndirect are read by a helper compute shader from GPU memory. | ||
| 946 | // Requires Vulkan bufferDeviceAddress, already enabled for the D3D12 address arena. | ||
| 947 | void configure_counted_dispatch(PFN_vkGetDeviceProcAddr get, const VkPhysicalDeviceMemoryProperties& memory, | ||
| 948 | const VkPhysicalDeviceLimits& limits) { | ||
| 949 | std::unique_lock lock(resource_mutex_); | ||
| 950 | counted_dispatch_ = std::make_unique<IndirectDispatchCounter>(get, device_, memory, limits); | ||
| 951 | } | ||
| 944 | VkDevice device() const { return device_; } | 952 | VkDevice device() const { return device_; } |
| 945 | 953 | ||
| 946 | // Debug markers become labels from here on; call before any queue or list uses the context. Incomplete sets | 954 | // Debug markers become labels from here on; call before any queue or list uses the context. Incomplete sets |
| @@ -1331,6 +1339,7 @@ private: | |||
| 1331 | ReplayFunctions replay_; | 1339 | ReplayFunctions replay_; |
| 1332 | std::unique_ptr<PredicateReducer> predicate_reducer_; | 1340 | std::unique_ptr<PredicateReducer> predicate_reducer_; |
| 1333 | std::unique_ptr<PredicatedBufferCopier> predicated_copier_; | 1341 | std::unique_ptr<PredicatedBufferCopier> predicated_copier_; |
| 1342 | std::unique_ptr<IndirectDispatchCounter> counted_dispatch_; | ||
| 1334 | std::uint32_t predicate_family_ = UINT32_MAX; | 1343 | std::uint32_t predicate_family_ = UINT32_MAX; |
| 1335 | std::uint32_t transfer_only_family_ = UINT32_MAX; | 1344 | std::uint32_t transfer_only_family_ = UINT32_MAX; |
| 1336 | DebugLabelFunctions labels_; | 1345 | DebugLabelFunctions labels_; |
| @@ -1563,6 +1572,7 @@ public: | |||
| 1563 | if (const auto* p = std::get_if<Predication>(&command); p && p->buffer) ++captures; | 1572 | if (const auto* p = std::get_if<Predication>(&command); p && p->buffer) ++captures; |
| 1564 | std::unique_ptr<PredicateReducer::Batch> batch; | 1573 | std::unique_ptr<PredicateReducer::Batch> batch; |
| 1565 | std::unique_ptr<PredicatedBufferCopier::Batch> copy_batch; | 1574 | std::unique_ptr<PredicatedBufferCopier::Batch> copy_batch; |
| 1575 | std::unique_ptr<IndirectDispatchCounter::Batch> counted_batch; | ||
| 1566 | if (captures) { | 1576 | if (captures) { |
| 1567 | if (kind_ == QueueKind::copy || !context_.predicate_reducer_ || family_ != context_.predicate_family_) | 1577 | if (kind_ == QueueKind::copy || !context_.predicate_reducer_ || family_ != context_.predicate_family_) |
| 1568 | return ReplayResult::unsupported; | 1578 | return ReplayResult::unsupported; |
| @@ -1600,6 +1610,19 @@ public: | |||
| 1600 | return context_.scratch_->get(bytes, buffer, offset); | 1610 | return context_.scratch_->get(bytes, buffer, offset); |
| 1601 | }; | 1611 | }; |
| 1602 | } | 1612 | } |
| 1613 | if (kind_ != QueueKind::copy && context_.counted_dispatch_) { | ||
| 1614 | replay.prepare_counted_dispatch = [this, &tracker_lock, &counted_batch]( | ||
| 1615 | VkBuffer args, VkDeviceSize args_offset, VkBuffer count, VkDeviceSize count_offset, | ||
| 1616 | std::uint32_t stride, std::uint32_t maximum, CountedDispatch& out) { | ||
| 1617 | // Pipeline creation is lazy and shared across queues; serialize it with other replay preparation. | ||
| 1618 | if (!tracker_lock.owns_lock()) tracker_lock.lock(); | ||
| 1619 | if (!counted_batch) counted_batch = context_.counted_dispatch_->batch(); | ||
| 1620 | return counted_batch->prepare(args, args_offset, count, count_offset, stride, maximum, out); | ||
| 1621 | }; | ||
| 1622 | replay.emit_counted_dispatch = [this](VkCommandBuffer cb, const CountedDispatch& job) { | ||
| 1623 | context_.counted_dispatch_->emit(cb, job); | ||
| 1624 | }; | ||
| 1625 | } | ||
| 1603 | if (context_.depth24_ && !transfer_only) { | 1626 | if (context_.depth24_ && !transfer_only) { |
| 1604 | replay.depth24_prepare = [this, &tracker_lock, &depth24_guard](Depth24Job& job, | 1627 | replay.depth24_prepare = [this, &tracker_lock, &depth24_guard](Depth24Job& job, |
| 1605 | std::vector<Depth24Job>& parts) { | 1628 | std::vector<Depth24Job>& parts) { |
| @@ -1829,6 +1852,7 @@ public: | |||
| 1829 | context_.tracker_ = std::move(next_tracker); | 1852 | context_.tracker_ = std::move(next_tracker); |
| 1830 | slot->predicate = std::move(batch); | 1853 | slot->predicate = std::move(batch); |
| 1831 | slot->predicated_copy = std::move(copy_batch); | 1854 | slot->predicated_copy = std::move(copy_batch); |
| 1855 | slot->counted_dispatch = std::move(counted_batch); | ||
| 1832 | queries.commit(); | 1856 | queries.commit(); |
| 1833 | slot->segments = std::move(queries.retained); | 1857 | slot->segments = std::move(queries.retained); |
| 1834 | } else { | 1858 | } else { |
| @@ -1889,6 +1913,7 @@ private: | |||
| 1889 | std::vector<std::shared_ptr<const OcclusionSegmentPool>> segments; | 1913 | std::vector<std::shared_ptr<const OcclusionSegmentPool>> segments; |
| 1890 | std::vector<VkEvent> events; // order_timestamp_copy's, reset and reused once the slot completes | 1914 | std::vector<VkEvent> events; // order_timestamp_copy's, reset and reused once the slot completes |
| 1891 | std::unique_ptr<PredicatedBufferCopier::Batch> predicated_copy; | 1915 | std::unique_ptr<PredicatedBufferCopier::Batch> predicated_copy; |
| 1916 | std::unique_ptr<IndirectDispatchCounter::Batch> counted_dispatch; | ||
| 1892 | std::unique_ptr<PredicateReducer::Batch> predicate; // retained until this slot completes | 1917 | std::unique_ptr<PredicateReducer::Batch> predicate; // retained until this slot completes |
| 1893 | }; | 1918 | }; |
| 1894 | 1919 | ||
| @@ -2007,6 +2032,7 @@ private: | |||
| 2007 | if (context_.vk.vkResetCommandBuffer(reuse->buffer, 0) != VK_SUCCESS) return nullptr; | 2032 | if (context_.vk.vkResetCommandBuffer(reuse->buffer, 0) != VK_SUCCESS) return nullptr; |
| 2008 | reuse->predicate.reset(); | 2033 | reuse->predicate.reset(); |
| 2009 | reuse->predicated_copy.reset(); | 2034 | reuse->predicated_copy.reset(); |
| 2035 | reuse->counted_dispatch.reset(); | ||
| 2010 | reuse->ticket = UINT64_MAX; // in use until submitted | 2036 | reuse->ticket = UINT64_MAX; // in use until submitted |
| 2011 | return reuse; | 2037 | return reuse; |
| 2012 | } | 2038 | } |
diff --git a/runtime/src/vulkan_indirect_dispatch_count.h b/runtime/src/vulkan_indirect_dispatch_count.h new file mode 100644 index 000000000..225f7c1fc --- /dev/null +++ b/runtime/src/vulkan_indirect_dispatch_count.h | |||
| @@ -0,0 +1,199 @@ | |||
| 1 | // GPU-only count gating for simple D3D12 ExecuteIndirect(DISPATCH) signatures. | ||
| 2 | // Vulkan has vkCmdDispatchIndirect but no vkCmdDispatchIndirectCount. We prepare a private indirect | ||
| 3 | // buffer with zero-group records past the GPU count, then execute all MaxCommandCount records. | ||
| 4 | #pragma once | ||
| 5 | #ifndef VK_NO_PROTOTYPES | ||
| 6 | #define VK_NO_PROTOTYPES | ||
| 7 | #endif | ||
| 8 | #include <vulkan/vulkan.h> | ||
| 9 | #include <algorithm> | ||
| 10 | #include <cstdint> | ||
| 11 | #include <limits> | ||
| 12 | #include <memory> | ||
| 13 | #include <vector> | ||
| 14 | #include "indirect_dispatch_count_spv.h" | ||
| 15 | |||
| 16 | namespace leonida { | ||
| 17 | |||
| 18 | struct CountedDispatch { | ||
| 19 | VkBuffer indirect = VK_NULL_HANDLE; | ||
| 20 | VkDeviceSize offset = 0; | ||
| 21 | VkDeviceAddress arguments = 0; | ||
| 22 | VkDeviceAddress count = 0; | ||
| 23 | VkDeviceAddress output = 0; | ||
| 24 | std::uint32_t stride = 0; | ||
| 25 | std::uint32_t max_count = 0; | ||
| 26 | }; | ||
| 27 | |||
| 28 | class IndirectDispatchCounter { | ||
| 29 | public: | ||
| 30 | IndirectDispatchCounter(PFN_vkGetDeviceProcAddr get, VkDevice device, | ||
| 31 | const VkPhysicalDeviceMemoryProperties& memory, const VkPhysicalDeviceLimits& limits) | ||
| 32 | : get_(get), device_(device), memory_(memory), limits_(limits) {} | ||
| 33 | ~IndirectDispatchCounter() { | ||
| 34 | if (pipeline_) fn<PFN_vkDestroyPipeline>("vkDestroyPipeline")(device_, pipeline_, nullptr); | ||
| 35 | if (layout_) fn<PFN_vkDestroyPipelineLayout>("vkDestroyPipelineLayout")(device_, layout_, nullptr); | ||
| 36 | } | ||
| 37 | IndirectDispatchCounter(const IndirectDispatchCounter&) = delete; | ||
| 38 | IndirectDispatchCounter& operator=(const IndirectDispatchCounter&) = delete; | ||
| 39 | |||
| 40 | // Retained by the command-buffer slot until its submission's timeline ticket completes. | ||
| 41 | struct Batch { | ||
| 42 | IndirectDispatchCounter& owner; | ||
| 43 | struct Allocation { | ||
| 44 | VkBuffer buffer = VK_NULL_HANDLE; | ||
| 45 | VkDeviceMemory memory = VK_NULL_HANDLE; | ||
| 46 | }; | ||
| 47 | std::vector<Allocation> allocations; | ||
| 48 | explicit Batch(IndirectDispatchCounter& value) : owner(value) {} | ||
| 49 | ~Batch() { | ||
| 50 | for (auto& a : allocations) { | ||
| 51 | if (a.buffer) owner.fn<PFN_vkDestroyBuffer>("vkDestroyBuffer")(owner.device_, a.buffer, nullptr); | ||
| 52 | if (a.memory) owner.fn<PFN_vkFreeMemory>("vkFreeMemory")(owner.device_, a.memory, nullptr); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | Batch(const Batch&) = delete; | ||
| 56 | Batch& operator=(const Batch&) = delete; | ||
| 57 | |||
| 58 | bool prepare(VkBuffer arguments, VkDeviceSize argument_offset, VkBuffer count, | ||
| 59 | VkDeviceSize count_offset, std::uint32_t stride, std::uint32_t max_count, | ||
| 60 | CountedDispatch& out) { | ||
| 61 | out = {}; | ||
| 62 | if (!arguments || !count || !stride || stride % 4 || !max_count || | ||
| 63 | !owner.limits_.maxComputeWorkGroupCount[0] || !owner.make_pipeline()) return false; | ||
| 64 | constexpr VkDeviceSize record_bytes = 3 * sizeof(std::uint32_t); | ||
| 65 | if (VkDeviceSize(max_count) > std::numeric_limits<VkDeviceSize>::max() / record_bytes) | ||
| 66 | return false; | ||
| 67 | const VkDeviceSize bytes = VkDeviceSize(max_count) * record_bytes; | ||
| 68 | VkBufferCreateInfo info{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; | ||
| 69 | info.size = bytes; | ||
| 70 | info.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT | | ||
| 71 | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT; | ||
| 72 | Allocation scratch{}; | ||
| 73 | if (owner.fn<PFN_vkCreateBuffer>("vkCreateBuffer")(owner.device_, &info, nullptr, &scratch.buffer) != VK_SUCCESS) | ||
| 74 | return false; | ||
| 75 | allocations.push_back(scratch); // all failure paths free it with the batch | ||
| 76 | auto& a = allocations.back(); | ||
| 77 | VkMemoryRequirements requirements{}; | ||
| 78 | owner.fn<PFN_vkGetBufferMemoryRequirements>("vkGetBufferMemoryRequirements")( | ||
| 79 | owner.device_, a.buffer, &requirements); | ||
| 80 | std::uint32_t memory_type = UINT32_MAX; | ||
| 81 | for (std::uint32_t i = 0; i < owner.memory_.memoryTypeCount; ++i) { | ||
| 82 | if (!(requirements.memoryTypeBits & (1u << i))) continue; | ||
| 83 | if (memory_type == UINT32_MAX) memory_type = i; | ||
| 84 | if (owner.memory_.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) { | ||
| 85 | memory_type = i; | ||
| 86 | break; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | if (memory_type == UINT32_MAX) return false; | ||
| 90 | VkMemoryAllocateFlagsInfo flags{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO}; | ||
| 91 | flags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT; | ||
| 92 | VkMemoryAllocateInfo allocation{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, &flags}; | ||
| 93 | allocation.allocationSize = requirements.size; | ||
| 94 | allocation.memoryTypeIndex = memory_type; | ||
| 95 | if (owner.fn<PFN_vkAllocateMemory>("vkAllocateMemory")(owner.device_, &allocation, nullptr, &a.memory) != VK_SUCCESS || | ||
| 96 | owner.fn<PFN_vkBindBufferMemory>("vkBindBufferMemory")(owner.device_, a.buffer, a.memory, 0) != VK_SUCCESS) | ||
| 97 | return false; | ||
| 98 | const auto address = owner.fn<PFN_vkGetBufferDeviceAddress>("vkGetBufferDeviceAddress"); | ||
| 99 | VkBufferDeviceAddressInfo addr{VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO}; | ||
| 100 | addr.buffer = arguments; | ||
| 101 | const VkDeviceAddress args_va = address(owner.device_, &addr); | ||
| 102 | addr.buffer = count; | ||
| 103 | const VkDeviceAddress count_va = address(owner.device_, &addr); | ||
| 104 | addr.buffer = a.buffer; | ||
| 105 | const VkDeviceAddress output_va = address(owner.device_, &addr); | ||
| 106 | if (!args_va || !count_va || !output_va || | ||
| 107 | argument_offset > UINT64_MAX - args_va || count_offset > UINT64_MAX - count_va) | ||
| 108 | return false; | ||
| 109 | out = {a.buffer, 0, args_va + argument_offset, count_va + count_offset, | ||
| 110 | output_va, stride, max_count}; | ||
| 111 | return true; | ||
| 112 | } | ||
| 113 | }; | ||
| 114 | |||
| 115 | std::unique_ptr<Batch> batch() { return std::make_unique<Batch>(*this); } | ||
| 116 | |||
| 117 | void emit(VkCommandBuffer cb, const CountedDispatch& job) const { | ||
| 118 | const auto barrier = fn<PFN_vkCmdPipelineBarrier>("vkCmdPipelineBarrier"); | ||
| 119 | VkMemoryBarrier before{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; | ||
| 120 | before.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT; | ||
| 121 | before.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; | ||
| 122 | barrier(cb, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, | ||
| 123 | 0, 1, &before, 0, nullptr, 0, nullptr); | ||
| 124 | fn<PFN_vkCmdBindPipeline>("vkCmdBindPipeline")(cb, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline_); | ||
| 125 | struct Push { | ||
| 126 | VkDeviceAddress arguments, count, output; | ||
| 127 | std::uint32_t stride_words, first_record, records; | ||
| 128 | }; | ||
| 129 | static_assert(sizeof(Push) == 40); | ||
| 130 | const VkDeviceSize dispatch_limit = VkDeviceSize(limits_.maxComputeWorkGroupCount[0]) * 64; | ||
| 131 | // Bound per-dispatch indexing to 32 bits, even when the D3D12 stride is very large. | ||
| 132 | // The shader loads three consecutive dwords, so leave room for the final +2. | ||
| 133 | const VkDeviceSize index_limit = (UINT32_MAX - 2u) / (job.stride / 4); | ||
| 134 | const VkDeviceSize chunk_limit = std::min(dispatch_limit, index_limit + 1); | ||
| 135 | for (VkDeviceSize first = 0; first < job.max_count;) { | ||
| 136 | const std::uint32_t count = std::uint32_t(std::min<VkDeviceSize>(chunk_limit, job.max_count - first)); | ||
| 137 | const Push push{job.arguments + first * job.stride, job.count, job.output + first * 12, | ||
| 138 | job.stride / 4, std::uint32_t(first), count}; | ||
| 139 | fn<PFN_vkCmdPushConstants>("vkCmdPushConstants")(cb, layout_, VK_SHADER_STAGE_COMPUTE_BIT, | ||
| 140 | 0, sizeof(push), &push); | ||
| 141 | fn<PFN_vkCmdDispatch>("vkCmdDispatch")(cb, (count + 63u) / 64u, 1, 1); | ||
| 142 | first += count; | ||
| 143 | } | ||
| 144 | VkMemoryBarrier after{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; | ||
| 145 | after.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT | VK_ACCESS_SHADER_READ_BIT; | ||
| 146 | after.dstAccessMask = VK_ACCESS_INDIRECT_COMMAND_READ_BIT | VK_ACCESS_MEMORY_READ_BIT | | ||
| 147 | VK_ACCESS_MEMORY_WRITE_BIT; | ||
| 148 | barrier(cb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, | ||
| 149 | 0, 1, &after, 0, nullptr, 0, nullptr); | ||
| 150 | } | ||
| 151 | |||
| 152 | private: | ||
| 153 | template<typename T> T fn(const char* name) const { return reinterpret_cast<T>(get_(device_, name)); } | ||
| 154 | bool make_pipeline() { | ||
| 155 | if (pipeline_) return true; | ||
| 156 | for (const char* name : {"vkCreateBuffer", "vkDestroyBuffer", "vkGetBufferMemoryRequirements", | ||
| 157 | "vkAllocateMemory", "vkFreeMemory", "vkBindBufferMemory", "vkGetBufferDeviceAddress", | ||
| 158 | "vkCreateShaderModule", "vkDestroyShaderModule", "vkCreatePipelineLayout", | ||
| 159 | "vkDestroyPipelineLayout", "vkCreateComputePipelines", "vkDestroyPipeline", | ||
| 160 | "vkCmdPipelineBarrier", "vkCmdBindPipeline", "vkCmdPushConstants", "vkCmdDispatch"}) | ||
| 161 | if (!get_(device_, name)) return false; | ||
| 162 | VkShaderModule module = VK_NULL_HANDLE; | ||
| 163 | VkPipelineLayout layout = VK_NULL_HANDLE; | ||
| 164 | VkPipeline pipeline = VK_NULL_HANDLE; | ||
| 165 | VkShaderModuleCreateInfo shader{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO}; | ||
| 166 | shader.codeSize = sizeof(indirect_dispatch_count_spirv); | ||
| 167 | shader.pCode = indirect_dispatch_count_spirv; | ||
| 168 | bool ok = fn<PFN_vkCreateShaderModule>("vkCreateShaderModule")(device_, &shader, nullptr, &module) == VK_SUCCESS; | ||
| 169 | const VkPushConstantRange range{VK_SHADER_STAGE_COMPUTE_BIT, 0, 40}; | ||
| 170 | VkPipelineLayoutCreateInfo layout_info{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO}; | ||
| 171 | layout_info.pushConstantRangeCount = 1; | ||
| 172 | layout_info.pPushConstantRanges = ⦥ | ||
| 173 | ok = ok && fn<PFN_vkCreatePipelineLayout>("vkCreatePipelineLayout")(device_, &layout_info, nullptr, &layout) == VK_SUCCESS; | ||
| 174 | VkComputePipelineCreateInfo info{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO}; | ||
| 175 | info.stage = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO}; | ||
| 176 | info.stage.stage = VK_SHADER_STAGE_COMPUTE_BIT; | ||
| 177 | info.stage.module = module; | ||
| 178 | info.stage.pName = "main"; | ||
| 179 | info.layout = layout; | ||
| 180 | ok = ok && fn<PFN_vkCreateComputePipelines>("vkCreateComputePipelines")( | ||
| 181 | device_, VK_NULL_HANDLE, 1, &info, nullptr, &pipeline) == VK_SUCCESS; | ||
| 182 | if (module) fn<PFN_vkDestroyShaderModule>("vkDestroyShaderModule")(device_, module, nullptr); | ||
| 183 | if (!ok) { | ||
| 184 | if (pipeline) fn<PFN_vkDestroyPipeline>("vkDestroyPipeline")(device_, pipeline, nullptr); | ||
| 185 | if (layout) fn<PFN_vkDestroyPipelineLayout>("vkDestroyPipelineLayout")(device_, layout, nullptr); | ||
| 186 | return false; | ||
| 187 | } | ||
| 188 | layout_ = layout; | ||
| 189 | pipeline_ = pipeline; | ||
| 190 | return true; | ||
| 191 | } | ||
| 192 | PFN_vkGetDeviceProcAddr get_; | ||
| 193 | VkDevice device_; | ||
| 194 | VkPhysicalDeviceMemoryProperties memory_; | ||
| 195 | VkPhysicalDeviceLimits limits_; | ||
| 196 | VkPipelineLayout layout_ = VK_NULL_HANDLE; | ||
| 197 | VkPipeline pipeline_ = VK_NULL_HANDLE; | ||
| 198 | }; | ||
| 199 | } // namespace leonida | ||
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 6bc59f326..a85a476ff 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h | |||
| @@ -37,6 +37,7 @@ | |||
| 37 | #include <vulkan/vulkan.h> | 37 | #include <vulkan/vulkan.h> |
| 38 | #include "vulkan_predicate.h" | 38 | #include "vulkan_predicate.h" |
| 39 | #include "vulkan_predicated_copy.h" | 39 | #include "vulkan_predicated_copy.h" |
| 40 | #include "vulkan_indirect_dispatch_count.h" | ||
| 40 | 41 | ||
| 41 | #include <algorithm> | 42 | #include <algorithm> |
| 42 | #include <array> | 43 | #include <array> |
| @@ -845,6 +846,11 @@ struct ReplayFunctions { | |||
| 845 | PFN_vkCmdDrawIndirectCount draw_indirect_count = nullptr; | 846 | PFN_vkCmdDrawIndirectCount draw_indirect_count = nullptr; |
| 846 | PFN_vkCmdDrawIndexedIndirectCount draw_indexed_indirect_count = nullptr; | 847 | PFN_vkCmdDrawIndexedIndirectCount draw_indexed_indirect_count = nullptr; |
| 847 | PFN_vkCmdDispatchIndirect dispatch_indirect = nullptr; | 848 | PFN_vkCmdDispatchIndirect dispatch_indirect = nullptr; |
| 849 | // A DISPATCH-only ExecuteIndirect signature with a GPU count buffer needs a compute prepass to clamp | ||
| 850 | // the number of commands. The resulting scratch buffer is owned by the queue's timeline-retained batch. | ||
| 851 | std::function<bool(VkBuffer, VkDeviceSize, VkBuffer, VkDeviceSize, std::uint32_t, std::uint32_t, | ||
| 852 | CountedDispatch&)> prepare_counted_dispatch; | ||
| 853 | std::function<void(VkCommandBuffer, const CountedDispatch&)> emit_counted_dispatch; | ||
| 848 | // Optional VK_KHR_acceleration_structure: BuildRaytracingAccelerationStructure. | 854 | // Optional VK_KHR_acceleration_structure: BuildRaytracingAccelerationStructure. |
| 849 | PFN_vkCmdBuildAccelerationStructuresKHR build_acceleration_structures = nullptr; | 855 | PFN_vkCmdBuildAccelerationStructuresKHR build_acceleration_structures = nullptr; |
| 850 | // XDXR BVH4 builds (xdxr_build.h), used instead of the KHR ones when set: `xdxr_check` refuses inputs the builder | 856 | // XDXR BVH4 builds (xdxr_build.h), used instead of the KHR ones when set: `xdxr_check` refuses inputs the builder |
| @@ -926,6 +932,7 @@ struct Prepared { | |||
| 926 | std::uint32_t statistics_begin = PreparedQuery::no_segment; | 932 | std::uint32_t statistics_begin = PreparedQuery::no_segment; |
| 927 | PredicateCapture predicate; | 933 | PredicateCapture predicate; |
| 928 | std::vector<PredicatedCopyDispatch> predicated_copy; | 934 | std::vector<PredicatedCopyDispatch> predicated_copy; |
| 935 | CountedDispatch counted_dispatch; | ||
| 929 | ResourceBinding binding; | 936 | ResourceBinding binding; |
| 930 | VkImageSubresourceRange range{}; // the barrier range, or the cleared view (cleared aspects only) | 937 | VkImageSubresourceRange range{}; // the barrier range, or the cleared view (cleared aspects only) |
| 931 | VkImageView view = VK_NULL_HANDLE; | 938 | VkImageView view = VK_NULL_HANDLE; |
| @@ -2114,7 +2121,8 @@ inline void plan_statistics_segments(PreparedList& out) { | |||
| 2114 | std::size_t next_query = 0; | 2121 | std::size_t next_query = 0; |
| 2115 | for (std::size_t i = 0; i < out.commands.size(); ++i) { | 2122 | for (std::size_t i = 0; i < out.commands.size(); ++i) { |
| 2116 | auto& p = out.commands[i]; | 2123 | auto& p = out.commands[i]; |
| 2117 | if (begin && (p.predicate.scratch || !p.predicated_copy.empty() || p.has_depth24)) | 2124 | if (begin && (p.predicate.scratch || !p.predicated_copy.empty() || p.has_depth24 || |
| 2125 | p.counted_dispatch.indirect)) | ||
| 2118 | inner.push_back({&p.statistics_end, &p.statistics_begin}); | 2126 | inner.push_back({&p.statistics_end, &p.statistics_begin}); |
| 2119 | if (next_query == out.queries.size() || out.queries[next_query].command != i) continue; | 2127 | if (next_query == out.queries.size() || out.queries[next_query].command != i) continue; |
| 2120 | auto& query = out.queries[next_query++]; | 2128 | auto& query = out.queries[next_query++]; |
| @@ -2163,7 +2171,8 @@ inline ReplayResult plan_overlapping_statistics_segments(PreparedList& out) { | |||
| 2163 | if (!query) { | 2171 | if (!query) { |
| 2164 | // A helper dispatch while a query runs would be counted into it (D3D-162): close the shared physical | 2172 | // A helper dispatch while a query runs would be counted into it (D3D-162): close the shared physical |
| 2165 | // segment before it and open a new one after, the way resolves do. | 2173 | // segment before it and open a new one after, the way resolves do. |
| 2166 | if (!active.empty() && (p.predicate.scratch || !p.predicated_copy.empty() || p.has_depth24)) { | 2174 | if (!active.empty() && (p.predicate.scratch || !p.predicated_copy.empty() || p.has_depth24 || |
| 2175 | p.counted_dispatch.indirect)) { | ||
| 2167 | p.statistics_end = current; | 2176 | p.statistics_end = current; |
| 2168 | current = out.statistics_segment_count++; | 2177 | current = out.statistics_segment_count++; |
| 2169 | p.statistics_begin = current; | 2178 | p.statistics_begin = current; |
| @@ -2685,10 +2694,19 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun | |||
| 2685 | if (!detail::indirect_range(*cnt, ind->count_offset, sizeof(std::uint32_t))) | 2694 | if (!detail::indirect_range(*cnt, ind->count_offset, sizeof(std::uint32_t))) |
| 2686 | return ReplayResult::invalid_indirect_arguments; | 2695 | return ReplayResult::invalid_indirect_arguments; |
| 2687 | p.source = *cnt; | 2696 | p.source = *cnt; |
| 2688 | if ((ind->kind == IndirectKind::draw && !vk.draw_indirect_count) || | 2697 | if (ind->kind == IndirectKind::dispatch) { |
| 2689 | (ind->kind == IndirectKind::draw_indexed && !vk.draw_indexed_indirect_count) || | 2698 | // Vulkan lacks vkCmdDispatchIndirectCount. Copy the GPU-visible commands to private scratch |
| 2690 | ind->kind == IndirectKind::dispatch) | 2699 | // and replace commands past min(count, max_count) with zero-group dispatches. |
| 2700 | if (!ind->pipeline || !vk.dispatch_indirect || !vk.prepare_counted_dispatch || | ||
| 2701 | !vk.emit_counted_dispatch || (statistics_active && !statistics_helpers) || | ||
| 2702 | !vk.prepare_counted_dispatch(p.binding.buffer, p.binding.buffer_offset + ind->argument_offset, | ||
| 2703 | p.source.buffer, p.source.buffer_offset + ind->count_offset, | ||
| 2704 | ind->stride, ind->max_count, p.counted_dispatch)) | ||
| 2705 | return ReplayResult::unsupported; | ||
| 2706 | } else if ((ind->kind == IndirectKind::draw && !vk.draw_indirect_count) || | ||
| 2707 | (ind->kind == IndirectKind::draw_indexed && !vk.draw_indexed_indirect_count)) { | ||
| 2691 | return ReplayResult::unsupported; | 2708 | return ReplayResult::unsupported; |
| 2709 | } | ||
| 2692 | } else { | 2710 | } else { |
| 2693 | if ((ind->kind == IndirectKind::draw && !vk.draw_indirect) || | 2711 | if ((ind->kind == IndirectKind::draw && !vk.draw_indirect) || |
| 2694 | (ind->kind == IndirectKind::draw_indexed && !vk.draw_indexed_indirect) || | 2712 | (ind->kind == IndirectKind::draw_indexed && !vk.draw_indexed_indirect) || |
| @@ -3500,7 +3518,7 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 3500 | vk.copy_buffer(command_buffer, p.source.buffer, p.binding.buffer, 1, p.buffer_copies.data()); | 3518 | vk.copy_buffer(command_buffer, p.source.buffer, p.binding.buffer, 1, p.buffer_copies.data()); |
| 3501 | } else if (const auto* ind = std::get_if<ExecuteIndirect>(&command)) { | 3519 | } else if (const auto* ind = std::get_if<ExecuteIndirect>(&command)) { |
| 3502 | const VkDeviceSize arguments = p.binding.buffer_offset + ind->argument_offset; | 3520 | const VkDeviceSize arguments = p.binding.buffer_offset + ind->argument_offset; |
| 3503 | if (ind->count_buffer) { | 3521 | if (ind->count_buffer && ind->kind != IndirectKind::dispatch) { |
| 3504 | const VkDeviceSize count = p.source.buffer_offset + ind->count_offset; | 3522 | const VkDeviceSize count = p.source.buffer_offset + ind->count_offset; |
| 3505 | if (ind->kind == IndirectKind::draw) { | 3523 | if (ind->kind == IndirectKind::draw) { |
| 3506 | vk.draw_indirect_count(command_buffer, p.binding.buffer, arguments, p.source.buffer, | 3524 | vk.draw_indirect_count(command_buffer, p.binding.buffer, arguments, p.source.buffer, |
| @@ -3515,6 +3533,12 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 3515 | } else if (ind->kind == IndirectKind::draw_indexed) { | 3533 | } else if (ind->kind == IndirectKind::draw_indexed) { |
| 3516 | vk.draw_indexed_indirect(command_buffer, p.binding.buffer, arguments, ind->max_count, ind->stride); | 3534 | vk.draw_indexed_indirect(command_buffer, p.binding.buffer, arguments, ind->max_count, ind->stride); |
| 3517 | } else if (ind->kind == IndirectKind::dispatch) { | 3535 | } else if (ind->kind == IndirectKind::dispatch) { |
| 3536 | if (ind->count_buffer) { | ||
| 3537 | // No D3D12 query must count this internal compute pass as an application dispatch. | ||
| 3538 | end_statistics(); | ||
| 3539 | vk.emit_counted_dispatch(command_buffer, p.counted_dispatch); | ||
| 3540 | begin_statistics(); | ||
| 3541 | } | ||
| 3518 | if (ind->pipeline) { | 3542 | if (ind->pipeline) { |
| 3519 | ShaderUserData data{}; | 3543 | ShaderUserData data{}; |
| 3520 | std::copy(ind->user_data.begin(), ind->user_data.end(), data.s); | 3544 | std::copy(ind->user_data.begin(), ind->user_data.end(), data.s); |
| @@ -3528,8 +3552,10 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 3528 | &data); | 3552 | &data); |
| 3529 | } | 3553 | } |
| 3530 | for (std::uint32_t k = 0; k < ind->max_count; ++k) { | 3554 | for (std::uint32_t k = 0; k < ind->max_count; ++k) { |
| 3531 | vk.dispatch_indirect(command_buffer, p.binding.buffer, | 3555 | vk.dispatch_indirect(command_buffer, |
| 3532 | arguments + VkDeviceSize(k) * ind->stride); | 3556 | ind->count_buffer ? p.counted_dispatch.indirect : p.binding.buffer, |
| 3557 | (ind->count_buffer ? p.counted_dispatch.offset + VkDeviceSize(k) * 12 | ||
| 3558 | : arguments + VkDeviceSize(k) * ind->stride)); | ||
| 3533 | } | 3559 | } |
| 3534 | } | 3560 | } |
| 3535 | } | 3561 | } |