diff --git a/engine/src/flutter/impeller/compositor/allocator.h b/engine/src/flutter/impeller/compositor/allocator.h index 8ad4913152..a494b70dd8 100644 --- a/engine/src/flutter/impeller/compositor/allocator.h +++ b/engine/src/flutter/impeller/compositor/allocator.h @@ -18,13 +18,18 @@ enum class StorageMode { }; class Context; -class Buffer; +class DeviceBuffer; class Allocator { public: ~Allocator(); - std::shared_ptr CreateBuffer(size_t length, std::string label); + bool IsValid() const; + + std::shared_ptr CreateBuffer(StorageMode mode, size_t length); + + std::shared_ptr CreateBufferWithCopy(const uint8_t* buffer, + size_t length); private: friend class Context; @@ -33,10 +38,10 @@ class Allocator { // MTLDevice APIs. But, in the future, this could be backed by named heaps // with specific limits. id device_; - StorageMode mode_; std::string allocator_label_; + bool is_valid_ = false; - Allocator(id device, StorageMode type, std::string label); + Allocator(id device, std::string label); FML_DISALLOW_COPY_AND_ASSIGN(Allocator); }; diff --git a/engine/src/flutter/impeller/compositor/allocator.mm b/engine/src/flutter/impeller/compositor/allocator.mm index 27b86c9b21..2cd0f4f57e 100644 --- a/engine/src/flutter/impeller/compositor/allocator.mm +++ b/engine/src/flutter/impeller/compositor/allocator.mm @@ -4,16 +4,27 @@ #include "impeller/compositor/allocator.h" +#include "flutter/fml/logging.h" #include "impeller/compositor/buffer.h" #include "impeller/compositor/device_buffer.h" namespace impeller { -Allocator::Allocator(id device, StorageMode type, std::string label) - : device_(device), mode_(type), allocator_label_(std::move(label)) {} +Allocator::Allocator(id device, std::string label) + : device_(device), allocator_label_(std::move(label)) { + if (!device_) { + return; + } + + is_valid_ = true; +} Allocator::~Allocator() = default; +bool Allocator::IsValid() const { + return is_valid_; +} + static MTLResourceOptions ResourceOptionsFromStorageType(StorageMode type) { switch (type) { case StorageMode::kHostCoherent: @@ -23,16 +34,33 @@ static MTLResourceOptions ResourceOptionsFromStorageType(StorageMode type) { } } -std::shared_ptr Allocator::CreateBuffer(size_t length, - std::string label) { +std::shared_ptr Allocator::CreateBufferWithCopy( + const uint8_t* buffer, + size_t length) { + auto new_buffer = CreateBuffer(StorageMode::kHostCoherent, length); + + if (!new_buffer) { + return nullptr; + } + + auto entire_range = Range{0, length}; + + if (!new_buffer->CopyHostBuffer(buffer, entire_range)) { + return nullptr; + } + + return new_buffer; +} + +std::shared_ptr Allocator::CreateBuffer(StorageMode mode, + size_t length) { auto buffer = [device_ newBufferWithLength:length - options:ResourceOptionsFromStorageType(mode_)]; + options:ResourceOptionsFromStorageType(mode)]; if (!buffer) { return nullptr; } - buffer.label = @(label.c_str()); - return std::shared_ptr(new Buffer(buffer, length, mode_, label)); + return std::shared_ptr(new DeviceBuffer(buffer, length, mode)); } } // namespace impeller diff --git a/engine/src/flutter/impeller/compositor/buffer.h b/engine/src/flutter/impeller/compositor/buffer.h index 824a96ba37..d29e32e1be 100644 --- a/engine/src/flutter/impeller/compositor/buffer.h +++ b/engine/src/flutter/impeller/compositor/buffer.h @@ -6,44 +6,17 @@ #include -#include "flutter/fml/macros.h" -#include "impeller/compositor/allocator.h" -#include "impeller/compositor/range.h" - namespace impeller { -class HostBuffer; +class DeviceBuffer; +class Allocator; -class BufferBase { +class Buffer { public: - ~BufferBase() = default; + virtual ~Buffer(); - virtual uint8_t* GetMapping() const = 0; - - virtual size_t GetLength() const = 0; -}; - -class BufferView final : public BufferBase { - public: - // |BufferBase| - ~BufferView() = default; - - // |BufferBase| - uint8_t* GetMapping() const override { - return parent_->GetMapping() + range_in_parent_.offset; - } - - // |BufferBase| - size_t GetLength() const override { return range_in_parent_.length; } - - private: - friend HostBuffer; - - std::shared_ptr parent_; - Range range_in_parent_; - - BufferView(std::shared_ptr parent, Range range_in_parent) - : parent_(std::move(parent)), range_in_parent_(range_in_parent) {} + virtual std::shared_ptr GetDeviceBuffer( + Allocator& allocator) const = 0; }; } // namespace impeller diff --git a/engine/src/flutter/impeller/compositor/buffer.mm b/engine/src/flutter/impeller/compositor/buffer.mm index 9e4c11e91a..fe8f4a860f 100644 --- a/engine/src/flutter/impeller/compositor/buffer.mm +++ b/engine/src/flutter/impeller/compositor/buffer.mm @@ -6,6 +6,6 @@ namespace impeller { -// +Buffer::~Buffer() = default; } // namespace impeller diff --git a/engine/src/flutter/impeller/compositor/buffer_view.h b/engine/src/flutter/impeller/compositor/buffer_view.h index e69de29bb2..aa07cf30ce 100644 --- a/engine/src/flutter/impeller/compositor/buffer_view.h +++ b/engine/src/flutter/impeller/compositor/buffer_view.h @@ -0,0 +1,20 @@ +// 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/fml/macros.h" +#include "impeller/compositor/buffer.h" +#include "impeller/compositor/range.h" + +namespace impeller { + +struct BufferView { + std::shared_ptr buffer; + Range range; + + constexpr operator bool() const { return static_cast(buffer); } +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/compositor/buffer_view.mm b/engine/src/flutter/impeller/compositor/buffer_view.mm index e69de29bb2..c7962504f6 100644 --- a/engine/src/flutter/impeller/compositor/buffer_view.mm +++ b/engine/src/flutter/impeller/compositor/buffer_view.mm @@ -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/compositor/buffer_view.h" + +namespace impeller { + +// + +} // namespace impeller diff --git a/engine/src/flutter/impeller/compositor/command.h b/engine/src/flutter/impeller/compositor/command.h index 1def25cd97..0676c44279 100644 --- a/engine/src/flutter/impeller/compositor/command.h +++ b/engine/src/flutter/impeller/compositor/command.h @@ -8,7 +8,7 @@ #include #include "flutter/fml/macros.h" -#include "impeller/compositor/buffer.h" +#include "impeller/compositor/buffer_view.h" #include "impeller/compositor/pipeline.h" namespace impeller { diff --git a/engine/src/flutter/impeller/compositor/context.h b/engine/src/flutter/impeller/compositor/context.h index fa4e4b3e2d..77dd15a826 100644 --- a/engine/src/flutter/impeller/compositor/context.h +++ b/engine/src/flutter/impeller/compositor/context.h @@ -15,6 +15,7 @@ namespace impeller { class ShaderLibrary; class CommandBuffer; +class Allocator; class Context { public: @@ -24,6 +25,8 @@ class Context { bool IsValid() const; + std::shared_ptr GetTransientsAllocator() const; + std::shared_ptr GetShaderLibrary() const; std::shared_ptr GetPipelineLibrary() const; @@ -36,6 +39,7 @@ class Context { id transfer_queue_ = nullptr; std::shared_ptr shader_library_; std::shared_ptr pipeline_library_; + std::shared_ptr transients_allocator_; bool is_valid_ = false; FML_DISALLOW_COPY_AND_ASSIGN(Context); diff --git a/engine/src/flutter/impeller/compositor/context.mm b/engine/src/flutter/impeller/compositor/context.mm index fd8b85a6b7..2ec6cb2fbe 100644 --- a/engine/src/flutter/impeller/compositor/context.mm +++ b/engine/src/flutter/impeller/compositor/context.mm @@ -6,6 +6,7 @@ #include "flutter/fml/logging.h" #include "flutter/fml/paths.h" +#include "impeller/compositor/allocator.h" #include "impeller/compositor/command_buffer.h" #include "impeller/compositor/shader_library.h" @@ -54,6 +55,14 @@ Context::Context(std::string shaders_directory) std::shared_ptr(new PipelineLibrary(device_)); } + { + transients_allocator_ = std::shared_ptr( + new Allocator(device_, "Impeller Transients Allocator")); + if (!transients_allocator_) { + return; + } + } + is_valid_ = true; } @@ -84,4 +93,8 @@ std::shared_ptr Context::CreateRenderCommandBuffer() const { return buffer; } +std::shared_ptr Context::GetTransientsAllocator() const { + return transients_allocator_; +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/compositor/device_buffer.h b/engine/src/flutter/impeller/compositor/device_buffer.h index a5741b752c..a484db7776 100644 --- a/engine/src/flutter/impeller/compositor/device_buffer.h +++ b/engine/src/flutter/impeller/compositor/device_buffer.h @@ -6,16 +6,26 @@ #include +#include #include #include "flutter/fml/macros.h" #include "impeller/compositor/allocator.h" +#include "impeller/compositor/buffer.h" +#include "impeller/compositor/range.h" namespace impeller { -class Buffer { +class DeviceBuffer final : public Buffer, + public std::enable_shared_from_this { public: - ~Buffer(); + ~DeviceBuffer(); + + [[nodiscard]] bool CopyHostBuffer(const uint8_t* source, + Range source_range, + size_t offset = 0u); + + id GetMTLBuffer() const; private: friend class Allocator; @@ -23,14 +33,14 @@ class Buffer { const id buffer_; const size_t size_; const StorageMode mode_; - const std::string label_; - Buffer(id buffer, - size_t size, - StorageMode mode, - std::string label); + DeviceBuffer(id buffer, size_t size, StorageMode mode); - FML_DISALLOW_COPY_AND_ASSIGN(Buffer); + // |Buffer| + std::shared_ptr GetDeviceBuffer( + Allocator& allocator) const override; + + FML_DISALLOW_COPY_AND_ASSIGN(DeviceBuffer); }; } // namespace impeller diff --git a/engine/src/flutter/impeller/compositor/device_buffer.mm b/engine/src/flutter/impeller/compositor/device_buffer.mm index e54cea72fa..7ecf61ea76 100644 --- a/engine/src/flutter/impeller/compositor/device_buffer.mm +++ b/engine/src/flutter/impeller/compositor/device_buffer.mm @@ -4,14 +4,45 @@ #include "impeller/compositor/device_buffer.h" +#include + namespace impeller { -Buffer::Buffer(id buffer, - size_t size, - StorageMode mode, - std::string label) - : buffer_(buffer), size_(size), mode_(mode), label_(std::move(label)) {} +DeviceBuffer::DeviceBuffer(id buffer, size_t size, StorageMode mode) + : buffer_(buffer), size_(size), mode_(mode) {} -Buffer::~Buffer() = default; +DeviceBuffer::~DeviceBuffer() = default; + +id DeviceBuffer::GetMTLBuffer() const { + return buffer_; +} + +[[nodiscard]] bool DeviceBuffer::CopyHostBuffer(const uint8_t* source, + Range source_range, + size_t offset) { + if (offset + source_range.length > size_) { + // Out of bounds of this buffer. + return false; + } + + auto dest = static_cast(buffer_.contents); + + if (!dest) { + // Probably StorageMode::kDevicePrivate. + return false; + } + + ::memcpy(dest + offset, source + source_range.offset, source_range.length); + + [buffer_ didModifyRange:NSMakeRange(offset, source_range.length)]; + + return true; +} + +// |Buffer| +std::shared_ptr DeviceBuffer::GetDeviceBuffer( + Allocator& allocator) const { + return shared_from_this(); +} } // namespace impeller diff --git a/engine/src/flutter/impeller/compositor/host_buffer.h b/engine/src/flutter/impeller/compositor/host_buffer.h index f2a192023c..e643b44935 100644 --- a/engine/src/flutter/impeller/compositor/host_buffer.h +++ b/engine/src/flutter/impeller/compositor/host_buffer.h @@ -8,31 +8,33 @@ #include "flutter/fml/macros.h" #include "impeller/compositor/buffer.h" -#include "impeller/compositor/host_buffer.h" +#include "impeller/compositor/buffer_view.h" namespace impeller { -class HostBuffer : public std::enable_shared_from_this, - public BufferBase { +class HostBuffer final : public std::enable_shared_from_this, + public Buffer { public: - std::shared_ptr Create(); - - std::shared_ptr Emplace(size_t length); - + // |Buffer| virtual ~HostBuffer(); - // |BufferBase| - uint8_t* GetMapping() const override { return buffer_; } + static std::shared_ptr Create(); - // |BufferBase| - size_t GetLength() const override { return length_; } + BufferView Emplace(const void* buffer, size_t length); [[nodiscard]] bool Truncate(size_t length); private: + mutable std::shared_ptr device_buffer_; uint8_t* buffer_ = nullptr; size_t length_ = 0; size_t reserved_ = 0; + size_t generation_ = 1u; + mutable size_t device_buffer_generation_ = 0u; + + // |Buffer| + std::shared_ptr GetDeviceBuffer( + Allocator& allocator) const override; [[nodiscard]] bool Reserve(size_t reserved); diff --git a/engine/src/flutter/impeller/compositor/host_buffer.mm b/engine/src/flutter/impeller/compositor/host_buffer.mm index 1925c4a9c4..c381fc7255 100644 --- a/engine/src/flutter/impeller/compositor/host_buffer.mm +++ b/engine/src/flutter/impeller/compositor/host_buffer.mm @@ -6,6 +6,9 @@ #include "flutter/fml/logging.h" +#include "impeller/compositor/allocator.h" +#include "impeller/compositor/buffer_view.h" + namespace impeller { std::shared_ptr HostBuffer::Create() { @@ -18,13 +21,15 @@ HostBuffer::~HostBuffer() { ::free(buffer_); } -std::shared_ptr HostBuffer::Emplace(size_t length) { +BufferView HostBuffer::Emplace(const void* buffer, size_t length) { auto old_length = length_; if (!Truncate(length_ + length)) { - return nullptr; + return {}; } - return std::shared_ptr( - new BufferView(shared_from_this(), Range{old_length, length})); + FML_DCHECK(buffer_); + generation_++; + ::memmove(buffer_, buffer, length); + return BufferView{shared_from_this(), Range{old_length, length}}; } bool HostBuffer::Truncate(size_t length) { @@ -72,4 +77,18 @@ bool HostBuffer::Reserve(size_t reserved) { return true; } +std::shared_ptr HostBuffer::GetDeviceBuffer( + Allocator& allocator) const { + if (generation_ == device_buffer_generation_) { + return device_buffer_; + } + auto new_buffer = allocator.CreateBufferWithCopy(buffer_, length_); + if (!new_buffer) { + return nullptr; + } + device_buffer_generation_ = generation_; + device_buffer_ = std::move(new_buffer); + return device_buffer_; +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/compositor/render_pass.h b/engine/src/flutter/impeller/compositor/render_pass.h index 54ee77ff90..f97bd1550f 100644 --- a/engine/src/flutter/impeller/compositor/render_pass.h +++ b/engine/src/flutter/impeller/compositor/render_pass.h @@ -72,7 +72,7 @@ class RenderPass { bool Record(Command command); - bool Encode() const; + bool Encode(Allocator& transients_allocator) const; private: friend class CommandBuffer; @@ -84,7 +84,8 @@ class RenderPass { RenderPass(id buffer, const RenderPassDescriptor& desc); - bool EncodeCommands(id pass) const; + bool EncodeCommands(Allocator& transients_allocator, + id pass) const; FML_DISALLOW_COPY_AND_ASSIGN(RenderPass); }; diff --git a/engine/src/flutter/impeller/compositor/render_pass.mm b/engine/src/flutter/impeller/compositor/render_pass.mm index fe2ad25fb4..d9d5604e41 100644 --- a/engine/src/flutter/impeller/compositor/render_pass.mm +++ b/engine/src/flutter/impeller/compositor/render_pass.mm @@ -6,6 +6,7 @@ #include "flutter/fml/closure.h" #include "flutter/fml/logging.h" +#include "impeller/compositor/device_buffer.h" #include "impeller/compositor/formats_metal.h" #include "impeller/shader_glue/shader_types.h" @@ -130,7 +131,7 @@ bool RenderPass::IsValid() const { return is_valid_; } -bool RenderPass::Encode() const { +bool RenderPass::Encode(Allocator& transients_allocator) const { if (!IsValid()) { return false; } @@ -142,38 +143,85 @@ bool RenderPass::Encode() const { // at a time. fml::ScopedCleanupClosure auto_end([pass]() { [pass endEncoding]; }); - return EncodeCommands(pass); + return EncodeCommands(transients_allocator, pass); } -void Bind(ShaderStage stage, const BufferView& view) {} +static bool Bind(id pass, + Allocator& allocator, + ShaderStage stage, + size_t bind_index, + const BufferView& view) { + if (!view.buffer) { + return false; + } -void Bind(ShaderStage stage, const Texture& view) {} + auto device_buffer = view.buffer->GetDeviceBuffer(allocator); + if (!device_buffer) { + return false; + } -void Bind(ShaderStage stage, const Sampler& view) {} + auto buffer = device_buffer->GetMTLBuffer(); + // The Metal call is a void return and we don't want to make it on nil. + if (!buffer) { + return false; + } -bool RenderPass::EncodeCommands(id pass) const { + [pass setVertexBuffer:buffer offset:view.range.offset atIndex:bind_index]; + return true; +} + +static bool Bind(Allocator& allocator, + ShaderStage stage, + size_t bind_index, + const Texture& view) { + FML_CHECK(false); + return false; +} + +static bool Bind(Allocator& allocator, + ShaderStage stage, + size_t bind_index, + const Sampler& view) { + FML_CHECK(false); + return false; +} + +bool RenderPass::EncodeCommands(Allocator& allocator, + id pass) const { // There a numerous opportunities here to ensure bindings are not repeated. // Stuff like setting the vertex buffer bindings over and over when just the // offsets could be updated (as recommended in best practices). - - auto bind_stage_resources = [](const Bindings& bindings, ShaderStage stage) { + auto bind_stage_resources = [&allocator, pass](const Bindings& bindings, + ShaderStage stage) -> bool { for (const auto buffer : bindings.buffers) { - Bind(stage, buffer.second); + if (!Bind(pass, allocator, stage, buffer.first, buffer.second)) { + return false; + } } for (const auto texture : bindings.textures) { - Bind(stage, *texture.second); + if (!Bind(allocator, stage, texture.first, *texture.second)) { + return false; + } } for (const auto sampler : bindings.samplers) { - Bind(stage, *sampler.second); + if (!Bind(allocator, stage, sampler.first, *sampler.second)) { + return false; + } } + return true; }; for (const auto& command : commands_) { [pass setRenderPipelineState:command.pipeline->GetMTLRenderPipelineState()]; [pass setDepthStencilState:command.pipeline->GetMTLDepthStencilState()]; [pass setFrontFacingWinding:MTLWindingClockwise]; [pass setCullMode:MTLCullModeBack]; - bind_stage_resources(command.vertex_bindings, ShaderStage::kVertex); - bind_stage_resources(command.fragment_bindings, ShaderStage::kFragment); + if (!bind_stage_resources(command.vertex_bindings, ShaderStage::kVertex)) { + return false; + } + if (!bind_stage_resources(command.fragment_bindings, + ShaderStage::kFragment)) { + return false; + } // [pass drawIndexedPrimitives:MTLPrimitiveTypeTriangleStrip // indexCount:command.index_count // indexType:MTLIndexTypeUInt32 diff --git a/engine/src/flutter/impeller/compositor/renderer.mm b/engine/src/flutter/impeller/compositor/renderer.mm index 942e1c5af4..b0b02a1ae8 100644 --- a/engine/src/flutter/impeller/compositor/renderer.mm +++ b/engine/src/flutter/impeller/compositor/renderer.mm @@ -48,7 +48,7 @@ bool Renderer::Render(const Surface& surface) { if (!render_pass) { return false; } - render_pass->Encode(); + render_pass->Encode(*context_->GetTransientsAllocator()); ::dispatch_semaphore_wait(frames_in_flight_sema_, DISPATCH_TIME_FOREVER);