diff --git a/engine/src/flutter/impeller/compositor/allocator.h b/engine/src/flutter/impeller/compositor/allocator.h index a494b70dd8..86c9a08e51 100644 --- a/engine/src/flutter/impeller/compositor/allocator.h +++ b/engine/src/flutter/impeller/compositor/allocator.h @@ -12,9 +12,35 @@ namespace impeller { +//------------------------------------------------------------------------------ +/// @brief Specified where the allocation resides and how it is used. +/// enum class StorageMode { - kHostCoherent, + //---------------------------------------------------------------------------- + /// Allocations can be mapped onto the hosts address space and also be used by + /// the device. + /// + kHostVisible, + //---------------------------------------------------------------------------- + /// Allocations can only be used by the device. This location is optimal for + /// use by the device. If the host needs to access these allocations, the + /// transfer queue must be used to transfer this allocation onto the a host + /// visible buffer. + /// kDevicePrivate, + //---------------------------------------------------------------------------- + /// Used by the device for temporary render targets. These allocations cannot + /// be transferred from and to other allocations using the transfer queue. + /// Render pass cannot initialize the contents of these buffers using load and + /// store actions. + /// + /// These allocations reside in tile memory which has higher bandwidth, lower + /// latency and lower power consumption. The total device memory usage is + /// also lower as a separate allocation does not need to be created in + /// device memory. Prefer using these allocations for intermediates like depth + /// and stencil buffers. + /// + kDeviceTransient, }; class Context; @@ -31,6 +57,8 @@ class Allocator { std::shared_ptr CreateBufferWithCopy(const uint8_t* buffer, size_t length); + static bool RequiresExplicitHostSynchronization(StorageMode mode); + private: friend class Context; diff --git a/engine/src/flutter/impeller/compositor/allocator.mm b/engine/src/flutter/impeller/compositor/allocator.mm index 2cd0f4f57e..5d86910986 100644 --- a/engine/src/flutter/impeller/compositor/allocator.mm +++ b/engine/src/flutter/impeller/compositor/allocator.mm @@ -4,6 +4,7 @@ #include "impeller/compositor/allocator.h" +#include "flutter/fml/build_config.h" #include "flutter/fml/logging.h" #include "impeller/compositor/buffer.h" #include "impeller/compositor/device_buffer.h" @@ -27,17 +28,41 @@ bool Allocator::IsValid() const { static MTLResourceOptions ResourceOptionsFromStorageType(StorageMode type) { switch (type) { - case StorageMode::kHostCoherent: + case StorageMode::kHostVisible: +#if OS_IOS + return MTLStorageModeShared; +#else return MTLResourceStorageModeManaged; +#endif case StorageMode::kDevicePrivate: return MTLResourceStorageModePrivate; + case StorageMode::kDeviceTransient: +#if OS_IOS + return MTLStorageModeMemoryless; +#else + return MTLResourceStorageModePrivate; +#endif } } +bool Allocator::RequiresExplicitHostSynchronization(StorageMode mode) { + if (mode != StorageMode::kHostVisible) { + return false; + } + +#if OS_IOS + // StorageMode::kHostVisible is MTLStorageModeShared already. + return false; +#else + // StorageMode::kHostVisible is MTLResourceStorageModeManaged. + return true; +#endif +} + std::shared_ptr Allocator::CreateBufferWithCopy( const uint8_t* buffer, size_t length) { - auto new_buffer = CreateBuffer(StorageMode::kHostCoherent, length); + auto new_buffer = CreateBuffer(StorageMode::kHostVisible, length); if (!new_buffer) { return nullptr; diff --git a/engine/src/flutter/impeller/compositor/device_buffer.mm b/engine/src/flutter/impeller/compositor/device_buffer.mm index 364e4063cf..157814f22b 100644 --- a/engine/src/flutter/impeller/compositor/device_buffer.mm +++ b/engine/src/flutter/impeller/compositor/device_buffer.mm @@ -49,6 +49,11 @@ std::shared_ptr DeviceBuffer::MakeTexture(TextureDescriptor desc, [[nodiscard]] bool DeviceBuffer::CopyHostBuffer(const uint8_t* source, Range source_range, size_t offset) { + if (mode_ != StorageMode::kHostVisible) { + // One of the storage modes where a transfer queue must be used. + return false; + } + if (offset + source_range.length > size_) { // Out of bounds of this buffer. return false; @@ -57,13 +62,14 @@ std::shared_ptr DeviceBuffer::MakeTexture(TextureDescriptor desc, auto dest = static_cast(buffer_.contents); if (!dest) { - // Probably StorageMode::kDevicePrivate. return false; } ::memmove(dest + offset, source + source_range.offset, source_range.length); - [buffer_ didModifyRange:NSMakeRange(offset, source_range.length)]; + if (Allocator::RequiresExplicitHostSynchronization(mode_)) { + [buffer_ didModifyRange:NSMakeRange(offset, source_range.length)]; + } return true; }