| Flutter Impeller |

switches.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/switches.h"
6
7 #include <algorithm>
8 #include <cctype>
9 #include <filesystem>
10 #include <map>
11
12 #include "flutter/fml/file.h"
13 #include "fml/command_line.h"
14 #include "impeller/compiler/types.h"
15 #include "impeller/compiler/utilities.h"
16
17 namespace impeller {
18 namespace compiler {
19
20 static const std::map<std::string, TargetPlatform> kKnownPlatforms = {
21 {"metal-desktop", TargetPlatform::kMetalDesktop},
22 {"metal-ios", TargetPlatform::kMetalIOS},
23 {"vulkan", TargetPlatform::kVulkan},
24 {"opengl-es", TargetPlatform::kOpenGLES},
25 {"opengl-desktop", TargetPlatform::kOpenGLDesktop},
26 };
27
28 static const std::map<std::string, TargetPlatform> kKnownRuntimeStages = {
29 {"sksl", TargetPlatform::kSkSL},
30 {"runtime-stage-metal", TargetPlatform::kRuntimeStageMetal},
31 {"runtime-stage-gles", TargetPlatform::kRuntimeStageGLES},
32 {"runtime-stage-vulkan", TargetPlatform::kRuntimeStageVulkan},
33 };
34
35 static const std::map<std::string, SourceType> kKnownSourceTypes = {
36 {"vert", SourceType::kVertexShader},
37 {"frag", SourceType::kFragmentShader},
38 {"comp", SourceType::kComputeShader},
39 };
40
41 void Switches::PrintHelp(std::ostream& stream) {
42 // clang-format off
43 const std::string optional_prefix = "[optional] ";
44 const std::string optional_multiple_prefix = "[optional,multiple] ";
45 // clang-format on
46
47 stream << std::endl;
48 stream << "ImpellerC is an offline shader processor and reflection engine."
49 << std::endl;
50 stream << "---------------------------------------------------------------"
51 << std::endl;
52 stream << "Expected invocation is:" << std::endl << std::endl;
53 stream << "./impellerc <One platform or multiple runtime stages> "
54 "--input=<source_file> --sl=<sl_output_file> <optional arguments>"
55 << std::endl
56 << std::endl;
57
58 stream << "Valid platforms are:" << std::endl << std::endl;
59 stream << "One of [";
60 for (const auto& platform : kKnownPlatforms) {
61 stream << " --" << platform.first;
62 }
63 stream << " ]" << std::endl << std::endl;
64
65 stream << "Valid runtime stages are:" << std::endl << std::endl;
66 stream << "At least one of [";
67 for (const auto& platform : kKnownRuntimeStages) {
68 stream << " --" << platform.first;
69 }
70 stream << " ]" << std::endl << std::endl;
71
72 stream << "Optional arguments:" << std::endl << std::endl;
73 stream << optional_prefix
74 << "--spirv=<spirv_output_file> (ignored for --shader-bundle)"
75 << std::endl;
76 stream << optional_prefix << "--input-type={";
77 for (const auto& source_type : kKnownSourceTypes) {
78 stream << source_type.first << ", ";
79 }
80 stream << "}" << std::endl;
81 stream << optional_prefix << "--source-language=glsl|hlsl (default: glsl)"
82 << std::endl;
83 stream << optional_prefix
84 << "--entry-point=<entry_point_name> (default: main; "
85 "ignored for glsl)"
86 << std::endl;
87 stream << optional_prefix
88 << "--iplr (causes --sl file to be emitted in "
89 "iplr format)"
90 << std::endl;
91 stream << optional_prefix
92 << "--shader-bundle=<bundle_spec> (causes --sl "
93 "file to be "
94 "emitted in Flutter GPU's shader bundle format)"
95 << std::endl;
96 stream << optional_prefix << "--reflection-json=<reflection_json_file>"
97 << std::endl;
98 stream << optional_prefix << "--reflection-header=<reflection_header_file>"
99 << std::endl;
100 stream << optional_prefix << "--reflection-cc=<reflection_cc_file>"
101 << std::endl;
102 stream << optional_multiple_prefix << "--include=<include_directory>"
103 << std::endl;
104 stream << optional_multiple_prefix << "--define=<define>" << std::endl;
105 stream << optional_prefix << "--depfile=<depfile_path>" << std::endl;
106 stream << optional_prefix << "--gles-language-version=<number>" << std::endl;
107 stream << optional_prefix << "--json" << std::endl;
108 stream << optional_prefix
109 << "--use-half-textures (force openGL semantics when "
110 "targeting metal)"
111 << std::endl;
112 stream << optional_prefix << "--require-framebuffer-fetch" << std::endl;
113 }
114
115 Switches::Switches() = default;
116
117 Switches::~Switches() = default;
118
119 static TargetPlatform TargetPlatformFromCommandLine(
120 const fml::CommandLine& command_line) {
121 auto target = TargetPlatform::kUnknown;
122 for (const auto& platform : kKnownPlatforms) {
123 if (command_line.HasOption(platform.first)) {
124 // If the platform has already been determined, the caller may have
125 // specified multiple platforms. This is an error and only one must be
126 // selected.
127 if (target != TargetPlatform::kUnknown) {
128 return TargetPlatform::kUnknown;
129 }
130 target = platform.second;
131 // Keep going to detect duplicates.
132 }
133 }
134 return target;
135 }
136
137 static std::vector<TargetPlatform> RuntimeStagesFromCommandLine(
138 const fml::CommandLine& command_line) {
139 std::vector<TargetPlatform> stages;
140 for (const auto& platform : kKnownRuntimeStages) {
141 if (command_line.HasOption(platform.first)) {
142 stages.push_back(platform.second);
143 }
144 }
145 return stages;
146 }
147
148 static SourceType SourceTypeFromCommandLine(
149 const fml::CommandLine& command_line) {
150 auto source_type_option =
151 command_line.GetOptionValueWithDefault("input-type", "");
152 auto source_type_search = kKnownSourceTypes.find(source_type_option);
153 if (source_type_search == kKnownSourceTypes.end()) {
154 return SourceType::kUnknown;
155 }
156 return source_type_search->second;
157 }
158
159 Switches::Switches(const fml::CommandLine& command_line)
160 : working_directory(std::make_shared<fml::UniqueFD>(fml::OpenDirectory(
161 Utf8FromPath(std::filesystem::current_path()).c_str(),
162 false, // create if necessary,
163 fml::FilePermission::kRead))),
164 source_file_name(command_line.GetOptionValueWithDefault("input", "")),
165 input_type(SourceTypeFromCommandLine(command_line)),
166 sl_file_name(command_line.GetOptionValueWithDefault("sl", "")),
167 iplr(command_line.HasOption("iplr")),
168 shader_bundle(
169 command_line.GetOptionValueWithDefault("shader-bundle", "")),
170 spirv_file_name(command_line.GetOptionValueWithDefault("spirv", "")),
171 reflection_json_name(
172 command_line.GetOptionValueWithDefault("reflection-json", "")),
173 reflection_header_name(
174 command_line.GetOptionValueWithDefault("reflection-header", "")),
175 reflection_cc_name(
176 command_line.GetOptionValueWithDefault("reflection-cc", "")),
177 depfile_path(command_line.GetOptionValueWithDefault("depfile", "")),
178 json_format(command_line.HasOption("json")),
179 gles_language_version(
180 stoi(command_line.GetOptionValueWithDefault("gles-language-version",
181 "0"))),
182 metal_version(
183 command_line.GetOptionValueWithDefault("metal-version", "1.2")),
184 entry_point(
185 command_line.GetOptionValueWithDefault("entry-point", "main")),
186 use_half_textures(command_line.HasOption("use-half-textures")),
187 require_framebuffer_fetch(
188 command_line.HasOption("require-framebuffer-fetch")),
189 target_platform_(TargetPlatformFromCommandLine(command_line)),
190 runtime_stages_(RuntimeStagesFromCommandLine(command_line)) {
191 auto language = ToLowerCase(
192 command_line.GetOptionValueWithDefault("source-language", "glsl"));
193
194 source_language = ToSourceLanguage(language);
195
196 if (!working_directory || !working_directory->is_valid()) {
197 return;
198 }
199
200 for (const auto& include_dir_path : command_line.GetOptionValues("include")) {
201 if (!include_dir_path.data()) {
202 continue;
203 }
204
205 // fml::OpenDirectoryReadOnly for Windows doesn't handle relative paths
206 // beginning with `../` well, so we build an absolute path.
207
208 // Get the current working directory as a utf8 encoded string.
209 // Note that the `include_dir_path` is already utf8 encoded, and so we
210 // mustn't attempt to double-convert it to utf8 lest multi-byte characters
211 // will become mangled.
212 std::filesystem::path include_dir_absolute;
213 if (std::filesystem::path(include_dir_path).is_absolute()) {
214 include_dir_absolute = std::filesystem::path(include_dir_path);
215 } else {
216 auto cwd = Utf8FromPath(std::filesystem::current_path());
217 include_dir_absolute = std::filesystem::absolute(
218 std::filesystem::path(cwd) / include_dir_path);
219 }
220
221 auto dir = std::make_shared<fml::UniqueFD>(fml::OpenDirectoryReadOnly(
222 *working_directory, include_dir_absolute.string().c_str()));
223 if (!dir || !dir->is_valid()) {
224 continue;
225 }
226
227 IncludeDir dir_entry;
228 dir_entry.name = include_dir_path;
229 dir_entry.dir = std::move(dir);
230
231 include_directories.emplace_back(std::move(dir_entry));
232 }
233
234 for (const auto& define : command_line.GetOptionValues("define")) {
235 defines.emplace_back(define);
236 }
237 }
238
239 bool Switches::AreValid(std::ostream& explain) const {
240 // When producing a shader bundle, all flags related to single shader inputs
241 // and outputs such as `--input` and `--spirv-file-name` are ignored. Instead,
242 // input files are read from the shader bundle spec and a single flatbuffer
243 // containing all compiled shaders and reflection state is output to `--sl`.
244 const bool shader_bundle_mode = !shader_bundle.empty();
245
246 bool valid = true;
247 if (target_platform_ == TargetPlatform::kUnknown && runtime_stages_.empty()) {
248 explain << "Either a target platform was not specified, or no runtime "
249 "stages were specified."
250 << std::endl;
251 valid = false;
252 }
253
254 if (source_language == SourceLanguage::kUnknown) {
255 explain << "Invalid source language type." << std::endl;
256 valid = false;
257 }
258
259 if (!working_directory || !working_directory->is_valid()) {
260 explain << "Could not open the working directory: \""
261 << Utf8FromPath(std::filesystem::current_path()).c_str() << "\""
262 << std::endl;
263 valid = false;
264 }
265
266 if (source_file_name.empty() && !shader_bundle_mode) {
267 explain << "Input file name was empty." << std::endl;
268 valid = false;
269 }
270
271 if (sl_file_name.empty()) {
272 explain << "Target shading language file name was empty." << std::endl;
273 valid = false;
274 }
275
276 if (spirv_file_name.empty() && !shader_bundle_mode) {
277 explain << "Spirv file name was empty." << std::endl;
278 valid = false;
279 }
280
281 if (iplr && shader_bundle_mode) {
282 explain << "--iplr and --shader-bundle flag cannot be specified at the "
283 "same time"
284 << std::endl;
285 valid = false;
286 }
287
288 return valid;
289 }
290
291 std::vector<TargetPlatform> Switches::PlatformsToCompile() const {
292 if (target_platform_ == TargetPlatform::kUnknown) {
293 return runtime_stages_;
294 }
295 return {target_platform_};
296 }
297
298 TargetPlatform Switches::SelectDefaultTargetPlatform() const {
299 if (target_platform_ == TargetPlatform::kUnknown) {
300 return runtime_stages_.front();
301 }
302 return target_platform_;
303 }
304
305 SourceOptions Switches::CreateSourceOptions(
306 std::optional<TargetPlatform> target_platform) const {
307 SourceOptions options;
308 options.target_platform =
309 target_platform.value_or(SelectDefaultTargetPlatform());
310 options.source_language = source_language;
311 if (input_type == SourceType::kUnknown) {
312 options.type = SourceTypeFromFileName(source_file_name);
313 } else {
314 options.type = input_type;
315 }
316 options.working_directory = working_directory;
317 options.file_name = source_file_name;
318 options.include_dirs = include_directories;
319 options.defines = defines;
320 options.entry_point_name = EntryPointFunctionNameFromSourceName(
321 source_file_name, options.type, options.source_language, entry_point);
322 options.json_format = json_format;
323 options.gles_language_version = gles_language_version;
324 options.metal_version = metal_version;
325 options.use_half_textures = use_half_textures;
326 options.require_framebuffer_fetch = require_framebuffer_fetch;
327 return options;
328 }
329
330 } // namespace compiler
331 } // namespace impeller
impeller::compiler::RuntimeStagesFromCommandLine
static std::vector< TargetPlatform > RuntimeStagesFromCommandLine(const fml::CommandLine &command_line)
Definition: switches.cc:137
impeller::compiler::SourceOptions::type
SourceType type
Definition: source_options.h:21
impeller::compiler::ToLowerCase
std::string ToLowerCase(std::string_view string)
Definition: utilities.cc:62
impeller::compiler::SourceOptions::include_dirs
std::vector< IncludeDir > include_dirs
Definition: source_options.h:25
impeller::compiler::SourceType::kUnknown
@ kUnknown
impeller::compiler::Switches::PrintHelp
static void PrintHelp(std::ostream &stream)
Definition: switches.cc:41
impeller::compiler::TargetPlatform::kMetalDesktop
@ kMetalDesktop
impeller::compiler::Switches::SelectDefaultTargetPlatform
TargetPlatform SelectDefaultTargetPlatform() const
Definition: switches.cc:298
impeller::compiler::SourceOptions::metal_version
std::string metal_version
Definition: source_options.h:31
impeller::compiler::SourceOptions
Definition: source_options.h:20
impeller::compiler::SourceOptions::use_half_textures
bool use_half_textures
Whether half-precision textures should be supported, requiring opengl semantics. Only used on metal t...
Definition: source_options.h:35
impeller::compiler::Switches::metal_version
std::string metal_version
Definition: switches.h:42
impeller::compiler::Switches::gles_language_version
uint32_t gles_language_version
Definition: switches.h:41
impeller::compiler::SourceOptions::json_format
bool json_format
Definition: source_options.h:30
impeller::compiler::TargetPlatform::kMetalIOS
@ kMetalIOS
switches.h
impeller::compiler::TargetPlatform
TargetPlatform
Definition: types.h:28
impeller::compiler::Switches::spirv_file_name
std::string spirv_file_name
Definition: switches.h:33
impeller::compiler::kKnownPlatforms
static const std::map< std::string, TargetPlatform > kKnownPlatforms
Definition: switches.cc:20
impeller::compiler::Switches::json_format
bool json_format
Definition: switches.h:39
impeller::compiler::Switches::use_half_textures
bool use_half_textures
Definition: switches.h:44
impeller::compiler::IncludeDir::name
std::string name
Definition: include_dir.h:18
impeller::compiler::SourceOptions::source_language
SourceLanguage source_language
Definition: source_options.h:23
impeller::compiler::TargetPlatformFromCommandLine
static TargetPlatform TargetPlatformFromCommandLine(const fml::CommandLine &command_line)
Definition: switches.cc:119
impeller::compiler::ToSourceLanguage
SourceLanguage ToSourceLanguage(const std::string &source_language)
Definition: types.cc:64
impeller::compiler::Switches::require_framebuffer_fetch
bool require_framebuffer_fetch
Definition: switches.h:45
impeller::compiler::TargetPlatform::kVulkan
@ kVulkan
impeller::compiler::SourceOptions::entry_point_name
std::string entry_point_name
Definition: source_options.h:27
impeller::compiler::IncludeDir
Definition: include_dir.h:16
impeller::compiler::SourceType::kFragmentShader
@ kFragmentShader
impeller::compiler::Switches::defines
std::vector< std::string > defines
Definition: switches.h:38
impeller::compiler::SourceOptions::require_framebuffer_fetch
bool require_framebuffer_fetch
Whether the GLSL framebuffer fetch extension will be required.
Definition: source_options.h:40
impeller::compiler::TargetPlatform::kRuntimeStageVulkan
@ kRuntimeStageVulkan
impeller::compiler::SourceOptions::gles_language_version
uint32_t gles_language_version
Definition: source_options.h:28
impeller::compiler::SourceType::kComputeShader
@ kComputeShader
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::SourceOptions::file_name
std::string file_name
Definition: source_options.h:26
utilities.h
impeller::compiler::SourceType
SourceType
Definition: types.h:21
impeller::compiler::SourceLanguage::kUnknown
@ kUnknown
impeller::compiler::Switches::sl_file_name
std::string sl_file_name
Definition: switches.h:30
impeller::compiler::Switches::PlatformsToCompile
std::vector< TargetPlatform > PlatformsToCompile() const
A vector containing at least one valid platform.
Definition: switches.cc:291
impeller::compiler::SourceOptions::working_directory
std::shared_ptr< fml::UniqueFD > working_directory
Definition: source_options.h:24
impeller::compiler::SourceTypeFromFileName
SourceType SourceTypeFromFileName(const std::string &file_name)
Definition: types.cc:30
impeller::compiler::Switches::iplr
bool iplr
Definition: switches.h:31
impeller::compiler::SourceOptions::target_platform
TargetPlatform target_platform
Definition: source_options.h:22
impeller::compiler::Switches::input_type
SourceType input_type
Definition: switches.h:26
impeller::compiler::SourceTypeFromCommandLine
static SourceType SourceTypeFromCommandLine(const fml::CommandLine &command_line)
Definition: switches.cc:148
std
Definition: comparable.h:95
impeller::compiler::TargetPlatform::kOpenGLDesktop
@ kOpenGLDesktop
impeller::compiler::SourceOptions::defines
std::vector< std::string > defines
Definition: source_options.h:29
impeller::compiler::TargetPlatform::kUnknown
@ kUnknown
impeller::compiler::TargetPlatform::kOpenGLES
@ kOpenGLES
impeller::compiler::Switches::CreateSourceOptions
SourceOptions CreateSourceOptions(std::optional< TargetPlatform > target_platform=std::nullopt) const
Definition: switches.cc:305
impeller::compiler::Switches::source_language
SourceLanguage source_language
Definition: switches.h:40
impeller::compiler::Switches::entry_point
std::string entry_point
Definition: switches.h:43
impeller::compiler::kKnownSourceTypes
static const std::map< std::string, SourceType > kKnownSourceTypes
Definition: switches.cc:35
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::IncludeDir::dir
std::shared_ptr< fml::UniqueFD > dir
Definition: include_dir.h:17
impeller::compiler::Switches::include_directories
std::vector< IncludeDir > include_directories
Definition: switches.h:24
impeller::compiler::SourceType::kVertexShader
@ kVertexShader
impeller
Definition: aiks_context.cc:10
impeller::compiler::kKnownRuntimeStages
static const std::map< std::string, TargetPlatform > kKnownRuntimeStages
Definition: switches.cc:28
impeller::compiler::Switches::source_file_name
std::string source_file_name
Definition: switches.h:25
impeller::compiler::Switches::~Switches
~Switches()
impeller::compiler::Switches::working_directory
std::shared_ptr< fml::UniqueFD > working_directory
Definition: switches.h:23
types.h
impeller::compiler::TargetPlatform::kSkSL
@ kSkSL
impeller::compiler::TargetPlatform::kRuntimeStageGLES
@ kRuntimeStageGLES
impeller::compiler::Switches::shader_bundle
std::string shader_bundle
Definition: switches.h:32
impeller::compiler::Switches::Switches
Switches()
impeller::compiler::Switches::AreValid
bool AreValid(std::ostream &explain) const
Definition: switches.cc:239