forked from vsg-dev/VulkanSceneGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphicsPipelineConfig.cpp
239 lines (197 loc) · 11.5 KB
/
GraphicsPipelineConfig.cpp
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
235
236
237
238
239
/* <editor-fold desc="MIT License">
Copyright(c) 2022 Robert Osfield
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</editor-fold> */
#include <vsg/io/Options.h>
#include <vsg/utils/GraphicsPipelineConfig.h>
using namespace vsg;
DescriptorConfig::DescriptorConfig(ref_ptr<ShaderSet> in_shaderSet) :
shaderSet(in_shaderSet)
{
}
bool DescriptorConfig::assignTexture(const std::string& name, ref_ptr<Data> textureData, ref_ptr<Sampler> sampler)
{
if (auto& textureBinding = shaderSet->getUniformBinding(name))
{
// set up bindings
if (!textureBinding.define.empty()) defines.push_back(textureBinding.define);
descriptorBindings.push_back(VkDescriptorSetLayoutBinding{textureBinding.binding, textureBinding.descriptorType, textureBinding.descriptorCount, textureBinding.stageFlags, nullptr});
if (!sampler) sampler = Sampler::create();
// create texture image and associated DescriptorSets and binding
auto texture = DescriptorImage::create(sampler, textureData ? textureData : textureBinding.data, textureBinding.binding, 0, textureBinding.descriptorType);
descriptors.push_back(texture);
return true;
}
return false;
}
bool DescriptorConfig::assignUniform(const std::string& name, ref_ptr<Data> data)
{
if (auto& uniformBinding = shaderSet->getUniformBinding(name))
{
// set up bindings
if (!uniformBinding.define.empty()) defines.push_back(uniformBinding.define);
descriptorBindings.push_back(VkDescriptorSetLayoutBinding{uniformBinding.binding, uniformBinding.descriptorType, uniformBinding.descriptorCount, uniformBinding.stageFlags, nullptr});
auto uniform = DescriptorBuffer::create(data ? data : uniformBinding.data, uniformBinding.binding);
descriptors.push_back(uniform);
return true;
}
return false;
}
void DescriptorConfig::init()
{
if (!descriptorBindings.empty())
{
auto descriptorSetLayout = DescriptorSetLayout::create(descriptorBindings);
descriptorSet = DescriptorSet::create(descriptorSetLayout, descriptors);
}
}
struct AssignGraphicsPipelineStates : public vsg::Visitor
{
vsg::GraphicsPipelineConfig* config = nullptr;
explicit AssignGraphicsPipelineStates(vsg::GraphicsPipelineConfig* in_config) :
config(in_config) {}
void apply(vsg::ColorBlendState& cbs) override { config->colorBlendState = ColorBlendState::create(cbs); }
void apply(vsg::DepthStencilState& mss) override { config->depthStencilState = DepthStencilState::create(mss); }
void apply(vsg::DynamicState& mss) override { config->dynamicState = DynamicState::create(mss); }
void apply(vsg::InputAssemblyState& ias) override { config->inputAssemblyState = InputAssemblyState::create(ias); }
void apply(vsg::MultisampleState& mss) override { config->multisampleState = MultisampleState::create(mss); }
void apply(vsg::RasterizationState& rs) override { config->rasterizationState = RasterizationState::create(rs); }
void apply(vsg::TessellationState& rs) override { config->tessellationState = TessellationState::create(rs); }
void apply(vsg::VertexInputState& ias) override { config->vertexInputState = VertexInputState::create(ias); }
void apply(vsg::ViewportState& ias) override { config->viewportState = ViewportState::create(ias); }
};
GraphicsPipelineConfig::GraphicsPipelineConfig(ref_ptr<ShaderSet> in_shaderSet) :
shaderSet(in_shaderSet)
{
// apply defaults
auto& graphicsPipelineStates = shaderSet->defaultGraphicsPipelineStates;
if (!graphicsPipelineStates.empty())
{
AssignGraphicsPipelineStates agps(this);
for (auto& gps : graphicsPipelineStates)
{
gps->accept(agps);
}
}
if (!vertexInputState) vertexInputState = VertexInputState::create();
if (!inputAssemblyState) inputAssemblyState = vsg::InputAssemblyState::create();
if (!rasterizationState) rasterizationState = vsg::RasterizationState::create();
if (!colorBlendState) colorBlendState = vsg::ColorBlendState::create();
if (!multisampleState) multisampleState = vsg::MultisampleState::create();
if (!depthStencilState) depthStencilState = vsg::DepthStencilState::create();
shaderHints = vsg::ShaderCompileSettings::create();
}
void GraphicsPipelineConfig::reset()
{
vertexInputState->vertexAttributeDescriptions.clear();
vertexInputState->vertexBindingDescriptions.clear();
descriptorBindings.clear();
descriptorSetLayout = {};
shaderHints->defines.clear();
}
bool GraphicsPipelineConfig::enableArray(const std::string& name, VkVertexInputRate vertexInputRate, uint32_t stride)
{
auto& attributeBinding = shaderSet->getAttributeBinding(name);
if (attributeBinding)
{
// set up bindings
uint32_t bindingIndex = baseAttributeBinding + static_cast<uint32_t>(vertexInputState->vertexAttributeDescriptions.size());
if (!attributeBinding.define.empty()) shaderHints->defines.push_back(attributeBinding.define);
vertexInputState->vertexAttributeDescriptions.push_back(VkVertexInputAttributeDescription{attributeBinding.location, bindingIndex, attributeBinding.format, 0});
vertexInputState->vertexBindingDescriptions.push_back(VkVertexInputBindingDescription{bindingIndex, stride, vertexInputRate});
return true;
}
return false;
}
bool GraphicsPipelineConfig::assignArray(DataList& arrays, const std::string& name, VkVertexInputRate vertexInputRate, ref_ptr<Data> array)
{
auto& attributeBinding = shaderSet->getAttributeBinding(name);
if (attributeBinding)
{
// set up bindings
uint32_t bindingIndex = baseAttributeBinding + static_cast<uint32_t>(arrays.size());
if (!attributeBinding.define.empty()) shaderHints->defines.push_back(attributeBinding.define);
vertexInputState->vertexAttributeDescriptions.push_back(VkVertexInputAttributeDescription{attributeBinding.location, bindingIndex, attributeBinding.format, 0});
vertexInputState->vertexBindingDescriptions.push_back(VkVertexInputBindingDescription{bindingIndex, array->getLayout().stride, vertexInputRate});
arrays.push_back(array);
return true;
}
return false;
}
bool GraphicsPipelineConfig::assignTexture(Descriptors& descriptors, const std::string& name, ref_ptr<Data> textureData, ref_ptr<Sampler> sampler)
{
if (auto& textureBinding = shaderSet->getUniformBinding(name))
{
// set up bindings
if (!textureBinding.define.empty()) shaderHints->defines.push_back(textureBinding.define);
descriptorBindings.push_back(VkDescriptorSetLayoutBinding{textureBinding.binding, textureBinding.descriptorType, textureBinding.descriptorCount, textureBinding.stageFlags, nullptr});
if (!sampler) sampler = Sampler::create();
// create texture image and associated DescriptorSets and binding
auto texture = vsg::DescriptorImage::create(sampler, textureData ? textureData : textureBinding.data, textureBinding.binding, 0, textureBinding.descriptorType);
descriptors.push_back(texture);
return true;
}
return false;
}
bool GraphicsPipelineConfig::assignUniform(Descriptors& descriptors, const std::string& name, ref_ptr<Data> data)
{
if (auto& uniformBinding = shaderSet->getUniformBinding(name))
{
// set up bindings
if (!uniformBinding.define.empty()) shaderHints->defines.push_back(uniformBinding.define);
descriptorBindings.push_back(VkDescriptorSetLayoutBinding{uniformBinding.binding, uniformBinding.descriptorType, uniformBinding.descriptorCount, uniformBinding.stageFlags, nullptr});
auto uniform = vsg::DescriptorBuffer::create(data ? data : uniformBinding.data, uniformBinding.binding);
descriptors.push_back(uniform);
return true;
}
return false;
}
int GraphicsPipelineConfig::compare(const Object& rhs_object) const
{
int result = Object::compare(rhs_object);
if (result != 0) return result;
auto& rhs = static_cast<decltype(*this)>(rhs_object);
if ((result = compare_pointer(colorBlendState, rhs.colorBlendState))) return result;
if ((result = compare_pointer(depthStencilState, rhs.depthStencilState))) return result;
if ((result = compare_pointer(dynamicState, rhs.dynamicState))) return result;
if ((result = compare_pointer(inputAssemblyState, rhs.inputAssemblyState))) return result;
if ((result = compare_pointer(multisampleState, rhs.multisampleState))) return result;
if ((result = compare_pointer(rasterizationState, rhs.rasterizationState))) return result;
if ((result = compare_pointer(tessellationState, rhs.tessellationState))) return result;
if ((result = compare_pointer(vertexInputState, rhs.vertexInputState))) return result;
if ((result = compare_pointer(viewportState, rhs.viewportState))) return result;
if ((result = compare_value(subpass, rhs.subpass))) return result;
if ((result = compare_value(baseAttributeBinding, rhs.baseAttributeBinding))) return result;
if ((result = compare_pointer(shaderSet, rhs.shaderSet))) return result;
if ((result = compare_pointer(shaderHints, rhs.shaderHints))) return result;
return compare_value_container(descriptorBindings, rhs.descriptorBindings);
}
void GraphicsPipelineConfig::init()
{
if (!descriptorSetLayout)
{
descriptorSetLayout = vsg::DescriptorSetLayout::create(descriptorBindings);
}
vsg::PushConstantRanges pushConstantRanges;
for (auto& pcb : shaderSet->pushConstantRanges)
{
if (pcb.define.empty()) pushConstantRanges.push_back(pcb.range);
}
vsg::DescriptorSetLayouts desriptorSetLayouts{descriptorSetLayout};
if (additionalDescrptorSetLayout) desriptorSetLayouts.push_back(additionalDescrptorSetLayout);
layout = vsg::PipelineLayout::create(desriptorSetLayouts, pushConstantRanges);
GraphicsPipelineStates pipelineStates;
if (colorBlendState) pipelineStates.push_back(colorBlendState);
if (depthStencilState) pipelineStates.push_back(depthStencilState);
if (dynamicState) pipelineStates.push_back(dynamicState);
if (inputAssemblyState) pipelineStates.push_back(inputAssemblyState);
if (multisampleState) pipelineStates.push_back(multisampleState);
if (rasterizationState) pipelineStates.push_back(rasterizationState);
if (tessellationState) pipelineStates.push_back(tessellationState);
if (vertexInputState) pipelineStates.push_back(vertexInputState);
if (viewportState) pipelineStates.push_back(viewportState);
graphicsPipeline = GraphicsPipeline::create(layout, shaderSet->getShaderStages(shaderHints), pipelineStates, subpass);
bindGraphicsPipeline = vsg::BindGraphicsPipeline::create(graphicsPipeline);
}