-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvulkan-pipeline.cc
234 lines (191 loc) · 8.97 KB
/
vulkan-pipeline.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <unordered_map>
#include <algorithm>
#include <set>
#include <vulkan/vulkan.hpp>
#include "vulkan-core.h"
using namespace space::core;
class GraphicsPipelineBuilder::Impl {
public:
Impl(
const vk::UniqueDevice *device,
const vk::UniquePipelineLayout *pipeline_layout,
const vk::UniqueRenderPass *render_pass,
vk::SampleCountFlagBits nsamples);
void SetPrimitiveTopology(vk::PrimitiveTopology topology);
void SetPolygonMode(vk::PolygonMode mode);
void SetFrontFace(vk::FrontFace front_face);
void DepthBuffered(const bool value) { depth_buffered_ = value; }
void AddShader(const vk::ShaderModule &shader,
const vk::ShaderStageFlagBits &stage,
const vk::SpecializationInfo *specialization_info);
void AddVertexInputBindingDescription(
const vk::VertexInputBindingDescription &input_binding);
bool AddVertexInputAttributeDescription(
const vk::VertexInputAttributeDescription &input_attribute);
void EnableDynamicState(const vk::DynamicState &state);
vk::UniquePipeline Create(vk::UniquePipelineCache *pipeline_cache);
private:
const vk::UniqueDevice *device_;
const vk::UniquePipelineLayout *pipeline_layout_;
const vk::UniqueRenderPass *render_pass_;
vk::SampleCountFlagBits nsamples_;
// Depth buffered?
bool depth_buffered_;
vk::PipelineInputAssemblyStateCreateInfo input_assembly_state_;
vk::PipelineRasterizationStateCreateInfo rasterization_state_;
// A unique shader per type.
std::unordered_map<enum vk::ShaderStageFlagBits,
vk::PipelineShaderStageCreateInfo> stages_;
std::unordered_map<uint32_t, vk::VertexInputBindingDescription> input_bindings_;
std::vector<vk::VertexInputAttributeDescription> input_attributes_;
// Enabled dynamic states
std::set<vk::DynamicState> dynamic_states_;
};
GraphicsPipelineBuilder::Impl::Impl(
const vk::UniqueDevice *device,
const vk::UniquePipelineLayout *pipeline_layout,
const vk::UniqueRenderPass *render_pass,
vk::SampleCountFlagBits nsamples)
: device_(device), pipeline_layout_(pipeline_layout),
render_pass_(render_pass), nsamples_(nsamples),
depth_buffered_(false),
input_assembly_state_(
vk::PipelineInputAssemblyStateCreateFlags(),
vk::PrimitiveTopology::eTriangleList),
rasterization_state_(
vk::PipelineRasterizationStateCreateFlags(), false, false,
vk::PolygonMode::eLine, vk::CullModeFlagBits::eNone,
vk::FrontFace::eClockwise, false, 0.0f, 0.0f, 0.0f, 1.0f) {}
void GraphicsPipelineBuilder::Impl::AddShader(
const vk::ShaderModule &shader,
const vk::ShaderStageFlagBits &stage,
const vk::SpecializationInfo *specialization_info) {
stages_.insert({stage, vk::PipelineShaderStageCreateInfo(
vk::PipelineShaderStageCreateFlags(), stage,
shader, "main", specialization_info)});
}
void GraphicsPipelineBuilder::Impl::SetPrimitiveTopology(vk::PrimitiveTopology topology) {
input_assembly_state_.topology = topology;
}
void GraphicsPipelineBuilder::Impl::SetPolygonMode(vk::PolygonMode mode) {
rasterization_state_.polygonMode = mode;
}
void GraphicsPipelineBuilder::Impl::SetFrontFace(vk::FrontFace front_face) {
rasterization_state_.frontFace = front_face;
}
void GraphicsPipelineBuilder::Impl::AddVertexInputBindingDescription(
const vk::VertexInputBindingDescription &input_binding) {
input_bindings_[input_binding.binding] = input_binding;
}
bool GraphicsPipelineBuilder::Impl::AddVertexInputAttributeDescription(
const vk::VertexInputAttributeDescription &input_attribute) {
// Check that the binding is already added.
if (input_bindings_.find(input_attribute.binding) == input_bindings_.end())
return false;
input_attributes_.push_back(input_attribute);
return true;
}
void GraphicsPipelineBuilder::Impl::EnableDynamicState(const vk::DynamicState &state) {
dynamic_states_.insert(state);
}
vk::UniquePipeline GraphicsPipelineBuilder::Impl::Create(vk::UniquePipelineCache *pipeline_cache) {
// Shaders
std::vector<vk::PipelineShaderStageCreateInfo> stages;
for (const auto &value : stages_) {
stages.push_back(value.second);
}
// Vertex input state
std::vector<vk::VertexInputBindingDescription> input_bindings;
for (const auto &value : input_bindings_) {
input_bindings.push_back(value.second);
}
vk::PipelineVertexInputStateCreateInfo vertex_input_state;
vertex_input_state.vertexBindingDescriptionCount = input_bindings_.size();
vertex_input_state.pVertexBindingDescriptions = input_bindings.data();
vertex_input_state.vertexAttributeDescriptionCount = input_attributes_.size();
vertex_input_state.pVertexAttributeDescriptions = input_attributes_.data();
// Viewport state
vk::PipelineViewportStateCreateInfo viewport_state(
vk::PipelineViewportStateCreateFlags(), 1, nullptr, 1, nullptr);
vk::PipelineMultisampleStateCreateInfo multisample_state({}, nsamples_);
vk::StencilOpState stencil_op_state(
vk::StencilOp::eKeep, vk::StencilOp::eKeep, vk::StencilOp::eKeep,
vk::CompareOp::eAlways);
vk::PipelineDepthStencilStateCreateInfo depth_stencil_state(
vk::PipelineDepthStencilStateCreateFlags(), depth_buffered_, depth_buffered_,
vk::CompareOp::eLessOrEqual, false, false, stencil_op_state, stencil_op_state);
vk::ColorComponentFlags color_component_flags(
vk::ColorComponentFlagBits::eR
| vk::ColorComponentFlagBits::eG
| vk::ColorComponentFlagBits::eB
| vk::ColorComponentFlagBits::eA);
vk::PipelineColorBlendAttachmentState pipeline_color_blend_attachment_state(
false, vk::BlendFactor::eZero, vk::BlendFactor::eZero, vk::BlendOp::eAdd,
vk::BlendFactor::eZero, vk::BlendFactor::eZero, vk::BlendOp::eAdd, color_component_flags);
vk::PipelineColorBlendStateCreateInfo color_blend_state(
vk::PipelineColorBlendStateCreateFlags(), false, vk::LogicOp::eNoOp,
1, &pipeline_color_blend_attachment_state, { { 1.0f, 1.0f, 1.0f, 1.0f } });
// Dynamic states
vk::PipelineDynamicStateCreateInfo dynamic_state;
std::vector<vk::DynamicState> enabled_states;
std::copy(dynamic_states_.begin(), dynamic_states_.end(),
std::back_inserter(enabled_states));
dynamic_state.pDynamicStates = enabled_states.data();
dynamic_state.dynamicStateCount = enabled_states.size();
// Build the stages array. Convert the unordered map in a vector.
vk::GraphicsPipelineCreateInfo graphics_pipeline_create_info(
vk::PipelineCreateFlags(), stages.size(), stages.data(),
&vertex_input_state, &input_assembly_state_,
nullptr, &viewport_state, &rasterization_state_,
&multisample_state, &depth_stencil_state,
&color_blend_state, &dynamic_state,
pipeline_layout_->get(), render_pass_->get(), 0, nullptr, 0);
auto &device = *device_;
return device->createGraphicsPipelineUnique(
pipeline_cache->get(), graphics_pipeline_create_info).value;
}
// PIMPL forwards.
GraphicsPipelineBuilder& GraphicsPipelineBuilder::SetPrimitiveTopology(
vk::PrimitiveTopology topology) { impl_->SetPrimitiveTopology(topology); return *this; }
GraphicsPipelineBuilder& GraphicsPipelineBuilder::SetPolygoneMode(
vk::PolygonMode mode) { impl_->SetPolygonMode(mode); return *this; }
GraphicsPipelineBuilder& GraphicsPipelineBuilder::SetFrontFace(
vk::FrontFace front_face) { impl_->SetFrontFace(front_face); return *this; }
GraphicsPipelineBuilder& GraphicsPipelineBuilder::DepthBuffered(const bool value){
impl_->DepthBuffered(value); return *this; }
GraphicsPipelineBuilder& GraphicsPipelineBuilder::AddFragmentShader(
const vk::ShaderModule &shader, const vk::SpecializationInfo *specialization_info) {
impl_->AddShader(shader, vk::ShaderStageFlagBits::eFragment, specialization_info);
return *this;
}
GraphicsPipelineBuilder& GraphicsPipelineBuilder::AddVertexShader(
const vk::ShaderModule &shader, const vk::SpecializationInfo *specialization_info) {
impl_->AddShader(shader, vk::ShaderStageFlagBits::eVertex, specialization_info);
return *this;
}
GraphicsPipelineBuilder& GraphicsPipelineBuilder::AddVertexInputBindingDescription(
uint32_t binding, uint32_t stride, vk::VertexInputRate input_rate) {
impl_->AddVertexInputBindingDescription({ binding, stride, input_rate });
return *this;
}
GraphicsPipelineBuilder& GraphicsPipelineBuilder::AddVertexInputAttributeDescription(
uint32_t location, uint32_t binding, vk::Format format, uint32_t offset) {
impl_->AddVertexInputAttributeDescription({location, binding, format, offset});
return *this;
}
GraphicsPipelineBuilder& GraphicsPipelineBuilder::EnableDynamicState(
const vk::DynamicState &state) {
impl_->EnableDynamicState(state);
return *this;
}
vk::UniquePipeline GraphicsPipelineBuilder::Create(
vk::UniquePipelineCache *pipeline_cache) {
return impl_->Create(pipeline_cache);
}
GraphicsPipelineBuilder::GraphicsPipelineBuilder(
const vk::UniqueDevice *device,
const vk::UniquePipelineLayout *pipeline_layout,
const vk::UniqueRenderPass *render_pass,
vk::SampleCountFlagBits nsamples)
: impl_(new Impl(device, pipeline_layout, render_pass, nsamples)) {}
GraphicsPipelineBuilder::~GraphicsPipelineBuilder() = default;