@@ -74,7 +74,7 @@ We can now create the buffer with `vkCreateBuffer`. Define a class member to
7474hold 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
96110The 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
199213allocate 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-
216226If memory allocation was successful, then we can now associate this memory with
217227the buffer using ` vkBindBufferMemory ` :
218228
@@ -225,6 +235,19 @@ offset within the region of memory. Since this memory is allocated specifically
225235for this the vertex buffer, the offset is simply `0`. If the offset is non-zero,
226236then 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
230253It is now time to copy the vertex data to the buffer. This is done by [ mapping
@@ -279,7 +302,7 @@ VkBuffer vertexBuffers[] = {vertexBuffer};
279302VkDeviceSize offsets[ ] = {0};
280303vkCmdBindVertexBuffers(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
285308The `vkCmdBindVertexBuffers` function is used to bind vertex buffers to
0 commit comments