Cleanup: GPU: Preprocessor: Use report callback on processing functions

This commit is contained in:
Clément Foucault
2024-10-11 18:52:34 +02:00
parent b1dfe39de1
commit 1c0247c871
3 changed files with 22 additions and 34 deletions

View File

@@ -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();

View File

@@ -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<typename T, bool no_linting = false> class Preprocessor {
T &report_error;
template<bool no_linting = false> class Preprocessor {
struct SharedVar {
std::string type;
std::string name;
@@ -36,34 +34,29 @@ template<typename T, bool no_linting = false> 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<typename ReportErrorF>
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<typename ReportErrorF>
std::string remove_comments(const std::string &str, const ReportErrorF &report_error)
{
std::string out_str = str;
{
@@ -141,7 +134,8 @@ template<typename T, bool no_linting = false> 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<typename ReportErrorF>
void matrix_constructor_linting(std::string str, const ReportErrorF &report_error)
{
if constexpr (no_linting) {
return;
@@ -157,7 +151,8 @@ template<typename T, bool no_linting = false> class Preprocessor {
}
}
void array_constructor_linting(std::string str)
template<typename ReportErrorF>
void array_constructor_linting(std::string str, const ReportErrorF &report_error)
{
if constexpr (no_linting) {
return;
@@ -260,9 +255,6 @@ template<typename T, bool no_linting = false> class Preprocessor {
}
};
template<typename T> class PreprocessorPython : public Preprocessor<T, true> {
public:
PreprocessorPython(T &error_cb) : Preprocessor<T, true>(error_cb){};
};
using PreprocessorPython = Preprocessor<true>;
} // namespace blender::gpu::shader

View File

@@ -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)