From 6043ae8331f233974ebbd0d39bdcd551bb8287d1 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 3 Apr 2025 21:57:46 +0300 Subject: [PATCH] refactor GLSLExtension --- src/assets/assetload_funcs.cpp | 7 ++- src/coders/GLSLExtension.cpp | 105 ++++++++++++++++++--------------- src/coders/GLSLExtension.hpp | 8 ++- test/coders/GLSLExtension.cpp | 2 +- 4 files changed, 71 insertions(+), 51 deletions(-) diff --git a/src/assets/assetload_funcs.cpp b/src/assets/assetload_funcs.cpp index 01c251b4..7558bbdc 100644 --- a/src/assets/assetload_funcs.cpp +++ b/src/assets/assetload_funcs.cpp @@ -73,9 +73,10 @@ assetload::postfunc assetload::shader( std::string vertexSource = io::read_string(vertexFile); std::string fragmentSource = io::read_string(fragmentFile); - vertexSource = Shader::preprocessor->process(vertexFile, vertexSource); - fragmentSource = - Shader::preprocessor->process(fragmentFile, fragmentSource); + auto& preprocessor = *Shader::preprocessor; + + vertexSource = preprocessor.process(vertexFile, vertexSource).code; + fragmentSource = preprocessor.process(fragmentFile, fragmentSource).code; return [=](auto assets) { assets->store( diff --git a/src/coders/GLSLExtension.cpp b/src/coders/GLSLExtension.cpp index fcacc372..76f2394a 100644 --- a/src/coders/GLSLExtension.cpp +++ b/src/coders/GLSLExtension.cpp @@ -9,7 +9,6 @@ #include "typedefs.hpp" #include "util/stringutil.hpp" #include "coders/BasicParser.hpp" -#include "graphics/core/PostEffect.hpp" static debug::Logger logger("glsl-extension"); @@ -26,7 +25,9 @@ void GLSLExtension::loadHeader(const std::string& name) { io::path file = paths->find("shaders/lib/" + name + ".glsl"); std::string source = io::read_string(file); addHeader(name, ""); - addHeader(name, process(file, source, true)); + + auto [code, _] = process(file, source, true); + addHeader(name, std::move(code)); } void GLSLExtension::addHeader(const std::string& name, std::string source) { @@ -123,60 +124,72 @@ public: clikeComment = true; } + bool processIncludeDirective() { + skipWhitespace(false); + if (peekNoJump() != '<') { + throw error("'<' expected"); + } + skip(1); + skipWhitespace(false); + auto headerName = parseName(); + skipWhitespace(false); + if (peekNoJump() != '>') { + throw error("'>' expected"); + } + skip(1); + skipWhitespace(false); + skipLine(); + + if (!glsl.hasHeader(headerName)) { + glsl.loadHeader(headerName); + } + ss << glsl.getHeader(headerName) << '\n'; + source_line(ss, line); + return false; + } + + bool processVersionDirective() { + parsing_warning(filename, line, "removed #version directive"); + source_line(ss, line); + skipLine(); + return false; + } + + bool processParamDirective() { + skipWhitespace(false); + auto typeName = parseName(); + auto type = param_type_from(typeName); + if (!type.has_value()) { + throw error("unsupported param type " + util::quote(typeName)); + } + skipWhitespace(false); + auto paramName = parseName(); + if (params.find(paramName) != params.end()) { + throw error("duplicating param " + util::quote(paramName)); + } + skipLine(); + + ss << "uniform " << typeName << " " << paramName << ";\n"; + params[paramName] = PostEffect::Param(type.value()); + return false; + } + bool processPreprocessorDirective() { skip(1); auto name = parseName(); if (name == "version") { - parsing_warning(filename, line, "removed #version directive"); - source_line(ss, line); - skipLine(); - return false; + return processVersionDirective(); } else if (name == "include") { - skipWhitespace(false); - if (peekNoJump() != '<') { - throw error("'<' expected"); - } - skip(1); - skipWhitespace(false); - auto headerName = parseName(); - skipWhitespace(false); - if (peekNoJump() != '>') { - throw error("'>' expected"); - } - skip(1); - skipWhitespace(false); - skipLine(); - - if (!glsl.hasHeader(headerName)) { - glsl.loadHeader(headerName); - } - ss << glsl.getHeader(headerName) << '\n'; - source_line(ss, line); - return false; + return processIncludeDirective(); } else if (name == "param") { - skipWhitespace(false); - auto typeName = parseName(); - auto type = param_type_from(typeName); - if (!type.has_value()) { - throw error("unsupported param type " + util::quote(typeName)); - } - skipWhitespace(false); - auto paramName = parseName(); - if (params.find(paramName) != params.end()) { - throw error("duplicating param " + util::quote(paramName)); - } - skipLine(); - - ss << "uniform " << typeName << " " << paramName << ";\n"; - params[paramName] = PostEffect::Param(type.value()); - return false; + return processParamDirective(); } return true; } - std::string process() { + GLSLExtension::ProcessingResult process() { while (hasNext()) { skipWhitespace(false); if (!hasNext()) { @@ -188,7 +201,7 @@ public: skip(1); } } - return ss.str(); + return {ss.str(), std::move(params)}; } private: GLSLExtension& glsl; @@ -196,7 +209,7 @@ private: std::stringstream ss; }; -std::string GLSLExtension::process( +GLSLExtension::ProcessingResult GLSLExtension::process( const io::path& file, const std::string& source, bool header ) { std::string filename = file.string(); diff --git a/src/coders/GLSLExtension.hpp b/src/coders/GLSLExtension.hpp index 63e15303..e78f06d7 100644 --- a/src/coders/GLSLExtension.hpp +++ b/src/coders/GLSLExtension.hpp @@ -5,6 +5,7 @@ #include #include "io/io.hpp" +#include "graphics/core/PostEffect.hpp" class ResPaths; @@ -25,7 +26,12 @@ public: bool hasDefine(const std::string& name) const; void loadHeader(const std::string& name); - std::string process( + struct ProcessingResult { + std::string code; + std::unordered_map params; + }; + + ProcessingResult process( const io::path& file, const std::string& source, bool header = false diff --git a/test/coders/GLSLExtension.cpp b/test/coders/GLSLExtension.cpp index ff26ebdf..8ad71898 100644 --- a/test/coders/GLSLExtension.cpp +++ b/test/coders/GLSLExtension.cpp @@ -24,7 +24,7 @@ TEST(GLSLExtension, processing) { " return mix(color, 1.0 - color, p_intensity);\n" "}\n", false); - std::cout << processed << std::endl; + std::cout << processed.code << std::endl; } catch (const parsing_error& err) { std::cerr << err.errorLog() << std::endl; throw;