From bd92016319ad4dfba36930d3479b3599813b4d9c Mon Sep 17 00:00:00 2001 From: spencer-lunarg Date: Sun, 6 Oct 2024 17:25:15 -0400 Subject: [PATCH] Add stdin parsing of spirv --- main.cpp | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/main.cpp b/main.cpp index 3f041295..52e7548e 100644 --- a/main.cpp +++ b/main.cpp @@ -14,6 +14,7 @@ limitations under the License. */ +#include #if defined(WIN32) #define _CRTDBG_MAP_ALLOC #include @@ -108,25 +109,37 @@ int main(int argn, char** argv) { bool ci_mode = arg_parser.GetFlag("ci", "ci"); std::string input_spv_path; - if (!arg_parser.GetArg(0, &input_spv_path)) { - std::cerr << "ERROR: no SPIR-V file specified" << std::endl; - return EXIT_FAILURE; - } + std::vector spv_data; - std::ifstream spv_ifstream(input_spv_path.c_str(), std::ios::binary); - if (!spv_ifstream.is_open()) { - std::cerr << "ERROR: could not open '" << input_spv_path << "' for reading" << std::endl; - return EXIT_FAILURE; - } + // Get SPIR-V data/input + if (arg_parser.GetArg(0, &input_spv_path)) { + std::ifstream spv_ifstream(input_spv_path.c_str(), std::ios::binary); + if (!spv_ifstream.is_open()) { + std::cerr << "ERROR: could not open '" << input_spv_path << "' for reading" << std::endl; + return EXIT_FAILURE; + } - spv_ifstream.seekg(0, std::ios::end); - size_t size = static_cast(spv_ifstream.tellg()); - spv_ifstream.seekg(0, std::ios::beg); + spv_ifstream.seekg(0, std::ios::end); + size_t size = static_cast(spv_ifstream.tellg()); + spv_ifstream.seekg(0, std::ios::beg); + + spv_data.resize(size); + spv_ifstream.read((char*)spv_data.data(), size); + } else { + uint8_t buffer[4096]; + size_t bytes_read = fread(buffer, 1, sizeof(buffer), stdin); + if (bytes_read == 0) { + std::cerr << "ERROR: no SPIR-V file specified" << std::endl; + return EXIT_FAILURE; + } + spv_data.insert(spv_data.end(), buffer, buffer + bytes_read); + while ((bytes_read = fread(buffer, 1, sizeof(buffer), stdin)) > 0) { + spv_data.insert(spv_data.end(), buffer, buffer + bytes_read); + } + } + // run reflection with input { - std::vector spv_data(size); - spv_ifstream.read(spv_data.data(), size); - spv_reflect::ShaderModule reflection(spv_data.size(), spv_data.data()); if (reflection.GetResult() != SPV_REFLECT_RESULT_SUCCESS) { std::cerr << "ERROR: could not process '" << input_spv_path << "' (is it a valid SPIR-V bytecode?)" << std::endl;