Skip to content

Commit d23d14d

Browse files
committed
[pipeline-builder|render-graph] Use graphics pipeline builder
1 parent f154ea8 commit d23d14d

File tree

5 files changed

+575
-77
lines changed

5 files changed

+575
-77
lines changed

include/inexor/vulkan-renderer/render_graph.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "inexor/vulkan-renderer/wrapper/framebuffer.hpp"
55
#include "inexor/vulkan-renderer/wrapper/image.hpp"
66
#include "inexor/vulkan-renderer/wrapper/pipeline.hpp"
7+
#include "inexor/vulkan-renderer/wrapper/pipeline_builder.hpp"
78
#include "inexor/vulkan-renderer/wrapper/pipeline_layout.hpp"
89
#include "inexor/vulkan-renderer/wrapper/swapchain.hpp"
910

@@ -390,6 +391,8 @@ class RenderGraph {
390391
// Stage execution order.
391392
std::vector<RenderStage *> m_stage_stack;
392393

394+
mutable wrapper::GraphicsPipelineBuilder m_graphics_pipeline_builder;
395+
393396
void update_dynamic_buffers(const wrapper::CommandBuffer &cmd_buf);
394397

395398
// Functions for building stage related vulkan objects.
@@ -403,7 +406,7 @@ class RenderGraph {
403406

404407
public:
405408
RenderGraph(wrapper::Device &device, const wrapper::Swapchain &swapchain)
406-
: m_device(device), m_swapchain(swapchain) {}
409+
: m_device(device), m_swapchain(swapchain), m_graphics_pipeline_builder(device) {}
407410

408411
/// @brief Adds either a render resource or render stage to the render graph.
409412
/// @return A mutable reference to the just-added resource or stage
Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
#pragma once
2+
3+
#include <vulkan/vulkan_core.h>
4+
5+
#include "inexor/vulkan-renderer/wrapper/make_info.hpp"
6+
#include "inexor/vulkan-renderer/wrapper/pipeline.hpp"
7+
8+
#include <memory>
9+
#include <string>
10+
#include <vector>
11+
12+
namespace inexor::vulkan_renderer::wrapper {
13+
14+
// Forward declarations
15+
class Device;
16+
17+
/// Builder class for VkPipelineCreateInfo
18+
/// @note that all members are initialized in the method ``reset()`` (This method is also called by the constructor)
19+
/// Calling ``reset()`` allows you to re-use this builder for the next graphics pipeline you want to build, so you don't
20+
/// need to create one builder per graphics pipeline you build
21+
class GraphicsPipelineBuilder {
22+
private:
23+
const Device &m_device;
24+
25+
std::vector<VkVertexInputBindingDescription> m_vertex_input_binding_descriptions;
26+
std::vector<VkVertexInputAttributeDescription> m_vertex_input_attribute_descriptions;
27+
// With the builder we can fill vertex binding descriptions and vertex attribute descriptions in here
28+
VkPipelineVertexInputStateCreateInfo m_vertex_input_sci;
29+
30+
// With the builder we can set topology in here
31+
VkPipelineInputAssemblyStateCreateInfo m_input_assembly_sci;
32+
33+
// With the builder we can set the patch control point count in here
34+
VkPipelineTessellationStateCreateInfo m_tesselation_sci;
35+
36+
std::vector<VkViewport> m_viewports;
37+
std::vector<VkRect2D> m_scissors;
38+
// With the builder we can set viewport(s) and scissor(s) in here
39+
VkPipelineViewportStateCreateInfo m_viewport_sci;
40+
41+
// With the builder we can set polygon mode, cull mode, front face, and line width
42+
// TODO: Implement methods to enable depth bias and for setting the depth bias parameters
43+
VkPipelineRasterizationStateCreateInfo m_rasterization_sci;
44+
45+
// With the builder we can set rasterization samples and min sample shading
46+
// TODO: Expose more multisampling parameters if desired
47+
VkPipelineMultisampleStateCreateInfo m_multisample_sci;
48+
49+
// With the builder we can't set individial fields of this struct,
50+
// since it's easier to specify an entire VkPipelineDepthStencilStateCreateInfo struct to the builder instead
51+
VkPipelineDepthStencilStateCreateInfo m_depth_stencil_sci;
52+
53+
// With the builder we can't set individial fields of this struct,
54+
// since it's easier to specify an entire VkPipelineColorBlendStateCreateInfo struct to the builder instead
55+
VkPipelineColorBlendStateCreateInfo m_color_blend_sci;
56+
57+
std::vector<VkDynamicState> m_dynamic_states;
58+
// This will be filled in the build() method
59+
// With the builder we can specify a std::vector<VkDynamicState>
60+
VkPipelineDynamicStateCreateInfo m_dynamic_states_sci;
61+
62+
VkPipelineLayout m_pipeline_layout{VK_NULL_HANDLE};
63+
VkRenderPass m_render_pass{VK_NULL_HANDLE};
64+
65+
// With the builder we can either call add_shader or set_shaders
66+
std::vector<VkPipelineShaderStageCreateInfo> m_shader_stages;
67+
// With the builder we can either call add_color_blend_attachment or set_color_blend_attachments
68+
std::vector<VkPipelineColorBlendAttachmentState> m_color_blend_attachment_states;
69+
70+
public:
71+
/// Default constructor
72+
/// @param device The device wrapper
73+
GraphicsPipelineBuilder(const Device &device);
74+
GraphicsPipelineBuilder(const GraphicsPipelineBuilder &) = delete;
75+
GraphicsPipelineBuilder(GraphicsPipelineBuilder &&other) noexcept;
76+
~GraphicsPipelineBuilder() = default;
77+
78+
GraphicsPipelineBuilder &operator=(const GraphicsPipelineBuilder &) = delete;
79+
GraphicsPipelineBuilder &operator=(GraphicsPipelineBuilder &&) = delete;
80+
81+
/// Add a shader stage
82+
/// @param shader The shader stage to add
83+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
84+
[[nodiscard]] GraphicsPipelineBuilder &add_shader(const VkPipelineShaderStageCreateInfo &shader);
85+
86+
/// Add a vertex input attribute description
87+
/// @param description The vertex input attribute description
88+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
89+
[[nodiscard]] GraphicsPipelineBuilder &
90+
add_vertex_input_attribute(const VkVertexInputAttributeDescription &description);
91+
92+
/// Add a vertex input binding description
93+
/// @param description The vertex input binding descriptions
94+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
95+
[[nodiscard]] GraphicsPipelineBuilder &add_vertex_input_binding(const VkVertexInputBindingDescription &description);
96+
97+
/// Add a color blend attachment
98+
/// @param attachment The color blend attachment
99+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
100+
[[nodiscard]] GraphicsPipelineBuilder &
101+
add_color_blend_attachment(const VkPipelineColorBlendAttachmentState &attachment);
102+
103+
/// Build the graphics pipeline with specified pipeline create flags
104+
/// @param name The debug name of the graphics pipeline
105+
/// @return The unique pointer instance of ``GraphicsPipeline`` that was created
106+
[[nodiscard]] std::unique_ptr<GraphicsPipeline> build(std::string name);
107+
108+
/// Reset all data in this class so the builder can be re-used
109+
/// @note This is called by the constructor
110+
void reset();
111+
112+
/// Set the color blend manually
113+
/// @param color_blend The color blend
114+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
115+
[[nodiscard]] GraphicsPipelineBuilder &set_color_blend(const VkPipelineColorBlendStateCreateInfo &color_blend);
116+
117+
/// Set all color blend attachments manually
118+
/// @note You should prefer to use ``add_color_blend_attachment`` instead
119+
/// @param attachments The color blend attachments
120+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
121+
[[nodiscard]] GraphicsPipelineBuilder &
122+
set_color_blend_attachments(const std::vector<VkPipelineColorBlendAttachmentState> &attachments);
123+
124+
/// Enable or disable culling
125+
/// @warning Disabling culling will have a significant performance impact
126+
/// @param culling_enabled ``true`` if culling is enabled
127+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
128+
[[nodiscard]] GraphicsPipelineBuilder &set_culling_mode(VkBool32 culling_enabled);
129+
130+
/// Set the depth stencil
131+
/// @param depth_stencil The depth stencil
132+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
133+
[[nodiscard]] GraphicsPipelineBuilder &
134+
set_depth_stencil(const VkPipelineDepthStencilStateCreateInfo &depth_stencil);
135+
136+
/// Set the dynamic states
137+
/// @param dynamic_states The dynamic states
138+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
139+
[[nodiscard]] GraphicsPipelineBuilder &set_dynamic_states(const std::vector<VkDynamicState> &dynamic_states);
140+
141+
/// Set the input assembly state create info
142+
/// @note If you just want to set the triangle topology, call ``set_triangle_topology`` instead, because this is the
143+
/// most powerful method of this method in case you really need to overwrite it
144+
/// @param input_assembly The pipeline input state create info
145+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
146+
[[nodiscard]] GraphicsPipelineBuilder &
147+
set_input_assembly(const VkPipelineInputAssemblyStateCreateInfo &input_assembly);
148+
149+
/// Set the line width of rasterization
150+
/// @param line_width The line width of rasterization
151+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
152+
[[nodiscard]] GraphicsPipelineBuilder &set_line_width(float width);
153+
154+
/// Set the most important MSAA settings
155+
/// @param sample_count The number of samples used in rasterization
156+
/// @param min_sample_shading A minimum fraction of sample shading
157+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
158+
[[nodiscard]] GraphicsPipelineBuilder &set_multisampling(VkSampleCountFlagBits sample_count,
159+
float min_sample_shading);
160+
161+
/// Store the pipeline layout
162+
/// @param layout The pipeline layout
163+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
164+
[[nodiscard]] GraphicsPipelineBuilder &set_pipeline_layout(VkPipelineLayout layout);
165+
166+
/// Set the triangle topology
167+
/// @param topology the primitive topology
168+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
169+
[[nodiscard]] GraphicsPipelineBuilder &set_primitive_topology(VkPrimitiveTopology topology);
170+
171+
/// Set the rasterization state of the graphics pipeline manually
172+
/// @param rasterization The rasterization state
173+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
174+
[[nodiscard]] GraphicsPipelineBuilder &
175+
set_rasterization(const VkPipelineRasterizationStateCreateInfo &rasterization);
176+
177+
/// Set the render pass
178+
/// @param render_pass The render pass
179+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
180+
[[nodiscard]] GraphicsPipelineBuilder &set_render_pass(VkRenderPass render_pass);
181+
182+
/// Set the scissor data in VkPipelineViewportStateCreateInfo
183+
/// There is another method called set_scissors in case multiple scissors will be used
184+
/// @param scissors The scissors in in VkPipelineViewportStateCreateInfo
185+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
186+
[[nodiscard]] GraphicsPipelineBuilder &set_scissor(const VkRect2D &scissor);
187+
188+
/// Set the scissor data in VkPipelineViewportStateCreateInfo (convert VkExtent2D to VkRect2D)
189+
/// @param extent The extent of the scissor
190+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
191+
[[nodiscard]] GraphicsPipelineBuilder &set_scissor(const VkExtent2D &extent);
192+
193+
/// Set the viewport data in VkPipelineViewportStateCreateInfo
194+
/// There is another method called set_scissors in case multiple scissors will be used
195+
/// @param scissor The scissor in in VkPipelineViewportStateCreateInfo
196+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
197+
[[nodiscard]] GraphicsPipelineBuilder &set_scissors(const std::vector<VkRect2D> &scissors);
198+
199+
/// Set the shader stage
200+
/// @param shader_stages The shader stages
201+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
202+
[[nodiscard]] GraphicsPipelineBuilder &set_shaders(const std::vector<VkPipelineShaderStageCreateInfo> &shaders);
203+
204+
/// Set the tesselation control point count
205+
/// @param control_point_count The tesselation control point count
206+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
207+
[[nodiscard]] GraphicsPipelineBuilder &set_tesselation_control_point_count(std::uint32_t control_point_count);
208+
209+
/// Set the vertex input attribute descriptions manually
210+
/// You should prefer to use ``add_vertex_input_attribute`` instead
211+
/// @param descriptions The vertex input attribute descriptions
212+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
213+
[[nodiscard]] GraphicsPipelineBuilder &
214+
set_vertex_input_attributes(const std::vector<VkVertexInputAttributeDescription> &descriptions);
215+
216+
/// Set the vertex input binding descriptions manually
217+
/// You should prefer to use ``add_vertex_input_binding`` instead
218+
/// @param descriptions The vertex input binding descriptions
219+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
220+
[[nodiscard]] GraphicsPipelineBuilder &
221+
set_vertex_input_bindings(const std::vector<VkVertexInputBindingDescription> &descriptions);
222+
223+
/// Set the viewport in VkPipelineViewportStateCreateInfo
224+
/// There is another method called set_viewports in case multiple viewports will be used
225+
/// @param viewport The viewport in VkPipelineViewportStateCreateInfo
226+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
227+
[[nodiscard]] GraphicsPipelineBuilder &set_viewport(const VkViewport &viewport);
228+
229+
/// Set the viewport in VkPipelineViewportStateCreateInfo (convert VkExtent2D to VkViewport)
230+
/// @param extent The extent of the viewport
231+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
232+
[[nodiscard]] GraphicsPipelineBuilder &set_viewport(const VkExtent2D &extent);
233+
234+
/// Set the viewport in VkPipelineViewportStateCreateInfo
235+
/// @param viewports The viewports in VkPipelineViewportStateCreateInfo
236+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
237+
[[nodiscard]] GraphicsPipelineBuilder &set_viewports(const std::vector<VkViewport> &viewports);
238+
239+
/// Set the wireframe mode
240+
/// @param wireframe ``true`` if wireframe is enabled
241+
/// @return A reference to the dereferenced this pointer (allows method calls to be chained)
242+
[[nodiscard]] GraphicsPipelineBuilder &set_wireframe(VkBool32 wireframe);
243+
};
244+
245+
} // namespace inexor::vulkan_renderer::wrapper

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ set(INEXOR_SOURCE_FILES
3535
vulkan-renderer/wrapper/image.cpp
3636
vulkan-renderer/wrapper/instance.cpp
3737
vulkan-renderer/wrapper/pipeline.cpp
38+
vulkan-renderer/wrapper/pipeline_builder.cpp
3839
vulkan-renderer/wrapper/pipeline_layout.cpp
3940
vulkan-renderer/wrapper/make_info.cpp
4041
vulkan-renderer/wrapper/sampler.cpp

0 commit comments

Comments
 (0)