Skip to content
Snippets Groups Projects
Verified Commit 9ae9cba2 authored by Tobias Frisch's avatar Tobias Frisch
Browse files

Changed spirv shader to be stored as uint32_t vector instead of char vector

parent c67838e3
No related branches found
No related tags found
1 merge request!97Resolve "Dokumentation vervollständigen"
...@@ -48,7 +48,7 @@ namespace vkcv { ...@@ -48,7 +48,7 @@ namespace vkcv {
* @param[in] stage The stage of the shader * @param[in] stage The stage of the shader
* @return Shader code binary of the given stage * @return Shader code binary of the given stage
*/ */
const std::vector<char> &getShaderBinary(ShaderStage stage) const; const std::vector<uint32_t> &getShaderBinary(ShaderStage stage) const;
/** /**
* @brief Returns whether a shader exists in the program for a * @brief Returns whether a shader exists in the program for a
...@@ -91,7 +91,7 @@ namespace vkcv { ...@@ -91,7 +91,7 @@ namespace vkcv {
*/ */
void reflectShader(ShaderStage shaderStage); void reflectShader(ShaderStage shaderStage);
std::unordered_map<ShaderStage, std::vector<char> > m_Shaders; std::unordered_map<ShaderStage, std::vector<uint32_t> > m_Shaders;
// contains all vertex input attachments used in the vertex buffer // contains all vertex input attachments used in the vertex buffer
std::vector<VertexAttachment> m_VertexAttachments; std::vector<VertexAttachment> m_VertexAttachments;
......
...@@ -147,23 +147,23 @@ namespace vkcv::rtx { ...@@ -147,23 +147,23 @@ namespace vkcv::rtx {
void RTXModule::createRTXPipelineAndLayout(uint32_t pushConstantSize, std::vector<DescriptorSetLayoutHandle> descriptorSetLayouts, ShaderProgram &rtxShader) { void RTXModule::createRTXPipelineAndLayout(uint32_t pushConstantSize, std::vector<DescriptorSetLayoutHandle> descriptorSetLayouts, ShaderProgram &rtxShader) {
// -- process vkcv::ShaderProgram into vk::ShaderModule // -- process vkcv::ShaderProgram into vk::ShaderModule
std::vector<char> rayGenShaderCode = rtxShader.getShaderBinary(ShaderStage::RAY_GEN); std::vector<uint32_t> rayGenShaderCode = rtxShader.getShaderBinary(ShaderStage::RAY_GEN);
vk::ShaderModuleCreateInfo rayGenShaderModuleInfo( vk::ShaderModuleCreateInfo rayGenShaderModuleInfo(
vk::ShaderModuleCreateFlags(), // vk::ShaderModuleCreateFlags flags_, vk::ShaderModuleCreateFlags(), // vk::ShaderModuleCreateFlags flags_,
rayGenShaderCode.size(), // size_t codeSize rayGenShaderCode.size() * sizeof(uint32_t), // size_t codeSize
(const uint32_t*)rayGenShaderCode.data() // const uint32_t* pCode rayGenShaderCode.data() // const uint32_t* pCode
); );
vk::ShaderModule rayGenShaderModule = m_core->getContext().getDevice().createShaderModule(rayGenShaderModuleInfo); vk::ShaderModule rayGenShaderModule = m_core->getContext().getDevice().createShaderModule(rayGenShaderModuleInfo);
if (!rayGenShaderModule) { if (!rayGenShaderModule) {
vkcv_log(LogLevel::ERROR, "The Ray Generation Shader Module could not be created!"); vkcv_log(LogLevel::ERROR, "The Ray Generation Shader Module could not be created!");
} }
std::vector<char> rayMissShaderCode = rtxShader.getShaderBinary(ShaderStage::RAY_MISS); std::vector<uint32_t> rayMissShaderCode = rtxShader.getShaderBinary(ShaderStage::RAY_MISS);
vk::ShaderModuleCreateInfo rayMissShaderModuleInfo( vk::ShaderModuleCreateInfo rayMissShaderModuleInfo(
vk::ShaderModuleCreateFlags(), // vk::ShaderModuleCreateFlags flags_, vk::ShaderModuleCreateFlags(), // vk::ShaderModuleCreateFlags flags_,
rayMissShaderCode.size(), //size_t codeSize rayMissShaderCode.size() * sizeof(uint32_t), //size_t codeSize
(const uint32_t*)rayMissShaderCode.data() // const uint32_t* pCode rayMissShaderCode.data() // const uint32_t* pCode
); );
vk::ShaderModule rayMissShaderModule = m_core->getContext().getDevice().createShaderModule(rayMissShaderModuleInfo); vk::ShaderModule rayMissShaderModule = m_core->getContext().getDevice().createShaderModule(rayMissShaderModuleInfo);
...@@ -171,11 +171,11 @@ namespace vkcv::rtx { ...@@ -171,11 +171,11 @@ namespace vkcv::rtx {
vkcv_log(LogLevel::ERROR, "The Ray Miss Shader Module could not be created!"); vkcv_log(LogLevel::ERROR, "The Ray Miss Shader Module could not be created!");
} }
std::vector<char> rayClosestHitShaderCode = rtxShader.getShaderBinary(ShaderStage::RAY_CLOSEST_HIT); std::vector<uint32_t> rayClosestHitShaderCode = rtxShader.getShaderBinary(ShaderStage::RAY_CLOSEST_HIT);
vk::ShaderModuleCreateInfo rayClosestHitShaderModuleInfo( vk::ShaderModuleCreateInfo rayClosestHitShaderModuleInfo(
vk::ShaderModuleCreateFlags(), // vk::ShaderModuleCreateFlags flags_, vk::ShaderModuleCreateFlags(), // vk::ShaderModuleCreateFlags flags_,
rayClosestHitShaderCode.size(), //size_t codeSize rayClosestHitShaderCode.size() * sizeof(uint32_t), //size_t codeSize
(const uint32_t*)rayClosestHitShaderCode.data() // const uint32_t* pCode_ rayClosestHitShaderCode.data() // const uint32_t* pCode_
); );
vk::ShaderModule rayClosestHitShaderModule = m_core->getContext().getDevice().createShaderModule(rayClosestHitShaderModuleInfo); vk::ShaderModule rayClosestHitShaderModule = m_core->getContext().getDevice().createShaderModule(rayClosestHitShaderModuleInfo);
if (!rayClosestHitShaderModule) { if (!rayClosestHitShaderModule) {
......
...@@ -112,8 +112,8 @@ namespace vkcv ...@@ -112,8 +112,8 @@ namespace vkcv
vk::Result ComputePipelineManager::createShaderModule(vk::ShaderModule &module, const ShaderProgram &shaderProgram, const ShaderStage stage) vk::Result ComputePipelineManager::createShaderModule(vk::ShaderModule &module, const ShaderProgram &shaderProgram, const ShaderStage stage)
{ {
std::vector<char> code = shaderProgram.getShaderBinary(stage); std::vector<uint32_t> code = shaderProgram.getShaderBinary(stage);
vk::ShaderModuleCreateInfo moduleInfo({}, code.size(), reinterpret_cast<uint32_t*>(code.data())); vk::ShaderModuleCreateInfo moduleInfo({}, code.size() * sizeof(uint32_t), code.data());
return m_Device.createShaderModule(&moduleInfo, nullptr, &module); return m_Device.createShaderModule(&moduleInfo, nullptr, &module);
} }
} }
\ No newline at end of file
...@@ -110,8 +110,8 @@ namespace vkcv ...@@ -110,8 +110,8 @@ namespace vkcv
vk::PipelineShaderStageCreateInfo* outCreateInfo) { vk::PipelineShaderStageCreateInfo* outCreateInfo) {
assert(outCreateInfo); assert(outCreateInfo);
std::vector<char> code = shaderProgram.getShaderBinary(stage); std::vector<uint32_t> code = shaderProgram.getShaderBinary(stage);
vk::ShaderModuleCreateInfo vertexModuleInfo({}, code.size(), reinterpret_cast<uint32_t*>(code.data())); vk::ShaderModuleCreateInfo vertexModuleInfo({}, code.size() * sizeof(uint32_t), code.data());
vk::ShaderModule shaderModule; vk::ShaderModule shaderModule;
if (device.createShaderModule(&vertexModuleInfo, nullptr, &shaderModule) != vk::Result::eSuccess) if (device.createShaderModule(&vertexModuleInfo, nullptr, &shaderModule) != vk::Result::eSuccess)
return false; return false;
......
...@@ -14,19 +14,25 @@ namespace vkcv { ...@@ -14,19 +14,25 @@ namespace vkcv {
* @param[in] relative path to the shader code * @param[in] relative path to the shader code
* @return vector of chars as a buffer for the code * @return vector of chars as a buffer for the code
*/ */
std::vector<char> readShaderCode(const std::filesystem::path &shaderPath) { std::vector<uint32_t> readShaderCode(const std::filesystem::path &shaderPath) {
std::ifstream file (shaderPath.string(), std::ios::ate | std::ios::binary); std::ifstream file (shaderPath.string(), std::ios::ate | std::ios::binary);
if (!file.is_open()) { if (!file.is_open()) {
vkcv_log(LogLevel::ERROR, "The file could not be opened"); vkcv_log(LogLevel::ERROR, "The file could not be opened: %s", shaderPath.c_str());
return std::vector<char>{}; return std::vector<uint32_t>();
} }
size_t fileSize = (size_t)file.tellg(); size_t fileSize = (size_t)file.tellg();
std::vector<char> buffer(fileSize);
if (fileSize % sizeof(uint32_t) != 0) {
vkcv_log(LogLevel::ERROR, "The file is not a valid shader: %s", shaderPath.c_str());
return std::vector<uint32_t>();
}
std::vector<uint32_t> buffer(fileSize / sizeof(uint32_t));
file.seekg(0); file.seekg(0);
file.read(buffer.data(), fileSize); file.read(reinterpret_cast<char*>(buffer.data()), fileSize);
file.close(); file.close();
return buffer; return buffer;
...@@ -82,7 +88,7 @@ namespace vkcv { ...@@ -82,7 +88,7 @@ namespace vkcv {
vkcv_log(LogLevel::WARNING, "Overwriting existing shader stage"); vkcv_log(LogLevel::WARNING, "Overwriting existing shader stage");
} }
const std::vector<char> shaderCode = readShaderCode(path); const std::vector<uint32_t> shaderCode = readShaderCode(path);
if (shaderCode.empty()) { if (shaderCode.empty()) {
return false; return false;
...@@ -93,7 +99,7 @@ namespace vkcv { ...@@ -93,7 +99,7 @@ namespace vkcv {
} }
} }
const std::vector<char> &ShaderProgram::getShaderBinary(ShaderStage stage) const const std::vector<uint32_t> &ShaderProgram::getShaderBinary(ShaderStage stage) const
{ {
return m_Shaders.at(stage); return m_Shaders.at(stage);
} }
...@@ -108,13 +114,9 @@ namespace vkcv { ...@@ -108,13 +114,9 @@ namespace vkcv {
void ShaderProgram::reflectShader(ShaderStage shaderStage) void ShaderProgram::reflectShader(ShaderStage shaderStage)
{ {
auto shaderCodeChar = m_Shaders.at(shaderStage); auto shaderCode = m_Shaders.at(shaderStage);
std::vector<uint32_t> shaderCode;
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(shaderCode);
spirv_cross::ShaderResources resources = comp.get_shader_resources(); spirv_cross::ShaderResources resources = comp.get_shader_resources();
//reflect vertex input //reflect vertex input
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment