summaryrefslogtreecommitdiff
path: root/tools/shadertoy/convert_shadertoy.py
blob: 2f402cc616b7b32ac7d066b6c863a03a30069cca (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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/env python3
# This file is part of the 64k demo project.
# Converts ShaderToy shader to demo effect boilerplate.
#
# Usage:
#   ./tools/shadertoy/convert_shadertoy.py <shader.txt> <EffectName>
#
# Example:
#   ./tools/shadertoy/convert_shadertoy.py tunnel.txt Tunnel
#   ./tools/shadertoy/convert_shadertoy.py tools/shadertoy/example.txt Rainbow
#
# Generates:
#   - src/gpu/effects/<effect>_effect.h
#   - src/gpu/effects/<effect>_effect.cc
#   - workspaces/main/shaders/<effect>.wgsl
#
# The script performs basic ShaderToy→WGSL conversion:
#   - Converts types (float→f32, vec2→vec2<f32>, etc.)
#   - Converts uniforms (iTime→uniforms.time, etc.)
#   - Extracts mainImage() body into fs_main()
#   - Generates boilerplate C++ effect class
#
# Manual fixes usually needed:
#   - fragColor assignments → return statements
#   - Variable name conflicts (e.g., shadowing 'p')
#   - Complex type inference
#   - Texture channel mappings
#   - Helper function signatures

import sys
import os
import re
from pathlib import Path

def to_snake_case(name):
    """Convert CamelCase to snake_case."""
    s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
    return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()

def to_upper_snake_case(name):
    """Convert CamelCase to UPPER_SNAKE_CASE."""
    return to_snake_case(name).upper()

def to_camel_case(name):
    """Convert snake_case to CamelCase."""
    return ''.join(word.capitalize() for word in name.split('_'))

def convert_shadertoy_to_wgsl(shader_code):
    """Basic ShaderToy to WGSL conversion."""
    # Extract mainImage first
    main_match = re.search(r'void\s+mainImage\s*\([^)]+\)\s*\{(.*)\}', shader_code, re.DOTALL)
    if main_match:
        main_body = main_match.group(1).strip()
        helpers = shader_code[:main_match.start()]
    else:
        main_body = ""
        helpers = shader_code

    # Replace common ShaderToy defines
    conversions = [
        (r'#define\s+TIME\s+iTime', ''),
        (r'#define\s+RESOLUTION\s+iResolution', ''),
        (r'#define\s+PI\s+[\d.]+', 'const PI: f32 = 3.141592654;'),
        (r'#define\s+TAU\s+\([^)]+\)', 'const TAU: f32 = 6.283185307;'),
        (r'#define\s+ROT\(a\)\s+mat2\([^)]+\)', ''),  # Will be converted to function

        # Common ShaderToy uniforms
        (r'\bTIME\b', 'uniforms.time'),
        (r'\biTime\b', 'uniforms.time'),
        (r'\bRESOLUTION\b', 'uniforms.resolution'),
        (r'\biResolution\b', 'uniforms.resolution'),
        (r'\bfragCoord\b', 'p.xy'),

        # Type conversions
        (r'\bfloat\b', 'f32'),
        (r'\bvec2\b', 'vec2<f32>'),
        (r'\bvec3\b', 'vec3<f32>'),
        (r'\bvec4\b', 'vec4<f32>'),
        (r'\bmat2\b', 'mat2x2<f32>'),
        (r'\bmat3\b', 'mat3x3<f32>'),
        (r'\bmat4\b', 'mat4x4<f32>'),

        # Function declarations (preserve return type context)
        (r'\bf32\s+(\w+)\s*\(', r'fn \1('),
        (r'\bvec2<f32>\s+(\w+)\s*\(', r'fn \1('),
        (r'\bvec3<f32>\s+(\w+)\s*\(', r'fn \1('),
        (r'\bvec4<f32>\s+(\w+)\s*\(', r'fn \1('),
        (r'\bvoid\s+(\w+)\s*\(', r'fn \1('),

        # Const declarations
        (r'\bconst\s+f32\s+(\w+)\s*=', r'const \1: f32 ='),
        (r'\bconst\s+vec2<f32>\s+(\w+)\s*=', r'const \1 ='),
        (r'\bconst\s+vec3<f32>\s+(\w+)\s*=', r'const \1 ='),
        (r'\bconst\s+vec4<f32>\s+(\w+)\s*=', r'const \1 ='),

        # Function calls that need fixing
        (r'\bfract\s*\(', 'fract('),
        (r'\bmod\s*\(([^,]+),\s*([^)]+)\)', r'(\1 % \2)'),
    ]

    converted_helpers = helpers
    for pattern, replacement in conversions:
        converted_helpers = re.sub(pattern, replacement, converted_helpers)

    # Convert mainImage body
    converted_main = main_body
    for pattern, replacement in conversions:
        converted_main = re.sub(pattern, replacement, converted_main)

    # Fix fragColor assignments -> returns
    converted_main = re.sub(r'\bfragColor\s*=\s*([^;]+);', r'return \1;', converted_main)

    # Indent main body
    indented_main = '\n'.join('  ' + line if line.strip() else '' for line in converted_main.split('\n'))

    # Build fragment function with Y-flip for ShaderToy convention
    fragment = f"""@fragment fn fs_main(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {{
  // Flip Y to match ShaderToy convention (origin at bottom-left)
  let flipped = vec2<f32>(p.x, uniforms.resolution.y - p.y);
  let q = flipped / uniforms.resolution;
  var coord = -1.0 + 2.0 * q;
  coord.x *= uniforms.resolution.x / uniforms.resolution.y;

{indented_main}
}}"""

    return converted_helpers + '\n\n' + fragment

def extract_main_image(shader_code):
    """Extract mainImage function body from ShaderToy code."""
    # Try to find mainImage function
    match = re.search(r'void\s+mainImage\s*\([^)]+\)\s*\{(.*)\}', shader_code, re.DOTALL)
    if match:
        return match.group(1).strip()

    # If no mainImage found, return whole shader
    return shader_code

def generate_header(effect_name, is_post_process=False):
    """Generate .h file content."""
    class_name = f"{effect_name}Effect"
    upper_name = to_upper_snake_case(effect_name)

    if is_post_process:
        return f"""// This file is part of the 64k demo project.
// {effect_name} effect - ShaderToy conversion (post-process)
// Generated by convert_shadertoy.py

#ifndef {upper_name}_EFFECT_H_
#define {upper_name}_EFFECT_H_

#include "gpu/effect.h"
#include "gpu/effects/post_process_helper.h"

class {class_name} : public PostProcessEffect {{
 public:
  {class_name}(const GpuContext& ctx);
  void render(WGPURenderPassEncoder pass, float time, float beat,
              float intensity, float aspect_ratio) override;
  void update_bind_group(WGPUTextureView input_view) override;
}};

#endif /* {upper_name}_EFFECT_H_ */
"""
    else:
        # Scene effect (simpler, like HeptagonEffect)
        return f"""// This file is part of the 64k demo project.
// {effect_name} effect - ShaderToy conversion (scene)
// Generated by convert_shadertoy.py

#ifndef {upper_name}_EFFECT_H_
#define {upper_name}_EFFECT_H_

#include "gpu/effect.h"

class {class_name} : public Effect {{
 public:
  {class_name}(const GpuContext& ctx);
  void render(WGPURenderPassEncoder pass, float time, float beat,
              float intensity, float aspect_ratio) override;

 private:
  RenderPass pass_;
}};

#endif /* {upper_name}_EFFECT_H_ */
"""

def generate_implementation(effect_name, is_post_process=False):
    """Generate .cc file content."""
    class_name = f"{effect_name}Effect"
    snake_name = to_snake_case(effect_name)

    if is_post_process:
        return f"""// This file is part of the 64k demo project.
// {effect_name} effect - ShaderToy conversion (post-process)
// Generated by convert_shadertoy.py

#include "gpu/demo_effects.h"
#include "gpu/effects/post_process_helper.h"
#include "gpu/gpu.h"

{class_name}::{class_name}(const GpuContext& ctx) : PostProcessEffect(ctx) {{
  pipeline_ = create_post_process_pipeline(ctx_.device, ctx_.format, {snake_name}_shader_wgsl);
}}

void {class_name}::render(WGPURenderPassEncoder pass, float time, float beat,
                          float intensity, float aspect_ratio) {{
  const CommonPostProcessUniforms u = {{
      .resolution = {{(float)width_, (float)height_}},
      ._pad = {{0.0f, 0.0f}},
      .aspect_ratio = aspect_ratio,
      .time = time,
      .beat = beat,
      .audio_intensity = intensity,
  }};
  uniforms_.update(ctx_.queue, u);

  wgpuRenderPassEncoderSetPipeline(pass, pipeline_);
  wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr);
  wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0);
}}

void {class_name}::update_bind_group(WGPUTextureView input_view) {{
  pp_update_bind_group(ctx_.device, pipeline_, &bind_group_, input_view, uniforms_.get());
}}
"""
    else:
        # Scene effect (simpler pattern like HeptagonEffect)
        return f"""// This file is part of the 64k demo project.
// {effect_name} effect - ShaderToy conversion (scene)
// Generated by convert_shadertoy.py

#include "gpu/demo_effects.h"
#include "gpu/gpu.h"

{class_name}::{class_name}(const GpuContext& ctx) : Effect(ctx) {{
  ResourceBinding bindings[] = {{{{uniforms_.get(), WGPUBufferBindingType_Uniform}}}};
  pass_ = gpu_create_render_pass(ctx_.device, ctx_.format, {snake_name}_shader_wgsl,
                                 bindings, 1);
  pass_.vertex_count = 3;
}}

void {class_name}::render(WGPURenderPassEncoder pass, float t, float b,
                          float i, float a) {{
  CommonPostProcessUniforms u = {{
      .resolution = {{(float)width_, (float)height_}},
      ._pad = {{0.0f, 0.0f}},
      .aspect_ratio = a,
      .time = t,
      .beat = b,
      .audio_intensity = i,
  }};
  uniforms_.update(ctx_.queue, u);
  wgpuRenderPassEncoderSetPipeline(pass, pass_.pipeline);
  wgpuRenderPassEncoderSetBindGroup(pass, 0, pass_.bind_group, 0, nullptr);
  wgpuRenderPassEncoderDraw(pass, pass_.vertex_count, 1, 0, 0);
}}
"""

def generate_shader(effect_name, shadertoy_code, is_post_process=False):
    """Generate .wgsl file content."""
    # Convert to WGSL (full shader, not just mainImage)
    converted = convert_shadertoy_to_wgsl(shadertoy_code)

    if is_post_process:
        bindings = """@group(0) @binding(0) var smplr: sampler;
@group(0) @binding(1) var txt: texture_2d<f32>;

#include "common_uniforms"

@group(0) @binding(2) var<uniform> uniforms: CommonUniforms;"""
    else:
        # Scene effect - only uniforms, no texture input
        bindings = """#include "common_uniforms"

@group(0) @binding(0) var<uniform> uniforms: CommonUniforms;"""

    return f"""// {effect_name} effect shader - ShaderToy conversion
// Generated by convert_shadertoy.py
// NOTE: Manual review recommended - conversion is basic

{bindings}

@vertex fn vs_main(@builtin(vertex_index) i: u32) -> @builtin(position) vec4<f32> {{
    var pos = array<vec2<f32>, 3>(
        vec2<f32>(-1.0, -1.0),
        vec2<f32>(3.0, -1.0),
        vec2<f32>(-1.0, 3.0)
    );
    return vec4<f32>(pos[i], 0.0, 1.0);
}}

{converted}
"""

def main():
    if len(sys.argv) < 3:
        print("Usage: convert_shadertoy.py <shader.txt> <EffectName> [--post-process]")
        print()
        print("Examples:")
        print("  ./tools/shadertoy/convert_shadertoy.py tunnel.txt Tunnel")
        print("  ./tools/shadertoy/convert_shadertoy.py blur.txt Blur --post-process")
        print()
        print("Options:")
        print("  --post-process  Generate post-process effect (operates on previous frame)")
        print("                  Default: scene effect (renders geometry)")
        print()
        print("This will generate:")
        print("  src/gpu/effects/<effect>_effect.h")
        print("  src/gpu/effects/<effect>_effect.cc")
        print("  workspaces/main/shaders/<effect>.wgsl")
        sys.exit(1)

    shader_file = sys.argv[1]
    effect_name = sys.argv[2]
    is_post_process = '--post-process' in sys.argv

    # Ensure effect name is CamelCase
    if '_' in effect_name:
        effect_name = to_camel_case(effect_name)

    # Read shader code
    if not os.path.exists(shader_file):
        print(f"Error: {shader_file} not found")
        sys.exit(1)

    with open(shader_file, 'r') as f:
        shadertoy_code = f.read()

    # Generate file names
    snake_name = to_snake_case(effect_name)
    upper_name = to_upper_snake_case(effect_name)

    # Script is in tools/shadertoy/, so go up two levels to repo root
    repo_root = Path(__file__).parent.parent.parent
    header_path = repo_root / "src" / "gpu" / "effects" / f"{snake_name}_effect.h"
    impl_path = repo_root / "src" / "gpu" / "effects" / f"{snake_name}_effect.cc"
    shader_path = repo_root / "workspaces" / "main" / "shaders" / f"{snake_name}.wgsl"

    # Generate files
    print(f"Generating effect: {effect_name}")
    print(f"  Header:     {header_path}")
    print(f"  Impl:       {impl_path}")
    print(f"  Shader:     {shader_path}")
    print()

    # Write files
    header_path.write_text(generate_header(effect_name, is_post_process))
    impl_path.write_text(generate_implementation(effect_name, is_post_process))
    shader_path.write_text(generate_shader(effect_name, shadertoy_code, is_post_process))

    effect_type = "post-process" if is_post_process else "scene"
    print(f"✓ Files generated ({effect_type} effect)")
    print()
    print("Next steps (see doc/EFFECT_WORKFLOW.md for details):")
    print()
    print("1. Add shader to workspaces/main/assets.txt:")
    print(f"   SHADER_{upper_name}, NONE, shaders/{snake_name}.wgsl, \"{effect_name} effect\"")
    print()
    print()
    print("2. Add shader declaration to src/gpu/effects/shaders.h:")
    print(f"   extern const char* {snake_name}_shader_wgsl;")
    print()
    print("3. Add shader definition to src/gpu/effects/shaders.cc:")
    print(f"   const char* {snake_name}_shader_wgsl = SafeGetAsset(AssetId::ASSET_SHADER_{upper_name});")
    print()
    print("4. Include header in src/gpu/demo_effects.h:")
    print(f'   #include "gpu/effects/{snake_name}_effect.h"')
    print()
    print("5. Add to timeline in workspaces/main/timeline.seq:")
    print(f"   EFFECT + {effect_name}Effect 0.0 10.0")
    print()
    print("6. Add to CMakeLists.txt GPU_SOURCES (both headless and normal mode):")
    print(f"   src/gpu/effects/{snake_name}_effect.cc")
    print()
    print("7. Update src/tests/gpu/test_demo_effects.cc:")
    test_list = "post_process_effects" if is_post_process else "scene_effects"
    print(f'   - Add "{{{effect_name}Effect", std::make_shared<{effect_name}Effect>(fixture.ctx())}}" to {test_list} list')
    print()
    print("8. Build and test:")
    print("   cmake --build build -j4")
    print("   ./build/demo64k")
    print()
    print("Note: Review generated shader for const expression issues (normalize, etc)")

if __name__ == '__main__':
    main()