add 'do-trace-shaders' setting and fix shaders recompiling

This commit is contained in:
MihailRis
2025-08-31 18:14:05 +03:00
parent 5e3373313b
commit a621b179d3
10 changed files with 84 additions and 26 deletions
+45 -8
View File
@@ -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;
}