Make Impeller ObjC agnostic.

This commit is contained in:
Chinmay Garde
2021-11-01 13:50:58 -07:00
committed by Dan Field
parent e129fbaba1
commit 0c66998e1c
16 changed files with 349 additions and 235 deletions

View File

@@ -10,8 +10,9 @@
#include "impeller/playground/playground.h"
#include "impeller/renderer/allocator.h"
#include "impeller/renderer/backend/metal/context_mtl.h"
#include "impeller/renderer/backend/metal/texture_mtl.h"
#include "impeller/renderer/context.h"
#include "impeller/renderer/formats_metal.h"
#include "impeller/renderer/formats.h"
#include "impeller/renderer/render_pass.h"
#include "impeller/renderer/renderer.h"
#include "impeller/renderer/surface.h"
@@ -141,7 +142,7 @@ bool Playground::OpenPlaygroundHere(Renderer::RenderCallback render_callback) {
RenderPassColorAttachment color0;
color0.texture =
std::make_shared<Texture>(color0_desc, current_drawable.texture);
std::make_shared<TextureMTL>(color0_desc, current_drawable.texture);
color0.clear_color = Color::SkyBlue();
color0.load_action = LoadAction::kClear;
color0.store_action = StoreAction::kStore;

View File

@@ -33,6 +33,8 @@ impeller_component("renderer") {
"backend/metal/shader_library_mtl.mm",
"backend/metal/texture_mtl.h",
"backend/metal/texture_mtl.mm",
"backend/metal/vertex_descriptor_mtl.h",
"backend/metal/vertex_descriptor_mtl.mm",
]
sources = [

View File

@@ -8,6 +8,7 @@
#include "flutter/fml/logging.h"
#include "impeller/renderer/backend/metal/device_buffer_mtl.h"
#include "impeller/renderer/backend/metal/formats_mtl.h"
#include "impeller/renderer/backend/metal/texture_mtl.h"
#include "impeller/renderer/buffer.h"
namespace impeller {
@@ -115,7 +116,7 @@ std::shared_ptr<Texture> AllocatorMTL::CreateTexture(
if (!texture) {
return nullptr;
}
return std::make_shared<Texture>(desc, texture);
return std::make_shared<TextureMTL>(desc, texture);
}
} // namespace impeller

View File

@@ -6,6 +6,7 @@
#include "flutter/fml/logging.h"
#include "impeller/renderer/backend/metal/formats_mtl.h"
#include "impeller/renderer/backend/metal/texture_mtl.h"
namespace impeller {
@@ -39,7 +40,7 @@ std::shared_ptr<Texture> DeviceBufferMTL::MakeTexture(TextureDescriptor desc,
return nullptr;
}
return std::make_shared<Texture>(desc, texture);
return std::make_shared<TextureMTL>(desc, texture);
}
[[nodiscard]] bool DeviceBufferMTL::CopyHostBuffer(const uint8_t* source,

View File

@@ -2,7 +2,7 @@
// 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 "impeller/renderer/backend/metal/formats_mtl.h"
#include <memory>

View File

@@ -7,6 +7,7 @@
#include "impeller/renderer/backend/metal/formats_mtl.h"
#include "impeller/renderer/backend/metal/pipeline_mtl.h"
#include "impeller/renderer/backend/metal/shader_function_mtl.h"
#include "impeller/renderer/backend/metal/vertex_descriptor_mtl.h"
namespace impeller {
@@ -34,7 +35,12 @@ static MTLRenderPipelineDescriptor* GetMTLRenderPipelineDescriptor(
}
if (const auto& vertex_descriptor = desc.GetVertexDescriptor()) {
descriptor.vertexDescriptor = vertex_descriptor->GetMTLVertexDescriptor();
VertexDescriptorMTL vertex_descriptor_mtl;
if (vertex_descriptor_mtl.SetStageInputs(
vertex_descriptor->GetStageInputs())) {
descriptor.vertexDescriptor =
vertex_descriptor_mtl.GetMTLVertexDescriptor();
}
}
for (const auto& item : desc.GetColorAttachmentDescriptors()) {

View File

@@ -59,11 +59,14 @@ static bool ConfigureStencilAttachment(
return true;
}
MTLRenderPassDescriptor* RenderPassDescriptor::ToMTLRenderPassDescriptor()
const {
// TODO(csg): Move this to formats_mtl.h
static MTLRenderPassDescriptor* ToMTLRenderPassDescriptor(
const RenderPassDescriptor& desc) {
auto result = [MTLRenderPassDescriptor renderPassDescriptor];
for (const auto& color : colors_) {
const auto& colors = desc.GetColorAttachments();
for (const auto& color : colors) {
if (!ConfigureColorAttachment(color.second,
result.colorAttachments[color.first])) {
FML_LOG(ERROR) << "Could not configure color attachment at index "
@@ -72,13 +75,17 @@ MTLRenderPassDescriptor* RenderPassDescriptor::ToMTLRenderPassDescriptor()
}
}
if (depth_.has_value() &&
!ConfigureDepthAttachment(depth_.value(), result.depthAttachment)) {
const auto& depth = desc.GetDepthAttachment();
if (depth.has_value() &&
!ConfigureDepthAttachment(depth.value(), result.depthAttachment)) {
return nil;
}
if (stencil_.has_value() &&
!ConfigureStencilAttachment(stencil_.value(), result.stencilAttachment)) {
const auto& stencil = desc.GetStencilAttachment();
if (stencil.has_value() &&
!ConfigureStencilAttachment(stencil.value(), result.stencilAttachment)) {
return nil;
}
@@ -88,7 +95,7 @@ MTLRenderPassDescriptor* RenderPassDescriptor::ToMTLRenderPassDescriptor()
RenderPassMTL::RenderPassMTL(id<MTLCommandBuffer> buffer,
const RenderPassDescriptor& desc)
: buffer_(buffer),
desc_(desc.ToMTLRenderPassDescriptor()),
desc_(ToMTLRenderPassDescriptor(desc)),
transients_buffer_(HostBuffer::Create()) {
if (!buffer_ || !desc_) {
return;

View File

@@ -15,16 +15,21 @@ namespace impeller {
class TextureMTL final : public Texture,
public BackendCast<TextureMTL, Texture> {
public:
TextureMTL(TextureDescriptor desc, id<MTLTexture> texture);
// |Texture|
~TextureMTL() override;
// |Texture|
void SetLabel(const std::string_view& label) override;
[[nodiscard]] bool SetContents(const uint8_t* contents,
size_t length) override;
// |Texture|
bool SetContents(const uint8_t* contents, size_t length) override;
// |Texture|
bool IsValid() const override;
// |Texture|
ISize GetSize() const override;
id<MTLTexture> GetMTLTexture() const;
@@ -33,8 +38,6 @@ class TextureMTL final : public Texture,
id<MTLTexture> texture_ = nullptr;
bool is_valid_ = false;
TextureMTL(TextureDescriptor desc, id<MTLTexture> texture);
FML_DISALLOW_COPY_AND_ASSIGN(TextureMTL);
};

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 <Metal/Metal.h>
#include "flutter/fml/macros.h"
#include "impeller/renderer/backend/metal/backend_cast.h"
#include "impeller/renderer/vertex_descriptor.h"
namespace impeller {
class VertexDescriptorMTL {
public:
VertexDescriptorMTL();
~VertexDescriptorMTL();
bool SetStageInputs(const std::vector<ShaderStageIOSlot>& inputs);
MTLVertexDescriptor* GetMTLVertexDescriptor() const;
private:
struct StageInput {
size_t location;
MTLVertexFormat format;
size_t length;
StageInput(size_t p_location, MTLVertexFormat p_format, size_t p_length)
: location(p_location), format(p_format), length(p_length) {}
};
std::vector<StageInput> stage_inputs_;
FML_DISALLOW_COPY_AND_ASSIGN(VertexDescriptorMTL);
};
} // namespace impeller

View File

@@ -0,0 +1,215 @@
// 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/backend/metal/vertex_descriptor_mtl.h"
#include "flutter/fml/logging.h"
namespace impeller {
VertexDescriptorMTL::VertexDescriptorMTL() = default;
VertexDescriptorMTL::~VertexDescriptorMTL() = default;
static MTLVertexFormat ReadStageInputFormat(const ShaderStageIOSlot& input) {
if (input.columns != 1) {
// All matrix types are unsupported as vertex inputs.
return MTLVertexFormatInvalid;
}
switch (input.type) {
case ShaderType::kFloat: {
if (input.bit_width == 8 * sizeof(float)) {
switch (input.vec_size) {
case 1:
return MTLVertexFormatFloat;
case 2:
return MTLVertexFormatFloat2;
case 3:
return MTLVertexFormatFloat3;
case 4:
return MTLVertexFormatFloat4;
}
}
return MTLVertexFormatInvalid;
}
case ShaderType::kHalfFloat: {
if (input.bit_width == 8 * sizeof(float) / 2) {
switch (input.vec_size) {
case 1:
return MTLVertexFormatHalf;
case 2:
return MTLVertexFormatHalf2;
case 3:
return MTLVertexFormatHalf3;
case 4:
return MTLVertexFormatHalf4;
}
}
return MTLVertexFormatInvalid;
}
case ShaderType::kDouble: {
// Unsupported.
return MTLVertexFormatInvalid;
}
case ShaderType::kBoolean: {
if (input.bit_width == 8 * sizeof(bool) && input.vec_size == 1) {
return MTLVertexFormatChar;
}
return MTLVertexFormatInvalid;
}
case ShaderType::kSignedByte: {
if (input.bit_width == 8 * sizeof(char)) {
switch (input.vec_size) {
case 1:
return MTLVertexFormatChar;
case 2:
return MTLVertexFormatChar2;
case 3:
return MTLVertexFormatChar3;
case 4:
return MTLVertexFormatChar4;
}
}
return MTLVertexFormatInvalid;
}
case ShaderType::kUnsignedByte: {
if (input.bit_width == 8 * sizeof(char)) {
switch (input.vec_size) {
case 1:
return MTLVertexFormatUChar;
case 2:
return MTLVertexFormatUChar2;
case 3:
return MTLVertexFormatUChar3;
case 4:
return MTLVertexFormatUChar4;
}
}
return MTLVertexFormatInvalid;
}
case ShaderType::kSignedShort: {
if (input.bit_width == 8 * sizeof(short)) {
switch (input.vec_size) {
case 1:
return MTLVertexFormatShort;
case 2:
return MTLVertexFormatShort2;
case 3:
return MTLVertexFormatShort3;
case 4:
return MTLVertexFormatShort4;
}
}
return MTLVertexFormatInvalid;
}
case ShaderType::kUnsignedShort: {
if (input.bit_width == 8 * sizeof(ushort)) {
switch (input.vec_size) {
case 1:
return MTLVertexFormatUShort;
case 2:
return MTLVertexFormatUShort2;
case 3:
return MTLVertexFormatUShort3;
case 4:
return MTLVertexFormatUShort4;
}
}
return MTLVertexFormatInvalid;
}
case ShaderType::kSignedInt: {
if (input.bit_width == 8 * sizeof(int32_t)) {
switch (input.vec_size) {
case 1:
return MTLVertexFormatInt;
case 2:
return MTLVertexFormatInt2;
case 3:
return MTLVertexFormatInt3;
case 4:
return MTLVertexFormatInt4;
}
}
return MTLVertexFormatInvalid;
}
case ShaderType::kUnsignedInt: {
if (input.bit_width == 8 * sizeof(uint32_t)) {
switch (input.vec_size) {
case 1:
return MTLVertexFormatUInt;
case 2:
return MTLVertexFormatUInt2;
case 3:
return MTLVertexFormatUInt3;
case 4:
return MTLVertexFormatUInt4;
}
}
return MTLVertexFormatInvalid;
}
case ShaderType::kSignedInt64: {
// Unsupported.
return MTLVertexFormatInvalid;
}
case ShaderType::kUnsignedInt64: {
// Unsupported.
return MTLVertexFormatInvalid;
}
case ShaderType::kAtomicCounter:
case ShaderType::kStruct:
case ShaderType::kImage:
case ShaderType::kSampledImage:
case ShaderType::kUnknown:
case ShaderType::kVoid:
case ShaderType::kSampler:
return MTLVertexFormatInvalid;
}
}
bool VertexDescriptorMTL::SetStageInputs(
const std::vector<ShaderStageIOSlot>& inputs) {
stage_inputs_.clear();
for (size_t i = 0; i < inputs.size(); i++) {
const auto& input = inputs[i];
auto vertex_format = ReadStageInputFormat(input);
if (vertex_format == MTLVertexFormatInvalid) {
FML_LOG(ERROR) << "Format for input " << input.name << " not supported.";
return false;
}
stage_inputs_.emplace_back(StageInput{
input.location, vertex_format, (input.bit_width * input.vec_size) / 8});
}
return true;
}
MTLVertexDescriptor* VertexDescriptorMTL::GetMTLVertexDescriptor() const {
auto descriptor = [MTLVertexDescriptor vertexDescriptor];
const size_t vertex_buffer_index =
PipelineVertexDescriptor::kReservedVertexBufferIndex;
size_t stride = 0u;
for (const auto& input : stage_inputs_) {
auto attrib = descriptor.attributes[input.location];
attrib.format = input.format;
attrib.offset = stride;
// All vertex inputs are interleaved and tightly packed in one buffer at a
// reserved index.
attrib.bufferIndex = vertex_buffer_index;
stride += input.length;
}
// Since it's all in one buffer, indicate its layout.
auto vertex_layout = descriptor.layouts[vertex_buffer_index];
vertex_layout.stride = stride;
vertex_layout.stepRate = 1u;
vertex_layout.stepFunction = MTLVertexStepFunctionPerVertex;
return descriptor;
}
} // namespace impeller

View File

@@ -4,7 +4,7 @@
#include "impeller/renderer/pipeline_descriptor.h"
#include "impeller/renderer/formats_metal.h"
#include "impeller/renderer/formats.h"
#include "impeller/renderer/shader_function.h"
#include "impeller/renderer/shader_library.h"
#include "impeller/renderer/vertex_descriptor.h"

View File

@@ -55,4 +55,19 @@ RenderPassDescriptor& RenderPassDescriptor::SetStencilAttachment(
return *this;
}
const std::map<size_t, RenderPassColorAttachment>&
RenderPassDescriptor::GetColorAttachments() const {
return colors_;
}
const std::optional<RenderPassDepthAttachment>&
RenderPassDescriptor::GetDepthAttachment() const {
return depth_;
}
const std::optional<RenderPassStencilAttachment>&
RenderPassDescriptor::GetStencilAttachment() const {
return stencil_;
}
} // namespace impeller

View File

@@ -32,6 +32,14 @@ class RenderPassDescriptor {
RenderPassDescriptor& SetStencilAttachment(
RenderPassStencilAttachment attachment);
const std::map<size_t, RenderPassColorAttachment>& GetColorAttachments()
const;
const std::optional<RenderPassDepthAttachment>& GetDepthAttachment() const;
const std::optional<RenderPassStencilAttachment>& GetStencilAttachment()
const;
private:
std::map<size_t, RenderPassColorAttachment> colors_;
std::optional<RenderPassDepthAttachment> depth_;

View File

@@ -8,6 +8,7 @@
#include <cstddef>
#include <string_view>
#include "flutter/fml/hash_combine.h"
#include "impeller/geometry/matrix.h"
namespace impeller {
@@ -48,6 +49,7 @@ struct ShaderUniformSlot {
};
struct ShaderStageIOSlot {
// Statically allocated const string containing advisory debug description.
const char* name;
size_t location;
size_t set;
@@ -56,6 +58,22 @@ struct ShaderStageIOSlot {
size_t bit_width;
size_t vec_size;
size_t columns;
constexpr size_t GetHash() const {
return fml::HashCombine(name, location, set, binding, type, bit_width,
vec_size, columns);
}
constexpr bool operator==(const ShaderStageIOSlot& other) const {
return name == other.name && //
location == other.location && //
set == other.set && //
binding == other.binding && //
type == other.type && //
bit_width == other.bit_width && //
vec_size == other.vec_size && //
columns == other.columns;
}
};
struct SampledImageSlot {

View File

@@ -4,220 +4,27 @@
#include "impeller/renderer/vertex_descriptor.h"
#include "flutter/fml/logging.h"
namespace impeller {
PipelineVertexDescriptor::PipelineVertexDescriptor() = default;
PipelineVertexDescriptor::~PipelineVertexDescriptor() = default;
static MTLVertexFormat ReadStageInputFormat(const ShaderStageIOSlot& input) {
if (input.columns != 1) {
// All matrix types are unsupported as vertex inputs.
return MTLVertexFormatInvalid;
}
switch (input.type) {
case ShaderType::kFloat: {
if (input.bit_width == 8 * sizeof(float)) {
switch (input.vec_size) {
case 1:
return MTLVertexFormatFloat;
case 2:
return MTLVertexFormatFloat2;
case 3:
return MTLVertexFormatFloat3;
case 4:
return MTLVertexFormatFloat4;
}
}
return MTLVertexFormatInvalid;
}
case ShaderType::kHalfFloat: {
if (input.bit_width == 8 * sizeof(float) / 2) {
switch (input.vec_size) {
case 1:
return MTLVertexFormatHalf;
case 2:
return MTLVertexFormatHalf2;
case 3:
return MTLVertexFormatHalf3;
case 4:
return MTLVertexFormatHalf4;
}
}
return MTLVertexFormatInvalid;
}
case ShaderType::kDouble: {
// Unsupported.
return MTLVertexFormatInvalid;
}
case ShaderType::kBoolean: {
if (input.bit_width == 8 * sizeof(bool) && input.vec_size == 1) {
return MTLVertexFormatChar;
}
return MTLVertexFormatInvalid;
}
case ShaderType::kSignedByte: {
if (input.bit_width == 8 * sizeof(char)) {
switch (input.vec_size) {
case 1:
return MTLVertexFormatChar;
case 2:
return MTLVertexFormatChar2;
case 3:
return MTLVertexFormatChar3;
case 4:
return MTLVertexFormatChar4;
}
}
return MTLVertexFormatInvalid;
}
case ShaderType::kUnsignedByte: {
if (input.bit_width == 8 * sizeof(char)) {
switch (input.vec_size) {
case 1:
return MTLVertexFormatUChar;
case 2:
return MTLVertexFormatUChar2;
case 3:
return MTLVertexFormatUChar3;
case 4:
return MTLVertexFormatUChar4;
}
}
return MTLVertexFormatInvalid;
}
case ShaderType::kSignedShort: {
if (input.bit_width == 8 * sizeof(short)) {
switch (input.vec_size) {
case 1:
return MTLVertexFormatShort;
case 2:
return MTLVertexFormatShort2;
case 3:
return MTLVertexFormatShort3;
case 4:
return MTLVertexFormatShort4;
}
}
return MTLVertexFormatInvalid;
}
case ShaderType::kUnsignedShort: {
if (input.bit_width == 8 * sizeof(ushort)) {
switch (input.vec_size) {
case 1:
return MTLVertexFormatUShort;
case 2:
return MTLVertexFormatUShort2;
case 3:
return MTLVertexFormatUShort3;
case 4:
return MTLVertexFormatUShort4;
}
}
return MTLVertexFormatInvalid;
}
case ShaderType::kSignedInt: {
if (input.bit_width == 8 * sizeof(int32_t)) {
switch (input.vec_size) {
case 1:
return MTLVertexFormatInt;
case 2:
return MTLVertexFormatInt2;
case 3:
return MTLVertexFormatInt3;
case 4:
return MTLVertexFormatInt4;
}
}
return MTLVertexFormatInvalid;
}
case ShaderType::kUnsignedInt: {
if (input.bit_width == 8 * sizeof(uint32_t)) {
switch (input.vec_size) {
case 1:
return MTLVertexFormatUInt;
case 2:
return MTLVertexFormatUInt2;
case 3:
return MTLVertexFormatUInt3;
case 4:
return MTLVertexFormatUInt4;
}
}
return MTLVertexFormatInvalid;
}
case ShaderType::kSignedInt64: {
// Unsupported.
return MTLVertexFormatInvalid;
}
case ShaderType::kUnsignedInt64: {
// Unsupported.
return MTLVertexFormatInvalid;
}
case ShaderType::kAtomicCounter:
case ShaderType::kStruct:
case ShaderType::kImage:
case ShaderType::kSampledImage:
case ShaderType::kUnknown:
case ShaderType::kVoid:
case ShaderType::kSampler:
return MTLVertexFormatInvalid;
}
}
bool PipelineVertexDescriptor::SetStageInputs(
const ShaderStageIOSlot* const stage_inputs[],
size_t count) {
stage_inputs_.clear();
inputs_.reserve(inputs_.size() + count);
for (size_t i = 0; i < count; i++) {
const ShaderStageIOSlot* input = stage_inputs[i];
auto vertex_format = ReadStageInputFormat(*input);
if (vertex_format == MTLVertexFormatInvalid) {
FML_LOG(ERROR) << "Format for input " << input->name << " not supported.";
return false;
}
stage_inputs_.emplace_back(
StageInput{input->location, vertex_format,
(input->bit_width * input->vec_size) / 8});
inputs_.emplace_back(*stage_inputs[i]);
}
return true;
}
MTLVertexDescriptor* PipelineVertexDescriptor::GetMTLVertexDescriptor() const {
auto descriptor = [MTLVertexDescriptor vertexDescriptor];
const size_t vertex_buffer_index = kReservedVertexBufferIndex;
size_t stride = 0u;
for (const auto& input : stage_inputs_) {
auto attrib = descriptor.attributes[input.location];
attrib.format = input.format;
attrib.offset = stride;
// All vertex inputs are interleaved and tightly packed in one buffer at a
// reserved index.
attrib.bufferIndex = vertex_buffer_index;
stride += input.length;
}
// Since it's all in one buffer, indicate its layout.
auto vertex_layout = descriptor.layouts[vertex_buffer_index];
vertex_layout.stride = stride;
vertex_layout.stepRate = 1u;
vertex_layout.stepFunction = MTLVertexStepFunctionPerVertex;
return descriptor;
}
// |Comparable<VertexDescriptor>|
std::size_t PipelineVertexDescriptor::GetHash() const {
size_t PipelineVertexDescriptor::GetHash() const {
auto seed = fml::HashCombine();
for (const auto& input : stage_inputs_) {
fml::HashCombineSeed(seed, input.location, input.format);
for (const auto& input : inputs_) {
fml::HashCombineSeed(seed, input.GetHash());
}
return seed;
}
@@ -225,7 +32,12 @@ std::size_t PipelineVertexDescriptor::GetHash() const {
// |Comparable<VertexDescriptor>|
bool PipelineVertexDescriptor::IsEqual(
const PipelineVertexDescriptor& other) const {
return stage_inputs_ == other.stage_inputs_;
return inputs_ == other.inputs_;
}
const std::vector<ShaderStageIOSlot>& PipelineVertexDescriptor::GetStageInputs()
const {
return inputs_;
}
} // namespace impeller

View File

@@ -28,6 +28,7 @@ class PipelineVertexDescriptor final
PipelineVertexDescriptor();
// |Comparable<PipelineVertexDescriptor>|
virtual ~PipelineVertexDescriptor();
template <size_t Size>
@@ -39,29 +40,16 @@ class PipelineVertexDescriptor final
bool SetStageInputs(const ShaderStageIOSlot* const stage_inputs[],
size_t count);
//| Comparable<VertexDescriptor>|
const std::vector<ShaderStageIOSlot>& GetStageInputs() const;
// |Comparable<VertexDescriptor>|
std::size_t GetHash() const override;
// |Comparable<VertexDescriptor>|
bool IsEqual(const PipelineVertexDescriptor& other) const override;
MTLVertexDescriptor* GetMTLVertexDescriptor() const;
private:
struct StageInput {
size_t location;
MTLVertexFormat format;
size_t length;
StageInput(size_t p_location, MTLVertexFormat p_format, size_t p_length)
: location(p_location), format(p_format), length(p_length) {}
constexpr bool operator==(const StageInput& other) const {
return location == other.location && format == other.format &&
length == other.length;
}
};
std::vector<StageInput> stage_inputs_;
std::vector<ShaderStageIOSlot> inputs_;
FML_DISALLOW_COPY_AND_ASSIGN(PipelineVertexDescriptor);
};