summaryrefslogtreecommitdiff
path: root/src/gpu/effects/shader_composer.h
blob: 961ffcbc5c94f671ecb8b79b0d1f3f0c64ad2c3e (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
36
37
38
39
40
// This file is part of the 64k demo project.
// It defines the ShaderComposer class for managing WGSL snippets.

#pragma once

#include "generated/assets.h" // For AssetId
#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 = {});
  
  std::string Compose(const std::vector<std::string>& dependencies,
                      AssetId main_code_asset_id,
                      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_;
};