diff --git a/source/blender/gpu/glsl_preprocess/glsl_preprocess.cc b/source/blender/gpu/glsl_preprocess/glsl_preprocess.cc index 316928bc8c6..95839fd53a0 100644 --- a/source/blender/gpu/glsl_preprocess/glsl_preprocess.cc +++ b/source/blender/gpu/glsl_preprocess/glsl_preprocess.cc @@ -38,8 +38,8 @@ int main(int argc, char **argv) exit(1); } - bool first_comment = true; - bool inside_comment = false; + std::stringstream buffer; + buffer << input_file.rdbuf(); int error = 0; size_t line_index = 0; @@ -60,29 +60,7 @@ int main(int argc, char **argv) blender::gpu::shader::Preprocessor processor(report_error); - std::string line; - while (std::getline(input_file, line)) { - line_index++; - - /* Remove license headers (first comment). */ - if (line.rfind("/*", 0) == 0 && first_comment) { - first_comment = false; - inside_comment = true; - } - - const bool skip_line = inside_comment; - - if (inside_comment && (line.find("*/") != std::string::npos)) { - inside_comment = false; - } - - if (skip_line) { - output_file << "\n"; - } - else { - processor << line << '\n'; - } - } + processor << buffer.str(); output_file << processor.str(); diff --git a/source/blender/gpu/glsl_preprocess/glsl_preprocess.hh b/source/blender/gpu/glsl_preprocess/glsl_preprocess.hh index be8d5159feb..10c1efef33c 100644 --- a/source/blender/gpu/glsl_preprocess/glsl_preprocess.hh +++ b/source/blender/gpu/glsl_preprocess/glsl_preprocess.hh @@ -40,6 +40,7 @@ template class Preprocessor { Preprocessor &operator<<(std::string str) { + str = remove_comments(str); threadgroup_variable_parsing(str); matrix_constructor_linting(str); array_constructor_linting(str); @@ -62,6 +63,54 @@ template class Preprocessor { } private: + std::string remove_comments(const std::string &str) + { + std::string out_str = str; + { + /* Multi-line comments. */ + size_t start, end = 0; + while ((start = out_str.find("/*", end)) != std::string::npos) { + end = out_str.find("*/", start + 2); + if (end == std::string::npos) { + break; + } + for (size_t i = start; i < end + 2; ++i) { + if (out_str[i] != '\n') { + out_str[i] = ' '; + } + } + } + + if (end == std::string::npos) { + /* TODO(fclem): Add line / char position to report. */ + report_error(str, std::smatch(), "Malformed multi-line comment."); + return out_str; + } + } + { + /* Single-line comments. */ + size_t start, end = 0; + while ((start = out_str.find("//", end)) != std::string::npos) { + end = out_str.find('\n', start + 2); + if (end == std::string::npos) { + break; + } + for (size_t i = start; i < end; ++i) { + out_str[i] = ' '; + } + } + + if (end == std::string::npos) { + /* TODO(fclem): Add line / char position to report. */ + report_error(str, std::smatch(), "Malformed single line comment, missing newline."); + return out_str; + } + } + /* Remove trailing whitespaces as they make the subsequent regex much slower. */ + std::regex regex("(\\ )*?\\n"); + return std::regex_replace(out_str, regex, "\n"); + } + std::string preprocessor_directive_mutation(const std::string &str) { /* Example: `#include "deps.glsl"` > `//include "deps.glsl"` */