GPU: GLSL: Strip comment at compile time

This speeds up the preprocessor and
will allow for faster load time.

# Conflicts:
#	source/blender/gpu/glsl_preprocess/glsl_preprocess.cc
This commit is contained in:
Clément Foucault
2024-10-11 11:15:29 +02:00
parent 317cf37680
commit 300ea9f083
2 changed files with 52 additions and 25 deletions

View File

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

View File

@@ -40,6 +40,7 @@ template<typename T, bool no_linting = false> 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<typename T, bool no_linting = false> 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"` */