Skip to content

Commit b4796be

Browse files
committed
Update Vulkan code to use RAII and modern Vulkan-Hpp bindings
Refactor code to replace raw Vulkan handles with RAII wrappers from Vulkan-Hpp for better resource management and safety. Key changes include RAII usage for buffers, images, and device memory, along with modernized function calls and parameter handling. Code readability and alignment with updated Vulkan practices were also improved.
1 parent ccb6d17 commit b4796be

File tree

3 files changed

+77
-133
lines changed

3 files changed

+77
-133
lines changed

attachments/29_mipmapping.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,9 +527,6 @@ class HelloTriangleApplication {
527527
vk::ImageMemoryBarrier barrier (vk::AccessFlagBits::eTransferWrite, vk::AccessFlagBits::eTransferRead
528528
, vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::eTransferSrcOptimal
529529
, vk::QueueFamilyIgnored, vk::QueueFamilyIgnored, image);
530-
barrier.image = image;
531-
barrier.srcQueueFamilyIndex = vk::QueueFamilyIgnored;
532-
barrier.dstQueueFamilyIndex = vk::QueueFamilyIgnored;
533530
barrier.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
534531
barrier.subresourceRange.baseArrayLayer = 0;
535532
barrier.subresourceRange.layerCount = 1;

en/08_Loading_models.adoc

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,20 @@
55
== Introduction
66

77
Your program is now ready to render textured 3D meshes, but the current geometry in the `vertices` and `indices` arrays is not very interesting yet.
8-
In this chapter we're going to extend the program to load the vertices and indices from an actual model file to make the graphics card actually do some work.
8+
In this chapter, we're going to extend the program to load the vertices and indices from an actual model file to make the graphics card actually do some work.
99

1010
Many graphics API tutorials have the reader write their own OBJ loader in a chapter like this.
1111
The problem with this is that any remotely interesting 3D application will soon require features that are not supported by this file format, like skeletal animation.
1212
We _will_ load mesh data from an OBJ model in this chapter, but we'll focus more on integrating the mesh data with the program itself rather than the details of loading it from a file.
1313

14-
== Library
15-
16-
We will use the https://github.com/syoyo/tinyobjloader[tinyobjloader] library to load vertices and faces from an OBJ file.
17-
It's fast and it's easy to integrate because it's a single file library like stb_image.
18-
Go to the repository linked above and download the `tiny_obj_loader.h` file to a folder in your library directory.
19-
20-
*Visual Studio*
21-
22-
Add the directory with `tiny_obj_loader.h` in it to the `Additional Include Directories` paths.
23-
24-
image::/images/include_dirs_tinyobjloader.png[]
25-
26-
*Makefile*
27-
28-
Add the directory with `tiny_obj_loader.h` to the include directories for GCC:
29-
30-
[,text]
31-
----
32-
VULKAN_SDK_PATH = /home/user/VulkanSDK/x.x.x.x/x86_64
33-
STB_INCLUDE_PATH = /home/user/libraries/stb
34-
TINYOBJ_INCLUDE_PATH = /home/user/libraries/tinyobjloader
35-
36-
...
37-
38-
CFLAGS = -std=c++17 -I$(VULKAN_SDK_PATH)/include -I$(STB_INCLUDE_PATH) -I$(TINYOBJ_INCLUDE_PATH)
39-
----
40-
4114
== Sample mesh
4215

43-
In this chapter we won't be enabling lighting yet, so it helps to use a sample model that has lighting baked into the texture.
16+
In this chapter, we won't be enabling lighting yet, so it helps to use a sample model that has lighting baked into the texture.
4417
An easy way to find such models is to look for 3D scans on https://sketchfab.com/[Sketchfab].
4518
Many of the models on that site are available in OBJ format with a permissive license.
4619

47-
For this tutorial I've decided to go with the https://sketchfab.com/3d-models/viking-room-a49f1b8e4f5c4ecf9e1fe7d81915ad38[Viking room] model by https://sketchfab.com/nigelgoh[nigelgoh] (https://web.archive.org/web/20200428202538/https://sketchfab.com/3d-models/viking-room-a49f1b8e4f5c4ecf9e1fe7d81915ad38[CC BY 4.0]).
48-
I tweaked the size and orientation of the model to use it as a drop in replacement for the current geometry:
20+
For this tutorial, I've decided to go with the https://sketchfab.com/3d-models/viking-room-a49f1b8e4f5c4ecf9e1fe7d81915ad38[Viking room] model by https://sketchfab.com/nigelgoh[nigelgoh] (https://web.archive.org/web/20200428202538/https://sketchfab.com/3d-models/viking-room-a49f1b8e4f5c4ecf9e1fe7d81915ad38[CC BY 4.0]).
21+
I tweaked the size and orientation of the model to use it as a drop-in replacement for the current geometry:
4922

5023
* xref:attachment$viking_room.obj[viking_room.obj]
5124
* xref:attachment$viking_room.png[viking_room.png]
@@ -58,9 +31,9 @@ Put two new configuration variables in your program to define the model and text
5831

5932
[,c++]
6033
----
61-
const uint32_t WIDTH = 800;
62-
const uint32_t HEIGHT = 600;
63-
34+
constexpr uint32_t WIDTH = 800;
35+
constexpr uint32_t HEIGHT = 600;
36+
constexpr uint64_t FenceTimeout = 100000000;
6437
const std::string MODEL_PATH = "models/viking_room.obj";
6538
const std::string TEXTURE_PATH = "textures/viking_room.png";
6639
----
@@ -81,16 +54,16 @@ Replace them with non-const containers as class members:
8154
----
8255
std::vector<Vertex> vertices;
8356
std::vector<uint32_t> indices;
84-
VkBuffer vertexBuffer;
85-
VkDeviceMemory vertexBufferMemory;
57+
std::unique_ptr<vk::raii::Buffer> vertexBuffer;
58+
std::unique_ptr<vk::raii::DeviceMemory> vertexBufferMemory;
8659
----
8760

8861
You should change the type of the indices from `uint16_t` to `uint32_t`, because there are going to be a lot more vertices than 65535.
8962
Remember to also change the `vkCmdBindIndexBuffer` parameter:
9063

9164
[,c++]
9265
----
93-
vkCmdBindIndexBuffer(commandBuffer, indexBuffer, 0, VK_INDEX_TYPE_UINT32);
66+
commandBuffers[currentFrame]->bindIndexBuffer( **indexBuffer, 0, vk::IndexType::eUint32 );
9467
----
9568

9669
The tinyobjloader library is included in the same way as STB libraries.
@@ -231,7 +204,7 @@ ____
231204

232205
== Vertex deduplication
233206

234-
Unfortunately we're not really taking advantage of the index buffer yet.
207+
Unfortunately, we're not really taking advantage of the index buffer yet.
235208
The `vertices` vector contains a lot of duplicated vertex data, because many vertices are included in multiple triangles.
236209
We should keep only the unique vertices and use the index buffer to reuse them whenever they come up.
237210
A straightforward way to implement this is to use a `map` or `unordered_map` to keep track of the unique vertices and respective indices:
@@ -301,14 +274,14 @@ The hash functions for the GLM types need to be included using the following hea
301274
----
302275

303276
The hash functions are defined in the `gtx` folder, which means that it is technically still an experimental extension to GLM.
304-
Therefore you need to define `GLM_ENABLE_EXPERIMENTAL` to use it.
277+
Therefore, you need to define `GLM_ENABLE_EXPERIMENTAL` to use it.
305278
It means that the API could change with a new version of GLM in the future, but in practice the API is very stable.
306279

307280
You should now be able to successfully compile and run your program.
308281
If you check the size of `vertices`, then you'll see that it has shrunk down from 1,500,000 to 265,645!
309282
That means that each vertex is reused in an average number of ~6 triangles.
310283
This definitely saves us a lot of GPU memory.
311284

312-
In the xref:09_Generating_Mipmaps.adoc[next chapter] we'll learn about a technique to improve texture rendering.
285+
In the xref:09_Generating_Mipmaps.adoc[next chapter,] we'll learn about a technique to improve texture rendering.
313286

314287
link:/attachments/28_model_loading.cpp[C{pp} code] / link:/attachments/27_shader_depth.vert[Vertex shader] / link:/attachments/27_shader_depth.frag[Fragment shader]

0 commit comments

Comments
 (0)