Fix code generation for stage inputs.

This commit is contained in:
Chinmay Garde
2021-05-19 13:26:12 -07:00
committed by Dan Field
parent 7e89c12acc
commit 16d26f23da
15 changed files with 272 additions and 42 deletions

View File

@@ -6,6 +6,7 @@ import("//flutter/testing/testing.gni")
source_set("compiler_lib") {
sources = [
"code_gen_template.h",
"compiler.cc",
"compiler.h",
"include_dir.h",
@@ -14,6 +15,8 @@ source_set("compiler_lib") {
"reflector.h",
"switches.cc",
"switches.h",
"utilities.cc",
"utilities.h",
]
public_deps = [

View File

@@ -0,0 +1,50 @@
// 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 <string_view>
namespace impeller {
namespace compiler {
constexpr std::string_view kReflectionHeaderTemplate = R"~~(
// THIS FILE IS GENERATED BY impellerc.
// DO NOT EDIT OR CHECK THIS INTO SOURCE CONTROL
#include "shader_types.h"
namespace impeller {
namespace shader {
struct {{camel_case(shader_name)}} {
static constexpr std::string_view kEntrypointName = "{{entrypoint}}";
static constexpr ShaderStage kShaderStage = {{shader_stage}};
// Stage Inputs
{% for stage_input in stage_inputs %}
// Stage input {{stage_input.name}}
static constexpr ShaderStageInput kInput{{camel_case(stage_input.name)}} = {"{{stage_input.name}}", {{stage_input.location}}};
{% endfor %}
}; // struct {{camel_case(shader_name)}}
} // namespace shader
} // namespace impeller
)~~";
constexpr std::string_view kReflectionCCTemplate = R"~~(
// THIS FILE IS GENERATED BY impellerc.
// DO NOT EDIT OR CHECK THIS INTO SOURCE CONTROL
#include "{{header_file_name}}"
namespace impeller {
namespace shader {
} // namespace shader
} // namespace impeller
)~~";
} // namespace compiler
} // namespace impeller

View File

@@ -199,7 +199,8 @@ static spv::ExecutionModel ToExecutionModel(Compiler::SourceType type) {
}
Compiler::Compiler(const fml::Mapping& source_mapping,
SourceOptions source_options)
SourceOptions source_options,
Reflector::Options reflector_options)
: options_(source_options) {
if (source_mapping.GetMapping() == nullptr) {
COMPILER_ERROR << "Could not read shader source.";
@@ -298,7 +299,8 @@ Compiler::Compiler(const fml::Mapping& source_mapping,
return;
}
reflector_ = std::make_unique<Reflector>(msl_compiler);
reflector_ =
std::make_unique<Reflector>(std::move(reflector_options), msl_compiler);
if (!reflector_->IsValid()) {
COMPILER_ERROR << "Could not complete reflection on generated shader.";

View File

@@ -44,7 +44,9 @@ class Compiler {
: type(SourceTypeFromFileName(file_name)), file_name(file_name) {}
};
Compiler(const fml::Mapping& source_mapping, SourceOptions options);
Compiler(const fml::Mapping& source_mapping,
SourceOptions options,
Reflector::Options reflector_options);
~Compiler();

View File

@@ -2,12 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <filesystem>
#include "flutter/fml/command_line.h"
#include "flutter/fml/file.h"
#include "flutter/fml/macros.h"
#include "flutter/fml/mapping.h"
#include "flutter/impeller/compiler/compiler.h"
#include "flutter/impeller/compiler/switches.h"
#include "flutter/impeller/compiler/utilities.h"
#include "third_party/shaderc/libshaderc/include/shaderc/shaderc.hpp"
namespace impeller {
@@ -42,7 +45,15 @@ bool Main(const fml::CommandLine& command_line) {
switches.source_file_name,
Compiler::SourceTypeFromFileName(switches.source_file_name));
Compiler compiler(*source_file_mapping, options);
Reflector::Options reflector_options;
reflector_options.shader_name =
InferShaderNameFromPath(switches.source_file_name);
reflector_options.header_file_name =
std::filesystem::path{switches.reflection_header_name}
.filename()
.native();
Compiler compiler(*source_file_mapping, options, reflector_options);
if (!compiler.IsValid()) {
std::cerr << "Compilation failed." << std::endl;
std::cerr << compiler.GetErrorMessages() << std::endl;

View File

@@ -9,6 +9,8 @@
#include "flutter/fml/closure.h"
#include "flutter/fml/logging.h"
#include "flutter/impeller/compiler/code_gen_template.h"
#include "flutter/impeller/compiler/utilities.h"
#include "inja/inja.hpp"
#include "rapidjson/document.h"
#include "rapidjson/prettywriter.h"
@@ -148,15 +150,16 @@ static bool ReflectUniformBuffer(Writer& writer,
static std::string ExecutionModelToString(spv::ExecutionModel model) {
switch (model) {
case spv::ExecutionModel::ExecutionModelVertex:
return "vertex";
return "ShaderStage::kVertex";
case spv::ExecutionModel::ExecutionModelFragment:
return "fragment";
return "ShaderStage::kFragment";
default:
return "unsupported";
return "ShaderStage::kUnsupported";
}
}
static std::shared_ptr<fml::Mapping> ReflectTemplateArguments(
const Reflector::Options& options,
const spirv_cross::CompilerMSL& compiler) {
auto buffer = std::make_shared<rapidjson::StringBuffer>();
Writer writer(*buffer);
@@ -174,8 +177,14 @@ static std::shared_ptr<fml::Mapping> ReflectTemplateArguments(
writer.Key("entrypoint");
writer.String(entrypoints.front().name);
writer.Key("shader_name");
writer.String(options.shader_name);
writer.Key("shader_stage");
writer.String(ExecutionModelToString(entrypoints.front().execution_model));
writer.Key("header_file_name");
writer.String(options.header_file_name);
}
{
@@ -206,27 +215,46 @@ static std::shared_ptr<fml::Mapping> ReflectTemplateArguments(
writer.EndObject(); // root
return std::make_shared<fml::NonOwnedMapping>(
buffer->GetString(), buffer->GetSize(), [buffer]() {});
reinterpret_cast<const uint8_t*>(buffer->GetString()), buffer->GetSize(),
[buffer](auto, auto) {});
}
static std::shared_ptr<fml::Mapping> ConstructReflectionHeader(
static std::shared_ptr<fml::Mapping> InflateTemplate(
const spirv_cross::CompilerMSL& compiler,
const std::string_view& tmpl,
const fml::Mapping* reflection_args) {
return nullptr;
if (!reflection_args) {
return nullptr;
}
inja::Environment env;
env.set_trim_blocks(true);
env.set_lstrip_blocks(true);
env.add_callback("camel_case", 1u, [](inja::Arguments& args) {
return ConvertToCamelCase(args.at(0u)->get<std::string>());
});
auto template_data = inja::json::parse(
reinterpret_cast<const char*>(reflection_args->GetMapping()));
auto inflated_template =
std::make_shared<std::string>(env.render(tmpl, template_data));
return std::make_shared<fml::NonOwnedMapping>(
reinterpret_cast<const uint8_t*>(inflated_template->data()),
inflated_template->size(), [inflated_template](auto, auto) {});
}
static std::shared_ptr<fml::Mapping> ConstructReflectionCC(
const spirv_cross::CompilerMSL& compiler,
const fml::Mapping* reflection_args) {
return nullptr;
}
Reflector::Reflector(const spirv_cross::CompilerMSL& compiler)
: template_arguments_(ReflectTemplateArguments(compiler)),
reflection_header_(
ConstructReflectionHeader(compiler, template_arguments_.get())),
reflection_cc_(
ConstructReflectionCC(compiler, template_arguments_.get())) {
Reflector::Reflector(Options options, const spirv_cross::CompilerMSL& compiler)
: options_(std::move(options)),
template_arguments_(ReflectTemplateArguments(options_, compiler)),
reflection_header_(InflateTemplate(compiler,
kReflectionHeaderTemplate,
template_arguments_.get())),
reflection_cc_(InflateTemplate(compiler,
kReflectionCCTemplate,
template_arguments_.get())) {
if (!template_arguments_) {
return;
}
@@ -253,11 +281,11 @@ std::shared_ptr<fml::Mapping> Reflector::GetReflectionJSON() const {
}
std::shared_ptr<fml::Mapping> Reflector::GetReflectionHeader() const {
return nullptr;
return reflection_header_;
}
std::shared_ptr<fml::Mapping> Reflector::GetReflectionCC() const {
return nullptr;
return reflection_cc_;
}
} // namespace compiler

View File

@@ -15,7 +15,12 @@ namespace compiler {
class Reflector {
public:
Reflector(const spirv_cross::CompilerMSL& compiler);
struct Options {
std::string shader_name;
std::string header_file_name;
};
Reflector(Options options, const spirv_cross::CompilerMSL& compiler);
~Reflector();
@@ -28,6 +33,7 @@ class Reflector {
std::shared_ptr<fml::Mapping> GetReflectionCC() const;
private:
const Options options_;
std::shared_ptr<fml::Mapping> template_arguments_;
std::shared_ptr<fml::Mapping> reflection_header_;
std::shared_ptr<fml::Mapping> reflection_cc_;

View File

@@ -9,8 +9,8 @@ template("impeller_shaders") {
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) {
impellerc_target_name = "impellerc_$target_name"
compiled_action_foreach(impellerc_target_name) {
tool = "//flutter/impeller/compiler:impellerc"
sources = invoker.shaders
@@ -21,7 +21,11 @@ template("impeller_shaders") {
reflection_header_intermediate = "$target_gen_dir/{{source_file_part}}.h"
reflection_cc_intermediate = "$target_gen_dir/{{source_file_part}}.cc"
outputs = [ metal_intermediate ]
outputs = [
metal_intermediate,
reflection_header_intermediate,
reflection_cc_intermediate,
]
depfile_path = "$target_gen_dir/{{source_file_part}}.d"
source_path = "{{source}}"
@@ -49,9 +53,31 @@ template("impeller_shaders") {
]
}
metal_library(target_name) {
metal_library_target_name = "metal_library_$target_name"
metal_library(metal_library_target_name) {
name = invoker.name
sources = get_target_outputs(":$metal_sources_target_name")
deps = [ ":$metal_sources_target_name" ]
sources = filter_include(get_target_outputs(":$impellerc_target_name"),
[ "*.metal" ])
deps = [ ":$impellerc_target_name" ]
}
shader_glue_target_name = "glue_$target_name"
source_set(shader_glue_target_name) {
sources = filter_include(get_target_outputs(":$impellerc_target_name"),
[
"*.h",
"*.cc",
])
deps = [
":$impellerc_target_name",
"//flutter/impeller/impeller:impeller_shader_glue_lib",
]
}
group(target_name) {
deps = [
":$metal_library_target_name",
":$shader_glue_target_name",
]
}
}

View File

@@ -0,0 +1,41 @@
// 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/utilities.h"
#include <filesystem>
#include <sstream>
namespace impeller {
namespace compiler {
std::string InferShaderNameFromPath(std::string_view path) {
return std::filesystem::path{path}.stem().string();
}
std::string ConvertToCamelCase(std::string_view string) {
if (string.empty()) {
return "";
}
std::stringstream stream;
bool next_upper = true;
for (size_t i = 0, count = string.length(); i < count; i++) {
auto ch = string.data()[i];
if (next_upper) {
next_upper = false;
stream << static_cast<char>(std::toupper(ch));
continue;
}
if (ch == '_') {
next_upper = true;
continue;
}
stream << ch;
}
return stream.str();
}
} // namespace compiler
} // namespace impeller

View File

@@ -0,0 +1,20 @@
// 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 <string>
#include <string_view>
#include "flutter/fml/macros.h"
namespace impeller {
namespace compiler {
std::string InferShaderNameFromPath(std::string_view path);
std::string ConvertToCamelCase(std::string_view string);
} // namespace compiler
} // namespace impeller

View File

@@ -17,6 +17,18 @@ impeller_shaders("shaders") {
]
}
config("impeller_shader_glue_lib_config") {
include_dirs = [ "compositor" ]
}
source_set("impeller_shader_glue_lib") {
public_configs = [ ":impeller_shader_glue_lib_config" ]
public = [ "compositor/shader_types.h" ]
sources = [ "compositor/shader_types.cc" ]
}
source_set("impeller") {
public_configs = [ ":impeller_config" ]
@@ -94,6 +106,7 @@ source_set("impeller") {
compositor_sources + geometry_sources + entity_sources + image_sources
deps = [
":impeller_shader_glue_lib",
":shaders",
"//flutter/fml",
"//flutter/impeller/third_party/stb",

View File

@@ -10,16 +10,12 @@
#include <string>
#include "flutter/fml/macros.h"
#include "flutter/impeller/impeller/compositor/shader_types.h"
namespace impeller {
class Context;
enum class ShaderStage {
kVertex,
kFragment,
};
class ShaderFunction {
public:
~ShaderFunction();

View File

@@ -0,0 +1,11 @@
// 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/impeller/compositor/shader_types.h"
namespace impeller {
//
} // namespace impeller

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.
#include <cstddef>
#include <string_view>
namespace impeller {
enum class ShaderStage {
kUnknown,
kVertex,
kFragment,
};
struct ShaderStageInput {
const char* name;
std::size_t location;
};
} // namespace impeller

View File

@@ -6,15 +6,15 @@ uniform UniformBufferObject {
uniform sampler2D world;
in vec2 inPosition;
in vec3 inPosition2;
in vec4 inAnotherPosition;
in vec2 position;
in vec3 position2;
in vec4 anotherPosition;
in float stuff;
out vec4 outStuff;
out vec4 otherStuff;
void main() {
gl_Position = ubo.uniforms.projection * ubo.uniforms.view * ubo.uniforms.model * vec4(inPosition2, 1.0) * inAnotherPosition;
outStuff = texture(world, inPosition);
gl_Position = ubo.uniforms.projection * ubo.uniforms.view * ubo.uniforms.model * vec4(position2, 1.0) * anotherPosition;
otherStuff = texture(world, position);
}