summaryrefslogtreecommitdiff
path: root/src/gpu/effects/shader_composer.cc
blob: 3e08df97869868aaa13f32cd0be2e4a022611ecb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// This file is part of the 64k demo project.
// It implements the ShaderComposer class.

#include "gpu/effects/shader_composer.h"
#include <sstream>

ShaderComposer& ShaderComposer::Get() {
  static ShaderComposer instance;
  return instance;
}

void ShaderComposer::RegisterSnippet(const std::string& name,
                                     const std::string& code) {
  snippets_[name] = code;
}

std::string
ShaderComposer::Compose(const std::vector<std::string>& dependencies,
                        const std::string& main_code) {
  std::stringstream ss;
  ss << "// Generated by ShaderComposer\n\n";

  for (const auto& dep : dependencies) {
    auto it = snippets_.find(dep);
    if (it != snippets_.end()) {
      ss << "// --- Snippet: " << dep << " ---\n";
      ss << it->second << "\n";
    }
  }

  ss << "// --- Main Code ---\n";
  ss << main_code;

  return ss.str();
}