Cleanup code

This commit is contained in:
2023-09-09 23:18:27 +02:00
parent 703f5c0d12
commit 2b90b5d84e
11 changed files with 91 additions and 45 deletions

View File

@@ -153,14 +153,12 @@ namespace openVulkanoCpp::Vulkan
Scene::Drawable** drawablePointer;
VulkanDrawContext drawContext { poolId, currentImageId, cmdHelper->cmdBuffer, this };
if (!scene->GetCamera()->renderCamera) resourceManager.PrepareCamera(scene->GetCamera());
drawContext.SetCamera(scene->GetCamera());
while((drawablePointer = jobQueue->Pop()) != nullptr)
{
Scene::Drawable* drawable = *drawablePointer;
drawContext.EncodeShader(drawable->GetShader());
static_cast<VulkanCamera*>(scene->GetCamera()->renderCamera)->Record(&drawContext);
//cmdHelper->cmdBuffer.pushConstants(drawContext.GetShader()->pipelineLayout, vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment, 0, 3 * sizeof(Math::Matrix4f) + sizeof(Math::Vector4f) + 8 * sizeof(float), &scene->GetCamera()->GetViewProjectionMatrix());
drawable->GetEncoder().vulkan(drawable, &drawContext);
}
cmdHelper->cmdBuffer.end();

View File

@@ -172,7 +172,7 @@ namespace openVulkanoCpp::Vulkan
}
}
void ResourceManager::PrepareCamera(Scene::Camera* camera)
VulkanCamera* ResourceManager::PrepareCamera(Scene::Camera* camera)
{
const std::unique_lock lock(mutex);
if (!camera->renderCamera)
@@ -187,6 +187,7 @@ namespace openVulkanoCpp::Vulkan
vkCam->Init(camera, uBuffer);
camera->renderCamera = vkCam;
}
return static_cast<VulkanCamera*>(camera->renderCamera);
}
UniformBuffer* ResourceManager::CreateUniformBuffer(const DescriptorSetLayoutBinding& binding, size_t size, void* data, uint32_t setId)

View File

@@ -33,6 +33,7 @@ namespace openVulkanoCpp
class Context;
class VulkanGeometry;
class VulkanTexture;
class VulkanCamera;
class UniformBuffer;
class ResourceManager : public ICloseable, public IShaderOwner
@@ -83,7 +84,7 @@ namespace openVulkanoCpp
VulkanTexture* PrepareTexture(Scene::Texture* texture);
void PrepareCamera(Scene::Camera* camera);
VulkanCamera* PrepareCamera(Scene::Camera* camera);
void RemoveShader(VulkanShader* shader) override;

View File

@@ -0,0 +1,46 @@
/*
* 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 "UniformBuffer.hpp"
#include "ManagedResource.hpp"
#include "ResourceManager.hpp"
#include "Vulkan/VulkanDrawContext.hpp"
#include "Vulkan/Scene/VulkanShader.hpp"
namespace openVulkanoCpp::Vulkan
{
void UniformBuffer::Init(ManagedBuffer* buffer, uint32_t frameOffset, uint32_t frameSize, vk::DescriptorSetLayout* descriptorSetLayout, const DescriptorSetLayoutBinding& binding, uint32_t setId)
{
m_buffer = buffer;
m_frameOffset = frameOffset;
const vk::DescriptorSetAllocateInfo descSetAllocInfo = { ResourceManager::INSTANCE->descriptorPool, 1, descriptorSetLayout };
m_descriptorSet = buffer->allocation->device.allocateDescriptorSets(descSetAllocInfo)[0];
vk::DescriptorBufferInfo bufferInfo = { buffer->buffer, 0, frameSize };
vk::WriteDescriptorSet writeDescriptorSet = { m_descriptorSet, binding.bindingId, 0, 1 };
writeDescriptorSet.descriptorType = static_cast<vk::DescriptorType>(binding.descriptorType);
writeDescriptorSet.pBufferInfo = &bufferInfo;
buffer->allocation->device.updateDescriptorSets(1, &writeDescriptorSet, 0, nullptr);
m_setOffset = setId;
}
void UniformBuffer::Close()
{ //TODO handle this better
m_buffer->allocation->device.freeDescriptorSets(ResourceManager::INSTANCE->descriptorPool, 1, &m_descriptorSet);
m_buffer = nullptr;
}
void UniformBuffer::Record(VulkanDrawContext* drawContext)
{
uint32_t frameOffset = m_frameOffset * drawContext->currentImageId;
drawContext->commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, drawContext->GetShader()->pipelineLayout, m_setOffset, m_setCount, &m_descriptorSet, (m_frameOffset) ? 1 : 0, &frameOffset);
}
void UniformBuffer::Update(void* data, uint32_t size, uint32_t bufferId) const
{
m_buffer->Copy(data, size, m_frameOffset * bufferId);
}
}

View File

@@ -7,16 +7,15 @@
#pragma once
#include "Base/ICloseable.hpp"
#include "ManagedResource.hpp"
#include "Vulkan/Scene/IRecordable.hpp"
#include "Vulkan/Scene/VulkanShader.hpp"
#include "Vulkan/VulkanDrawContext.hpp"
namespace openVulkanoCpp::Vulkan
{
class UniformBuffer final : IRecordable, ICloseable
struct ManagedBuffer;
class UniformBuffer final : public IRecordable, public ICloseable
{
ManagedBuffer* m_buffer;
ManagedBuffer* m_buffer = nullptr;
vk::DescriptorSet m_descriptorSet;
uint32_t m_frameOffset;
uint32_t m_setOffset, m_setCount = 1;
@@ -27,36 +26,12 @@ namespace openVulkanoCpp::Vulkan
if (m_buffer) Close();
}
void Init(ManagedBuffer* buffer, uint32_t frameOffset, uint32_t frameSize, vk::DescriptorSetLayout* descriptorSetLayout, const DescriptorSetLayoutBinding& binding, uint32_t setId)
{
m_buffer = buffer;
m_frameOffset = frameOffset;
void Init(ManagedBuffer* buffer, uint32_t frameOffset, uint32_t frameSize, vk::DescriptorSetLayout* descriptorSetLayout, const DescriptorSetLayoutBinding& binding, uint32_t setId);
const vk::DescriptorSetAllocateInfo descSetAllocInfo = { ResourceManager::INSTANCE->descriptorPool, 1, descriptorSetLayout };
m_descriptorSet = buffer->allocation->device.allocateDescriptorSets(descSetAllocInfo)[0];
vk::DescriptorBufferInfo bufferInfo = { buffer->buffer, 0, frameSize };
vk::WriteDescriptorSet writeDescriptorSet = { m_descriptorSet, binding.bindingId, 0, 1 };
writeDescriptorSet.descriptorType = static_cast<vk::DescriptorType>(binding.descriptorType);
writeDescriptorSet.pBufferInfo = &bufferInfo;
buffer->allocation->device.updateDescriptorSets(1, &writeDescriptorSet, 0, nullptr);
m_setOffset = setId;
}
void Close() override;
void Close() override
{ //TODO handle this better
m_buffer->allocation->device.freeDescriptorSets(ResourceManager::INSTANCE->descriptorPool, 1, &m_descriptorSet);
m_buffer = nullptr;
}
void Record(VulkanDrawContext* drawContext) override;
void Record(VulkanDrawContext* drawContext) override
{
uint32_t frameOffset = m_frameOffset * drawContext->currentImageId;
drawContext->commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, drawContext->GetShader()->pipelineLayout, m_setOffset, m_setCount, &m_descriptorSet, (m_frameOffset) ? 1 : 0, &frameOffset);
}
void Update(void* data, uint32_t size, uint32_t bufferId) const
{
m_buffer->Copy(data, size, m_frameOffset * bufferId);
}
void Update(void* data, uint32_t size, uint32_t bufferId) const;
};
}

View File

@@ -14,25 +14,25 @@ namespace openVulkanoCpp::Vulkan
{
class VulkanCamera : public ICloseable, public IRecordable
{
Scene::Camera* camera = nullptr;
UniformBuffer* buffer = nullptr;
Scene::Camera* m_camera = nullptr;
UniformBuffer* m_buffer = nullptr;
public:
void Init(Scene::Camera* camera, UniformBuffer* uniformBuffer)
{
this->camera = camera;
buffer = uniformBuffer;
m_camera = camera;
m_buffer = uniformBuffer;
}
void Record(VulkanDrawContext* context) override
{
buffer->Update(camera->GetData(), Scene::Camera::SIZE, context->currentImageId);
buffer->Record(context);
m_buffer->Update(m_camera->GetData(), Scene::Camera::SIZE, context->currentImageId);
m_buffer->Record(context);
}
void Close() override
{
buffer->Close();
m_buffer->Close();
}
};
}

View File

@@ -4,6 +4,8 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "IRecordable.hpp"
#include "Vulkan/Image.hpp"
#include "Vulkan/Context.hpp"

View File

@@ -7,6 +7,7 @@
#include "VulkanDrawContext.hpp"
#include "Scene/Shader/Shader.hpp"
#include "Scene/VulkanShader.hpp"
#include "Scene/VulkanCamera.hpp"
namespace openVulkanoCpp::Vulkan
{
@@ -20,6 +21,7 @@ namespace openVulkanoCpp::Vulkan
else if (m_lastShader == vkShader) return; // Skip it if shader is already bound
vkShader->Record(this);
m_lastShader = vkShader;
if (m_lastCamera) m_lastCamera->Record(this);
}
void VulkanDrawContext::EncodeShader(VulkanShader* vkShader)
@@ -28,4 +30,16 @@ namespace openVulkanoCpp::Vulkan
vkShader->Record(this);
m_lastShader = vkShader;
}
void VulkanDrawContext::SetNode(Scene::Node* node)
{
}
void VulkanDrawContext::SetCamera(Scene::Camera* camera)
{
if (!camera->renderCamera) m_lastCamera = ResourceManager::INSTANCE->PrepareCamera(camera);
else m_lastCamera = static_cast<VulkanCamera*>(camera->renderCamera);
if (m_lastShader) m_lastCamera->Record(this);
}
}

View File

@@ -13,6 +13,7 @@ namespace openVulkanoCpp::Vulkan
class VulkanDrawContext
{
VulkanShader* m_lastShader = nullptr;
VulkanCamera* m_lastCamera = nullptr;
public:
const size_t encoderThreadId;
@@ -28,6 +29,10 @@ namespace openVulkanoCpp::Vulkan
void EncodeShader(VulkanShader* shader);
void SetNode(Scene::Node* node);
void SetCamera(Scene::Camera* camera);
VulkanShader* GetShader() const { return m_lastShader; }
};
}