From ac7562385571b80d4c25442384cae6a971a63916 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Wed, 12 Jul 2023 19:26:07 -0700 Subject: [PATCH 1/2] [Impeller] Add RAII wrappers for VMA objects. Uses `fml::UniqueObject`. --- ci/licenses_golden/licenses_flutter | 2 + impeller/renderer/backend/vulkan/BUILD.gn | 2 + .../renderer/backend/vulkan/allocator_vk.cc | 217 ++++++++---------- .../renderer/backend/vulkan/allocator_vk.h | 7 +- .../backend/vulkan/device_buffer_vk.cc | 28 +-- .../backend/vulkan/device_buffer_vk.h | 35 +-- impeller/renderer/backend/vulkan/formats_vk.h | 1 - .../backend/vulkan/pipeline_library_vk.h | 1 - .../renderer/backend/vulkan/render_pass_vk.cc | 2 - impeller/renderer/backend/vulkan/sampler_vk.h | 1 - impeller/renderer/backend/vulkan/vk.h | 2 - impeller/renderer/backend/vulkan/vma.cc | 11 + impeller/renderer/backend/vulkan/vma.h | 132 +++++++++++ 13 files changed, 262 insertions(+), 179 deletions(-) create mode 100644 impeller/renderer/backend/vulkan/vma.cc create mode 100644 impeller/renderer/backend/vulkan/vma.h diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index c612f38ea0eed..951a3dfe551ca 100644 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -4250,6 +4250,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 diff --git a/impeller/renderer/backend/vulkan/BUILD.gn b/impeller/renderer/backend/vulkan/BUILD.gn index bb75156ab63db..4515720b412e9 100644 --- a/impeller/renderer/backend/vulkan/BUILD.gn +++ b/impeller/renderer/backend/vulkan/BUILD.gn @@ -94,6 +94,8 @@ impeller_component("vulkan") { "vertex_descriptor_vk.cc", "vertex_descriptor_vk.h", "vk.h", + "vma.cc", + "vma.h", ] public_deps = [ diff --git a/impeller/renderer/backend/vulkan/allocator_vk.cc b/impeller/renderer/backend/vulkan/allocator_vk.cc index a865ca8ee98b6..345a37bddd9a9 100644 --- a/impeller/renderer/backend/vulkan/allocator_vk.cc +++ b/impeller/renderer/backend/vulkan/allocator_vk.cc @@ -16,6 +16,75 @@ namespace impeller { +static constexpr vk::Flags +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(buffer_info); + + VmaAllocationCreateInfo allocation_info = {}; + allocation_info.usage = VMA_MEMORY_USAGE_AUTO; + allocation_info.preferredFlags = static_cast( + 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, uint32_t vulkan_api_version, const vk::PhysicalDevice& physical_device, @@ -93,26 +162,16 @@ AllocatorVK::AllocatorVK(std::weak_ptr 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 -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(image), // - allocation // - ); - } - } - FML_DISALLOW_COPY_AND_ASSIGN(ImageResource); }; @@ -439,7 +444,7 @@ std::shared_ptr AllocatorVK::OnCreateTexture( auto source = std::make_shared( ContextVK::Cast(*context).GetResourceManager(), // desc, // - allocator_, // + allocator_.get(), // device_holder->GetDevice(), // supports_memoryless_textures_ // ); @@ -477,13 +482,16 @@ std::shared_ptr 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 AllocatorVK::OnCreateBuffer( return {}; } - return std::make_shared(desc, // - context_, // - allocator_, // - buffer_allocation, // - buffer_allocation_info, // - vk::Buffer{buffer} // + return std::make_shared( + 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(buffer_info); - - VmaAllocationCreateInfo allocation_info = {}; - allocation_info.usage = VMA_MEMORY_USAGE_AUTO; - allocation_info.preferredFlags = static_cast( - 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 diff --git a/impeller/renderer/backend/vulkan/allocator_vk.h b/impeller/renderer/backend/vulkan/allocator_vk.h index abb65ba836778..68b08aa708721 100644 --- a/impeller/renderer/backend/vulkan/allocator_vk.h +++ b/impeller/renderer/backend/vulkan/allocator_vk.h @@ -13,6 +13,7 @@ #include "impeller/renderer/backend/vulkan/device_holder.h" #include "impeller/renderer/backend/vulkan/vk.h" +#include #include namespace impeller { @@ -28,8 +29,8 @@ class AllocatorVK final : public Allocator { static constexpr size_t kPoolCount = 3; fml::RefPtr vk_; - VmaAllocator allocator_ = {}; - VmaPool staging_buffer_pools_[kPoolCount] = {}; + UniqueAllocatorVMA allocator_; + std::array staging_buffer_pools_; std::weak_ptr context_; std::weak_ptr 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); }; diff --git a/impeller/renderer/backend/vulkan/device_buffer_vk.cc b/impeller/renderer/backend/vulkan/device_buffer_vk.cc index d113e853bb194..5386ae3c037e8 100644 --- a/impeller/renderer/backend/vulkan/device_buffer_vk.cc +++ b/impeller/renderer/backend/vulkan/device_buffer_vk.cc @@ -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, - 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(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 diff --git a/impeller/renderer/backend/vulkan/device_buffer_vk.h b/impeller/renderer/backend/vulkan/device_buffer_vk.h index bf5c033b482d8..7cc934f72f135 100644 --- a/impeller/renderer/backend/vulkan/device_buffer_vk.h +++ b/impeller/renderer/backend/vulkan/device_buffer_vk.h @@ -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, - 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(buffer), - allocation); + std::swap(o.info, info); } FML_DISALLOW_COPY_AND_ASSIGN(BufferResource); diff --git a/impeller/renderer/backend/vulkan/formats_vk.h b/impeller/renderer/backend/vulkan/formats_vk.h index 2bf4aaedffb1e..634f647f0f533 100644 --- a/impeller/renderer/backend/vulkan/formats_vk.h +++ b/impeller/renderer/backend/vulkan/formats_vk.h @@ -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 { diff --git a/impeller/renderer/backend/vulkan/pipeline_library_vk.h b/impeller/renderer/backend/vulkan/pipeline_library_vk.h index 538a2b9834c62..4db81e06bb2ab 100644 --- a/impeller/renderer/backend/vulkan/pipeline_library_vk.h +++ b/impeller/renderer/backend/vulkan/pipeline_library_vk.h @@ -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 { diff --git a/impeller/renderer/backend/vulkan/render_pass_vk.cc b/impeller/renderer/backend/vulkan/render_pass_vk.cc index 4c2820b7906ee..82d2214a5f5b2 100644 --- a/impeller/renderer/backend/vulkan/render_pass_vk.cc +++ b/impeller/renderer/backend/vulkan/render_pass_vk.cc @@ -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 { diff --git a/impeller/renderer/backend/vulkan/sampler_vk.h b/impeller/renderer/backend/vulkan/sampler_vk.h index f67a64f9e83d6..5286cef2fbab6 100644 --- a/impeller/renderer/backend/vulkan/sampler_vk.h +++ b/impeller/renderer/backend/vulkan/sampler_vk.h @@ -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 { diff --git a/impeller/renderer/backend/vulkan/vk.h b/impeller/renderer/backend/vulkan/vk.h index 3eef9a3cf6fd6..efbf94b840536 100644 --- a/impeller/renderer/backend/vulkan/vk.h +++ b/impeller/renderer/backend/vulkan/vk.h @@ -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" diff --git a/impeller/renderer/backend/vulkan/vma.cc b/impeller/renderer/backend/vulkan/vma.cc new file mode 100644 index 0000000000000..5c28daa885f42 --- /dev/null +++ b/impeller/renderer/backend/vulkan/vma.cc @@ -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 diff --git a/impeller/renderer/backend/vulkan/vma.h b/impeller/renderer/backend/vulkan/vma.h new file mode 100644 index 0000000000000..b3fdaefe27281 --- /dev/null +++ b/impeller/renderer/backend/vulkan/vma.h @@ -0,0 +1,132 @@ +// 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; + +// ----------------------------------------------------------------------------- +// 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; + +// ----------------------------------------------------------------------------- +// 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, buffer.buffer, buffer.allocation); + } +}; + +using UniqueBufferVMA = fml::UniqueObject; + +// ----------------------------------------------------------------------------- +// 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, image.image, image.allocation); + } +}; + +using UniqueImageVMA = fml::UniqueObject; + +} // namespace impeller From f7622904c703c2b8249256385d7f304fe50afe7b Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Wed, 12 Jul 2023 21:41:57 -0700 Subject: [PATCH 2/2] CI fixes. --- ci/licenses_golden/licenses_flutter | 2 ++ impeller/renderer/backend/vulkan/vma.h | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index 951a3dfe551ca..63837aff909f9 100644 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -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 diff --git a/impeller/renderer/backend/vulkan/vma.h b/impeller/renderer/backend/vulkan/vma.h index b3fdaefe27281..f33a396997f01 100644 --- a/impeller/renderer/backend/vulkan/vma.h +++ b/impeller/renderer/backend/vulkan/vma.h @@ -89,7 +89,8 @@ struct BufferVMATraits { static void Free(const BufferVMA& buffer) { TRACE_EVENT0("impeller", "DestroyBuffer"); - ::vmaDestroyBuffer(buffer.allocator, buffer.buffer, buffer.allocation); + ::vmaDestroyBuffer(buffer.allocator, static_cast(buffer.buffer), + buffer.allocation); } }; @@ -123,7 +124,8 @@ struct ImageVMATraits { static void Free(const ImageVMA& image) { TRACE_EVENT0("impeller", "DestroyImage"); - ::vmaDestroyImage(image.allocator, image.image, image.allocation); + ::vmaDestroyImage(image.allocator, static_cast(image.image), + image.allocation); } };