-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvulkan_pipeline.cpp
More file actions
406 lines (356 loc) · 17.5 KB
/
vulkan_pipeline.cpp
File metadata and controls
406 lines (356 loc) · 17.5 KB
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
// Inspired from Matej Sakmary, https://github.com/MatejSakmary, Licensed under Apache License Version 2.0
#include "vulkan_pipeline.hpp"
vector<char> readFile(const string &filename)
{
// ate -> start reading at the end of the file
// binary -> read the file as binary
ifstream file(filename, ios::ate | ios::binary);
if (!file.is_open())
{
throw runtime_error("failed to open file " + filename);
}
size_t fileSize = (size_t)file.tellg();
vector<char> buffer(fileSize);
file.seekg(0);
file.read(buffer.data(), fileSize);
file.close();
return buffer;
}
VkShaderModule createShaderModule(shared_ptr<VulkanDevice> device, const vector<char> &code)
{
VkShaderModuleCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
createInfo.codeSize = code.size();
/* bytecode is a uint32 pointer not char pointer
NOTE: when casting like this we need to make sure to satisfy
the allignment requirements of uint32 -> vector does this
for us */
createInfo.pCode = reinterpret_cast<const uint32_t *>(code.data());
VkShaderModule shaderModule;
if (vkCreateShaderModule(device->device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS)
{
throw runtime_error("APP::CREATE_SHADER_MODULE::failed to create shader module");
}
return shaderModule;
}
#pragma region initializers
VkPipelineShaderStageCreateInfo VulkanPipeline::initVertexShaderStageCI( VkShaderModule module)
{
VkPipelineShaderStageCreateInfo vertexShaderStageInfo{};
vertexShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
vertexShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
vertexShaderStageInfo.module = module;
// specify entry point
vertexShaderStageInfo.pName = "main";
// Used to initialize some constants in our shader -> used to alter behavior at pipeline creation so compiler can still perform some optimalizations
vertexShaderStageInfo.pSpecializationInfo = nullptr;
return vertexShaderStageInfo;
}
VkPipelineShaderStageCreateInfo VulkanPipeline::initFragmentShaderStageCI( VkShaderModule module)
{
VkPipelineShaderStageCreateInfo fragmentShaderStageInfo{};
fragmentShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
fragmentShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
fragmentShaderStageInfo.module = module;
fragmentShaderStageInfo.pName = "main";
fragmentShaderStageInfo.pSpecializationInfo = nullptr;
return fragmentShaderStageInfo;
}
VkPipelineShaderStageCreateInfo VulkanPipeline::initComputeShaderStageCI( VkShaderModule module)
{
VkPipelineShaderStageCreateInfo computeShaderStageInfo{};
computeShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
computeShaderStageInfo.stage = VK_SHADER_STAGE_COMPUTE_BIT;
computeShaderStageInfo.module = module;
computeShaderStageInfo.pName = "main";
computeShaderStageInfo.pSpecializationInfo = nullptr;
return computeShaderStageInfo;
}
VkPipelineVertexInputStateCreateInfo VulkanPipeline::initVertexStageInputStateCI( const vector<VkVertexInputBindingDescription> &bindingDescriptions, const vector<VkVertexInputAttributeDescription> &attributeDescriptions)
{
VkPipelineVertexInputStateCreateInfo vertexInputInfo{};
vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
vertexInputInfo.vertexBindingDescriptionCount = static_cast<uint32_t>(bindingDescriptions.size());
vertexInputInfo.vertexAttributeDescriptionCount = static_cast<uint32_t>(attributeDescriptions.size());
vertexInputInfo.pVertexBindingDescriptions = bindingDescriptions.data();
vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions.data();
return vertexInputInfo;
}
VkPipelineInputAssemblyStateCreateInfo VulkanPipeline::initInputAssemblyStateCI( VkPrimitiveTopology topology, VkBool32 primitiveRestartEnable)
{
VkPipelineInputAssemblyStateCreateInfo inputAssembly{};
inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
inputAssembly.topology = topology;
// If this is set to true, it is possible to break up lines and triangles in _STRIP topology by using a special index
inputAssembly.primitiveRestartEnable = primitiveRestartEnable;
return inputAssembly;
}
VkViewport VulkanPipeline::initViewport( float x, float y, float width, float height, float mindepth, float maxdepth)
{
VkViewport viewport {};
viewport.x = x;
viewport.y = y;
// Size of the swap chain and images might differ from the WIDTH and HEIGHT of the window. The swap chain images will be used as framebuffers so use their size
viewport.width = width;
viewport.height = height;
viewport.minDepth = mindepth;
viewport.maxDepth = maxdepth;
return viewport;
}
VkPipelineViewportStateCreateInfo VulkanPipeline::initViewportStateCI( bool dynamicState, const VkViewport &viewport, const VkRect2D &scissor)
{
if(dynamicState)
{
throw runtime_error("APP::GRAPHICS_PIPELINE::INIT_VIEWPORT_STATE::Dynamic state\
not supported");
}
VkPipelineViewportStateCreateInfo viewportState{};
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewportState.viewportCount = 1;
viewportState.pViewports = &viewport;
viewportState.scissorCount = 1;
viewportState.pScissors = &scissor;
return viewportState;
}
VkPipelineViewportStateCreateInfo VulkanPipeline::initViewportStateCI(
bool dynamicState,
uint32_t viewportCount,
uint32_t scissorCount,
const vector<VkViewport> &viewports,
const vector<VkRect2D> &scissors)
{
if(dynamicState)
{
throw runtime_error("APP::GRAPHICS_PIPELINE::INIT_VIEWPORT_STATE::Dynamic state\
not supported");
}
VkPipelineViewportStateCreateInfo viewportState{};
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewportState.viewportCount = static_cast<uint32_t>(viewports.size());
viewportState.pViewports = viewports.data();
viewportState.scissorCount = static_cast<uint32_t>(scissors.size());
viewportState.pScissors = scissors.data();
return viewportState;
}
VkPipelineRasterizationStateCreateInfo VulkanPipeline::initRaserizationStateCI(
VkPolygonMode polygonMode,
VkCullModeFlags cullMode,
VkFrontFace frontFace,
VkBool32 depthClampEnable,
VkBool32 rasterizerDiscardEnable,
float lineWidth)
{
VkPipelineRasterizationStateCreateInfo rasterizer{};
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
// If this is set to true fragments that are beyond the near and far planes are clamped to them instead of discarding them
rasterizer.depthClampEnable = depthClampEnable;
// If this is true geometry never passes through the rasterizer stage
rasterizer.rasterizerDiscardEnable = rasterizerDiscardEnable;
rasterizer.polygonMode = polygonMode;
rasterizer.lineWidth = lineWidth;
rasterizer.cullMode = cullMode;
rasterizer.frontFace = frontFace;
// rasterizer can alter depth values by addig a constant value or biasing them based on fragment's slope
rasterizer.depthBiasEnable = VK_FALSE;
rasterizer.depthBiasConstantFactor = 0.0f;
rasterizer.depthBiasClamp = 0.0f;
rasterizer.depthBiasSlopeFactor = 0.0f;
return rasterizer;
}
VkPipelineMultisampleStateCreateInfo VulkanPipeline::initMultisampleStateCI(
VkBool32 enableSampleShading,
float minSampleShading,
VkSampleCountFlagBits rasterizationSamples)
{
VkPipelineMultisampleStateCreateInfo multisampling{};
multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
// Enable sample shading in the pipeline
multisampling.sampleShadingEnable = enableSampleShading;
// Min sample shading for sample shading, closer is smoother
multisampling.minSampleShading = minSampleShading;
multisampling.rasterizationSamples = rasterizationSamples;
multisampling.pSampleMask = nullptr;
multisampling.alphaToCoverageEnable = VK_FALSE;
return multisampling;
}
VkPipelineDepthStencilStateCreateInfo VulkanPipeline::initDepthStencilStateCI(
VkBool32 depthTestEnable,
VkBool32 depthWriteEnable,
VkCompareOp depthCompareOp,
VkBool32 stencilTestEnable)
{
VkPipelineDepthStencilStateCreateInfo depthStencil{};
depthStencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
depthStencil.depthTestEnable = depthTestEnable;
depthStencil.depthWriteEnable = depthWriteEnable;
depthStencil.depthCompareOp = depthCompareOp;
// Used for optional depth bounds tests - allows me to keep only the values which fall between the specified range
depthStencil.depthBoundsTestEnable = VK_FALSE;
depthStencil.minDepthBounds = 0.0f; // OPTIONAL
depthStencil.maxDepthBounds = 1.0f; // OPTIONAL
depthStencil.stencilTestEnable = stencilTestEnable;
depthStencil.front = {}; // OPTIONAL
depthStencil.back = {}; // OPTIONAL
return depthStencil;
}
VkPipelineColorBlendAttachmentState VulkanPipeline::initColorBlendAttachmentAlpha0ignore(
VkColorComponentFlags colorWriteMask,
VkBool32 blendEnable)
{
// Contains configuration per attached framebuffer
VkPipelineColorBlendAttachmentState colorBlendAttachment{};
colorBlendAttachment.colorWriteMask = colorWriteMask;
colorBlendAttachment.blendEnable = blendEnable;
colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
return colorBlendAttachment;
}
VkPipelineColorBlendAttachmentState VulkanPipeline::initColorBlendAttachmentSrcAlphaDst( VkColorComponentFlags colorWriteMask, VkBool32 blendEnable)
{
// Contains configuration per attached framebuffer
VkPipelineColorBlendAttachmentState colorBlendAttachment{};
colorBlendAttachment.colorWriteMask = colorWriteMask;
colorBlendAttachment.blendEnable = blendEnable;
colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_ONE;
colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
return colorBlendAttachment;
}
VkPipelineColorBlendAttachmentState VulkanPipeline::initColorBlendAttachment( VkColorComponentFlags colorWriteMask, VkBool32 blendEnable)
{
// Contains configuration per attached framebuffer
VkPipelineColorBlendAttachmentState colorBlendAttachment{};
colorBlendAttachment.colorWriteMask = colorWriteMask;
colorBlendAttachment.blendEnable = blendEnable;
colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE;
colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
return colorBlendAttachment;
}
VkPipelineColorBlendStateCreateInfo VulkanPipeline::initColorBlendStateCI( const VkPipelineColorBlendAttachmentState &pAttachment)
{
// Contains global color blending settings
VkPipelineColorBlendStateCreateInfo colorBlending{};
colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
// If true use bitwise blending note that this will automatically disable the first method (blend attachment) for every attached framebuffer
colorBlending.logicOpEnable = VK_FALSE;
colorBlending.logicOp = VK_LOGIC_OP_COPY; // OPTIONAL
colorBlending.attachmentCount = 1;
colorBlending.pAttachments = &pAttachment;
colorBlending.blendConstants[0] = 0.0f; // OPTIONAL
colorBlending.blendConstants[1] = 0.0f; // OPTIONAL
colorBlending.blendConstants[2] = 0.0f; // OPTIONAL
colorBlending.blendConstants[3] = 0.0f; // OPTIONAL
return colorBlending;
}
VkPipelineColorBlendStateCreateInfo VulkanPipeline::initColorBlendStateCI( uint32_t attachmentCount, const vector<VkPipelineColorBlendAttachmentState> &pAttachments)
{
// Contains global color blending settings
VkPipelineColorBlendStateCreateInfo colorBlending{};
colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
// If true use bitwise blending note that this will automatically disable the first method (blend attachment) for every attached framebuffer
colorBlending.logicOpEnable = VK_FALSE;
colorBlending.logicOp = VK_LOGIC_OP_COPY; // OPTIONAL
colorBlending.attachmentCount = attachmentCount;
colorBlending.pAttachments = pAttachments.data();
colorBlending.blendConstants[0] = 0.0f; // OPTIONAL
colorBlending.blendConstants[1] = 0.0f; // OPTIONAL
colorBlending.blendConstants[2] = 0.0f; // OPTIONAL
colorBlending.blendConstants[3] = 0.0f; // OPTIONAL
return colorBlending;
}
VkPipelineLayoutCreateInfo VulkanPipeline::initPiplineLayoutCI( const VkDescriptorSetLayout &pSetLayout)
{
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutInfo.setLayoutCount = 1;
pipelineLayoutInfo.pSetLayouts = &pSetLayout;
pipelineLayoutInfo.pushConstantRangeCount = 0;
pipelineLayoutInfo.pPushConstantRanges = nullptr;
return pipelineLayoutInfo;
}
VkPipelineLayoutCreateInfo VulkanPipeline::initPiplineLayoutCI( uint32_t setLayoutCount, const vector<VkDescriptorSetLayout> &pSetLayouts)
{
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutInfo.setLayoutCount = setLayoutCount;
pipelineLayoutInfo.pSetLayouts = pSetLayouts.data();
pipelineLayoutInfo.pushConstantRangeCount = 0; // OPTIONAL
pipelineLayoutInfo.pPushConstantRanges = nullptr; // OPTIONAL
return pipelineLayoutInfo;
}
#pragma endregion initializers
VulkanPipeline::VulkanPipeline( shared_ptr<VulkanDevice> device, const VkPipelineLayoutCreateInfo &layoutCreateInfo, const VkPipelineShaderStageCreateInfo &shaderStage) : device{device}
{
if (vkCreatePipelineLayout(device->device, &layoutCreateInfo, nullptr, &layout) != VK_SUCCESS)
{
throw runtime_error("APP::CREATE_COMPUTE_PIPELINE::failed to create compute pipeline layout");
}
VkComputePipelineCreateInfo computePipelineCI{};
computePipelineCI.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO;
computePipelineCI.layout = layout;
computePipelineCI.flags = 0;
computePipelineCI.stage = shaderStage;
if (vkCreateComputePipelines(device->device, VK_NULL_HANDLE, 1, &computePipelineCI,
nullptr, &pipeline) != VK_SUCCESS)
{
throw runtime_error("RENDERER::COMPUTE_PIPELINE::Failed to create compute pipeline");
}
}
VulkanPipeline::VulkanPipeline(
shared_ptr<VulkanDevice> device,
uint32_t stageCount,
const vector<VkPipelineShaderStageCreateInfo> pShaderStages,
const VkPipelineVertexInputStateCreateInfo &vertexInputStage,
const VkPipelineInputAssemblyStateCreateInfo &inputAssemblyState,
const VkPipelineViewportStateCreateInfo &viewportState,
const VkPipelineRasterizationStateCreateInfo &rasterizationState,
const VkPipelineMultisampleStateCreateInfo &multisampleState,
const VkPipelineDepthStencilStateCreateInfo &depthStencilState,
const VkPipelineColorBlendStateCreateInfo &colorBlendState,
const VkPipelineLayoutCreateInfo &layoutCreateInfo,
const VkRenderPass renderPass,
const uint32_t subpass) : device{device}
{
if (vkCreatePipelineLayout(device->device, &layoutCreateInfo, nullptr, &layout) != VK_SUCCESS)
{
throw runtime_error("APP::CREATE_GRAPHICS_PIPELINE::failed to create pipeline layout");
}
VkGraphicsPipelineCreateInfo pipelineInfo{};
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pipelineInfo.stageCount = stageCount;
pipelineInfo.pStages = pShaderStages.data();
pipelineInfo.pVertexInputState = &vertexInputStage;
pipelineInfo.pInputAssemblyState = &inputAssemblyState;
pipelineInfo.pViewportState = &viewportState;
pipelineInfo.pRasterizationState = &rasterizationState;
pipelineInfo.pMultisampleState = &multisampleState;
pipelineInfo.pDepthStencilState = &depthStencilState;
pipelineInfo.pColorBlendState = &colorBlendState;
pipelineInfo.layout = layout;
pipelineInfo.renderPass = renderPass;
pipelineInfo.subpass = subpass;
// Vulkan allows you to create a new graphics pipeline by deriving from an existing one
pipelineInfo.basePipelineHandle = VK_NULL_HANDLE; // OPTIONAL
pipelineInfo.basePipelineIndex = -1; // OPTIONAL
if (vkCreateGraphicsPipelines(device->device, VK_NULL_HANDLE, 1, &pipelineInfo,
nullptr, &pipeline) != VK_SUCCESS)
{
throw runtime_error("PIPELINE::CONSTRUCTOR:Failed to create graphics pipeline");
}
}
VulkanPipeline::~VulkanPipeline()
{
vkDestroyPipeline(device->device, pipeline, nullptr);
vkDestroyPipelineLayout(device->device, layout, nullptr);
}