Skip to content

Commit

Permalink
[render-graph] Batch submit infos and wait for last frame to finish
Browse files Browse the repository at this point in the history
  • Loading branch information
yeetari committed Jun 17, 2021
1 parent 2793f4a commit e67b02b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
4 changes: 3 additions & 1 deletion include/inexor/vulkan-renderer/render_graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,9 @@ class RenderGraph {
/// @param wait_semaphore The semaphore to wait on before rendering, typically some kind of "swapchain image
/// available" semaphore
/// @param graphics_queue The graphics queue to push rendering commands to
VkSemaphore render(std::uint32_t image_index, VkSemaphore wait_semaphore, VkQueue graphics_queue);
/// @param signal_fence The fence to signal on completion of the whole frame
VkSemaphore render(std::uint32_t image_index, VkSemaphore wait_semaphore, VkQueue graphics_queue,
VkFence signal_fence);
};

template <typename T>
Expand Down
1 change: 1 addition & 0 deletions include/inexor/vulkan-renderer/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class VulkanRenderer {
std::unique_ptr<wrapper::Swapchain> m_swapchain;
std::unique_ptr<wrapper::CommandPool> m_command_pool;
std::unique_ptr<ImGUIOverlay> m_imgui_overlay;
std::unique_ptr<wrapper::Fence> m_frame_finished_fence;
std::unique_ptr<wrapper::Semaphore> m_image_available_semaphore;
std::unique_ptr<RenderGraph> m_render_graph;

Expand Down
19 changes: 13 additions & 6 deletions src/vulkan-renderer/render_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,8 @@ void RenderGraph::compile(const RenderResource *target) {
}
}

VkSemaphore RenderGraph::render(std::uint32_t image_index, VkSemaphore wait_semaphore, VkQueue graphics_queue) {
VkSemaphore RenderGraph::render(std::uint32_t image_index, VkSemaphore wait_semaphore, VkQueue graphics_queue,
VkFence signal_fence) {
// Update dynamic buffers.
for (auto &buffer_resource : m_buffer_resources) {
if (buffer_resource->m_data_upload_needed) {
Expand Down Expand Up @@ -540,15 +541,21 @@ VkSemaphore RenderGraph::render(std::uint32_t image_index, VkSemaphore wait_sema
};
submit_info.pWaitDstStageMask = wait_stage_mask.data();

// TODO: Batch submit infos.
std::vector<VkSemaphore> wait_semaphores;
for (const auto *stage : m_stage_stack) {
const auto &physical = *stage->m_physical;
wait_semaphores.push_back(wait_semaphore);
wait_semaphore = stage->m_physical->m_finished_semaphore->get();
}

std::vector<VkSubmitInfo> submit_infos;
for (std::size_t i = 0; i < m_stage_stack.size(); i++) {
const auto &physical = *m_stage_stack[i]->m_physical;
submit_info.pCommandBuffers = physical.m_command_buffers[image_index].ptr();
submit_info.pSignalSemaphores = physical.m_finished_semaphore->ptr();
submit_info.pWaitSemaphores = &wait_semaphore;
vkQueueSubmit(graphics_queue, 1, &submit_info, nullptr);
wait_semaphore = physical.m_finished_semaphore->get();
submit_info.pWaitSemaphores = &wait_semaphores[i];
submit_infos.push_back(submit_info);
}
vkQueueSubmit(graphics_queue, submit_infos.size(), submit_infos.data(), signal_fence);
return m_stage_stack.back()->m_physical->m_finished_semaphore->get();
}

Expand Down
10 changes: 8 additions & 2 deletions src/vulkan-renderer/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ void VulkanRenderer::recreate_swapchain() {
m_render_graph = std::make_unique<RenderGraph>(*m_device, m_command_pool->get(), *m_swapchain);
setup_render_graph();

m_frame_finished_fence.reset();
m_image_available_semaphore.reset();
m_frame_finished_fence = std::make_unique<wrapper::Fence>(*m_device, "Farme finished fence", true);
m_image_available_semaphore = std::make_unique<wrapper::Semaphore>(*m_device, "Image available semaphore");

m_camera = std::make_unique<Camera>(glm::vec3(5.0f, 5.0f, 10.0f), 250.0f, 0.0f,
Expand All @@ -100,9 +102,13 @@ void VulkanRenderer::render_frame() {
return;
}

// Wait for last frame to finish rendering.
m_frame_finished_fence->block();
m_frame_finished_fence->reset();

const auto image_index = m_swapchain->acquire_next_image(*m_image_available_semaphore);
VkSemaphore wait_semaphore =
m_render_graph->render(image_index, m_image_available_semaphore->get(), m_device->graphics_queue());
VkSemaphore wait_semaphore = m_render_graph->render(image_index, m_image_available_semaphore->get(),
m_device->graphics_queue(), m_frame_finished_fence->get());

// TODO(): Create a queue wrapper class
auto present_info = wrapper::make_info<VkPresentInfoKHR>();
Expand Down

0 comments on commit e67b02b

Please sign in to comment.