Lugdunum  0.1.0
GraphicsPipeline.inl
Go to the documentation of this file.
1 // GraphicsPipeline::InputBinding
2 
3 
4 inline GraphicsPipeline::InputBinding::InputBinding(GraphicsPipeline& graphicPipelineBuilder, uint32_t idx) : _graphicPipelineBuilder(graphicPipelineBuilder), _idx(idx) {}
5 
6 inline void GraphicsPipeline::InputBinding::addAttributes(VkFormat format, uint32_t offset) {
8  return addAttributes(0, format, offset);
9  } else {
10  return addAttributes(_graphicPipelineBuilder._inputAttributes.back().location + 1, format, offset);
11  }
12 }
13 
14 inline void GraphicsPipeline::InputBinding::addAttributes(uint32_t location, VkFormat format, uint32_t offset) {
15  const VkVertexInputAttributeDescription inputAttribute{
16  /* inputAttribute.location */ location,
17  /* inputAttribute.binding */ _graphicPipelineBuilder._inputBindings[_idx].binding,
18  /* inputAttribute.format */ format,
19  /* inputAttribute.offset */ offset
20  };
21 
22  _graphicPipelineBuilder._inputAttributes.push_back(std::move(inputAttribute));
23 }
24 
25 
26 // GraphicsPipeline::ViewportState
27 
28 
29 inline GraphicsPipeline::ViewportState::ViewportState(GraphicsPipeline& graphicPipelineBuilder) : _graphicPipelineBuilder(graphicPipelineBuilder) {}
30 
31 inline void GraphicsPipeline::ViewportState::addViewport(const VkViewport& viewport) {
32  _graphicPipelineBuilder._viewports.push_back(viewport);
33 }
34 
35 inline void GraphicsPipeline::ViewportState::addScissor(const VkRect2D& scissor) {
36  _graphicPipelineBuilder._scissors.push_back(scissor);
37 }
38 
39 
40 // GraphicsPipeline::RasterizationState
41 
42 
44 
46  _graphicPipelineBuilder._rasterizationState.depthClampEnable = VK_TRUE;
47 }
48 
50  _graphicPipelineBuilder._rasterizationState.depthClampEnable = VK_FALSE;
51 }
52 
54  _graphicPipelineBuilder._rasterizationState.rasterizerDiscardEnable = VK_TRUE;
55 }
56 
58  _graphicPipelineBuilder._rasterizationState.rasterizerDiscardEnable = VK_FALSE;
59 }
60 
61 inline void GraphicsPipeline::RasterizationState::setPolygonMode(VkPolygonMode polygonMode) {
62  _graphicPipelineBuilder._rasterizationState.polygonMode = polygonMode;
63 }
64 
65 inline void GraphicsPipeline::RasterizationState::setCullMode(VkCullModeFlagBits cullMode) {
66  _graphicPipelineBuilder._rasterizationState.cullMode = cullMode;
67 }
68 
69 inline void GraphicsPipeline::RasterizationState::setFrontFace(VkFrontFace frontFace) {
70  _graphicPipelineBuilder._rasterizationState.frontFace = frontFace;
71 }
72 
73 inline void GraphicsPipeline::RasterizationState::enableDepthBias(float constantFactor, float clamp, float slopeFactor) {
74  _graphicPipelineBuilder._rasterizationState.depthBiasEnable = VK_TRUE;
75  _graphicPipelineBuilder._rasterizationState.depthBiasConstantFactor = constantFactor;
76  _graphicPipelineBuilder._rasterizationState.depthBiasClamp = clamp;
77  _graphicPipelineBuilder._rasterizationState.depthBiasSlopeFactor = slopeFactor;
78 }
79 
81  _graphicPipelineBuilder._rasterizationState.depthBiasEnable = VK_FALSE;
82 }
83 
85  _graphicPipelineBuilder._rasterizationState.lineWidth = lineWidth;
86 }
87 
88 
89 // GraphicsPipeline::MultisampleState
90 
91 
92 inline GraphicsPipeline::MultisampleState::MultisampleState(GraphicsPipeline& graphicPipelineBuilder) : _graphicPipelineBuilder(graphicPipelineBuilder) {}
93 
94 inline void GraphicsPipeline::MultisampleState::setRasterizationSamples(VkSampleCountFlagBits rasterizationSamples) {
95  _graphicPipelineBuilder._multisampleState.rasterizationSamples = rasterizationSamples;
96 }
97 
99  _graphicPipelineBuilder._multisampleState.sampleShadingEnable = VK_TRUE;
100 }
101 
103  _graphicPipelineBuilder._multisampleState.sampleShadingEnable = VK_FALSE;
104 }
105 
106 inline void GraphicsPipeline::MultisampleState::setMinSampleShading(float minSampleShading) {
107  _graphicPipelineBuilder._multisampleState.minSampleShading = minSampleShading;
108 }
109 
111  _graphicPipelineBuilder._multisampleState.alphaToCoverageEnable = VK_TRUE;
112 }
113 
115  _graphicPipelineBuilder._multisampleState.alphaToCoverageEnable = VK_FALSE;
116 }
117 
119  _graphicPipelineBuilder._multisampleState.alphaToOneEnable = VK_TRUE;
120 }
121 
123  _graphicPipelineBuilder._multisampleState.alphaToOneEnable = VK_FALSE;
124 }
125 
126 
127 // GraphicsPipeline::DepthStencilState
128 
129 
130 inline GraphicsPipeline::DepthStencilState::DepthStencilState(GraphicsPipeline& graphicPipelineBuilder) : _graphicPipelineBuilder(graphicPipelineBuilder) {}
131 
132 inline void GraphicsPipeline::DepthStencilState::enableDepthTest(VkCompareOp depthCompareOp) {
133  _graphicPipelineBuilder._depthStencilState.depthTestEnable = VK_TRUE;
134  _graphicPipelineBuilder._depthStencilState.depthCompareOp = depthCompareOp;
135 }
136 
138  _graphicPipelineBuilder._depthStencilState.depthTestEnable = VK_FALSE;
139 }
140 
142  _graphicPipelineBuilder._depthStencilState.depthWriteEnable = VK_TRUE;
143 }
144 
146  _graphicPipelineBuilder._depthStencilState.depthWriteEnable = VK_FALSE;
147 }
148 
149 inline void GraphicsPipeline::DepthStencilState::enableDepthBoundsTest(float minDepthBounds, float maxDepthBounds) {
150  _graphicPipelineBuilder._depthStencilState.depthBoundsTestEnable = VK_TRUE;
151  _graphicPipelineBuilder._depthStencilState.minDepthBounds = minDepthBounds;
152  _graphicPipelineBuilder._depthStencilState.maxDepthBounds = maxDepthBounds;
153 }
154 
156  _graphicPipelineBuilder._depthStencilState.depthBoundsTestEnable = VK_FALSE;
157 }
158 
159 inline void GraphicsPipeline::DepthStencilState::enableStencilTest(const VkStencilOpState& front, const VkStencilOpState& back) {
160  _graphicPipelineBuilder._depthStencilState.stencilTestEnable = VK_TRUE;
163 }
164 
166  _graphicPipelineBuilder._depthStencilState.stencilTestEnable = VK_FALSE;
167 }
168 
169 
170 // GraphicsPipeline::ColorBlendState
171 
172 
173 inline GraphicsPipeline::ColorBlendState::ColorBlendState(GraphicsPipeline& graphicPipelineBuilder) : _graphicPipelineBuilder(graphicPipelineBuilder) {}
174 
175 inline void GraphicsPipeline::ColorBlendState::enableLogicOp(VkLogicOp logicOp) {
176  _graphicPipelineBuilder._colorBlendState.logicOpEnable = VK_TRUE;
177  _graphicPipelineBuilder._colorBlendState.logicOp = logicOp;
178 }
179 
181  _graphicPipelineBuilder._colorBlendState.logicOpEnable = VK_FALSE;
182 }
183 
184 inline void GraphicsPipeline::ColorBlendState::addAttachment(const VkPipelineColorBlendAttachmentState& attachment) {
185  _graphicPipelineBuilder._colorBlendAttachments.push_back(attachment);
186 }
187 
188 inline void GraphicsPipeline::ColorBlendState::setBlendConstants(const float blendConstants[4]) {
189  for (uint8_t i = 0; i < 4; ++i) {
190  _graphicPipelineBuilder._colorBlendState.blendConstants[i] = blendConstants[i];
191  }
192 }
193 
194 
195 // GraphicsPipeline
196 
197 inline void GraphicsPipeline::setShader(VkShaderStageFlagBits stage, const char* entry, API::ShaderModule shaderModule) {
198  const VkPipelineShaderStageCreateInfo shaderStage{
199  /* shaderStage.sType */ VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
200  /* shaderStage.pNext */ nullptr,
201  /* shaderStage.flags */ 0,
202  /* shaderStage.stage */ stage,
203  /* shaderStage.module */ static_cast<VkShaderModule>(shaderModule),
204  /* shaderStage.pName */ entry,
205  /* shaderStage.pSpecializationInfo */ nullptr
206  };
207 
208  _shaderStages.push_back(std::move(shaderStage));
209  _shaderModules.push_back(std::move(shaderModule));
210 }
211 
212 inline bool GraphicsPipeline::setShaderFromFile(VkShaderStageFlagBits stage, const char* entry, const std::string& filename) {
213  API::Builder::ShaderModule shaderModuleBuilder(_device);
214 
215  if (!shaderModuleBuilder.loadFromFile(filename)) {
216  LUG_LOG.error("Builder::GraphicsPipeline: Create load from file {}", filename);
217  return false;
218  }
219 
220  VkResult result{ VK_SUCCESS };
221  API::ShaderModule shaderModule;
222  if (!shaderModuleBuilder.build(shaderModule, &result)) {
223  LUG_LOG.error("Builder::GraphicsPipeline: Create create shader from file {}: {}", filename, result);
224  return false;
225  }
226 
227  setShader(stage, entry, std::move(shaderModule));
228  return true;
229 }
230 
231 inline bool GraphicsPipeline::setShaderFromData(VkShaderStageFlagBits stage, const char* entry, const std::vector<uint32_t>& data) {
232  API::Builder::ShaderModule shaderModuleBuilder(_device);
233 
234  shaderModuleBuilder.loadFromData(data);
235 
236  VkResult result{VK_SUCCESS};
237  API::ShaderModule shaderModule;
238  if (!shaderModuleBuilder.build(shaderModule, &result)) {
239  LUG_LOG.error("Builder::GraphicsPipeline: Create create shader from content: {}", result);
240  return false;
241  }
242 
243  setShader(stage, entry, std::move(shaderModule));
244  return true;
245 }
246 
247 inline GraphicsPipeline::InputBinding GraphicsPipeline::addInputBinding(uint32_t stride, VkVertexInputRate inputRate) {
248  if (_inputBindings.size() == 0) {
249  return addInputBinding(0, stride, inputRate);
250  } else {
251  return addInputBinding(_inputBindings.back().binding + 1, stride, inputRate);
252  }
253 }
254 
255 inline GraphicsPipeline::InputBinding GraphicsPipeline::addInputBinding(uint32_t binding, uint32_t stride, VkVertexInputRate inputRate) {
256  const VkVertexInputBindingDescription inputBinding{
257  /* inputBinding.binding */ binding,
258  /* inputBinding.stride */ stride,
259  /* inputBinding.inputRate */ inputRate
260  };
261 
262  _inputBindings.push_back(std::move(inputBinding));
263  return GraphicsPipeline::InputBinding(*this, static_cast<uint32_t>(_inputBindings.size() - 1));
264 }
265 
266 inline void GraphicsPipeline::setInputAssemblyInfo(VkPrimitiveTopology topology, bool primitiveRestartEnable) {
267  _topology = topology;
268  _primitiveRestartEnable = primitiveRestartEnable;
269 }
270 
271 inline void GraphicsPipeline::setDynamicStates(const std::set<VkDynamicState>& dynamicStates) {
272  _dynamicStates = dynamicStates;
273 }
274 
276  _pipelineLayout = std::move(pipelineLayout);
277 }
278 
279 inline void GraphicsPipeline::setRenderPass(API::RenderPass renderPass, uint32_t subpass) {
280  _renderPass = std::move(renderPass);
281  _subpass = subpass;
282 }
283 
284 inline void GraphicsPipeline::setPipelineCache(VkPipelineCache pipelineCache) {
285  _pipelineCache = pipelineCache;
286 }
287 
289  return GraphicsPipeline::ViewportState(*this);
290 }
291 
294 }
295 
298 }
299 
302 }
303 
305  return GraphicsPipeline::ColorBlendState(*this);
306 }
bool loadFromFile(const std::string &filename)
void enableDepthBias(float constantFactor, float clamp, float slopeFactor)
VkPipelineRasterizationStateCreateInfo _rasterizationState
void setPipelineLayout(API::PipelineLayout pipelineLayout)
void enableDepthBoundsTest(float minDepthBounds, float maxDepthBounds)
void setRenderPass(API::RenderPass renderPass, uint32_t subpass)
std::vector< VkPipelineShaderStageCreateInfo > _shaderStages
std::vector< VkVertexInputAttributeDescription > _inputAttributes
void addAttachment(const VkPipelineColorBlendAttachmentState &attachment)
VkPipelineMultisampleStateCreateInfo _multisampleState
void setInputAssemblyInfo(VkPrimitiveTopology topology, bool primitiveRestartEnable)
bool setShaderFromFile(VkShaderStageFlagBits stage, const char *entry, const std::string &filename)
VkPipelineColorBlendStateCreateInfo _colorBlendState
void setRasterizationSamples(VkSampleCountFlagBits rasterizationSamples)
void enableStencilTest(const VkStencilOpState &front, const VkStencilOpState &back)
#define LUG_LOG
Definition: Logger.hpp:73
void setPipelineCache(VkPipelineCache pipelineCache)
std::vector< VkPipelineColorBlendAttachmentState > _colorBlendAttachments
InputBinding addInputBinding(uint32_t stride, VkVertexInputRate inputRate=VK_VERTEX_INPUT_RATE_VERTEX)
bool build(API::ShaderModule &instance, VkResult *returnResult=nullptr)
std::vector< VkVertexInputBindingDescription > _inputBindings
VkPipelineDepthStencilStateCreateInfo _depthStencilState
void setDynamicStates(const std::set< VkDynamicState > &dynamicStates)
void setShader(VkShaderStageFlagBits stage, const char *entry, API::ShaderModule shaderModule)
bool setShaderFromData(VkShaderStageFlagBits stage, const char *entry, const std::vector< uint32_t > &data)
void loadFromData(const std::vector< uint32_t > &data)