Wire up color attachment descriptors.

This commit is contained in:
Chinmay Garde
2021-05-26 12:19:09 -07:00
committed by Dan Field
parent 865b955371
commit 979456a10b
11 changed files with 415 additions and 1 deletions

View File

@@ -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",

View File

@@ -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 <Metal/Metal.h>
#include <string>
#include "flutter/fml/macros.h"
namespace impeller {
enum class StorageMode {
kHostCoherent,
kDevicePrivate,
};
class Context;
class Buffer;
class Allocator {
public:
~Allocator();
std::shared_ptr<Buffer> 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<MTLDevice> device_;
StorageMode mode_;
std::string allocator_label_;
Allocator(id<MTLDevice> device, StorageMode type, std::string label);
FML_DISALLOW_COPY_AND_ASSIGN(Allocator);
};
} // namespace impeller

View File

@@ -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<MTLDevice> 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<Buffer> 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<Buffer>(new Buffer(buffer, length, mode_, label));
}
} // namespace impeller

View File

@@ -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<MTLBuffer> buffer_;
const size_t size_;
const StorageMode mode_;
const std::string label_;
Buffer(id<MTLBuffer> buffer,
size_t size,
StorageMode mode,
std::string label);
FML_DISALLOW_COPY_AND_ASSIGN(Buffer);
};
} // namespace impeller

View File

@@ -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<MTLBuffer> buffer,
size_t size,
StorageMode mode,
std::string label)
: buffer_(buffer), size_(size), mode_(mode), label_(std::move(label)) {}
Buffer::~Buffer() = default;
} // namespace impeller

View File

@@ -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

View File

@@ -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 <cstdint>
#include <type_traits>
#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)
/// <color_blend_op>
/// (dst_color_blend_factor * old_color.rgb);
/// final_color.a = (src_alpha_blend_factor * new_color.a)
/// <alpha_blend_op>
/// (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<ColorWriteMask> 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

View File

@@ -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 <Metal/Metal.h>
#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<ColorWriteMask> type) {
using UnderlyingType = decltype(type);
MTLColorWriteMask mask = MTLColorWriteMaskNone;
if (type & static_cast<UnderlyingType>(ColorWriteMask::kRed)) {
mask |= MTLColorWriteMaskRed;
}
if (type & static_cast<UnderlyingType>(ColorWriteMask::kGreen)) {
mask |= MTLColorWriteMaskGreen;
}
if (type & static_cast<UnderlyingType>(ColorWriteMask::kBlue)) {
mask |= MTLColorWriteMaskBlue;
}
if (type & static_cast<UnderlyingType>(ColorWriteMask::kAlpha)) {
mask |= MTLColorWriteMaskAlpha;
}
return mask;
};
MTLRenderPipelineColorAttachmentDescriptor*
ToMTLRenderPipelineColorAttachmentDescriptor(
ColorAttachmentDescriptor descriptor);
} // namespace impeller

View File

@@ -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

View File

@@ -13,8 +13,10 @@
#include <string_view>
#include <type_traits>
#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> {
PipelineDescriptor& SetVertexDescriptor(
std::shared_ptr<VertexDescriptor> vertex_descriptor);
PipelineDescriptor& SetColorAttachmentDescriptor(
size_t index,
ColorAttachmentDescriptor desc);
MTLRenderPipelineDescriptor* GetMTLRenderPipelineDescriptor() const;
// Comparable<PipelineDescriptor>
@@ -50,6 +56,7 @@ class PipelineDescriptor : public Comparable<PipelineDescriptor> {
std::string label_;
size_t sample_count_ = 1;
std::map<ShaderStage, std::shared_ptr<const ShaderFunction>> entrypoints_;
std::map<size_t, ColorAttachmentDescriptor> color_attachment_descriptors_;
std::shared_ptr<VertexDescriptor> vertex_descriptor_;
};

View File

@@ -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;
}