blob: cd63ee9c8c840f928c7e15ac4f6185906ec1cfd1 [file] [log] [blame]
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001/*
2 * Copyright 2015-2016 ARM Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Hans-Kristian Arntzen147e53a2016-04-04 09:36:04 +020017#include "spirv_cpp.hpp"
Bill Hollings5f2d6662016-04-11 11:31:03 -040018#include "spirv_msl.hpp"
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +020019#include <algorithm>
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010020#include <cstdio>
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +020021#include <cstring>
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010022#include <functional>
23#include <limits>
24#include <memory>
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +020025#include <stdexcept>
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010026#include <unordered_map>
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010027#include <unordered_set>
28
29using namespace spv;
Hans-Kristian Arntzen147e53a2016-04-04 09:36:04 +020030using namespace spirv_cross;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010031using namespace std;
32
33struct CLIParser;
34struct CLICallbacks
35{
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +020036 void add(const char *cli, const function<void(CLIParser &)> &func)
37 {
38 callbacks[cli] = func;
39 }
40 unordered_map<string, function<void(CLIParser &)>> callbacks;
41 function<void()> error_handler;
42 function<void(const char *)> default_handler;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010043};
44
45struct CLIParser
46{
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +020047 CLIParser(CLICallbacks cbs_, int argc_, char *argv_[])
48 : cbs(move(cbs_))
49 , argc(argc_)
50 , argv(argv_)
51 {
52 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010053
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +020054 bool parse()
55 {
56 try
57 {
58 while (argc && !ended_state)
59 {
60 const char *next = *argv++;
61 argc--;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010062
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +020063 if (*next != '-' && cbs.default_handler)
64 {
65 cbs.default_handler(next);
66 }
67 else
68 {
69 auto itr = cbs.callbacks.find(next);
70 if (itr == ::end(cbs.callbacks))
71 {
72 throw logic_error("Invalid argument.\n");
73 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010074
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +020075 itr->second(*this);
76 }
77 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010078
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +020079 return true;
80 }
81 catch (...)
82 {
83 if (cbs.error_handler)
84 {
85 cbs.error_handler();
86 }
87 return false;
88 }
89 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010090
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +020091 void end()
92 {
93 ended_state = true;
94 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010095
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +020096 uint32_t next_uint()
97 {
98 if (!argc)
99 {
100 throw logic_error("Tried to parse uint, but nothing left in arguments.\n");
101 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100102
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200103 uint32_t val = stoul(*argv);
104 if (val > numeric_limits<uint32_t>::max())
105 {
106 throw out_of_range("next_uint() out of range.\n");
107 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100108
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200109 argc--;
110 argv++;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100111
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200112 return val;
113 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100114
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200115 double next_double()
116 {
117 if (!argc)
118 {
119 throw logic_error("Tried to parse double, but nothing left in arguments.\n");
120 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100121
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200122 double val = stod(*argv);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100123
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200124 argc--;
125 argv++;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100126
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200127 return val;
128 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100129
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200130 const char *next_string()
131 {
132 if (!argc)
133 {
134 throw logic_error("Tried to parse string, but nothing left in arguments.\n");
135 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100136
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200137 const char *ret = *argv;
138 argc--;
139 argv++;
140 return ret;
141 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100142
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200143 CLICallbacks cbs;
144 int argc;
145 char **argv;
146 bool ended_state = false;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100147};
148
149static vector<uint32_t> read_spirv_file(const char *path)
150{
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200151 FILE *file = fopen(path, "rb");
152 if (!file)
153 {
154 fprintf(stderr, "Failed to open SPIRV file: %s\n", path);
155 return {};
156 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100157
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200158 fseek(file, 0, SEEK_END);
159 long len = ftell(file) / sizeof(uint32_t);
160 rewind(file);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100161
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200162 vector<uint32_t> spirv(len);
163 if (fread(spirv.data(), sizeof(uint32_t), len, file) != size_t(len))
164 spirv.clear();
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100165
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200166 fclose(file);
167 return spirv;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100168}
169
170static bool write_string_to_file(const char *path, const char *string)
171{
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200172 FILE *file = fopen(path, "w");
173 if (!file)
174 {
175 fprintf(file, "Failed to write file: %s\n", path);
176 return false;
177 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100178
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200179 fprintf(file, "%s", string);
180 fclose(file);
181 return true;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100182}
183
184static void print_resources(const Compiler &compiler, const char *tag, const vector<Resource> &resources)
185{
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200186 fprintf(stderr, "%s\n", tag);
187 fprintf(stderr, "=============\n\n");
188 for (auto &res : resources)
189 {
190 auto &type = compiler.get_type(res.type_id);
191 auto mask = compiler.get_decoration_mask(res.id);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100192
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200193 // If we don't have a name, use the fallback for the type instead of the variable
194 // for SSBOs and UBOs since those are the only meaningful names to use externally.
195 // Push constant blocks are still accessed by name and not block name, even though they are technically Blocks.
196 bool is_push_constant = compiler.get_storage_class(res.id) == StorageClassPushConstant;
197 bool is_block = (compiler.get_decoration_mask(type.self) &
198 ((1ull << DecorationBlock) | (1ull << DecorationBufferBlock))) != 0;
Hans-Kristian Arntzen5c24d992016-07-12 21:20:18 +0200199 uint32_t fallback_id = !is_push_constant && is_block ? res.base_type_id : res.id;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100200
Hans-Kristian Arntzen5c24d992016-07-12 21:20:18 +0200201 string array;
202 for (auto arr : type.array)
203 array = join("[", arr ? convert_to_string(arr) : "", "]") + array;
204
205 fprintf(stderr, " ID %03u : %s%s", res.id,
206 !res.name.empty() ? res.name.c_str() : compiler.get_fallback_name(fallback_id).c_str(), array.c_str());
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100207
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200208 if (mask & (1ull << DecorationLocation))
209 fprintf(stderr, " (Location : %u)", compiler.get_decoration(res.id, DecorationLocation));
210 if (mask & (1ull << DecorationDescriptorSet))
211 fprintf(stderr, " (Set : %u)", compiler.get_decoration(res.id, DecorationDescriptorSet));
212 if (mask & (1ull << DecorationBinding))
213 fprintf(stderr, " (Binding : %u)", compiler.get_decoration(res.id, DecorationBinding));
214 if (mask & (1ull << DecorationInputAttachmentIndex))
215 fprintf(stderr, " (Attachment : %u)", compiler.get_decoration(res.id, DecorationInputAttachmentIndex));
216 fprintf(stderr, "\n");
217 }
218 fprintf(stderr, "=============\n\n");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100219}
220
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +0200221static const char *execution_model_to_str(spv::ExecutionModel model)
222{
223 switch (model)
224 {
225 case spv::ExecutionModelVertex:
226 return "vertex";
227 case spv::ExecutionModelTessellationControl:
228 return "tessellation control";
229 case ExecutionModelTessellationEvaluation:
230 return "tessellation evaluation";
231 case ExecutionModelGeometry:
232 return "geometry";
233 case ExecutionModelFragment:
234 return "fragment";
235 case ExecutionModelGLCompute:
236 return "compute";
237 default:
238 return "???";
239 }
240}
241
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100242static void print_resources(const Compiler &compiler, const ShaderResources &res)
243{
Hans-Kristian Arntzen3c285a12016-07-04 13:30:05 +0200244 uint64_t modes = compiler.get_execution_mode_mask();
245
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +0200246 fprintf(stderr, "Entry points:\n");
247 auto entry_points = compiler.get_entry_points();
248 for (auto &e : entry_points)
249 fprintf(stderr, " %s (%s)\n", e.c_str(), execution_model_to_str(compiler.get_entry_point(e).model));
250 fprintf(stderr, "\n");
251
Hans-Kristian Arntzen3c285a12016-07-04 13:30:05 +0200252 fprintf(stderr, "Execution modes:\n");
253 for (unsigned i = 0; i < 64; i++)
254 {
255 if (!(modes & (1ull << i)))
256 continue;
257
258 auto mode = static_cast<ExecutionMode>(i);
259 uint32_t arg0 = compiler.get_execution_mode_argument(mode, 0);
260 uint32_t arg1 = compiler.get_execution_mode_argument(mode, 1);
261 uint32_t arg2 = compiler.get_execution_mode_argument(mode, 2);
262
263 switch (static_cast<ExecutionMode>(i))
264 {
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200265 case ExecutionModeInvocations:
266 fprintf(stderr, " Invocations: %u\n", arg0);
267 break;
Hans-Kristian Arntzen3c285a12016-07-04 13:30:05 +0200268
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200269 case ExecutionModeLocalSize:
270 fprintf(stderr, " LocalSize: (%u, %u, %u)\n", arg0, arg1, arg2);
271 break;
Hans-Kristian Arntzen3c285a12016-07-04 13:30:05 +0200272
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200273 case ExecutionModeOutputVertices:
274 fprintf(stderr, " OutputVertices: %u\n", arg0);
275 break;
Hans-Kristian Arntzen3c285a12016-07-04 13:30:05 +0200276
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200277#define CHECK_MODE(m) \
278 case ExecutionMode##m: \
279 fprintf(stderr, " %s\n", #m); \
280 break
281 CHECK_MODE(SpacingEqual);
282 CHECK_MODE(SpacingFractionalEven);
283 CHECK_MODE(SpacingFractionalOdd);
284 CHECK_MODE(VertexOrderCw);
285 CHECK_MODE(VertexOrderCcw);
286 CHECK_MODE(PixelCenterInteger);
287 CHECK_MODE(OriginUpperLeft);
288 CHECK_MODE(OriginLowerLeft);
289 CHECK_MODE(EarlyFragmentTests);
290 CHECK_MODE(PointMode);
291 CHECK_MODE(Xfb);
292 CHECK_MODE(DepthReplacing);
293 CHECK_MODE(DepthGreater);
294 CHECK_MODE(DepthLess);
295 CHECK_MODE(DepthUnchanged);
296 CHECK_MODE(LocalSizeHint);
297 CHECK_MODE(InputPoints);
298 CHECK_MODE(InputLines);
299 CHECK_MODE(InputLinesAdjacency);
300 CHECK_MODE(Triangles);
301 CHECK_MODE(InputTrianglesAdjacency);
302 CHECK_MODE(Quads);
303 CHECK_MODE(Isolines);
304 CHECK_MODE(OutputPoints);
305 CHECK_MODE(OutputLineStrip);
306 CHECK_MODE(OutputTriangleStrip);
307 CHECK_MODE(VecTypeHint);
308 CHECK_MODE(ContractionOff);
Hans-Kristian Arntzen3c285a12016-07-04 13:30:05 +0200309
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200310 default:
311 break;
Hans-Kristian Arntzen3c285a12016-07-04 13:30:05 +0200312 }
313 }
314 fprintf(stderr, "\n");
315
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200316 print_resources(compiler, "subpass inputs", res.subpass_inputs);
317 print_resources(compiler, "inputs", res.stage_inputs);
318 print_resources(compiler, "outputs", res.stage_outputs);
319 print_resources(compiler, "textures", res.sampled_images);
320 print_resources(compiler, "images", res.storage_images);
321 print_resources(compiler, "ssbos", res.storage_buffers);
322 print_resources(compiler, "ubos", res.uniform_buffers);
323 print_resources(compiler, "push", res.push_constant_buffers);
324 print_resources(compiler, "counters", res.atomic_counters);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100325}
326
327static void print_push_constant_resources(const Compiler &compiler, const vector<Resource> &res)
328{
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200329 for (auto &block : res)
330 {
331 auto ranges = compiler.get_active_buffer_ranges(block.id);
332 fprintf(stderr, "Active members in buffer: %s\n",
333 !block.name.empty() ? block.name.c_str() : compiler.get_fallback_name(block.id).c_str());
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100334
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200335 fprintf(stderr, "==================\n\n");
336 for (auto &range : ranges)
337 {
Hans-Kristian Arntzen5c24d992016-07-12 21:20:18 +0200338 const auto &name = compiler.get_member_name(block.base_type_id, range.index);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100339
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200340 fprintf(stderr, "Member #%3u (%s): Offset: %4u, Range: %4u\n", range.index,
341 !name.empty() ? name.c_str() : compiler.get_fallback_member_name(range.index).c_str(),
342 unsigned(range.offset), unsigned(range.range));
343 }
344 fprintf(stderr, "==================\n\n");
345 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100346}
347
348struct PLSArg
349{
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200350 PlsFormat format;
351 string name;
352};
353
354struct Remap
355{
356 string src_name;
357 string dst_name;
358 unsigned components;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100359};
360
361struct CLIArguments
362{
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200363 const char *input = nullptr;
364 const char *output = nullptr;
365 const char *cpp_interface_name = nullptr;
366 uint32_t version = 0;
367 bool es = false;
368 bool set_version = false;
369 bool set_es = false;
370 bool dump_resources = false;
371 bool force_temporary = false;
372 bool flatten_ubo = false;
373 bool fixup = false;
374 vector<PLSArg> pls_in;
375 vector<PLSArg> pls_out;
376 vector<Remap> remaps;
377 vector<string> extensions;
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +0200378 string entry;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100379
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200380 uint32_t iterations = 1;
381 bool cpp = false;
382 bool metal = false;
383 bool vulkan_semantics = false;
Hans-Kristian Arntzenf61a5d12016-08-26 12:58:50 +0200384 bool remove_unused = false;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100385};
386
387static void print_help()
388{
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200389 fprintf(stderr, "Usage: spirv-cross [--output <output path>] [SPIR-V file] [--es] [--no-es] [--version <GLSL "
390 "version>] [--dump-resources] [--help] [--force-temporary] [--cpp] [--cpp-interface-name <name>] "
391 "[--metal] [--vulkan-semantics] [--flatten-ubo] [--fixup-clipspace] [--iterations iter] [--pls-in "
392 "format input-name] [--pls-out format output-name] [--remap source_name target_name components] "
Hans-Kristian Arntzenf61a5d12016-08-26 12:58:50 +0200393 "[--extension ext] [--entry name] [--remove-unused-variables]\n");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100394}
395
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200396static bool remap_generic(Compiler &compiler, const vector<Resource> &resources, const Remap &remap)
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100397{
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200398 auto itr =
399 find_if(begin(resources), end(resources), [&remap](const Resource &res) { return res.name == remap.src_name; });
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100400
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200401 if (itr != end(resources))
402 {
403 compiler.set_remapped_variable_state(itr->id, true);
404 compiler.set_name(itr->id, remap.dst_name);
405 compiler.set_subpass_input_remapped_components(itr->id, remap.components);
406 return true;
407 }
408 else
409 return false;
410}
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100411
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200412static vector<PlsRemap> remap_pls(const vector<PLSArg> &pls_variables, const vector<Resource> &resources,
413 const vector<Resource> *secondary_resources)
414{
415 vector<PlsRemap> ret;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100416
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200417 for (auto &pls : pls_variables)
418 {
419 bool found = false;
420 for (auto &res : resources)
421 {
422 if (res.name == pls.name)
423 {
424 ret.push_back({ res.id, pls.format });
425 found = true;
426 break;
427 }
428 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100429
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200430 if (!found && secondary_resources)
431 {
432 for (auto &res : *secondary_resources)
433 {
434 if (res.name == pls.name)
435 {
436 ret.push_back({ res.id, pls.format });
437 found = true;
438 break;
439 }
440 }
441 }
442
443 if (!found)
444 fprintf(stderr, "Did not find stage input/output/target with name \"%s\".\n", pls.name.c_str());
445 }
446
447 return ret;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100448}
449
450static PlsFormat pls_format(const char *str)
451{
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200452 if (!strcmp(str, "r11f_g11f_b10f"))
453 return PlsR11FG11FB10F;
454 else if (!strcmp(str, "r32f"))
455 return PlsR32F;
456 else if (!strcmp(str, "rg16f"))
457 return PlsRG16F;
458 else if (!strcmp(str, "rg16"))
459 return PlsRG16;
460 else if (!strcmp(str, "rgb10_a2"))
461 return PlsRGB10A2;
462 else if (!strcmp(str, "rgba8"))
463 return PlsRGBA8;
464 else if (!strcmp(str, "rgba8i"))
465 return PlsRGBA8I;
466 else if (!strcmp(str, "rgba8ui"))
467 return PlsRGBA8UI;
468 else if (!strcmp(str, "rg16i"))
469 return PlsRG16I;
470 else if (!strcmp(str, "rgb10_a2ui"))
471 return PlsRGB10A2UI;
472 else if (!strcmp(str, "rg16ui"))
473 return PlsRG16UI;
474 else if (!strcmp(str, "r32ui"))
475 return PlsR32UI;
476 else
477 return PlsNone;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100478}
479
480int main(int argc, char *argv[])
481{
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200482 CLIArguments args;
483 CLICallbacks cbs;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100484
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200485 cbs.add("--help", [](CLIParser &parser) {
486 print_help();
487 parser.end();
488 });
489 cbs.add("--output", [&args](CLIParser &parser) { args.output = parser.next_string(); });
490 cbs.add("--es", [&args](CLIParser &) {
491 args.es = true;
492 args.set_es = true;
493 });
494 cbs.add("--no-es", [&args](CLIParser &) {
495 args.es = false;
496 args.set_es = true;
497 });
498 cbs.add("--version", [&args](CLIParser &parser) {
499 args.version = parser.next_uint();
500 args.set_version = true;
501 });
502 cbs.add("--dump-resources", [&args](CLIParser &) { args.dump_resources = true; });
503 cbs.add("--force-temporary", [&args](CLIParser &) { args.force_temporary = true; });
504 cbs.add("--flatten-ubo", [&args](CLIParser &) { args.flatten_ubo = true; });
505 cbs.add("--fixup-clipspace", [&args](CLIParser &) { args.fixup = true; });
506 cbs.add("--iterations", [&args](CLIParser &parser) { args.iterations = parser.next_uint(); });
507 cbs.add("--cpp", [&args](CLIParser &) { args.cpp = true; });
508 cbs.add("--cpp-interface-name", [&args](CLIParser &parser) { args.cpp_interface_name = parser.next_string(); });
509 cbs.add("--metal", [&args](CLIParser &) { args.metal = true; });
510 cbs.add("--vulkan-semantics", [&args](CLIParser &) { args.vulkan_semantics = true; });
511 cbs.add("--extension", [&args](CLIParser &parser) { args.extensions.push_back(parser.next_string()); });
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +0200512 cbs.add("--entry", [&args](CLIParser &parser) { args.entry = parser.next_string(); });
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200513 cbs.add("--remap", [&args](CLIParser &parser) {
514 string src = parser.next_string();
515 string dst = parser.next_string();
516 uint32_t components = parser.next_uint();
517 args.remaps.push_back({ move(src), move(dst), components });
518 });
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100519
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200520 cbs.add("--pls-in", [&args](CLIParser &parser) {
521 auto fmt = pls_format(parser.next_string());
522 auto name = parser.next_string();
523 args.pls_in.push_back({ move(fmt), move(name) });
524 });
525 cbs.add("--pls-out", [&args](CLIParser &parser) {
526 auto fmt = pls_format(parser.next_string());
527 auto name = parser.next_string();
528 args.pls_out.push_back({ move(fmt), move(name) });
529 });
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100530
Hans-Kristian Arntzenf61a5d12016-08-26 12:58:50 +0200531 cbs.add("--remove-unused-variables", [&args](CLIParser &) { args.remove_unused = true; });
532
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200533 cbs.default_handler = [&args](const char *value) { args.input = value; };
534 cbs.error_handler = [] { print_help(); };
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100535
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200536 CLIParser parser{ move(cbs), argc - 1, argv + 1 };
537 if (!parser.parse())
538 {
539 return EXIT_FAILURE;
540 }
541 else if (parser.ended_state)
542 {
543 return EXIT_SUCCESS;
544 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100545
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200546 if (!args.input)
547 {
548 fprintf(stderr, "Didn't specify input file.\n");
549 print_help();
550 return EXIT_FAILURE;
551 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100552
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200553 unique_ptr<CompilerGLSL> compiler;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100554
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200555 if (args.cpp)
556 {
557 compiler = unique_ptr<CompilerGLSL>(new CompilerCPP(read_spirv_file(args.input)));
558 if (args.cpp_interface_name)
559 static_cast<CompilerCPP *>(compiler.get())->set_interface_name(args.cpp_interface_name);
560 }
561 else if (args.metal)
562 compiler = unique_ptr<CompilerMSL>(new CompilerMSL(read_spirv_file(args.input)));
563 else
564 compiler = unique_ptr<CompilerGLSL>(new CompilerGLSL(read_spirv_file(args.input)));
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100565
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +0200566 if (!args.entry.empty())
567 compiler->set_entry_point(args.entry);
568
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200569 if (!args.set_version && !compiler->get_options().version)
570 {
571 fprintf(stderr, "Didn't specify GLSL version and SPIR-V did not specify language.\n");
572 print_help();
573 return EXIT_FAILURE;
574 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100575
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200576 CompilerGLSL::Options opts = compiler->get_options();
577 if (args.set_version)
578 opts.version = args.version;
579 if (args.set_es)
580 opts.es = args.es;
581 opts.force_temporary = args.force_temporary;
582 opts.vulkan_semantics = args.vulkan_semantics;
583 opts.vertex.fixup_clipspace = args.fixup;
584 compiler->set_options(opts);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100585
Hans-Kristian Arntzenf61a5d12016-08-26 12:58:50 +0200586 ShaderResources res;
587 if (args.remove_unused)
588 {
589 auto active = compiler->get_active_interface_variables();
590 res = compiler->get_shader_resources(active);
591 compiler->set_enabled_interface_variables(move(active));
592 }
593 else
594 res = compiler->get_shader_resources();
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100595
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200596 if (args.flatten_ubo)
597 for (auto &ubo : res.uniform_buffers)
598 compiler->flatten_interface_block(ubo.id);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100599
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200600 auto pls_inputs = remap_pls(args.pls_in, res.stage_inputs, &res.subpass_inputs);
601 auto pls_outputs = remap_pls(args.pls_out, res.stage_outputs, nullptr);
602 compiler->remap_pixel_local_storage(move(pls_inputs), move(pls_outputs));
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100603
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200604 for (auto &ext : args.extensions)
605 compiler->require_extension(ext);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100606
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200607 for (auto &remap : args.remaps)
608 {
609 if (remap_generic(*compiler, res.stage_inputs, remap))
610 continue;
611 if (remap_generic(*compiler, res.stage_outputs, remap))
612 continue;
613 if (remap_generic(*compiler, res.subpass_inputs, remap))
614 continue;
615 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100616
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200617 if (args.dump_resources)
618 {
619 print_resources(*compiler, res);
620 print_push_constant_resources(*compiler, res.push_constant_buffers);
621 }
622
623 string glsl;
624 for (uint32_t i = 0; i < args.iterations; i++)
625 glsl = compiler->compile();
626
627 if (args.output)
628 write_string_to_file(args.output, glsl.c_str());
629 else
630 printf("%s", glsl.c_str());
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100631}