Add ShaderReflector
- Used to look at the shader files and find set/binding automatically
This commit is contained in:
@@ -29,9 +29,9 @@ namespace Copium
|
||||
|
||||
const std::vector<VertexPassthrough> verticesPassthrough = {
|
||||
VertexPassthrough{{-1.0f, -1.0f}},
|
||||
VertexPassthrough{{ 1.0f, -1.0f}},
|
||||
VertexPassthrough{{ 1.0f, 1.0f}},
|
||||
VertexPassthrough{{-1.0f, 1.0f}},
|
||||
VertexPassthrough{{ 1.0f, 1.0f}},
|
||||
VertexPassthrough{{ 1.0f, -1.0f}},
|
||||
};
|
||||
|
||||
const std::vector<uint16_t> indicesPassthrough = {
|
||||
@@ -69,7 +69,7 @@ namespace Copium
|
||||
if (framebuffer->GetWidth() != vulkan->GetSwapChain().GetExtent().width || framebuffer->GetHeight() != vulkan->GetSwapChain().GetExtent().height)
|
||||
{
|
||||
framebuffer->Resize(vulkan->GetSwapChain().GetExtent().width / 8, vulkan->GetSwapChain().GetExtent().height / 8);
|
||||
descriptorSetPassthrough->AddSampler(framebuffer->GetColorAttachment(), 0);
|
||||
descriptorSetPassthrough->SetSampler(framebuffer->GetColorAttachment(), 0);
|
||||
}
|
||||
|
||||
if (!vulkan->GetSwapChain().BeginPresent())
|
||||
@@ -113,26 +113,21 @@ namespace Copium
|
||||
descriptorPool = std::make_unique<DescriptorPool>(*vulkan);
|
||||
|
||||
descriptorSet = std::make_unique<DescriptorSet>(*vulkan, *descriptorPool, graphicsPipeline->GetDescriptorSetLayout(0));
|
||||
descriptorSet->AddUniform(*shaderUniformBuffer, 0);
|
||||
descriptorSet->AddSampler(*texture2D, 1);
|
||||
descriptorSet->SetUniformBuffer(*shaderUniformBuffer, 0);
|
||||
descriptorSet->SetSampler(*texture2D, 1);
|
||||
|
||||
descriptorSetPassthrough = std::make_unique<DescriptorSet>(*vulkan, *descriptorPool, graphicsPipelinePassthrough->GetDescriptorSetLayout(0));
|
||||
descriptorSetPassthrough->AddSampler(framebuffer->GetColorAttachment(), 0);
|
||||
descriptorSetPassthrough->SetSampler(framebuffer->GetColorAttachment(), 0);
|
||||
}
|
||||
|
||||
void Application::InitializeGraphicsPipeline()
|
||||
{
|
||||
PipelineCreator creator{framebuffer->GetRenderPass(), "res/shaders/shader.vert", "res/shaders/shader.frag"};
|
||||
creator.AddDescriptorSetLayoutBinding(0, 0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT);
|
||||
creator.AddDescriptorSetLayoutBinding(0, 1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT);
|
||||
creator.SetVertexDescriptor(Vertex::GetDescriptor());
|
||||
creator.SetCullMode(VK_CULL_MODE_NONE);
|
||||
graphicsPipeline = std::make_unique<Pipeline>(*vulkan, creator);
|
||||
|
||||
PipelineCreator creatorPassthrough{vulkan->GetSwapChain().GetRenderPass(), "res/shaders/passthrough.vert", "res/shaders/passthrough.frag"};
|
||||
creatorPassthrough.AddDescriptorSetLayoutBinding(0, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT);
|
||||
creatorPassthrough.SetVertexDescriptor(VertexPassthrough::GetDescriptor());
|
||||
creatorPassthrough.SetCullMode(VK_CULL_MODE_NONE);
|
||||
graphicsPipelinePassthrough = std::make_unique<Pipeline>(*vulkan, creatorPassthrough);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace Copium
|
||||
vertexBuffer{vulkan, RendererVertex::GetDescriptor(), vertexCount},
|
||||
descriptorSet{vulkan, descriptorPool, pipeline.GetDescriptorSetLayout(0)}
|
||||
{
|
||||
descriptorSet.AddSamplers(samplers, 0);
|
||||
descriptorSet.SetSamplers(samplers, 0);
|
||||
}
|
||||
|
||||
RendererVertexBuffer& DrawCall::GetVertexBuffer()
|
||||
|
||||
@@ -25,10 +25,10 @@ namespace Copium
|
||||
void Renderer::Quad(const glm::vec2& from, const glm::vec2& to, const glm::vec3& color)
|
||||
{
|
||||
AllocateQuad();
|
||||
AddVertex(from, color, -1, glm::vec2{0, 0});
|
||||
AddVertex(glm::vec2{to.x, from.y}, color, -1, glm::vec2{0, 0});
|
||||
AddVertex(to, color, -1, glm::vec2{0, 0});
|
||||
AddVertex(glm::vec2{from.x, to.y}, color, -1, glm::vec2{0, 0});
|
||||
AddVertex(to, color, -1, glm::vec2{0, 0});
|
||||
AddVertex(glm::vec2{to.x, from.y}, color, -1, glm::vec2{0, 0});
|
||||
AddVertex(from, color, -1, glm::vec2{0, 0});
|
||||
}
|
||||
|
||||
|
||||
@@ -36,10 +36,10 @@ namespace Copium
|
||||
{
|
||||
AllocateQuad();
|
||||
int texIndex = AllocateSampler(sampler);
|
||||
AddVertex(from, glm::vec3{1,1,1}, texIndex, texCoord1);
|
||||
AddVertex(glm::vec2{to.x, from.y}, glm::vec3{1, 1, 1}, texIndex, glm::vec2{texCoord2.x, texCoord1.y});
|
||||
AddVertex(to, glm::vec3{1,1,1}, texIndex, texCoord2);
|
||||
AddVertex(glm::vec2{from.x, to.y}, glm::vec3{1, 1, 1}, texIndex, glm::vec2{texCoord1.x, texCoord2.y});
|
||||
AddVertex(to, glm::vec3{1,1,1}, texIndex, texCoord2);
|
||||
AddVertex(glm::vec2{to.x, from.y}, glm::vec3{1, 1, 1}, texIndex, glm::vec2{texCoord2.x, texCoord1.y});
|
||||
AddVertex(from, glm::vec3{1,1,1}, texIndex, texCoord1);
|
||||
}
|
||||
|
||||
void Renderer::AddVertex(const glm::vec2& position, const glm::vec3& color, int texindex, const glm::vec2& texCoord)
|
||||
@@ -88,7 +88,6 @@ namespace Copium
|
||||
{
|
||||
PipelineCreator creator{renderPass, "res/shaders/renderer.vert", "res/shaders/renderer.frag"};
|
||||
creator.SetVertexDescriptor(RendererVertex::GetDescriptor());
|
||||
creator.AddDescriptorSetLayoutBinding(0, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, MAX_NUM_TEXTURES, VK_SHADER_STAGE_FRAGMENT_BIT);
|
||||
creator.SetDepthTest(false);
|
||||
graphicsPipeline = std::make_unique<Pipeline>(vulkan, creator);
|
||||
}
|
||||
@@ -107,7 +106,7 @@ namespace Copium
|
||||
Flush();
|
||||
NextDrawCall();
|
||||
}
|
||||
currentDrawCall->GetDescriptorSet().AddSampler(sampler, 0, vulkan.GetSwapChain().GetFlightIndex(), textureCount);
|
||||
currentDrawCall->GetDescriptorSet().SetSampler(sampler, 0, vulkan.GetSwapChain().GetFlightIndex(), textureCount);
|
||||
samplers[textureCount] = &sampler;
|
||||
textureCount++;
|
||||
return textureCount - 1;
|
||||
@@ -117,7 +116,6 @@ namespace Copium
|
||||
{
|
||||
if (quadCount + 1 > MAX_NUM_QUADS)
|
||||
{
|
||||
CP_INFO("Flush");
|
||||
Flush();
|
||||
NextDrawCall();
|
||||
}
|
||||
|
||||
@@ -16,6 +16,21 @@ namespace Copium
|
||||
return buffer;
|
||||
}
|
||||
|
||||
std::vector<uint32_t> FileSystem::ReadFile32(const std::string& filename)
|
||||
{
|
||||
std::ifstream file(filename, std::ios::ate | std::ios::binary);
|
||||
CP_ASSERT(file.is_open(), "ReadFile32 : Failed to open file");
|
||||
|
||||
size_t fileSize = (size_t)file.tellg();
|
||||
CP_ASSERT(fileSize % 4 == 0, "ReadFile32 : byte size is not divisible by 4");
|
||||
std::vector<uint32_t> buffer(fileSize / 4);
|
||||
|
||||
file.seekg(0);
|
||||
file.read((char*)buffer.data(), fileSize);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
std::string FileSystem::ReadFileStr(const std::string& filename)
|
||||
{
|
||||
std::ifstream file(filename, std::ios::ate | std::ios::binary);
|
||||
|
||||
@@ -14,6 +14,7 @@ namespace Copium
|
||||
CP_STATIC_CLASS(FileSystem);
|
||||
public:
|
||||
static std::vector<char> ReadFile(const std::string& filename);
|
||||
static std::vector<uint32_t> ReadFile32(const std::string& filename);
|
||||
static std::string ReadFileStr(const std::string& filename);
|
||||
static void WriteFile(const std::string& filename, const std::string& data);
|
||||
static void WriteFile(const std::string& filename, const char* data, size_t size);
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
#include "copium/util/ShaderReflector.h"
|
||||
|
||||
#include "copium/util/FileSystem.h"
|
||||
|
||||
#include <string_view>
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
|
||||
ShaderReflector::ShaderReflector(const std::string& vertexGlslFile, const std::string& fragmentGlslFile)
|
||||
{
|
||||
ParseGlslFile(vertexGlslFile, ShaderType::Vertex);
|
||||
ParseGlslFile(fragmentGlslFile, ShaderType::Fragment);
|
||||
}
|
||||
|
||||
void ShaderReflector::ParseGlslFile(const std::string& glslFile, ShaderType shaderType)
|
||||
{
|
||||
std::string str = FileSystem::ReadFileStr(glslFile);
|
||||
int index = 0;
|
||||
while (index < str.size())
|
||||
{
|
||||
ParseWhitespace(str, index);
|
||||
if (str[index] == '#')
|
||||
{
|
||||
ParseLine(str, index);
|
||||
continue;
|
||||
}
|
||||
if (std::string_view(&str[index], sizeof("layout") - 1) == "layout")
|
||||
{
|
||||
ParseLayout(str, index, shaderType);
|
||||
continue;
|
||||
}
|
||||
ParseLine(str, index);
|
||||
}
|
||||
}
|
||||
|
||||
void ShaderReflector::ParseLine(const std::string& str, int& index)
|
||||
{
|
||||
while(str[index] != '\n' && index < str.size()) index++;
|
||||
}
|
||||
|
||||
void ShaderReflector::ParseWhitespace(const std::string& str, int& index)
|
||||
{
|
||||
while ((str[index] == '\n' || str[index] == ' ' || str[index] == '\t' || str[index] == '\r') && index < str.size()) index++;
|
||||
}
|
||||
|
||||
void ShaderReflector::ParseLayout(const std::string& str, int& index, ShaderType shaderType)
|
||||
{
|
||||
// TODO: Make more robust, currently we might get a crash on the string if the glsl file is invalid
|
||||
index += sizeof("layout") - 1;
|
||||
ParseWhitespace(str, index);
|
||||
index++; // "("
|
||||
ParseWhitespace(str, index);
|
||||
if (std::string_view(&str[index], sizeof("set") - 1) != "set")
|
||||
{
|
||||
ParseLine(str, index);
|
||||
return;
|
||||
}
|
||||
ShaderBinding shaderBinding;
|
||||
shaderBinding.shaderType = shaderType;
|
||||
index += sizeof("set") - 1;
|
||||
ParseWhitespace(str, index);
|
||||
index++; // "="
|
||||
ParseWhitespace(str, index);
|
||||
char* end;
|
||||
shaderBinding.set = std::strtol(&str[index], &end, 10);
|
||||
index = end - str.c_str();
|
||||
ParseWhitespace(str, index);
|
||||
index++; // ","
|
||||
ParseWhitespace(str, index);
|
||||
index += sizeof("binding") - 1;
|
||||
ParseWhitespace(str, index);
|
||||
index++; // "="
|
||||
ParseWhitespace(str, index);
|
||||
shaderBinding.binding = std::strtol(&str[index], &end, 10);
|
||||
index = end - str.c_str();
|
||||
ParseWhitespace(str, index);
|
||||
index++; // ")
|
||||
ParseWhitespace(str, index);
|
||||
index += sizeof("uniform") - 1;
|
||||
ParseWhitespace(str, index);
|
||||
|
||||
std::string_view type = ParseWord(str, index);
|
||||
ParseWhitespace(str, index);
|
||||
if (str[index] == '{') ParseUniformBuffer(str, index);
|
||||
ParseWhitespace(str, index);
|
||||
std::string_view name = ParseWord(str, index);
|
||||
shaderBinding.name = name;
|
||||
ParseWhitespace(str, index);
|
||||
|
||||
if (str[index] == '[')
|
||||
{
|
||||
index++;
|
||||
shaderBinding.arraySize = std::strtol(&str[index], &end, 10);
|
||||
}
|
||||
else
|
||||
{
|
||||
shaderBinding.arraySize = 1;
|
||||
}
|
||||
|
||||
ParseLine(str, index);
|
||||
if (type == "sampler2D")
|
||||
shaderBinding.bindingType = BindingType::Sampler2D;
|
||||
else
|
||||
shaderBinding.bindingType = BindingType::UniformBuffer;
|
||||
CP_ASSERT(bindings.emplace(shaderBinding).second, "ParseLayout : multiple layouts with the same binding");
|
||||
}
|
||||
|
||||
std::string_view ShaderReflector::ParseWord(const std::string& str, int& index)
|
||||
{
|
||||
int start = index;
|
||||
while (((str[index] >= 'a' && str[index] < 'z') ||
|
||||
(str[index] >= 'A' && str[index] < 'Z') ||
|
||||
(str[index] >= '0' && str[index] <= '9')) ||
|
||||
str[index] == '_' &&
|
||||
index < str.size()) index++;
|
||||
return std::string_view(&str[start], index - start);
|
||||
}
|
||||
|
||||
void ShaderReflector::ParseUniformBuffer(const std::string& str, int& index)
|
||||
{
|
||||
while (str[index] != '}' && index < str.size()) index++;
|
||||
if (index < str.size()) index++; // go past "}"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <set>
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
enum class BindingType
|
||||
{
|
||||
Sampler2D, UniformBuffer
|
||||
};
|
||||
|
||||
enum class ShaderType
|
||||
{
|
||||
Vertex, Fragment
|
||||
};
|
||||
|
||||
struct ShaderBinding
|
||||
{
|
||||
std::string name;
|
||||
uint32_t set;
|
||||
uint32_t binding;
|
||||
uint32_t arraySize;
|
||||
BindingType bindingType;
|
||||
ShaderType shaderType;
|
||||
|
||||
bool operator<(const ShaderBinding& rhs) const
|
||||
{
|
||||
if (set != rhs.set)
|
||||
return set < rhs.set;
|
||||
if (binding != rhs.binding)
|
||||
return binding < rhs.binding;
|
||||
}
|
||||
};
|
||||
|
||||
class ShaderReflector
|
||||
{
|
||||
public:
|
||||
std::set<ShaderBinding> bindings;
|
||||
public:
|
||||
ShaderReflector(const std::string& vertexGlslFile, const std::string& fragmentGlslFile);
|
||||
|
||||
private:
|
||||
void ParseGlslFile(const std::string& glslFile, ShaderType type);
|
||||
void ParseLine(const std::string& str, int& index);
|
||||
void ParseWhitespace(const std::string& str, int& index);
|
||||
void ParseLayout(const std::string& str, int& index, ShaderType type);
|
||||
std::string_view ParseWord(const std::string& str, int& index);
|
||||
void ParseUniformBuffer(const std::string& str, int& index);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user