[Impeller] Add RAII wrappers for VMA objects. (flutter/engine#43626)

Uses `fml::UniqueObject<T>`.
This commit is contained in:
Chinmay Garde
2023-07-12 22:18:06 -07:00
committed by GitHub
parent a9fe0fd8ac
commit 88df88c3ff
13 changed files with 266 additions and 179 deletions

View File

@@ -1562,6 +1562,8 @@ ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/texture_vk.h + ../../.
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/vertex_descriptor_vk.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/vertex_descriptor_vk.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/vk.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/vma.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/vma.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/blit_command.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/blit_command.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/blit_pass.cc + ../../../flutter/LICENSE
@@ -4252,6 +4254,8 @@ FILE: ../../../flutter/impeller/renderer/backend/vulkan/texture_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/vertex_descriptor_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/vertex_descriptor_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/vma.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/vma.h
FILE: ../../../flutter/impeller/renderer/blit_command.cc
FILE: ../../../flutter/impeller/renderer/blit_command.h
FILE: ../../../flutter/impeller/renderer/blit_pass.cc

View File

@@ -94,6 +94,8 @@ impeller_component("vulkan") {
"vertex_descriptor_vk.cc",
"vertex_descriptor_vk.h",
"vk.h",
"vma.cc",
"vma.h",
]
public_deps = [

View File

@@ -16,6 +16,75 @@
namespace impeller {
static constexpr vk::Flags<vk::MemoryPropertyFlagBits>
ToVKBufferMemoryPropertyFlags(StorageMode mode) {
switch (mode) {
case StorageMode::kHostVisible:
return vk::MemoryPropertyFlagBits::eHostVisible;
case StorageMode::kDevicePrivate:
return vk::MemoryPropertyFlagBits::eDeviceLocal;
case StorageMode::kDeviceTransient:
return vk::MemoryPropertyFlagBits::eLazilyAllocated;
}
FML_UNREACHABLE();
}
static VmaAllocationCreateFlags ToVmaAllocationBufferCreateFlags(
StorageMode mode) {
VmaAllocationCreateFlags flags = 0;
switch (mode) {
case StorageMode::kHostVisible:
flags |= VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT;
flags |= VMA_ALLOCATION_CREATE_MAPPED_BIT;
return flags;
case StorageMode::kDevicePrivate:
return flags;
case StorageMode::kDeviceTransient:
return flags;
}
FML_UNREACHABLE();
}
static PoolVMA CreateBufferPool(VmaAllocator allocator) {
vk::BufferCreateInfo buffer_info;
buffer_info.usage = vk::BufferUsageFlagBits::eVertexBuffer |
vk::BufferUsageFlagBits::eIndexBuffer |
vk::BufferUsageFlagBits::eUniformBuffer |
vk::BufferUsageFlagBits::eStorageBuffer |
vk::BufferUsageFlagBits::eTransferSrc |
vk::BufferUsageFlagBits::eTransferDst;
buffer_info.size = 1u; // doesn't matter
buffer_info.sharingMode = vk::SharingMode::eExclusive;
auto buffer_info_native =
static_cast<vk::BufferCreateInfo::NativeType>(buffer_info);
VmaAllocationCreateInfo allocation_info = {};
allocation_info.usage = VMA_MEMORY_USAGE_AUTO;
allocation_info.preferredFlags = static_cast<VkMemoryPropertyFlags>(
ToVKBufferMemoryPropertyFlags(StorageMode::kHostVisible));
allocation_info.flags =
ToVmaAllocationBufferCreateFlags(StorageMode::kHostVisible);
uint32_t memTypeIndex;
auto result = vk::Result{vmaFindMemoryTypeIndexForBufferInfo(
allocator, &buffer_info_native, &allocation_info, &memTypeIndex)};
if (result != vk::Result::eSuccess) {
return {};
}
VmaPoolCreateInfo pool_create_info = {};
pool_create_info.memoryTypeIndex = memTypeIndex;
pool_create_info.flags = VMA_POOL_CREATE_IGNORE_BUFFER_IMAGE_GRANULARITY_BIT |
VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT;
VmaPool pool = {};
result = vk::Result{::vmaCreatePool(allocator, &pool_create_info, &pool)};
if (result != vk::Result::eSuccess) {
return {};
}
return {allocator, pool};
}
AllocatorVK::AllocatorVK(std::weak_ptr<Context> context,
uint32_t vulkan_api_version,
const vk::PhysicalDevice& physical_device,
@@ -93,26 +162,16 @@ AllocatorVK::AllocatorVK(std::weak_ptr<Context> context,
VALIDATION_LOG << "Could not create memory allocator";
return;
}
for (auto i = 0u; i < kPoolCount; i++) {
created_buffer_pools_ &=
CreateBufferPool(allocator, &staging_buffer_pools_[i]);
for (size_t i = 0u; i < staging_buffer_pools_.size(); i++) {
staging_buffer_pools_[i].reset(CreateBufferPool(allocator));
created_buffer_pools_ &= staging_buffer_pools_[i].is_valid();
}
allocator_ = allocator;
allocator_.reset(allocator);
supports_memoryless_textures_ = capabilities.SupportsMemorylessTextures();
is_valid_ = true;
}
AllocatorVK::~AllocatorVK() {
TRACE_EVENT0("impeller", "DestroyAllocatorVK");
if (allocator_) {
for (auto i = 0u; i < kPoolCount; i++) {
if (staging_buffer_pools_[i]) {
::vmaDestroyPool(allocator_, staging_buffer_pools_[i]);
}
}
::vmaDestroyAllocator(allocator_);
}
}
AllocatorVK::~AllocatorVK() = default;
// |Allocator|
bool AllocatorVK::IsValid() const {
@@ -206,35 +265,6 @@ ToVKTextureMemoryPropertyFlags(StorageMode mode,
FML_UNREACHABLE();
}
static constexpr vk::Flags<vk::MemoryPropertyFlagBits>
ToVKBufferMemoryPropertyFlags(StorageMode mode) {
switch (mode) {
case StorageMode::kHostVisible:
return vk::MemoryPropertyFlagBits::eHostVisible;
case StorageMode::kDevicePrivate:
return vk::MemoryPropertyFlagBits::eDeviceLocal;
case StorageMode::kDeviceTransient:
return vk::MemoryPropertyFlagBits::eLazilyAllocated;
}
FML_UNREACHABLE();
}
static VmaAllocationCreateFlags ToVmaAllocationBufferCreateFlags(
StorageMode mode) {
VmaAllocationCreateFlags flags = 0;
switch (mode) {
case StorageMode::kHostVisible:
flags |= VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT;
flags |= VMA_ALLOCATION_CREATE_MAPPED_BIT;
return flags;
case StorageMode::kDevicePrivate:
return flags;
case StorageMode::kDeviceTransient:
return flags;
}
FML_UNREACHABLE();
}
static VmaAllocationCreateFlags ToVmaAllocationCreateFlags(StorageMode mode,
bool is_texture,
size_t size) {
@@ -357,8 +387,8 @@ class AllocatedTextureSourceVK final : public TextureSourceVK {
<< vk::to_string(result);
return;
}
resource_.Reset(
ImageResource(image, allocator, allocation, std::move(image_view)));
resource_.Reset(ImageResource(ImageVMA{allocator, allocation, image},
std::move(image_view)));
is_valid_ = true;
}
@@ -366,7 +396,7 @@ class AllocatedTextureSourceVK final : public TextureSourceVK {
bool IsValid() const { return is_valid_; }
vk::Image GetImage() const override { return resource_->image; }
vk::Image GetImage() const override { return resource_->image.get().image; }
vk::ImageView GetImageView() const override {
return resource_->image_view.get();
@@ -374,44 +404,19 @@ class AllocatedTextureSourceVK final : public TextureSourceVK {
private:
struct ImageResource {
vk::Image image = {};
VmaAllocator allocator = {};
VmaAllocation allocation = {};
UniqueImageVMA image;
vk::UniqueImageView image_view;
ImageResource() = default;
ImageResource(vk::Image p_image,
VmaAllocator p_allocator,
VmaAllocation p_allocation,
vk::UniqueImageView p_image_view)
: image(p_image),
allocator(p_allocator),
allocation(p_allocation),
image_view(std::move(p_image_view)) {}
ImageResource(ImageVMA p_image, vk::UniqueImageView p_image_view)
: image(p_image), image_view(std::move(p_image_view)) {}
ImageResource(ImageResource&& o) {
std::swap(image, o.image);
std::swap(allocator, o.allocator);
std::swap(allocation, o.allocation);
std::swap(image_view, o.image_view);
}
~ImageResource() {
if (!image) {
return;
}
TRACE_EVENT0("impeller", "DestroyDeviceTexture");
image_view.reset();
if (image) {
::vmaDestroyImage(
allocator, //
static_cast<typename decltype(image)::NativeType>(image), //
allocation //
);
}
}
FML_DISALLOW_COPY_AND_ASSIGN(ImageResource);
};
@@ -439,7 +444,7 @@ std::shared_ptr<Texture> AllocatorVK::OnCreateTexture(
auto source = std::make_shared<AllocatedTextureSourceVK>(
ContextVK::Cast(*context).GetResourceManager(), //
desc, //
allocator_, //
allocator_.get(), //
device_holder->GetDevice(), //
supports_memoryless_textures_ //
);
@@ -477,13 +482,16 @@ std::shared_ptr<DeviceBuffer> AllocatorVK::OnCreateBuffer(
allocation_info.flags = ToVmaAllocationBufferCreateFlags(desc.storage_mode);
if (created_buffer_pools_ && desc.storage_mode == StorageMode::kHostVisible &&
raster_thread_id_ == std::this_thread::get_id()) {
allocation_info.pool = staging_buffer_pools_[frame_count_ % kPoolCount];
allocation_info.pool =
staging_buffer_pools_[frame_count_ % staging_buffer_pools_.size()]
.get()
.pool;
}
VkBuffer buffer = {};
VmaAllocation buffer_allocation = {};
VmaAllocationInfo buffer_allocation_info = {};
auto result = vk::Result{::vmaCreateBuffer(allocator_, //
auto result = vk::Result{::vmaCreateBuffer(allocator_.get(), //
&buffer_info_native, //
&allocation_info, //
&buffer, //
@@ -497,53 +505,14 @@ std::shared_ptr<DeviceBuffer> AllocatorVK::OnCreateBuffer(
return {};
}
return std::make_shared<DeviceBufferVK>(desc, //
context_, //
allocator_, //
buffer_allocation, //
buffer_allocation_info, //
vk::Buffer{buffer} //
return std::make_shared<DeviceBufferVK>(
desc, //
context_, //
UniqueBufferVMA{BufferVMA{allocator_.get(), //
buffer_allocation, //
vk::Buffer{buffer}}}, //
buffer_allocation_info //
);
}
// static
bool AllocatorVK::CreateBufferPool(VmaAllocator allocator, VmaPool* pool) {
vk::BufferCreateInfo buffer_info;
buffer_info.usage = vk::BufferUsageFlagBits::eVertexBuffer |
vk::BufferUsageFlagBits::eIndexBuffer |
vk::BufferUsageFlagBits::eUniformBuffer |
vk::BufferUsageFlagBits::eStorageBuffer |
vk::BufferUsageFlagBits::eTransferSrc |
vk::BufferUsageFlagBits::eTransferDst;
buffer_info.size = 1u; // doesn't matter
buffer_info.sharingMode = vk::SharingMode::eExclusive;
auto buffer_info_native =
static_cast<vk::BufferCreateInfo::NativeType>(buffer_info);
VmaAllocationCreateInfo allocation_info = {};
allocation_info.usage = VMA_MEMORY_USAGE_AUTO;
allocation_info.preferredFlags = static_cast<VkMemoryPropertyFlags>(
ToVKBufferMemoryPropertyFlags(StorageMode::kHostVisible));
allocation_info.flags =
ToVmaAllocationBufferCreateFlags(StorageMode::kHostVisible);
uint32_t memTypeIndex;
auto result = vk::Result{vmaFindMemoryTypeIndexForBufferInfo(
allocator, &buffer_info_native, &allocation_info, &memTypeIndex)};
if (result != vk::Result::eSuccess) {
return false;
}
VmaPoolCreateInfo pool_create_info = {};
pool_create_info.memoryTypeIndex = memTypeIndex;
pool_create_info.flags = VMA_POOL_CREATE_IGNORE_BUFFER_IMAGE_GRANULARITY_BIT |
VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT;
result = vk::Result{vmaCreatePool(allocator, &pool_create_info, pool)};
if (result != vk::Result::eSuccess) {
return false;
}
return true;
}
} // namespace impeller

View File

@@ -13,6 +13,7 @@
#include "impeller/renderer/backend/vulkan/device_holder.h"
#include "impeller/renderer/backend/vulkan/vk.h"
#include <array>
#include <memory>
namespace impeller {
@@ -28,8 +29,8 @@ class AllocatorVK final : public Allocator {
static constexpr size_t kPoolCount = 3;
fml::RefPtr<vulkan::VulkanProcTable> vk_;
VmaAllocator allocator_ = {};
VmaPool staging_buffer_pools_[kPoolCount] = {};
UniqueAllocatorVMA allocator_;
std::array<UniquePoolVMA, kPoolCount> staging_buffer_pools_;
std::weak_ptr<Context> context_;
std::weak_ptr<DeviceHolder> device_holder_;
ISize max_texture_size_;
@@ -66,8 +67,6 @@ class AllocatorVK final : public Allocator {
// |Allocator|
ISize GetMaxTextureSizeSupported() const override;
static bool CreateBufferPool(VmaAllocator allocator, VmaPool* pool);
FML_DISALLOW_COPY_AND_ASSIGN(AllocatorVK);
};

View File

@@ -6,27 +6,22 @@
#include "flutter/fml/logging.h"
#include "flutter/fml/trace_event.h"
#include "vulkan/vulkan_handles.hpp"
namespace impeller {
DeviceBufferVK::DeviceBufferVK(DeviceBufferDescriptor desc,
std::weak_ptr<Context> context,
VmaAllocator allocator,
VmaAllocation allocation,
VmaAllocationInfo info,
vk::Buffer buffer)
UniqueBufferVMA buffer,
VmaAllocationInfo info)
: DeviceBuffer(desc),
context_(std::move(context)),
resource_(ContextVK::Cast(*context_.lock().get()).GetResourceManager(),
BufferResource{
allocator, //
allocation, //
info, //
buffer //
std::move(buffer), //
info //
}) {}
DeviceBufferVK::~DeviceBufferVK() {}
DeviceBufferVK::~DeviceBufferVK() = default;
uint8_t* DeviceBufferVK::OnGetContents() const {
return static_cast<uint8_t*>(resource_->info.pMappedData);
@@ -51,17 +46,18 @@ bool DeviceBufferVK::OnCopyHostBuffer(const uint8_t* source,
bool DeviceBufferVK::SetLabel(const std::string& label) {
auto context = context_.lock();
if (!context || !resource_->buffer) {
if (!context || !resource_->buffer.is_valid()) {
// The context could have died at this point.
return false;
}
::vmaSetAllocationName(resource_->allocator, //
resource_->allocation, //
label.c_str() //
::vmaSetAllocationName(resource_->buffer.get().allocator, //
resource_->buffer.get().allocation, //
label.c_str() //
);
return ContextVK::Cast(*context).SetDebugName(resource_->buffer, label);
return ContextVK::Cast(*context).SetDebugName(resource_->buffer.get().buffer,
label);
}
bool DeviceBufferVK::SetLabel(const std::string& label, Range range) {
@@ -70,7 +66,7 @@ bool DeviceBufferVK::SetLabel(const std::string& label, Range range) {
}
vk::Buffer DeviceBufferVK::GetBuffer() const {
return resource_->buffer;
return resource_->buffer.get().buffer;
}
} // namespace impeller

View File

@@ -12,6 +12,7 @@
#include "impeller/core/device_buffer.h"
#include "impeller/renderer/backend/vulkan/context_vk.h"
#include "impeller/renderer/backend/vulkan/resource_manager_vk.h"
#include "impeller/renderer/backend/vulkan/vma.h"
namespace impeller {
@@ -20,10 +21,8 @@ class DeviceBufferVK final : public DeviceBuffer,
public:
DeviceBufferVK(DeviceBufferDescriptor desc,
std::weak_ptr<Context> context,
VmaAllocator allocator,
VmaAllocation allocation,
VmaAllocationInfo info,
vk::Buffer buffer);
UniqueBufferVMA buffer,
VmaAllocationInfo info);
// |DeviceBuffer|
~DeviceBufferVK() override;
@@ -34,37 +33,17 @@ class DeviceBufferVK final : public DeviceBuffer,
friend class AllocatorVK;
struct BufferResource {
VmaAllocator allocator = {};
VmaAllocation allocation = {};
UniqueBufferVMA buffer;
VmaAllocationInfo info = {};
vk::Buffer buffer = {};
BufferResource() = default;
BufferResource(VmaAllocator p_allocator,
VmaAllocation p_allocation,
VmaAllocationInfo p_info,
vk::Buffer p_buffer)
: allocator(p_allocator),
allocation(p_allocation),
info(p_info),
buffer(p_buffer) {}
BufferResource(UniqueBufferVMA p_buffer, VmaAllocationInfo p_info)
: buffer(std::move(p_buffer)), info(p_info) {}
BufferResource(BufferResource&& o) {
std::swap(o.allocator, allocator);
std::swap(o.allocation, allocation);
std::swap(o.info, info);
std::swap(o.buffer, buffer);
}
~BufferResource() {
if (!buffer) {
return;
}
TRACE_EVENT0("impeller", "DestroyDeviceBuffer");
::vmaDestroyBuffer(allocator,
static_cast<decltype(buffer)::NativeType>(buffer),
allocation);
std::swap(o.info, info);
}
FML_DISALLOW_COPY_AND_ASSIGN(BufferResource);

View File

@@ -8,7 +8,6 @@
#include "impeller/core/formats.h"
#include "impeller/core/shader_types.h"
#include "impeller/renderer/backend/vulkan/vk.h"
#include "vulkan/vulkan_enums.hpp"
namespace impeller {

View File

@@ -17,7 +17,6 @@
#include "impeller/renderer/backend/vulkan/pipeline_vk.h"
#include "impeller/renderer/backend/vulkan/vk.h"
#include "impeller/renderer/pipeline_library.h"
#include "vulkan/vulkan_handles.hpp"
namespace impeller {

View File

@@ -23,8 +23,6 @@
#include "impeller/renderer/backend/vulkan/sampler_vk.h"
#include "impeller/renderer/backend/vulkan/shared_object_vk.h"
#include "impeller/renderer/backend/vulkan/texture_vk.h"
#include "vulkan/vulkan_enums.hpp"
#include "vulkan/vulkan_structs.hpp"
namespace impeller {

View File

@@ -9,7 +9,6 @@
#include "impeller/core/sampler.h"
#include "impeller/renderer/backend/vulkan/shared_object_vk.h"
#include "impeller/renderer/backend/vulkan/vk.h"
#include "vulkan/vulkan_handles.hpp"
namespace impeller {

View File

@@ -67,5 +67,3 @@
#include "vulkan/vulkan.hpp"
static_assert(VK_HEADER_VERSION >= 215, "Vulkan headers must not be too old.");
#include "flutter/flutter_vma/flutter_vma.h"

View File

@@ -0,0 +1,11 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "impeller/renderer/backend/vulkan/vma.h"
namespace impeller {
//
} // namespace impeller

View File

@@ -0,0 +1,134 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#pragma once
#include "flutter/flutter_vma/flutter_vma.h"
#include "flutter/fml/trace_event.h"
#include "flutter/fml/unique_object.h"
#include "impeller/renderer/backend/vulkan/vk.h"
namespace impeller {
// -----------------------------------------------------------------------------
// Unique handles to VMA allocators.
// -----------------------------------------------------------------------------
struct AllocatorVMATraits {
static VmaAllocator InvalidValue() { return {}; }
static bool IsValid(const VmaAllocator& value) {
return value != InvalidValue();
}
static void Free(VmaAllocator allocator) {
TRACE_EVENT0("impeller", "DestroyAllocator");
::vmaDestroyAllocator(allocator);
}
};
using UniqueAllocatorVMA = fml::UniqueObject<VmaAllocator, AllocatorVMATraits>;
// -----------------------------------------------------------------------------
// Unique handles to VMA pools.
// -----------------------------------------------------------------------------
struct PoolVMA {
VmaAllocator allocator = {};
VmaPool pool = {};
constexpr bool operator==(const PoolVMA& other) const {
return allocator == other.allocator && pool == other.pool;
}
constexpr bool operator!=(const PoolVMA& other) const {
return !(*this == other);
}
};
struct PoolVMATraits {
static PoolVMA InvalidValue() { return {}; }
static bool IsValid(const PoolVMA& value) {
return value.allocator != VmaAllocator{};
}
static void Free(const PoolVMA& pool) {
TRACE_EVENT0("impeller", "DestroyPool");
::vmaDestroyPool(pool.allocator, pool.pool);
}
};
using UniquePoolVMA = fml::UniqueObject<PoolVMA, PoolVMATraits>;
// -----------------------------------------------------------------------------
// Unique handles to VMA buffers.
// -----------------------------------------------------------------------------
struct BufferVMA {
VmaAllocator allocator = {};
VmaAllocation allocation = {};
vk::Buffer buffer = {};
constexpr bool operator==(const BufferVMA& other) const {
return allocator == other.allocator && allocation == other.allocation &&
buffer == other.buffer;
}
constexpr bool operator!=(const BufferVMA& other) const {
return !(*this == other);
}
};
struct BufferVMATraits {
static BufferVMA InvalidValue() { return {}; }
static bool IsValid(const BufferVMA& value) {
return value.allocator != VmaAllocator{};
}
static void Free(const BufferVMA& buffer) {
TRACE_EVENT0("impeller", "DestroyBuffer");
::vmaDestroyBuffer(buffer.allocator, static_cast<VkBuffer>(buffer.buffer),
buffer.allocation);
}
};
using UniqueBufferVMA = fml::UniqueObject<BufferVMA, BufferVMATraits>;
// -----------------------------------------------------------------------------
// Unique handles to VMA images.
// -----------------------------------------------------------------------------
struct ImageVMA {
VmaAllocator allocator = {};
VmaAllocation allocation = {};
vk::Image image = {};
constexpr bool operator==(const ImageVMA& other) const {
return allocator == other.allocator && allocation == other.allocation &&
image == other.image;
}
constexpr bool operator!=(const ImageVMA& other) const {
return !(*this == other);
}
};
struct ImageVMATraits {
static ImageVMA InvalidValue() { return {}; }
static bool IsValid(const ImageVMA& value) {
return value.allocator != VmaAllocator{};
}
static void Free(const ImageVMA& image) {
TRACE_EVENT0("impeller", "DestroyImage");
::vmaDestroyImage(image.allocator, static_cast<VkImage>(image.image),
image.allocation);
}
};
using UniqueImageVMA = fml::UniqueObject<ImageVMA, ImageVMATraits>;
} // namespace impeller