Commit bd3cc748 authored by Simeon Hermann's avatar Simeon Hermann
Browse files

[#10] transformed the code of the ShaderProgram class into Vulkan C++

parent 44638d6c
#include <iostream>
#include <vkcv/Context.hpp>
#include <vkcv/Window.hpp>
#include <vkcv/ShaderProgram.hpp>
int main(int argc, const char** argv) {
const char* applicationName = "First Triangle";
......@@ -33,8 +34,8 @@ int main(int argc, const char** argv) {
}
vkcv::ShaderProgram shaderProgram = vkcv::ShaderProgram::create(context);
shaderProgram.addShader(VK_SHADER_STAGE_VERTEX_BIT, "../../../../../shaders/vert.spv");
shaderProgram.addShader(VK_SHADER_STAGE_FRAGMENT_BIT, "../../../../../shaders/frag.spv");
shaderProgram.addShader(vk::ShaderStageFlagBits::eVertex , "../../../../../shaders/vert.spv");
shaderProgram.addShader(vk::ShaderStageFlagBits::eFragment, "../../../../../shaders/frag.spv");
while (window.isWindowOpen()) {
window.pollEvents();
......
/**
* @authors Simeon Hermann
* @authors Simeon Hermann, Leonie Franken
* @file src/vkcv/ShaderProgram.cpp
* @brief ShaderProgram class to handle and prepare the shader stages for a graphics pipeline
*/
#include "ShaderProgram.hpp"
#include <fstream>
#include <iostream>
std::vector<const char*> validationLayers = {
"VK_LAYER_KHRONOS_validation"
......@@ -33,22 +29,17 @@ namespace vkcv {
return buffer;
}
VkShaderModule ShaderProgram::createShaderModule(const std::vector<char>& shaderCode) {
VkShaderModuleCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
createInfo.codeSize = shaderCode.size();
createInfo.pCode = reinterpret_cast<const uint32_t*>(shaderCode.data());
VkShaderModule shaderModule;
if (vkCreateShaderModule(m_context.getDevice(), &createInfo, nullptr, &shaderModule) != VK_SUCCESS) {
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;
}
VkPipelineShaderStageCreateInfo ShaderProgram::createShaderStage(VkShaderModule& shaderModule, VkShaderStageFlagBits shaderStage) {
VkPipelineShaderStageCreateInfo shaderStageInfo{};
shaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
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";
......@@ -62,20 +53,20 @@ namespace vkcv {
return ShaderProgram(context);
}
void ShaderProgram::addShader(VkShaderStageFlagBits shaderStage, const std::string& filepath) {
void ShaderProgram::addShader(vk::ShaderStageFlagBits 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);
VkShaderModule shaderModule = createShaderModule(shaderCode);
VkPipelineShaderStageCreateInfo shaderInfo = createShaderStage(shaderModule, shaderStage);
vk::ShaderModule shaderModule = createShaderModule(shaderCode);
vk::PipelineShaderStageCreateInfo shaderInfo = createShaderStage(shaderModule, shaderStage);
m_shaderStagesList.push_back(shaderInfo);
vkDestroyShaderModule(m_context.getDevice(), shaderModule, nullptr);
m_context.getDevice().destroyShaderModule(shaderModule, nullptr);
}
}
bool ShaderProgram::containsShaderStage(VkShaderStageFlagBits shaderStage) {
bool ShaderProgram::containsShaderStage(vk::ShaderStageFlagBits shaderStage) {
for (int i = 0; i < m_shaderStagesList.size(); i++) {
if (m_shaderStagesList[i].stage == shaderStage) {
return true;
......@@ -84,7 +75,7 @@ namespace vkcv {
return false;
}
bool ShaderProgram::deleteShaderStage(VkShaderStageFlagBits shaderStage) {
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);
......@@ -94,7 +85,7 @@ namespace vkcv {
return false;
}
std::vector<VkPipelineShaderStageCreateInfo> ShaderProgram::getShaderStages() {
std::vector<vk::PipelineShaderStageCreateInfo> ShaderProgram::getShaderStages() {
return m_shaderStagesList;
}
......
#pragma once
/**
* @authors Simeon Hermann
* @authors Simeon Hermann, Leonie Franken
* @file src/vkcv/ShaderProgram.hpp
* @brief ShaderProgram class to handle and prepare the shader stages for a graphics pipeline
*/
#define GLFW_INCLUDE_VULKAN
#include <vector>
#include <map>
#include <fstream>
#include <iostream>
#include <vulkan/vulkan.hpp>
#include "Context.hpp"
......@@ -17,7 +18,7 @@ namespace vkcv {
private:
vkcv::Context& m_context;
std::vector<VkPipelineShaderStageCreateInfo> m_shaderStagesList;
std::vector<vk::PipelineShaderStageCreateInfo> m_shaderStagesList;
/**
* Constructor of ShaderProgram requires a context for the logical device.
* @param context of the app
......@@ -39,7 +40,7 @@ namespace vkcv {
* @param[in] a vector of chars as a buffer for the code
* @return shader module
*/
VkShaderModule createShaderModule(const std::vector<char>& shaderCode);
vk::ShaderModule createShaderModule(const std::vector<char>& shaderCode);
/**
* Creates a shader stage (info struct) for the to be added shader.
......@@ -48,7 +49,7 @@ namespace vkcv {
* @param[in] flag that signals the respective shaderStage
* @return pipeline shader stage info struct
*/
VkPipelineShaderStageCreateInfo createShaderStage(VkShaderModule& shaderModule, VkShaderStageFlagBits shaderStage);
vk::PipelineShaderStageCreateInfo createShaderStage(vk::ShaderModule& shaderModule, vk::ShaderStageFlagBits shaderStage);
public:
......@@ -72,28 +73,28 @@ 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(VkShaderStageFlagBits shaderStage, const std::string& filepath);
void addShader(vk::ShaderStageFlagBits 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(VkShaderStageFlagBits shaderStage);
bool containsShaderStage(vk::ShaderStageFlagBits 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(VkShaderStageFlagBits shaderStage);
bool deleteShaderStage(vk::ShaderStageFlagBits 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<VkPipelineShaderStageCreateInfo> getShaderStages();
std::vector<vk::PipelineShaderStageCreateInfo> getShaderStages();
/**
* Returns the number of shader stages in the shader program.
......
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