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 SourceType SourceTypeFromFileName(const std::filesystem::path& file_name) {
18  std::string extension = file_name.extension().string();
19  if (extension == ".vert") {
21  }
22 
23  if (extension == ".frag") {
25  }
26 
27  if (extension == ".comp") {
29  }
30 
31  return SourceType::kUnknown;
32 }
33 
34 SourceType SourceTypeFromString(std::string name) {
35  name = ToLowerCase(name);
36 
37  if (name == "vertex") {
39  }
40 
41  if (name == "fragment") {
43  }
44 
45  if (name == "compute") {
47  }
48 
49  return SourceType::kUnknown;
50 }
51 
52 SourceLanguage ToSourceLanguage(const std::string& source_language) {
53  if (source_language == "glsl") {
54  return SourceLanguage::kGLSL;
55  }
56  if (source_language == "hlsl") {
57  return SourceLanguage::kHLSL;
58  }
60 }
61 
62 std::string TargetPlatformToString(TargetPlatform platform) {
63  switch (platform) {
65  return "Unknown";
67  return "MetalDesktop";
69  return "MetaliOS";
71  return "OpenGLES";
73  return "OpenGLDesktop";
75  return "Vulkan";
77  return "RuntimeStageMetal";
79  return "RuntimeStageGLES";
81  return "RuntimeStageGLES3";
83  return "RuntimeStageVulkan";
85  return "SkSL";
86  }
87  FML_UNREACHABLE();
88 }
89 
90 std::string SourceLanguageToString(SourceLanguage source_language) {
91  switch (source_language) {
93  return "Unknown";
95  return "GLSL";
97  return "HLSL";
98  }
99 }
100 
102  const std::filesystem::path& file_name,
103  SourceType type,
104  SourceLanguage source_language,
105  const std::string& entry_point_name) {
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) {
114  stream << "unknown";
115  break;
117  stream << "vertex";
118  break;
120  stream << "fragment";
121  break;
123  stream << "compute";
124  break;
125  }
126  stream << "_main";
127  return stream.str();
128 }
129 
130 std::string ShaderCErrorToString(shaderc_compilation_status status) {
131  using Status = shaderc_compilation_status;
132  switch (status) {
133  case Status::shaderc_compilation_status_success:
134  return "Success";
135  case Status::shaderc_compilation_status_invalid_stage:
136  return "Invalid shader stage specified";
137  case Status::shaderc_compilation_status_compilation_error:
138  return "Compilation error";
139  case Status::shaderc_compilation_status_internal_error:
140  return "Internal error";
141  case Status::shaderc_compilation_status_null_result_object:
142  return "Internal error. Null result object";
143  case Status::shaderc_compilation_status_invalid_assembly:
144  return "Invalid assembly";
145  case Status::shaderc_compilation_status_validation_error:
146  return "Validation error";
147  case Status::shaderc_compilation_status_transformation_error:
148  return "Transform error";
149  case Status::shaderc_compilation_status_configuration_error:
150  return "Configuration error";
151  }
152  return "Unknown internal error";
153 }
154 
155 shaderc_shader_kind ToShaderCShaderKind(SourceType type) {
156  switch (type) {
158  return shaderc_shader_kind::shaderc_vertex_shader;
160  return shaderc_shader_kind::shaderc_fragment_shader;
162  return shaderc_shader_kind::shaderc_compute_shader;
164  break;
165  }
166  return shaderc_shader_kind::shaderc_glsl_infer_from_source;
167 }
168 
169 spv::ExecutionModel ToExecutionModel(SourceType type) {
170  switch (type) {
172  return spv::ExecutionModel::ExecutionModelVertex;
174  return spv::ExecutionModel::ExecutionModelFragment;
176  return spv::ExecutionModel::ExecutionModelGLCompute;
178  break;
179  }
180  return spv::ExecutionModel::ExecutionModelMax;
181 }
182 
183 spirv_cross::CompilerMSL::Options::Platform TargetPlatformToMSLPlatform(
184  TargetPlatform platform) {
185  switch (platform) {
188  return spirv_cross::CompilerMSL::Options::Platform::iOS;
190  return spirv_cross::CompilerMSL::Options::Platform::macOS;
199  return spirv_cross::CompilerMSL::Options::Platform::macOS;
200  }
201  FML_UNREACHABLE();
202 }
203 
204 std::string SourceTypeToString(SourceType type) {
205  switch (type) {
207  return "unknown";
209  return "vert";
211  return "frag";
213  return "comp";
214  }
215  FML_UNREACHABLE();
216 }
217 
219  switch (platform) {
221  return "unknown";
225  return "metal";
231  return "glsl";
234  return "vk.spirv";
235  }
236  FML_UNREACHABLE();
237 }
238 
240  switch (platform) {
245  return true;
253  return false;
254  }
255  FML_UNREACHABLE();
256 }
257 
259  switch (platform) {
263  return true;
272  return false;
273  }
274  FML_UNREACHABLE();
275 }
276 
278  switch (platform) {
281  return true;
291  return false;
292  }
293  FML_UNREACHABLE();
294 }
295 
296 } // namespace compiler
297 } // namespace impeller
std::string ConvertToEntrypointName(std::string_view string)
Ensure that the entrypoint name is a valid identifier in the target language.
Definition: utilities.cc:68
std::string ToLowerCase(std::string_view string)
Definition: utilities.cc:61
std::string SourceLanguageToString(SourceLanguage source_language)
Definition: types.cc:90
std::string TargetPlatformToString(TargetPlatform platform)
Definition: types.cc:62
std::string TargetPlatformSLExtension(TargetPlatform platform)
Definition: types.cc:218
shaderc_shader_kind ToShaderCShaderKind(SourceType type)
Definition: types.cc:155
std::string SourceTypeToString(SourceType type)
Definition: types.cc:204
SourceType SourceTypeFromFileName(const std::filesystem::path &file_name)
Definition: types.cc:17
std::string EntryPointFunctionNameFromSourceName(const std::filesystem::path &file_name, SourceType type, SourceLanguage source_language, const std::string &entry_point_name)
Definition: types.cc:101
SourceType SourceTypeFromString(std::string name)
Definition: types.cc:34
bool TargetPlatformIsMetal(TargetPlatform platform)
Definition: types.cc:258
bool TargetPlatformIsOpenGL(TargetPlatform platform)
Definition: types.cc:239
std::string ShaderCErrorToString(shaderc_compilation_status status)
Definition: types.cc:130
bool TargetPlatformIsVulkan(TargetPlatform platform)
Definition: types.cc:277
std::string Utf8FromPath(const std::filesystem::path &path)
Converts a native format path to a utf8 string.
Definition: utilities.cc:30
spirv_cross::CompilerMSL::Options::Platform TargetPlatformToMSLPlatform(TargetPlatform platform)
Definition: types.cc:183
SourceLanguage ToSourceLanguage(const std::string &source_language)
Definition: types.cc:52
spv::ExecutionModel ToExecutionModel(SourceType type)
Definition: types.cc:169