Flutter Engine
The Flutter Engine
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::MappingCompileToSPV (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 15 of file spirv_compiler.cc.

17 : options_(options), sources_(std::move(sources)) {}
const char * options

◆ ~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 "
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) {
56 << " to SPIRV failed; "
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}
GAsyncResult * result
#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:100
shaderc_shader_kind ToShaderCShaderKind(SourceType type)
Definition: types.cc:184
std::string ShaderCErrorToString(shaderc_compilation_status status)
Definition: types.cc:159

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