Wire up reflection.

This commit is contained in:
Chinmay Garde
2021-05-11 19:51:02 -07:00
committed by Dan Field
parent 96b67c1e11
commit 3457f77a68
7 changed files with 109 additions and 14 deletions

View File

@@ -9,6 +9,8 @@ source_set("compiler_lib") {
"compiler.cc",
"compiler.h",
"logger.h",
"reflector.cc",
"reflector.h",
]
public_deps = [

View File

@@ -146,9 +146,14 @@ Compiler::Compiler(const fml::Mapping& source_mapping,
}
shaderc::CompileOptions options;
// Make sure reflection is as effective as possible. The generated shaders
// will be processed later by backend specific compilers. So optimizations
// here are irrelevant and get in the way of generating reflection code.
options.SetGenerateDebugInfo();
options.SetOptimizationLevel(
shaderc_optimization_level::shaderc_optimization_level_size);
shaderc_optimization_level::shaderc_optimization_level_zero);
options.SetSourceLanguage(
shaderc_source_language::shaderc_source_language_glsl);
options.SetForcedVersionProfile(450, shaderc_profile::shaderc_profile_core);
@@ -156,8 +161,10 @@ Compiler::Compiler(const fml::Mapping& source_mapping,
shaderc_target_env::shaderc_target_env_vulkan,
shaderc_env_version::shaderc_env_version_vulkan_1_1);
options.SetTargetSpirv(shaderc_spirv_version::shaderc_spirv_version_1_3);
options.SetAutoBindUniforms(true);
options.SetAutoMapLocations(true);
options.SetIncluder(std::make_unique<Includer>(options_.working_directory));
shaderc::Compiler spv_compiler;
@@ -207,19 +214,11 @@ Compiler::Compiler(const fml::Mapping& source_mapping,
return;
}
const auto resources = msl_compiler.get_shader_resources();
reflector_ = std::make_unique<Reflector>(msl_compiler);
for (const auto& stage_input : resources.stage_inputs) {
FML_LOG(ERROR) << stage_input.name;
}
for (const auto& stage_output : resources.stage_outputs) {
FML_LOG(ERROR) << stage_output.name;
}
for (const auto& uniform_buffer : resources.uniform_buffers) {
FML_LOG(ERROR) << uniform_buffer.name;
auto type = msl_compiler.get_type(uniform_buffer.base_type_id);
if (!reflector_->IsValid()) {
COMPILER_ERROR << "Could not complete reflection on generated shader.";
return;
}
is_valid_ = true;

View File

@@ -9,6 +9,7 @@
#include "flutter/fml/macros.h"
#include "flutter/fml/mapping.h"
#include "flutter/impeller/compiler/reflector.h"
#include "shaderc/shaderc.hpp"
#include "third_party/spirv_cross/spirv_msl.hpp"
@@ -54,6 +55,7 @@ class Compiler {
std::shared_ptr<shaderc::SpvCompilationResult> spv_result_;
std::shared_ptr<std::string> msl_string_;
std::stringstream error_stream_;
std::unique_ptr<Reflector> reflector_;
bool is_valid_ = false;
std::string GetSourcePrefix() const;

View File

@@ -14,5 +14,5 @@ in vec2 inTextureCoord;
out vec4 outColor;
void main() {
outColor = texture(textureSampler, inTextureCoord * myHello.more_stuff);
outColor = texture(textureSampler, inTextureCoord * goodbyeHello.yet_more_stuff);
}

View File

@@ -3,4 +3,6 @@ struct Hello {
vec4 stuff;
vec2 more_stuff;
float dunno;
float this_bit_is_not_used;
vec2 yet_more_stuff;
};

View File

@@ -0,0 +1,62 @@
// 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/reflector.h"
#include "flutter/fml/logging.h"
namespace impeller {
namespace compiler {
std::string SPIRVTypeToString(const spirv_cross::CompilerMSL& compiler,
const spirv_cross::SPIRType& type,
size_t indent = 0) {
std::string pad(indent, '-');
std::stringstream stream;
stream << std::endl;
// stream << pad << "Type ID: " << type.identifier << std::endl;
if (auto identifier = compiler.get_name(type.basetype); !identifier.empty()) {
stream << pad << "OpName: " << identifier << std::endl;
}
for (const auto& member : type.member_types) {
stream << SPIRVTypeToString(compiler, compiler.get_type(member),
indent += 2);
}
return stream.str();
}
Reflector::Reflector(const spirv_cross::CompilerMSL& compiler) {
auto resources = compiler.get_shader_resources();
for (const auto& stage_input : resources.stage_inputs) {
FML_LOG(ERROR) << stage_input.name;
}
for (const auto& stage_output : resources.stage_outputs) {
FML_LOG(ERROR) << stage_output.name;
}
for (const auto& uniform_buffer : resources.uniform_buffers) {
FML_LOG(ERROR) << uniform_buffer.name;
auto type = compiler.get_type(uniform_buffer.type_id);
FML_LOG(ERROR) << SPIRVTypeToString(compiler, type);
}
is_valid_ = true;
}
Reflector::~Reflector() = default;
bool Reflector::IsValid() const {
return is_valid_;
}
} // namespace compiler
} // namespace impeller

View File

@@ -0,0 +1,28 @@
// 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 "flutter/fml/macros.h"
#include "third_party/spirv_cross/spirv_msl.hpp"
namespace impeller {
namespace compiler {
class Reflector {
public:
Reflector(const spirv_cross::CompilerMSL& compiler);
~Reflector();
bool IsValid() const;
private:
bool is_valid_ = false;
FML_DISALLOW_COPY_AND_ASSIGN(Reflector);
};
} // namespace compiler
} // namespace impeller