Flutter Impeller
types.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
7 #include <cctype>
8 #include <filesystem>
9 #include <sstream>
10 
11 #include "flutter/fml/logging.h"
13 
14 namespace impeller {
15 namespace compiler {
16 
17 static bool StringEndWith(const std::string& string,
18  const std::string& suffix) {
19  if (suffix.size() > string.size()) {
20  return false;
21  }
22 
23  if (suffix.empty() || suffix.empty()) {
24  return false;
25  }
26 
27  return string.rfind(suffix) == (string.size() - suffix.size());
28 }
29 
30 SourceType SourceTypeFromFileName(const std::string& file_name) {
31  if (StringEndWith(file_name, ".vert")) {
33  }
34 
35  if (StringEndWith(file_name, ".frag")) {
37  }
38 
39  if (StringEndWith(file_name, ".comp")) {
41  }
42 
43  return SourceType::kUnknown;
44 }
45 
46 SourceType SourceTypeFromString(std::string name) {
47  name = ToLowerCase(name);
48 
49  if (name == "vertex") {
51  }
52 
53  if (name == "fragment") {
55  }
56 
57  if (name == "compute") {
59  }
60 
61  return SourceType::kUnknown;
62 }
63 
64 SourceLanguage ToSourceLanguage(const std::string& source_language) {
65  if (source_language == "glsl") {
66  return SourceLanguage::kGLSL;
67  }
68  if (source_language == "hlsl") {
69  return SourceLanguage::kHLSL;
70  }
72 }
73 
74 std::string TargetPlatformToString(TargetPlatform platform) {
75  switch (platform) {
77  return "Unknown";
79  return "MetalDesktop";
81  return "MetaliOS";
83  return "OpenGLES";
85  return "OpenGLDesktop";
87  return "Vulkan";
89  return "RuntimeStageMetal";
91  return "RuntimeStageGLES";
93  return "RuntimeStageGLES3";
95  return "RuntimeStageVulkan";
97  return "SkSL";
98  }
99  FML_UNREACHABLE();
100 }
101 
102 std::string SourceLanguageToString(SourceLanguage source_language) {
103  switch (source_language) {
105  return "Unknown";
107  return "GLSL";
109  return "HLSL";
110  }
111 }
112 
114  const std::string& file_name,
116  SourceLanguage source_language,
117  const std::string& entry_point_name) {
118  if (source_language == SourceLanguage::kHLSL) {
119  return entry_point_name;
120  }
121 
122  std::stringstream stream;
123  std::filesystem::path file_path(file_name);
124  stream << ConvertToEntrypointName(Utf8FromPath(file_path.stem())) << "_";
125  switch (type) {
127  stream << "unknown";
128  break;
130  stream << "vertex";
131  break;
133  stream << "fragment";
134  break;
136  stream << "compute";
137  break;
138  }
139  stream << "_main";
140  return stream.str();
141 }
142 
144  switch (platform) {
154  return true;
157  return false;
158  }
159  FML_UNREACHABLE();
160 }
161 
162 std::string ShaderCErrorToString(shaderc_compilation_status status) {
163  using Status = shaderc_compilation_status;
164  switch (status) {
165  case Status::shaderc_compilation_status_success:
166  return "Success";
167  case Status::shaderc_compilation_status_invalid_stage:
168  return "Invalid shader stage specified";
169  case Status::shaderc_compilation_status_compilation_error:
170  return "Compilation error";
171  case Status::shaderc_compilation_status_internal_error:
172  return "Internal error";
173  case Status::shaderc_compilation_status_null_result_object:
174  return "Internal error. Null result object";
175  case Status::shaderc_compilation_status_invalid_assembly:
176  return "Invalid assembly";
177  case Status::shaderc_compilation_status_validation_error:
178  return "Validation error";
179  case Status::shaderc_compilation_status_transformation_error:
180  return "Transform error";
181  case Status::shaderc_compilation_status_configuration_error:
182  return "Configuration error";
183  }
184  return "Unknown internal error";
185 }
186 
187 shaderc_shader_kind ToShaderCShaderKind(SourceType type) {
188  switch (type) {
190  return shaderc_shader_kind::shaderc_vertex_shader;
192  return shaderc_shader_kind::shaderc_fragment_shader;
194  return shaderc_shader_kind::shaderc_compute_shader;
196  break;
197  }
198  return shaderc_shader_kind::shaderc_glsl_infer_from_source;
199 }
200 
201 spv::ExecutionModel ToExecutionModel(SourceType type) {
202  switch (type) {
204  return spv::ExecutionModel::ExecutionModelVertex;
206  return spv::ExecutionModel::ExecutionModelFragment;
208  return spv::ExecutionModel::ExecutionModelGLCompute;
210  break;
211  }
212  return spv::ExecutionModel::ExecutionModelMax;
213 }
214 
215 spirv_cross::CompilerMSL::Options::Platform TargetPlatformToMSLPlatform(
216  TargetPlatform platform) {
217  switch (platform) {
220  return spirv_cross::CompilerMSL::Options::Platform::iOS;
222  return spirv_cross::CompilerMSL::Options::Platform::macOS;
231  return spirv_cross::CompilerMSL::Options::Platform::macOS;
232  }
233  FML_UNREACHABLE();
234 }
235 
237  switch (type) {
239  return "unknown";
241  return "vert";
243  return "frag";
245  return "comp";
246  }
247  FML_UNREACHABLE();
248 }
249 
251  switch (platform) {
253  return "unknown";
257  return "metal";
263  return "glsl";
266  return "vk.spirv";
267  }
268  FML_UNREACHABLE();
269 }
270 
272  switch (platform) {
277  return true;
285  return false;
286  }
287  FML_UNREACHABLE();
288 }
289 
291  switch (platform) {
295  return true;
304  return false;
305  }
306  FML_UNREACHABLE();
307 }
308 
310  switch (platform) {
313  return true;
323  return false;
324  }
325  FML_UNREACHABLE();
326 }
327 
329  switch (platform) {
335  return true;
342  return false;
343  }
344  FML_UNREACHABLE();
345 }
346 
347 } // namespace compiler
348 } // namespace impeller
GLenum type
std::string ConvertToEntrypointName(std::string_view string)
Ensure that the entrypoint name is a valid identifier in the target language.
Definition: utilities.cc:69
std::string ToLowerCase(std::string_view string)
Definition: utilities.cc:62
static bool StringEndWith(const std::string &string, const std::string &suffix)
Definition: types.cc:17
std::string SourceLanguageToString(SourceLanguage source_language)
Definition: types.cc:102
std::string TargetPlatformToString(TargetPlatform platform)
Definition: types.cc:74
std::string TargetPlatformSLExtension(TargetPlatform platform)
Definition: types.cc:250
shaderc_shader_kind ToShaderCShaderKind(SourceType type)
Definition: types.cc:187
std::string SourceTypeToString(SourceType type)
Definition: types.cc:236
SourceType SourceTypeFromString(std::string name)
Definition: types.cc:46
SourceType SourceTypeFromFileName(const std::string &file_name)
Definition: types.cc:30
std::string EntryPointFunctionNameFromSourceName(const std::string &file_name, SourceType type, SourceLanguage source_language, const std::string &entry_point_name)
Definition: types.cc:113
bool TargetPlatformIsMetal(TargetPlatform platform)
Definition: types.cc:290
bool TargetPlatformIsOpenGL(TargetPlatform platform)
Definition: types.cc:271
std::string ShaderCErrorToString(shaderc_compilation_status status)
Definition: types.cc:162
bool TargetPlatformIsVulkan(TargetPlatform platform)
Definition: types.cc:309
std::string Utf8FromPath(const std::filesystem::path &path)
Converts a native format path to a utf8 string.
Definition: utilities.cc:30
bool TargetPlatformBundlesSkSL(TargetPlatform platform)
Definition: types.cc:328
spirv_cross::CompilerMSL::Options::Platform TargetPlatformToMSLPlatform(TargetPlatform platform)
Definition: types.cc:215
SourceLanguage ToSourceLanguage(const std::string &source_language)
Definition: types.cc:64
bool TargetPlatformNeedsReflection(TargetPlatform platform)
Definition: types.cc:143
spv::ExecutionModel ToExecutionModel(SourceType type)
Definition: types.cc:201