Refactor UniformBuffers and DescriptorSets
- Refactor UniformBuffers to keep track of the uniforms it contains - Refactor DescriptorSets to initialize UniformsBuffers and keep track of all its bindings - DescriptorSets can now be created from the Pipeline - Add custom DescriptorSets to Renderer
This commit is contained in:
@@ -4,9 +4,11 @@
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
UniformBuffer::UniformBuffer(Vulkan& vulkan, VkDeviceSize size)
|
||||
: Buffer{vulkan, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, size, SwapChain::MAX_FRAMES_IN_FLIGHT}
|
||||
{}
|
||||
UniformBuffer::UniformBuffer(Vulkan& vulkan, ShaderBinding binding)
|
||||
: Buffer{vulkan, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, binding.GetUniformBufferSize(), SwapChain::MAX_FRAMES_IN_FLIGHT}, binding{binding}
|
||||
{
|
||||
buffer.resize(Buffer::GetSize());
|
||||
}
|
||||
|
||||
VkDescriptorBufferInfo UniformBuffer::GetDescriptorBufferInfo(int index) const
|
||||
{
|
||||
@@ -16,4 +18,58 @@ namespace Copium
|
||||
bufferInfo.range = size;
|
||||
return bufferInfo;
|
||||
}
|
||||
|
||||
void UniformBuffer::Set(const std::string& str, const glm::mat3& data)
|
||||
{
|
||||
CP_ASSERT(binding.GetUniformType(str) == UniformType::Mat3, "Set : Uniform type missmatch = %s", str.c_str());
|
||||
uint32_t offset = binding.GetUniformOffset(str);
|
||||
memcpy(buffer.data() + offset, &data, sizeof(glm::mat3));
|
||||
}
|
||||
|
||||
void UniformBuffer::Set(const std::string& str, const glm::mat4& data)
|
||||
{
|
||||
CP_ASSERT(binding.GetUniformType(str) == UniformType::Mat4, "Set : Uniform type missmatch = %s", str.c_str());
|
||||
uint32_t offset = binding.GetUniformOffset(str);
|
||||
memcpy(buffer.data() + offset, &data, sizeof(glm::mat4));
|
||||
}
|
||||
|
||||
void UniformBuffer::Set(const std::string& str, const glm::vec2& data)
|
||||
{
|
||||
CP_ASSERT(binding.GetUniformType(str) == UniformType::Vec2, "Set : Uniform type missmatch = %s", str.c_str());
|
||||
uint32_t offset = binding.GetUniformOffset(str);
|
||||
memcpy(buffer.data() + offset, &data, sizeof(glm::vec2));
|
||||
}
|
||||
|
||||
void UniformBuffer::Set(const std::string& str, const glm::vec3& data)
|
||||
{
|
||||
CP_ASSERT(binding.GetUniformType(str) == UniformType::Vec3, "Set : Uniform type missmatch = %s", str.c_str());
|
||||
uint32_t offset = binding.GetUniformOffset(str);
|
||||
memcpy(buffer.data() + offset, &data, sizeof(glm::vec3));
|
||||
}
|
||||
|
||||
void UniformBuffer::Set(const std::string& str, const glm::vec4& data)
|
||||
{
|
||||
CP_ASSERT(binding.GetUniformType(str) == UniformType::Vec4, "Set : Uniform type missmatch = %s", str.c_str());
|
||||
uint32_t offset = binding.GetUniformOffset(str);
|
||||
memcpy(buffer.data() + offset, &data, sizeof(glm::vec4));
|
||||
}
|
||||
|
||||
void UniformBuffer::Set(const std::string& str, float data)
|
||||
{
|
||||
CP_ASSERT(binding.GetUniformType(str) == UniformType::Float, "Set : Uniform type missmatch = %s", str.c_str());
|
||||
uint32_t offset = binding.GetUniformOffset(str);
|
||||
memcpy(buffer.data() + offset, &data, sizeof(float));
|
||||
}
|
||||
|
||||
void UniformBuffer::Set(const std::string& str, int data)
|
||||
{
|
||||
CP_ASSERT(binding.GetUniformType(str) == UniformType::Int, "Set : Uniform type missmatch = %s", str.c_str());
|
||||
uint32_t offset = binding.GetUniformOffset(str);
|
||||
memcpy(buffer.data() + offset, &data, sizeof(int));
|
||||
}
|
||||
|
||||
void UniformBuffer::Update()
|
||||
{
|
||||
Buffer::Update(buffer.data(), vulkan.GetSwapChain().GetFlightIndex());
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
#include "copium/buffer/Buffer.h"
|
||||
#include "copium/core/Vulkan.h"
|
||||
#include "copium/pipeline/ShaderBinding.h"
|
||||
#include "copium/util/Common.h"
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <vulkan/vulkan.hpp>
|
||||
|
||||
namespace Copium
|
||||
@@ -12,20 +14,22 @@ namespace Copium
|
||||
{
|
||||
CP_DELETE_COPY_AND_MOVE_CTOR(UniformBuffer);
|
||||
|
||||
ShaderBinding binding;
|
||||
std::vector<uint8_t> buffer;
|
||||
|
||||
public:
|
||||
UniformBuffer(Vulkan& vulkan, VkDeviceSize size);
|
||||
UniformBuffer(Vulkan& vulkan, ShaderBinding binding);
|
||||
|
||||
VkDescriptorBufferInfo GetDescriptorBufferInfo(int index) const;
|
||||
|
||||
template <typename T>
|
||||
void Update(const T& t);
|
||||
void Set(const std::string& str, const glm::mat3& data);
|
||||
void Set(const std::string& str, const glm::mat4& data);
|
||||
void Set(const std::string& str, const glm::vec2& data);
|
||||
void Set(const std::string& str, const glm::vec3& data);
|
||||
void Set(const std::string& str, const glm::vec4& data);
|
||||
void Set(const std::string& str, float data);
|
||||
void Set(const std::string& str, int data);
|
||||
|
||||
void Update();
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void UniformBuffer::Update(const T& t)
|
||||
{
|
||||
CP_ASSERT(sizeof(T) == Buffer::GetSize(), "Update : Template size is not the same as buffer size %u != %u", sizeof(T), Buffer::GetSize());
|
||||
Buffer::Update((void*)&t, vulkan.GetSwapChain().GetFlightIndex());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user