diff --git a/engine/src/flutter/impeller/impeller/compositor/BUILD.gn b/engine/src/flutter/impeller/impeller/compositor/BUILD.gn index a23f1db99e..f725371a42 100644 --- a/engine/src/flutter/impeller/impeller/compositor/BUILD.gn +++ b/engine/src/flutter/impeller/impeller/compositor/BUILD.gn @@ -6,10 +6,18 @@ import("//flutter/impeller/tools/impeller.gni") impeller_component("compositor") { sources = [ + "allocator.h", + "allocator.mm", + "buffer.h", + "buffer.mm", "comparable.cc", "comparable.h", "context.h", "context.mm", + "formats.cc", + "formats.h", + "formats_metal.h", + "formats_metal.mm", "pipeline.h", "pipeline.mm", "pipeline_descriptor.h", diff --git a/engine/src/flutter/impeller/impeller/compositor/allocator.h b/engine/src/flutter/impeller/impeller/compositor/allocator.h new file mode 100644 index 0000000000..8ad4913152 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/compositor/allocator.h @@ -0,0 +1,44 @@ +// 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 + +#include + +#include "flutter/fml/macros.h" + +namespace impeller { + +enum class StorageMode { + kHostCoherent, + kDevicePrivate, +}; + +class Context; +class Buffer; + +class Allocator { + public: + ~Allocator(); + + std::shared_ptr CreateBuffer(size_t length, std::string label); + + private: + friend class Context; + + // In the prototype, we are going to be allocating resources directly with the + // MTLDevice APIs. But, in the future, this could be backed by named heaps + // with specific limits. + id device_; + StorageMode mode_; + std::string allocator_label_; + + Allocator(id device, StorageMode type, std::string label); + + FML_DISALLOW_COPY_AND_ASSIGN(Allocator); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/allocator.mm b/engine/src/flutter/impeller/impeller/compositor/allocator.mm new file mode 100644 index 0000000000..496160ef08 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/compositor/allocator.mm @@ -0,0 +1,37 @@ +// 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/allocator.h" + +#include "impeller/compositor/buffer.h" + +namespace impeller { + +Allocator::Allocator(id device, StorageMode type, std::string label) + : device_(device), mode_(type), allocator_label_(std::move(label)) {} + +Allocator::~Allocator() = default; + +static MTLResourceOptions ResourceOptionsFromStorageType(StorageMode type) { + switch (type) { + case StorageMode::kHostCoherent: + return MTLResourceStorageModeManaged; + case StorageMode::kDevicePrivate: + return MTLResourceStorageModePrivate; + } +} + +std::shared_ptr Allocator::CreateBuffer(size_t length, + std::string label) { + auto buffer = + [device_ newBufferWithLength:length + options:ResourceOptionsFromStorageType(mode_)]; + if (!buffer) { + return nullptr; + } + buffer.label = @(label.c_str()); + return std::shared_ptr(new Buffer(buffer, length, mode_, label)); +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/buffer.h b/engine/src/flutter/impeller/impeller/compositor/buffer.h new file mode 100644 index 0000000000..ecaeeb32e3 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/compositor/buffer.h @@ -0,0 +1,32 @@ +// 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/allocator.h" + +namespace impeller { + +class Buffer { + public: + ~Buffer(); + + private: + friend class Allocator; + + 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); + + FML_DISALLOW_COPY_AND_ASSIGN(Buffer); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/buffer.mm b/engine/src/flutter/impeller/impeller/compositor/buffer.mm new file mode 100644 index 0000000000..01c5fed7fe --- /dev/null +++ b/engine/src/flutter/impeller/impeller/compositor/buffer.mm @@ -0,0 +1,17 @@ +// 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.h" + +namespace impeller { + +Buffer::Buffer(id buffer, + size_t size, + StorageMode mode, + std::string label) + : buffer_(buffer), size_(size), mode_(mode), label_(std::move(label)) {} + +Buffer::~Buffer() = default; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/formats.cc b/engine/src/flutter/impeller/impeller/compositor/formats.cc new file mode 100644 index 0000000000..c992bc4abc --- /dev/null +++ b/engine/src/flutter/impeller/impeller/compositor/formats.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/compositor/formats.h" + +namespace impeller { + +// + +} // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/formats.h b/engine/src/flutter/impeller/impeller/compositor/formats.h new file mode 100644 index 0000000000..a91076610f --- /dev/null +++ b/engine/src/flutter/impeller/impeller/compositor/formats.h @@ -0,0 +1,104 @@ +// 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 +#include + +#include "flutter/fml/hash_combine.h" +#include "flutter/fml/macros.h" + +namespace impeller { + +enum class PixelFormat { + kUnknown, +}; + +enum class BlendFactor { + kZero, + kOne, + kSourceColor, + kOneMinusSourceColor, + kSourceAlpha, + kOneMinusSourceAlpha, + kDestinationColor, + kOneMinusDestinationColor, + kDestinationAlpha, + kOneMinusDestinationAlpha, + kSourceAlphaSaturated, + kBlendColor, + kOneMinusBlendColor, + kBlendAlpha, + kOneMinusBlendAlpha, +}; + +enum class BlendOperation { + kAdd, + kSubtract, + kReverseSubtract, + kMin, + kMax, +}; + +enum class ColorWriteMask : uint64_t { + kNone = 0, + kRed = 1 << 0, + kGreen = 1 << 1, + kBlue = 1 << 2, + kAlpha = 1 << 3, + kAll = kRed | kGreen | kBlue, +}; + +struct ColorAttachmentDescriptor { + PixelFormat format = PixelFormat::kUnknown; + bool blending_enabled = false; + + //---------------------------------------------------------------------------- + /// Blending at specific color attachments follows the pseudocode: + /// ``` + /// if (blending_enabled) { + /// final_color.rgb = (src_color_blend_factor * new_color.rgb) + /// + /// (dst_color_blend_factor * old_color.rgb); + /// final_color.a = (src_alpha_blend_factor * new_color.a) + /// + /// (dst_alpha_blend_factor * old_color.a); + /// } else { + /// final_color = new_color; + /// } + /// final_color = final_color & write_mask; + /// ``` + + BlendFactor src_color_blend_factor; + BlendOperation color_blend_op; + BlendFactor dst_color_blend_factor; + + BlendFactor src_alpha_blend_factor; + BlendOperation alpha_blend_op; + BlendFactor dst_alpha_blend_factor; + + std::underlying_type_t write_mask; + + constexpr bool operator==(const ColorAttachmentDescriptor& o) const { + return format == o.format && // + blending_enabled == o.blending_enabled && // + src_color_blend_factor == o.src_color_blend_factor && // + color_blend_op == o.color_blend_op && // + dst_color_blend_factor == o.dst_color_blend_factor && // + src_alpha_blend_factor == o.src_alpha_blend_factor && // + alpha_blend_op == o.alpha_blend_op && // + dst_alpha_blend_factor == o.dst_alpha_blend_factor && // + write_mask == o.write_mask; + } + + constexpr size_t Hash() const { + return fml::HashCombine(format, blending_enabled, src_color_blend_factor, + color_blend_op, dst_color_blend_factor, + src_alpha_blend_factor, alpha_blend_op, + dst_alpha_blend_factor, write_mask); + } +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/formats_metal.h b/engine/src/flutter/impeller/impeller/compositor/formats_metal.h new file mode 100644 index 0000000000..1a3dfa039b --- /dev/null +++ b/engine/src/flutter/impeller/impeller/compositor/formats_metal.h @@ -0,0 +1,103 @@ +// 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 + +#include "flutter/fml/macros.h" +#include "impeller/compositor/formats.h" + +namespace impeller { + +constexpr MTLPixelFormat ToMTLPixelFormat(PixelFormat format) { + switch (format) { + case PixelFormat::kUnknown: + return MTLPixelFormatInvalid; + } + return MTLPixelFormatInvalid; +}; + +constexpr MTLBlendFactor ToMTLBlendFactor(BlendFactor type) { + switch (type) { + case BlendFactor::kZero: + return MTLBlendFactorZero; + case BlendFactor::kOne: + return MTLBlendFactorOne; + case BlendFactor::kSourceColor: + return MTLBlendFactorSourceColor; + case BlendFactor::kOneMinusSourceColor: + return MTLBlendFactorOneMinusSourceColor; + case BlendFactor::kSourceAlpha: + return MTLBlendFactorSourceAlpha; + case BlendFactor::kOneMinusSourceAlpha: + return MTLBlendFactorOneMinusSourceAlpha; + case BlendFactor::kDestinationColor: + return MTLBlendFactorDestinationColor; + case BlendFactor::kOneMinusDestinationColor: + return MTLBlendFactorOneMinusDestinationColor; + case BlendFactor::kDestinationAlpha: + return MTLBlendFactorDestinationAlpha; + case BlendFactor::kOneMinusDestinationAlpha: + return MTLBlendFactorOneMinusDestinationAlpha; + case BlendFactor::kSourceAlphaSaturated: + return MTLBlendFactorSourceAlphaSaturated; + case BlendFactor::kBlendColor: + return MTLBlendFactorBlendColor; + case BlendFactor::kOneMinusBlendColor: + return MTLBlendFactorOneMinusBlendColor; + case BlendFactor::kBlendAlpha: + return MTLBlendFactorBlendAlpha; + case BlendFactor::kOneMinusBlendAlpha: + return MTLBlendFactorOneMinusBlendAlpha; + } + return MTLBlendFactorZero; +}; + +constexpr MTLBlendOperation ToMTLBlendOperation(BlendOperation type) { + switch (type) { + case BlendOperation::kAdd: + return MTLBlendOperationAdd; + case BlendOperation::kSubtract: + return MTLBlendOperationSubtract; + case BlendOperation::kReverseSubtract: + return MTLBlendOperationReverseSubtract; + case BlendOperation::kMin: + return MTLBlendOperationMin; + case BlendOperation::kMax: + return MTLBlendOperationMax; + } + return MTLBlendOperationAdd; +}; + +constexpr MTLColorWriteMask ToMTLColorWriteMask( + std::underlying_type_t type) { + using UnderlyingType = decltype(type); + + MTLColorWriteMask mask = MTLColorWriteMaskNone; + + if (type & static_cast(ColorWriteMask::kRed)) { + mask |= MTLColorWriteMaskRed; + } + + if (type & static_cast(ColorWriteMask::kGreen)) { + mask |= MTLColorWriteMaskGreen; + } + + if (type & static_cast(ColorWriteMask::kBlue)) { + mask |= MTLColorWriteMaskBlue; + } + + if (type & static_cast(ColorWriteMask::kAlpha)) { + mask |= MTLColorWriteMaskAlpha; + } + + return mask; +}; + +MTLRenderPipelineColorAttachmentDescriptor* +ToMTLRenderPipelineColorAttachmentDescriptor( + ColorAttachmentDescriptor descriptor); + +} // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/formats_metal.mm b/engine/src/flutter/impeller/impeller/compositor/formats_metal.mm new file mode 100644 index 0000000000..6efd3115b4 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/compositor/formats_metal.mm @@ -0,0 +1,33 @@ +// 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/formats_metal.h" + +namespace impeller { + +MTLRenderPipelineColorAttachmentDescriptor* +ToMTLRenderPipelineColorAttachmentDescriptor( + ColorAttachmentDescriptor descriptor) { + auto des = [[MTLRenderPipelineColorAttachmentDescriptor alloc] init]; + des.pixelFormat = ToMTLPixelFormat(descriptor.format); + + des.blendingEnabled = descriptor.blending_enabled; + + des.sourceRGBBlendFactor = + ToMTLBlendFactor(descriptor.src_color_blend_factor); + des.rgbBlendOperation = ToMTLBlendOperation(descriptor.color_blend_op); + des.destinationRGBBlendFactor = + ToMTLBlendFactor(descriptor.dst_color_blend_factor); + + des.sourceAlphaBlendFactor = + ToMTLBlendFactor(descriptor.src_alpha_blend_factor); + des.alphaBlendOperation = ToMTLBlendOperation(descriptor.alpha_blend_op); + des.destinationAlphaBlendFactor = + ToMTLBlendFactor(descriptor.dst_alpha_blend_factor); + + des.writeMask = ToMTLColorWriteMask(descriptor.write_mask); + return des; +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.h b/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.h index 0370aee54e..8cddcc0065 100644 --- a/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.h +++ b/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.h @@ -13,8 +13,10 @@ #include #include +#include "flutter/fml/hash_combine.h" #include "flutter/fml/macros.h" #include "impeller/compositor/comparable.h" +#include "impeller/compositor/formats.h" #include "impeller/shader_glue/shader_types.h" namespace impeller { @@ -38,6 +40,10 @@ class PipelineDescriptor : public Comparable { PipelineDescriptor& SetVertexDescriptor( std::shared_ptr vertex_descriptor); + PipelineDescriptor& SetColorAttachmentDescriptor( + size_t index, + ColorAttachmentDescriptor desc); + MTLRenderPipelineDescriptor* GetMTLRenderPipelineDescriptor() const; // Comparable @@ -50,6 +56,7 @@ class PipelineDescriptor : public Comparable { std::string label_; size_t sample_count_ = 1; std::map> entrypoints_; + std::map color_attachment_descriptors_; std::shared_ptr vertex_descriptor_; }; diff --git a/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.mm b/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.mm index 7cb9098f6c..b133df4a72 100644 --- a/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.mm +++ b/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.mm @@ -4,6 +4,7 @@ #include "impeller/compositor/pipeline_descriptor.h" +#include "impeller/compositor/formats_metal.h" #include "impeller/compositor/shader_library.h" #include "impeller/compositor/vertex_descriptor.h" @@ -27,6 +28,10 @@ std::size_t PipelineDescriptor::GetHash() const { if (vertex_descriptor_) { fml::HashCombineSeed(seed, vertex_descriptor_->GetHash()); } + for (const auto& des : color_attachment_descriptors_) { + fml::HashCombineSeed(seed, des.first); + fml::HashCombineSeed(seed, des.second.Hash()); + } return seed; } @@ -34,7 +39,8 @@ std::size_t PipelineDescriptor::GetHash() const { bool PipelineDescriptor::IsEqual(const PipelineDescriptor& other) const { return label_ == other.label_ && sample_count_ == other.sample_count_ && DeepCompareMap(entrypoints_, other.entrypoints_) && - DeepComparePointer(vertex_descriptor_, other.vertex_descriptor_); + DeepComparePointer(vertex_descriptor_, other.vertex_descriptor_) && + color_attachment_descriptors_ == other.color_attachment_descriptors_; } PipelineDescriptor& PipelineDescriptor::SetLabel( @@ -69,6 +75,13 @@ PipelineDescriptor& PipelineDescriptor::SetVertexDescriptor( return *this; } +PipelineDescriptor& PipelineDescriptor::SetColorAttachmentDescriptor( + size_t index, + ColorAttachmentDescriptor desc) { + color_attachment_descriptors_[index] = std::move(desc); + return *this; +} + MTLRenderPipelineDescriptor* PipelineDescriptor::GetMTLRenderPipelineDescriptor() const { auto descriptor = [[MTLRenderPipelineDescriptor alloc] init]; @@ -88,6 +101,11 @@ PipelineDescriptor::GetMTLRenderPipelineDescriptor() const { descriptor.vertexDescriptor = vertex_descriptor_->GetMTLVertexDescriptor(); } + for (const auto& item : color_attachment_descriptors_) { + descriptor.colorAttachments[item.first] = + ToMTLRenderPipelineColorAttachmentDescriptor(item.second); + } + return descriptor; }