Add ShaderReflector

- Used to look at the shader files and find set/binding automatically
This commit is contained in:
Thraix
2023-03-14 22:56:11 +01:00
parent 6e463b3560
commit 4fe719858d
20 changed files with 382 additions and 151 deletions
@@ -16,7 +16,7 @@ namespace Copium
descriptorPool.FreeDescriptorSets(descriptorSets);
}
void DescriptorSet::AddUniform(const UniformBuffer& uniformBuffer, uint32_t binding)
void DescriptorSet::SetUniformBuffer(const UniformBuffer& uniformBuffer, uint32_t binding)
{
for (size_t i = 0; i < descriptorSets.size(); ++i) {
VkDescriptorBufferInfo bufferInfo = uniformBuffer.GetDescriptorBufferInfo(i);
@@ -35,15 +35,15 @@ namespace Copium
}
}
void DescriptorSet::AddSampler(const Sampler& sampler, uint32_t binding, int arrayIndex)
void DescriptorSet::SetSampler(const Sampler& sampler, uint32_t binding, int arrayIndex)
{
for (size_t i = 0; i < descriptorSets.size(); ++i)
{
AddSampler(sampler, binding, i, arrayIndex);
SetSampler(sampler, binding, i, arrayIndex);
}
}
void DescriptorSet::AddSampler(const Sampler& sampler, uint32_t binding, int index, int arrayIndex)
void DescriptorSet::SetSampler(const Sampler& sampler, uint32_t binding, int index, int arrayIndex)
{
CP_ASSERT(index >= 0 && index < descriptorSets.size(), "AddSampler : index is out of range");
VkDescriptorImageInfo imageInfo = sampler.GetDescriptorImageInfo(index);
@@ -60,7 +60,7 @@ namespace Copium
vkUpdateDescriptorSets(vulkan.GetDevice(), 1, &descriptorWrite, 0, nullptr);
}
void DescriptorSet::AddSamplers(const std::vector<const Sampler*>& samplers, uint32_t binding)
void DescriptorSet::SetSamplers(const std::vector<const Sampler*>& samplers, uint32_t binding)
{
for (size_t i = 0; i < descriptorSets.size(); ++i) {
std::vector<VkWriteDescriptorSet> descriptorWrites{samplers.size()};
@@ -23,10 +23,10 @@ namespace Copium
DescriptorSet(Vulkan& vulkan, DescriptorPool& descriptorPool, VkDescriptorSetLayout descriptorSetLayout);
~DescriptorSet();
void AddUniform(const UniformBuffer& uniformBuffer, uint32_t binding);
void AddSampler(const Sampler& sampler, uint32_t binding, int arrayIndex = 0);
void AddSampler(const Sampler& sampler, uint32_t binding, int index, int arrayIndex = 0);
void AddSamplers(const std::vector<const Sampler*>& sampler, uint32_t binding);
void SetUniformBuffer(const UniformBuffer& uniformBuffer, uint32_t binding);
void SetSampler(const Sampler& sampler, uint32_t binding, int arrayIndex = 0);
void SetSampler(const Sampler& sampler, uint32_t binding, int index, int arrayIndex = 0);
void SetSamplers(const std::vector<const Sampler*>& sampler, uint32_t binding);
operator VkDescriptorSet() const;
};
}
@@ -4,40 +4,71 @@
namespace Copium
{
PipelineCreator::PipelineCreator(VkRenderPass renderPass, const std::string& vertexShader, const std::string& fragmentShader)
: vertexShader{vertexShader},
fragmentShader{fragmentShader},
renderPass{renderPass}
{}
PipelineCreator::PipelineCreator(VkRenderPass renderPass, const std::string& vertexShader, const std::string& fragmentShader)
: vertexShader{vertexShader},
fragmentShader{fragmentShader},
renderPass{renderPass}
{
AddShaderDescription();
}
void PipelineCreator::SetVertexDescriptor(const VertexDescriptor& descriptor)
{
vertexDescriptor = descriptor;
}
void PipelineCreator::SetVertexDescriptor(const VertexDescriptor& descriptor)
{
vertexDescriptor = descriptor;
}
void PipelineCreator::AddDescriptorSetLayoutBinding(uint32_t set, uint32_t binding, VkDescriptorType type, uint32_t count, VkShaderStageFlags stageFlags)
{
CP_ASSERT(set <= descriptorSetLayouts.size(), "AddDescriptorSetLayoutBinding : Cannot add descriptor set with set number greater than the current set count");
descriptorSetLayouts[set].emplace_back(DescriptorSetBinding{binding, type, count, stageFlags});
}
void PipelineCreator::SetPrimitiveTopology(VkPrimitiveTopology primitiveTopology)
{
topology = primitiveTopology;
}
void PipelineCreator::SetPrimitiveTopology(VkPrimitiveTopology primitiveTopology)
{
topology = primitiveTopology;
}
void PipelineCreator::SetCullMode(VkCullModeFlags flags)
{
cullMode = flags;
}
void PipelineCreator::SetCullMode(VkCullModeFlags flags)
{
cullMode = flags;
}
void PipelineCreator::SetCullFrontFace(VkFrontFace cullFrontFace)
{
frontFace = cullFrontFace;
}
void PipelineCreator::SetCullFrontFace(VkFrontFace cullFrontFace)
{
frontFace = cullFrontFace;
}
void PipelineCreator::SetDepthTest(bool depthTest)
{
this->depthTest = depthTest;
}
void PipelineCreator::SetDepthTest(bool depthTest)
{
this->depthTest = depthTest;
}
void PipelineCreator::AddShaderDescription()
{
ShaderReflector reflector{vertexShader, fragmentShader};
for (auto& binding : reflector.bindings)
{
descriptorSetLayouts[binding.set].emplace_back(DescriptorSetBinding{binding.binding, GetDescriptorType(binding.bindingType), binding.arraySize, GetShaderStageFlags(binding.shaderType)});
}
}
VkDescriptorType PipelineCreator::GetDescriptorType(BindingType type)
{
switch (type)
{
case BindingType::Sampler2D:
return VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
case BindingType::UniformBuffer:
return VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
default:
CP_ABORT("GetDescriptorType : Unhandled switch case");
}
}
VkShaderStageFlags PipelineCreator::GetShaderStageFlags(ShaderType type)
{
switch (type)
{
case ShaderType::Vertex:
return VK_SHADER_STAGE_VERTEX_BIT;
case ShaderType::Fragment:
return VK_SHADER_STAGE_FRAGMENT_BIT;
default:
CP_ABORT("GetShaderStageFlags : Unhandled switch case");
}
}
}
@@ -1,6 +1,7 @@
#pragma once
#include "copium/pipeline/VertexDescriptor.h"
#include "copium/util/ShaderReflector.h"
#include <map>
#include <string>
@@ -8,36 +9,39 @@
namespace Copium
{
class PipelineCreator
{
struct DescriptorSetBinding
{
uint32_t binding;
VkDescriptorType type;
uint32_t count;
VkShaderStageFlags flags;
};
friend class Pipeline;
private:
std::map<uint32_t, std::vector<DescriptorSetBinding>> descriptorSetLayouts{};
class PipelineCreator
{
struct DescriptorSetBinding
{
uint32_t binding;
VkDescriptorType type;
uint32_t count;
VkShaderStageFlags flags;
};
friend class Pipeline;
private:
std::map<uint32_t, std::vector<DescriptorSetBinding>> descriptorSetLayouts{};
std::string vertexShader;
std::string fragmentShader;
VertexDescriptor vertexDescriptor{};
VkPrimitiveTopology topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
VkCullModeFlags cullMode = VK_CULL_MODE_BACK_BIT;
VkFrontFace frontFace = VK_FRONT_FACE_CLOCKWISE;
VkRenderPass renderPass = VK_NULL_HANDLE;
bool depthTest = true;
std::string vertexShader;
std::string fragmentShader;
VertexDescriptor vertexDescriptor{};
VkPrimitiveTopology topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
VkCullModeFlags cullMode = VK_CULL_MODE_FRONT_BIT;
VkFrontFace frontFace = VK_FRONT_FACE_CLOCKWISE;
VkRenderPass renderPass = VK_NULL_HANDLE;
bool depthTest = true;
public:
PipelineCreator(VkRenderPass renderPass, const std::string& vertexShader, const std::string& fragmentShader);
public:
PipelineCreator(VkRenderPass renderPass, const std::string& vertexShader, const std::string& fragmentShader);
void SetVertexDescriptor(const VertexDescriptor& descriptor);
void AddDescriptorSetLayoutBinding(uint32_t set, uint32_t binding, VkDescriptorType type, uint32_t count, VkShaderStageFlags stageFlags);
void SetPrimitiveTopology(VkPrimitiveTopology primitiveTopology);
void SetCullMode(VkCullModeFlags flags);
void SetCullFrontFace(VkFrontFace cullFrontFace);
void SetDepthTest(bool depthTest);
};
void SetVertexDescriptor(const VertexDescriptor& descriptor);
void SetPrimitiveTopology(VkPrimitiveTopology primitiveTopology);
void SetCullMode(VkCullModeFlags flags);
void SetCullFrontFace(VkFrontFace cullFrontFace);
void SetDepthTest(bool depthTest);
private:
void AddShaderDescription();
static VkDescriptorType GetDescriptorType(BindingType type);
static VkShaderStageFlags GetShaderStageFlags(ShaderType type);
};
}
+2 -3
View File
@@ -80,9 +80,8 @@ namespace Copium
if (FileSystem::DateModified(filename) < FileSystem::DateModified(spvFilename))
{
CP_DEBUG("InitializeShaderModuleFromGlslFile : Loading cached shader file: %s", filename.c_str());
std::vector<char> data = FileSystem::ReadFile(spvFilename);
CP_ASSERT(data.size() % 4 == 0, "Spv data size is not a factor of 4");
return InitializeShaderModule((const uint32_t*)data.data(), data.size());
std::vector<uint32_t> data = FileSystem::ReadFile32(spvFilename);
return InitializeShaderModule(data.data(), data.size() * sizeof(uint32_t));
}
}
}