Wire up support for include dirs.

This commit is contained in:
Chinmay Garde
2021-05-13 12:18:06 -07:00
committed by Dan Field
parent 815f123c31
commit 6f4dadc939
9 changed files with 82 additions and 12 deletions

View File

@@ -26,12 +26,49 @@ struct IncluderData {
class Includer final : public shaderc::CompileOptions::IncluderInterface {
public:
Includer(std::shared_ptr<fml::UniqueFD> working_directory)
: working_directory_(std::move(working_directory)) {}
Includer(std::shared_ptr<fml::UniqueFD> working_directory,
std::vector<std::shared_ptr<fml::UniqueFD>> include_dirs)
: working_directory_(std::move(working_directory)),
include_dirs_(std::move(include_dirs)) {}
// |shaderc::CompileOptions::IncluderInterface|
~Includer() override = default;
std::unique_ptr<fml::FileMapping> TryOpenMapping(
const std::shared_ptr<fml::UniqueFD>& 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<fml::FileMapping> 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<fml::UniqueFD> working_directory_;
std::vector<std::shared_ptr<fml::UniqueFD>> 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<Includer>(options_.working_directory));
options.SetIncluder(std::make_unique<Includer>(options_.working_directory,
options_.include_dirs));
shaderc::Compiler spv_compiler;
if (!spv_compiler.IsValid()) {

View File

@@ -29,6 +29,7 @@ class Compiler {
struct SourceOptions {
SourceType type = SourceType::kUnknown;
std::shared_ptr<fml::UniqueFD> working_directory;
std::vector<std::shared_ptr<fml::UniqueFD>> include_dirs;
std::string file_name = "main.glsl";
std::string entry_point_name = "main";

View File

@@ -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()) {

View File

@@ -20,8 +20,6 @@ class AutoLogger {
~AutoLogger() {
logger_ << std::endl;
logger_.flush();
FML_DLOG(INFO) << logger_.str();
}
template <class T>

View File

@@ -16,6 +16,7 @@ void Switches::PrintHelp(std::ostream& stream) {
stream << "--input=<glsl_file>" << std::endl;
stream << "--metal=<metal_output_file>" << std::endl;
stream << "--spirv=<spirv_output_file>" << std::endl;
stream << "[Multiple] --include=<include_directory>" << 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::UniqueFD>(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;

View File

@@ -16,6 +16,7 @@ namespace compiler {
struct Switches {
std::shared_ptr<fml::UniqueFD> working_directory;
std::vector<std::shared_ptr<fml::UniqueFD>> include_directories;
std::string source_file_name;
std::string metal_file_name;
std::string spirv_file_name;

View File

@@ -28,6 +28,7 @@ template("impeller_shaders") {
"--input=$source_path",
"--metal=$metal_intermediate_path",
"--spirv=$spirv_intermediate_path",
"--include={{source_dir}}",
]
}
}

View File

@@ -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;
}

View File

@@ -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;
};