Setup depth stencil state.

This commit is contained in:
Chinmay Garde
2021-05-26 19:05:19 -07:00
committed by Dan Field
parent ec219c627f
commit 97eee62317
8 changed files with 267 additions and 5 deletions

View File

@@ -5,6 +5,7 @@
#pragma once
#include <cstdint>
#include <functional>
#include <type_traits>
#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<impeller::DepthAttachmentDescriptor> {
constexpr std::size_t operator()(
const impeller::DepthAttachmentDescriptor& des) const {
return des.GetHash();
}
};
template <>
struct hash<impeller::StencilAttachmentDescriptor> {
constexpr std::size_t operator()(
const impeller::StencilAttachmentDescriptor& des) const {
return des.GetHash();
}
};
} // namespace std

View File

@@ -4,6 +4,8 @@
#pragma once
#include <optional>
#include <Metal/Metal.h>
#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<DepthAttachmentDescriptor> depth,
std::optional<StencilAttachmentDescriptor> front,
std::optional<StencilAttachmentDescriptor> back);
} // namespace impeller

View File

@@ -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<DepthAttachmentDescriptor> depth,
std::optional<StencilAttachmentDescriptor> front,
std::optional<StencilAttachmentDescriptor> 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

View File

@@ -26,8 +26,10 @@ class Pipeline {
Type type_ = Type::kUnknown;
id<MTLRenderPipelineState> state_;
id<MTLDepthStencilState> depth_stencil_state_;
Pipeline(id<MTLRenderPipelineState> state);
Pipeline(id<MTLRenderPipelineState> state,
id<MTLDepthStencilState> depth_stencil_state);
FML_DISALLOW_COPY_AND_ASSIGN(Pipeline);
};

View File

@@ -6,7 +6,9 @@
namespace impeller {
Pipeline::Pipeline(id<MTLRenderPipelineState> state) : state_(state) {
Pipeline::Pipeline(id<MTLRenderPipelineState> state,
id<MTLDepthStencilState> depth_stencil_state)
: state_(state), depth_stencil_state_(depth_stencil_state) {
if (state_ != nil) {
type_ = Type::kRender;
}

View File

@@ -44,10 +44,23 @@ class PipelineDescriptor : public Comparable<PipelineDescriptor> {
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<MTLDepthStencilState> CreateDepthStencilDescriptor(
id<MTLDevice> device) const;
// Comparable<PipelineDescriptor>
std::size_t GetHash() const override;
@@ -61,6 +74,11 @@ class PipelineDescriptor : public Comparable<PipelineDescriptor> {
std::map<size_t, ColorAttachmentDescriptor> color_attachment_descriptors_;
std::shared_ptr<VertexDescriptor> vertex_descriptor_;
PixelFormat depth_stencil_pixel_format_ = PixelFormat::kUnknown;
std::optional<DepthAttachmentDescriptor> depth_attachment_descriptor_;
std::optional<StencilAttachmentDescriptor>
front_stencil_attachment_descriptor_;
std::optional<StencilAttachmentDescriptor>
back_stencil_attachment_descriptor_;
};
} // namespace impeller

View File

@@ -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<MTLDepthStencilState> PipelineDescriptor::CreateDepthStencilDescriptor(
id<MTLDevice> device) const {
auto descriptor =
ToMTLDepthStencilDescriptor(depth_attachment_descriptor_, //
front_stencil_attachment_descriptor_, //
back_stencil_attachment_descriptor_ //
);
return [device newDepthStencilStateWithDescriptor:descriptor];
}
} // namespace impeller

View File

@@ -33,8 +33,9 @@ std::future<std::shared_ptr<Pipeline>> PipelineLibrary::GetRenderPipeline(
<< error.localizedDescription.UTF8String;
promise->set_value(nullptr);
} else {
auto new_pipeline =
std::shared_ptr<Pipeline>(new Pipeline(render_pipeline_state));
auto new_pipeline = std::shared_ptr<Pipeline>(
new Pipeline(render_pipeline_state,
descriptor.CreateDepthStencilDescriptor(device_)));
promise->set_value(new_pipeline);
this->SavePipeline(descriptor, new_pipeline);
}