24 using Define = std::pair<std::string, std::string>;
29 VertexInput(std::vector<VkVertexInputBindingDescription> bindingDescriptions = {},
30 std::vector<VkVertexInputAttributeDescription> attributeDescriptions = {})
31 : bindingDescriptions(std::move(bindingDescriptions))
32 , attributeDescriptions(std::move(attributeDescriptions))
36 const std::vector<VkVertexInputBindingDescription>& GetBindingDescriptions()
const
38 return bindingDescriptions;
40 const std::vector<VkVertexInputAttributeDescription>& GetAttributeDescriptions()
const
42 return attributeDescriptions;
47 return bindingDescriptions.front().binding < rhs.bindingDescriptions.front().binding;
52 node[
"bindingDescriptions"] >> vertexInput.bindingDescriptions;
53 node[
"attributeDescriptions"] >> vertexInput.attributeDescriptions;
59 node[
"bindingDescriptions"] << vertexInput.bindingDescriptions;
60 node[
"attributeDescriptions"] << vertexInput.attributeDescriptions;
66 std::vector<VkVertexInputBindingDescription> bindingDescriptions;
67 std::vector<VkVertexInputAttributeDescription> attributeDescriptions;
75 explicit Uniform(int32_t binding = -1, int32_t offset = -1, int32_t size = -1, int32_t glType = -1,
76 bool readOnly =
false,
bool writeOnly =
false, VkShaderStageFlags stageFlags = 0)
82 , writeOnly(writeOnly)
83 , stageFlags(stageFlags)
87 int32_t GetBinding()
const
91 int32_t GetOffset()
const
95 int32_t GetSize()
const
99 int32_t GetGlType()
const
103 bool IsReadOnly()
const
107 bool IsWriteOnly()
const
111 VkShaderStageFlags GetStageFlags()
const
116 bool operator==(
const Uniform& rhs)
const
118 return binding == rhs.binding && offset == rhs.offset && size == rhs.size && glType == rhs.glType &&
119 readOnly == rhs.readOnly && writeOnly == rhs.writeOnly && stageFlags == rhs.stageFlags;
122 bool operator!=(
const Uniform& rhs)
const
124 return !operator==(rhs);
129 node[
"binding"] >> uniform.binding;
130 node[
"offset"] >> uniform.offset;
131 node[
"size"] >> uniform.size;
132 node[
"glType"] >> uniform.glType;
133 node[
"readOnly"] >> uniform.readOnly;
134 node[
"writeOnly"] >> uniform.writeOnly;
135 node[
"stageFlags"] >> uniform.stageFlags;
141 node[
"binding"].Set(uniform.binding);
142 node[
"offset"].Set(uniform.offset);
143 node[
"size"].Set(uniform.size);
144 node[
"glType"].Set(uniform.glType);
145 node[
"readOnly"].Set(uniform.readOnly);
146 node[
"writeOnly"].Set(uniform.writeOnly);
147 node[
"stageFlags"].Set(uniform.stageFlags);
158 VkShaderStageFlags stageFlags;
174 explicit UniformBlock(int32_t binding = -1, int32_t size = -1, VkShaderStageFlags stageFlags = 0,
175 Type type = Type::Uniform)
178 , stageFlags(stageFlags)
183 int32_t GetBinding()
const
187 int32_t GetSize()
const
191 VkShaderStageFlags GetStageFlags()
const
199 const std::map<std::string, Uniform>& GetUniforms()
const
204 std::optional<Uniform> GetUniform(
const std::string& name)
const
206 auto it = uniforms.find(name);
207 return (it != uniforms.end()) ? std::optional<Uniform>(it->second) : std::nullopt;
212 return binding == rhs.binding && size == rhs.size && stageFlags == rhs.stageFlags && type == rhs.type &&
213 uniforms == rhs.uniforms;
217 return !operator==(rhs);
222 node[
"binding"] >> uniformBlock.binding;
223 node[
"size"] >> uniformBlock.size;
224 node[
"stageFlags"] >> uniformBlock.stageFlags;
225 node[
"type"] >> uniformBlock.type;
226 node[
"uniforms"] >> uniformBlock.uniforms;
231 node[
"binding"].Set(uniformBlock.binding);
232 node[
"size"].Set(uniformBlock.size);
233 node[
"stageFlags"].Set(uniformBlock.stageFlags);
234 node[
"type"].Set(uniformBlock.type);
235 node[
"uniforms"].Set(uniformBlock.uniforms);
242 VkShaderStageFlags stageFlags;
244 std::map<std::string, Uniform> uniforms;
252 explicit Attribute(int32_t set = -1, int32_t location = -1, int32_t size = -1, int32_t glType = -1)
260 int32_t GetSet()
const
264 int32_t GetLocation()
const
268 int32_t GetSize()
const
272 int32_t GetGlType()
const
277 bool operator==(
const Attribute& rhs)
const
279 return set == rhs.set && location == rhs.location && size == rhs.size && glType == rhs.glType;
281 bool operator!=(
const Attribute& rhs)
const
283 return !operator==(rhs);
288 node[
"set"] >> attribute.set;
289 node[
"location"] >> attribute.location;
290 node[
"size"] >> attribute.size;
291 node[
"glType"] >> attribute.glType;
296 node[
"set"] << attribute.set;
297 node[
"location"] << attribute.location;
298 node[
"size"] << attribute.size;
299 node[
"glType"] << attribute.glType;
315 explicit Constant(int32_t binding = -1, int32_t size = -1, VkShaderStageFlags stageFlags = 0,
319 , stageFlags(stageFlags)
324 int32_t GetBinding()
const
328 int32_t GetSize()
const
332 VkShaderStageFlags GetStageFlags()
const
336 int32_t GetGlType()
const
341 bool operator==(
const Constant& rhs)
const
343 return binding == rhs.binding && size == rhs.size && stageFlags == rhs.stageFlags &&
344 glType == rhs.glType;
346 bool operator!=(
const Constant& rhs)
const
348 return !operator==(rhs);
353 node[
"binding"] >> constant.binding;
354 node[
"size"] >> constant.size;
355 node[
"stageFlags"] >> constant.stageFlags;
356 node[
"glType"] >> constant.glType;
361 node[
"binding"] << constant.binding;
362 node[
"size"] << constant.size;
363 node[
"stageFlags"] << constant.stageFlags;
364 node[
"glType"] << constant.glType;
371 VkShaderStageFlags stageFlags;
377 bool ReportedNotFound(
const std::string& name,
bool reportIfFound)
const;
378 static VkFormat GlTypeToVk(int32_t type);
379 std::optional<uint32_t> GetDescriptorLocation(
const std::string& name)
const;
380 std::optional<uint32_t> GetDescriptorSize(
const std::string& name)
const;
381 std::optional<Uniform> GetUniform(
const std::string& name)
const;
382 std::optional<UniformBlock> GetUniformBlock(
const std::string& name)
const;
383 std::optional<Attribute> GetAttribute(
const std::string& name)
const;
384 std::vector<VkPushConstantRange> GetPushConstantRanges()
const;
386 std::optional<VkDescriptorType> GetDescriptorType(uint32_t location)
const;
387 static VkShaderStageFlagBits GetShaderStage(
const VirtualPath& filename);
388 VkShaderModule CreateShaderModule(
const VirtualPath& moduleName,
const std::string& moduleCode,
389 const std::string& preamble, VkShaderStageFlagBits moduleFlag);
393 return m_Stages.back();
395 uint32_t GetLastDescriptorBinding()
const
397 return m_LastDescriptorBinding;
399 const std::map<std::string, Uniform>& GetUniforms()
const
403 const std::map<std::string, UniformBlock>& GetUniformBlocks()
const
405 return m_UniformBlocks;
407 const std::map<std::string, Attribute>& GetAttributes()
const
411 const std::map<std::string, Constant>& GetConstants()
const
415 const std::array<std::optional<uint32_t>, 3>& GetLocalSizes()
const
419 const std::vector<VkDescriptorSetLayoutBinding>& GetDescriptorSetLayouts()
const
421 return m_DescriptorSetLayouts;
423 const std::vector<VkDescriptorPoolSize>& GetDescriptorPools()
const
425 return m_DescriptorPools;
427 const std::vector<VkVertexInputAttributeDescription>& GetAttributeDescriptions()
const
429 return m_AttributeDescriptions;
432 friend const Node& operator>>(
const Node& node, Shader& shader);
433 friend Node& operator<<(Node& node,
const Shader& shader);
436 void ReflectShader(
const SpvReflectShaderModule& spvModule, VkShaderStageFlags stageFlag);
439 std::vector<VirtualPath> m_Stages;
440 std::map<std::string, Uniform> m_Uniforms;
441 std::map<std::string, UniformBlock> m_UniformBlocks;
442 std::map<std::string, Attribute> m_Attributes;
443 std::map<std::string, Constant> m_Constants;
444 std::array<std::optional<uint32_t>, 3> m_LocalSizes;
445 std::map<std::string, uint32_t> m_DescriptorLocations;
446 std::map<std::string, uint32_t> m_DescriptorSizes;
447 std::vector<VkDescriptorSetLayoutBinding> m_DescriptorSetLayouts;
448 uint32_t m_LastDescriptorBinding = 0;
449 std::vector<VkDescriptorPoolSize> m_DescriptorPools;
450 std::map<uint32_t, VkDescriptorType> m_DescriptorTypes;
451 std::vector<VkVertexInputAttributeDescription> m_AttributeDescriptions;
452 mutable std::vector<std::string> m_NotFoundNames;