| Flutter Impeller |

Public Member Functions | List of all members
impeller::compiler::Compiler Class Reference

`#include <compiler.h>`

## Public Member Functions
| Compiler (const std::shared_ptr< const fml::Mapping > &source_mapping, const SourceOptions &options, Reflector::Options reflector_options)
| ~Compiler ()
bool | IsValid () const
std::shared_ptr< fml::Mapping > | GetSPIRVAssembly () const
std::shared_ptr< fml::Mapping > | GetSLShaderSource () const
std::string | GetErrorMessages () const
const std::vector< std::string > & | GetIncludedFileNames () const
std::unique_ptr< fml::Mapping > | CreateDepfileContents (std::initializer_list< std::string > targets) const
const Reflector * | GetReflector () const

## Detailed Description

Definition at line 25 of file compiler.h.

## Constructor & Destructor Documentation

## ◆ Compiler()

impeller::compiler::Compiler::Compiler | ( | const std::shared_ptr< const fml::Mapping > & | source_mapping,
|  | const SourceOptions & | options,
|  | Reflector::Options | reflector_options
| ) |  |

Definition at line 244 of file compiler.cc.

247 : options_(source_options) {
248 if (!source_mapping || source_mapping->GetMapping() == nullptr) {
249 COMPILER_ERROR(error_stream_)
250 << "Could not read shader source or shader source was empty.";
251 return;
252 }
253
254 if (source_options.target_platform == TargetPlatform::kUnknown) {
255 COMPILER_ERROR(error_stream_) << "Target platform not specified.";
256 return;
257 }
258
259 SPIRVCompilerOptions spirv_options;
260
261 // Make sure reflection is as effective as possible. The generated shaders
262 // will be processed later by backend specific compilers.
263 spirv_options.generate_debug_info = true;
264
265 switch (options_.source_language) {
266 case SourceLanguage::kGLSL:
267 // Expects GLSL 4.60 (Core Profile).
268 // https://www.khronos.org/registry/OpenGL/specs/gl/GLSLangSpec.4.60.pdf
269 spirv_options.source_langauge =
270 shaderc_source_language::shaderc_source_language_glsl;
271 spirv_options.source_profile = SPIRVCompilerSourceProfile{
272 shaderc_profile::shaderc_profile_core, //
273 460, //
274 };
275 break;
276 case SourceLanguage::kHLSL:
277 spirv_options.source_langauge =
278 shaderc_source_language::shaderc_source_language_hlsl;
279 break;
280 case SourceLanguage::kUnknown:
281 COMPILER_ERROR(error_stream_) << "Source language invalid.";
282 return;
283 }
284
285 switch (source_options.target_platform) {
286 case TargetPlatform::kMetalDesktop:
287 case TargetPlatform::kMetalIOS: {
288 SPIRVCompilerTargetEnv target;
289
290 if (source_options.use_half_textures) {
291 target.env = shaderc_target_env::shaderc_target_env_opengl;
292 target.version = shaderc_env_version::shaderc_env_version_opengl_4_5;
293 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_0;
294 } else {
295 target.env = shaderc_target_env::shaderc_target_env_vulkan;
296 target.version = shaderc_env_version::shaderc_env_version_vulkan_1_1;
297 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_3;
298 }
299
300 spirv_options.target = target;
301 } break;
302 case TargetPlatform::kOpenGLES:
303 case TargetPlatform::kOpenGLDesktop:
304 case TargetPlatform::kVulkan:
305 case TargetPlatform::kRuntimeStageVulkan: {
306 SPIRVCompilerTargetEnv target;
307
308 target.env = shaderc_target_env::shaderc_target_env_vulkan;
309 target.version = shaderc_env_version::shaderc_env_version_vulkan_1_1;
310 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_3;
311
312 if (source_options.target_platform ==
313 TargetPlatform::kRuntimeStageVulkan) {
314 spirv_options.macro_definitions.push_back("IMPELLER_GRAPHICS_BACKEND");
315 spirv_options.relaxed_vulkan_rules = true;
316 }
317 spirv_options.target = target;
318 } break;
319 case TargetPlatform::kRuntimeStageMetal:
320 case TargetPlatform::kRuntimeStageGLES: {
321 SPIRVCompilerTargetEnv target;
322
323 target.env = shaderc_target_env::shaderc_target_env_opengl;
324 target.version = shaderc_env_version::shaderc_env_version_opengl_4_5;
325 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_0;
326
327 spirv_options.target = target;
328 spirv_options.macro_definitions.push_back("IMPELLER_GRAPHICS_BACKEND");
329 } break;
330 case TargetPlatform::kSkSL: {
331 SPIRVCompilerTargetEnv target;
332
333 target.env = shaderc_target_env::shaderc_target_env_opengl;
334 target.version = shaderc_env_version::shaderc_env_version_opengl_4_5;
335 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_0;
336
337 // When any optimization level above 'zero' is enabled, the phi merges at
338 // loop continue blocks are rendered using syntax that is supported in
339 // GLSL, but not in SkSL.
340 // https://bugs.chromium.org/p/skia/issues/detail?id=13518.
341 spirv_options.optimization_level =
342 shaderc_optimization_level::shaderc_optimization_level_zero;
343 spirv_options.target = target;
344 spirv_options.macro_definitions.push_back("SKIA_GRAPHICS_BACKEND");
345 } break;
346 case TargetPlatform::kUnknown:
347 COMPILER_ERROR(error_stream_) << "Target platform invalid.";
348 return;
349 }
350
351 // Implicit definition that indicates that this compilation is for the device
352 // (instead of the host).
353 spirv_options.macro_definitions.push_back("IMPELLER_DEVICE");
354 for (const auto& define : source_options.defines) {
355 spirv_options.macro_definitions.push_back(define);
356 }
357
358 std::vector<std::string> included_file_names;
359 spirv_options.includer = std::make_shared<Includer>(
360 options_.working_directory, options_.include_dirs,
361 [&included_file_names](auto included_name) {
362 included_file_names.emplace_back(std::move(included_name));
363 });
364
365 // SPIRV Generation.
366 SPIRVCompiler spv_compiler(source_options, source_mapping);
367
368 spirv_assembly_ = spv_compiler.CompileToSPV(
369 error_stream_, spirv_options.BuildShadercOptions());
370
371 if (!spirv_assembly_) {
372 return;
373 } else {
374 included_file_names_ = std::move(included_file_names);
375 }
376
377 // SL Generation.
378 spirv_cross::Parser parser(
379 reinterpret_cast<const uint32_t*>(spirv_assembly_->GetMapping()),
380 spirv_assembly_->GetSize() / sizeof(uint32_t));
381 // The parser and compiler must be run separately because the parser contains
382 // meta information (like type member names) that are useful for reflection.
383 parser.parse();
384
385 const auto parsed_ir =
386 std::make_shared<spirv_cross::ParsedIR>(parser.get_parsed_ir());
387
388 auto sl_compiler = CreateCompiler(*parsed_ir, options_);
389
390 if (!sl_compiler) {
391 COMPILER_ERROR(error_stream_)
392 << "Could not create compiler for target platform.";
393 return;
394 }
395
396 // We need to invoke the compiler even if we don't use the SL mapping later
397 // for Vulkan. The reflector needs information that is only valid after a
398 // successful compilation call.
399 auto sl_compilation_result =
400 CreateMappingWithString(sl_compiler.GetCompiler()->compile());
401
402 // If the target is Vulkan, our shading language is SPIRV which we already
403 // have. We just need to strip it of debug information. If it isn't, we need
404 // to invoke the appropriate compiler to compile the SPIRV to the target SL.
405 if (source_options.target_platform == TargetPlatform::kVulkan ||
406 source_options.target_platform == TargetPlatform::kRuntimeStageVulkan) {
407 auto stripped_spirv_options = spirv_options;
408 stripped_spirv_options.generate_debug_info = false;
409 sl_mapping_ = spv_compiler.CompileToSPV(
410 error_stream_, stripped_spirv_options.BuildShadercOptions());
411 } else {
412 sl_mapping_ = sl_compilation_result;
413 }
414
415 if (!sl_mapping_) {
416 COMPILER_ERROR(error_stream_) << "Could not generate SL from SPIRV";
417 return;
418 }
419
420 reflector_ = std::make_unique<Reflector>(std::move(reflector_options), //
421 parsed_ir, //
422 GetSLShaderSource(), //
423 sl_compiler //
424 );
425
426 if (!reflector_->IsValid()) {
427 COMPILER_ERROR(error_stream_)
428 << "Could not complete reflection on generated shader.";
429 return;
430 }
431
432 is_valid_ = true;
433 }

References impeller::compiler::SPIRVCompilerOptions::BuildShadercOptions(), COMPILER_ERROR, impeller::compiler::SPIRVCompiler::CompileToSPV(), impeller::compiler::CreateCompiler(), impeller::CreateMappingWithString(), impeller::compiler::SourceOptions::defines, impeller::compiler::SPIRVCompilerTargetEnv::env, impeller::compiler::SPIRVCompilerOptions::generate_debug_info, GetSLShaderSource(), impeller::compiler::SourceOptions::include_dirs, impeller::compiler::SPIRVCompilerOptions::includer, impeller::compiler::kGLSL, impeller::compiler::kHLSL, impeller::compiler::kMetalDesktop, impeller::compiler::kMetalIOS, impeller::compiler::kOpenGLDesktop, impeller::compiler::kOpenGLES, impeller::compiler::kRuntimeStageGLES, impeller::compiler::kRuntimeStageMetal, impeller::compiler::kRuntimeStageVulkan, impeller::compiler::kSkSL, impeller::compiler::kUnknown, impeller::compiler::kVulkan, impeller::compiler::SPIRVCompilerOptions::macro_definitions, impeller::compiler::SPIRVCompilerOptions::optimization_level, impeller::compiler::SPIRVCompilerOptions::relaxed_vulkan_rules, impeller::compiler::SPIRVCompilerOptions::source_langauge, impeller::compiler::SourceOptions::source_language, impeller::compiler::SPIRVCompilerOptions::source_profile, impeller::compiler::SPIRVCompilerTargetEnv::spirv_version, impeller::compiler::SPIRVCompilerOptions::target, impeller::compiler::SourceOptions::target_platform, impeller::compiler::SourceOptions::use_half_textures, impeller::compiler::SPIRVCompilerTargetEnv::version, and impeller::compiler::SourceOptions::working_directory.

## ◆ ~Compiler()

impeller::compiler::Compiler::~Compiler | ( |  | ) |
| default

## Member Function Documentation

## ◆ CreateDepfileContents()

std::unique_ptr< fml::Mapping > impeller::compiler::Compiler::CreateDepfileContents | ( | std::initializer_list< std::string > | targets | ) | const

Definition at line 483 of file compiler.cc.

484 {
485 // https://github.com/ninja-build/ninja/blob/master/src/depfile_parser.cc#L28
486 const auto targets = JoinStrings(targets_names, " ");
487 const auto dependencies = GetDependencyNames(" ");
488
489 std::stringstream stream;
490 stream << targets << ": " << dependencies << "\n";
491
492 auto contents = std::make_shared<std::string>(stream.str());
493 return std::make_unique<fml::NonOwnedMapping>(
494 reinterpret_cast<const uint8_t*>(contents->data()), contents->size(),
495 [contents](auto, auto) {});
496 }

References impeller::compiler::JoinStrings().

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

## ◆ GetErrorMessages()

std::string impeller::compiler::Compiler::GetErrorMessages | ( |  | ) | const

Definition at line 455 of file compiler.cc.

455 {
456 return error_stream_.str();
457 }

Referenced by impeller::compiler::testing::CompilerTest::CanCompileAndReflect(), impeller::compiler::CompileSkSL(), impeller::compiler::GenerateShaderBackendFB(), impeller::compiler::Main(), and impeller::compiler::OutputIPLR().

## ◆ GetIncludedFileNames()

const std::vector< std::string > & impeller::compiler::Compiler::GetIncludedFileNames | ( |  | ) | const

Definition at line 459 of file compiler.cc.

459 {
460 return included_file_names_;
461 }

## ◆ GetReflector()

const Reflector * impeller::compiler::Compiler::GetReflector | ( |  | ) | const

Definition at line 498 of file compiler.cc.

498 {
499 return reflector_.get();
500 }

Referenced by impeller::compiler::testing::CompilerTest::CanCompileAndReflect(), impeller::compiler::CompileSkSL(), impeller::compiler::GenerateShaderBackendFB(), impeller::compiler::OutputIPLR(), and impeller::compiler::OutputReflectionData().

## ◆ GetSLShaderSource()

std::shared_ptr< fml::Mapping > impeller::compiler::Compiler::GetSLShaderSource | ( |  | ) | const

Definition at line 441 of file compiler.cc.

441 {
442 return sl_mapping_;
443 }

Referenced by impeller::compiler::testing::CompilerTest::CanCompileAndReflect(), Compiler(), and impeller::compiler::OutputSLFile().

## ◆ GetSPIRVAssembly()

std::shared_ptr< fml::Mapping > impeller::compiler::Compiler::GetSPIRVAssembly | ( |  | ) | const

Definition at line 437 of file compiler.cc.

437 {
438 return spirv_assembly_;
439 }

Referenced by impeller::compiler::testing::CompilerTest::CanCompileAndReflect(), and impeller::compiler::Main().

## ◆ IsValid()

bool impeller::compiler::Compiler::IsValid | ( |  | ) | const

Definition at line 445 of file compiler.cc.

445 {
446 return is_valid_;
447 }

Referenced by impeller::compiler::testing::CompilerTest::CanCompileAndReflect(), impeller::compiler::CompileSkSL(), impeller::compiler::GenerateShaderBackendFB(), impeller::compiler::Main(), and impeller::compiler::OutputIPLR().

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

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

impeller::compiler::SourceOptions::include_dirs
std::vector< IncludeDir > include_dirs
Definition: source_options.h:25
impeller::compiler::TargetPlatform::kMetalDesktop
@ kMetalDesktop
impeller::compiler::TargetPlatform::kMetalIOS
@ kMetalIOS
impeller::compiler::SourceLanguage::kGLSL
@ kGLSL
impeller::compiler::SourceOptions::source_language
SourceLanguage source_language
Definition: source_options.h:23
impeller::compiler::SourceLanguage::kHLSL
@ kHLSL
impeller::compiler::TargetPlatform::kVulkan
@ kVulkan
impeller::compiler::TargetPlatform::kRuntimeStageVulkan
@ kRuntimeStageVulkan
impeller::compiler::JoinStrings
static std::string JoinStrings(std::vector< std::string > items, const std::string &separator)
Definition: compiler.cc:463
impeller::compiler::SourceLanguage::kUnknown
@ kUnknown
impeller::compiler::SourceOptions::working_directory
std::shared_ptr< fml::UniqueFD > working_directory
Definition: source_options.h:24
impeller::CreateMappingWithString
std::shared_ptr< fml::Mapping > CreateMappingWithString(std::string string)
Definition: allocation.cc:111
impeller::compiler::TargetPlatform::kOpenGLDesktop
@ kOpenGLDesktop
impeller::compiler::TargetPlatform::kUnknown
@ kUnknown
impeller::compiler::TargetPlatform::kOpenGLES
@ kOpenGLES
impeller::compiler::Compiler::GetSLShaderSource
std::shared_ptr< fml::Mapping > GetSLShaderSource() const
Definition: compiler.cc:441
impeller::compiler::CreateCompiler
static CompilerBackend CreateCompiler(const spirv_cross::ParsedIR &ir, const SourceOptions &source_options)
Definition: compiler.cc:210
impeller::compiler::TargetPlatform::kRuntimeStageMetal
@ kRuntimeStageMetal
COMPILER_ERROR
#define COMPILER_ERROR(stream)
Definition: logger.h:39
impeller::compiler::TargetPlatform::kSkSL
@ kSkSL
impeller::compiler::TargetPlatform::kRuntimeStageGLES
@ kRuntimeStageGLES