Skip to content

Commit 1ba6a03

Browse files
committed
Fix vector sizes for presentCompleteSemaphores in chapter 16.
1 parent 251e88a commit 1ba6a03

File tree

2 files changed

+41
-36
lines changed

2 files changed

+41
-36
lines changed

attachments/15_hello_triangle.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ class HelloTriangleApplication {
414414
}
415415

416416
void transition_image_layout(
417-
uint32_t currentFrame,
417+
uint32_t imageIndex,
418418
vk::ImageLayout old_layout,
419419
vk::ImageLayout new_layout,
420420
vk::AccessFlags2 src_access_mask,
@@ -431,7 +431,7 @@ class HelloTriangleApplication {
431431
.newLayout = new_layout,
432432
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
433433
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
434-
.image = swapChainImages[currentFrame],
434+
.image = swapChainImages[imageIndex],
435435
.subresourceRange = {
436436
.aspectMask = vk::ImageAspectFlagBits::eColor,
437437
.baseMipLevel = 0,

attachments/16_frames_in_flight.cpp

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,10 @@ class HelloTriangleApplication {
6363
vk::raii::CommandPool commandPool = nullptr;
6464
std::vector<vk::raii::CommandBuffer> commandBuffers;
6565

66-
std::vector<vk::raii::Semaphore> presentCompleteSemaphore;
67-
std::vector<vk::raii::Semaphore> renderFinishedSemaphore;
66+
std::vector<vk::raii::Semaphore> presentCompleteSemaphores;
67+
std::vector<vk::raii::Semaphore> renderFinishedSemaphores;
6868
std::vector<vk::raii::Fence> inFlightFences;
69-
uint32_t semaphoreIndex = 0;
70-
uint32_t currentFrame = 0;
69+
uint32_t frameIndex = 0;
7170

7271

7372
std::vector<const char*> requiredDeviceExtension = {
@@ -373,7 +372,8 @@ class HelloTriangleApplication {
373372
}
374373

375374
void recordCommandBuffer(uint32_t imageIndex) {
376-
commandBuffers[currentFrame].begin( {} );
375+
auto& commandBuffer = commandBuffers[frameIndex];
376+
commandBuffer.begin( {} );
377377
// Before starting rendering, transition the swapchain image to COLOR_ATTACHMENT_OPTIMAL
378378
transition_image_layout(
379379
imageIndex,
@@ -398,12 +398,12 @@ class HelloTriangleApplication {
398398
.colorAttachmentCount = 1,
399399
.pColorAttachments = &attachmentInfo
400400
};
401-
commandBuffers[currentFrame].beginRendering(renderingInfo);
402-
commandBuffers[currentFrame].bindPipeline(vk::PipelineBindPoint::eGraphics, *graphicsPipeline);
403-
commandBuffers[currentFrame].setViewport(0, vk::Viewport(0.0f, 0.0f, static_cast<float>(swapChainExtent.width), static_cast<float>(swapChainExtent.height), 0.0f, 1.0f));
404-
commandBuffers[currentFrame].setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), swapChainExtent ) );
405-
commandBuffers[currentFrame].draw(3, 1, 0, 0);
406-
commandBuffers[currentFrame].endRendering();
401+
commandBuffer.beginRendering(renderingInfo);
402+
commandBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, *graphicsPipeline);
403+
commandBuffer.setViewport(0, vk::Viewport(0.0f, 0.0f, static_cast<float>(swapChainExtent.width), static_cast<float>(swapChainExtent.height), 0.0f, 1.0f));
404+
commandBuffer.setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), swapChainExtent ) );
405+
commandBuffer.draw(3, 1, 0, 0);
406+
commandBuffer.endRendering();
407407
// After rendering, transition the swapchain image to PRESENT_SRC
408408
transition_image_layout(
409409
imageIndex,
@@ -414,7 +414,7 @@ class HelloTriangleApplication {
414414
vk::PipelineStageFlagBits2::eColorAttachmentOutput, // srcStage
415415
vk::PipelineStageFlagBits2::eBottomOfPipe // dstStage
416416
);
417-
commandBuffers[currentFrame].end();
417+
commandBuffer.end();
418418
}
419419

420420
void transition_image_layout(
@@ -449,52 +449,57 @@ class HelloTriangleApplication {
449449
.imageMemoryBarrierCount = 1,
450450
.pImageMemoryBarriers = &barrier
451451
};
452-
commandBuffers[currentFrame].pipelineBarrier2(dependency_info);
452+
commandBuffers[frameIndex].pipelineBarrier2(dependency_info);
453453
}
454454

455455
void createSyncObjects() {
456-
presentCompleteSemaphore.clear();
457-
renderFinishedSemaphore.clear();
458-
inFlightFences.clear();
456+
assert( presentCompleteSemaphores.empty() && renderFinishedSemaphores.empty() && inFlightFences.empty());
459457

460458
for (size_t i = 0; i < swapChainImages.size(); i++) {
461-
presentCompleteSemaphore.emplace_back(device, vk::SemaphoreCreateInfo());
462-
renderFinishedSemaphore.emplace_back(device, vk::SemaphoreCreateInfo());
459+
renderFinishedSemaphores.emplace_back(device, vk::SemaphoreCreateInfo());
463460
}
464461

465-
466462
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
467-
inFlightFences.emplace_back(device, vk::FenceCreateInfo { .flags = vk::FenceCreateFlagBits::eSignaled });
463+
presentCompleteSemaphores.emplace_back(device, vk::SemaphoreCreateInfo());
464+
inFlightFences.emplace_back(device, vk::FenceCreateInfo { .flags = vk::FenceCreateFlagBits::eSignaled });
468465
}
469466
}
470467

471468
void drawFrame() {
472-
while ( vk::Result::eTimeout == device.waitForFences( *inFlightFences[currentFrame], vk::True, UINT64_MAX ) )
469+
// Note: inFlightFences, presentCompleteSemaphores, and commandBuffers are indexed by frameIndex,
470+
// while renderFinishedSemaphores is indexed by imageIndex
471+
while ( vk::Result::eTimeout == device.waitForFences( *inFlightFences[frameIndex], vk::True, UINT64_MAX ) )
473472
;
474-
auto [result, imageIndex] = swapChain.acquireNextImage( UINT64_MAX, *presentCompleteSemaphore[semaphoreIndex], nullptr );
473+
device.resetFences(*inFlightFences[frameIndex]);
474+
475+
auto [result, imageIndex] = swapChain.acquireNextImage( UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr );
475476

476-
device.resetFences( *inFlightFences[currentFrame] );
477-
commandBuffers[currentFrame].reset();
477+
commandBuffers[frameIndex].reset();
478478
recordCommandBuffer(imageIndex);
479479

480480
vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput );
481-
const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore[semaphoreIndex],
482-
.pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffers[currentFrame],
483-
.signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore[imageIndex] };
484-
queue.submit(submitInfo, *inFlightFences[currentFrame]);
485-
486-
487-
const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex],
488-
.swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex };
481+
const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1,
482+
.pWaitSemaphores = &*presentCompleteSemaphores[frameIndex],
483+
.pWaitDstStageMask = &waitDestinationStageMask,
484+
.commandBufferCount = 1,
485+
.pCommandBuffers = &*commandBuffers[frameIndex],
486+
.signalSemaphoreCount = 1,
487+
.pSignalSemaphores = &*renderFinishedSemaphores[imageIndex] };
488+
queue.submit(submitInfo, *inFlightFences[frameIndex]);
489+
490+
const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1,
491+
.pWaitSemaphores = &*renderFinishedSemaphores[imageIndex],
492+
.swapchainCount = 1,
493+
.pSwapchains = &*swapChain,
494+
.pImageIndices = &imageIndex };
489495
result = queue.presentKHR( presentInfoKHR );
490496
switch ( result )
491497
{
492498
case vk::Result::eSuccess: break;
493499
case vk::Result::eSuboptimalKHR: std::cout << "vk::Queue::presentKHR returned vk::Result::eSuboptimalKHR !\n"; break;
494500
default: break; // an unexpected result is returned!
495501
}
496-
semaphoreIndex = (semaphoreIndex + 1) % presentCompleteSemaphore.size();
497-
currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
502+
frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT;
498503
}
499504

500505
[[nodiscard]] vk::raii::ShaderModule createShaderModule(const std::vector<char>& code) const {

0 commit comments

Comments
 (0)