12#include "flutter/fml/file.h"
43 const std::string optional_prefix =
"[optional] ";
44 const std::string optional_multiple_prefix =
"[optional,multiple] ";
48 stream <<
"ImpellerC is an offline shader processor and reflection engine."
50 stream <<
"---------------------------------------------------------------"
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>"
58 stream <<
"Valid platforms are:" << std::endl << std::endl;
61 stream <<
" --" << platform.first;
63 stream <<
" ]" << std::endl << std::endl;
65 stream <<
"Valid runtime stages are:" << std::endl << std::endl;
66 stream <<
"At least one of [";
68 stream <<
" --" << platform.first;
70 stream <<
" ]" << std::endl << std::endl;
72 stream <<
"Optional arguments:" << std::endl << std::endl;
73 stream << optional_prefix
74 <<
"--spirv=<spirv_output_file> (ignored for --shader-bundle)"
76 stream << optional_prefix <<
"--input-type={";
78 stream << source_type.first <<
", ";
80 stream <<
"}" << std::endl;
81 stream << optional_prefix <<
"--source-language=glsl|hlsl (default: glsl)"
83 stream << optional_prefix
84 <<
"--entry-point=<entry_point_name> (default: main; "
87 stream << optional_prefix
88 <<
"--iplr (causes --sl file to be emitted in "
91 stream << optional_prefix
92 <<
"--shader-bundle=<bundle_spec> (causes --sl "
94 "emitted in Flutter GPU's shader bundle format)"
96 stream << optional_prefix <<
"--reflection-json=<reflection_json_file>"
98 stream << optional_prefix <<
"--reflection-header=<reflection_header_file>"
100 stream << optional_prefix <<
"--reflection-cc=<reflection_cc_file>"
102 stream << optional_multiple_prefix <<
"--include=<include_directory>"
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 "
112 stream << optional_prefix <<
"--require-framebuffer-fetch" << std::endl;
123 if (command_line.
HasOption(platform.first)) {
139 std::vector<TargetPlatform> stages;
141 if (command_line.
HasOption(platform.first)) {
142 stages.push_back(platform.second);
150 auto source_type_option =
156 return source_type_search->second;
160 : working_directory(
std::make_shared<
fml::UniqueFD>(
fml::OpenDirectory(
163 fml::FilePermission::kRead))),
164 source_file_name(command_line.GetOptionValueWithDefault(
"input",
"")),
166 sl_file_name(command_line.GetOptionValueWithDefault(
"sl",
"")),
167 iplr(command_line.HasOption(
"iplr")),
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",
"")),
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",
183 command_line.GetOptionValueWithDefault(
"metal-version",
"1.2")),
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")),
200 for (
const auto& include_dir_path : command_line.
GetOptionValues(
"include")) {
201 if (!include_dir_path.data()) {
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);
216 auto cwd =
Utf8FromPath(std::filesystem::current_path());
217 include_dir_absolute = std::filesystem::absolute(
218 std::filesystem::path(cwd) / include_dir_path);
223 if (!dir || !dir->is_valid()) {
228 dir_entry.
name = include_dir_path;
229 dir_entry.
dir = std::move(dir);
248 !shader_bundle_mode) {
249 explain <<
"Either a target platform was not specified, or no runtime "
250 "stages were specified."
256 explain <<
"Invalid source language type." << std::endl;
261 explain <<
"Could not open the working directory: \""
262 <<
Utf8FromPath(std::filesystem::current_path()).c_str() <<
"\""
268 explain <<
"Input file name was empty." << std::endl;
273 explain <<
"Target shading language file name was empty." << std::endl;
278 explain <<
"Spirv file name was empty." << std::endl;
282 if (
iplr && shader_bundle_mode) {
283 explain <<
"--iplr and --shader-bundle flag cannot be specified at the "
294 return runtime_stages_;
296 return {target_platform_};
301 !runtime_stages_.empty()) {
302 return runtime_stages_.front();
304 return target_platform_;
308 std::optional<TargetPlatform> target_platform)
const {
std::vector< std::string_view > GetOptionValues(std::string_view name) const
std::string GetOptionValueWithDefault(std::string_view name, std::string_view default_value) const
bool HasOption(std::string_view name, size_t *index=nullptr) const
bool require_framebuffer_fetch
std::vector< TargetPlatform > PlatformsToCompile() const
A vector containing at least one valid platform.
SourceLanguage source_language
std::string metal_version
uint32_t gles_language_version
std::string shader_bundle
std::vector< std::string > defines
std::shared_ptr< fml::UniqueFD > working_directory
SourceOptions CreateSourceOptions(std::optional< TargetPlatform > target_platform=std::nullopt) const
std::string spirv_file_name
bool AreValid(std::ostream &explain) const
static void PrintHelp(std::ostream &stream)
std::string source_file_name
std::vector< IncludeDir > include_directories
TargetPlatform SelectDefaultTargetPlatform() const
fml::UniqueFD OpenDirectoryReadOnly(const fml::UniqueFD &base_directory, const char *path)
std::string ToLowerCase(std::string_view string)
static std::vector< TargetPlatform > RuntimeStagesFromCommandLine(const fml::CommandLine &command_line)
static const std::map< std::string, TargetPlatform > kKnownPlatforms
SourceType SourceTypeFromFileName(const std::string &file_name)
std::string EntryPointFunctionNameFromSourceName(const std::string &file_name, SourceType type, SourceLanguage source_language, const std::string &entry_point_name)
static const std::map< std::string, TargetPlatform > kKnownRuntimeStages
std::string Utf8FromPath(const std::filesystem::path &path)
Converts a native format path to a utf8 string.
static TargetPlatform TargetPlatformFromCommandLine(const fml::CommandLine &command_line)
SourceLanguage ToSourceLanguage(const std::string &source_language)
static const std::map< std::string, SourceType > kKnownSourceTypes
static SourceType SourceTypeFromCommandLine(const fml::CommandLine &command_line)
std::shared_ptr< fml::UniqueFD > dir
TargetPlatform target_platform