From 956973b17b5a22276aa6299d8fa578138270200e Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Mon, 24 May 2021 02:39:52 -0700 Subject: [PATCH] Parse member names. --- .../src/flutter/impeller/compiler/compiler.cc | 13 ++- .../src/flutter/impeller/compiler/compiler.h | 1 + .../flutter/impeller/compiler/reflector.cc | 90 +++++++++++++++---- .../src/flutter/impeller/compiler/reflector.h | 5 +- .../impeller/impeller/primitives/box.vert | 26 +++--- 5 files changed, 101 insertions(+), 34 deletions(-) diff --git a/engine/src/flutter/impeller/compiler/compiler.cc b/engine/src/flutter/impeller/compiler/compiler.cc index 33dcd59a9a..57af0f7b6b 100644 --- a/engine/src/flutter/impeller/compiler/compiler.cc +++ b/engine/src/flutter/impeller/compiler/compiler.cc @@ -278,8 +278,13 @@ Compiler::Compiler(const fml::Mapping& source_mapping, } // MSL Generation. - spirv_cross::CompilerMSL msl_compiler( - spv_result_->cbegin(), spv_result_->cend() - spv_result_->cbegin()); + spirv_cross::Parser parser(spv_result_->cbegin(), + spv_result_->cend() - spv_result_->cbegin()); + // The parser and compiler must be run separately because the parser contains + // meta information (like type member names) that are useful for reflection. + parser.parse(); + const auto& parsed_ir = parser.get_parsed_ir(); + spirv_cross::CompilerMSL msl_compiler(parsed_ir); { msl_compiler.rename_entry_point("main", options_.entry_point_name, @@ -299,8 +304,8 @@ Compiler::Compiler(const fml::Mapping& source_mapping, return; } - reflector_ = - std::make_unique(std::move(reflector_options), msl_compiler); + reflector_ = std::make_unique(std::move(reflector_options), + parsed_ir, msl_compiler); if (!reflector_->IsValid()) { COMPILER_ERROR << "Could not complete reflection on generated shader."; diff --git a/engine/src/flutter/impeller/compiler/compiler.h b/engine/src/flutter/impeller/compiler/compiler.h index 3c1dd4e4ff..7dec9d39ac 100644 --- a/engine/src/flutter/impeller/compiler/compiler.h +++ b/engine/src/flutter/impeller/compiler/compiler.h @@ -14,6 +14,7 @@ #include "flutter/impeller/compiler/reflector.h" #include "shaderc/shaderc.hpp" #include "third_party/spirv_cross/spirv_msl.hpp" +#include "third_party/spirv_cross/spirv_parser.hpp" namespace impeller { namespace compiler { diff --git a/engine/src/flutter/impeller/compiler/reflector.cc b/engine/src/flutter/impeller/compiler/reflector.cc index f7f2d87e7c..a2d31b3604 100644 --- a/engine/src/flutter/impeller/compiler/reflector.cc +++ b/engine/src/flutter/impeller/compiler/reflector.cc @@ -4,6 +4,7 @@ #include "flutter/impeller/compiler/reflector.h" +#include #include #include @@ -66,9 +67,47 @@ static std::string BaseTypeToString(spirv_cross::SPIRType::BaseType type) { } } +static std::optional GetMemberNameAtIndexIfExists( + const spirv_cross::ParsedIR& ir, + const spirv_cross::CompilerMSL& compiler, + const spirv_cross::SPIRType& type, + size_t index) { + if (type.type_alias != 0) { + return GetMemberNameAtIndexIfExists( + ir, compiler, compiler.get_type(type.type_alias), index); + } + + if (auto found = ir.meta.find(type.self); found != ir.meta.end()) { + const auto& members = found->second.members; + if (index < members.size() && !members[index].alias.empty()) { + return members[index].alias; + } + } + return std::nullopt; +} + +static std::string GetMemberNameAtIndex( + const spirv_cross::ParsedIR& ir, + const spirv_cross::CompilerMSL& compiler, + const spirv_cross::SPIRType& type, + size_t index) { + if (auto name = GetMemberNameAtIndexIfExists(ir, compiler, type, index); + name.has_value()) { + return name.value(); + } + + static std::atomic_size_t sUnnamedMembersID; + std::stringstream stream; + stream << "unnamed_" << sUnnamedMembersID++; + return stream.str(); +} + static bool ReflectType(Writer& writer, + const spirv_cross::ParsedIR& ir, const spirv_cross::CompilerMSL& compiler, - const spirv_cross::SPIRType& type) { + const spirv_cross::TypeID& type_id) { + const auto type = compiler.get_type(type_id); + writer.Key("type"); writer.StartObject(); @@ -84,24 +123,32 @@ static bool ReflectType(Writer& writer, writer.Key("columns"); writer.Uint64(type.columns); - writer.Key("member_types"); - writer.StartArray(); - for (const auto& member : type.member_types) { - if (!ReflectType(writer, compiler, compiler.get_type(member))) { - return false; + if (!type.member_types.empty()) { + writer.Key("member"); + writer.StartArray(); + for (size_t i = 0; i < type.member_types.size(); i++) { + writer.StartObject(); + { + writer.Key("type_id"); + writer.Uint64(type.member_types[i]); + writer.Key("member_name"); + writer.String(GetMemberNameAtIndex(ir, compiler, type, i)); + } + writer.EndObject(); } + writer.EndArray(); } - writer.EndArray(); writer.EndObject(); return true; } static bool ReflectBaseResource(Writer& writer, + const spirv_cross::ParsedIR& ir, const spirv_cross::CompilerMSL& compiler, const spirv_cross::Resource& res) { writer.Key("name"); - writer.String(compiler.get_name(res.id)); + writer.String(res.name); writer.Key("descriptor_set"); writer.Uint64(compiler.get_decoration( @@ -115,19 +162,20 @@ static bool ReflectBaseResource(Writer& writer, writer.Uint64( compiler.get_decoration(res.id, spv::Decoration::DecorationLocation)); - if (!ReflectType(writer, compiler, compiler.get_type(res.type_id))) { + if (!ReflectType(writer, ir, compiler, res.type_id)) { return false; } return true; } -static bool ReflectStageInput(Writer& writer, - const spirv_cross::CompilerMSL& compiler, - const spirv_cross::Resource& input) { +static bool ReflectStageIO(Writer& writer, + const spirv_cross::ParsedIR& ir, + const spirv_cross::CompilerMSL& compiler, + const spirv_cross::Resource& io) { writer.StartObject(); - if (!ReflectBaseResource(writer, compiler, input)) { + if (!ReflectBaseResource(writer, ir, compiler, io)) { return false; } @@ -136,11 +184,12 @@ static bool ReflectStageInput(Writer& writer, } static bool ReflectUniformBuffer(Writer& writer, + const spirv_cross::ParsedIR& ir, const spirv_cross::CompilerMSL& compiler, const spirv_cross::Resource& buffer) { writer.StartObject(); - if (!ReflectBaseResource(writer, compiler, buffer)) { + if (!ReflectBaseResource(writer, ir, compiler, buffer)) { return false; } @@ -177,6 +226,7 @@ static std::string StringToShaderStage(std::string str) { static std::shared_ptr ReflectTemplateArguments( const Reflector::Options& options, + const spirv_cross::ParsedIR& ir, const spirv_cross::CompilerMSL& compiler) { auto buffer = std::make_shared(); Writer writer(*buffer); @@ -209,7 +259,7 @@ static std::shared_ptr ReflectTemplateArguments( writer.StartArray(); for (const auto& uniform_buffer : compiler.get_shader_resources().uniform_buffers) { - if (!ReflectUniformBuffer(writer, compiler, uniform_buffer)) { + if (!ReflectUniformBuffer(writer, ir, compiler, uniform_buffer)) { FML_LOG(ERROR) << "Could not reflect uniform buffer."; return nullptr; } @@ -221,7 +271,7 @@ static std::shared_ptr ReflectTemplateArguments( writer.Key("stage_inputs"); writer.StartArray(); for (const auto& input : compiler.get_shader_resources().stage_inputs) { - if (!ReflectStageInput(writer, compiler, input)) { + if (!ReflectStageIO(writer, ir, compiler, input)) { FML_LOG(ERROR) << "Could not reflect stage input."; return nullptr; } @@ -233,7 +283,7 @@ static std::shared_ptr ReflectTemplateArguments( writer.Key("stage_outputs"); writer.StartArray(); for (const auto& output : compiler.get_shader_resources().stage_outputs) { - if (!ReflectStageInput(writer, compiler, output)) { + if (!ReflectStageIO(writer, ir, compiler, output)) { FML_LOG(ERROR) << "Could not reflect stage output."; return nullptr; } @@ -279,9 +329,11 @@ static std::shared_ptr InflateTemplate( inflated_template->size(), [inflated_template](auto, auto) {}); } -Reflector::Reflector(Options options, const spirv_cross::CompilerMSL& compiler) +Reflector::Reflector(Options options, + const spirv_cross::ParsedIR& ir, + const spirv_cross::CompilerMSL& compiler) : options_(std::move(options)), - template_arguments_(ReflectTemplateArguments(options_, compiler)), + template_arguments_(ReflectTemplateArguments(options_, ir, compiler)), reflection_header_(InflateTemplate(compiler, kReflectionHeaderTemplate, template_arguments_.get())), diff --git a/engine/src/flutter/impeller/compiler/reflector.h b/engine/src/flutter/impeller/compiler/reflector.h index 337488a05a..7346b9895d 100644 --- a/engine/src/flutter/impeller/compiler/reflector.h +++ b/engine/src/flutter/impeller/compiler/reflector.h @@ -9,6 +9,7 @@ #include "flutter/fml/macros.h" #include "flutter/fml/mapping.h" #include "third_party/spirv_cross/spirv_msl.hpp" +#include "third_party/spirv_cross/spirv_parser.hpp" namespace impeller { namespace compiler { @@ -20,7 +21,9 @@ class Reflector { std::string header_file_name; }; - Reflector(Options options, const spirv_cross::CompilerMSL& compiler); + Reflector(Options options, + const spirv_cross::ParsedIR& ir, + const spirv_cross::CompilerMSL& compiler); ~Reflector(); diff --git a/engine/src/flutter/impeller/impeller/primitives/box.vert b/engine/src/flutter/impeller/impeller/primitives/box.vert index eda8247d19..0a9d4524ab 100644 --- a/engine/src/flutter/impeller/impeller/primitives/box.vert +++ b/engine/src/flutter/impeller/impeller/primitives/box.vert @@ -2,19 +2,25 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -uniform UniformBuffer { - mat4 mvp; -} uniforms; +struct MyAwesomeStruct { + float mvp0; + mat4x2 mvp1; + mat4 mvp2; + mat4 mvp3; +}; + +uniform UniformBuffer1 { + MyAwesomeStruct s1; + MyAwesomeStruct s2; + MyAwesomeStruct s3; +} uniforms1; uniform UniformBuffer2 { - uniform mat4 longGOP; + MyAwesomeStruct s11; + float hello12; + MyAwesomeStruct s13; } uniforms2; -struct Position { - vec3 position; - vec3 color; -}; - in vec3 vertexPosition; in vec3 vertexColor; in dvec4 hello; @@ -25,6 +31,6 @@ out vec3 color; void main() { - gl_Position = uniforms2.longGOP * uniforms.mvp * vec4(vertexPosition, 1.0); + gl_Position = uniforms1.s1.mvp2 * uniforms2.s13.mvp3 * vec4(vertexPosition, 1.0); color = vertexColor; }