diff --git a/runtime/src/vulkan_indirect_dispatch_count.h b/runtime/src/vulkan_indirect_dispatch_count.h index 8e0f7d4aa..01cd01ec2 100644 --- a/runtime/src/vulkan_indirect_dispatch_count.h +++ b/runtime/src/vulkan_indirect_dispatch_count.h @@ -80,6 +80,9 @@ class IndirectDispatchCounter { CountedDispatch& out) { out = {}; constexpr VkDeviceSize record_bytes = 3 * sizeof(std::uint32_t); + // Without a count buffer (LEONIDA_INDIRECT_GROUP_BUDGET only) `count_offset` means nothing: it is neither + // checked nor added to an address, and the job has no count (as IndirectArgumentClamp::prepare). + if (!count) count_offset = 0; if (!arguments || (!count && !indirect_group_budget()) || stride < record_bytes || stride % 4 || (argument_offset % 4) != 0 || (count_offset % 4) != 0 || !max_count || !owner.limits_.maxComputeWorkGroupCount[0] || !owner.make_pipeline()) return false; diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 4f3c47654..df3b3dfd0 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -4145,7 +4145,9 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun if (!ind->pipeline || !vk.dispatch_indirect || !vk.prepare_counted_dispatch || !vk.emit_counted_dispatch || (statistics_active && !statistics_helpers) || !vk.prepare_counted_dispatch(p.binding.buffer, p.binding.buffer_offset + ind->argument_offset, - p.source.buffer, p.source.buffer_offset + ind->count_offset, + ind->count_buffer ? p.source.buffer : VK_NULL_HANDLE, + ind->count_buffer ? p.source.buffer_offset + ind->count_offset + : 0, ind->stride, ind->max_count, p.counted_dispatch)) return unsupported_at(__LINE__); } else if (!vk.dispatch_indirect) { diff --git a/runtime/test/indirect_dispatch_count_alloc_test.cpp b/runtime/test/indirect_dispatch_count_alloc_test.cpp index 9e3237792..4e9620967 100644 --- a/runtime/test/indirect_dispatch_count_alloc_test.cpp +++ b/runtime/test/indirect_dispatch_count_alloc_test.cpp @@ -2,7 +2,9 @@ // - skips undersized memory heaps (VUID-vkAllocateMemory-pAllocateInfo-01713) via memory_type_order; // - falls back to subsequent compatible memory types on VK_ERROR_OUT_OF_DEVICE_MEMORY; // - rejects stride < 12 (4 or 8) and non-4-byte-aligned argument/count offsets; -// - chunks emit() dispatches cleanly without zero-group or 32-bit workgroup overflow. +// - chunks emit() dispatches cleanly without zero-group or 32-bit workgroup overflow; +// - with LEONIDA_INDIRECT_GROUP_BUDGET and no count buffer, ignores the count offset: no alignment check, no count +// address and has_count 0 in emit()'s push constants (the test sets the budget to 4 before anything reads it). // No GPU, Vulkan loader, or game files required. #ifndef VK_NO_PROTOTYPES #define VK_NO_PROTOTYPES @@ -11,6 +13,7 @@ #include #include +#include #include #include #include @@ -31,6 +34,8 @@ struct MockState { std::vector> alloc_attempts; std::vector dispatch_groups_x; std::vector push_records; + std::vector push_counts; // the count address of each push + std::vector push_has_count; }; MockState* g_mock = nullptr; @@ -119,6 +124,12 @@ VKAPI_ATTR void VKAPI_CALL mock_cmd_push_constants(VkCommandBuffer, VkPipelineLa std::uint32_t records = 0; std::memcpy(&records, static_cast(values) + 32, sizeof(records)); g_mock->push_records.push_back(records); + std::uint64_t count = 0; + std::uint32_t has_count = 0; + std::memcpy(&count, static_cast(values) + 8, sizeof(count)); + std::memcpy(&has_count, static_cast(values) + 40, sizeof(has_count)); + g_mock->push_counts.push_back(count); + g_mock->push_has_count.push_back(has_count); } } @@ -165,6 +176,12 @@ VkPhysicalDeviceLimits make_limits(std::uint32_t max_wg_x = 65535u) { int main() { using namespace leonida; + // indirect_group_budget() reads this once, on its first call; set it before any. +#ifdef _WIN32 + _putenv_s("LEONIDA_INDIRECT_GROUP_BUDGET", "4"); +#else + setenv("LEONIDA_INDIRECT_GROUP_BUDGET", "4", 1); +#endif const VkDevice dummy_device = make_handle(1); const VkBuffer args_buf = make_handle(100); const VkBuffer count_buf = make_handle(200); @@ -280,6 +297,40 @@ int main() { CHECK(state.dispatch_groups_x[0] == 2u && state.dispatch_groups_x[1] == 1u); } + // 6. LEONIDA_INDIRECT_GROUP_BUDGET with no count buffer: the count offset is not used. An unaligned one is not + // refused, a large one does not become a count address (0x40 from a null buffer), and emit() pushes no count + // (has_count 0), so the shader never reads one. With a count buffer the offset is still checked and added. + { + CHECK(indirect_group_budget() == 4u); + MockState state; + g_mock = &state; + VkPhysicalDeviceMemoryProperties mem{}; + mem.memoryHeapCount = 1; + mem.memoryHeaps[0] = {VkDeviceSize(1) << 30, VK_MEMORY_HEAP_DEVICE_LOCAL_BIT}; + mem.memoryTypeCount = 1; + mem.memoryTypes[0] = {VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, 0}; + + IndirectDispatchCounter counter(mock_get_device_proc_addr, dummy_device, mem, make_limits()); + auto batch = counter.batch(); + CountedDispatch out{}; + CHECK(batch->prepare(args_buf, 0, VK_NULL_HANDLE, 2, 12, 4u, out)); + CHECK(out.count == 0); + CHECK(batch->prepare(args_buf, 0, VK_NULL_HANDLE, 64, 12, 4u, out)); + CHECK(out.count == 0); + CHECK(batch->prepare(args_buf, 0, VK_NULL_HANDLE, ~VkDeviceSize{3}, 12, 4u, out)); + CHECK(out.count == 0); + counter.emit(dummy_cb, out); + CHECK(state.push_counts.size() == 1 && state.push_counts[0] == 0 && state.push_has_count[0] == 0); + + CHECK(!batch->prepare(args_buf, 0, count_buf, 2, 12, 4u, out)); + CHECK(batch->prepare(args_buf, 0, count_buf, 64, 12, 4u, out)); + CHECK(out.count == 0x10000000ull + 200 * 16 + 64); // the mock's address of count_buf, plus the offset + state.push_counts.clear(); + state.push_has_count.clear(); + counter.emit(dummy_cb, out); + CHECK(state.push_counts.size() == 1 && state.push_counts[0] == out.count && state.push_has_count[0] == 1); + } + std::printf("indirect_dispatch_count_alloc_test: all checks passed\n"); return 0; }