Skip to content

Commit 7c37cd7

Browse files
Merge pull request #1639 from vsg-dev/FrameSemaphores
Frame semaphores
2 parents 62b679a + 567cd3b commit 7c37cd7

File tree

5 files changed

+31
-18
lines changed

5 files changed

+31
-18
lines changed

include/vsg/app/Window.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,11 @@ namespace vsg
122122
ref_ptr<ImageView> imageView;
123123
ref_ptr<Framebuffer> framebuffer;
124124
ref_ptr<Semaphore> imageAvailableSemaphore;
125+
ref_ptr<Semaphore> renderFinishedSemaphore;
125126
};
126127

127128
using Frames = std::vector<Frame>;
129+
using Semaphores = std::vector<ref_ptr<Semaphore>>;
128130

129131
Frame& frame(size_t i) { return _frames[i]; }
130132
Frames& frames() { return _frames; }
@@ -173,10 +175,11 @@ namespace vsg
173175
ref_ptr<Image> _multisampleDepthImage;
174176
ref_ptr<ImageView> _multisampleDepthImageView;
175177

176-
ref_ptr<Semaphore> _availableSemaphore;
177-
178178
Frames _frames;
179179
std::vector<size_t> _indices;
180+
181+
Semaphores _availableSemaphores;
182+
size_t _availableSemaphoreIndex = 0;
180183
};
181184
VSG_type_name(vsg::Window);
182185

src/vsg/app/Presentation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ VkResult Presentation::present()
3434
{
3535
vk_swapchains.emplace_back(*(window->getOrCreateSwapchain()));
3636
indices.emplace_back(static_cast<uint32_t>(imageIndex));
37+
38+
auto& renderFinishedSemaphore = window->frame(imageIndex).renderFinishedSemaphore;
39+
vk_semaphores.push_back(renderFinishedSemaphore->vk());
3740
}
3841
}
3942

src/vsg/app/RecordAndSubmitTask.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,27 +215,33 @@ VkResult RecordAndSubmitTask::finish(ref_ptr<RecordedCommandBuffers> recordedCom
215215
if (earlyDataTransferredSemaphore) transferTask->assignTransferConsumedCompletedSemaphore(TransferTask::TRANSFER_BEFORE_RECORD_TRAVERSAL, earlyTransferConsumerCompletedSemaphore);
216216
if (lateDataTransferredSemaphore) transferTask->assignTransferConsumedCompletedSemaphore(TransferTask::TRANSFER_AFTER_RECORD_TRAVERSAL, lateTransferConsumerCompletedSemaphore);
217217

218+
current_fence->dependentSemaphores().clear();
219+
218220
for (auto& window : windows)
219221
{
220222
auto imageIndex = window->imageIndex();
221223
if (imageIndex >= window->numFrames()) continue;
222224

223-
auto& semaphore = window->frame(imageIndex).imageAvailableSemaphore;
225+
auto& frame = window->frame(imageIndex);
224226

225-
vk_waitSemaphores.emplace_back(*semaphore);
226-
vk_waitStages.emplace_back(semaphore->pipelineStageFlags());
227+
vk_waitSemaphores.emplace_back(frame.imageAvailableSemaphore->vk());
228+
vk_waitStages.emplace_back(frame.imageAvailableSemaphore->pipelineStageFlags());
229+
230+
vk_signalSemaphores.emplace_back(frame.renderFinishedSemaphore->vk());
231+
current_fence->dependentSemaphores().push_back(frame.renderFinishedSemaphore);
227232
}
228233

229234
for (auto& semaphore : waitSemaphores)
230235
{
231-
vk_waitSemaphores.emplace_back(*(semaphore));
236+
vk_waitSemaphores.emplace_back(semaphore->vk());
232237
vk_waitStages.emplace_back(semaphore->pipelineStageFlags());
233238
}
234239

235240
current_fence->dependentSemaphores() = signalSemaphores;
236241
for (auto& semaphore : signalSemaphores)
237242
{
238-
vk_signalSemaphores.emplace_back(*(semaphore));
243+
vk_signalSemaphores.emplace_back(semaphore->vk());
244+
current_fence->dependentSemaphores().push_back(semaphore);
239245
}
240246

241247
if (earlyDataTransferredSemaphore)

src/vsg/app/Viewer.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,13 +524,10 @@ void Viewer::assignRecordAndSubmitTaskAndPresentation(CommandGraphs in_commandGr
524524

525525
Windows activeWindows(findWindows.windows.begin(), findWindows.windows.end());
526526

527-
auto renderFinishedSemaphore = vsg::Semaphore::create(device);
528-
529527
// set up Submission with CommandBuffer and signals
530528
auto recordAndSubmitTask = vsg::RecordAndSubmitTask::create(device, numBuffers);
531529
recordAndSubmitTask->commandGraphs = commandGraphs;
532530
recordAndSubmitTask->databasePager = databasePager;
533-
recordAndSubmitTask->signalSemaphores.emplace_back(renderFinishedSemaphore);
534531
recordAndSubmitTask->windows = activeWindows;
535532
recordAndSubmitTask->queue = mainQueue;
536533
recordAndSubmitTasks.emplace_back(recordAndSubmitTask);
@@ -541,7 +538,6 @@ void Viewer::assignRecordAndSubmitTaskAndPresentation(CommandGraphs in_commandGr
541538
if (instrumentation) recordAndSubmitTask->assignInstrumentation(instrumentation);
542539

543540
auto presentation = vsg::Presentation::create();
544-
presentation->waitSemaphores.emplace_back(renderFinishedSemaphore);
545541
presentation->windows = activeWindows;
546542
presentation->queue = device->getQueue(deviceQueueFamily.presentFamily);
547543
presentations.emplace_back(presentation);

src/vsg/app/Window.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,6 @@ void Window::buildSwapchain()
370370
// set up framebuffer and associated resources
371371
auto& imageViews = _swapchain->getImageViews();
372372

373-
_availableSemaphore = vsg::Semaphore::create(_device, _traits->imageAvailableSemaphoreWaitFlag);
374-
375373
size_t initial_indexValue = imageViews.size();
376374
for (size_t i = 0; i < imageViews.size(); ++i)
377375
{
@@ -391,12 +389,18 @@ void Window::buildSwapchain()
391389
ref_ptr<Framebuffer> fb = Framebuffer::create(_renderPass, attachments, _extent2D.width, _extent2D.height, 1);
392390

393391
ref_ptr<Semaphore> ias = vsg::Semaphore::create(_device, _traits->imageAvailableSemaphoreWaitFlag);
392+
ref_ptr<Semaphore> rfs = vsg::Semaphore::create(_device);
394393

395-
//_frames.push_back({multisampling ? _multisampleImageView : imageViews[i], fb, ias});
396-
_frames.push_back({imageViews[i], fb, ias});
394+
_frames.push_back({imageViews[i], fb, ias, rfs});
397395
_indices.push_back(initial_indexValue);
398396
}
399397

398+
_availableSemaphoreIndex = 0;
399+
for (size_t i = 0; i < imageViews.size(); ++i)
400+
{
401+
_availableSemaphores.push_back(vsg::Semaphore::create(_device, _traits->imageAvailableSemaphoreWaitFlag));
402+
}
403+
400404
{
401405
// ensure image attachments are setup on GPU.
402406
auto commandPool = CommandPool::create(_device, graphicsFamily);
@@ -434,18 +438,19 @@ VkResult Window::acquireNextImage(uint64_t timeout)
434438
{
435439
if (!_swapchain) _initSwapchain();
436440

437-
if (!_availableSemaphore) _availableSemaphore = vsg::Semaphore::create(_device, _traits->imageAvailableSemaphoreWaitFlag);
441+
auto& availableSemaphore = _availableSemaphores[_availableSemaphoreIndex];
442+
_availableSemaphoreIndex = (_availableSemaphoreIndex + 1) % _availableSemaphores.size();
438443

439444
// check the dimensions of the swapchain and window extents are consistent, if not return a VK_ERROR_OUT_OF_DATE_KHR
440445
if (_swapchain->getExtent() != _extent2D) return VK_ERROR_OUT_OF_DATE_KHR;
441446

442447
uint32_t nextImageIndex;
443-
VkResult result = _swapchain->acquireNextImage(timeout, _availableSemaphore, {}, nextImageIndex);
448+
VkResult result = _swapchain->acquireNextImage(timeout, availableSemaphore, {}, nextImageIndex);
444449

445450
if (result == VK_SUCCESS)
446451
{
447452
// the acquired image's semaphore must be available now so make it the new _availableSemaphore and set its entry to the one to use for the next frame by swapping ref_ptr<>'s
448-
_availableSemaphore.swap(_frames[nextImageIndex].imageAvailableSemaphore);
453+
availableSemaphore.swap(_frames[nextImageIndex].imageAvailableSemaphore);
449454

450455
// shift up previous frame indices
451456
for (size_t i = _indices.size() - 1; i > 0; --i)

0 commit comments

Comments
 (0)