forked from Dynamitos/Seele
48 lines
6.6 KiB
Markdown
48 lines
6.6 KiB
Markdown
# Materials & Shader Graphs (ShaderExpression)
|
|||
|
|
|
||
|
|
> The node-graph material system that emits Slang at runtime: Material, MaterialInstance, ShaderExpression, and the shader compiler entry.
|
||
|
|
|
||
|
|
This is a **navigation** doc: it tells you where things live, how data actually flows, and the sharp edges — so you can orient in minutes instead of hours. It complements the repository's [AGENTS.md](../../AGENTS.md) (map + rules) and the [cross-repo guide](../cross-repo.md) (engine ↔ demo contract). Everything below is grounded in the checked-out source at commit `d3ed573`.
|
||
|
|
|
||
|
|
## Key types & where they live
|
||
|
|
|
||
|
|
| Type | Defined in |
|
||
|
|
|------|------------|
|
||
|
|
| `Material` | `src/Engine/Material/Material.h` |
|
||
|
|
| `MaterialInstance` | `src/Engine/Material/MaterialInstance.h` |
|
||
|
|
| `ShaderExpression` | `src/Engine/Material/ShaderExpression.h` |
|
||
|
|
| `MaterialAsset` | `src/Engine/Asset/MaterialAsset.h` |
|
||
|
|
| `MaterialInstanceAsset` | `src/Engine/Asset/MaterialInstanceAsset.h` |
|
||
|
|
|
||
|
|
Paths above are resolved against the actual source tree (namespace ≈ directory in this codebase). A `(search)` marker means the file contains the name but may also define others — open it and grep. `git grep` is always a fallback. The full per-file map is in **Where to look** below.
|
||
|
|
|
||
|
|
## Data flow / lifecycle
|
||
|
|
|
||
|
|
Material (src/Engine/Material/Material.h + Material.cpp, 2.4KB header + 7.7KB impl) is the runtime material object (DEFINE_REF(Material) -> PMaterial). It owns a ShaderExpression (the compiled shader graph) + a Map of texture parameter bindings + scalar/vec/matrix parameter values. Material::update() (or equivalent) re-evaluates the ShaderExpression when parameters change and rebuilds the Slang source string. MaterialInstance (MaterialInstance.h + .cpp, 1.5KB + 2.6KB) is a per-entity or per-asset material variant that references a base Material (PMaterial) and overrides specific parameters (texture swaps, scalar overrides). MaterialInstanceAsset (Asset/MaterialInstanceAsset.h + .cpp, 791B + 1KB) is the loadable asset wrapper. MaterialAsset (Asset/MaterialAsset.h + .cpp, 805B + 1.2KB) is the loadable asset for the base material — it loads the material definition from disk (JSON or binary) and constructs the Material + ShaderExpression. ShaderExpression (src/Engine/Material/ShaderExpression.h + .cpp, 12.6KB header + 14KB impl) is the node-graph representation of a material shader: it is a directed acyclic graph of shader nodes (math ops, texture samples, parameter reads, branching, loops) that is compiled to Slang source code. The ShaderExpression::compile() (or equivalent) walks the node graph and emits Slang source, which is then handed to the ShaderCompiler (via Graphics::beginShaderCompilation) for GPU compilation. The node types are defined in ShaderExpression.h (12.6KB — large header with all node structs). The material system is the HIGHEST-LEVEL abstraction in the engine: game code creates MaterialAsset (loaded from disk) -> Material (runtime) -> MaterialInstance (per-entity override) -> ShaderExpression (node graph) -> Slang source -> ShaderCompiler -> GPU shader. The AssetViewer tool (src/AssetViewer/main.cpp, 7KB) is a standalone tool that can browse/inspect assets including materials.
|
||
|
|
|
||
|
|
## Where to look
|
||
|
|
|
||
|
|
| Path | What's there |
|
||
|
|
|------|--------------|
|
||
|
|
| `src/Engine/Material/Material.h + Material.cpp` | runtime material, 7.7KB impl |
|
||
|
|
| `src/Engine/Material/MaterialInstance.h + .cpp` | per-entity material override |
|
||
|
|
| `src/Engine/Material/ShaderExpression.h + .cpp` | node graph -> Slang source, 12.6KB + 14KB |
|
||
|
|
| `src/Engine/Asset/MaterialAsset.h + .cpp` | loadable material asset |
|
||
|
|
| `src/Engine/Asset/MaterialInstanceAsset.h + .cpp` | loadable material instance asset |
|
||
|
|
| `src/Editor/Asset/MaterialLoader.h + .cpp` | editor-side material loader, 9.5KB impl |
|
||
|
|
| `src/AssetViewer/main.cpp` | standalone asset browser |
|
||
|
|
|
||
|
|
## Gotchas & sharp edges
|
||
|
|
|
||
|
|
- ShaderExpression is the most complex single file in the material system (12.6KB header + 14KB impl). It defines ALL shader node types as C++ structs in the header. The node graph is a DAG — there is no cycle detection at compile time (a malformed graph can cause infinite recursion in the Slang emitter).
|
||
|
|
- The material system compiles Slang at RUNTIME — every material change triggers a full Slang compilation cycle. For a project with many materials, this can cause significant stutter on first use of each material. The PipelineCache mitigates this by caching compiled pipeline binaries, but the Slang compilation itself is not cached (only the GPU pipeline binaries are).
|
||
|
|
- MaterialInstance overrides are PARAMETER-LEVEL only — you cannot override the shader graph topology at the instance level. To use a different shader graph, you need a different Material (and therefore a different MaterialAsset).
|
||
|
|
- The material loader in the Editor (MaterialLoader.cpp, 9.5KB) reads material definitions from a JSON or binary format on disk. The format is NOT documented in a schema file in the repository — you must reverse-engineer it from MaterialLoader.cpp or the AssetViewer.
|
||
|
|
- MaterialAsset and MaterialInstanceAsset are separate asset types. A MaterialInstanceAsset references a MaterialAsset by name/ID. If the referenced MaterialAsset is missing or corrupted, the MaterialInstanceAsset will fail to load (check the error handling in MaterialInstanceAsset.cpp).
|
||
|
|
- The Slang compiler (slang-compile.cpp) is a standalone C++ file that links against the Slang compiler library (libslang). The Slang library is an external dependency (not in the Seele source tree). It must be present at build time. The slang-compile.cpp file (9KB) wraps the Slang C API to compile a Slang source string into SPIR-V (Vulkan target) or MetalSL (Metal target).
|
||
|
|
- Material::update() is called by the game code (or the MeshUpdater system) when material parameters change. If you modify a material parameter at runtime without calling update(), the GPU shader will not see the change. The ShaderExpression must be re-evaluated and the Slang re-compiled (or the parameter must be uploaded as a runtime uniform without recompilation — check Material.cpp for which approach is used).
|
||
|
|
- There is NO 'material shader cache' at the Slang level. The PipelineCache caches GPU pipeline binaries (VkPipeline / MTLRenderPipelineState), but if the Slang source changes (because a parameter was added/removed from the graph), the cache key changes and a full recompile occurs. Changing parameter VALUES (not the graph topology) should not trigger a recompile — check Material.cpp to confirm the parameter upload path bypasses Slang compilation.
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
*Generated from a full read of the checked-out source at commit `d3ed573`. If a path has moved, `git grep` for the type name — the engine is small enough to find anything in seconds.*
|