| 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
5 #include "impeller/compiler/types.h"
6
7 #include <cctype>
8 #include <filesystem>
9 #include <sstream>
10
11 #include "flutter/fml/logging.h"
12 #include "impeller/compiler/utilities.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")) {
32 return SourceType::kVertexShader;
33 }
34
35 if (StringEndWith(file_name, ".frag")) {
36 return SourceType::kFragmentShader;
37 }
38
39 if (StringEndWith(file_name, ".comp")) {
40 return SourceType::kComputeShader;
41 }
42
43 return SourceType::kUnknown;
44 }
45
46 SourceType SourceTypeFromString(std::string name) {
47 name = ToLowerCase(name);
48
49 if (name == "vertex") {
50 return SourceType::kVertexShader;
51 }
52
53 if (name == "fragment") {
54 return SourceType::kFragmentShader;
55 }
56
57 if (name == "compute") {
58 return SourceType::kComputeShader;
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 }
71 return SourceLanguage::kUnknown;
72 }
73
74 std::string TargetPlatformToString(TargetPlatform platform) {
75 switch (platform) {
76 case TargetPlatform::kUnknown:
77 return "Unknown";
78 case TargetPlatform::kMetalDesktop:
79 return "MetalDesktop";
80 case TargetPlatform::kMetalIOS:
81 return "MetaliOS";
82 case TargetPlatform::kOpenGLES:
83 return "OpenGLES";
84 case TargetPlatform::kOpenGLDesktop:
85 return "OpenGLDesktop";
86 case TargetPlatform::kVulkan:
87 return "Vulkan";
88 case TargetPlatform::kRuntimeStageMetal:
89 return "RuntimeStageMetal";
90 case TargetPlatform::kRuntimeStageGLES:
91 return "RuntimeStageGLES";
92 case TargetPlatform::kRuntimeStageVulkan:
93 return "RuntimeStageVulkan";
94 case TargetPlatform::kSkSL:
95 return "SkSL";
96 }
97 FML_UNREACHABLE();
98 }
99
100 std::string SourceLanguageToString(SourceLanguage source_language) {
101 switch (source_language) {
102 case SourceLanguage::kUnknown:
103 return "Unknown";
104 case SourceLanguage::kGLSL:
105 return "GLSL";
106 case SourceLanguage::kHLSL:
107 return "HLSL";
108 }
109 }
110
111 std::string EntryPointFunctionNameFromSourceName(
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) {
124 case SourceType::kUnknown:
125 stream << "unknown";
126 break;
127 case SourceType::kVertexShader:
128 stream << "vertex";
129 break;
130 case SourceType::kFragmentShader:
131 stream << "fragment";
132 break;
133 case SourceType::kComputeShader:
134 stream << "compute";
135 break;
136 }
137 stream << "_main";
138 return stream.str();
139 }
140
141 bool TargetPlatformNeedsReflection(TargetPlatform platform) {
142 switch (platform) {
143 case TargetPlatform::kMetalIOS:
144 case TargetPlatform::kMetalDesktop:
145 case TargetPlatform::kOpenGLES:
146 case TargetPlatform::kOpenGLDesktop:
147 case TargetPlatform::kRuntimeStageMetal:
148 case TargetPlatform::kRuntimeStageGLES:
149 case TargetPlatform::kRuntimeStageVulkan:
150 case TargetPlatform::kVulkan:
151 return true;
152 case TargetPlatform::kUnknown:
153 case TargetPlatform::kSkSL:
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) {
186 case SourceType::kVertexShader:
187 return shaderc_shader_kind::shaderc_vertex_shader;
188 case SourceType::kFragmentShader:
189 return shaderc_shader_kind::shaderc_fragment_shader;
190 case SourceType::kComputeShader:
191 return shaderc_shader_kind::shaderc_compute_shader;
192 case SourceType::kUnknown:
193 break;
194 }
195 return shaderc_shader_kind::shaderc_glsl_infer_from_source;
196 }
197
198 spv::ExecutionModel ToExecutionModel(SourceType type) {
199 switch (type) {
200 case SourceType::kVertexShader:
201 return spv::ExecutionModel::ExecutionModelVertex;
202 case SourceType::kFragmentShader:
203 return spv::ExecutionModel::ExecutionModelFragment;
204 case SourceType::kComputeShader:
205 return spv::ExecutionModel::ExecutionModelGLCompute;
206 case SourceType::kUnknown:
207 break;
208 }
209 return spv::ExecutionModel::ExecutionModelMax;
210 }
211
212 spirv_cross::CompilerMSL::Options::Platform TargetPlatformToMSLPlatform(
213 TargetPlatform platform) {
214 switch (platform) {
215 case TargetPlatform::kMetalIOS:
216 case TargetPlatform::kRuntimeStageMetal:
217 return spirv_cross::CompilerMSL::Options::Platform::iOS;
218 case TargetPlatform::kMetalDesktop:
219 return spirv_cross::CompilerMSL::Options::Platform::macOS;
220 case TargetPlatform::kSkSL:
221 case TargetPlatform::kOpenGLES:
222 case TargetPlatform::kOpenGLDesktop:
223 case TargetPlatform::kRuntimeStageGLES:
224 case TargetPlatform::kRuntimeStageVulkan:
225 case TargetPlatform::kVulkan:
226 case TargetPlatform::kUnknown:
227 return spirv_cross::CompilerMSL::Options::Platform::macOS;
228 }
229 FML_UNREACHABLE();
230 }
231
232 std::string SourceTypeToString(SourceType type) {
233 switch (type) {
234 case SourceType::kUnknown:
235 return "unknown";
236 case SourceType::kVertexShader:
237 return "vert";
238 case SourceType::kFragmentShader:
239 return "frag";
240 case SourceType::kComputeShader:
241 return "comp";
242 }
243 FML_UNREACHABLE();
244 }
245
246 std::string TargetPlatformSLExtension(TargetPlatform platform) {
247 switch (platform) {
248 case TargetPlatform::kUnknown:
249 return "unknown";
250 case TargetPlatform::kMetalDesktop:
251 case TargetPlatform::kMetalIOS:
252 case TargetPlatform::kRuntimeStageMetal:
253 return "metal";
254 case TargetPlatform::kSkSL:
255 case TargetPlatform::kOpenGLES:
256 case TargetPlatform::kOpenGLDesktop:
257 case TargetPlatform::kRuntimeStageGLES:
258 return "glsl";
259 case TargetPlatform::kVulkan:
260 case TargetPlatform::kRuntimeStageVulkan:
261 return "vk.spirv";
262 }
263 FML_UNREACHABLE();
264 }
265
266 bool TargetPlatformIsOpenGL(TargetPlatform platform) {
267 switch (platform) {
268 case TargetPlatform::kOpenGLES:
269 case TargetPlatform::kOpenGLDesktop:
270 case TargetPlatform::kRuntimeStageGLES:
271 return true;
272 case TargetPlatform::kMetalDesktop:
273 case TargetPlatform::kRuntimeStageMetal:
274 case TargetPlatform::kRuntimeStageVulkan:
275 case TargetPlatform::kMetalIOS:
276 case TargetPlatform::kUnknown:
277 case TargetPlatform::kSkSL:
278 case TargetPlatform::kVulkan:
279 return false;
280 }
281 FML_UNREACHABLE();
282 }
283
284 bool TargetPlatformIsMetal(TargetPlatform platform) {
285 switch (platform) {
286 case TargetPlatform::kMetalDesktop:
287 case TargetPlatform::kMetalIOS:
288 case TargetPlatform::kRuntimeStageMetal:
289 return true;
290 case TargetPlatform::kUnknown:
291 case TargetPlatform::kSkSL:
292 case TargetPlatform::kOpenGLES:
293 case TargetPlatform::kOpenGLDesktop:
294 case TargetPlatform::kRuntimeStageGLES:
295 case TargetPlatform::kRuntimeStageVulkan:
296 case TargetPlatform::kVulkan:
297 return false;
298 }
299 FML_UNREACHABLE();
300 }
301
302 bool TargetPlatformIsVulkan(TargetPlatform platform) {
303 switch (platform) {
304 case TargetPlatform::kRuntimeStageVulkan:
305 case TargetPlatform::kVulkan:
306 return true;
307 case TargetPlatform::kMetalDesktop:
308 case TargetPlatform::kMetalIOS:
309 case TargetPlatform::kRuntimeStageMetal:
310 case TargetPlatform::kUnknown:
311 case TargetPlatform::kSkSL:
312 case TargetPlatform::kOpenGLES:
313 case TargetPlatform::kOpenGLDesktop:
314 case TargetPlatform::kRuntimeStageGLES:
315 return false;
316 }
317 FML_UNREACHABLE();
318 }
319
320 bool TargetPlatformBundlesSkSL(TargetPlatform platform) {
321 switch (platform) {
322 case TargetPlatform::kSkSL:
323 case TargetPlatform::kRuntimeStageMetal:
324 case TargetPlatform::kRuntimeStageGLES:
325 case TargetPlatform::kRuntimeStageVulkan:
326 return true;
327 case TargetPlatform::kMetalDesktop:
328 case TargetPlatform::kMetalIOS:
329 case TargetPlatform::kUnknown:
330 case TargetPlatform::kOpenGLES:
331 case TargetPlatform::kOpenGLDesktop:
332 case TargetPlatform::kVulkan:
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:28
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:21
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:41
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_context.cc:10
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