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

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

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

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

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

454  {
455  return included_file_names_;
456 }

◆ GetReflector()

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

◆ GetSLShaderSource()

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

Definition at line 436 of file compiler.cc.

436  {
437  return sl_mapping_;
438 }

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

432  {
433  return spirv_assembly_;
434 }

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

◆ IsValid()

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

The documentation for this class was generated from the following files:
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:458
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:436
impeller::compiler::CreateCompiler
static CompilerBackend CreateCompiler(const spirv_cross::ParsedIR &ir, const SourceOptions &source_options)
Definition: compiler.cc:211
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