Flutter Impeller
impeller::compiler Namespace Reference

Namespaces

 testing
 

Classes

class  Compiler
 
struct  CompilerBackend
 
struct  IncludeDir
 
struct  IncluderData
 
class  Includer
 
class  AutoLogger
 
struct  KnownType
 
struct  VertexType
 
struct  StructMember
 
class  Reflector
 
class  RuntimeStageData
 
class  ShaderBundleData
 
struct  SourceOptions
 
class  UniqueIncluder
 Wraps a shared includer so unique includers may be created to satisfy the shaderc API. This is a simple proxy object and does nothing. More...
 
struct  SPIRVCompilerSourceProfile
 
struct  SPIRVCompilerTargetEnv
 
struct  SPIRVCompilerOptions
 
class  SPIRVCompiler
 
class  CompilerSkSL
 
class  Switches
 
struct  StructField
 
struct  UniformDescription
 
struct  InputDescription
 
struct  ShaderConfig
 A shader config parsed as part of a ShaderBundleConfig. More...
 

Typedefs

using ShaderBundleConfig = std::unordered_map< std::string, ShaderConfig >
 

Enumerations

enum class  SourceType {
  kUnknown ,
  kVertexShader ,
  kFragmentShader ,
  kComputeShader
}
 
enum class  TargetPlatform {
  kUnknown ,
  kMetalDesktop ,
  kMetalIOS ,
  kOpenGLES ,
  kOpenGLDesktop ,
  kVulkan ,
  kRuntimeStageMetal ,
  kRuntimeStageGLES ,
  kRuntimeStageGLES3 ,
  kRuntimeStageVulkan ,
  kSkSL
}
 
enum class  SourceLanguage {
  kUnknown ,
  kGLSL ,
  kHLSL
}
 

Functions

static uint32_t ParseMSLVersion (const std::string &msl_version)
 
static CompilerBackend CreateMSLCompiler (const spirv_cross::ParsedIR &ir, const SourceOptions &source_options, std::optional< uint32_t > msl_version_override={})
 
static CompilerBackend CreateVulkanCompiler (const spirv_cross::ParsedIR &ir, const SourceOptions &source_options)
 
static CompilerBackend CreateGLSLCompiler (const spirv_cross::ParsedIR &ir, const SourceOptions &source_options)
 
static CompilerBackend CreateSkSLCompiler (const spirv_cross::ParsedIR &ir, const SourceOptions &source_options)
 
static bool EntryPointMustBeNamedMain (TargetPlatform platform)
 
static CompilerBackend CreateCompiler (const spirv_cross::ParsedIR &ir, const SourceOptions &source_options)
 
static std::string JoinStrings (std::vector< std::string > items, const std::string &separator)
 
bool Main (const fml::CommandLine &command_line)
 
static std::string ExecutionModelToStringName (spv::ExecutionModel model)
 
static std::string StringToShaderStage (const std::string &str)
 
static std::optional< RuntimeStageBackend > GetRuntimeStageBackend (TargetPlatform target_platform)
 
static std::string ToString (CompilerBackend::Type type)
 
static std::string TypeNameWithPaddingOfSize (size_t size)
 
static std::optional< KnownType > ReadKnownScalarType (spirv_cross::SPIRType::BaseType type)
 
static size_t GetReflectedStructSize (const std::vector< StructMember > &members)
 Get the reflected struct size. In the vast majority of the cases, this is the same as the declared struct size as given by the compiler. But, additional padding may need to be introduced after the end of the struct to keep in line with the alignment requirement of the individual struct members. This method figures out the actual size of the reflected struct that can be referenced in native code. More...
 
static VertexType VertexTypeFromInputResource (const spirv_cross::Compiler &compiler, const spirv_cross::Resource *resource)
 
static std::optional< fb::Stage > ToStage (spv::ExecutionModel stage)
 
static std::optional< fb::Stage > ToJsonStage (spv::ExecutionModel stage)
 
static std::optional< fb::UniformDataType > ToUniformType (spirv_cross::SPIRType::BaseType type)
 
static std::optional< fb::InputDataType > ToInputType (spirv_cross::SPIRType::BaseType type)
 
static std::optional< uint32_t > ToJsonType (spirv_cross::SPIRType::BaseType type)
 
static std::string RuntimeStageBackendToString (RuntimeStageBackend backend)
 
std::optional< ShaderBundleConfig > ParseShaderBundleConfig (const std::string &bundle_config_json, std::ostream &error_stream)
 Parse a shader bundle configuration from a given JSON string. More...
 
std::vector< std::string_view > GetShaderBundleTargetPlatformDefines (TargetPlatform platform)
 The platform-discriminating preprocessor defines injected when compiling a bundled shader for platform (for example IMPELLER_TARGET_METAL). These let bundled Flutter GPU shaders specialize for the backend they will run on, matching the defines that Impeller's own shaders receive from the build templates. More...
 
static std::unique_ptr< fb::shaderbundle::BackendShaderT > GenerateShaderBackendFB (TargetPlatform target_platform, SourceOptions &options, const std::string &shader_name, const ShaderConfig &shader_config, std::set< std::string > *out_dependencies)
 
static std::unique_ptr< fb::shaderbundle::ShaderT > GenerateShaderFB (SourceOptions options, const std::string &shader_name, const ShaderConfig &shader_config, std::set< std::string > *out_dependencies)
 
std::optional< fb::shaderbundle::ShaderBundleT > GenerateShaderBundleFlatbuffer (const std::string &bundle_config_json, const SourceOptions &options, std::set< std::string > *out_dependencies=nullptr)
 Parses the JSON shader bundle configuration and invokes the compiler multiple times to produce a shader bundle flatbuffer. More...
 
static bool OutputBundleDepfile (const Switches &switches, const std::string &target, const std::set< std::string > &dependencies)
 
bool GenerateShaderBundle (Switches &switches)
 Parses the JSON shader bundle configuration and invokes the compiler multiple times to produce a shader bundle flatbuffer, which is then output to the sl file. More...
 
static std::optional< fb::shaderbundle::ShaderStage > ToStage (spv::ExecutionModel stage)
 
static std::optional< fb::shaderbundle::UniformDataType > ToUniformType (spirv_cross::SPIRType::BaseType type)
 
static std::optional< fb::shaderbundle::InputDataType > ToInputType (spirv_cross::SPIRType::BaseType type)
 
static void SetDefaultLimitations (shaderc::CompileOptions &compiler_opts)
 
static void SetBindingBaseOffset (shaderc::CompileOptions &options)
 
void report_and_exit (const std::string &msg)
 
static TargetPlatform TargetPlatformFromCommandLine (const fml::CommandLine &command_line)
 
static std::vector< TargetPlatform > RuntimeStagesFromCommandLine (const fml::CommandLine &command_line)
 
static SourceType SourceTypeFromCommandLine (const fml::CommandLine &command_line)
 
static std::filesystem::path GetOptionAsPath (const fml::CommandLine &command_line, const char *arg)
 
SourceType SourceTypeFromFileName (const std::filesystem::path &file_name)
 
SourceType SourceTypeFromString (std::string name)
 
SourceLanguage ToSourceLanguage (const std::string &source_language)
 
std::string TargetPlatformToString (TargetPlatform platform)
 
std::string SourceLanguageToString (SourceLanguage source_language)
 
std::string EntryPointFunctionNameFromSourceName (const std::filesystem::path &file_name, SourceType type, SourceLanguage source_language, const std::string &entry_point_name)
 
std::string ShaderCErrorToString (shaderc_compilation_status status)
 
shaderc_shader_kind ToShaderCShaderKind (SourceType type)
 
spv::ExecutionModel ToExecutionModel (SourceType type)
 
spirv_cross::CompilerMSL::Options::Platform TargetPlatformToMSLPlatform (TargetPlatform platform)
 
std::string SourceTypeToString (SourceType type)
 
std::string TargetPlatformSLExtension (TargetPlatform platform)
 
bool TargetPlatformIsOpenGL (TargetPlatform platform)
 
bool TargetPlatformIsMetal (TargetPlatform platform)
 
bool TargetPlatformIsVulkan (TargetPlatform platform)
 
bool SetPermissiveAccess (const std::filesystem::path &p)
 Sets the file access mode of the file at path 'p' to 0644. More...
 
std::string Utf8FromPath (const std::filesystem::path &path)
 Converts a native format path to a utf8 string. More...
 
std::string InferShaderNameFromPath (const std::filesystem::path &path)
 
std::string ToCamelCase (std::string_view string)
 
std::string ToLowerCase (std::string_view string)
 
std::string ConvertToEntrypointName (std::string_view string)
 Ensure that the entrypoint name is a valid identifier in the target language. More...
 
bool StringStartsWith (const std::string &target, const std::string &prefix)
 

Variables

constexpr std::string_view kReflectionHeaderTemplate
 
constexpr std::string_view kReflectionCCTemplate
 
static const uint32_t kMaxUniformBufferSize = 6208
 
constexpr char kExternalTexturePrefix [] = "SAMPLER_EXTERNAL_OES_"
 
static const char * kFormatVersionKey = "format_version"
 
static const char * kStageKey = "stage"
 
static const char * kTargetPlatformKey = "target_platform"
 
static const char * kEntrypointKey = "entrypoint"
 
static const char * kUniformsKey = "uniforms"
 
static const char * kShaderKey = "shader"
 
static const char * kUniformNameKey = "name"
 
static const char * kUniformLocationKey = "location"
 
static const char * kUniformTypeKey = "type"
 
static const char * kUniformRowsKey = "rows"
 
static const char * kUniformColumnsKey = "columns"
 
static const char * kUniformBitWidthKey = "bit_width"
 
static const char * kUniformArrayElementsKey = "array_elements"
 
static const char * kUniformStructElementsKey = "struct_elements"
 
static const std::map< std::string, TargetPlatform > kKnownPlatforms
 
static const std::vector< std::pair< std::string, TargetPlatform > > kKnownRuntimeStages
 
static const std::map< std::string, SourceType > kKnownSourceTypes
 

Typedef Documentation

◆ ShaderBundleConfig

using impeller::compiler::ShaderBundleConfig = typedef std::unordered_map<std::string, ShaderConfig>

Definition at line 97 of file types.h.

Enumeration Type Documentation

◆ SourceLanguage

Enumerator
kUnknown 
kGLSL 
kHLSL 

Definition at line 44 of file types.h.

◆ SourceType

Enumerator
kUnknown 
kVertexShader 
kFragmentShader 
kComputeShader 

Definition at line 23 of file types.h.

◆ TargetPlatform

Enumerator
kUnknown 
kMetalDesktop 
kMetalIOS 
kOpenGLES 
kOpenGLDesktop 
kVulkan 
kRuntimeStageMetal 
kRuntimeStageGLES 
kRuntimeStageGLES3 
kRuntimeStageVulkan 
kSkSL 

Definition at line 30 of file types.h.

Function Documentation

◆ ConvertToEntrypointName()

std::string impeller::compiler::ConvertToEntrypointName ( std::string_view  string)

Ensure that the entrypoint name is a valid identifier in the target language.

Definition at line 68 of file utilities.cc.

68  {
69  if (string.empty()) {
70  return "";
71  }
72  std::stringstream stream;
73  // Append a prefix if the first character is not a letter.
74  if (!std::isalpha(string.data()[0])) {
75  stream << "i_";
76  }
77  for (size_t i = 0, count = string.length(); i < count; i++) {
78  auto ch = string.data()[i];
79  if (std::isalnum(ch) || ch == '_') {
80  stream << ch;
81  }
82  }
83  return stream.str();
84 }

Referenced by EntryPointFunctionNameFromSourceName(), and impeller::compiler::testing::TEST().

◆ CreateCompiler()

static CompilerBackend impeller::compiler::CreateCompiler ( const spirv_cross::ParsedIR &  ir,
const SourceOptions &  source_options 
)
static

Definition at line 306 of file compiler.cc.

307  {
308  CompilerBackend compiler;
309  switch (source_options.target_platform) {
310  case TargetPlatform::kMetalDesktop:
311  case TargetPlatform::kMetalIOS:
312  case TargetPlatform::kRuntimeStageMetal:
313  compiler = CreateMSLCompiler(ir, source_options);
314  break;
315  case TargetPlatform::kVulkan:
316  case TargetPlatform::kRuntimeStageVulkan:
317  compiler = CreateVulkanCompiler(ir, source_options);
318  break;
319  case TargetPlatform::kOpenGLES:
320  case TargetPlatform::kOpenGLDesktop:
321  case TargetPlatform::kRuntimeStageGLES:
322  case TargetPlatform::kRuntimeStageGLES3:
323  compiler = CreateGLSLCompiler(ir, source_options);
324  break;
325  case TargetPlatform::kSkSL:
326  compiler = CreateSkSLCompiler(ir, source_options);
327  break;
328  case TargetPlatform::kUnknown:
329  FML_UNREACHABLE();
330  }
331  if (!compiler) {
332  return {};
333  }
334  auto* backend = compiler.GetCompiler();
335  if (!EntryPointMustBeNamedMain(source_options.target_platform) &&
336  source_options.source_language == SourceLanguage::kGLSL) {
337  backend->rename_entry_point("main", source_options.entry_point_name,
338  ToExecutionModel(source_options.type));
339  }
340  return compiler;
341 }
static CompilerBackend CreateSkSLCompiler(const spirv_cross::ParsedIR &ir, const SourceOptions &source_options)
Definition: compiler.cc:280
static CompilerBackend CreateVulkanCompiler(const spirv_cross::ParsedIR &ir, const SourceOptions &source_options)
Definition: compiler.cc:199
static bool EntryPointMustBeNamedMain(TargetPlatform platform)
Definition: compiler.cc:286
static CompilerBackend CreateMSLCompiler(const spirv_cross::ParsedIR &ir, const SourceOptions &source_options, std::optional< uint32_t > msl_version_override={})
Definition: compiler.cc:128
static CompilerBackend CreateGLSLCompiler(const spirv_cross::ParsedIR &ir, const SourceOptions &source_options)
Definition: compiler.cc:211
spv::ExecutionModel ToExecutionModel(SourceType type)
Definition: types.cc:169

References CreateGLSLCompiler(), CreateMSLCompiler(), CreateSkSLCompiler(), CreateVulkanCompiler(), impeller::compiler::SourceOptions::entry_point_name, EntryPointMustBeNamedMain(), impeller::compiler::CompilerBackend::GetCompiler(), kGLSL, kMetalDesktop, kMetalIOS, kOpenGLDesktop, kOpenGLES, kRuntimeStageGLES, kRuntimeStageGLES3, kRuntimeStageMetal, kRuntimeStageVulkan, kSkSL, kUnknown, kVulkan, impeller::compiler::SourceOptions::source_language, impeller::compiler::SourceOptions::target_platform, ToExecutionModel(), and impeller::compiler::SourceOptions::type.

Referenced by impeller::compiler::Compiler::Compiler(), and Main().

◆ CreateGLSLCompiler()

static CompilerBackend impeller::compiler::CreateGLSLCompiler ( const spirv_cross::ParsedIR &  ir,
const SourceOptions &  source_options 
)
static

Definition at line 211 of file compiler.cc.

212  {
213  auto gl_compiler = std::make_shared<spirv_cross::CompilerGLSL>(ir);
214 
215  // Walk the variables and insert the external image extension if any of them
216  // begins with the external texture prefix. Unfortunately, we can't walk
217  // `gl_compiler->get_shader_resources().separate_samplers` until the compiler
218  // is further along.
219  //
220  // Unfortunately, we can't just let the shader author add this extension and
221  // use `samplerExternalOES` directly because compiling to spirv requires the
222  // source language profile to be at least 310 ES, but this extension is
223  // incompatible with ES 310+.
224  for (auto& id : ir.ids_for_constant_or_variable) {
225  if (StringStartsWith(ir.get_name(id), kExternalTexturePrefix)) {
226  if (source_options.gles_language_version >= 300) {
227  gl_compiler->require_extension(kEGLImageExternalExtension300);
228  } else {
229  gl_compiler->require_extension(kEGLImageExternalExtension);
230  }
231  break;
232  }
233  }
234 
235  spirv_cross::CompilerGLSL::Options sl_options;
236  sl_options.force_zero_initialized_variables = true;
237  sl_options.vertex.fixup_clipspace = true;
238  if (source_options.target_platform == TargetPlatform::kOpenGLES ||
239  source_options.target_platform == TargetPlatform::kRuntimeStageGLES ||
240  source_options.target_platform == TargetPlatform::kRuntimeStageGLES3) {
241  sl_options.version = source_options.gles_language_version > 0
242  ? source_options.gles_language_version
243  : 100;
244  // If we have requested GLES3 and/or Compute Shaders,
245  // promote the language version accordingly
246  if (source_options.type == SourceType::kComputeShader &&
247  sl_options.version < 310) {
248  sl_options.version = 310;
249  } else if (source_options.target_platform ==
250  TargetPlatform::kRuntimeStageGLES3 &&
251  sl_options.version < 300) {
252  sl_options.version = 300;
253  }
254  sl_options.es = true;
255  if (source_options.require_framebuffer_fetch &&
256  source_options.type == SourceType::kFragmentShader) {
257  gl_compiler->remap_ext_framebuffer_fetch(0, 0, true);
258  }
259  gl_compiler->set_variable_type_remap_callback(
260  [&](const spirv_cross::SPIRType& type, const std::string& var_name,
261  std::string& name_of_type) {
262  if (StringStartsWith(var_name, kExternalTexturePrefix)) {
263  name_of_type = "samplerExternalOES";
264  }
265  });
266  } else {
267  sl_options.version = source_options.gles_language_version > 0
268  ? source_options.gles_language_version
269  : 120;
270  if (source_options.type == SourceType::kComputeShader &&
271  sl_options.version < 430) {
272  sl_options.version = 430;
273  }
274  sl_options.es = false;
275  }
276  gl_compiler->set_common_options(sl_options);
277  return CompilerBackend(gl_compiler);
278 }
constexpr char kExternalTexturePrefix[]
Definition: constants.h:11
bool StringStartsWith(const std::string &target, const std::string &prefix)
Definition: utilities.cc:86

References impeller::compiler::SourceOptions::gles_language_version, kComputeShader, kExternalTexturePrefix, kFragmentShader, kOpenGLES, kRuntimeStageGLES, kRuntimeStageGLES3, impeller::compiler::SourceOptions::require_framebuffer_fetch, StringStartsWith(), impeller::compiler::SourceOptions::target_platform, and impeller::compiler::SourceOptions::type.

Referenced by CreateCompiler().

◆ CreateMSLCompiler()

static CompilerBackend impeller::compiler::CreateMSLCompiler ( const spirv_cross::ParsedIR &  ir,
const SourceOptions &  source_options,
std::optional< uint32_t >  msl_version_override = {} 
)
static

Definition at line 128 of file compiler.cc.

131  {}) {
132  auto sl_compiler = std::make_shared<spirv_cross::CompilerMSL>(ir);
133  spirv_cross::CompilerMSL::Options sl_options;
134  sl_options.platform =
135  TargetPlatformToMSLPlatform(source_options.target_platform);
136  sl_options.msl_version = msl_version_override.value_or(
137  ParseMSLVersion(source_options.metal_version));
138  sl_options.ios_use_simdgroup_functions =
139  sl_options.is_ios() &&
140  sl_options.msl_version >=
141  spirv_cross::CompilerMSL::Options::make_msl_version(2, 4, 0);
142  sl_options.use_framebuffer_fetch_subpasses = true;
143  sl_compiler->set_msl_options(sl_options);
144 
145  // Sort the float and sampler uniforms according to their declared/decorated
146  // order. For user authored fragment shaders, the API for setting uniform
147  // values uses the index of the uniform in the declared order. By default, the
148  // metal backend of spirv-cross will order uniforms according to usage. To fix
149  // this, we use the sorted order and the add_msl_resource_binding API to force
150  // the ordering to match the declared order. Note that while this code runs
151  // for all compiled shaders, it will only affect vertex and fragment shaders
152  // due to the specified stage.
153  auto floats =
154  SortUniforms(&ir, sl_compiler.get(), spirv_cross::SPIRType::Float);
155  auto images =
156  SortUniforms(&ir, sl_compiler.get(), spirv_cross::SPIRType::SampledImage);
157 
158  spv::ExecutionModel execution_model =
159  spv::ExecutionModel::ExecutionModelFragment;
160  if (source_options.type == SourceType::kVertexShader) {
161  execution_model = spv::ExecutionModel::ExecutionModelVertex;
162  }
163 
164  uint32_t buffer_offset = 0;
165  uint32_t sampler_offset = 0;
166  for (auto& float_id : floats) {
167  sl_compiler->add_msl_resource_binding(
168  {.stage = execution_model,
169  .basetype = spirv_cross::SPIRType::BaseType::Float,
170  .desc_set = sl_compiler->get_decoration(float_id,
171  spv::DecorationDescriptorSet),
172  .binding =
173  sl_compiler->get_decoration(float_id, spv::DecorationBinding),
174  .count = 1u,
175  .msl_buffer = buffer_offset});
176  buffer_offset++;
177  }
178  for (auto& image_id : images) {
179  sl_compiler->add_msl_resource_binding({
180  .stage = execution_model,
181  .basetype = spirv_cross::SPIRType::BaseType::SampledImage,
182  .desc_set =
183  sl_compiler->get_decoration(image_id, spv::DecorationDescriptorSet),
184  .binding =
185  sl_compiler->get_decoration(image_id, spv::DecorationBinding),
186  .count = 1u,
187  // A sampled image is both an image and a sampler, so both
188  // offsets need to be set or depending on the partiular shader
189  // the bindings may be incorrect.
190  .msl_texture = sampler_offset,
191  .msl_sampler = sampler_offset,
192  });
193  sampler_offset++;
194  }
195 
196  return CompilerBackend(sl_compiler);
197 }
spirv_cross::CompilerMSL::Options::Platform TargetPlatformToMSLPlatform(TargetPlatform platform)
Definition: types.cc:183
static uint32_t ParseMSLVersion(const std::string &msl_version)
Definition: compiler.cc:105
std::vector< spirv_cross::ID > SortUniforms(const spirv_cross::ParsedIR *ir, const spirv_cross::Compiler *compiler, std::optional< spirv_cross::SPIRType::BaseType > type_filter, bool include)
Sorts uniform declarations in an IR according to decoration order.

Referenced by CreateCompiler().

◆ CreateSkSLCompiler()

static CompilerBackend impeller::compiler::CreateSkSLCompiler ( const spirv_cross::ParsedIR &  ir,
const SourceOptions &  source_options 
)
static

Definition at line 280 of file compiler.cc.

281  {
282  auto sksl_compiler = std::make_shared<CompilerSkSL>(ir);
283  return CompilerBackend(sksl_compiler);
284 }

Referenced by CreateCompiler().

◆ CreateVulkanCompiler()

static CompilerBackend impeller::compiler::CreateVulkanCompiler ( const spirv_cross::ParsedIR &  ir,
const SourceOptions &  source_options 
)
static

Definition at line 199 of file compiler.cc.

201  {
202  auto gl_compiler = std::make_shared<spirv_cross::CompilerGLSL>(ir);
203  spirv_cross::CompilerGLSL::Options sl_options;
204  sl_options.force_zero_initialized_variables = true;
205  sl_options.vertex.fixup_clipspace = true;
206  sl_options.vulkan_semantics = true;
207  gl_compiler->set_common_options(sl_options);
208  return CompilerBackend(gl_compiler);
209 }

Referenced by CreateCompiler().

◆ EntryPointFunctionNameFromSourceName()

std::string impeller::compiler::EntryPointFunctionNameFromSourceName ( const std::filesystem::path &  file_name,
SourceType  type,
SourceLanguage  source_language,
const std::string &  entry_point_name 
)

Definition at line 101 of file types.cc.

105  {
106  if (source_language == SourceLanguage::kHLSL) {
107  return entry_point_name;
108  }
109 
110  std::stringstream stream;
111  stream << ConvertToEntrypointName(Utf8FromPath(file_name.stem())) << "_";
112  switch (type) {
113  case SourceType::kUnknown:
114  stream << "unknown";
115  break;
116  case SourceType::kVertexShader:
117  stream << "vertex";
118  break;
119  case SourceType::kFragmentShader:
120  stream << "fragment";
121  break;
122  case SourceType::kComputeShader:
123  stream << "compute";
124  break;
125  }
126  stream << "_main";
127  return stream.str();
128 }
std::string ConvertToEntrypointName(std::string_view string)
Ensure that the entrypoint name is a valid identifier in the target language.
Definition: utilities.cc:68
std::string Utf8FromPath(const std::filesystem::path &path)
Converts a native format path to a utf8 string.
Definition: utilities.cc:30

References ConvertToEntrypointName(), kComputeShader, kFragmentShader, kHLSL, kUnknown, kVertexShader, and Utf8FromPath().

Referenced by impeller::compiler::testing::CompilerTestBase::CanCompileAndReflect(), impeller::compiler::Switches::CreateSourceOptions(), and GenerateShaderBackendFB().

◆ EntryPointMustBeNamedMain()

static bool impeller::compiler::EntryPointMustBeNamedMain ( TargetPlatform  platform)
static

Definition at line 286 of file compiler.cc.

286  {
287  switch (platform) {
288  case TargetPlatform::kUnknown:
289  FML_UNREACHABLE();
290  case TargetPlatform::kMetalDesktop:
291  case TargetPlatform::kMetalIOS:
292  case TargetPlatform::kVulkan:
293  case TargetPlatform::kRuntimeStageMetal:
294  case TargetPlatform::kRuntimeStageVulkan:
295  return false;
296  case TargetPlatform::kSkSL:
297  case TargetPlatform::kOpenGLES:
298  case TargetPlatform::kOpenGLDesktop:
299  case TargetPlatform::kRuntimeStageGLES:
300  case TargetPlatform::kRuntimeStageGLES3:
301  return true;
302  }
303  FML_UNREACHABLE();
304 }

References kMetalDesktop, kMetalIOS, kOpenGLDesktop, kOpenGLES, kRuntimeStageGLES, kRuntimeStageGLES3, kRuntimeStageMetal, kRuntimeStageVulkan, kSkSL, kUnknown, and kVulkan.

Referenced by CreateCompiler().

◆ ExecutionModelToStringName()

static std::string impeller::compiler::ExecutionModelToStringName ( spv::ExecutionModel  model)
static

Definition at line 35 of file reflector.cc.

35  {
36  switch (model) {
37  case spv::ExecutionModel::ExecutionModelVertex:
38  return "vertex";
39  case spv::ExecutionModel::ExecutionModelFragment:
40  return "fragment";
41  case spv::ExecutionModel::ExecutionModelGLCompute:
42  return "compute";
43  default:
44  return "unsupported";
45  }
46 }

◆ GenerateShaderBackendFB()

static std::unique_ptr<fb::shaderbundle::BackendShaderT> impeller::compiler::GenerateShaderBackendFB ( TargetPlatform  target_platform,
SourceOptions &  options,
const std::string &  shader_name,
const ShaderConfig &  shader_config,
std::set< std::string > *  out_dependencies 
)
static

Override options.

Definition at line 115 of file shader_bundle.cc.

119  {
120  auto result = std::make_unique<fb::shaderbundle::BackendShaderT>();
121 
122  std::shared_ptr<fml::FileMapping> source_file_mapping =
123  fml::FileMapping::CreateReadOnly(shader_config.source_file_name);
124  if (!source_file_mapping) {
125  std::cerr << "Could not open file for bundled shader \"" << shader_name
126  << "\"." << std::endl;
127  return nullptr;
128  }
129 
130  /// Override options.
131  options.target_platform = target_platform;
132  options.file_name = shader_name; // This is just used for error messages.
133  options.type = shader_config.type;
134  options.source_language = shader_config.language;
135  options.entry_point_name = EntryPointFunctionNameFromSourceName(
136  shader_config.source_file_name, options.type, options.source_language,
137  shader_config.entry_point);
138 
139  // Inject the platform-discriminating defines (e.g. IMPELLER_TARGET_METAL) so
140  // bundled shaders can specialize per backend. These are added to a local copy
141  // because `options` is shared across every backend compiled here; pushing
142  // onto it directly would accumulate defines from previously compiled
143  // backends.
144  SourceOptions backend_options = options;
145  backend_options.target_platform = target_platform;
146  for (const auto& define :
147  GetShaderBundleTargetPlatformDefines(target_platform)) {
148  backend_options.defines.emplace_back(define);
149  }
150 
151  Reflector::Options reflector_options;
152  reflector_options.target_platform = target_platform;
153  reflector_options.entry_point_name = options.entry_point_name;
154  reflector_options.shader_name = shader_name;
155 
156  Compiler compiler(source_file_mapping, backend_options, reflector_options);
157  if (!compiler.IsValid()) {
158  std::cerr << "Compilation failed for bundled shader \"" << shader_name
159  << "\"." << std::endl;
160  std::cerr << compiler.GetErrorMessages() << std::endl;
161  return nullptr;
162  }
163 
164  // Record dependencies so the caller can emit a depfile. The shader's
165  // source file plus every transitive `#include` that contributed to
166  // the compilation. The same source is compiled across multiple
167  // target platforms; the std::set dedupes naturally.
168  if (out_dependencies) {
169  out_dependencies->insert(shader_config.source_file_name);
170  for (const auto& included : compiler.GetIncludedFileNames()) {
171  out_dependencies->insert(included);
172  }
173  }
174 
175  auto reflector = compiler.GetReflector();
176  if (reflector == nullptr) {
177  std::cerr << "Could not create reflector for bundled shader \""
178  << shader_name << "\"." << std::endl;
179  return nullptr;
180  }
181 
182  auto bundle_data = reflector->GetShaderBundleData();
183  if (!bundle_data) {
184  std::cerr << "Bundled shader information was nil for \"" << shader_name
185  << "\"." << std::endl;
186  return nullptr;
187  }
188 
189  result = bundle_data->CreateFlatbuffer();
190  if (!result) {
191  std::cerr << "Failed to create flatbuffer for bundled shader \""
192  << shader_name << "\"." << std::endl;
193  return nullptr;
194  }
195 
196  return result;
197 }
std::vector< std::string_view > GetShaderBundleTargetPlatformDefines(TargetPlatform platform)
The platform-discriminating preprocessor defines injected when compiling a bundled shader for platfor...
std::string EntryPointFunctionNameFromSourceName(const std::filesystem::path &file_name, SourceType type, SourceLanguage source_language, const std::string &entry_point_name)
Definition: types.cc:101

References impeller::compiler::SourceOptions::defines, impeller::compiler::ShaderConfig::entry_point, impeller::compiler::Reflector::Options::entry_point_name, impeller::compiler::SourceOptions::entry_point_name, EntryPointFunctionNameFromSourceName(), impeller::compiler::SourceOptions::file_name, impeller::compiler::Compiler::GetErrorMessages(), impeller::compiler::Compiler::GetIncludedFileNames(), impeller::compiler::Compiler::GetReflector(), impeller::compiler::Reflector::GetShaderBundleData(), GetShaderBundleTargetPlatformDefines(), impeller::compiler::Compiler::IsValid(), impeller::compiler::ShaderConfig::language, impeller::compiler::Reflector::Options::shader_name, impeller::compiler::ShaderConfig::source_file_name, impeller::compiler::SourceOptions::source_language, impeller::compiler::Reflector::Options::target_platform, impeller::compiler::SourceOptions::target_platform, impeller::compiler::SourceOptions::type, and impeller::compiler::ShaderConfig::type.

Referenced by GenerateShaderFB().

◆ GenerateShaderBundle()

bool impeller::compiler::GenerateShaderBundle ( Switches &  switches)

Parses the JSON shader bundle configuration and invokes the compiler multiple times to produce a shader bundle flatbuffer, which is then output to the sl file.

  1. Parse the shader bundle and generate the flatbuffer result.

Collect dependencies along the way so a depfile can be emitted after the bundle is written. The same source file is compiled across multiple target platforms; the std::set dedupes naturally.

  1. Serialize the shader bundle and write to disk.
  2. Output a depfile if one was requested.

Lets build systems (notably Dart's hooks framework, which flutter_gpu_shaders' buildShaderBundleJson consumer goes through) rerun the bundle build when any contributing source file or #included header changes.

Definition at line 309 of file shader_bundle.cc.

309  {
310  // --------------------------------------------------------------------------
311  /// 1. Parse the shader bundle and generate the flatbuffer result.
312  ///
313  /// Collect dependencies along the way so a depfile can be emitted
314  /// after the bundle is written. The same source file is compiled
315  /// across multiple target platforms; the std::set dedupes naturally.
316  ///
317 
318  std::set<std::string> dependencies;
319  const bool want_depfile = !switches.depfile_path.empty();
320  auto shader_bundle = GenerateShaderBundleFlatbuffer(
321  switches.shader_bundle, switches.CreateSourceOptions(),
322  want_depfile ? &dependencies : nullptr);
323  if (!shader_bundle.has_value()) {
324  // Specific error messages are already handled by
325  // GenerateShaderBundleFlatbuffer.
326  return false;
327  }
328 
329  // --------------------------------------------------------------------------
330  /// 2. Serialize the shader bundle and write to disk.
331  ///
332 
333  auto builder = std::make_shared<flatbuffers::FlatBufferBuilder>();
334  builder->Finish(fb::shaderbundle::ShaderBundle::Pack(*builder.get(),
335  &shader_bundle.value()),
336  fb::shaderbundle::ShaderBundleIdentifier());
337  auto mapping = std::make_shared<fml::NonOwnedMapping>(
338  builder->GetBufferPointer(), builder->GetSize(),
339  [builder](auto, auto) {});
340 
341  auto sl_file_name = std::filesystem::absolute(
342  std::filesystem::current_path() / switches.sl_file_name);
343 
344  if (!fml::WriteAtomically(*switches.working_directory, //
345  Utf8FromPath(sl_file_name).c_str(), //
346  *mapping //
347  )) {
348  std::cerr << "Could not write file to " << switches.sl_file_name
349  << std::endl;
350  return false;
351  }
352  // Tools that consume the runtime stage data expect the access mode to
353  // be 0644.
354  if (!SetPermissiveAccess(sl_file_name)) {
355  return false;
356  }
357 
358  // --------------------------------------------------------------------------
359  /// 3. Output a depfile if one was requested.
360  ///
361  /// Lets build systems (notably Dart's `hooks` framework, which
362  /// `flutter_gpu_shaders`' `buildShaderBundleJson` consumer goes
363  /// through) rerun the bundle build when any contributing source file
364  /// or `#include`d header changes.
365 
366  if (want_depfile) {
367  if (!OutputBundleDepfile(switches, Utf8FromPath(sl_file_name),
368  dependencies)) {
369  return false;
370  }
371  }
372 
373  return true;
374 }
bool SetPermissiveAccess(const std::filesystem::path &p)
Sets the file access mode of the file at path 'p' to 0644.
Definition: utilities.cc:16
std::optional< fb::shaderbundle::ShaderBundleT > GenerateShaderBundleFlatbuffer(const std::string &bundle_config_json, const SourceOptions &options, std::set< std::string > *out_dependencies)
Parses the JSON shader bundle configuration and invokes the compiler multiple times to produce a shad...
static bool OutputBundleDepfile(const Switches &switches, const std::string &target, const std::set< std::string > &dependencies)

References impeller::compiler::Switches::CreateSourceOptions(), impeller::compiler::Switches::depfile_path, GenerateShaderBundleFlatbuffer(), OutputBundleDepfile(), SetPermissiveAccess(), impeller::compiler::Switches::shader_bundle, impeller::compiler::Switches::sl_file_name, Utf8FromPath(), and impeller::compiler::Switches::working_directory.

Referenced by Main().

◆ GenerateShaderBundleFlatbuffer()

std::optional< fb::shaderbundle::ShaderBundleT > impeller::compiler::GenerateShaderBundleFlatbuffer ( const std::string &  bundle_config_json,
const SourceOptions &  options,
std::set< std::string > *  out_dependencies = nullptr 
)

Parses the JSON shader bundle configuration and invokes the compiler multiple times to produce a shader bundle flatbuffer.

Note
Exposed only for testing purposes. Use GenerateShaderBundle directly.
Parameters
out_dependenciesOptional. When non-null, populated with the set of source files (including transitive #includes) that contributed to the generated bundle. Used by GenerateShaderBundle to emit a depfile when --depfile is set.
  1. Parse the bundle configuration.
  2. Build the deserialized shader bundle.

Definition at line 239 of file shader_bundle.cc.

242  {
243  // --------------------------------------------------------------------------
244  /// 1. Parse the bundle configuration.
245  ///
246 
247  std::optional<ShaderBundleConfig> bundle_config =
248  ParseShaderBundleConfig(bundle_config_json, std::cerr);
249  if (!bundle_config) {
250  return std::nullopt;
251  }
252 
253  // --------------------------------------------------------------------------
254  /// 2. Build the deserialized shader bundle.
255  ///
256 
257  fb::shaderbundle::ShaderBundleT shader_bundle;
258  shader_bundle.format_version = static_cast<uint32_t>(
259  fb::shaderbundle::ShaderBundleFormatVersion::kVersion);
260 
261  for (const auto& [shader_name, shader_config] : bundle_config.value()) {
262  std::unique_ptr<fb::shaderbundle::ShaderT> shader =
263  GenerateShaderFB(options, shader_name, shader_config, out_dependencies);
264  if (!shader) {
265  return std::nullopt;
266  }
267  shader_bundle.shaders.push_back(std::move(shader));
268  }
269 
270  return shader_bundle;
271 }
static std::unique_ptr< fb::shaderbundle::ShaderT > GenerateShaderFB(SourceOptions options, const std::string &shader_name, const ShaderConfig &shader_config, std::set< std::string > *out_dependencies)
std::optional< ShaderBundleConfig > ParseShaderBundleConfig(const std::string &bundle_config_json, std::ostream &error_stream)
Parse a shader bundle configuration from a given JSON string.

References GenerateShaderFB(), and ParseShaderBundleConfig().

Referenced by GenerateShaderBundle(), and impeller::compiler::testing::TEST().

◆ GenerateShaderFB()

static std::unique_ptr<fb::shaderbundle::ShaderT> impeller::compiler::GenerateShaderFB ( SourceOptions  options,
const std::string &  shader_name,
const ShaderConfig &  shader_config,
std::set< std::string > *  out_dependencies 
)
static

Definition at line 199 of file shader_bundle.cc.

203  {
204  auto result = std::make_unique<fb::shaderbundle::ShaderT>();
205  result->name = shader_name;
206  result->metal_ios =
207  GenerateShaderBackendFB(TargetPlatform::kMetalIOS, options, shader_name,
208  shader_config, out_dependencies);
209  if (!result->metal_ios) {
210  return nullptr;
211  }
212  result->metal_desktop =
213  GenerateShaderBackendFB(TargetPlatform::kMetalDesktop, options,
214  shader_name, shader_config, out_dependencies);
215  if (!result->metal_desktop) {
216  return nullptr;
217  }
218  result->opengl_es =
219  GenerateShaderBackendFB(TargetPlatform::kOpenGLES, options, shader_name,
220  shader_config, out_dependencies);
221  if (!result->opengl_es) {
222  return nullptr;
223  }
224  result->opengl_desktop =
225  GenerateShaderBackendFB(TargetPlatform::kOpenGLDesktop, options,
226  shader_name, shader_config, out_dependencies);
227  if (!result->opengl_desktop) {
228  return nullptr;
229  }
230  result->vulkan =
231  GenerateShaderBackendFB(TargetPlatform::kVulkan, options, shader_name,
232  shader_config, out_dependencies);
233  if (!result->vulkan) {
234  return nullptr;
235  }
236  return result;
237 }
static std::unique_ptr< fb::shaderbundle::BackendShaderT > GenerateShaderBackendFB(TargetPlatform target_platform, SourceOptions &options, const std::string &shader_name, const ShaderConfig &shader_config, std::set< std::string > *out_dependencies)

References GenerateShaderBackendFB(), kMetalDesktop, kMetalIOS, kOpenGLDesktop, kOpenGLES, and kVulkan.

Referenced by GenerateShaderBundleFlatbuffer().

◆ GetOptionAsPath()

static std::filesystem::path impeller::compiler::GetOptionAsPath ( const fml::CommandLine &  command_line,
const char *  arg 
)
static

Definition at line 171 of file switches.cc.

173  {
174  std::string value = command_line.GetOptionValueWithDefault(arg, "");
175  return std::filesystem::path(std::u8string(value.begin(), value.end()));
176 }
int32_t value

References value.

◆ GetReflectedStructSize()

static size_t impeller::compiler::GetReflectedStructSize ( const std::vector< StructMember > &  members)
static

Get the reflected struct size. In the vast majority of the cases, this is the same as the declared struct size as given by the compiler. But, additional padding may need to be introduced after the end of the struct to keep in line with the alignment requirement of the individual struct members. This method figures out the actual size of the reflected struct that can be referenced in native code.

Parameters
[in]membersThe members
Returns
The reflected structure size.

Definition at line 883 of file reflector.cc.

883  {
884  auto struct_size = 0u;
885  for (const auto& member : members) {
886  struct_size += member.byte_length;
887  }
888  return struct_size;
889 }

◆ GetRuntimeStageBackend()

static std::optional<RuntimeStageBackend> impeller::compiler::GetRuntimeStageBackend ( TargetPlatform  target_platform)
static

Definition at line 347 of file reflector.cc.

348  {
349  switch (target_platform) {
350  case TargetPlatform::kUnknown:
351  case TargetPlatform::kMetalDesktop:
352  case TargetPlatform::kMetalIOS:
353  case TargetPlatform::kOpenGLES:
354  case TargetPlatform::kOpenGLDesktop:
355  case TargetPlatform::kVulkan:
356  return std::nullopt;
357  case TargetPlatform::kRuntimeStageMetal:
358  return RuntimeStageBackend::kMetal;
359  case TargetPlatform::kRuntimeStageGLES:
360  return RuntimeStageBackend::kOpenGLES;
361  case TargetPlatform::kRuntimeStageGLES3:
362  return RuntimeStageBackend::kOpenGLES3;
363  case TargetPlatform::kRuntimeStageVulkan:
364  return RuntimeStageBackend::kVulkan;
365  case TargetPlatform::kSkSL:
366  return RuntimeStageBackend::kSkSL;
367  }
368  FML_UNREACHABLE();
369 }

References impeller::kMetal, kMetalDesktop, kMetalIOS, kOpenGLDesktop, kOpenGLES, impeller::kOpenGLES, impeller::kOpenGLES3, kRuntimeStageGLES, kRuntimeStageGLES3, kRuntimeStageMetal, kRuntimeStageVulkan, kSkSL, impeller::kSkSL, kUnknown, kVulkan, and impeller::kVulkan.

Referenced by impeller::testing::TEST_P().

◆ GetShaderBundleTargetPlatformDefines()

std::vector< std::string_view > impeller::compiler::GetShaderBundleTargetPlatformDefines ( TargetPlatform  platform)

The platform-discriminating preprocessor defines injected when compiling a bundled shader for platform (for example IMPELLER_TARGET_METAL). These let bundled Flutter GPU shaders specialize for the backend they will run on, matching the defines that Impeller's own shaders receive from the build templates.

Note
Exposed only for testing purposes.

Definition at line 90 of file shader_bundle.cc.

91  {
92  switch (platform) {
93  case TargetPlatform::kMetalIOS:
94  return {"IMPELLER_TARGET_METAL", "IMPELLER_TARGET_METAL_IOS"};
95  case TargetPlatform::kMetalDesktop:
96  return {"IMPELLER_TARGET_METAL", "IMPELLER_TARGET_METAL_DESKTOP"};
97  case TargetPlatform::kOpenGLES:
98  return {"IMPELLER_TARGET_OPENGLES"};
99  case TargetPlatform::kOpenGLDesktop:
100  return {"IMPELLER_TARGET_OPENGL"};
101  case TargetPlatform::kVulkan:
102  return {"IMPELLER_TARGET_VULKAN"};
103  case TargetPlatform::kSkSL:
104  case TargetPlatform::kRuntimeStageMetal:
105  case TargetPlatform::kRuntimeStageGLES:
106  case TargetPlatform::kRuntimeStageGLES3:
107  case TargetPlatform::kRuntimeStageVulkan:
108  case TargetPlatform::kUnknown:
109  return {};
110  }
111  return {};
112 }

References kMetalDesktop, kMetalIOS, kOpenGLDesktop, kOpenGLES, kRuntimeStageGLES, kRuntimeStageGLES3, kRuntimeStageMetal, kRuntimeStageVulkan, kSkSL, kUnknown, and kVulkan.

Referenced by GenerateShaderBackendFB(), and impeller::compiler::testing::TEST().

◆ InferShaderNameFromPath()

std::string impeller::compiler::InferShaderNameFromPath ( const std::filesystem::path &  path)

Definition at line 34 of file utilities.cc.

34  {
35  return Utf8FromPath(path.stem());
36 }

References Utf8FromPath().

◆ JoinStrings()

static std::string impeller::compiler::JoinStrings ( std::vector< std::string >  items,
const std::string &  separator 
)
static

Definition at line 713 of file compiler.cc.

714  {
715  std::stringstream stream;
716  for (size_t i = 0, count = items.size(); i < count; i++) {
717  const auto is_last = (i == count - 1);
718 
719  stream << items[i];
720  if (!is_last) {
721  stream << separator;
722  }
723  }
724  return stream.str();
725 }

Referenced by impeller::compiler::Compiler::CreateDepfileContents().

◆ Main()

bool impeller::compiler::Main ( const fml::CommandLine &  command_line)
  1. Output the source file. When in IPLR/RuntimeStage mode, output the serialized IPLR flatbuffer. Otherwise output the shader source in the target shading language.
  2. Output SPIRV file.
  3. Output shader reflection data. May include a JSON file, a C++ header, and/or a C++ TU.
  4. Output a depfile.

Definition at line 197 of file impellerc_main.cc.

197  {
198  fml::InstallCrashHandler();
199  if (command_line.HasOption("help")) {
200  Switches::PrintHelp(std::cout);
201  return true;
202  }
203 
204  Switches switches(command_line);
205  if (!switches.AreValid(std::cerr)) {
206  std::cerr << "Invalid flags specified." << std::endl;
207  Switches::PrintHelp(std::cerr);
208  return false;
209  }
210 
211  if (!switches.shader_bundle.empty()) {
212  // Invoke the compiler multiple times to build a shader bundle with the
213  // given shader_bundle spec.
214  return GenerateShaderBundle(switches);
215  }
216 
217  std::shared_ptr<fml::FileMapping> source_file_mapping =
218  fml::FileMapping::CreateReadOnly(Utf8FromPath(switches.source_file_name));
219  if (!source_file_mapping) {
220  std::cerr << "Could not open input file." << std::endl;
221  return false;
222  }
223 
224  std::vector<std::shared_ptr<Compiler>> compilers;
225  compilers.reserve(switches.PlatformsToCompile().size());
226  for (const auto& platform : switches.PlatformsToCompile()) {
227  std::shared_ptr<Compiler> compiler =
228  CreateCompiler(platform, source_file_mapping, switches);
229  if (compiler->IsValid()) {
230  compilers.push_back(compiler);
231  } else {
232  std::cerr << "Compilation failed for target: "
233  << TargetPlatformToString(platform) << std::endl;
234 
235  std::string verbose_error_messages = compiler->GetVerboseErrorMessages();
236  if (verbose_error_messages.empty()) {
237  // No verbose error messages. Output the regular error messages.
238  std::cerr << compiler->GetErrorMessages();
239  } else {
240  if (switches.verbose) {
241  // Verbose messages are available and the --verbose flag was set.
242  // Directly output the verbose error messages.
243  std::cerr << verbose_error_messages;
244  } else {
245  // Verbose messages are available and the --verbose flag was not set.
246  // Output the regular error messages and write the verbose error
247  // messages to a file.
248  std::cerr << compiler->GetErrorMessages();
249  OutputVerboseErrorFile(verbose_error_messages, switches);
250  }
251  }
252 
253  return false;
254  }
255  }
256 
257  // --------------------------------------------------------------------------
258  /// 1. Output the source file. When in IPLR/RuntimeStage mode, output the
259  /// serialized IPLR flatbuffer. Otherwise output the shader source in the
260  /// target shading language.
261  ///
262 
263  if (switches.iplr) {
264  if (!OutputIPLR(compilers, switches)) {
265  return false;
266  }
267  } else {
268  // Non-IPLR mode is supported only for single platform targets. There is
269  // exactly 1 created compiler for this case.
270  FML_DCHECK(compilers.size() == 1);
271  if (!OutputSLFile(*compilers.front(), switches)) {
272  return false;
273  }
274  }
275 
276  // Use the first compiler for outputting the SPIRV file, reflection data, and
277  // the depfile. The SPIRV and depfile outputs do not depend on the target
278  // platform, so any valid compiler can be used. Reflection data output is only
279  // supported for single platform targets, so it uses the first (only) valid
280  // compiler as well.
281  auto first_valid_compiler = compilers.front();
282 
283  // --------------------------------------------------------------------------
284  /// 2. Output SPIRV file.
285  ///
286 
287  if (!OutputSPIRV(*first_valid_compiler, switches)) {
288  return false;
289  }
290 
291  // --------------------------------------------------------------------------
292  /// 3. Output shader reflection data.
293  /// May include a JSON file, a C++ header, and/or a C++ TU.
294  ///
295 
296  if (ShouldOutputReflectionData(switches)) {
297  // Outputting reflection data is supported only for single platform targets.
298  FML_DCHECK(compilers.size() == 1);
299  if (!OutputReflectionData(*first_valid_compiler, switches)) {
300  return false;
301  }
302  }
303 
304  // --------------------------------------------------------------------------
305  /// 4. Output a depfile.
306  ///
307 
308  // Dep file output does not depend on the target platform. Any valid compiler
309  // can be used to output it. Arbitrarily pick the first valid compiler.
310  if (!OutputDepfile(*first_valid_compiler, switches)) {
311  return false;
312  }
313 
314  return true;
315 }
std::string TargetPlatformToString(TargetPlatform platform)
Definition: types.cc:62
bool GenerateShaderBundle(Switches &switches)
Parses the JSON shader bundle configuration and invokes the compiler multiple times to produce a shad...
static CompilerBackend CreateCompiler(const spirv_cross::ParsedIR &ir, const SourceOptions &source_options)
Definition: compiler.cc:306

References impeller::compiler::Switches::AreValid(), CreateCompiler(), GenerateShaderBundle(), impeller::compiler::Switches::iplr, impeller::compiler::Switches::PlatformsToCompile(), impeller::compiler::Switches::PrintHelp(), impeller::compiler::Switches::shader_bundle, impeller::compiler::Switches::source_file_name, TargetPlatformToString(), Utf8FromPath(), and impeller::compiler::Switches::verbose.

Referenced by main().

◆ OutputBundleDepfile()

static bool impeller::compiler::OutputBundleDepfile ( const Switches &  switches,
const std::string &  target,
const std::set< std::string > &  dependencies 
)
static

Write a Ninja-style depfile listing every source file (including #included headers) that contributed to the shader bundle at target.

Format mirrors Compiler::CreateDepfileContents for single-shader compiles: <target>: <dep1> <dep2> ... <depN>\n. See https://github.com/ninja-build/ninja/blob/master/src/depfile_parser.cc#L28

Definition at line 281 of file shader_bundle.cc.

283  {
284  std::stringstream stream;
285  stream << target << ":";
286  for (const auto& dep : dependencies) {
287  stream << " " << dep;
288  }
289  stream << "\n";
290  const auto contents = std::make_shared<std::string>(stream.str());
291  const fml::NonOwnedMapping mapping(
292  reinterpret_cast<const uint8_t*>(contents->data()), contents->size(),
293  [contents](auto, auto) {});
294 
295  // Pass the relative path straight through; fml::WriteAtomically
296  // resolves it against switches.working_directory (a directory fd
297  // representing the build system's intended working dir, which may
298  // differ from std::filesystem::current_path()).
299  if (!fml::WriteAtomically(*switches.working_directory,
300  Utf8FromPath(switches.depfile_path).c_str(),
301  mapping)) {
302  std::cerr << "Could not write depfile to " << switches.depfile_path
303  << std::endl;
304  return false;
305  }
306  return true;
307 }

References impeller::compiler::Switches::depfile_path, Utf8FromPath(), and impeller::compiler::Switches::working_directory.

Referenced by GenerateShaderBundle().

◆ ParseMSLVersion()

static uint32_t impeller::compiler::ParseMSLVersion ( const std::string &  msl_version)
static

Definition at line 105 of file compiler.cc.

105  {
106  std::stringstream sstream(msl_version);
107  std::string version_part;
108  uint32_t major = 1;
109  uint32_t minor = 2;
110  uint32_t patch = 0;
111  if (std::getline(sstream, version_part, '.')) {
112  major = std::stoi(version_part);
113  if (std::getline(sstream, version_part, '.')) {
114  minor = std::stoi(version_part);
115  if (std::getline(sstream, version_part, '.')) {
116  patch = std::stoi(version_part);
117  }
118  }
119  }
120  if (major < 1 || (major == 1 && minor < 2)) {
121  std::cerr << "--metal-version version must be at least 1.2. Have "
122  << msl_version << std::endl;
123  }
124  return spirv_cross::CompilerMSL::Options::make_msl_version(major, minor,
125  patch);
126 }

◆ ParseShaderBundleConfig()

std::optional< ShaderBundleConfig > impeller::compiler::ParseShaderBundleConfig ( const std::string &  bundle_config_json,
std::ostream &  error_stream 
)

Parse a shader bundle configuration from a given JSON string.

Note
Exposed only for testing purposes. Use GenerateShaderBundle directly.

Definition at line 25 of file shader_bundle.cc.

27  {
28  auto json = nlohmann::json::parse(bundle_config_json, nullptr, false);
29  if (json.is_discarded() || !json.is_object()) {
30  error_stream << "The shader bundle is not a valid JSON object."
31  << std::endl;
32  return std::nullopt;
33  }
34 
35  ShaderBundleConfig bundle;
36  for (auto& [shader_name, shader_value] : json.items()) {
37  if (bundle.find(shader_name) != bundle.end()) {
38  error_stream << "Duplicate shader \"" << shader_name << "\"."
39  << std::endl;
40  return std::nullopt;
41  }
42  if (!shader_value.is_object()) {
43  error_stream << "Invalid shader entry \"" << shader_name
44  << "\": Entry is not a JSON object." << std::endl;
45  return std::nullopt;
46  }
47 
48  ShaderConfig shader;
49 
50  if (!shader_value.contains("file")) {
51  error_stream << "Invalid shader entry \"" << shader_name
52  << "\": Missing required \"file\" field." << std::endl;
53  return std::nullopt;
54  }
55  shader.source_file_name = shader_value["file"];
56 
57  if (!shader_value.contains("type")) {
58  error_stream << "Invalid shader entry \"" << shader_name
59  << "\": Missing required \"type\" field." << std::endl;
60  return std::nullopt;
61  }
62  shader.type = SourceTypeFromString(shader_value["type"]);
63  if (shader.type == SourceType::kUnknown) {
64  error_stream << "Invalid shader entry \"" << shader_name
65  << "\": Shader type " << shader_value["type"]
66  << " is unknown." << std::endl;
67  return std::nullopt;
68  }
69 
70  shader.language = shader_value.contains("language")
71  ? ToSourceLanguage(shader_value["language"])
73  if (shader.language == SourceLanguage::kUnknown) {
74  error_stream << "Invalid shader entry \"" << shader_name
75  << "\": Unknown language type " << shader_value["language"]
76  << "." << std::endl;
77  return std::nullopt;
78  }
79 
80  shader.entry_point = shader_value.contains("entry_point")
81  ? shader_value["entry_point"]
82  : "main";
83 
84  bundle[shader_name] = shader;
85  }
86 
87  return bundle;
88 }
std::unordered_map< std::string, ShaderConfig > ShaderBundleConfig
Definition: types.h:97
SourceType SourceTypeFromString(std::string name)
Definition: types.cc:34
SourceLanguage ToSourceLanguage(const std::string &source_language)
Definition: types.cc:52

References impeller::compiler::ShaderConfig::entry_point, kGLSL, kUnknown, impeller::compiler::ShaderConfig::language, impeller::compiler::ShaderConfig::source_file_name, SourceTypeFromString(), ToSourceLanguage(), and impeller::compiler::ShaderConfig::type.

Referenced by GenerateShaderBundleFlatbuffer(), and impeller::compiler::testing::TEST().

◆ ReadKnownScalarType()

static std::optional<KnownType> impeller::compiler::ReadKnownScalarType ( spirv_cross::SPIRType::BaseType  type)
static

Definition at line 836 of file reflector.cc.

837  {
838  switch (type) {
839  case spirv_cross::SPIRType::BaseType::Boolean:
840  return KnownType{
841  .name = "bool",
842  .byte_size = sizeof(bool),
843  };
844  case spirv_cross::SPIRType::BaseType::Float:
845  return KnownType{
846  .name = "Scalar",
847  .byte_size = sizeof(Scalar),
848  };
849  case spirv_cross::SPIRType::BaseType::Half:
850  return KnownType{
851  .name = "Half",
852  .byte_size = sizeof(Half),
853  };
854  case spirv_cross::SPIRType::BaseType::UInt:
855  return KnownType{
856  .name = "uint32_t",
857  .byte_size = sizeof(uint32_t),
858  };
859  case spirv_cross::SPIRType::BaseType::Int:
860  return KnownType{
861  .name = "int32_t",
862  .byte_size = sizeof(int32_t),
863  };
864  default:
865  break;
866  }
867  return std::nullopt;
868 }
float Scalar
Definition: scalar.h:19

References impeller::compiler::KnownType::name.

◆ report_and_exit()

void impeller::compiler::report_and_exit ( const std::string &  msg)

Definition at line 16 of file spirv_sksl.cc.

16  {
17  fprintf(stderr, "There was a compiler error: %s\n", msg.c_str());
18  fflush(stderr);
19  exit(1);
20 }

◆ RuntimeStageBackendToString()

static std::string impeller::compiler::RuntimeStageBackendToString ( RuntimeStageBackend  backend)
static

Definition at line 199 of file runtime_stage_data.cc.

199  {
200  switch (backend) {
201  case RuntimeStageBackend::kSkSL:
202  return "sksl";
203  case RuntimeStageBackend::kMetal:
204  return "metal";
205  case RuntimeStageBackend::kOpenGLES:
206  return "opengles";
207  case RuntimeStageBackend::kVulkan:
208  return "vulkan";
209  case RuntimeStageBackend::kOpenGLES3:
210  return "opengles3";
211  }
212 }

References impeller::kMetal, impeller::kOpenGLES, impeller::kOpenGLES3, impeller::kSkSL, and impeller::kVulkan.

Referenced by impeller::compiler::RuntimeStageData::CreateJsonMapping().

◆ RuntimeStagesFromCommandLine()

static std::vector<TargetPlatform> impeller::compiler::RuntimeStagesFromCommandLine ( const fml::CommandLine &  command_line)
static

Definition at line 147 of file switches.cc.

148  {
149  std::vector<TargetPlatform> stages;
150  for (const auto& platform : kKnownRuntimeStages) {
151  if (command_line.HasOption(platform.first)) {
152  stages.push_back(platform.second);
153  }
154  }
155  return stages;
156 }
static const std::vector< std::pair< std::string, TargetPlatform > > kKnownRuntimeStages
Definition: switches.cc:29

References kKnownRuntimeStages.

◆ SetBindingBaseOffset()

static void impeller::compiler::SetBindingBaseOffset ( shaderc::CompileOptions &  options)
static

Definition at line 211 of file spirv_compiler.cc.

211  {
212  constexpr uint32_t kBindingBaseOffset = 64;
213  static const shaderc_uniform_kind kUniformKinds[] = {
214  shaderc_uniform_kind::shaderc_uniform_kind_sampler,
215  shaderc_uniform_kind::shaderc_uniform_kind_texture,
216  shaderc_uniform_kind::shaderc_uniform_kind_image,
217  shaderc_uniform_kind::shaderc_uniform_kind_buffer, // UBOs
218  shaderc_uniform_kind::shaderc_uniform_kind_storage_buffer, // SSBOs
219  };
220 
221  for (size_t i = 0u; i < sizeof(kUniformKinds) / sizeof(shaderc_uniform_kind);
222  i++) {
223  options.SetBindingBaseForStage(
224  shaderc_shader_kind::shaderc_fragment_shader, //
225  kUniformKinds[i], //
226  kBindingBaseOffset //
227  );
228  }
229 }

Referenced by impeller::compiler::SPIRVCompilerOptions::BuildShadercOptions().

◆ SetDefaultLimitations()

static void impeller::compiler::SetDefaultLimitations ( shaderc::CompileOptions &  compiler_opts)
static

Definition at line 93 of file spirv_compiler.cc.

93  {
94  using Limit = std::pair<shaderc_limit, int>;
95  static constexpr std::array<Limit, 83> limits = {
96  Limit{shaderc_limit::shaderc_limit_max_lights, 8},
97  Limit{shaderc_limit::shaderc_limit_max_clip_planes, 6},
98  Limit{shaderc_limit::shaderc_limit_max_texture_units, 2},
99  Limit{shaderc_limit::shaderc_limit_max_texture_coords, 8},
100  Limit{shaderc_limit::shaderc_limit_max_vertex_attribs, 16},
101  Limit{shaderc_limit::shaderc_limit_max_vertex_uniform_components, 4096},
102  Limit{shaderc_limit::shaderc_limit_max_varying_floats, 60},
103  Limit{shaderc_limit::shaderc_limit_max_vertex_texture_image_units, 16},
104  Limit{shaderc_limit::shaderc_limit_max_combined_texture_image_units, 80},
105  Limit{shaderc_limit::shaderc_limit_max_texture_image_units, 16},
106  Limit{shaderc_limit::shaderc_limit_max_fragment_uniform_components, 1024},
107  Limit{shaderc_limit::shaderc_limit_max_draw_buffers, 8},
108  Limit{shaderc_limit::shaderc_limit_max_vertex_uniform_vectors, 256},
109  Limit{shaderc_limit::shaderc_limit_max_varying_vectors, 15},
110  Limit{shaderc_limit::shaderc_limit_max_fragment_uniform_vectors, 256},
111  Limit{shaderc_limit::shaderc_limit_max_vertex_output_vectors, 16},
112  Limit{shaderc_limit::shaderc_limit_max_fragment_input_vectors, 15},
113  Limit{shaderc_limit::shaderc_limit_min_program_texel_offset, -8},
114  Limit{shaderc_limit::shaderc_limit_max_program_texel_offset, 7},
115  Limit{shaderc_limit::shaderc_limit_max_clip_distances, 8},
116  Limit{shaderc_limit::shaderc_limit_max_compute_work_group_count_x, 65535},
117  Limit{shaderc_limit::shaderc_limit_max_compute_work_group_count_y, 65535},
118  Limit{shaderc_limit::shaderc_limit_max_compute_work_group_count_z, 65535},
119  Limit{shaderc_limit::shaderc_limit_max_compute_work_group_size_x, 1024},
120  Limit{shaderc_limit::shaderc_limit_max_compute_work_group_size_y, 1024},
121  Limit{shaderc_limit::shaderc_limit_max_compute_work_group_size_z, 64},
122  Limit{shaderc_limit::shaderc_limit_max_compute_uniform_components, 512},
123  Limit{shaderc_limit::shaderc_limit_max_compute_texture_image_units, 16},
124  Limit{shaderc_limit::shaderc_limit_max_compute_image_uniforms, 8},
125  Limit{shaderc_limit::shaderc_limit_max_compute_atomic_counters, 8},
126  Limit{shaderc_limit::shaderc_limit_max_compute_atomic_counter_buffers, 1},
127  Limit{shaderc_limit::shaderc_limit_max_varying_components, 60},
128  Limit{shaderc_limit::shaderc_limit_max_vertex_output_components, 64},
129  Limit{shaderc_limit::shaderc_limit_max_geometry_input_components, 64},
130  Limit{shaderc_limit::shaderc_limit_max_geometry_output_components, 128},
131  Limit{shaderc_limit::shaderc_limit_max_fragment_input_components, 128},
132  Limit{shaderc_limit::shaderc_limit_max_image_units, 8},
133  Limit{shaderc_limit::
134  shaderc_limit_max_combined_image_units_and_fragment_outputs,
135  8},
136  Limit{shaderc_limit::shaderc_limit_max_combined_shader_output_resources,
137  8},
138  Limit{shaderc_limit::shaderc_limit_max_image_samples, 0},
139  Limit{shaderc_limit::shaderc_limit_max_vertex_image_uniforms, 0},
140  Limit{shaderc_limit::shaderc_limit_max_tess_control_image_uniforms, 0},
141  Limit{shaderc_limit::shaderc_limit_max_tess_evaluation_image_uniforms, 0},
142  Limit{shaderc_limit::shaderc_limit_max_geometry_image_uniforms, 0},
143  Limit{shaderc_limit::shaderc_limit_max_fragment_image_uniforms, 8},
144  Limit{shaderc_limit::shaderc_limit_max_combined_image_uniforms, 8},
145  Limit{shaderc_limit::shaderc_limit_max_geometry_texture_image_units, 16},
146  Limit{shaderc_limit::shaderc_limit_max_geometry_output_vertices, 256},
147  Limit{shaderc_limit::shaderc_limit_max_geometry_total_output_components,
148  1024},
149  Limit{shaderc_limit::shaderc_limit_max_geometry_uniform_components, 512},
150  Limit{shaderc_limit::shaderc_limit_max_geometry_varying_components, 60},
151  Limit{shaderc_limit::shaderc_limit_max_tess_control_input_components,
152  128},
153  Limit{shaderc_limit::shaderc_limit_max_tess_control_output_components,
154  128},
155  Limit{shaderc_limit::shaderc_limit_max_tess_control_texture_image_units,
156  16},
157  Limit{shaderc_limit::shaderc_limit_max_tess_control_uniform_components,
158  1024},
159  Limit{
160  shaderc_limit::shaderc_limit_max_tess_control_total_output_components,
161  4096},
162  Limit{shaderc_limit::shaderc_limit_max_tess_evaluation_input_components,
163  128},
164  Limit{shaderc_limit::shaderc_limit_max_tess_evaluation_output_components,
165  128},
166  Limit{
167  shaderc_limit::shaderc_limit_max_tess_evaluation_texture_image_units,
168  16},
169  Limit{shaderc_limit::shaderc_limit_max_tess_evaluation_uniform_components,
170  1024},
171  Limit{shaderc_limit::shaderc_limit_max_tess_patch_components, 120},
172  Limit{shaderc_limit::shaderc_limit_max_patch_vertices, 32},
173  Limit{shaderc_limit::shaderc_limit_max_tess_gen_level, 64},
174  Limit{shaderc_limit::shaderc_limit_max_viewports, 16},
175  Limit{shaderc_limit::shaderc_limit_max_vertex_atomic_counters, 0},
176  Limit{shaderc_limit::shaderc_limit_max_tess_control_atomic_counters, 0},
177  Limit{shaderc_limit::shaderc_limit_max_tess_evaluation_atomic_counters,
178  0},
179  Limit{shaderc_limit::shaderc_limit_max_geometry_atomic_counters, 0},
180  Limit{shaderc_limit::shaderc_limit_max_fragment_atomic_counters, 8},
181  Limit{shaderc_limit::shaderc_limit_max_combined_atomic_counters, 8},
182  Limit{shaderc_limit::shaderc_limit_max_atomic_counter_bindings, 1},
183  Limit{shaderc_limit::shaderc_limit_max_vertex_atomic_counter_buffers, 0},
184  Limit{
185  shaderc_limit::shaderc_limit_max_tess_control_atomic_counter_buffers,
186  0},
187  Limit{shaderc_limit::
188  shaderc_limit_max_tess_evaluation_atomic_counter_buffers,
189  0},
190  Limit{shaderc_limit::shaderc_limit_max_geometry_atomic_counter_buffers,
191  0},
192  Limit{shaderc_limit::shaderc_limit_max_fragment_atomic_counter_buffers,
193  0},
194  Limit{shaderc_limit::shaderc_limit_max_combined_atomic_counter_buffers,
195  1},
196  Limit{shaderc_limit::shaderc_limit_max_atomic_counter_buffer_size, 32},
197  Limit{shaderc_limit::shaderc_limit_max_transform_feedback_buffers, 4},
198  Limit{shaderc_limit::
199  shaderc_limit_max_transform_feedback_interleaved_components,
200  64},
201  Limit{shaderc_limit::shaderc_limit_max_cull_distances, 8},
202  Limit{shaderc_limit::shaderc_limit_max_combined_clip_and_cull_distances,
203  8},
204  Limit{shaderc_limit::shaderc_limit_max_samples, 4},
205  };
206  for (auto& [limit, value] : limits) {
207  compiler_opts.SetLimit(limit, value);
208  }
209 }

References value.

Referenced by impeller::compiler::SPIRVCompilerOptions::BuildShadercOptions().

◆ SetPermissiveAccess()

bool impeller::compiler::SetPermissiveAccess ( const std::filesystem::path &  p)

Sets the file access mode of the file at path 'p' to 0644.

Definition at line 16 of file utilities.cc.

16  {
17  auto permissions =
18  std::filesystem::perms::owner_read | std::filesystem::perms::owner_write |
19  std::filesystem::perms::group_read | std::filesystem::perms::others_read;
20  std::error_code error;
21  std::filesystem::permissions(p, permissions, error);
22  if (error) {
23  std::cerr << "Failed to set access on file '" << p
24  << "': " << error.message() << std::endl;
25  return false;
26  }
27  return true;
28 }

Referenced by GenerateShaderBundle().

◆ ShaderCErrorToString()

std::string impeller::compiler::ShaderCErrorToString ( shaderc_compilation_status  status)

Definition at line 130 of file types.cc.

130  {
131  using Status = shaderc_compilation_status;
132  switch (status) {
133  case Status::shaderc_compilation_status_success:
134  return "Success";
135  case Status::shaderc_compilation_status_invalid_stage:
136  return "Invalid shader stage specified";
137  case Status::shaderc_compilation_status_compilation_error:
138  return "Compilation error";
139  case Status::shaderc_compilation_status_internal_error:
140  return "Internal error";
141  case Status::shaderc_compilation_status_null_result_object:
142  return "Internal error. Null result object";
143  case Status::shaderc_compilation_status_invalid_assembly:
144  return "Invalid assembly";
145  case Status::shaderc_compilation_status_validation_error:
146  return "Validation error";
147  case Status::shaderc_compilation_status_transformation_error:
148  return "Transform error";
149  case Status::shaderc_compilation_status_configuration_error:
150  return "Configuration error";
151  }
152  return "Unknown internal error";
153 }

Referenced by impeller::compiler::SPIRVCompiler::CompileToSPV().

◆ SourceLanguageToString()

std::string impeller::compiler::SourceLanguageToString ( SourceLanguage  source_language)

Definition at line 90 of file types.cc.

90  {
91  switch (source_language) {
92  case SourceLanguage::kUnknown:
93  return "Unknown";
94  case SourceLanguage::kGLSL:
95  return "GLSL";
96  case SourceLanguage::kHLSL:
97  return "HLSL";
98  }
99 }

References kGLSL, kHLSL, and kUnknown.

Referenced by impeller::compiler::SPIRVCompiler::CompileToSPV().

◆ SourceTypeFromCommandLine()

static SourceType impeller::compiler::SourceTypeFromCommandLine ( const fml::CommandLine &  command_line)
static

Definition at line 158 of file switches.cc.

159  {
160  auto source_type_option =
161  command_line.GetOptionValueWithDefault("input-type", "");
162  auto source_type_search = kKnownSourceTypes.find(source_type_option);
163  if (source_type_search == kKnownSourceTypes.end()) {
164  return SourceType::kUnknown;
165  }
166  return source_type_search->second;
167 }
static const std::map< std::string, SourceType > kKnownSourceTypes
Definition: switches.cc:37

References kKnownSourceTypes, and kUnknown.

◆ SourceTypeFromFileName()

SourceType impeller::compiler::SourceTypeFromFileName ( const std::filesystem::path &  file_name)

Definition at line 17 of file types.cc.

17  {
18  std::string extension = file_name.extension().string();
19  if (extension == ".vert") {
20  return SourceType::kVertexShader;
21  }
22 
23  if (extension == ".frag") {
24  return SourceType::kFragmentShader;
25  }
26 
27  if (extension == ".comp") {
28  return SourceType::kComputeShader;
29  }
30 
31  return SourceType::kUnknown;
32 }

References kComputeShader, kFragmentShader, kUnknown, and kVertexShader.

Referenced by impeller::compiler::testing::CompilerTestBase::CanCompileAndReflect(), impeller::compiler::Switches::CreateSourceOptions(), and impeller::compiler::testing::TEST().

◆ SourceTypeFromString()

SourceType impeller::compiler::SourceTypeFromString ( std::string  name)

Definition at line 34 of file types.cc.

34  {
35  name = ToLowerCase(name);
36 
37  if (name == "vertex") {
38  return SourceType::kVertexShader;
39  }
40 
41  if (name == "fragment") {
42  return SourceType::kFragmentShader;
43  }
44 
45  if (name == "compute") {
46  return SourceType::kComputeShader;
47  }
48 
49  return SourceType::kUnknown;
50 }
std::string ToLowerCase(std::string_view string)
Definition: utilities.cc:61

References kComputeShader, kFragmentShader, kUnknown, kVertexShader, and ToLowerCase().

Referenced by ParseShaderBundleConfig().

◆ SourceTypeToString()

std::string impeller::compiler::SourceTypeToString ( SourceType  type)

Definition at line 204 of file types.cc.

204  {
205  switch (type) {
206  case SourceType::kUnknown:
207  return "unknown";
208  case SourceType::kVertexShader:
209  return "vert";
210  case SourceType::kFragmentShader:
211  return "frag";
212  case SourceType::kComputeShader:
213  return "comp";
214  }
215  FML_UNREACHABLE();
216 }

References kComputeShader, kFragmentShader, kUnknown, and kVertexShader.

◆ StringStartsWith()

bool impeller::compiler::StringStartsWith ( const std::string &  target,
const std::string &  prefix 
)

Definition at line 86 of file utilities.cc.

86  {
87  if (prefix.length() > target.length()) {
88  return false;
89  }
90  for (size_t i = 0; i < prefix.length(); i++) {
91  if (target[i] != prefix[i]) {
92  return false;
93  }
94  }
95  return true;
96 }

Referenced by CreateGLSLCompiler().

◆ StringToShaderStage()

static std::string impeller::compiler::StringToShaderStage ( const std::string &  str)
static

Definition at line 48 of file reflector.cc.

48  {
49  if (str == "vertex") {
50  return "ShaderStage::kVertex";
51  }
52 
53  if (str == "fragment") {
54  return "ShaderStage::kFragment";
55  }
56 
57  if (str == "compute") {
58  return "ShaderStage::kCompute";
59  }
60 
61  return "ShaderStage::kUnknown";
62 }

◆ TargetPlatformFromCommandLine()

static TargetPlatform impeller::compiler::TargetPlatformFromCommandLine ( const fml::CommandLine &  command_line)
static

Definition at line 129 of file switches.cc.

130  {
131  auto target = TargetPlatform::kUnknown;
132  for (const auto& platform : kKnownPlatforms) {
133  if (command_line.HasOption(platform.first)) {
134  // If the platform has already been determined, the caller may have
135  // specified multiple platforms. This is an error and only one must be
136  // selected.
137  if (target != TargetPlatform::kUnknown) {
138  return TargetPlatform::kUnknown;
139  }
140  target = platform.second;
141  // Keep going to detect duplicates.
142  }
143  }
144  return target;
145 }
static const std::map< std::string, TargetPlatform > kKnownPlatforms
Definition: switches.cc:20

References kKnownPlatforms, and kUnknown.

◆ TargetPlatformIsMetal()

bool impeller::compiler::TargetPlatformIsMetal ( TargetPlatform  platform)

Definition at line 258 of file types.cc.

258  {
259  switch (platform) {
260  case TargetPlatform::kMetalDesktop:
261  case TargetPlatform::kMetalIOS:
262  case TargetPlatform::kRuntimeStageMetal:
263  return true;
264  case TargetPlatform::kUnknown:
265  case TargetPlatform::kSkSL:
266  case TargetPlatform::kOpenGLES:
267  case TargetPlatform::kOpenGLDesktop:
268  case TargetPlatform::kRuntimeStageGLES:
269  case TargetPlatform::kRuntimeStageGLES3:
270  case TargetPlatform::kRuntimeStageVulkan:
271  case TargetPlatform::kVulkan:
272  return false;
273  }
274  FML_UNREACHABLE();
275 }

References kMetalDesktop, kMetalIOS, kOpenGLDesktop, kOpenGLES, kRuntimeStageGLES, kRuntimeStageGLES3, kRuntimeStageMetal, kRuntimeStageVulkan, kSkSL, kUnknown, and kVulkan.

◆ TargetPlatformIsOpenGL()

bool impeller::compiler::TargetPlatformIsOpenGL ( TargetPlatform  platform)

Definition at line 239 of file types.cc.

239  {
240  switch (platform) {
241  case TargetPlatform::kOpenGLES:
242  case TargetPlatform::kOpenGLDesktop:
243  case TargetPlatform::kRuntimeStageGLES:
244  case TargetPlatform::kRuntimeStageGLES3:
245  return true;
246  case TargetPlatform::kMetalDesktop:
247  case TargetPlatform::kRuntimeStageMetal:
248  case TargetPlatform::kRuntimeStageVulkan:
249  case TargetPlatform::kMetalIOS:
250  case TargetPlatform::kUnknown:
251  case TargetPlatform::kSkSL:
252  case TargetPlatform::kVulkan:
253  return false;
254  }
255  FML_UNREACHABLE();
256 }

References kMetalDesktop, kMetalIOS, kOpenGLDesktop, kOpenGLES, kRuntimeStageGLES, kRuntimeStageGLES3, kRuntimeStageMetal, kRuntimeStageVulkan, kSkSL, kUnknown, and kVulkan.

Referenced by impeller::compiler::testing::TEST_P().

◆ TargetPlatformIsVulkan()

bool impeller::compiler::TargetPlatformIsVulkan ( TargetPlatform  platform)

Definition at line 277 of file types.cc.

277  {
278  switch (platform) {
279  case TargetPlatform::kRuntimeStageVulkan:
280  case TargetPlatform::kVulkan:
281  return true;
282  case TargetPlatform::kMetalDesktop:
283  case TargetPlatform::kMetalIOS:
284  case TargetPlatform::kRuntimeStageMetal:
285  case TargetPlatform::kUnknown:
286  case TargetPlatform::kSkSL:
287  case TargetPlatform::kOpenGLES:
288  case TargetPlatform::kOpenGLDesktop:
289  case TargetPlatform::kRuntimeStageGLES:
290  case TargetPlatform::kRuntimeStageGLES3:
291  return false;
292  }
293  FML_UNREACHABLE();
294 }

References kMetalDesktop, kMetalIOS, kOpenGLDesktop, kOpenGLES, kRuntimeStageGLES, kRuntimeStageGLES3, kRuntimeStageMetal, kRuntimeStageVulkan, kSkSL, kUnknown, and kVulkan.

Referenced by impeller::compiler::testing::TEST_P().

◆ TargetPlatformSLExtension()

std::string impeller::compiler::TargetPlatformSLExtension ( TargetPlatform  platform)

Definition at line 218 of file types.cc.

218  {
219  switch (platform) {
220  case TargetPlatform::kUnknown:
221  return "unknown";
222  case TargetPlatform::kMetalDesktop:
223  case TargetPlatform::kMetalIOS:
224  case TargetPlatform::kRuntimeStageMetal:
225  return "metal";
226  case TargetPlatform::kSkSL:
227  case TargetPlatform::kOpenGLES:
228  case TargetPlatform::kOpenGLDesktop:
229  case TargetPlatform::kRuntimeStageGLES:
230  case TargetPlatform::kRuntimeStageGLES3:
231  return "glsl";
232  case TargetPlatform::kVulkan:
233  case TargetPlatform::kRuntimeStageVulkan:
234  return "vk.spirv";
235  }
236  FML_UNREACHABLE();
237 }

References kMetalDesktop, kMetalIOS, kOpenGLDesktop, kOpenGLES, kRuntimeStageGLES, kRuntimeStageGLES3, kRuntimeStageMetal, kRuntimeStageVulkan, kSkSL, kUnknown, and kVulkan.

Referenced by impeller::compiler::testing::SLFileName().

◆ TargetPlatformToMSLPlatform()

spirv_cross::CompilerMSL::Options::Platform impeller::compiler::TargetPlatformToMSLPlatform ( TargetPlatform  platform)

Definition at line 183 of file types.cc.

184  {
185  switch (platform) {
186  case TargetPlatform::kMetalIOS:
187  case TargetPlatform::kRuntimeStageMetal:
188  return spirv_cross::CompilerMSL::Options::Platform::iOS;
189  case TargetPlatform::kMetalDesktop:
190  return spirv_cross::CompilerMSL::Options::Platform::macOS;
191  case TargetPlatform::kSkSL:
192  case TargetPlatform::kOpenGLES:
193  case TargetPlatform::kOpenGLDesktop:
194  case TargetPlatform::kRuntimeStageGLES:
195  case TargetPlatform::kRuntimeStageGLES3:
196  case TargetPlatform::kRuntimeStageVulkan:
197  case TargetPlatform::kVulkan:
198  case TargetPlatform::kUnknown:
199  return spirv_cross::CompilerMSL::Options::Platform::macOS;
200  }
201  FML_UNREACHABLE();
202 }

References kMetalDesktop, kMetalIOS, kOpenGLDesktop, kOpenGLES, kRuntimeStageGLES, kRuntimeStageGLES3, kRuntimeStageMetal, kRuntimeStageVulkan, kSkSL, kUnknown, and kVulkan.

◆ TargetPlatformToString()

std::string impeller::compiler::TargetPlatformToString ( TargetPlatform  platform)

Definition at line 62 of file types.cc.

62  {
63  switch (platform) {
64  case TargetPlatform::kUnknown:
65  return "Unknown";
66  case TargetPlatform::kMetalDesktop:
67  return "MetalDesktop";
68  case TargetPlatform::kMetalIOS:
69  return "MetaliOS";
70  case TargetPlatform::kOpenGLES:
71  return "OpenGLES";
72  case TargetPlatform::kOpenGLDesktop:
73  return "OpenGLDesktop";
74  case TargetPlatform::kVulkan:
75  return "Vulkan";
76  case TargetPlatform::kRuntimeStageMetal:
77  return "RuntimeStageMetal";
78  case TargetPlatform::kRuntimeStageGLES:
79  return "RuntimeStageGLES";
80  case TargetPlatform::kRuntimeStageGLES3:
81  return "RuntimeStageGLES3";
82  case TargetPlatform::kRuntimeStageVulkan:
83  return "RuntimeStageVulkan";
84  case TargetPlatform::kSkSL:
85  return "SkSL";
86  }
87  FML_UNREACHABLE();
88 }

References kMetalDesktop, kMetalIOS, kOpenGLDesktop, kOpenGLES, kRuntimeStageGLES, kRuntimeStageGLES3, kRuntimeStageMetal, kRuntimeStageVulkan, kSkSL, kUnknown, and kVulkan.

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

◆ ToCamelCase()

std::string impeller::compiler::ToCamelCase ( std::string_view  string)

Definition at line 38 of file utilities.cc.

38  {
39  if (string.empty()) {
40  return "";
41  }
42 
43  std::stringstream stream;
44  bool next_upper = true;
45  for (size_t i = 0, count = string.length(); i < count; i++) {
46  auto ch = string.data()[i];
47  if (next_upper) {
48  next_upper = false;
49  stream << static_cast<char>(std::toupper(ch));
50  continue;
51  }
52  if (ch == '_') {
53  next_upper = true;
54  continue;
55  }
56  stream << ch;
57  }
58  return stream.str();
59 }

◆ ToExecutionModel()

spv::ExecutionModel impeller::compiler::ToExecutionModel ( SourceType  type)

Definition at line 169 of file types.cc.

169  {
170  switch (type) {
171  case SourceType::kVertexShader:
172  return spv::ExecutionModel::ExecutionModelVertex;
173  case SourceType::kFragmentShader:
174  return spv::ExecutionModel::ExecutionModelFragment;
175  case SourceType::kComputeShader:
176  return spv::ExecutionModel::ExecutionModelGLCompute;
177  case SourceType::kUnknown:
178  break;
179  }
180  return spv::ExecutionModel::ExecutionModelMax;
181 }

References kComputeShader, kFragmentShader, kUnknown, and kVertexShader.

Referenced by CreateCompiler().

◆ ToInputType() [1/2]

static std::optional<fb::InputDataType> impeller::compiler::ToInputType ( spirv_cross::SPIRType::BaseType  type)
static

Definition at line 95 of file runtime_stage_data.cc.

96  {
97  switch (type) {
98  case spirv_cross::SPIRType::Boolean:
99  return fb::InputDataType::kBoolean;
100  case spirv_cross::SPIRType::SByte:
101  return fb::InputDataType::kSignedByte;
102  case spirv_cross::SPIRType::UByte:
103  return fb::InputDataType::kUnsignedByte;
104  case spirv_cross::SPIRType::Short:
105  return fb::InputDataType::kSignedShort;
106  case spirv_cross::SPIRType::UShort:
107  return fb::InputDataType::kUnsignedShort;
108  case spirv_cross::SPIRType::Int:
109  return fb::InputDataType::kSignedInt;
110  case spirv_cross::SPIRType::UInt:
111  return fb::InputDataType::kUnsignedInt;
112  case spirv_cross::SPIRType::Int64:
113  return fb::InputDataType::kSignedInt64;
114  case spirv_cross::SPIRType::UInt64:
115  return fb::InputDataType::kUnsignedInt64;
116  case spirv_cross::SPIRType::Float:
118  case spirv_cross::SPIRType::Double:
119  return fb::InputDataType::kDouble;
120  case spirv_cross::SPIRType::Unknown:
121  case spirv_cross::SPIRType::Void:
122  case spirv_cross::SPIRType::Half:
123  case spirv_cross::SPIRType::AtomicCounter:
124  case spirv_cross::SPIRType::Struct:
125  case spirv_cross::SPIRType::Image:
126  case spirv_cross::SPIRType::SampledImage:
127  case spirv_cross::SPIRType::Sampler:
128  case spirv_cross::SPIRType::AccelerationStructure:
129  case spirv_cross::SPIRType::RayQuery:
130  case spirv_cross::SPIRType::ControlPointArray:
131  case spirv_cross::SPIRType::Interpolant:
132  case spirv_cross::SPIRType::Char:
133  return std::nullopt;
134  }
135  FML_UNREACHABLE();
136 }

References impeller::kFloat.

Referenced by impeller::compiler::ShaderBundleData::CreateFlatbuffer(), and impeller::compiler::RuntimeStageData::CreateStageFlatbuffer().

◆ ToInputType() [2/2]

static std::optional<fb::shaderbundle::InputDataType> impeller::compiler::ToInputType ( spirv_cross::SPIRType::BaseType  type)
static

Definition at line 100 of file shader_bundle_data.cc.

101  {
102  switch (type) {
103  case spirv_cross::SPIRType::Boolean:
104  return fb::shaderbundle::InputDataType::kBoolean;
105  case spirv_cross::SPIRType::SByte:
106  return fb::shaderbundle::InputDataType::kSignedByte;
107  case spirv_cross::SPIRType::UByte:
108  return fb::shaderbundle::InputDataType::kUnsignedByte;
109  case spirv_cross::SPIRType::Short:
110  return fb::shaderbundle::InputDataType::kSignedShort;
111  case spirv_cross::SPIRType::UShort:
112  return fb::shaderbundle::InputDataType::kUnsignedShort;
113  case spirv_cross::SPIRType::Int:
114  return fb::shaderbundle::InputDataType::kSignedInt;
115  case spirv_cross::SPIRType::UInt:
116  return fb::shaderbundle::InputDataType::kUnsignedInt;
117  case spirv_cross::SPIRType::Int64:
118  return fb::shaderbundle::InputDataType::kSignedInt64;
119  case spirv_cross::SPIRType::UInt64:
120  return fb::shaderbundle::InputDataType::kUnsignedInt64;
121  case spirv_cross::SPIRType::Float:
123  case spirv_cross::SPIRType::Double:
124  return fb::shaderbundle::InputDataType::kDouble;
125  case spirv_cross::SPIRType::Unknown:
126  case spirv_cross::SPIRType::Void:
127  case spirv_cross::SPIRType::Half:
128  case spirv_cross::SPIRType::AtomicCounter:
129  case spirv_cross::SPIRType::Struct:
130  case spirv_cross::SPIRType::Image:
131  case spirv_cross::SPIRType::SampledImage:
132  case spirv_cross::SPIRType::Sampler:
133  case spirv_cross::SPIRType::AccelerationStructure:
134  case spirv_cross::SPIRType::RayQuery:
135  case spirv_cross::SPIRType::ControlPointArray:
136  case spirv_cross::SPIRType::Interpolant:
137  case spirv_cross::SPIRType::Char:
138  return std::nullopt;
139  }
140  FML_UNREACHABLE();
141 }

References impeller::kFloat.

◆ ToJsonStage()

static std::optional<fb::Stage> impeller::compiler::ToJsonStage ( spv::ExecutionModel  stage)
static

Definition at line 47 of file runtime_stage_data.cc.

47  {
48  switch (stage) {
49  case spv::ExecutionModel::ExecutionModelVertex:
50  return fb::Stage::kVertex;
51  case spv::ExecutionModel::ExecutionModelFragment:
52  return fb::Stage::kFragment;
53  case spv::ExecutionModel::ExecutionModelGLCompute:
54  return fb::Stage::kCompute;
55  default:
56  return std::nullopt;
57  }
58  FML_UNREACHABLE();
59 }

Referenced by impeller::compiler::RuntimeStageData::CreateJsonMapping().

◆ ToJsonType()

static std::optional<uint32_t> impeller::compiler::ToJsonType ( spirv_cross::SPIRType::BaseType  type)
static

Definition at line 138 of file runtime_stage_data.cc.

139  {
140  switch (type) {
141  case spirv_cross::SPIRType::Boolean:
142  return 0; // fb::UniformDataType::kBoolean;
143  case spirv_cross::SPIRType::SByte:
144  return 1; // fb::UniformDataType::kSignedByte;
145  case spirv_cross::SPIRType::UByte:
146  return 2; // fb::UniformDataType::kUnsignedByte;
147  case spirv_cross::SPIRType::Short:
148  return 3; // fb::UniformDataType::kSignedShort;
149  case spirv_cross::SPIRType::UShort:
150  return 4; // fb::UniformDataType::kUnsignedShort;
151  case spirv_cross::SPIRType::Int:
152  return 5; // fb::UniformDataType::kSignedInt;
153  case spirv_cross::SPIRType::UInt:
154  return 6; // fb::UniformDataType::kUnsignedInt;
155  case spirv_cross::SPIRType::Int64:
156  return 7; // fb::UniformDataType::kSignedInt64;
157  case spirv_cross::SPIRType::UInt64:
158  return 8; // fb::UniformDataType::kUnsignedInt64;
159  case spirv_cross::SPIRType::Half:
160  return 9; // b::UniformDataType::kHalfFloat;
161  case spirv_cross::SPIRType::Float:
162  return 10; // fb::UniformDataType::kFloat;
163  case spirv_cross::SPIRType::Double:
164  return 11; // fb::UniformDataType::kDouble;
165  case spirv_cross::SPIRType::SampledImage:
166  return 12; // fb::UniformDataType::kSampledImage;
167  case spirv_cross::SPIRType::Struct:
168  return 13;
169  case spirv_cross::SPIRType::AccelerationStructure:
170  case spirv_cross::SPIRType::AtomicCounter:
171  case spirv_cross::SPIRType::Char:
172  case spirv_cross::SPIRType::ControlPointArray:
173  case spirv_cross::SPIRType::Image:
174  case spirv_cross::SPIRType::Interpolant:
175  case spirv_cross::SPIRType::RayQuery:
176  case spirv_cross::SPIRType::Sampler:
177  case spirv_cross::SPIRType::Unknown:
178  case spirv_cross::SPIRType::Void:
179  return std::nullopt;
180  }
181  FML_UNREACHABLE();
182 }

Referenced by impeller::compiler::RuntimeStageData::CreateJsonMapping().

◆ ToLowerCase()

std::string impeller::compiler::ToLowerCase ( std::string_view  string)

Definition at line 61 of file utilities.cc.

61  {
62  std::string result = std::string(string);
63  std::transform(result.begin(), result.end(), result.begin(),
64  [](char x) { return std::tolower(x); });
65  return result;
66 }
int32_t x

References transform, and x.

Referenced by SourceTypeFromString(), and impeller::compiler::Switches::Switches().

◆ ToShaderCShaderKind()

shaderc_shader_kind impeller::compiler::ToShaderCShaderKind ( SourceType  type)

Definition at line 155 of file types.cc.

155  {
156  switch (type) {
157  case SourceType::kVertexShader:
158  return shaderc_shader_kind::shaderc_vertex_shader;
159  case SourceType::kFragmentShader:
160  return shaderc_shader_kind::shaderc_fragment_shader;
161  case SourceType::kComputeShader:
162  return shaderc_shader_kind::shaderc_compute_shader;
163  case SourceType::kUnknown:
164  break;
165  }
166  return shaderc_shader_kind::shaderc_glsl_infer_from_source;
167 }

References kComputeShader, kFragmentShader, kUnknown, and kVertexShader.

Referenced by impeller::compiler::SPIRVCompiler::CompileToSPV().

◆ ToSourceLanguage()

SourceLanguage impeller::compiler::ToSourceLanguage ( const std::string &  source_language)

Definition at line 52 of file types.cc.

52  {
53  if (source_language == "glsl") {
54  return SourceLanguage::kGLSL;
55  }
56  if (source_language == "hlsl") {
57  return SourceLanguage::kHLSL;
58  }
59  return SourceLanguage::kUnknown;
60 }

References kGLSL, kHLSL, and kUnknown.

Referenced by ParseShaderBundleConfig(), and impeller::compiler::Switches::Switches().

◆ ToStage() [1/2]

static std::optional<fb::Stage> impeller::compiler::ToStage ( spv::ExecutionModel  stage)
static

Definition at line 33 of file runtime_stage_data.cc.

33  {
34  switch (stage) {
35  case spv::ExecutionModel::ExecutionModelVertex:
36  return fb::Stage::kVertex;
37  case spv::ExecutionModel::ExecutionModelFragment:
38  return fb::Stage::kFragment;
39  case spv::ExecutionModel::ExecutionModelGLCompute:
40  return fb::Stage::kCompute;
41  default:
42  return std::nullopt;
43  }
44  FML_UNREACHABLE();
45 }

Referenced by impeller::compiler::ShaderBundleData::CreateFlatbuffer(), and impeller::compiler::RuntimeStageData::CreateStageFlatbuffer().

◆ ToStage() [2/2]

static std::optional<fb::shaderbundle::ShaderStage> impeller::compiler::ToStage ( spv::ExecutionModel  stage)
static

Definition at line 41 of file shader_bundle_data.cc.

42  {
43  switch (stage) {
44  case spv::ExecutionModel::ExecutionModelVertex:
45  return fb::shaderbundle::ShaderStage::kVertex;
46  case spv::ExecutionModel::ExecutionModelFragment:
47  return fb::shaderbundle::ShaderStage::kFragment;
48  case spv::ExecutionModel::ExecutionModelGLCompute:
49  return fb::shaderbundle::ShaderStage::kCompute;
50  default:
51  return std::nullopt;
52  }
53  FML_UNREACHABLE();
54 }

◆ ToString()

static std::string impeller::compiler::ToString ( CompilerBackend::Type  type)
static

Definition at line 652 of file reflector.cc.

652  {
653  switch (type) {
654  case CompilerBackend::Type::kMSL:
655  return "Metal Shading Language";
656  case CompilerBackend::Type::kGLSL:
657  return "OpenGL Shading Language";
658  case CompilerBackend::Type::kGLSLVulkan:
659  return "OpenGL Shading Language (Relaxed Vulkan Semantics)";
660  case CompilerBackend::Type::kSkSL:
661  return "SkSL Shading Language";
662  }
663  FML_UNREACHABLE();
664 }

References impeller::compiler::CompilerBackend::kGLSL, impeller::compiler::CompilerBackend::kGLSLVulkan, impeller::compiler::CompilerBackend::kMSL, and impeller::compiler::CompilerBackend::kSkSL.

◆ ToUniformType() [1/2]

static std::optional<fb::UniformDataType> impeller::compiler::ToUniformType ( spirv_cross::SPIRType::BaseType  type)
static

Definition at line 61 of file runtime_stage_data.cc.

62  {
63  switch (type) {
64  case spirv_cross::SPIRType::Float:
66  case spirv_cross::SPIRType::SampledImage:
68  case spirv_cross::SPIRType::Struct:
70  case spirv_cross::SPIRType::Boolean:
71  case spirv_cross::SPIRType::SByte:
72  case spirv_cross::SPIRType::UByte:
73  case spirv_cross::SPIRType::Short:
74  case spirv_cross::SPIRType::UShort:
75  case spirv_cross::SPIRType::Int:
76  case spirv_cross::SPIRType::UInt:
77  case spirv_cross::SPIRType::Int64:
78  case spirv_cross::SPIRType::UInt64:
79  case spirv_cross::SPIRType::Half:
80  case spirv_cross::SPIRType::Double:
81  case spirv_cross::SPIRType::AccelerationStructure:
82  case spirv_cross::SPIRType::AtomicCounter:
83  case spirv_cross::SPIRType::Char:
84  case spirv_cross::SPIRType::ControlPointArray:
85  case spirv_cross::SPIRType::Image:
86  case spirv_cross::SPIRType::Interpolant:
87  case spirv_cross::SPIRType::RayQuery:
88  case spirv_cross::SPIRType::Sampler:
89  case spirv_cross::SPIRType::Unknown:
90  case spirv_cross::SPIRType::Void:
91  return std::nullopt;
92  }
93  FML_UNREACHABLE();
94 }

References impeller::kFloat, impeller::kSampledImage, and impeller::kStruct.

Referenced by impeller::compiler::ShaderBundleData::CreateFlatbuffer(), and impeller::compiler::RuntimeStageData::CreateStageFlatbuffer().

◆ ToUniformType() [2/2]

static std::optional<fb::shaderbundle::UniformDataType> impeller::compiler::ToUniformType ( spirv_cross::SPIRType::BaseType  type)
static

Definition at line 56 of file shader_bundle_data.cc.

57  {
58  switch (type) {
59  case spirv_cross::SPIRType::Boolean:
60  return fb::shaderbundle::UniformDataType::kBoolean;
61  case spirv_cross::SPIRType::SByte:
62  return fb::shaderbundle::UniformDataType::kSignedByte;
63  case spirv_cross::SPIRType::UByte:
64  return fb::shaderbundle::UniformDataType::kUnsignedByte;
65  case spirv_cross::SPIRType::Short:
66  return fb::shaderbundle::UniformDataType::kSignedShort;
67  case spirv_cross::SPIRType::UShort:
68  return fb::shaderbundle::UniformDataType::kUnsignedShort;
69  case spirv_cross::SPIRType::Int:
70  return fb::shaderbundle::UniformDataType::kSignedInt;
71  case spirv_cross::SPIRType::UInt:
72  return fb::shaderbundle::UniformDataType::kUnsignedInt;
73  case spirv_cross::SPIRType::Int64:
74  return fb::shaderbundle::UniformDataType::kSignedInt64;
75  case spirv_cross::SPIRType::UInt64:
76  return fb::shaderbundle::UniformDataType::kUnsignedInt64;
77  case spirv_cross::SPIRType::Half:
78  return fb::shaderbundle::UniformDataType::kHalfFloat;
79  case spirv_cross::SPIRType::Float:
81  case spirv_cross::SPIRType::Double:
82  return fb::shaderbundle::UniformDataType::kDouble;
83  case spirv_cross::SPIRType::SampledImage:
85  case spirv_cross::SPIRType::AccelerationStructure:
86  case spirv_cross::SPIRType::AtomicCounter:
87  case spirv_cross::SPIRType::Char:
88  case spirv_cross::SPIRType::ControlPointArray:
89  case spirv_cross::SPIRType::Image:
90  case spirv_cross::SPIRType::Interpolant:
91  case spirv_cross::SPIRType::RayQuery:
92  case spirv_cross::SPIRType::Sampler:
93  case spirv_cross::SPIRType::Struct:
94  case spirv_cross::SPIRType::Unknown:
95  case spirv_cross::SPIRType::Void:
96  return std::nullopt;
97  }
98  FML_UNREACHABLE();
99 }

References impeller::kFloat, and impeller::kSampledImage.

◆ TypeNameWithPaddingOfSize()

static std::string impeller::compiler::TypeNameWithPaddingOfSize ( size_t  size)
static

Definition at line 825 of file reflector.cc.

825  {
826  std::stringstream stream;
827  stream << "Padding<" << size << ">";
828  return stream.str();
829 }

Referenced by VertexTypeFromInputResource().

◆ Utf8FromPath()

std::string impeller::compiler::Utf8FromPath ( const std::filesystem::path &  path)

Converts a native format path to a utf8 string.

    This utility uses `path::u8string()` to convert native paths to
    utf8. If the given path doesn't match the underlying native path
    format, and the native path format isn't utf8 (i.e. Windows, which
    has utf16 paths), the path will get mangled. 

Definition at line 30 of file utilities.cc.

30  {
31  return reinterpret_cast<const char*>(path.u8string().c_str());
32 }

Referenced by impeller::compiler::Switches::AreValid(), impeller::compiler::SPIRVCompiler::CompileToSPV(), EntryPointFunctionNameFromSourceName(), GenerateShaderBundle(), InferShaderNameFromPath(), Main(), OutputBundleDepfile(), and impeller::compiler::Switches::Switches().

◆ VertexTypeFromInputResource()

static VertexType impeller::compiler::VertexTypeFromInputResource ( const spirv_cross::Compiler &  compiler,
const spirv_cross::Resource *  resource 
)
static

Definition at line 1377 of file reflector.cc.

1379  {
1380  VertexType result;
1381  result.variable_name = resource->name;
1382  const auto& type = compiler.get_type(resource->type_id);
1383  result.base_type = type.basetype;
1384  const auto total_size = type.columns * type.vecsize * type.width / 8u;
1385  result.byte_length = total_size;
1386 
1387  if (type.basetype == spirv_cross::SPIRType::BaseType::Float &&
1388  type.columns == 1u && type.vecsize == 2u &&
1389  type.width == sizeof(float) * 8u) {
1390  result.type_name = "Point";
1391  } else if (type.basetype == spirv_cross::SPIRType::BaseType::Float &&
1392  type.columns == 1u && type.vecsize == 4u &&
1393  type.width == sizeof(float) * 8u) {
1394  result.type_name = "Vector4";
1395  } else if (type.basetype == spirv_cross::SPIRType::BaseType::Float &&
1396  type.columns == 1u && type.vecsize == 3u &&
1397  type.width == sizeof(float) * 8u) {
1398  result.type_name = "Vector3";
1399  } else if (type.basetype == spirv_cross::SPIRType::BaseType::Float &&
1400  type.columns == 1u && type.vecsize == 1u &&
1401  type.width == sizeof(float) * 8u) {
1402  result.type_name = "Scalar";
1403  } else if (type.basetype == spirv_cross::SPIRType::BaseType::Int &&
1404  type.columns == 1u && type.vecsize == 1u &&
1405  type.width == sizeof(int32_t) * 8u) {
1406  result.type_name = "int32_t";
1407  } else {
1408  // Catch all unknown padding.
1409  result.type_name = TypeNameWithPaddingOfSize(total_size);
1410  }
1411 
1412  return result;
1413 }
static std::string TypeNameWithPaddingOfSize(size_t size)
Definition: reflector.cc:825

References impeller::compiler::VertexType::base_type, impeller::compiler::VertexType::byte_length, impeller::compiler::VertexType::type_name, TypeNameWithPaddingOfSize(), and impeller::compiler::VertexType::variable_name.

Variable Documentation

◆ kEntrypointKey

const char* impeller::compiler::kEntrypointKey = "entrypoint"
static

◆ kExternalTexturePrefix

constexpr char impeller::compiler::kExternalTexturePrefix[] = "SAMPLER_EXTERNAL_OES_"
constexpr

Definition at line 11 of file constants.h.

Referenced by CreateGLSLCompiler().

◆ kFormatVersionKey

const char* impeller::compiler::kFormatVersionKey = "format_version"
static

◆ kKnownPlatforms

const std::map<std::string, TargetPlatform> impeller::compiler::kKnownPlatforms
static
Initial value:
= {
{"metal-desktop", TargetPlatform::kMetalDesktop},
{"metal-ios", TargetPlatform::kMetalIOS},
{"vulkan", TargetPlatform::kVulkan},
{"opengl-es", TargetPlatform::kOpenGLES},
{"opengl-desktop", TargetPlatform::kOpenGLDesktop},
}

Definition at line 20 of file switches.cc.

Referenced by impeller::compiler::Switches::PrintHelp(), and TargetPlatformFromCommandLine().

◆ kKnownRuntimeStages

const std::vector<std::pair<std::string, TargetPlatform> > impeller::compiler::kKnownRuntimeStages
static
Initial value:
= {
{"sksl", TargetPlatform::kSkSL},
{"runtime-stage-metal", TargetPlatform::kRuntimeStageMetal},
{"runtime-stage-gles", TargetPlatform::kRuntimeStageGLES},
{"runtime-stage-gles3", TargetPlatform::kRuntimeStageGLES3},
{"runtime-stage-vulkan", TargetPlatform::kRuntimeStageVulkan},
}

Definition at line 29 of file switches.cc.

Referenced by impeller::compiler::Switches::PrintHelp(), and RuntimeStagesFromCommandLine().

◆ kKnownSourceTypes

const std::map<std::string, SourceType> impeller::compiler::kKnownSourceTypes
static
Initial value:
= {
{"vert", SourceType::kVertexShader},
{"frag", SourceType::kFragmentShader},
{"comp", SourceType::kComputeShader},
}

Definition at line 37 of file switches.cc.

Referenced by impeller::compiler::Switches::PrintHelp(), and SourceTypeFromCommandLine().

◆ kMaxUniformBufferSize

const uint32_t impeller::compiler::kMaxUniformBufferSize = 6208
static

Definition at line 103 of file compiler.cc.

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

◆ kReflectionCCTemplate

constexpr std::string_view impeller::compiler::kReflectionCCTemplate
constexpr

Definition at line 209 of file code_gen_template.h.

◆ kReflectionHeaderTemplate

constexpr std::string_view impeller::compiler::kReflectionHeaderTemplate
constexpr

Definition at line 10 of file code_gen_template.h.

◆ kShaderKey

const char* impeller::compiler::kShaderKey = "shader"
static

◆ kStageKey

const char* impeller::compiler::kStageKey = "stage"
static

◆ kTargetPlatformKey

const char* impeller::compiler::kTargetPlatformKey = "target_platform"
static

Definition at line 186 of file runtime_stage_data.cc.

◆ kUniformArrayElementsKey

const char* impeller::compiler::kUniformArrayElementsKey = "array_elements"
static

◆ kUniformBitWidthKey

const char* impeller::compiler::kUniformBitWidthKey = "bit_width"
static

◆ kUniformColumnsKey

const char* impeller::compiler::kUniformColumnsKey = "columns"
static

◆ kUniformLocationKey

const char* impeller::compiler::kUniformLocationKey = "location"
static

◆ kUniformNameKey

const char* impeller::compiler::kUniformNameKey = "name"
static

◆ kUniformRowsKey

const char* impeller::compiler::kUniformRowsKey = "rows"
static

◆ kUniformsKey

const char* impeller::compiler::kUniformsKey = "uniforms"
static

◆ kUniformStructElementsKey

const char* impeller::compiler::kUniformStructElementsKey = "struct_elements"
static

Definition at line 197 of file runtime_stage_data.cc.

◆ kUniformTypeKey

const char* impeller::compiler::kUniformTypeKey = "type"
static