Skip to content

Commit

Permalink
add draw_indirect
Browse files Browse the repository at this point in the history
  • Loading branch information
Hatrickek authored and martty committed May 9, 2024
1 parent 3672d3a commit 780ef64
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
35 changes: 35 additions & 0 deletions include/vuk/runtime/CommandBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,32 @@ namespace vuk {
fixed_vector<FormatOrIgnore, VUK_MAX_ATTRIBUTES> list;
};

struct DrawIndirectCommand {
uint32_t vertexCount = {};
uint32_t instanceCount = {};
uint32_t firstVertex = {};
uint32_t firstInstance = {};

operator VkDrawIndirectCommand const&() const noexcept {
return *reinterpret_cast<const VkDrawIndirectCommand*>(this);
}

operator VkDrawIndirectCommand&() noexcept {
return *reinterpret_cast<VkDrawIndirectCommand*>(this);
}

bool operator==(VkDrawIndirectCommand const& rhs) const noexcept {
return (vertexCount == rhs.vertexCount) && (instanceCount == rhs.instanceCount) && (firstVertex == rhs.firstVertex) &&
(firstInstance == rhs.firstInstance);
}

bool operator!=(VkDrawIndirectCommand const& rhs) const noexcept {
return !operator==(rhs);
}
};
static_assert(sizeof(DrawIndirectCommand) == sizeof(VkDrawIndirectCommand), "struct and wrapper have different size!");
static_assert(std::is_standard_layout_v<DrawIndirectCommand>, "struct wrapper is not a standard layout!");

struct DrawIndexedIndirectCommand {
uint32_t indexCount = {};
uint32_t instanceCount = {};
Expand Down Expand Up @@ -472,6 +498,15 @@ namespace vuk {
/// @param first_vertex Index of the first vertex to draw
/// @param first_instance Index of the first instance to draw
CommandBuffer& draw(size_t vertex_count, size_t instance_count, size_t first_vertex, size_t first_instance);

/// @brief Issue an indirect draw
/// @param command_count Number of indirect commands to be used
/// @param indirect_buffer Buffer of indirect commands
CommandBuffer& draw_indirect(size_t command_count, const Buffer& indirect_buffer);
/// @brief Issue an indirect draw
/// @param commands Indirect commands to be uploaded and used for this draw
CommandBuffer& draw_indirect(std::span<DrawIndirectCommand> commands);

/// @brief Isuse an indexed draw
/// @param index_count Number of vertices to draw
/// @param instance_count Number of instances to draw
Expand Down
1 change: 1 addition & 0 deletions include/vuk/runtime/vk/VkPFNRequired.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ VUK_X(vkCmdResolveImage)
VUK_X(vkCmdPipelineBarrier)
VUK_X(vkCmdWriteTimestamp)
VUK_X(vkCmdDraw)
VUK_X(vkCmdDrawIndirect)
VUK_X(vkCmdDrawIndexed)
VUK_X(vkCmdDrawIndexedIndirect)
VUK_X(vkCmdDispatch)
Expand Down
27 changes: 27 additions & 0 deletions src/runtime/vk/CommandBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,33 @@ namespace vuk {
return *this;
}

CommandBuffer& CommandBuffer::draw_indirect(size_t command_count, const Buffer& indirect_buffer) {
VUK_EARLY_RET();
if (!_bind_graphics_pipeline_state()) {
return *this;
}
ctx.vkCmdDrawIndirect(command_buffer, indirect_buffer.buffer, (uint32_t)indirect_buffer.offset, (uint32_t)command_count, sizeof(DrawIndirectCommand));
return *this;
}

CommandBuffer& CommandBuffer::draw_indirect(std::span<DrawIndirectCommand> commands) {
VUK_EARLY_RET();
if (!_bind_graphics_pipeline_state()) {
return *this;
}

auto res = allocate_buffer(*allocator, { MemoryUsage::eCPUtoGPU, commands.size_bytes(), 1 });
if (!res) {
current_error = std::move(res);
return *this;
}

auto& buf = *res;
memcpy(buf->mapped_ptr, commands.data(), commands.size_bytes());
ctx.vkCmdDrawIndirect(command_buffer, buf->buffer, (uint32_t)buf->offset, (uint32_t)commands.size(), sizeof(DrawIndirectCommand));
return *this;
}

CommandBuffer& CommandBuffer::draw_indexed(size_t index_count, size_t instance_count, size_t first_index, int32_t vertex_offset, size_t first_instance) {
VUK_EARLY_RET();
if (!_bind_graphics_pipeline_state()) {
Expand Down

0 comments on commit 780ef64

Please sign in to comment.