28 VkPrimitiveTopology
topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
32 VkFrontFace
front_face = VK_FRONT_FACE_COUNTER_CLOCKWISE;
56 m_device(VK_NULL_HANDLE)
63 std::vector<VkPipelineShaderStageCreateInfo> shader_stages;
67 auto vert_code = readFile(create_info.
vert_path);
68 VkShaderModule vert_module = createShaderModule(vert_code);
69 VkPipelineShaderStageCreateInfo vert_stage_info = {};
70 vert_stage_info.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
71 vert_stage_info.stage = VK_SHADER_STAGE_VERTEX_BIT;
72 vert_stage_info.module = vert_module;
73 vert_stage_info.pName =
"main";
75 shader_stages.push_back(vert_stage_info);
80 auto geom_code = readFile(create_info.
geom_path);
81 VkShaderModule geom_module = createShaderModule(geom_code);
82 VkPipelineShaderStageCreateInfo geom_stage_info = {};
83 geom_stage_info.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
84 geom_stage_info.stage = VK_SHADER_STAGE_GEOMETRY_BIT;
85 geom_stage_info.module = geom_module;
86 geom_stage_info.pName =
"main";
88 shader_stages.push_back(geom_stage_info);
93 auto frag_code = readFile(create_info.
frag_path);
94 VkShaderModule frag_module = createShaderModule(frag_code);
95 VkPipelineShaderStageCreateInfo frag_stage_info = {};
96 frag_stage_info.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
97 frag_stage_info.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
98 frag_stage_info.module = frag_module;
99 frag_stage_info.pName =
"main";
101 shader_stages.push_back(frag_stage_info);
105 VkPipelineVertexInputStateCreateInfo vertex_input_info = {};
106 vertex_input_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
107 vertex_input_info.vertexBindingDescriptionCount = 0;
108 vertex_input_info.pVertexBindingDescriptions =
nullptr;
109 vertex_input_info.vertexAttributeDescriptionCount = 0;
110 vertex_input_info.pVertexAttributeDescriptions =
nullptr;
114 vertex_input_info.vertexBindingDescriptionCount = 1;
119 vertex_input_info.vertexAttributeDescriptionCount =
static_cast<uint32_t
>(create_info.
attribute_descriptions.size());
124 VkPipelineInputAssemblyStateCreateInfo input_assembly = {};
125 input_assembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
126 input_assembly.topology = create_info.
topology;
127 input_assembly.primitiveRestartEnable = VK_FALSE;
130 VkPipelineDynamicStateCreateInfo dynamic_state{};
131 dynamic_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
132 dynamic_state.dynamicStateCount =
static_cast<uint32_t
>(create_info.
dynamic_states.size());
136 VkViewport viewport = {};
139 viewport.width =
static_cast<float>(create_info.
extent.width);
140 viewport.height =
static_cast<float>(create_info.
extent.height);
141 viewport.minDepth = 0.0f;
142 viewport.maxDepth = 1.0f;
144 VkRect2D scissor = {};
145 scissor.offset = { 0, 0 };
146 scissor.extent = create_info.
extent;
148 VkPipelineViewportStateCreateInfo viewport_state = {};
149 viewport_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
150 viewport_state.viewportCount = 1;
151 viewport_state.scissorCount = 1;
154 viewport_state.pViewports = &viewport;
158 viewport_state.pScissors = &scissor;
162 VkPipelineRasterizationStateCreateInfo rasterizer = {};
163 rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
164 rasterizer.depthClampEnable = VK_FALSE;
165 rasterizer.rasterizerDiscardEnable = VK_FALSE;
167 rasterizer.lineWidth = 1.0f;
168 rasterizer.cullMode = create_info.
cull_mode;
169 rasterizer.frontFace = create_info.
front_face;
175 VkPipelineMultisampleStateCreateInfo multisampling = {};
176 multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
177 multisampling.sampleShadingEnable = VK_FALSE;
178 multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
181 VkPipelineColorBlendAttachmentState color_blend_attachment = {};
182 color_blend_attachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
183 color_blend_attachment.blendEnable = VK_FALSE;
187 color_blend_attachment.blendEnable = VK_TRUE;
188 color_blend_attachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
189 color_blend_attachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
190 color_blend_attachment.colorBlendOp = VK_BLEND_OP_ADD;
191 color_blend_attachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
192 color_blend_attachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
193 color_blend_attachment.alphaBlendOp = VK_BLEND_OP_ADD;
196 std::vector<VkPipelineColorBlendAttachmentState> color_blend_attachments(create_info.
color_formats.size(), color_blend_attachment);
198 VkPipelineColorBlendStateCreateInfo color_blending = {};
199 color_blending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
200 color_blending.logicOpEnable = VK_FALSE;
201 color_blending.attachmentCount =
static_cast<uint32_t
>(color_blend_attachments.size());
202 color_blending.pAttachments = color_blend_attachments.data();
205 VkPipelineDepthStencilStateCreateInfo depth_stencil = {};
206 depth_stencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
207 depth_stencil.depthTestEnable = VK_TRUE;
208 depth_stencil.depthWriteEnable = VK_TRUE;
209 depth_stencil.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
210 depth_stencil.depthBoundsTestEnable = VK_FALSE;
211 depth_stencil.stencilTestEnable = VK_FALSE;
215 depth_stencil.depthTestEnable = VK_TRUE;
216 depth_stencil.depthWriteEnable = VK_TRUE;
220 VkPipelineRenderingCreateInfo rendering_info = {};
221 rendering_info.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO;
222 rendering_info.colorAttachmentCount =
static_cast<uint32_t
>(create_info.
color_formats.size());
223 rendering_info.pColorAttachmentFormats = create_info.
color_formats.data();
224 rendering_info.depthAttachmentFormat = create_info.
depth_format;
227 VkPipelineLayoutCreateInfo pipeline_layout_info = {};
228 pipeline_layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
231 pipeline_layout_info.pushConstantRangeCount =
static_cast<uint32_t
>(create_info.
push_constant_ranges.size());
235 vkCreatePipelineLayout(m_device, &pipeline_layout_info,
nullptr, &
layout),
236 "Failed to create pipeline layout"
239 VkGraphicsPipelineCreateInfo pipeline_info = {};
240 pipeline_info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
241 pipeline_info.stageCount =
static_cast<uint32_t
>(shader_stages.size());
242 pipeline_info.pStages = shader_stages.data();
243 pipeline_info.pVertexInputState = &vertex_input_info;
244 pipeline_info.pInputAssemblyState = &input_assembly;
245 pipeline_info.pViewportState = &viewport_state;
246 pipeline_info.pRasterizationState = &rasterizer;
247 pipeline_info.pMultisampleState = &multisampling;
248 pipeline_info.pColorBlendState = &color_blending;
249 pipeline_info.pDepthStencilState = &depth_stencil;
250 pipeline_info.pDynamicState = &dynamic_state;
251 pipeline_info.layout =
layout;
255 pipeline_info.pNext = &rendering_info;
259 pipeline_info.renderPass = create_info.
render_pass;
260 pipeline_info.subpass = create_info.
subpass;
264 vkCreateGraphicsPipelines(m_device, VK_NULL_HANDLE, 1, &pipeline_info,
nullptr, &
pipeline),
265 "Failed to create graphics pipeline"
268 for (
auto & shader_stage : shader_stages)
270 vkDestroyShaderModule(m_device, shader_stage.module,
nullptr);
281 m_device(other.m_device)
283 other.pipeline = VK_NULL_HANDLE;
284 other.layout = VK_NULL_HANDLE;
285 other.m_device = VK_NULL_HANDLE;
296 m_device = other.m_device;
298 other.pipeline = VK_NULL_HANDLE;
299 other.layout = VK_NULL_HANDLE;
300 other.m_device = VK_NULL_HANDLE;
313 if (m_device != VK_NULL_HANDLE)
317 vkDestroyPipeline(m_device,
pipeline,
nullptr);
321 if (
layout != VK_NULL_HANDLE)
323 vkDestroyPipelineLayout(m_device,
layout,
nullptr);
326 m_device = VK_NULL_HANDLE;
337 std::vector<char> readFile(
const std::string & filename)
342 std::ios::ate | std::ios::binary
347 throw std::runtime_error(
"Failed to open file: " + filename);
350 size_t file_size =
static_cast<size_t>(file.tellg());
351 std::vector<char> buffer(file_size);
354 file.read(buffer.data(), file_size);
360 VkShaderModule createShaderModule(
const std::vector<char> & code)
362 VkShaderModuleCreateInfo create_info = {};
363 create_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
364 create_info.codeSize = code.size();
365 create_info.pCode =
reinterpret_cast<const uint32_t *
>(code.data());
367 VkShaderModule shader_module;
369 vkCreateShaderModule(m_device, &create_info,
nullptr, &shader_module),
370 "Failed to create shader module"
373 return shader_module;
Definition: Pipeline.hpp:13
Pipeline & operator=(Pipeline &&other) noexcept
Definition: Pipeline.hpp:288
VkPipeline pipeline
Definition: Pipeline.hpp:330
Pipeline(VkDevice device, const CreateInfo &create_info)
Definition: Pipeline.hpp:60
~Pipeline()
Definition: Pipeline.hpp:306
Pipeline & operator=(const Pipeline &)=delete
VkPipelineLayout layout
Definition: Pipeline.hpp:331
void clear()
Definition: Pipeline.hpp:311
Pipeline()
Definition: Pipeline.hpp:53
Pipeline(const Pipeline &)=delete
Pipeline(Pipeline &&other) noexcept
Definition: Pipeline.hpp:278
Definition: Pipeline.hpp:18
std::vector< VkVertexInputAttributeDescription > attribute_descriptions
Definition: Pipeline.hpp:26
VkPolygonMode polygon_mode
Definition: Pipeline.hpp:30
bool enable_alpha_blending
Definition: Pipeline.hpp:50
uint32_t subpass
Definition: Pipeline.hpp:46
VkCullModeFlags cull_mode
Definition: Pipeline.hpp:31
VkFormat depth_format
Definition: Pipeline.hpp:35
std::string geom_path
Definition: Pipeline.hpp:22
std::vector< VkDescriptorSetLayout > descriptor_set_layouts
Definition: Pipeline.hpp:42
VkFrontFace front_face
Definition: Pipeline.hpp:32
std::vector< VkDynamicState > dynamic_states
Definition: Pipeline.hpp:48
std::optional< VkVertexInputBindingDescription > binding_description
Definition: Pipeline.hpp:25
std::string frag_path
Definition: Pipeline.hpp:23
std::vector< VkFormat > color_formats
Definition: Pipeline.hpp:34
std::string vert_path
Definition: Pipeline.hpp:21
VkBool32 depth_bias_enable
Definition: Pipeline.hpp:37
float depth_bias_clamp
Definition: Pipeline.hpp:39
VkRenderPass render_pass
Definition: Pipeline.hpp:45
std::vector< VkPushConstantRange > push_constant_ranges
Definition: Pipeline.hpp:43
VkExtent2D extent
Definition: Pipeline.hpp:19
float depth_bias_slope_factor
Definition: Pipeline.hpp:40
float depth_bias_constant_factor
Definition: Pipeline.hpp:38
VkPrimitiveTopology topology
Definition: Pipeline.hpp:28
#define VK_CHECK(function, message)
Definition: vk_define.hpp:11