refactor GLSLExtension
This commit is contained in:
parent
1feee3a809
commit
6043ae8331
@ -73,9 +73,10 @@ assetload::postfunc assetload::shader(
|
|||||||
std::string vertexSource = io::read_string(vertexFile);
|
std::string vertexSource = io::read_string(vertexFile);
|
||||||
std::string fragmentSource = io::read_string(fragmentFile);
|
std::string fragmentSource = io::read_string(fragmentFile);
|
||||||
|
|
||||||
vertexSource = Shader::preprocessor->process(vertexFile, vertexSource);
|
auto& preprocessor = *Shader::preprocessor;
|
||||||
fragmentSource =
|
|
||||||
Shader::preprocessor->process(fragmentFile, fragmentSource);
|
vertexSource = preprocessor.process(vertexFile, vertexSource).code;
|
||||||
|
fragmentSource = preprocessor.process(fragmentFile, fragmentSource).code;
|
||||||
|
|
||||||
return [=](auto assets) {
|
return [=](auto assets) {
|
||||||
assets->store(
|
assets->store(
|
||||||
|
|||||||
@ -9,7 +9,6 @@
|
|||||||
#include "typedefs.hpp"
|
#include "typedefs.hpp"
|
||||||
#include "util/stringutil.hpp"
|
#include "util/stringutil.hpp"
|
||||||
#include "coders/BasicParser.hpp"
|
#include "coders/BasicParser.hpp"
|
||||||
#include "graphics/core/PostEffect.hpp"
|
|
||||||
|
|
||||||
static debug::Logger logger("glsl-extension");
|
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");
|
io::path file = paths->find("shaders/lib/" + name + ".glsl");
|
||||||
std::string source = io::read_string(file);
|
std::string source = io::read_string(file);
|
||||||
addHeader(name, "");
|
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) {
|
void GLSLExtension::addHeader(const std::string& name, std::string source) {
|
||||||
@ -123,60 +124,72 @@ public:
|
|||||||
clikeComment = true;
|
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() {
|
bool processPreprocessorDirective() {
|
||||||
skip(1);
|
skip(1);
|
||||||
|
|
||||||
auto name = parseName();
|
auto name = parseName();
|
||||||
|
|
||||||
if (name == "version") {
|
if (name == "version") {
|
||||||
parsing_warning(filename, line, "removed #version directive");
|
return processVersionDirective();
|
||||||
source_line(ss, line);
|
|
||||||
skipLine();
|
|
||||||
return false;
|
|
||||||
} else if (name == "include") {
|
} else if (name == "include") {
|
||||||
skipWhitespace(false);
|
return processIncludeDirective();
|
||||||
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;
|
|
||||||
} else if (name == "param") {
|
} else if (name == "param") {
|
||||||
skipWhitespace(false);
|
return processParamDirective();
|
||||||
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 true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string process() {
|
GLSLExtension::ProcessingResult process() {
|
||||||
while (hasNext()) {
|
while (hasNext()) {
|
||||||
skipWhitespace(false);
|
skipWhitespace(false);
|
||||||
if (!hasNext()) {
|
if (!hasNext()) {
|
||||||
@ -188,7 +201,7 @@ public:
|
|||||||
skip(1);
|
skip(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ss.str();
|
return {ss.str(), std::move(params)};
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
GLSLExtension& glsl;
|
GLSLExtension& glsl;
|
||||||
@ -196,7 +209,7 @@ private:
|
|||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::string GLSLExtension::process(
|
GLSLExtension::ProcessingResult GLSLExtension::process(
|
||||||
const io::path& file, const std::string& source, bool header
|
const io::path& file, const std::string& source, bool header
|
||||||
) {
|
) {
|
||||||
std::string filename = file.string();
|
std::string filename = file.string();
|
||||||
|
|||||||
@ -5,6 +5,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "io/io.hpp"
|
#include "io/io.hpp"
|
||||||
|
#include "graphics/core/PostEffect.hpp"
|
||||||
|
|
||||||
class ResPaths;
|
class ResPaths;
|
||||||
|
|
||||||
@ -25,7 +26,12 @@ public:
|
|||||||
bool hasDefine(const std::string& name) const;
|
bool hasDefine(const std::string& name) const;
|
||||||
void loadHeader(const std::string& name);
|
void loadHeader(const std::string& name);
|
||||||
|
|
||||||
std::string process(
|
struct ProcessingResult {
|
||||||
|
std::string code;
|
||||||
|
std::unordered_map<std::string, PostEffect::Param> params;
|
||||||
|
};
|
||||||
|
|
||||||
|
ProcessingResult process(
|
||||||
const io::path& file,
|
const io::path& file,
|
||||||
const std::string& source,
|
const std::string& source,
|
||||||
bool header = false
|
bool header = false
|
||||||
|
|||||||
@ -24,7 +24,7 @@ TEST(GLSLExtension, processing) {
|
|||||||
" return mix(color, 1.0 - color, p_intensity);\n"
|
" return mix(color, 1.0 - color, p_intensity);\n"
|
||||||
"}\n",
|
"}\n",
|
||||||
false);
|
false);
|
||||||
std::cout << processed << std::endl;
|
std::cout << processed.code << std::endl;
|
||||||
} catch (const parsing_error& err) {
|
} catch (const parsing_error& err) {
|
||||||
std::cerr << err.errorLog() << std::endl;
|
std::cerr << err.errorLog() << std::endl;
|
||||||
throw;
|
throw;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user