Skip to content

Commit 7c3365f

Browse files
committed
Update additional samples to use new sync
Replace fences with timeline semaphores Cleanup
1 parent cf01437 commit 7c3365f

3 files changed

Lines changed: 316 additions & 305 deletions

File tree

base/vulkanexamplebase.cpp

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,6 @@ VkResult VulkanExampleBase::createInstance()
179179
return result;
180180
}
181181

182-
// @todo: Only used by few examples, can be removed when new sync is fully in place
183-
void VulkanExampleBase::renderFrame()
184-
{
185-
VulkanExampleBase::prepareFrame();
186-
submitInfo.commandBufferCount = 1;
187-
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
188-
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
189-
VulkanExampleBase::submitFrame();
190-
}
191-
192182
std::string VulkanExampleBase::getWindowTitle() const
193183
{
194184
std::string windowTitle{ title + " - " + deviceProperties.deviceName };
@@ -758,12 +748,14 @@ void VulkanExampleBase::drawUI(const VkCommandBuffer commandBuffer)
758748
}
759749
}
760750

761-
void VulkanExampleBase::prepareFrame()
751+
void VulkanExampleBase::prepareFrame(bool waitForFence)
762752
{
763753
if (useNewSync) {
764754
// Ensure command buffer execution has finished
765-
VK_CHECK_RESULT(vkWaitForFences(device, 1, &waitFences[currentBuffer], VK_TRUE, UINT64_MAX));
766-
VK_CHECK_RESULT(vkResetFences(device, 1, &waitFences[currentBuffer]));
755+
if (waitForFence) {
756+
VK_CHECK_RESULT(vkWaitForFences(device, 1, &waitFences[currentBuffer], VK_TRUE, UINT64_MAX));
757+
VK_CHECK_RESULT(vkResetFences(device, 1, &waitFences[currentBuffer]));
758+
}
767759
// Update UI if necessary
768760
updateOverlay();
769761
// Acquire the next image from the swap chain
@@ -797,13 +789,20 @@ void VulkanExampleBase::prepareFrame()
797789
}
798790
}
799791

800-
void VulkanExampleBase::submitFrame(bool skipQueueSubmit)
792+
void VulkanExampleBase::submitFrame(VkCommandBuffer cmdBuffer, bool skipQueueSubmit)
801793
{
802794
if (useNewSync) {
803795
// @todo: make this an argument
804796
if (!skipQueueSubmit) {
797+
const VkPipelineStageFlags waitPipelineStage{ VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
798+
VkSubmitInfo submitInfo{ .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO };
799+
submitInfo.pWaitDstStageMask = &waitPipelineStage;
805800
submitInfo.commandBufferCount = 1;
806-
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
801+
if (cmdBuffer != VK_NULL_HANDLE) {
802+
submitInfo.pCommandBuffers = &cmdBuffer;
803+
} else {
804+
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
805+
}
807806
submitInfo.pWaitSemaphores = &presentCompleteSemaphores[currentBuffer];
808807
submitInfo.waitSemaphoreCount = 1;
809808
submitInfo.pSignalSemaphores = &renderCompleteSemaphores[currentImageIndex];
@@ -1204,17 +1203,6 @@ bool VulkanExampleBase::initVulkan()
12041203
// Ensures that the image is not presented until all commands have been submitted and executed
12051204
VK_CHECK_RESULT(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &semaphores.renderComplete));
12061205

1207-
// Set up submit info structure
1208-
// Semaphores will stay the same during application lifetime
1209-
// Command buffer submission info is set by each example
1210-
// @todo: Rework when new sync is fully in place, feels odd building/setting this up in multiple places
1211-
submitInfo = vks::initializers::submitInfo();
1212-
submitInfo.pWaitDstStageMask = &submitPipelineStages;
1213-
submitInfo.waitSemaphoreCount = 1;
1214-
submitInfo.pWaitSemaphores = &semaphores.presentComplete;
1215-
submitInfo.signalSemaphoreCount = 1;
1216-
submitInfo.pSignalSemaphores = &semaphores.renderComplete;
1217-
12181206
return true;
12191207
}
12201208

base/vulkanexamplebase.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,6 @@ class VulkanExampleBase
132132
VkFormat depthFormat{VK_FORMAT_UNDEFINED};
133133
// Command buffer pool
134134
VkCommandPool cmdPool{ VK_NULL_HANDLE };
135-
/** @brief Pipeline stages used to wait at for graphics queue submissions */
136-
VkPipelineStageFlags submitPipelineStages = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
137-
// Contains command buffers and semaphores to be presented to the queue
138-
VkSubmitInfo submitInfo{};
139135
// Command buffers used for rendering
140136
std::vector<VkCommandBuffer> drawCmdBuffers;
141137
// Global render pass for frame buffer writes
@@ -159,13 +155,13 @@ class VulkanExampleBase
159155
// Command buffer submission and execution
160156
VkSemaphore renderComplete;
161157
} semaphores{};
162-
std::vector<VkFence> waitFences;
163158

164159
// This sample will be using a new synchronisation setup, which is closer to what should be used with Vulkan
165160
bool useNewSync{ false };
166161
uint32_t currentImageIndex{ 0 };
167162
std::vector<VkSemaphore> presentCompleteSemaphores{};
168163
std::vector<VkSemaphore> renderCompleteSemaphores{};
164+
std::vector<VkFence> waitFences;
169165

170166
bool requiresStencil{ false };
171167
public:
@@ -414,12 +410,11 @@ class VulkanExampleBase
414410
void drawUI(const VkCommandBuffer commandBuffer);
415411

416412
/** Prepare the next frame for workload submission by acquiring the next swap chain image and waiting for the previous command buffer to finish */
417-
void prepareFrame();
413+
// @todo: rework once new sync is in place, maybe overload with submission info
414+
void prepareFrame(bool waitForFence = true);
418415
/** @brief Presents the current image to the swap chain */
419416
// @todo: rework once new sync is in place, maybe overload with submission info
420-
void submitFrame(bool skipQueueSubmit = false);
421-
/** @brief (Virtual) Default image acquire + submission and command buffer submission function */
422-
virtual void renderFrame();
417+
void submitFrame(VkCommandBuffer cmdBuffer = VK_NULL_HANDLE, bool skipQueueSubmit = false);
423418

424419
/** @brief (Virtual) Called when the UI overlay is updating, can be used to add custom elements to the overlay */
425420
virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay);

0 commit comments

Comments
 (0)