Skip to content
2 changes: 1 addition & 1 deletion backends/vulkan/runtime/api/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ void Context::submit_cmd_to_gpu(VkFence fence_handle, const bool final_use) {
}

void Context::flush() {
VK_CHECK(vkQueueWaitIdle(queue()));
VK_CHECK(vkQueueWaitIdle(queue().handle));

command_pool_.flush();
descriptor_pool_.flush();
Expand Down
8 changes: 6 additions & 2 deletions backends/vulkan/runtime/api/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ class Context final {
return device_;
}

inline VkQueue queue() {
return queue_.handle;
inline vkapi::Adapter::Queue& queue() {
return queue_;
}

// Device Caches
Expand Down Expand Up @@ -230,6 +230,10 @@ class Context final {
VkFence fence_handle = VK_NULL_HANDLE,
const bool final_use = false);

vkapi::CommandBuffer& extract_cmd() {
return cmd_;
}

void flush();

#ifdef VULKAN_DEBUG
Expand Down
33 changes: 29 additions & 4 deletions backends/vulkan/runtime/graph/ComputeGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ ComputeGraph::~ComputeGraph() {

prepack_nodes_.clear();
execute_nodes_.clear();
deferred_cmd_list_.clear();

context_->flush();
}
Expand Down Expand Up @@ -767,6 +768,30 @@ void ComputeGraph::submit_current_cmd_and_wait(const bool final_use) {
context_->fences().return_fence(fence);
}

void ComputeGraph::submit_deferred_cmds() {
VkSemaphore prev_semaphore = VK_NULL_HANDLE;
vkapi::VulkanFence fence = context_->fences().get_fence();

for (uint32_t i = 0; i < deferred_cmd_list_.size(); i++) {
auto& cmd = deferred_cmd_list_[i];
VkSemaphore wait_semaphore = prev_semaphore;
VkSemaphore signal_semaphore = cmd.get_signal_semaphore();
prev_semaphore = signal_semaphore;

if (cmd) {
cmd.end();
context_->adapter_ptr()->submit_cmd(
context_->queue(),
cmd.get_submit_handle(false),
i == (deferred_cmd_list_.size() - 1) ? fence.get_submit_handle() : VK_NULL_HANDLE,
wait_semaphore,
signal_semaphore);
}
}
fence.wait();
context_->fences().return_fence(fence);
}

void ComputeGraph::prepack() {
int i = 0;
bool submitted = false;
Expand Down Expand Up @@ -805,6 +830,7 @@ void ComputeGraph::prepack() {
}

void ComputeGraph::encode_execute() {
deferred_cmd_list_.clear();
context_->flush();
context_->set_cmd(/*reusable = */ true);

Expand All @@ -813,13 +839,12 @@ void ComputeGraph::encode_execute() {
for (std::unique_ptr<ExecuteNode>& node : execute_nodes_) {
node->encode(this);
}

deferred_cmd_list_.emplace_back(std::move(context_->extract_cmd()));
}

void ComputeGraph::execute() {
vkapi::VulkanFence fence = context_->fences().get_fence();
context_->submit_cmd_to_gpu(fence.get_submit_handle());
fence.wait();
context_->fences().return_fence(fence);
submit_deferred_cmds();
execute_count_++;
}

Expand Down
8 changes: 8 additions & 0 deletions backends/vulkan/runtime/graph/ComputeGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ class ComputeGraph final {
// Utility constexpr to express byte quantities
constexpr static size_t MB = 1024 * 1024;

// List of command buffers deferred for submission
std::vector<vkapi::CommandBuffer> deferred_cmd_list_;

protected:
size_t values_in_use_ = 0;
size_t execute_count_ = 0;
Expand Down Expand Up @@ -851,6 +854,11 @@ class ComputeGraph final {
*/
void submit_current_cmd_and_wait(const bool final_use = false);

/*
* Submits all the commands gathered in deferred_cmd_bufs_ to the GPU.
*/
void submit_deferred_cmds();

public:
//
// Graph Prepacking
Expand Down
Loading