| 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 55 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 15 of file spirv_compiler.cc.

17 : 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 21 of file spirv_compiler.cc.

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

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(), and impeller::compiler::SourceOptions::type.

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

impeller::compiler::SourceOptions::type
SourceType type
Definition: source_options.h:21
COMPILER_ERROR_NO_PREFIX
#define COMPILER_ERROR_NO_PREFIX(stream)
Definition: logger.h:42
impeller::compiler::SourceLanguageToString
std::string SourceLanguageToString(SourceLanguage source_language)
Definition: types.cc:100
impeller::compiler::SourceOptions::source_language
SourceLanguage source_language
Definition: source_options.h:23
impeller::compiler::SourceOptions::entry_point_name
std::string entry_point_name
Definition: source_options.h:27
impeller::compiler::SourceOptions::file_name
std::string file_name
Definition: source_options.h:26
impeller::compiler::ToShaderCShaderKind
shaderc_shader_kind ToShaderCShaderKind(SourceType type)
Definition: types.cc:184
COMPILER_ERROR
#define COMPILER_ERROR(stream)
Definition: logger.h:39
impeller::compiler::ShaderCErrorToString
std::string ShaderCErrorToString(shaderc_compilation_status status)
Definition: types.cc:159