2016-03-02 17:09:16 +00:00
|
|
|
/*
|
2017-01-28 08:00:40 +00:00
|
|
|
* Copyright 2015-2017 ARM Limited
|
2016-03-02 17:09:16 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-04-04 07:36:04 +00:00
|
|
|
#include "spirv_cpp.hpp"
|
2018-03-02 06:47:08 +00:00
|
|
|
#include "spirv_cross_util.hpp"
|
2017-08-11 18:54:58 +00:00
|
|
|
#include "spirv_glsl.hpp"
|
2017-01-25 16:30:52 +00:00
|
|
|
#include "spirv_hlsl.hpp"
|
2017-02-04 09:26:26 +00:00
|
|
|
#include "spirv_msl.hpp"
|
2018-06-03 18:16:37 +00:00
|
|
|
#include "spirv_reflect.hpp"
|
2016-07-06 09:04:06 +00:00
|
|
|
#include <algorithm>
|
2016-03-02 17:09:16 +00:00
|
|
|
#include <cstdio>
|
2016-07-06 09:04:06 +00:00
|
|
|
#include <cstring>
|
2016-03-02 17:09:16 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <limits>
|
|
|
|
#include <memory>
|
2016-07-06 09:04:06 +00:00
|
|
|
#include <stdexcept>
|
2016-03-02 17:09:16 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
|
|
|
|
2017-01-12 09:57:44 +00:00
|
|
|
#ifdef _MSC_VER
|
2017-01-13 15:32:54 +00:00
|
|
|
#pragma warning(disable : 4996)
|
2017-01-12 09:57:44 +00:00
|
|
|
#endif
|
|
|
|
|
2016-03-02 17:09:16 +00:00
|
|
|
using namespace spv;
|
2016-04-04 07:36:04 +00:00
|
|
|
using namespace spirv_cross;
|
2016-03-02 17:09:16 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2016-12-15 19:46:10 +00:00
|
|
|
#ifdef SPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS
|
2017-09-08 07:33:34 +00:00
|
|
|
static inline void THROW(const char *str)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "SPIRV-Cross will abort: %s\n", str);
|
|
|
|
fflush(stderr);
|
|
|
|
abort();
|
|
|
|
}
|
2016-12-15 19:46:10 +00:00
|
|
|
#else
|
2017-02-16 10:06:57 +00:00
|
|
|
#define THROW(x) throw runtime_error(x)
|
2016-12-15 19:46:10 +00:00
|
|
|
#endif
|
|
|
|
|
2016-03-02 17:09:16 +00:00
|
|
|
struct CLIParser;
|
|
|
|
struct CLICallbacks
|
|
|
|
{
|
2016-07-06 09:04:06 +00:00
|
|
|
void add(const char *cli, const function<void(CLIParser &)> &func)
|
|
|
|
{
|
|
|
|
callbacks[cli] = func;
|
|
|
|
}
|
|
|
|
unordered_map<string, function<void(CLIParser &)>> callbacks;
|
|
|
|
function<void()> error_handler;
|
|
|
|
function<void(const char *)> default_handler;
|
2016-03-02 17:09:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CLIParser
|
|
|
|
{
|
2016-07-06 09:04:06 +00:00
|
|
|
CLIParser(CLICallbacks cbs_, int argc_, char *argv_[])
|
|
|
|
: cbs(move(cbs_))
|
|
|
|
, argc(argc_)
|
|
|
|
, argv(argv_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parse()
|
|
|
|
{
|
2016-12-15 19:46:10 +00:00
|
|
|
#ifndef SPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS
|
2016-07-06 09:04:06 +00:00
|
|
|
try
|
2016-12-15 19:46:10 +00:00
|
|
|
#endif
|
2016-07-06 09:04:06 +00:00
|
|
|
{
|
|
|
|
while (argc && !ended_state)
|
|
|
|
{
|
|
|
|
const char *next = *argv++;
|
|
|
|
argc--;
|
|
|
|
|
|
|
|
if (*next != '-' && cbs.default_handler)
|
|
|
|
{
|
|
|
|
cbs.default_handler(next);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto itr = cbs.callbacks.find(next);
|
|
|
|
if (itr == ::end(cbs.callbacks))
|
|
|
|
{
|
2016-12-15 19:46:10 +00:00
|
|
|
THROW("Invalid argument");
|
2016-07-06 09:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
itr->second(*this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-12-15 19:46:10 +00:00
|
|
|
#ifndef SPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS
|
2016-07-06 09:04:06 +00:00
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
if (cbs.error_handler)
|
|
|
|
{
|
|
|
|
cbs.error_handler();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-15 19:46:10 +00:00
|
|
|
#endif
|
2016-07-06 09:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void end()
|
|
|
|
{
|
|
|
|
ended_state = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t next_uint()
|
|
|
|
{
|
|
|
|
if (!argc)
|
|
|
|
{
|
2016-12-15 19:46:10 +00:00
|
|
|
THROW("Tried to parse uint, but nothing left in arguments");
|
2016-07-06 09:04:06 +00:00
|
|
|
}
|
|
|
|
|
2018-06-03 10:00:22 +00:00
|
|
|
uint64_t val = stoul(*argv);
|
2016-07-06 09:04:06 +00:00
|
|
|
if (val > numeric_limits<uint32_t>::max())
|
|
|
|
{
|
2016-12-15 19:46:10 +00:00
|
|
|
THROW("next_uint() out of range");
|
2016-07-06 09:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
|
2018-06-03 10:00:22 +00:00
|
|
|
return uint32_t(val);
|
2016-07-06 09:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double next_double()
|
|
|
|
{
|
|
|
|
if (!argc)
|
|
|
|
{
|
2016-12-15 19:46:10 +00:00
|
|
|
THROW("Tried to parse double, but nothing left in arguments");
|
2016-07-06 09:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double val = stod(*argv);
|
|
|
|
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2018-06-03 18:16:37 +00:00
|
|
|
// Return a string only if it's not prefixed with `--`, otherwise return the default value
|
2018-06-20 17:25:38 +00:00
|
|
|
const char *next_value_string(const char *default_value)
|
2018-06-03 18:16:37 +00:00
|
|
|
{
|
|
|
|
if (!argc)
|
|
|
|
{
|
2018-06-20 17:25:38 +00:00
|
|
|
return default_value;
|
2018-06-03 18:16:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == strncmp("--", *argv, 2))
|
|
|
|
{
|
2018-06-20 17:25:38 +00:00
|
|
|
return default_value;
|
2018-06-03 18:16:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return next_string();
|
|
|
|
}
|
|
|
|
|
2016-07-06 09:04:06 +00:00
|
|
|
const char *next_string()
|
|
|
|
{
|
|
|
|
if (!argc)
|
|
|
|
{
|
2016-12-15 19:46:10 +00:00
|
|
|
THROW("Tried to parse string, but nothing left in arguments");
|
2016-07-06 09:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *ret = *argv;
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
CLICallbacks cbs;
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
bool ended_state = false;
|
2016-03-02 17:09:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static vector<uint32_t> read_spirv_file(const char *path)
|
|
|
|
{
|
2016-07-06 09:04:06 +00:00
|
|
|
FILE *file = fopen(path, "rb");
|
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to open SPIRV file: %s\n", path);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
long len = ftell(file) / sizeof(uint32_t);
|
|
|
|
rewind(file);
|
|
|
|
|
|
|
|
vector<uint32_t> spirv(len);
|
|
|
|
if (fread(spirv.data(), sizeof(uint32_t), len, file) != size_t(len))
|
|
|
|
spirv.clear();
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
return spirv;
|
2016-03-02 17:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool write_string_to_file(const char *path, const char *string)
|
|
|
|
{
|
2016-07-06 09:04:06 +00:00
|
|
|
FILE *file = fopen(path, "w");
|
|
|
|
if (!file)
|
|
|
|
{
|
2017-05-30 14:17:51 +00:00
|
|
|
fprintf(stderr, "Failed to write file: %s\n", path);
|
2016-07-06 09:04:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(file, "%s", string);
|
|
|
|
fclose(file);
|
|
|
|
return true;
|
2016-03-02 17:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void print_resources(const Compiler &compiler, const char *tag, const vector<Resource> &resources)
|
|
|
|
{
|
2016-07-06 09:04:06 +00:00
|
|
|
fprintf(stderr, "%s\n", tag);
|
|
|
|
fprintf(stderr, "=============\n\n");
|
2017-04-19 15:33:14 +00:00
|
|
|
bool print_ssbo = !strcmp(tag, "ssbos");
|
|
|
|
|
2016-07-06 09:04:06 +00:00
|
|
|
for (auto &res : resources)
|
|
|
|
{
|
|
|
|
auto &type = compiler.get_type(res.type_id);
|
|
|
|
|
2017-04-19 15:33:14 +00:00
|
|
|
if (print_ssbo && compiler.buffer_is_hlsl_counter_buffer(res.id))
|
|
|
|
continue;
|
|
|
|
|
2016-07-06 09:04:06 +00:00
|
|
|
// If we don't have a name, use the fallback for the type instead of the variable
|
|
|
|
// for SSBOs and UBOs since those are the only meaningful names to use externally.
|
|
|
|
// Push constant blocks are still accessed by name and not block name, even though they are technically Blocks.
|
|
|
|
bool is_push_constant = compiler.get_storage_class(res.id) == StorageClassPushConstant;
|
2018-03-12 12:09:25 +00:00
|
|
|
bool is_block = compiler.get_decoration_bitset(type.self).get(DecorationBlock) ||
|
|
|
|
compiler.get_decoration_bitset(type.self).get(DecorationBufferBlock);
|
2016-11-29 08:04:28 +00:00
|
|
|
bool is_sized_block = is_block && (compiler.get_storage_class(res.id) == StorageClassUniform ||
|
|
|
|
compiler.get_storage_class(res.id) == StorageClassUniformConstant);
|
2016-07-12 19:20:18 +00:00
|
|
|
uint32_t fallback_id = !is_push_constant && is_block ? res.base_type_id : res.id;
|
2016-07-06 09:04:06 +00:00
|
|
|
|
2016-11-28 14:01:36 +00:00
|
|
|
uint32_t block_size = 0;
|
2018-09-10 09:08:47 +00:00
|
|
|
uint32_t runtime_array_stride = 0;
|
2016-11-29 08:04:28 +00:00
|
|
|
if (is_sized_block)
|
2018-09-10 09:08:47 +00:00
|
|
|
{
|
|
|
|
auto &base_type = compiler.get_type(res.base_type_id);
|
|
|
|
block_size = uint32_t(compiler.get_declared_struct_size(base_type));
|
|
|
|
runtime_array_stride = uint32_t(compiler.get_declared_struct_size_runtime_array(base_type, 1) -
|
|
|
|
compiler.get_declared_struct_size_runtime_array(base_type, 0));
|
|
|
|
}
|
2016-11-28 14:01:36 +00:00
|
|
|
|
2018-06-03 10:00:22 +00:00
|
|
|
Bitset mask;
|
|
|
|
if (print_ssbo)
|
|
|
|
mask = compiler.get_buffer_block_flags(res.id);
|
|
|
|
else
|
|
|
|
mask = compiler.get_decoration_bitset(res.id);
|
|
|
|
|
2016-07-12 19:20:18 +00:00
|
|
|
string array;
|
|
|
|
for (auto arr : type.array)
|
|
|
|
array = join("[", arr ? convert_to_string(arr) : "", "]") + array;
|
|
|
|
|
|
|
|
fprintf(stderr, " ID %03u : %s%s", res.id,
|
|
|
|
!res.name.empty() ? res.name.c_str() : compiler.get_fallback_name(fallback_id).c_str(), array.c_str());
|
2016-07-06 09:04:06 +00:00
|
|
|
|
2018-03-12 12:09:25 +00:00
|
|
|
if (mask.get(DecorationLocation))
|
2016-07-06 09:04:06 +00:00
|
|
|
fprintf(stderr, " (Location : %u)", compiler.get_decoration(res.id, DecorationLocation));
|
2018-03-12 12:09:25 +00:00
|
|
|
if (mask.get(DecorationDescriptorSet))
|
2016-07-06 09:04:06 +00:00
|
|
|
fprintf(stderr, " (Set : %u)", compiler.get_decoration(res.id, DecorationDescriptorSet));
|
2018-03-12 12:09:25 +00:00
|
|
|
if (mask.get(DecorationBinding))
|
2016-07-06 09:04:06 +00:00
|
|
|
fprintf(stderr, " (Binding : %u)", compiler.get_decoration(res.id, DecorationBinding));
|
2018-03-12 12:09:25 +00:00
|
|
|
if (mask.get(DecorationInputAttachmentIndex))
|
2016-07-06 09:04:06 +00:00
|
|
|
fprintf(stderr, " (Attachment : %u)", compiler.get_decoration(res.id, DecorationInputAttachmentIndex));
|
2018-03-12 12:09:25 +00:00
|
|
|
if (mask.get(DecorationNonReadable))
|
2017-01-05 17:16:33 +00:00
|
|
|
fprintf(stderr, " writeonly");
|
2018-03-12 12:09:25 +00:00
|
|
|
if (mask.get(DecorationNonWritable))
|
2017-01-05 17:16:33 +00:00
|
|
|
fprintf(stderr, " readonly");
|
2016-11-29 08:04:28 +00:00
|
|
|
if (is_sized_block)
|
2018-09-10 09:08:47 +00:00
|
|
|
{
|
2016-11-28 14:01:36 +00:00
|
|
|
fprintf(stderr, " (BlockSize : %u bytes)", block_size);
|
2018-09-10 09:08:47 +00:00
|
|
|
if (runtime_array_stride)
|
|
|
|
fprintf(stderr, " (Unsized array stride: %u bytes)", runtime_array_stride);
|
|
|
|
}
|
2017-04-19 15:33:14 +00:00
|
|
|
|
|
|
|
uint32_t counter_id = 0;
|
|
|
|
if (print_ssbo && compiler.buffer_get_hlsl_counter_buffer(res.id, counter_id))
|
|
|
|
fprintf(stderr, " (HLSL counter buffer ID: %u)", counter_id);
|
2016-07-06 09:04:06 +00:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
fprintf(stderr, "=============\n\n");
|
2016-03-02 17:09:16 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 09:16:02 +00:00
|
|
|
static const char *execution_model_to_str(spv::ExecutionModel model)
|
|
|
|
{
|
|
|
|
switch (model)
|
|
|
|
{
|
|
|
|
case spv::ExecutionModelVertex:
|
|
|
|
return "vertex";
|
|
|
|
case spv::ExecutionModelTessellationControl:
|
|
|
|
return "tessellation control";
|
|
|
|
case ExecutionModelTessellationEvaluation:
|
|
|
|
return "tessellation evaluation";
|
|
|
|
case ExecutionModelGeometry:
|
|
|
|
return "geometry";
|
|
|
|
case ExecutionModelFragment:
|
|
|
|
return "fragment";
|
|
|
|
case ExecutionModelGLCompute:
|
|
|
|
return "compute";
|
|
|
|
default:
|
|
|
|
return "???";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 17:09:16 +00:00
|
|
|
static void print_resources(const Compiler &compiler, const ShaderResources &res)
|
|
|
|
{
|
2018-03-12 12:09:25 +00:00
|
|
|
auto &modes = compiler.get_execution_mode_bitset();
|
2016-07-04 11:30:05 +00:00
|
|
|
|
2016-07-28 09:16:02 +00:00
|
|
|
fprintf(stderr, "Entry points:\n");
|
2018-03-01 13:00:04 +00:00
|
|
|
auto entry_points = compiler.get_entry_points_and_stages();
|
2016-07-28 09:16:02 +00:00
|
|
|
for (auto &e : entry_points)
|
2018-03-01 13:00:04 +00:00
|
|
|
fprintf(stderr, " %s (%s)\n", e.name.c_str(), execution_model_to_str(e.execution_model));
|
2016-07-28 09:16:02 +00:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
2016-07-04 11:30:05 +00:00
|
|
|
fprintf(stderr, "Execution modes:\n");
|
2018-03-12 12:09:25 +00:00
|
|
|
modes.for_each_bit([&](uint32_t i) {
|
2016-07-04 11:30:05 +00:00
|
|
|
auto mode = static_cast<ExecutionMode>(i);
|
|
|
|
uint32_t arg0 = compiler.get_execution_mode_argument(mode, 0);
|
|
|
|
uint32_t arg1 = compiler.get_execution_mode_argument(mode, 1);
|
|
|
|
uint32_t arg2 = compiler.get_execution_mode_argument(mode, 2);
|
|
|
|
|
|
|
|
switch (static_cast<ExecutionMode>(i))
|
|
|
|
{
|
2016-07-06 09:04:06 +00:00
|
|
|
case ExecutionModeInvocations:
|
|
|
|
fprintf(stderr, " Invocations: %u\n", arg0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ExecutionModeLocalSize:
|
|
|
|
fprintf(stderr, " LocalSize: (%u, %u, %u)\n", arg0, arg1, arg2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ExecutionModeOutputVertices:
|
|
|
|
fprintf(stderr, " OutputVertices: %u\n", arg0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
#define CHECK_MODE(m) \
|
|
|
|
case ExecutionMode##m: \
|
|
|
|
fprintf(stderr, " %s\n", #m); \
|
|
|
|
break
|
|
|
|
CHECK_MODE(SpacingEqual);
|
|
|
|
CHECK_MODE(SpacingFractionalEven);
|
|
|
|
CHECK_MODE(SpacingFractionalOdd);
|
|
|
|
CHECK_MODE(VertexOrderCw);
|
|
|
|
CHECK_MODE(VertexOrderCcw);
|
|
|
|
CHECK_MODE(PixelCenterInteger);
|
|
|
|
CHECK_MODE(OriginUpperLeft);
|
|
|
|
CHECK_MODE(OriginLowerLeft);
|
|
|
|
CHECK_MODE(EarlyFragmentTests);
|
|
|
|
CHECK_MODE(PointMode);
|
|
|
|
CHECK_MODE(Xfb);
|
|
|
|
CHECK_MODE(DepthReplacing);
|
|
|
|
CHECK_MODE(DepthGreater);
|
|
|
|
CHECK_MODE(DepthLess);
|
|
|
|
CHECK_MODE(DepthUnchanged);
|
|
|
|
CHECK_MODE(LocalSizeHint);
|
|
|
|
CHECK_MODE(InputPoints);
|
|
|
|
CHECK_MODE(InputLines);
|
|
|
|
CHECK_MODE(InputLinesAdjacency);
|
|
|
|
CHECK_MODE(Triangles);
|
|
|
|
CHECK_MODE(InputTrianglesAdjacency);
|
|
|
|
CHECK_MODE(Quads);
|
|
|
|
CHECK_MODE(Isolines);
|
|
|
|
CHECK_MODE(OutputPoints);
|
|
|
|
CHECK_MODE(OutputLineStrip);
|
|
|
|
CHECK_MODE(OutputTriangleStrip);
|
|
|
|
CHECK_MODE(VecTypeHint);
|
|
|
|
CHECK_MODE(ContractionOff);
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2016-07-04 11:30:05 +00:00
|
|
|
}
|
2018-03-12 12:09:25 +00:00
|
|
|
});
|
2016-07-04 11:30:05 +00:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
2016-07-06 09:04:06 +00:00
|
|
|
print_resources(compiler, "subpass inputs", res.subpass_inputs);
|
|
|
|
print_resources(compiler, "inputs", res.stage_inputs);
|
|
|
|
print_resources(compiler, "outputs", res.stage_outputs);
|
|
|
|
print_resources(compiler, "textures", res.sampled_images);
|
2016-09-11 11:47:06 +00:00
|
|
|
print_resources(compiler, "separate images", res.separate_images);
|
2016-09-10 11:05:35 +00:00
|
|
|
print_resources(compiler, "separate samplers", res.separate_samplers);
|
2016-07-06 09:04:06 +00:00
|
|
|
print_resources(compiler, "images", res.storage_images);
|
|
|
|
print_resources(compiler, "ssbos", res.storage_buffers);
|
|
|
|
print_resources(compiler, "ubos", res.uniform_buffers);
|
|
|
|
print_resources(compiler, "push", res.push_constant_buffers);
|
|
|
|
print_resources(compiler, "counters", res.atomic_counters);
|
2016-03-02 17:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void print_push_constant_resources(const Compiler &compiler, const vector<Resource> &res)
|
|
|
|
{
|
2016-07-06 09:04:06 +00:00
|
|
|
for (auto &block : res)
|
|
|
|
{
|
|
|
|
auto ranges = compiler.get_active_buffer_ranges(block.id);
|
|
|
|
fprintf(stderr, "Active members in buffer: %s\n",
|
|
|
|
!block.name.empty() ? block.name.c_str() : compiler.get_fallback_name(block.id).c_str());
|
|
|
|
|
|
|
|
fprintf(stderr, "==================\n\n");
|
|
|
|
for (auto &range : ranges)
|
|
|
|
{
|
2016-07-12 19:20:18 +00:00
|
|
|
const auto &name = compiler.get_member_name(block.base_type_id, range.index);
|
2016-07-06 09:04:06 +00:00
|
|
|
|
|
|
|
fprintf(stderr, "Member #%3u (%s): Offset: %4u, Range: %4u\n", range.index,
|
|
|
|
!name.empty() ? name.c_str() : compiler.get_fallback_member_name(range.index).c_str(),
|
|
|
|
unsigned(range.offset), unsigned(range.range));
|
|
|
|
}
|
|
|
|
fprintf(stderr, "==================\n\n");
|
|
|
|
}
|
2016-03-02 17:09:16 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 13:16:07 +00:00
|
|
|
static void print_spec_constants(const Compiler &compiler)
|
|
|
|
{
|
|
|
|
auto spec_constants = compiler.get_specialization_constants();
|
|
|
|
fprintf(stderr, "Specialization constants\n");
|
|
|
|
fprintf(stderr, "==================\n\n");
|
|
|
|
for (auto &c : spec_constants)
|
|
|
|
fprintf(stderr, "ID: %u, Spec ID: %u\n", c.id, c.constant_id);
|
|
|
|
fprintf(stderr, "==================\n\n");
|
|
|
|
}
|
|
|
|
|
2017-08-15 13:27:53 +00:00
|
|
|
static void print_capabilities_and_extensions(const Compiler &compiler)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Capabilities\n");
|
|
|
|
fprintf(stderr, "============\n");
|
|
|
|
for (auto &capability : compiler.get_declared_capabilities())
|
|
|
|
fprintf(stderr, "Capability: %u\n", static_cast<unsigned>(capability));
|
|
|
|
fprintf(stderr, "============\n\n");
|
|
|
|
|
|
|
|
fprintf(stderr, "Extensions\n");
|
|
|
|
fprintf(stderr, "============\n");
|
|
|
|
for (auto &ext : compiler.get_declared_extensions())
|
|
|
|
fprintf(stderr, "Extension: %s\n", ext.c_str());
|
|
|
|
fprintf(stderr, "============\n\n");
|
|
|
|
}
|
|
|
|
|
2016-03-02 17:09:16 +00:00
|
|
|
struct PLSArg
|
|
|
|
{
|
2016-07-06 09:04:06 +00:00
|
|
|
PlsFormat format;
|
|
|
|
string name;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Remap
|
|
|
|
{
|
|
|
|
string src_name;
|
|
|
|
string dst_name;
|
|
|
|
unsigned components;
|
2016-03-02 17:09:16 +00:00
|
|
|
};
|
|
|
|
|
2016-09-20 08:17:41 +00:00
|
|
|
struct VariableTypeRemap
|
|
|
|
{
|
|
|
|
string variable_name;
|
|
|
|
string new_variable_type;
|
|
|
|
};
|
|
|
|
|
2017-07-24 07:29:20 +00:00
|
|
|
struct InterfaceVariableRename
|
|
|
|
{
|
|
|
|
StorageClass storageClass;
|
|
|
|
uint32_t location;
|
|
|
|
string variable_name;
|
2017-06-25 13:16:09 +00:00
|
|
|
};
|
|
|
|
|
2016-03-02 17:09:16 +00:00
|
|
|
struct CLIArguments
|
|
|
|
{
|
2016-07-06 09:04:06 +00:00
|
|
|
const char *input = nullptr;
|
|
|
|
const char *output = nullptr;
|
|
|
|
const char *cpp_interface_name = nullptr;
|
|
|
|
uint32_t version = 0;
|
2017-02-16 10:17:12 +00:00
|
|
|
uint32_t shader_model = 0;
|
2017-11-07 20:38:13 +00:00
|
|
|
uint32_t msl_version = 0;
|
2016-07-06 09:04:06 +00:00
|
|
|
bool es = false;
|
|
|
|
bool set_version = false;
|
2017-02-16 10:17:12 +00:00
|
|
|
bool set_shader_model = false;
|
2017-11-07 20:38:13 +00:00
|
|
|
bool set_msl_version = false;
|
2016-07-06 09:04:06 +00:00
|
|
|
bool set_es = false;
|
|
|
|
bool dump_resources = false;
|
|
|
|
bool force_temporary = false;
|
|
|
|
bool flatten_ubo = false;
|
|
|
|
bool fixup = false;
|
2017-08-03 11:02:59 +00:00
|
|
|
bool yflip = false;
|
2017-05-22 13:30:43 +00:00
|
|
|
bool sso = false;
|
2018-06-22 08:01:38 +00:00
|
|
|
bool support_nonzero_baseinstance = true;
|
2018-09-20 01:36:33 +00:00
|
|
|
bool msl_swizzle_texture_samples = false;
|
2016-07-06 09:04:06 +00:00
|
|
|
vector<PLSArg> pls_in;
|
|
|
|
vector<PLSArg> pls_out;
|
|
|
|
vector<Remap> remaps;
|
|
|
|
vector<string> extensions;
|
2016-09-20 08:17:41 +00:00
|
|
|
vector<VariableTypeRemap> variable_type_remaps;
|
2017-06-23 20:20:42 +00:00
|
|
|
vector<InterfaceVariableRename> interface_variable_renames;
|
2017-11-13 08:46:45 +00:00
|
|
|
vector<HLSLVertexAttributeRemap> hlsl_attr_remap;
|
2016-07-28 09:16:02 +00:00
|
|
|
string entry;
|
2018-03-01 13:00:04 +00:00
|
|
|
string entry_stage;
|
2016-07-06 09:04:06 +00:00
|
|
|
|
2018-03-01 13:00:04 +00:00
|
|
|
struct Rename
|
|
|
|
{
|
|
|
|
string old_name;
|
|
|
|
string new_name;
|
|
|
|
ExecutionModel execution_model;
|
|
|
|
};
|
|
|
|
vector<Rename> entry_point_rename;
|
2017-11-13 12:49:11 +00:00
|
|
|
|
2016-07-06 09:04:06 +00:00
|
|
|
uint32_t iterations = 1;
|
|
|
|
bool cpp = false;
|
2018-06-03 18:16:37 +00:00
|
|
|
string reflect;
|
2017-03-20 01:06:21 +00:00
|
|
|
bool msl = false;
|
2017-01-25 16:30:52 +00:00
|
|
|
bool hlsl = false;
|
2017-05-04 08:10:30 +00:00
|
|
|
bool hlsl_compat = false;
|
2016-07-06 09:04:06 +00:00
|
|
|
bool vulkan_semantics = false;
|
2017-05-22 15:40:00 +00:00
|
|
|
bool flatten_multidimensional_arrays = false;
|
2017-09-08 07:56:06 +00:00
|
|
|
bool use_420pack_extension = true;
|
2016-08-26 10:58:50 +00:00
|
|
|
bool remove_unused = false;
|
2018-05-23 19:34:26 +00:00
|
|
|
bool combined_samplers_inherit_bindings = false;
|
2016-03-02 17:09:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void print_help()
|
|
|
|
{
|
2018-04-18 14:19:55 +00:00
|
|
|
fprintf(stderr, "Usage: spirv-cross\n"
|
|
|
|
"\t[--output <output path>]\n"
|
|
|
|
"\t[SPIR-V file]\n"
|
|
|
|
"\t[--es]\n"
|
|
|
|
"\t[--no-es]\n"
|
|
|
|
"\t[--version <GLSL version>]\n"
|
|
|
|
"\t[--dump-resources]\n"
|
|
|
|
"\t[--help]\n"
|
|
|
|
"\t[--force-temporary]\n"
|
|
|
|
"\t[--vulkan-semantics]\n"
|
|
|
|
"\t[--flatten-ubo]\n"
|
|
|
|
"\t[--fixup-clipspace]\n"
|
|
|
|
"\t[--flip-vert-y]\n"
|
|
|
|
"\t[--iterations iter]\n"
|
|
|
|
"\t[--cpp]\n"
|
|
|
|
"\t[--cpp-interface-name <name>]\n"
|
|
|
|
"\t[--msl]\n"
|
|
|
|
"\t[--msl-version <MMmmpp>]\n"
|
|
|
|
"\t[--hlsl]\n"
|
2018-06-22 08:01:38 +00:00
|
|
|
"\t[--reflect]\n"
|
2018-04-18 14:19:55 +00:00
|
|
|
"\t[--shader-model]\n"
|
|
|
|
"\t[--hlsl-enable-compat]\n"
|
|
|
|
"\t[--separate-shader-objects]\n"
|
|
|
|
"\t[--pls-in format input-name]\n"
|
|
|
|
"\t[--pls-out format output-name]\n"
|
|
|
|
"\t[--remap source_name target_name components]\n"
|
|
|
|
"\t[--extension ext]\n"
|
|
|
|
"\t[--entry name]\n"
|
|
|
|
"\t[--stage <stage (vert, frag, geom, tesc, tese comp)>]\n"
|
|
|
|
"\t[--remove-unused-variables]\n"
|
|
|
|
"\t[--flatten-multidimensional-arrays]\n"
|
|
|
|
"\t[--no-420pack-extension]\n"
|
|
|
|
"\t[--remap-variable-type <variable_name> <new_variable_type>]\n"
|
|
|
|
"\t[--rename-interface-variable <in|out> <location> <new_variable_name>]\n"
|
|
|
|
"\t[--set-hlsl-vertex-input-semantic <location> <semantic>]\n"
|
|
|
|
"\t[--rename-entry-point <old> <new> <stage>]\n"
|
2018-06-22 08:01:38 +00:00
|
|
|
"\t[--combined-samplers-inherit-bindings]\n"
|
|
|
|
"\t[--no-support-nonzero-baseinstance]\n"
|
2017-07-24 07:29:20 +00:00
|
|
|
"\n");
|
2016-03-02 17:09:16 +00:00
|
|
|
}
|
|
|
|
|
2016-07-06 09:04:06 +00:00
|
|
|
static bool remap_generic(Compiler &compiler, const vector<Resource> &resources, const Remap &remap)
|
2016-03-02 17:09:16 +00:00
|
|
|
{
|
2016-07-06 09:04:06 +00:00
|
|
|
auto itr =
|
|
|
|
find_if(begin(resources), end(resources), [&remap](const Resource &res) { return res.name == remap.src_name; });
|
|
|
|
|
|
|
|
if (itr != end(resources))
|
|
|
|
{
|
|
|
|
compiler.set_remapped_variable_state(itr->id, true);
|
|
|
|
compiler.set_name(itr->id, remap.dst_name);
|
|
|
|
compiler.set_subpass_input_remapped_components(itr->id, remap.components);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static vector<PlsRemap> remap_pls(const vector<PLSArg> &pls_variables, const vector<Resource> &resources,
|
|
|
|
const vector<Resource> *secondary_resources)
|
|
|
|
{
|
|
|
|
vector<PlsRemap> ret;
|
|
|
|
|
|
|
|
for (auto &pls : pls_variables)
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
for (auto &res : resources)
|
|
|
|
{
|
|
|
|
if (res.name == pls.name)
|
|
|
|
{
|
|
|
|
ret.push_back({ res.id, pls.format });
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found && secondary_resources)
|
|
|
|
{
|
|
|
|
for (auto &res : *secondary_resources)
|
|
|
|
{
|
|
|
|
if (res.name == pls.name)
|
|
|
|
{
|
|
|
|
ret.push_back({ res.id, pls.format });
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
fprintf(stderr, "Did not find stage input/output/target with name \"%s\".\n", pls.name.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2016-03-02 17:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static PlsFormat pls_format(const char *str)
|
|
|
|
{
|
2016-07-06 09:04:06 +00:00
|
|
|
if (!strcmp(str, "r11f_g11f_b10f"))
|
|
|
|
return PlsR11FG11FB10F;
|
|
|
|
else if (!strcmp(str, "r32f"))
|
|
|
|
return PlsR32F;
|
|
|
|
else if (!strcmp(str, "rg16f"))
|
|
|
|
return PlsRG16F;
|
|
|
|
else if (!strcmp(str, "rg16"))
|
|
|
|
return PlsRG16;
|
|
|
|
else if (!strcmp(str, "rgb10_a2"))
|
|
|
|
return PlsRGB10A2;
|
|
|
|
else if (!strcmp(str, "rgba8"))
|
|
|
|
return PlsRGBA8;
|
|
|
|
else if (!strcmp(str, "rgba8i"))
|
|
|
|
return PlsRGBA8I;
|
|
|
|
else if (!strcmp(str, "rgba8ui"))
|
|
|
|
return PlsRGBA8UI;
|
|
|
|
else if (!strcmp(str, "rg16i"))
|
|
|
|
return PlsRG16I;
|
|
|
|
else if (!strcmp(str, "rgb10_a2ui"))
|
|
|
|
return PlsRGB10A2UI;
|
|
|
|
else if (!strcmp(str, "rg16ui"))
|
|
|
|
return PlsRG16UI;
|
|
|
|
else if (!strcmp(str, "r32ui"))
|
|
|
|
return PlsR32UI;
|
|
|
|
else
|
|
|
|
return PlsNone;
|
2016-03-02 17:09:16 +00:00
|
|
|
}
|
|
|
|
|
2018-03-01 13:00:04 +00:00
|
|
|
static ExecutionModel stage_to_execution_model(const std::string &stage)
|
|
|
|
{
|
|
|
|
if (stage == "vert")
|
|
|
|
return ExecutionModelVertex;
|
|
|
|
else if (stage == "frag")
|
|
|
|
return ExecutionModelFragment;
|
|
|
|
else if (stage == "comp")
|
|
|
|
return ExecutionModelGLCompute;
|
|
|
|
else if (stage == "tesc")
|
|
|
|
return ExecutionModelTessellationControl;
|
|
|
|
else if (stage == "tese")
|
|
|
|
return ExecutionModelTessellationEvaluation;
|
|
|
|
else if (stage == "geom")
|
|
|
|
return ExecutionModelGeometry;
|
|
|
|
else
|
|
|
|
SPIRV_CROSS_THROW("Invalid stage.");
|
|
|
|
}
|
|
|
|
|
2017-09-08 07:33:34 +00:00
|
|
|
static int main_inner(int argc, char *argv[])
|
2016-03-02 17:09:16 +00:00
|
|
|
{
|
2016-07-06 09:04:06 +00:00
|
|
|
CLIArguments args;
|
|
|
|
CLICallbacks cbs;
|
|
|
|
|
|
|
|
cbs.add("--help", [](CLIParser &parser) {
|
|
|
|
print_help();
|
|
|
|
parser.end();
|
|
|
|
});
|
|
|
|
cbs.add("--output", [&args](CLIParser &parser) { args.output = parser.next_string(); });
|
|
|
|
cbs.add("--es", [&args](CLIParser &) {
|
|
|
|
args.es = true;
|
|
|
|
args.set_es = true;
|
|
|
|
});
|
|
|
|
cbs.add("--no-es", [&args](CLIParser &) {
|
|
|
|
args.es = false;
|
|
|
|
args.set_es = true;
|
|
|
|
});
|
|
|
|
cbs.add("--version", [&args](CLIParser &parser) {
|
|
|
|
args.version = parser.next_uint();
|
|
|
|
args.set_version = true;
|
|
|
|
});
|
|
|
|
cbs.add("--dump-resources", [&args](CLIParser &) { args.dump_resources = true; });
|
|
|
|
cbs.add("--force-temporary", [&args](CLIParser &) { args.force_temporary = true; });
|
|
|
|
cbs.add("--flatten-ubo", [&args](CLIParser &) { args.flatten_ubo = true; });
|
|
|
|
cbs.add("--fixup-clipspace", [&args](CLIParser &) { args.fixup = true; });
|
2017-08-03 11:02:59 +00:00
|
|
|
cbs.add("--flip-vert-y", [&args](CLIParser &) { args.yflip = true; });
|
2016-07-06 09:04:06 +00:00
|
|
|
cbs.add("--iterations", [&args](CLIParser &parser) { args.iterations = parser.next_uint(); });
|
|
|
|
cbs.add("--cpp", [&args](CLIParser &) { args.cpp = true; });
|
2018-06-21 14:43:52 +00:00
|
|
|
cbs.add("--reflect", [&args](CLIParser &parser) { args.reflect = parser.next_value_string("json"); });
|
2016-07-06 09:04:06 +00:00
|
|
|
cbs.add("--cpp-interface-name", [&args](CLIParser &parser) { args.cpp_interface_name = parser.next_string(); });
|
2017-03-20 01:06:21 +00:00
|
|
|
cbs.add("--metal", [&args](CLIParser &) { args.msl = true; }); // Legacy compatibility
|
|
|
|
cbs.add("--msl", [&args](CLIParser &) { args.msl = true; });
|
2017-01-25 16:30:52 +00:00
|
|
|
cbs.add("--hlsl", [&args](CLIParser &) { args.hlsl = true; });
|
2017-05-04 08:10:30 +00:00
|
|
|
cbs.add("--hlsl-enable-compat", [&args](CLIParser &) { args.hlsl_compat = true; });
|
2016-07-06 09:04:06 +00:00
|
|
|
cbs.add("--vulkan-semantics", [&args](CLIParser &) { args.vulkan_semantics = true; });
|
2017-05-22 15:40:00 +00:00
|
|
|
cbs.add("--flatten-multidimensional-arrays", [&args](CLIParser &) { args.flatten_multidimensional_arrays = true; });
|
2017-09-08 07:56:06 +00:00
|
|
|
cbs.add("--no-420pack-extension", [&args](CLIParser &) { args.use_420pack_extension = false; });
|
2018-09-20 01:36:33 +00:00
|
|
|
cbs.add("--msl-swizzle-texture-samples", [&args](CLIParser &) { args.msl_swizzle_texture_samples = true; });
|
2016-07-06 09:04:06 +00:00
|
|
|
cbs.add("--extension", [&args](CLIParser &parser) { args.extensions.push_back(parser.next_string()); });
|
2017-11-13 12:49:11 +00:00
|
|
|
cbs.add("--rename-entry-point", [&args](CLIParser &parser) {
|
|
|
|
auto old_name = parser.next_string();
|
|
|
|
auto new_name = parser.next_string();
|
2018-03-01 13:00:04 +00:00
|
|
|
auto model = stage_to_execution_model(parser.next_string());
|
|
|
|
args.entry_point_rename.push_back({ old_name, new_name, move(model) });
|
2017-11-13 12:49:11 +00:00
|
|
|
});
|
2016-07-28 09:16:02 +00:00
|
|
|
cbs.add("--entry", [&args](CLIParser &parser) { args.entry = parser.next_string(); });
|
2018-03-01 13:00:04 +00:00
|
|
|
cbs.add("--stage", [&args](CLIParser &parser) { args.entry_stage = parser.next_string(); });
|
2017-05-22 13:30:43 +00:00
|
|
|
cbs.add("--separate-shader-objects", [&args](CLIParser &) { args.sso = true; });
|
2017-11-13 08:46:45 +00:00
|
|
|
cbs.add("--set-hlsl-vertex-input-semantic", [&args](CLIParser &parser) {
|
|
|
|
HLSLVertexAttributeRemap remap;
|
|
|
|
remap.location = parser.next_uint();
|
|
|
|
remap.semantic = parser.next_string();
|
|
|
|
args.hlsl_attr_remap.push_back(move(remap));
|
|
|
|
});
|
|
|
|
|
2016-07-06 09:04:06 +00:00
|
|
|
cbs.add("--remap", [&args](CLIParser &parser) {
|
2017-11-13 08:52:35 +00:00
|
|
|
string src = parser.next_string();
|
|
|
|
string dst = parser.next_string();
|
|
|
|
uint32_t components = parser.next_uint();
|
|
|
|
args.remaps.push_back({ move(src), move(dst), components });
|
2016-07-06 09:04:06 +00:00
|
|
|
});
|
|
|
|
|
2016-09-20 08:17:41 +00:00
|
|
|
cbs.add("--remap-variable-type", [&args](CLIParser &parser) {
|
|
|
|
string var_name = parser.next_string();
|
|
|
|
string new_type = parser.next_string();
|
|
|
|
args.variable_type_remaps.push_back({ move(var_name), move(new_type) });
|
|
|
|
});
|
|
|
|
|
2017-06-23 20:20:42 +00:00
|
|
|
cbs.add("--rename-interface-variable", [&args](CLIParser &parser) {
|
2017-06-25 13:16:09 +00:00
|
|
|
StorageClass cls = StorageClassMax;
|
|
|
|
string clsStr = parser.next_string();
|
|
|
|
if (clsStr == "in")
|
|
|
|
cls = StorageClassInput;
|
|
|
|
else if (clsStr == "out")
|
|
|
|
cls = StorageClassOutput;
|
|
|
|
|
2017-06-23 20:20:42 +00:00
|
|
|
uint32_t loc = parser.next_uint();
|
|
|
|
string var_name = parser.next_string();
|
2017-06-25 13:16:09 +00:00
|
|
|
args.interface_variable_renames.push_back({ cls, loc, move(var_name) });
|
2017-06-23 20:20:42 +00:00
|
|
|
});
|
|
|
|
|
2016-07-06 09:04:06 +00:00
|
|
|
cbs.add("--pls-in", [&args](CLIParser &parser) {
|
|
|
|
auto fmt = pls_format(parser.next_string());
|
|
|
|
auto name = parser.next_string();
|
|
|
|
args.pls_in.push_back({ move(fmt), move(name) });
|
|
|
|
});
|
|
|
|
cbs.add("--pls-out", [&args](CLIParser &parser) {
|
|
|
|
auto fmt = pls_format(parser.next_string());
|
|
|
|
auto name = parser.next_string();
|
|
|
|
args.pls_out.push_back({ move(fmt), move(name) });
|
|
|
|
});
|
2017-02-16 10:17:12 +00:00
|
|
|
cbs.add("--shader-model", [&args](CLIParser &parser) {
|
|
|
|
args.shader_model = parser.next_uint();
|
|
|
|
args.set_shader_model = true;
|
|
|
|
});
|
2017-11-07 20:38:13 +00:00
|
|
|
cbs.add("--msl-version", [&args](CLIParser &parser) {
|
|
|
|
args.msl_version = parser.next_uint();
|
|
|
|
args.set_msl_version = true;
|
|
|
|
});
|
2016-07-06 09:04:06 +00:00
|
|
|
|
2016-08-26 10:58:50 +00:00
|
|
|
cbs.add("--remove-unused-variables", [&args](CLIParser &) { args.remove_unused = true; });
|
2018-05-25 08:14:13 +00:00
|
|
|
cbs.add("--combined-samplers-inherit-bindings",
|
|
|
|
[&args](CLIParser &) { args.combined_samplers_inherit_bindings = true; });
|
2016-08-26 10:58:50 +00:00
|
|
|
|
2018-06-22 08:01:38 +00:00
|
|
|
cbs.add("--no-support-nonzero-baseinstance", [&](CLIParser &) { args.support_nonzero_baseinstance = false; });
|
|
|
|
|
2016-07-06 09:04:06 +00:00
|
|
|
cbs.default_handler = [&args](const char *value) { args.input = value; };
|
|
|
|
cbs.error_handler = [] { print_help(); };
|
|
|
|
|
|
|
|
CLIParser parser{ move(cbs), argc - 1, argv + 1 };
|
|
|
|
if (!parser.parse())
|
|
|
|
{
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
else if (parser.ended_state)
|
|
|
|
{
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!args.input)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Didn't specify input file.\n");
|
|
|
|
print_help();
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2018-06-18 16:30:16 +00:00
|
|
|
// Special case reflection because it has little to do with the path followed by code-outputting compilers
|
|
|
|
if (!args.reflect.empty())
|
|
|
|
{
|
|
|
|
CompilerReflection compiler(read_spirv_file(args.input));
|
|
|
|
compiler.set_format(args.reflect);
|
|
|
|
auto json = compiler.compile();
|
|
|
|
if (args.output)
|
|
|
|
write_string_to_file(args.output, json.c_str());
|
|
|
|
else
|
|
|
|
printf("%s", json.c_str());
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2016-07-06 09:04:06 +00:00
|
|
|
|
2018-06-18 16:30:16 +00:00
|
|
|
unique_ptr<CompilerGLSL> compiler;
|
2016-09-10 11:56:36 +00:00
|
|
|
bool combined_image_samplers = false;
|
2018-02-21 12:43:16 +00:00
|
|
|
bool build_dummy_sampler = false;
|
2016-09-10 11:56:36 +00:00
|
|
|
|
2016-07-06 09:04:06 +00:00
|
|
|
if (args.cpp)
|
|
|
|
{
|
|
|
|
compiler = unique_ptr<CompilerGLSL>(new CompilerCPP(read_spirv_file(args.input)));
|
|
|
|
if (args.cpp_interface_name)
|
|
|
|
static_cast<CompilerCPP *>(compiler.get())->set_interface_name(args.cpp_interface_name);
|
|
|
|
}
|
2017-03-20 01:06:21 +00:00
|
|
|
else if (args.msl)
|
2017-03-12 21:42:51 +00:00
|
|
|
{
|
2016-07-06 09:04:06 +00:00
|
|
|
compiler = unique_ptr<CompilerMSL>(new CompilerMSL(read_spirv_file(args.input)));
|
2017-03-12 21:42:51 +00:00
|
|
|
|
|
|
|
auto *msl_comp = static_cast<CompilerMSL *>(compiler.get());
|
2018-03-09 14:25:25 +00:00
|
|
|
auto msl_opts = msl_comp->get_msl_options();
|
2017-11-07 20:38:13 +00:00
|
|
|
if (args.set_msl_version)
|
|
|
|
msl_opts.msl_version = args.msl_version;
|
2018-09-20 01:36:33 +00:00
|
|
|
msl_opts.swizzle_texture_samples = args.msl_swizzle_texture_samples;
|
2018-03-09 14:25:25 +00:00
|
|
|
msl_comp->set_msl_options(msl_opts);
|
2017-03-12 21:42:51 +00:00
|
|
|
}
|
2017-01-25 16:30:52 +00:00
|
|
|
else if (args.hlsl)
|
|
|
|
compiler = unique_ptr<CompilerHLSL>(new CompilerHLSL(read_spirv_file(args.input)));
|
2016-07-06 09:04:06 +00:00
|
|
|
else
|
2016-09-10 11:56:36 +00:00
|
|
|
{
|
|
|
|
combined_image_samplers = !args.vulkan_semantics;
|
2018-02-21 12:43:16 +00:00
|
|
|
build_dummy_sampler = true;
|
2016-07-06 09:04:06 +00:00
|
|
|
compiler = unique_ptr<CompilerGLSL>(new CompilerGLSL(read_spirv_file(args.input)));
|
2016-09-10 11:56:36 +00:00
|
|
|
}
|
2016-07-06 09:04:06 +00:00
|
|
|
|
2016-09-20 08:17:41 +00:00
|
|
|
if (!args.variable_type_remaps.empty())
|
|
|
|
{
|
|
|
|
auto remap_cb = [&](const SPIRType &, const string &name, string &out) -> void {
|
|
|
|
for (const VariableTypeRemap &remap : args.variable_type_remaps)
|
|
|
|
if (name == remap.variable_name)
|
|
|
|
out = remap.new_variable_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
compiler->set_variable_type_remap_callback(move(remap_cb));
|
|
|
|
}
|
|
|
|
|
2017-11-13 12:49:11 +00:00
|
|
|
for (auto &rename : args.entry_point_rename)
|
2018-03-01 13:00:04 +00:00
|
|
|
compiler->rename_entry_point(rename.old_name, rename.new_name, rename.execution_model);
|
|
|
|
|
|
|
|
auto entry_points = compiler->get_entry_points_and_stages();
|
|
|
|
auto entry_point = args.entry;
|
|
|
|
ExecutionModel model = ExecutionModelMax;
|
|
|
|
|
|
|
|
if (!args.entry_stage.empty())
|
|
|
|
{
|
|
|
|
model = stage_to_execution_model(args.entry_stage);
|
|
|
|
if (entry_point.empty())
|
|
|
|
{
|
|
|
|
// Just use the first entry point with this stage.
|
|
|
|
for (auto &e : entry_points)
|
|
|
|
{
|
|
|
|
if (e.execution_model == model)
|
|
|
|
{
|
|
|
|
entry_point = e.name;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry_point.empty())
|
|
|
|
{
|
2018-03-01 13:03:59 +00:00
|
|
|
fprintf(stderr, "Could not find an entry point with stage: %s\n", args.entry_stage.c_str());
|
2018-03-01 13:00:04 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Make sure both stage and name exists.
|
|
|
|
bool exists = false;
|
|
|
|
for (auto &e : entry_points)
|
|
|
|
{
|
|
|
|
if (e.execution_model == model && e.name == entry_point)
|
|
|
|
{
|
|
|
|
exists = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!exists)
|
|
|
|
{
|
2018-03-01 13:03:59 +00:00
|
|
|
fprintf(stderr, "Could not find an entry point %s with stage: %s\n", entry_point.c_str(),
|
|
|
|
args.entry_stage.c_str());
|
2018-03-01 13:00:04 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!entry_point.empty())
|
|
|
|
{
|
|
|
|
// Make sure there is just one entry point with this name, or the stage
|
|
|
|
// is ambiguous.
|
|
|
|
uint32_t stage_count = 0;
|
|
|
|
for (auto &e : entry_points)
|
|
|
|
{
|
|
|
|
if (e.name == entry_point)
|
|
|
|
{
|
|
|
|
stage_count++;
|
|
|
|
model = e.execution_model;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stage_count == 0)
|
|
|
|
{
|
2018-03-01 13:03:59 +00:00
|
|
|
fprintf(stderr, "There is no entry point with name: %s\n", entry_point.c_str());
|
2018-03-01 13:00:04 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
else if (stage_count > 1)
|
|
|
|
{
|
2018-03-01 13:03:59 +00:00
|
|
|
fprintf(stderr, "There is more than one entry point with name: %s. Use --stage.\n", entry_point.c_str());
|
2018-03-01 13:00:04 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2017-11-13 12:49:11 +00:00
|
|
|
|
2018-03-01 13:00:04 +00:00
|
|
|
if (!entry_point.empty())
|
|
|
|
compiler->set_entry_point(entry_point, model);
|
2016-07-28 09:16:02 +00:00
|
|
|
|
2018-03-09 14:25:25 +00:00
|
|
|
if (!args.set_version && !compiler->get_common_options().version)
|
2016-07-06 09:04:06 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Didn't specify GLSL version and SPIR-V did not specify language.\n");
|
|
|
|
print_help();
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2018-03-09 14:25:25 +00:00
|
|
|
CompilerGLSL::Options opts = compiler->get_common_options();
|
2016-07-06 09:04:06 +00:00
|
|
|
if (args.set_version)
|
|
|
|
opts.version = args.version;
|
|
|
|
if (args.set_es)
|
|
|
|
opts.es = args.es;
|
|
|
|
opts.force_temporary = args.force_temporary;
|
2017-05-22 13:30:43 +00:00
|
|
|
opts.separate_shader_objects = args.sso;
|
2017-05-22 15:40:00 +00:00
|
|
|
opts.flatten_multidimensional_arrays = args.flatten_multidimensional_arrays;
|
2017-09-08 07:56:06 +00:00
|
|
|
opts.enable_420pack_extension = args.use_420pack_extension;
|
2016-07-06 09:04:06 +00:00
|
|
|
opts.vulkan_semantics = args.vulkan_semantics;
|
|
|
|
opts.vertex.fixup_clipspace = args.fixup;
|
2017-08-03 11:02:59 +00:00
|
|
|
opts.vertex.flip_vert_y = args.yflip;
|
2018-06-22 08:01:38 +00:00
|
|
|
opts.vertex.support_nonzero_base_instance = args.support_nonzero_baseinstance;
|
2018-03-09 14:25:25 +00:00
|
|
|
compiler->set_common_options(opts);
|
2016-07-06 09:04:06 +00:00
|
|
|
|
2017-02-16 10:17:12 +00:00
|
|
|
// Set HLSL specific options.
|
|
|
|
if (args.hlsl)
|
|
|
|
{
|
|
|
|
auto *hlsl = static_cast<CompilerHLSL *>(compiler.get());
|
2018-03-09 14:25:25 +00:00
|
|
|
auto hlsl_opts = hlsl->get_hlsl_options();
|
2017-02-16 10:17:12 +00:00
|
|
|
if (args.set_shader_model)
|
|
|
|
{
|
|
|
|
if (args.shader_model < 30)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Shader model earlier than 30 (3.0) not supported.\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
hlsl_opts.shader_model = args.shader_model;
|
|
|
|
}
|
2017-05-04 08:10:30 +00:00
|
|
|
|
|
|
|
if (args.hlsl_compat)
|
|
|
|
{
|
|
|
|
// Enable all compat options.
|
|
|
|
hlsl_opts.point_size_compat = true;
|
2018-02-23 14:56:25 +00:00
|
|
|
hlsl_opts.point_coord_compat = true;
|
2017-05-04 08:10:30 +00:00
|
|
|
}
|
2018-03-09 14:25:25 +00:00
|
|
|
hlsl->set_hlsl_options(hlsl_opts);
|
2017-02-16 10:17:12 +00:00
|
|
|
}
|
|
|
|
|
2018-02-21 12:43:16 +00:00
|
|
|
if (build_dummy_sampler)
|
2018-03-12 12:09:25 +00:00
|
|
|
{
|
|
|
|
uint32_t sampler = compiler->build_dummy_sampler_for_combined_images();
|
|
|
|
if (sampler != 0)
|
|
|
|
{
|
|
|
|
// Set some defaults to make validation happy.
|
|
|
|
compiler->set_decoration(sampler, DecorationDescriptorSet, 0);
|
|
|
|
compiler->set_decoration(sampler, DecorationBinding, 0);
|
|
|
|
}
|
|
|
|
}
|
2018-02-21 12:43:16 +00:00
|
|
|
|
2016-08-26 10:58:50 +00:00
|
|
|
ShaderResources res;
|
|
|
|
if (args.remove_unused)
|
|
|
|
{
|
|
|
|
auto active = compiler->get_active_interface_variables();
|
|
|
|
res = compiler->get_shader_resources(active);
|
|
|
|
compiler->set_enabled_interface_variables(move(active));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
res = compiler->get_shader_resources();
|
2016-03-02 17:09:16 +00:00
|
|
|
|
2016-07-06 09:04:06 +00:00
|
|
|
if (args.flatten_ubo)
|
2017-01-21 09:27:14 +00:00
|
|
|
{
|
2016-07-06 09:04:06 +00:00
|
|
|
for (auto &ubo : res.uniform_buffers)
|
2017-01-16 22:19:49 +00:00
|
|
|
compiler->flatten_buffer_block(ubo.id);
|
2017-01-21 09:27:14 +00:00
|
|
|
for (auto &ubo : res.push_constant_buffers)
|
|
|
|
compiler->flatten_buffer_block(ubo.id);
|
|
|
|
}
|
2016-07-06 09:04:06 +00:00
|
|
|
|
|
|
|
auto pls_inputs = remap_pls(args.pls_in, res.stage_inputs, &res.subpass_inputs);
|
|
|
|
auto pls_outputs = remap_pls(args.pls_out, res.stage_outputs, nullptr);
|
|
|
|
compiler->remap_pixel_local_storage(move(pls_inputs), move(pls_outputs));
|
|
|
|
|
|
|
|
for (auto &ext : args.extensions)
|
|
|
|
compiler->require_extension(ext);
|
|
|
|
|
|
|
|
for (auto &remap : args.remaps)
|
|
|
|
{
|
|
|
|
if (remap_generic(*compiler, res.stage_inputs, remap))
|
|
|
|
continue;
|
|
|
|
if (remap_generic(*compiler, res.stage_outputs, remap))
|
|
|
|
continue;
|
|
|
|
if (remap_generic(*compiler, res.subpass_inputs, remap))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-07-24 07:29:20 +00:00
|
|
|
for (auto &rename : args.interface_variable_renames)
|
2017-06-23 20:20:42 +00:00
|
|
|
{
|
2017-06-25 13:16:09 +00:00
|
|
|
if (rename.storageClass == StorageClassInput)
|
2018-03-02 06:47:08 +00:00
|
|
|
spirv_cross_util::rename_interface_variable(*compiler, res.stage_inputs, rename.location,
|
|
|
|
rename.variable_name);
|
2017-06-25 13:16:09 +00:00
|
|
|
else if (rename.storageClass == StorageClassOutput)
|
2018-03-02 06:47:08 +00:00
|
|
|
spirv_cross_util::rename_interface_variable(*compiler, res.stage_outputs, rename.location,
|
|
|
|
rename.variable_name);
|
2017-06-23 20:20:42 +00:00
|
|
|
else
|
|
|
|
{
|
2017-06-25 13:16:09 +00:00
|
|
|
fprintf(stderr, "error at --rename-interface-variable <in|out> ...\n");
|
2017-06-23 20:20:42 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-06 09:04:06 +00:00
|
|
|
if (args.dump_resources)
|
|
|
|
{
|
|
|
|
print_resources(*compiler, res);
|
|
|
|
print_push_constant_resources(*compiler, res.push_constant_buffers);
|
2016-09-17 13:16:07 +00:00
|
|
|
print_spec_constants(*compiler);
|
2017-08-15 13:27:53 +00:00
|
|
|
print_capabilities_and_extensions(*compiler);
|
2016-07-06 09:04:06 +00:00
|
|
|
}
|
|
|
|
|
2016-09-10 11:56:36 +00:00
|
|
|
if (combined_image_samplers)
|
|
|
|
{
|
|
|
|
compiler->build_combined_image_samplers();
|
2018-05-23 19:34:26 +00:00
|
|
|
if (args.combined_samplers_inherit_bindings)
|
|
|
|
spirv_cross_util::inherit_combined_sampler_bindings(*compiler);
|
|
|
|
|
2016-09-10 11:56:36 +00:00
|
|
|
// Give the remapped combined samplers new names.
|
|
|
|
for (auto &remap : compiler->get_combined_image_samplers())
|
|
|
|
{
|
2016-09-11 10:13:31 +00:00
|
|
|
compiler->set_name(remap.combined_id, join("SPIRV_Cross_Combined", compiler->get_name(remap.image_id),
|
2016-09-10 11:56:36 +00:00
|
|
|
compiler->get_name(remap.sampler_id)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-05 09:27:42 +00:00
|
|
|
if (args.hlsl)
|
|
|
|
{
|
|
|
|
auto *hlsl_compiler = static_cast<CompilerHLSL *>(compiler.get());
|
|
|
|
uint32_t new_builtin = hlsl_compiler->remap_num_workgroups_builtin();
|
|
|
|
if (new_builtin)
|
|
|
|
{
|
|
|
|
hlsl_compiler->set_decoration(new_builtin, DecorationDescriptorSet, 0);
|
|
|
|
hlsl_compiler->set_decoration(new_builtin, DecorationBinding, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-06 09:04:06 +00:00
|
|
|
string glsl;
|
|
|
|
for (uint32_t i = 0; i < args.iterations; i++)
|
2017-11-13 08:46:45 +00:00
|
|
|
{
|
|
|
|
if (args.hlsl)
|
|
|
|
glsl = static_cast<CompilerHLSL *>(compiler.get())->compile(move(args.hlsl_attr_remap));
|
|
|
|
else
|
|
|
|
glsl = compiler->compile();
|
|
|
|
}
|
2016-07-06 09:04:06 +00:00
|
|
|
|
|
|
|
if (args.output)
|
|
|
|
write_string_to_file(args.output, glsl.c_str());
|
|
|
|
else
|
|
|
|
printf("%s", glsl.c_str());
|
2017-09-08 07:33:34 +00:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
#ifdef SPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS
|
|
|
|
return main_inner(argc, argv);
|
|
|
|
#else
|
|
|
|
// Make sure we catch the exception or it just disappears into the aether on Windows.
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return main_inner(argc, argv);
|
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "SPIRV-Cross threw an exception: %s\n", e.what());
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
#endif
|
2016-07-06 09:04:06 +00:00
|
|
|
}
|