vsg 1.1.3
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
21namespace vsg
22{
23
24 struct VSG_DECLSPEC AttributeBinding
25 {
26 std::string name;
27 std::string define;
28 uint32_t location = 0;
29 VkFormat format = VK_FORMAT_UNDEFINED;
30 ref_ptr<Data> data;
31
32 int compare(const AttributeBinding& rhs) const;
33
34 explicit operator bool() const noexcept { return !name.empty(); }
35 };
36 VSG_type_name(vsg::AttributeBinding);
37
38 struct VSG_DECLSPEC DescriptorBinding
39 {
40 std::string name;
41 std::string define;
42 uint32_t set = 0;
43 uint32_t binding = 0;
44 VkDescriptorType descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
45 uint32_t descriptorCount = 0;
46 VkShaderStageFlags stageFlags = 0;
47 ref_ptr<Data> data;
48
49 int compare(const DescriptorBinding& rhs) const;
50
51 explicit operator bool() const noexcept { return !name.empty(); }
52 };
53 VSG_type_name(vsg::DescriptorBinding);
54
55 struct VSG_DECLSPEC PushConstantRange
56 {
57 std::string name;
58 std::string define;
59 VkPushConstantRange range;
60
61 int compare(const PushConstantRange& rhs) const;
62 };
63 VSG_type_name(vsg::PushConstantRange);
64
65 struct VSG_DECLSPEC DefinesArrayState
66 {
67 std::set<std::string> defines;
68 ref_ptr<ArrayState> arrayState;
69
70 int compare(const DefinesArrayState& rhs) const;
71 };
72 VSG_type_name(vsg::DefinesArrayState);
73
75 struct VSG_DECLSPEC CustomDescriptorSetBinding : public Inherit<Object, CustomDescriptorSetBinding>
76 {
77 CustomDescriptorSetBinding(uint32_t in_set = 0);
78
79 uint32_t set = 0;
80
81 int compare(const Object& rhs) const override;
82
83 void read(Input& input) override;
84 void write(Output& output) const override;
85
86 virtual bool compatibleDescriptorSetLayout(const DescriptorSetLayout& dsl) const = 0;
87 virtual ref_ptr<DescriptorSetLayout> createDescriptorSetLayout() = 0;
88 virtual ref_ptr<StateCommand> createStateCommand(ref_ptr<PipelineLayout> layout) = 0;
89 };
91
93 struct VSG_DECLSPEC ViewDependentStateBinding : public Inherit<CustomDescriptorSetBinding, ViewDependentStateBinding>
94 {
95 ViewDependentStateBinding(uint32_t in_set = 0);
96
97 int compare(const Object& rhs) const override;
98
99 void read(Input& input) override;
100 void write(Output& output) const override;
101
102 ref_ptr<DescriptorSetLayout> viewDescriptorSetLayout;
103
104 bool compatibleDescriptorSetLayout(const DescriptorSetLayout& dsl) const override;
105 ref_ptr<DescriptorSetLayout> createDescriptorSetLayout() override;
106 ref_ptr<StateCommand> createStateCommand(ref_ptr<PipelineLayout> layout) override;
107 };
108 VSG_type_name(vsg::ViewDependentStateBinding);
109
111 class VSG_DECLSPEC ShaderSet : public Inherit<Object, ShaderSet>
112 {
113 public:
114 ShaderSet();
115 explicit ShaderSet(const ShaderStages& in_stages, ref_ptr<ShaderCompileSettings> in_hints = {});
116
118 ShaderStages stages;
119
120 std::vector<AttributeBinding> attributeBindings;
121 std::vector<DescriptorBinding> descriptorBindings;
122 std::vector<PushConstantRange> pushConstantRanges;
123 std::vector<DefinesArrayState> definesArrayStates; // put more constrained ArrayState matches first so they are matched first.
124 std::set<std::string> optionalDefines;
125 GraphicsPipelineStates defaultGraphicsPipelineStates;
126 std::vector<ref_ptr<CustomDescriptorSetBinding>> customDescriptorSetBindings;
127
128 ref_ptr<ShaderCompileSettings> defaultShaderHints;
130 std::map<ref_ptr<ShaderCompileSettings>, ShaderStages, DereferenceLess> variants;
131
133 std::mutex mutex;
134
136 void addAttributeBinding(const std::string& name, const std::string& define, uint32_t location, VkFormat format, ref_ptr<Data> data);
137
139 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);
140
141 [[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); }
142
144 void addPushConstantRange(const std::string& name, const std::string& define, VkShaderStageFlags stageFlags, uint32_t offset, uint32_t size);
145
147 AttributeBinding& getAttributeBinding(const std::string& name);
148
150 const AttributeBinding& getAttributeBinding(const std::string& name) const;
151
153 DescriptorBinding& getDescriptorBinding(const std::string& name);
154
156 const DescriptorBinding& getDescriptorBinding(const std::string& name) const;
157
158 [[deprecated("use getDescriptorBinding(..)")]] DescriptorBinding& getUniformBinding(const std::string& name) { return getDescriptorBinding(name); }
159
160 [[deprecated("use getDescriptorBinding(..)")]] const DescriptorBinding& getUnifomrBinding(const std::string& name) const { return getDescriptorBinding(name); }
161
163 ref_ptr<ArrayState> getSuitableArrayState(const std::set<std::string>& defines) const;
164
167
169 std::pair<uint32_t, uint32_t> descriptorSetRange() const;
170
172 virtual bool compatibleDescriptorSetLayout(const DescriptorSetLayout& dsl, const std::set<std::string>& defines, uint32_t set) const;
173
175 virtual ref_ptr<DescriptorSetLayout> createDescriptorSetLayout(const std::set<std::string>& defines, uint32_t set) const;
176
178 virtual bool compatiblePipelineLayout(const PipelineLayout& layout, const std::set<std::string>& defines) const;
179
181 inline ref_ptr<PipelineLayout> createPipelineLayout(const std::set<std::string>& defines) { return createPipelineLayout(defines, descriptorSetRange()); }
182
188 virtual ref_ptr<PipelineLayout> createPipelineLayout(const std::set<std::string>& defines, std::pair<uint32_t, uint32_t> range) const;
189
190 int compare(const Object& rhs) const override;
191
192 void read(Input& input) override;
193 void write(Output& output) const override;
194
195 protected:
196 virtual ~ShaderSet();
197
198 AttributeBinding _nullAttributeBinding;
199 DescriptorBinding _nullDescriptorBinding;
200 };
201 VSG_type_name(vsg::ShaderSet);
202
204 extern VSG_DECLSPEC ref_ptr<ShaderSet> createFlatShadedShaderSet(ref_ptr<const Options> options = {});
205
207 extern VSG_DECLSPEC ref_ptr<ShaderSet> createPhongShaderSet(ref_ptr<const Options> options = {});
208
210 extern VSG_DECLSPEC ref_ptr<ShaderSet> createPhysicsBasedRenderingShaderSet(ref_ptr<const Options> options = {});
211
212} // namespace vsg
DescriptorSetLayout encapsulates VkDescriptorSetLayout and VkDescriptorSetLayoutCreateInfo settings u...
Definition DescriptorSetLayout.h:28
Definition Inherit.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:112
std::mutex mutex
mutex used by getShaderStages(..) to ensure the variants map can be used from multiple threads.
Definition ShaderSet.h:133
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.
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.
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)
add an uniform binding. Not thread safe, should only be called when initially setting up the ShaderSe...
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:118
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:130
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 addAttributeBinding(const std::string &name, const std::string &define, uint32_t location, VkFormat format, ref_ptr< Data > data)
add an attribute binding, Not thread safe, should only be called when initially setting up the Shader...
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:181
Definition ref_ptr.h:22
Definition ShaderSet.h:25
Base class for specifying custom DescriptorSetLayout and StateCommand.
Definition ShaderSet.h:76
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:66
less functor for comparing ref_ptr<Object> typically used with std::set<> etc.
Definition compare.h:107
Definition ShaderSet.h:39
Definition ShaderSet.h:56
Custom state binding class for providing the DescriptorSetLayout and StateCommand required to pass vi...
Definition ShaderSet.h:94
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,...