| 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::kRuntimeStageGLES3:
93 return "RuntimeStageGLES3";
94 case TargetPlatform::kRuntimeStageVulkan:
95 return "RuntimeStageVulkan";
96 case TargetPlatform::kSkSL:
97 return "SkSL";
98 }
99 FML_UNREACHABLE();
100 }
101
102 std::string SourceLanguageToString(SourceLanguage source_language) {
103 switch (source_language) {
104 case SourceLanguage::kUnknown:
105 return "Unknown";
106 case SourceLanguage::kGLSL:
107 return "GLSL";
108 case SourceLanguage::kHLSL:
109 return "HLSL";
110 }
111 }
112
113 std::string EntryPointFunctionNameFromSourceName(
114 const std::string& file_name,
115 SourceType type,
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) {
126 case SourceType::kUnknown:
127 stream << "unknown";
128 break;
129 case SourceType::kVertexShader:
130 stream << "vertex";
131 break;
132 case SourceType::kFragmentShader:
133 stream << "fragment";
134 break;
135 case SourceType::kComputeShader:
136 stream << "compute";
137 break;
138 }
139 stream << "_main";
140 return stream.str();
141 }
142
143 bool TargetPlatformNeedsReflection(TargetPlatform platform) {
144 switch (platform) {
145 case TargetPlatform::kMetalIOS:
146 case TargetPlatform::kMetalDesktop:
147 case TargetPlatform::kOpenGLES:
148 case TargetPlatform::kOpenGLDesktop:
149 case TargetPlatform::kRuntimeStageMetal:
150 case TargetPlatform::kRuntimeStageGLES:
151 case TargetPlatform::kRuntimeStageGLES3:
152 case TargetPlatform::kRuntimeStageVulkan:
153 case TargetPlatform::kVulkan:
154 return true;
155 case TargetPlatform::kUnknown:
156 case TargetPlatform::kSkSL:
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) {
189 case SourceType::kVertexShader:
190 return shaderc_shader_kind::shaderc_vertex_shader;
191 case SourceType::kFragmentShader:
192 return shaderc_shader_kind::shaderc_fragment_shader;
193 case SourceType::kComputeShader:
194 return shaderc_shader_kind::shaderc_compute_shader;
195 case SourceType::kUnknown:
196 break;
197 }
198 return shaderc_shader_kind::shaderc_glsl_infer_from_source;
199 }
200
201 spv::ExecutionModel ToExecutionModel(SourceType type) {
202 switch (type) {
203 case SourceType::kVertexShader:
204 return spv::ExecutionModel::ExecutionModelVertex;
205 case SourceType::kFragmentShader:
206 return spv::ExecutionModel::ExecutionModelFragment;
207 case SourceType::kComputeShader:
208 return spv::ExecutionModel::ExecutionModelGLCompute;
209 case SourceType::kUnknown:
210 break;
211 }
212 return spv::ExecutionModel::ExecutionModelMax;
213 }
214
215 spirv_cross::CompilerMSL::Options::Platform TargetPlatformToMSLPlatform(
216 TargetPlatform platform) {
217 switch (platform) {
218 case TargetPlatform::kMetalIOS:
219 case TargetPlatform::kRuntimeStageMetal:
220 return spirv_cross::CompilerMSL::Options::Platform::iOS;
221 case TargetPlatform::kMetalDesktop:
222 return spirv_cross::CompilerMSL::Options::Platform::macOS;
223 case TargetPlatform::kSkSL:
224 case TargetPlatform::kOpenGLES:
225 case TargetPlatform::kOpenGLDesktop:
226 case TargetPlatform::kRuntimeStageGLES:
227 case TargetPlatform::kRuntimeStageGLES3:
228 case TargetPlatform::kRuntimeStageVulkan:
229 case TargetPlatform::kVulkan:
230 case TargetPlatform::kUnknown:
231 return spirv_cross::CompilerMSL::Options::Platform::macOS;
232 }
233 FML_UNREACHABLE();
234 }
235
236 std::string SourceTypeToString(SourceType type) {
237 switch (type) {
238 case SourceType::kUnknown:
239 return "unknown";
240 case SourceType::kVertexShader:
241 return "vert";
242 case SourceType::kFragmentShader:
243 return "frag";
244 case SourceType::kComputeShader:
245 return "comp";
246 }
247 FML_UNREACHABLE();
248 }
249
250 std::string TargetPlatformSLExtension(TargetPlatform platform) {
251 switch (platform) {
252 case TargetPlatform::kUnknown:
253 return "unknown";
254 case TargetPlatform::kMetalDesktop:
255 case TargetPlatform::kMetalIOS:
256 case TargetPlatform::kRuntimeStageMetal:
257 return "metal";
258 case TargetPlatform::kSkSL:
259 case TargetPlatform::kOpenGLES:
260 case TargetPlatform::kOpenGLDesktop:
261 case TargetPlatform::kRuntimeStageGLES:
262 case TargetPlatform::kRuntimeStageGLES3:
263 return "glsl";
264 case TargetPlatform::kVulkan:
265 case TargetPlatform::kRuntimeStageVulkan:
266 return "vk.spirv";
267 }
268 FML_UNREACHABLE();
269 }
270
271 bool TargetPlatformIsOpenGL(TargetPlatform platform) {
272 switch (platform) {
273 case TargetPlatform::kOpenGLES:
274 case TargetPlatform::kOpenGLDesktop:
275 case TargetPlatform::kRuntimeStageGLES:
276 case TargetPlatform::kRuntimeStageGLES3:
277 return true;
278 case TargetPlatform::kMetalDesktop:
279 case TargetPlatform::kRuntimeStageMetal:
280 case TargetPlatform::kRuntimeStageVulkan:
281 case TargetPlatform::kMetalIOS:
282 case TargetPlatform::kUnknown:
283 case TargetPlatform::kSkSL:
284 case TargetPlatform::kVulkan:
285 return false;
286 }
287 FML_UNREACHABLE();
288 }
289
290 bool TargetPlatformIsMetal(TargetPlatform platform) {
291 switch (platform) {
292 case TargetPlatform::kMetalDesktop:
293 case TargetPlatform::kMetalIOS:
294 case TargetPlatform::kRuntimeStageMetal:
295 return true;
296 case TargetPlatform::kUnknown:
297 case TargetPlatform::kSkSL:
298 case TargetPlatform::kOpenGLES:
299 case TargetPlatform::kOpenGLDesktop:
300 case TargetPlatform::kRuntimeStageGLES:
301 case TargetPlatform::kRuntimeStageGLES3:
302 case TargetPlatform::kRuntimeStageVulkan:
303 case TargetPlatform::kVulkan:
304 return false;
305 }
306 FML_UNREACHABLE();
307 }
308
309 bool TargetPlatformIsVulkan(TargetPlatform platform) {
310 switch (platform) {
311 case TargetPlatform::kRuntimeStageVulkan:
312 case TargetPlatform::kVulkan:
313 return true;
314 case TargetPlatform::kMetalDesktop:
315 case TargetPlatform::kMetalIOS:
316 case TargetPlatform::kRuntimeStageMetal:
317 case TargetPlatform::kUnknown:
318 case TargetPlatform::kSkSL:
319 case TargetPlatform::kOpenGLES:
320 case TargetPlatform::kOpenGLDesktop:
321 case TargetPlatform::kRuntimeStageGLES:
322 case TargetPlatform::kRuntimeStageGLES3:
323 return false;
324 }
325 FML_UNREACHABLE();
326 }
327
328 bool TargetPlatformBundlesSkSL(TargetPlatform platform) {
329 switch (platform) {
330 case TargetPlatform::kSkSL:
331 case TargetPlatform::kRuntimeStageMetal:
332 case TargetPlatform::kRuntimeStageGLES:
333 case TargetPlatform::kRuntimeStageGLES3:
334 case TargetPlatform::kRuntimeStageVulkan:
335 return true;
336 case TargetPlatform::kMetalDesktop:
337 case TargetPlatform::kMetalIOS:
338 case TargetPlatform::kUnknown:
339 case TargetPlatform::kOpenGLES:
340 case TargetPlatform::kOpenGLDesktop:
341 case TargetPlatform::kVulkan:
342 return false;
343 }
344 FML_UNREACHABLE();
345 }
346
347 } // namespace compiler
348 } // namespace impeller
type
GLenum type
Definition: blit_command_gles.cc:151
types.h
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::StringEndWith
static bool StringEndWith(const std::string &string, const std::string &suffix)
Definition: types.cc:17
impeller::compiler::SourceType
SourceType
Definition: types.h:21
impeller::compiler::SourceType::kUnknown
@ kUnknown
impeller::compiler::SourceType::kFragmentShader
@ kFragmentShader
impeller::compiler::SourceType::kVertexShader
@ kVertexShader
impeller::compiler::SourceType::kComputeShader
@ kComputeShader
impeller::compiler::SourceLanguageToString
std::string SourceLanguageToString(SourceLanguage source_language)
Definition: types.cc:102
impeller::compiler::TargetPlatformToString
std::string TargetPlatformToString(TargetPlatform platform)
Definition: types.cc:74
impeller::compiler::TargetPlatformSLExtension
std::string TargetPlatformSLExtension(TargetPlatform platform)
Definition: types.cc:250
impeller::compiler::ToShaderCShaderKind
shaderc_shader_kind ToShaderCShaderKind(SourceType type)
Definition: types.cc:187
impeller::compiler::SourceTypeToString
std::string SourceTypeToString(SourceType type)
Definition: types.cc:236
impeller::compiler::SourceLanguage
SourceLanguage
Definition: types.h:42
impeller::compiler::SourceLanguage::kUnknown
@ kUnknown
impeller::compiler::SourceLanguage::kGLSL
@ kGLSL
impeller::compiler::SourceLanguage::kHLSL
@ kHLSL
impeller::compiler::SourceTypeFromString
SourceType SourceTypeFromString(std::string name)
Definition: types.cc:46
impeller::compiler::SourceTypeFromFileName
SourceType SourceTypeFromFileName(const std::string &file_name)
Definition: types.cc:30
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:113
impeller::compiler::TargetPlatformIsMetal
bool TargetPlatformIsMetal(TargetPlatform platform)
Definition: types.cc:290
impeller::compiler::TargetPlatformIsOpenGL
bool TargetPlatformIsOpenGL(TargetPlatform platform)
Definition: types.cc:271
impeller::compiler::ShaderCErrorToString
std::string ShaderCErrorToString(shaderc_compilation_status status)
Definition: types.cc:162
impeller::compiler::TargetPlatformIsVulkan
bool TargetPlatformIsVulkan(TargetPlatform platform)
Definition: types.cc:309
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::TargetPlatformBundlesSkSL
bool TargetPlatformBundlesSkSL(TargetPlatform platform)
Definition: types.cc:328
impeller::compiler::TargetPlatformToMSLPlatform
spirv_cross::CompilerMSL::Options::Platform TargetPlatformToMSLPlatform(TargetPlatform platform)
Definition: types.cc:215
impeller::compiler::ToSourceLanguage
SourceLanguage ToSourceLanguage(const std::string &source_language)
Definition: types.cc:64
impeller::compiler::TargetPlatformNeedsReflection
bool TargetPlatformNeedsReflection(TargetPlatform platform)
Definition: types.cc:143
impeller::compiler::TargetPlatform
TargetPlatform
Definition: types.h:28
impeller::compiler::TargetPlatform::kRuntimeStageMetal
@ kRuntimeStageMetal
impeller::compiler::TargetPlatform::kOpenGLES
@ kOpenGLES
impeller::compiler::TargetPlatform::kUnknown
@ kUnknown
impeller::compiler::TargetPlatform::kRuntimeStageVulkan
@ kRuntimeStageVulkan
impeller::compiler::TargetPlatform::kRuntimeStageGLES3
@ kRuntimeStageGLES3
impeller::compiler::TargetPlatform::kVulkan
@ kVulkan
impeller::compiler::TargetPlatform::kMetalDesktop
@ kMetalDesktop
impeller::compiler::TargetPlatform::kMetalIOS
@ kMetalIOS
impeller::compiler::TargetPlatform::kSkSL
@ kSkSL
impeller::compiler::TargetPlatform::kOpenGLDesktop
@ kOpenGLDesktop
impeller::compiler::TargetPlatform::kRuntimeStageGLES
@ kRuntimeStageGLES
impeller::compiler::ToExecutionModel
spv::ExecutionModel ToExecutionModel(SourceType type)
Definition: types.cc:201
impeller
Definition: allocation.cc:12
utilities.h