Skip to content

Commit 1c43532

Browse files
committed
Modify
1 parent b083d19 commit 1c43532

File tree

15 files changed

+134
-369
lines changed

15 files changed

+134
-369
lines changed

docs/codes/0132_rendering/main.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -739,15 +739,7 @@ class HelloTriangleApplication {
739739
m_device.waitForFences( *m_inFlightFence, true, UINT64_MAX );
740740
m_device.resetFences( *m_inFlightFence );
741741

742-
vk::AcquireNextImageInfoKHR nextImageInfo(
743-
m_swapChain,
744-
UINT64_MAX,
745-
m_imageAvailableSemaphore,
746-
{}, // fence
747-
0x1 // single GPU
748-
);
749-
750-
uint32_t imageIndex = m_device.acquireNextImage2KHR(nextImageInfo).second;
742+
uint32_t imageIndex = m_swapChain.acquireNextImage(UINT64_MAX, m_imageAvailableSemaphore).second;
751743

752744
m_commandBuffers[0].reset();
753745
recordCommandBuffer(m_commandBuffers[0], imageIndex);

docs/codes/0132_rendering/main.diff

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/src/main.cpp b/src/main.cpp
2-
index 5638a66..b7d7645 100644
2+
index 5638a66..392cdef 100644
33
--- a/src/main.cpp
44
+++ b/src/main.cpp
55
@@ -65,6 +65,9 @@ private:
@@ -49,7 +49,7 @@ index 5638a66..b7d7645 100644
4949

5050
m_renderPass = m_device.createRenderPass(renderPassInfo);
5151
}
52-
@@ -702,6 +722,57 @@ private:
52+
@@ -702,6 +722,49 @@ private:
5353
commandBuffer.end();
5454
}
5555
/////////////////////////////////////////////////////////////////
@@ -70,15 +70,7 @@ index 5638a66..b7d7645 100644
7070
+ m_device.waitForFences( *m_inFlightFence, true, UINT64_MAX );
7171
+ m_device.resetFences( *m_inFlightFence );
7272
+
73-
+ vk::AcquireNextImageInfoKHR nextImageInfo(
74-
+ m_swapChain,
75-
+ UINT64_MAX,
76-
+ m_imageAvailableSemaphore,
77-
+ {}, // fence
78-
+ 0x1 // single GPU
79-
+ );
80-
+
81-
+ uint32_t imageIndex = m_device.acquireNextImage2KHR(nextImageInfo).second;
73+
+ uint32_t imageIndex = m_swapChain.acquireNextImage(UINT64_MAX, m_imageAvailableSemaphore).second;
8274
+
8375
+ m_commandBuffers[0].reset();
8476
+ recordCommandBuffer(m_commandBuffers[0], imageIndex);

docs/codes/0133_flightframe/main.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -747,15 +747,7 @@ class HelloTriangleApplication {
747747
}
748748
m_device.resetFences( *m_inFlightFences[currentFrame] );
749749

750-
vk::AcquireNextImageInfoKHR nextImageInfo(
751-
m_swapChain,
752-
UINT64_MAX,
753-
m_imageAvailableSemaphores[currentFrame],
754-
{}, // fence
755-
0x1 // single GPU
756-
);
757-
758-
uint32_t imageIndex = m_device.acquireNextImage2KHR(nextImageInfo).second;
750+
uint32_t imageIndex = m_swapChain.acquireNextImage(UINT64_MAX, m_imageAvailableSemaphores[currentFrame]).second;
759751

760752
m_commandBuffers[currentFrame].reset();
761753
recordCommandBuffer(m_commandBuffers[currentFrame], imageIndex);

docs/codes/0133_flightframe/main.diff

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/src/main.cpp b/src/main.cpp
2-
index 3d4f0c6..dd674e4 100644
2+
index 392cdef..52cd070 100644
33
--- a/src/main.cpp
44
+++ b/src/main.cpp
55
@@ -41,6 +41,8 @@ private:
@@ -48,7 +48,7 @@ index 3d4f0c6..dd674e4 100644
4848
);
4949
m_commandBuffers = m_device.allocateCommandBuffers(allocInfo);
5050
}
51-
@@ -731,46 +734,56 @@ private:
51+
@@ -731,38 +734,48 @@ private:
5252
vk::FenceCreateFlagBits::eSignaled // flags
5353
);
5454

@@ -70,16 +70,8 @@ index 3d4f0c6..dd674e4 100644
7070
+ }
7171
+ m_device.resetFences( *m_inFlightFences[currentFrame] );
7272

73-
vk::AcquireNextImageInfoKHR nextImageInfo(
74-
m_swapChain,
75-
UINT64_MAX,
76-
- m_imageAvailableSemaphore,
77-
+ m_imageAvailableSemaphores[currentFrame],
78-
{}, // fence
79-
0x1 // single GPU
80-
);
81-
82-
uint32_t imageIndex = m_device.acquireNextImage2KHR(nextImageInfo).second;
73+
- uint32_t imageIndex = m_swapChain.acquireNextImage(UINT64_MAX, m_imageAvailableSemaphore).second;
74+
+ uint32_t imageIndex = m_swapChain.acquireNextImage(UINT64_MAX, m_imageAvailableSemaphores[currentFrame]).second;
8375

8476
- m_commandBuffers[0].reset();
8577
- recordCommandBuffer(m_commandBuffers[0], imageIndex);

docs/codes/0140_recreateswapchain/main.cpp

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -747,28 +747,16 @@ class HelloTriangleApplication {
747747
throw std::runtime_error{"waitForFences error"};
748748
}
749749

750-
vk::AcquireNextImageInfoKHR nextImageInfo(
751-
m_swapChain,
752-
UINT64_MAX,
753-
m_imageAvailableSemaphores[currentFrame],
754-
{}, // fence
755-
0x1 // single GPU
756-
);
757-
758-
// std::pair<vk::Result, uint32_t>
759-
auto pair = m_device.acquireNextImage2KHR(nextImageInfo);
760-
switch(pair.first){
761-
case vk::Result::eSuccess:
762-
case vk::Result::eSuboptimalKHR:
763-
break;
764-
case vk::Result::eErrorOutOfDateKHR:
765-
m_framebufferResized = false;
750+
uint32_t imageIndex;
751+
try{
752+
// std::pair<vk::Result, uint32_t>
753+
auto [res, idx] = m_swapChain.acquireNextImage(UINT64_MAX, m_imageAvailableSemaphores[currentFrame]);
754+
imageIndex = idx;
755+
} catch (const vk::OutOfDateKHRError&){
766756
recreateSwapChain();
767757
return;
768-
default:
769-
throw std::runtime_error("failed to acquire swap chain image!");
770-
}
771-
uint32_t imageIndex = pair.second;
758+
} // Do not catch other exceptions
759+
772760
// Only reset the fence if we are submitting work
773761
m_device.resetFences( *m_inFlightFences[currentFrame] );
774762

@@ -793,19 +781,15 @@ class HelloTriangleApplication {
793781
presentInfo.setSwapchains( *m_swapChain );
794782
presentInfo.pImageIndices = &imageIndex;
795783

796-
switch( m_presentQueue.presentKHR(presentInfo) ){
797-
case vk::Result::eSuccess:
798-
break;
799-
case vk::Result::eErrorOutOfDateKHR:
800-
case vk::Result::eSuboptimalKHR:
801-
m_framebufferResized = false;
784+
try{
785+
auto res = m_presentQueue.presentKHR(presentInfo);
786+
if( res == vk::Result::eSuboptimalKHR ) {
802787
recreateSwapChain();
803-
break;
804-
default:
805-
throw std::runtime_error("failed to acquire swap chain image!");
788+
}
789+
} catch (const vk::OutOfDateKHRError&){
790+
recreateSwapChain();
806791
}
807792
if( m_framebufferResized ){
808-
m_framebufferResized = false;
809793
recreateSwapChain();
810794
}
811795

@@ -819,12 +803,6 @@ class HelloTriangleApplication {
819803
auto app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
820804
app->m_framebufferResized = true;
821805
}
822-
void cleanupSwapChain() {
823-
m_swapChainFramebuffers.clear();
824-
m_swapChainImageViews.clear();
825-
// m_swapChainImages.clear(); // optional
826-
m_swapChain = nullptr;
827-
}
828806
void recreateSwapChain() {
829807
int width = 0, height = 0;
830808
glfwGetFramebufferSize(m_window, &width, &height);
@@ -835,11 +813,16 @@ class HelloTriangleApplication {
835813

836814
m_device.waitIdle();
837815

838-
cleanupSwapChain();
816+
m_swapChainFramebuffers.clear();
817+
m_swapChainImageViews.clear();
818+
// m_swapChainImages.clear(); // optional
819+
m_swapChain = nullptr;
839820

840821
createSwapChain();
841822
createImageViews();
842823
createFramebuffers();
824+
825+
m_framebufferResized = false;
843826
}
844827
/////////////////////////////////////////////////////////////////
845828
};
-5.01 KB
Binary file not shown.

docs/codes/0200_vertexinput/main.cpp

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -753,28 +753,16 @@ class HelloTriangleApplication {
753753
throw std::runtime_error{"waitForFences error"};
754754
}
755755

756-
vk::AcquireNextImageInfoKHR nextImageInfo(
757-
m_swapChain,
758-
UINT64_MAX,
759-
m_imageAvailableSemaphores[currentFrame],
760-
{}, // fence
761-
0x1 // single GPU
762-
);
763-
764-
// std::pair<vk::Result, uint32_t>
765-
auto pair = m_device.acquireNextImage2KHR(nextImageInfo);
766-
switch(pair.first){
767-
case vk::Result::eSuccess:
768-
case vk::Result::eSuboptimalKHR:
769-
break;
770-
case vk::Result::eErrorOutOfDateKHR:
771-
m_framebufferResized = false;
756+
uint32_t imageIndex;
757+
try{
758+
// std::pair<vk::Result, uint32_t>
759+
auto [res, idx] = m_swapChain.acquireNextImage(UINT64_MAX, m_imageAvailableSemaphores[currentFrame]);
760+
imageIndex = idx;
761+
} catch (const vk::OutOfDateKHRError&){
772762
recreateSwapChain();
773763
return;
774-
default:
775-
throw std::runtime_error("failed to acquire swap chain image!");
776-
}
777-
uint32_t imageIndex = pair.second;
764+
} // Do not catch other exceptions
765+
778766
// Only reset the fence if we are submitting work
779767
m_device.resetFences( *m_inFlightFences[currentFrame] );
780768

@@ -799,19 +787,15 @@ class HelloTriangleApplication {
799787
presentInfo.setSwapchains( *m_swapChain );
800788
presentInfo.pImageIndices = &imageIndex;
801789

802-
switch( m_presentQueue.presentKHR(presentInfo) ){
803-
case vk::Result::eSuccess:
804-
break;
805-
case vk::Result::eErrorOutOfDateKHR:
806-
case vk::Result::eSuboptimalKHR:
807-
m_framebufferResized = false;
790+
try{
791+
auto res = m_presentQueue.presentKHR(presentInfo);
792+
if( res == vk::Result::eSuboptimalKHR ) {
808793
recreateSwapChain();
809-
break;
810-
default:
811-
throw std::runtime_error("failed to acquire swap chain image!");
794+
}
795+
} catch (const vk::OutOfDateKHRError&){
796+
recreateSwapChain();
812797
}
813798
if( m_framebufferResized ){
814-
m_framebufferResized = false;
815799
recreateSwapChain();
816800
}
817801

@@ -825,12 +809,6 @@ class HelloTriangleApplication {
825809
auto app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
826810
app->m_framebufferResized = true;
827811
}
828-
void cleanupSwapChain() {
829-
m_swapChainFramebuffers.clear();
830-
m_swapChainImageViews.clear();
831-
// m_swapChainImages.clear(); // optional
832-
m_swapChain = nullptr;
833-
}
834812
void recreateSwapChain() {
835813
int width = 0, height = 0;
836814
glfwGetFramebufferSize(m_window, &width, &height);
@@ -841,11 +819,16 @@ class HelloTriangleApplication {
841819

842820
m_device.waitIdle();
843821

844-
cleanupSwapChain();
822+
m_swapChainFramebuffers.clear();
823+
m_swapChainImageViews.clear();
824+
// m_swapChainImages.clear(); // optional
825+
m_swapChain = nullptr;
845826

846827
createSwapChain();
847828
createImageViews();
848829
createFramebuffers();
830+
831+
m_framebufferResized = false;
849832
}
850833
/////////////////////////////////////////////////////////////////
851834

docs/codes/0200_vertexinput/main.diff

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/src/main.cpp b/src/main.cpp
2-
index e51eb7f..7880e6c 100644
2+
index a9ee442..ed84bbb 100644
33
--- a/src/main.cpp
44
+++ b/src/main.cpp
55
@@ -1,6 +1,7 @@
@@ -22,8 +22,8 @@ index e51eb7f..7880e6c 100644
2222
vk::PipelineInputAssemblyStateCreateInfo inputAssembly(
2323
{}, // flags
2424
vk::PrimitiveTopology::eTriangleList, // topology
25-
@@ -842,6 +848,44 @@ private:
26-
createFramebuffers();
25+
@@ -825,6 +831,44 @@ private:
26+
m_framebufferResized = false;
2727
}
2828
/////////////////////////////////////////////////////////////////
2929
+

0 commit comments

Comments
 (0)