| 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 "impeller/compiler/types.h"
14 #include "impeller/compiler/utilities.h"
15
16 namespace impeller {
17 namespace compiler {
18
19 static const std::map<std::string, TargetPlatform> kKnownPlatforms = {
20 {"metal-desktop", TargetPlatform::kMetalDesktop},
21 {"metal-ios", TargetPlatform::kMetalIOS},
22 {"vulkan", TargetPlatform::kVulkan},
23 {"opengl-es", TargetPlatform::kOpenGLES},
24 {"opengl-desktop", TargetPlatform::kOpenGLDesktop},
25 {"sksl", TargetPlatform::kSkSL},
26 {"runtime-stage-metal", TargetPlatform::kRuntimeStageMetal},
27 {"runtime-stage-gles", TargetPlatform::kRuntimeStageGLES},
28 {"runtime-stage-vulkan", TargetPlatform::kRuntimeStageVulkan},
29 };
30
31 static const std::map<std::string, SourceType> kKnownSourceTypes = {
32 {"vert", SourceType::kVertexShader},
33 {"frag", SourceType::kFragmentShader},
34 {"tesc", SourceType::kTessellationControlShader},
35 {"tese", SourceType::kTessellationEvaluationShader},
36 {"comp", SourceType::kComputeShader},
37 };
38
39 void Switches::PrintHelp(std::ostream& stream) {
40 stream << std::endl;
41 stream << "ImpellerC is an offline shader processor and reflection engine."
42 << std::endl;
43 stream << "---------------------------------------------------------------"
44 << std::endl;
45 stream << "Valid Argument are:" << std::endl;
46 stream << "One of [";
47 for (const auto& platform : kKnownPlatforms) {
48 stream << " --" << platform.first;
49 }
50 stream << " ]" << std::endl;
51 stream << "--input=<source_file>" << std::endl;
52 stream << "[optional] --input-type={";
53 for (const auto& source_type : kKnownSourceTypes) {
54 stream << source_type.first << ", ";
55 }
56 stream << "}" << std::endl;
57 stream << "--sl=<sl_output_file>" << std::endl;
58 stream << "--spirv=<spirv_output_file>" << std::endl;
59 stream << "[optional] --source-language=glsl|hlsl (default: glsl)"
60 << std::endl;
61 stream << "[optional] --entry-point=<entry_point_name> (default: main; "
62 "ignored for glsl)"
63 << std::endl;
64 stream << "[optional] --iplr (causes --sl file to be emitted in iplr format)"
65 << std::endl;
66 stream << "[optional] --reflection-json=<reflection_json_file>" << std::endl;
67 stream << "[optional] --reflection-header=<reflection_header_file>"
68 << std::endl;
69 stream << "[optional] --reflection-cc=<reflection_cc_file>" << std::endl;
70 stream << "[optional,multiple] --include=<include_directory>" << std::endl;
71 stream << "[optional,multiple] --define=<define>" << std::endl;
72 stream << "[optional] --depfile=<depfile_path>" << std::endl;
73 stream << "[optional] --gles-language-version=<number>" << std::endl;
74 stream << "[optional] --json" << std::endl;
75 stream << "[optional] --use-half-textures (force openGL semantics when "
76 "targeting metal)"
77 << std::endl;
78 }
79
80 Switches::Switches() = default;
81
82 Switches::~Switches() = default;
83
84 static TargetPlatform TargetPlatformFromCommandLine(
85 const fml::CommandLine& command_line) {
86 auto target = TargetPlatform::kUnknown;
87 for (const auto& platform : kKnownPlatforms) {
88 if (command_line.HasOption(platform.first)) {
89 // If the platform has already been determined, the caller may have
90 // specified multiple platforms. This is an error and only one must be
91 // selected.
92 if (target != TargetPlatform::kUnknown) {
93 return TargetPlatform::kUnknown;
94 }
95 target = platform.second;
96 // Keep going to detect duplicates.
97 }
98 }
99 return target;
100 }
101
102 static SourceType SourceTypeFromCommandLine(
103 const fml::CommandLine& command_line) {
104 auto source_type_option =
105 command_line.GetOptionValueWithDefault("input-type", "");
106 auto source_type_search = kKnownSourceTypes.find(source_type_option);
107 if (source_type_search == kKnownSourceTypes.end()) {
108 return SourceType::kUnknown;
109 }
110 return source_type_search->second;
111 }
112
113 Switches::Switches(const fml::CommandLine& command_line)
114 : target_platform(TargetPlatformFromCommandLine(command_line)),
115 working_directory(std::make_shared<fml::UniqueFD>(fml::OpenDirectory(
116 Utf8FromPath(std::filesystem::current_path()).c_str(),
117 false, // create if necessary,
118 fml::FilePermission::kRead))),
119 source_file_name(command_line.GetOptionValueWithDefault("input", "")),
120 input_type(SourceTypeFromCommandLine(command_line)),
121 sl_file_name(command_line.GetOptionValueWithDefault("sl", "")),
122 iplr(command_line.HasOption("iplr")),
123 spirv_file_name(command_line.GetOptionValueWithDefault("spirv", "")),
124 reflection_json_name(
125 command_line.GetOptionValueWithDefault("reflection-json", "")),
126 reflection_header_name(
127 command_line.GetOptionValueWithDefault("reflection-header", "")),
128 reflection_cc_name(
129 command_line.GetOptionValueWithDefault("reflection-cc", "")),
130 depfile_path(command_line.GetOptionValueWithDefault("depfile", "")),
131 json_format(command_line.HasOption("json")),
132 gles_language_version(
133 stoi(command_line.GetOptionValueWithDefault("gles-language-version",
134 "0"))),
135 metal_version(
136 command_line.GetOptionValueWithDefault("metal-version", "1.2")),
137 entry_point(
138 command_line.GetOptionValueWithDefault("entry-point", "main")),
139 use_half_textures(command_line.HasOption("use-half-textures")) {
140 auto language =
141 command_line.GetOptionValueWithDefault("source-language", "glsl");
142 std::transform(language.begin(), language.end(), language.begin(),
143 [](char x) { return std::tolower(x); });
144 if (language == "glsl") {
145 source_language = SourceLanguage::kGLSL;
146 } else if (language == "hlsl") {
147 source_language = SourceLanguage::kHLSL;
148 }
149
150 if (!working_directory || !working_directory->is_valid()) {
151 return;
152 }
153
154 for (const auto& include_dir_path : command_line.GetOptionValues("include")) {
155 if (!include_dir_path.data()) {
156 continue;
157 }
158
159 // fml::OpenDirectoryReadOnly for Windows doesn't handle relative paths
160 // beginning with `../` well, so we build an absolute path.
161
162 // Get the current working directory as a utf8 encoded string.
163 // Note that the `include_dir_path` is already utf8 encoded, and so we
164 // mustn't attempt to double-convert it to utf8 lest multi-byte characters
165 // will become mangled.
166 std::filesystem::path include_dir_absolute;
167 if (std::filesystem::path(include_dir_path).is_absolute()) {
168 include_dir_absolute = std::filesystem::path(include_dir_path);
169 } else {
170 auto cwd = Utf8FromPath(std::filesystem::current_path());
171 include_dir_absolute = std::filesystem::absolute(
172 std::filesystem::path(cwd) / include_dir_path);
173 }
174
175 auto dir = std::make_shared<fml::UniqueFD>(fml::OpenDirectoryReadOnly(
176 *working_directory, include_dir_absolute.string().c_str()));
177 if (!dir || !dir->is_valid()) {
178 continue;
179 }
180
181 IncludeDir dir_entry;
182 dir_entry.name = include_dir_path;
183 dir_entry.dir = std::move(dir);
184
185 include_directories.emplace_back(std::move(dir_entry));
186 }
187
188 for (const auto& define : command_line.GetOptionValues("define")) {
189 defines.emplace_back(define);
190 }
191 }
192
193 bool Switches::AreValid(std::ostream& explain) const {
194 bool valid = true;
195 if (target_platform == TargetPlatform::kUnknown) {
196 explain << "The target platform (only one) was not specified." << std::endl;
197 valid = false;
198 }
199
200 if (source_language == SourceLanguage::kUnknown) {
201 explain << "Invalid source language type." << std::endl;
202 valid = false;
203 }
204
205 if (!working_directory || !working_directory->is_valid()) {
206 explain << "Could not open the working directory: \""
207 << Utf8FromPath(std::filesystem::current_path()).c_str() << "\""
208 << std::endl;
209 valid = false;
210 }
211
212 if (source_file_name.empty()) {
213 explain << "Input file name was empty." << std::endl;
214 valid = false;
215 }
216
217 if (sl_file_name.empty()) {
218 explain << "Target shading language file name was empty." << std::endl;
219 valid = false;
220 }
221
222 if (spirv_file_name.empty()) {
223 explain << "Spirv file name was empty." << std::endl;
224 valid = false;
225 }
226 return valid;
227 }
228
229 } // namespace compiler
230 } // namespace impeller
impeller::compiler::Switches::defines
std::vector< std::string > defines
Definition: switches.h:33
impeller::compiler::Switches::Switches
Switches()
impeller::compiler::Switches::target_platform
TargetPlatform target_platform
Definition: switches.h:21
impeller::compiler::SourceType::kUnknown
@ kUnknown
impeller::compiler::TargetPlatform::kMetalDesktop
@ kMetalDesktop
impeller::compiler::Switches::working_directory
std::shared_ptr< fml::UniqueFD > working_directory
Definition: switches.h:22
impeller::compiler::Switches::include_directories
std::vector< IncludeDir > include_directories
Definition: switches.h:23
impeller::compiler::TargetPlatform::kMetalIOS
@ kMetalIOS
impeller::compiler::SourceType::kTessellationControlShader
@ kTessellationControlShader
switches.h
impeller::compiler::TargetPlatform
TargetPlatform
Definition: types.h:28
impeller::compiler::Switches::AreValid
bool AreValid(std::ostream &explain) const
Definition: switches.cc:193
impeller::compiler::kKnownPlatforms
static const std::map< std::string, TargetPlatform > kKnownPlatforms
Definition: switches.cc:19
impeller::compiler::SourceLanguage::kGLSL
@ kGLSL
impeller::compiler::IncludeDir::name
std::string name
Definition: include_dir.h:17
impeller::compiler::Switches::source_file_name
std::string source_file_name
Definition: switches.h:24
impeller::compiler::TargetPlatformFromCommandLine
static TargetPlatform TargetPlatformFromCommandLine(const fml::CommandLine &command_line)
Definition: switches.cc:84
impeller::compiler::SourceLanguage::kHLSL
@ kHLSL
impeller::compiler::TargetPlatform::kVulkan
@ kVulkan
impeller::compiler::IncludeDir
Definition: include_dir.h:15
impeller::compiler::SourceType::kFragmentShader
@ kFragmentShader
impeller::compiler::TargetPlatform::kRuntimeStageVulkan
@ kRuntimeStageVulkan
impeller::compiler::Switches::spirv_file_name
std::string spirv_file_name
Definition: switches.h:28
impeller::compiler::Switches::source_language
SourceLanguage source_language
Definition: switches.h:35
impeller::compiler::SourceType::kComputeShader
@ kComputeShader
utilities.h
impeller::compiler::SourceType
SourceType
Definition: types.h:19
impeller::compiler::SourceLanguage::kUnknown
@ kUnknown
impeller::compiler::SourceTypeFromCommandLine
static SourceType SourceTypeFromCommandLine(const fml::CommandLine &command_line)
Definition: switches.cc:102
std
Definition: comparable.h:98
impeller::compiler::TargetPlatform::kOpenGLDesktop
@ kOpenGLDesktop
impeller::compiler::Switches::sl_file_name
std::string sl_file_name
Definition: switches.h:26
impeller::compiler::TargetPlatform::kUnknown
@ kUnknown
impeller::compiler::TargetPlatform::kOpenGLES
@ kOpenGLES
impeller::compiler::kKnownSourceTypes
static const std::map< std::string, SourceType > kKnownSourceTypes
Definition: switches.cc:31
impeller::compiler::Utf8FromPath
std::string Utf8FromPath(const std::filesystem::path &path)
Converts a native format path to a utf8 string.
Definition: utilities.cc:14
impeller::compiler::TargetPlatform::kRuntimeStageMetal
@ kRuntimeStageMetal
impeller::compiler::IncludeDir::dir
std::shared_ptr< fml::UniqueFD > dir
Definition: include_dir.h:16
impeller::compiler::SourceType::kVertexShader
@ kVertexShader
impeller
Definition: aiks_context.cc:10
impeller::compiler::SourceType::kTessellationEvaluationShader
@ kTessellationEvaluationShader
impeller::compiler::Switches::~Switches
~Switches()
impeller::compiler::Switches::PrintHelp
static void PrintHelp(std::ostream &stream)
Definition: switches.cc:39
types.h
impeller::compiler::TargetPlatform::kSkSL
@ kSkSL
impeller::compiler::TargetPlatform::kRuntimeStageGLES
@ kRuntimeStageGLES