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...
 
TargetPlatform SelectDefaultTargetPlatform () const
 
SourceOptions CreateSourceOptions (std::optional< TargetPlatform > target_platform=std::nullopt) 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::string source_file_name = ""
 
SourceType input_type = SourceType::kUnknown
 
std::string sl_file_name = ""
 
bool iplr = false
 
std::string shader_bundle = ""
 
std::string spirv_file_name = ""
 
std::string reflection_json_name = ""
 
std::string reflection_header_name = ""
 
std::string reflection_cc_name = ""
 
std::string 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
 

Detailed Description

Definition at line 21 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 163 of file switches.cc.

164  : working_directory(std::make_shared<fml::UniqueFD>(fml::OpenDirectory(
165  Utf8FromPath(std::filesystem::current_path()).c_str(),
166  false, // create if necessary,
167  fml::FilePermission::kRead))),
168  source_file_name(command_line.GetOptionValueWithDefault("input", "")),
169  input_type(SourceTypeFromCommandLine(command_line)),
170  sl_file_name(command_line.GetOptionValueWithDefault("sl", "")),
171  iplr(command_line.HasOption("iplr")),
173  command_line.GetOptionValueWithDefault("shader-bundle", "")),
174  spirv_file_name(command_line.GetOptionValueWithDefault("spirv", "")),
176  command_line.GetOptionValueWithDefault("reflection-json", "")),
178  command_line.GetOptionValueWithDefault("reflection-header", "")),
180  command_line.GetOptionValueWithDefault("reflection-cc", "")),
181  depfile_path(command_line.GetOptionValueWithDefault("depfile", "")),
182  json_format(command_line.HasOption("json")),
184  stoi(command_line.GetOptionValueWithDefault("gles-language-version",
185  "0"))),
187  command_line.GetOptionValueWithDefault("metal-version", "1.2")),
188  entry_point(
189  command_line.GetOptionValueWithDefault("entry-point", "main")),
191  command_line.GetOptionValueWithDefault("entry-point-prefix", "")),
192  use_half_textures(command_line.HasOption("use-half-textures")),
194  command_line.HasOption("require-framebuffer-fetch")),
195  target_platform_(TargetPlatformFromCommandLine(command_line)),
196  runtime_stages_(RuntimeStagesFromCommandLine(command_line)) {
197  auto language = ToLowerCase(
198  command_line.GetOptionValueWithDefault("source-language", "glsl"));
199 
200  source_language = ToSourceLanguage(language);
201 
202  if (!working_directory || !working_directory->is_valid()) {
203  return;
204  }
205 
206  for (const auto& include_dir_path : command_line.GetOptionValues("include")) {
207  if (!include_dir_path.data()) {
208  continue;
209  }
210 
211  // fml::OpenDirectoryReadOnly for Windows doesn't handle relative paths
212  // beginning with `../` well, so we build an absolute path.
213 
214  // Get the current working directory as a utf8 encoded string.
215  // Note that the `include_dir_path` is already utf8 encoded, and so we
216  // mustn't attempt to double-convert it to utf8 lest multi-byte characters
217  // will become mangled.
218  std::filesystem::path include_dir_absolute;
219  if (std::filesystem::path(include_dir_path).is_absolute()) {
220  include_dir_absolute = std::filesystem::path(include_dir_path);
221  } else {
222  auto cwd = Utf8FromPath(std::filesystem::current_path());
223  include_dir_absolute = std::filesystem::absolute(
224  std::filesystem::path(cwd) / include_dir_path);
225  }
226 
227  auto dir = std::make_shared<fml::UniqueFD>(fml::OpenDirectoryReadOnly(
228  *working_directory, include_dir_absolute.string().c_str()));
229  if (!dir || !dir->is_valid()) {
230  continue;
231  }
232 
233  IncludeDir dir_entry;
234  dir_entry.name = include_dir_path;
235  dir_entry.dir = std::move(dir);
236 
237  include_directories.emplace_back(std::move(dir_entry));
238  }
239 
240  for (const auto& define : command_line.GetOptionValues("define")) {
241  defines.emplace_back(define);
242  }
243 }
std::string reflection_json_name
Definition: switches.h:34
std::string entry_point_prefix
Definition: switches.h:44
SourceLanguage source_language
Definition: switches.h:40
std::string reflection_header_name
Definition: switches.h:35
std::vector< std::string > defines
Definition: switches.h:38
std::shared_ptr< fml::UniqueFD > working_directory
Definition: switches.h:23
std::string reflection_cc_name
Definition: switches.h:36
std::string spirv_file_name
Definition: switches.h:33
std::string source_file_name
Definition: switches.h:25
std::vector< IncludeDir > include_directories
Definition: switches.h:24
std::string ToLowerCase(std::string_view string)
Definition: utilities.cc:62
static std::vector< TargetPlatform > RuntimeStagesFromCommandLine(const fml::CommandLine &command_line)
Definition: switches.cc:141
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:123
SourceLanguage ToSourceLanguage(const std::string &source_language)
Definition: types.cc:64
static SourceType SourceTypeFromCommandLine(const fml::CommandLine &command_line)
Definition: switches.cc:152

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 245 of file switches.cc.

245  {
246  // When producing a shader bundle, all flags related to single shader inputs
247  // and outputs such as `--input` and `--spirv-file-name` are ignored. Instead,
248  // input files are read from the shader bundle spec and a single flatbuffer
249  // containing all compiled shaders and reflection state is output to `--sl`.
250  const bool shader_bundle_mode = !shader_bundle.empty();
251 
252  bool valid = true;
253  if (target_platform_ == TargetPlatform::kUnknown && runtime_stages_.empty() &&
254  !shader_bundle_mode) {
255  explain << "Either a target platform was not specified, or no runtime "
256  "stages were specified."
257  << std::endl;
258  valid = false;
259  }
260 
261  if (source_language == SourceLanguage::kUnknown && !shader_bundle_mode) {
262  explain << "Invalid source language type." << std::endl;
263  valid = false;
264  }
265 
266  if (!working_directory || !working_directory->is_valid()) {
267  explain << "Could not open the working directory: \""
268  << Utf8FromPath(std::filesystem::current_path()).c_str() << "\""
269  << std::endl;
270  valid = false;
271  }
272 
273  if (source_file_name.empty() && !shader_bundle_mode) {
274  explain << "Input file name was empty." << std::endl;
275  valid = false;
276  }
277 
278  if (sl_file_name.empty()) {
279  explain << "Target shading language file name was empty." << std::endl;
280  valid = false;
281  }
282 
283  if (spirv_file_name.empty() && !shader_bundle_mode) {
284  explain << "Spirv file name was empty." << std::endl;
285  valid = false;
286  }
287 
288  if (iplr && shader_bundle_mode) {
289  explain << "--iplr and --shader-bundle flag cannot be specified at the "
290  "same time"
291  << std::endl;
292  valid = false;
293  }
294 
295  return valid;
296 }

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 ( std::optional< TargetPlatform target_platform = std::nullopt) const

Definition at line 313 of file switches.cc.

314  {
315  SourceOptions options;
316  options.target_platform =
317  target_platform.value_or(SelectDefaultTargetPlatform());
318  options.source_language = source_language;
321  } else {
322  options.type = input_type;
323  }
324  options.working_directory = working_directory;
325  options.file_name = source_file_name;
326  options.include_dirs = include_directories;
327  options.defines = defines;
328  options.entry_point_name =
331  source_file_name, options.type, options.source_language, entry_point);
332  options.json_format = json_format;
333  options.gles_language_version = gles_language_version;
334  options.metal_version = metal_version;
335  options.use_half_textures = use_half_textures;
336  options.require_framebuffer_fetch = require_framebuffer_fetch;
337  return options;
338 }
TargetPlatform SelectDefaultTargetPlatform() const
Definition: switches.cc:305
SourceType SourceTypeFromFileName(const std::string &file_name)
Definition: types.cc:30
std::string EntryPointFunctionNameFromSourceName(const std::string &file_name, SourceType type, SourceLanguage source_language, const std::string &entry_point_name)
Definition: types.cc:113

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, SelectDefaultTargetPlatform(), source_file_name, impeller::compiler::SourceOptions::source_language, source_language, impeller::compiler::SourceTypeFromFileName(), impeller::compiler::SourceOptions::target_platform, 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::CompileSkSL(), impeller::compiler::GenerateShaderBundle(), impeller::compiler::Main(), impeller::compiler::OutputIPLR(), 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 298 of file switches.cc.

298  {
299  if (target_platform_ == TargetPlatform::kUnknown) {
300  return runtime_stages_;
301  }
302  return {target_platform_};
303 }

References impeller::compiler::kUnknown.

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

◆ PrintHelp()

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

Definition at line 42 of file switches.cc.

42  {
43  // clang-format off
44  const std::string optional_prefix = "[optional] ";
45  const std::string optional_multiple_prefix = "[optional,multiple] ";
46  // clang-format on
47 
48  stream << std::endl;
49  stream << "ImpellerC is an offline shader processor and reflection engine."
50  << std::endl;
51  stream << "---------------------------------------------------------------"
52  << std::endl;
53  stream << "Expected invocation is:" << std::endl << std::endl;
54  stream << "./impellerc <One platform or multiple runtime stages> "
55  "--input=<source_file> --sl=<sl_output_file> <optional arguments>"
56  << std::endl
57  << std::endl;
58 
59  stream << "Valid platforms are:" << std::endl << std::endl;
60  stream << "One of [";
61  for (const auto& platform : kKnownPlatforms) {
62  stream << " --" << platform.first;
63  }
64  stream << " ]" << std::endl << std::endl;
65 
66  stream << "Valid runtime stages are:" << std::endl << std::endl;
67  stream << "At least one of [";
68  for (const auto& platform : kKnownRuntimeStages) {
69  stream << " --" << platform.first;
70  }
71  stream << " ]" << std::endl << std::endl;
72 
73  stream << "Optional arguments:" << std::endl << std::endl;
74  stream << optional_prefix
75  << "--spirv=<spirv_output_file> (ignored for --shader-bundle)"
76  << std::endl;
77  stream << optional_prefix << "--input-type={";
78  for (const auto& source_type : kKnownSourceTypes) {
79  stream << source_type.first << ", ";
80  }
81  stream << "}" << std::endl;
82  stream << optional_prefix << "--source-language=glsl|hlsl (default: glsl)"
83  << std::endl;
84  stream << optional_prefix
85  << "--entry-point=<entry_point_name> (default: main; "
86  "ignored for glsl)"
87  << std::endl;
88  stream << optional_prefix
89  << "--entry-point-prefix=<entry_point_prefix> (default: empty)"
90  << std::endl;
91  stream << optional_prefix
92  << "--iplr (causes --sl file to be emitted in "
93  "iplr format)"
94  << std::endl;
95  stream << optional_prefix
96  << "--shader-bundle=<bundle_spec> (causes --sl "
97  "file to be "
98  "emitted in Flutter GPU's shader bundle format)"
99  << std::endl;
100  stream << optional_prefix << "--reflection-json=<reflection_json_file>"
101  << std::endl;
102  stream << optional_prefix << "--reflection-header=<reflection_header_file>"
103  << std::endl;
104  stream << optional_prefix << "--reflection-cc=<reflection_cc_file>"
105  << std::endl;
106  stream << optional_multiple_prefix << "--include=<include_directory>"
107  << std::endl;
108  stream << optional_multiple_prefix << "--define=<define>" << std::endl;
109  stream << optional_prefix << "--depfile=<depfile_path>" << std::endl;
110  stream << optional_prefix << "--gles-language-version=<number>" << std::endl;
111  stream << optional_prefix << "--json" << std::endl;
112  stream << optional_prefix
113  << "--use-half-textures (force openGL semantics when "
114  "targeting metal)"
115  << std::endl;
116  stream << optional_prefix << "--require-framebuffer-fetch" << std::endl;
117 }
static const std::map< std::string, TargetPlatform > kKnownPlatforms
Definition: switches.cc:20
static const std::map< std::string, TargetPlatform > kKnownRuntimeStages
Definition: switches.cc:28
static const std::map< std::string, SourceType > kKnownSourceTypes
Definition: switches.cc:36

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

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

◆ SelectDefaultTargetPlatform()

TargetPlatform impeller::compiler::Switches::SelectDefaultTargetPlatform ( ) const

Definition at line 305 of file switches.cc.

305  {
306  if (target_platform_ == TargetPlatform::kUnknown &&
307  !runtime_stages_.empty()) {
308  return runtime_stages_.front();
309  }
310  return target_platform_;
311 }

References impeller::compiler::kUnknown.

Referenced by CreateSourceOptions(), impeller::compiler::OutputDepfile(), and impeller::compiler::OutputIPLR().

Member Data Documentation

◆ defines

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

Definition at line 38 of file switches.h.

Referenced by CreateSourceOptions(), and Switches().

◆ depfile_path

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

Definition at line 37 of file switches.h.

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

◆ entry_point

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

Definition at line 43 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 44 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 41 of file switches.h.

Referenced by CreateSourceOptions().

◆ include_directories

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

Definition at line 24 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 26 of file switches.h.

Referenced by CreateSourceOptions().

◆ iplr

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

Definition at line 31 of file switches.h.

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

◆ json_format

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

Definition at line 39 of file switches.h.

Referenced by CreateSourceOptions(), and impeller::compiler::OutputIPLR().

◆ metal_version

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

Definition at line 42 of file switches.h.

Referenced by CreateSourceOptions().

◆ reflection_cc_name

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

Definition at line 36 of file switches.h.

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

◆ reflection_header_name

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

◆ reflection_json_name

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

Definition at line 34 of file switches.h.

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

◆ require_framebuffer_fetch

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

Definition at line 46 of file switches.h.

Referenced by CreateSourceOptions().

◆ shader_bundle

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

◆ sl_file_name

std::string 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 30 of file switches.h.

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

◆ source_file_name

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

◆ source_language

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

◆ spirv_file_name

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

◆ use_half_textures

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

Definition at line 45 of file switches.h.

Referenced by CreateSourceOptions().

◆ working_directory


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