Lugdunum  0.1.0
GraphicsPipeline.cpp
Go to the documentation of this file.
2 
4 
5 namespace lug {
6 namespace Graphics {
7 namespace Vulkan {
8 namespace API {
9 namespace Builder {
10 
11 GraphicsPipeline::GraphicsPipeline(const API::Device& device) : _device(device) {}
12 
13 bool GraphicsPipeline::build(API::GraphicsPipeline& graphicPipeline, VkResult* returnResult) {
14  // Vertex input state
15  const VkPipelineVertexInputStateCreateInfo vertexInputCreateInfo{
16  /* vertexInputCreateInfo.sType */ VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
17  /* vertexInputCreateInfo.pNext */ nullptr,
18  /* vertexInputCreateInfo.flags */ 0,
19  /* vertexInputCreateInfo.vertexBindingDescriptionCount */ static_cast<uint32_t>(_inputBindings.size()),
20  /* vertexInputCreateInfo.pVertexBindingDescriptions */ _inputBindings.data(),
21  /* vertexInputCreateInfo.vertexAttributeDescriptionCount */ static_cast<uint32_t>(_inputAttributes.size()),
22  /* vertexInputCreateInfo.pVertexAttributeDescriptions */ _inputAttributes.data()
23  };
24 
25  // Input assembly state
26  const VkPipelineInputAssemblyStateCreateInfo inputAssemblyCreateInfo{
27  /* inputAssemblyCreateInfo.sType */ VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
28  /* inputAssemblyCreateInfo.pNext */ nullptr,
29  /* inputAssemblyCreateInfo.flags */ 0,
30  /* inputAssemblyCreateInfo.topology */ _topology,
31  /* inputAssemblyCreateInfo.primitiveRestartEnable */ _primitiveRestartEnable
32  };
33 
34  // Viewport state
35  _viewportState.viewportCount = static_cast<uint32_t>(_viewports.size());
36  _viewportState.pViewports = _viewports.data();
37  _viewportState.scissorCount = static_cast<uint32_t>(_scissors.size());
38  _viewportState.pScissors = _scissors.data();
39 
40  // Color Blend state
41  _colorBlendState.attachmentCount = static_cast<uint32_t>(_colorBlendAttachments.size());
42  _colorBlendState.pAttachments = _colorBlendAttachments.data();
43 
44  // Dynamic state
45  std::vector<VkDynamicState> dynamicStates(_dynamicStates.begin(), _dynamicStates.end());
46 
47  const VkPipelineDynamicStateCreateInfo dynamicStateCreateInfo{
48  /* dynamicStateCreateInfo.sType */ VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
49  /* dynamicStateCreateInfo.pNext */ nullptr,
50  /* dynamicStateCreateInfo.flags */ 0,
51  /* dynamicStateCreateInfo.dynamicStateCount */ static_cast<uint32_t>(dynamicStates.size()),
52  /* dynamicStateCreateInfo.pDynamicStates */ dynamicStates.data()
53  };
54 
55  // Graphics Pipeline
56  const VkGraphicsPipelineCreateInfo createInfo{
57  /* createInfo.sType */ VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
58  /* createInfo.pNext */ nullptr,
59  /* createInfo.flags */ 0,
60  /* createInfo.stageCount */ static_cast<uint32_t>(_shaderStages.size()),
61  /* createInfo.pStages */ _shaderStages.data(),
62  /* createInfo.pVertexInputState */ &vertexInputCreateInfo,
63  /* createInfo.pInputAssemblyState */ &inputAssemblyCreateInfo,
64  /* createInfo.pTessellationState */ nullptr,
65  /* createInfo.pViewportState */ &_viewportState,
66  /* createInfo.pRasterizationState */ &_rasterizationState,
67  /* createInfo.pMultisampleState */ &_multisampleState,
68  /* createInfo.pDepthStencilState */ &_depthStencilState,
69  /* createInfo.pColorBlendState */ &_colorBlendState,
70  /* createInfo.pDynamicState */ &dynamicStateCreateInfo,
71  /* createInfo.layout */ static_cast<VkPipelineLayout>(_pipelineLayout),
72  /* createInfo.renderPass */ static_cast<VkRenderPass>(_renderPass),
73  /* createInfo.subpass */ _subpass,
74  /* createInfo.basePipelineHandle */ VK_NULL_HANDLE,
75  /* createInfo.basePipelineIndex */ 0
76  };
77 
78  // Create the graphics pipeline
79  VkPipeline vkGraphicPipeline = VK_NULL_HANDLE;
80  VkResult result = vkCreateGraphicsPipelines(static_cast<VkDevice>(_device), _pipelineCache, 1, &createInfo, nullptr, &vkGraphicPipeline);
81 
82  if (returnResult) {
83  *returnResult = result;
84  }
85 
86  if (result != VK_SUCCESS) {
87  return false;
88  }
89 
90  graphicPipeline = API::GraphicsPipeline(vkGraphicPipeline, &_device, std::move(_renderPass), std::move(_pipelineLayout));
91 
92  return true;
93 }
94 
95 std::unique_ptr<API::GraphicsPipeline> GraphicsPipeline::build(VkResult* returnResult) {
96  std::unique_ptr<API::GraphicsPipeline> graphicPipeline = std::make_unique<API::GraphicsPipeline>();
97  return build(*graphicPipeline, returnResult) ? std::move(graphicPipeline) : nullptr;
98 }
99 
100 } // Builder
101 } // API
102 } // Vulkan
103 } // Graphics
104 } // lug
VkPipelineRasterizationStateCreateInfo _rasterizationState
std::vector< VkPipelineShaderStageCreateInfo > _shaderStages
std::vector< VkVertexInputAttributeDescription > _inputAttributes
bool build(API::GraphicsPipeline &graphicPipeline, VkResult *returnResult=nullptr)
VkPipelineMultisampleStateCreateInfo _multisampleState
VkPipelineColorBlendStateCreateInfo _colorBlendState
std::vector< VkPipelineColorBlendAttachmentState > _colorBlendAttachments
std::vector< VkVertexInputBindingDescription > _inputBindings
VkPipelineDepthStencilStateCreateInfo _depthStencilState