diff --git a/engine/src/flutter/impeller/compiler/compiler.cc b/engine/src/flutter/impeller/compiler/compiler.cc index 0ed0886b7f..435874df6b 100644 --- a/engine/src/flutter/impeller/compiler/compiler.cc +++ b/engine/src/flutter/impeller/compiler/compiler.cc @@ -26,12 +26,49 @@ struct IncluderData { class Includer final : public shaderc::CompileOptions::IncluderInterface { public: - Includer(std::shared_ptr working_directory) - : working_directory_(std::move(working_directory)) {} + Includer(std::shared_ptr working_directory, + std::vector> include_dirs) + : working_directory_(std::move(working_directory)), + include_dirs_(std::move(include_dirs)) {} // |shaderc::CompileOptions::IncluderInterface| ~Includer() override = default; + std::unique_ptr TryOpenMapping( + const std::shared_ptr& des, + const char* requested_source) const { + if (!des || !des->is_valid()) { + return nullptr; + } + + if (requested_source == nullptr) { + return nullptr; + } + + std::string source(requested_source); + if (source.empty()) { + return nullptr; + } + + return fml::FileMapping::CreateReadOnly(*des, requested_source); + } + + std::unique_ptr FindFirstMapping( + const char* requested_source) const { + // Always try the working directory first no matter what the include + // directories are. + if (auto mapping = TryOpenMapping(working_directory_, requested_source)) { + return mapping; + } + + for (const auto& include_dir : include_dirs_) { + if (auto mapping = TryOpenMapping(include_dir, requested_source)) { + return mapping; + } + } + return nullptr; + } + // |shaderc::CompileOptions::IncluderInterface| shaderc_include_result* GetInclude(const char* requested_source, shaderc_include_type type, @@ -56,8 +93,7 @@ class Includer final : public shaderc::CompileOptions::IncluderInterface { return result.release(); } - auto file = - fml::FileMapping::CreateReadOnly(*working_directory_, requested_source); + auto file = FindFirstMapping(requested_source); if (!file || file->GetMapping() == nullptr) { return result.release(); @@ -84,6 +120,7 @@ class Includer final : public shaderc::CompileOptions::IncluderInterface { private: std::shared_ptr working_directory_; + std::vector> include_dirs_; FML_DISALLOW_COPY_AND_ASSIGN(Includer); }; @@ -165,7 +202,8 @@ Compiler::Compiler(const fml::Mapping& source_mapping, options.SetAutoBindUniforms(true); options.SetAutoMapLocations(true); - options.SetIncluder(std::make_unique(options_.working_directory)); + options.SetIncluder(std::make_unique(options_.working_directory, + options_.include_dirs)); shaderc::Compiler spv_compiler; if (!spv_compiler.IsValid()) { diff --git a/engine/src/flutter/impeller/compiler/compiler.h b/engine/src/flutter/impeller/compiler/compiler.h index 87d0e59b12..a162cd98e9 100644 --- a/engine/src/flutter/impeller/compiler/compiler.h +++ b/engine/src/flutter/impeller/compiler/compiler.h @@ -29,6 +29,7 @@ class Compiler { struct SourceOptions { SourceType type = SourceType::kUnknown; std::shared_ptr working_directory; + std::vector> include_dirs; std::string file_name = "main.glsl"; std::string entry_point_name = "main"; diff --git a/engine/src/flutter/impeller/compiler/impellerc_main.cc b/engine/src/flutter/impeller/compiler/impellerc_main.cc index 822bc4c025..4142bd6365 100644 --- a/engine/src/flutter/impeller/compiler/impellerc_main.cc +++ b/engine/src/flutter/impeller/compiler/impellerc_main.cc @@ -37,6 +37,7 @@ bool Main(const fml::CommandLine& command_line) { options.type = Compiler::SourceTypeFromFileName(switches.source_file_name); options.working_directory = switches.working_directory; options.file_name = switches.source_file_name; + options.include_dirs = switches.include_directories; Compiler compiler(*source_file_mapping, options); if (!compiler.IsValid()) { diff --git a/engine/src/flutter/impeller/compiler/logger.h b/engine/src/flutter/impeller/compiler/logger.h index cf4ab47f24..b4938a5f46 100644 --- a/engine/src/flutter/impeller/compiler/logger.h +++ b/engine/src/flutter/impeller/compiler/logger.h @@ -20,8 +20,6 @@ class AutoLogger { ~AutoLogger() { logger_ << std::endl; logger_.flush(); - - FML_DLOG(INFO) << logger_.str(); } template diff --git a/engine/src/flutter/impeller/compiler/switches.cc b/engine/src/flutter/impeller/compiler/switches.cc index 5cb06593d0..e7f2a8b5d6 100644 --- a/engine/src/flutter/impeller/compiler/switches.cc +++ b/engine/src/flutter/impeller/compiler/switches.cc @@ -16,6 +16,7 @@ void Switches::PrintHelp(std::ostream& stream) { stream << "--input=" << std::endl; stream << "--metal=" << std::endl; stream << "--spirv=" << std::endl; + stream << "[Multiple] --include=" << std::endl; } Switches::Switches() = default; @@ -29,7 +30,22 @@ Switches::Switches(const fml::CommandLine& command_line) fml::FilePermission::kRead))), source_file_name(command_line.GetOptionValueWithDefault("input", "")), metal_file_name(command_line.GetOptionValueWithDefault("metal", "")), - spirv_file_name(command_line.GetOptionValueWithDefault("spirv", "")) {} + spirv_file_name(command_line.GetOptionValueWithDefault("spirv", "")) { + if (!working_directory || !working_directory->is_valid()) { + return; + } + for (const auto& include_dir_path : command_line.GetOptionValues("include")) { + if (!include_dir_path.data()) { + continue; + } + auto dir = std::make_shared(fml::OpenDirectoryReadOnly( + *working_directory, include_dir_path.data())); + if (!dir || !dir->is_valid()) { + continue; + } + include_directories.emplace_back(std::move(dir)); + } +} bool Switches::AreValid(std::ostream& explain) const { bool valid = true; diff --git a/engine/src/flutter/impeller/compiler/switches.h b/engine/src/flutter/impeller/compiler/switches.h index 0259f25308..afd437a457 100644 --- a/engine/src/flutter/impeller/compiler/switches.h +++ b/engine/src/flutter/impeller/compiler/switches.h @@ -16,6 +16,7 @@ namespace compiler { struct Switches { std::shared_ptr working_directory; + std::vector> include_directories; std::string source_file_name; std::string metal_file_name; std::string spirv_file_name; diff --git a/engine/src/flutter/impeller/compiler/tools/compiler.gni b/engine/src/flutter/impeller/compiler/tools/compiler.gni index 2ec27e4479..92bdc70df8 100644 --- a/engine/src/flutter/impeller/compiler/tools/compiler.gni +++ b/engine/src/flutter/impeller/compiler/tools/compiler.gni @@ -28,6 +28,7 @@ template("impeller_shaders") { "--input=$source_path", "--metal=$metal_intermediate_path", "--spirv=$spirv_intermediate_path", + "--include={{source_dir}}", ] } } 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 76733c345d..e86bae3da2 100644 --- a/engine/src/flutter/impeller/impeller/compositor/shaders/box/box.vert +++ b/engine/src/flutter/impeller/impeller/compositor/shaders/box/box.vert @@ -1,11 +1,16 @@ +#include "types.h" + uniform UniformBufferObject { - mat4 model; - mat4 view; - mat4 projection; + Uniforms uniforms; } ubo; in vec3 inPosition; +in float stuff; + +out float outStuff; + void main() { - gl_Position = ubo.projection * ubo.view * ubo.model * vec4(inPosition, 1.0); + gl_Position = ubo.uniforms.projection * ubo.uniforms.view * ubo.uniforms.model * vec4(inPosition, 1.0); + outStuff = stuff; } diff --git a/engine/src/flutter/impeller/impeller/compositor/shaders/box/types.h b/engine/src/flutter/impeller/impeller/compositor/shaders/box/types.h new file mode 100644 index 0000000000..d8775956ba --- /dev/null +++ b/engine/src/flutter/impeller/impeller/compositor/shaders/box/types.h @@ -0,0 +1,9 @@ +// 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. + +struct Uniforms { + mat4 model; + mat4 view; + mat4 projection; +};