2020-03-02 20:47:43 +00:00
|
|
|
// Copyright 2020 The Tint Authors.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-04-30 01:16:12 +00:00
|
|
|
#include <cstdio>
|
2020-03-02 20:47:43 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
2020-09-30 00:05:23 +00:00
|
|
|
#include <sstream>
|
2020-04-30 01:16:12 +00:00
|
|
|
#include <string>
|
2020-03-02 20:47:43 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2020-06-22 14:36:34 +00:00
|
|
|
#if TINT_BUILD_SPV_READER
|
|
|
|
#include "spirv-tools/libspirv.hpp"
|
|
|
|
#endif // TINT_BUILD_SPV_READER
|
|
|
|
|
2020-11-03 16:26:09 +00:00
|
|
|
#include "tint/tint.h"
|
2020-11-03 13:15:19 +00:00
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
enum class Format {
|
|
|
|
kNone = -1,
|
|
|
|
kSpirv,
|
|
|
|
kSpvAsm,
|
|
|
|
kWgsl,
|
2020-06-23 17:48:40 +00:00
|
|
|
kMsl,
|
2020-07-20 22:13:37 +00:00
|
|
|
kHlsl,
|
2020-03-02 20:47:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Options {
|
|
|
|
bool show_help = false;
|
|
|
|
|
|
|
|
std::string input_filename;
|
2020-04-30 01:16:12 +00:00
|
|
|
std::string output_file = "-"; // Default to stdout
|
2020-03-02 20:47:43 +00:00
|
|
|
|
|
|
|
bool parse_only = false;
|
|
|
|
bool dump_ast = false;
|
2020-10-08 20:22:35 +00:00
|
|
|
bool dawn_validation = false;
|
2021-01-05 22:45:50 +00:00
|
|
|
bool demangle = false;
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2020-04-30 01:16:12 +00:00
|
|
|
Format format = Format::kNone;
|
2020-09-03 01:46:36 +00:00
|
|
|
|
|
|
|
bool emit_single_entry_point = false;
|
|
|
|
tint::ast::PipelineStage stage;
|
|
|
|
std::string ep_name;
|
2020-09-30 00:05:23 +00:00
|
|
|
|
|
|
|
std::vector<std::string> transforms;
|
2020-03-02 20:47:43 +00:00
|
|
|
};
|
|
|
|
|
2020-06-22 14:36:34 +00:00
|
|
|
const char kUsage[] = R"(Usage: tint [options] <input-file>
|
2020-03-02 20:47:43 +00:00
|
|
|
|
|
|
|
options:
|
2020-07-20 22:13:37 +00:00
|
|
|
--format <spirv|spvasm|wgsl|msl|hlsl> -- Output format.
|
2020-04-30 01:16:12 +00:00
|
|
|
If not provided, will be inferred from output
|
|
|
|
filename extension:
|
|
|
|
.spvasm -> spvasm
|
2020-07-20 22:13:37 +00:00
|
|
|
.spv -> spirv
|
|
|
|
.wgsl -> wgsl
|
|
|
|
.metal -> msl
|
|
|
|
.hlsl -> hlsl
|
2020-04-30 01:16:12 +00:00
|
|
|
If none matches, then default to SPIR-V assembly.
|
2020-09-03 01:46:36 +00:00
|
|
|
-ep <compute|fragment|vertex> <name> -- Output single entry point
|
2020-04-30 01:16:12 +00:00
|
|
|
--output-file <name> -- Output file name. Use "-" for standard output
|
|
|
|
-o <name> -- Output file name. Use "-" for standard output
|
2020-12-04 09:06:09 +00:00
|
|
|
--transform <name list> -- Runs transforms, name list is comma separated
|
2020-09-30 00:05:23 +00:00
|
|
|
Available transforms:
|
|
|
|
bound_array_accessors
|
2020-12-03 14:54:09 +00:00
|
|
|
emit_vertex_point_size
|
2020-12-08 21:07:24 +00:00
|
|
|
first_index_offset
|
2020-03-02 20:47:43 +00:00
|
|
|
--parse-only -- Stop after parsing the input
|
|
|
|
--dump-ast -- Dump the generated AST to stdout
|
2020-10-08 20:22:35 +00:00
|
|
|
--dawn-validation -- SPIRV outputs are validated with the same flags
|
|
|
|
as Dawn does. Has no effect on non-SPIRV outputs.
|
2021-01-05 22:45:50 +00:00
|
|
|
--demangle -- Preserve original source names. Demangle them.
|
|
|
|
Affects AST dumping, and text-based output languages.
|
2020-03-02 20:47:43 +00:00
|
|
|
-h -- This help text)";
|
|
|
|
|
2020-03-18 14:08:48 +00:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wunused-parameter"
|
2020-03-02 20:47:43 +00:00
|
|
|
Format parse_format(const std::string& fmt) {
|
2020-03-18 14:08:48 +00:00
|
|
|
#pragma clang diagnostic pop
|
|
|
|
|
|
|
|
#if TINT_BUILD_SPV_WRITER
|
2020-03-02 20:47:43 +00:00
|
|
|
if (fmt == "spirv")
|
|
|
|
return Format::kSpirv;
|
|
|
|
if (fmt == "spvasm")
|
|
|
|
return Format::kSpvAsm;
|
2020-03-18 14:08:48 +00:00
|
|
|
#endif // TINT_BUILD_SPV_WRITER
|
|
|
|
|
|
|
|
#if TINT_BUILD_WGSL_WRITER
|
2020-03-02 20:47:43 +00:00
|
|
|
if (fmt == "wgsl")
|
|
|
|
return Format::kWgsl;
|
2020-03-18 14:08:48 +00:00
|
|
|
#endif // TINT_BUILD_WGSL_WRITER
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2020-06-23 17:48:40 +00:00
|
|
|
#if TINT_BUILD_MSL_WRITER
|
|
|
|
if (fmt == "msl")
|
|
|
|
return Format::kMsl;
|
|
|
|
#endif // TINT_BUILD_MSL_WRITER
|
|
|
|
|
2020-07-20 22:13:37 +00:00
|
|
|
#if TINT_BUILD_HLSL_WRITER
|
|
|
|
if (fmt == "hlsl")
|
|
|
|
return Format::kHlsl;
|
|
|
|
#endif // TINT_BUILD_HLSL_WRITER
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
return Format::kNone;
|
|
|
|
}
|
|
|
|
|
2020-04-30 01:16:12 +00:00
|
|
|
/// @param input input string
|
|
|
|
/// @param suffix potential suffix string
|
|
|
|
/// @returns true if input ends with the given suffix.
|
|
|
|
bool ends_with(const std::string& input, const std::string& suffix) {
|
|
|
|
const auto input_len = input.size();
|
|
|
|
const auto suffix_len = suffix.size();
|
|
|
|
// Avoid integer overflow.
|
2020-05-01 16:17:03 +00:00
|
|
|
return (input_len >= suffix_len) &&
|
|
|
|
(input_len - suffix_len == input.rfind(suffix));
|
2020-04-30 01:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @param filename the filename to inspect
|
|
|
|
/// @returns the inferred format for the filename suffix
|
2020-06-23 17:48:40 +00:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wunused-parameter"
|
2020-04-30 01:16:12 +00:00
|
|
|
Format infer_format(const std::string& filename) {
|
2020-06-23 17:48:40 +00:00
|
|
|
#pragma clang diagnostic pop
|
|
|
|
|
|
|
|
#if TINT_BUILD_SPV_WRITER
|
2020-04-30 01:16:12 +00:00
|
|
|
if (ends_with(filename, ".spv")) {
|
|
|
|
return Format::kSpirv;
|
|
|
|
}
|
|
|
|
if (ends_with(filename, ".spvasm")) {
|
|
|
|
return Format::kSpvAsm;
|
|
|
|
}
|
2020-06-23 17:48:40 +00:00
|
|
|
#endif // TINT_BUILD_SPV_WRITER
|
|
|
|
|
|
|
|
#if TINT_BUILD_WGSL_WRITER
|
2020-04-30 01:16:12 +00:00
|
|
|
if (ends_with(filename, ".wgsl")) {
|
|
|
|
return Format::kWgsl;
|
|
|
|
}
|
2020-06-23 17:48:40 +00:00
|
|
|
#endif // TINT_BUILD_WGSL_WRITER
|
|
|
|
|
|
|
|
#if TINT_BUILD_MSL_WRITER
|
|
|
|
if (ends_with(filename, ".metal")) {
|
|
|
|
return Format::kMsl;
|
|
|
|
}
|
|
|
|
#endif // TINT_BUILD_WGSL_WRITER
|
|
|
|
|
2020-04-30 01:16:12 +00:00
|
|
|
return Format::kNone;
|
|
|
|
}
|
|
|
|
|
2020-09-03 01:46:36 +00:00
|
|
|
tint::ast::PipelineStage convert_to_pipeline_stage(const std::string& name) {
|
|
|
|
if (name == "compute") {
|
|
|
|
return tint::ast::PipelineStage::kCompute;
|
|
|
|
}
|
|
|
|
if (name == "fragment") {
|
|
|
|
return tint::ast::PipelineStage::kFragment;
|
|
|
|
}
|
|
|
|
if (name == "vertex") {
|
|
|
|
return tint::ast::PipelineStage::kVertex;
|
|
|
|
}
|
|
|
|
return tint::ast::PipelineStage::kNone;
|
|
|
|
}
|
|
|
|
|
2020-09-30 00:05:23 +00:00
|
|
|
std::vector<std::string> split_transform_names(std::string list) {
|
|
|
|
std::vector<std::string> res;
|
|
|
|
|
|
|
|
std::stringstream str(list);
|
|
|
|
while (str.good()) {
|
|
|
|
std::string substr;
|
|
|
|
getline(str, substr, ',');
|
|
|
|
res.push_back(substr);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
bool ParseArgs(const std::vector<std::string>& args, Options* opts) {
|
|
|
|
for (size_t i = 1; i < args.size(); ++i) {
|
|
|
|
const std::string& arg = args[i];
|
|
|
|
if (arg == "--format") {
|
|
|
|
++i;
|
|
|
|
if (i >= args.size()) {
|
|
|
|
std::cerr << "Missing value for --format argument." << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
opts->format = parse_format(args[i]);
|
|
|
|
|
|
|
|
if (opts->format == Format::kNone) {
|
|
|
|
std::cerr << "Unknown output format: " << args[i] << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-03 01:46:36 +00:00
|
|
|
} else if (arg == "-ep") {
|
|
|
|
if (i + 2 >= args.size()) {
|
|
|
|
std::cerr << "Missing values for -ep" << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
opts->stage = convert_to_pipeline_stage(args[i]);
|
|
|
|
if (opts->stage == tint::ast::PipelineStage::kNone) {
|
|
|
|
std::cerr << "Invalid pipeline stage: " << args[i] << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
opts->ep_name = args[i];
|
|
|
|
opts->emit_single_entry_point = true;
|
|
|
|
|
2020-07-08 15:28:26 +00:00
|
|
|
} else if (arg == "-o" || arg == "--output-name") {
|
2020-03-02 20:47:43 +00:00
|
|
|
++i;
|
|
|
|
if (i >= args.size()) {
|
2020-04-30 01:16:12 +00:00
|
|
|
std::cerr << "Missing value for " << arg << std::endl;
|
2020-03-02 20:47:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-30 01:16:12 +00:00
|
|
|
opts->output_file = args[i];
|
2020-03-02 20:47:43 +00:00
|
|
|
|
|
|
|
} else if (arg == "-h" || arg == "--help") {
|
|
|
|
opts->show_help = true;
|
2020-09-30 00:05:23 +00:00
|
|
|
} else if (arg == "--transform") {
|
|
|
|
++i;
|
|
|
|
if (i >= args.size()) {
|
|
|
|
std::cerr << "Missing value for " << arg << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
opts->transforms = split_transform_names(args[i]);
|
2020-03-02 20:47:43 +00:00
|
|
|
} else if (arg == "--parse-only") {
|
|
|
|
opts->parse_only = true;
|
|
|
|
} else if (arg == "--dump-ast") {
|
|
|
|
opts->dump_ast = true;
|
2020-10-08 20:22:35 +00:00
|
|
|
} else if (arg == "--dawn-validation") {
|
|
|
|
opts->dawn_validation = true;
|
2021-01-05 22:45:50 +00:00
|
|
|
} else if (arg == "--demangle") {
|
|
|
|
opts->demangle = true;
|
2020-03-02 20:47:43 +00:00
|
|
|
} else if (!arg.empty()) {
|
2020-07-06 13:15:26 +00:00
|
|
|
if (arg[0] == '-') {
|
|
|
|
std::cerr << "Unrecognized option: " << arg << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!opts->input_filename.empty()) {
|
|
|
|
std::cerr << "More than one input file specified: '"
|
|
|
|
<< opts->input_filename << "' and '" << arg << "'"
|
|
|
|
<< std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-02 20:47:43 +00:00
|
|
|
opts->input_filename = arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-02 15:31:08 +00:00
|
|
|
/// Copies the content from the file named `input_file` to `buffer`,
|
|
|
|
/// assuming each element in the file is of type `T`. If any error occurs,
|
2020-03-10 22:54:12 +00:00
|
|
|
/// writes error messages to the standard error stream and returns false.
|
2020-12-02 15:31:08 +00:00
|
|
|
/// Assumes the size of a `T` object is divisible by its required alignment.
|
2020-03-10 22:54:12 +00:00
|
|
|
/// @returns true if we successfully read the file.
|
2020-03-18 14:08:48 +00:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wunused-template"
|
2020-03-10 22:54:12 +00:00
|
|
|
template <typename T>
|
|
|
|
bool ReadFile(const std::string& input_file, std::vector<T>* buffer) {
|
2020-04-08 18:52:53 +00:00
|
|
|
#pragma clang diagnostic pop
|
2020-03-18 14:08:48 +00:00
|
|
|
|
2020-03-10 22:54:12 +00:00
|
|
|
if (!buffer) {
|
|
|
|
std::cerr << "The buffer pointer was null" << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
FILE* file = nullptr;
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
fopen_s(&file, input_file.c_str(), "rb");
|
|
|
|
#else
|
|
|
|
file = fopen(input_file.c_str(), "rb");
|
|
|
|
#endif
|
|
|
|
if (!file) {
|
|
|
|
std::cerr << "Failed to open " << input_file << std::endl;
|
2020-03-10 22:54:12 +00:00
|
|
|
return false;
|
2020-03-02 20:47:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
uint64_t tell_file_size = static_cast<uint64_t>(ftell(file));
|
|
|
|
if (tell_file_size <= 0) {
|
|
|
|
std::cerr << "Input file of incorrect size: " << input_file << std::endl;
|
|
|
|
fclose(file);
|
|
|
|
return {};
|
|
|
|
}
|
2020-03-10 22:54:12 +00:00
|
|
|
const auto file_size = static_cast<size_t>(tell_file_size);
|
|
|
|
if (0 != (file_size % sizeof(T))) {
|
|
|
|
std::cerr << "File " << input_file
|
|
|
|
<< " does not contain an integral number of objects: "
|
|
|
|
<< file_size << " bytes in the file, require " << sizeof(T)
|
|
|
|
<< " bytes per object" << std::endl;
|
|
|
|
fclose(file);
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-02 20:47:43 +00:00
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
|
2020-03-10 22:54:12 +00:00
|
|
|
buffer->clear();
|
|
|
|
buffer->resize(file_size / sizeof(T));
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2020-03-10 22:54:12 +00:00
|
|
|
size_t bytes_read = fread(buffer->data(), 1, file_size, file);
|
2020-03-02 20:47:43 +00:00
|
|
|
fclose(file);
|
|
|
|
if (bytes_read != file_size) {
|
|
|
|
std::cerr << "Failed to read " << input_file << std::endl;
|
2020-03-10 22:54:12 +00:00
|
|
|
return false;
|
2020-03-02 20:47:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 22:54:12 +00:00
|
|
|
return true;
|
2020-03-02 20:47:43 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 15:31:08 +00:00
|
|
|
/// Writes the given `buffer` into the file named as `output_file` using the
|
|
|
|
/// given `mode`. If `output_file` is empty or "-", writes to standard
|
|
|
|
/// output. If any error occurs, returns false and outputs error message to
|
|
|
|
/// standard error. The ContainerT type must have data() and size() methods,
|
|
|
|
/// like `std::string` and `std::vector` do.
|
2020-04-30 01:16:12 +00:00
|
|
|
/// @returns true on success
|
|
|
|
template <typename ContainerT>
|
2020-05-01 16:17:03 +00:00
|
|
|
bool WriteFile(const std::string& output_file,
|
|
|
|
const std::string mode,
|
|
|
|
const ContainerT& buffer) {
|
2020-04-30 01:16:12 +00:00
|
|
|
const bool use_stdout = output_file.empty() || output_file == "-";
|
|
|
|
FILE* file = stdout;
|
|
|
|
|
|
|
|
if (!use_stdout) {
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
fopen_s(&file, output_file.c_str(), mode.c_str());
|
|
|
|
#else
|
|
|
|
file = fopen(output_file.c_str(), mode.c_str());
|
|
|
|
#endif
|
|
|
|
if (!file) {
|
|
|
|
std::cerr << "Could not open file " << output_file << " for writing"
|
|
|
|
<< std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-01 16:17:03 +00:00
|
|
|
size_t written =
|
|
|
|
fwrite(buffer.data(), sizeof(typename ContainerT::value_type),
|
|
|
|
buffer.size(), file);
|
2020-04-30 01:16:12 +00:00
|
|
|
if (buffer.size() != written) {
|
|
|
|
if (use_stdout) {
|
|
|
|
std::cerr << "Could not write all output to standard output" << std::endl;
|
|
|
|
} else {
|
|
|
|
std::cerr << "Could not write to file " << output_file << std::endl;
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!use_stdout) {
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-18 14:08:48 +00:00
|
|
|
#if TINT_BUILD_SPV_WRITER
|
2020-03-02 20:47:43 +00:00
|
|
|
std::string Disassemble(const std::vector<uint32_t>& data) {
|
|
|
|
std::string spv_errors;
|
|
|
|
spv_target_env target_env = SPV_ENV_UNIVERSAL_1_0;
|
|
|
|
|
|
|
|
auto msg_consumer = [&spv_errors](spv_message_level_t level, const char*,
|
|
|
|
const spv_position_t& position,
|
|
|
|
const char* message) {
|
|
|
|
switch (level) {
|
|
|
|
case SPV_MSG_FATAL:
|
|
|
|
case SPV_MSG_INTERNAL_ERROR:
|
|
|
|
case SPV_MSG_ERROR:
|
|
|
|
spv_errors += "error: line " + std::to_string(position.index) + ": " +
|
|
|
|
message + "\n";
|
|
|
|
break;
|
|
|
|
case SPV_MSG_WARNING:
|
|
|
|
spv_errors += "warning: line " + std::to_string(position.index) + ": " +
|
|
|
|
message + "\n";
|
|
|
|
break;
|
|
|
|
case SPV_MSG_INFO:
|
|
|
|
spv_errors += "info: line " + std::to_string(position.index) + ": " +
|
|
|
|
message + "\n";
|
|
|
|
break;
|
|
|
|
case SPV_MSG_DEBUG:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
spvtools::SpirvTools tools(target_env);
|
|
|
|
tools.SetMessageConsumer(msg_consumer);
|
|
|
|
|
|
|
|
std::string result;
|
2020-04-01 23:40:53 +00:00
|
|
|
if (!tools.Disassemble(data, &result,
|
|
|
|
SPV_BINARY_TO_TEXT_OPTION_INDENT |
|
|
|
|
SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES)) {
|
|
|
|
std::cerr << spv_errors << std::endl;
|
|
|
|
}
|
2020-03-02 20:47:43 +00:00
|
|
|
return result;
|
|
|
|
}
|
2020-03-18 14:08:48 +00:00
|
|
|
#endif // TINT_BUILD_SPV_WRITER
|
2020-03-02 20:47:43 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
int main(int argc, const char** argv) {
|
|
|
|
std::vector<std::string> args(argv, argv + argc);
|
|
|
|
Options options;
|
|
|
|
|
|
|
|
if (!ParseArgs(args, &options)) {
|
|
|
|
std::cerr << "Failed to parse arguments." << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.show_help) {
|
|
|
|
std::cout << kUsage << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
2020-04-30 01:16:12 +00:00
|
|
|
|
|
|
|
// Implement output format defaults.
|
|
|
|
if (options.format == Format::kNone) {
|
|
|
|
// Try inferring from filename.
|
|
|
|
options.format = infer_format(options.output_file);
|
|
|
|
}
|
|
|
|
if (options.format == Format::kNone) {
|
|
|
|
// Ultimately, default to SPIR-V assembly. That's nice for interactive use.
|
|
|
|
options.format = Format::kSpvAsm;
|
2020-03-02 20:47:43 +00:00
|
|
|
}
|
|
|
|
|
2020-11-26 16:50:02 +00:00
|
|
|
auto diag_printer = tint::diag::Printer::create(stderr, true);
|
|
|
|
tint::diag::Formatter diag_formatter;
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
std::unique_ptr<tint::reader::Reader> reader;
|
2020-10-30 20:44:53 +00:00
|
|
|
std::unique_ptr<tint::Source::File> source_file;
|
2020-03-18 14:08:48 +00:00
|
|
|
#if TINT_BUILD_WGSL_READER
|
2020-03-10 22:54:12 +00:00
|
|
|
if (options.input_filename.size() > 5 &&
|
|
|
|
options.input_filename.substr(options.input_filename.size() - 5) ==
|
|
|
|
".wgsl") {
|
|
|
|
std::vector<uint8_t> data;
|
|
|
|
if (!ReadFile<uint8_t>(options.input_filename, &data)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2020-10-30 20:44:53 +00:00
|
|
|
source_file = std::make_unique<tint::Source::File>(
|
|
|
|
options.input_filename, std::string(data.begin(), data.end()));
|
2020-12-02 21:17:58 +00:00
|
|
|
reader = std::make_unique<tint::reader::wgsl::Parser>(source_file.get());
|
2020-03-02 20:47:43 +00:00
|
|
|
}
|
2020-03-18 14:08:48 +00:00
|
|
|
#endif // TINT_BUILD_WGSL_READER
|
|
|
|
|
|
|
|
#if TINT_BUILD_SPV_READER
|
2020-06-22 14:36:34 +00:00
|
|
|
// Handle SPIR-V binary input, in files ending with .spv
|
2020-03-10 22:54:12 +00:00
|
|
|
if (options.input_filename.size() > 4 &&
|
|
|
|
options.input_filename.substr(options.input_filename.size() - 4) ==
|
|
|
|
".spv") {
|
|
|
|
std::vector<uint32_t> data;
|
|
|
|
if (!ReadFile<uint32_t>(options.input_filename, &data)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2020-12-02 21:17:58 +00:00
|
|
|
reader = std::make_unique<tint::reader::spirv::Parser>(data);
|
2020-03-10 22:54:12 +00:00
|
|
|
}
|
2020-06-22 14:36:34 +00:00
|
|
|
// Handle SPIR-V assembly input, in files ending with .spvasm
|
|
|
|
if (options.input_filename.size() > 7 &&
|
|
|
|
options.input_filename.substr(options.input_filename.size() - 7) ==
|
|
|
|
".spvasm") {
|
|
|
|
std::vector<char> text;
|
|
|
|
if (!ReadFile<char>(options.input_filename, &text)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2020-08-13 17:33:59 +00:00
|
|
|
// Use Vulkan 1.1, since this is what Tint, internally, is expecting.
|
|
|
|
spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_1);
|
2020-06-22 14:36:34 +00:00
|
|
|
tools.SetMessageConsumer([](spv_message_level_t, const char*,
|
|
|
|
const spv_position_t& pos, const char* msg) {
|
|
|
|
std::cerr << (pos.line + 1) << ":" << (pos.column + 1) << ": " << msg
|
|
|
|
<< std::endl;
|
|
|
|
});
|
|
|
|
std::vector<uint32_t> data;
|
|
|
|
if (!tools.Assemble(text.data(), text.size(), &data,
|
|
|
|
SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2020-12-02 21:17:58 +00:00
|
|
|
reader = std::make_unique<tint::reader::spirv::Parser>(data);
|
2020-06-22 14:36:34 +00:00
|
|
|
}
|
2020-03-18 14:08:48 +00:00
|
|
|
#endif // TINT_BUILD_SPV_READER
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
if (!reader) {
|
|
|
|
std::cerr << "Failed to create reader for input file: "
|
|
|
|
<< options.input_filename << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!reader->Parse()) {
|
2020-11-26 16:50:02 +00:00
|
|
|
diag_formatter.format(reader->diagnostics(), diag_printer.get());
|
2020-03-02 20:47:43 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2020-04-06 18:43:56 +00:00
|
|
|
auto mod = reader->module();
|
|
|
|
if (!mod.IsValid()) {
|
|
|
|
std::cerr << "Invalid module generated..." << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-12-02 21:17:58 +00:00
|
|
|
tint::TypeDeterminer td(&mod);
|
2020-04-20 14:20:01 +00:00
|
|
|
if (!td.Determine()) {
|
2020-04-21 13:05:42 +00:00
|
|
|
std::cerr << "Type Determination: " << td.error() << std::endl;
|
2020-03-02 20:47:43 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-11-16 14:46:27 +00:00
|
|
|
if (options.dump_ast) {
|
2021-01-05 22:45:50 +00:00
|
|
|
auto ast_str = mod.to_str();
|
|
|
|
if (options.demangle) {
|
|
|
|
ast_str = tint::Demangler().Demangle(mod, ast_str);
|
|
|
|
}
|
|
|
|
std::cout << std::endl << ast_str << std::endl;
|
2020-11-16 14:46:27 +00:00
|
|
|
}
|
|
|
|
if (options.parse_only) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
tint::Validator v;
|
2020-07-30 02:27:03 +00:00
|
|
|
if (!v.Validate(&mod)) {
|
2020-11-26 16:50:02 +00:00
|
|
|
diag_formatter.format(v.diagnostics(), diag_printer.get());
|
2020-09-30 00:05:23 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-12-02 21:17:58 +00:00
|
|
|
tint::transform::Manager transform_manager;
|
2020-09-30 00:05:23 +00:00
|
|
|
for (const auto& name : options.transforms) {
|
|
|
|
// TODO(dsinclair): The vertex pulling transform requires setup code to
|
|
|
|
// be run that needs user input. Should we find a way to support that here
|
|
|
|
// maybe through a provided file?
|
|
|
|
|
|
|
|
if (name == "bound_array_accessors") {
|
|
|
|
transform_manager.append(
|
2020-12-04 09:06:09 +00:00
|
|
|
std::make_unique<tint::transform::BoundArrayAccessors>());
|
2020-12-03 14:54:09 +00:00
|
|
|
} else if (name == "emit_vertex_point_size") {
|
|
|
|
transform_manager.append(
|
2020-12-04 09:06:09 +00:00
|
|
|
std::make_unique<tint::transform::EmitVertexPointSize>());
|
2020-12-08 21:07:24 +00:00
|
|
|
} else if (name == "first_index_offset") {
|
|
|
|
transform_manager.append(
|
|
|
|
std::make_unique<tint::transform::FirstIndexOffset>(0, 0));
|
2020-09-30 00:05:23 +00:00
|
|
|
} else {
|
|
|
|
std::cerr << "Unknown transform name: " << name << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2020-12-04 09:06:09 +00:00
|
|
|
|
|
|
|
auto out = transform_manager.Run(&mod);
|
|
|
|
if (out.diagnostics.contains_errors()) {
|
|
|
|
diag_formatter.format(out.diagnostics, diag_printer.get());
|
2020-03-02 20:47:43 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-12-04 09:06:09 +00:00
|
|
|
mod = std::move(out.module);
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
std::unique_ptr<tint::writer::Writer> writer;
|
2020-03-18 14:08:48 +00:00
|
|
|
|
|
|
|
#if TINT_BUILD_SPV_WRITER
|
2020-03-02 20:47:43 +00:00
|
|
|
if (options.format == Format::kSpirv || options.format == Format::kSpvAsm) {
|
2021-01-11 16:24:32 +00:00
|
|
|
writer = std::make_unique<tint::writer::spirv::Generator>(&mod);
|
2020-03-18 14:08:48 +00:00
|
|
|
}
|
|
|
|
#endif // TINT_BUILD_SPV_WRITER
|
|
|
|
|
|
|
|
#if TINT_BUILD_WGSL_WRITER
|
|
|
|
if (options.format == Format::kWgsl) {
|
2020-12-02 21:17:58 +00:00
|
|
|
writer = std::make_unique<tint::writer::wgsl::Generator>(std::move(mod));
|
2020-03-18 14:08:48 +00:00
|
|
|
}
|
|
|
|
#endif // TINT_BUILD_WGSL_WRITER
|
|
|
|
|
2020-06-23 17:48:40 +00:00
|
|
|
#if TINT_BUILD_MSL_WRITER
|
|
|
|
if (options.format == Format::kMsl) {
|
2021-01-11 16:24:32 +00:00
|
|
|
writer = std::make_unique<tint::writer::msl::Generator>(&mod);
|
2020-06-23 17:48:40 +00:00
|
|
|
}
|
2020-07-20 22:13:37 +00:00
|
|
|
#endif // TINT_BUILD_MSL_WRITER
|
|
|
|
|
|
|
|
#if TINT_BUILD_HLSL_WRITER
|
|
|
|
if (options.format == Format::kHlsl) {
|
2021-01-11 16:24:32 +00:00
|
|
|
writer = std::make_unique<tint::writer::hlsl::Generator>(&mod);
|
2020-07-20 22:13:37 +00:00
|
|
|
}
|
|
|
|
#endif // TINT_BUILD_HLSL_WRITER
|
2020-06-23 17:48:40 +00:00
|
|
|
|
2020-03-18 14:08:48 +00:00
|
|
|
if (!writer) {
|
2020-03-02 20:47:43 +00:00
|
|
|
std::cerr << "Unknown output format specified" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-09-03 01:46:36 +00:00
|
|
|
if (options.emit_single_entry_point) {
|
|
|
|
if (!writer->GenerateEntryPoint(options.stage, options.ep_name)) {
|
|
|
|
std::cerr << "Failed to generate: " << writer->error() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!writer->Generate()) {
|
|
|
|
std::cerr << "Failed to generate: " << writer->error() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2020-03-02 20:47:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 14:08:48 +00:00
|
|
|
#if TINT_BUILD_SPV_WRITER
|
2020-10-08 20:22:35 +00:00
|
|
|
bool dawn_validation_failed = false;
|
2020-10-14 15:16:41 +00:00
|
|
|
std::ostringstream stream;
|
|
|
|
|
2020-10-08 20:22:35 +00:00
|
|
|
if (options.dawn_validation) {
|
|
|
|
// Use Vulkan 1.1, since this is what Tint, internally, uses.
|
|
|
|
spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_1);
|
2020-10-14 15:16:41 +00:00
|
|
|
tools.SetMessageConsumer([&stream](spv_message_level_t, const char*,
|
|
|
|
const spv_position_t& pos,
|
|
|
|
const char* msg) {
|
|
|
|
stream << (pos.line + 1) << ":" << (pos.column + 1) << ": " << msg
|
|
|
|
<< std::endl;
|
2020-10-08 20:22:35 +00:00
|
|
|
});
|
|
|
|
auto* w = static_cast<tint::writer::spirv::Generator*>(writer.get());
|
|
|
|
if (!tools.Validate(w->result().data(), w->result().size(),
|
|
|
|
spvtools::ValidatorOptions())) {
|
|
|
|
dawn_validation_failed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
if (options.format == Format::kSpvAsm) {
|
2020-04-15 20:47:55 +00:00
|
|
|
auto* w = static_cast<tint::writer::spirv::Generator*>(writer.get());
|
2020-03-02 20:47:43 +00:00
|
|
|
auto str = Disassemble(w->result());
|
2020-04-30 01:16:12 +00:00
|
|
|
if (!WriteFile(options.output_file, "w", str)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2020-03-18 14:08:48 +00:00
|
|
|
}
|
|
|
|
if (options.format == Format::kSpirv) {
|
2020-05-06 21:49:55 +00:00
|
|
|
auto* w = static_cast<tint::writer::spirv::Generator*>(writer.get());
|
2020-04-30 01:16:12 +00:00
|
|
|
if (!WriteFile(options.output_file, "wb", w->result())) {
|
|
|
|
return 1;
|
|
|
|
}
|
2020-03-18 14:08:48 +00:00
|
|
|
}
|
2020-10-08 20:22:35 +00:00
|
|
|
if (dawn_validation_failed) {
|
2020-10-14 15:16:41 +00:00
|
|
|
std::cerr << std::endl << std::endl << "Validation Failure:" << std::endl;
|
|
|
|
std::cerr << stream.str();
|
2020-10-08 20:22:35 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2020-03-18 14:08:48 +00:00
|
|
|
#endif // TINT_BUILD_SPV_WRITER
|
|
|
|
|
2020-08-12 17:49:09 +00:00
|
|
|
if (options.format != Format::kSpvAsm && options.format != Format::kSpirv) {
|
2020-06-23 17:48:40 +00:00
|
|
|
auto* w = static_cast<tint::writer::Text*>(writer.get());
|
2021-01-05 22:45:50 +00:00
|
|
|
auto output = w->result();
|
|
|
|
if (options.demangle) {
|
|
|
|
output = tint::Demangler().Demangle(mod, output);
|
|
|
|
}
|
|
|
|
if (!WriteFile(options.output_file, "w", output)) {
|
2020-04-30 01:16:12 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2020-03-02 20:47:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|