diff --git a/client/render/text_rasterizer.cpp b/client/render/text_rasterizer.cpp index d6c1a2d0..d87d2b60 100644 --- a/client/render/text_rasterizer.cpp +++ b/client/render/text_rasterizer.cpp @@ -312,7 +312,7 @@ text text_rasterizer::render(std::string_view s) .dstAccessMask = vk::AccessFlagBits::eTransferWrite, .oldLayout = vk::ImageLayout::eUndefined, .newLayout = vk::ImageLayout::eTransferDstOptimal, - .image = vk::Image{return_value.image}, + .image = return_value.image, .subresourceRange = { .aspectMask = vk::ImageAspectFlagBits::eColor, .baseMipLevel = 0, diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index 4ca6cdf7..17217cda 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -226,7 +226,7 @@ void scenes::lobby::rasterize_status_string() status_string_rasterized_text = status_string_rasterizer.render(status_string); vk::ImageViewCreateInfo iv_info{ - .image = (VkImage)status_string_rasterized_text.image, + .image = status_string_rasterized_text.image, .viewType = vk::ImageViewType::e2D, .format = status_string_rasterized_text.format, .components = { diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index c589de9f..3c222117 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -26,15 +26,10 @@ #include "application.h" #include "decoder/shard_accumulator.h" -#include "magic_enum.hpp" -#include "glm/fwd.hpp" #include "spdlog/spdlog.h" -#include "utils/check.h" #include "utils/ranges.h" #include "utils/sync_queue.h" -#include "vk/device_memory.h" #include "wivrn_packets.h" -#include "xr/details/enumerate.h" #include #include #include @@ -273,17 +268,21 @@ void scenes::stream::render() std::vector image_barriers; for (size_t i = 0; i < decoder_output.size(); i++) { - vk::ImageMemoryBarrier barrier; - barrier.srcAccessMask = vk::AccessFlagBits::eNone; - barrier.dstAccessMask = vk::AccessFlagBits::eShaderRead; - barrier.oldLayout = vk::ImageLayout::eUndefined; - barrier.newLayout = shard_accumulator::framebuffer_expected_layout; - barrier.image = (vk::Image)decoder_output[i].image; - barrier.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor; - barrier.subresourceRange.baseMipLevel = 0; - barrier.subresourceRange.levelCount = 1; - barrier.subresourceRange.baseArrayLayer = 0; - barrier.subresourceRange.layerCount = 1; + vk::ImageMemoryBarrier barrier + { + .srcAccessMask = vk::AccessFlagBits::eNone, + .dstAccessMask = vk::AccessFlagBits::eShaderRead, + .oldLayout = vk::ImageLayout::eUndefined, + .newLayout = shard_accumulator::framebuffer_expected_layout, + .image = vk::Image{decoder_output[i].image}, + .subresourceRange = { + .aspectMask = vk::ImageAspectFlagBits::eColor, + .baseMipLevel = 0, + .levelCount = 1, + .baseArrayLayer = 0, + .layerCount = 1, + }, + }; image_barriers.push_back(barrier); } @@ -328,17 +327,20 @@ void scenes::stream::render() image_barriers.clear(); for (size_t i = 0; i < decoder_output.size(); i++) { - vk::ImageMemoryBarrier barrier; - barrier.srcAccessMask = vk::AccessFlagBits::eNone; - barrier.dstAccessMask = vk::AccessFlagBits::eShaderRead; - barrier.oldLayout = shard_accumulator::framebuffer_expected_layout; - barrier.newLayout = vk::ImageLayout::eShaderReadOnlyOptimal; - barrier.image = (vk::Image)decoder_output[i].image; - barrier.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor; - barrier.subresourceRange.baseMipLevel = 0; - barrier.subresourceRange.levelCount = 1; - barrier.subresourceRange.baseArrayLayer = 0; - barrier.subresourceRange.layerCount = 1; + vk::ImageMemoryBarrier barrier{ + .srcAccessMask = vk::AccessFlagBits::eNone, + .dstAccessMask = vk::AccessFlagBits::eShaderRead, + .oldLayout = shard_accumulator::framebuffer_expected_layout, + .newLayout = vk::ImageLayout::eShaderReadOnlyOptimal, + .image = vk::Image{decoder_output[i].image}, + .subresourceRange = { + .aspectMask = vk::ImageAspectFlagBits::eColor, + .baseMipLevel = 0, + .levelCount = 1, + .baseArrayLayer = 0, + .layerCount = 1, + }, + }; image_barriers.push_back(barrier); } @@ -436,45 +438,45 @@ void scenes::stream::setup(const to_headset::video_stream_description & descript const uint32_t width = description.width / view_count; const uint32_t height = description.height; - VkExtent3D decoder_out_size{width, height, 1}; + vk::Extent3D decoder_out_size{width, height, 1}; for (size_t i = 0; i < view_count; i++) { decoder_output[i].format = vk::Format::eA8B8G8R8SrgbPack32; decoder_output[i].size.width = width; decoder_output[i].size.height = height; - vk::ImageCreateInfo image_info; - image_info.flags = vk::ImageCreateFlags{}; - image_info.imageType = vk::ImageType::e2D; - image_info.format = vk::Format::eA8B8G8R8SrgbPack32; - image_info.extent = decoder_out_size; - image_info.mipLevels = 1; - image_info.arrayLayers = 1; - image_info.samples = vk::SampleCountFlagBits::e1; - image_info.tiling = vk::ImageTiling::eOptimal; - image_info.usage = vk::ImageUsageFlagBits::eSampled | shard_accumulator::framebuffer_usage; - image_info.sharingMode = vk::SharingMode::eExclusive; - image_info.initialLayout = vk::ImageLayout::eUndefined; + vk::ImageCreateInfo image_info{ + .flags = vk::ImageCreateFlags{}, + .imageType = vk::ImageType::e2D, + .format = vk::Format::eA8B8G8R8SrgbPack32, + .extent = decoder_out_size, + .mipLevels = 1, + .arrayLayers = 1, + .samples = vk::SampleCountFlagBits::e1, + .tiling = vk::ImageTiling::eOptimal, + .usage = vk::ImageUsageFlagBits::eSampled | shard_accumulator::framebuffer_usage, + .sharingMode = vk::SharingMode::eExclusive, + .initialLayout = vk::ImageLayout::eUndefined, + }; VmaAllocationCreateInfo alloc_info{}; alloc_info.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; decoder_output[i].image = image_allocation{image_info, alloc_info}; - vk::ImageViewCreateInfo image_view_info; - - image_view_info.image = (VkImage)decoder_output[i].image; - image_view_info.viewType = vk::ImageViewType::e2D; - image_view_info.format = vk::Format::eA8B8G8R8SrgbPack32; - image_view_info.components.r = vk::ComponentSwizzle::eIdentity; - image_view_info.components.g = vk::ComponentSwizzle::eIdentity; - image_view_info.components.b = vk::ComponentSwizzle::eIdentity; - image_view_info.components.a = vk::ComponentSwizzle::eIdentity; - image_view_info.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor; - image_view_info.subresourceRange.baseMipLevel = 0; - image_view_info.subresourceRange.levelCount = 1; - image_view_info.subresourceRange.baseArrayLayer = 0; - image_view_info.subresourceRange.layerCount = 1; + vk::ImageViewCreateInfo image_view_info{ + .image = vk::Image{decoder_output[i].image}, + .viewType = vk::ImageViewType::e2D, + .format = vk::Format::eA8B8G8R8SrgbPack32, + .components = {}, + .subresourceRange = { + .aspectMask = vk::ImageAspectFlagBits::eColor, + .baseMipLevel = 0, + .levelCount = 1, + .baseArrayLayer = 0, + .layerCount = 1, + }, + }; decoder_output[i].image_view = vk::raii::ImageView(device, image_view_info); } @@ -484,7 +486,7 @@ void scenes::stream::setup(const to_headset::video_stream_description & descript for (size_t i = 0; i < view_count; i++) { - blit_targets[i].image = (VkImage)decoder_output[i].image; + blit_targets[i].image = vk::Image{decoder_output[i].image}; blit_targets[i].image_view = *decoder_output[i].image_view; blit_targets[i].extent.width = width; blit_targets[i].extent.height = height; @@ -515,7 +517,7 @@ void scenes::stream::setup(const to_headset::video_stream_description & descript std::vector images; for (renderpass_output & i: decoder_output) { - images.push_back((vk::Image)i.image); + images.push_back(i.image); } reprojector.emplace(device, physical_device, images, swapchain_images, extent, swapchains[0].format(), description); diff --git a/client/scenes/stream_reprojection.cpp b/client/scenes/stream_reprojection.cpp index f2ff0130..66004b0d 100644 --- a/client/scenes/stream_reprojection.cpp +++ b/client/scenes/stream_reprojection.cpp @@ -168,7 +168,7 @@ stream_reprojection::stream_reprojection(vk::raii::Device& device, vk::raii::Phy }; vk::DescriptorBufferInfo buffer_info{ - .buffer = (vk::Buffer)buffer, + .buffer = buffer, .offset = offset, .range = sizeof(uniform), }; diff --git a/client/vk/allocation.cpp b/client/vk/allocation.cpp index a981641c..817a060c 100644 --- a/client/vk/allocation.cpp +++ b/client/vk/allocation.cpp @@ -20,97 +20,82 @@ #include "allocation.h" #include "application.h" +#include "spdlog/spdlog.h" #include "utils/check.h" +#include "vulkan/vulkan_raii.hpp" #include #include -buffer_allocation::buffer_allocation(const vk::BufferCreateInfo & buffer_info, const VmaAllocationCreateInfo & alloc_info) +std::pair basic_allocation_traits::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(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{device, tmp}, allocation}; + } -buffer_allocation::~buffer_allocation() +void basic_allocation_traits::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::map(VmaAllocation allocation) { VmaAllocator allocator = application::get_allocator(); + + void * mapped; + CHECK_VK(vmaMapMemory(allocator, allocation, &mapped)); + return mapped; +} + +std::pair basic_allocation_traits::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(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{device, tmp}, allocation}; } -image_allocation::~image_allocation() +void basic_allocation_traits::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::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; } diff --git a/client/vk/allocation.h b/client/vk/allocation.h index 73406447..68e9c153 100644 --- a/client/vk/allocation.h +++ b/client/vk/allocation.h @@ -22,31 +22,73 @@ #include "vk_mem_alloc.h" #include -class buffer_allocation +template +struct basic_allocation_traits { +}; + +template<> +struct basic_allocation_traits +{ + using CreateInfo = vk::BufferCreateInfo; + using NativeCreateInfo = CreateInfo::NativeType; + using RaiiType = vk::raii::Buffer; + + static std::pair 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 +{ + using CreateInfo = vk::ImageCreateInfo; + using NativeCreateInfo = CreateInfo::NativeType; + using RaiiType = vk::raii::Image; + + static std::pair create( + const CreateInfo& image_info, + const VmaAllocationCreateInfo* alloc_info); + + static void destroy( + RaiiType& image, + VmaAllocation allocation, + void * mapped); + + static void * map(VmaAllocation allocation); +}; + +template +class basic_allocation +{ +public: + using CType = T::CType; + using traits = basic_allocation_traits; + 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 + U * data() + { + return reinterpret_cast(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 ℑ - } - - const vk::raii::Image* operator->() const - { - return ℑ - } - - 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; +using image_allocation = basic_allocation;