Wire up depfile support and Metal shader linking in GN rules.

This commit is contained in:
Chinmay Garde
2021-05-14 11:27:30 -07:00
committed by Dan Field
parent 2afe4d2fbb
commit 87d1a4fa8e
15 changed files with 282 additions and 120 deletions

View File

@@ -8,6 +8,7 @@ source_set("compiler_lib") {
sources = [
"compiler.cc",
"compiler.h",
"include_dir.h",
"logger.h",
"reflector.cc",
"reflector.h",

View File

@@ -4,9 +4,11 @@
#include "flutter/impeller/compiler/compiler.h"
#include "flutter/impeller/compiler/logger.h"
#include <memory>
#include <sstream>
#include "flutter/fml/paths.h"
#include "flutter/impeller/compiler/logger.h"
namespace impeller {
namespace compiler {
@@ -27,17 +29,19 @@ struct IncluderData {
class Includer final : public shaderc::CompileOptions::IncluderInterface {
public:
Includer(std::shared_ptr<fml::UniqueFD> working_directory,
std::vector<std::shared_ptr<fml::UniqueFD>> include_dirs)
std::vector<IncludeDir> include_dirs,
std::function<void(std::string)> on_file_included)
: working_directory_(std::move(working_directory)),
include_dirs_(std::move(include_dirs)) {}
include_dirs_(std::move(include_dirs)),
on_file_included_(std::move(on_file_included)) {}
// |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()) {
const IncludeDir& dir,
const char* requested_source) {
if (!dir.dir || !dir.dir->is_valid()) {
return nullptr;
}
@@ -50,15 +54,27 @@ class Includer final : public shaderc::CompileOptions::IncluderInterface {
return nullptr;
}
return fml::FileMapping::CreateReadOnly(*des, requested_source);
auto mapping = fml::FileMapping::CreateReadOnly(*dir.dir, requested_source);
if (!mapping || !mapping->IsValid()) {
return nullptr;
}
on_file_included_(fml::paths::JoinPaths({dir.name, requested_source}));
return mapping;
}
std::unique_ptr<fml::FileMapping> FindFirstMapping(
const char* requested_source) const {
const char* requested_source) {
// Always try the working directory first no matter what the include
// directories are.
if (auto mapping = TryOpenMapping(working_directory_, requested_source)) {
return mapping;
{
IncludeDir dir;
dir.name = ".";
dir.dir = working_directory_;
if (auto mapping = TryOpenMapping(dir, requested_source)) {
return mapping;
}
}
for (const auto& include_dir : include_dirs_) {
@@ -120,8 +136,8 @@ class Includer final : public shaderc::CompileOptions::IncluderInterface {
private:
std::shared_ptr<fml::UniqueFD> working_directory_;
std::vector<std::shared_ptr<fml::UniqueFD>> include_dirs_;
std::string included_file_paths_;
std::vector<IncludeDir> include_dirs_;
std::function<void(std::string)> on_file_included_;
FML_DISALLOW_COPY_AND_ASSIGN(Includer);
};
@@ -203,8 +219,14 @@ Compiler::Compiler(const fml::Mapping& source_mapping,
options.SetAutoBindUniforms(true);
options.SetAutoMapLocations(true);
options.SetIncluder(std::make_unique<Includer>(options_.working_directory,
options_.include_dirs));
options.AddMacroDefinition("IMPELLER_DEVICE");
std::vector<std::string> included_file_names;
options.SetIncluder(std::make_unique<Includer>(
options_.working_directory, options_.include_dirs,
[&included_file_names](auto included_name) {
included_file_names.emplace_back(std::move(included_name));
}));
shaderc::Compiler spv_compiler;
if (!spv_compiler.IsValid()) {
@@ -234,6 +256,8 @@ Compiler::Compiler(const fml::Mapping& source_mapping,
COMPILER_ERROR_NO_PREFIX << spv_result_->GetErrorMessage();
}
return;
} else {
included_file_names_ = std::move(included_file_names);
}
// MSL Generation.
@@ -329,5 +353,43 @@ std::string Compiler::GetErrorMessages() const {
return error_stream_.str();
}
const std::vector<std::string>& Compiler::GetIncludedFileNames() const {
return included_file_names_;
}
static std::string JoinStrings(std::vector<std::string> items,
std::string separator) {
std::stringstream stream;
for (size_t i = 0, count = items.size(); i < count; i++) {
const auto is_last = (i == count - 1);
stream << items[i];
if (!is_last) {
stream << separator;
}
}
return stream.str();
}
std::string Compiler::GetDependencyNames(std::string separator) const {
std::vector<std::string> dependencies = included_file_names_;
dependencies.push_back(options_.file_name);
return JoinStrings(dependencies, separator);
}
std::unique_ptr<fml::Mapping> Compiler::CreateDepfileContents(
std::initializer_list<std::string> targets_names) const {
const auto targets = JoinStrings(targets_names, " ");
const auto dependencies = GetDependencyNames(" ");
std::stringstream stream;
stream << targets << ":\n\t" << dependencies << "\n";
auto contents = std::make_shared<std::string>(stream.str());
return std::make_unique<fml::NonOwnedMapping>(
reinterpret_cast<const uint8_t*>(contents->data()), contents->size(),
[contents](auto, auto) {});
}
} // namespace compiler
} // namespace impeller

View File

@@ -4,11 +4,13 @@
#pragma once
#include <initializer_list>
#include <sstream>
#include <string>
#include "flutter/fml/macros.h"
#include "flutter/fml/mapping.h"
#include "flutter/impeller/compiler/include_dir.h"
#include "flutter/impeller/compiler/reflector.h"
#include "shaderc/shaderc.hpp"
#include "third_party/spirv_cross/spirv_msl.hpp"
@@ -29,7 +31,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::vector<IncludeDir> include_dirs;
std::string file_name = "main.glsl";
std::string entry_point_name = "main";
@@ -51,16 +53,24 @@ class Compiler {
std::string GetErrorMessages() const;
const std::vector<std::string>& GetIncludedFileNames() const;
std::unique_ptr<fml::Mapping> CreateDepfileContents(
std::initializer_list<std::string> targets) const;
private:
SourceOptions options_;
std::shared_ptr<shaderc::SpvCompilationResult> spv_result_;
std::shared_ptr<std::string> msl_string_;
std::stringstream error_stream_;
std::unique_ptr<Reflector> reflector_;
std::vector<std::string> included_file_names_;
bool is_valid_ = false;
std::string GetSourcePrefix() const;
std::string GetDependencyNames(std::string separator) const;
FML_DISALLOW_COPY_AND_ASSIGN(Compiler);
};

View File

@@ -0,0 +1,16 @@
// 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.
#ifdef IMPELLER_DEVICE
#define Vector3 vec3
#define Vector4 vec4
#define Matrix mat4
#else // IMPELLER_DEVICE
#include "flutter/impeller/impeller/geometry/matrix.h"
#include "flutter/impeller/impeller/geometry/vector.h"
#endif // IMPELLER_DEVICE

View File

@@ -1,8 +1,13 @@
// 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 "common_types.h"
struct Hello {
vec4 stuff;
vec2 more_stuff;
Vector4 stuff;
Vector3 more_stuff;
float dunno;
float this_bit_is_not_used;
vec2 yet_more_stuff;
Vector4 yet_more_stuff;
};

View File

@@ -62,6 +62,16 @@ bool Main(const fml::CommandLine& command_line) {
return false;
}
if (!switches.depfile_path.empty()) {
if (!fml::WriteAtomically(
*switches.working_directory, switches.depfile_path.c_str(),
*compiler.CreateDepfileContents({switches.metal_file_name}))) {
std::cerr << "Could not write depfile to " << switches.depfile_path
<< std::endl;
return false;
}
}
return true;
}

View File

@@ -0,0 +1,21 @@
// 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 <memory>
#include <string>
#include "flutter/fml/unique_fd.h"
namespace impeller {
namespace compiler {
struct IncludeDir {
std::shared_ptr<fml::UniqueFD> dir;
std::string name;
};
} // namespace compiler
} // namespace impeller

View File

@@ -11,108 +11,108 @@
namespace impeller {
namespace compiler {
std::string SpvReflectInterfaceVariableToString(
const SpvReflectInterfaceVariable& var) {
std::stringstream stream;
stream << "--- Interface Variable" << std::endl;
stream << "spirv_id=" << var.spirv_id << std::endl;
if (var.name) {
stream << "name=" << var.name << std::endl;
}
stream << "location=" << var.location << std::endl;
stream << "storage_class=" << var.storage_class << std::endl;
if (var.semantic) {
stream << "semantic=" << var.semantic << std::endl;
}
stream << "decoration_flags=" << var.decoration_flags << std::endl;
stream << "built_in=" << var.built_in << std::endl;
// stream << "numeric=" << var.numeric << std::endl;
// stream << "array=" << var.array << std::endl;
stream << "member_count=" << var.member_count << std::endl;
// stream << "members=" << var.members << std::endl;
stream << "format=" << var.format << std::endl;
stream << "type_description=" << var.type_description << std::endl;
return stream.str();
}
// std::string SpvReflectInterfaceVariableToString(
// const SpvReflectInterfaceVariable& var) {
// std::stringstream stream;
// stream << "--- Interface Variable" << std::endl;
// stream << "spirv_id=" << var.spirv_id << std::endl;
// if (var.name) {
// stream << "name=" << var.name << std::endl;
// }
// stream << "location=" << var.location << std::endl;
// stream << "storage_class=" << var.storage_class << std::endl;
// if (var.semantic) {
// stream << "semantic=" << var.semantic << std::endl;
// }
// stream << "decoration_flags=" << var.decoration_flags << std::endl;
// stream << "built_in=" << var.built_in << std::endl;
// // stream << "numeric=" << var.numeric << std::endl;
// // stream << "array=" << var.array << std::endl;
// stream << "member_count=" << var.member_count << std::endl;
// // stream << "members=" << var.members << std::endl;
// stream << "format=" << var.format << std::endl;
// stream << "type_description=" << var.type_description << std::endl;
// return stream.str();
// }
std::string SpvReflectDescriptorBindingToString(
const SpvReflectDescriptorBinding& des) {
std::stringstream stream;
stream << "--- Descriptor Set Binding" << std::endl;
stream << "spirv_id=" << des.spirv_id << std::endl;
if (des.name) {
stream << "name=" << des.name << std::endl;
}
stream << "binding=" << des.binding << std::endl;
stream << "input_attachment_index=" << des.input_attachment_index
<< std::endl;
stream << "set=" << des.set << std::endl;
stream << "descriptor_type=" << des.descriptor_type << std::endl;
stream << "resource_type=" << des.resource_type << std::endl;
// stream << "image=" << des.image << std::endl;
// stream << "block=" << des.block << std::endl;
// stream << "array=" << des.array << std::endl;
stream << "count=" << des.count << std::endl;
stream << "accessed=" << des.accessed << std::endl;
stream << "uav_counter_id=" << des.uav_counter_id << std::endl;
stream << "uav_counter_binding=" << des.uav_counter_binding << std::endl;
stream << "type_description=" << des.type_description << std::endl;
return stream.str();
}
// std::string SpvReflectDescriptorBindingToString(
// const SpvReflectDescriptorBinding& des) {
// std::stringstream stream;
// stream << "--- Descriptor Set Binding" << std::endl;
// stream << "spirv_id=" << des.spirv_id << std::endl;
// if (des.name) {
// stream << "name=" << des.name << std::endl;
// }
// stream << "binding=" << des.binding << std::endl;
// stream << "input_attachment_index=" << des.input_attachment_index
// << std::endl;
// stream << "set=" << des.set << std::endl;
// stream << "descriptor_type=" << des.descriptor_type << std::endl;
// stream << "resource_type=" << des.resource_type << std::endl;
// // stream << "image=" << des.image << std::endl;
// // stream << "block=" << des.block << std::endl;
// // stream << "array=" << des.array << std::endl;
// stream << "count=" << des.count << std::endl;
// stream << "accessed=" << des.accessed << std::endl;
// stream << "uav_counter_id=" << des.uav_counter_id << std::endl;
// stream << "uav_counter_binding=" << des.uav_counter_binding << std::endl;
// stream << "type_description=" << des.type_description << std::endl;
// return stream.str();
// }
Reflector::Reflector(const fml::Mapping& spirv_binary) {
if (spirv_binary.GetMapping() == nullptr) {
return;
}
// if (spirv_binary.GetMapping() == nullptr) {
// return;
// }
spv_reflect::ShaderModule module(spirv_binary.GetSize(),
spirv_binary.GetMapping());
// spv_reflect::ShaderModule module(spirv_binary.GetSize(),
// spirv_binary.GetMapping());
if (module.GetResult() != SpvReflectResult::SPV_REFLECT_RESULT_SUCCESS) {
return;
}
// if (module.GetResult() != SpvReflectResult::SPV_REFLECT_RESULT_SUCCESS) {
// return;
// }
FML_LOG(ERROR) << "~~~~~~~~~~~~~ Interface Variables ~~~~~~~~~~~~~";
// FML_LOG(ERROR) << "~~~~~~~~~~~~~ Interface Variables ~~~~~~~~~~~~~";
{
uint32_t count = 0;
if (module.EnumerateInterfaceVariables(&count, nullptr) !=
SpvReflectResult::SPV_REFLECT_RESULT_SUCCESS) {
return;
}
// {
// uint32_t count = 0;
// if (module.EnumerateInterfaceVariables(&count, nullptr) !=
// SpvReflectResult::SPV_REFLECT_RESULT_SUCCESS) {
// return;
// }
std::vector<SpvReflectInterfaceVariable*> input_variables;
input_variables.resize(count);
if (module.EnumerateInterfaceVariables(&count, input_variables.data()) !=
SpvReflectResult::SPV_REFLECT_RESULT_SUCCESS) {
return;
}
// std::vector<SpvReflectInterfaceVariable*> input_variables;
// input_variables.resize(count);
// if (module.EnumerateInterfaceVariables(&count, input_variables.data()) !=
// SpvReflectResult::SPV_REFLECT_RESULT_SUCCESS) {
// return;
// }
for (const auto& input_variable : input_variables) {
FML_LOG(ERROR) << SpvReflectInterfaceVariableToString(*input_variable);
}
}
// for (const auto& input_variable : input_variables) {
// FML_LOG(ERROR) << SpvReflectInterfaceVariableToString(*input_variable);
// }
// }
FML_LOG(ERROR) << "~~~~~~~~~~~~~ Descriptor Bindings ~~~~~~~~~~~~~";
// FML_LOG(ERROR) << "~~~~~~~~~~~~~ Descriptor Bindings ~~~~~~~~~~~~~";
{
uint32_t count = 0;
if (module.EnumerateDescriptorBindings(&count, nullptr) !=
SpvReflectResult::SPV_REFLECT_RESULT_SUCCESS) {
return;
}
// {
// uint32_t count = 0;
// if (module.EnumerateDescriptorBindings(&count, nullptr) !=
// SpvReflectResult::SPV_REFLECT_RESULT_SUCCESS) {
// return;
// }
std::vector<SpvReflectDescriptorBinding*> input_variables;
input_variables.resize(count);
if (module.EnumerateDescriptorBindings(&count, input_variables.data()) !=
SpvReflectResult::SPV_REFLECT_RESULT_SUCCESS) {
return;
}
// std::vector<SpvReflectDescriptorBinding*> input_variables;
// input_variables.resize(count);
// if (module.EnumerateDescriptorBindings(&count, input_variables.data()) !=
// SpvReflectResult::SPV_REFLECT_RESULT_SUCCESS) {
// return;
// }
for (const auto& input_variable : input_variables) {
FML_LOG(ERROR) << SpvReflectDescriptorBindingToString(*input_variable);
}
}
// for (const auto& input_variable : input_variables) {
// FML_LOG(ERROR) << SpvReflectDescriptorBindingToString(*input_variable);
// }
// }
is_valid_ = true;
}

View File

@@ -16,7 +16,8 @@ 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;
stream << "[optional,multiple] --include=<include_directory>" << std::endl;
stream << "[optional] --depfile=<depfile_path>" << std::endl;
}
Switches::Switches() = default;
@@ -30,10 +31,12 @@ 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", "")),
depfile_path(command_line.GetOptionValueWithDefault("depfile", "")) {
if (!working_directory || !working_directory->is_valid()) {
return;
}
for (const auto& include_dir_path : command_line.GetOptionValues("include")) {
if (!include_dir_path.data()) {
continue;
@@ -43,7 +46,12 @@ Switches::Switches(const fml::CommandLine& command_line)
if (!dir || !dir->is_valid()) {
continue;
}
include_directories.emplace_back(std::move(dir));
IncludeDir dir_entry;
dir_entry.name = include_dir_path;
dir_entry.dir = std::move(dir);
include_directories.emplace_back(std::move(dir_entry));
}
}

View File

@@ -10,16 +10,18 @@
#include "flutter/fml/command_line.h"
#include "flutter/fml/macros.h"
#include "flutter/fml/unique_fd.h"
#include "flutter/impeller/compiler/include_dir.h"
namespace impeller {
namespace compiler {
struct Switches {
std::shared_ptr<fml::UniqueFD> working_directory;
std::vector<std::shared_ptr<fml::UniqueFD>> include_directories;
std::vector<IncludeDir> include_directories;
std::string source_file_name;
std::string metal_file_name;
std::string spirv_file_name;
std::string depfile_path;
Switches();

View File

@@ -3,11 +3,14 @@
# found in the LICENSE file.
import("//build/compiled_action.gni")
import("//flutter/impeller/tools/metal/metal_library.gni")
template("impeller_shaders") {
compiled_action_foreach(target_name) {
assert(defined(invoker.shaders), "Impeller shaders must be specified.")
assert(defined(invoker.shaders), "Impeller shaders must be specified.")
assert(defined(invoker.name), "Name of the shader library must be specified.")
metal_sources_target_name = "metal_sources_$target_name"
compiled_action_foreach(metal_sources_target_name) {
tool = "//flutter/impeller/compiler:impellerc"
sources = invoker.shaders
@@ -15,20 +18,28 @@ template("impeller_shaders") {
metal_intermediate = "$target_gen_dir/{{source_file_part}}.metal"
spirv_intermediate = "$target_gen_dir/{{source_file_part}}.spirv"
outputs = [
metal_intermediate,
spirv_intermediate,
]
outputs = [ metal_intermediate ]
depfile_path = "$target_gen_dir/{{source_file_part}}.d"
source_path = "{{source}}"
metal_intermediate_path = rebase_path(metal_intermediate, root_build_dir)
spirv_intermediate_path = rebase_path(spirv_intermediate, root_build_dir)
depfile_intermediate_path = rebase_path(depfile_path, root_build_dir)
depfile = depfile_path
args = [
"--input=$source_path",
"--metal=$metal_intermediate_path",
"--spirv=$spirv_intermediate_path",
"--include={{source_dir}}",
"--depfile=$depfile_intermediate_path",
]
}
metal_library(target_name) {
name = invoker.name
sources = get_target_outputs(":$metal_sources_target_name")
deps = [ ":$metal_sources_target_name" ]
}
}

View File

@@ -10,6 +10,7 @@ config("impeller_config") {
}
impeller_shaders("shaders") {
name = "impeller"
shaders = [
"compositor/shaders/box/box.frag",
"compositor/shaders/box/box.vert",

View File

@@ -1,3 +1,5 @@
#include "types.h"
out vec4 outColor;
void main() {

View File

@@ -6,11 +6,14 @@ uniform UniformBufferObject {
in vec3 inPosition;
in float stuff;
in vec4 inAnotherPosition;
layout(location = 3) in float stuff;
out float outStuff;
void main() {
gl_Position = ubo.uniforms.projection * ubo.uniforms.view * ubo.uniforms.model * vec4(inPosition, 1.0);
gl_Position = ubo.uniforms.projection * ubo.uniforms.view * ubo.uniforms.model * vec4(inPosition, 1.0) * inAnotherPosition;
outStuff = stuff;
}

View File

@@ -9,6 +9,16 @@ template("metal_library") {
metal_library_name = invoker.name
action("$target_name") {
forward_variables_from(invoker,
"*",
[
"inputs",
"outputs",
"script",
"depfile",
"args",
])
inputs = invoker.sources
metal_library_path = "$root_out_dir/shaders/$metal_library_name.metallib"