Flutter Impeller
impeller::compiler::SPIRVCompiler Class Reference

#include <spirv_compiler.h>

Public Member Functions

 SPIRVCompiler (const SourceOptions &options, std::shared_ptr< const fml::Mapping > sources)
 
 ~SPIRVCompiler ()
 
std::shared_ptr< fml::Mapping > CompileToSPV (std::stringstream &error_stream, const shaderc::CompileOptions &spirv_options) const
 

Detailed Description

Definition at line 57 of file spirv_compiler.h.

Constructor & Destructor Documentation

◆ SPIRVCompiler()

impeller::compiler::SPIRVCompiler::SPIRVCompiler ( const SourceOptions options,
std::shared_ptr< const fml::Mapping >  sources 
)

Definition at line 16 of file spirv_compiler.cc.

18  : options_(options), sources_(std::move(sources)) {}

◆ ~SPIRVCompiler()

impeller::compiler::SPIRVCompiler::~SPIRVCompiler ( )
default

Member Function Documentation

◆ CompileToSPV()

std::shared_ptr< fml::Mapping > impeller::compiler::SPIRVCompiler::CompileToSPV ( std::stringstream &  error_stream,
const shaderc::CompileOptions &  spirv_options 
) const

Definition at line 22 of file spirv_compiler.cc.

24  {
25  if (!sources_ || sources_->GetMapping() == nullptr) {
26  COMPILER_ERROR(stream) << "Invalid sources for SPIRV Compiler.";
27  return nullptr;
28  }
29 
30  shaderc::Compiler spv_compiler;
31  if (!spv_compiler.IsValid()) {
32  COMPILER_ERROR(stream) << "Could not initialize the "
34  << " to SPIRV compiler.";
35  return nullptr;
36  }
37 
38  const auto shader_kind = ToShaderCShaderKind(options_.type);
39 
40  if (shader_kind == shaderc_shader_kind::shaderc_glsl_infer_from_source) {
41  COMPILER_ERROR(stream) << "Could not figure out shader stage.";
42  return nullptr;
43  }
44 
45  auto result = std::make_shared<shaderc::SpvCompilationResult>(
46  spv_compiler.CompileGlslToSpv(
47  reinterpret_cast<const char*>(sources_->GetMapping()), // source_text
48  sources_->GetSize(), // source_text_size
49  shader_kind, // shader_kind
50  Utf8FromPath(options_.file_name).c_str(), // input_file_name
51  options_.entry_point_name.c_str(), // entry_point_name
52  spirv_options // options
53  ));
54  if (result->GetCompilationStatus() !=
55  shaderc_compilation_status::shaderc_compilation_status_success) {
57  << " to SPIRV failed; "
59  result->GetCompilationStatus())
60  << ". " << result->GetNumErrors() << " error(s) and "
61  << result->GetNumWarnings() << " warning(s).";
62  // It should normally be enough to check that there are errors or warnings,
63  // but some cases result in no errors or warnings and still have an error
64  // message. If there's a message we should print it.
65  if (result->GetNumErrors() > 0 || result->GetNumWarnings() > 0 ||
66  !result->GetErrorMessage().empty()) {
67  COMPILER_ERROR_NO_PREFIX(stream) << result->GetErrorMessage();
68  }
69  return nullptr;
70  }
71 
72  if (!result) {
73  COMPILER_ERROR(stream) << "Could not fetch SPIRV from compile job.";
74  return nullptr;
75  }
76 
77  const auto data_length = (result->cend() - result->cbegin()) *
78  sizeof(decltype(result)::element_type::element_type);
79 
80  return std::make_unique<fml::NonOwnedMapping>(
81  reinterpret_cast<const uint8_t*>(result->cbegin()), //
82  data_length, //
83  [result](auto, auto) {} //
84  );
85 }
#define COMPILER_ERROR_NO_PREFIX(stream)
Definition: logger.h:42
#define COMPILER_ERROR(stream)
Definition: logger.h:39
std::string SourceLanguageToString(SourceLanguage source_language)
Definition: types.cc:90
shaderc_shader_kind ToShaderCShaderKind(SourceType type)
Definition: types.cc:174
std::string ShaderCErrorToString(shaderc_compilation_status status)
Definition: types.cc:149
std::string Utf8FromPath(const std::filesystem::path &path)
Converts a native format path to a utf8 string.
Definition: utilities.cc:30
std::filesystem::path file_name

References COMPILER_ERROR, COMPILER_ERROR_NO_PREFIX, impeller::compiler::SourceOptions::entry_point_name, impeller::compiler::SourceOptions::file_name, impeller::compiler::ShaderCErrorToString(), impeller::compiler::SourceOptions::source_language, impeller::compiler::SourceLanguageToString(), impeller::compiler::ToShaderCShaderKind(), impeller::compiler::SourceOptions::type, and impeller::compiler::Utf8FromPath().

Referenced by impeller::compiler::Compiler::Compiler().


The documentation for this class was generated from the following files: