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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
# Sequence: DAG-based Effect Routing
**Status:** Operational
Explicit node system with DAG effect routing.
## Quick Start
```bash
# Compile timeline
python3 tools/seq_compiler.py workspaces/main/timeline.seq --output src/generated/timeline.cc
```
## Timeline Syntax
```
# BPM 120 (optional, currently ignored)
SEQUENCE <start_time> <priority> ["name"]
# Node declarations (optional, auto-inferred as u8x4_norm)
NODE temp1 u8x4_norm
NODE depth depth24
NODE gbuf_normal f16x8
# Asset dependencies (validated at compile-time)
ASSET shader_blur
# Effect routing with priority modifier
EFFECT <+|=|-> EffectClass input1 input2 -> output1 output2 start end [params]
```
**Priority modifiers** (REQUIRED):
- `+` : Increment priority (foreground)
- `=` : Same priority as previous
- `-` : Decrement priority (background)
**Node types**: `u8x4_norm` (default), `f32x4`, `f16x8`, `depth24`, `compute_f32`
**Reserved nodes**: `source` (input), `sink` (output)
**Effect times**: Absolute seconds. `seq_compiler` adds the sequence start offset automatically.
### Examples
**Simple chain:**
```
SEQUENCE 0.0 0 "basic"
EFFECT + HeptagonEffect source -> temp1 0.0 10.0
EFFECT + GaussianBlur temp1 -> sink 0.0 10.0
```
**Multi-output (G-buffer):**
```
SEQUENCE 0.0 0 "deferred"
NODE gbuf_n f16x8
NODE gbuf_p f32x4
NODE depth depth24
EFFECT + DeferredRender source depth -> gbuf_n gbuf_p 0.0 10.0
EFFECT + Compose gbuf_n gbuf_p -> sink 0.0 10.0
```
**Ping-pong (auto-optimized):**
```
SEQUENCE 0.0 0 "blur"
# Compiler detects alternating pattern, aliases temp2 -> temp1
EFFECT + BlurH source -> temp1 0.0 10.0
EFFECT + BlurV temp1 -> temp2 0.0 10.0
EFFECT + Sharpen temp2 -> sink 0.0 10.0
```
## Architecture
### Sequence Class
```cpp
class Sequence {
NodeRegistry nodes_; // Typed texture management
std::vector<EffectDAGNode> effect_dag_; // Topologically sorted
UniformsSequenceParams params_; // Per-frame uniforms
virtual void preprocess(float time, float beat_time, float beat_phase, float audio_intensity);
virtual void render_effects(WGPUCommandEncoder encoder);
};
```
### Effect Class
```cpp
class Effect {
std::vector<std::string> input_nodes_;
std::vector<std::string> output_nodes_;
// Optional: declare internal nodes (depth buffers, intermediate textures).
virtual void declare_nodes(NodeRegistry& registry) {}
// Required: render this effect for the current frame.
virtual void render(WGPUCommandEncoder encoder,
const UniformsSequenceParams& params,
NodeRegistry& nodes) = 0;
// Optional: called after ALL effects in the sequence have rendered.
// Use for end-of-frame bookkeeping, e.g. copying temporal feedback buffers.
// Default implementation is a no-op.
virtual void post_render(WGPUCommandEncoder encoder, NodeRegistry& nodes) {}
};
```
### Frame execution order
Each frame, `Sequence::render_effects()` runs two passes over the DAG:
1. **Render pass** — `dispatch_render()` on every effect in topological order
2. **Post-render pass** — `post_render()` on every effect in the same order
This ordering guarantees that by the time any `post_render()` runs, all output
textures for the frame are fully written. It is safe to read any node's texture
in `post_render()`.
### Temporal feedback pattern
DAG-based sequences cannot express read-after-write cycles within a single frame.
Use `post_render()` + a persistent internal node to implement temporal feedback
(e.g. CNN prev-frame input):
```cpp
class MyEffect : public Effect {
std::string node_prev_; // internal persistent texture
std::string source_node_; // node to capture at end of frame
public:
void set_source_node(const std::string& n) { source_node_ = n; }
void declare_nodes(NodeRegistry& reg) override {
reg.declare_node(node_prev_, NodeType::U8X4_NORM, -1, -1);
}
void render(...) override {
// Read node_prev_ — contains source_node_ output from the *previous* frame.
WGPUTextureView prev = nodes.get_view(node_prev_);
// ... use prev
}
void post_render(WGPUCommandEncoder enc, NodeRegistry& nodes) override {
if (source_node_.empty() || !nodes.has_node(source_node_)) return;
// Copy this frame's output into node_prev_ for next frame.
WGPUTexelCopyTextureInfo src = {.texture = nodes.get_texture(source_node_)};
WGPUTexelCopyTextureInfo dst = {.texture = nodes.get_texture(node_prev_)};
WGPUExtent3D ext = {(uint32_t)width_, (uint32_t)height_, 1};
wgpuCommandEncoderCopyTextureToTexture(enc, &src, &dst, &ext);
}
};
```
**Why not `input_nodes_[0]` / ping-pong as prev?** The ping-pong alias makes
`source` equal to last frame's `sink` only when the effect is the first in the
sequence and no post-CNN effects overwrite `sink`. `post_render()` is
unconditionally correct regardless of sequence structure.
**Current user**: `GBufferEffect` uses this pattern for `prev.rgb` (CNN temporal
feedback). Call `gbuf->set_cnn_output_node("cnn_out_node")` once at setup.
### Node System
**Types**: Match WGSL texture formats
- `U8X4_NORM`: RGBA8Unorm — default for source/sink/intermediate; `COPY_SRC|COPY_DST`
- `F32X4`: RGBA32Float — HDR, compute outputs
- `F16X8`: 8-channel float16 — G-buffer normals/vectors
- `DEPTH24`: Depth24Plus — 3D rendering
- `COMPUTE_F32`: Storage buffer — non-texture compute data
- `GBUF_ALBEDO`: RGBA16Float — G-buffer albedo/normal MRT; `RENDER_ATTACHMENT|TEXTURE_BINDING|STORAGE_BINDING|COPY_SRC`
- `GBUF_DEPTH32`: Depth32Float — G-buffer depth; `RENDER_ATTACHMENT|TEXTURE_BINDING|COPY_SRC`
- `GBUF_R8`: RGBA8Unorm — G-buffer single-channel (shadow, transp); `STORAGE_BINDING|TEXTURE_BINDING|RENDER_ATTACHMENT`
- `GBUF_RGBA32UINT`: RGBA32Uint — packed feature textures (CNN v3 feat_tex0/1); `STORAGE_BINDING|TEXTURE_BINDING`
**`COPY_SRC|COPY_DST`** is required on any node used with `wgpuCommandEncoderCopyTextureToTexture`.
`U8X4_NORM` has both; use it for temporal feedback dest nodes.
**Aliasing**: Compiler detects ping-pong patterns (Effect i writes A reads B, Effect i+1 writes B reads A) and aliases nodes to same backing texture.
## Compiler Features
**seq_compiler.py** generates optimized C++ from `.seq`:
1. **DAG Validation**: Cycle detection, connectivity checks
2. **Topological Sort**: Execution order from dependencies
3. **Ping-pong Detection**: Automatic node aliasing
4. **Code Generation**: Sequence subclasses with node declarations and effect DAG
**Output**: Single `.cc` file with:
- Multiple `Sequence` subclasses (one per SEQUENCE)
- `InitializeSequences()` - Registry initialization
- `GetActiveSequence(float time)` - Active sequence lookup
- `RenderTimeline()` - Encoder-based and surface-based variants
## Creating Effects
**For standard post-process:**
```cpp
class MyEffect : public Effect {
WGPURenderPipeline pipeline_;
UniformBuffer<UniformsSequenceParams> uniforms_;
MyEffect(const GpuContext& ctx, const std::vector<std::string>& inputs,
const std::vector<std::string>& outputs, float start, float end)
: Effect(ctx, inputs, outputs, start, end) {
uniforms_.init(ctx_.device);
pipeline_ = create_post_process_pipeline(ctx_.device,
WGPUTextureFormat_RGBA8Unorm,
my_shader_wgsl);
}
void render(WGPUCommandEncoder encoder, const UniformsSequenceParams& params,
NodeRegistry& nodes) override {
WGPUTextureView input = nodes.get_view(input_nodes_[0]);
WGPUTextureView output = nodes.get_view(output_nodes_[0]);
uniforms_.update(ctx_.queue, params);
// ... render pass
}
};
```
**For 3D effects with depth:**
```cpp
class My3DEffect : public Effect {
std::string depth_node_;
My3DEffect(...) : Effect(...), depth_node_(outputs[0] + "_depth") {}
void declare_nodes(NodeRegistry& registry) override {
registry.declare_node(depth_node_, NodeType::DEPTH24, -1, -1);
}
void render(...) {
WGPUTextureView color = nodes.get_view(output_nodes_[0]);
WGPUTextureView depth = nodes.get_view(depth_node_);
// ... render pass with depth attachment
}
};
```
## Uniform Access
```cpp
params.time; // Physical seconds (constant speed)
params.beat_time; // Musical beats (tempo-scaled)
params.beat_phase; // Fractional beat 0.0-1.0
params.audio_intensity; // Audio peak for beat sync
params.resolution; // vec2(width, height)
params.aspect_ratio; // width/height
```
## Status & Limitations
**Implemented:**
- DAG validation, topological sort, ping-pong optimization
- Multi-input/multi-output effects
- Node aliasing (compile-time optimization)
- 12 effects: Passthrough, Placeholder, GaussianBlur, Heptagon, Particles, RotatingCube, Hybrid3D, Flash, PeakMeter, Scene1, Scene2, Scratch
**Missing/Future:**
- Flatten mode (`--flatten` generates same code as dev mode)
- BPM handling (parsed but ignored)
- GetDemoDuration() calculation (hardcoded 40.0f)
- Asset validation (not checked against AssetId enum)
- Node type compatibility checking
## See Also
- `doc/EFFECT_WORKFLOW.md` - Step-by-step effect creation
- `tools/seq_compiler.py` - Compiler implementation
- `workspaces/main/timeline.seq` - Example timeline
|