diff --git a/client/application.cpp b/client/application.cpp index 09b27aba..9ce651eb 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -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 diff --git a/client/application.h b/client/application.h index dc64c00e..6e9d59eb 100644 --- a/client/application.h +++ b/client/application.h @@ -34,7 +34,7 @@ #include #include #include -#include "vk_mem_alloc.h" +#include "vk/vk_allocator.h" #include "utils/singleton.h" #include #include @@ -90,6 +90,9 @@ class application : public singleton vk::raii::CommandPool vk_cmdpool = nullptr; vk::raii::PipelineCache pipeline_cache = nullptr; + // Vulkan memory allocator stuff + std::optional allocator; + // OpenXR stuff void initialize_actions(); @@ -107,10 +110,6 @@ class application : public singleton 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; diff --git a/client/decoder/ffmpeg/ffmpeg_decoder.cpp b/client/decoder/ffmpeg/ffmpeg_decoder.cpp index ec740506..9aa9b623 100644 --- a/client/decoder/ffmpeg/ffmpeg_decoder.cpp +++ b/client/decoder/ffmpeg/ffmpeg_decoder.cpp @@ -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(); diff --git a/client/render/gpu_buffer.h b/client/render/gpu_buffer.h index 3a754cf9..a536dba4 100644 --- a/client/render/gpu_buffer.h +++ b/client/render/gpu_buffer.h @@ -22,6 +22,7 @@ #include #include #include +#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}, diff --git a/client/render/image_loader.cpp b/client/render/image_loader.cpp index b68f9c0d..ffe2b2ff 100644 --- a/client/render/image_loader.cpp +++ b/client/render/image_loader.cpp @@ -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, diff --git a/client/render/scene_renderer.cpp b/client/render/scene_renderer.cpp index 24ec6e12..49828020 100644 --- a/client/render/scene_renderer.cpp +++ b/client/render/scene_renderer.cpp @@ -139,6 +139,7 @@ std::shared_ptr scene_renderer::create_default_material(vk device.waitForFences(*fence, true, 1'000'000'000); // TODO check for timeout default_material->buffer = std::make_shared( + 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)"}; } } diff --git a/client/render/text_rasterizer.cpp b/client/render/text_rasterizer.cpp index 23ecf224..798ebab6 100644 --- a/client/render/text_rasterizer.cpp +++ b/client/render/text_rasterizer.cpp @@ -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, }, diff --git a/client/scenes/lobby_gui.cpp b/client/scenes/lobby_gui.cpp index 49e167a0..d5d12d16 100644 --- a/client/scenes/lobby_gui.cpp +++ b/client/scenes/lobby_gui.cpp @@ -17,11 +17,12 @@ * along with this program. If not, see . */ +#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 #include #include "../external/IconsFontAwesome6.h" diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index b58528e9..3a495a4b 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -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}, diff --git a/client/scenes/stream_reprojection.cpp b/client/scenes/stream_reprojection.cpp index d13049f5..4353fb02 100644 --- a/client/scenes/stream_reprojection.cpp +++ b/client/scenes/stream_reprojection.cpp @@ -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(reinterpret_cast(data) + i * uniform_size)); diff --git a/client/vk/allocation.cpp b/client/vk/allocation.cpp index 96d22ceb..2227aacb 100644 --- a/client/vk/allocation.cpp +++ b/client/vk/allocation.cpp @@ -18,7 +18,6 @@ */ #include "allocation.h" -#include "application.h" #include "utils/check.h" @@ -27,27 +26,26 @@ #include std::pair basic_allocation_traits::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{device, tmp}, allocation}; - } void basic_allocation_traits::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::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 basic_allocation_traits::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{device, tmp}, allocation}; } void basic_allocation_traits::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); diff --git a/client/vk/allocation.h b/client/vk/allocation.h index 5e0ecebc..48cf1d17 100644 --- a/client/vk/allocation.h +++ b/client/vk/allocation.h @@ -19,11 +19,11 @@ #pragma once +#include "vk_allocator.h" #include "vk_mem_alloc.h" #include -#include "application.h" -template +template struct basic_allocation_traits { }; @@ -34,7 +34,7 @@ struct basic_allocation_traits_base static void unmap(VmaAllocation allocation); }; -template<> +template <> struct basic_allocation_traits : basic_allocation_traits_base { using CreateInfo = vk::BufferCreateInfo; @@ -42,16 +42,17 @@ struct basic_allocation_traits : basic_allocation_traits_base using RaiiType = vk::raii::Buffer; static std::pair 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 : basic_allocation_traits_base { using CreateInfo = vk::ImageCreateInfo; @@ -59,16 +60,17 @@ struct basic_allocation_traits : basic_allocation_traits_base using RaiiType = vk::raii::Image; static std::pair 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 +template 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 + template U * data() { - return reinterpret_cast(map()); + return reinterpret_cast(map()); } - const CreateInfo& info() const + const CreateInfo & info() const { return create_info; } }; - using buffer_allocation = basic_allocation; using image_allocation = basic_allocation; diff --git a/client/vk/vk_allocator.cpp b/client/vk/vk_allocator.cpp new file mode 100644 index 00000000..1c50fba2 --- /dev/null +++ b/client/vk/vk_allocator.cpp @@ -0,0 +1,32 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * Copyright (C) 2024 Patrick Nicolas + * + * 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 . + */ + +#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); +} diff --git a/client/vk/vk_allocator.h b/client/vk/vk_allocator.h new file mode 100644 index 00000000..182ca114 --- /dev/null +++ b/client/vk/vk_allocator.h @@ -0,0 +1,37 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * Copyright (C) 2024 Patrick Nicolas + * + * 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 . + */ + +#pragma once + +#include "utils/singleton.h" +#include "vk_mem_alloc.h" + +class vk_allocator : public singleton +{ + VmaAllocator handle = nullptr; + +public: + vk_allocator(const VmaAllocatorCreateInfo &); + ~vk_allocator(); + + operator VmaAllocator() + { + return handle; + } +}; diff --git a/common/utils/singleton.h b/common/utils/singleton.h index 18899f5d..6890fca7 100644 --- a/common/utils/singleton.h +++ b/common/utils/singleton.h @@ -20,7 +20,6 @@ #pragma once #include -#include template class singleton @@ -42,6 +41,9 @@ protected: instance_ = nullptr; } + singleton(const singleton&) = delete; + singleton& operator=(const singleton&) = delete; + public: static T & instance() {