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

Skip to content

Commit d878474

Browse files
committed
Clarify the purpose of shader modules and their lifetime requirements (fixes #121)
1 parent 224b98c commit d878474

1 file changed

Lines changed: 12 additions & 23 deletions

File tree

03_Drawing_a_triangle/02_Graphics_pipeline_basics/01_Shader_modules.md

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ We're now going to compile these into SPIR-V bytecode using the
241241
Create a `compile.bat` file with the following contents:
242242

243243
```bash
244-
C:/VulkanSDK/1.0.17.0/Bin32/glslangValidator.exe -V shader.vert
245-
C:/VulkanSDK/1.0.17.0/Bin32/glslangValidator.exe -V shader.frag
244+
C:/VulkanSDK/x.x.x.x/Bin32/glslangValidator.exe -V shader.vert
245+
C:/VulkanSDK/x.x.x.x/Bin32/glslangValidator.exe -V shader.frag
246246
pause
247247
```
248248

@@ -341,7 +341,7 @@ void createGraphicsPipeline() {
341341
```
342342

343343
Make sure that the shaders are loaded correctly by printing the size of the
344-
buffers and checking if they match the actual file size in bytes.
344+
buffers and checking if they match the actual file size in bytes. Note that the code doesn't need to be null terminated since it's binary code and we will later be explicit about its size.
345345

346346
## Creating shader modules
347347

@@ -394,25 +394,18 @@ shader module:
394394
return shaderModule;
395395
```
396396

397-
The shader module objects are only required during the pipeline creation
398-
process, so instead of declaring them as class members, we'll make them local
399-
variables in the `createGraphicsPipeline` function:
397+
Shader modules are just a thin wrapper around the shader bytecode that we've previously loaded from a file and the functions defined in it. The compilation and linking of the SPIR-V bytecode to machine code for execution by the GPU doesn't happen until the graphics pipeline is created. That means that we're allowed to destroy the shader modules again as soon as pipeline creation is finished, which is why we'll make them local variables in the `createGraphicsPipeline` function instead of class members:
400398

401399
```c++
402-
VkShaderModule vertShaderModule;
403-
VkShaderModule fragShaderModule;
404-
```
405-
406-
Call the helper function we created to load the shader modules:
400+
void createGraphicsPipeline() {
401+
auto vertShaderCode = readFile("shaders/vert.spv");
402+
auto fragShaderCode = readFile("shaders/frag.spv");
407403

408-
```c++
409-
vertShaderModule = createShaderModule(vertShaderCode);
410-
fragShaderModule = createShaderModule(fragShaderCode);
404+
VkShaderModule vertShaderModule = createShaderModule(vertShaderCode);
405+
VkShaderModule fragShaderModule = createShaderModule(fragShaderCode);
411406
```
412407
413-
They should be cleaned up when the graphics pipeline has been created and
414-
`createGraphicsPipeline` returns, so make sure that they are deleted at the end
415-
of the function:
408+
The cleanup should then happen at the end of the function by adding two calls to `vkDestroyShaderModule`. ALl of the remaining code in this chapter will be inserted before these lines.
416409
417410
```c++
418411
...
@@ -423,11 +416,7 @@ of the function:
423416

424417
## Shader stage creation
425418

426-
The `VkShaderModule` object is just a dumb wrapper around the bytecode buffer.
427-
The shaders aren't linked to each other yet and they haven't even been given a
428-
purpose yet. Assigning a shader module to either the vertex or fragment shader
429-
stage in the pipeline happens through a `VkPipelineShaderStageCreateInfo`
430-
structure, which is part of the actual pipeline creation process.
419+
To actually use the shaders we'll need to assign them to a specific pipeline stage through `VkPipelineShaderStageCreateInfo` structures as part of the actual pipeline creation process.
431420

432421
We'll start by filling in the structure for the vertex shader, again in the
433422
`createGraphicsPipeline` function.
@@ -448,7 +437,7 @@ vertShaderStageInfo.pName = "main";
448437
```
449438

450439
The next two members specify the shader module containing the code, and the
451-
function to invoke. That means that it's possible to combine multiple fragment
440+
function to invoke, known as the *entrypoint*. That means that it's possible to combine multiple fragment
452441
shaders into a single shader module and use different entry points to
453442
differentiate between their behaviors. In this case we'll stick to the standard
454443
`main`, however.

0 commit comments

Comments
 (0)