vsg 1.1.10
VulkanSceneGraph library
Loading...
Searching...
No Matches
ShaderSet.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2022 Robert Osfield
6
7Permission 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:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE 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.
12
13</editor-fold> */
14
15#include <vsg/core/compare.h>
16#include <vsg/state/ArrayState.h>
17#include <vsg/state/GraphicsPipeline.h>
18#include <vsg/state/Sampler.h>
19#include <vsg/state/ShaderStage.h>
20#include <vsg/utils/CoordinateSpace.h>
21
22namespace vsg
23{
24
25 struct VSG_DECLSPEC AttributeBinding
26 {
27 std::string name;
28 std::string define;
29 uint32_t location = 0;
30 VkFormat format = VK_FORMAT_UNDEFINED;
31 CoordinateSpace coordinateSpace = CoordinateSpace::NO_PREFERENCE;
32 ref_ptr<Data> data;
33
34 int compare(const AttributeBinding& rhs) const;
35
36 explicit operator bool() const noexcept { return !name.empty(); }
37 };
38 VSG_type_name(vsg::AttributeBinding);
39
40 struct VSG_DECLSPEC DescriptorBinding
41 {
42 std::string name;
43 std::string define;
44 uint32_t set = 0;
45 uint32_t binding = 0;
46 VkDescriptorType descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
47 uint32_t descriptorCount = 0;
48 VkShaderStageFlags stageFlags = 0;
49 CoordinateSpace coordinateSpace = CoordinateSpace::NO_PREFERENCE;
50 ref_ptr<Data> data;
51
52 int compare(const DescriptorBinding& rhs) const;
53
54 explicit operator bool() const noexcept { return !name.empty(); }
55 };
56 VSG_type_name(vsg::DescriptorBinding);
57
58 struct VSG_DECLSPEC PushConstantRange
59 {
60 std::string name;
61 std::string define;
62 VkPushConstantRange range;
63
64 int compare(const PushConstantRange& rhs) const;
65 };
66 VSG_type_name(vsg::PushConstantRange);
67
68 struct VSG_DECLSPEC DefinesArrayState
69 {
70 std::set<std::string> defines;
71 ref_ptr<ArrayState> arrayState;
72
73 int compare(const DefinesArrayState& rhs) const;
74 };
75 VSG_type_name(vsg::DefinesArrayState);
76
78 struct VSG_DECLSPEC CustomDescriptorSetBinding : public Inherit<Object, CustomDescriptorSetBinding>
79 {
80 explicit CustomDescriptorSetBinding(uint32_t in_set = 0);
81
82 uint32_t set = 0;
83
84 int compare(const Object& rhs) const override;
85
86 void read(Input& input) override;
87 void write(Output& output) const override;
88
89 virtual bool compatibleDescriptorSetLayout(const DescriptorSetLayout& dsl) const = 0;
90 virtual ref_ptr<DescriptorSetLayout> createDescriptorSetLayout() = 0;
91 virtual ref_ptr<StateCommand> createStateCommand(ref_ptr<PipelineLayout> layout) = 0;
92 };
94
96 struct VSG_DECLSPEC ViewDependentStateBinding : public Inherit<CustomDescriptorSetBinding, ViewDependentStateBinding>
97 {
98 explicit ViewDependentStateBinding(uint32_t in_set = 0);
99
100 int compare(const Object& rhs) const override;
101
102 void read(Input& input) override;
103 void write(Output& output) const override;
104
105 ref_ptr<DescriptorSetLayout> viewDescriptorSetLayout;
106
107 bool compatibleDescriptorSetLayout(const DescriptorSetLayout& dsl) const override;
108 ref_ptr<DescriptorSetLayout> createDescriptorSetLayout() override;
109 ref_ptr<StateCommand> createStateCommand(ref_ptr<PipelineLayout> layout) override;
110 };
111 VSG_type_name(vsg::ViewDependentStateBinding);
112
114 class VSG_DECLSPEC ShaderSet : public Inherit<Object, ShaderSet>
115 {
116 public:
117 ShaderSet();
118 explicit ShaderSet(const ShaderStages& in_stages, ref_ptr<ShaderCompileSettings> in_hints = {});
119
121 ShaderStages stages;
122
123 std::vector<AttributeBinding> attributeBindings;
124 std::vector<DescriptorBinding> descriptorBindings;
125 std::vector<PushConstantRange> pushConstantRanges;
126 std::vector<DefinesArrayState> definesArrayStates; // put more constrained ArrayState matches first so they are matched first.
127 std::set<std::string> optionalDefines;
128 GraphicsPipelineStates defaultGraphicsPipelineStates;
129 std::vector<ref_ptr<CustomDescriptorSetBinding>> customDescriptorSetBindings;
130
131 ref_ptr<ShaderCompileSettings> defaultShaderHints;
133 std::map<ref_ptr<ShaderCompileSettings>, ShaderStages, DereferenceLess> variants;
134
136 std::mutex mutex;
137
139 void addAttributeBinding(const std::string& name, const std::string& define, uint32_t location, VkFormat format, ref_ptr<Data> data, CoordinateSpace coordinateSpace = CoordinateSpace::NO_PREFERENCE);
140
142 void addDescriptorBinding(const std::string& name, const std::string& define, uint32_t set, uint32_t binding, VkDescriptorType descriptorType, uint32_t descriptorCount, VkShaderStageFlags stageFlags, ref_ptr<Data> data, CoordinateSpace coordinateSpace = CoordinateSpace::NO_PREFERENCE);
143
144 [[deprecated("use addDescriptorBinding(..)")]] void addUniformBinding(const std::string& name, const std::string& define, uint32_t set, uint32_t binding, VkDescriptorType descriptorType, uint32_t descriptorCount, VkShaderStageFlags stageFlags, ref_ptr<Data> data) { addDescriptorBinding(name, define, set, binding, descriptorType, descriptorCount, stageFlags, data); }
145
147 void addPushConstantRange(const std::string& name, const std::string& define, VkShaderStageFlags stageFlags, uint32_t offset, uint32_t size);
148
150 AttributeBinding& getAttributeBinding(const std::string& name);
151
153 const AttributeBinding& getAttributeBinding(const std::string& name) const;
154
156 DescriptorBinding& getDescriptorBinding(const std::string& name);
157
159 const DescriptorBinding& getDescriptorBinding(const std::string& name) const;
160
161 [[deprecated("use getDescriptorBinding(..)")]] DescriptorBinding& getUniformBinding(const std::string& name) { return getDescriptorBinding(name); }
162
163 [[deprecated("use getDescriptorBinding(..)")]] const DescriptorBinding& getUnifomrBinding(const std::string& name) const { return getDescriptorBinding(name); }
164
166 ref_ptr<ArrayState> getSuitableArrayState(const std::set<std::string>& defines) const;
167
170
172 std::pair<uint32_t, uint32_t> descriptorSetRange() const;
173
175 virtual bool compatibleDescriptorSetLayout(const DescriptorSetLayout& dsl, const std::set<std::string>& defines, uint32_t set) const;
176
178 virtual ref_ptr<DescriptorSetLayout> createDescriptorSetLayout(const std::set<std::string>& defines, uint32_t set) const;
179
181 virtual bool compatiblePipelineLayout(const PipelineLayout& layout, const std::set<std::string>& defines) const;
182
184 inline ref_ptr<PipelineLayout> createPipelineLayout(const std::set<std::string>& defines) { return createPipelineLayout(defines, descriptorSetRange()); }
185
191 virtual ref_ptr<PipelineLayout> createPipelineLayout(const std::set<std::string>& defines, std::pair<uint32_t, uint32_t> range) const;
192
193 int compare(const Object& rhs) const override;
194
195 void read(Input& input) override;
196 void write(Output& output) const override;
197
198 protected:
199 virtual ~ShaderSet();
200
201 AttributeBinding _nullAttributeBinding;
202 DescriptorBinding _nullDescriptorBinding;
203 };
204 VSG_type_name(vsg::ShaderSet);
205
207 extern VSG_DECLSPEC ref_ptr<ShaderSet> createFlatShadedShaderSet(ref_ptr<const Options> options = {});
208
210 extern VSG_DECLSPEC ref_ptr<ShaderSet> createPhongShaderSet(ref_ptr<const Options> options = {});
211
213 extern VSG_DECLSPEC ref_ptr<ShaderSet> createPhysicsBasedRenderingShaderSet(ref_ptr<const Options> options = {});
214
215} // namespace vsg
DescriptorSetLayout encapsulates VkDescriptorSetLayout and VkDescriptorSetLayoutCreateInfo settings u...
Definition DescriptorSetLayout.h:28
Definition Input.h:44
Definition Object.h:60
Definition Output.h:41
PipelineLayout encapsulates VkPipelineLayout and the VkPipelineLayoutCreateInfo settings used to set ...
Definition PipelineLayout.h:27
ShaderSet provides a collection of shader related settings to provide a form of shader introspection.
Definition ShaderSet.h:115
std::mutex mutex
mutex used by getShaderStages(..) to ensure the variants map can be used from multiple threads.
Definition ShaderSet.h:136
ref_ptr< ArrayState > getSuitableArrayState(const std::set< std::string > &defines) const
get the first ArrayState that has matches with defines in the specified list of defines.
void addAttributeBinding(const std::string &name, const std::string &define, uint32_t location, VkFormat format, ref_ptr< Data > data, CoordinateSpace coordinateSpace=CoordinateSpace::NO_PREFERENCE)
add an attribute binding, Not thread safe, should only be called when initially setting up the Shader...
int compare(const Object &rhs) const override
compare two objects, return -1 if this object is less than rhs, return 0 if it's equal,...
virtual bool compatibleDescriptorSetLayout(const DescriptorSetLayout &dsl, const std::set< std::string > &defines, uint32_t set) const
return true of specified descriptor set layout is compatible with what is required for this ShaderSet
const AttributeBinding & getAttributeBinding(const std::string &name) const
get the const AttributeBinding associated with name
void addPushConstantRange(const std::string &name, const std::string &define, VkShaderStageFlags stageFlags, uint32_t offset, uint32_t size)
add a push constant range. Not thread safe, should only be called when initially setting up the Shade...
const DescriptorBinding & getDescriptorBinding(const std::string &name) const
get the const DescriptorBinding associated with name
virtual ref_ptr< DescriptorSetLayout > createDescriptorSetLayout(const std::set< std::string > &defines, uint32_t set) const
create the descriptor set layout.
AttributeBinding & getAttributeBinding(const std::string &name)
get the AttributeBinding associated with name
ShaderStages getShaderStages(ref_ptr< ShaderCompileSettings > scs={})
get the ShaderStages variant that uses specified ShaderCompileSettings.
std::pair< uint32_t, uint32_t > descriptorSetRange() const
return the <minimum_set, maximum_set+1> range of set numbers encompassing DescriptorBindings
ShaderStages stages
base ShaderStages that other variants are based on.
Definition ShaderSet.h:121
DescriptorBinding & getDescriptorBinding(const std::string &name)
get the DescriptorBinding associated with name
virtual ref_ptr< PipelineLayout > createPipelineLayout(const std::set< std::string > &defines, std::pair< uint32_t, uint32_t > range) const
std::map< ref_ptr< ShaderCompileSettings >, ShaderStages, DereferenceLess > variants
variants of the rootShaderModule compiled for different combinations of ShaderCompileSettings
Definition ShaderSet.h:133
virtual bool compatiblePipelineLayout(const PipelineLayout &layout, const std::set< std::string > &defines) const
return true of specified pipline layout is compatible with what is required for this ShaderSet
void addDescriptorBinding(const std::string &name, const std::string &define, uint32_t set, uint32_t binding, VkDescriptorType descriptorType, uint32_t descriptorCount, VkShaderStageFlags stageFlags, ref_ptr< Data > data, CoordinateSpace coordinateSpace=CoordinateSpace::NO_PREFERENCE)
add an uniform binding. Not thread safe, should only be called when initially setting up the ShaderSe...
ref_ptr< PipelineLayout > createPipelineLayout(const std::set< std::string > &defines)
create the pipeline layout for all descriptor sets enabled by specified defines or required by defaul...
Definition ShaderSet.h:184
Definition ref_ptr.h:22
Definition ShaderSet.h:26
Base class for specifying custom DescriptorSetLayout and StateCommand.
Definition ShaderSet.h:79
int compare(const Object &rhs) const override
compare two objects, return -1 if this object is less than rhs, return 0 if it's equal,...
Definition ShaderSet.h:69
less functor for comparing ref_ptr<Object> typically used with std::set<> etc.
Definition compare.h:107
Definition ShaderSet.h:41
Definition ShaderSet.h:59
Custom state binding class for providing the DescriptorSetLayout and StateCommand required to pass vi...
Definition ShaderSet.h:97
int compare(const Object &rhs) const override
compare two objects, return -1 if this object is less than rhs, return 0 if it's equal,...