summaryrefslogtreecommitdiff
path: root/tools/shadertoy/README.md
blob: d12865f6ad88baf449589ae11b6d2f5434c2d36c (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
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
# ShaderToy Conversion Guide

Quick guide to convert ShaderToy shaders to demo effects.

**For complete workflow:** See `doc/EFFECT_WORKFLOW.md` for full integration checklist.

## Quick Start (Automated)

```bash
# Save ShaderToy code to a file
cat > tunnel.txt << 'EOF'
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = fragCoord / iResolution.xy;
    vec3 col = 0.5 + 0.5 * cos(iTime + uv.xyx + vec3(0,2,4));
    fragColor = vec4(col, 1.0);
}
EOF

# Generate effect files
./tools/shadertoy/convert_shadertoy.py tunnel.txt Tunnel

# Follow printed instructions to integrate
```

## Files

**Automated Script:**
- `convert_shadertoy.py` - Generates all files from ShaderToy code
- `example.txt` - Example ShaderToy shader for testing

**Manual Templates:**
- `template.h` - Header boilerplate
- `template.cc` - Implementation boilerplate
- `template.wgsl` - Shader boilerplate with conversion notes

## Manual Steps

### 1. Copy Templates

```bash
# Choose effect name (e.g., "tunnel", "plasma", "warp")
EFFECT_NAME="myeffect"

cp tools/shadertoy/template.h src/gpu/effects/${EFFECT_NAME}_effect.h
cp tools/shadertoy/template.cc src/gpu/effects/${EFFECT_NAME}_effect.cc
cp tools/shadertoy/template.wgsl workspaces/main/shaders/${EFFECT_NAME}.wgsl
```

### 2. Rename Class

In both `.h` and `.cc`:
- `ShaderToyEffect` → `MyEffectEffect`
- `SHADERTOY_EFFECT_H_` → `MYEFFECT_EFFECT_H_`
- `shadertoy_effect.h` → `myeffect_effect.h`

### 3. Convert Shader

In `.wgsl`, paste ShaderToy `mainImage()` into `fs_main()`:

**ShaderToy:**
```glsl
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = fragCoord / iResolution.xy;
    fragColor = vec4(uv, 0.5, 1.0);
}
```

**WGSL:**
```wgsl
@fragment fn fs_main(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
    let uv = p.xy / uniforms.resolution;
    return vec4<f32>(uv, 0.5, 1.0);
}
```

### 4. Update Asset Name

In `.cc`, update `AssetId::ASSET_SHADERTOY_SHADER` to match your shader filename:
```cpp
AssetId::ASSET_MYEFFECT_SHADER
```

### 5. Add to Assets

In `workspaces/main/assets.txt`:
```
shaders/myeffect.wgsl
```

### 6. Register Effect

In `src/gpu/demo_effects.h`:
```cpp
#include "gpu/effects/myeffect_effect.h"
```

In `workspaces/main/timeline.seq`:
```
SEQUENCE 0.0 0
  EFFECT + MyEffectEffect 0.0 10.0
```

### 7. Update CMakeLists.txt

Add effect source to `CMakeLists.txt` GPU_SOURCES (both headless and normal mode sections):
```cmake
src/gpu/effects/myeffect_effect.cc
```

### 8. Update Tests

In `src/tests/gpu/test_demo_effects.cc`:
- Add to `post_process_effects` list (lines 80-93) if it's a post-process effect
- OR add to `scene_effects` list (lines 125-137) if it's a scene effect
- Example: `{"MyEffectEffect", std::make_shared<MyEffectEffect>(fixture.ctx())},`

### 9. Build & Test

```bash
cmake --build build -j4
./build/demo64k

# Run tests
cmake -S . -B build -DDEMO_BUILD_TESTS=ON
cmake --build build -j4
cd build && ctest
```

## Example Conversion

**Input ShaderToy:**
```glsl
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = fragCoord / iResolution.xy;
    vec3 col = 0.5 + 0.5 * cos(iTime + uv.xyx + vec3(0,2,4));
    fragColor = vec4(col, 1.0);
}
```

**Generated WGSL (after script + manual fixes):**
```wgsl
@fragment fn fs_main(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
    let uv = p.xy / uniforms.resolution;
    let col = vec3<f32>(0.5) + 0.5 * cos(uniforms.time + uv.xyx + vec3<f32>(0.0, 2.0, 4.0));
    return vec4<f32>(col, 1.0);
}
```

## Common Conversions

| ShaderToy | WGSL |
|-----------|------|
| `iResolution.xy` | `uniforms.resolution` |
| `iTime` | `uniforms.time` |
| `fragCoord` | `p.xy` |
| `float` | `f32` |
| `vec2` | `vec2<f32>` |
| `mod(x, y)` | `x % y` |
| `texture(iChannel0, uv)` | `textureSample(txt, smplr, uv)` |
| `fragColor = ...` | `return ...` |
| `vec2 p = ...` | `let p = vec2<f32>(...)` or `var p: vec2<f32> = ...` |

## Custom Parameters

For tunable values:

**C++ (`.h`):**
```cpp
struct MyEffectParams {
    float speed;
    float scale;
    float _pad[2];
};
static_assert(sizeof(MyEffectParams) == 16, "...");
```

**WGSL:**
```wgsl
struct MyEffectParams {
    speed: f32,
    scale: f32,
    _pad0: f32,
    _pad1: f32,
}
@group(0) @binding(3) var<uniform> params: MyEffectParams;
```

## Available Uniforms

Always available in `uniforms: CommonUniforms`:
- `resolution: vec2<f32>` - Screen resolution
- `aspect_ratio: f32` - Width/height
- `time: f32` - Demo time (seconds)
- `beat: f32` - Music beat sync (0-1)
- `audio_intensity: f32` - Audio reactivity

## Next Steps

- See `doc/CONTRIBUTING.md` for commit policy
- See `doc/SEQUENCE.md` for timeline syntax
- See existing effects in `src/gpu/effects/` for examples