Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions runtime/src/vulkan_indirect_dispatch_count.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion runtime/src/vulkan_replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
53 changes: 52 additions & 1 deletion runtime/test/indirect_dispatch_count_alloc_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -11,6 +13,7 @@

#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <vector>
Expand All @@ -31,6 +34,8 @@ struct MockState {
std::vector<std::pair<std::uint32_t, VkDeviceSize>> alloc_attempts;
std::vector<std::uint32_t> dispatch_groups_x;
std::vector<std::uint32_t> push_records;
std::vector<std::uint64_t> push_counts; // the count address of each push
std::vector<std::uint32_t> push_has_count;
};

MockState* g_mock = nullptr;
Expand Down Expand Up @@ -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<const std::uint8_t*>(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<const std::uint8_t*>(values) + 8, sizeof(count));
std::memcpy(&has_count, static_cast<const std::uint8_t*>(values) + 40, sizeof(has_count));
g_mock->push_counts.push_back(count);
g_mock->push_has_count.push_back(has_count);
}
}

Expand Down Expand Up @@ -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<VkDevice>(1);
const VkBuffer args_buf = make_handle<VkBuffer>(100);
const VkBuffer count_buf = make_handle<VkBuffer>(200);
Expand Down Expand Up @@ -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;
}
Loading