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 <filesystem>
8 #include <sstream>
9 
10 #include "flutter/fml/logging.h"
12 
13 namespace impeller {
14 namespace compiler {
15 
16 static bool StringEndWith(const std::string& string,
17  const std::string& suffix) {
18  if (suffix.size() > string.size()) {
19  return false;
20  }
21 
22  if (suffix.empty() || suffix.empty()) {
23  return false;
24  }
25 
26  return string.rfind(suffix) == (string.size() - suffix.size());
27 }
28 
29 SourceType SourceTypeFromFileName(const std::string& file_name) {
30  if (StringEndWith(file_name, ".vert")) {
32  }
33 
34  if (StringEndWith(file_name, ".frag")) {
36  }
37 
38  if (StringEndWith(file_name, ".tesc")) {
40  }
41 
42  if (StringEndWith(file_name, ".tese")) {
44  }
45 
46  if (StringEndWith(file_name, ".comp")) {
48  }
49 
50  return SourceType::kUnknown;
51 }
52 
53 std::string TargetPlatformToString(TargetPlatform platform) {
54  switch (platform) {
56  return "Unknown";
58  return "MetalDesktop";
60  return "MetaliOS";
62  return "OpenGLES";
64  return "OpenGLDesktop";
66  return "Vulkan";
68  return "RuntimeStageMetal";
70  return "RuntimeStageGLES";
72  return "RuntimeStageVulkan";
74  return "SkSL";
75  }
76  FML_UNREACHABLE();
77 }
78 
79 std::string SourceLanguageToString(SourceLanguage source_language) {
80  switch (source_language) {
82  return "Unknown";
84  return "GLSL";
86  return "HLSL";
87  }
88 }
89 
91  const std::string& file_name,
92  SourceType type,
93  SourceLanguage source_language,
94  const std::string& entry_point_name) {
95  if (source_language == SourceLanguage::kHLSL) {
96  return entry_point_name;
97  }
98 
99  std::stringstream stream;
100  std::filesystem::path file_path(file_name);
101  stream << ConvertToEntrypointName(Utf8FromPath(file_path.stem())) << "_";
102  switch (type) {
104  stream << "unknown";
105  break;
107  stream << "vertex";
108  break;
110  stream << "fragment";
111  break;
113  stream << "tess_control";
114  break;
116  stream << "tess_eval";
117  break;
119  stream << "compute";
120  break;
121  }
122  stream << "_main";
123  return stream.str();
124 }
125 
127  switch (platform) {
136  return true;
139  return false;
140  }
141  FML_UNREACHABLE();
142 }
143 
144 std::string ShaderCErrorToString(shaderc_compilation_status status) {
145  using Status = shaderc_compilation_status;
146  switch (status) {
147  case Status::shaderc_compilation_status_success:
148  return "Success";
149  case Status::shaderc_compilation_status_invalid_stage:
150  return "Invalid shader stage specified";
151  case Status::shaderc_compilation_status_compilation_error:
152  return "Compilation error";
153  case Status::shaderc_compilation_status_internal_error:
154  return "Internal error";
155  case Status::shaderc_compilation_status_null_result_object:
156  return "Internal error. Null result object";
157  case Status::shaderc_compilation_status_invalid_assembly:
158  return "Invalid assembly";
159  case Status::shaderc_compilation_status_validation_error:
160  return "Validation error";
161  case Status::shaderc_compilation_status_transformation_error:
162  return "Transformation error";
163  case Status::shaderc_compilation_status_configuration_error:
164  return "Configuration error";
165  }
166  return "Unknown internal error";
167 }
168 
169 shaderc_shader_kind ToShaderCShaderKind(SourceType type) {
170  switch (type) {
172  return shaderc_shader_kind::shaderc_vertex_shader;
174  return shaderc_shader_kind::shaderc_fragment_shader;
176  return shaderc_shader_kind::shaderc_tess_control_shader;
178  return shaderc_shader_kind::shaderc_tess_evaluation_shader;
180  return shaderc_shader_kind::shaderc_compute_shader;
182  break;
183  }
184  return shaderc_shader_kind::shaderc_glsl_infer_from_source;
185 }
186 
187 spv::ExecutionModel ToExecutionModel(SourceType type) {
188  switch (type) {
190  return spv::ExecutionModel::ExecutionModelVertex;
192  return spv::ExecutionModel::ExecutionModelFragment;
194  return spv::ExecutionModel::ExecutionModelTessellationControl;
196  return spv::ExecutionModel::ExecutionModelTessellationEvaluation;
198  return spv::ExecutionModel::ExecutionModelGLCompute;
200  break;
201  }
202  return spv::ExecutionModel::ExecutionModelMax;
203 }
204 
205 spirv_cross::CompilerMSL::Options::Platform TargetPlatformToMSLPlatform(
206  TargetPlatform platform) {
207  switch (platform) {
210  return spirv_cross::CompilerMSL::Options::Platform::iOS;
212  return spirv_cross::CompilerMSL::Options::Platform::macOS;
220  return spirv_cross::CompilerMSL::Options::Platform::macOS;
221  }
222  FML_UNREACHABLE();
223 }
224 
225 std::string SourceTypeToString(SourceType type) {
226  switch (type) {
228  return "unknown";
230  return "vert";
232  return "frag";
234  return "tesc";
236  return "tese";
238  return "comp";
239  }
240  FML_UNREACHABLE();
241 }
242 
244  switch (platform) {
246  return "unknown";
250  return "metal";
255  return "glsl";
258  return "vk.spirv";
259  }
260  FML_UNREACHABLE();
261 }
262 
264  switch (platform) {
268  return true;
276  return false;
277  }
278  FML_UNREACHABLE();
279 }
280 
282  switch (platform) {
286  return true;
294  return false;
295  }
296  FML_UNREACHABLE();
297 }
298 
300  switch (platform) {
303  return true;
312  return false;
313  }
314  FML_UNREACHABLE();
315 }
316 
318  switch (platform) {
323  return true;
330  return false;
331  }
332  FML_UNREACHABLE();
333 }
334 
335 } // namespace compiler
336 } // 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:46
impeller::compiler::SourceType::kUnknown
@ kUnknown
impeller::compiler::TargetPlatformSLExtension
std::string TargetPlatformSLExtension(TargetPlatform platform)
Definition: types.cc:243
impeller::compiler::TargetPlatform::kMetalDesktop
@ kMetalDesktop
impeller::compiler::TargetPlatformToMSLPlatform
spirv_cross::CompilerMSL::Options::Platform TargetPlatformToMSLPlatform(TargetPlatform platform)
Definition: types.cc:205
impeller::compiler::SourceLanguageToString
std::string SourceLanguageToString(SourceLanguage source_language)
Definition: types.cc:79
impeller::compiler::TargetPlatformNeedsReflection
bool TargetPlatformNeedsReflection(TargetPlatform platform)
Definition: types.cc:126
impeller::compiler::TargetPlatform::kMetalIOS
@ kMetalIOS
impeller::compiler::SourceType::kTessellationControlShader
@ kTessellationControlShader
impeller::compiler::TargetPlatform
TargetPlatform
Definition: types.h:28
impeller::compiler::SourceLanguage::kGLSL
@ kGLSL
impeller::compiler::StringEndWith
static bool StringEndWith(const std::string &string, const std::string &suffix)
Definition: types.cc:16
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:263
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:90
impeller::compiler::TargetPlatformIsMetal
bool TargetPlatformIsMetal(TargetPlatform platform)
Definition: types.cc:281
impeller::compiler::TargetPlatformIsVulkan
bool TargetPlatformIsVulkan(TargetPlatform platform)
Definition: types.cc:299
utilities.h
impeller::compiler::SourceType
SourceType
Definition: types.h:19
impeller::compiler::SourceLanguage::kUnknown
@ kUnknown
impeller::compiler::ToExecutionModel
spv::ExecutionModel ToExecutionModel(SourceType type)
Definition: types.cc:187
impeller::compiler::SourceTypeFromFileName
SourceType SourceTypeFromFileName(const std::string &file_name)
Definition: types.cc:29
impeller::compiler::SourceLanguage
SourceLanguage
Definition: types.h:41
impeller::compiler::TargetPlatform::kOpenGLDesktop
@ kOpenGLDesktop
impeller::compiler::TargetPlatformToString
std::string TargetPlatformToString(TargetPlatform platform)
Definition: types.cc:53
impeller::compiler::TargetPlatform::kUnknown
@ kUnknown
impeller::compiler::SourceTypeToString
std::string SourceTypeToString(SourceType type)
Definition: types.cc:225
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:14
impeller::compiler::TargetPlatform::kRuntimeStageMetal
@ kRuntimeStageMetal
impeller::compiler::SourceType::kVertexShader
@ kVertexShader
impeller::compiler::ToShaderCShaderKind
shaderc_shader_kind ToShaderCShaderKind(SourceType type)
Definition: types.cc:169
impeller::compiler::TargetPlatformBundlesSkSL
bool TargetPlatformBundlesSkSL(TargetPlatform platform)
Definition: types.cc:317
impeller
Definition: aiks_context.cc:10
impeller::compiler::SourceType::kTessellationEvaluationShader
@ kTessellationEvaluationShader
impeller::compiler::ShaderCErrorToString
std::string ShaderCErrorToString(shaderc_compilation_status status)
Definition: types.cc:144
types.h
impeller::compiler::TargetPlatform::kSkSL
@ kSkSL
impeller::compiler::TargetPlatform::kRuntimeStageGLES
@ kRuntimeStageGLES