Add stubs for backend library.

This commit is contained in:
Chinmay Garde
2021-10-20 11:33:08 -07:00
committed by Dan Field
parent d4308e38ee
commit 1401218afc
27 changed files with 638 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
# 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.
import("//flutter/impeller/tools/impeller.gni")
impeller_component("metal") {
sources = []
}

View File

@@ -0,0 +1,22 @@
// 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/renderer/allocator.h"
namespace impeller {
class AllocatorMTL final : public Allocator {
public:
AllocatorMTL();
~AllocatorMTL() override;
private:
FML_DISALLOW_COPY_AND_ASSIGN(AllocatorMTL);
};
} // namespace impeller

View File

@@ -0,0 +1,22 @@
// 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/renderer/command_buffer.h"
namespace impeller {
class CommabdBufferMTL final : public CommabdBuffer {
public:
CommabdBufferMTL();
~CommabdBufferMTL() override;
private:
FML_DISALLOW_COPY_AND_ASSIGN(CommabdBufferMTL);
};
} // namespace impeller

View File

@@ -0,0 +1,22 @@
// 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/renderer/context.h"
namespace impeller {
class ContextMTL final : public Context {
public:
ContextMTL();
~ContextMTL() override;
private:
FML_DISALLOW_COPY_AND_ASSIGN(ContextMTL);
};
} // namespace impeller

View File

@@ -0,0 +1,22 @@
// 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/renderer/device_buffer.h"
namespace impeller {
class DeviceBufferMTL final : public DeviceBuffer {
public:
DeviceBufferMTL();
~DeviceBufferMTL() override;
private:
FML_DISALLOW_COPY_AND_ASSIGN(DeviceBufferMTL);
};
} // namespace impeller

View File

@@ -0,0 +1,263 @@
// 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 <optional>
#include <Metal/Metal.h>
#include "flutter/fml/macros.h"
#include "impeller/geometry/color.h"
#include "impeller/renderer/formats.h"
#include "impeller/renderer/texture_descriptor.h"
namespace impeller {
class RenderPassDescriptor;
constexpr MTLPixelFormat ToMTLPixelFormat(PixelFormat format) {
switch (format) {
case PixelFormat::kUnknown:
return MTLPixelFormatInvalid;
case PixelFormat::kB8G8R8A8UNormInt:
return MTLPixelFormatBGRA8Unorm;
case PixelFormat::kB8G8R8A8UNormIntSRGB:
return MTLPixelFormatBGRA8Unorm_sRGB;
case PixelFormat::kD32FloatS8UNormInt:
return MTLPixelFormatDepth32Float_Stencil8;
case PixelFormat::kR8G8B8A8UNormInt:
return MTLPixelFormatRGBA8Unorm;
case PixelFormat::kR8G8B8A8UNormIntSRGB:
return MTLPixelFormatRGBA8Unorm_sRGB;
}
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 MTLPrimitiveType ToMTLPrimitiveType(PrimitiveType type) {
switch (type) {
case PrimitiveType::kTriangle:
return MTLPrimitiveTypeTriangle;
case PrimitiveType::kTriangleStrip:
return MTLPrimitiveTypeTriangleStrip;
case PrimitiveType::kLine:
return MTLPrimitiveTypeLine;
case PrimitiveType::kLineStrip:
return MTLPrimitiveTypeLineStrip;
case PrimitiveType::kPoint:
return MTLPrimitiveTypePoint;
}
return MTLPrimitiveTypePoint;
}
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;
};
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::kSetToReferneceValue:
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;
};
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 LoadAction FromMTLLoadAction(MTLLoadAction action) {
switch (action) {
case MTLLoadActionDontCare:
return LoadAction::kDontCare;
case MTLLoadActionLoad:
return LoadAction::kLoad;
case MTLLoadActionClear:
return LoadAction::kClear;
default:
break;
}
return LoadAction::kDontCare;
}
constexpr MTLStoreAction ToMTLStoreAction(StoreAction action) {
switch (action) {
case StoreAction::kDontCare:
return MTLStoreActionDontCare;
case StoreAction::kStore:
return MTLStoreActionStore;
}
return MTLStoreActionDontCare;
}
constexpr StoreAction FromMTLStoreAction(MTLStoreAction action) {
switch (action) {
case MTLStoreActionDontCare:
return StoreAction::kDontCare;
case MTLStoreActionStore:
return StoreAction::kStore;
default:
break;
}
return StoreAction::kDontCare;
}
constexpr MTLSamplerMinMagFilter ToMTLSamplerMinMagFilter(MinMagFilter filter) {
switch (filter) {
case MinMagFilter::kNearest:
return MTLSamplerMinMagFilterNearest;
case MinMagFilter::kLinear:
return MTLSamplerMinMagFilterLinear;
}
return MTLSamplerMinMagFilterNearest;
}
constexpr MTLSamplerAddressMode ToMTLSamplerAddressMode(
SamplerAddressMode mode) {
switch (mode) {
case SamplerAddressMode::kClampToEdge:
return MTLSamplerAddressModeClampToEdge;
case SamplerAddressMode::kRepeat:
return MTLSamplerAddressModeRepeat;
case SamplerAddressMode::kMirror:
return MTLSamplerAddressModeMirrorRepeat;
}
return MTLSamplerAddressModeClampToEdge;
}
constexpr MTLClearColor ToMTLClearColor(const Color& color) {
return MTLClearColorMake(color.red, color.green, color.blue, color.alpha);
}
MTLRenderPipelineColorAttachmentDescriptor*
ToMTLRenderPipelineColorAttachmentDescriptor(
PipelineColorAttachment descriptor);
MTLDepthStencilDescriptor* ToMTLDepthStencilDescriptor(
std::optional<PipelineDepthAttachment> depth,
std::optional<PipelineStencilAttachment> front,
std::optional<PipelineStencilAttachment> back);
MTLTextureDescriptor* ToMTLTextureDescriptor(const TextureDescriptor& desc);
} // namespace impeller

View File

@@ -0,0 +1,102 @@
// 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/renderer/formats_metal.h"
#include <memory>
#include "impeller/renderer/render_pass.h"
namespace impeller {
MTLRenderPipelineColorAttachmentDescriptor*
ToMTLRenderPipelineColorAttachmentDescriptor(
PipelineColorAttachment 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;
}
MTLStencilDescriptor* ToMTLStencilDescriptor(
const PipelineStencilAttachment& 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<PipelineDepthAttachment> depth,
std::optional<PipelineStencilAttachment> front,
std::optional<PipelineStencilAttachment> back) {
if (!depth) {
depth = PipelineDepthAttachment{
// 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;
}
MTLTextureDescriptor* ToMTLTextureDescriptor(const TextureDescriptor& desc) {
auto mtl_desc = [[MTLTextureDescriptor alloc] init];
mtl_desc.pixelFormat = ToMTLPixelFormat(desc.format);
mtl_desc.width = desc.size.width;
mtl_desc.height = desc.size.height;
mtl_desc.mipmapLevelCount = desc.mip_count;
mtl_desc.usage = MTLTextureUsageUnknown;
if (desc.usage & static_cast<TextureUsageMask>(TextureUsage::kUnknown)) {
mtl_desc.usage |= MTLTextureUsageUnknown;
}
if (desc.usage & static_cast<TextureUsageMask>(TextureUsage::kShaderRead)) {
mtl_desc.usage |= MTLTextureUsageShaderRead;
}
if (desc.usage & static_cast<TextureUsageMask>(TextureUsage::kShaderWrite)) {
mtl_desc.usage |= MTLTextureUsageShaderWrite;
}
if (desc.usage & static_cast<TextureUsageMask>(TextureUsage::kRenderTarget)) {
mtl_desc.usage |= MTLTextureUsageRenderTarget;
}
return mtl_desc;
}
} // namespace impeller

View File

@@ -0,0 +1,22 @@
// 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/renderer/pipeline_library.h"
namespace impeller {
class PipelineLibraryMTL final : public PipelineLibrary {
public:
PipelineLibraryMTL();
~PipelineLibraryMTL() override;
private:
FML_DISALLOW_COPY_AND_ASSIGN(PipelineLibraryMTL);
};
} // namespace impeller

View File

@@ -0,0 +1,22 @@
// 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/renderer/pipeline.h"
namespace impeller {
class PipelineMTL final : public Pipeline {
public:
PipelineMTL();
~PipelineMTL() override;
private:
FML_DISALLOW_COPY_AND_ASSIGN(PipelineMTL);
};
} // namespace impeller

View File

@@ -0,0 +1,22 @@
// 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/renderer/render_pass.h"
namespace impeller {
class RenderPassMTL final : public RenderPass {
public:
RenderPassMTL();
~RenderPassMTL() override;
private:
FML_DISALLOW_COPY_AND_ASSIGN(RenderPassMTL);
};
} // namespace impeller

View File

@@ -0,0 +1,22 @@
// 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/renderer/renderer.h"
namespace impeller {
class RendererMTL final : public Renderer {
public:
RendererMTL();
~RendererMTL() override;
private:
FML_DISALLOW_COPY_AND_ASSIGN(RendererMTL);
};
} // namespace impeller

View File

@@ -0,0 +1,22 @@
// 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/renderer/sampler.h"
namespace impeller {
class SamplerMTL final : public Sampler {
public:
SamplerMTL();
~SamplerMTL() override;
private:
FML_DISALLOW_COPY_AND_ASSIGN(SamplerMTL);
};
} // namespace impeller

View File

@@ -0,0 +1,22 @@
// 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/renderer/shader_function.h"
namespace impeller {
class ShaderFunctionMTL final : public ShaderFunction {
public:
ShaderFunctionMTL();
~ShaderFunctionMTL() override;
private:
FML_DISALLOW_COPY_AND_ASSIGN(ShaderFunctionMTL);
};
} // namespace impeller

View File

@@ -0,0 +1,22 @@
// 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/renderer/shader_library.h"
namespace impeller {
class ShaderLibraryMTL final : public ShaderLibrary {
public:
ShaderLibraryMTL();
~ShaderLibraryMTL() override;
private:
FML_DISALLOW_COPY_AND_ASSIGN(ShaderLibraryMTL);
};
} // namespace impeller

View File

@@ -0,0 +1,22 @@
// 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/renderer/texture.h"
namespace impeller {
class TextureMTL final : public Texture {
public:
TextureMTL();
~TextureMTL() override;
private:
FML_DISALLOW_COPY_AND_ASSIGN(TextureMTL);
};
} // namespace impeller