From 97eee62317f1c64b728643f826d5e34e3d13b799 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Wed, 26 May 2021 19:05:19 -0700 Subject: [PATCH] Setup depth stencil state. --- .../impeller/impeller/compositor/formats.h | 108 ++++++++++++++++++ .../impeller/compositor/formats_metal.h | 51 +++++++++ .../impeller/compositor/formats_metal.mm | 43 +++++++ .../impeller/impeller/compositor/pipeline.h | 4 +- .../impeller/impeller/compositor/pipeline.mm | 4 +- .../impeller/compositor/pipeline_descriptor.h | 18 +++ .../compositor/pipeline_descriptor.mm | 39 ++++++- .../impeller/compositor/pipeline_library.mm | 5 +- 8 files changed, 267 insertions(+), 5 deletions(-) diff --git a/engine/src/flutter/impeller/impeller/compositor/formats.h b/engine/src/flutter/impeller/impeller/compositor/formats.h index a91076610f..fd52c4a325 100644 --- a/engine/src/flutter/impeller/impeller/compositor/formats.h +++ b/engine/src/flutter/impeller/impeller/compositor/formats.h @@ -5,6 +5,7 @@ #pragma once #include +#include #include #include "flutter/fml/hash_combine.h" @@ -101,4 +102,111 @@ struct ColorAttachmentDescriptor { } }; +enum class CompareFunction { + kNever, + kLess, + kEqual, + kLessEqual, + kGreater, + kNotEqual, + kGreaterEqual, + kAlways, +}; + +enum class StencilOperation { + kKeep, + kZero, + kReplace, + kIncrementClamp, + kDecrementClamp, + kInvert, + kIncrementWrap, + kDecrementWrap, +}; + +struct DepthAttachmentDescriptor { + //---------------------------------------------------------------------------- + /// Indicates how to compare the value with that in the depth buffer. + /// + CompareFunction depth_compare; + //---------------------------------------------------------------------------- + /// Indicates when writes must be performed to the depth buffer. + /// + bool depth_write_enabled; + + constexpr bool operator==(const DepthAttachmentDescriptor& o) const { + return depth_compare == o.depth_compare && + depth_write_enabled == o.depth_write_enabled; + } + + constexpr size_t GetHash() const { + return fml::HashCombine(depth_compare, depth_write_enabled); + } +}; + +struct StencilAttachmentDescriptor { + //---------------------------------------------------------------------------- + /// Indicates the operation to perform between the reference value and the + /// value in the stencil buffer. Both values have the read_mask applied to + /// them before performing this operation. + /// + CompareFunction stencil_compare; + //---------------------------------------------------------------------------- + /// Indicates what to do when the stencil test has failed. + /// + StencilOperation stencil_failure; + //---------------------------------------------------------------------------- + /// Indicates what to do when the stencil test passes but the depth test + /// fails. + /// + StencilOperation depth_failure; + //---------------------------------------------------------------------------- + /// Indicates what to do when both the stencil and depth tests pass. + /// + StencilOperation depth_stencil_pass; + //---------------------------------------------------------------------------- + /// The mask applied to the reference and stencil buffer values before + /// performing the stencil_compare operation. + /// + uint32_t read_mask; + //---------------------------------------------------------------------------- + /// The mask applied to the new stencil value before it is written into the + /// stencil buffer. + /// + uint32_t write_mask; + + constexpr bool operator==(const StencilAttachmentDescriptor& o) const { + return stencil_compare == o.stencil_compare && + stencil_failure == o.stencil_failure && + depth_failure == o.depth_failure && + depth_stencil_pass == o.depth_stencil_pass && + read_mask == o.read_mask && write_mask == o.write_mask; + } + + constexpr size_t GetHash() const { + return fml::HashCombine(stencil_compare, stencil_failure, depth_failure, + depth_stencil_pass, read_mask); + } +}; + } // namespace impeller + +namespace std { + +template <> +struct hash { + constexpr std::size_t operator()( + const impeller::DepthAttachmentDescriptor& des) const { + return des.GetHash(); + } +}; + +template <> +struct hash { + constexpr std::size_t operator()( + const impeller::StencilAttachmentDescriptor& des) const { + return des.GetHash(); + } +}; + +} // namespace std diff --git a/engine/src/flutter/impeller/impeller/compositor/formats_metal.h b/engine/src/flutter/impeller/impeller/compositor/formats_metal.h index 1a3dfa039b..de69023df3 100644 --- a/engine/src/flutter/impeller/impeller/compositor/formats_metal.h +++ b/engine/src/flutter/impeller/impeller/compositor/formats_metal.h @@ -4,6 +4,8 @@ #pragma once +#include + #include #include "flutter/fml/macros.h" @@ -96,8 +98,57 @@ constexpr MTLColorWriteMask ToMTLColorWriteMask( return mask; }; +constexpr MTLCompareFunction ToMTLCompareFunction(CompareFunction func) { + switch (func) { + case CompareFunction::kNever: + return MTLCompareFunctionNever; + case CompareFunction::kLess: + return MTLCompareFunctionLess; + case CompareFunction::kEqual: + return MTLCompareFunctionEqual; + case CompareFunction::kLessEqual: + return MTLCompareFunctionLessEqual; + case CompareFunction::kGreater: + return MTLCompareFunctionGreater; + case CompareFunction::kNotEqual: + return MTLCompareFunctionNotEqual; + case CompareFunction::kGreaterEqual: + return MTLCompareFunctionGreaterEqual; + case CompareFunction::kAlways: + return MTLCompareFunctionAlways; + } + return MTLCompareFunctionAlways; +}; + +constexpr MTLStencilOperation ToMTLStencilOperation(StencilOperation op) { + switch (op) { + case StencilOperation::kKeep: + return MTLStencilOperationKeep; + case StencilOperation::kZero: + return MTLStencilOperationZero; + case StencilOperation::kReplace: + return MTLStencilOperationReplace; + case StencilOperation::kIncrementClamp: + return MTLStencilOperationIncrementClamp; + case StencilOperation::kDecrementClamp: + return MTLStencilOperationDecrementClamp; + case StencilOperation::kInvert: + return MTLStencilOperationInvert; + case StencilOperation::kIncrementWrap: + return MTLStencilOperationIncrementWrap; + case StencilOperation::kDecrementWrap: + return MTLStencilOperationDecrementWrap; + } + return MTLStencilOperationKeep; +}; + MTLRenderPipelineColorAttachmentDescriptor* ToMTLRenderPipelineColorAttachmentDescriptor( ColorAttachmentDescriptor descriptor); +MTLDepthStencilDescriptor* ToMTLDepthStencilDescriptor( + std::optional depth, + std::optional front, + std::optional back); + } // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/formats_metal.mm b/engine/src/flutter/impeller/impeller/compositor/formats_metal.mm index 6efd3115b4..d5411ba008 100644 --- a/engine/src/flutter/impeller/impeller/compositor/formats_metal.mm +++ b/engine/src/flutter/impeller/impeller/compositor/formats_metal.mm @@ -30,4 +30,47 @@ ToMTLRenderPipelineColorAttachmentDescriptor( return des; } +MTLStencilDescriptor* ToMTLStencilDescriptor( + const StencilAttachmentDescriptor& descriptor) { + auto des = [[MTLStencilDescriptor alloc] init]; + des.stencilCompareFunction = ToMTLCompareFunction(descriptor.stencil_compare); + des.stencilFailureOperation = + ToMTLStencilOperation(descriptor.stencil_failure); + des.depthFailureOperation = ToMTLStencilOperation(descriptor.depth_failure); + des.depthStencilPassOperation = + ToMTLStencilOperation(descriptor.depth_stencil_pass); + + des.readMask = descriptor.read_mask; + des.writeMask = descriptor.write_mask; + + return des; +} + +MTLDepthStencilDescriptor* ToMTLDepthStencilDescriptor( + std::optional depth, + std::optional front, + std::optional back) { + if (!depth) { + depth = DepthAttachmentDescriptor{ + // Always pass the depth test. + .depth_compare = CompareFunction::kAlways, + .depth_write_enabled = false, + }; + } + + auto des = [[MTLDepthStencilDescriptor alloc] init]; + + des.depthCompareFunction = ToMTLCompareFunction(depth->depth_compare); + des.depthWriteEnabled = depth->depth_write_enabled; + + if (front.has_value()) { + des.frontFaceStencil = ToMTLStencilDescriptor(front.value()); + } + if (back.has_value()) { + des.backFaceStencil = ToMTLStencilDescriptor(back.value()); + } + + return des; +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/pipeline.h b/engine/src/flutter/impeller/impeller/compositor/pipeline.h index e3f75d7b4e..064a2d04d7 100644 --- a/engine/src/flutter/impeller/impeller/compositor/pipeline.h +++ b/engine/src/flutter/impeller/impeller/compositor/pipeline.h @@ -26,8 +26,10 @@ class Pipeline { Type type_ = Type::kUnknown; id state_; + id depth_stencil_state_; - Pipeline(id state); + Pipeline(id state, + id depth_stencil_state); FML_DISALLOW_COPY_AND_ASSIGN(Pipeline); }; diff --git a/engine/src/flutter/impeller/impeller/compositor/pipeline.mm b/engine/src/flutter/impeller/impeller/compositor/pipeline.mm index a682a77d0c..c0822ad272 100644 --- a/engine/src/flutter/impeller/impeller/compositor/pipeline.mm +++ b/engine/src/flutter/impeller/impeller/compositor/pipeline.mm @@ -6,7 +6,9 @@ namespace impeller { -Pipeline::Pipeline(id state) : state_(state) { +Pipeline::Pipeline(id state, + id depth_stencil_state) + : state_(state), depth_stencil_state_(depth_stencil_state) { if (state_ != nil) { type_ = Type::kRender; } diff --git a/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.h b/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.h index e565f23199..3c68996c78 100644 --- a/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.h +++ b/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.h @@ -44,10 +44,23 @@ class PipelineDescriptor : public Comparable { size_t index, ColorAttachmentDescriptor desc); + PipelineDescriptor& SetDepthStencilAttachmentDescriptor( + DepthAttachmentDescriptor desc); + + PipelineDescriptor& SetStencilAttachmentDescriptors( + StencilAttachmentDescriptor front_and_back); + + PipelineDescriptor& SetStencilAttachmentDescriptors( + StencilAttachmentDescriptor front, + StencilAttachmentDescriptor back); + PipelineDescriptor& SetDepthStencilPixelFormat(PixelFormat format); MTLRenderPipelineDescriptor* GetMTLRenderPipelineDescriptor() const; + id CreateDepthStencilDescriptor( + id device) const; + // Comparable std::size_t GetHash() const override; @@ -61,6 +74,11 @@ class PipelineDescriptor : public Comparable { std::map color_attachment_descriptors_; std::shared_ptr vertex_descriptor_; PixelFormat depth_stencil_pixel_format_ = PixelFormat::kUnknown; + std::optional depth_attachment_descriptor_; + std::optional + front_stencil_attachment_descriptor_; + std::optional + back_stencil_attachment_descriptor_; }; } // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.mm b/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.mm index b983f523bc..916c2ec463 100644 --- a/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.mm +++ b/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.mm @@ -33,6 +33,9 @@ std::size_t PipelineDescriptor::GetHash() const { fml::HashCombineSeed(seed, vertex_descriptor_->GetHash()); } fml::HashCombineSeed(seed, depth_stencil_pixel_format_); + fml::HashCombineSeed(seed, depth_attachment_descriptor_); + fml::HashCombineSeed(seed, front_stencil_attachment_descriptor_); + fml::HashCombineSeed(seed, back_stencil_attachment_descriptor_); return seed; } @@ -42,7 +45,12 @@ bool PipelineDescriptor::IsEqual(const PipelineDescriptor& other) const { DeepCompareMap(entrypoints_, other.entrypoints_) && color_attachment_descriptors_ == other.color_attachment_descriptors_ && DeepComparePointer(vertex_descriptor_, other.vertex_descriptor_) && - depth_stencil_pixel_format_ == other.depth_stencil_pixel_format_; + depth_stencil_pixel_format_ == other.depth_stencil_pixel_format_ && + depth_attachment_descriptor_ == other.depth_attachment_descriptor_ && + front_stencil_attachment_descriptor_ == + other.front_stencil_attachment_descriptor_ && + back_stencil_attachment_descriptor_ == + other.back_stencil_attachment_descriptor_; } PipelineDescriptor& PipelineDescriptor::SetLabel( @@ -90,6 +98,25 @@ PipelineDescriptor& PipelineDescriptor::SetDepthStencilPixelFormat( return *this; } +PipelineDescriptor& PipelineDescriptor::SetDepthStencilAttachmentDescriptor( + DepthAttachmentDescriptor desc) { + depth_attachment_descriptor_ = desc; + return *this; +} + +PipelineDescriptor& PipelineDescriptor::SetStencilAttachmentDescriptors( + StencilAttachmentDescriptor front_and_back) { + return SetStencilAttachmentDescriptors(front_and_back, front_and_back); +} + +PipelineDescriptor& PipelineDescriptor::SetStencilAttachmentDescriptors( + StencilAttachmentDescriptor front, + StencilAttachmentDescriptor back) { + front_stencil_attachment_descriptor_ = front; + back_stencil_attachment_descriptor_ = back; + return *this; +} + MTLRenderPipelineDescriptor* PipelineDescriptor::GetMTLRenderPipelineDescriptor() const { auto descriptor = [[MTLRenderPipelineDescriptor alloc] init]; @@ -122,4 +149,14 @@ PipelineDescriptor::GetMTLRenderPipelineDescriptor() const { return descriptor; } +id PipelineDescriptor::CreateDepthStencilDescriptor( + id device) const { + auto descriptor = + ToMTLDepthStencilDescriptor(depth_attachment_descriptor_, // + front_stencil_attachment_descriptor_, // + back_stencil_attachment_descriptor_ // + ); + return [device newDepthStencilStateWithDescriptor:descriptor]; +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/pipeline_library.mm b/engine/src/flutter/impeller/impeller/compositor/pipeline_library.mm index 6ede19f03f..1c6ff24727 100644 --- a/engine/src/flutter/impeller/impeller/compositor/pipeline_library.mm +++ b/engine/src/flutter/impeller/impeller/compositor/pipeline_library.mm @@ -33,8 +33,9 @@ std::future> PipelineLibrary::GetRenderPipeline( << error.localizedDescription.UTF8String; promise->set_value(nullptr); } else { - auto new_pipeline = - std::shared_ptr(new Pipeline(render_pipeline_state)); + auto new_pipeline = std::shared_ptr( + new Pipeline(render_pipeline_state, + descriptor.CreateDepthStencilDescriptor(device_))); promise->set_value(new_pipeline); this->SavePipeline(descriptor, new_pipeline); }