Files
OpenVulkano/openVulkanoCpp/Vulkan/Scene/VulkanShader.cpp

118 lines
4.9 KiB
C++

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "VulkanShader.hpp"
#include "Vulkan/Context.hpp"
#include "Scene/Shader.hpp"
#include "Vulkan/Resources/IShaderOwner.hpp"
namespace openVulkanoCpp::Vulkan
{
void VulkanShader::Init(Context* context, Scene::Shader* shader, IShaderOwner* owner)
{
this->device = context->device->device;
this->shader = shader;
this->owner = owner;
this->context = context;
shaderModules.reserve(shader->shaderPrograms.size());
shaderStageCreateInfo.resize(shader->shaderPrograms.size());
int i = 0;
for(const auto& shaderProgram : shader->shaderPrograms)
{
const auto shaderModule = context->device->CreateShaderModule(shaderProgram.GetShaderNameVulkan());
shaderModules.push_back(shaderModule);
shaderStageCreateInfo[i] = {{}, static_cast<vk::ShaderStageFlagBits>(static_cast<uint32_t>(shaderProgram.type)), shaderModule, "main"};
i++;
}
BuildPipeline();
}
void VulkanShader::BuildPipeline()
{
std::vector<vk::VertexInputBindingDescription> vertexBindDesc;
std::vector<vk::VertexInputAttributeDescription> attributeDescriptions;
vertexBindDesc.reserve(shader->vertexInputDescriptions.size());
for(const auto& description : shader->vertexInputDescriptions)
{
vertexBindDesc.emplace_back(description.bindingId, description.vertexSize, vk::VertexInputRate::eVertex);
if (shader->vertexInputDescriptions.size() > 1)
{
for(const auto& param : description.inputParameters)
{
attributeDescriptions.push_back(reinterpret_cast<const vk::VertexInputAttributeDescription&>(param));
}
}
}
uint32_t attributeDescriptionsSize;
vk::VertexInputAttributeDescription* attributeDescriptionsData;
if (shader->vertexInputDescriptions.size() == 1)
{ // Reuse already existing vertex attribute description
static_assert(sizeof(VertexInputParameter) == sizeof(vk::VertexInputAttributeDescription));
attributeDescriptionsSize = shader->vertexInputDescriptions[0].inputParameters.size();
attributeDescriptionsData = reinterpret_cast<vk::VertexInputAttributeDescription*>(shader->vertexInputDescriptions[0].inputParameters.data());
}
else
{
attributeDescriptionsSize = attributeDescriptions.size();
attributeDescriptionsData = attributeDescriptions.data();
}
vk::PipelineViewportStateCreateInfo viewportStateCreateInfo = { {}, 1, &context->swapChain.GetFullscreenViewport(), 1, &context->swapChain.GetFullscreenScissor() };
vk::PipelineVertexInputStateCreateInfo pipelineVertexInputStateCreateInfo = {
{}, static_cast<uint32_t>(vertexBindDesc.size()), vertexBindDesc.data(),
attributeDescriptionsSize, attributeDescriptionsData };
vk::PipelineInputAssemblyStateCreateInfo inputAssembly = { {}, static_cast<vk::PrimitiveTopology>(shader->topology), 0 };
vk::PipelineRasterizationStateCreateInfo rasterizer = {};
rasterizer.cullMode = static_cast<vk::CullModeFlagBits>(shader->cullMode);
vk::PipelineMultisampleStateCreateInfo msaa = {};
vk::PipelineDepthStencilStateCreateInfo depth = { {}, 1, 1, vk::CompareOp::eLess };
depth.maxDepthBounds = 1;
vk::PipelineColorBlendAttachmentState colorBlendAttachment = {};
colorBlendAttachment.colorWriteMask = vk::ColorComponentFlagBits::eA | vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eR;
if (shader->alphaBlend)
{
colorBlendAttachment.blendEnable = true;
colorBlendAttachment.srcColorBlendFactor = vk::BlendFactor::eSrcAlpha;
colorBlendAttachment.dstColorBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha;
colorBlendAttachment.colorBlendOp = vk::BlendOp::eAdd;
colorBlendAttachment.srcAlphaBlendFactor = vk::BlendFactor::eOne;
colorBlendAttachment.dstAlphaBlendFactor = vk::BlendFactor::eZero;
colorBlendAttachment.alphaBlendOp = vk::BlendOp::eAdd;
}
vk::PipelineColorBlendStateCreateInfo colorInfo = {};
colorInfo.attachmentCount = 1;
colorInfo.pAttachments = &colorBlendAttachment;
vk::GraphicsPipelineCreateInfo pipelineCreateInfo = { {}, static_cast<uint32_t>(shaderStageCreateInfo.size()), shaderStageCreateInfo.data(), &pipelineVertexInputStateCreateInfo, &inputAssembly,
nullptr, &viewportStateCreateInfo, &rasterizer, &msaa, &depth, &colorInfo, nullptr, context->pipeline.pipelineLayout, context->swapChainRenderPass.renderPass };
pipeline = this->device.createGraphicsPipeline({}, pipelineCreateInfo).value;
}
void VulkanShader::Resize()
{
device.destroyPipeline(pipeline);
BuildPipeline();
}
void VulkanShader::Record(vk::CommandBuffer& cmdBuffer, uint32_t bufferId)
{
cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline);
}
void VulkanShader::Close()
{
owner->RemoveShader(this);
shader = nullptr;
device.destroyPipeline(pipeline);
for(auto& shaderModule : shaderModules)
{
device.destroyShaderModule(shaderModule);
}
}
}