Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
impeller::compiler Namespace Reference

Namespaces

namespace  testing
 

Classes

class  AutoLogger
 
class  Compiler
 
struct  CompilerBackend
 
class  CompilerSkSL
 
struct  IncludeDir
 
class  Includer
 
struct  IncluderData
 
struct  InputDescription
 
struct  KnownType
 
class  Reflector
 
class  RuntimeStageData
 
class  ShaderBundleData
 
struct  ShaderConfig
 A shader config parsed as part of a ShaderBundleConfig. More...
 
struct  SourceOptions
 
class  SPIRVCompiler
 
struct  SPIRVCompilerOptions
 
struct  SPIRVCompilerSourceProfile
 
struct  SPIRVCompilerTargetEnv
 
struct  StructMember
 
class  Switches
 
struct  UniformDescription
 
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  VertexType
 

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)
 
static Reflector::Options CreateReflectorOptions (const SourceOptions &options, const Switches &switches)
 
static std::shared_ptr< RuntimeStageData::ShaderCompileSkSL (std::shared_ptr< fml::Mapping > source_file_mapping, const Switches &switches)
 
static bool OutputIPLR (const Switches &switches, const std::shared_ptr< fml::Mapping > &source_file_mapping)
 
static bool OutputSLFile (const Compiler &compiler, const Switches &switches)
 
static bool OutputReflectionData (const Compiler &compiler, const Switches &switches, const SourceOptions &options)
 
static bool OutputDepfile (const Compiler &compiler, const Switches &switches)
 
bool Main (const fml::CommandLine &command_line)
 
static std::string ExecutionModelToString (spv::ExecutionModel model)
 
static std::string StringToShaderStage (const std::string &str)
 
static std::optional< RuntimeStageBackendGetRuntimeStageBackend (TargetPlatform target_platform)
 
static std::string ToString (CompilerBackend::Type type)
 
static std::string TypeNameWithPaddingOfSize (size_t size)
 
static std::optional< KnownTypeReadKnownScalarType (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.
 
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< ShaderBundleConfigParseShaderBundleConfig (const std::string &bundle_config_json, std::ostream &error_stream)
 Parse a shader bundle configuration from a given JSON string.
 
static std::unique_ptr< fb::shaderbundle::BackendShaderT > GenerateShaderBackendFB (TargetPlatform target_platform, SourceOptions &options, const std::string &shader_name, const ShaderConfig &shader_config)
 
static std::unique_ptr< fb::shaderbundle::ShaderT > GenerateShaderFB (SourceOptions options, const std::string &shader_name, const ShaderConfig &shader_config)
 
std::optional< fb::shaderbundle::ShaderBundleT > GenerateShaderBundleFlatbuffer (const std::string &bundle_config_json, const SourceOptions &options)
 Parses the JSON shader bundle configuration and invokes the compiler multiple times to produce a shader bundle flatbuffer.
 
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.
 
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< TargetPlatformRuntimeStagesFromCommandLine (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)
 
bool TargetPlatformNeedsReflection (TargetPlatform platform)
 
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 TargetPlatformBundlesSkSL (TargetPlatform platform)
 
bool SetPermissiveAccess (const std::filesystem::path &p)
 Sets the file access mode of the file at path 'p' to 0644.
 
std::string Utf8FromPath (const std::filesystem::path &path)
 Converts a native format path to a utf8 string.
 
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.
 
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 std::map< std::string, TargetPlatformkKnownPlatforms
 
static const std::map< std::string, TargetPlatformkKnownRuntimeStages
 
static const std::map< std::string, SourceTypekKnownSourceTypes
 

Typedef Documentation

◆ ShaderBundleConfig

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

Definition at line 83 of file types.h.

Enumeration Type Documentation

◆ SourceLanguage

Enumerator
kUnknown 
kGLSL 
kHLSL 

Definition at line 43 of file types.h.

◆ SourceType

enum class impeller::compiler::SourceType
strong
Enumerator
kUnknown 
kVertexShader 
kFragmentShader 
kComputeShader 

Definition at line 22 of file types.h.

◆ TargetPlatform

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

Definition at line 29 of file types.h.

Function Documentation

◆ CompileSkSL()

static std::shared_ptr< RuntimeStageData::Shader > impeller::compiler::CompileSkSL ( std::shared_ptr< fml::Mapping source_file_mapping,
const Switches switches 
)
static

Run the shader compiler to geneate SkSL reflection data. If there is an error, prints error text and returns nullptr.

Definition at line 37 of file impellerc_main.cc.

39 {
40 auto options = switches.CreateSourceOptions(TargetPlatform::kSkSL);
41
42 Reflector::Options sksl_reflector_options =
43 CreateReflectorOptions(options, switches);
44 sksl_reflector_options.target_platform = TargetPlatform::kSkSL;
45
46 Compiler sksl_compiler =
47 Compiler(std::move(source_file_mapping), options, sksl_reflector_options);
48 if (!sksl_compiler.IsValid()) {
49 std::cerr << "Compilation to SkSL failed." << std::endl;
50 std::cerr << sksl_compiler.GetErrorMessages() << std::endl;
51 return nullptr;
52 }
53 return sksl_compiler.GetReflector()->GetRuntimeStageShaderData();
54}
const Reflector * GetReflector() const
Definition compiler.cc:547
std::string GetErrorMessages() const
Definition compiler.cc:504
std::shared_ptr< RuntimeStageData::Shader > GetRuntimeStageShaderData() const
Definition reflector.cc:130
SourceOptions CreateSourceOptions(std::optional< TargetPlatform > target_platform=std::nullopt) const
Definition switches.cc:320
static Reflector::Options CreateReflectorOptions(const SourceOptions &options, const Switches &switches)

References CreateReflectorOptions(), impeller::compiler::Switches::CreateSourceOptions(), impeller::compiler::Compiler::GetErrorMessages(), impeller::compiler::Compiler::GetReflector(), impeller::compiler::Reflector::GetRuntimeStageShaderData(), impeller::compiler::Compiler::IsValid(), kSkSL, and impeller::compiler::Reflector::Options::target_platform.

Referenced by OutputIPLR().

◆ 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}
size_t length
std::shared_ptr< const fml::Mapping > data

References data, i, and length.

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

231 {
232 CompilerBackend compiler;
233 switch (source_options.target_platform) {
234 case TargetPlatform::kMetalDesktop:
235 case TargetPlatform::kMetalIOS:
236 case TargetPlatform::kRuntimeStageMetal:
237 compiler = CreateMSLCompiler(ir, source_options);
238 break;
239 case TargetPlatform::kVulkan:
240 case TargetPlatform::kRuntimeStageVulkan:
241 compiler = CreateVulkanCompiler(ir, source_options);
242 break;
243 case TargetPlatform::kUnknown:
244 case TargetPlatform::kOpenGLES:
245 case TargetPlatform::kOpenGLDesktop:
246 case TargetPlatform::kRuntimeStageGLES:
247 case TargetPlatform::kRuntimeStageGLES3:
248 compiler = CreateGLSLCompiler(ir, source_options);
249 break;
250 case TargetPlatform::kSkSL:
251 compiler = CreateSkSLCompiler(ir, source_options);
252 }
253 if (!compiler) {
254 return {};
255 }
256 auto* backend = compiler.GetCompiler();
257 if (!EntryPointMustBeNamedMain(source_options.target_platform) &&
258 source_options.source_language == SourceLanguage::kGLSL) {
259 backend->rename_entry_point("main", source_options.entry_point_name,
260 ToExecutionModel(source_options.type));
261 }
262 return compiler;
263}
static CompilerBackend CreateSkSLCompiler(const spirv_cross::ParsedIR &ir, const SourceOptions &source_options)
Definition compiler.cc:204
static CompilerBackend CreateVulkanCompiler(const spirv_cross::ParsedIR &ir, const SourceOptions &source_options)
Definition compiler.cc:134
static bool EntryPointMustBeNamedMain(TargetPlatform platform)
Definition compiler.cc:210
static CompilerBackend CreateMSLCompiler(const spirv_cross::ParsedIR &ir, const SourceOptions &source_options, std::optional< uint32_t > msl_version_override={})
Definition compiler.cc:63
static CompilerBackend CreateGLSLCompiler(const spirv_cross::ParsedIR &ir, const SourceOptions &source_options)
Definition compiler.cc:146
spv::ExecutionModel ToExecutionModel(SourceType type)
Definition types.cc:188
spirv_cross::Compiler * GetCompiler()

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().

◆ CreateGLSLCompiler()

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

Definition at line 146 of file compiler.cc.

147 {
148 auto gl_compiler = std::make_shared<spirv_cross::CompilerGLSL>(ir);
149
150 // Walk the variables and insert the external image extension if any of them
151 // begins with the external texture prefix. Unfortunately, we can't walk
152 // `gl_compiler->get_shader_resources().separate_samplers` until the compiler
153 // is further along.
154 //
155 // Unfortunately, we can't just let the shader author add this extension and
156 // use `samplerExternalOES` directly because compiling to spirv requires the
157 // source language profile to be at least 310 ES, but this extension is
158 // incompatible with ES 310+.
159 for (auto& id : ir.ids_for_constant_or_variable) {
160 if (StringStartsWith(ir.get_name(id), kExternalTexturePrefix)) {
161 if (source_options.gles_language_version >= 300) {
162 gl_compiler->require_extension(kEGLImageExternalExtension300);
163 } else {
164 gl_compiler->require_extension(kEGLImageExternalExtension);
165 }
166 break;
167 }
168 }
169
170 spirv_cross::CompilerGLSL::Options sl_options;
171 sl_options.force_zero_initialized_variables = true;
172 sl_options.vertex.fixup_clipspace = true;
173 if (source_options.target_platform == TargetPlatform::kOpenGLES ||
174 source_options.target_platform == TargetPlatform::kRuntimeStageGLES ||
175 source_options.target_platform == TargetPlatform::kRuntimeStageGLES3) {
176 sl_options.version = source_options.gles_language_version > 0
177 ? source_options.gles_language_version
178 : 100;
179 sl_options.es = true;
180 if (source_options.target_platform == TargetPlatform::kRuntimeStageGLES3) {
181 sl_options.version = 300;
182 }
183 if (source_options.require_framebuffer_fetch &&
184 source_options.type == SourceType::kFragmentShader) {
185 gl_compiler->remap_ext_framebuffer_fetch(0, 0, true);
186 }
187 gl_compiler->set_variable_type_remap_callback(
188 [&](const spirv_cross::SPIRType& type, const std::string& var_name,
189 std::string& name_of_type) {
190 if (StringStartsWith(var_name, kExternalTexturePrefix)) {
191 name_of_type = "samplerExternalOES";
192 }
193 });
194 } else {
195 sl_options.version = source_options.gles_language_version > 0
196 ? source_options.gles_language_version
197 : 120;
198 sl_options.es = false;
199 }
200 gl_compiler->set_common_options(sl_options);
201 return CompilerBackend(gl_compiler);
202}
GLenum type
bool StringStartsWith(const std::string &target, const std::string &prefix)
Definition utilities.cc:86
bool require_framebuffer_fetch
Whether the GLSL framebuffer fetch extension will be required.

References impeller::compiler::SourceOptions::gles_language_version, kExternalTexturePrefix, kFragmentShader, kOpenGLES, kRuntimeStageGLES, kRuntimeStageGLES3, impeller::compiler::SourceOptions::require_framebuffer_fetch, StringStartsWith(), impeller::compiler::SourceOptions::target_platform, impeller::compiler::SourceOptions::type, and 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 63 of file compiler.cc.

66 {}) {
67 auto sl_compiler = std::make_shared<spirv_cross::CompilerMSL>(ir);
68 spirv_cross::CompilerMSL::Options sl_options;
69 sl_options.platform =
70 TargetPlatformToMSLPlatform(source_options.target_platform);
71 sl_options.msl_version = msl_version_override.value_or(
72 ParseMSLVersion(source_options.metal_version));
73 sl_options.ios_use_simdgroup_functions =
74 sl_options.is_ios() &&
75 sl_options.msl_version >=
76 spirv_cross::CompilerMSL::Options::make_msl_version(2, 4, 0);
77 sl_options.use_framebuffer_fetch_subpasses = true;
78 sl_compiler->set_msl_options(sl_options);
79
80 // Sort the float and sampler uniforms according to their declared/decorated
81 // order. For user authored fragment shaders, the API for setting uniform
82 // values uses the index of the uniform in the declared order. By default, the
83 // metal backend of spirv-cross will order uniforms according to usage. To fix
84 // this, we use the sorted order and the add_msl_resource_binding API to force
85 // the ordering to match the declared order. Note that while this code runs
86 // for all compiled shaders, it will only affect vertex and fragment shaders
87 // due to the specified stage.
88 auto floats =
89 SortUniforms(&ir, sl_compiler.get(), spirv_cross::SPIRType::Float);
90 auto images =
91 SortUniforms(&ir, sl_compiler.get(), spirv_cross::SPIRType::SampledImage);
92
93 spv::ExecutionModel execution_model =
94 spv::ExecutionModel::ExecutionModelFragment;
95 if (source_options.type == SourceType::kVertexShader) {
96 execution_model = spv::ExecutionModel::ExecutionModelVertex;
97 }
98
99 uint32_t buffer_offset = 0;
100 uint32_t sampler_offset = 0;
101 for (auto& float_id : floats) {
102 sl_compiler->add_msl_resource_binding(
103 {.stage = execution_model,
104 .basetype = spirv_cross::SPIRType::BaseType::Float,
105 .desc_set = sl_compiler->get_decoration(float_id,
106 spv::DecorationDescriptorSet),
107 .binding =
108 sl_compiler->get_decoration(float_id, spv::DecorationBinding),
109 .count = 1u,
110 .msl_buffer = buffer_offset});
111 buffer_offset++;
112 }
113 for (auto& image_id : images) {
114 sl_compiler->add_msl_resource_binding({
115 .stage = execution_model,
116 .basetype = spirv_cross::SPIRType::BaseType::SampledImage,
117 .desc_set =
118 sl_compiler->get_decoration(image_id, spv::DecorationDescriptorSet),
119 .binding =
120 sl_compiler->get_decoration(image_id, spv::DecorationBinding),
121 .count = 1u,
122 // A sampled image is both an image and a sampler, so both
123 // offsets need to be set or depending on the partiular shader
124 // the bindings may be incorrect.
125 .msl_texture = sampler_offset,
126 .msl_sampler = sampler_offset,
127 });
128 sampler_offset++;
129 }
130
131 return CompilerBackend(sl_compiler);
132}
std::array< MockImage, 3 > images
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().

◆ CreateReflectorOptions()

static Reflector::Options impeller::compiler::CreateReflectorOptions ( const SourceOptions options,
const Switches switches 
)
static

Definition at line 23 of file impellerc_main.cc.

24 {
25 Reflector::Options reflector_options;
26 reflector_options.target_platform = options.target_platform;
27 reflector_options.entry_point_name = options.entry_point_name;
28 reflector_options.shader_name =
30 reflector_options.header_file_name =
31 Utf8FromPath(switches.reflection_header_name.filename());
32 return reflector_options;
33}
std::filesystem::path source_file_name
Definition switches.h:26
std::filesystem::path reflection_header_name
Definition switches.h:36
std::string InferShaderNameFromPath(const std::filesystem::path &path)
Definition utilities.cc:34
std::string Utf8FromPath(const std::filesystem::path &path)
Converts a native format path to a utf8 string.
Definition utilities.cc:30

References impeller::compiler::Reflector::Options::entry_point_name, impeller::compiler::SourceOptions::entry_point_name, impeller::compiler::Reflector::Options::header_file_name, InferShaderNameFromPath(), impeller::compiler::Switches::reflection_header_name, impeller::compiler::Reflector::Options::shader_name, impeller::compiler::Switches::source_file_name, impeller::compiler::Reflector::Options::target_platform, impeller::compiler::SourceOptions::target_platform, and Utf8FromPath().

Referenced by CompileSkSL(), Main(), and OutputIPLR().

◆ CreateSkSLCompiler()

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

Definition at line 204 of file compiler.cc.

205 {
206 auto sksl_compiler = std::make_shared<CompilerSkSL>(ir);
207 return CompilerBackend(sksl_compiler);
208}

Referenced by CreateCompiler().

◆ CreateVulkanCompiler()

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

Definition at line 134 of file compiler.cc.

136 {
137 auto gl_compiler = std::make_shared<spirv_cross::CompilerGLSL>(ir);
138 spirv_cross::CompilerGLSL::Options sl_options;
139 sl_options.force_zero_initialized_variables = true;
140 sl_options.vertex.fixup_clipspace = true;
141 sl_options.vulkan_semantics = true;
142 gl_compiler->set_common_options(sl_options);
143 return CompilerBackend(gl_compiler);
144}

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}

References ConvertToEntrypointName(), kComputeShader, kFragmentShader, kHLSL, kUnknown, kVertexShader, type, 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 210 of file compiler.cc.

210 {
211 switch (platform) {
212 case TargetPlatform::kUnknown:
214 case TargetPlatform::kMetalDesktop:
215 case TargetPlatform::kMetalIOS:
216 case TargetPlatform::kVulkan:
217 case TargetPlatform::kRuntimeStageMetal:
218 case TargetPlatform::kRuntimeStageVulkan:
219 return false;
220 case TargetPlatform::kSkSL:
221 case TargetPlatform::kOpenGLES:
222 case TargetPlatform::kOpenGLDesktop:
223 case TargetPlatform::kRuntimeStageGLES:
224 case TargetPlatform::kRuntimeStageGLES3:
225 return true;
226 }
228}
#define FML_UNREACHABLE()
Definition logging.h:128

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

Referenced by CreateCompiler().

◆ ExecutionModelToString()

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

Definition at line 34 of file reflector.cc.

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

◆ 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 
)
static

Override options.

Definition at line 85 of file shader_bundle.cc.

88 {
89 auto result = std::make_unique<fb::shaderbundle::BackendShaderT>();
90
91 std::shared_ptr<fml::FileMapping> source_file_mapping =
93 if (!source_file_mapping) {
94 std::cerr << "Could not open file for bundled shader \"" << shader_name
95 << "\"." << std::endl;
96 return nullptr;
97 }
98
99 /// Override options.
100 options.target_platform = target_platform;
101 options.file_name = shader_name; // This is just used for error messages.
102 options.type = shader_config.type;
103 options.source_language = shader_config.language;
104 options.entry_point_name = EntryPointFunctionNameFromSourceName(
105 shader_config.source_file_name, options.type, options.source_language,
106 shader_config.entry_point);
107
108 Reflector::Options reflector_options;
109 reflector_options.target_platform = options.target_platform;
110 reflector_options.entry_point_name = options.entry_point_name;
111 reflector_options.shader_name = shader_name;
112
113 Compiler compiler(source_file_mapping, options, reflector_options);
114 if (!compiler.IsValid()) {
115 std::cerr << "Compilation failed for bundled shader \"" << shader_name
116 << "\"." << std::endl;
117 std::cerr << compiler.GetErrorMessages() << std::endl;
118 return nullptr;
119 }
120
121 auto reflector = compiler.GetReflector();
122 if (reflector == nullptr) {
123 std::cerr << "Could not create reflector for bundled shader \""
124 << shader_name << "\"." << std::endl;
125 return nullptr;
126 }
127
128 auto bundle_data = reflector->GetShaderBundleData();
129 if (!bundle_data) {
130 std::cerr << "Bundled shader information was nil for \"" << shader_name
131 << "\"." << std::endl;
132 return nullptr;
133 }
134
135 result = bundle_data->CreateFlatbuffer();
136 if (!result) {
137 std::cerr << "Failed to create flatbuffer for bundled shader \""
138 << shader_name << "\"." << std::endl;
139 return nullptr;
140 }
141
142 return result;
143}
static std::unique_ptr< FileMapping > CreateReadOnly(const std::string &path)
Definition mapping.cc:20
std::filesystem::path file_name

References fml::FileMapping::CreateReadOnly(), 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::GetReflector(), impeller::compiler::Reflector::GetShaderBundleData(), 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.
  1. Serialize the shader bundle and write to disk.

Definition at line 212 of file shader_bundle.cc.

212 {
213 // --------------------------------------------------------------------------
214 /// 1. Parse the shader bundle and generate the flatbuffer result.
215 ///
216
217 auto shader_bundle = GenerateShaderBundleFlatbuffer(
218 switches.shader_bundle, switches.CreateSourceOptions());
219 if (!shader_bundle.has_value()) {
220 // Specific error messages are already handled by
221 // GenerateShaderBundleFlatbuffer.
222 return false;
223 }
224
225 // --------------------------------------------------------------------------
226 /// 2. Serialize the shader bundle and write to disk.
227 ///
228
229 auto builder = std::make_shared<flatbuffers::FlatBufferBuilder>();
230 builder->Finish(fb::shaderbundle::ShaderBundle::Pack(*builder.get(),
231 &shader_bundle.value()),
232 fb::shaderbundle::ShaderBundleIdentifier());
233 auto mapping = std::make_shared<fml::NonOwnedMapping>(
234 builder->GetBufferPointer(), builder->GetSize(),
235 [builder](auto, auto) {});
236
237 auto sl_file_name = std::filesystem::absolute(
238 std::filesystem::current_path() / switches.sl_file_name);
239
240 if (!fml::WriteAtomically(*switches.working_directory, //
241 Utf8FromPath(sl_file_name).c_str(), //
242 *mapping //
243 )) {
244 std::cerr << "Could not write file to " << switches.sl_file_name
245 << std::endl;
246 return false;
247 }
248 // Tools that consume the runtime stage data expect the access mode to
249 // be 0644.
250 if (!SetPermissiveAccess(sl_file_name)) {
251 return false;
252 }
253
254 return true;
255}
std::filesystem::path sl_file_name
Definition switches.h:31
std::shared_ptr< fml::UniqueFD > working_directory
Definition switches.h:24
bool WriteAtomically(const fml::UniqueFD &base_directory, const char *file_name, const Mapping &mapping)
std::optional< fb::shaderbundle::ShaderBundleT > GenerateShaderBundleFlatbuffer(const std::string &bundle_config_json, const SourceOptions &options)
Parses the JSON shader bundle configuration and invokes the compiler multiple times to produce a shad...

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

Referenced by Main().

◆ GenerateShaderBundleFlatbuffer()

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

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.
  1. Parse the bundle configuration.
  1. Build the deserialized shader bundle.

Definition at line 179 of file shader_bundle.cc.

181 {
182 // --------------------------------------------------------------------------
183 /// 1. Parse the bundle configuration.
184 ///
185
186 std::optional<ShaderBundleConfig> bundle_config =
187 ParseShaderBundleConfig(bundle_config_json, std::cerr);
188 if (!bundle_config) {
189 return std::nullopt;
190 }
191
192 // --------------------------------------------------------------------------
193 /// 2. Build the deserialized shader bundle.
194 ///
195
196 fb::shaderbundle::ShaderBundleT shader_bundle;
197 shader_bundle.format_version = static_cast<uint32_t>(
198 fb::shaderbundle::ShaderBundleFormatVersion::kVersion);
199
200 for (const auto& [shader_name, shader_config] : bundle_config.value()) {
201 std::unique_ptr<fb::shaderbundle::ShaderT> shader =
202 GenerateShaderFB(options, shader_name, shader_config);
203 if (!shader) {
204 return std::nullopt;
205 }
206 shader_bundle.shaders.push_back(std::move(shader));
207 }
208
209 return shader_bundle;
210}
int32_t value
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 
)
static

Definition at line 145 of file shader_bundle.cc.

148 {
149 auto result = std::make_unique<fb::shaderbundle::ShaderT>();
150 result->name = shader_name;
151 result->metal_ios = GenerateShaderBackendFB(
152 TargetPlatform::kMetalIOS, options, shader_name, shader_config);
153 if (!result->metal_ios) {
154 return nullptr;
155 }
156 result->metal_desktop = GenerateShaderBackendFB(
157 TargetPlatform::kMetalDesktop, options, shader_name, shader_config);
158 if (!result->metal_desktop) {
159 return nullptr;
160 }
161 result->opengl_es = GenerateShaderBackendFB(
162 TargetPlatform::kOpenGLES, options, shader_name, shader_config);
163 if (!result->opengl_es) {
164 return nullptr;
165 }
166 result->opengl_desktop = GenerateShaderBackendFB(
167 TargetPlatform::kOpenGLDesktop, options, shader_name, shader_config);
168 if (!result->opengl_desktop) {
169 return nullptr;
170 }
171 result->vulkan = GenerateShaderBackendFB(TargetPlatform::kVulkan, options,
172 shader_name, shader_config);
173 if (!result->vulkan) {
174 return nullptr;
175 }
176 return result;
177}
static std::unique_ptr< fb::shaderbundle::BackendShaderT > GenerateShaderBackendFB(TargetPlatform target_platform, SourceOptions &options, const std::string &shader_name, const ShaderConfig &shader_config)

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 165 of file switches.cc.

167 {
168 std::string value = command_line.GetOptionValueWithDefault(arg, "");
169 return std::filesystem::path(std::u8string(value.begin(), value.end()));
170}
std::string GetOptionValueWithDefault(std::string_view name, std::string_view default_value) const

References fml::CommandLine::GetOptionValueWithDefault(), and 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 816 of file reflector.cc.

816 {
817 auto struct_size = 0u;
818 for (const auto& member : members) {
819 struct_size += member.byte_length;
820 }
821 return struct_size;
822}

◆ GetRuntimeStageBackend()

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

Definition at line 316 of file reflector.cc.

317 {
318 switch (target_platform) {
319 case TargetPlatform::kUnknown:
320 case TargetPlatform::kMetalDesktop:
321 case TargetPlatform::kMetalIOS:
322 case TargetPlatform::kOpenGLES:
323 case TargetPlatform::kOpenGLDesktop:
324 case TargetPlatform::kVulkan:
325 return std::nullopt;
326 case TargetPlatform::kRuntimeStageMetal:
327 return RuntimeStageBackend::kMetal;
328 case TargetPlatform::kRuntimeStageGLES:
329 return RuntimeStageBackend::kOpenGLES;
330 case TargetPlatform::kRuntimeStageGLES3:
331 return RuntimeStageBackend::kOpenGLES3;
332 case TargetPlatform::kRuntimeStageVulkan:
333 return RuntimeStageBackend::kVulkan;
334 case TargetPlatform::kSkSL:
335 return RuntimeStageBackend::kSkSL;
336 }
338}

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

◆ 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().

Referenced by CreateReflectorOptions().

◆ JoinStrings()

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

Definition at line 512 of file compiler.cc.

513 {
514 std::stringstream stream;
515 for (size_t i = 0, count = items.size(); i < count; i++) {
516 const auto is_last = (i == count - 1);
517
518 stream << items[i];
519 if (!is_last) {
520 stream << separator;
521 }
522 }
523 return stream.str();
524}

References i.

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

◆ Main()

bool impeller::compiler::Main ( const fml::CommandLine command_line)

Definition at line 232 of file impellerc_main.cc.

232 {
234 if (command_line.HasOption("help")) {
235 Switches::PrintHelp(std::cout);
236 return true;
237 }
238
239 Switches switches(command_line);
240 if (!switches.AreValid(std::cerr)) {
241 std::cerr << "Invalid flags specified." << std::endl;
242 Switches::PrintHelp(std::cerr);
243 return false;
244 }
245
246 if (!switches.shader_bundle.empty()) {
247 // Invoke the compiler multiple times to build a shader bundle with the
248 // given shader_bundle spec.
249 return GenerateShaderBundle(switches);
250 }
251
252 std::shared_ptr<fml::FileMapping> source_file_mapping =
253 fml::FileMapping::CreateReadOnly(Utf8FromPath(switches.source_file_name));
254 if (!source_file_mapping) {
255 std::cerr << "Could not open input file." << std::endl;
256 return false;
257 }
258
259 if (switches.iplr && !OutputIPLR(switches, source_file_mapping)) {
260 return false;
261 }
262
263 // Create at least one compiler to output the SL file, reflection data, and a
264 // depfile.
265
266 SourceOptions options = switches.CreateSourceOptions();
267
268 // Invoke the compiler and generate reflection data for a single shader.
269
270 Reflector::Options reflector_options =
271 CreateReflectorOptions(options, switches);
272
273 Compiler compiler(source_file_mapping, options, reflector_options);
274 if (!compiler.IsValid()) {
275 std::cerr << "Compilation failed." << std::endl;
276 std::cerr << compiler.GetErrorMessages() << std::endl;
277 return false;
278 }
279
280 auto spriv_file_name = std::filesystem::absolute(
281 std::filesystem::current_path() / switches.spirv_file_name);
282 if (!fml::WriteAtomically(*switches.working_directory,
283 Utf8FromPath(spriv_file_name).c_str(),
284 *compiler.GetSPIRVAssembly())) {
285 std::cerr << "Could not write file to " << switches.spirv_file_name
286 << std::endl;
287 return false;
288 }
289
290 if (!switches.iplr && !OutputSLFile(compiler, switches)) {
291 return false;
292 }
293
294 if (!OutputReflectionData(compiler, switches, options)) {
295 return false;
296 }
297
298 if (!OutputDepfile(compiler, switches)) {
299 return false;
300 }
301
302 return true;
303}
bool HasOption(std::string_view name, size_t *index=nullptr) const
void InstallCrashHandler()
Definition backtrace.cc:126
static bool OutputDepfile(const Compiler &compiler, const Switches &switches)
static bool OutputReflectionData(const Compiler &compiler, const Switches &switches, const SourceOptions &options)
static bool OutputIPLR(const Switches &switches, const std::shared_ptr< fml::Mapping > &source_file_mapping)
static bool OutputSLFile(const Compiler &compiler, const Switches &switches)

References impeller::compiler::Switches::AreValid(), fml::FileMapping::CreateReadOnly(), CreateReflectorOptions(), impeller::compiler::Switches::CreateSourceOptions(), GenerateShaderBundle(), impeller::compiler::Compiler::GetErrorMessages(), impeller::compiler::Compiler::GetSPIRVAssembly(), fml::CommandLine::HasOption(), fml::InstallCrashHandler(), impeller::compiler::Switches::iplr, impeller::compiler::Compiler::IsValid(), OutputDepfile(), OutputIPLR(), OutputReflectionData(), OutputSLFile(), impeller::compiler::Switches::PrintHelp(), impeller::compiler::Switches::shader_bundle, impeller::compiler::Switches::source_file_name, impeller::compiler::Switches::spirv_file_name, Utf8FromPath(), impeller::compiler::Switches::working_directory, and fml::WriteAtomically().

Referenced by main().

◆ OutputDepfile()

static bool impeller::compiler::OutputDepfile ( const Compiler compiler,
const Switches switches 
)
static
  1. Output a depfile.

Definition at line 194 of file impellerc_main.cc.

194 {
195 // --------------------------------------------------------------------------
196 /// 4. Output a depfile.
197 ///
198
199 if (!switches.depfile_path.empty()) {
200 std::string result_file;
201 switch (switches.SelectDefaultTargetPlatform()) {
202 case TargetPlatform::kMetalDesktop:
203 case TargetPlatform::kMetalIOS:
204 case TargetPlatform::kOpenGLES:
205 case TargetPlatform::kOpenGLDesktop:
206 case TargetPlatform::kRuntimeStageMetal:
207 case TargetPlatform::kRuntimeStageGLES:
208 case TargetPlatform::kRuntimeStageGLES3:
209 case TargetPlatform::kRuntimeStageVulkan:
210 case TargetPlatform::kSkSL:
211 case TargetPlatform::kVulkan:
212 result_file = Utf8FromPath(switches.sl_file_name);
213 break;
214 case TargetPlatform::kUnknown:
215 result_file = Utf8FromPath(switches.spirv_file_name);
216 break;
217 }
218 auto depfile_path = std::filesystem::absolute(
219 std::filesystem::current_path() / switches.depfile_path);
221 Utf8FromPath(depfile_path).c_str(),
222 *compiler.CreateDepfileContents({result_file}))) {
223 std::cerr << "Could not write depfile to " << switches.depfile_path
224 << std::endl;
225 return false;
226 }
227 }
228
229 return true;
230}
std::unique_ptr< fml::Mapping > CreateDepfileContents(std::initializer_list< std::string > targets) const
Definition compiler.cc:532
std::filesystem::path spirv_file_name
Definition switches.h:34
std::filesystem::path depfile_path
Definition switches.h:38
TargetPlatform SelectDefaultTargetPlatform() const
Definition switches.cc:312

References impeller::compiler::Compiler::CreateDepfileContents(), impeller::compiler::Switches::depfile_path, kMetalDesktop, kMetalIOS, kOpenGLDesktop, kOpenGLES, kRuntimeStageGLES, kRuntimeStageGLES3, kRuntimeStageMetal, kRuntimeStageVulkan, kSkSL, kUnknown, kVulkan, impeller::compiler::Switches::SelectDefaultTargetPlatform(), impeller::compiler::Switches::sl_file_name, impeller::compiler::Switches::spirv_file_name, Utf8FromPath(), impeller::compiler::Switches::working_directory, and fml::WriteAtomically().

Referenced by Main().

◆ OutputIPLR()

static bool impeller::compiler::OutputIPLR ( const Switches switches,
const std::shared_ptr< fml::Mapping > &  source_file_mapping 
)
static

Definition at line 56 of file impellerc_main.cc.

58 {
59 FML_DCHECK(switches.iplr);
60
61 RuntimeStageData stages;
62 std::shared_ptr<RuntimeStageData::Shader> sksl_shader;
64 sksl_shader = CompileSkSL(source_file_mapping, switches);
65 if (!sksl_shader) {
66 return false;
67 }
68 stages.AddShader(sksl_shader);
69 }
70
71 for (const auto& platform : switches.PlatformsToCompile()) {
72 if (platform == TargetPlatform::kSkSL) {
73 // Already handled above.
74 continue;
75 }
76 SourceOptions options = switches.CreateSourceOptions(platform);
77
78 // Invoke the compiler and generate reflection data for a single shader.
79
80 Reflector::Options reflector_options =
81 CreateReflectorOptions(options, switches);
82 Compiler compiler(source_file_mapping, options, reflector_options);
83 if (!compiler.IsValid()) {
84 std::cerr << "Compilation failed." << std::endl;
85 std::cerr << compiler.GetErrorMessages() << std::endl;
86 return false;
87 }
88
89 auto reflector = compiler.GetReflector();
90 if (reflector == nullptr) {
91 std::cerr << "Could not create reflector." << std::endl;
92 return false;
93 }
94
95 auto stage_data = reflector->GetRuntimeStageShaderData();
96 if (!stage_data) {
97 std::cerr << "Runtime stage information was nil." << std::endl;
98 return false;
99 }
100
101 stages.AddShader(stage_data);
102 }
103
104 auto stage_data_mapping = switches.json_format ? stages.CreateJsonMapping()
105 : stages.CreateMapping();
106 if (!stage_data_mapping) {
107 std::cerr << "Runtime stage data could not be created." << std::endl;
108 return false;
109 }
110 if (!fml::WriteAtomically(*switches.working_directory, //
111 Utf8FromPath(switches.sl_file_name).c_str(), //
112 *stage_data_mapping //
113 )) {
114 std::cerr << "Could not write file to " << switches.sl_file_name
115 << std::endl;
116 return false;
117 }
118 // Tools that consume the runtime stage data expect the access mode to
119 // be 0644.
120 if (!SetPermissiveAccess(switches.sl_file_name)) {
121 return false;
122 }
123 return true;
124}
std::shared_ptr< fml::Mapping > CreateMapping() const
void AddShader(const std::shared_ptr< Shader > &data)
std::shared_ptr< fml::Mapping > CreateJsonMapping() const
#define FML_DCHECK(condition)
Definition logging.h:122
bool SetPermissiveAccess(const std::filesystem::path &p)
Sets the file access mode of the file at path 'p' to 0644.
Definition utilities.cc:16
static std::shared_ptr< RuntimeStageData::Shader > CompileSkSL(std::shared_ptr< fml::Mapping > source_file_mapping, const Switches &switches)
bool TargetPlatformBundlesSkSL(TargetPlatform platform)
Definition types.cc:315

References impeller::compiler::RuntimeStageData::AddShader(), CompileSkSL(), impeller::compiler::RuntimeStageData::CreateJsonMapping(), impeller::compiler::RuntimeStageData::CreateMapping(), CreateReflectorOptions(), impeller::compiler::Switches::CreateSourceOptions(), FML_DCHECK, impeller::compiler::Compiler::GetErrorMessages(), impeller::compiler::Compiler::GetReflector(), impeller::compiler::Reflector::GetRuntimeStageShaderData(), impeller::compiler::Switches::iplr, impeller::compiler::Compiler::IsValid(), impeller::compiler::Switches::json_format, kSkSL, impeller::compiler::Switches::PlatformsToCompile(), impeller::compiler::Switches::SelectDefaultTargetPlatform(), SetPermissiveAccess(), impeller::compiler::Switches::sl_file_name, TargetPlatformBundlesSkSL(), Utf8FromPath(), impeller::compiler::Switches::working_directory, and fml::WriteAtomically().

Referenced by Main().

◆ OutputReflectionData()

static bool impeller::compiler::OutputReflectionData ( const Compiler compiler,
const Switches switches,
const SourceOptions options 
)
static
  1. Output shader reflection data. May include a JSON file, a C++ header, and/or a C++ TU.

Definition at line 144 of file impellerc_main.cc.

146 {
147 // --------------------------------------------------------------------------
148 /// 3. Output shader reflection data.
149 /// May include a JSON file, a C++ header, and/or a C++ TU.
150 ///
151
153 if (!switches.reflection_json_name.empty()) {
154 auto reflection_json_name = std::filesystem::absolute(
155 std::filesystem::current_path() / switches.reflection_json_name);
157 *switches.working_directory,
158 Utf8FromPath(reflection_json_name).c_str(),
159 *compiler.GetReflector()->GetReflectionJSON())) {
160 std::cerr << "Could not write reflection json to "
161 << switches.reflection_json_name << std::endl;
162 return false;
163 }
164 }
165
166 if (!switches.reflection_header_name.empty()) {
167 auto reflection_header_name = std::filesystem::absolute(
168 std::filesystem::current_path() / switches.reflection_header_name);
170 *switches.working_directory,
171 Utf8FromPath(reflection_header_name).c_str(),
172 *compiler.GetReflector()->GetReflectionHeader())) {
173 std::cerr << "Could not write reflection header to "
174 << switches.reflection_header_name << std::endl;
175 return false;
176 }
177 }
178
179 if (!switches.reflection_cc_name.empty()) {
180 auto reflection_cc_name = std::filesystem::absolute(
181 std::filesystem::current_path() / switches.reflection_cc_name);
183 Utf8FromPath(reflection_cc_name).c_str(),
184 *compiler.GetReflector()->GetReflectionCC())) {
185 std::cerr << "Could not write reflection CC to "
186 << switches.reflection_cc_name << std::endl;
187 return false;
188 }
189 }
190 }
191 return true;
192}
std::shared_ptr< fml::Mapping > GetReflectionJSON() const
Definition reflector.cc:109
std::shared_ptr< fml::Mapping > GetReflectionCC() const
Definition reflector.cc:126
std::shared_ptr< fml::Mapping > GetReflectionHeader() const
Definition reflector.cc:122
std::filesystem::path reflection_cc_name
Definition switches.h:37
std::filesystem::path reflection_json_name
Definition switches.h:35
bool TargetPlatformNeedsReflection(TargetPlatform platform)
Definition types.cc:130

References impeller::compiler::Reflector::GetReflectionCC(), impeller::compiler::Reflector::GetReflectionHeader(), impeller::compiler::Reflector::GetReflectionJSON(), impeller::compiler::Compiler::GetReflector(), impeller::compiler::Switches::reflection_cc_name, impeller::compiler::Switches::reflection_header_name, impeller::compiler::Switches::reflection_json_name, impeller::compiler::SourceOptions::target_platform, TargetPlatformNeedsReflection(), Utf8FromPath(), impeller::compiler::Switches::working_directory, and fml::WriteAtomically().

Referenced by Main().

◆ OutputSLFile()

static bool impeller::compiler::OutputSLFile ( const Compiler compiler,
const Switches switches 
)
static
  1. Output the source file. When in IPLR/RuntimeStage mode, output the serialized IPLR flatbuffer.

Definition at line 126 of file impellerc_main.cc.

126 {
127 // --------------------------------------------------------------------------
128 /// 2. Output the source file. When in IPLR/RuntimeStage mode, output the
129 /// serialized IPLR flatbuffer.
130 ///
131
132 auto sl_file_name = std::filesystem::absolute(
133 std::filesystem::current_path() / switches.sl_file_name);
135 Utf8FromPath(sl_file_name).c_str(),
136 *compiler.GetSLShaderSource())) {
137 std::cerr << "Could not write file to " << switches.sl_file_name
138 << std::endl;
139 return false;
140 }
141 return true;
142}
std::shared_ptr< fml::Mapping > GetSLShaderSource() const
Definition compiler.cc:490

References impeller::compiler::Compiler::GetSLShaderSource(), impeller::compiler::Switches::sl_file_name, Utf8FromPath(), impeller::compiler::Switches::working_directory, and fml::WriteAtomically().

Referenced by Main().

◆ ParseMSLVersion()

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

Definition at line 40 of file compiler.cc.

40 {
41 std::stringstream sstream(msl_version);
42 std::string version_part;
43 uint32_t major = 1;
44 uint32_t minor = 2;
45 uint32_t patch = 0;
46 if (std::getline(sstream, version_part, '.')) {
47 major = std::stoi(version_part);
48 if (std::getline(sstream, version_part, '.')) {
49 minor = std::stoi(version_part);
50 if (std::getline(sstream, version_part, '.')) {
51 patch = std::stoi(version_part);
52 }
53 }
54 }
55 if (major < 1 || (major == 1 && minor < 2)) {
56 std::cerr << "--metal-version version must be at least 1.2. Have "
57 << msl_version << std::endl;
58 }
59 return spirv_cross::CompilerMSL::Options::make_msl_version(major, minor,
60 patch);
61}

◆ 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 19 of file shader_bundle.cc.

21 {
22 auto json = nlohmann::json::parse(bundle_config_json, nullptr, false);
23 if (json.is_discarded() || !json.is_object()) {
24 error_stream << "The shader bundle is not a valid JSON object."
25 << std::endl;
26 return std::nullopt;
27 }
28
29 ShaderBundleConfig bundle;
30 for (auto& [shader_name, shader_value] : json.items()) {
31 if (bundle.find(shader_name) != bundle.end()) {
32 error_stream << "Duplicate shader \"" << shader_name << "\"."
33 << std::endl;
34 return std::nullopt;
35 }
36 if (!shader_value.is_object()) {
37 error_stream << "Invalid shader entry \"" << shader_name
38 << "\": Entry is not a JSON object." << std::endl;
39 return std::nullopt;
40 }
41
42 ShaderConfig shader;
43
44 if (!shader_value.contains("file")) {
45 error_stream << "Invalid shader entry \"" << shader_name
46 << "\": Missing required \"file\" field." << std::endl;
47 return std::nullopt;
48 }
49 shader.source_file_name = shader_value["file"];
50
51 if (!shader_value.contains("type")) {
52 error_stream << "Invalid shader entry \"" << shader_name
53 << "\": Missing required \"type\" field." << std::endl;
54 return std::nullopt;
55 }
56 shader.type = SourceTypeFromString(shader_value["type"]);
57 if (shader.type == SourceType::kUnknown) {
58 error_stream << "Invalid shader entry \"" << shader_name
59 << "\": Shader type " << shader_value["type"]
60 << " is unknown." << std::endl;
61 return std::nullopt;
62 }
63
64 shader.language = shader_value.contains("language")
65 ? ToSourceLanguage(shader_value["language"])
67 if (shader.language == SourceLanguage::kUnknown) {
68 error_stream << "Invalid shader entry \"" << shader_name
69 << "\": Unknown language type " << shader_value["language"]
70 << "." << std::endl;
71 return std::nullopt;
72 }
73
74 shader.entry_point = shader_value.contains("entry_point")
75 ? shader_value["entry_point"]
76 : "main";
77
78 bundle[shader_name] = shader;
79 }
80
81 return bundle;
82}
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(), impeller::compiler::testing::TEST(), impeller::compiler::testing::TEST(), impeller::compiler::testing::TEST(), impeller::compiler::testing::TEST(), impeller::compiler::testing::TEST(), impeller::compiler::testing::TEST(), and impeller::compiler::testing::TEST().

◆ ReadKnownScalarType()

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

Definition at line 769 of file reflector.cc.

770 {
771 switch (type) {
772 case spirv_cross::SPIRType::BaseType::Boolean:
773 return KnownType{
774 .name = "bool",
775 .byte_size = sizeof(bool),
776 };
777 case spirv_cross::SPIRType::BaseType::Float:
778 return KnownType{
779 .name = "Scalar",
780 .byte_size = sizeof(Scalar),
781 };
782 case spirv_cross::SPIRType::BaseType::Half:
783 return KnownType{
784 .name = "Half",
785 .byte_size = sizeof(Half),
786 };
787 case spirv_cross::SPIRType::BaseType::UInt:
788 return KnownType{
789 .name = "uint32_t",
790 .byte_size = sizeof(uint32_t),
791 };
792 case spirv_cross::SPIRType::BaseType::Int:
793 return KnownType{
794 .name = "int32_t",
795 .byte_size = sizeof(int32_t),
796 };
797 default:
798 break;
799 }
800 return std::nullopt;
801}
float Scalar
Definition scalar.h:19

References impeller::compiler::KnownType::name, and type.

◆ 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 198 of file runtime_stage_data.cc.

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

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 141 of file switches.cc.

142 {
143 std::vector<TargetPlatform> stages;
144 for (const auto& platform : kKnownRuntimeStages) {
145 if (command_line.HasOption(platform.first)) {
146 stages.push_back(platform.second);
147 }
148 }
149 return stages;
150}
static const std::map< std::string, TargetPlatform > kKnownRuntimeStages
Definition switches.cc:28

References fml::CommandLine::HasOption(), and 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}

References i.

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}
const uint8_t uint32_t uint32_t GError ** error

References error.

Referenced by GenerateShaderBundle(), and OutputIPLR().

◆ ShaderCErrorToString()

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

Definition at line 149 of file types.cc.

149 {
150 using Status = shaderc_compilation_status;
151 switch (status) {
152 case Status::shaderc_compilation_status_success:
153 return "Success";
154 case Status::shaderc_compilation_status_invalid_stage:
155 return "Invalid shader stage specified";
156 case Status::shaderc_compilation_status_compilation_error:
157 return "Compilation error";
158 case Status::shaderc_compilation_status_internal_error:
159 return "Internal error";
160 case Status::shaderc_compilation_status_null_result_object:
161 return "Internal error. Null result object";
162 case Status::shaderc_compilation_status_invalid_assembly:
163 return "Invalid assembly";
164 case Status::shaderc_compilation_status_validation_error:
165 return "Validation error";
166 case Status::shaderc_compilation_status_transformation_error:
167 return "Transform error";
168 case Status::shaderc_compilation_status_configuration_error:
169 return "Configuration error";
170 }
171 return "Unknown internal error";
172}

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 152 of file switches.cc.

153 {
154 auto source_type_option =
155 command_line.GetOptionValueWithDefault("input-type", "");
156 auto source_type_search = kKnownSourceTypes.find(source_type_option);
157 if (source_type_search == kKnownSourceTypes.end()) {
158 return SourceType::kUnknown;
159 }
160 return source_type_search->second;
161}
static const std::map< std::string, SourceType > kKnownSourceTypes
Definition switches.cc:36

References fml::CommandLine::GetOptionValueWithDefault(), 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 {
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}
const char * name
Definition fuchsia.cc:49
std::string ToLowerCase(std::string_view string)
Definition utilities.cc:61

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

Referenced by ParseShaderBundleConfig().

◆ SourceTypeToString()

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

Definition at line 223 of file types.cc.

223 {
224 switch (type) {
225 case SourceType::kUnknown:
226 return "unknown";
227 case SourceType::kVertexShader:
228 return "vert";
229 case SourceType::kFragmentShader:
230 return "frag";
231 case SourceType::kComputeShader:
232 return "comp";
233 }
235}

References FML_UNREACHABLE, kComputeShader, kFragmentShader, kUnknown, kVertexShader, and type.

◆ 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}
uint32_t * target

References i, and target.

Referenced by CreateGLSLCompiler().

◆ StringToShaderStage()

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

Definition at line 47 of file reflector.cc.

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

◆ TargetPlatformBundlesSkSL()

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

Definition at line 315 of file types.cc.

315 {
316 switch (platform) {
317 case TargetPlatform::kSkSL:
318 case TargetPlatform::kRuntimeStageMetal:
319 case TargetPlatform::kRuntimeStageGLES:
320 case TargetPlatform::kRuntimeStageGLES3:
321 case TargetPlatform::kRuntimeStageVulkan:
322 return true;
323 case TargetPlatform::kMetalDesktop:
324 case TargetPlatform::kMetalIOS:
325 case TargetPlatform::kUnknown:
326 case TargetPlatform::kOpenGLES:
327 case TargetPlatform::kOpenGLDesktop:
328 case TargetPlatform::kVulkan:
329 return false;
330 }
332}

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

Referenced by OutputIPLR().

◆ TargetPlatformFromCommandLine()

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

Definition at line 123 of file switches.cc.

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

References fml::CommandLine::HasOption(), kKnownPlatforms, kUnknown, and target.

◆ TargetPlatformIsMetal()

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

Definition at line 277 of file types.cc.

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

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

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

◆ TargetPlatformIsOpenGL()

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

Definition at line 258 of file types.cc.

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

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

◆ TargetPlatformIsVulkan()

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

Definition at line 296 of file types.cc.

296 {
297 switch (platform) {
298 case TargetPlatform::kRuntimeStageVulkan:
299 case TargetPlatform::kVulkan:
300 return true;
301 case TargetPlatform::kMetalDesktop:
302 case TargetPlatform::kMetalIOS:
303 case TargetPlatform::kRuntimeStageMetal:
304 case TargetPlatform::kUnknown:
305 case TargetPlatform::kSkSL:
306 case TargetPlatform::kOpenGLES:
307 case TargetPlatform::kOpenGLDesktop:
308 case TargetPlatform::kRuntimeStageGLES:
309 case TargetPlatform::kRuntimeStageGLES3:
310 return false;
311 }
313}

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

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

◆ TargetPlatformNeedsReflection()

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

Definition at line 130 of file types.cc.

130 {
131 switch (platform) {
132 case TargetPlatform::kMetalIOS:
133 case TargetPlatform::kMetalDesktop:
134 case TargetPlatform::kOpenGLES:
135 case TargetPlatform::kOpenGLDesktop:
136 case TargetPlatform::kRuntimeStageMetal:
137 case TargetPlatform::kRuntimeStageGLES:
138 case TargetPlatform::kRuntimeStageGLES3:
139 case TargetPlatform::kRuntimeStageVulkan:
140 case TargetPlatform::kVulkan:
141 return true;
142 case TargetPlatform::kUnknown:
143 case TargetPlatform::kSkSL:
144 return false;
145 }
147}

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

Referenced by impeller::compiler::testing::CompilerTestBase::CanCompileAndReflect(), and OutputReflectionData().

◆ TargetPlatformSLExtension()

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

Definition at line 237 of file types.cc.

237 {
238 switch (platform) {
239 case TargetPlatform::kUnknown:
240 return "unknown";
241 case TargetPlatform::kMetalDesktop:
242 case TargetPlatform::kMetalIOS:
243 case TargetPlatform::kRuntimeStageMetal:
244 return "metal";
245 case TargetPlatform::kSkSL:
246 case TargetPlatform::kOpenGLES:
247 case TargetPlatform::kOpenGLDesktop:
248 case TargetPlatform::kRuntimeStageGLES:
249 case TargetPlatform::kRuntimeStageGLES3:
250 return "glsl";
251 case TargetPlatform::kVulkan:
252 case TargetPlatform::kRuntimeStageVulkan:
253 return "vk.spirv";
254 }
256}

References FML_UNREACHABLE, 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 202 of file types.cc.

203 {
204 switch (platform) {
205 case TargetPlatform::kMetalIOS:
206 case TargetPlatform::kRuntimeStageMetal:
207 return spirv_cross::CompilerMSL::Options::Platform::iOS;
208 case TargetPlatform::kMetalDesktop:
209 return spirv_cross::CompilerMSL::Options::Platform::macOS;
210 case TargetPlatform::kSkSL:
211 case TargetPlatform::kOpenGLES:
212 case TargetPlatform::kOpenGLDesktop:
213 case TargetPlatform::kRuntimeStageGLES:
214 case TargetPlatform::kRuntimeStageGLES3:
215 case TargetPlatform::kRuntimeStageVulkan:
216 case TargetPlatform::kVulkan:
217 case TargetPlatform::kUnknown:
218 return spirv_cross::CompilerMSL::Options::Platform::macOS;
219 }
221}

References FML_UNREACHABLE, 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 }
88}

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

◆ 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}

References i, and length.

◆ ToExecutionModel()

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

Definition at line 188 of file types.cc.

188 {
189 switch (type) {
190 case SourceType::kVertexShader:
191 return spv::ExecutionModel::ExecutionModelVertex;
192 case SourceType::kFragmentShader:
193 return spv::ExecutionModel::ExecutionModelFragment;
194 case SourceType::kComputeShader:
195 return spv::ExecutionModel::ExecutionModelGLCompute;
196 case SourceType::kUnknown:
197 break;
198 }
199 return spv::ExecutionModel::ExecutionModelMax;
200}

References kComputeShader, kFragmentShader, kUnknown, kVertexShader, and type.

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:
117 return fb::InputDataType::kFloat;
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 }
136}

References FML_UNREACHABLE, and type.

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:
122 return fb::shaderbundle::InputDataType::kFloat;
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 }
141}

References FML_UNREACHABLE, and type.

◆ 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 }
59}

References FML_UNREACHABLE.

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 }
182}

References FML_UNREACHABLE, and type.

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 x.

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

◆ ToShaderCShaderKind()

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

Definition at line 174 of file types.cc.

174 {
175 switch (type) {
176 case SourceType::kVertexShader:
177 return shaderc_shader_kind::shaderc_vertex_shader;
178 case SourceType::kFragmentShader:
179 return shaderc_shader_kind::shaderc_fragment_shader;
180 case SourceType::kComputeShader:
181 return shaderc_shader_kind::shaderc_compute_shader;
182 case SourceType::kUnknown:
183 break;
184 }
185 return shaderc_shader_kind::shaderc_glsl_infer_from_source;
186}

References kComputeShader, kFragmentShader, kUnknown, kVertexShader, and type.

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 }
45}

References FML_UNREACHABLE.

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 }
54}

References FML_UNREACHABLE.

◆ ToString()

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

Definition at line 590 of file reflector.cc.

590 {
591 switch (type) {
592 case CompilerBackend::Type::kMSL:
593 return "Metal Shading Language";
594 case CompilerBackend::Type::kGLSL:
595 return "OpenGL Shading Language";
596 case CompilerBackend::Type::kGLSLVulkan:
597 return "OpenGL Shading Language (Relaxed Vulkan Semantics)";
598 case CompilerBackend::Type::kSkSL:
599 return "SkSL Shading Language";
600 }
602}

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

◆ 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:
65 return fb::UniformDataType::kFloat;
66 case spirv_cross::SPIRType::SampledImage:
67 return fb::UniformDataType::kSampledImage;
68 case spirv_cross::SPIRType::Struct:
69 return fb::UniformDataType::kStruct;
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 }
94}

References FML_UNREACHABLE, and type.

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:
80 return fb::shaderbundle::UniformDataType::kFloat;
81 case spirv_cross::SPIRType::Double:
82 return fb::shaderbundle::UniformDataType::kDouble;
83 case spirv_cross::SPIRType::SampledImage:
84 return fb::shaderbundle::UniformDataType::kSampledImage;
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 }
99}

References FML_UNREACHABLE, and type.

◆ TypeNameWithPaddingOfSize()

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

Definition at line 758 of file reflector.cc.

758 {
759 std::stringstream stream;
760 stream << "Padding<" << size << ">";
761 return stream.str();
762}

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(), CreateReflectorOptions(), EntryPointFunctionNameFromSourceName(), GenerateShaderBundle(), InferShaderNameFromPath(), Main(), OutputDepfile(), OutputIPLR(), OutputReflectionData(), OutputSLFile(), 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 1217 of file reflector.cc.

1219 {
1220 VertexType result;
1221 result.variable_name = resource->name;
1222 const auto& type = compiler.get_type(resource->type_id);
1223 result.base_type = type.basetype;
1224 const auto total_size = type.columns * type.vecsize * type.width / 8u;
1225 result.byte_length = total_size;
1226
1227 if (type.basetype == spirv_cross::SPIRType::BaseType::Float &&
1228 type.columns == 1u && type.vecsize == 2u &&
1229 type.width == sizeof(float) * 8u) {
1230 result.type_name = "Point";
1231 } else if (type.basetype == spirv_cross::SPIRType::BaseType::Float &&
1232 type.columns == 1u && type.vecsize == 4u &&
1233 type.width == sizeof(float) * 8u) {
1234 result.type_name = "Vector4";
1235 } else if (type.basetype == spirv_cross::SPIRType::BaseType::Float &&
1236 type.columns == 1u && type.vecsize == 3u &&
1237 type.width == sizeof(float) * 8u) {
1238 result.type_name = "Vector3";
1239 } else if (type.basetype == spirv_cross::SPIRType::BaseType::Float &&
1240 type.columns == 1u && type.vecsize == 1u &&
1241 type.width == sizeof(float) * 8u) {
1242 result.type_name = "Scalar";
1243 } else if (type.basetype == spirv_cross::SPIRType::BaseType::Int &&
1244 type.columns == 1u && type.vecsize == 1u &&
1245 type.width == sizeof(int32_t) * 8u) {
1246 result.type_name = "int32_t";
1247 } else {
1248 // Catch all unknown padding.
1249 result.type_name = TypeNameWithPaddingOfSize(total_size);
1250 }
1251
1252 return result;
1253}
static std::string TypeNameWithPaddingOfSize(size_t size)
Definition reflector.cc:758
spirv_cross::SPIRType::BaseType base_type

References impeller::compiler::VertexType::base_type, impeller::compiler::VertexType::byte_length, type, 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.

20 {
21 {"metal-desktop", TargetPlatform::kMetalDesktop},
22 {"metal-ios", TargetPlatform::kMetalIOS},
23 {"vulkan", TargetPlatform::kVulkan},
24 {"opengl-es", TargetPlatform::kOpenGLES},
25 {"opengl-desktop", TargetPlatform::kOpenGLDesktop},
26};

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

◆ kKnownRuntimeStages

const std::map<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 28 of file switches.cc.

28 {
29 {"sksl", TargetPlatform::kSkSL},
30 {"runtime-stage-metal", TargetPlatform::kRuntimeStageMetal},
31 {"runtime-stage-gles", TargetPlatform::kRuntimeStageGLES},
32 {"runtime-stage-gles3", TargetPlatform::kRuntimeStageGLES3},
33 {"runtime-stage-vulkan", TargetPlatform::kRuntimeStageVulkan},
34};

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 36 of file switches.cc.

36 {
37 {"vert", SourceType::kVertexShader},
38 {"frag", SourceType::kFragmentShader},
39 {"comp", SourceType::kComputeShader},
40};

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

◆ kMaxUniformBufferSize

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

Definition at line 38 of file compiler.cc.

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

◆ kReflectionCCTemplate

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

Definition at line 202 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

◆ kUniformTypeKey

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