From a8db4feed1b9d50302d7a349729094394df3c99f Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Sun, 6 Jun 2021 13:44:08 -0700 Subject: [PATCH] Wire up render pass creation. --- .../src/flutter/impeller/compositor/BUILD.gn | 2 + .../impeller/compositor/command_buffer.cc | 13 --- .../impeller/compositor/command_buffer.h | 18 +++- .../impeller/compositor/command_buffer.mm | 62 +++++++++++++ .../src/flutter/impeller/compositor/context.h | 7 +- .../flutter/impeller/compositor/context.mm | 22 +++-- .../impeller/compositor/formats_metal.h | 28 ++++++ .../flutter/impeller/compositor/render_pass.h | 19 +++- .../impeller/compositor/render_pass.mm | 92 ++++++++++++++++++- .../flutter/impeller/compositor/surface.mm | 13 ++- .../src/flutter/impeller/compositor/texture.h | 6 ++ .../flutter/impeller/compositor/texture.mm | 4 + 12 files changed, 247 insertions(+), 39 deletions(-) delete mode 100644 engine/src/flutter/impeller/compositor/command_buffer.cc create mode 100644 engine/src/flutter/impeller/compositor/command_buffer.mm diff --git a/engine/src/flutter/impeller/compositor/BUILD.gn b/engine/src/flutter/impeller/compositor/BUILD.gn index 6bc76c7c2a..2288e13e96 100644 --- a/engine/src/flutter/impeller/compositor/BUILD.gn +++ b/engine/src/flutter/impeller/compositor/BUILD.gn @@ -12,6 +12,8 @@ impeller_component("compositor") { "buffer.mm", "command.h", "command.mm", + "command_buffer.h", + "command_buffer.mm", "comparable.cc", "comparable.h", "context.h", diff --git a/engine/src/flutter/impeller/compositor/command_buffer.cc b/engine/src/flutter/impeller/compositor/command_buffer.cc deleted file mode 100644 index 9e877858dc..0000000000 --- a/engine/src/flutter/impeller/compositor/command_buffer.cc +++ /dev/null @@ -1,13 +0,0 @@ -// 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/command_buffer.h" - -namespace impeller { - -Class::Class() = default; - -Class::~Class() = default; - -} // namespace impeller diff --git a/engine/src/flutter/impeller/compositor/command_buffer.h b/engine/src/flutter/impeller/compositor/command_buffer.h index 06f99c9589..3233312ef1 100644 --- a/engine/src/flutter/impeller/compositor/command_buffer.h +++ b/engine/src/flutter/impeller/compositor/command_buffer.h @@ -4,16 +4,24 @@ #pragma once +#include + #include +#include #include "flutter/fml/macros.h" +#include "impeller/compositor/render_pass.h" namespace impeller { +class Context; + class CommandBuffer { public: ~CommandBuffer(); + bool IsValid() const; + enum class CommitResult { kPending, kError, @@ -23,8 +31,16 @@ class CommandBuffer { using CommitCallback = std::function; void Commit(CommitCallback callback); + std::shared_ptr CreateRenderPass( + const RenderPassDescriptor& desc) const; + private: - CommandBuffer(); + friend class Context; + + id buffer_ = nullptr; + bool is_valid_ = false; + + CommandBuffer(id queue); FML_DISALLOW_COPY_AND_ASSIGN(CommandBuffer); }; diff --git a/engine/src/flutter/impeller/compositor/command_buffer.mm b/engine/src/flutter/impeller/compositor/command_buffer.mm new file mode 100644 index 0000000000..cd6ffa29da --- /dev/null +++ b/engine/src/flutter/impeller/compositor/command_buffer.mm @@ -0,0 +1,62 @@ +// 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/command_buffer.h" + +namespace impeller { + +CommandBuffer::CommandBuffer(id queue) + : buffer_([queue commandBuffer]) {} + +CommandBuffer::~CommandBuffer() = default; + +bool CommandBuffer::IsValid() const { + return is_valid_; +} + +static CommandBuffer::CommitResult ToCommitResult( + MTLCommandBufferStatus status) { + switch (status) { + case MTLCommandBufferStatusCompleted: + return CommandBuffer::CommitResult::kCompleted; + case MTLCommandBufferStatusEnqueued: + return CommandBuffer::CommitResult::kPending; + default: + break; + } + return CommandBuffer::CommitResult::kError; +} + +void CommandBuffer::Commit(CommitCallback callback) { + if (!callback) { + callback = [](auto) {}; + } + + if (!buffer_) { + callback(CommitResult::kError); + return; + } + + [buffer_ addCompletedHandler:^(id buffer) { + callback(ToCommitResult(buffer.status)); + }]; + [buffer_ commit]; + buffer_ = nil; +} + +std::shared_ptr CommandBuffer::CreateRenderPass( + const RenderPassDescriptor& desc) const { + if (!buffer_) { + return nullptr; + } + + auto pass = std::shared_ptr(new RenderPass(buffer_, desc)); + if (!pass->IsValid()) { + return nullptr; + } + + return pass; +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/compositor/context.h b/engine/src/flutter/impeller/compositor/context.h index 6dee81f7b4..fa4e4b3e2d 100644 --- a/engine/src/flutter/impeller/compositor/context.h +++ b/engine/src/flutter/impeller/compositor/context.h @@ -14,6 +14,7 @@ namespace impeller { class ShaderLibrary; +class CommandBuffer; class Context { public: @@ -23,14 +24,12 @@ class Context { bool IsValid() const; - id GetRenderQueue() const; - - id GetTransferQueue() const; - std::shared_ptr GetShaderLibrary() const; std::shared_ptr GetPipelineLibrary() const; + std::shared_ptr CreateRenderCommandBuffer() const; + private: id device_ = nullptr; id render_queue_ = nullptr; diff --git a/engine/src/flutter/impeller/compositor/context.mm b/engine/src/flutter/impeller/compositor/context.mm index 81f9fa0d90..fd8b85a6b7 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/command_buffer.h" #include "impeller/compositor/shader_library.h" namespace impeller { @@ -62,14 +63,6 @@ bool Context::IsValid() const { return is_valid_; } -id Context::GetRenderQueue() const { - return render_queue_; -} - -id Context::GetTransferQueue() const { - return transfer_queue_; -} - std::shared_ptr Context::GetShaderLibrary() const { return shader_library_; } @@ -78,4 +71,17 @@ std::shared_ptr Context::GetPipelineLibrary() const { return pipeline_library_; } +std::shared_ptr Context::CreateRenderCommandBuffer() const { + if (!IsValid()) { + return nullptr; + } + + auto buffer = + std::shared_ptr(new CommandBuffer(render_queue_)); + if (!buffer->IsValid()) { + return nullptr; + } + return buffer; +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/compositor/formats_metal.h b/engine/src/flutter/impeller/compositor/formats_metal.h index de69023df3..a93ab76771 100644 --- a/engine/src/flutter/impeller/compositor/formats_metal.h +++ b/engine/src/flutter/impeller/compositor/formats_metal.h @@ -10,6 +10,7 @@ #include "flutter/fml/macros.h" #include "impeller/compositor/formats.h" +#include "impeller/geometry/color.h" namespace impeller { @@ -142,6 +143,33 @@ constexpr MTLStencilOperation ToMTLStencilOperation(StencilOperation op) { return MTLStencilOperationKeep; }; +constexpr MTLLoadAction ToMTLLoadAction(LoadAction action) { + switch (action) { + case LoadAction::kDontCare: + return MTLLoadActionDontCare; + case LoadAction::kLoad: + return MTLLoadActionLoad; + case LoadAction::kClear: + return MTLLoadActionClear; + } + + return MTLLoadActionDontCare; +} + +constexpr MTLStoreAction ToMTLStoreAction(StoreAction action) { + switch (action) { + case StoreAction::kDontCare: + return MTLStoreActionDontCare; + case StoreAction::kStore: + return MTLStoreActionStore; + } + return MTLStoreActionDontCare; +} + +constexpr MTLClearColor ToMTLClearColor(const Color& color) { + return MTLClearColorMake(color.red, color.green, color.blue, color.alpha); +} + MTLRenderPipelineColorAttachmentDescriptor* ToMTLRenderPipelineColorAttachmentDescriptor( ColorAttachmentDescriptor descriptor); diff --git a/engine/src/flutter/impeller/compositor/render_pass.h b/engine/src/flutter/impeller/compositor/render_pass.h index d7444a3159..c01cbbb56d 100644 --- a/engine/src/flutter/impeller/compositor/render_pass.h +++ b/engine/src/flutter/impeller/compositor/render_pass.h @@ -4,6 +4,8 @@ #pragma once +#include + #include #include "flutter/fml/macros.h" @@ -13,6 +15,8 @@ namespace impeller { +class CommandBuffer; + struct RenderPassAttachment { std::shared_ptr texture; LoadAction load_action = LoadAction::kDontCare; @@ -48,20 +52,27 @@ class RenderPassDescriptor { RenderPassDescriptor& SetStencilAttachment( StencilRenderPassAttachment attachment); + MTLRenderPassDescriptor* ToMTLRenderPassDescriptor() const; + private: - std::map color_; + std::map colors_; std::optional depth_; std::optional stencil_; }; class RenderPass { public: - RenderPass(RenderPassDescriptor desc); - ~RenderPass(); + bool IsValid() const; + private: - RenderPassDescriptor desc_; + friend class CommandBuffer; + + id pass_ = nil; + bool is_valid_ = false; + + RenderPass(id buffer, const RenderPassDescriptor& desc); 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 05dad0b13d..06d9c81b98 100644 --- a/engine/src/flutter/impeller/compositor/render_pass.mm +++ b/engine/src/flutter/impeller/compositor/render_pass.mm @@ -4,6 +4,9 @@ #include "impeller/compositor/render_pass.h" +#include "flutter/fml/logging.h" +#include "impeller/compositor/formats_metal.h" + namespace impeller { RenderPassDescriptor::RenderPassDescriptor() = default; @@ -14,7 +17,7 @@ RenderPassDescriptor& RenderPassDescriptor::SetColorAttachment( ColorRenderPassAttachment attachment, size_t index) { if (attachment) { - color_[index] = attachment; + colors_[index] = attachment; } return *this; } @@ -35,8 +38,93 @@ RenderPassDescriptor& RenderPassDescriptor::SetStencilAttachment( return *this; } -RenderPass::RenderPass(RenderPassDescriptor desc) : desc_(std::move(desc)) {} +static bool ConfigureAttachment(const RenderPassAttachment& desc, + MTLRenderPassAttachmentDescriptor* attachment) { + if (!desc.texture) { + return false; + } + + attachment.texture = desc.texture->GetMTLTexture(); + attachment.loadAction = ToMTLLoadAction(desc.load_action); + attachment.storeAction = ToMTLStoreAction(desc.store_action); + return true; +} + +static bool ConfigureColorAttachment( + const ColorRenderPassAttachment& desc, + MTLRenderPassColorAttachmentDescriptor* attachment) { + if (!ConfigureAttachment(desc, attachment)) { + return false; + } + attachment.clearColor = ToMTLClearColor(desc.clear_color); + return true; +} + +static bool ConfigureDepthAttachment( + const DepthRenderPassAttachment& desc, + MTLRenderPassDepthAttachmentDescriptor* attachment) { + if (!ConfigureAttachment(desc, attachment)) { + return false; + } + attachment.clearDepth = desc.clear_depth; + return true; +} + +static bool ConfigureStencilAttachment( + const StencilRenderPassAttachment& desc, + MTLRenderPassStencilAttachmentDescriptor* attachment) { + if (!ConfigureAttachment(desc, attachment)) { + return false; + } + attachment.clearStencil = desc.clear_stencil; + return true; +} + +MTLRenderPassDescriptor* RenderPassDescriptor::ToMTLRenderPassDescriptor() + const { + auto result = [MTLRenderPassDescriptor renderPassDescriptor]; + + for (const auto& color : colors_) { + if (!ConfigureColorAttachment(color.second, + result.colorAttachments[color.first])) { + FML_LOG(ERROR) << "Could not configure color attachment at index " + << color.first; + return nil; + } + } + + if (depth_.has_value() && + !ConfigureDepthAttachment(depth_.value(), result.depthAttachment)) { + return nil; + } + + if (stencil_.has_value() && + !ConfigureStencilAttachment(stencil_.value(), result.stencilAttachment)) { + return nil; + } + + return result; +} + +RenderPass::RenderPass(id buffer, + const RenderPassDescriptor& desc) { + auto descriptor = desc.ToMTLRenderPassDescriptor(); + if (!descriptor) { + return; + } + + pass_ = [buffer renderCommandEncoderWithDescriptor:descriptor]; + if (!pass_) { + return; + } + + is_valid_ = true; +} RenderPass::~RenderPass() = default; +bool RenderPass::IsValid() const { + return is_valid_; +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/compositor/surface.mm b/engine/src/flutter/impeller/compositor/surface.mm index 7cfa292a11..adbd20c1d5 100644 --- a/engine/src/flutter/impeller/compositor/surface.mm +++ b/engine/src/flutter/impeller/compositor/surface.mm @@ -5,6 +5,7 @@ #include "impeller/compositor/surface.h" #include "flutter/fml/logging.h" +#include "impeller/compositor/command_buffer.h" namespace impeller { @@ -31,7 +32,7 @@ bool Surface::Render() const { return false; } - auto command_buffer = [context_->GetRenderQueue() commandBuffer]; + auto command_buffer = context_->CreateRenderCommandBuffer(); if (!command_buffer) { return false; @@ -39,12 +40,10 @@ bool Surface::Render() const { ::dispatch_semaphore_wait(frames_in_flight_sema_, DISPATCH_TIME_FOREVER); - __block dispatch_semaphore_t block_sema = frames_in_flight_sema_; - [command_buffer addCompletedHandler:^(id buffer) { - ::dispatch_semaphore_signal(block_sema); - }]; - - [command_buffer commit]; + command_buffer->Commit( + [sema = frames_in_flight_sema_](CommandBuffer::CommitResult) { + ::dispatch_semaphore_signal(sema); + }); return true; } diff --git a/engine/src/flutter/impeller/compositor/texture.h b/engine/src/flutter/impeller/compositor/texture.h index 0157c9d855..8e7c55c5b6 100644 --- a/engine/src/flutter/impeller/compositor/texture.h +++ b/engine/src/flutter/impeller/compositor/texture.h @@ -4,6 +4,8 @@ #pragma once +#include + #include "flutter/fml/macros.h" namespace impeller { @@ -14,7 +16,11 @@ class Texture { ~Texture(); + id GetMTLTexture() const; + private: + id texture_; + FML_DISALLOW_COPY_AND_ASSIGN(Texture); }; diff --git a/engine/src/flutter/impeller/compositor/texture.mm b/engine/src/flutter/impeller/compositor/texture.mm index c42458f417..dc9ea9130c 100644 --- a/engine/src/flutter/impeller/compositor/texture.mm +++ b/engine/src/flutter/impeller/compositor/texture.mm @@ -10,4 +10,8 @@ Texture::Texture() = default; Texture::~Texture() = default; +id Texture::GetMTLTexture() const { + return texture_; +} + } // namespace impeller