From 0c647a8896d127ba2526f18ed034f48bb35c878a Mon Sep 17 00:00:00 2001 From: David Neto Date: Mon, 22 Jun 2020 14:36:34 +0000 Subject: [PATCH] sample: handle SPIR-V assembly input Change-Id: Ic9f5263cdb398a2853dc49cfef3956cb9c8de0dc Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/23583 Reviewed-by: dan sinclair --- samples/main.cc | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/samples/main.cc b/samples/main.cc index d5570a9e5b..46a59a1336 100644 --- a/samples/main.cc +++ b/samples/main.cc @@ -19,6 +19,10 @@ #include #include +#if TINT_BUILD_SPV_READER +#include "spirv-tools/libspirv.hpp" +#endif // TINT_BUILD_SPV_READER + #include "tint/tint.h" namespace { @@ -42,7 +46,7 @@ struct Options { Format format = Format::kNone; }; -const char kUsage[] = R"(Usage: tint [options] SCRIPT [SCRIPTS...] +const char kUsage[] = R"(Usage: tint [options] options: --format -- Output format. @@ -328,6 +332,7 @@ int main(int argc, const char** argv) { #endif // TINT_BUILD_WGSL_READER #if TINT_BUILD_SPV_READER + // Handle SPIR-V binary input, in files ending with .spv if (options.input_filename.size() > 4 && options.input_filename.substr(options.input_filename.size() - 4) == ".spv") { @@ -337,6 +342,30 @@ int main(int argc, const char** argv) { } reader = std::make_unique(&ctx, data); } + // 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 text; + if (!ReadFile(options.input_filename, &text)) { + return 1; + } + // By default, use SPIR-V 1.3, the original proposal for SPIR-V binary + // input for WebGPU. This lines up with the SPIRV-Tools validation + // for the SPV_ENV_WEBGPU0 environment. + spvtools::SpirvTools tools(SPV_ENV_UNIVERSAL_1_3); + 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 data; + if (!tools.Assemble(text.data(), text.size(), &data, + SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS)) { + return 1; + } + reader = std::make_unique(&ctx, data); + } #endif // TINT_BUILD_SPV_READER if (!reader) {