From 8590d305ec01967d99bf7f5627517e77e4d813d9 Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Tue, 11 Jul 2023 10:29:34 -0700 Subject: [PATCH 1/9] [Impeller] allocate buffers out of a pool on the raster thread. --- impeller/core/allocator.cc | 2 + impeller/core/allocator.h | 4 + .../renderer/backend/vulkan/allocator_vk.cc | 78 +++++++++++++++++++ .../renderer/backend/vulkan/allocator_vk.h | 8 ++ .../backend/vulkan/swapchain_impl_vk.cc | 1 + shell/common/snapshot_controller_impeller.cc | 1 + 6 files changed, 94 insertions(+) diff --git a/impeller/core/allocator.cc b/impeller/core/allocator.cc index be380b671ec27..99b7eea7c748c 100644 --- a/impeller/core/allocator.cc +++ b/impeller/core/allocator.cc @@ -61,4 +61,6 @@ uint16_t Allocator::MinimumBytesPerRow(PixelFormat format) const { return BytesPerPixelForPixelFormat(format); } +void Allocator::IncrementFrame() {} + } // namespace impeller diff --git a/impeller/core/allocator.h b/impeller/core/allocator.h index 25b3dae07a22c..5058906f38125 100644 --- a/impeller/core/allocator.h +++ b/impeller/core/allocator.h @@ -45,6 +45,10 @@ class Allocator { virtual ISize GetMaxTextureSizeSupported() const = 0; + /// @brief Increment an internal frame used to cycle through a ring buffer of + /// allocation pools. + virtual void IncrementFrame(); + protected: Allocator(); diff --git a/impeller/renderer/backend/vulkan/allocator_vk.cc b/impeller/renderer/backend/vulkan/allocator_vk.cc index 6ce8ea7e6812f..43d0dc8840c3d 100644 --- a/impeller/renderer/backend/vulkan/allocator_vk.cc +++ b/impeller/renderer/backend/vulkan/allocator_vk.cc @@ -93,6 +93,11 @@ AllocatorVK::AllocatorVK(std::weak_ptr context, VALIDATION_LOG << "Could not create memory allocator"; return; } + for (auto i = 0u; i < 3u; i++) { + if (!CreateBufferPool(allocator, &staging_buffer_pools_[i])) { + return; + } + } allocator_ = allocator; supports_memoryless_textures_ = capabilities.SupportsMemorylessTextures(); is_valid_ = true; @@ -101,6 +106,11 @@ AllocatorVK::AllocatorVK(std::weak_ptr context, AllocatorVK::~AllocatorVK() { TRACE_EVENT0("impeller", "DestroyAllocatorVK"); if (allocator_) { + for (auto i = 0u; i < 3u; i++) { + if (staging_buffer_pools_[i]) { + ::vmaDestroyPool(allocator_, staging_buffer_pools_[i]); + } + } ::vmaDestroyAllocator(allocator_); } } @@ -210,6 +220,22 @@ static constexpr VkMemoryPropertyFlags ToVKBufferMemoryPropertyFlags( 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) { @@ -388,6 +414,11 @@ std::shared_ptr AllocatorVK::OnCreateTexture( return std::make_shared(context_, std::move(source)); } +void AllocatorVK::IncrementFrame() { + frame_count_++; + raster_thread_id_ = std::this_thread::get_id(); +} + // |Allocator| std::shared_ptr AllocatorVK::OnCreateBuffer( const DeviceBufferDescriptor& desc) { @@ -410,6 +441,11 @@ std::shared_ptr AllocatorVK::OnCreateBuffer( ToVKBufferMemoryPropertyFlags(desc.storage_mode); allocation_info.flags = ToVmaAllocationCreateFlags( desc.storage_mode, /*is_texture=*/false, desc.size); + allocation_info.flags = ToVmaAllocationBufferCreateFlags(desc.storage_mode); + if (desc.storage_mode == StorageMode::kHostVisible && + raster_thread_id_ == std::this_thread::get_id()) { + allocation_info.pool = staging_buffer_pools_[frame_count_ % 3u]; + } VkBuffer buffer = {}; VmaAllocation buffer_allocation = {}; @@ -437,4 +473,46 @@ std::shared_ptr AllocatorVK::OnCreateBuffer( ); } +// 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 = + 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) { + VALIDATION_LOG << "Could not find memory type for buffer pool."; + 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) { + VALIDATION_LOG << "Could not create buffer pool."; + 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 fba13f2b7cf12..e9c562dc5a07d 100644 --- a/impeller/renderer/backend/vulkan/allocator_vk.h +++ b/impeller/renderer/backend/vulkan/allocator_vk.h @@ -27,11 +27,14 @@ class AllocatorVK final : public Allocator { fml::RefPtr vk_; VmaAllocator allocator_ = {}; + VmaPool staging_buffer_pools_[3] = {}; std::weak_ptr context_; std::weak_ptr device_holder_; ISize max_texture_size_; bool is_valid_ = false; bool supports_memoryless_textures_ = false; + uint32_t frame_count_ = 0; + std::thread::id raster_thread_id_; AllocatorVK(std::weak_ptr context, uint32_t vulkan_api_version, @@ -45,6 +48,9 @@ class AllocatorVK final : public Allocator { // |Allocator| bool IsValid() const; + // |Allocator| + void IncrementFrame() override; + // |Allocator| std::shared_ptr OnCreateBuffer( const DeviceBufferDescriptor& desc) override; @@ -56,6 +62,8 @@ 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/swapchain_impl_vk.cc b/impeller/renderer/backend/vulkan/swapchain_impl_vk.cc index 81a3a39c7b5ce..976f476eba2aa 100644 --- a/impeller/renderer/backend/vulkan/swapchain_impl_vk.cc +++ b/impeller/renderer/backend/vulkan/swapchain_impl_vk.cc @@ -326,6 +326,7 @@ SwapchainImplVK::AcquireResult SwapchainImplVK::AcquireNextDrawable() { } const auto& context = ContextVK::Cast(*context_strong); + context.GetResourceAllocator()->IncrementFrame(); current_frame_ = (current_frame_ + 1u) % synchronizers_.size(); diff --git a/shell/common/snapshot_controller_impeller.cc b/shell/common/snapshot_controller_impeller.cc index ea68bd91188a4..435e67a7ab782 100644 --- a/shell/common/snapshot_controller_impeller.cc +++ b/shell/common/snapshot_controller_impeller.cc @@ -39,6 +39,7 @@ sk_sp SnapshotControllerImpeller::DoMakeRasterSnapshot( impeller::Picture picture = dispatcher.EndRecordingAsPicture(); auto context = GetDelegate().GetAiksContext(); if (context) { + context->GetContext()->GetResourceAllocator()->IncrementFrame(); auto max_size = context->GetContext() ->GetResourceAllocator() ->GetMaxTextureSizeSupported(); From 866761f3e6ff8fcfe2c88bd24b4883774253870f Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Tue, 11 Jul 2023 11:16:02 -0700 Subject: [PATCH 2/9] fall back to host visible only --- impeller/renderer/backend/vulkan/allocator_vk.cc | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/impeller/renderer/backend/vulkan/allocator_vk.cc b/impeller/renderer/backend/vulkan/allocator_vk.cc index 43d0dc8840c3d..a16518b237565 100644 --- a/impeller/renderer/backend/vulkan/allocator_vk.cc +++ b/impeller/renderer/backend/vulkan/allocator_vk.cc @@ -498,8 +498,15 @@ bool AllocatorVK::CreateBufferPool(VmaAllocator allocator, VmaPool* pool) { auto result = vk::Result{vmaFindMemoryTypeIndexForBufferInfo( allocator, &buffer_info_native, &allocation_info, &memTypeIndex)}; if (result != vk::Result::eSuccess) { - VALIDATION_LOG << "Could not find memory type for buffer pool."; - return false; + // We might be on a machine without host coherent/host visible/device local. + // Fall back to host visible. + allocation_info.preferredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; + result = vk::Result{vmaFindMemoryTypeIndexForBufferInfo( + allocator, &buffer_info_native, &allocation_info, &memTypeIndex)}; + if (result != vk::Result::eSuccess) { + VALIDATION_LOG << "Could not find memory type for buffer pool."; + return false; + } } VmaPoolCreateInfo pool_create_info = {}; From 1a01926017511e7427e6a7f57322bd45aa0f2563 Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Tue, 11 Jul 2023 12:32:33 -0700 Subject: [PATCH 3/9] wip testing --- impeller/renderer/backend/vulkan/allocator_vk.cc | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/impeller/renderer/backend/vulkan/allocator_vk.cc b/impeller/renderer/backend/vulkan/allocator_vk.cc index a16518b237565..6d6bad1020269 100644 --- a/impeller/renderer/backend/vulkan/allocator_vk.cc +++ b/impeller/renderer/backend/vulkan/allocator_vk.cc @@ -439,8 +439,6 @@ std::shared_ptr AllocatorVK::OnCreateBuffer( allocation_info.usage = ToVMAMemoryUsage(); allocation_info.preferredFlags = ToVKBufferMemoryPropertyFlags(desc.storage_mode); - allocation_info.flags = ToVmaAllocationCreateFlags( - desc.storage_mode, /*is_texture=*/false, desc.size); allocation_info.flags = ToVmaAllocationBufferCreateFlags(desc.storage_mode); if (desc.storage_mode == StorageMode::kHostVisible && raster_thread_id_ == std::this_thread::get_id()) { @@ -491,6 +489,7 @@ bool AllocatorVK::CreateBufferPool(VmaAllocator allocator, VmaPool* pool) { allocation_info.usage = VMA_MEMORY_USAGE_AUTO; allocation_info.preferredFlags = ToVKBufferMemoryPropertyFlags(StorageMode::kHostVisible); + // TESTING allocation_info.flags = ToVmaAllocationBufferCreateFlags(StorageMode::kHostVisible); @@ -498,15 +497,8 @@ bool AllocatorVK::CreateBufferPool(VmaAllocator allocator, VmaPool* pool) { auto result = vk::Result{vmaFindMemoryTypeIndexForBufferInfo( allocator, &buffer_info_native, &allocation_info, &memTypeIndex)}; if (result != vk::Result::eSuccess) { - // We might be on a machine without host coherent/host visible/device local. - // Fall back to host visible. - allocation_info.preferredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; - result = vk::Result{vmaFindMemoryTypeIndexForBufferInfo( - allocator, &buffer_info_native, &allocation_info, &memTypeIndex)}; - if (result != vk::Result::eSuccess) { - VALIDATION_LOG << "Could not find memory type for buffer pool."; - return false; - } + VALIDATION_LOG << "Could not find memory type for buffer pool."; + return false; } VmaPoolCreateInfo pool_create_info = {}; From c85a337d27877039bff5dd7e8acd528345e4b02c Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Tue, 11 Jul 2023 12:43:57 -0700 Subject: [PATCH 4/9] testing --- impeller/renderer/backend/vulkan/allocator_vk.cc | 8 ++++---- impeller/renderer/backend/vulkan/allocator_vk.h | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/impeller/renderer/backend/vulkan/allocator_vk.cc b/impeller/renderer/backend/vulkan/allocator_vk.cc index 6d6bad1020269..f19f1ff879e5e 100644 --- a/impeller/renderer/backend/vulkan/allocator_vk.cc +++ b/impeller/renderer/backend/vulkan/allocator_vk.cc @@ -93,7 +93,7 @@ AllocatorVK::AllocatorVK(std::weak_ptr context, VALIDATION_LOG << "Could not create memory allocator"; return; } - for (auto i = 0u; i < 3u; i++) { + for (auto i = 0u; i < kPoolCount; i++) { if (!CreateBufferPool(allocator, &staging_buffer_pools_[i])) { return; } @@ -442,7 +442,7 @@ std::shared_ptr AllocatorVK::OnCreateBuffer( allocation_info.flags = ToVmaAllocationBufferCreateFlags(desc.storage_mode); if (desc.storage_mode == StorageMode::kHostVisible && raster_thread_id_ == std::this_thread::get_id()) { - allocation_info.pool = staging_buffer_pools_[frame_count_ % 3u]; + allocation_info.pool = staging_buffer_pools_[frame_count_ % kPoolCount]; } VkBuffer buffer = {}; @@ -490,8 +490,8 @@ bool AllocatorVK::CreateBufferPool(VmaAllocator allocator, VmaPool* pool) { allocation_info.preferredFlags = ToVKBufferMemoryPropertyFlags(StorageMode::kHostVisible); // TESTING - allocation_info.flags = - ToVmaAllocationBufferCreateFlags(StorageMode::kHostVisible); + // allocation_info.flags = + // ToVmaAllocationBufferCreateFlags(StorageMode::kHostVisible); uint32_t memTypeIndex; auto result = vk::Result{vmaFindMemoryTypeIndexForBufferInfo( diff --git a/impeller/renderer/backend/vulkan/allocator_vk.h b/impeller/renderer/backend/vulkan/allocator_vk.h index e9c562dc5a07d..395d37ca45a69 100644 --- a/impeller/renderer/backend/vulkan/allocator_vk.h +++ b/impeller/renderer/backend/vulkan/allocator_vk.h @@ -25,9 +25,11 @@ class AllocatorVK final : public Allocator { private: friend class ContextVK; + static constexpr size_t kPoolCount = 3; + fml::RefPtr vk_; VmaAllocator allocator_ = {}; - VmaPool staging_buffer_pools_[3] = {}; + VmaPool staging_buffer_pools_[kPoolCount] = {}; std::weak_ptr context_; std::weak_ptr device_holder_; ISize max_texture_size_; From 3947f81546db5c83c8c2909e3115b4cb2a0938dc Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Tue, 11 Jul 2023 12:56:25 -0700 Subject: [PATCH 5/9] ++ --- impeller/renderer/backend/vulkan/allocator_vk.cc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/impeller/renderer/backend/vulkan/allocator_vk.cc b/impeller/renderer/backend/vulkan/allocator_vk.cc index f19f1ff879e5e..33547c9fd7633 100644 --- a/impeller/renderer/backend/vulkan/allocator_vk.cc +++ b/impeller/renderer/backend/vulkan/allocator_vk.cc @@ -209,9 +209,7 @@ static constexpr VkMemoryPropertyFlags ToVKBufferMemoryPropertyFlags( StorageMode mode) { switch (mode) { case StorageMode::kHostVisible: - return VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | - VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; + return VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; case StorageMode::kDevicePrivate: return VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; case StorageMode::kDeviceTransient: From b8bee473817551c9b45ef0f553344736e4706cba Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Tue, 11 Jul 2023 14:07:29 -0700 Subject: [PATCH 6/9] ++ --- impeller/renderer/backend/vulkan/allocator_vk.cc | 16 ++++++++-------- impeller/renderer/backend/vulkan/allocator_vk.h | 2 ++ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/impeller/renderer/backend/vulkan/allocator_vk.cc b/impeller/renderer/backend/vulkan/allocator_vk.cc index 33547c9fd7633..b6c294b84e7f5 100644 --- a/impeller/renderer/backend/vulkan/allocator_vk.cc +++ b/impeller/renderer/backend/vulkan/allocator_vk.cc @@ -94,9 +94,8 @@ AllocatorVK::AllocatorVK(std::weak_ptr context, return; } for (auto i = 0u; i < kPoolCount; i++) { - if (!CreateBufferPool(allocator, &staging_buffer_pools_[i])) { - return; - } + created_buffer_pools_ &= + CreateBufferPool(allocator, &staging_buffer_pools_[i]); } allocator_ = allocator; supports_memoryless_textures_ = capabilities.SupportsMemorylessTextures(); @@ -192,7 +191,9 @@ static constexpr VkMemoryPropertyFlags ToVKTextureMemoryPropertyFlags( bool supports_memoryless_textures) { switch (mode) { case StorageMode::kHostVisible: - return VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; + return VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | + VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | + VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; case StorageMode::kDevicePrivate: return VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; case StorageMode::kDeviceTransient: @@ -438,7 +439,7 @@ std::shared_ptr AllocatorVK::OnCreateBuffer( allocation_info.preferredFlags = ToVKBufferMemoryPropertyFlags(desc.storage_mode); allocation_info.flags = ToVmaAllocationBufferCreateFlags(desc.storage_mode); - if (desc.storage_mode == StorageMode::kHostVisible && + 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]; } @@ -487,9 +488,8 @@ bool AllocatorVK::CreateBufferPool(VmaAllocator allocator, VmaPool* pool) { allocation_info.usage = VMA_MEMORY_USAGE_AUTO; allocation_info.preferredFlags = ToVKBufferMemoryPropertyFlags(StorageMode::kHostVisible); - // TESTING - // allocation_info.flags = - // ToVmaAllocationBufferCreateFlags(StorageMode::kHostVisible); + allocation_info.flags = + ToVmaAllocationBufferCreateFlags(StorageMode::kHostVisible); uint32_t memTypeIndex; auto result = vk::Result{vmaFindMemoryTypeIndexForBufferInfo( diff --git a/impeller/renderer/backend/vulkan/allocator_vk.h b/impeller/renderer/backend/vulkan/allocator_vk.h index 395d37ca45a69..ef4a93624e1e9 100644 --- a/impeller/renderer/backend/vulkan/allocator_vk.h +++ b/impeller/renderer/backend/vulkan/allocator_vk.h @@ -35,6 +35,8 @@ class AllocatorVK final : public Allocator { ISize max_texture_size_; bool is_valid_ = false; bool supports_memoryless_textures_ = false; + // TODO(jonahwilliams): figure out why CI can't create these buffer pools. + bool created_buffer_pools_ = true; uint32_t frame_count_ = 0; std::thread::id raster_thread_id_; From 9d4d44a1626df192a2822c498c578fc33249baa9 Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Tue, 11 Jul 2023 15:00:48 -0700 Subject: [PATCH 7/9] ++ --- impeller/renderer/backend/vulkan/allocator_vk.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/impeller/renderer/backend/vulkan/allocator_vk.cc b/impeller/renderer/backend/vulkan/allocator_vk.cc index b6c294b84e7f5..73b8dd702d9dd 100644 --- a/impeller/renderer/backend/vulkan/allocator_vk.cc +++ b/impeller/renderer/backend/vulkan/allocator_vk.cc @@ -495,7 +495,6 @@ bool AllocatorVK::CreateBufferPool(VmaAllocator allocator, VmaPool* pool) { auto result = vk::Result{vmaFindMemoryTypeIndexForBufferInfo( allocator, &buffer_info_native, &allocation_info, &memTypeIndex)}; if (result != vk::Result::eSuccess) { - VALIDATION_LOG << "Could not find memory type for buffer pool."; return false; } @@ -506,7 +505,6 @@ bool AllocatorVK::CreateBufferPool(VmaAllocator allocator, VmaPool* pool) { result = vk::Result{vmaCreatePool(allocator, &pool_create_info, pool)}; if (result != vk::Result::eSuccess) { - VALIDATION_LOG << "Could not create buffer pool."; return false; } return true; From 4efb6a8d0656fb846f945e25b017e92193d07ff1 Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Wed, 12 Jul 2023 12:49:58 -0700 Subject: [PATCH 8/9] use context didAcquireSurfaceFrame --- impeller/core/allocator.cc | 2 +- impeller/core/allocator.h | 2 +- impeller/renderer/backend/vulkan/allocator_vk.cc | 4 ++-- impeller/renderer/backend/vulkan/allocator_vk.h | 2 +- impeller/renderer/backend/vulkan/context_vk.cc | 3 +++ impeller/renderer/backend/vulkan/swapchain_impl_vk.cc | 1 - shell/common/snapshot_controller_impeller.cc | 1 - 7 files changed, 8 insertions(+), 7 deletions(-) diff --git a/impeller/core/allocator.cc b/impeller/core/allocator.cc index 99b7eea7c748c..075a738fe5b4f 100644 --- a/impeller/core/allocator.cc +++ b/impeller/core/allocator.cc @@ -61,6 +61,6 @@ uint16_t Allocator::MinimumBytesPerRow(PixelFormat format) const { return BytesPerPixelForPixelFormat(format); } -void Allocator::IncrementFrame() {} +void Allocator::DidAcquireSurfaceFrame() {} } // namespace impeller diff --git a/impeller/core/allocator.h b/impeller/core/allocator.h index 5058906f38125..62b86b72b047f 100644 --- a/impeller/core/allocator.h +++ b/impeller/core/allocator.h @@ -47,7 +47,7 @@ class Allocator { /// @brief Increment an internal frame used to cycle through a ring buffer of /// allocation pools. - virtual void IncrementFrame(); + virtual void DidAcquireSurfaceFrame(); protected: Allocator(); diff --git a/impeller/renderer/backend/vulkan/allocator_vk.cc b/impeller/renderer/backend/vulkan/allocator_vk.cc index 73b8dd702d9dd..9acd3ef3ce54e 100644 --- a/impeller/renderer/backend/vulkan/allocator_vk.cc +++ b/impeller/renderer/backend/vulkan/allocator_vk.cc @@ -105,7 +105,7 @@ AllocatorVK::AllocatorVK(std::weak_ptr context, AllocatorVK::~AllocatorVK() { TRACE_EVENT0("impeller", "DestroyAllocatorVK"); if (allocator_) { - for (auto i = 0u; i < 3u; i++) { + for (auto i = 0u; i < kPoolCount; i++) { if (staging_buffer_pools_[i]) { ::vmaDestroyPool(allocator_, staging_buffer_pools_[i]); } @@ -413,7 +413,7 @@ std::shared_ptr AllocatorVK::OnCreateTexture( return std::make_shared(context_, std::move(source)); } -void AllocatorVK::IncrementFrame() { +void AllocatorVK::DidAcquireSurfaceFrame() { frame_count_++; raster_thread_id_ = std::this_thread::get_id(); } diff --git a/impeller/renderer/backend/vulkan/allocator_vk.h b/impeller/renderer/backend/vulkan/allocator_vk.h index ef4a93624e1e9..abb65ba836778 100644 --- a/impeller/renderer/backend/vulkan/allocator_vk.h +++ b/impeller/renderer/backend/vulkan/allocator_vk.h @@ -53,7 +53,7 @@ class AllocatorVK final : public Allocator { bool IsValid() const; // |Allocator| - void IncrementFrame() override; + void DidAcquireSurfaceFrame() override; // |Allocator| std::shared_ptr OnCreateBuffer( diff --git a/impeller/renderer/backend/vulkan/context_vk.cc b/impeller/renderer/backend/vulkan/context_vk.cc index 0b1fc69001e33..676da85fbe608 100644 --- a/impeller/renderer/backend/vulkan/context_vk.cc +++ b/impeller/renderer/backend/vulkan/context_vk.cc @@ -481,6 +481,9 @@ std::unique_ptr ContextVK::AcquireNextSurface() { if (surface && pipeline_library_) { pipeline_library_->DidAcquireSurfaceFrame(); } + if (allocator_) { + allocator_->DidAcquireSurfaceFrame(); + } return surface; } diff --git a/impeller/renderer/backend/vulkan/swapchain_impl_vk.cc b/impeller/renderer/backend/vulkan/swapchain_impl_vk.cc index 976f476eba2aa..81a3a39c7b5ce 100644 --- a/impeller/renderer/backend/vulkan/swapchain_impl_vk.cc +++ b/impeller/renderer/backend/vulkan/swapchain_impl_vk.cc @@ -326,7 +326,6 @@ SwapchainImplVK::AcquireResult SwapchainImplVK::AcquireNextDrawable() { } const auto& context = ContextVK::Cast(*context_strong); - context.GetResourceAllocator()->IncrementFrame(); current_frame_ = (current_frame_ + 1u) % synchronizers_.size(); diff --git a/shell/common/snapshot_controller_impeller.cc b/shell/common/snapshot_controller_impeller.cc index 435e67a7ab782..ea68bd91188a4 100644 --- a/shell/common/snapshot_controller_impeller.cc +++ b/shell/common/snapshot_controller_impeller.cc @@ -39,7 +39,6 @@ sk_sp SnapshotControllerImpeller::DoMakeRasterSnapshot( impeller::Picture picture = dispatcher.EndRecordingAsPicture(); auto context = GetDelegate().GetAiksContext(); if (context) { - context->GetContext()->GetResourceAllocator()->IncrementFrame(); auto max_size = context->GetContext() ->GetResourceAllocator() ->GetMaxTextureSizeSupported(); From 10d29ccf112e75cea8ff2f47e0a08a7ac3fd236d Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Wed, 12 Jul 2023 15:27:19 -0700 Subject: [PATCH 9/9] use c++ types --- .../renderer/backend/vulkan/allocator_vk.cc | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/impeller/renderer/backend/vulkan/allocator_vk.cc b/impeller/renderer/backend/vulkan/allocator_vk.cc index 9acd3ef3ce54e..07d7fc23ea797 100644 --- a/impeller/renderer/backend/vulkan/allocator_vk.cc +++ b/impeller/renderer/backend/vulkan/allocator_vk.cc @@ -186,35 +186,35 @@ static constexpr VmaMemoryUsage ToVMAMemoryUsage() { return VMA_MEMORY_USAGE_AUTO; } -static constexpr VkMemoryPropertyFlags ToVKTextureMemoryPropertyFlags( - StorageMode mode, - bool supports_memoryless_textures) { +static constexpr vk::Flags +ToVKTextureMemoryPropertyFlags(StorageMode mode, + bool supports_memoryless_textures) { switch (mode) { case StorageMode::kHostVisible: - return VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | - VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; + return vk::MemoryPropertyFlagBits::eHostVisible | + vk::MemoryPropertyFlagBits::eDeviceLocal | + vk::MemoryPropertyFlagBits::eHostCoherent; case StorageMode::kDevicePrivate: - return VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; + return vk::MemoryPropertyFlagBits::eDeviceLocal; case StorageMode::kDeviceTransient: if (supports_memoryless_textures) { - return VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT | - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; + return vk::MemoryPropertyFlagBits::eLazilyAllocated | + vk::MemoryPropertyFlagBits::eDeviceLocal; } - return VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; + return vk::MemoryPropertyFlagBits::eDeviceLocal; } FML_UNREACHABLE(); } -static constexpr VkMemoryPropertyFlags ToVKBufferMemoryPropertyFlags( - StorageMode mode) { +static constexpr vk::Flags +ToVKBufferMemoryPropertyFlags(StorageMode mode) { switch (mode) { case StorageMode::kHostVisible: - return VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; + return vk::MemoryPropertyFlagBits::eHostVisible; case StorageMode::kDevicePrivate: - return VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; + return vk::MemoryPropertyFlagBits::eDeviceLocal; case StorageMode::kDeviceTransient: - return VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT; + return vk::MemoryPropertyFlagBits::eLazilyAllocated; } FML_UNREACHABLE(); } @@ -294,8 +294,9 @@ class AllocatedTextureSourceVK final : public TextureSourceVK { VmaAllocationCreateInfo alloc_nfo = {}; alloc_nfo.usage = ToVMAMemoryUsage(); - alloc_nfo.preferredFlags = ToVKTextureMemoryPropertyFlags( - desc.storage_mode, supports_memoryless_textures); + alloc_nfo.preferredFlags = + static_cast(ToVKTextureMemoryPropertyFlags( + desc.storage_mode, supports_memoryless_textures)); alloc_nfo.flags = ToVmaAllocationCreateFlags(desc.storage_mode, /*is_texture=*/true, desc.GetByteSizeOfBaseMipLevel()); @@ -436,8 +437,8 @@ std::shared_ptr AllocatorVK::OnCreateBuffer( VmaAllocationCreateInfo allocation_info = {}; allocation_info.usage = ToVMAMemoryUsage(); - allocation_info.preferredFlags = - ToVKBufferMemoryPropertyFlags(desc.storage_mode); + allocation_info.preferredFlags = static_cast( + ToVKBufferMemoryPropertyFlags(desc.storage_mode)); allocation_info.flags = ToVmaAllocationBufferCreateFlags(desc.storage_mode); if (created_buffer_pools_ && desc.storage_mode == StorageMode::kHostVisible && raster_thread_id_ == std::this_thread::get_id()) { @@ -486,8 +487,8 @@ bool AllocatorVK::CreateBufferPool(VmaAllocator allocator, VmaPool* pool) { VmaAllocationCreateInfo allocation_info = {}; allocation_info.usage = VMA_MEMORY_USAGE_AUTO; - allocation_info.preferredFlags = - ToVKBufferMemoryPropertyFlags(StorageMode::kHostVisible); + allocation_info.preferredFlags = static_cast( + ToVKBufferMemoryPropertyFlags(StorageMode::kHostVisible)); allocation_info.flags = ToVmaAllocationBufferCreateFlags(StorageMode::kHostVisible);