add 'do-trace-shaders' setting and fix shaders recompiling
This commit is contained in:
@@ -22,6 +22,10 @@ void GLSLExtension::setPaths(const ResPaths* paths) {
|
||||
this->paths = paths;
|
||||
}
|
||||
|
||||
void GLSLExtension::setTraceOutput(bool enabled) {
|
||||
this->traceOutput = enabled;
|
||||
}
|
||||
|
||||
void GLSLExtension::loadHeader(const std::string& name) {
|
||||
if (paths == nullptr) {
|
||||
return;
|
||||
@@ -29,7 +33,7 @@ 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));
|
||||
addHeader(name, process(file, source, true, {}));
|
||||
}
|
||||
|
||||
void GLSLExtension::addHeader(const std::string& name, ProcessingResult header) {
|
||||
@@ -123,13 +127,22 @@ static Value default_value_for(Type type) {
|
||||
|
||||
class GLSLParser : public BasicParser<char> {
|
||||
public:
|
||||
GLSLParser(GLSLExtension& glsl, std::string_view file, std::string_view source, bool header)
|
||||
GLSLParser(
|
||||
GLSLExtension& glsl,
|
||||
std::string_view file,
|
||||
std::string_view source,
|
||||
bool header,
|
||||
const std::vector<std::string>& defines
|
||||
)
|
||||
: BasicParser(file, source), glsl(glsl) {
|
||||
if (!header) {
|
||||
ss << "#version " << GLSLExtension::VERSION << '\n';
|
||||
}
|
||||
for (auto& entry : glsl.getDefines()) {
|
||||
ss << "#define " << entry.first << " " << entry.second << '\n';
|
||||
for (auto& entry : defines) {
|
||||
ss << "#define " << entry << '\n';
|
||||
}
|
||||
for (auto& entry : defines) {
|
||||
ss << "#define " << entry << '\n';
|
||||
}
|
||||
}
|
||||
uint linenum = 1;
|
||||
source_line(ss, linenum);
|
||||
@@ -289,10 +302,34 @@ private:
|
||||
std::stringstream ss;
|
||||
};
|
||||
|
||||
static void trace_output(
|
||||
const io::path& file,
|
||||
const std::string& source,
|
||||
const GLSLExtension::ProcessingResult& result
|
||||
) {
|
||||
std::stringstream ss;
|
||||
ss << "export:trace/" << file.name();
|
||||
io::path outfile = ss.str();
|
||||
try {
|
||||
io::create_directories(outfile.parent());
|
||||
io::write_string(outfile, result.code);
|
||||
} catch (const std::runtime_error& err) {
|
||||
logger.error() << "error on saving GLSLExtension::preprocess output ("
|
||||
<< outfile.string() << "): " << err.what();
|
||||
}
|
||||
}
|
||||
|
||||
GLSLExtension::ProcessingResult GLSLExtension::process(
|
||||
const io::path& file, const std::string& source, bool header
|
||||
const io::path& file,
|
||||
const std::string& source,
|
||||
bool header,
|
||||
const std::vector<std::string>& defines
|
||||
) {
|
||||
std::string filename = file.string();
|
||||
GLSLParser parser(*this, filename, source, header);
|
||||
return parser.process();
|
||||
GLSLParser parser(*this, filename, source, header, defines);
|
||||
auto result = parser.process();
|
||||
if (traceOutput) {
|
||||
trace_output(file, source, result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "io/io.hpp"
|
||||
#include "data/setting.hpp"
|
||||
#include "graphics/core/PostEffect.hpp"
|
||||
|
||||
class ResPaths;
|
||||
@@ -19,6 +20,7 @@ public:
|
||||
};
|
||||
|
||||
void setPaths(const ResPaths* paths);
|
||||
void setTraceOutput(bool enabled);
|
||||
|
||||
void define(const std::string& name, std::string value);
|
||||
void undefine(const std::string& name);
|
||||
@@ -37,7 +39,8 @@ public:
|
||||
ProcessingResult process(
|
||||
const io::path& file,
|
||||
const std::string& source,
|
||||
bool header = false
|
||||
bool header,
|
||||
const std::vector<std::string>& defines
|
||||
);
|
||||
|
||||
static inline std::string VERSION = "330 core";
|
||||
@@ -46,4 +49,5 @@ private:
|
||||
std::unordered_map<std::string, std::string> defines;
|
||||
|
||||
const ResPaths* paths = nullptr;
|
||||
bool traceOutput = false;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user