| Flutter Impeller |

Public Member Functions | List of all members
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 "
33 << SourceLanguageToString(options_.source_language)
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) {
56 COMPILER_ERROR(stream) << SourceLanguageToString(options_.source_language)
57 << " to SPIRV failed; "
58 << ShaderCErrorToString(
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 }
COMPILER_ERROR_NO_PREFIX
#define COMPILER_ERROR_NO_PREFIX(stream)
Definition: logger.h:42
COMPILER_ERROR
#define COMPILER_ERROR(stream)
Definition: logger.h:39
impeller::compiler::SourceLanguageToString
std::string SourceLanguageToString(SourceLanguage source_language)
Definition: types.cc:90
impeller::compiler::ToShaderCShaderKind
shaderc_shader_kind ToShaderCShaderKind(SourceType type)
Definition: types.cc:174
impeller::compiler::ShaderCErrorToString
std::string ShaderCErrorToString(shaderc_compilation_status status)
Definition: types.cc:149
impeller::compiler::Utf8FromPath
std::string Utf8FromPath(const std::filesystem::path &path)
Converts a native format path to a utf8 string.
Definition: utilities.cc:30
impeller::compiler::SourceOptions::source_language
SourceLanguage source_language
Definition: source_options.h:23
impeller::compiler::SourceOptions::file_name
std::filesystem::path file_name
Definition: source_options.h:26
impeller::compiler::SourceOptions::entry_point_name
std::string entry_point_name
Definition: source_options.h:27
impeller::compiler::SourceOptions::type
SourceType type
Definition: source_options.h:21

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:

- impeller/compiler/spirv_compiler.h
- impeller/compiler/spirv_compiler.cc