Setup reflection JSON.

This commit is contained in:
Chinmay Garde
2021-05-18 15:07:34 -07:00
committed by Dan Field
parent f7191cde15
commit 7e89c12acc
13 changed files with 343 additions and 32 deletions

View File

@@ -19,6 +19,8 @@ source_set("compiler_lib") {
public_deps = [
"//flutter/fml",
"//flutter/runtime:libdart",
"//third_party/inja",
"//third_party/rapidjson",
"//third_party/shaderc_flutter",
"//third_party/spirv_cross_flutter",
"//third_party/spirv_reflect_flutter",

View File

@@ -298,7 +298,7 @@ Compiler::Compiler(const fml::Mapping& source_mapping,
return;
}
reflector_ = std::make_unique<Reflector>(*GetSPIRVAssembly());
reflector_ = std::make_unique<Reflector>(msl_compiler);
if (!reflector_->IsValid()) {
COMPILER_ERROR << "Could not complete reflection on generated shader.";
@@ -433,5 +433,9 @@ std::unique_ptr<fml::Mapping> Compiler::CreateDepfileContents(
[contents](auto, auto) {});
}
const Reflector* Compiler::GetReflector() const {
return reflector_.get();
}
} // namespace compiler
} // namespace impeller

View File

@@ -61,6 +61,8 @@ class Compiler {
std::unique_ptr<fml::Mapping> CreateDepfileContents(
std::initializer_list<std::string> targets) const;
const Reflector* GetReflector() const;
private:
SourceOptions options_;
std::shared_ptr<shaderc::SpvCompilationResult> spv_result_;

View File

@@ -50,13 +50,16 @@ TEST_F(CompilerTest, ShaderKindMatchingIsSuccessful) {
}
TEST_F(CompilerTest, CanCompileSample) {
constexpr const char* kShaderFixtureName = "sample.frag";
constexpr const char* kShaderFixtureName = "sample.vert";
auto fixture = flutter::testing::OpenFixtureAsMapping(kShaderFixtureName);
ASSERT_NE(fixture->GetMapping(), nullptr);
Compiler::SourceOptions options(kShaderFixtureName);
options.working_directory = std::make_shared<fml::UniqueFD>(
flutter::testing::OpenFixturesDirectory());
Compiler compiler(*fixture.get(), options);
if (!compiler.IsValid()) {
FML_LOG(ERROR) << compiler.GetErrorMessages();
}
ASSERT_TRUE(compiler.IsValid());
WriteCompilerIntermediates(compiler, kShaderFixtureName);
}

View File

@@ -1,23 +1,20 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#include "types.h"
// Uniforms
layout(set = 0, binding = 0) uniform UniformBufferObject {
mat4 mvp;
uniform UniformBufferObject {
Uniforms uniforms;
} ubo;
// In
uniform sampler2D world;
layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inNormal;
layout(location = 2) in vec2 inTextureCoords;
in vec2 inPosition;
in vec3 inPosition22;
in vec4 inAnotherPosition;
in float stuff;
// Out
layout(location = 0) out vec2 outTextureCoords;
out vec4 outStuff;
void main() {
gl_Position = ubo.mvp * vec4(inPosition, 1.0);
outTextureCoords = inTextureCoords;
gl_Position = ubo.uniforms.projection * ubo.uniforms.view * ubo.uniforms.model * vec4(inPosition22, 1.0) * inAnotherPosition;
outStuff = texture(world, inPosition);
}

View File

@@ -2,12 +2,8 @@
// 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 {
Vector4 stuff;
Vector3 more_stuff;
float dunno;
float this_bit_is_not_used;
Vector4 yet_more_stuff;
struct Uniforms {
mat4 model;
mat4 view;
mat4 projection;
};

View File

@@ -65,6 +65,37 @@ bool Main(const fml::CommandLine& command_line) {
return false;
}
if (!switches.reflection_json_name.empty()) {
if (!fml::WriteAtomically(*switches.working_directory,
switches.reflection_json_name.c_str(),
*compiler.GetReflector()->GetReflectionJSON())) {
std::cerr << "Could not write reflection json to "
<< switches.reflection_json_name << std::endl;
return false;
}
}
if (!switches.reflection_header_name.empty()) {
if (!fml::WriteAtomically(
*switches.working_directory,
switches.reflection_header_name.c_str(),
*compiler.GetReflector()->GetReflectionHeader())) {
std::cerr << "Could not write reflection header to "
<< switches.reflection_header_name << std::endl;
return false;
}
}
if (!switches.reflection_cc_name.empty()) {
if (!fml::WriteAtomically(*switches.working_directory,
switches.reflection_cc_name.c_str(),
*compiler.GetReflector()->GetReflectionCC())) {
std::cerr << "Could not write reflection CC to "
<< switches.reflection_cc_name << std::endl;
return false;
}
}
if (!switches.depfile_path.empty()) {
if (!fml::WriteAtomically(
*switches.working_directory, switches.depfile_path.c_str(),

View File

@@ -4,14 +4,241 @@
#include "flutter/impeller/compiler/reflector.h"
#include <optional>
#include <sstream>
#include "flutter/fml/closure.h"
#include "flutter/fml/logging.h"
#include "inja/inja.hpp"
#include "rapidjson/document.h"
#include "rapidjson/prettywriter.h"
#include "rapidjson/rapidjson.h"
#include "rapidjson/stringbuffer.h"
namespace impeller {
namespace compiler {
Reflector::Reflector(const spirv_cross::CompilerMSL& compiler) {
using Writer = rapidjson::PrettyWriter<rapidjson::StringBuffer>;
static std::string BaseTypeToString(spirv_cross::SPIRType::BaseType type) {
using Type = spirv_cross::SPIRType::BaseType;
switch (type) {
case Type::Void:
return "Void";
case Type::Boolean:
return "Boolean";
case Type::SByte:
return "SByte";
case Type::UByte:
return "UByte";
case Type::Short:
return "Short";
case Type::UShort:
return "UShort";
case Type::Int:
return "Int";
case Type::UInt:
return "UInt";
case Type::Int64:
return "Int64";
case Type::UInt64:
return "UInt64";
case Type::AtomicCounter:
return "AtomicCounter";
case Type::Half:
return "Half";
case Type::Float:
return "Float";
case Type::Double:
return "Double";
case Type::Struct:
return "Struct";
case Type::Image:
return "Image";
case Type::SampledImage:
return "SampledImage";
case Type::Sampler:
return "Sampler";
case Type::AccelerationStructure:
return "AccelerationStructure";
case Type::RayQuery:
return "RayQuery";
default:
return "unknown";
}
}
static bool ReflectType(Writer& writer,
const spirv_cross::CompilerMSL& compiler,
const spirv_cross::SPIRType& type) {
writer.Key("type");
writer.StartObject();
writer.Key("type_name");
writer.String(BaseTypeToString(type.basetype));
writer.Key("member_types");
writer.StartArray();
for (const auto& member : type.member_types) {
if (!ReflectType(writer, compiler, compiler.get_type(member))) {
return false;
}
}
writer.EndArray();
writer.EndObject();
return true;
}
static bool ReflectBaseResource(Writer& writer,
const spirv_cross::CompilerMSL& compiler,
const spirv_cross::Resource& res) {
writer.Key("name");
writer.String(compiler.get_name(res.id));
writer.Key("descriptor_set");
writer.Uint64(compiler.get_decoration(
res.id, spv::Decoration::DecorationDescriptorSet));
writer.Key("binding");
writer.Uint64(
compiler.get_decoration(res.id, spv::Decoration::DecorationBinding));
writer.Key("location");
writer.Uint64(
compiler.get_decoration(res.id, spv::Decoration::DecorationLocation));
if (!ReflectType(writer, compiler, compiler.get_type(res.type_id))) {
return false;
}
return true;
}
static bool ReflectStageInput(Writer& writer,
const spirv_cross::CompilerMSL& compiler,
const spirv_cross::Resource& input) {
writer.StartObject();
if (!ReflectBaseResource(writer, compiler, input)) {
return false;
}
writer.EndObject();
return true;
}
static bool ReflectUniformBuffer(Writer& writer,
const spirv_cross::CompilerMSL& compiler,
const spirv_cross::Resource& buffer) {
writer.StartObject();
if (!ReflectBaseResource(writer, compiler, buffer)) {
return false;
}
writer.Key("index");
writer.Uint64(
compiler.get_decoration(buffer.id, spv::Decoration::DecorationIndex));
writer.EndObject();
return true;
}
static std::string ExecutionModelToString(spv::ExecutionModel model) {
switch (model) {
case spv::ExecutionModel::ExecutionModelVertex:
return "vertex";
case spv::ExecutionModel::ExecutionModelFragment:
return "fragment";
default:
return "unsupported";
}
}
static std::shared_ptr<fml::Mapping> ReflectTemplateArguments(
const spirv_cross::CompilerMSL& compiler) {
auto buffer = std::make_shared<rapidjson::StringBuffer>();
Writer writer(*buffer);
writer.StartObject(); // root
{
const auto& entrypoints = compiler.get_entry_points_and_stages();
if (entrypoints.size() != 1) {
FML_LOG(ERROR) << "Incorrect number of entrypoints in the shader. Found "
<< entrypoints.size() << " but expected 1.";
return nullptr;
}
writer.Key("entrypoint");
writer.String(entrypoints.front().name);
writer.Key("shader_stage");
writer.String(ExecutionModelToString(entrypoints.front().execution_model));
}
{
writer.Key("uniform_buffers");
writer.StartArray();
for (const auto& uniform_buffer :
compiler.get_shader_resources().uniform_buffers) {
if (!ReflectUniformBuffer(writer, compiler, uniform_buffer)) {
FML_LOG(ERROR) << "Could not reflect uniform buffer.";
return nullptr;
}
}
writer.EndArray();
}
{
writer.Key("stage_inputs");
writer.StartArray();
for (const auto& input : compiler.get_shader_resources().stage_inputs) {
if (!ReflectStageInput(writer, compiler, input)) {
FML_LOG(ERROR) << "Could not reflect uniform buffer.";
return nullptr;
}
}
writer.EndArray();
}
writer.EndObject(); // root
return std::make_shared<fml::NonOwnedMapping>(
buffer->GetString(), buffer->GetSize(), [buffer]() {});
}
static std::shared_ptr<fml::Mapping> ConstructReflectionHeader(
const spirv_cross::CompilerMSL& compiler,
const fml::Mapping* reflection_args) {
return nullptr;
}
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())) {
if (!template_arguments_) {
return;
}
if (!reflection_header_) {
return;
}
if (!reflection_cc_) {
return;
}
is_valid_ = true;
}
@@ -21,5 +248,17 @@ bool Reflector::IsValid() const {
return is_valid_;
}
std::shared_ptr<fml::Mapping> Reflector::GetReflectionJSON() const {
return template_arguments_;
}
std::shared_ptr<fml::Mapping> Reflector::GetReflectionHeader() const {
return nullptr;
}
std::shared_ptr<fml::Mapping> Reflector::GetReflectionCC() const {
return nullptr;
}
} // namespace compiler
} // namespace impeller

View File

@@ -4,6 +4,8 @@
#pragma once
#include <memory>
#include "flutter/fml/macros.h"
#include "flutter/fml/mapping.h"
#include "third_party/spirv_cross/spirv_msl.hpp"
@@ -19,7 +21,16 @@ class Reflector {
bool IsValid() const;
std::shared_ptr<fml::Mapping> GetReflectionJSON() const;
std::shared_ptr<fml::Mapping> GetReflectionHeader() const;
std::shared_ptr<fml::Mapping> GetReflectionCC() const;
private:
std::shared_ptr<fml::Mapping> template_arguments_;
std::shared_ptr<fml::Mapping> reflection_header_;
std::shared_ptr<fml::Mapping> reflection_cc_;
bool is_valid_ = false;
FML_DISALLOW_COPY_AND_ASSIGN(Reflector);

View File

@@ -16,6 +16,10 @@ 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 << "[optional] --reflection-json=<reflection_json_file>" << std::endl;
stream << "[optional] --reflection-header=<reflection_header_file>"
<< std::endl;
stream << "[optional] --reflection-cc=<reflection_cc_file>" << std::endl;
stream << "[optional,multiple] --include=<include_directory>" << std::endl;
stream << "[optional] --depfile=<depfile_path>" << std::endl;
}
@@ -32,6 +36,12 @@ Switches::Switches(const fml::CommandLine& command_line)
source_file_name(command_line.GetOptionValueWithDefault("input", "")),
metal_file_name(command_line.GetOptionValueWithDefault("metal", "")),
spirv_file_name(command_line.GetOptionValueWithDefault("spirv", "")),
reflection_json_name(
command_line.GetOptionValueWithDefault("reflection-json", "")),
reflection_header_name(
command_line.GetOptionValueWithDefault("reflection-header", "")),
reflection_cc_name(
command_line.GetOptionValueWithDefault("reflection-cc", "")),
depfile_path(command_line.GetOptionValueWithDefault("depfile", "")) {
if (!working_directory || !working_directory->is_valid()) {
return;

View File

@@ -21,6 +21,9 @@ struct Switches {
std::string source_file_name;
std::string metal_file_name;
std::string spirv_file_name;
std::string reflection_json_name;
std::string reflection_header_name;
std::string reflection_cc_name;
std::string depfile_path;
Switches();

View File

@@ -17,6 +17,9 @@ template("impeller_shaders") {
metal_intermediate = "$target_gen_dir/{{source_file_part}}.metal"
spirv_intermediate = "$target_gen_dir/{{source_file_part}}.spirv"
reflection_json_intermediate = "$target_gen_dir/{{source_file_part}}.json"
reflection_header_intermediate = "$target_gen_dir/{{source_file_part}}.h"
reflection_cc_intermediate = "$target_gen_dir/{{source_file_part}}.cc"
outputs = [ metal_intermediate ]
@@ -26,12 +29,21 @@ template("impeller_shaders") {
spirv_intermediate_path = rebase_path(spirv_intermediate, root_build_dir)
depfile_intermediate_path = rebase_path(depfile_path, root_build_dir)
reflection_json_path =
rebase_path(reflection_json_intermediate, root_build_dir)
reflection_header_path =
rebase_path(reflection_header_intermediate, root_build_dir)
reflection_cc_path = rebase_path(reflection_cc_intermediate, root_build_dir)
depfile = depfile_path
args = [
"--input=$source_path",
"--metal=$metal_intermediate_path",
"--spirv=$spirv_intermediate_path",
"--reflection-json=$reflection_json_path",
"--reflection-header=$reflection_header_path",
"--reflection-cc=$reflection_cc_path",
"--include={{source_dir}}",
"--depfile=$depfile_intermediate_path",
]

View File

@@ -4,16 +4,17 @@ uniform UniformBufferObject {
Uniforms uniforms;
} ubo;
in vec3 inPosition;
uniform sampler2D world;
in vec2 inPosition;
in vec3 inPosition2;
in vec4 inAnotherPosition;
in float stuff;
layout(location = 3) in float stuff;
out float outStuff;
out vec4 outStuff;
void main() {
gl_Position = ubo.uniforms.projection * ubo.uniforms.view * ubo.uniforms.model * vec4(inPosition, 1.0) * inAnotherPosition;
outStuff = stuff;
gl_Position = ubo.uniforms.projection * ubo.uniforms.view * ubo.uniforms.model * vec4(inPosition2, 1.0) * inAnotherPosition;
outStuff = texture(world, inPosition);
}