From 96b67c1e116ed8d7876c1908ce91e8b7c7ac48e9 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Tue, 11 May 2021 14:47:37 -0700 Subject: [PATCH] Implement includer interface. --- engine/src/flutter/impeller/compiler/BUILD.gn | 5 +- .../src/flutter/impeller/compiler/compiler.cc | 82 +++++++++++++++++-- .../impeller/compiler/compiler_unittests.cc | 9 ++ .../impeller/compiler/fixtures/sample.frag | 7 +- .../impeller/compiler/fixtures/types.h | 6 ++ 5 files changed, 97 insertions(+), 12 deletions(-) create mode 100644 engine/src/flutter/impeller/compiler/fixtures/types.h diff --git a/engine/src/flutter/impeller/compiler/BUILD.gn b/engine/src/flutter/impeller/compiler/BUILD.gn index 68c7e50f339..b1b3b4b24b0 100644 --- a/engine/src/flutter/impeller/compiler/BUILD.gn +++ b/engine/src/flutter/impeller/compiler/BUILD.gn @@ -28,7 +28,10 @@ executable("compiler") { } test_fixtures("compiler_test_fixtures") { - fixtures = [ "fixtures/sample.frag" ] + fixtures = [ + "fixtures/sample.frag", + "fixtures/types.h", + ] } executable("compiler_unittests") { diff --git a/engine/src/flutter/impeller/compiler/compiler.cc b/engine/src/flutter/impeller/compiler/compiler.cc index 0d209fa1589..0c2c2bda59d 100644 --- a/engine/src/flutter/impeller/compiler/compiler.cc +++ b/engine/src/flutter/impeller/compiler/compiler.cc @@ -16,9 +16,18 @@ namespace compiler { #define COMPILER_ERROR_NO_PREFIX ::impeller::compiler::AutoLogger(error_stream_) +struct IncluderData { + std::string file_name; + std::unique_ptr mapping; + + IncluderData(std::string p_file_name, std::unique_ptr p_mapping) + : file_name(std::move(p_file_name)), mapping(std::move(p_mapping)) {} +}; + class Includer final : public shaderc::CompileOptions::IncluderInterface { public: - Includer() = default; + Includer(std::shared_ptr working_directory) + : working_directory_(std::move(working_directory)) {} // |shaderc::CompileOptions::IncluderInterface| ~Includer() override = default; @@ -28,16 +37,54 @@ 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; + auto result = std::make_unique(); + + // Default initialize to failed inclusion. + result->source_name = ""; + result->source_name_length = 0; + + constexpr const char* kFileNotFoundMessage = "Included file not found."; + result->content = kFileNotFoundMessage; + result->content_length = ::strlen(kFileNotFoundMessage); + result->user_data = nullptr; + + if (!working_directory_ || !working_directory_->is_valid()) { + return result.release(); + } + + if (requested_source == nullptr) { + return result.release(); + } + + auto file = + fml::FileMapping::CreateReadOnly(*working_directory_, requested_source); + + if (!file || file->GetMapping() == nullptr) { + return result.release(); + } + + auto includer_data = + std::make_unique(requested_source, std::move(file)); + + result->source_name = includer_data->file_name.c_str(); + result->source_name_length = includer_data->file_name.length(); + result->content = reinterpret_castcontent)>( + includer_data->mapping->GetMapping()); + result->content_length = includer_data->mapping->GetSize(); + result->user_data = includer_data.release(); + + return result.release(); } // |shaderc::CompileOptions::IncluderInterface| void ReleaseInclude(shaderc_include_result* data) override { - FML_CHECK(false); + delete reinterpret_cast(data->user_data); + delete data; } private: + std::shared_ptr working_directory_; + FML_DISALLOW_COPY_AND_ASSIGN(Includer); }; @@ -104,13 +151,14 @@ Compiler::Compiler(const fml::Mapping& source_mapping, shaderc_optimization_level::shaderc_optimization_level_size); options.SetSourceLanguage( shaderc_source_language::shaderc_source_language_glsl); + options.SetForcedVersionProfile(450, shaderc_profile::shaderc_profile_core); options.SetTargetEnvironment( shaderc_target_env::shaderc_target_env_vulkan, 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()); + options.SetIncluder(std::make_unique(options_.working_directory)); shaderc::Compiler spv_compiler; if (!spv_compiler.IsValid()) { @@ -118,6 +166,7 @@ Compiler::Compiler(const fml::Mapping& source_mapping, return; } + // SPIRV Generation. spv_result_ = std::make_shared( spv_compiler.CompileGlslToSpv( reinterpret_cast( @@ -141,8 +190,16 @@ Compiler::Compiler(const fml::Mapping& source_mapping, return; } + // MSL Generation. spirv_cross::CompilerMSL msl_compiler( spv_result_->cbegin(), spv_result_->cend() - spv_result_->cbegin()); + + { + spirv_cross::CompilerMSL::Options msl_options; + msl_options.platform = spirv_cross::CompilerMSL::Options::Platform::macOS; + msl_compiler.set_msl_options(msl_options); + } + msl_string_ = std::make_shared(msl_compiler.compile()); if (!msl_string_) { @@ -150,6 +207,21 @@ Compiler::Compiler(const fml::Mapping& source_mapping, return; } + const auto resources = msl_compiler.get_shader_resources(); + + for (const auto& stage_input : resources.stage_inputs) { + FML_LOG(ERROR) << stage_input.name; + } + + for (const auto& stage_output : resources.stage_outputs) { + FML_LOG(ERROR) << stage_output.name; + } + + for (const auto& uniform_buffer : resources.uniform_buffers) { + FML_LOG(ERROR) << uniform_buffer.name; + auto type = msl_compiler.get_type(uniform_buffer.base_type_id); + } + is_valid_ = true; } diff --git a/engine/src/flutter/impeller/compiler/compiler_unittests.cc b/engine/src/flutter/impeller/compiler/compiler_unittests.cc index a0cc4f758e6..09399364590 100644 --- a/engine/src/flutter/impeller/compiler/compiler_unittests.cc +++ b/engine/src/flutter/impeller/compiler/compiler_unittests.cc @@ -25,8 +25,17 @@ TEST(CompilerTest, CanCompileSample) { auto fixture = flutter::testing::OpenFixtureAsMapping("sample.frag"); ASSERT_NE(fixture->GetMapping(), nullptr); Compiler::SourceOptions options("sample.frag"); + options.working_directory = std::make_shared( + flutter::testing::OpenFixturesDirectory()); Compiler compiler(*fixture.get(), options); ASSERT_TRUE(compiler.IsValid()); + + auto desktop = fml::OpenDirectory("/Users/chinmaygarde/Desktop", false, + fml::FilePermission::kRead); + fml::WriteAtomically(desktop, "sample.frag.spirv", + *compiler.GetSPIRVAssembly()); + fml::WriteAtomically(desktop, "sample.frag.metal", + *compiler.GetMSLShaderSource()); } } // namespace testing diff --git a/engine/src/flutter/impeller/compiler/fixtures/sample.frag b/engine/src/flutter/impeller/compiler/fixtures/sample.frag index 4c6fa55dfb0..21aaed4215e 100644 --- a/engine/src/flutter/impeller/compiler/fixtures/sample.frag +++ b/engine/src/flutter/impeller/compiler/fixtures/sample.frag @@ -1,10 +1,5 @@ -#version 450 -struct Hello { - vec4 stuff; - vec2 more_stuff; - float dunno; -}; +#include "types.h" uniform Uniforms { Hello myHello; diff --git a/engine/src/flutter/impeller/compiler/fixtures/types.h b/engine/src/flutter/impeller/compiler/fixtures/types.h new file mode 100644 index 00000000000..e45148586b8 --- /dev/null +++ b/engine/src/flutter/impeller/compiler/fixtures/types.h @@ -0,0 +1,6 @@ + +struct Hello { + vec4 stuff; + vec2 more_stuff; + float dunno; +};