diff --git a/engine/src/flutter/impeller/compiler/BUILD.gn b/engine/src/flutter/impeller/compiler/BUILD.gn index 640563aabe..4ec2db0dfb 100644 --- a/engine/src/flutter/impeller/compiler/BUILD.gn +++ b/engine/src/flutter/impeller/compiler/BUILD.gn @@ -18,6 +18,7 @@ source_set("compiler_lib") { "//flutter/runtime:libdart", "//third_party/shaderc_flutter", "//third_party/spirv_cross_flutter", + "//third_party/spirv_reflect_flutter", ] } @@ -32,6 +33,7 @@ executable("compiler") { test_fixtures("compiler_test_fixtures") { fixtures = [ "fixtures/sample.frag", + "fixtures/sample.vert", "fixtures/types.h", ] } diff --git a/engine/src/flutter/impeller/compiler/compiler.cc b/engine/src/flutter/impeller/compiler/compiler.cc index 1037af643f..0ed0886b7f 100644 --- a/engine/src/flutter/impeller/compiler/compiler.cc +++ b/engine/src/flutter/impeller/compiler/compiler.cc @@ -214,7 +214,7 @@ Compiler::Compiler(const fml::Mapping& source_mapping, return; } - reflector_ = std::make_unique(msl_compiler); + reflector_ = std::make_unique(*GetSPIRVAssembly()); if (!reflector_->IsValid()) { COMPILER_ERROR << "Could not complete reflection on generated shader."; diff --git a/engine/src/flutter/impeller/compiler/compiler_unittests.cc b/engine/src/flutter/impeller/compiler/compiler_unittests.cc index 0939936459..4ff37a35f3 100644 --- a/engine/src/flutter/impeller/compiler/compiler_unittests.cc +++ b/engine/src/flutter/impeller/compiler/compiler_unittests.cc @@ -10,7 +10,35 @@ namespace impeller { namespace compiler { namespace testing { -TEST(CompilerTest, ShaderKindMatchingIsSuccessful) { +class CompilerTest : public ::testing::Test { + public: + CompilerTest() + : directory_(fml::OpenDirectory("/Users/chinmaygarde/Desktop/shaders", + false, + fml::FilePermission::kRead)) { + FML_CHECK(directory_.is_valid()); + } + + ~CompilerTest() {} + + void WriteCompilerIntermediates(const Compiler& compiler, + const std::string& base_name) { + ASSERT_TRUE(compiler.IsValid()); + fml::WriteAtomically(directory_, + std::string{base_name + std::string{".spirv"}}.c_str(), + *compiler.GetSPIRVAssembly()); + fml::WriteAtomically(directory_, + std::string{base_name + std::string{".metal"}}.c_str(), + *compiler.GetMSLShaderSource()); + } + + private: + fml::UniqueFD directory_; + + FML_DISALLOW_COPY_AND_ASSIGN(CompilerTest); +}; + +TEST_F(CompilerTest, ShaderKindMatchingIsSuccessful) { ASSERT_EQ(Compiler::SourceTypeFromFileName("hello.vert"), Compiler::SourceType::kVertexShader); ASSERT_EQ(Compiler::SourceTypeFromFileName("hello.frag"), @@ -21,21 +49,16 @@ TEST(CompilerTest, ShaderKindMatchingIsSuccessful) { Compiler::SourceType::kUnknown); } -TEST(CompilerTest, CanCompileSample) { - auto fixture = flutter::testing::OpenFixtureAsMapping("sample.frag"); +TEST_F(CompilerTest, CanCompileSample) { + constexpr const char* kShaderFixtureName = "sample.frag"; + auto fixture = flutter::testing::OpenFixtureAsMapping(kShaderFixtureName); ASSERT_NE(fixture->GetMapping(), nullptr); - Compiler::SourceOptions options("sample.frag"); + Compiler::SourceOptions options(kShaderFixtureName); options.working_directory = std::make_shared( flutter::testing::OpenFixturesDirectory()); Compiler compiler(*fixture.get(), options); ASSERT_TRUE(compiler.IsValid()); - - auto desktop = fml::OpenDirectory("/Users/chinmaygarde/Desktop", false, - fml::FilePermission::kRead); - fml::WriteAtomically(desktop, "sample.frag.spirv", - *compiler.GetSPIRVAssembly()); - fml::WriteAtomically(desktop, "sample.frag.metal", - *compiler.GetMSLShaderSource()); + WriteCompilerIntermediates(compiler, kShaderFixtureName); } } // namespace testing diff --git a/engine/src/flutter/impeller/compiler/fixtures/sample.frag b/engine/src/flutter/impeller/compiler/fixtures/sample.frag index 7c20b20b57..eef7915f18 100644 --- a/engine/src/flutter/impeller/compiler/fixtures/sample.frag +++ b/engine/src/flutter/impeller/compiler/fixtures/sample.frag @@ -9,9 +9,9 @@ uniform Uniforms { uniform sampler2D textureSampler; -in vec2 inTextureCoord; +layout(location = 3) in vec2 inTextureCoord; -out vec4 outColor; +layout(location = 4) out vec4 outColor; void main() { outColor = texture(textureSampler, inTextureCoord * goodbyeHello.yet_more_stuff); diff --git a/engine/src/flutter/impeller/compiler/fixtures/sample.vert b/engine/src/flutter/impeller/compiler/fixtures/sample.vert new file mode 100644 index 0000000000..727406ac86 --- /dev/null +++ b/engine/src/flutter/impeller/compiler/fixtures/sample.vert @@ -0,0 +1,23 @@ +#version 450 +#extension GL_ARB_separate_shader_objects : enable + +// Uniforms + +layout(set = 0, binding = 0) uniform UniformBufferObject { + mat4 mvp; +} ubo; + +// In + +layout(location = 0) in vec3 inPosition; +layout(location = 1) in vec3 inNormal; +layout(location = 2) in vec2 inTextureCoords; + +// Out + +layout(location = 0) out vec2 outTextureCoords; + +void main() { + gl_Position = ubo.mvp * vec4(inPosition, 1.0); + outTextureCoords = inTextureCoords; +} diff --git a/engine/src/flutter/impeller/compiler/reflector.cc b/engine/src/flutter/impeller/compiler/reflector.cc index 191d56825e..cc1d82c404 100644 --- a/engine/src/flutter/impeller/compiler/reflector.cc +++ b/engine/src/flutter/impeller/compiler/reflector.cc @@ -4,49 +4,114 @@ #include "flutter/impeller/compiler/reflector.h" +#include + #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::string SpvReflectInterfaceVariableToString( + const SpvReflectInterfaceVariable& var) { 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; + stream << "--- Interface Variable" << std::endl; + stream << "spirv_id=" << var.spirv_id << std::endl; + if (var.name) { + stream << "name=" << var.name << std::endl; } - - for (const auto& member : type.member_types) { - stream << SPIRVTypeToString(compiler, compiler.get_type(member), - indent += 2); + 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(); } -Reflector::Reflector(const spirv_cross::CompilerMSL& compiler) { - auto resources = compiler.get_shader_resources(); +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(); +} - for (const auto& stage_input : resources.stage_inputs) { - FML_LOG(ERROR) << stage_input.name; +Reflector::Reflector(const fml::Mapping& spirv_binary) { + if (spirv_binary.GetMapping() == nullptr) { + return; } - for (const auto& stage_output : resources.stage_outputs) { - FML_LOG(ERROR) << stage_output.name; + spv_reflect::ShaderModule module(spirv_binary.GetSize(), + spirv_binary.GetMapping()); + + if (module.GetResult() != SpvReflectResult::SPV_REFLECT_RESULT_SUCCESS) { + return; } - 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) << "~~~~~~~~~~~~~ Interface Variables ~~~~~~~~~~~~~"; - FML_LOG(ERROR) << SPIRVTypeToString(compiler, type); + { + uint32_t count = 0; + if (module.EnumerateInterfaceVariables(&count, nullptr) != + SpvReflectResult::SPV_REFLECT_RESULT_SUCCESS) { + return; + } + + std::vector 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); + } + } + + FML_LOG(ERROR) << "~~~~~~~~~~~~~ Descriptor Bindings ~~~~~~~~~~~~~"; + + { + uint32_t count = 0; + if (module.EnumerateDescriptorBindings(&count, nullptr) != + SpvReflectResult::SPV_REFLECT_RESULT_SUCCESS) { + return; + } + + std::vector 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); + } } is_valid_ = true; diff --git a/engine/src/flutter/impeller/compiler/reflector.h b/engine/src/flutter/impeller/compiler/reflector.h index 3d91130a7c..185d558096 100644 --- a/engine/src/flutter/impeller/compiler/reflector.h +++ b/engine/src/flutter/impeller/compiler/reflector.h @@ -5,14 +5,15 @@ #pragma once #include "flutter/fml/macros.h" -#include "third_party/spirv_cross/spirv_msl.hpp" +#include "flutter/fml/mapping.h" +#include "third_party/spirv_reflect/spirv_reflect.h" namespace impeller { namespace compiler { class Reflector { public: - Reflector(const spirv_cross::CompilerMSL& compiler); + Reflector(const fml::Mapping& spirv_binary); ~Reflector(); diff --git a/engine/src/flutter/impeller/impeller/BUILD.gn b/engine/src/flutter/impeller/impeller/BUILD.gn index 4807e1a4d8..414a45dfef 100644 --- a/engine/src/flutter/impeller/impeller/BUILD.gn +++ b/engine/src/flutter/impeller/impeller/BUILD.gn @@ -8,6 +8,13 @@ config("impeller_config") { include_dirs = [ ".." ] } +impeller_shaders("shaders") { + shaders = [ + "compositor/shaders/box/box.frag", + "compositor/shaders/box/box.vert", + ] +} + source_set("impeller") { public_configs = [ ":impeller_config" ] diff --git a/engine/src/flutter/impeller/impeller/compositor/shaders/box/box.frag b/engine/src/flutter/impeller/impeller/compositor/shaders/box/box.frag index 69036b6cf4..1b9f713ace 100644 --- a/engine/src/flutter/impeller/impeller/compositor/shaders/box/box.frag +++ b/engine/src/flutter/impeller/impeller/compositor/shaders/box/box.frag @@ -1,12 +1,5 @@ -#version 450 -#extension GL_ARB_separate_shader_objects : enable - -layout(binding = 1) uniform sampler2D textureSampler; - -layout(location = 0) in vec2 inTextureCoord; - -layout(location = 0) out vec4 outColor; +out vec4 outColor; void main() { - outColor = texture(textureSampler, inTextureCoord); + outColor = vec4(1.0); } diff --git a/engine/src/flutter/impeller/impeller/compositor/shaders/box/box.vert b/engine/src/flutter/impeller/impeller/compositor/shaders/box/box.vert index ae31781c1b..76733c345d 100644 --- a/engine/src/flutter/impeller/impeller/compositor/shaders/box/box.vert +++ b/engine/src/flutter/impeller/impeller/compositor/shaders/box/box.vert @@ -1,18 +1,11 @@ -#version 450 -#extension GL_ARB_separate_shader_objects : enable - -layout(binding = 0) uniform UniformBufferObject { +uniform UniformBufferObject { mat4 model; mat4 view; mat4 projection; } ubo; -layout(location = 0) in vec3 inPosition; -layout(location = 1) in vec2 inTextureCoord; - -layout(location = 0) out vec2 outTextureCoord; +in vec3 inPosition; void main() { gl_Position = ubo.projection * ubo.view * ubo.model * vec4(inPosition, 1.0); - outTextureCoord = inTextureCoord; }