diff --git a/source/blender/gpu/glsl_preprocess/glsl_preprocess.cc b/source/blender/gpu/glsl_preprocess/glsl_preprocess.cc index 95839fd53a0..539bb6e8636 100644 --- a/source/blender/gpu/glsl_preprocess/glsl_preprocess.cc +++ b/source/blender/gpu/glsl_preprocess/glsl_preprocess.cc @@ -58,11 +58,9 @@ int main(int argc, char **argv) error++; }; - blender::gpu::shader::Preprocessor processor(report_error); + blender::gpu::shader::Preprocessor processor; - processor << buffer.str(); - - output_file << processor.str(); + output_file << processor.process(buffer.str(), report_error); input_file.close(); output_file.close(); diff --git a/source/blender/gpu/glsl_preprocess/glsl_preprocess.hh b/source/blender/gpu/glsl_preprocess/glsl_preprocess.hh index 10c1efef33c..94dac8afe2a 100644 --- a/source/blender/gpu/glsl_preprocess/glsl_preprocess.hh +++ b/source/blender/gpu/glsl_preprocess/glsl_preprocess.hh @@ -23,9 +23,7 @@ namespace blender::gpu::shader { * Implementation speed is not a huge concern as we only apply this at compile time or on python * shaders source. */ -template class Preprocessor { - T &report_error; - +template class Preprocessor { struct SharedVar { std::string type; std::string name; @@ -36,34 +34,29 @@ template class Preprocessor { std::stringstream output_; public: - Preprocessor(T &error_cb) : report_error(error_cb) {} - - Preprocessor &operator<<(std::string str) + /* Takes a whole source file and output processed source. */ + template + std::string process(std::string str, const ReportErrorF &report_error) { - str = remove_comments(str); + str = remove_comments(str, report_error); threadgroup_variable_parsing(str); - matrix_constructor_linting(str); - array_constructor_linting(str); + matrix_constructor_linting(str, report_error); + array_constructor_linting(str, report_error); str = preprocessor_directive_mutation(str); str = argument_decorator_macro_injection(str); str = array_constructor_macro_injection(str); - output_ << str; - return *this; + return str + suffix(); } - Preprocessor &operator<<(char c) + std::string process(const std::string &str) { - output_ << c; - return *this; - } - - std::string str() - { - return output_.str() + suffix(); + auto no_err_report = [](std::string, std::smatch, const char *) {}; + return process(str, no_err_report); } private: - std::string remove_comments(const std::string &str) + template + std::string remove_comments(const std::string &str, const ReportErrorF &report_error) { std::string out_str = str; { @@ -141,7 +134,8 @@ template class Preprocessor { } /* TODO(fclem): Too many false positive and false negative to be applied to python shaders. */ - void matrix_constructor_linting(std::string str) + template + void matrix_constructor_linting(std::string str, const ReportErrorF &report_error) { if constexpr (no_linting) { return; @@ -157,7 +151,8 @@ template class Preprocessor { } } - void array_constructor_linting(std::string str) + template + void array_constructor_linting(std::string str, const ReportErrorF &report_error) { if constexpr (no_linting) { return; @@ -260,9 +255,6 @@ template class Preprocessor { } }; -template class PreprocessorPython : public Preprocessor { - public: - PreprocessorPython(T &error_cb) : Preprocessor(error_cb){}; -}; +using PreprocessorPython = Preprocessor; } // namespace blender::gpu::shader diff --git a/source/blender/gpu/intern/gpu_shader.cc b/source/blender/gpu/intern/gpu_shader.cc index 1884d822d9b..32e32de0dc3 100644 --- a/source/blender/gpu/intern/gpu_shader.cc +++ b/source/blender/gpu/intern/gpu_shader.cc @@ -303,10 +303,8 @@ GPUShader *GPU_shader_create_from_info(const GPUShaderCreateInfo *_info) static std::string preprocess_source(StringRefNull original) { - auto no_err_report = [](std::string, std::smatch, const char *) {}; - gpu::shader::PreprocessorPython processor(no_err_report); - processor << std::string(original); - return processor.str(); + gpu::shader::PreprocessorPython processor; + return processor.process(original); }; GPUShader *GPU_shader_create_from_info_python(const GPUShaderCreateInfo *_info)