Commit dba34639 authored by Leonie Charlotte Franken's avatar Leonie Charlotte Franken
Browse files

restructured Shader Program to fit Core structure. Resolved missed merge conflict in main.cpp

parent 1a7ca10c
Pipeline #24724 failed with stage
in 17 seconds
......@@ -10,14 +10,17 @@
#include <fstream>
#include <iostream>
#include <vulkan/vulkan.hpp>
#include "Context.hpp"
namespace vkcv {
class ShaderProgram final {
public:
enum ShaderStage {VERTEX, FRAGMENT, COMPUTE};
enum class ShaderStage {
VERTEX,
FRAGMENT,
COMPUTE
};
/**
......@@ -39,28 +42,35 @@ namespace vkcv {
* @param[in] flag that signals the respective shaderStage (e.g. VK_SHADER_STAGE_VERTEX_BIT)
* @param[in] relative path to the shader code (e.g. "../../../../../shaders/vert.spv")
*/
void addShader(vk::ShaderStageFlagBits shaderStage, const std::string& filepath);
void addShader(ShaderProgram::ShaderStage shaderStage, const std::string& filepath);
/**
* Tests if the shader program contains a certain shader stage.
* @param[in] flag that signals the respective shader stage (e.g. VK_SHADER_STAGE_VERTEX_BIT)
* @return boolean that is true if the shader program contains the shader stage
*/
bool containsShaderStage(vk::ShaderStageFlagBits shaderStage);
bool containsShaderStage(ShaderProgram::ShaderStage shaderStage);
/**
* Deletes the given shader stage in the shader program.
* @param[in] flag that signals the respective shader stage (e.g. VK_SHADER_STAGE_VERTEX_BIT)
* @return boolean that is false if the shader stage was not found in the shader program
*/
bool deleteShaderStage(vk::ShaderStageFlagBits shaderStage);
bool deleteShaderStage(ShaderProgram::ShaderStage shaderStage);
/**
* Returns a list with all the shader stages in the shader program.
* Needed for the transfer to the pipeline.
* @return vector list with all shader stage info structs
*/
std::vector<vk::PipelineShaderStageCreateInfo> getShaderStages();
std::vector<vk::ShaderStageFlagBits> getShaderStages();
/**
* Returns a list with all the shader code in the shader program.
* Needed for the transfer to the pipeline.
* @return vector list with all shader code char vecs
*/
std::vector<std::vector<char>> getShaderCode();
/**
* Returns the number of shader stages in the shader program.
......@@ -78,16 +88,13 @@ namespace vkcv {
std::vector<vk::ShaderStageFlagBits> shaderStageFlag;
};
vkcv::Context& m_context;
std::vector<vk::PipelineShaderStageCreateInfo> m_shaderStagesList;
ShaderStages m_shaderStages;
/**
* Constructor of ShaderProgram requires a context for the logical device.
* @param context of the app
*/
ShaderProgram(vkcv::Context& context);
ShaderProgram();
/**
* Reads the file of a given shader code.
......@@ -97,6 +104,13 @@ namespace vkcv {
*/
std::vector<char> readFile(const std::string& filepath);
/**
* Converts ShaderStage Enum into vk::ShaderStageFlagBits
* @param[in] ShaderStage enum
* @return vk::ShaderStageFlagBits
*/
vk::ShaderStageFlagBits convertToShaderStageFlagBits(ShaderProgram::ShaderStage shaderStage);
/**
* Creates a shader module that encapsulates the read shader code.
* Only used within the class.
......@@ -104,7 +118,7 @@ namespace vkcv {
* @param[in] a vector of chars as a buffer for the code
* @return shader module
*/
vk::ShaderModule createShaderModule(const std::vector<char>& shaderCode);
//vk::ShaderModule createShaderModule(const std::vector<char>& shaderCode); -> Core
/**
* Creates a shader stage (info struct) for the to be added shader.
......@@ -113,11 +127,7 @@ namespace vkcv {
* @param[in] flag that signals the respective shaderStage
* @return pipeline shader stage info struct
*/
vk::PipelineShaderStageCreateInfo createShaderStage(vk::ShaderModule& shaderModule, vk::ShaderStageFlagBits shaderStage);
//vk::PipelineShaderStageCreateInfo createShaderStage(vk::ShaderModule& shaderModule, vk::ShaderStageFlagBits shaderStage); -> Core
};
}
......@@ -34,13 +34,6 @@ int main(int argc, const char** argv) {
default: std::cout << "Unknown GPU vendor?! Either you're on an exotic system or your driver is broken..." << std::endl;
}
<<<<<<< HEAD
vkcv::ShaderProgram shaderProgram = vkcv::ShaderProgram::create(context);
shaderProgram.addShader(vk::ShaderStageFlagBits::eVertex , "../../../../../shaders/vert.spv");
shaderProgram.addShader(vk::ShaderStageFlagBits::eFragment, "../../../../../shaders/frag.spv");
while (window.isWindowOpen()) {
=======
/*
* BufferHandle triangleVertices = core.createBuffer(vertices);
* BufferHandle triangleIndices = core.createBuffer(indices);
......@@ -65,7 +58,6 @@ int main(int argc, const char** argv) {
// TBD: synchronization
>>>>>>> 74ff8e7041d7958006a434cf4d3a7fee0a3fa724
window.pollEvents();
}
return 0;
......
......@@ -4,7 +4,7 @@
* @brief ShaderProgram class to handle and prepare the shader stages for a graphics pipeline
*/
#include "ShaderProgram.hpp"
#include "vkcv/ShaderProgram.hpp"
std::vector<const char*> validationLayers = {
"VK_LAYER_KHRONOS_validation"
......@@ -31,22 +31,34 @@ namespace vkcv {
return buffer;
}
vk::ShaderModule ShaderProgram::createShaderModule(const std::vector<char>& shaderCode) {
vk::ShaderStageFlagBits ShaderProgram::convertToShaderStageFlagBits(ShaderProgram::ShaderStage shaderStage){
switch (shaderStage) {
case ShaderStage::VERTEX:
return vk::ShaderStageFlagBits::eVertex;
case ShaderStage::FRAGMENT:
return vk::ShaderStageFlagBits::eFragment;
case ShaderStage::COMPUTE:
return vk::ShaderStageFlagBits::eCompute;
}
throw std::runtime_error("Shader Type not yet implemented.");
}
/*vk::ShaderModule ShaderProgram::createShaderModule(const std::vector<char>& shaderCode) {
vk::ShaderModuleCreateInfo createInfo({}, shaderCode.size(), reinterpret_cast<const uint32_t*>(shaderCode.data()));
vk::ShaderModule shaderModule;
if ((m_context.getDevice().createShaderModule(&createInfo, nullptr, &shaderModule)) != vk::Result::eSuccess) {
throw std::runtime_error("Failed to create shader module!");
}
return shaderModule;
}
}*/
vk::PipelineShaderStageCreateInfo ShaderProgram::createShaderStage(vk::ShaderModule& shaderModule, vk::ShaderStageFlagBits shaderStage) {
/*vk::PipelineShaderStageCreateInfo ShaderProgram::createShaderStage(vk::ShaderModule& shaderModule, vk::ShaderStageFlagBits shaderStage) {
vk::PipelineShaderStageCreateInfo shaderStageInfo({}, shaderStage, shaderModule, "main", {});
shaderStageInfo.stage = shaderStage;
shaderStageInfo.module = shaderModule;
shaderStageInfo.pName = "main";
return shaderStageInfo;
}
}*/
ShaderProgram::~ShaderProgram() {
}
......@@ -55,47 +67,53 @@ namespace vkcv {
return ShaderProgram();
}
//TODO: Enum übergeben statt ShaderStageFlagBits
void ShaderProgram::addShader(vk::ShaderStageFlagBits shaderStage, const std::string& filepath) {
void ShaderProgram::addShader(ShaderProgram::ShaderStage shaderStage, const std::string& filepath) {
if (containsShaderStage(shaderStage)) {
throw std::runtime_error("Shader program already contains this particular shader stage.");
}
else {
auto shaderCode = readFile(filepath);
vk::ShaderStageFlagBits convertedShaderStage = convertToShaderStageFlagBits(shaderStage);
//vk::ShaderModule shaderModule = createShaderModule(shaderCode);
//vk::PipelineShaderStageCreateInfo shaderInfo = createShaderStage(shaderModule, shaderStage);
//m_shaderStagesList.push_back(shaderInfo);
//m_context.getDevice().destroyShaderModule(shaderModule, nullptr);
m_shaderStages.shaderCode.push_back(shaderCode);
m_shaderStages.shaderStageFlag.push_back(shaderStage);
m_shaderStages.shaderStageFlag.push_back(convertedShaderStage);
}
}
bool ShaderProgram::containsShaderStage(vk::ShaderStageFlagBits shaderStage) {
for (int i = 0; i < m_shaderStagesList.size(); i++) {
if (m_shaderStagesList[i].stage == shaderStage) {
bool ShaderProgram::containsShaderStage(ShaderProgram::ShaderStage shaderStage) {
vk::ShaderStageFlagBits convertedShaderStage = convertToShaderStageFlagBits(shaderStage);
for (int i = 0; i < m_shaderStages.shaderStageFlag.size(); i++) {
if (m_shaderStages.shaderStageFlag[i] == convertedShaderStage) {
return true;
}
}
return false;
}
bool ShaderProgram::deleteShaderStage(vk::ShaderStageFlagBits shaderStage) {
for (int i = 0; i < m_shaderStagesList.size() - 1; i++) {
if (m_shaderStagesList[i].stage == shaderStage) {
m_shaderStagesList.erase(m_shaderStagesList.begin() + i);
bool ShaderProgram::deleteShaderStage(ShaderProgram::ShaderStage shaderStage) {
vk::ShaderStageFlagBits convertedShaderStage = convertToShaderStageFlagBits(shaderStage);
for (int i = 0; i < m_shaderStages.shaderStageFlag.size() - 1; i++) {
if (m_shaderStages.shaderStageFlag[i] == convertedShaderStage) {
m_shaderStages.shaderStageFlag.erase(m_shaderStages.shaderStageFlag.begin() + i);
m_shaderStages.shaderCode.erase(m_shaderStages.shaderCode.begin() + i);
return true;
}
}
return false;
}
std::vector<vk::PipelineShaderStageCreateInfo> ShaderProgram::getShaderStages() {
return m_shaderStagesList;
std::vector<vk::ShaderStageFlagBits> ShaderProgram::getShaderStages() {
return m_shaderStages.shaderStageFlag;
}
std::vector<std::vector<char>> ShaderProgram::getShaderCode() {
return m_shaderStages.shaderCode;
}
int ShaderProgram::getShaderStagesCount() {
return m_shaderStagesList.size();
return m_shaderStages.shaderStageFlag.size();
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment