Skip to content
Snippets Groups Projects
Commit aad45984 authored by Leonie Franken's avatar Leonie Franken
Browse files

[#36] vertex layout reflection done

parent 2d2a35ed
No related branches found
No related tags found
1 merge request!28Resolve "Shader Program Reflection"
Pipeline #25172 passed
......@@ -51,4 +51,7 @@ set(vkcv_sources
${vkcv_source}/vkcv/Framebuffer.hpp
${vkcv_source}/vkcv/Framebuffer.cpp
${vkcv_include}/vkcv/VertexLayout.hpp
${vkcv_source}/vkcv/VertexLayout.cpp
)
......@@ -11,6 +11,7 @@
#include <filesystem>
#include <vulkan/vulkan.hpp>
#include <spirv_cross.hpp>
#include "vkcv/VertexLayout.hpp"
namespace vkcv {
......@@ -54,10 +55,13 @@ namespace vkcv {
bool existsShader(ShaderStage shaderStage) const;
void reflectShader(ShaderStage shaderStage) const;
void reflectShader(ShaderStage shaderStage);
VertexLayout& getVertexLayout();
private:
std::unordered_map<ShaderStage, Shader> m_Shaders;
VertexLayout m_VertexLayout;
};
}
#pragma once
#include <unordered_map>
#include <vector>
#include <iostream>
namespace vkcv{
enum class VertexFormat{
FLOAT,
FLOAT2,
FLOAT3,
FLOAT4,
INT,
INT2,
INT3,
INT4
};
struct VertexInputAttachment{
VertexInputAttachment() = delete;
VertexInputAttachment(uint32_t location, uint32_t binding, VertexFormat format, uint32_t offset) noexcept;
uint32_t location;
uint32_t binding;
VertexFormat format;
uint32_t offset;
};
struct VertexLayout{
VertexLayout() noexcept;
VertexLayout(const std::vector<VertexInputAttachment> &inputs) noexcept;
std::unordered_map<uint32_t, VertexInputAttachment> attachmentMap;
uint32_t stride;
};
}
\ No newline at end of file
......@@ -27,8 +27,46 @@ namespace vkcv {
return buffer;
}
VertexFormat convertFormat(spirv_cross::SPIRType::BaseType basetype, uint32_t vecsize){
switch (basetype) {
case spirv_cross::SPIRType::Int:
switch (vecsize) {
case 1:
return VertexFormat::INT;
case 2:
return VertexFormat::INT2;
case 3:
return VertexFormat::INT3;
case 4:
return VertexFormat::INT4;
default:
break;
}
break;
case spirv_cross::SPIRType::Float:
switch (vecsize) {
case 1:
return VertexFormat::FLOAT;
case 2:
return VertexFormat::FLOAT2;
case 3:
return VertexFormat::FLOAT3;
case 4:
return VertexFormat::FLOAT4;
default:
break;
}
break;
default:
break;
}
std::cout << "Shader Program Reflection: unknown Vertex Format" << std::endl;
return VertexFormat::FLOAT;
}
ShaderProgram::ShaderProgram() noexcept :
m_Shaders{}
m_Shaders{},
m_VertexLayout{}
{}
bool ShaderProgram::addShader(ShaderStage shaderStage, const std::filesystem::path &shaderPath)
......@@ -60,27 +98,37 @@ namespace vkcv {
return true;
}
void ShaderProgram::reflectShader(ShaderStage shaderStage) const
void ShaderProgram::reflectShader(ShaderStage shaderStage)
{
auto shaderCodeChar = m_Shaders.at(shaderStage).shaderCode;
std::vector<uint32_t> shaderCode; //convert from char to uint 32. Is this the best way? Prob not.
std::vector<uint32_t> shaderCode;
for (uint32_t i = 0; i < shaderCodeChar.size(); i++) {
shaderCode.push_back((uint32_t) shaderCodeChar[i]);
for (uint32_t i = 0; i < shaderCodeChar.size()/4; i++) {
shaderCode.push_back(((uint32_t*) shaderCodeChar.data())[i]);
}
//spirv_cross::Compiler comp(move(shaderCode));
/*
spirv_cross::Compiler comp(move(shaderCode));
spirv_cross::ShaderResources resources = comp.get_shader_resources();
//testprint
for (auto &u : resources.uniform_buffers)
{
uint32_t set = comp.get_decoration(u.id, spv::DecorationDescriptorSet);
uint32_t binding = comp.get_decoration(u.id, spv::DecorationBinding);
std::cout << 'Found UBO ' << &u << ' at set = ' << set << ', binding = ' << binding << std::endl;
std::vector<VertexInputAttachment> inputVec;
uint32_t offset = 0;
for (uint32_t i = 0; i < resources.stage_inputs.size() ; i++){
auto &u = resources.stage_inputs[i];
const spirv_cross::SPIRType &base_type = comp.get_type(u.base_type_id);
VertexInputAttachment input = VertexInputAttachment(comp.get_decoration(u.id,spv::DecorationLocation),
0,
convertFormat(base_type.basetype, base_type.vecsize),
offset);
inputVec.push_back(input);
offset += base_type.vecsize * base_type.width/8;
}
*/
m_VertexLayout = VertexLayout(inputVec);
}
VertexLayout& ShaderProgram::getVertexLayout(){
return m_VertexLayout;
}
}
//
// Created by Charlotte on 28.05.2021.
//
#include "vkcv/VertexLayout.hpp"
namespace vkcv {
uint32_t static getFormatSize(VertexFormat format) {
switch (format) {
case VertexFormat::FLOAT:
return 4;
case VertexFormat::FLOAT2:
return 8;
case VertexFormat::FLOAT3:
return 12;
case VertexFormat::FLOAT4:
return 16;
case VertexFormat::INT:
return 4;
case VertexFormat::INT2:
return 8;
case VertexFormat::INT3:
return 12;
case VertexFormat::INT4:
return 16;
default:
break;
}
std::cout << "VertexLayout: No format given" << std::endl;
return 0;
}
VertexInputAttachment::VertexInputAttachment(uint32_t location, uint32_t binding, VertexFormat format, uint32_t offset) noexcept:
location{location},
binding{binding},
format{format},
offset{offset}
{}
VertexLayout::VertexLayout() noexcept :
stride{0},
attachmentMap()
{}
VertexLayout::VertexLayout(const std::vector<VertexInputAttachment> &inputs) noexcept {
stride = 0;
for (const auto &input : inputs) {
attachmentMap.insert(std::make_pair(input.location, input));
stride += getFormatSize(input.format);
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment