Skip to content

Commit

Permalink
modify sync strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
ChanLee1123 committed Oct 4, 2019
1 parent 3a579d1 commit a60f966
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/HelloTriangle.cpp
Expand Up @@ -24,7 +24,7 @@
#include <vector>

#include <vulkan/vulkan.h> // also assume core+WSI commands are loaded
static_assert( VK_HEADER_VERSION >= REQUIRED_HEADER_VERSION, "Update your SDK! This app is written against Vulkan header version " STRINGIZE(REQUIRED_HEADER_VERSION) "." );
//static_assert( VK_HEADER_VERSION >= REQUIRED_HEADER_VERSION, "Update your SDK! This app is written against Vulkan header version " STRINGIZE(REQUIRED_HEADER_VERSION) "." );

#include "EnumerateScheme.h"
#include "ErrorHandling.h"
Expand Down Expand Up @@ -71,7 +71,7 @@ constexpr uint32_t initialWindowWidth = 800;
constexpr uint32_t initialWindowHeight = 800;

//constexpr VkPresentModeKHR presentMode = VK_PRESENT_MODE_IMMEDIATE_KHR; // better not be used often because of coil whine
constexpr VkPresentModeKHR presentMode = VK_PRESENT_MODE_FIFO_KHR;
constexpr VkPresentModeKHR presentMode = VK_PRESENT_MODE_MAILBOX_KHR;

// pipeline settings
constexpr VkClearValue clearColor = { { {0.1f, 0.1f, 0.1f, 1.0f} } };
Expand Down Expand Up @@ -360,12 +360,12 @@ int helloTriangle() try{
vector<VkCommandBuffer> commandBuffers;

vector<VkSemaphore> imageReadySs; // has to be NULL for the case the app ends before even first swapchain
VkSemaphore renderDoneS = VK_NULL_HANDLE; // has to be NULL for the case the app ends before even first swapchain
vector<VkSemaphore> renderDoneSs; // has to be NULL for the case the app ends before even first swapchain

// workaround for validation layer "leak" + might also help driver to cleanup old resources
// this should not be needed for real-word app, because they are likely to use fences naturaly (e.g. user input reaction)
// read https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/issues/1628
const uint32_t maxInflightSubmissions = 2; // more than 2 probably does not make much sense
uint32_t maxInflightSubmissions; // more than 2 probably does not make much sense
uint32_t submissionNr = 0; // index of the current submission modulo maxInflightSubmission
vector<VkFence> submissionFences;

Expand Down Expand Up @@ -399,7 +399,7 @@ int helloTriangle() try{
killFences( device, submissionFences );

// semaphores might be in signaled state, so kill them too to get fresh unsignaled
killSemaphore( device, renderDoneS );
killSemaphores( device, renderDoneSs );
killSemaphores( device, imageReadySs );

// only reset + later reuse already allocated and create new only if needed
Expand Down Expand Up @@ -450,8 +450,9 @@ int helloTriangle() try{
endCommandBuffer( commandBuffers[i] );
}

maxInflightSubmissions = swapchainImages.size();
imageReadySs = initSemaphores( device, maxInflightSubmissions );
renderDoneS = initSemaphore( device );
renderDoneSs = initSemaphores( device, maxInflightSubmissions );

submissionFences = initFences( device, maxInflightSubmissions, VK_FENCE_CREATE_SIGNALED_BIT ); // signaled fence means previous execution finished, so we start rendering presignaled
submissionNr = 0;
Expand All @@ -472,10 +473,11 @@ int helloTriangle() try{

uint32_t nextSwapchainImageIndex = getNextImageIndex( device, swapchain, imageReadySs[submissionNr] );

submitToQueue( queue, commandBuffers[nextSwapchainImageIndex], imageReadySs[submissionNr], renderDoneS, submissionFences[submissionNr] );
submissionNr = (submissionNr + 1) % maxInflightSubmissions;
submitToQueue( queue, commandBuffers[nextSwapchainImageIndex], imageReadySs[submissionNr], renderDoneSs[submissionNr], submissionFences[submissionNr] );

present( queue, swapchain, nextSwapchainImageIndex, renderDoneSs[submissionNr] );

present( queue, swapchain, nextSwapchainImageIndex, renderDoneS );
submissionNr = (submissionNr + 1) % maxInflightSubmissions;
}
catch( VulkanResultException ex ){
if( ex.result == VK_SUBOPTIMAL_KHR || ex.result == VK_ERROR_OUT_OF_DATE_KHR ){
Expand Down Expand Up @@ -503,7 +505,7 @@ int helloTriangle() try{


// kill swapchain
killSemaphore( device, renderDoneS );
killSemaphores( device, renderDoneSs );
killSemaphores( device, imageReadySs );

// command buffers killed with pool
Expand Down

0 comments on commit a60f966

Please sign in to comment.