| 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 24 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 280 of file compiler.cc.

283 : options_(source_options) {
284 if (!source_mapping || source_mapping->GetMapping() == nullptr) {
285 COMPILER_ERROR(error_stream_)
286 << "Could not read shader source or shader source was empty.";
287 return;
288 }
289
290 if (source_options.target_platform == TargetPlatform::kUnknown) {
291 COMPILER_ERROR(error_stream_) << "Target platform not specified.";
292 return;
293 }
294
295 SPIRVCompilerOptions spirv_options;
296
297 // Make sure reflection is as effective as possible. The generated shaders
298 // will be processed later by backend specific compilers.
299 spirv_options.generate_debug_info = true;
300
301 switch (options_.source_language) {
302 case SourceLanguage::kGLSL:
303 // Expects GLSL 4.60 (Core Profile).
304 // https://www.khronos.org/registry/OpenGL/specs/gl/GLSLangSpec.4.60.pdf
305 spirv_options.source_langauge =
306 shaderc_source_language::shaderc_source_language_glsl;
307 spirv_options.source_profile = SPIRVCompilerSourceProfile{
308 shaderc_profile::shaderc_profile_core, //
309 460, //
310 };
311 break;
312 case SourceLanguage::kHLSL:
313 spirv_options.source_langauge =
314 shaderc_source_language::shaderc_source_language_hlsl;
315 break;
316 case SourceLanguage::kUnknown:
317 COMPILER_ERROR(error_stream_) << "Source language invalid.";
318 return;
319 }
320
321 switch (source_options.target_platform) {
322 case TargetPlatform::kMetalDesktop:
323 case TargetPlatform::kMetalIOS: {
324 SPIRVCompilerTargetEnv target;
325
326 if (source_options.use_half_textures) {
327 target.env = shaderc_target_env::shaderc_target_env_opengl;
328 target.version = shaderc_env_version::shaderc_env_version_opengl_4_5;
329 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_0;
330 } else {
331 target.env = shaderc_target_env::shaderc_target_env_vulkan;
332 target.version = shaderc_env_version::shaderc_env_version_vulkan_1_1;
333 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_3;
334 }
335
336 spirv_options.target = target;
337 } break;
338 case TargetPlatform::kOpenGLES:
339 case TargetPlatform::kOpenGLDesktop:
340 case TargetPlatform::kVulkan:
341 case TargetPlatform::kRuntimeStageVulkan: {
342 SPIRVCompilerTargetEnv target;
343
344 target.env = shaderc_target_env::shaderc_target_env_vulkan;
345 target.version = shaderc_env_version::shaderc_env_version_vulkan_1_1;
346 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_3;
347
348 if (source_options.target_platform ==
349 TargetPlatform::kRuntimeStageVulkan) {
350 spirv_options.macro_definitions.push_back("IMPELLER_GRAPHICS_BACKEND");
351 spirv_options.relaxed_vulkan_rules = true;
352 }
353 spirv_options.target = target;
354 } break;
355 case TargetPlatform::kRuntimeStageMetal:
356 case TargetPlatform::kRuntimeStageGLES:
357 case TargetPlatform::kRuntimeStageGLES3: {
358 SPIRVCompilerTargetEnv target;
359
360 target.env = shaderc_target_env::shaderc_target_env_opengl;
361 target.version = shaderc_env_version::shaderc_env_version_opengl_4_5;
362 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_0;
363
364 spirv_options.target = target;
365 spirv_options.macro_definitions.push_back("IMPELLER_GRAPHICS_BACKEND");
366 } break;
367 case TargetPlatform::kSkSL: {
368 SPIRVCompilerTargetEnv target;
369
370 target.env = shaderc_target_env::shaderc_target_env_opengl;
371 target.version = shaderc_env_version::shaderc_env_version_opengl_4_5;
372 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_0;
373
374 // When any optimization level above 'zero' is enabled, the phi merges at
375 // loop continue blocks are rendered using syntax that is supported in
376 // GLSL, but not in SkSL.
377 // https://bugs.chromium.org/p/skia/issues/detail?id=13518.
378 spirv_options.optimization_level =
379 shaderc_optimization_level::shaderc_optimization_level_zero;
380 spirv_options.target = target;
381 spirv_options.macro_definitions.push_back("SKIA_GRAPHICS_BACKEND");
382 } break;
383 case TargetPlatform::kUnknown:
384 COMPILER_ERROR(error_stream_) << "Target platform invalid.";
385 return;
386 }
387
388 // Implicit definition that indicates that this compilation is for the device
389 // (instead of the host).
390 spirv_options.macro_definitions.push_back("IMPELLER_DEVICE");
391 for (const auto& define : source_options.defines) {
392 spirv_options.macro_definitions.push_back(define);
393 }
394
395 std::vector<std::string> included_file_names;
396 spirv_options.includer = std::make_shared<Includer>(
397 options_.working_directory, options_.include_dirs,
398 [&included_file_names](auto included_name) {
399 included_file_names.emplace_back(std::move(included_name));
400 });
401
402 // SPIRV Generation.
403 SPIRVCompiler spv_compiler(source_options, source_mapping);
404
405 spirv_assembly_ = spv_compiler.CompileToSPV(
406 error_stream_, spirv_options.BuildShadercOptions());
407
408 if (!spirv_assembly_) {
409 return;
410 } else {
411 included_file_names_ = std::move(included_file_names);
412 }
413
414 // SL Generation.
415 spirv_cross::Parser parser(
416 reinterpret_cast<const uint32_t*>(spirv_assembly_->GetMapping()),
417 spirv_assembly_->GetSize() / sizeof(uint32_t));
418 // The parser and compiler must be run separately because the parser contains
419 // meta information (like type member names) that are useful for reflection.
420 parser.parse();
421
422 const auto parsed_ir =
423 std::make_shared<spirv_cross::ParsedIR>(parser.get_parsed_ir());
424
425 auto sl_compiler = CreateCompiler(*parsed_ir, options_);
426
427 if (!sl_compiler) {
428 COMPILER_ERROR(error_stream_)
429 << "Could not create compiler for target platform.";
430 return;
431 }
432
433 uint32_t ubo_size = CalculateUBOSize(sl_compiler.GetCompiler());
434 if (ubo_size > kMaxUniformBufferSize) {
435 COMPILER_ERROR(error_stream_) << "Uniform buffer size exceeds max ("
436 << kMaxUniformBufferSize << "): " << ubo_size;
437 return;
438 }
439
440 // We need to invoke the compiler even if we don't use the SL mapping later
441 // for Vulkan. The reflector needs information that is only valid after a
442 // successful compilation call.
443 auto sl_compilation_result =
444 CreateMappingWithString(sl_compiler.GetCompiler()->compile());
445
446 // If the target is Vulkan, our shading language is SPIRV which we already
447 // have. We just need to strip it of debug information. If it isn't, we need
448 // to invoke the appropriate compiler to compile the SPIRV to the target SL.
449 if (source_options.target_platform == TargetPlatform::kVulkan ||
450 source_options.target_platform == TargetPlatform::kRuntimeStageVulkan) {
451 auto stripped_spirv_options = spirv_options;
452 stripped_spirv_options.generate_debug_info = false;
453 sl_mapping_ = spv_compiler.CompileToSPV(
454 error_stream_, stripped_spirv_options.BuildShadercOptions());
455 } else {
456 sl_mapping_ = sl_compilation_result;
457 }
458
459 if (!sl_mapping_) {
460 COMPILER_ERROR(error_stream_) << "Could not generate SL from SPIRV";
461 return;
462 }
463
464 reflector_ = std::make_unique<Reflector>(std::move(reflector_options), //
465 parsed_ir, //
466 GetSLShaderSource(), //
467 sl_compiler //
468 );
469
470 if (!reflector_->IsValid()) {
471 COMPILER_ERROR(error_stream_)
472 << "Could not complete reflection on generated shader.";
473 return;
474 }
475
476 is_valid_ = true;
477 }
impeller::compiler::Compiler::GetSLShaderSource
std::shared_ptr< fml::Mapping > GetSLShaderSource() const
Definition: compiler.cc:485
COMPILER_ERROR
#define COMPILER_ERROR(stream)
Definition: logger.h:39
impeller::compiler::kMaxUniformBufferSize
static const uint32_t kMaxUniformBufferSize
Definition: compiler.cc:38
impeller::compiler::SourceLanguage::kUnknown
@ kUnknown
impeller::compiler::SourceLanguage::kGLSL
@ kGLSL
impeller::compiler::SourceLanguage::kHLSL
@ kHLSL
impeller::compiler::CreateCompiler
static CompilerBackend CreateCompiler(const spirv_cross::ParsedIR &ir, const SourceOptions &source_options)
Definition: compiler.cc:230
impeller::compiler::TargetPlatform::kRuntimeStageMetal
@ kRuntimeStageMetal
impeller::compiler::TargetPlatform::kOpenGLES
@ kOpenGLES
impeller::compiler::TargetPlatform::kUnknown
@ kUnknown
impeller::compiler::TargetPlatform::kRuntimeStageVulkan
@ kRuntimeStageVulkan
impeller::compiler::TargetPlatform::kRuntimeStageGLES3
@ kRuntimeStageGLES3
impeller::compiler::TargetPlatform::kVulkan
@ kVulkan
impeller::compiler::TargetPlatform::kMetalDesktop
@ kMetalDesktop
impeller::compiler::TargetPlatform::kMetalIOS
@ kMetalIOS
impeller::compiler::TargetPlatform::kSkSL
@ kSkSL
impeller::compiler::TargetPlatform::kOpenGLDesktop
@ kOpenGLDesktop
impeller::compiler::TargetPlatform::kRuntimeStageGLES
@ kRuntimeStageGLES
impeller::CreateMappingWithString
std::shared_ptr< fml::Mapping > CreateMappingWithString(std::string string)
Creates a mapping with string data.
Definition: allocation.cc:111
impeller::compiler::SourceOptions::source_language
SourceLanguage source_language
Definition: source_options.h:23
impeller::compiler::SourceOptions::include_dirs
std::vector< IncludeDir > include_dirs
Definition: source_options.h:25
impeller::compiler::SourceOptions::working_directory
std::shared_ptr< fml::UniqueFD > working_directory
Definition: source_options.h:24

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::kMaxUniformBufferSize, impeller::compiler::kMetalDesktop, impeller::compiler::kMetalIOS, impeller::compiler::kOpenGLDesktop, impeller::compiler::kOpenGLES, impeller::compiler::kRuntimeStageGLES, impeller::compiler::kRuntimeStageGLES3, 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 527 of file compiler.cc.

528 {
529 // https://github.com/ninja-build/ninja/blob/master/src/depfile_parser.cc#L28
530 const auto targets = JoinStrings(targets_names, " ");
531 const auto dependencies = GetDependencyNames(" ");
532
533 std::stringstream stream;
534 stream << targets << ": " << dependencies << "\n";
535
536 auto contents = std::make_shared<std::string>(stream.str());
537 return std::make_unique<fml::NonOwnedMapping>(
538 reinterpret_cast<const uint8_t*>(contents->data()), contents->size(),
539 [contents](auto, auto) {});
540 }
impeller::compiler::JoinStrings
static std::string JoinStrings(std::vector< std::string > items, const std::string &separator)
Definition: compiler.cc:507

References impeller::compiler::JoinStrings().

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

## ◆ GetErrorMessages()

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

Definition at line 499 of file compiler.cc.

499 {
500 return error_stream_.str();
501 }

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 503 of file compiler.cc.

503 {
504 return included_file_names_;
505 }

## ◆ GetReflector()

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

Definition at line 542 of file compiler.cc.

542 {
543 return reflector_.get();
544 }

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 485 of file compiler.cc.

485 {
486 return sl_mapping_;
487 }

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 481 of file compiler.cc.

481 {
482 return spirv_assembly_;
483 }

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

## ◆ IsValid()

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

Definition at line 489 of file compiler.cc.

489 {
490 return is_valid_;
491 }

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