Skip to content

Commit

Permalink
[vulkan] re-worked image acquisition and presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
PanosK92 committed Feb 5, 2025
1 parent 2e5f8db commit bf544a1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 27 deletions.
5 changes: 2 additions & 3 deletions runtime/RHI/RHI_SwapChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,9 @@ namespace spartan
RHI_Present_Mode m_present_mode = RHI_Present_Mode::Immediate;

// misc
uint32_t m_buffer_index = std::numeric_limits<uint32_t>::max();
uint32_t m_image_index = std::numeric_limits<uint32_t>::max();
uint32_t m_buffer_index = 0;
uint32_t m_image_index = 0;
void* m_sdl_window = nullptr;
bool m_image_acquired = false;
std::array<RHI_Image_Layout, buffer_count> m_layouts = { RHI_Image_Layout::Max };
std::array<std::shared_ptr<RHI_SyncPrimitive>, buffer_count> m_image_acquired_semaphore;
std::array<std::shared_ptr<RHI_SyncPrimitive>, buffer_count> m_image_acquired_fence;
Expand Down
4 changes: 2 additions & 2 deletions runtime/RHI/Vulkan/Vulkan_Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2153,9 +2153,9 @@ namespace spartan

// misc

void RHI_Device::SetResourceName(void* resource, const RHI_Resource_Type resource_type, const std::string name)
void RHI_Device::SetResourceName(void* resource, const RHI_Resource_Type resource_type, const string name)
{
if (Debugging::IsValidationLayerEnabled()) // function pointers are not initialized if validation disabled
if (Debugging::IsValidationLayerEnabled()) // function pointers are not initialized if validation disabled
{
SP_ASSERT(resource != nullptr);
SP_ASSERT(functions::set_object_name != nullptr);
Expand Down
26 changes: 4 additions & 22 deletions runtime/RHI/Vulkan/Vulkan_SwapChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,15 +410,7 @@ namespace spartan

void RHI_SwapChain::AcquireNextImage()
{
// reset any previous state if needed
if (m_buffer_index != numeric_limits<uint32_t>::max())
{
m_image_acquired_fence[m_buffer_index]->Wait();
m_image_acquired_fence[m_buffer_index]->Reset();
}

// prepare for the acquisition
m_buffer_index = (m_buffer_index + 1) % m_buffer_count;
RHI_SyncPrimitive* signal_semaphore = m_image_acquired_semaphore[m_buffer_index].get();
RHI_SyncPrimitive* signal_fence = m_image_acquired_fence[m_buffer_index].get();

Expand All @@ -439,7 +431,6 @@ namespace spartan

if (result == VK_SUCCESS)
{
m_image_acquired = true;
return;
}
else if (result == VK_NOT_READY)
Expand All @@ -452,8 +443,6 @@ namespace spartan
SP_ASSERT_VK(result);
}
}

m_image_acquired = false;
}

void RHI_SwapChain::Present()
Expand All @@ -468,24 +457,17 @@ namespace spartan
bool presents_to_this_swapchain = cmd_list->GetSwapchainId() == m_object_id;
if (presents_to_this_swapchain)
{
RHI_SyncPrimitive* semaphore = cmd_list->GetRenderingCompleteSemaphore();
semaphore->has_been_waited_for = m_image_acquired ? true : false;
RHI_SyncPrimitive* semaphore = cmd_list->GetRenderingCompleteSemaphore();
semaphore->has_been_waited_for = true;
m_wait_semaphores.emplace_back(semaphore);
}

// get semaphore from vkAcquireNextImageKHR
RHI_SyncPrimitive* image_acquired_semaphore = m_image_acquired_semaphore[m_buffer_index].get();
m_wait_semaphores.emplace_back(image_acquired_semaphore);

if (m_image_acquired)
{
queue->Present(m_rhi_swapchain, m_image_index, m_wait_semaphores);
m_image_acquired = false;
}
else
{
m_image_acquired_semaphore[m_buffer_index] = make_shared<RHI_SyncPrimitive>(RHI_SyncPrimitive_Type::Semaphore, "swapchain_image_acquired");
}
queue->Present(m_rhi_swapchain, m_image_index, m_wait_semaphores);
m_buffer_index = (m_buffer_index + 1) % m_buffer_count;
}

void RHI_SwapChain::SetLayout(const RHI_Image_Layout& layout, RHI_CommandList* cmd_list)
Expand Down

0 comments on commit bf544a1

Please sign in to comment.