| 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 SourceType SourceTypeFromFileName(const std::filesystem::path& file_name) {
18 std::string extension = file_name.extension().string();
19 if (extension == ".vert") {
20 return SourceType::kVertexShader;
21 }
22
23 if (extension == ".frag") {
24 return SourceType::kFragmentShader;
25 }
26
27 if (extension == ".comp") {
28 return SourceType::kComputeShader;
29 }
30
31 return SourceType::kUnknown;
32 }
33
34 SourceType SourceTypeFromString(std::string name) {
35 name = ToLowerCase(name);
36
37 if (name == "vertex") {
38 return SourceType::kVertexShader;
39 }
40
41 if (name == "fragment") {
42 return SourceType::kFragmentShader;
43 }
44
45 if (name == "compute") {
46 return SourceType::kComputeShader;
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 }
59 return SourceLanguage::kUnknown;
60 }
61
62 std::string TargetPlatformToString(TargetPlatform platform) {
63 switch (platform) {
64 case TargetPlatform::kUnknown:
65 return "Unknown";
66 case TargetPlatform::kMetalDesktop:
67 return "MetalDesktop";
68 case TargetPlatform::kMetalIOS:
69 return "MetaliOS";
70 case TargetPlatform::kOpenGLES:
71 return "OpenGLES";
72 case TargetPlatform::kOpenGLDesktop:
73 return "OpenGLDesktop";
74 case TargetPlatform::kVulkan:
75 return "Vulkan";
76 case TargetPlatform::kRuntimeStageMetal:
77 return "RuntimeStageMetal";
78 case TargetPlatform::kRuntimeStageGLES:
79 return "RuntimeStageGLES";
80 case TargetPlatform::kRuntimeStageGLES3:
81 return "RuntimeStageGLES3";
82 case TargetPlatform::kRuntimeStageVulkan:
83 return "RuntimeStageVulkan";
84 case TargetPlatform::kSkSL:
85 return "SkSL";
86 }
87 FML_UNREACHABLE();
88 }
89
90 std::string SourceLanguageToString(SourceLanguage source_language) {
91 switch (source_language) {
92 case SourceLanguage::kUnknown:
93 return "Unknown";
94 case SourceLanguage::kGLSL:
95 return "GLSL";
96 case SourceLanguage::kHLSL:
97 return "HLSL";
98 }
99 }
100
101 std::string EntryPointFunctionNameFromSourceName(
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) {
113 case SourceType::kUnknown:
114 stream << "unknown";
115 break;
116 case SourceType::kVertexShader:
117 stream << "vertex";
118 break;
119 case SourceType::kFragmentShader:
120 stream << "fragment";
121 break;
122 case SourceType::kComputeShader:
123 stream << "compute";
124 break;
125 }
126 stream << "_main";
127 return stream.str();
128 }
129
130 bool TargetPlatformNeedsReflection(TargetPlatform platform) {
131 switch (platform) {
132 case TargetPlatform::kMetalIOS:
133 case TargetPlatform::kMetalDesktop:
134 case TargetPlatform::kOpenGLES:
135 case TargetPlatform::kOpenGLDesktop:
136 case TargetPlatform::kRuntimeStageMetal:
137 case TargetPlatform::kRuntimeStageGLES:
138 case TargetPlatform::kRuntimeStageGLES3:
139 case TargetPlatform::kRuntimeStageVulkan:
140 case TargetPlatform::kVulkan:
141 return true;
142 case TargetPlatform::kUnknown:
143 case TargetPlatform::kSkSL:
144 return false;
145 }
146 FML_UNREACHABLE();
147 }
148
149 std::string ShaderCErrorToString(shaderc_compilation_status status) {
150 using Status = shaderc_compilation_status;
151 switch (status) {
152 case Status::shaderc_compilation_status_success:
153 return "Success";
154 case Status::shaderc_compilation_status_invalid_stage:
155 return "Invalid shader stage specified";
156 case Status::shaderc_compilation_status_compilation_error:
157 return "Compilation error";
158 case Status::shaderc_compilation_status_internal_error:
159 return "Internal error";
160 case Status::shaderc_compilation_status_null_result_object:
161 return "Internal error. Null result object";
162 case Status::shaderc_compilation_status_invalid_assembly:
163 return "Invalid assembly";
164 case Status::shaderc_compilation_status_validation_error:
165 return "Validation error";
166 case Status::shaderc_compilation_status_transformation_error:
167 return "Transform error";
168 case Status::shaderc_compilation_status_configuration_error:
169 return "Configuration error";
170 }
171 return "Unknown internal error";
172 }
173
174 shaderc_shader_kind ToShaderCShaderKind(SourceType type) {
175 switch (type) {
176 case SourceType::kVertexShader:
177 return shaderc_shader_kind::shaderc_vertex_shader;
178 case SourceType::kFragmentShader:
179 return shaderc_shader_kind::shaderc_fragment_shader;
180 case SourceType::kComputeShader:
181 return shaderc_shader_kind::shaderc_compute_shader;
182 case SourceType::kUnknown:
183 break;
184 }
185 return shaderc_shader_kind::shaderc_glsl_infer_from_source;
186 }
187
188 spv::ExecutionModel ToExecutionModel(SourceType type) {
189 switch (type) {
190 case SourceType::kVertexShader:
191 return spv::ExecutionModel::ExecutionModelVertex;
192 case SourceType::kFragmentShader:
193 return spv::ExecutionModel::ExecutionModelFragment;
194 case SourceType::kComputeShader:
195 return spv::ExecutionModel::ExecutionModelGLCompute;
196 case SourceType::kUnknown:
197 break;
198 }
199 return spv::ExecutionModel::ExecutionModelMax;
200 }
201
202 spirv_cross::CompilerMSL::Options::Platform TargetPlatformToMSLPlatform(
203 TargetPlatform platform) {
204 switch (platform) {
205 case TargetPlatform::kMetalIOS:
206 case TargetPlatform::kRuntimeStageMetal:
207 return spirv_cross::CompilerMSL::Options::Platform::iOS;
208 case TargetPlatform::kMetalDesktop:
209 return spirv_cross::CompilerMSL::Options::Platform::macOS;
210 case TargetPlatform::kSkSL:
211 case TargetPlatform::kOpenGLES:
212 case TargetPlatform::kOpenGLDesktop:
213 case TargetPlatform::kRuntimeStageGLES:
214 case TargetPlatform::kRuntimeStageGLES3:
215 case TargetPlatform::kRuntimeStageVulkan:
216 case TargetPlatform::kVulkan:
217 case TargetPlatform::kUnknown:
218 return spirv_cross::CompilerMSL::Options::Platform::macOS;
219 }
220 FML_UNREACHABLE();
221 }
222
223 std::string SourceTypeToString(SourceType type) {
224 switch (type) {
225 case SourceType::kUnknown:
226 return "unknown";
227 case SourceType::kVertexShader:
228 return "vert";
229 case SourceType::kFragmentShader:
230 return "frag";
231 case SourceType::kComputeShader:
232 return "comp";
233 }
234 FML_UNREACHABLE();
235 }
236
237 std::string TargetPlatformSLExtension(TargetPlatform platform) {
238 switch (platform) {
239 case TargetPlatform::kUnknown:
240 return "unknown";
241 case TargetPlatform::kMetalDesktop:
242 case TargetPlatform::kMetalIOS:
243 case TargetPlatform::kRuntimeStageMetal:
244 return "metal";
245 case TargetPlatform::kSkSL:
246 case TargetPlatform::kOpenGLES:
247 case TargetPlatform::kOpenGLDesktop:
248 case TargetPlatform::kRuntimeStageGLES:
249 case TargetPlatform::kRuntimeStageGLES3:
250 return "glsl";
251 case TargetPlatform::kVulkan:
252 case TargetPlatform::kRuntimeStageVulkan:
253 return "vk.spirv";
254 }
255 FML_UNREACHABLE();
256 }
257
258 bool TargetPlatformIsOpenGL(TargetPlatform platform) {
259 switch (platform) {
260 case TargetPlatform::kOpenGLES:
261 case TargetPlatform::kOpenGLDesktop:
262 case TargetPlatform::kRuntimeStageGLES:
263 case TargetPlatform::kRuntimeStageGLES3:
264 return true;
265 case TargetPlatform::kMetalDesktop:
266 case TargetPlatform::kRuntimeStageMetal:
267 case TargetPlatform::kRuntimeStageVulkan:
268 case TargetPlatform::kMetalIOS:
269 case TargetPlatform::kUnknown:
270 case TargetPlatform::kSkSL:
271 case TargetPlatform::kVulkan:
272 return false;
273 }
274 FML_UNREACHABLE();
275 }
276
277 bool TargetPlatformIsMetal(TargetPlatform platform) {
278 switch (platform) {
279 case TargetPlatform::kMetalDesktop:
280 case TargetPlatform::kMetalIOS:
281 case TargetPlatform::kRuntimeStageMetal:
282 return true;
283 case TargetPlatform::kUnknown:
284 case TargetPlatform::kSkSL:
285 case TargetPlatform::kOpenGLES:
286 case TargetPlatform::kOpenGLDesktop:
287 case TargetPlatform::kRuntimeStageGLES:
288 case TargetPlatform::kRuntimeStageGLES3:
289 case TargetPlatform::kRuntimeStageVulkan:
290 case TargetPlatform::kVulkan:
291 return false;
292 }
293 FML_UNREACHABLE();
294 }
295
296 bool TargetPlatformIsVulkan(TargetPlatform platform) {
297 switch (platform) {
298 case TargetPlatform::kRuntimeStageVulkan:
299 case TargetPlatform::kVulkan:
300 return true;
301 case TargetPlatform::kMetalDesktop:
302 case TargetPlatform::kMetalIOS:
303 case TargetPlatform::kRuntimeStageMetal:
304 case TargetPlatform::kUnknown:
305 case TargetPlatform::kSkSL:
306 case TargetPlatform::kOpenGLES:
307 case TargetPlatform::kOpenGLDesktop:
308 case TargetPlatform::kRuntimeStageGLES:
309 case TargetPlatform::kRuntimeStageGLES3:
310 return false;
311 }
312 FML_UNREACHABLE();
313 }
314
315 bool TargetPlatformBundlesSkSL(TargetPlatform platform) {
316 switch (platform) {
317 case TargetPlatform::kSkSL:
318 case TargetPlatform::kRuntimeStageMetal:
319 case TargetPlatform::kRuntimeStageGLES:
320 case TargetPlatform::kRuntimeStageGLES3:
321 case TargetPlatform::kRuntimeStageVulkan:
322 return true;
323 case TargetPlatform::kMetalDesktop:
324 case TargetPlatform::kMetalIOS:
325 case TargetPlatform::kUnknown:
326 case TargetPlatform::kOpenGLES:
327 case TargetPlatform::kOpenGLDesktop:
328 case TargetPlatform::kVulkan:
329 return false;
330 }
331 FML_UNREACHABLE();
332 }
333
334 } // namespace compiler
335 } // namespace impeller
type
GLenum type
Definition: blit_command_gles.cc:153
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:68
impeller::compiler::ToLowerCase
std::string ToLowerCase(std::string_view string)
Definition: utilities.cc:61
impeller::compiler::SourceType
SourceType
Definition: types.h:22
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:90
impeller::compiler::TargetPlatformToString
std::string TargetPlatformToString(TargetPlatform platform)
Definition: types.cc:62
impeller::compiler::TargetPlatformSLExtension
std::string TargetPlatformSLExtension(TargetPlatform platform)
Definition: types.cc:237
impeller::compiler::ToShaderCShaderKind
shaderc_shader_kind ToShaderCShaderKind(SourceType type)
Definition: types.cc:174
impeller::compiler::SourceTypeToString
std::string SourceTypeToString(SourceType type)
Definition: types.cc:223
impeller::compiler::SourceTypeFromFileName
SourceType SourceTypeFromFileName(const std::filesystem::path &file_name)
Definition: types.cc:17
impeller::compiler::SourceLanguage
SourceLanguage
Definition: types.h:43
impeller::compiler::SourceLanguage::kUnknown
@ kUnknown
impeller::compiler::SourceLanguage::kGLSL
@ kGLSL
impeller::compiler::SourceLanguage::kHLSL
@ kHLSL
impeller::compiler::EntryPointFunctionNameFromSourceName
std::string EntryPointFunctionNameFromSourceName(const std::filesystem::path &file_name, SourceType type, SourceLanguage source_language, const std::string &entry_point_name)
Definition: types.cc:101
impeller::compiler::SourceTypeFromString
SourceType SourceTypeFromString(std::string name)
Definition: types.cc:34
impeller::compiler::TargetPlatformIsMetal
bool TargetPlatformIsMetal(TargetPlatform platform)
Definition: types.cc:277
impeller::compiler::TargetPlatformIsOpenGL
bool TargetPlatformIsOpenGL(TargetPlatform platform)
Definition: types.cc:258
impeller::compiler::ShaderCErrorToString
std::string ShaderCErrorToString(shaderc_compilation_status status)
Definition: types.cc:149
impeller::compiler::TargetPlatformIsVulkan
bool TargetPlatformIsVulkan(TargetPlatform platform)
Definition: types.cc:296
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:315
impeller::compiler::TargetPlatformToMSLPlatform
spirv_cross::CompilerMSL::Options::Platform TargetPlatformToMSLPlatform(TargetPlatform platform)
Definition: types.cc:202
impeller::compiler::ToSourceLanguage
SourceLanguage ToSourceLanguage(const std::string &source_language)
Definition: types.cc:52
impeller::compiler::TargetPlatformNeedsReflection
bool TargetPlatformNeedsReflection(TargetPlatform platform)
Definition: types.cc:130
impeller::compiler::TargetPlatform
TargetPlatform
Definition: types.h:29
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:188
impeller
Definition: allocation.cc:12
utilities.h