diff --git a/engine/src/flutter/impeller/compiler/BUILD.gn b/engine/src/flutter/impeller/compiler/BUILD.gn index 540ccc7328..6b8bba531a 100644 --- a/engine/src/flutter/impeller/compiler/BUILD.gn +++ b/engine/src/flutter/impeller/compiler/BUILD.gn @@ -19,6 +19,8 @@ source_set("compiler_lib") { public_deps = [ "//flutter/fml", "//flutter/runtime:libdart", + "//third_party/inja", + "//third_party/rapidjson", "//third_party/shaderc_flutter", "//third_party/spirv_cross_flutter", "//third_party/spirv_reflect_flutter", diff --git a/engine/src/flutter/impeller/compiler/compiler.cc b/engine/src/flutter/impeller/compiler/compiler.cc index f95cbe4a4c..a6a4c59bae 100644 --- a/engine/src/flutter/impeller/compiler/compiler.cc +++ b/engine/src/flutter/impeller/compiler/compiler.cc @@ -298,7 +298,7 @@ Compiler::Compiler(const fml::Mapping& source_mapping, return; } - reflector_ = std::make_unique(*GetSPIRVAssembly()); + reflector_ = std::make_unique(msl_compiler); if (!reflector_->IsValid()) { COMPILER_ERROR << "Could not complete reflection on generated shader."; @@ -433,5 +433,9 @@ std::unique_ptr Compiler::CreateDepfileContents( [contents](auto, auto) {}); } +const Reflector* Compiler::GetReflector() const { + return reflector_.get(); +} + } // namespace compiler } // namespace impeller diff --git a/engine/src/flutter/impeller/compiler/compiler.h b/engine/src/flutter/impeller/compiler/compiler.h index 7c2b7f0e8d..7ff3883ff8 100644 --- a/engine/src/flutter/impeller/compiler/compiler.h +++ b/engine/src/flutter/impeller/compiler/compiler.h @@ -61,6 +61,8 @@ class Compiler { std::unique_ptr CreateDepfileContents( std::initializer_list targets) const; + const Reflector* GetReflector() const; + private: SourceOptions options_; std::shared_ptr spv_result_; diff --git a/engine/src/flutter/impeller/compiler/compiler_unittests.cc b/engine/src/flutter/impeller/compiler/compiler_unittests.cc index 4ff37a35f3..d496312c68 100644 --- a/engine/src/flutter/impeller/compiler/compiler_unittests.cc +++ b/engine/src/flutter/impeller/compiler/compiler_unittests.cc @@ -50,13 +50,16 @@ TEST_F(CompilerTest, ShaderKindMatchingIsSuccessful) { } TEST_F(CompilerTest, CanCompileSample) { - constexpr const char* kShaderFixtureName = "sample.frag"; + constexpr const char* kShaderFixtureName = "sample.vert"; auto fixture = flutter::testing::OpenFixtureAsMapping(kShaderFixtureName); ASSERT_NE(fixture->GetMapping(), nullptr); Compiler::SourceOptions options(kShaderFixtureName); options.working_directory = std::make_shared( flutter::testing::OpenFixturesDirectory()); Compiler compiler(*fixture.get(), options); + if (!compiler.IsValid()) { + FML_LOG(ERROR) << compiler.GetErrorMessages(); + } ASSERT_TRUE(compiler.IsValid()); WriteCompilerIntermediates(compiler, kShaderFixtureName); } diff --git a/engine/src/flutter/impeller/compiler/fixtures/sample.vert b/engine/src/flutter/impeller/compiler/fixtures/sample.vert index 727406ac86..ba455ccb86 100644 --- a/engine/src/flutter/impeller/compiler/fixtures/sample.vert +++ b/engine/src/flutter/impeller/compiler/fixtures/sample.vert @@ -1,23 +1,20 @@ -#version 450 -#extension GL_ARB_separate_shader_objects : enable +#include "types.h" -// Uniforms - -layout(set = 0, binding = 0) uniform UniformBufferObject { - mat4 mvp; +uniform UniformBufferObject { + Uniforms uniforms; } ubo; -// In +uniform sampler2D world; -layout(location = 0) in vec3 inPosition; -layout(location = 1) in vec3 inNormal; -layout(location = 2) in vec2 inTextureCoords; +in vec2 inPosition; +in vec3 inPosition22; +in vec4 inAnotherPosition; +in float stuff; -// Out - -layout(location = 0) out vec2 outTextureCoords; +out vec4 outStuff; void main() { - gl_Position = ubo.mvp * vec4(inPosition, 1.0); - outTextureCoords = inTextureCoords; + gl_Position = ubo.uniforms.projection * ubo.uniforms.view * ubo.uniforms.model * vec4(inPosition22, 1.0) * inAnotherPosition; + outStuff = texture(world, inPosition); } + diff --git a/engine/src/flutter/impeller/compiler/fixtures/types.h b/engine/src/flutter/impeller/compiler/fixtures/types.h index 86572a6655..d8775956ba 100644 --- a/engine/src/flutter/impeller/compiler/fixtures/types.h +++ b/engine/src/flutter/impeller/compiler/fixtures/types.h @@ -2,12 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "common_types.h" - -struct Hello { - Vector4 stuff; - Vector3 more_stuff; - float dunno; - float this_bit_is_not_used; - Vector4 yet_more_stuff; +struct Uniforms { + mat4 model; + mat4 view; + mat4 projection; }; diff --git a/engine/src/flutter/impeller/compiler/impellerc_main.cc b/engine/src/flutter/impeller/compiler/impellerc_main.cc index 31c32fe0e0..bd7f885bab 100644 --- a/engine/src/flutter/impeller/compiler/impellerc_main.cc +++ b/engine/src/flutter/impeller/compiler/impellerc_main.cc @@ -65,6 +65,37 @@ bool Main(const fml::CommandLine& command_line) { return false; } + if (!switches.reflection_json_name.empty()) { + if (!fml::WriteAtomically(*switches.working_directory, + switches.reflection_json_name.c_str(), + *compiler.GetReflector()->GetReflectionJSON())) { + std::cerr << "Could not write reflection json to " + << switches.reflection_json_name << std::endl; + return false; + } + } + + if (!switches.reflection_header_name.empty()) { + if (!fml::WriteAtomically( + *switches.working_directory, + switches.reflection_header_name.c_str(), + *compiler.GetReflector()->GetReflectionHeader())) { + std::cerr << "Could not write reflection header to " + << switches.reflection_header_name << std::endl; + return false; + } + } + + if (!switches.reflection_cc_name.empty()) { + if (!fml::WriteAtomically(*switches.working_directory, + switches.reflection_cc_name.c_str(), + *compiler.GetReflector()->GetReflectionCC())) { + std::cerr << "Could not write reflection CC to " + << switches.reflection_cc_name << std::endl; + return false; + } + } + if (!switches.depfile_path.empty()) { if (!fml::WriteAtomically( *switches.working_directory, switches.depfile_path.c_str(), diff --git a/engine/src/flutter/impeller/compiler/reflector.cc b/engine/src/flutter/impeller/compiler/reflector.cc index 4e32f26432..8b14a2d557 100644 --- a/engine/src/flutter/impeller/compiler/reflector.cc +++ b/engine/src/flutter/impeller/compiler/reflector.cc @@ -4,14 +4,241 @@ #include "flutter/impeller/compiler/reflector.h" +#include #include +#include "flutter/fml/closure.h" #include "flutter/fml/logging.h" +#include "inja/inja.hpp" +#include "rapidjson/document.h" +#include "rapidjson/prettywriter.h" +#include "rapidjson/rapidjson.h" +#include "rapidjson/stringbuffer.h" namespace impeller { namespace compiler { -Reflector::Reflector(const spirv_cross::CompilerMSL& compiler) { +using Writer = rapidjson::PrettyWriter; + +static std::string BaseTypeToString(spirv_cross::SPIRType::BaseType type) { + using Type = spirv_cross::SPIRType::BaseType; + switch (type) { + case Type::Void: + return "Void"; + case Type::Boolean: + return "Boolean"; + case Type::SByte: + return "SByte"; + case Type::UByte: + return "UByte"; + case Type::Short: + return "Short"; + case Type::UShort: + return "UShort"; + case Type::Int: + return "Int"; + case Type::UInt: + return "UInt"; + case Type::Int64: + return "Int64"; + case Type::UInt64: + return "UInt64"; + case Type::AtomicCounter: + return "AtomicCounter"; + case Type::Half: + return "Half"; + case Type::Float: + return "Float"; + case Type::Double: + return "Double"; + case Type::Struct: + return "Struct"; + case Type::Image: + return "Image"; + case Type::SampledImage: + return "SampledImage"; + case Type::Sampler: + return "Sampler"; + case Type::AccelerationStructure: + return "AccelerationStructure"; + case Type::RayQuery: + return "RayQuery"; + default: + return "unknown"; + } +} + +static bool ReflectType(Writer& writer, + const spirv_cross::CompilerMSL& compiler, + const spirv_cross::SPIRType& type) { + writer.Key("type"); + writer.StartObject(); + + writer.Key("type_name"); + writer.String(BaseTypeToString(type.basetype)); + + writer.Key("member_types"); + writer.StartArray(); + for (const auto& member : type.member_types) { + if (!ReflectType(writer, compiler, compiler.get_type(member))) { + return false; + } + } + writer.EndArray(); + + writer.EndObject(); + return true; +} + +static bool ReflectBaseResource(Writer& writer, + const spirv_cross::CompilerMSL& compiler, + const spirv_cross::Resource& res) { + writer.Key("name"); + writer.String(compiler.get_name(res.id)); + + writer.Key("descriptor_set"); + writer.Uint64(compiler.get_decoration( + res.id, spv::Decoration::DecorationDescriptorSet)); + + writer.Key("binding"); + writer.Uint64( + compiler.get_decoration(res.id, spv::Decoration::DecorationBinding)); + + writer.Key("location"); + writer.Uint64( + compiler.get_decoration(res.id, spv::Decoration::DecorationLocation)); + + if (!ReflectType(writer, compiler, compiler.get_type(res.type_id))) { + return false; + } + + return true; +} + +static bool ReflectStageInput(Writer& writer, + const spirv_cross::CompilerMSL& compiler, + const spirv_cross::Resource& input) { + writer.StartObject(); + + if (!ReflectBaseResource(writer, compiler, input)) { + return false; + } + + writer.EndObject(); + return true; +} + +static bool ReflectUniformBuffer(Writer& writer, + const spirv_cross::CompilerMSL& compiler, + const spirv_cross::Resource& buffer) { + writer.StartObject(); + + if (!ReflectBaseResource(writer, compiler, buffer)) { + return false; + } + + writer.Key("index"); + writer.Uint64( + compiler.get_decoration(buffer.id, spv::Decoration::DecorationIndex)); + + writer.EndObject(); + return true; +} + +static std::string ExecutionModelToString(spv::ExecutionModel model) { + switch (model) { + case spv::ExecutionModel::ExecutionModelVertex: + return "vertex"; + case spv::ExecutionModel::ExecutionModelFragment: + return "fragment"; + default: + return "unsupported"; + } +} + +static std::shared_ptr ReflectTemplateArguments( + const spirv_cross::CompilerMSL& compiler) { + auto buffer = std::make_shared(); + Writer writer(*buffer); + + writer.StartObject(); // root + + { + const auto& entrypoints = compiler.get_entry_points_and_stages(); + if (entrypoints.size() != 1) { + FML_LOG(ERROR) << "Incorrect number of entrypoints in the shader. Found " + << entrypoints.size() << " but expected 1."; + return nullptr; + } + + writer.Key("entrypoint"); + writer.String(entrypoints.front().name); + + writer.Key("shader_stage"); + writer.String(ExecutionModelToString(entrypoints.front().execution_model)); + } + + { + writer.Key("uniform_buffers"); + writer.StartArray(); + for (const auto& uniform_buffer : + compiler.get_shader_resources().uniform_buffers) { + if (!ReflectUniformBuffer(writer, compiler, uniform_buffer)) { + FML_LOG(ERROR) << "Could not reflect uniform buffer."; + return nullptr; + } + } + writer.EndArray(); + } + + { + writer.Key("stage_inputs"); + writer.StartArray(); + for (const auto& input : compiler.get_shader_resources().stage_inputs) { + if (!ReflectStageInput(writer, compiler, input)) { + FML_LOG(ERROR) << "Could not reflect uniform buffer."; + return nullptr; + } + } + writer.EndArray(); + } + + writer.EndObject(); // root + + return std::make_shared( + buffer->GetString(), buffer->GetSize(), [buffer]() {}); +} + +static std::shared_ptr ConstructReflectionHeader( + const spirv_cross::CompilerMSL& compiler, + const fml::Mapping* reflection_args) { + return nullptr; +} + +static std::shared_ptr ConstructReflectionCC( + const spirv_cross::CompilerMSL& compiler, + const fml::Mapping* reflection_args) { + return nullptr; +} + +Reflector::Reflector(const spirv_cross::CompilerMSL& compiler) + : template_arguments_(ReflectTemplateArguments(compiler)), + reflection_header_( + ConstructReflectionHeader(compiler, template_arguments_.get())), + reflection_cc_( + ConstructReflectionCC(compiler, template_arguments_.get())) { + if (!template_arguments_) { + return; + } + + if (!reflection_header_) { + return; + } + + if (!reflection_cc_) { + return; + } + is_valid_ = true; } @@ -21,5 +248,17 @@ bool Reflector::IsValid() const { return is_valid_; } +std::shared_ptr Reflector::GetReflectionJSON() const { + return template_arguments_; +} + +std::shared_ptr Reflector::GetReflectionHeader() const { + return nullptr; +} + +std::shared_ptr Reflector::GetReflectionCC() const { + return nullptr; +} + } // namespace compiler } // namespace impeller diff --git a/engine/src/flutter/impeller/compiler/reflector.h b/engine/src/flutter/impeller/compiler/reflector.h index 06722b8b9d..a68e1a9602 100644 --- a/engine/src/flutter/impeller/compiler/reflector.h +++ b/engine/src/flutter/impeller/compiler/reflector.h @@ -4,6 +4,8 @@ #pragma once +#include + #include "flutter/fml/macros.h" #include "flutter/fml/mapping.h" #include "third_party/spirv_cross/spirv_msl.hpp" @@ -19,7 +21,16 @@ class Reflector { bool IsValid() const; + std::shared_ptr GetReflectionJSON() const; + + std::shared_ptr GetReflectionHeader() const; + + std::shared_ptr GetReflectionCC() const; + private: + std::shared_ptr template_arguments_; + std::shared_ptr reflection_header_; + std::shared_ptr reflection_cc_; bool is_valid_ = false; FML_DISALLOW_COPY_AND_ASSIGN(Reflector); diff --git a/engine/src/flutter/impeller/compiler/switches.cc b/engine/src/flutter/impeller/compiler/switches.cc index 2ea0e8549d..9b01363fd8 100644 --- a/engine/src/flutter/impeller/compiler/switches.cc +++ b/engine/src/flutter/impeller/compiler/switches.cc @@ -16,6 +16,10 @@ void Switches::PrintHelp(std::ostream& stream) { stream << "--input=" << std::endl; stream << "--metal=" << std::endl; stream << "--spirv=" << std::endl; + stream << "[optional] --reflection-json=" << std::endl; + stream << "[optional] --reflection-header=" + << std::endl; + stream << "[optional] --reflection-cc=" << std::endl; stream << "[optional,multiple] --include=" << std::endl; stream << "[optional] --depfile=" << std::endl; } @@ -32,6 +36,12 @@ Switches::Switches(const fml::CommandLine& command_line) source_file_name(command_line.GetOptionValueWithDefault("input", "")), metal_file_name(command_line.GetOptionValueWithDefault("metal", "")), spirv_file_name(command_line.GetOptionValueWithDefault("spirv", "")), + reflection_json_name( + command_line.GetOptionValueWithDefault("reflection-json", "")), + reflection_header_name( + command_line.GetOptionValueWithDefault("reflection-header", "")), + reflection_cc_name( + command_line.GetOptionValueWithDefault("reflection-cc", "")), depfile_path(command_line.GetOptionValueWithDefault("depfile", "")) { if (!working_directory || !working_directory->is_valid()) { return; diff --git a/engine/src/flutter/impeller/compiler/switches.h b/engine/src/flutter/impeller/compiler/switches.h index d9e1df1d92..7028d4ab4e 100644 --- a/engine/src/flutter/impeller/compiler/switches.h +++ b/engine/src/flutter/impeller/compiler/switches.h @@ -21,6 +21,9 @@ struct Switches { std::string source_file_name; std::string metal_file_name; std::string spirv_file_name; + std::string reflection_json_name; + std::string reflection_header_name; + std::string reflection_cc_name; std::string depfile_path; Switches(); diff --git a/engine/src/flutter/impeller/compiler/tools/compiler.gni b/engine/src/flutter/impeller/compiler/tools/compiler.gni index e3230b4244..e6d5c65e1c 100644 --- a/engine/src/flutter/impeller/compiler/tools/compiler.gni +++ b/engine/src/flutter/impeller/compiler/tools/compiler.gni @@ -17,6 +17,9 @@ template("impeller_shaders") { metal_intermediate = "$target_gen_dir/{{source_file_part}}.metal" spirv_intermediate = "$target_gen_dir/{{source_file_part}}.spirv" + reflection_json_intermediate = "$target_gen_dir/{{source_file_part}}.json" + reflection_header_intermediate = "$target_gen_dir/{{source_file_part}}.h" + reflection_cc_intermediate = "$target_gen_dir/{{source_file_part}}.cc" outputs = [ metal_intermediate ] @@ -26,12 +29,21 @@ template("impeller_shaders") { spirv_intermediate_path = rebase_path(spirv_intermediate, root_build_dir) depfile_intermediate_path = rebase_path(depfile_path, root_build_dir) + reflection_json_path = + rebase_path(reflection_json_intermediate, root_build_dir) + reflection_header_path = + rebase_path(reflection_header_intermediate, root_build_dir) + reflection_cc_path = rebase_path(reflection_cc_intermediate, root_build_dir) + depfile = depfile_path args = [ "--input=$source_path", "--metal=$metal_intermediate_path", "--spirv=$spirv_intermediate_path", + "--reflection-json=$reflection_json_path", + "--reflection-header=$reflection_header_path", + "--reflection-cc=$reflection_cc_path", "--include={{source_dir}}", "--depfile=$depfile_intermediate_path", ] diff --git a/engine/src/flutter/impeller/impeller/compositor/shaders/box/box.vert b/engine/src/flutter/impeller/impeller/compositor/shaders/box/box.vert index b3a757b5a4..e8839eb27c 100644 --- a/engine/src/flutter/impeller/impeller/compositor/shaders/box/box.vert +++ b/engine/src/flutter/impeller/impeller/compositor/shaders/box/box.vert @@ -4,16 +4,17 @@ uniform UniformBufferObject { Uniforms uniforms; } ubo; -in vec3 inPosition; +uniform sampler2D world; +in vec2 inPosition; +in vec3 inPosition2; in vec4 inAnotherPosition; +in float stuff; -layout(location = 3) in float stuff; - -out float outStuff; +out vec4 outStuff; void main() { - gl_Position = ubo.uniforms.projection * ubo.uniforms.view * ubo.uniforms.model * vec4(inPosition, 1.0) * inAnotherPosition; - outStuff = stuff; + gl_Position = ubo.uniforms.projection * ubo.uniforms.view * ubo.uniforms.model * vec4(inPosition2, 1.0) * inAnotherPosition; + outStuff = texture(world, inPosition); }