blob: e41c251e5896c423b44e26a0099c7c7a8a6ea0c0 [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 Arntzen75471fb2016-03-02 18:09:16 +0100384};
385
386static void print_help()
387{
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200388 fprintf(stderr, "Usage: spirv-cross [--output <output path>] [SPIR-V file] [--es] [--no-es] [--version <GLSL "
389 "version>] [--dump-resources] [--help] [--force-temporary] [--cpp] [--cpp-interface-name <name>] "
390 "[--metal] [--vulkan-semantics] [--flatten-ubo] [--fixup-clipspace] [--iterations iter] [--pls-in "
391 "format input-name] [--pls-out format output-name] [--remap source_name target_name components] "
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +0200392 "[--extension ext] [--entry name]\n");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100393}
394
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200395static bool remap_generic(Compiler &compiler, const vector<Resource> &resources, const Remap &remap)
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100396{
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200397 auto itr =
398 find_if(begin(resources), end(resources), [&remap](const Resource &res) { return res.name == remap.src_name; });
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100399
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200400 if (itr != end(resources))
401 {
402 compiler.set_remapped_variable_state(itr->id, true);
403 compiler.set_name(itr->id, remap.dst_name);
404 compiler.set_subpass_input_remapped_components(itr->id, remap.components);
405 return true;
406 }
407 else
408 return false;
409}
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100410
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200411static vector<PlsRemap> remap_pls(const vector<PLSArg> &pls_variables, const vector<Resource> &resources,
412 const vector<Resource> *secondary_resources)
413{
414 vector<PlsRemap> ret;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100415
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200416 for (auto &pls : pls_variables)
417 {
418 bool found = false;
419 for (auto &res : resources)
420 {
421 if (res.name == pls.name)
422 {
423 ret.push_back({ res.id, pls.format });
424 found = true;
425 break;
426 }
427 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100428
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200429 if (!found && secondary_resources)
430 {
431 for (auto &res : *secondary_resources)
432 {
433 if (res.name == pls.name)
434 {
435 ret.push_back({ res.id, pls.format });
436 found = true;
437 break;
438 }
439 }
440 }
441
442 if (!found)
443 fprintf(stderr, "Did not find stage input/output/target with name \"%s\".\n", pls.name.c_str());
444 }
445
446 return ret;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100447}
448
449static PlsFormat pls_format(const char *str)
450{
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200451 if (!strcmp(str, "r11f_g11f_b10f"))
452 return PlsR11FG11FB10F;
453 else if (!strcmp(str, "r32f"))
454 return PlsR32F;
455 else if (!strcmp(str, "rg16f"))
456 return PlsRG16F;
457 else if (!strcmp(str, "rg16"))
458 return PlsRG16;
459 else if (!strcmp(str, "rgb10_a2"))
460 return PlsRGB10A2;
461 else if (!strcmp(str, "rgba8"))
462 return PlsRGBA8;
463 else if (!strcmp(str, "rgba8i"))
464 return PlsRGBA8I;
465 else if (!strcmp(str, "rgba8ui"))
466 return PlsRGBA8UI;
467 else if (!strcmp(str, "rg16i"))
468 return PlsRG16I;
469 else if (!strcmp(str, "rgb10_a2ui"))
470 return PlsRGB10A2UI;
471 else if (!strcmp(str, "rg16ui"))
472 return PlsRG16UI;
473 else if (!strcmp(str, "r32ui"))
474 return PlsR32UI;
475 else
476 return PlsNone;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100477}
478
479int main(int argc, char *argv[])
480{
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200481 CLIArguments args;
482 CLICallbacks cbs;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100483
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200484 cbs.add("--help", [](CLIParser &parser) {
485 print_help();
486 parser.end();
487 });
488 cbs.add("--output", [&args](CLIParser &parser) { args.output = parser.next_string(); });
489 cbs.add("--es", [&args](CLIParser &) {
490 args.es = true;
491 args.set_es = true;
492 });
493 cbs.add("--no-es", [&args](CLIParser &) {
494 args.es = false;
495 args.set_es = true;
496 });
497 cbs.add("--version", [&args](CLIParser &parser) {
498 args.version = parser.next_uint();
499 args.set_version = true;
500 });
501 cbs.add("--dump-resources", [&args](CLIParser &) { args.dump_resources = true; });
502 cbs.add("--force-temporary", [&args](CLIParser &) { args.force_temporary = true; });
503 cbs.add("--flatten-ubo", [&args](CLIParser &) { args.flatten_ubo = true; });
504 cbs.add("--fixup-clipspace", [&args](CLIParser &) { args.fixup = true; });
505 cbs.add("--iterations", [&args](CLIParser &parser) { args.iterations = parser.next_uint(); });
506 cbs.add("--cpp", [&args](CLIParser &) { args.cpp = true; });
507 cbs.add("--cpp-interface-name", [&args](CLIParser &parser) { args.cpp_interface_name = parser.next_string(); });
508 cbs.add("--metal", [&args](CLIParser &) { args.metal = true; });
509 cbs.add("--vulkan-semantics", [&args](CLIParser &) { args.vulkan_semantics = true; });
510 cbs.add("--extension", [&args](CLIParser &parser) { args.extensions.push_back(parser.next_string()); });
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +0200511 cbs.add("--entry", [&args](CLIParser &parser) { args.entry = parser.next_string(); });
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200512 cbs.add("--remap", [&args](CLIParser &parser) {
513 string src = parser.next_string();
514 string dst = parser.next_string();
515 uint32_t components = parser.next_uint();
516 args.remaps.push_back({ move(src), move(dst), components });
517 });
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100518
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200519 cbs.add("--pls-in", [&args](CLIParser &parser) {
520 auto fmt = pls_format(parser.next_string());
521 auto name = parser.next_string();
522 args.pls_in.push_back({ move(fmt), move(name) });
523 });
524 cbs.add("--pls-out", [&args](CLIParser &parser) {
525 auto fmt = pls_format(parser.next_string());
526 auto name = parser.next_string();
527 args.pls_out.push_back({ move(fmt), move(name) });
528 });
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100529
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200530 cbs.default_handler = [&args](const char *value) { args.input = value; };
531 cbs.error_handler = [] { print_help(); };
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100532
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200533 CLIParser parser{ move(cbs), argc - 1, argv + 1 };
534 if (!parser.parse())
535 {
536 return EXIT_FAILURE;
537 }
538 else if (parser.ended_state)
539 {
540 return EXIT_SUCCESS;
541 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100542
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200543 if (!args.input)
544 {
545 fprintf(stderr, "Didn't specify input file.\n");
546 print_help();
547 return EXIT_FAILURE;
548 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100549
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200550 unique_ptr<CompilerGLSL> compiler;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100551
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200552 if (args.cpp)
553 {
554 compiler = unique_ptr<CompilerGLSL>(new CompilerCPP(read_spirv_file(args.input)));
555 if (args.cpp_interface_name)
556 static_cast<CompilerCPP *>(compiler.get())->set_interface_name(args.cpp_interface_name);
557 }
558 else if (args.metal)
559 compiler = unique_ptr<CompilerMSL>(new CompilerMSL(read_spirv_file(args.input)));
560 else
561 compiler = unique_ptr<CompilerGLSL>(new CompilerGLSL(read_spirv_file(args.input)));
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100562
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +0200563 if (!args.entry.empty())
564 compiler->set_entry_point(args.entry);
565
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200566 if (!args.set_version && !compiler->get_options().version)
567 {
568 fprintf(stderr, "Didn't specify GLSL version and SPIR-V did not specify language.\n");
569 print_help();
570 return EXIT_FAILURE;
571 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100572
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200573 CompilerGLSL::Options opts = compiler->get_options();
574 if (args.set_version)
575 opts.version = args.version;
576 if (args.set_es)
577 opts.es = args.es;
578 opts.force_temporary = args.force_temporary;
579 opts.vulkan_semantics = args.vulkan_semantics;
580 opts.vertex.fixup_clipspace = args.fixup;
581 compiler->set_options(opts);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100582
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200583 auto res = compiler->get_shader_resources();
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100584
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200585 if (args.flatten_ubo)
586 for (auto &ubo : res.uniform_buffers)
587 compiler->flatten_interface_block(ubo.id);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100588
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200589 auto pls_inputs = remap_pls(args.pls_in, res.stage_inputs, &res.subpass_inputs);
590 auto pls_outputs = remap_pls(args.pls_out, res.stage_outputs, nullptr);
591 compiler->remap_pixel_local_storage(move(pls_inputs), move(pls_outputs));
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100592
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200593 for (auto &ext : args.extensions)
594 compiler->require_extension(ext);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100595
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200596 for (auto &remap : args.remaps)
597 {
598 if (remap_generic(*compiler, res.stage_inputs, remap))
599 continue;
600 if (remap_generic(*compiler, res.stage_outputs, remap))
601 continue;
602 if (remap_generic(*compiler, res.subpass_inputs, remap))
603 continue;
604 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100605
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +0200606 if (args.dump_resources)
607 {
608 print_resources(*compiler, res);
609 print_push_constant_resources(*compiler, res.push_constant_buffers);
610 }
611
612 string glsl;
613 for (uint32_t i = 0; i < args.iterations; i++)
614 glsl = compiler->compile();
615
616 if (args.output)
617 write_string_to_file(args.output, glsl.c_str());
618 else
619 printf("%s", glsl.c_str());
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100620}