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
+1 -1
View File
@@ -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,
+1 -1
View File
@@ -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 = {
+58 -56
View File
@@ -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 <algorithm>
#include <chrono>
#include <mutex>
@@ -273,17 +268,21 @@ void scenes::stream::render()
std::vector<vk::ImageMemoryBarrier> 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<vk::Image> 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);
+1 -1
View File
@@ -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),
};
+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>;