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

Skip to content

Commit c79f967

Browse files
committed
Remove VDeleter from graphics pipeline chapters
1 parent 17c559c commit c79f967

9 files changed

Lines changed: 302 additions & 473 deletions

File tree

03_Drawing_a_triangle/02_Graphics_pipeline_basics/01_Shader_modules.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -350,15 +350,13 @@ Before we can pass the code to the pipeline, we have to wrap it in a
350350
do that.
351351

352352
```c++
353-
void createShaderModule(const std::vector<char>& code, VDeleter<VkShaderModule>& shaderModule) {
353+
VkShaderModule createShaderModule(const std::vector<char>& code) {
354354

355355
}
356356
```
357357
358358
The function will take a buffer with the bytecode as parameter and create a
359-
`VkShaderModule` from it. Instead of returning this handle directly, it's
360-
written to the variable specified for the second parameter, which makes it
361-
easier to wrap it in a deleter variable when calling `createShaderModule`.
359+
`VkShaderModule` from it.
362360
363361
Creating a shader module is simple, we only need to specify a pointer to the
364362
buffer with the bytecode and the length of it. This information is specified in
@@ -380,7 +378,8 @@ createInfo.pCode = codeAligned.data();
380378
The `VkShaderModule` can then be created with a call to `vkCreateShaderModule`:
381379

382380
```c++
383-
if (vkCreateShaderModule(device, &createInfo, nullptr, shaderModule.replace()) != VK_SUCCESS) {
381+
VkShaderModule shaderModule;
382+
if (vkCreateShaderModule(device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) {
384383
throw std::runtime_error("failed to create shader module!");
385384
}
386385
```
@@ -395,17 +394,26 @@ process, so instead of declaring them as class members, we'll make them local
395394
variables in the `createGraphicsPipeline` function:
396395

397396
```c++
398-
VDeleter<VkShaderModule> vertShaderModule{device, vkDestroyShaderModule};
399-
VDeleter<VkShaderModule> fragShaderModule{device, vkDestroyShaderModule};
397+
VkShaderModule vertShaderModule;
398+
VkShaderModule fragShaderModule;
400399
```
401400

402-
They will be automatically cleaned up when the graphics pipeline has been
403-
created and `createGraphicsPipeline` returns. Now just call the helper function
404-
we created and we're done:
401+
Call the helper function we created to load the shader modules:
405402

406403
```c++
407-
createShaderModule(vertShaderCode, vertShaderModule);
408-
createShaderModule(fragShaderCode, fragShaderModule);
404+
vertShaderModule = createShaderModule(vertShaderCode);
405+
fragShaderModule = createShaderModule(fragShaderCode);
406+
```
407+
408+
They should be cleaned up when the graphics pipeline has been created and
409+
`createGraphicsPipeline` returns, so make sure that they are deleted at the end
410+
of the function:
411+
412+
```c++
413+
...
414+
vkDestroyShaderModule(device, fragShaderModule, nullptr);
415+
vkDestroyShaderModule(device, vertShaderModule, nullptr);
416+
}
409417
```
410418
411419
## Shader stage creation

03_Drawing_a_triangle/02_Graphics_pipeline_basics/02_Fixed_functions.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ Create a class member to hold this object, because we'll refer to it from other
365365
functions at a later point in time:
366366

367367
```c++
368-
VDeleter<VkPipelineLayout> pipelineLayout{device, vkDestroyPipelineLayout};
368+
VkPipelineLayout pipelineLayout;
369369
```
370370

371371
And then create the object in the `createGraphicsPipeline` function:
@@ -378,14 +378,22 @@ pipelineLayoutInfo.pSetLayouts = nullptr; // Optional
378378
pipelineLayoutInfo.pushConstantRangeCount = 0; // Optional
379379
pipelineLayoutInfo.pPushConstantRanges = 0; // Optional
380380

381-
if (vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr,
382-
pipelineLayout.replace()) != VK_SUCCESS) {
381+
if (vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelineLayout) != VK_SUCCESS) {
383382
throw std::runtime_error("failed to create pipeline layout!");
384383
}
385384
```
386385

387386
The structure also specifies *push constants*, which are another way of passing
388-
dynamic values to shaders that we'll get into later.
387+
dynamic values to shaders that we may get into in a future chapter. The pipeline
388+
layout is may be referenced throughout the program's lifetime, so it should be
389+
destroyed at the end:
390+
391+
```c++
392+
void cleanup() {
393+
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
394+
...
395+
}
396+
```
389397

390398
## Conclusion
391399

03_Drawing_a_triangle/02_Graphics_pipeline_basics/03_Render_passes.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ we can create the render pass itself. Create a new class member variable to hold
173173
the `VkRenderPass` object right above the `pipelineLayout` variable:
174174

175175
```c++
176-
VDeleter<VkRenderPass> renderPass{device, vkDestroyRenderPass};
176+
VkRenderPass renderPass;
177+
VkPipelineLayout pipelineLayout;
177178
```
178179

179180
The render pass object can then be created by filling in the
@@ -189,11 +190,22 @@ renderPassInfo.pAttachments = &colorAttachment;
189190
renderPassInfo.subpassCount = 1;
190191
renderPassInfo.pSubpasses = &subpass;
191192

192-
if (vkCreateRenderPass(device, &renderPassInfo, nullptr, renderPass.replace()) != VK_SUCCESS) {
193+
if (vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass) != VK_SUCCESS) {
193194
throw std::runtime_error("failed to create render pass!");
194195
}
195196
```
196197

198+
Just like the pipeline layout, the render pass will be referenced throughout the
199+
program, so it should only be cleaned up at the end:
200+
201+
```c++
202+
void cleanup() {
203+
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
204+
vkDestroyRenderPass(device, renderPass, nullptr);
205+
...
206+
}
207+
```
208+
197209
That was a lot of work, but in the next chapter it all comes together to finally
198210
create the graphics pipeline object!
199211

03_Drawing_a_triangle/02_Graphics_pipeline_basics/04_Conclusion.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ Now prepare for the final step by creating a class member to hold the
7373
`VkPipeline` object:
7474

7575
```c++
76-
VDeleter<VkPipeline> graphicsPipeline{device, vkDestroyPipeline};
76+
VkPipeline graphicsPipeline;
7777
```
7878

7979
And finally create the graphics pipeline:
8080

8181
```c++
82-
if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, graphicsPipeline.replace()) != VK_SUCCESS) {
82+
if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS) {
8383
throw std::runtime_error("failed to create graphics pipeline!");
8484
}
8585
```
@@ -96,6 +96,17 @@ store and reuse data relevant to pipeline creation across multiple calls to
9696
stored to a file. This makes it possible to significantly speed up pipeline
9797
creation at a later time. We'll get into this in the pipeline cache chapter.
9898

99+
The graphics pipeline is required for all common drawing operations, so it
100+
should also only be destroyed at the end of the program:
101+
102+
```c++
103+
void cleanup() {
104+
vkDestroyPipeline(device, graphicsPipeline, nullptr);
105+
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
106+
...
107+
}
108+
```
109+
99110
Now run your program to confirm that all this hard work has resulted in a
100111
successful pipeline creation! We are already getting quite close to seeing
101112
something pop up on the screen. In the next couple of chapters we'll set up the

0 commit comments

Comments
 (0)