From 7b0c5e880e6c5e673def9865462d5bf88b71ead8 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Fri, 21 May 2021 20:49:33 +0000 Subject: [PATCH] samples/main: Print the WGSL on error A diagnostic is not very helpful without the surrounding code. This is especially useful when debugging issues with the SPIR-V reader and post-transform. Change-Id: Idfeeaebf0cf0be98093838f8c8ad8bca4866b5b0 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/51922 Commit-Queue: Ben Clayton Reviewed-by: Antonio Maiorano --- samples/main.cc | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/samples/main.cc b/samples/main.cc index 44a98984e1..aa91c8ddcc 100644 --- a/samples/main.cc +++ b/samples/main.cc @@ -552,6 +552,18 @@ std::string Disassemble(const std::vector& data) { } #endif // TINT_BUILD_SPV_WRITER +/// PrintWGSL writes the WGSL of the program to the provided ostream, if the +/// WGSL writer is enabled, otherwise it does nothing. +/// @param out the output stream to write the WGSL to +/// @param program the program +void PrintWGSL(std::ostream& out, const tint::Program& program) { +#if TINT_BUILD_WGSL_WRITER + tint::writer::wgsl::Generator writer(&program); + writer.Generate(); + out << std::endl << writer.result() << std::endl; +#endif +} + } // namespace int main(int argc, const char** argv) { @@ -717,6 +729,7 @@ int main(int argc, const char** argv) { auto out = transform_manager.Run(program.get(), std::move(transform_inputs)); if (!out.program.IsValid()) { + PrintWGSL(std::cerr, out.program); diag_formatter.format(out.program.Diagnostics(), diag_printer.get()); return 1; } @@ -789,6 +802,7 @@ int main(int argc, const char** argv) { } if (!writer->Generate()) { + PrintWGSL(std::cerr, out.program); std::cerr << "Failed to generate: " << writer->error() << std::endl; return 1; }