Make vk_allocator a singleton

This commit is contained in:
Patrick Nicolas
2024-02-07 18:25:54 +01:00
parent 595d27f8dc
commit df394ec079
15 changed files with 163 additions and 96 deletions
+20 -22
View File
@@ -18,7 +18,6 @@
*/
#include "allocation.h"
#include "application.h"
#include "utils/check.h"
@@ -27,27 +26,26 @@
#include <vk_mem_alloc.h>
std::pair<vk::raii::Buffer, VmaAllocation> basic_allocation_traits<VkBuffer>::create(
const CreateInfo& buffer_info,
const VmaAllocationCreateInfo& alloc_info)
vk::raii::Device & device,
const CreateInfo & buffer_info,
const VmaAllocationCreateInfo & alloc_info)
{
VmaAllocator allocator = application::get_allocator();
vk::raii::Device& device = application::get_device();
VmaAllocator allocator = vk_allocator::instance();
VmaAllocation allocation;
VkBuffer tmp;
CHECK_VK(vmaCreateBuffer(allocator, &(NativeCreateInfo&)buffer_info, &alloc_info, &tmp, &allocation, nullptr));
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};
}
void basic_allocation_traits<VkBuffer>::destroy(
vk::raii::Buffer& buffer,
VmaAllocation allocation,
void * mapped)
vk::raii::Buffer & buffer,
VmaAllocation allocation,
void * mapped)
{
VmaAllocator allocator = application::get_allocator();
VmaAllocator allocator = vk_allocator::instance();
if (mapped)
vmaUnmapMemory(allocator, allocation);
@@ -57,7 +55,7 @@ void basic_allocation_traits<VkBuffer>::destroy(
void * basic_allocation_traits_base::map(VmaAllocation allocation)
{
VmaAllocator allocator = application::get_allocator();
VmaAllocator allocator = vk_allocator::instance();
void * mapped;
CHECK_VK(vmaMapMemory(allocator, allocation, &mapped));
@@ -66,32 +64,32 @@ void * basic_allocation_traits_base::map(VmaAllocation allocation)
void basic_allocation_traits_base::unmap(VmaAllocation allocation)
{
VmaAllocator allocator = application::get_allocator();
VmaAllocator allocator = vk_allocator::instance();
vmaUnmapMemory(allocator, allocation);
}
std::pair<vk::raii::Image, VmaAllocation> basic_allocation_traits<VkImage>::create(
const CreateInfo& image_info,
const VmaAllocationCreateInfo& alloc_info)
vk::raii::Device & device,
const CreateInfo & image_info,
const VmaAllocationCreateInfo & alloc_info)
{
VmaAllocator allocator = application::get_allocator();
vk::raii::Device& device = application::get_device();
VmaAllocator allocator = vk_allocator::instance();
VmaAllocation allocation;
VkImage tmp;
CHECK_VK(vmaCreateImage(allocator, &(NativeCreateInfo&)image_info, &alloc_info, &tmp, &allocation, nullptr));
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};
}
void basic_allocation_traits<VkImage>::destroy(
vk::raii::Image& image,
VmaAllocation allocation,
void * mapped)
vk::raii::Image & image,
VmaAllocation allocation,
void * mapped)
{
VmaAllocator allocator = application::get_allocator();
VmaAllocator allocator = vk_allocator::instance();
if (mapped)
vmaUnmapMemory(allocator, allocation);
+37 -36
View File
@@ -19,11 +19,11 @@
#pragma once
#include "vk_allocator.h"
#include "vk_mem_alloc.h"
#include <vulkan/vulkan_raii.hpp>
#include "application.h"
template<typename T>
template <typename T>
struct basic_allocation_traits
{
};
@@ -34,7 +34,7 @@ struct basic_allocation_traits_base
static void unmap(VmaAllocation allocation);
};
template<>
template <>
struct basic_allocation_traits<VkBuffer> : basic_allocation_traits_base
{
using CreateInfo = vk::BufferCreateInfo;
@@ -42,16 +42,17 @@ struct basic_allocation_traits<VkBuffer> : basic_allocation_traits_base
using RaiiType = vk::raii::Buffer;
static std::pair<RaiiType, VmaAllocation> create(
const CreateInfo& buffer_info,
const VmaAllocationCreateInfo& alloc_info);
vk::raii::Device & device,
const CreateInfo & buffer_info,
const VmaAllocationCreateInfo & alloc_info);
static void destroy(
RaiiType& buffer,
VmaAllocation allocation,
void * mapped);
RaiiType & buffer,
VmaAllocation allocation,
void * mapped);
};
template<>
template <>
struct basic_allocation_traits<VkImage> : basic_allocation_traits_base
{
using CreateInfo = vk::ImageCreateInfo;
@@ -59,16 +60,17 @@ struct basic_allocation_traits<VkImage> : basic_allocation_traits_base
using RaiiType = vk::raii::Image;
static std::pair<RaiiType, VmaAllocation> create(
const CreateInfo& image_info,
const VmaAllocationCreateInfo& alloc_info);
vk::raii::Device & device,
const CreateInfo & image_info,
const VmaAllocationCreateInfo & alloc_info);
static void destroy(
RaiiType& image,
VmaAllocation allocation,
void * mapped);
RaiiType & image,
VmaAllocation allocation,
void * mapped);
};
template<typename T>
template <typename T>
class basic_allocation
{
public:
@@ -105,44 +107,44 @@ public:
return allocation;
}
RaiiType* operator->()
RaiiType * operator->()
{
return &resource;
}
const T* operator->() const
const T * operator->() const
{
return &resource;
}
basic_allocation() = default;
basic_allocation(const CreateInfo& create_info, const VmaAllocationCreateInfo& alloc_info) :
create_info(create_info)
basic_allocation(vk::raii::Device & device, const CreateInfo & create_info, const VmaAllocationCreateInfo & alloc_info) :
create_info(create_info)
{
std::tie(resource, allocation) = traits::create(create_info, alloc_info);
std::tie(resource, allocation) = traits::create(device, create_info, alloc_info);
}
basic_allocation(const CreateInfo& create_info, VmaAllocationCreateInfo alloc_info, const std::string& name) :
create_info(create_info)
basic_allocation(vk::raii::Device & device, const CreateInfo & create_info, VmaAllocationCreateInfo alloc_info, const std::string & name) :
create_info(create_info)
{
std::tie(resource, allocation) = traits::create(create_info, alloc_info);
std::tie(resource, allocation) = traits::create(device, create_info, alloc_info);
vmaSetAllocationName(application::get_allocator(), allocation, name.c_str());
vmaSetAllocationName(vk_allocator::instance(), allocation, name.c_str());
}
basic_allocation(const basic_allocation&) = delete;
basic_allocation(basic_allocation&& other) :
allocation(other.allocation),
resource(std::move(other.resource)),
mapped(other.mapped),
create_info(other.create_info)
basic_allocation(const basic_allocation &) = delete;
basic_allocation(basic_allocation && other) :
allocation(other.allocation),
resource(std::move(other.resource)),
mapped(other.mapped),
create_info(other.create_info)
{
other.allocation = nullptr;
other.mapped = nullptr;
}
const basic_allocation& operator=(const basic_allocation&) = delete;
const basic_allocation& operator=(basic_allocation&& other)
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);
@@ -175,18 +177,17 @@ public:
mapped = nullptr;
}
template<typename U>
template <typename U>
U * data()
{
return reinterpret_cast<U*>(map());
return reinterpret_cast<U *>(map());
}
const CreateInfo& info() const
const CreateInfo & info() const
{
return create_info;
}
};
using buffer_allocation = basic_allocation<vk::Buffer>;
using image_allocation = basic_allocation<vk::Image>;
+32
View File
@@ -0,0 +1,32 @@
/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "vk_allocator.h"
#include "utils/check.h"
vk_allocator::vk_allocator(const VmaAllocatorCreateInfo & info)
{
CHECK_VK(vmaCreateAllocator(&info, &handle));
}
vk_allocator::~vk_allocator()
{
if (handle)
vmaDestroyAllocator(handle);
}
+37
View File
@@ -0,0 +1,37 @@
/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "utils/singleton.h"
#include "vk_mem_alloc.h"
class vk_allocator : public singleton<vk_allocator>
{
VmaAllocator handle = nullptr;
public:
vk_allocator(const VmaAllocatorCreateInfo &);
~vk_allocator();
operator VmaAllocator()
{
return handle;
}
};