summaryrefslogtreecommitdiff
path: root/src/gpu/effects/shader_composer.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/gpu/effects/shader_composer.cc')
-rw-r--r--src/gpu/effects/shader_composer.cc48
1 files changed, 43 insertions, 5 deletions
diff --git a/src/gpu/effects/shader_composer.cc b/src/gpu/effects/shader_composer.cc
index 3e08df9..8d66ad7 100644
--- a/src/gpu/effects/shader_composer.cc
+++ b/src/gpu/effects/shader_composer.cc
@@ -2,6 +2,7 @@
// It implements the ShaderComposer class.
#include "gpu/effects/shader_composer.h"
+#include <set>
#include <sstream>
ShaderComposer& ShaderComposer::Get() {
@@ -14,22 +15,59 @@ void ShaderComposer::RegisterSnippet(const std::string& name,
snippets_[name] = code;
}
+void ShaderComposer::ResolveRecursive(const std::string& source,
+ std::stringstream& ss,
+ std::set<std::string>& included) {
+ std::istringstream stream(source);
+ std::string line;
+ while (std::getline(stream, line)) {
+ // Check for #include "snippet_name"
+ if (line.compare(0, 9, "#include ") == 0) {
+ size_t start = line.find('"');
+ size_t end = line.find('"', start + 1);
+ if (start != std::string::npos && end != std::string::npos) {
+ std::string name = line.substr(start + 1, end - start - 1);
+ if (included.find(name) == included.end()) {
+ included.insert(name);
+ auto it = snippets_.find(name);
+ if (it != snippets_.end()) {
+ ss << "// --- Included: " << name << " ---\n";
+ ResolveRecursive(it->second, ss, included);
+ ss << "// --- End Include: " << name << " ---\n";
+ } else {
+ ss << "// ERROR: Snippet not found: " << name << "\n";
+ }
+ }
+ }
+ } else {
+ ss << line << "\n";
+ }
+ }
+}
+
std::string
ShaderComposer::Compose(const std::vector<std::string>& dependencies,
const std::string& main_code) {
std::stringstream ss;
ss << "// Generated by ShaderComposer\n\n";
+ std::set<std::string> included;
+
+ // Process explicit dependencies first
for (const auto& dep : dependencies) {
- auto it = snippets_.find(dep);
- if (it != snippets_.end()) {
- ss << "// --- Snippet: " << dep << " ---\n";
- ss << it->second << "\n";
+ if (included.find(dep) == included.end()) {
+ included.insert(dep);
+ auto it = snippets_.find(dep);
+ if (it != snippets_.end()) {
+ ss << "// --- Dependency: " << dep << " ---\n";
+ ResolveRecursive(it->second, ss, included);
+ ss << "\n";
+ }
}
}
ss << "// --- Main Code ---\n";
- ss << main_code;
+ ResolveRecursive(main_code, ss, included);
return ss.str();
}