Skip to content

Commit d436b8d

Browse files
committed
Carry changes to all other chapters
1 parent dd21bdc commit d436b8d

22 files changed

+823
-668
lines changed

attachments/17_swap_chain_recreation.cpp

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

67-
std::vector<vk::raii::Semaphore> presentCompleteSemaphore;
68-
std::vector<vk::raii::Semaphore> renderFinishedSemaphore;
67+
std::vector<vk::raii::Semaphore> presentCompleteSemaphores;
68+
std::vector<vk::raii::Semaphore> renderFinishedSemaphores;
6969
std::vector<vk::raii::Fence> inFlightFences;
70-
uint32_t semaphoreIndex = 0;
71-
uint32_t currentFrame = 0;
70+
uint32_t frameIndex = 0;
7271

7372
bool framebufferResized = false;
7473

@@ -411,7 +410,8 @@ class HelloTriangleApplication
411410

412411
void recordCommandBuffer(uint32_t imageIndex)
413412
{
414-
commandBuffers[currentFrame].begin({});
413+
auto &commandBuffer = commandBuffers[frameIndex];
414+
commandBuffer.begin({});
415415
// Before starting rendering, transition the swapchain image to COLOR_ATTACHMENT_OPTIMAL
416416
transition_image_layout(
417417
imageIndex,
@@ -434,12 +434,12 @@ class HelloTriangleApplication
434434
.layerCount = 1,
435435
.colorAttachmentCount = 1,
436436
.pColorAttachments = &attachmentInfo};
437-
commandBuffers[currentFrame].beginRendering(renderingInfo);
438-
commandBuffers[currentFrame].bindPipeline(vk::PipelineBindPoint::eGraphics, *graphicsPipeline);
439-
commandBuffers[currentFrame].setViewport(0, vk::Viewport(0.0f, 0.0f, static_cast<float>(swapChainExtent.width), static_cast<float>(swapChainExtent.height), 0.0f, 1.0f));
440-
commandBuffers[currentFrame].setScissor(0, vk::Rect2D(vk::Offset2D(0, 0), swapChainExtent));
441-
commandBuffers[currentFrame].draw(3, 1, 0, 0);
442-
commandBuffers[currentFrame].endRendering();
437+
commandBuffer.beginRendering(renderingInfo);
438+
commandBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, *graphicsPipeline);
439+
commandBuffer.setViewport(0, vk::Viewport(0.0f, 0.0f, static_cast<float>(swapChainExtent.width), static_cast<float>(swapChainExtent.height), 0.0f, 1.0f));
440+
commandBuffer.setScissor(0, vk::Rect2D(vk::Offset2D(0, 0), swapChainExtent));
441+
commandBuffer.draw(3, 1, 0, 0);
442+
commandBuffer.endRendering();
443443
// After rendering, transition the swapchain image to PRESENT_SRC
444444
transition_image_layout(
445445
imageIndex,
@@ -450,7 +450,7 @@ class HelloTriangleApplication
450450
vk::PipelineStageFlagBits2::eColorAttachmentOutput, // srcStage
451451
vk::PipelineStageFlagBits2::eBottomOfPipe // dstStage
452452
);
453-
commandBuffers[currentFrame].end();
453+
commandBuffer.end();
454454
}
455455

456456
void transition_image_layout(
@@ -482,32 +482,34 @@ class HelloTriangleApplication
482482
.dependencyFlags = {},
483483
.imageMemoryBarrierCount = 1,
484484
.pImageMemoryBarriers = &barrier};
485-
commandBuffers[currentFrame].pipelineBarrier2(dependency_info);
485+
commandBuffers[frameIndex].pipelineBarrier2(dependency_info);
486486
}
487487

488488
void createSyncObjects()
489489
{
490-
presentCompleteSemaphore.clear();
491-
renderFinishedSemaphore.clear();
492-
inFlightFences.clear();
490+
assert(presentCompleteSemaphores.empty() && renderFinishedSemaphores.empty() && inFlightFences.empty());
493491

494492
for (size_t i = 0; i < swapChainImages.size(); i++)
495493
{
496-
presentCompleteSemaphore.emplace_back(device, vk::SemaphoreCreateInfo());
497-
renderFinishedSemaphore.emplace_back(device, vk::SemaphoreCreateInfo());
494+
renderFinishedSemaphores.emplace_back(device, vk::SemaphoreCreateInfo());
498495
}
499496

500497
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
501498
{
499+
presentCompleteSemaphores.emplace_back(device, vk::SemaphoreCreateInfo());
502500
inFlightFences.emplace_back(device, vk::FenceCreateInfo{.flags = vk::FenceCreateFlagBits::eSignaled});
503501
}
504502
}
505503

506504
void drawFrame()
507505
{
508-
while (vk::Result::eTimeout == device.waitForFences(*inFlightFences[currentFrame], vk::True, UINT64_MAX))
506+
// Note: inFlightFences, presentCompleteSemaphores, and commandBuffers are indexed by frameIndex,
507+
// while renderFinishedSemaphores is indexed by imageIndex
508+
while (vk::Result::eTimeout == device.waitForFences(*inFlightFences[frameIndex], vk::True, UINT64_MAX))
509509
;
510-
auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphore[semaphoreIndex], nullptr);
510+
device.resetFences(*inFlightFences[frameIndex]);
511+
512+
auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr);
511513

512514
if (result == vk::Result::eErrorOutOfDateKHR)
513515
{
@@ -519,19 +521,29 @@ class HelloTriangleApplication
519521
throw std::runtime_error("failed to acquire swap chain image!");
520522
}
521523

522-
device.resetFences(*inFlightFences[currentFrame]);
523-
commandBuffers[currentFrame].reset();
524+
device.resetFences(*inFlightFences[frameIndex]);
525+
commandBuffers[frameIndex].reset();
524526
recordCommandBuffer(imageIndex);
525527

526528
vk::PipelineStageFlags waitDestinationStageMask(vk::PipelineStageFlagBits::eColorAttachmentOutput);
527-
const vk::SubmitInfo submitInfo{.waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore[semaphoreIndex], .pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffers[currentFrame], .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore[imageIndex]};
528-
queue.submit(submitInfo, *inFlightFences[currentFrame]);
529+
const vk::SubmitInfo submitInfo{.waitSemaphoreCount = 1,
530+
.pWaitSemaphores = &*presentCompleteSemaphores[frameIndex],
531+
.pWaitDstStageMask = &waitDestinationStageMask,
532+
.commandBufferCount = 1,
533+
.pCommandBuffers = &*commandBuffers[frameIndex],
534+
.signalSemaphoreCount = 1,
535+
.pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]};
536+
queue.submit(submitInfo, *inFlightFences[frameIndex]);
529537

530538
try
531539
{
532-
const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex], .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex};
540+
const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1,
541+
.pWaitSemaphores = &*renderFinishedSemaphores[imageIndex],
542+
.swapchainCount = 1,
543+
.pSwapchains = &*swapChain,
544+
.pImageIndices = &imageIndex};
533545
result = queue.presentKHR(presentInfoKHR);
534-
if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized)
546+
if (result == vk::Result::eSuboptimalKHR || framebufferResized)
535547
{
536548
framebufferResized = false;
537549
recreateSwapChain();
@@ -553,8 +565,7 @@ class HelloTriangleApplication
553565
throw;
554566
}
555567
}
556-
semaphoreIndex = (semaphoreIndex + 1) % presentCompleteSemaphore.size();
557-
currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
568+
frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT;
558569
}
559570

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

attachments/18_vertex_input.cpp

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,10 @@ class HelloTriangleApplication
8989
vk::raii::CommandPool commandPool = nullptr;
9090
std::vector<vk::raii::CommandBuffer> commandBuffers;
9191

92-
std::vector<vk::raii::Semaphore> presentCompleteSemaphore;
93-
std::vector<vk::raii::Semaphore> renderFinishedSemaphore;
92+
std::vector<vk::raii::Semaphore> presentCompleteSemaphores;
93+
std::vector<vk::raii::Semaphore> renderFinishedSemaphores;
9494
std::vector<vk::raii::Fence> inFlightFences;
95-
uint32_t semaphoreIndex = 0;
96-
uint32_t currentFrame = 0;
95+
uint32_t frameIndex = 0;
9796

9897
bool framebufferResized = false;
9998

@@ -430,7 +429,8 @@ class HelloTriangleApplication
430429

431430
void recordCommandBuffer(uint32_t imageIndex)
432431
{
433-
commandBuffers[currentFrame].begin({});
432+
auto &commandBuffer = commandBuffers[frameIndex];
433+
commandBuffer.begin({});
434434
// Before starting rendering, transition the swapchain image to COLOR_ATTACHMENT_OPTIMAL
435435
transition_image_layout(
436436
imageIndex,
@@ -453,12 +453,12 @@ class HelloTriangleApplication
453453
.layerCount = 1,
454454
.colorAttachmentCount = 1,
455455
.pColorAttachments = &attachmentInfo};
456-
commandBuffers[currentFrame].beginRendering(renderingInfo);
457-
commandBuffers[currentFrame].bindPipeline(vk::PipelineBindPoint::eGraphics, *graphicsPipeline);
458-
commandBuffers[currentFrame].setViewport(0, vk::Viewport(0.0f, 0.0f, static_cast<float>(swapChainExtent.width), static_cast<float>(swapChainExtent.height), 0.0f, 1.0f));
459-
commandBuffers[currentFrame].setScissor(0, vk::Rect2D(vk::Offset2D(0, 0), swapChainExtent));
460-
commandBuffers[currentFrame].draw(3, 1, 0, 0);
461-
commandBuffers[currentFrame].endRendering();
456+
commandBuffer.beginRendering(renderingInfo);
457+
commandBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, *graphicsPipeline);
458+
commandBuffer.setViewport(0, vk::Viewport(0.0f, 0.0f, static_cast<float>(swapChainExtent.width), static_cast<float>(swapChainExtent.height), 0.0f, 1.0f));
459+
commandBuffer.setScissor(0, vk::Rect2D(vk::Offset2D(0, 0), swapChainExtent));
460+
commandBuffer.draw(3, 1, 0, 0);
461+
commandBuffer.endRendering();
462462
// After rendering, transition the swapchain image to PRESENT_SRC
463463
transition_image_layout(
464464
imageIndex,
@@ -469,7 +469,7 @@ class HelloTriangleApplication
469469
vk::PipelineStageFlagBits2::eColorAttachmentOutput, // srcStage
470470
vk::PipelineStageFlagBits2::eBottomOfPipe // dstStage
471471
);
472-
commandBuffers[currentFrame].end();
472+
commandBuffer.end();
473473
}
474474

475475
void transition_image_layout(
@@ -501,32 +501,34 @@ class HelloTriangleApplication
501501
.dependencyFlags = {},
502502
.imageMemoryBarrierCount = 1,
503503
.pImageMemoryBarriers = &barrier};
504-
commandBuffers[currentFrame].pipelineBarrier2(dependency_info);
504+
commandBuffers[frameIndex].pipelineBarrier2(dependency_info);
505505
}
506506

507507
void createSyncObjects()
508508
{
509-
presentCompleteSemaphore.clear();
510-
renderFinishedSemaphore.clear();
511-
inFlightFences.clear();
509+
assert(presentCompleteSemaphores.empty() && renderFinishedSemaphores.empty() && inFlightFences.empty());
512510

513511
for (size_t i = 0; i < swapChainImages.size(); i++)
514512
{
515-
presentCompleteSemaphore.emplace_back(device, vk::SemaphoreCreateInfo());
516-
renderFinishedSemaphore.emplace_back(device, vk::SemaphoreCreateInfo());
513+
renderFinishedSemaphores.emplace_back(device, vk::SemaphoreCreateInfo());
517514
}
518515

519516
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
520517
{
518+
presentCompleteSemaphores.emplace_back(device, vk::SemaphoreCreateInfo());
521519
inFlightFences.emplace_back(device, vk::FenceCreateInfo{.flags = vk::FenceCreateFlagBits::eSignaled});
522520
}
523521
}
524522

525523
void drawFrame()
526524
{
527-
while (vk::Result::eTimeout == device.waitForFences(*inFlightFences[currentFrame], vk::True, UINT64_MAX))
525+
// Note: inFlightFences, presentCompleteSemaphores, and commandBuffers are indexed by frameIndex,
526+
// while renderFinishedSemaphores is indexed by imageIndex
527+
while (vk::Result::eTimeout == device.waitForFences(*inFlightFences[frameIndex], vk::True, UINT64_MAX))
528528
;
529-
auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphore[semaphoreIndex], nullptr);
529+
device.resetFences(*inFlightFences[frameIndex]);
530+
531+
auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr);
530532

531533
if (result == vk::Result::eErrorOutOfDateKHR)
532534
{
@@ -538,17 +540,26 @@ class HelloTriangleApplication
538540
throw std::runtime_error("failed to acquire swap chain image!");
539541
}
540542

541-
device.resetFences(*inFlightFences[currentFrame]);
542-
commandBuffers[currentFrame].reset();
543+
commandBuffers[frameIndex].reset();
543544
recordCommandBuffer(imageIndex);
544545

545546
vk::PipelineStageFlags waitDestinationStageMask(vk::PipelineStageFlagBits::eColorAttachmentOutput);
546-
const vk::SubmitInfo submitInfo{.waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore[semaphoreIndex], .pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffers[currentFrame], .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore[imageIndex]};
547-
queue.submit(submitInfo, *inFlightFences[currentFrame]);
547+
const vk::SubmitInfo submitInfo{.waitSemaphoreCount = 1,
548+
.pWaitSemaphores = &*presentCompleteSemaphores[frameIndex],
549+
.pWaitDstStageMask = &waitDestinationStageMask,
550+
.commandBufferCount = 1,
551+
.pCommandBuffers = &*commandBuffers[frameIndex],
552+
.signalSemaphoreCount = 1,
553+
.pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]};
554+
queue.submit(submitInfo, *inFlightFences[frameIndex]);
548555

549556
try
550557
{
551-
const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex], .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex};
558+
const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1,
559+
.pWaitSemaphores = &*renderFinishedSemaphores[imageIndex],
560+
.swapchainCount = 1,
561+
.pSwapchains = &*swapChain,
562+
.pImageIndices = &imageIndex};
552563
result = queue.presentKHR(presentInfoKHR);
553564
if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized)
554565
{
@@ -572,8 +583,7 @@ class HelloTriangleApplication
572583
throw;
573584
}
574585
}
575-
semaphoreIndex = (semaphoreIndex + 1) % presentCompleteSemaphore.size();
576-
currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
586+
frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT;
577587
}
578588

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

0 commit comments

Comments
 (0)