Flutter Impeller
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 ReflectorGetReflector () 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) {
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;
313  spirv_options.source_langauge =
314  shaderc_source_language::shaderc_source_language_hlsl;
315  break;
317  COMPILER_ERROR(error_stream_) << "Source language invalid.";
318  return;
319  }
320 
321  switch (source_options.target_platform) {
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;
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 ==
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;
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  if (source_options.target_platform == TargetPlatform::kRuntimeStageGLES ||
367  source_options.target_platform ==
369  spirv_options.macro_definitions.push_back("IMPELLER_TARGET_OPENGLES");
370  }
371  } break;
372  case TargetPlatform::kSkSL: {
373  SPIRVCompilerTargetEnv target;
374 
375  target.env = shaderc_target_env::shaderc_target_env_opengl;
376  target.version = shaderc_env_version::shaderc_env_version_opengl_4_5;
377  target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_0;
378 
379  // When any optimization level above 'zero' is enabled, the phi merges at
380  // loop continue blocks are rendered using syntax that is supported in
381  // GLSL, but not in SkSL.
382  // https://bugs.chromium.org/p/skia/issues/detail?id=13518.
383  spirv_options.optimization_level =
384  shaderc_optimization_level::shaderc_optimization_level_zero;
385  spirv_options.target = target;
386  spirv_options.macro_definitions.push_back("SKIA_GRAPHICS_BACKEND");
387  } break;
389  COMPILER_ERROR(error_stream_) << "Target platform invalid.";
390  return;
391  }
392 
393  // Implicit definition that indicates that this compilation is for the device
394  // (instead of the host).
395  spirv_options.macro_definitions.push_back("IMPELLER_DEVICE");
396  for (const auto& define : source_options.defines) {
397  spirv_options.macro_definitions.push_back(define);
398  }
399 
400  std::vector<std::string> included_file_names;
401  spirv_options.includer = std::make_shared<Includer>(
402  options_.working_directory, options_.include_dirs,
403  [&included_file_names](auto included_name) {
404  included_file_names.emplace_back(std::move(included_name));
405  });
406 
407  // SPIRV Generation.
408  SPIRVCompiler spv_compiler(source_options, source_mapping);
409 
410  spirv_assembly_ = spv_compiler.CompileToSPV(
411  error_stream_, spirv_options.BuildShadercOptions());
412 
413  if (!spirv_assembly_) {
414  return;
415  } else {
416  included_file_names_ = std::move(included_file_names);
417  }
418 
419  // SL Generation.
420  spirv_cross::Parser parser(
421  reinterpret_cast<const uint32_t*>(spirv_assembly_->GetMapping()),
422  spirv_assembly_->GetSize() / sizeof(uint32_t));
423  // The parser and compiler must be run separately because the parser contains
424  // meta information (like type member names) that are useful for reflection.
425  parser.parse();
426 
427  const auto parsed_ir =
428  std::make_shared<spirv_cross::ParsedIR>(parser.get_parsed_ir());
429 
430  auto sl_compiler = CreateCompiler(*parsed_ir, options_);
431 
432  if (!sl_compiler) {
433  COMPILER_ERROR(error_stream_)
434  << "Could not create compiler for target platform.";
435  return;
436  }
437 
438  uint32_t ubo_size = CalculateUBOSize(sl_compiler.GetCompiler());
439  if (ubo_size > kMaxUniformBufferSize) {
440  COMPILER_ERROR(error_stream_) << "Uniform buffer size exceeds max ("
441  << kMaxUniformBufferSize << "): " << ubo_size;
442  return;
443  }
444 
445  // We need to invoke the compiler even if we don't use the SL mapping later
446  // for Vulkan. The reflector needs information that is only valid after a
447  // successful compilation call.
448  auto sl_compilation_result =
449  CreateMappingWithString(sl_compiler.GetCompiler()->compile());
450 
451  // If the target is Vulkan, our shading language is SPIRV which we already
452  // have. We just need to strip it of debug information. If it isn't, we need
453  // to invoke the appropriate compiler to compile the SPIRV to the target SL.
454  if (source_options.target_platform == TargetPlatform::kVulkan ||
455  source_options.target_platform == TargetPlatform::kRuntimeStageVulkan) {
456  auto stripped_spirv_options = spirv_options;
457  stripped_spirv_options.generate_debug_info = false;
458  sl_mapping_ = spv_compiler.CompileToSPV(
459  error_stream_, stripped_spirv_options.BuildShadercOptions());
460  } else {
461  sl_mapping_ = sl_compilation_result;
462  }
463 
464  if (!sl_mapping_) {
465  COMPILER_ERROR(error_stream_) << "Could not generate SL from SPIRV";
466  return;
467  }
468 
469  reflector_ = std::make_unique<Reflector>(std::move(reflector_options), //
470  parsed_ir, //
471  GetSLShaderSource(), //
472  sl_compiler //
473  );
474 
475  if (!reflector_->IsValid()) {
476  COMPILER_ERROR(error_stream_)
477  << "Could not complete reflection on generated shader.";
478  return;
479  }
480 
481  is_valid_ = true;
482 }
std::shared_ptr< fml::Mapping > GetSLShaderSource() const
Definition: compiler.cc:490
#define COMPILER_ERROR(stream)
Definition: logger.h:39
static const uint32_t kMaxUniformBufferSize
Definition: compiler.cc:38
static CompilerBackend CreateCompiler(const spirv_cross::ParsedIR &ir, const SourceOptions &source_options)
Definition: compiler.cc:230
std::shared_ptr< fml::Mapping > CreateMappingWithString(std::string string)
Creates a mapping with string data.
Definition: allocation.cc:111
std::vector< IncludeDir > include_dirs
std::shared_ptr< fml::UniqueFD > working_directory

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

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

References impeller::compiler::JoinStrings().

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

◆ GetErrorMessages()

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

◆ GetIncludedFileNames()

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

Definition at line 508 of file compiler.cc.

508  {
509  return included_file_names_;
510 }

◆ GetReflector()

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

◆ GetSLShaderSource()

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

Definition at line 490 of file compiler.cc.

490  {
491  return sl_mapping_;
492 }

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

486  {
487  return spirv_assembly_;
488 }

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

◆ IsValid()

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

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