Skip to content
Snippets Groups Projects
Commit ae06cc85 authored by Lars Hoerttrich's avatar Lars Hoerttrich
Browse files

[#22] Outsourced some functionality from hpp to cpp

parent 362702b3
No related branches found
No related tags found
1 merge request!18Resolve "Resource Management"
Pipeline #24918 passed
...@@ -14,13 +14,10 @@ namespace vkcv { ...@@ -14,13 +14,10 @@ namespace vkcv {
//Enum of buffertypes //Enum of buffertypes
enum BufferType { VERTEX, UNIFORM, STORAGE }; enum BufferType { VERTEX, UNIFORM, STORAGE };
//(temporary?) struct example for T //Functions outsourced to Buffer.cpp file:
//TODO void outsourcedDestructor(vk::Device device, vk::DeviceMemory bufferMemory, vk::Buffer buffer);
struct vertex_t { vk::Buffer outsourcedCreateBuffer(vk::Device device, BufferType type, size_t size);
//glm::vec3 pos; vk::DeviceMemory outsourcedAllocateMemory(vk::Device device, vk::PhysicalDevice physicalDevice, vk::MemoryRequirements memoryRequirements);
//glm::vec3 color;
float x, y, z;
};
template<typename T> template<typename T>
class Buffer { class Buffer {
...@@ -35,8 +32,7 @@ namespace vkcv { ...@@ -35,8 +32,7 @@ namespace vkcv {
Buffer<T>() = delete; Buffer<T>() = delete;
// is never called directly // is never called directly
~Buffer<T>() noexcept { ~Buffer<T>() noexcept {
m_Device.freeMemory(m_BufferMemory); outsourcedDestructor(m_Device, m_BufferMemory, m_Buffer);
m_Device.destroyBuffer(m_Buffer);
} }
Buffer<T>(const Buffer<T>& other) = delete; // copy-ctor Buffer<T>(const Buffer<T>& other) = delete; // copy-ctor
...@@ -77,7 +73,13 @@ namespace vkcv { ...@@ -77,7 +73,13 @@ namespace vkcv {
BufferType getType() { return m_Type; }; BufferType getType() { return m_Type; };
size_t getSize() { return m_Size; }; size_t getSize() { return m_Size; };
// TODO: we will probably need staging-buffer here later (possible add in BufferManager later?) /**
* Maps this buffers Memory, fills this buffer with @p data of type T and count @p count
* unmaps afterwards.
* @p data Pointer to data
* @p count Amount of data of type T
*/
// TODO: we will probably need staging-buffer here later (possible add in BufferManager later?)
void fill(T* data, size_t count) { void fill(T* data, size_t count) {
const vk::MemoryRequirements requirements = m_Device.getBufferMemoryRequirements(m_Buffer); const vk::MemoryRequirements requirements = m_Device.getBufferMemoryRequirements(m_Buffer);
...@@ -111,15 +113,8 @@ namespace vkcv { ...@@ -111,15 +113,8 @@ namespace vkcv {
static Buffer<T> create(vk::Device device, vk::PhysicalDevice physicalDevice, BufferType type, size_t size) { static Buffer<T> create(vk::Device device, vk::PhysicalDevice physicalDevice, BufferType type, size_t size) {
vk::Buffer buffer = nullptr; vk::Buffer buffer = nullptr;
switch (type) {
case VERTEX: { buffer = outsourcedCreateBuffer(device, type, sizeof(T) * size);
//create vertex buffer
buffer = device.createBuffer(vk::BufferCreateInfo(vk::BufferCreateFlags(), sizeof(T) * size, vk::BufferUsageFlagBits::eVertexBuffer));
}
default: {
// TODO: maybe an issue
}
}
if (!buffer) { if (!buffer) {
//TODO: potential issue //TODO: potential issue
...@@ -128,11 +123,7 @@ namespace vkcv { ...@@ -128,11 +123,7 @@ namespace vkcv {
//get requirements for allocation //get requirements for allocation
const vk::MemoryRequirements requirements = device.getBufferMemoryRequirements(buffer); const vk::MemoryRequirements requirements = device.getBufferMemoryRequirements(buffer);
//find Memory Type vk::DeviceMemory memory= outsourcedAllocateMemory(device, physicalDevice, requirements);
uint32_t memoryType = searchMemoryType(physicalDevice.getMemoryProperties(), requirements.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
//allocate memory for buffer
vk::DeviceMemory memory = device.allocateMemory(vk::MemoryAllocateInfo(requirements.size, memoryType));
if (!memory) { if (!memory) {
//TODO: other potential issue //TODO: other potential issue
...@@ -167,31 +158,6 @@ namespace vkcv { ...@@ -167,31 +158,6 @@ namespace vkcv {
m_Type(type), m_Type(type),
m_Size(size), m_Size(size),
m_DataP(nullptr) m_DataP(nullptr)
{} {}
/**
* @brief searches memory type index for buffer allocation, inspired by vulkan tutorial and "https://github.com/KhronosGroup/Vulkan-Hpp/blob/master/samples/utils/utils.hpp"
* @param physicalMemoryProperties Memory Properties of physical device
* @param typeBits
* @param requirements Property flags that are required
* @return memory type index for Buffer
*/
static uint32_t searchMemoryType(vk::PhysicalDeviceMemoryProperties const& physicalMemoryProperties, uint32_t typeBits, vk::MemoryPropertyFlags requirements) {
uint32_t memoryTypeIndex = uint32_t(0);
for (uint32_t i = 0; i < physicalMemoryProperties.memoryTypeCount; i++)
{
if ((typeBits & 1) &&
((physicalMemoryProperties.memoryTypes[i].propertyFlags & requirements) == requirements))
{
memoryTypeIndex = i;
break;
}
typeBits >>= 1;
}
return memoryTypeIndex;
}
}; };
} }
...@@ -6,5 +6,56 @@ ...@@ -6,5 +6,56 @@
#include"vkcv/Buffer.hpp" #include"vkcv/Buffer.hpp"
namespace vkcv { namespace vkcv {
/**
* @brief searches memory type index for buffer allocation, inspired by vulkan tutorial and "https://github.com/KhronosGroup/Vulkan-Hpp/blob/master/samples/utils/utils.hpp"
* @param physicalMemoryProperties Memory Properties of physical device
* @param typeBits
* @param requirements Property flags that are required
* @return memory type index for Buffer
*/
uint32_t searchMemoryType(vk::PhysicalDeviceMemoryProperties const& physicalMemoryProperties, uint32_t typeBits, vk::MemoryPropertyFlags requirements) {
uint32_t memoryTypeIndex = uint32_t(0);
for (uint32_t i = 0; i < physicalMemoryProperties.memoryTypeCount; i++)
{
if ((typeBits & 1) &&
((physicalMemoryProperties.memoryTypes[i].propertyFlags & requirements) == requirements))
{
memoryTypeIndex = i;
break;
}
typeBits >>= 1;
}
return memoryTypeIndex;
}
void outsourcedDestructor(vk::Device device, vk::DeviceMemory bufferMemory, vk::Buffer buffer)
{
if (device) {
device.freeMemory(bufferMemory);
device.destroyBuffer(buffer);
}
}
vk::Buffer outsourcedCreateBuffer(vk::Device device, BufferType type, size_t size)
{
switch (type) {
case VERTEX: {
//create vertex buffer
return device.createBuffer(vk::BufferCreateInfo(vk::BufferCreateFlags(), size, vk::BufferUsageFlagBits::eVertexBuffer));
}
default: {
// TODO: maybe an issue
}
}
return vk::Buffer(); //should never be reached
}
vk::DeviceMemory outsourcedAllocateMemory(vk::Device device, vk::PhysicalDevice physicalDevice, vk::MemoryRequirements memoryRequirements)
{
//find Memory Type
uint32_t memoryType = searchMemoryType(physicalDevice.getMemoryProperties(), memoryRequirements.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
return device.allocateMemory(vk::MemoryAllocateInfo(memoryRequirements.size, memoryType));
}
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment