Skip to content
Snippets Groups Projects
Commit e238f0d9 authored by Alexander Gauggel's avatar Alexander Gauggel
Browse files

Merge 11-pipeline-state-object

parents 7c39b392 ab4cc615
No related branches found
No related tags found
4 merge requests!12Resolve "Swapchain Class",!7Resolve "Shader Program Class",!5Resolve "Pipeline State Object",!4Resolve "Renderpass Class"
...@@ -12,10 +12,13 @@ set(vkcv_sources ...@@ -12,10 +12,13 @@ set(vkcv_sources
${vkcv_include}/vkcv/Window.hpp ${vkcv_include}/vkcv/Window.hpp
${vkcv_source}/vkcv/Window.cpp ${vkcv_source}/vkcv/Window.cpp
${vkcv_include}/vkcv/SwapChain.hpp ${vkcv_include}/vkcv/SwapChain.hpp
${vkcv_source}/vkcv/SwapChain.cpp ${vkcv_source}/vkcv/SwapChain.cpp
${vkcv_include}/vkcv/ShaderProgram.hpp ${vkcv_include}/vkcv/ShaderProgram.hpp
${vkcv_source}/vkcv/ShaderProgram.cpp ${vkcv_source}/vkcv/ShaderProgram.cpp
${vkcv_include}/vkcv/Pipeline.hpp
${vkcv_source}/vkcv/Pipeline.cpp
) )
...@@ -9,13 +9,13 @@ ...@@ -9,13 +9,13 @@
#include "vkcv/SwapChain.hpp" #include "vkcv/SwapChain.hpp"
#include "vkcv/Window.hpp" #include "vkcv/Window.hpp"
#include "vkcv/Handles.hpp" #include "vkcv/Handles.hpp"
#include "vkcv/Pipeline.hpp"
namespace vkcv namespace vkcv
{ {
// TODO: // TODO:
class Buffer; class Buffer;
class Renderpass; class Renderpass;
class Pipeline;
class Core final class Core final
{ {
...@@ -35,6 +35,13 @@ namespace vkcv ...@@ -35,6 +35,13 @@ namespace vkcv
std::vector<vk::ImageView> m_swapchainImageViews; std::vector<vk::ImageView> m_swapchainImageViews;
const Window& m_window; const Window& m_window;
uint64_t m_NextPipelineId;
std::vector<vk::Pipeline> m_Pipelines;
std::vector<vk::PipelineLayout> m_PipelineLayouts;
uint64_t m_NextRenderpassId;
std::vector<vk::RenderPass> m_Renderpasses;
public: public:
/** /**
* Destructor of #Core destroys the Vulkan objects contained in the core's context. * Destructor of #Core destroys the Vulkan objects contained in the core's context.
...@@ -98,10 +105,19 @@ namespace vkcv ...@@ -98,10 +105,19 @@ namespace vkcv
std::vector<const char*> instanceExtensions = {}, std::vector<const char*> instanceExtensions = {},
std::vector<const char*> deviceExtensions = {}); std::vector<const char*> deviceExtensions = {});
/**
* Creates a basic vulkan graphics pipeline using @p pipeline from the pipeline class and returns it using the @p handle.
* Fixed Functions for pipeline are set with standard values.
*
* @param pipeline a pipeline object from the pipeline class
* @param handle a handle to return the created vulkan handle
* @return True if Pipeline creation was successfull, False if not
*/
bool createPipeline(const Pipeline &pipeline, PipelineHandle &handle);
// TODO: // TODO:
BufferHandle createBuffer(const Buffer &buf); BufferHandle createBuffer(const Buffer &buf);
PassHandle createRenderPass(const Renderpass &pass) ; PassHandle createRenderPass(const Renderpass &pass) ;
PipelineHandle createPipeline(const Pipeline &pipeline);
}; };
} }
/**
* @authors Mara Vogt, Mark Mints
* @file src/vkcv/Pipeline.hpp
* @brief Pipeline class to handle shader stages
*/
#ifndef VKCV_PIPELINE_HPP
#define VKCV_PIPELINE_HPP
#include <vector>
#include <cstdint>
#include "vkcv/Handles.hpp"
namespace vkcv {
class Pipeline {
public:
/**
* Default constructer is deleted!
*/
Pipeline() = delete;
/**
* Constructor for the pipeline. Creates a pipeline using @p vertexCode, @p fragmentCode as well as the
* dimensions of the application window @p width and @p height. A handle for the Render Pass is also needed, @p passHandle.
*
* @param vertexCode Spir-V of Vertex Shader
* @param fragCode Spir-V of Fragment Shader
* @param height height of the application window
* @param width width of the application window
* @param passHandle handle for Render Pass
*/
Pipeline(const std::vector<uint32_t> &vertexCode, const std::vector<uint32_t> &fragCode, uint32_t height, uint32_t width, PassHandle &passHandle);
std::vector<uint32_t> m_vertexCode;
std::vector<uint32_t> m_fragCode;
uint32_t m_height;
uint32_t m_width;
PassHandle m_passHandle;
};
}
#endif //VKCV_PIPELINE_HPP
...@@ -349,17 +349,170 @@ namespace vkcv ...@@ -349,17 +349,170 @@ namespace vkcv
m_Context(std::move(context)), m_Context(std::move(context)),
m_window(window), m_window(window),
m_swapchain(swapChain), m_swapchain(swapChain),
m_swapchainImageViews(imageViews) m_swapchainImageViews(imageViews),
m_NextPipelineId(0),
m_Pipelines{},
m_PipelineLayouts{},
m_NextRenderpassId(0),
m_Renderpasses{}
{} {}
Core::~Core() { Core::~Core() {
std::cout<< " Core " << std::endl; std::cout << " Core " << std::endl;
for (auto image : m_swapchainImageViews) {
m_Context.getDevice().destroyImageView(image);
}
m_Context.getDevice().destroySwapchainKHR(m_swapchain.getSwapchain());
m_Context.getInstance().destroySurfaceKHR(m_swapchain.getSurface());
}
bool Core::createPipeline(const Pipeline &pipeline, PipelineHandle &handle) {
// vertex shader stage
vk::ShaderModuleCreateInfo vertexModuleInfo({},pipeline.m_vertexCode.size(), pipeline.m_vertexCode.data());
vk::ShaderModule vertexModule{};
if(m_Context.m_Device.createShaderModule(&vertexModuleInfo, nullptr, &vertexModule) != vk::Result::eSuccess)
return false;
vk::PipelineShaderStageCreateInfo pipelineVertexShaderStageInfo(
{},
vk::ShaderStageFlagBits::eVertex,
vertexModule,
"main",
nullptr
);
// fragment shader stage
vk::ShaderModuleCreateInfo fragmentModuleInfo({},pipeline.m_fragCode.size(), pipeline.m_fragCode.data());
vk::ShaderModule fragmentModule{};
if(m_Context.m_Device.createShaderModule(&fragmentModuleInfo, nullptr, &fragmentModule) != vk::Result::eSuccess)
return false;
vk::PipelineShaderStageCreateInfo pipelineFragmentShaderStageInfo(
{},
vk::ShaderStageFlagBits::eFragment,
fragmentModule,
"main",
nullptr
);
// vertex input state
vk::VertexInputBindingDescription vertexInputBindingDescription(0,12,vk::VertexInputRate::eVertex);
vk::VertexInputAttributeDescription vertexInputAttributeDescription(0, 0, vk::Format::eR32G32B32Sfloat, 0);
vk::PipelineVertexInputStateCreateInfo pipelineVertexInputStateCreateInfo(
{},
1,
&vertexInputBindingDescription,
1,
&vertexInputAttributeDescription
);
// input assembly state
vk::PipelineInputAssemblyStateCreateInfo pipelineInputAssemblyStateCreateInfo(
{},
vk::PrimitiveTopology::eTriangleList,
false
);
// viewport state
vk::Viewport viewport(0.f, 0.f, static_cast<float>(pipeline.m_width), static_cast<float>(pipeline.m_height), 0.f, 1.f);
vk::Rect2D scissor({0,0},{pipeline.m_width, pipeline.m_height});
vk::PipelineViewportStateCreateInfo pipelineViewportStateCreateInfo({}, 1, &viewport, 1, &scissor);
// rasterization state
vk::PipelineRasterizationStateCreateInfo pipelineRasterizationStateCreateInfo(
{},
false,
false,
vk::PolygonMode::eFill,
vk::CullModeFlagBits::eNone,
vk::FrontFace::eCounterClockwise,
false,
0.f,
0.f,
0.f,
1.f
);
// multisample state
vk::PipelineMultisampleStateCreateInfo pipelineMultisampleStateCreateInfo(
{},
vk::SampleCountFlagBits::e1,
false,
0.f,
nullptr,
false,
false
);
// color blend state
vk::ColorComponentFlags colorWriteMask(VK_COLOR_COMPONENT_R_BIT |
VK_COLOR_COMPONENT_G_BIT |
VK_COLOR_COMPONENT_B_BIT |
VK_COLOR_COMPONENT_A_BIT);
vk::PipelineColorBlendAttachmentState colorBlendAttachmentState(
false,
vk::BlendFactor::eOne,
vk::BlendFactor::eOne,
vk::BlendOp::eAdd,
vk::BlendFactor::eOne,
vk::BlendFactor::eOne,
vk::BlendOp::eAdd,
colorWriteMask
);
vk::PipelineColorBlendStateCreateInfo pipelineColorBlendStateCreateInfo(
{},
false,
vk::LogicOp::eClear,
0,
&colorBlendAttachmentState,
{1.f,1.f,1.f,1.f}
);
for( auto image: m_swapchainImageViews ){ // pipeline layout
m_Context.getDevice().destroyImageView(image); vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo(
} {},
0,
{},
0,
{}
);
vk::PipelineLayout vkPipelineLayout{};
if(m_Context.m_Device.createPipelineLayout(&pipelineLayoutCreateInfo, nullptr, &vkPipelineLayout) != vk::Result::eSuccess)
return false;
// graphics pipeline create
std::vector<vk::PipelineShaderStageCreateInfo> shaderStages = {pipelineVertexShaderStageInfo, pipelineFragmentShaderStageInfo};
vk::GraphicsPipelineCreateInfo graphicsPipelineCreateInfo(
{},
static_cast<uint32_t>(shaderStages.size()),
shaderStages.data(),
&pipelineVertexInputStateCreateInfo,
&pipelineInputAssemblyStateCreateInfo,
nullptr,
&pipelineViewportStateCreateInfo,
&pipelineRasterizationStateCreateInfo,
&pipelineMultisampleStateCreateInfo,
nullptr,
&pipelineColorBlendStateCreateInfo,
nullptr,
vkPipelineLayout,
m_Renderpasses[pipeline.m_passHandle.id],
0,
{},
0
);
m_Context.getDevice().destroySwapchainKHR(m_swapchain.getSwapchain()); vk::Pipeline vkPipeline{};
m_Context.getInstance().destroySurfaceKHR( m_swapchain.getSurface() ); if(m_Context.m_Device.createGraphicsPipelines(nullptr, 1, &graphicsPipelineCreateInfo, nullptr, &vkPipeline) != vk::Result::eSuccess)
return false;
m_Pipelines.push_back(vkPipeline);
m_PipelineLayouts.push_back(vkPipelineLayout);
handle.id = m_NextPipelineId++;
return true;
} }
} }
/**
* @authors Mara Vogt, Mark Mints
* @file src/vkcv/Pipeline.cpp
* @brief Pipeline class to handle shader stages
*/
#include "vkcv/Pipeline.hpp"
namespace vkcv {
Pipeline::Pipeline(const std::vector<uint32_t> &vertexCode, const std::vector<uint32_t> &fragCode, uint32_t height, uint32_t width, PassHandle &passHandle):
m_vertexCode(vertexCode), m_fragCode(fragCode), m_height(height), m_width(width), m_passHandle(passHandle) {}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment