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 "RuntimeStageVulkan";
95  return "SkSL";
96  }
97  FML_UNREACHABLE();
98 }
99 
100 std::string SourceLanguageToString(SourceLanguage source_language) {
101  switch (source_language) {
103  return "Unknown";
105  return "GLSL";
107  return "HLSL";
108  }
109 }
110 
112  const std::string& file_name,
113  SourceType type,
114  SourceLanguage source_language,
115  const std::string& entry_point_name) {
116  if (source_language == SourceLanguage::kHLSL) {
117  return entry_point_name;
118  }
119 
120  std::stringstream stream;
121  std::filesystem::path file_path(file_name);
122  stream << ConvertToEntrypointName(Utf8FromPath(file_path.stem())) << "_";
123  switch (type) {
125  stream << "unknown";
126  break;
128  stream << "vertex";
129  break;
131  stream << "fragment";
132  break;
134  stream << "compute";
135  break;
136  }
137  stream << "_main";
138  return stream.str();
139 }
140 
142  switch (platform) {
151  return true;
154  return false;
155  }
156  FML_UNREACHABLE();
157 }
158 
159 std::string ShaderCErrorToString(shaderc_compilation_status status) {
160  using Status = shaderc_compilation_status;
161  switch (status) {
162  case Status::shaderc_compilation_status_success:
163  return "Success";
164  case Status::shaderc_compilation_status_invalid_stage:
165  return "Invalid shader stage specified";
166  case Status::shaderc_compilation_status_compilation_error:
167  return "Compilation error";
168  case Status::shaderc_compilation_status_internal_error:
169  return "Internal error";
170  case Status::shaderc_compilation_status_null_result_object:
171  return "Internal error. Null result object";
172  case Status::shaderc_compilation_status_invalid_assembly:
173  return "Invalid assembly";
174  case Status::shaderc_compilation_status_validation_error:
175  return "Validation error";
176  case Status::shaderc_compilation_status_transformation_error:
177  return "Transform error";
178  case Status::shaderc_compilation_status_configuration_error:
179  return "Configuration error";
180  }
181  return "Unknown internal error";
182 }
183 
184 shaderc_shader_kind ToShaderCShaderKind(SourceType type) {
185  switch (type) {
187  return shaderc_shader_kind::shaderc_vertex_shader;
189  return shaderc_shader_kind::shaderc_fragment_shader;
191  return shaderc_shader_kind::shaderc_compute_shader;
193  break;
194  }
195  return shaderc_shader_kind::shaderc_glsl_infer_from_source;
196 }
197 
198 spv::ExecutionModel ToExecutionModel(SourceType type) {
199  switch (type) {
201  return spv::ExecutionModel::ExecutionModelVertex;
203  return spv::ExecutionModel::ExecutionModelFragment;
205  return spv::ExecutionModel::ExecutionModelGLCompute;
207  break;
208  }
209  return spv::ExecutionModel::ExecutionModelMax;
210 }
211 
212 spirv_cross::CompilerMSL::Options::Platform TargetPlatformToMSLPlatform(
213  TargetPlatform platform) {
214  switch (platform) {
217  return spirv_cross::CompilerMSL::Options::Platform::iOS;
219  return spirv_cross::CompilerMSL::Options::Platform::macOS;
227  return spirv_cross::CompilerMSL::Options::Platform::macOS;
228  }
229  FML_UNREACHABLE();
230 }
231 
232 std::string SourceTypeToString(SourceType type) {
233  switch (type) {
235  return "unknown";
237  return "vert";
239  return "frag";
241  return "comp";
242  }
243  FML_UNREACHABLE();
244 }
245 
247  switch (platform) {
249  return "unknown";
253  return "metal";
258  return "glsl";
261  return "vk.spirv";
262  }
263  FML_UNREACHABLE();
264 }
265 
267  switch (platform) {
271  return true;
279  return false;
280  }
281  FML_UNREACHABLE();
282 }
283 
285  switch (platform) {
289  return true;
297  return false;
298  }
299  FML_UNREACHABLE();
300 }
301 
303  switch (platform) {
306  return true;
315  return false;
316  }
317  FML_UNREACHABLE();
318 }
319 
321  switch (platform) {
326  return true;
333  return false;
334  }
335  FML_UNREACHABLE();
336 }
337 
338 } // namespace compiler
339 } // namespace impeller
impeller::compiler::ConvertToEntrypointName
std::string ConvertToEntrypointName(std::string_view string)
Ensure that the entrypoint name is a valid identifier in the target language.
Definition: utilities.cc:69
impeller::compiler::ToLowerCase
std::string ToLowerCase(std::string_view string)
Definition: utilities.cc:62
impeller::compiler::SourceType::kUnknown
@ kUnknown
impeller::compiler::TargetPlatformSLExtension
std::string TargetPlatformSLExtension(TargetPlatform platform)
Definition: types.cc:246
impeller::compiler::TargetPlatform::kMetalDesktop
@ kMetalDesktop
impeller::compiler::TargetPlatformToMSLPlatform
spirv_cross::CompilerMSL::Options::Platform TargetPlatformToMSLPlatform(TargetPlatform platform)
Definition: types.cc:212
impeller::compiler::SourceLanguageToString
std::string SourceLanguageToString(SourceLanguage source_language)
Definition: types.cc:100
impeller::compiler::TargetPlatformNeedsReflection
bool TargetPlatformNeedsReflection(TargetPlatform platform)
Definition: types.cc:141
impeller::compiler::TargetPlatform::kMetalIOS
@ kMetalIOS
impeller::compiler::TargetPlatform
TargetPlatform
Definition: types.h:29
impeller::compiler::SourceLanguage::kGLSL
@ kGLSL
impeller::compiler::StringEndWith
static bool StringEndWith(const std::string &string, const std::string &suffix)
Definition: types.cc:17
impeller::compiler::ToSourceLanguage
SourceLanguage ToSourceLanguage(const std::string &source_language)
Definition: types.cc:64
impeller::compiler::SourceLanguage::kHLSL
@ kHLSL
impeller::compiler::TargetPlatform::kVulkan
@ kVulkan
impeller::compiler::SourceType::kFragmentShader
@ kFragmentShader
impeller::compiler::TargetPlatform::kRuntimeStageVulkan
@ kRuntimeStageVulkan
impeller::compiler::SourceType::kComputeShader
@ kComputeShader
impeller::compiler::TargetPlatformIsOpenGL
bool TargetPlatformIsOpenGL(TargetPlatform platform)
Definition: types.cc:266
impeller::compiler::EntryPointFunctionNameFromSourceName
std::string EntryPointFunctionNameFromSourceName(const std::string &file_name, SourceType type, SourceLanguage source_language, const std::string &entry_point_name)
Definition: types.cc:111
impeller::compiler::TargetPlatformIsMetal
bool TargetPlatformIsMetal(TargetPlatform platform)
Definition: types.cc:284
impeller::compiler::TargetPlatformIsVulkan
bool TargetPlatformIsVulkan(TargetPlatform platform)
Definition: types.cc:302
utilities.h
impeller::compiler::SourceType
SourceType
Definition: types.h:22
impeller::compiler::SourceLanguage::kUnknown
@ kUnknown
impeller::compiler::ToExecutionModel
spv::ExecutionModel ToExecutionModel(SourceType type)
Definition: types.cc:198
impeller::compiler::SourceTypeFromFileName
SourceType SourceTypeFromFileName(const std::string &file_name)
Definition: types.cc:30
impeller::compiler::SourceLanguage
SourceLanguage
Definition: types.h:42
impeller::compiler::TargetPlatform::kOpenGLDesktop
@ kOpenGLDesktop
impeller::compiler::SourceTypeFromString
SourceType SourceTypeFromString(std::string name)
Definition: types.cc:46
impeller::compiler::TargetPlatformToString
std::string TargetPlatformToString(TargetPlatform platform)
Definition: types.cc:74
impeller::compiler::TargetPlatform::kUnknown
@ kUnknown
impeller::compiler::SourceTypeToString
std::string SourceTypeToString(SourceType type)
Definition: types.cc:232
impeller::compiler::TargetPlatform::kOpenGLES
@ kOpenGLES
impeller::compiler::Utf8FromPath
std::string Utf8FromPath(const std::filesystem::path &path)
Converts a native format path to a utf8 string.
Definition: utilities.cc:30
impeller::compiler::TargetPlatform::kRuntimeStageMetal
@ kRuntimeStageMetal
impeller::compiler::SourceType::kVertexShader
@ kVertexShader
impeller::compiler::ToShaderCShaderKind
shaderc_shader_kind ToShaderCShaderKind(SourceType type)
Definition: types.cc:184
impeller::compiler::TargetPlatformBundlesSkSL
bool TargetPlatformBundlesSkSL(TargetPlatform platform)
Definition: types.cc:320
impeller
Definition: aiks_blur_unittests.cc:20
impeller::compiler::ShaderCErrorToString
std::string ShaderCErrorToString(shaderc_compilation_status status)
Definition: types.cc:159
types.h
impeller::compiler::TargetPlatform::kSkSL
@ kSkSL
impeller::compiler::TargetPlatform::kRuntimeStageGLES
@ kRuntimeStageGLES