Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e15039a

Browse files
committed
Remove VDeleter from drawing chapters
1 parent c79f967 commit e15039a

7 files changed

Lines changed: 238 additions & 312 deletions

File tree

03_Drawing_a_triangle/03_Drawing/00_Framebuffers.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ at drawing time.
1414
To that end, create another `std::vector` class member to hold the framebuffers:
1515

1616
```c++
17-
std::vector<VDeleter<VkFramebuffer>> swapChainFramebuffers;
17+
std::vector<VkFramebuffer> swapChainFramebuffers;
1818
```
1919

2020
We'll create the objects for this array in a new function `createFramebuffers`
@@ -45,7 +45,7 @@ Start by resizing the container to hold all of the framebuffers:
4545

4646
```c++
4747
void createFramebuffers() {
48-
swapChainFramebuffers.resize(swapChainImageViews.size(), VDeleter<VkFramebuffer>{device, vkDestroyFramebuffer});
48+
swapChainFramebuffers.resize(swapChainImageViews.size());
4949
}
5050
```
5151

@@ -66,7 +66,7 @@ for (size_t i = 0; i < swapChainImageViews.size(); i++) {
6666
framebufferInfo.height = swapChainExtent.height;
6767
framebufferInfo.layers = 1;
6868

69-
if (vkCreateFramebuffer(device, &framebufferInfo, nullptr, swapChainFramebuffers[i].replace()) != VK_SUCCESS) {
69+
if (vkCreateFramebuffer(device, &framebufferInfo, nullptr, &swapChainFramebuffers[i]) != VK_SUCCESS) {
7070
throw std::runtime_error("failed to create framebuffer!");
7171
}
7272
}
@@ -85,6 +85,19 @@ The `width` and `height` parameters are self-explanatory and `layers` refers to
8585
the number of layers in image arrays. Our swap chain images are single images,
8686
so the number of layers is `1`.
8787

88+
We should delete the framebuffers before the image views and render pass that
89+
they are based on, but only after we've finished rendering:
90+
91+
```c++
92+
void cleanup() {
93+
for (size_t i = 0; i < swapChainFramebuffers.size(); i++) {
94+
vkDestroyFramebuffer(device, swapChainFramebuffers[i], nullptr);
95+
}
96+
97+
...
98+
}
99+
```
100+
88101
We've now reached the milestone where we have all of the objects that are
89102
required for rendering. In the next chapter we're going to write the first
90103
actual drawing commands.

03_Drawing_a_triangle/03_Drawing/01_Command_buffers.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pools manage the memory that is used to store the buffers and command buffers
1212
are allocated from them. Add a new class member to store a `VkCommandPool`:
1313

1414
```c++
15-
VDeleter<VkCommandPool> commandPool{device, vkDestroyCommandPool};
15+
VkCommandPool commandPool;
1616
```
1717

1818
Then create a new function `createCommandPool` and call it from `initVulkan`
@@ -69,13 +69,23 @@ execute them many times in the main loop, so we're not going to use either of
6969
these flags.
7070

7171
```c++
72-
if (vkCreateCommandPool(device, &poolInfo, nullptr, commandPool.replace()) != VK_SUCCESS) {
72+
if (vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool) != VK_SUCCESS) {
7373
throw std::runtime_error("failed to create command pool!");
7474
}
7575
```
7676

7777
Finish creating the command pool using the `vkCreateCommandPool` function. It
78-
doesn't have any special parameters.
78+
doesn't have any special parameters. Commands will be used throughout the
79+
program to draw things on the screen, so the pool should only be destroyed at
80+
the end:
81+
82+
```c++
83+
void cleanup() {
84+
vkDestroyCommandPool(device, commandPool, nullptr);
85+
86+
...
87+
}
88+
```
7989

8090
## Command buffer allocation
8191

@@ -84,7 +94,7 @@ them. Because one of the drawing commands involves binding the right
8494
`VkFramebuffer`, we'll actually have to record a command buffer for every image
8595
in the swap chain once again. To that end, create a list of `VkCommandBuffer`
8696
objects as class member. Command buffers will be automatically freed when their
87-
command pool is destroyed, so we don't need a `VDeleter`.
97+
command pool is destroyed, so we don't need an explicit cleanup.
8898

8999
```c++
90100
std::vector<VkCommandBuffer> commandBuffers;

03_Drawing_a_triangle/03_Drawing/02_Rendering_and_presentation.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ presentation can happen. Create two class members to store these semaphore
5555
objects:
5656

5757
```c++
58-
VDeleter<VkSemaphore> imageAvailableSemaphore{device, vkDestroySemaphore};
59-
VDeleter<VkSemaphore> renderFinishedSemaphore{device, vkDestroySemaphore};
58+
VkSemaphore imageAvailableSemaphore;
59+
VkSemaphore renderFinishedSemaphore;
6060
```
6161

6262
To create the semaphores, we'll add the last `create` function for this part of
@@ -102,8 +102,8 @@ Future versions of the Vulkan API or extensions may add functionality for the
102102
the semaphores follows the familiar pattern with `vkCreateSemaphore`:
103103

104104
```c++
105-
if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, imageAvailableSemaphore.replace()) != VK_SUCCESS ||
106-
vkCreateSemaphore(device, &semaphoreInfo, nullptr, renderFinishedSemaphore.replace()) != VK_SUCCESS) {
105+
if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAvailableSemaphore) != VK_SUCCESS ||
106+
vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphore) != VK_SUCCESS) {
107107

108108
throw std::runtime_error("failed to create semaphores!");
109109
}
@@ -310,8 +310,8 @@ something resembling the following when you run your program:
310310
![](/images/triangle.png)
311311
312312
Yay! Unfortunately, you'll see that when validation layers are enabled, the
313-
program crashes as soon as you close it. The message printed to the terminal
314-
from `debugCallback` tells us why:
313+
program crashes as soon as you close it. The messages printed to the terminal
314+
from `debugCallback` tell us why:
315315
316316
![](/images/semaphore_in_use.png)
317317
@@ -331,8 +331,6 @@ void mainLoop() {
331331
}
332332
333333
vkDeviceWaitIdle(device);
334-
335-
glfwDestroyWindow(window);
336334
}
337335
```
338336

0 commit comments

Comments
 (0)