Flutter Impeller
impeller::compiler::Switches Class Reference

#include <switches.h>

Public Member Functions

 Switches ()
 
 ~Switches ()
 
 Switches (const fml::CommandLine &command_line)
 
bool AreValid (std::ostream &explain) const
 
std::vector< TargetPlatformPlatformsToCompile () const
 A vector containing at least one valid platform. More...
 
SourceOptions CreateSourceOptions () const
 

Static Public Member Functions

static void PrintHelp (std::ostream &stream)
 

Public Attributes

std::shared_ptr< fml::UniqueFD > working_directory = nullptr
 
std::vector< IncludeDirinclude_directories = {}
 
std::filesystem::path source_file_name
 
SourceType input_type = SourceType::kUnknown
 
std::filesystem::path sl_file_name
 
bool iplr = false
 
std::string shader_bundle = ""
 
std::filesystem::path spirv_file_name
 
std::filesystem::path reflection_json_name
 
std::filesystem::path reflection_header_name
 
std::filesystem::path reflection_cc_name
 
std::filesystem::path depfile_path
 
std::vector< std::string > defines = {}
 
bool json_format = false
 
SourceLanguage source_language = SourceLanguage::kUnknown
 
uint32_t gles_language_version = 0
 
std::string metal_version = ""
 
std::string entry_point = ""
 
std::string entry_point_prefix = ""
 
bool use_half_textures = false
 
bool require_framebuffer_fetch = false
 
bool verbose = false
 

Detailed Description

Definition at line 22 of file switches.h.

Constructor & Destructor Documentation

◆ Switches() [1/2]

impeller::compiler::Switches::Switches ( )
default

◆ ~Switches()

impeller::compiler::Switches::~Switches ( )
default

◆ Switches() [2/2]

impeller::compiler::Switches::Switches ( const fml::CommandLine &  command_line)
explicit

Definition at line 178 of file switches.cc.

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")),
188  command_line.GetOptionValueWithDefault("shader-bundle", "")),
189  spirv_file_name(GetOptionAsPath(command_line, "spirv")),
190  reflection_json_name(GetOptionAsPath(command_line, "reflection-json")),
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")),
197  stoi(command_line.GetOptionValueWithDefault("gles-language-version",
198  "0"))),
200  command_line.GetOptionValueWithDefault("metal-version", "1.2")),
201  entry_point(
202  command_line.GetOptionValueWithDefault("entry-point", "main")),
204  command_line.GetOptionValueWithDefault("entry-point-prefix", "")),
205  use_half_textures(command_line.HasOption("use-half-textures")),
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 }
std::filesystem::path reflection_cc_name
Definition: switches.h:37
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
std::filesystem::path reflection_json_name
Definition: switches.h:35
std::filesystem::path spirv_file_name
Definition: switches.h:34
std::filesystem::path depfile_path
Definition: switches.h:38
std::vector< IncludeDir > include_directories
Definition: switches.h:25
std::filesystem::path source_file_name
Definition: switches.h:26
std::filesystem::path reflection_header_name
Definition: switches.h:36
std::string ToLowerCase(std::string_view string)
Definition: utilities.cc:61
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 Utf8FromPath(const std::filesystem::path &path)
Converts a native format path to a utf8 string.
Definition: utilities.cc:30
static TargetPlatform TargetPlatformFromCommandLine(const fml::CommandLine &command_line)
Definition: switches.cc:129
SourceLanguage ToSourceLanguage(const std::string &source_language)
Definition: types.cc:52
static SourceType SourceTypeFromCommandLine(const fml::CommandLine &command_line)
Definition: switches.cc:158

References defines, impeller::compiler::IncludeDir::dir, include_directories, impeller::compiler::IncludeDir::name, source_language, impeller::compiler::ToLowerCase(), impeller::compiler::ToSourceLanguage(), impeller::compiler::Utf8FromPath(), and working_directory.

Member Function Documentation

◆ AreValid()

bool impeller::compiler::Switches::AreValid ( std::ostream &  explain) const

Definition at line 259 of file switches.cc.

259  {
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 }

References iplr, impeller::compiler::kUnknown, shader_bundle, sl_file_name, source_file_name, source_language, spirv_file_name, impeller::compiler::Utf8FromPath(), and working_directory.

Referenced by impeller::compiler::Main(), and impeller::compiler::testing::TEST().

◆ CreateSourceOptions()

SourceOptions impeller::compiler::Switches::CreateSourceOptions ( ) const

Creates source options from these switches. The returned options does not have a set TargetPlatform because that cannot be determined based purely on the switches. Clients must set a valid TargetPlatform on the returned options before before it is used.

Definition at line 319 of file switches.cc.

319  {
320  SourceOptions options;
321  options.source_language = source_language;
324  } else {
325  options.type = input_type;
326  }
327  options.working_directory = working_directory;
328  options.file_name = source_file_name;
329  options.include_dirs = include_directories;
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;
336  options.gles_language_version = gles_language_version;
337  options.metal_version = metal_version;
338  options.use_half_textures = use_half_textures;
339  options.require_framebuffer_fetch = require_framebuffer_fetch;
340  return options;
341 }
SourceType SourceTypeFromFileName(const std::filesystem::path &file_name)
Definition: types.cc:17
std::string EntryPointFunctionNameFromSourceName(const std::filesystem::path &file_name, SourceType type, SourceLanguage source_language, const std::string &entry_point_name)
Definition: types.cc:101

References impeller::compiler::SourceOptions::defines, defines, entry_point, impeller::compiler::SourceOptions::entry_point_name, entry_point_prefix, impeller::compiler::EntryPointFunctionNameFromSourceName(), impeller::compiler::SourceOptions::file_name, impeller::compiler::SourceOptions::gles_language_version, gles_language_version, include_directories, impeller::compiler::SourceOptions::include_dirs, input_type, impeller::compiler::SourceOptions::json_format, json_format, impeller::compiler::kUnknown, impeller::compiler::SourceOptions::metal_version, metal_version, impeller::compiler::SourceOptions::require_framebuffer_fetch, require_framebuffer_fetch, source_file_name, impeller::compiler::SourceOptions::source_language, source_language, impeller::compiler::SourceTypeFromFileName(), impeller::compiler::SourceOptions::type, impeller::compiler::SourceOptions::use_half_textures, use_half_textures, impeller::compiler::SourceOptions::working_directory, and working_directory.

Referenced by impeller::compiler::GenerateShaderBundle(), and impeller::compiler::testing::TEST().

◆ PlatformsToCompile()

std::vector< TargetPlatform > impeller::compiler::Switches::PlatformsToCompile ( ) const

A vector containing at least one valid platform.

Definition at line 312 of file switches.cc.

312  {
313  if (target_platform_ == TargetPlatform::kUnknown) {
314  return runtime_stages_;
315  }
316  return {target_platform_};
317 }

References impeller::compiler::kUnknown.

Referenced by impeller::compiler::Main().

◆ PrintHelp()

void impeller::compiler::Switches::PrintHelp ( std::ostream &  stream)
static

Definition at line 43 of file switches.cc.

43  {
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 }
static const std::map< std::string, TargetPlatform > kKnownPlatforms
Definition: switches.cc:20
static const std::vector< std::pair< std::string, TargetPlatform > > kKnownRuntimeStages
Definition: switches.cc:29
static const std::map< std::string, SourceType > kKnownSourceTypes
Definition: switches.cc:37

References impeller::compiler::kKnownPlatforms, impeller::compiler::kKnownRuntimeStages, and impeller::compiler::kKnownSourceTypes.

Referenced by impeller::compiler::Main().

Member Data Documentation

◆ defines

std::vector<std::string> impeller::compiler::Switches::defines = {}

Definition at line 39 of file switches.h.

Referenced by CreateSourceOptions(), and Switches().

◆ depfile_path

std::filesystem::path impeller::compiler::Switches::depfile_path

Definition at line 38 of file switches.h.

◆ entry_point

std::string impeller::compiler::Switches::entry_point = ""

Definition at line 44 of file switches.h.

Referenced by CreateSourceOptions(), and impeller::compiler::testing::TEST().

◆ entry_point_prefix

std::string impeller::compiler::Switches::entry_point_prefix = ""

Definition at line 45 of file switches.h.

Referenced by CreateSourceOptions(), and impeller::compiler::testing::TEST().

◆ gles_language_version

uint32_t impeller::compiler::Switches::gles_language_version = 0

Definition at line 42 of file switches.h.

Referenced by CreateSourceOptions().

◆ include_directories

std::vector<IncludeDir> impeller::compiler::Switches::include_directories = {}

Definition at line 25 of file switches.h.

Referenced by CreateSourceOptions(), Switches(), and impeller::compiler::testing::TEST().

◆ input_type

SourceType impeller::compiler::Switches::input_type = SourceType::kUnknown

Definition at line 27 of file switches.h.

Referenced by CreateSourceOptions().

◆ iplr

bool impeller::compiler::Switches::iplr = false

Definition at line 32 of file switches.h.

Referenced by AreValid(), and impeller::compiler::Main().

◆ json_format

bool impeller::compiler::Switches::json_format = false

Definition at line 40 of file switches.h.

Referenced by CreateSourceOptions().

◆ metal_version

std::string impeller::compiler::Switches::metal_version = ""

Definition at line 43 of file switches.h.

Referenced by CreateSourceOptions().

◆ reflection_cc_name

std::filesystem::path impeller::compiler::Switches::reflection_cc_name

Definition at line 37 of file switches.h.

◆ reflection_header_name

std::filesystem::path impeller::compiler::Switches::reflection_header_name

Definition at line 36 of file switches.h.

◆ reflection_json_name

std::filesystem::path impeller::compiler::Switches::reflection_json_name

Definition at line 35 of file switches.h.

◆ require_framebuffer_fetch

bool impeller::compiler::Switches::require_framebuffer_fetch = false

Definition at line 47 of file switches.h.

Referenced by CreateSourceOptions().

◆ shader_bundle

std::string impeller::compiler::Switches::shader_bundle = ""

◆ sl_file_name

std::filesystem::path impeller::compiler::Switches::sl_file_name

The raw shader file output by the compiler. For –iplr and –shader-bundle modes, this is used as the filename for the output flatbuffer output.

Definition at line 31 of file switches.h.

Referenced by AreValid(), and impeller::compiler::GenerateShaderBundle().

◆ source_file_name

std::filesystem::path impeller::compiler::Switches::source_file_name

◆ source_language

SourceLanguage impeller::compiler::Switches::source_language = SourceLanguage::kUnknown

◆ spirv_file_name

std::filesystem::path impeller::compiler::Switches::spirv_file_name

Definition at line 34 of file switches.h.

Referenced by AreValid().

◆ use_half_textures

bool impeller::compiler::Switches::use_half_textures = false

Definition at line 46 of file switches.h.

Referenced by CreateSourceOptions().

◆ verbose

bool impeller::compiler::Switches::verbose = false

Definition at line 48 of file switches.h.

Referenced by impeller::compiler::Main().

◆ working_directory

std::shared_ptr<fml::UniqueFD> impeller::compiler::Switches::working_directory = nullptr

The documentation for this class was generated from the following files: