vsg  1.0.4
VulkanSceneGraph library
ShaderSet.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2022 Robert Osfield
6 
7 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:
8 
9 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10 
11 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.
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 
21 namespace 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 UniformBinding
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 UniformBinding& rhs) const;
50 
51  explicit operator bool() const noexcept { return !name.empty(); }
52  };
53  VSG_type_name(vsg::UniformBinding);
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  class VSG_DECLSPEC ShaderSet : public Inherit<Object, ShaderSet>
76  {
77  public:
78  ShaderSet();
79  explicit ShaderSet(const ShaderStages& in_stages, ref_ptr<ShaderCompileSettings> in_hints = {});
80 
82  ShaderStages stages;
83 
84  std::vector<AttributeBinding> attributeBindings;
85  std::vector<UniformBinding> uniformBindings;
86  std::vector<PushConstantRange> pushConstantRanges;
87  std::vector<DefinesArrayState> definesArrayStates; // put more constrained ArrayState matches first so they are matched first.
88  std::set<std::string> optionalDefines;
89  GraphicsPipelineStates defaultGraphicsPipelineStates;
90 
91  ref_ptr<ShaderCompileSettings> defaultShaderHints;
93  std::map<ref_ptr<ShaderCompileSettings>, ShaderStages, DereferenceLess> variants;
94 
96  std::mutex mutex;
97 
99  void addAttributeBinding(std::string name, std::string define, uint32_t location, VkFormat format, ref_ptr<Data> data);
100 
102  void addUniformBinding(std::string name, std::string define, uint32_t set, uint32_t binding, VkDescriptorType descriptorType, uint32_t descriptorCount, VkShaderStageFlags stageFlags, ref_ptr<Data> data);
103 
105  void addPushConstantRange(std::string name, std::string define, VkShaderStageFlags stageFlags, uint32_t offset, uint32_t size);
106 
108  AttributeBinding& getAttributeBinding(const std::string& name);
109 
111  UniformBinding& getUniformBinding(const std::string& name);
112 
114  const AttributeBinding& getAttributeBinding(const std::string& name) const;
115 
117  const UniformBinding& getUniformBinding(const std::string& name) const;
118 
120  ref_ptr<ArrayState> getSuitableArrayState(const std::set<std::string>& defines) const;
121 
124 
125  int compare(const Object& rhs) const override;
126 
127  void read(Input& input) override;
128  void write(Output& output) const override;
129 
130  protected:
131  virtual ~ShaderSet();
132 
133  AttributeBinding _nullAttributeBinding;
134  UniformBinding _nullUniformBinding;
135  };
136  VSG_type_name(vsg::ShaderSet);
137 
139  extern VSG_DECLSPEC ref_ptr<ShaderSet> createFlatShadedShaderSet(ref_ptr<const Options> options = {});
140 
142  extern VSG_DECLSPEC ref_ptr<ShaderSet> createPhongShaderSet(ref_ptr<const Options> options = {});
143 
145  extern VSG_DECLSPEC ref_ptr<ShaderSet> createPhysicsBasedRenderingShaderSet(ref_ptr<const Options> options = {});
146 
147 } // namespace vsg
Definition: Inherit.h:28
Definition: Input.h:43
Definition: Object.h:42
Definition: Output.h:40
ShaderSet provides collection of shader related settings to provide a form of shader introspection.
Definition: ShaderSet.h:76
UniformBinding & getUniformBinding(const std::string &name)
get the UniformBinding associated with name
std::mutex mutex
mutex used be getShaderStages(..) so ensure the variants map can be used from multiple threads.
Definition: ShaderSet.h:96
void addPushConstantRange(std::string name, std::string define, VkShaderStageFlags stageFlags, uint32_t offset, uint32_t size)
add an uniform binding. Not thread safe, should only be called when initially setting up the ShaderSe...
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,...
void addAttributeBinding(std::string name, 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...
ShaderStages getShaderStages(ref_ptr< ShaderCompileSettings > scs={})
get the ShaderStages variant that uses specified ShaderCompileSettings.
ShaderStages stages
base ShaderStages that other variants as based on.
Definition: ShaderSet.h:82
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.
const UniformBinding & getUniformBinding(const std::string &name) const
get the const UniformBinding associated with name
std::map< ref_ptr< ShaderCompileSettings >, ShaderStages, DereferenceLess > variants
variants of the rootShaderModule compiled for different combinations of ShaderCompileSettings
Definition: ShaderSet.h:93
AttributeBinding & getAttributeBinding(const std::string &name)
get the AttributeBinding associated with name
void addUniformBinding(std::string name, 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...
const AttributeBinding & getAttributeBinding(const std::string &name) const
get the const AttributeBinding associated with name
Definition: ref_ptr.h:22
Definition: ShaderSet.h:25
Definition: ShaderSet.h:66
less functor for comparing ref_ptr<Object> typically used with std::set<> etc.
Definition: compare.h:107
Definition: ShaderSet.h:56
Definition: ShaderSet.h:39