-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvkc_pipeline.cpp
459 lines (363 loc) · 24.7 KB
/
vkc_pipeline.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#include "vkc_pipeline.h"
/**
* Create the graphics pipeline.
*/
VkcPipeline::VkcPipeline(const VkcSwapchain *swapchain, const VkcDevice *device)
{
// Fill descriptor set binding info.
QVector<VkDescriptorSetLayoutBinding> setBindings =
{
{
0, // uint32_t binding;
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, // VkDescriptorType descriptorType;
1, // uint32_t descriptorCount;
VK_SHADER_STAGE_VERTEX_BIT, // VkShaderStageFlags stageFlags;
nullptr // const VkSampler* pImmutableSamplers;
},
{
10, // uint32_t binding;
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, // VkDescriptorType descriptorType;
1, // uint32_t descriptorCount;
VK_SHADER_STAGE_FRAGMENT_BIT, // VkShaderStageFlags stageFlags;
nullptr // const VkSampler* pImmutableSamplers;
}
};
// Fill desctriptor set size info.
QVector<VkDescriptorPoolSize> poolSizes =
{
{
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, // VkDescriptorType type;
1 // uint32_t descriptorCount;
},
{
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, // VkDescriptorType type;
1 // uint32_t descriptorCount;
}
};
// Fill descriptor set layout info.
VkDescriptorSetLayoutCreateInfo setLayoutInfo =
{
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkDescriptorSetLayoutCreateFlags flags;
(uint32_t)setBindings.size(), // uint32_t bindingCount;
setBindings.data() // const VkDescriptorSetLayoutBinding* pBindings;
};
// Create descriptor set layout.
vkCreateDescriptorSetLayout(device->logical, &setLayoutInfo, nullptr, &setLayout);
// Fill descriptor pool info.
VkDescriptorPoolCreateInfo descriptorPoolInfo =
{
VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, // VkDescriptorPoolCreateFlags flags;
1, // uint32_t maxSets;
(uint32_t)poolSizes.size(), // uint32_t poolSizeCount;
poolSizes.data() // const VkDescriptorPoolSize* pPoolSizes;
};
// Create descriptor pool.
vkCreateDescriptorPool(device->logical, &descriptorPoolInfo, nullptr, &descriptorPool);
// Fill descriptor set allocate info.
VkDescriptorSetAllocateInfo descriptorSetAllocateInfo =
{
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
descriptorPool, // VkDescriptorPool descriptorPool;
1, // uint32_t descriptorSetCount;
&setLayout // const VkDescriptorSetLayout* pSetLayouts;
};
// Allocate space for descriptor set.
vkAllocateDescriptorSets(device->logical, &descriptorSetAllocateInfo, &descriptorSet);
// Fill pipeline layout info.
VkPipelineLayoutCreateInfo pipelineLayoutInfo =
{
VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkPipelineLayoutCreateFlags flags;
1, // uint32_t setLayoutCount;
&setLayout, // const VkDescriptorSetLayout* pSetLayouts;
0, // uint32_t pushConstantRangeCount;
nullptr // const VkPushConstantRange* pPushConstantRanges;
};
// Create pipeline layout.
vkCreatePipelineLayout(device->logical, &pipelineLayoutInfo, nullptr, &layout);
// Create shaders.
createShader(vertShader, "shader.vert.spv", device);
createShader(fragShader, "shader.frag.spv", device);
// Fill shader stage info.
QVector<VkPipelineShaderStageCreateInfo> shaderStages =
{
{
VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkPipelineShaderStageCreateFlags flags;
VK_SHADER_STAGE_VERTEX_BIT, // VkShaderStageFlagBits stage;
vertShader, // VkShaderModule module;
"main", // const char* pName;
nullptr // const VkSpecializationInfo* pSpecializationInfo;
},
{
VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkPipelineShaderStageCreateFlags flags;
VK_SHADER_STAGE_FRAGMENT_BIT, // VkShaderStageFlagBits stage;
fragShader, // VkShaderModule module;
"main", // const char* pName;
nullptr // const VkSpecializationInfo* pSpecializationInfo;
}
};
// Fill vertex input binding description.
VkVertexInputBindingDescription vertexBinding =
{
0, // uint32_t binding;
sizeof(VkVertex), // uint32_t stride;
VK_VERTEX_INPUT_RATE_VERTEX, // VkVertexInputRate inputRate;
};
// Fill vertex input attribute description.
QVector<VkVertexInputAttributeDescription> vertexAttributes =
{
{
0, // uint32_t location;
0, // uint32_t binding;
VK_FORMAT_R32G32B32_SFLOAT, // VkFormat format;
offsetof(VkVertex, x) // uint32_t offset;
},
{
1, // uint32_t location;
0, // uint32_t binding;
VK_FORMAT_R32G32_SFLOAT, // VkFormat format;
offsetof(VkVertex, u) // uint32_t offset;
},
{
2, // uint32_t location;
0, // uint32_t binding;
VK_FORMAT_R32G32B32_SFLOAT, // VkFormat format;
offsetof(VkVertex, nx) // uint32_t offset;
}
};
// Fill vertex input state info.
VkPipelineVertexInputStateCreateInfo vertexInfo =
{
VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkPipelineVertexInputStateCreateFlags flags;
1, // uint32_t vertexBindingDescriptionCount;
&vertexBinding, // const VkVertexInputBindingDescription* pVertexBindingDescriptions;
(uint32_t)vertexAttributes.size(), // uint32_t vertexAttributeDescriptionCount;
vertexAttributes.data() // const VkVertexInputAttributeDescription* pVertexAttributeDescriptions;
};
VkPipelineInputAssemblyStateCreateInfo inputAssemblyInfo =
{
VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkPipelineInputAssemblyStateCreateFlags flags;
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, // VkPrimitiveTopology topology;
VK_FALSE, // VkBool32 primitiveRestartEnable;
};
/*
* Space reserved for VkPipelineTessellationStateCreateInfo.
*/
// Fill viewport info.
VkViewport viewport =
{
0.0f, // float x;
0.0f, // float y;
(float)swapchain->extent.width, // float width;
(float)swapchain->extent.height, // float height;
0.0f, // float minDepth;
1.0f, // float maxDepth;
};
// Fill scissors info.
VkRect2D scissors =
{
{0, 0}, // VkOffset2D offset;
swapchain->extent // VkExtent2D extent;
};
// Fill viewport state info.
VkPipelineViewportStateCreateInfo viewportInfo =
{
VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkPipelineViewportStateCreateFlags flags;
1, // uint32_t viewportCount;
&viewport, // const VkViewport* pViewports;
1, // uint32_t scissorCount;
&scissors // const VkRect2D* pScissors;
};
// Fill rasterization state info.
VkPipelineRasterizationStateCreateInfo rasterisationInfo =
{
VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkPipelineRasterizationStateCreateFlags flags;
VK_FALSE, // VkBool32 depthClampEnable;
VK_FALSE, // VkBool32 rasterizerDiscardEnable;
VK_POLYGON_MODE_FILL, // VkPolygonMode polygonMode;
VK_CULL_MODE_BACK_BIT, // VkCullModeFlags cullMode;
VK_FRONT_FACE_COUNTER_CLOCKWISE, // VkFrontFace frontFace;
VK_FALSE, // VkBool32 depthBiasEnable;
0, // float depthBiasConstantFactor;
0, // float depthBiasClamp;
0, // float depthBiasSlopeFactor;
1, // float lineWidth;
};
// Fill multisample state info.
VkPipelineMultisampleStateCreateInfo multisampleInfo =
{
VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkPipelineMultisampleStateCreateFlags flags;
VK_SAMPLE_COUNT_1_BIT, // VkSampleCountFlagBits rasterizationSamples;
VK_FALSE, // VkBool32 sampleShadingEnable;
0, // float minSampleShading;
nullptr, // const VkSampleMask* pSampleMask;
VK_FALSE, // VkBool32 alphaToCoverageEnable;
VK_FALSE, // VkBool32 alphaToOneEnable;
};
// Fill stencil operation state info.
VkStencilOpState stencilInfo =
{
VK_STENCIL_OP_KEEP, // VkStencilOp failOp;
VK_STENCIL_OP_INCREMENT_AND_CLAMP, // VkStencilOp passOp;
VK_STENCIL_OP_INCREMENT_AND_CLAMP, // VkStencilOp depthFailOp;
VK_COMPARE_OP_ALWAYS, // VkCompareOp compareOp;
0, // uint32_t compareMask;
0, // uint32_t writeMask;
0 // uint32_t reference;
};
// Fill depth state info.
VkPipelineDepthStencilStateCreateInfo depthStencilInfo =
{
VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkPipelineDepthStencilStateCreateFlags flags;
VK_TRUE, // VkBool32 depthTestEnable;
VK_TRUE, // VkBool32 depthWriteEnable;
VK_COMPARE_OP_GREATER_OR_EQUAL, // VkCompareOp depthCompareOp;
VK_FALSE, // VkBool32 depthBoundsTestEnable;
VK_TRUE, // VkBool32 stencilTestEnable;
stencilInfo, // VkStencilOpState front;
stencilInfo, // VkStencilOpState back;
0.0f, // float minDepthBounds;
0.0f // float maxDepthBounds;
};
// Fill color blend attachment state info.
VkPipelineColorBlendAttachmentState colorBlendState =
{
VK_TRUE, // VkBool32 blendEnable;
VK_BLEND_FACTOR_SRC_ALPHA, // VkBlendFactor srcColorBlendFactor;
VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, // VkBlendFactor dstColorBlendFactor;
VK_BLEND_OP_ADD, // VkBlendOp colorBlendOp;
VK_BLEND_FACTOR_SRC_ALPHA, // VkBlendFactor srcAlphaBlendFactor;
VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, // VkBlendFactor dstAlphaBlendFactor;
VK_BLEND_OP_ADD, // VkBlendOp alphaBlendOp;
VK_COLOR_COMPONENT_R_BIT | // VkColorComponentFlags colorWriteMask;
VK_COLOR_COMPONENT_G_BIT |
VK_COLOR_COMPONENT_B_BIT
};
// Fill color blend state info.
VkPipelineColorBlendStateCreateInfo colorBlendInfo =
{
VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkPipelineColorBlendStateCreateFlags flags;
VK_FALSE, // VkBool32 logicOpEnable;
VK_LOGIC_OP_NO_OP, // VkLogicOp logicOp;
1, // uint32_t attachmentCount;
&colorBlendState, // const VkPipelineColorBlendAttachmentState* pAttachments;
{1.0f, 1.0f, 1.0f, 1.0f} // float blendConstants[4];
};
// Select dynamic states.
QVector<VkDynamicState> dynamicStates =
{
VK_DYNAMIC_STATE_VIEWPORT,
VK_DYNAMIC_STATE_SCISSOR,
//VK_DYNAMIC_STATE_STENCIL_REFERENCE
};
// Fill dynamic state info.
VkPipelineDynamicStateCreateInfo dynamicStateInfo =
{
VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkPipelineDynamicStateCreateFlags flags;
(uint32_t)dynamicStates.count(), // uint32_t dynamicStateCount;
dynamicStates.data() // const VkDynamicState* pDynamicStates;
};
// Fill graphics pipeline info.
VkGraphicsPipelineCreateInfo pipelineInfo =
{
VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkPipelineCreateFlags flags;
(uint32_t)shaderStages.size(), // uint32_t stageCount;
shaderStages.data(), // const VkPipelineShaderStageCreateInfo* pStages;
&vertexInfo, // const VkPipelineVertexInputStateCreateInfo* pVertexInputState;
&inputAssemblyInfo, // const VkPipelineInputAssemblyStateCreateInfo* pInputAssemblyState;
nullptr, // const VkPipelineTessellationStateCreateInfo* pTessellationState;
&viewportInfo, // const VkPipelineViewportStateCreateInfo* pViewportState;
&rasterisationInfo, // const VkPipelineRasterizationStateCreateInfo* pRasterizationState;
&multisampleInfo, // const VkPipelineMultisampleStateCreateInfo* pMultisampleState;
&depthStencilInfo, // const VkPipelineDepthStencilStateCreateInfo* pDepthStencilState;
&colorBlendInfo, // const VkPipelineColorBlendStateCreateInfo* pColorBlendState;
&dynamicStateInfo, // const VkPipelineDynamicStateCreateInfo* pDynamicState;
layout, // VkPipelineLayout layout;
swapchain->renderPass, // VkRenderPass renderPass;
0, // uint32_t subpass;
VK_NULL_HANDLE, // VkPipeline basePipelineHandle;
0 // int32_t basePipelineIndex;
};
// Create graphics pipeline.
vkCreateGraphicsPipelines(device->logical, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &handle);
this->logicalDevice = device->logical;
}
/**
* Destroy the graphics pipeline.
*/
VkcPipeline::~VkcPipeline()
{
if (logicalDevice != VK_NULL_HANDLE)
{
if (descriptorSet != VK_NULL_HANDLE)
vkFreeDescriptorSets(logicalDevice, descriptorPool, 1, &descriptorSet);
if (setLayout != VK_NULL_HANDLE)
vkDestroyDescriptorSetLayout(logicalDevice, setLayout, nullptr);
if (descriptorPool != VK_NULL_HANDLE)
vkDestroyDescriptorPool(logicalDevice, descriptorPool, nullptr);
if (vertShader != VK_NULL_HANDLE)
vkDestroyShaderModule(logicalDevice, vertShader, nullptr);
if (fragShader != VK_NULL_HANDLE)
vkDestroyShaderModule(logicalDevice, fragShader, nullptr);
if (layout != VK_NULL_HANDLE)
vkDestroyPipelineLayout(logicalDevice, layout, nullptr);
if (handle != VK_NULL_HANDLE)
vkDestroyPipeline(logicalDevice, handle, nullptr);
}
}
/**
* Load shader module from file.
*/
VkResult VkcPipeline::createShader(VkShaderModule &shader, QString fileName, const VkcDevice *device)
{
// Open shader file in binary.
QFile shaderFile("data/shaders/" + fileName);
if(!shaderFile.exists())
{
qDebug() << "ERROR: [@qDebug] - Shader \"" << fileName << "\" not found.";
return VK_ERROR_INITIALIZATION_FAILED;
}
shaderFile.open(QIODevice::ReadOnly);
// Read shader data.
QByteArray shaderData = shaderFile.readAll();
// Fill shader module info.
VkShaderModuleCreateInfo shaderInfo =
{
VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkShaderModuleCreateFlags flags;
(size_t)shaderData.size(), // size_t codeSize;
(const uint32_t*)shaderData.data() // const uint32_t* pCode;
};
// Create shader module.
mgAssert(vkCreateShaderModule(device->logical, &shaderInfo, nullptr, &shader));
return VK_SUCCESS;
}