From 1256ee3d1cf928c1b601bc00832c924b2d2e1dd9 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Mon, 10 May 2021 17:09:14 -0700 Subject: [PATCH] Wire up one-shot conversion of GLSL to MSL. --- engine/src/flutter/impeller/compiler/BUILD.gn | 35 +++- .../src/flutter/impeller/compiler/compiler.cc | 169 ++++++++++++++++-- .../src/flutter/impeller/compiler/compiler.h | 24 ++- .../impeller/compiler/compiler_unittests.cc | 34 ++++ engine/src/flutter/impeller/compiler/logger.h | 40 +++++ 5 files changed, 281 insertions(+), 21 deletions(-) create mode 100644 engine/src/flutter/impeller/compiler/compiler_unittests.cc create mode 100644 engine/src/flutter/impeller/compiler/logger.h diff --git a/engine/src/flutter/impeller/compiler/BUILD.gn b/engine/src/flutter/impeller/compiler/BUILD.gn index f22c416536..68c7e50f33 100644 --- a/engine/src/flutter/impeller/compiler/BUILD.gn +++ b/engine/src/flutter/impeller/compiler/BUILD.gn @@ -2,18 +2,45 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -executable("compiler") { - output_name = "impellerc" +import("//flutter/testing/testing.gni") +source_set("compiler_lib") { sources = [ "compiler.cc", "compiler.h", - "impellerc_main.cc", + "logger.h", ] - deps = [ + public_deps = [ "//flutter/fml", "//flutter/runtime:libdart", "//third_party/shaderc_flutter", + "//third_party/spirv_cross_flutter", + ] +} + +executable("compiler") { + output_name = "impellerc" + + sources = [ "impellerc_main.cc" ] + + deps = [ ":compiler_lib" ] +} + +test_fixtures("compiler_test_fixtures") { + fixtures = [ "fixtures/sample.frag" ] +} + +executable("compiler_unittests") { + testonly = true + + output_name = "impellerc_unittests" + + sources = [ "compiler_unittests.cc" ] + + deps = [ + ":compiler_lib", + ":compiler_test_fixtures", + "//flutter/testing", ] } diff --git a/engine/src/flutter/impeller/compiler/compiler.cc b/engine/src/flutter/impeller/compiler/compiler.cc index d9b6ac0e47..0d209fa158 100644 --- a/engine/src/flutter/impeller/compiler/compiler.cc +++ b/engine/src/flutter/impeller/compiler/compiler.cc @@ -4,11 +4,18 @@ #include "flutter/impeller/compiler/compiler.h" +#include "flutter/impeller/compiler/logger.h" + #include namespace impeller { namespace compiler { +#define COMPILER_ERROR \ + ::impeller::compiler::AutoLogger(error_stream_) << GetSourcePrefix() + +#define COMPILER_ERROR_NO_PREFIX ::impeller::compiler::AutoLogger(error_stream_) + class Includer final : public shaderc::CompileOptions::IncluderInterface { public: Includer() = default; @@ -21,29 +28,73 @@ class Includer final : public shaderc::CompileOptions::IncluderInterface { shaderc_include_type type, const char* requesting_source, size_t include_depth) override { + FML_CHECK(false); return nullptr; } // |shaderc::CompileOptions::IncluderInterface| - void ReleaseInclude(shaderc_include_result* data) override {} + void ReleaseInclude(shaderc_include_result* data) override { + FML_CHECK(false); + } private: FML_DISALLOW_COPY_AND_ASSIGN(Includer); }; +static std::string ShaderCErrorToString(shaderc_compilation_status status) { + switch (status) { + case shaderc_compilation_status::shaderc_compilation_status_success: + return "Success"; + case shaderc_compilation_status::shaderc_compilation_status_invalid_stage: + return "Invalid Shader Stage Specified"; + case shaderc_compilation_status:: + shaderc_compilation_status_compilation_error: + return "Compilation Error"; + case shaderc_compilation_status::shaderc_compilation_status_internal_error: + return "Internal Error"; + case shaderc_compilation_status:: + shaderc_compilation_status_null_result_object: + return "Internal error. Null Result Object"; + case shaderc_compilation_status:: + shaderc_compilation_status_invalid_assembly: + return "Invalid Assembly"; + case shaderc_compilation_status:: + shaderc_compilation_status_validation_error: + return "Validation Error"; + case shaderc_compilation_status:: + shaderc_compilation_status_transformation_error: + return "Transformation Error"; + case shaderc_compilation_status:: + shaderc_compilation_status_configuration_error: + return "Configuration Error"; + } + return "Unknown Internal Error"; +} + static shaderc_shader_kind ToShaderCShaderKind(Compiler::SourceType type) { switch (type) { case Compiler::SourceType::kVertexShader: return shaderc_shader_kind::shaderc_vertex_shader; case Compiler::SourceType::kFragmentShader: return shaderc_shader_kind::shaderc_fragment_shader; + case Compiler::SourceType::kUnknown: + break; } - return shaderc_shader_kind::shaderc_vertex_shader; + return shaderc_shader_kind::shaderc_glsl_infer_from_source; } Compiler::Compiler(const fml::Mapping& source_mapping, - SourceOptions source_options) { + SourceOptions source_options) + : options_(source_options) { if (source_mapping.GetMapping() == nullptr) { + COMPILER_ERROR << "Could not read shader source."; + return; + } + + auto shader_kind = ToShaderCShaderKind(source_options.type); + + if (shader_kind == shaderc_shader_kind::shaderc_glsl_infer_from_source) { + COMPILER_ERROR << "Could not figure out shader stage."; return; } @@ -55,30 +106,118 @@ Compiler::Compiler(const fml::Mapping& source_mapping, shaderc_source_language::shaderc_source_language_glsl); options.SetTargetEnvironment( shaderc_target_env::shaderc_target_env_vulkan, - shaderc_env_version::shaderc_env_version_opengl_4_5); + shaderc_env_version::shaderc_env_version_vulkan_1_1); options.SetTargetSpirv(shaderc_spirv_version::shaderc_spirv_version_1_3); options.SetAutoBindUniforms(true); options.SetAutoMapLocations(true); options.SetIncluder(std::make_unique()); - shaderc::Compiler compiler; - if (!compiler.IsValid()) { + shaderc::Compiler spv_compiler; + if (!spv_compiler.IsValid()) { + COMPILER_ERROR << "Could not initialize the GLSL to SPIRV compiler."; + return; + } + + spv_result_ = std::make_shared( + spv_compiler.CompileGlslToSpv( + reinterpret_cast( + source_mapping.GetMapping()), // source_text + source_mapping.GetSize(), // source_text_size + shader_kind, // shader_kind + source_options.file_name.c_str(), // input_file_name + source_options.entry_point_name.c_str(), // entry_point_name + options // options + )); + + if (spv_result_->GetCompilationStatus() != + shaderc_compilation_status::shaderc_compilation_status_success) { + COMPILER_ERROR << "GLSL to SPIRV failed; " + << ShaderCErrorToString(spv_result_->GetCompilationStatus()) + << ". " << spv_result_->GetNumErrors() << " error(s) and " + << spv_result_->GetNumWarnings() << " warning(s)."; + if (spv_result_->GetNumErrors() > 0 || spv_result_->GetNumWarnings() > 0) { + COMPILER_ERROR_NO_PREFIX << spv_result_->GetErrorMessage(); + } + return; + } + + spirv_cross::CompilerMSL msl_compiler( + spv_result_->cbegin(), spv_result_->cend() - spv_result_->cbegin()); + msl_string_ = std::make_shared(msl_compiler.compile()); + + if (!msl_string_) { + COMPILER_ERROR << "Could not generate MSL from SPIRV"; return; } - result_ = compiler.CompileGlslToSpv( - reinterpret_cast( - source_mapping.GetMapping()), // source_text - source_mapping.GetSize(), // source_text_size - ToShaderCShaderKind(source_options.type), // shader_kind - source_options.file_name.c_str(), // input_file_name - source_options.entry_point_name.c_str(), // entry_point_name - options // options - ); is_valid_ = true; } Compiler::~Compiler() = default; +std::unique_ptr Compiler::GetSPIRVAssembly() const { + if (!spv_result_) { + return nullptr; + } + const auto data_length = + (spv_result_->cend() - spv_result_->cbegin()) * + sizeof(decltype(spv_result_)::element_type::element_type); + + return std::make_unique( + reinterpret_cast(spv_result_->cbegin()), data_length, + [result = spv_result_](auto, auto) mutable { result.reset(); }); +} + +std::unique_ptr Compiler::GetMSLShaderSource() const { + if (!msl_string_) { + return nullptr; + } + + return std::make_unique( + reinterpret_cast(msl_string_->c_str()), + msl_string_->length(), + [string = msl_string_](auto, auto) mutable { string.reset(); }); +} + +bool Compiler::IsValid() const { + return is_valid_; +} + +static bool StringEndWith(const std::string& string, + const std::string& suffix) { + if (suffix.size() > string.size()) { + return false; + } + + if (suffix.empty() || suffix.empty()) { + return false; + } + + return string.rfind(suffix) == (string.size() - suffix.size()); +} + +Compiler::SourceType Compiler::SourceTypeFromFileName( + const std::string& file_name) { + if (StringEndWith(file_name, ".vert")) { + return Compiler::SourceType::kVertexShader; + } + + if (StringEndWith(file_name, ".frag")) { + return Compiler::SourceType::kFragmentShader; + } + + return Compiler::SourceType::kUnknown; +} + +std::string Compiler::GetSourcePrefix() const { + std::stringstream stream; + stream << options_.file_name << ": "; + return stream.str(); +} + +std::string Compiler::GetErrorMessages() const { + return error_stream_.str(); +} + } // namespace compiler } // namespace impeller diff --git a/engine/src/flutter/impeller/compiler/compiler.h b/engine/src/flutter/impeller/compiler/compiler.h index 192ec68ffa..6d7647792c 100644 --- a/engine/src/flutter/impeller/compiler/compiler.h +++ b/engine/src/flutter/impeller/compiler/compiler.h @@ -4,11 +4,13 @@ #pragma once +#include #include #include "flutter/fml/macros.h" #include "flutter/fml/mapping.h" #include "shaderc/shaderc.hpp" +#include "third_party/spirv_cross/spirv_msl.hpp" namespace impeller { namespace compiler { @@ -16,6 +18,7 @@ namespace compiler { class Compiler { public: enum class SourceType { + kUnknown, kVertexShader, kFragmentShader, }; @@ -23,9 +26,15 @@ class Compiler { static SourceType SourceTypeFromFileName(const std::string& file_name); struct SourceOptions { - SourceType type = SourceType::kVertexShader; + SourceType type = SourceType::kUnknown; + std::shared_ptr working_directory; std::string file_name = "main.glsl"; std::string entry_point_name = "main"; + + SourceOptions() = default; + + SourceOptions(const std::string& file_name) + : type(SourceTypeFromFileName(file_name)), file_name(file_name) {} }; Compiler(const fml::Mapping& source_mapping, SourceOptions options); @@ -34,10 +43,21 @@ class Compiler { bool IsValid() const; + std::unique_ptr GetSPIRVAssembly() const; + + std::unique_ptr GetMSLShaderSource() const; + + std::string GetErrorMessages() const; + private: - shaderc::SpvCompilationResult result_; + SourceOptions options_; + std::shared_ptr spv_result_; + std::shared_ptr msl_string_; + std::stringstream error_stream_; bool is_valid_ = false; + std::string GetSourcePrefix() const; + FML_DISALLOW_COPY_AND_ASSIGN(Compiler); }; diff --git a/engine/src/flutter/impeller/compiler/compiler_unittests.cc b/engine/src/flutter/impeller/compiler/compiler_unittests.cc new file mode 100644 index 0000000000..a0cc4f758e --- /dev/null +++ b/engine/src/flutter/impeller/compiler/compiler_unittests.cc @@ -0,0 +1,34 @@ +// 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 "flutter/impeller/compiler/compiler.h" +#include "flutter/testing/testing.h" +#include "gtest/gtest.h" + +namespace impeller { +namespace compiler { +namespace testing { + +TEST(CompilerTest, ShaderKindMatchingIsSuccessful) { + ASSERT_EQ(Compiler::SourceTypeFromFileName("hello.vert"), + Compiler::SourceType::kVertexShader); + ASSERT_EQ(Compiler::SourceTypeFromFileName("hello.frag"), + Compiler::SourceType::kFragmentShader); + ASSERT_EQ(Compiler::SourceTypeFromFileName("hello.msl"), + Compiler::SourceType::kUnknown); + ASSERT_EQ(Compiler::SourceTypeFromFileName("hello.glsl"), + Compiler::SourceType::kUnknown); +} + +TEST(CompilerTest, CanCompileSample) { + auto fixture = flutter::testing::OpenFixtureAsMapping("sample.frag"); + ASSERT_NE(fixture->GetMapping(), nullptr); + Compiler::SourceOptions options("sample.frag"); + Compiler compiler(*fixture.get(), options); + ASSERT_TRUE(compiler.IsValid()); +} + +} // namespace testing +} // namespace compiler +} // namespace impeller diff --git a/engine/src/flutter/impeller/compiler/logger.h b/engine/src/flutter/impeller/compiler/logger.h new file mode 100644 index 0000000000..cf4ab47f24 --- /dev/null +++ b/engine/src/flutter/impeller/compiler/logger.h @@ -0,0 +1,40 @@ +// 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 +#include + +#include "flutter/fml/logging.h" +#include "flutter/fml/macros.h" + +namespace impeller { +namespace compiler { + +class AutoLogger { + public: + AutoLogger(std::stringstream& logger) : logger_(logger) {} + + ~AutoLogger() { + logger_ << std::endl; + logger_.flush(); + + FML_DLOG(INFO) << logger_.str(); + } + + template + AutoLogger& operator<<(const T& object) { + logger_ << object; + return *this; + } + + private: + std::stringstream& logger_; + + FML_DISALLOW_COPY_AND_ASSIGN(AutoLogger); +}; + +} // namespace compiler +} // namespace impeller