Lugdunum  0.1.0
GraphicsPipeline.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <set>
5 #include <string>
6 #include <vector>
7 
13 
14 namespace lug {
15 namespace Graphics {
16 namespace Vulkan {
17 namespace API {
18 
19 class Device;
20 
21 namespace Builder {
22 
24 public:
26  friend class GraphicsPipeline;
27 
28  public:
29  InputBinding(const InputBinding&) = default;
30  InputBinding(InputBinding&&) = default;
31 
32  InputBinding& operator=(const InputBinding&) = default;
33  InputBinding& operator=(InputBinding&&) = default;
34 
35  ~InputBinding() = default;
36 
37  // Setters
38  void addAttributes(VkFormat format, uint32_t offset = 0);
39  void addAttributes(uint32_t location, VkFormat format, uint32_t offset = 0);
40 
41  private:
42  explicit InputBinding(GraphicsPipeline& graphicPipelineBuilder, uint32_t idx);
43 
44  private:
46  uint32_t _idx;
47  };
48 
50  friend class GraphicsPipeline;
51 
52  public:
53  ViewportState(const ViewportState&) = default;
54  ViewportState(ViewportState&&) = default;
55 
56  ViewportState& operator=(const ViewportState&) = default;
57  ViewportState& operator=(ViewportState&&) = default;
58 
59  ~ViewportState() = default;
60 
61  // Setters
62  void addViewport(const VkViewport& viewport);
63  void addScissor(const VkRect2D& scissor);
64 
65  private:
66  ViewportState(GraphicsPipeline& graphicPipelineBuilder);
67 
68  private:
70  };
71 
73  friend class GraphicsPipeline;
74 
75  public:
76  RasterizationState(const RasterizationState&) = default;
78 
79  RasterizationState& operator=(const RasterizationState&) = default;
80  RasterizationState& operator=(RasterizationState&&) = default;
81 
82  ~RasterizationState() = default;
83 
84  // Setters
85  void enableDepthClamp();
86  void disableDepthClamp();
87 
88  void enableRasterizerDiscard();
89  void disableRasterizerDiscard();
90 
91  void setPolygonMode(VkPolygonMode polygonMode);
92  void setCullMode(VkCullModeFlagBits cullMode);
93  void setFrontFace(VkFrontFace frontFace);
94 
95  void enableDepthBias(float constantFactor, float clamp, float slopeFactor);
96  void disableDepthBias();
97 
98  void setLineWidth(float lineWidth);
99 
100  private:
101  explicit RasterizationState(GraphicsPipeline& graphicPipelineBuilder);
102 
103  private:
105  };
106 
108  friend class GraphicsPipeline;
109 
110  public:
111  MultisampleState(const MultisampleState&) = default;
112  MultisampleState(MultisampleState&&) = default;
113 
114  MultisampleState& operator=(const MultisampleState&) = default;
115  MultisampleState& operator=(MultisampleState&&) = default;
116 
117  ~MultisampleState() = default;
118 
119  // Setters
120  void setRasterizationSamples(VkSampleCountFlagBits rasterizationSamples);
121 
122  void enableSampleShading();
123  void disableSampleShading();
124 
125  void setMinSampleShading(float minSampleShading);
126 
127  // TODO: Setter for pSampleMask
128 
129  void enableAlphaToCoverage();
130  void disableAlphaToCoverage();
131 
132  void enableAlphaToOne();
133  void disableAlphaToOne();
134 
135  private:
136  explicit MultisampleState(GraphicsPipeline& graphicPipelineBuilder);
137 
138  private:
140  };
141 
143  friend class GraphicsPipeline;
144 
145  public:
146  DepthStencilState(const DepthStencilState&) = default;
148 
149  DepthStencilState& operator=(const DepthStencilState&) = default;
150  DepthStencilState& operator=(DepthStencilState&&) = default;
151 
152  ~DepthStencilState() = default;
153 
154  // Setters
155  void enableDepthTest(VkCompareOp depthCompareOp);
156  void disableDepthTest();
157 
158  void enableDepthWrite(); // The depth test need to be enable also
159  void disableDepthWrite();
160 
161  void enableDepthBoundsTest(float minDepthBounds, float maxDepthBounds);
162  void disableDepthBoundsTest();
163 
164  void enableStencilTest(const VkStencilOpState& front, const VkStencilOpState& back);
165  void disableStencilTest();
166 
167  private:
168  DepthStencilState(GraphicsPipeline& graphicPipelineBuilder);
169 
170  private:
172  };
173 
175  friend class GraphicsPipeline;
176 
177  public:
178  ColorBlendState(const ColorBlendState&) = default;
179  ColorBlendState(ColorBlendState&&) = default;
180 
181  ColorBlendState& operator=(const ColorBlendState&) = default;
182  ColorBlendState& operator=(ColorBlendState&&) = default;
183 
184  ~ColorBlendState() = default;
185 
186  // Setters
187  void enableLogicOp(VkLogicOp logicOp);
188  void disableLogicOp();
189 
190  void addAttachment(const VkPipelineColorBlendAttachmentState& attachment);
191 
192  void setBlendConstants(const float blendConstants[4]);
193 
194  private:
195  ColorBlendState(GraphicsPipeline& graphicPipelineBuilder);
196 
197  private:
199  };
200 
201 public:
202  GraphicsPipeline(const API::Device& device);
203 
204  GraphicsPipeline(const GraphicsPipeline&) = delete;
206 
207  GraphicsPipeline& operator=(const GraphicsPipeline&) = delete;
208  GraphicsPipeline& operator=(GraphicsPipeline&&) = delete;
209 
210  ~GraphicsPipeline() = default;
211 
212  // Setters
213  void setShader(VkShaderStageFlagBits stage, const char* entry, API::ShaderModule shaderModule);
214  bool setShaderFromFile(VkShaderStageFlagBits stage, const char* entry, const std::string& filename);
215  bool setShaderFromData(VkShaderStageFlagBits stage, const char* entry, const std::vector<uint32_t>& data);
216 
217  InputBinding addInputBinding(uint32_t stride, VkVertexInputRate inputRate = VK_VERTEX_INPUT_RATE_VERTEX);
218  InputBinding addInputBinding(uint32_t binding, uint32_t stride, VkVertexInputRate inputRate = VK_VERTEX_INPUT_RATE_VERTEX);
219 
220  void setInputAssemblyInfo(VkPrimitiveTopology topology, bool primitiveRestartEnable);
221  void setDynamicStates(const std::set<VkDynamicState>& dynamicStates);
222 
223  void setPipelineLayout(API::PipelineLayout pipelineLayout);
224  void setRenderPass(API::RenderPass renderPass, uint32_t subpass);
225  void setPipelineCache(VkPipelineCache pipelineCache);
226 
227  // Getters
228  ViewportState getViewportState();
229  RasterizationState getRasterizationState();
230  MultisampleState getMultisampleState();
231  DepthStencilState getDepthStencilState();
232  ColorBlendState getColorBlendState();
233 
234  // Build methods
235  bool build(API::GraphicsPipeline& graphicPipeline, VkResult* returnResult = nullptr);
236  std::unique_ptr<API::GraphicsPipeline> build(VkResult* returnResult = nullptr);
237 
238 private:
240 
241  // Shaders state
242  std::vector<API::ShaderModule> _shaderModules;
243  std::vector<VkPipelineShaderStageCreateInfo> _shaderStages;
244 
245  // Vertex input state
246  std::vector<VkVertexInputBindingDescription> _inputBindings;
247  std::vector<VkVertexInputAttributeDescription> _inputAttributes;
248 
249  // Input assembly state
250  VkPrimitiveTopology _topology{VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST};
251  bool _primitiveRestartEnable{VK_FALSE};
252 
253  // Viewport state
254  VkPipelineViewportStateCreateInfo _viewportState{
255  /* _viewportState.sType */ VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
256  /* _viewportState.pNext */ nullptr,
257  /* _viewportState.flags */ 0,
258  /* _viewportState.viewportCount */ 0, // Will be set to _viewports.size()
259  /* _viewportState.pViewports */ nullptr, // Will be set to _viewports.data()
260  /* _viewportState.scissorCount */ 0, // Will be set to _scissors.size()
261  /* _viewportState.pScissors */ nullptr // Will be set to _scissors.data()
262  };
263 
264  std::vector<VkViewport> _viewports;
265  std::vector<VkRect2D> _scissors;
266 
267  // Rasterization state
268  VkPipelineRasterizationStateCreateInfo _rasterizationState{
269  /* _rasterizationState.sType */ VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
270  /* _rasterizationState.pNext */ nullptr,
271  /* _rasterizationState.flags */ 0,
272  /* _rasterizationState.depthClampEnable */ VK_FALSE,
273  /* _rasterizationState._rasterizationStateDiscardEnable */ VK_FALSE,
274  /* _rasterizationState.polygonMode */ VK_POLYGON_MODE_FILL,
275  /* _rasterizationState.cullMode */ VK_CULL_MODE_BACK_BIT,
276  /* _rasterizationState.frontFace */ VK_FRONT_FACE_COUNTER_CLOCKWISE,
277  /* _rasterizationState.depthBiasEnable */ VK_FALSE,
278  /* _rasterizationState.depthBiasConstantFactor */ 0.0f,
279  /* _rasterizationState.depthBiasClamp */ 0.0f,
280  /* _rasterizationState.depthBiasSlopeFactor */ 0.0f,
281  /* _rasterizationState.lineWidth */ 1.0f
282  };
283 
284  // Multisample state
285  VkPipelineMultisampleStateCreateInfo _multisampleState{
286  /* _multisampleState.sType */ VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
287  /* _multisampleState.pNext */ nullptr,
288  /* _multisampleState.flags */ 0,
289  /* _multisampleState.rasterizationSamples */ VK_SAMPLE_COUNT_1_BIT,
290  /* _multisampleState.sampleShadingEnable */ VK_FALSE,
291  /* _multisampleState.minSampleShading */ 0.0f,
292  /* _multisampleState.pSampleMask */ nullptr,
293  /* _multisampleState.alphaToCoverageEnable */ VK_FALSE,
294  /* _multisampleState.alphaToOneEnable */ VK_FALSE
295  };
296 
297  // Depth and Stencil state
298  VkPipelineDepthStencilStateCreateInfo _depthStencilState{
299  /* _depthStencilState.sType */ VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
300  /* _depthStencilState.pNext */ nullptr,
301  /* _depthStencilState.flags */ 0,
302  /* _depthStencilState.depthTestEnable */ VK_FALSE,
303  /* _depthStencilState.depthWriteEnable */ VK_FALSE,
304  /* _depthStencilState.depthCompareOp */ VK_COMPARE_OP_NEVER,
305  /* _depthStencilState.depthBoundsTestEnable */ VK_FALSE,
306  /* _depthStencilState.stencilTestEnable */ VK_FALSE,
307  /* _depthStencilState.front */ {},
308  /* _depthStencilState.back */ {},
309  /* _depthStencilState.minDepthBounds */ 0.0f,
310  /* _depthStencilState.maxDepthBounds */ 0.0f
311  };
312 
313  // Color Blend state
314  VkPipelineColorBlendStateCreateInfo _colorBlendState{
315  /* _colorBlendState.sType */ VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
316  /* _colorBlendState.pNext */ nullptr,
317  /* _colorBlendState.flags */ 0,
318  /* _colorBlendState.logicOpEnable */ VK_FALSE,
319  /* _colorBlendState.logicOp */ VK_LOGIC_OP_CLEAR,
320  /* _colorBlendState.attachmentCount */ 0, // Will be set to _colorBlendAttachments.size()
321  /* _colorBlendState.pAttachments */ nullptr, // Will be set to _colorBlendAttachments.data()
322  /* _colorBlendState.blendConstants */ {0.0f, 0.0f, 0.0f, 0.0f}
323  };
324 
325  std::vector<VkPipelineColorBlendAttachmentState> _colorBlendAttachments;
326 
327  // Dynamic state
328  std::set<VkDynamicState> _dynamicStates;
329 
330  // General
333  uint32_t _subpass{0};
334  VkPipelineCache _pipelineCache{VK_NULL_HANDLE};
335 };
336 
338 
339 } // Builder
340 } // API
341 } // Vulkan
342 } // Graphics
343 } // lug
#define LUG_GRAPHICS_API
Definition: Export.hpp:11
void setBlendConstants(const float blendConstants[4]) const
std::vector< VkPipelineShaderStageCreateInfo > _shaderStages
std::vector< VkVertexInputAttributeDescription > _inputAttributes
Resource::SharedPtr< lug::Graphics::Render::Material > build(const ::lug::Graphics::Builder::Material &builder)
Definition: Material.cpp:14
std::vector< VkPipelineColorBlendAttachmentState > _colorBlendAttachments
std::vector< VkVertexInputBindingDescription > _inputBindings