22#include "aeongames/AeonEngine.hpp"
23#include "aeongames/CRC.hpp"
24#include "aeongames/Material.hpp"
25#include "aeongames/Vector2.hpp"
29#include "VulkanMemoryPoolBuffer.hpp"
30#include "VulkanRenderer.hpp"
31#include "VulkanUtilities.hpp"
36 mVulkanRenderer { aVulkanRenderer },
37 mUniformBuffer { mVulkanRenderer,
38 ( ( aStackSize - 1 ) | ( mVulkanRenderer.GetPhysicalDeviceProperties().limits.minUniformBufferOffsetAlignment - 1 ) ) + 1,
39 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
40 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT }
42 InitializeDescriptorPool();
43 InitializeDescriptorSet();
47 mVulkanRenderer { aVulkanMemoryPoolBuffer.mVulkanRenderer },
48 mUniformBuffer {
std::move ( aVulkanMemoryPoolBuffer.mUniformBuffer ) }
50 std::swap ( mOffset, aVulkanMemoryPoolBuffer.mOffset );
51 std::swap ( mVkDescriptorPool, aVulkanMemoryPoolBuffer.mVkDescriptorPool );
52 std::swap ( mVkDescriptorSet, aVulkanMemoryPoolBuffer.mVkDescriptorSet );
56 mVulkanRenderer { aVulkanRenderer }, mUniformBuffer{aVulkanRenderer} {}
60 mUniformBuffer.Initialize ( ( ( aStackSize - 1 ) | ( mVulkanRenderer.GetPhysicalDeviceProperties().limits.minUniformBufferOffsetAlignment - 1 ) ) + 1,
61 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
62 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT );
63 InitializeDescriptorPool();
64 InitializeDescriptorSet();
67 VulkanMemoryPoolBuffer::~VulkanMemoryPoolBuffer()
74 FinalizeDescriptorPool();
75 mUniformBuffer.Finalize();
78 void VulkanMemoryPoolBuffer::InitializeDescriptorPool()
83 void VulkanMemoryPoolBuffer::InitializeDescriptorSet()
85 VkDescriptorSetLayoutCreateInfo descriptor_set_layout_create_info{};
86 descriptor_set_layout_create_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
87 descriptor_set_layout_create_info.bindingCount = 1;
88 VkDescriptorSetLayoutBinding descriptor_set_layout_binding{};
89 descriptor_set_layout_binding.binding = 0;
90 descriptor_set_layout_binding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
91 descriptor_set_layout_binding.descriptorCount = 1;
94 descriptor_set_layout_binding.stageFlags = VK_SHADER_STAGE_ALL;
95 descriptor_set_layout_binding.pImmutableSamplers =
nullptr;
96 descriptor_set_layout_create_info.pBindings = &descriptor_set_layout_binding;
97 mVkDescriptorSet =
CreateDescriptorSet ( mVulkanRenderer.GetDevice(), mVkDescriptorPool, mVulkanRenderer.GetDescriptorSetLayout ( descriptor_set_layout_create_info ) );
99 VkDescriptorBufferInfo descriptor_buffer_info = { mUniformBuffer.GetBuffer(), 0, mUniformBuffer.GetSize() };
100 VkWriteDescriptorSet write_descriptor_set{};
101 write_descriptor_set.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
102 write_descriptor_set.pNext =
nullptr;
103 write_descriptor_set.dstSet = mVkDescriptorSet;
104 write_descriptor_set.dstBinding = 0;
105 write_descriptor_set.dstArrayElement = 0;
106 write_descriptor_set.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
107 write_descriptor_set.descriptorCount = 1;
108 write_descriptor_set.pBufferInfo = &descriptor_buffer_info;
109 write_descriptor_set.pImageInfo =
nullptr;
110 write_descriptor_set.pTexelBufferView =
nullptr;
111 vkUpdateDescriptorSets ( mVulkanRenderer.GetDevice(), 1, &write_descriptor_set, 0,
nullptr );
114 void VulkanMemoryPoolBuffer::FinalizeDescriptorPool()
116 if ( mVkDescriptorPool != VK_NULL_HANDLE )
118 vkDestroyDescriptorPool ( mVulkanRenderer.GetDevice(), mVkDescriptorPool,
nullptr );
119 mVkDescriptorPool = VK_NULL_HANDLE;
125 size_t offset = mOffset;
126 mOffset += ( ( aSize - 1 ) | ( mVulkanRenderer.GetPhysicalDeviceProperties().limits.minUniformBufferOffsetAlignment - 1 ) ) + 1;
127 if ( mOffset > mUniformBuffer.GetSize() )
130 std::cout <<
LogLevel::Error <<
"Memory Pool Buffer cannot fulfill allocation request." << std::endl;
131 throw std::runtime_error (
"Memory Pool Buffer cannot fulfill allocation request." );
143 return mVkDescriptorSet;
147 return mUniformBuffer;
Defines log severity levels and stream output for the AeonGames engine.
Header for the 3D vector class.
Header for the 4D vector class.
Provides access to a region within a memory pool buffer.
Abstract interface for GPU/memory buffer operations.
const VkDescriptorSet & GetDescriptorSet() const
Get the Vulkan descriptor set for this pool buffer.
void Initialize(size_t aStackSize)
Initialize the pool buffer with the given size.
VulkanMemoryPoolBuffer(const VulkanRenderer &aVulkanRenderer, size_t aStackSize)
Construct with a renderer and initial pool size.
const Buffer & GetBuffer() const final
Get a reference to the underlying Buffer.
void Finalize()
Release pool buffer and descriptor resources.
BufferAccessor Allocate(size_t aSize) final
Allocate a sub-region from the memory pool.
void Reset() final
Reset the pool, freeing all previous allocations.
Vulkan rendering backend implementing the Renderer interface.
const VkDevice & GetDevice() const
Get the logical device handle.
<- This is here just for the literals
VkDescriptorPool CreateDescriptorPool(const VkDevice &aVkDevice, const std::vector< VkDescriptorPoolSize > &aVkDescriptorPoolSizes)
Create a Vulkan descriptor pool from the given pool sizes.
VkDescriptorSet CreateDescriptorSet(const VkDevice &aVkDevice, const VkDescriptorPool &aVkDescriptorPool, const VkDescriptorSetLayout &aVkDescriptorSetLayout, uint32_t aDescriptorSetCount)
Allocate a Vulkan descriptor set from the given pool and layout.