From cf480353787066a5cb327e73d33d4b31655b0525 Mon Sep 17 00:00:00 2001 From: Brett Date: Wed, 8 Feb 2023 22:30:20 -0500 Subject: [PATCH] shader processing --- .../CMakeFiles/clion-Release-log.txt | 2 +- cmake-build-release/build.ninja | 9 +- include/blt/std/loader.h | 50 +++++++++++ include/blt/std/logging.h | 12 +-- src/blt/std/loader.cpp | 83 +++++++++++++++++++ src/blt/std/logging.cpp | 9 +- 6 files changed, 153 insertions(+), 12 deletions(-) create mode 100644 include/blt/std/loader.h create mode 100644 src/blt/std/loader.cpp diff --git a/cmake-build-release/CMakeFiles/clion-Release-log.txt b/cmake-build-release/CMakeFiles/clion-Release-log.txt index 59fe53c..5069349 100644 --- a/cmake-build-release/CMakeFiles/clion-Release-log.txt +++ b/cmake-build-release/CMakeFiles/clion-Release-log.txt @@ -1,5 +1,5 @@ /opt/netbrains/clion/clion-2022.1.3/bin/cmake/linux/bin/cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_MAKE_PROGRAM=/opt/netbrains/clion/clion-2022.1.3/bin/ninja/linux/ninja -DBUILD_TESTS:BOOL=ON -G Ninja -S /home/brett/Documents/code/c++/BLT -B /home/brett/Documents/code/c++/BLT/cmake-build-release -Standard Files /home/brett/Documents/code/c++/BLT/src/blt/std/filesystem.cpp;/home/brett/Documents/code/c++/BLT/src/blt/std/format.cpp;/home/brett/Documents/code/c++/BLT/src/blt/std/logging.cpp;/home/brett/Documents/code/c++/BLT/src/blt/std/system.cpp +Standard Files /home/brett/Documents/code/c++/BLT/src/blt/std/filesystem.cpp;/home/brett/Documents/code/c++/BLT/src/blt/std/format.cpp;/home/brett/Documents/code/c++/BLT/src/blt/std/loader.cpp;/home/brett/Documents/code/c++/BLT/src/blt/std/logging.cpp;/home/brett/Documents/code/c++/BLT/src/blt/std/system.cpp Profiler Files /home/brett/Documents/code/c++/BLT/src/blt/profiling/profiler.cpp Source: /home/brett/Documents/code/c++/BLT Current Source: /home/brett/Documents/code/c++/BLT diff --git a/cmake-build-release/build.ninja b/cmake-build-release/build.ninja index a61a8a9..826259d 100644 --- a/cmake-build-release/build.ninja +++ b/cmake-build-release/build.ninja @@ -63,6 +63,13 @@ build CMakeFiles/BLT.dir/src/blt/std/format.cpp.o: CXX_COMPILER__BLT_Release /ho OBJECT_DIR = CMakeFiles/BLT.dir OBJECT_FILE_DIR = CMakeFiles/BLT.dir/src/blt/std +build CMakeFiles/BLT.dir/src/blt/std/loader.cpp.o: CXX_COMPILER__BLT_Release /home/brett/Documents/code/c++/BLT/src/blt/std/loader.cpp || cmake_object_order_depends_target_BLT + DEP_FILE = CMakeFiles/BLT.dir/src/blt/std/loader.cpp.o.d + FLAGS = -O3 -DNDEBUG -fdiagnostics-color=always + INCLUDES = -I/home/brett/Documents/code/c++/BLT/include -I/home/brett/Documents/code/c++/BLT/cmake-build-release/config + OBJECT_DIR = CMakeFiles/BLT.dir + OBJECT_FILE_DIR = CMakeFiles/BLT.dir/src/blt/std + build CMakeFiles/BLT.dir/src/blt/std/logging.cpp.o: CXX_COMPILER__BLT_Release /home/brett/Documents/code/c++/BLT/src/blt/std/logging.cpp || cmake_object_order_depends_target_BLT DEP_FILE = CMakeFiles/BLT.dir/src/blt/std/logging.cpp.o.d FLAGS = -O3 -DNDEBUG -fdiagnostics-color=always @@ -106,7 +113,7 @@ build CMakeFiles/BLT.dir/src/blt/nbt/nbt_block.cpp.o: CXX_COMPILER__BLT_Release ############################################# # Link the static library libBLT.a -build libBLT.a: CXX_STATIC_LIBRARY_LINKER__BLT_Release CMakeFiles/BLT.dir/src/blt/std/filesystem.cpp.o CMakeFiles/BLT.dir/src/blt/std/format.cpp.o CMakeFiles/BLT.dir/src/blt/std/logging.cpp.o CMakeFiles/BLT.dir/src/blt/std/system.cpp.o CMakeFiles/BLT.dir/src/blt/profiling/profiler.cpp.o CMakeFiles/BLT.dir/src/blt/nbt/nbt.cpp.o CMakeFiles/BLT.dir/src/blt/nbt/nbt_block.cpp.o +build libBLT.a: CXX_STATIC_LIBRARY_LINKER__BLT_Release CMakeFiles/BLT.dir/src/blt/std/filesystem.cpp.o CMakeFiles/BLT.dir/src/blt/std/format.cpp.o CMakeFiles/BLT.dir/src/blt/std/loader.cpp.o CMakeFiles/BLT.dir/src/blt/std/logging.cpp.o CMakeFiles/BLT.dir/src/blt/std/system.cpp.o CMakeFiles/BLT.dir/src/blt/profiling/profiler.cpp.o CMakeFiles/BLT.dir/src/blt/nbt/nbt.cpp.o CMakeFiles/BLT.dir/src/blt/nbt/nbt_block.cpp.o LANGUAGE_COMPILE_FLAGS = -O3 -DNDEBUG OBJECT_DIR = CMakeFiles/BLT.dir POST_BUILD = : diff --git a/include/blt/std/loader.h b/include/blt/std/loader.h new file mode 100644 index 0000000..d8c13a5 --- /dev/null +++ b/include/blt/std/loader.h @@ -0,0 +1,50 @@ +/* + * Created by Brett on 08/02/23. + * Licensed under GNU General Public License V3.0 + * See LICENSE file for license detail + */ + +#ifndef BLT_TESTS_LOADER_H +#define BLT_TESTS_LOADER_H + +#include +#include +#include +#include +#include +#include +#include + +namespace blt::fs { + std::vector getLinesFromFile(const std::string& path); + + std::vector recursiveShaderInclude(const std::string& path); + + std::string loadShaderFile(const std::string& path) { + std::stringstream stringStream; + + auto lines = recursiveShaderInclude(path); + + for (const auto& line : lines) { + // now process the defines, if they exist +// if (line.starts_with("#define")) { +// auto defineParts = String::split(line, " "); +// // create a new define statement in the defines place but with the respective value. +// if (defines.contains(defineParts[1])) { +// stringStream << "#define "; +// stringStream << defineParts[1] << " "; +// stringStream << defines[defineParts[1]]; +// stringStream << "\n"; +// continue; +// } +// } + stringStream << line; + stringStream << "\n"; + } + + //tlog << stringStream.str(); + return stringStream.str(); + } +} + +#endif //BLT_TESTS_LOADER_H diff --git a/include/blt/std/logging.h b/include/blt/std/logging.h index 42763b3..2a4b7c2 100644 --- a/include/blt/std/logging.h +++ b/include/blt/std/logging.h @@ -116,12 +116,12 @@ namespace blt::logging { #define BLT_ERROR(format, ...) #define BLT_FATAL(format, ...) #else - #define BLT_TRACE(format, ...) log_internal(format, blt::logging::TRACE, __FILE__, __LINE__, true, ##__VA_ARGS__); - #define BLT_DEBUG(format, ...) log_internal(format, blt::logging::DEBUG, __FILE__, __LINE__, true, ##__VA_ARGS__); - #define BLT_INFO(format, ...) log_internal(format, blt::logging::INFO, __FILE__, __LINE__, true, ##__VA_ARGS__); - #define BLT_WARN(format, ...) log_internal(format, blt::logging::WARN, __FILE__, __LINE__, true, ##__VA_ARGS__); - #define BLT_ERROR(format, ...) log_internal(format, blt::logging::ERROR, __FILE__, __LINE__, true, ##__VA_ARGS__); - #define BLT_FATAL(format, ...) log_internal(format, blt::logging::FATAL, __FILE__, __LINE__, true, ##__VA_ARGS__); + #define BLT_TRACE(format, ...) log_internal(format, blt::logging::TRACE, __FILE__, __LINE__, true, ##__VA_ARGS__) + #define BLT_DEBUG(format, ...) log_internal(format, blt::logging::DEBUG, __FILE__, __LINE__, true, ##__VA_ARGS__) + #define BLT_INFO(format, ...) log_internal(format, blt::logging::INFO, __FILE__, __LINE__, true, ##__VA_ARGS__) + #define BLT_WARN(format, ...) log_internal(format, blt::logging::WARN, __FILE__, __LINE__, true, ##__VA_ARGS__) + #define BLT_ERROR(format, ...) log_internal(format, blt::logging::ERROR, __FILE__, __LINE__, true, ##__VA_ARGS__) + #define BLT_FATAL(format, ...) log_internal(format, blt::logging::FATAL, __FILE__, __LINE__, true, ##__VA_ARGS__) #endif #endif //BLT_LOGGING_H diff --git a/src/blt/std/loader.cpp b/src/blt/std/loader.cpp new file mode 100644 index 0000000..03745bd --- /dev/null +++ b/src/blt/std/loader.cpp @@ -0,0 +1,83 @@ +/* + * Created by Brett on 08/02/23. + * Licensed under GNU General Public License V3.0 + * See LICENSE file for license detail + */ +#include + +std::vector blt::fs::getLinesFromFile(const std::string& path) { + std::string shaderSource; + std::ifstream shaderFile; + if (!shaderFile.good()) + BLT_ERROR("Input stream not good!\n"); + // ensure ifstream objects can throw exceptions: + shaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); + try { + // open file + shaderFile.open(path); + std::stringstream shaderStream; + // read file's buffer contents into streams + shaderStream << shaderFile.rdbuf(); + // close file handlers + shaderFile.close(); + // convert stream into std::string + shaderSource = shaderStream.str(); + } catch (std::ifstream::failure& e) { + BLT_FATAL("Unable to read file '%s'!\n", path.c_str()); + } + + // split the shader into the lines, this way we can get out the #include statements. + return string::split(shaderSource, "\n"); +} + +std::vector blt::fs::recursiveShaderInclude(const std::string& path) { + std::string pathOnly = path.substr(0, path.find_last_of('/')); + + auto mainLines = getLinesFromFile(path); + std::unordered_map> includes; + + for (int i = 0; i < mainLines.size(); i++) { + auto& line = mainLines[i]; + // if the line is an include statement then we want to add lines recursively. + if (string::starts_with(line, "#include")) { + std::vector include_statement = string::split(line, "<"); + + if (include_statement.size() <= 1) + include_statement = string::split(line, "\""); + + string::trim(line); + if (!(string::ends_with(line, ">") || string::ends_with(line, "\""))) { + BLT_FATAL("Shader file contains an invalid #include statement. (Missing terminator)\n"); + std::abort(); + } + try { + // filter out the > or " at the end of the include statement. + std::string file; + file = include_statement[1]; + if (string::ends_with(include_statement[1], ">")) + file = file.substr(0, file.size() - 1); + + BLT_TRACE("Recusing into %s/%s\n", pathOnly.c_str(), file.c_str()); + + includes.insert({i, recursiveShaderInclude((pathOnly + "/" + file))}); + } catch (std::exception& e) { + BLT_FATAL("Shader file contains an invalid #include statement. (Missing < or \")\n"); + std::abort(); + } + } + } + + std::vector returnLines; + + // now combine all the loaded files while respecting the include's position in the file. + for (int i = 0; i < mainLines.size(); i++) { + if (!includes[i].empty()) { + auto includedFileLines = includes[i]; + + for (const auto& line : includedFileLines) + returnLines.push_back(line); + } else + returnLines.push_back(mainLines[i]); + } + return returnLines; +} diff --git a/src/blt/std/logging.cpp b/src/blt/std/logging.cpp index 8916d40..cba84d8 100644 --- a/src/blt/std/logging.cpp +++ b/src/blt/std/logging.cpp @@ -122,14 +122,15 @@ namespace blt::logging { if (level < BLT_LOGGING_PROPERTIES.minLevel) return; std::string outputString = System::getTimeStringLog(); - bool includeExtras = BLT_LOGGING_PROPERTIES.m_logWithData && currentLine >= 0; - if (includeExtras) { - outputString += "["; + + if (BLT_LOGGING_PROPERTIES.m_logWithData && currentLine >= 0) { + outputString += '['; outputString += filename(file); - outputString += ":"; + outputString += ':'; outputString += std::to_string(currentLine); outputString += "] "; } + outputString += levelNames[level]; outputString += str;