diff --git a/engine/src/flutter/impeller/compiler/code_gen_template.h b/engine/src/flutter/impeller/compiler/code_gen_template.h index 88185ad9bf..ff03f6789e 100644 --- a/engine/src/flutter/impeller/compiler/code_gen_template.h +++ b/engine/src/flutter/impeller/compiler/code_gen_template.h @@ -24,7 +24,7 @@ struct {{camel_case(shader_name)}}{{camel_case(shader_stage)}}Info { // Stage Inputs {% for stage_input in stage_inputs %} // Stage input {{stage_input.name}} - static constexpr ShaderStageInput kInput{{camel_case(stage_input.name)}} = { + static constexpr auto kInput{{camel_case(stage_input.name)}} = ShaderStageInput { "{{stage_input.name}}", // name {{stage_input.location}}u, // attribute location {{stage_input.type.type_name}}, // type @@ -33,6 +33,13 @@ struct {{camel_case(shader_name)}}{{camel_case(shader_stage)}}Info { {{stage_input.type.columns}}u // number of columns }; {% endfor %} + + static constexpr const ShaderStageInput* kAllShaderStageInputs[] = { +{% for stage_input in stage_inputs %} + &kInput{{camel_case(stage_input.name)}}, // {{stage_input.name}} +{% endfor %} + }; + }; // struct {{camel_case(shader_name)}}{{camel_case(shader_stage)}}Info } // namespace shader diff --git a/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.h b/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.h index 7be7c10819..1796ccf38b 100644 --- a/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.h +++ b/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.h @@ -4,17 +4,26 @@ #pragma once +#include + +#include + #include "flutter/fml/macros.h" #include "impeller/compositor/comparable.h" +#include "impeller/shader_glue/shader_types.h" namespace impeller { -class VertexDescriptor : public Comparable { +class VertexDescriptor final : public Comparable { public: VertexDescriptor(); virtual ~VertexDescriptor(); + bool SetStageInputs(const ShaderStageInput* stage_inputs[], size_t count); + + MTLVertexDescriptor* GetMTLVertexDescriptor() const; + //| Comparable| std::size_t GetHash() const override; @@ -22,6 +31,19 @@ class VertexDescriptor : public Comparable { bool IsEqual(const VertexDescriptor& other) const override; private: + struct StageInput { + size_t location; + MTLVertexFormat format; + + StageInput(size_t p_location, MTLVertexFormat p_format) + : location(p_location), format(p_format) {} + + constexpr bool operator==(const StageInput& other) const { + return location == other.location && format == other.format; + } + }; + std::vector stage_inputs_; + FML_DISALLOW_COPY_AND_ASSIGN(VertexDescriptor); }; diff --git a/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.mm b/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.mm index 4ad3df8a29..ed5fc797ef 100644 --- a/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.mm +++ b/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.mm @@ -12,16 +12,204 @@ VertexDescriptor::VertexDescriptor() = default; VertexDescriptor::~VertexDescriptor() = default; +static MTLVertexFormat ReadStageInputFormat(const ShaderStageInput& 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 == 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 == 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 == sizeof(bool) && input.vec_size == 1) { + return MTLVertexFormatChar; + } + return MTLVertexFormatInvalid; + } + case ShaderType::kSignedByte: { + if (input.bit_width == 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 == 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 == 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 == 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 == 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 == 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 VertexDescriptor::SetStageInputs(const ShaderStageInput* stage_inputs[], + size_t count) { + stage_inputs_.clear(); + + for (size_t i = 0; i < count; i++) { + const ShaderStageInput* 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}); + } + + return true; +} + +MTLVertexDescriptor* VertexDescriptor::GetMTLVertexDescriptor() const { + auto descriptor = [MTLVertexDescriptor vertexDescriptor]; + + for (const auto& input : stage_inputs_) { + auto attrib = descriptor.attributes[input.location]; + attrib.format = input.format; + attrib.offset = 0u; + attrib.bufferIndex = 0u; + } + + return descriptor; +} + // |Comparable| std::size_t VertexDescriptor::GetHash() const { - FML_CHECK(false); - return 0; + auto seed = fml::HashCombine(); + for (const auto& input : stage_inputs_) { + fml::HashCombineSeed(seed, input.location, input.format); + } + return seed; } // |Comparable| bool VertexDescriptor::IsEqual(const VertexDescriptor& other) const { - FML_CHECK(false); - return false; + return stage_inputs_ == other.stage_inputs_; } } // namespace impeller