More templates and more designated initializers

This commit is contained in:
Guillaume Meunier
2024-01-28 11:33:31 +01:00
parent 3a5eb04ace
commit 3b4d50f707
6 changed files with 214 additions and 198 deletions
+44 -59
View File
@@ -20,97 +20,82 @@
#include "allocation.h"
#include "application.h"
#include "spdlog/spdlog.h"
#include "utils/check.h"
#include "vulkan/vulkan_raii.hpp"
#include <utility>
#include <vk_mem_alloc.h>
buffer_allocation::buffer_allocation(const vk::BufferCreateInfo & buffer_info, const VmaAllocationCreateInfo & alloc_info)
std::pair<vk::raii::Buffer, VmaAllocation> basic_allocation_traits<VkBuffer>::create(
const CreateInfo& buffer_info,
const VmaAllocationCreateInfo* alloc_info)
{
VmaAllocator allocator = application::get_allocator();
vk::raii::Device& device = application::get_device();
VmaAllocation allocation;
VkBuffer tmp;
CHECK_VK(vmaCreateBuffer(allocator, &static_cast<const VkBufferCreateInfo&>(buffer_info), &alloc_info, &tmp, &allocation, nullptr));
buffer = vk::raii::Buffer(application::get_device(), tmp);
CHECK_VK(vmaCreateBuffer(allocator, &(NativeCreateInfo&)buffer_info, alloc_info, &tmp, &allocation, nullptr));
return std::pair<vk::raii::Buffer, VmaAllocation>{vk::raii::Buffer{device, tmp}, allocation};
}
buffer_allocation::~buffer_allocation()
void basic_allocation_traits<VkBuffer>::destroy(
vk::raii::Buffer& buffer,
VmaAllocation allocation,
void * mapped)
{
VmaAllocator allocator = application::get_allocator();
if (data)
if (mapped)
vmaUnmapMemory(allocator, allocation);
vmaDestroyBuffer(allocator, buffer.release(), allocation);
}
buffer_allocation::buffer_allocation(buffer_allocation && other) :
allocation(other.allocation),
buffer(std::move(other.buffer)),
data(other.data)
{
other.allocation = nullptr;
other.data = nullptr;
}
void * buffer_allocation::map()
{
if (data)
return data;
VmaAllocator allocator = application::get_allocator();
CHECK_VK(vmaMapMemory(allocator, allocation, &data));
return data;
}
const buffer_allocation & buffer_allocation::operator=(buffer_allocation && other)
{
std::swap(allocation, other.allocation);
std::swap(buffer, other.buffer);
return *this;
}
image_allocation::image_allocation(const vk::ImageCreateInfo & image_info, const VmaAllocationCreateInfo & alloc_info)
void * basic_allocation_traits<VkBuffer>::map(VmaAllocation allocation)
{
VmaAllocator allocator = application::get_allocator();
void * mapped;
CHECK_VK(vmaMapMemory(allocator, allocation, &mapped));
return mapped;
}
std::pair<vk::raii::Image, VmaAllocation> basic_allocation_traits<VkImage>::create(
const CreateInfo& image_info,
const VmaAllocationCreateInfo* alloc_info)
{
VmaAllocator allocator = application::get_allocator();
vk::raii::Device& device = application::get_device();
VmaAllocation allocation;
VkImage tmp;
CHECK_VK(vmaCreateImage(allocator, &static_cast<const VkImageCreateInfo&>(image_info), &alloc_info, &tmp, &allocation, nullptr));
image = vk::raii::Image(application::get_device(), tmp);
CHECK_VK(vmaCreateImage(allocator, &(NativeCreateInfo&)image_info, alloc_info, &tmp, &allocation, nullptr));
return std::pair<vk::raii::Image, VmaAllocation>{vk::raii::Image{device, tmp}, allocation};
}
image_allocation::~image_allocation()
void basic_allocation_traits<VkImage>::destroy(
vk::raii::Image& image,
VmaAllocation allocation,
void * mapped)
{
VmaAllocator allocator = application::get_allocator();
if (data)
if (mapped)
vmaUnmapMemory(allocator, allocation);
vmaDestroyImage(allocator, image.release(), allocation);
}
image_allocation::image_allocation(image_allocation && other) :
allocation(other.allocation),
image(std::move(other.image)),
data(other.data)
void * basic_allocation_traits<VkImage>::map(VmaAllocation allocation)
{
other.allocation = nullptr;
other.data = nullptr;
}
void * image_allocation::map()
{
if (data)
return data;
VmaAllocator allocator = application::get_allocator();
CHECK_VK(vmaMapMemory(allocator, allocation, &data));
return data;
}
const image_allocation & image_allocation::operator=(image_allocation && other)
{
std::swap(allocation, other.allocation);
std::swap(image, other.image);
return *this;
void * mapped;
CHECK_VK(vmaMapMemory(allocator, allocation, &mapped));
return mapped;
}
+109 -80
View File
@@ -22,31 +22,73 @@
#include "vk_mem_alloc.h"
#include <vulkan/vulkan_raii.hpp>
class buffer_allocation
template<typename T>
struct basic_allocation_traits
{
};
template<>
struct basic_allocation_traits<VkBuffer>
{
using CreateInfo = vk::BufferCreateInfo;
using NativeCreateInfo = CreateInfo::NativeType;
using RaiiType = vk::raii::Buffer;
static std::pair<RaiiType, VmaAllocation> create(
const CreateInfo& buffer_info,
const VmaAllocationCreateInfo* alloc_info);
static void destroy(
RaiiType& buffer,
VmaAllocation allocation,
void * mapped);
static void * map(VmaAllocation allocation);
};
template<>
struct basic_allocation_traits<VkImage>
{
using CreateInfo = vk::ImageCreateInfo;
using NativeCreateInfo = CreateInfo::NativeType;
using RaiiType = vk::raii::Image;
static std::pair<RaiiType, VmaAllocation> create(
const CreateInfo& image_info,
const VmaAllocationCreateInfo* alloc_info);
static void destroy(
RaiiType& image,
VmaAllocation allocation,
void * mapped);
static void * map(VmaAllocation allocation);
};
template<typename T>
class basic_allocation
{
public:
using CType = T::CType;
using traits = basic_allocation_traits<CType>;
using CreateInfo = traits::CreateInfo;
using NativeCreateInfo = CreateInfo::NativeType;
using RaiiType = traits::RaiiType;
private:
VmaAllocation allocation = nullptr;
vk::raii::Buffer buffer = nullptr;
void * data = nullptr;
RaiiType resource = nullptr;
void * mapped = nullptr;
public:
operator VkBuffer() const
operator T()
{
return *buffer;
return *resource;
}
operator vk::Buffer() const
operator CType()
{
return *buffer;
}
operator const vk::raii::Buffer&() const
{
return buffer;
}
operator vk::raii::Buffer&()
{
return buffer;
return *resource;
}
operator VmaAllocation() const
@@ -54,76 +96,63 @@ public:
return allocation;
}
vk::raii::Buffer* operator->()
RaiiType* operator->()
{
return &buffer;
return &resource;
}
const vk::raii::Buffer* operator->() const
const T* operator->() const
{
return &buffer;
return &resource;
}
buffer_allocation() = default;
buffer_allocation(const vk::BufferCreateInfo& buffer_info, const VmaAllocationCreateInfo& alloc_info);
buffer_allocation(const buffer_allocation&) = delete;
buffer_allocation(buffer_allocation&&);
const buffer_allocation& operator=(const buffer_allocation&) = delete;
const buffer_allocation& operator=(buffer_allocation&&);
~buffer_allocation();
basic_allocation() = default;
basic_allocation(const CreateInfo& buffer_info, const VmaAllocationCreateInfo& alloc_info)
{
std::tie(resource, allocation) = traits::create(buffer_info, &alloc_info);
}
void * map();
basic_allocation(const basic_allocation&) = delete;
basic_allocation(basic_allocation&& other) :
allocation(other.allocation),
resource(std::move(other.resource)),
mapped(other.mapped)
{
other.allocation = nullptr;
other.mapped = nullptr;
}
const basic_allocation& operator=(const basic_allocation&) = delete;
const basic_allocation& operator=(basic_allocation&& other)
{
std::swap(allocation, other.allocation);
std::swap(resource, other.resource);
std::swap(mapped, other.mapped);
return *this;
}
~basic_allocation()
{
traits::destroy(resource, allocation, mapped);
}
void * map()
{
if (mapped)
return mapped;
mapped = traits::map(allocation);
return mapped;
}
template<typename U>
U * data()
{
return reinterpret_cast<U*>(map());
}
};
class image_allocation
{
VmaAllocation allocation = nullptr;
vk::raii::Image image = nullptr;
void * data = nullptr;
public:
operator VkImage() const
{
return *image;
}
operator vk::Image() const
{
return *image;
}
operator const vk::raii::Image&() const
{
return image;
}
operator vk::raii::Image&()
{
return image;
}
operator VmaAllocation() const
{
return allocation;
}
vk::raii::Image* operator->()
{
return &image;
}
const vk::raii::Image* operator->() const
{
return &image;
}
image_allocation() = default;
image_allocation(const vk::ImageCreateInfo& image_info, const VmaAllocationCreateInfo& alloc_info);
image_allocation(const image_allocation&) = delete;
image_allocation(image_allocation&&);
const image_allocation& operator=(const image_allocation&) = delete;
const image_allocation& operator=(image_allocation&&);
~image_allocation();
void * map();
};
using buffer_allocation = basic_allocation<vk::Buffer>;
using image_allocation = basic_allocation<vk::Image>;