Wire up one-shot conversion of GLSL to MSL.
This commit is contained in:
@@ -2,18 +2,45 @@
|
||||
# Use of this source code is governed by a BSD-style license that can be
|
||||
# found in the LICENSE file.
|
||||
|
||||
executable("compiler") {
|
||||
output_name = "impellerc"
|
||||
import("//flutter/testing/testing.gni")
|
||||
|
||||
source_set("compiler_lib") {
|
||||
sources = [
|
||||
"compiler.cc",
|
||||
"compiler.h",
|
||||
"impellerc_main.cc",
|
||||
"logger.h",
|
||||
]
|
||||
|
||||
deps = [
|
||||
public_deps = [
|
||||
"//flutter/fml",
|
||||
"//flutter/runtime:libdart",
|
||||
"//third_party/shaderc_flutter",
|
||||
"//third_party/spirv_cross_flutter",
|
||||
]
|
||||
}
|
||||
|
||||
executable("compiler") {
|
||||
output_name = "impellerc"
|
||||
|
||||
sources = [ "impellerc_main.cc" ]
|
||||
|
||||
deps = [ ":compiler_lib" ]
|
||||
}
|
||||
|
||||
test_fixtures("compiler_test_fixtures") {
|
||||
fixtures = [ "fixtures/sample.frag" ]
|
||||
}
|
||||
|
||||
executable("compiler_unittests") {
|
||||
testonly = true
|
||||
|
||||
output_name = "impellerc_unittests"
|
||||
|
||||
sources = [ "compiler_unittests.cc" ]
|
||||
|
||||
deps = [
|
||||
":compiler_lib",
|
||||
":compiler_test_fixtures",
|
||||
"//flutter/testing",
|
||||
]
|
||||
}
|
||||
|
||||
@@ -4,11 +4,18 @@
|
||||
|
||||
#include "flutter/impeller/compiler/compiler.h"
|
||||
|
||||
#include "flutter/impeller/compiler/logger.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace impeller {
|
||||
namespace compiler {
|
||||
|
||||
#define COMPILER_ERROR \
|
||||
::impeller::compiler::AutoLogger(error_stream_) << GetSourcePrefix()
|
||||
|
||||
#define COMPILER_ERROR_NO_PREFIX ::impeller::compiler::AutoLogger(error_stream_)
|
||||
|
||||
class Includer final : public shaderc::CompileOptions::IncluderInterface {
|
||||
public:
|
||||
Includer() = default;
|
||||
@@ -21,29 +28,73 @@ class Includer final : public shaderc::CompileOptions::IncluderInterface {
|
||||
shaderc_include_type type,
|
||||
const char* requesting_source,
|
||||
size_t include_depth) override {
|
||||
FML_CHECK(false);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// |shaderc::CompileOptions::IncluderInterface|
|
||||
void ReleaseInclude(shaderc_include_result* data) override {}
|
||||
void ReleaseInclude(shaderc_include_result* data) override {
|
||||
FML_CHECK(false);
|
||||
}
|
||||
|
||||
private:
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(Includer);
|
||||
};
|
||||
|
||||
static std::string ShaderCErrorToString(shaderc_compilation_status status) {
|
||||
switch (status) {
|
||||
case shaderc_compilation_status::shaderc_compilation_status_success:
|
||||
return "Success";
|
||||
case shaderc_compilation_status::shaderc_compilation_status_invalid_stage:
|
||||
return "Invalid Shader Stage Specified";
|
||||
case shaderc_compilation_status::
|
||||
shaderc_compilation_status_compilation_error:
|
||||
return "Compilation Error";
|
||||
case shaderc_compilation_status::shaderc_compilation_status_internal_error:
|
||||
return "Internal Error";
|
||||
case shaderc_compilation_status::
|
||||
shaderc_compilation_status_null_result_object:
|
||||
return "Internal error. Null Result Object";
|
||||
case shaderc_compilation_status::
|
||||
shaderc_compilation_status_invalid_assembly:
|
||||
return "Invalid Assembly";
|
||||
case shaderc_compilation_status::
|
||||
shaderc_compilation_status_validation_error:
|
||||
return "Validation Error";
|
||||
case shaderc_compilation_status::
|
||||
shaderc_compilation_status_transformation_error:
|
||||
return "Transformation Error";
|
||||
case shaderc_compilation_status::
|
||||
shaderc_compilation_status_configuration_error:
|
||||
return "Configuration Error";
|
||||
}
|
||||
return "Unknown Internal Error";
|
||||
}
|
||||
|
||||
static shaderc_shader_kind ToShaderCShaderKind(Compiler::SourceType type) {
|
||||
switch (type) {
|
||||
case Compiler::SourceType::kVertexShader:
|
||||
return shaderc_shader_kind::shaderc_vertex_shader;
|
||||
case Compiler::SourceType::kFragmentShader:
|
||||
return shaderc_shader_kind::shaderc_fragment_shader;
|
||||
case Compiler::SourceType::kUnknown:
|
||||
break;
|
||||
}
|
||||
return shaderc_shader_kind::shaderc_vertex_shader;
|
||||
return shaderc_shader_kind::shaderc_glsl_infer_from_source;
|
||||
}
|
||||
|
||||
Compiler::Compiler(const fml::Mapping& source_mapping,
|
||||
SourceOptions source_options) {
|
||||
SourceOptions source_options)
|
||||
: options_(source_options) {
|
||||
if (source_mapping.GetMapping() == nullptr) {
|
||||
COMPILER_ERROR << "Could not read shader source.";
|
||||
return;
|
||||
}
|
||||
|
||||
auto shader_kind = ToShaderCShaderKind(source_options.type);
|
||||
|
||||
if (shader_kind == shaderc_shader_kind::shaderc_glsl_infer_from_source) {
|
||||
COMPILER_ERROR << "Could not figure out shader stage.";
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -55,30 +106,118 @@ Compiler::Compiler(const fml::Mapping& source_mapping,
|
||||
shaderc_source_language::shaderc_source_language_glsl);
|
||||
options.SetTargetEnvironment(
|
||||
shaderc_target_env::shaderc_target_env_vulkan,
|
||||
shaderc_env_version::shaderc_env_version_opengl_4_5);
|
||||
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>());
|
||||
|
||||
shaderc::Compiler compiler;
|
||||
if (!compiler.IsValid()) {
|
||||
shaderc::Compiler spv_compiler;
|
||||
if (!spv_compiler.IsValid()) {
|
||||
COMPILER_ERROR << "Could not initialize the GLSL to SPIRV compiler.";
|
||||
return;
|
||||
}
|
||||
|
||||
spv_result_ = std::make_shared<shaderc::SpvCompilationResult>(
|
||||
spv_compiler.CompileGlslToSpv(
|
||||
reinterpret_cast<const char*>(
|
||||
source_mapping.GetMapping()), // source_text
|
||||
source_mapping.GetSize(), // source_text_size
|
||||
shader_kind, // shader_kind
|
||||
source_options.file_name.c_str(), // input_file_name
|
||||
source_options.entry_point_name.c_str(), // entry_point_name
|
||||
options // options
|
||||
));
|
||||
|
||||
if (spv_result_->GetCompilationStatus() !=
|
||||
shaderc_compilation_status::shaderc_compilation_status_success) {
|
||||
COMPILER_ERROR << "GLSL to SPIRV failed; "
|
||||
<< ShaderCErrorToString(spv_result_->GetCompilationStatus())
|
||||
<< ". " << spv_result_->GetNumErrors() << " error(s) and "
|
||||
<< spv_result_->GetNumWarnings() << " warning(s).";
|
||||
if (spv_result_->GetNumErrors() > 0 || spv_result_->GetNumWarnings() > 0) {
|
||||
COMPILER_ERROR_NO_PREFIX << spv_result_->GetErrorMessage();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
spirv_cross::CompilerMSL msl_compiler(
|
||||
spv_result_->cbegin(), spv_result_->cend() - spv_result_->cbegin());
|
||||
msl_string_ = std::make_shared<std::string>(msl_compiler.compile());
|
||||
|
||||
if (!msl_string_) {
|
||||
COMPILER_ERROR << "Could not generate MSL from SPIRV";
|
||||
return;
|
||||
}
|
||||
|
||||
result_ = compiler.CompileGlslToSpv(
|
||||
reinterpret_cast<const char*>(
|
||||
source_mapping.GetMapping()), // source_text
|
||||
source_mapping.GetSize(), // source_text_size
|
||||
ToShaderCShaderKind(source_options.type), // shader_kind
|
||||
source_options.file_name.c_str(), // input_file_name
|
||||
source_options.entry_point_name.c_str(), // entry_point_name
|
||||
options // options
|
||||
);
|
||||
is_valid_ = true;
|
||||
}
|
||||
|
||||
Compiler::~Compiler() = default;
|
||||
|
||||
std::unique_ptr<fml::Mapping> Compiler::GetSPIRVAssembly() const {
|
||||
if (!spv_result_) {
|
||||
return nullptr;
|
||||
}
|
||||
const auto data_length =
|
||||
(spv_result_->cend() - spv_result_->cbegin()) *
|
||||
sizeof(decltype(spv_result_)::element_type::element_type);
|
||||
|
||||
return std::make_unique<fml::NonOwnedMapping>(
|
||||
reinterpret_cast<const uint8_t*>(spv_result_->cbegin()), data_length,
|
||||
[result = spv_result_](auto, auto) mutable { result.reset(); });
|
||||
}
|
||||
|
||||
std::unique_ptr<fml::Mapping> Compiler::GetMSLShaderSource() const {
|
||||
if (!msl_string_) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return std::make_unique<fml::NonOwnedMapping>(
|
||||
reinterpret_cast<const uint8_t*>(msl_string_->c_str()),
|
||||
msl_string_->length(),
|
||||
[string = msl_string_](auto, auto) mutable { string.reset(); });
|
||||
}
|
||||
|
||||
bool Compiler::IsValid() const {
|
||||
return is_valid_;
|
||||
}
|
||||
|
||||
static bool StringEndWith(const std::string& string,
|
||||
const std::string& suffix) {
|
||||
if (suffix.size() > string.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (suffix.empty() || suffix.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return string.rfind(suffix) == (string.size() - suffix.size());
|
||||
}
|
||||
|
||||
Compiler::SourceType Compiler::SourceTypeFromFileName(
|
||||
const std::string& file_name) {
|
||||
if (StringEndWith(file_name, ".vert")) {
|
||||
return Compiler::SourceType::kVertexShader;
|
||||
}
|
||||
|
||||
if (StringEndWith(file_name, ".frag")) {
|
||||
return Compiler::SourceType::kFragmentShader;
|
||||
}
|
||||
|
||||
return Compiler::SourceType::kUnknown;
|
||||
}
|
||||
|
||||
std::string Compiler::GetSourcePrefix() const {
|
||||
std::stringstream stream;
|
||||
stream << options_.file_name << ": ";
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
std::string Compiler::GetErrorMessages() const {
|
||||
return error_stream_.str();
|
||||
}
|
||||
|
||||
} // namespace compiler
|
||||
} // namespace impeller
|
||||
|
||||
@@ -4,11 +4,13 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "flutter/fml/mapping.h"
|
||||
#include "shaderc/shaderc.hpp"
|
||||
#include "third_party/spirv_cross/spirv_msl.hpp"
|
||||
|
||||
namespace impeller {
|
||||
namespace compiler {
|
||||
@@ -16,6 +18,7 @@ namespace compiler {
|
||||
class Compiler {
|
||||
public:
|
||||
enum class SourceType {
|
||||
kUnknown,
|
||||
kVertexShader,
|
||||
kFragmentShader,
|
||||
};
|
||||
@@ -23,9 +26,15 @@ class Compiler {
|
||||
static SourceType SourceTypeFromFileName(const std::string& file_name);
|
||||
|
||||
struct SourceOptions {
|
||||
SourceType type = SourceType::kVertexShader;
|
||||
SourceType type = SourceType::kUnknown;
|
||||
std::shared_ptr<fml::UniqueFD> working_directory;
|
||||
std::string file_name = "main.glsl";
|
||||
std::string entry_point_name = "main";
|
||||
|
||||
SourceOptions() = default;
|
||||
|
||||
SourceOptions(const std::string& file_name)
|
||||
: type(SourceTypeFromFileName(file_name)), file_name(file_name) {}
|
||||
};
|
||||
|
||||
Compiler(const fml::Mapping& source_mapping, SourceOptions options);
|
||||
@@ -34,10 +43,21 @@ class Compiler {
|
||||
|
||||
bool IsValid() const;
|
||||
|
||||
std::unique_ptr<fml::Mapping> GetSPIRVAssembly() const;
|
||||
|
||||
std::unique_ptr<fml::Mapping> GetMSLShaderSource() const;
|
||||
|
||||
std::string GetErrorMessages() const;
|
||||
|
||||
private:
|
||||
shaderc::SpvCompilationResult result_;
|
||||
SourceOptions options_;
|
||||
std::shared_ptr<shaderc::SpvCompilationResult> spv_result_;
|
||||
std::shared_ptr<std::string> msl_string_;
|
||||
std::stringstream error_stream_;
|
||||
bool is_valid_ = false;
|
||||
|
||||
std::string GetSourcePrefix() const;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(Compiler);
|
||||
};
|
||||
|
||||
|
||||
34
engine/src/flutter/impeller/compiler/compiler_unittests.cc
Normal file
34
engine/src/flutter/impeller/compiler/compiler_unittests.cc
Normal file
@@ -0,0 +1,34 @@
|
||||
// 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/compiler.h"
|
||||
#include "flutter/testing/testing.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace impeller {
|
||||
namespace compiler {
|
||||
namespace testing {
|
||||
|
||||
TEST(CompilerTest, ShaderKindMatchingIsSuccessful) {
|
||||
ASSERT_EQ(Compiler::SourceTypeFromFileName("hello.vert"),
|
||||
Compiler::SourceType::kVertexShader);
|
||||
ASSERT_EQ(Compiler::SourceTypeFromFileName("hello.frag"),
|
||||
Compiler::SourceType::kFragmentShader);
|
||||
ASSERT_EQ(Compiler::SourceTypeFromFileName("hello.msl"),
|
||||
Compiler::SourceType::kUnknown);
|
||||
ASSERT_EQ(Compiler::SourceTypeFromFileName("hello.glsl"),
|
||||
Compiler::SourceType::kUnknown);
|
||||
}
|
||||
|
||||
TEST(CompilerTest, CanCompileSample) {
|
||||
auto fixture = flutter::testing::OpenFixtureAsMapping("sample.frag");
|
||||
ASSERT_NE(fixture->GetMapping(), nullptr);
|
||||
Compiler::SourceOptions options("sample.frag");
|
||||
Compiler compiler(*fixture.get(), options);
|
||||
ASSERT_TRUE(compiler.IsValid());
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace compiler
|
||||
} // namespace impeller
|
||||
40
engine/src/flutter/impeller/compiler/logger.h
Normal file
40
engine/src/flutter/impeller/compiler/logger.h
Normal file
@@ -0,0 +1,40 @@
|
||||
// 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 <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "flutter/fml/logging.h"
|
||||
#include "flutter/fml/macros.h"
|
||||
|
||||
namespace impeller {
|
||||
namespace compiler {
|
||||
|
||||
class AutoLogger {
|
||||
public:
|
||||
AutoLogger(std::stringstream& logger) : logger_(logger) {}
|
||||
|
||||
~AutoLogger() {
|
||||
logger_ << std::endl;
|
||||
logger_.flush();
|
||||
|
||||
FML_DLOG(INFO) << logger_.str();
|
||||
}
|
||||
|
||||
template <class T>
|
||||
AutoLogger& operator<<(const T& object) {
|
||||
logger_ << object;
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
std::stringstream& logger_;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(AutoLogger);
|
||||
};
|
||||
|
||||
} // namespace compiler
|
||||
} // namespace impeller
|
||||
Reference in New Issue
Block a user