alice-sdf 5.1.0

ALICE-SDF: 3D/Spatial Data Specialist — Store laws, not polygons. GLSL/WGSL/HLSL transpile, SVO, GPU marching cubes, texture-fit, procedural CSG. Optional bridges to alice-physics / alice-codec / libasp / alice-cache behind features.
Documentation
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
//! ALICE-SDF CLI
//!
//! Command-line interface for SDF operations.
//!
//! Author: Moroya Sakamoto

#![allow(
    clippy::uninlined_format_args,
    clippy::needless_pass_by_value,
    clippy::let_unit_value,
    clippy::format_push_string,
    clippy::cast_precision_loss,
    clippy::too_many_lines,
    clippy::doc_markdown,
    clippy::ignored_unit_patterns
)]

#[cfg(feature = "cli")]
use alice_sdf::io::{export_3mf, export_ply, export_stl, PlyConfig};
#[cfg(feature = "cli")]
use alice_sdf::prelude::*;
#[cfg(feature = "cli")]
use std::path::PathBuf;

#[cfg(feature = "cli")]
use clap::{Parser, Subcommand};

// Deep Fried Edition: JIT + Rayon imports
#[cfg(all(feature = "cli", feature = "jit"))]
use alice_sdf::compiled::jit::JitSimdSdf;
#[cfg(feature = "cli")]
#[allow(unused_imports)]
use rayon::prelude::*;

#[cfg(feature = "cli")]
#[derive(Parser)]
#[command(name = "alice-sdf")]
#[command(author = "Moroya Sakamoto")]
#[command(version = alice_sdf::VERSION)]
#[command(about = "ALICE-SDF: 3D/Spatial Data Specialist", long_about = None)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[cfg(feature = "cli")]
#[derive(Subcommand)]
enum Commands {
    /// Display file information
    Info {
        /// Input file (.asdf or .asdf.json)
        file: PathBuf,
    },

    /// Convert between formats
    Convert {
        /// Input file
        input: PathBuf,
        /// Output file
        #[arg(short, long)]
        output: PathBuf,
    },

    /// Convert SDF to mesh (OBJ format)
    ToMesh {
        /// Input SDF file
        input: PathBuf,
        /// Output OBJ file
        #[arg(short, long)]
        output: PathBuf,
        /// Grid resolution
        #[arg(short, long, default_value = "64")]
        resolution: usize,
        /// Bounding box size
        #[arg(short, long, default_value = "2.0")]
        bounds: f32,
    },

    /// Create a demo SDF file
    Demo {
        /// Output file
        #[arg(short, long, default_value = "demo.asdf")]
        output: PathBuf,
    },

    /// Benchmark SDF evaluation
    Bench {
        /// Input file (or uses demo shape)
        file: Option<PathBuf>,
        /// Number of points to evaluate
        #[arg(short, long, default_value = "1000000")]
        points: usize,
    },

    /// Export SDF to mesh file (auto-detect format from extension)
    Export {
        /// Input SDF file (.asdf or .asdf.json)
        input: PathBuf,
        /// Output mesh file (.obj, .glb, .fbx, .usda, .abc)
        #[arg(short, long)]
        output: PathBuf,
        /// Grid resolution for marching cubes
        #[arg(short, long, default_value = "64")]
        resolution: usize,
        /// Bounding box half-size
        #[arg(short, long, default_value = "2.0")]
        bounds: f32,
    },

    /// Generate 3D-printable mesh from SDF (STL/3MF output)
    Print {
        /// Input SDF file (.asdf or .asdf.json)
        input: PathBuf,
        /// Output mesh file (.3mf, .stl, .obj)
        #[arg(short, long)]
        output: Option<PathBuf>,
        /// Grid resolution for marching cubes (higher = finer detail)
        #[arg(short, long, default_value = "128")]
        resolution: usize,
        /// Bounding box half-size (auto-detect if omitted)
        #[arg(short, long)]
        bounds: Option<f32>,
    },

    /// Fit a texture image to procedural noise formula
    #[cfg(feature = "texture-fit")]
    TextureFit {
        /// Input image file (PNG/JPG)
        input: PathBuf,
        /// Maximum octaves
        #[arg(long, default_value = "8")]
        octaves: u32,
        /// Target PSNR (dB)
        #[arg(long, default_value = "28.0")]
        target_psnr: f32,
        /// Iterations per octave
        #[arg(long, default_value = "500")]
        iterations: u32,
        /// Output JSON file for parameters
        #[arg(short, long)]
        output: Option<PathBuf>,
        /// Shader language: wgsl, hlsl, or glsl
        #[arg(long)]
        shader: Option<String>,
        /// Shader output file
        #[arg(long)]
        shader_output: Option<PathBuf>,
    },
}

#[cfg(feature = "cli")]
fn main() {
    let cli = Cli::parse();

    match cli.command {
        Commands::Info { file } => cmd_info(file),
        Commands::Convert { input, output } => cmd_convert(input, output),
        Commands::ToMesh {
            input,
            output,
            resolution,
            bounds,
        } => cmd_to_mesh(input, output, resolution, bounds),
        Commands::Demo { output } => cmd_demo(output),
        Commands::Export {
            input,
            output,
            resolution,
            bounds,
        } => cmd_export(input, output, resolution, bounds),
        Commands::Print {
            input,
            output,
            resolution,
            bounds,
        } => cmd_print(input, output, resolution, bounds),
        Commands::Bench { file, points } => cmd_bench(file, points),
        #[cfg(feature = "texture-fit")]
        Commands::TextureFit {
            input,
            octaves,
            target_psnr,
            iterations,
            output,
            shader,
            shader_output,
        } => cmd_texture_fit(
            input,
            octaves,
            target_psnr,
            iterations,
            output,
            shader,
            shader_output,
        ),
    }
}

#[cfg(not(feature = "cli"))]
fn main() {
    eprintln!("CLI not enabled. Build with --features cli");
    std::process::exit(1);
}

#[cfg(feature = "cli")]
fn cmd_info(path: PathBuf) {
    match get_info(&path) {
        Ok(info) => println!("{}", info),
        Err(e) => {
            eprintln!("Error: {}", e);
            std::process::exit(1);
        }
    }
}

#[cfg(feature = "cli")]
fn cmd_convert(input: PathBuf, output: PathBuf) {
    match load(&input) {
        Ok(tree) => match save(&tree, &output) {
            Ok(_) => println!("Converted {} -> {}", input.display(), output.display()),
            Err(e) => {
                eprintln!("Save error: {}", e);
                std::process::exit(1);
            }
        },
        Err(e) => {
            eprintln!("Load error: {}", e);
            std::process::exit(1);
        }
    }
}

#[cfg(feature = "cli")]
fn cmd_to_mesh(input: PathBuf, output: PathBuf, resolution: usize, bounds: f32) {
    let tree = match load(&input) {
        Ok(t) => t,
        Err(e) => {
            eprintln!("Load error: {}", e);
            std::process::exit(1);
        }
    };

    let config = MarchingCubesConfig {
        resolution,
        iso_level: 0.0,
        compute_normals: true,
        ..Default::default()
    };

    let min = Vec3::splat(-bounds);
    let max = Vec3::splat(bounds);

    println!("Generating mesh with resolution {}...", resolution);
    let mesh = sdf_to_mesh(&tree.root, min, max, &config);

    // Write OBJ file
    let mut obj = String::new();
    obj.push_str("# Generated by ALICE-SDF\n");
    obj.push_str(&format!("# Vertices: {}\n", mesh.vertex_count()));
    obj.push_str(&format!("# Triangles: {}\n\n", mesh.triangle_count()));

    for v in &mesh.vertices {
        obj.push_str(&format!(
            "v {} {} {}\n",
            v.position.x, v.position.y, v.position.z
        ));
    }

    obj.push('\n');

    for v in &mesh.vertices {
        obj.push_str(&format!(
            "vn {} {} {}\n",
            v.normal.x, v.normal.y, v.normal.z
        ));
    }

    obj.push('\n');

    for chunk in mesh.indices.chunks(3) {
        if chunk.len() == 3 {
            let a = chunk[0] + 1;
            let b = chunk[1] + 1;
            let c = chunk[2] + 1;
            obj.push_str(&format!("f {}//{} {}//{} {}//{}\n", a, a, b, b, c, c));
        }
    }

    match std::fs::write(&output, obj) {
        Ok(_) => println!(
            "Saved {} vertices, {} triangles to {}",
            mesh.vertex_count(),
            mesh.triangle_count(),
            output.display()
        ),
        Err(e) => {
            eprintln!("Write error: {}", e);
            std::process::exit(1);
        }
    }
}

#[cfg(feature = "cli")]
fn cmd_export(input: PathBuf, output: PathBuf, resolution: usize, bounds: f32) {
    let tree = match load(&input) {
        Ok(t) => t,
        Err(e) => {
            eprintln!("Load error: {}", e);
            std::process::exit(1);
        }
    };

    let config = MarchingCubesConfig {
        resolution,
        iso_level: 0.0,
        compute_normals: true,
        ..Default::default()
    };

    let min = Vec3::splat(-bounds);
    let max = Vec3::splat(bounds);

    println!("Generating mesh with resolution {}...", resolution);
    let mesh = sdf_to_mesh(&tree.root, min, max, &config);
    println!(
        "  {} vertices, {} triangles",
        mesh.vertex_count(),
        mesh.triangle_count()
    );

    let ext = output
        .extension()
        .and_then(|e| e.to_str())
        .unwrap_or("")
        .to_lowercase();

    let result = match ext.as_str() {
        "obj" => export_obj(&mesh, &output, &ObjConfig::default(), None),
        "glb" => export_glb(&mesh, &output, &GltfConfig::default(), None),
        "fbx" => export_fbx(&mesh, &output, &FbxConfig::default(), None),
        "usda" | "usd" => export_usda(&mesh, &output, &UsdConfig::default(), None),
        "abc" => export_alembic(&mesh, &output, &AlembicConfig::default()),
        "stl" => export_stl(&mesh, &output),
        "3mf" => export_3mf(&mesh, &output),
        "ply" => export_ply(&mesh, &output, &PlyConfig::default()),
        _ => {
            eprintln!("Unsupported format: .{}", ext);
            eprintln!("Supported: .obj, .glb, .fbx, .usda, .abc, .stl, .3mf, .ply");
            std::process::exit(1);
        }
    };

    match result {
        Ok(_) => println!("Exported to {}", output.display()),
        Err(e) => {
            eprintln!("Export error: {}", e);
            std::process::exit(1);
        }
    }
}

#[cfg(feature = "cli")]
fn cmd_print(
    input: PathBuf,
    output: Option<PathBuf>,
    resolution: usize,
    bounds_override: Option<f32>,
) {
    use alice_sdf::mesh::sdf_to_mesh_compiled;

    let total_start = std::time::Instant::now();

    let tree = match load(&input) {
        Ok(t) => t,
        Err(e) => {
            eprintln!("Load error: {}", e);
            std::process::exit(1);
        }
    };

    let node_count = tree.node_count();
    println!("Loaded SDF: {} nodes", node_count);

    // Compile SDF for faster mesh generation (SIMD batch eval)
    let compile_start = std::time::Instant::now();
    let compiled = CompiledSdf::compile(&tree.root);
    let compile_ms = compile_start.elapsed().as_secs_f64() * 1000.0;

    // Auto-detect bounds from tight AABB if not specified
    let (min, max) = if let Some(b) = bounds_override {
        (Vec3::splat(-b), Vec3::splat(b))
    } else {
        // Use large initial search for 3D printing scale (up to 500mm)
        let aabb_config = alice_sdf::tight_aabb::TightAabbConfig::preset_large();
        let aabb = alice_sdf::tight_aabb::compute_tight_aabb_with_config(&tree.root, &aabb_config);
        if aabb.min == Vec3::ZERO && aabb.max == Vec3::ZERO {
            eprintln!("Warning: Could not detect bounds. Using default [-200, 200]³");
            (Vec3::splat(-200.0), Vec3::splat(200.0))
        } else {
            let center = (aabb.min + aabb.max) * 0.5;
            let half = (aabb.max - aabb.min) * 0.5;
            let max_extent = half.x.max(half.y).max(half.z);
            let padded = max_extent * 1.1;
            (center - Vec3::splat(padded), center + Vec3::splat(padded))
        }
    };

    let config = MarchingCubesConfig {
        resolution,
        iso_level: 0.0,
        compute_normals: true,
        ..Default::default()
    };

    println!(
        "Generating mesh: resolution={}, bounds=[{:.1}, {:.1}, {:.1}] to [{:.1}, {:.1}, {:.1}]",
        resolution, min.x, min.y, min.z, max.x, max.y, max.z
    );

    // Use compiled SDF for faster marching cubes (SIMD 8-wide batch evaluation)
    let mesh_start = std::time::Instant::now();
    let mesh = sdf_to_mesh_compiled(&compiled, min, max, &config);
    let mesh_ms = mesh_start.elapsed().as_secs_f64() * 1000.0;

    println!(
        "  {} vertices, {} triangles (compile: {:.1}ms, mesh: {:.1}ms)",
        mesh.vertex_count(),
        mesh.triangle_count(),
        compile_ms,
        mesh_ms
    );

    // Determine output path and format
    let out_path = output.unwrap_or_else(|| {
        let stem = input.file_stem().unwrap_or_default().to_string_lossy();
        PathBuf::from(format!("{}.3mf", stem))
    });

    let ext = out_path
        .extension()
        .and_then(|e| e.to_str())
        .unwrap_or("3mf")
        .to_lowercase();

    let export_start = std::time::Instant::now();
    let result = match ext.as_str() {
        "3mf" => export_3mf(&mesh, &out_path),
        "stl" => export_stl(&mesh, &out_path),
        "obj" => export_obj(&mesh, &out_path, &ObjConfig::default(), None),
        "ply" => export_ply(&mesh, &out_path, &PlyConfig::default()),
        _ => {
            eprintln!("Unsupported print format: .{}", ext);
            eprintln!("Supported: .3mf (recommended), .stl, .obj, .ply");
            std::process::exit(1);
        }
    };
    let export_ms = export_start.elapsed().as_secs_f64() * 1000.0;

    match result {
        Ok(_) => {
            let file_size = std::fs::metadata(&out_path).map(|m| m.len()).unwrap_or(0);
            let total_ms = total_start.elapsed().as_secs_f64() * 1000.0;
            println!(
                "Exported to {} ({:.1} KB, export: {:.1}ms, total: {:.1}ms)",
                out_path.display(),
                file_size as f64 / 1024.0,
                export_ms,
                total_ms
            );
            println!("Ready for slicing (Bambu Studio / Orca Slicer)");
        }
        Err(e) => {
            eprintln!("Export error: {}", e);
            std::process::exit(1);
        }
    }
}

#[cfg(feature = "cli")]
fn cmd_demo(output: PathBuf) {
    let shape = SdfNode::sphere(1.0)
        .smooth_subtract(SdfNode::box3d(1.2, 1.2, 1.2), 0.1)
        .smooth_union(
            SdfNode::cylinder(0.3, 2.0).rotate_euler(std::f32::consts::FRAC_PI_2, 0.0, 0.0),
            0.1,
        )
        .smooth_union(SdfNode::cylinder(0.3, 2.0), 0.1)
        .smooth_union(
            SdfNode::cylinder(0.3, 2.0).rotate_euler(0.0, 0.0, std::f32::consts::FRAC_PI_2),
            0.1,
        );

    let tree = SdfTree::new(shape);

    match save(&tree, &output) {
        Ok(_) => println!(
            "Created demo SDF with {} nodes at {}",
            tree.node_count(),
            output.display()
        ),
        Err(e) => {
            eprintln!("Save error: {}", e);
            std::process::exit(1);
        }
    }
}

/// Deep Fried Crispy Edition Benchmark
///
/// Uses JIT + SoA + Rayon for maximum throughput with GPU comparison:
/// - JIT: Native SIMD machine code (no interpreter overhead)
/// - SoA: Structure of Arrays for optimal SIMD loads
/// - Rayon: Multi-threaded parallel execution
/// - GPU: WebGPU compute shader for comparison
#[cfg(all(feature = "cli", feature = "jit"))]
#[allow(
    clippy::disallowed_methods,
    reason = "benchmark input points for the CLI; not part of any output"
)]
fn cmd_bench(file: Option<PathBuf>, points: usize) {
    // 1. Load or create SDF node
    let node = file.map_or_else(
        || {
            // Demo shape (complex enough to stress test)
            SdfNode::sphere(1.0)
                .smooth_union(SdfNode::box3d(1.0, 1.0, 1.0), 0.2)
                .twist(0.5)
        },
        |path| match load(&path) {
            Ok(tree) => tree.root,
            Err(e) => {
                eprintln!("Load error: {}", e);
                std::process::exit(1);
            }
        },
    );

    println!("=== Deep Fried Crispy Edition Benchmark ===");
    println!("Points: {}", points);
    println!("Node count: {}", node.node_count());
    println!("Threads: {}", rayon::current_num_threads());

    // 2. Compile all backends
    println!("\n--- Compilation Phase ---");
    let compile_start = std::time::Instant::now();
    let compiled_sdf = CompiledSdf::compile(&node);
    println!(
        "Stack VM: {:.3}ms",
        compile_start.elapsed().as_secs_f64() * 1000.0
    );

    let jit_start = std::time::Instant::now();
    let jit = match JitSimdSdf::compile(&compiled_sdf) {
        Ok(jit) => jit,
        Err(e) => {
            eprintln!("JIT Compile error: {}", e);
            std::process::exit(1);
        }
    };
    println!(
        "JIT SIMD : {:.3}ms",
        jit_start.elapsed().as_secs_f64() * 1000.0
    );

    // GPU compilation (optional)
    #[cfg(feature = "gpu")]
    let gpu = {
        let gpu_start = std::time::Instant::now();
        match alice_sdf::compiled::GpuEvaluator::new(&node) {
            Ok(g) => {
                println!(
                    "GPU WGSL : {:.3}ms",
                    gpu_start.elapsed().as_secs_f64() * 1000.0
                );
                Some(g)
            }
            Err(e) => {
                println!("GPU WGSL : unavailable ({:?})", e);
                None
            }
        }
    };

    // 3. Generate test data in SoA layout
    println!("\n--- Data Generation ---");
    let data_start = std::time::Instant::now();
    let mut px = vec![0.0f32; points];
    let mut py = vec![0.0f32; points];
    let mut pz = vec![0.0f32; points];

    px.par_iter_mut()
        .zip(py.par_iter_mut())
        .zip(pz.par_iter_mut())
        .enumerate()
        .for_each(|(i, ((x, y), z))| {
            let t = i as f32 / points as f32;
            *x = (t * 1234.567).sin() * 50.0;
            *y = (t * 2345.678).cos() * 50.0;
            *z = (t * 3456.789).sin() * 50.0;
        });
    println!(
        "SoA data : {:.3}ms",
        data_start.elapsed().as_secs_f64() * 1000.0
    );

    // Also prepare AoS for GPU
    let points_vec: Vec<Vec3> = (0..points)
        .map(|i| Vec3::new(px[i], py[i], pz[i]))
        .collect();

    // 4. Benchmark all modes
    println!("\n--- Benchmark Results ---");
    println!(
        "{:<20} {:>12} {:>15} {:>12}",
        "Mode", "Time (ms)", "Throughput", "ns/point"
    );
    println!("{}", "-".repeat(62));

    // CPU Scalar (interpreted)
    let start = std::time::Instant::now();
    let _results_scalar = eval_batch_parallel(&node, &points_vec);
    let elapsed = start.elapsed();
    print_result("CPU Scalar", elapsed, points);

    // CPU SIMD (stack VM)
    let start = std::time::Instant::now();
    let _results_simd =
        alice_sdf::compiled::eval_compiled_batch_simd_parallel(&compiled_sdf, &points_vec);
    let elapsed = start.elapsed();
    print_result("CPU SIMD (VM)", elapsed, points);

    // CPU JIT SIMD
    let start = std::time::Instant::now();
    let num_threads = rayon::current_num_threads();
    let chunk_size = points.div_ceil(num_threads);
    let _results_jit: Vec<f32> = px
        .par_chunks(chunk_size)
        .zip(py.par_chunks(chunk_size))
        .zip(pz.par_chunks(chunk_size))
        .flat_map(|((cx, cy), cz)| jit.eval_batch(cx, cy, cz))
        .collect();
    let elapsed = start.elapsed();
    print_result("CPU JIT SIMD", elapsed, points);

    // GPU Compute: one persistent buffer pool, batches above 256K points are
    // split into chunks (`eval_batch_auto`), so the GPU buffers are allocated
    // once (grown to the first chunk) and a multi-million-point run stays
    // within the storage-buffer binding limit.
    #[cfg(feature = "gpu")]
    if let Some(ref gpu) = gpu {
        let mut pool = gpu.create_buffer_pool(0);
        let start = std::time::Instant::now();
        let results_gpu = gpu.eval_batch_auto(&points_vec, &mut pool);
        let elapsed = start.elapsed();
        match results_gpu {
            Ok(_) => print_result("GPU Compute", elapsed, points),
            Err(e) => println!("{:<20} failed ({})", "GPU Compute", e),
        }
    }

    println!("{}", "-".repeat(62));
}

#[cfg(feature = "cli")]
#[allow(dead_code)]
fn print_result(mode: &str, elapsed: std::time::Duration, points: usize) {
    let seconds = elapsed.as_secs_f64();
    let throughput = points as f64 / seconds / 1_000_000.0;
    let ns_per_point = seconds * 1_000_000_000.0 / points as f64;
    println!(
        "{:<20} {:>10.3}ms {:>12.2} M/s {:>10.2} ns",
        mode,
        seconds * 1000.0,
        throughput,
        ns_per_point
    );
}

/// Fallback benchmark (no JIT feature)
#[cfg(all(feature = "cli", not(feature = "jit")))]
#[allow(
    clippy::disallowed_methods,
    reason = "benchmark input points for the CLI; not part of any output"
)]
fn cmd_bench(file: Option<PathBuf>, points: usize) {
    let node = file.map_or_else(
        || {
            SdfNode::sphere(1.0)
                .smooth_union(SdfNode::box3d(1.0, 1.0, 1.0), 0.2)
                .twist(0.5)
        },
        |path| match load(&path) {
            Ok(tree) => tree.root,
            Err(e) => {
                eprintln!("Load error: {e}");
                std::process::exit(1);
            }
        },
    );

    println!("=== Interpreter Benchmark (JIT not enabled) ===");
    println!("Hint: Build with --features jit for 20x+ speedup");
    println!("Points: {}", points);
    println!("Node count: {}", node.node_count());

    // Generate random points
    let test_points: Vec<Vec3> = (0..points)
        .map(|i| {
            let t = i as f32 / points as f32;
            Vec3::new(
                (t * 123.456).sin() * 2.0,
                (t * 234.567).sin() * 2.0,
                (t * 345.678).sin() * 2.0,
            )
        })
        .collect();

    // Benchmark
    let start = std::time::Instant::now();
    let _results = eval_batch_parallel(&node, &test_points);
    let elapsed = start.elapsed();

    let points_per_sec = points as f64 / elapsed.as_secs_f64();

    println!("\n--------------------------------------------------");
    println!("Time       : {:.3} ms", elapsed.as_secs_f64() * 1000.0);
    println!(
        "Throughput : {:.2} M points/sec",
        points_per_sec / 1_000_000.0
    );
    println!("--------------------------------------------------");
}

#[cfg(all(feature = "cli", feature = "texture-fit"))]
fn cmd_texture_fit(
    input: PathBuf,
    octaves: u32,
    target_psnr: f32,
    iterations: u32,
    output: Option<PathBuf>,
    shader: Option<String>,
    shader_output: Option<PathBuf>,
) {
    use alice_sdf::texture::{fit_texture, generate_shader, ShaderLanguage, TextureFitConfig};

    let config = TextureFitConfig {
        max_octaves: octaves,
        target_psnr_db: target_psnr,
        iterations_per_octave: iterations,
        tileable: true,
    };

    println!("Fitting texture: {}", input.display());
    println!("  Max octaves: {}", octaves);
    println!("  Target PSNR: {:.1} dB", target_psnr);
    println!("  Iterations/octave: {}", iterations);

    let start = std::time::Instant::now();
    let result = match fit_texture(&input, &config) {
        Ok(r) => r,
        Err(e) => {
            eprintln!("Error: {}", e);
            std::process::exit(1);
        }
    };
    let elapsed = start.elapsed();

    let total_octaves: usize = result.octaves.iter().map(std::vec::Vec::len).sum();
    println!("\nResult:");
    println!("  Image: {}x{}", result.width, result.height);
    println!("  Octaves: {}", total_octaves);
    println!("  PSNR: {:.2} dB", result.psnr_db);
    println!("  NMSE: {:.4}", result.nmse);
    println!("  Time: {:.3}s", elapsed.as_secs_f64());

    // Save JSON
    if let Some(out_path) = output {
        let json = serde_json::to_string_pretty(&result).unwrap();
        match std::fs::write(&out_path, json) {
            Ok(_) => println!("  Saved parameters to {}", out_path.display()),
            Err(e) => eprintln!("  Failed to save JSON: {}", e),
        }
    }

    // Generate shader
    if let Some(lang_str) = shader {
        let lang: ShaderLanguage = match lang_str.parse() {
            Ok(l) => l,
            Err(e) => {
                eprintln!("Error: {}", e);
                std::process::exit(1);
            }
        };

        let source_name = input.file_name().map_or_else(
            || "unknown".to_string(),
            |n| n.to_string_lossy().to_string(),
        );

        let shader_code = generate_shader(&result, lang, &source_name);

        if let Some(shader_path) = shader_output {
            match std::fs::write(&shader_path, &shader_code) {
                Ok(_) => println!("  Saved shader to {}", shader_path.display()),
                Err(e) => eprintln!("  Failed to save shader: {}", e),
            }
        } else {
            println!("\n--- Generated Shader ({:?}) ---", lang);
            println!("{}", shader_code);
        }
    }
}