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
+2 -9
View File
@@ -569,12 +569,11 @@ void application::initialize_vulkan()
pipeline_cache = vk::raii::PipelineCache(vk_device, pipeline_cache_info);
VmaAllocatorCreateInfo info{
allocator.emplace(VmaAllocatorCreateInfo{
.physicalDevice = *vk_physical_device,
.device = *vk_device,
.instance = *vk_instance,
};
CHECK_VK(vmaCreateAllocator(&info, &allocator));
});
}
void application::log_views()
@@ -997,12 +996,6 @@ void application::cleanup()
i->spaces_by_name.clear();
}
if (allocator)
{
vmaDestroyAllocator(allocator);
allocator = nullptr;
}
#ifdef __ANDROID__
jni::jni_thread::detach();
#endif
+4 -10
View File
@@ -34,7 +34,7 @@
#include <openxr/openxr.h>
#include <openxr/openxr_platform.h>
#include <openxr/openxr_reflection.h>
#include "vk_mem_alloc.h"
#include "vk/vk_allocator.h"
#include "utils/singleton.h"
#include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp>
@@ -90,6 +90,9 @@ class application : public singleton<application>
vk::raii::CommandPool vk_cmdpool = nullptr;
vk::raii::PipelineCache pipeline_cache = nullptr;
// Vulkan memory allocator stuff
std::optional<vk_allocator> allocator;
// OpenXR stuff
void initialize_actions();
@@ -107,10 +110,6 @@ class application : public singleton<application>
xr::space right_grip_space;
xr::space right_aim_space;
// Vulkan memory allocator stuff
VmaAllocator allocator;
bool session_running = false;
bool session_focused = false;
bool session_visible = false;
@@ -331,11 +330,6 @@ public:
return server_address;
}
static VmaAllocator get_allocator()
{
return instance().allocator;
}
static vk::raii::PhysicalDevice& get_physical_device()
{
return instance().vk_physical_device;
+1 -1
View File
@@ -105,7 +105,7 @@ decoder::decoder(
.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
};
decoded_images[i].image = image_allocation(image_info, alloc_info);
decoded_images[i].image = image_allocation(device, image_info, alloc_info);
decoded_images[i].image.map();
+2
View File
@@ -22,6 +22,7 @@
#include <vulkan/vulkan.hpp>
#include <fastgltf/types.hpp>
#include <fastgltf/tools.hpp>
#include "application.h"
#include "vk/allocation.h"
#include "utils/alignment.h"
@@ -117,6 +118,7 @@ public:
buffer_allocation copy_to_gpu()
{
buffer_allocation gpu_buffer{
application::get_device(),
vk::BufferCreateInfo{
.size = bytes.size(),
.usage = usage},
+4 -1
View File
@@ -154,19 +154,22 @@ void image_loader::do_load(vk::raii::Device & device, vk::raii::CommandBuffer &
// Copy to staging buffer
staging_buffer = buffer_allocation{
device,
vk::BufferCreateInfo{
.size = byte_size,
.usage = vk::BufferUsageFlagBits::eTransferSrc},
VmaAllocationCreateInfo{
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
.usage = VMA_MEMORY_USAGE_AUTO,
}, "image_loader::do_load (staging)"};
},
"image_loader::do_load (staging)"};
memcpy(staging_buffer.map(), pixels, byte_size);
staging_buffer.unmap();
// Allocate image
image = image_allocation{
device,
vk::ImageCreateInfo{
.imageType = vk::ImageType::e2D,
.format = format,
+12 -10
View File
@@ -139,6 +139,7 @@ std::shared_ptr<scene_data::material> scene_renderer::create_default_material(vk
device.waitForFences(*fence, true, 1'000'000'000); // TODO check for timeout
default_material->buffer = std::make_shared<buffer_allocation>(
device,
vk::BufferCreateInfo{
.size = sizeof(default_material->staging),
.usage = vk::BufferUsageFlagBits::eUniformBuffer
@@ -371,7 +372,8 @@ scene_renderer::output_image scene_renderer::create_output_image_data(vk::Image
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
});
out.depth_buffer = image_allocation{vk::ImageCreateInfo{
out.depth_buffer = image_allocation{device,
vk::ImageCreateInfo{
.imageType = vk::ImageType::e2D,
.format = depth_format,
.extent = {
@@ -388,7 +390,7 @@ scene_renderer::output_image scene_renderer::create_output_image_data(vk::Image
},
VmaAllocationCreateInfo{
.flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT,
.usage = VMA_MEMORY_USAGE_AUTO,
.usage = VMA_MEMORY_USAGE_AUTO,
.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, // TODO: check
}};
@@ -579,14 +581,14 @@ void scene_renderer::start_frame()
f.staging_buffer_offset = 0;
if (!f.staging_buffer)
{
f.staging_buffer = buffer_allocation{vk::BufferCreateInfo{
.size = 1048576,
.usage = vk::BufferUsageFlagBits::eUniformBuffer},
VmaAllocationCreateInfo{
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE},
"scene_renderer::render (UBO staging)"
};
f.staging_buffer = buffer_allocation{device,
vk::BufferCreateInfo{
.size = 1048576,
.usage = vk::BufferUsageFlagBits::eUniformBuffer},
VmaAllocationCreateInfo{
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE},
"scene_renderer::render (UBO staging)"};
}
}
+4 -2
View File
@@ -140,7 +140,8 @@ text_rasterizer::text_rasterizer(vk::raii::Device & device, vk::raii::PhysicalDe
image_allocation text_rasterizer::create_image(vk::Extent2D size)
{
image_allocation alloc{vk::ImageCreateInfo{
image_allocation alloc{device,
vk::ImageCreateInfo{
.imageType = vk::ImageType::e2D,
.format = text::format,
.extent = {
@@ -167,7 +168,8 @@ image_allocation text_rasterizer::create_image(vk::Extent2D size)
buffer_allocation text_rasterizer::create_buffer(size_t size)
{
buffer_allocation alloc{vk::BufferCreateInfo{
buffer_allocation alloc{device,
vk::BufferCreateInfo{
.size = size,
.usage = vk::BufferUsageFlagBits::eTransferSrc,
},
+3 -2
View File
@@ -17,11 +17,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "application.h"
#include "imgui.h"
#include "imgui_internal.h"
#include "lobby.h"
#include "wivrn_packets.h"
#include "../common/version.h"
#include "version.h"
#include <spdlog/fmt/fmt.h>
#include <utils/strings.h>
#include "../external/IconsFontAwesome6.h"
+1 -1
View File
@@ -517,7 +517,7 @@ void scenes::stream::setup(const to_headset::video_stream_description & descript
.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
};
decoder_output[i].image = image_allocation{image_info, alloc_info};
decoder_output[i].image = image_allocation{device, image_info, alloc_info};
vk::ImageViewCreateInfo image_view_info{
.image = vk::Image{decoder_output[i].image},
+1 -1
View File
@@ -83,7 +83,7 @@ stream_reprojection::stream_reprojection(vk::raii::Device& device, vk::raii::Phy
.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
};
buffer = buffer_allocation(create_info, alloc_info);
buffer = buffer_allocation(device, create_info, alloc_info);
void * data = buffer.map();
for (size_t i = 0; i < input_images.size(); i++)
ubo.push_back(reinterpret_cast<uniform *>(reinterpret_cast<uintptr_t>(data) + i * uniform_size));
+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;
}
};
+3 -1
View File
@@ -20,7 +20,6 @@
#pragma once
#include <cassert>
#include <concepts>
template<typename T>
class singleton
@@ -42,6 +41,9 @@ protected:
instance_ = nullptr;
}
singleton(const singleton&) = delete;
singleton& operator=(const singleton&) = delete;
public:
static T & instance()
{