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

Skip to content

Commit fa80e74

Browse files
committed
Remove VDeleter from vertex buffer chapters
1 parent 55b4803 commit fa80e74

9 files changed

Lines changed: 470 additions & 501 deletions

04_Vertex_buffers/00_Vertex_input_description.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ auto bindingDescription = Vertex::getBindingDescription();
202202
auto attributeDescriptions = Vertex::getAttributeDescriptions();
203203

204204
vertexInputInfo.vertexBindingDescriptionCount = 1;
205-
vertexInputInfo.vertexAttributeDescriptionCount = attributeDescriptions.size();
205+
vertexInputInfo.vertexAttributeDescriptionCount = static_cast<uint32_t>(attributeDescriptions.size());
206206
vertexInputInfo.pVertexBindingDescriptions = &bindingDescription;
207207
vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions.data();
208208
```

04_Vertex_buffers/01_Vertex_buffer_creation.md

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ We can now create the buffer with `vkCreateBuffer`. Define a class member to
7474
hold the buffer handle and call it `vertexBuffer`.
7575

7676
```c++
77-
VDeleter<VkBuffer> vertexBuffer{device, vkDestroyBuffer};
77+
VkBuffer vertexBuffer;
7878

7979
...
8080

@@ -85,12 +85,26 @@ void createVertexBuffer() {
8585
bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
8686
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
8787

88-
if (vkCreateBuffer(device, &bufferInfo, nullptr, vertexBuffer.replace()) != VK_SUCCESS) {
88+
if (vkCreateBuffer(device, &bufferInfo, nullptr, &vertexBuffer) != VK_SUCCESS) {
8989
throw std::runtime_error("failed to create vertex buffer!");
9090
}
9191
}
9292
```
9393

94+
The buffer should be available for use in rendering commands until the end of
95+
the program and it does not depend on the swap chain, so we'll clean it up in
96+
the original `cleanup` function:
97+
98+
```c++
99+
void cleanup() {
100+
cleanupSwapChain();
101+
102+
vkDestroyBuffer(device, vertexBuffer, nullptr);
103+
104+
...
105+
}
106+
```
107+
94108
## Memory requirements
95109

96110
The buffer has been created, but it doesn't actually have any memory assigned to
@@ -199,20 +213,16 @@ desired property. Create a class member to store the handle to the memory and
199213
allocate it with `vkAllocateMemory`.
200214

201215
```c++
202-
VDeleter<VkBuffer> vertexBuffer{device, vkDestroyBuffer};
203-
VDeleter<VkDeviceMemory> vertexBufferMemory{device, vkFreeMemory};
216+
VkBuffer vertexBuffer;
217+
VkDeviceMemory vertexBufferMemory;
204218

205219
...
206220

207-
if (vkAllocateMemory(device, &allocInfo, nullptr, vertexBufferMemory.replace()) != VK_SUCCESS) {
221+
if (vkAllocateMemory(device, &allocInfo, nullptr, &vertexBufferMemory) != VK_SUCCESS) {
208222
throw std::runtime_error("failed to allocate vertex buffer memory!");
209223
}
210224
```
211225

212-
Note that specifying the `vertexBuffer` and `vertexBufferMemory` members in this
213-
order will cause the memory to be freed before the buffer is destroyed, but
214-
that's allowed as long as the buffer is no longer used.
215-
216226
If memory allocation was successful, then we can now associate this memory with
217227
the buffer using `vkBindBufferMemory`:
218228

@@ -225,6 +235,19 @@ offset within the region of memory. Since this memory is allocated specifically
225235
for this the vertex buffer, the offset is simply `0`. If the offset is non-zero,
226236
then it is required to be divisible by `memRequirements.alignment`.
227237
238+
Of course, just like dynamic memory allocation in C++, the memory should be
239+
freed at some point. Memory that is bound to a buffer object may be freed once
240+
the buffer is no longer used, so let's free it after the buffer has been
241+
destroyed:
242+
243+
```c++
244+
void cleanup() {
245+
cleanupSwapChain();
246+
247+
vkDestroyBuffer(device, vertexBuffer, nullptr);
248+
vkFreeMemory(device, vertexBufferMemory, nullptr);
249+
```
250+
228251
## Filling the vertex buffer
229252

230253
It is now time to copy the vertex data to the buffer. This is done by [mapping
@@ -279,7 +302,7 @@ VkBuffer vertexBuffers[] = {vertexBuffer};
279302
VkDeviceSize offsets[] = {0};
280303
vkCmdBindVertexBuffers(commandBuffers[i], 0, 1, vertexBuffers, offsets);
281304

282-
vkCmdDraw(commandBuffers[i], vertices.size(), 1, 0, 0);
305+
vkCmdDraw(commandBuffers[i], static_cast<uint32_t>(vertices.size()), 1, 0, 0);
283306
```
284307
285308
The `vkCmdBindVertexBuffers` function is used to bind vertex buffers to

04_Vertex_buffers/02_Staging_buffer.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ to move buffer creation to a helper function. Create a new function
4444
`createBuffer` and move the code in `createVertexBuffer` (except mapping) to it.
4545

4646
```c++
47-
void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VDeleter<VkBuffer>& buffer, VDeleter<VkDeviceMemory>& bufferMemory) {
47+
void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer, VkDeviceMemory& bufferMemory) {
4848
VkBufferCreateInfo bufferInfo = {};
4949
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
5050
bufferInfo.size = size;
5151
bufferInfo.usage = usage;
5252
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
5353

54-
if (vkCreateBuffer(device, &bufferInfo, nullptr, buffer.replace()) != VK_SUCCESS) {
54+
if (vkCreateBuffer(device, &bufferInfo, nullptr, &buffer) != VK_SUCCESS) {
5555
throw std::runtime_error("failed to create buffer!");
5656
}
5757

@@ -63,7 +63,7 @@ void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyF
6363
allocInfo.allocationSize = memRequirements.size;
6464
allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties);
6565

66-
if (vkAllocateMemory(device, &allocInfo, nullptr, bufferMemory.replace()) != VK_SUCCESS) {
66+
if (vkAllocateMemory(device, &allocInfo, nullptr, &bufferMemory) != VK_SUCCESS) {
6767
throw std::runtime_error("failed to allocate buffer memory!");
6868
}
6969

@@ -101,8 +101,8 @@ as temporary buffer and use a device local one as actual vertex buffer.
101101
void createVertexBuffer() {
102102
VkDeviceSize bufferSize = sizeof(vertices[0]) * vertices.size();
103103

104-
VDeleter<VkBuffer> stagingBuffer{device, vkDestroyBuffer};
105-
VDeleter<VkDeviceMemory> stagingBufferMemory{device, vkFreeMemory};
104+
VkBuffer stagingBuffer;
105+
VkDeviceMemory stagingBufferMemory;
106106
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, stagingBuffer, stagingBufferMemory);
107107

108108
void* data;
@@ -229,6 +229,19 @@ createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERT
229229
copyBuffer(stagingBuffer, vertexBuffer, bufferSize);
230230
```
231231

232+
After copying the data from the staging buffer to the device buffer, we should
233+
clean it up:
234+
235+
```c++
236+
...
237+
238+
copyBuffer(stagingBuffer, vertexBuffer, bufferSize);
239+
240+
vkDestroyBuffer(device, stagingBuffer, nullptr);
241+
vkFreeMemory(device, stagingBufferMemory, nullptr);
242+
}
243+
```
244+
232245
Run your program to verify that you're seeing the familiar triangle again. It
233246
may not be visible, but its vertex data is now being loaded from high
234247
performance memory. This will matter when we're going to start rendering more

04_Vertex_buffers/03_Index_buffer.md

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ the GPU to be able to access them. Define two new class members to hold the
5454
resources for the index buffer:
5555

5656
```c++
57-
VDeleter<VkBuffer> vertexBuffer{device, vkDestroyBuffer};
58-
VDeleter<VkDeviceMemory> vertexBufferMemory{device, vkFreeMemory};
59-
VDeleter<VkBuffer> indexBuffer{device, vkDestroyBuffer};
60-
VDeleter<VkDeviceMemory> indexBufferMemory{device, vkFreeMemory};
57+
VkBuffer vertexBuffer;
58+
VkDeviceMemory vertexBufferMemory;
59+
VkBuffer indexBuffer;
60+
VkDeviceMemory indexBufferMemory;
6161
```
6262

6363
The `createIndexBuffer` function that we'll add now is almost identical to
@@ -74,8 +74,8 @@ void initVulkan() {
7474
void createIndexBuffer() {
7575
VkDeviceSize bufferSize = sizeof(indices[0]) * indices.size();
7676

77-
VDeleter<VkBuffer> stagingBuffer{device, vkDestroyBuffer};
78-
VDeleter<VkDeviceMemory> stagingBufferMemory{device, vkFreeMemory};
77+
VkBuffer stagingBuffer;
78+
VkDeviceMemory stagingBufferMemory;
7979
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, stagingBuffer, stagingBufferMemory);
8080

8181
void* data;
@@ -86,6 +86,9 @@ void createIndexBuffer() {
8686
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, indexBuffer, indexBufferMemory);
8787

8888
copyBuffer(stagingBuffer, indexBuffer, bufferSize);
89+
90+
vkDestroyBuffer(device, stagingBuffer, nullptr);
91+
vkFreeMemory(device, stagingBufferMemory, nullptr);
8992
}
9093
```
9194

@@ -97,6 +100,23 @@ number of indices times the size of the index type, either `uint16_t` or
97100
process is exactly the same. We create a staging buffer to copy the contents of
98101
`indices` to and then copy it to the final device local index buffer.
99102

103+
The index buffer should be cleaned up at the end of the program, just like the
104+
vertex buffer:
105+
106+
```c++
107+
void cleanup() {
108+
cleanupSwapChain();
109+
110+
vkDestroyBuffer(device, indexBuffer, nullptr);
111+
vkFreeMemory(device, indexBufferMemory, nullptr);
112+
113+
vkDestroyBuffer(device, vertexBuffer, nullptr);
114+
vkFreeMemory(device, vertexBufferMemory, nullptr);
115+
116+
...
117+
}
118+
```
119+
100120
## Using an index buffer
101121

102122
Using an index buffer for drawing involves two changes to
@@ -122,7 +142,7 @@ the drawing command to tell Vulkan to use the index buffer. Remove the
122142
`vkCmdDraw` line and replace it with `vkCmdDrawIndexed`:
123143
124144
```c++
125-
vkCmdDrawIndexed(commandBuffers[i], indices.size(), 1, 0, 0, 0);
145+
vkCmdDrawIndexed(commandBuffers[i], static_cast<uint32_t>(indices.size()), 1, 0, 0, 0);
126146
```
127147

128148
A call to this function is very similar to `vkCmdDraw`. The first two parameters

0 commit comments

Comments
 (0)