summaryrefslogtreecommitdiff
path: root/src/gpu/effects/shader_composer.h
blob: 9eb43f40eebe42d7b42f032f1b14ea5f8e8f4ba1 (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 defines the ShaderComposer class for managing WGSL snippets.

#pragma once

#include <map>
#include <set>
#include <string>
#include <vector>

class ShaderComposer {
 public:
  static ShaderComposer& Get();

  // Register a snippet (e.g. "common_math", "sdf_primitives")
  void RegisterSnippet(const std::string& name, const std::string& code);

  using CompositionMap = std::map<std::string, std::string>;

  // Assemble a final shader string by prepending required snippets
  // and recursively resolving #include "snippet_name" directives.
  // Optional substitutions: map "placeholder_name" -> "actual_snippet_name"
  std::string Compose(const std::vector<std::string>& dependencies,
                      const std::string& main_code,
                      const CompositionMap& substitutions = {});

 private:
  ShaderComposer() = default;

  void ResolveRecursive(const std::string& source, std::stringstream& ss,
                        std::set<std::string>& included,
                        const CompositionMap& substitutions);

  std::map<std::string, std::string> snippets_;
};