Update shader to use new render resource system

This commit is contained in:
Georg Hagen
2024-08-21 13:33:15 +02:00
parent 3940a72084
commit b13c1c54ae
6 changed files with 26 additions and 38 deletions

View File

@@ -17,7 +17,8 @@ namespace OpenVulkano::Vulkan
VulkanShader::~VulkanShader()
{
if (shader) Close();
if (owner) owner->RemoveShader(this);
owner = nullptr;
device.destroyPipeline(pipeline);
for(auto& shaderModule : shaderModules)
{
@@ -28,12 +29,9 @@ namespace OpenVulkano::Vulkan
device.destroyDescriptorSetLayout(descriptorSetLayout);
}
void VulkanShader::Init(Context* context, Scene::Shader* shader, IShaderOwner* owner)
VulkanShader::VulkanShader(Context* context, Scene::Shader* shader, IShaderOwner* owner)
: IRenderResource<Scene::Shader>(shader), device(context->device->device), owner(owner), context(context)
{
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;
@@ -45,11 +43,11 @@ namespace OpenVulkano::Vulkan
i++;
}
BuildPipeline();
shader->renderShader = this;
}
void VulkanShader::BuildPipeline()
{
Scene::Shader* shader = GetOwner();
std::vector<vk::VertexInputBindingDescription> vertexBindDesc;
std::vector<vk::VertexInputAttributeDescription> attributeDescriptions;
vertexBindDesc.reserve(shader->vertexInputDescriptions.size());
@@ -129,14 +127,6 @@ namespace OpenVulkano::Vulkan
context->commandBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline);
}
void VulkanShader::Close()
{
shader->renderShader = nullptr;
shader = nullptr;
if (owner) owner->RemoveShader(this);
owner = nullptr;
}
void VulkanShader::CreatePipelineLayout()
{
if (!descriptorSetLayouts.empty())
@@ -150,6 +140,7 @@ namespace OpenVulkano::Vulkan
descriptorSetLayouts.push_back(device.createDescriptorSetLayout({ {}, layoutBindings1.size(), layoutBindings1.data() }));
descriptorSetLayouts.push_back(device.createDescriptorSetLayout({ {}, layoutBindings2.size(), layoutBindings2.data() }));
Scene::Shader* shader = GetOwner();
for(const auto& set : shader->descriptorSets)
{
vk::DescriptorSetLayoutCreateInfo createInfo { {}, static_cast<uint32_t>(set.size()), reinterpret_cast<const vk::DescriptorSetLayoutBinding*>(set.data()) };
@@ -159,4 +150,9 @@ namespace OpenVulkano::Vulkan
vk::PipelineLayoutCreateInfo plci = {{}, static_cast<uint32_t>(descriptorSetLayouts.size()), descriptorSetLayouts.data(), static_cast<uint32_t>(shader->pushConstantRanges.size()), pcRanges };
pipelineLayout = this->device.createPipelineLayout(plci);
}
void VulkanShader::Release()
{
if (owner) owner->RemoveShader(this);
}
}

View File

@@ -7,26 +7,20 @@
#pragma once
#include "IRecordable.hpp"
#include "Base/ICloseable.hpp"
#include "Scene/Shader/Shader.hpp"
#include <vulkan/vulkan.hpp>
#include <vector>
namespace OpenVulkano
{
namespace Scene
{
class Shader;
}
namespace Vulkan
{
class Context;
class IShaderOwner;
class VulkanShader final : public ICloseable, public IRecordable
class VulkanShader final : public IRenderResource<Scene::Shader>, public IRecordable
{
public:
Scene::Shader* shader = nullptr;
vk::Device device;
std::vector<vk::ShaderModule> shaderModules; // TODO manage live time somewhere else to allow sharing of shader programs
std::vector<vk::PipelineShaderStageCreateInfo> shaderStageCreateInfo;
@@ -36,17 +30,15 @@ namespace OpenVulkano
IShaderOwner* owner = nullptr;
Context* context = nullptr;
VulkanShader() = default;
VulkanShader(Context* context, Scene::Shader* shader, IShaderOwner* owner);
~VulkanShader() override;
void Init(Context* context, Scene::Shader* shader, IShaderOwner* owner);
void Resize();
void Record(VulkanDrawContext* context) override;
void Close() override;
void Release() override;
private:
void BuildPipeline();

View File

@@ -76,7 +76,7 @@ namespace OpenVulkano::Vulkan
void Record(VulkanDrawContext* context) override
{
int setId = -1, i = 0;
for (const auto& descriptorSet : context->GetShader()->shader->descriptorSets)
for (const auto& descriptorSet : context->GetShader()->GetOwner()->descriptorSets)
{
for (const auto& descriptor : descriptorSet)
{