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