primal-barracuda 0.4.0

barraCuda — BARrier-free Rust Abstracted Cross-platform Unified Dimensional Algebra. Vendor-agnostic GPU compute via WGSL.
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
// SPDX-License-Identifier: AGPL-3.0-or-later

//! GPU-accelerated 2D Perlin noise and fBm (Fractal Brownian Motion).
//!
//! Computes coherent noise for batch coordinate arrays on the GPU.
//! The standard 256-entry Perlin permutation table is uploaded once as
//! a storage buffer; each thread evaluates noise for one (x, y) pair.
//!
//! # Provenance
//!
//! Absorbed from ludoSpring V2 CPU reference.
//! - Perlin, K. (1985). "An image synthesizer." SIGGRAPH '85.
//! - Perlin, K. (2002). "Improving noise." SIGGRAPH '02.
//! - Gustavson, S. (2005). "Simplex noise demystified."

use crate::device::WgpuDevice;
use crate::device::capabilities::WORKGROUP_SIZE_1D;
use crate::error::Result;
use bytemuck::{Pod, Zeroable};
use std::sync::Arc;

const PERLIN_SHADER: &str = include_str!("../../shaders/procedural/perlin_2d_f64.wgsl");
const PERLIN_F32_SHADER: &str = include_str!("../../shaders/procedural/perlin_2d_f32.wgsl");
const FBM_SHADER: &str = include_str!("../../shaders/procedural/fbm_2d_f64.wgsl");

/// Standard Perlin permutation table (Perlin 2002).
const PERM_TABLE: [u32; 256] = [
    151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69,
    142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219,
    203, 117, 35, 11, 32, 57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175,
    74, 165, 71, 134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230,
    220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76,
    132, 187, 208, 89, 18, 169, 200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173,
    186, 3, 64, 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206,
    59, 227, 47, 16, 58, 17, 182, 189, 28, 42, 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163,
    70, 221, 153, 101, 155, 167, 43, 172, 9, 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232,
    178, 185, 112, 104, 218, 246, 97, 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162,
    241, 81, 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204,
    176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141,
    128, 195, 78, 66, 215, 61, 156, 180,
];

#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable)]
struct PerlinParams {
    n_points: u32,
    _pad0: u32,
    _pad1: u32,
    _pad2: u32,
}

#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable)]
struct FbmParams {
    n_points: u32,
    octaves: u32,
    _pad0: u32,
    _pad1: u32,
    lacunarity: f64,
    persistence: f64,
}

/// GPU-accelerated batch 2D Perlin noise.
pub struct PerlinNoiseGpu {
    device: Arc<WgpuDevice>,
}

impl PerlinNoiseGpu {
    /// Create a new Perlin noise compute instance.
    #[must_use]
    pub fn new(device: Arc<WgpuDevice>) -> Self {
        Self { device }
    }

    /// Compute Perlin 2D noise for a batch of (x, y) coordinate pairs.
    ///
    /// `coords` must have even length: `[x0, y0, x1, y1, ...]`.
    /// Returns one f64 per coordinate pair, approximately in \[-1, 1\].
    ///
    /// # Errors
    ///
    /// Returns [`Err`] on GPU dispatch or buffer mapping failure.
    pub fn perlin_2d(&self, coords: &[f64]) -> Result<Vec<f64>> {
        let n_points = coords.len() / 2;
        if n_points == 0 {
            return Ok(vec![]);
        }

        let params = PerlinParams {
            n_points: crate::error::u32_from_usize(n_points)?,
            _pad0: 0,
            _pad1: 0,
            _pad2: 0,
        };

        let wg_count = params.n_points.div_ceil(WORKGROUP_SIZE_1D);

        let coord_buf = self.device.create_buffer_f64_init("perlin:coords", coords);
        let out_buf = self.device.create_buffer_f64(n_points)?;
        let perm_buf = self
            .device
            .create_buffer_u32_init("perlin:perm", &PERM_TABLE);
        let params_buf = self.device.create_uniform_buffer("perlin:params", &params);

        crate::device::compute_pipeline::ComputeDispatch::new(&self.device, "perlin_2d")
            .shader(PERLIN_SHADER, "main")
            .f64()
            .storage_read(0, &coord_buf)
            .storage_rw(1, &out_buf)
            .storage_read(2, &perm_buf)
            .uniform(3, &params_buf)
            .dispatch(wg_count, 1, 1)
            .submit()?;

        self.device.read_f64_buffer(&out_buf, n_points)
    }

    /// Compute fBm (Fractal Brownian Motion) for a batch of (x, y) pairs.
    ///
    /// `coords` must have even length: `[x0, y0, x1, y1, ...]`.
    /// Returns one f64 per coordinate pair, normalized to approximately \[-1, 1\].
    ///
    /// # Errors
    ///
    /// Returns [`Err`] on GPU dispatch or buffer mapping failure.
    pub fn fbm_2d(
        &self,
        coords: &[f64],
        octaves: u32,
        lacunarity: f64,
        persistence: f64,
    ) -> Result<Vec<f64>> {
        let n_points = coords.len() / 2;
        if n_points == 0 {
            return Ok(vec![]);
        }

        let params = FbmParams {
            n_points: crate::error::u32_from_usize(n_points)?,
            octaves,
            _pad0: 0,
            _pad1: 0,
            lacunarity,
            persistence,
        };

        let wg_count = params.n_points.div_ceil(WORKGROUP_SIZE_1D);

        let coord_buf = self.device.create_buffer_f64_init("fbm:coords", coords);
        let out_buf = self.device.create_buffer_f64(n_points)?;
        let perm_buf = self.device.create_buffer_u32_init("fbm:perm", &PERM_TABLE);
        let params_buf = self.device.create_uniform_buffer("fbm:params", &params);

        crate::device::compute_pipeline::ComputeDispatch::new(&self.device, "fbm_2d")
            .shader(FBM_SHADER, "main")
            .f64()
            .storage_read(0, &coord_buf)
            .storage_rw(1, &out_buf)
            .storage_read(2, &perm_buf)
            .uniform(3, &params_buf)
            .dispatch(wg_count, 1, 1)
            .submit()?;

        self.device.read_f64_buffer(&out_buf, n_points)
    }
}

/// GPU-accelerated batch 2D Perlin noise (f32 variant).
///
/// Identical algorithm to [`PerlinNoiseGpu`] but operates on f32 data.
/// Does not require the `f64` GPU extension — runs on all WebGPU devices.
/// Designed for game/real-time use (ludoSpring).
pub struct PerlinNoiseGpuF32 {
    device: Arc<WgpuDevice>,
}

impl PerlinNoiseGpuF32 {
    /// Create a new f32 Perlin noise compute instance.
    #[must_use]
    pub fn new(device: Arc<WgpuDevice>) -> Self {
        Self { device }
    }

    /// Compute Perlin 2D noise for a batch of (x, y) coordinate pairs (f32).
    ///
    /// `coords` must have even length: `[x0, y0, x1, y1, ...]`.
    /// Returns one f32 per coordinate pair, approximately in \[-1, 1\].
    ///
    /// # Errors
    ///
    /// Returns [`Err`] on GPU dispatch or buffer mapping failure.
    pub fn perlin_2d(&self, coords: &[f32]) -> Result<Vec<f32>> {
        let n_points = coords.len() / 2;
        if n_points == 0 {
            return Ok(vec![]);
        }

        let params = PerlinParams {
            n_points: crate::error::u32_from_usize(n_points)?,
            _pad0: 0,
            _pad1: 0,
            _pad2: 0,
        };

        let wg_count = params.n_points.div_ceil(WORKGROUP_SIZE_1D);

        let coord_buf = self
            .device
            .create_buffer_f32_init("perlin_f32:coords", coords);
        let out_buf = self.device.create_buffer_f32(n_points)?;
        let perm_buf = self
            .device
            .create_buffer_u32_init("perlin_f32:perm", &PERM_TABLE);
        let params_buf = self
            .device
            .create_uniform_buffer("perlin_f32:params", &params);

        crate::device::compute_pipeline::ComputeDispatch::new(&self.device, "perlin_2d_f32")
            .shader(PERLIN_F32_SHADER, "main")
            .storage_read(0, &coord_buf)
            .storage_rw(1, &out_buf)
            .storage_read(2, &perm_buf)
            .uniform(3, &params_buf)
            .dispatch(wg_count, 1, 1)
            .submit()?;

        self.device.read_buffer_f32(&out_buf, n_points)
    }
}

/// Floor-and-mask to permutation table index (0..=255).
#[expect(
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    reason = "floor + mask to 0..255 guarantees valid usize index"
)]
fn perm_index(v: f64) -> usize {
    v.floor() as usize & 255
}

/// f32 variant of [`perm_index`].
#[expect(
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    reason = "floor + mask to 0..255 guarantees valid usize index"
)]
fn perm_index_f32(v: f32) -> usize {
    v.floor() as usize & 255
}

/// CPU reference: 2D Perlin noise (f32). Returns value in approximately \[-1, 1\].
///
/// Matches the f32 GPU shader output for cross-validation.
#[must_use]
pub fn perlin_2d_cpu_f32(x: f32, y: f32) -> f32 {
    fn fade(t: f32) -> f32 {
        t * t * t * t.mul_add(t.mul_add(6.0, -15.0), 10.0)
    }
    fn lerp(a: f32, b: f32, t: f32) -> f32 {
        t.mul_add(b - a, a)
    }
    fn grad2(hash: u32, x: f32, y: f32) -> f32 {
        match hash & 3 {
            0 => x + y,
            1 => -x + y,
            2 => x - y,
            _ => -x - y,
        }
    }

    let xi = perm_index_f32(x);
    let yi = perm_index_f32(y);
    let xf = x - x.floor();
    let yf = y - y.floor();

    let u = fade(xf);
    let v = fade(yf);

    let aa = PERM_TABLE[(PERM_TABLE[xi] as usize + yi) & 255];
    let ab = PERM_TABLE[(PERM_TABLE[xi] as usize + yi + 1) & 255];
    let ba = PERM_TABLE[(PERM_TABLE[(xi + 1) & 255] as usize + yi) & 255];
    let bb = PERM_TABLE[(PERM_TABLE[(xi + 1) & 255] as usize + yi + 1) & 255];

    lerp(
        lerp(grad2(aa, xf, yf), grad2(ba, xf - 1.0, yf), u),
        lerp(grad2(ab, xf, yf - 1.0), grad2(bb, xf - 1.0, yf - 1.0), u),
        v,
    )
}

/// CPU reference: 2D Perlin noise. Returns value in approximately \[-1, 1\].
///
/// Matches the GPU shader output for cross-validation.
#[must_use]
pub fn perlin_2d_cpu(x: f64, y: f64) -> f64 {
    fn fade(t: f64) -> f64 {
        t * t * t * t.mul_add(t.mul_add(6.0, -15.0), 10.0)
    }
    fn lerp(a: f64, b: f64, t: f64) -> f64 {
        t.mul_add(b - a, a)
    }
    fn grad2(hash: u32, x: f64, y: f64) -> f64 {
        match hash & 3 {
            0 => x + y,
            1 => -x + y,
            2 => x - y,
            _ => -x - y,
        }
    }

    let xi = perm_index(x);
    let yi = perm_index(y);
    let xf = x - x.floor();
    let yf = y - y.floor();

    let u = fade(xf);
    let v = fade(yf);

    let aa = PERM_TABLE[(PERM_TABLE[xi] as usize + yi) & 255];
    let ab = PERM_TABLE[(PERM_TABLE[xi] as usize + yi + 1) & 255];
    let ba = PERM_TABLE[(PERM_TABLE[(xi + 1) & 255] as usize + yi) & 255];
    let bb = PERM_TABLE[(PERM_TABLE[(xi + 1) & 255] as usize + yi + 1) & 255];

    lerp(
        lerp(grad2(aa, xf, yf), grad2(ba, xf - 1.0, yf), u),
        lerp(grad2(ab, xf, yf - 1.0), grad2(bb, xf - 1.0, yf - 1.0), u),
        v,
    )
}

/// CPU reference: 3D Perlin noise. Returns value in approximately \[-1, 1\].
///
/// Classic Perlin (2002) with 3D gradient vectors, trilinear interpolation,
/// and quintic fade. Guarantees zero at all integer lattice points.
///
/// # References
///
/// - Perlin, K. (2002). "Improving noise." SIGGRAPH '02.
#[must_use]
pub fn perlin_3d_cpu(x: f64, y: f64, z: f64) -> f64 {
    fn fade(t: f64) -> f64 {
        t * t * t * t.mul_add(t.mul_add(6.0, -15.0), 10.0)
    }
    fn lerp(a: f64, b: f64, t: f64) -> f64 {
        t.mul_add(b - a, a)
    }
    fn grad3(hash: u32, x: f64, y: f64, z: f64) -> f64 {
        match hash & 15 {
            0 => x + y,
            1 => -x + y,
            2 => x - y,
            3 => -x - y,
            4 => x + z,
            5 => -x + z,
            6 => x - z,
            7 => -x - z,
            8 => y + z,
            9 => -y + z,
            10 => y - z,
            11 => -y - z,
            12 => y + x,
            13 => -y + z,
            14 => y - x,
            _ => -y - z,
        }
    }

    let xi = perm_index(x);
    let yi = perm_index(y);
    let zi = perm_index(z);
    let xf = x - x.floor();
    let yf = y - y.floor();
    let zf = z - z.floor();

    let u = fade(xf);
    let v = fade(yf);
    let w = fade(zf);

    let p = &PERM_TABLE;
    let a = p[xi] as usize + yi;
    let aa = p[a & 255] as usize + zi;
    let ab = p[(a + 1) & 255] as usize + zi;
    let b = p[(xi + 1) & 255] as usize + yi;
    let ba = p[b & 255] as usize + zi;
    let bb = p[(b + 1) & 255] as usize + zi;

    lerp(
        lerp(
            lerp(
                grad3(p[aa & 255], xf, yf, zf),
                grad3(p[ba & 255], xf - 1.0, yf, zf),
                u,
            ),
            lerp(
                grad3(p[ab & 255], xf, yf - 1.0, zf),
                grad3(p[bb & 255], xf - 1.0, yf - 1.0, zf),
                u,
            ),
            v,
        ),
        lerp(
            lerp(
                grad3(p[(aa + 1) & 255], xf, yf, zf - 1.0),
                grad3(p[(ba + 1) & 255], xf - 1.0, yf, zf - 1.0),
                u,
            ),
            lerp(
                grad3(p[(ab + 1) & 255], xf, yf - 1.0, zf - 1.0),
                grad3(p[(bb + 1) & 255], xf - 1.0, yf - 1.0, zf - 1.0),
                u,
            ),
            v,
        ),
        w,
    )
}

/// CPU reference: 2D fBm using Perlin noise.
#[must_use]
pub fn fbm_2d_cpu(x: f64, y: f64, octaves: u32, lacunarity: f64, persistence: f64) -> f64 {
    let mut value = 0.0;
    let mut amplitude = 1.0;
    let mut frequency = 1.0;
    let mut max_value = 0.0;

    for _ in 0..octaves {
        value += perlin_2d_cpu(x * frequency, y * frequency) * amplitude;
        max_value += amplitude;
        amplitude *= persistence;
        frequency *= lacunarity;
    }

    value / max_value
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn perlin_2d_cpu_in_range() {
        for i in 0..50_i32 {
            for j in 0..50_i32 {
                let v = perlin_2d_cpu(f64::from(i) * 0.1, f64::from(j) * 0.1);
                assert!((-2.0..=2.0).contains(&v), "value {v} out of range");
            }
        }
    }

    #[test]
    fn perlin_at_integer_coords_is_zero() {
        let v = perlin_2d_cpu(1.0, 1.0);
        assert!(
            v.abs() < 1e-10,
            "perlin at integer coords should be ~0, got {v}"
        );
    }

    #[test]
    fn fbm_is_coherent() {
        let a = fbm_2d_cpu(1.0, 1.0, 4, 2.0, 0.5);
        let b = fbm_2d_cpu(1.001, 1.001, 4, 2.0, 0.5);
        assert!((a - b).abs() < 0.1, "nearby samples should be similar");
    }

    #[test]
    fn fbm_normalizes_output() {
        let v = fbm_2d_cpu(0.5, 0.5, 8, 2.0, 0.5);
        assert!(
            (-2.0..=2.0).contains(&v),
            "fbm output {v} should be bounded"
        );
    }

    #[test]
    fn params_layout_perlin() {
        assert_eq!(std::mem::size_of::<PerlinParams>(), 16);
    }

    #[test]
    fn params_layout_fbm() {
        assert_eq!(std::mem::size_of::<FbmParams>(), 32);
    }

    #[test]
    fn shader_source_valid() {
        assert!(PERLIN_SHADER.contains("perlin"));
        assert!(PERLIN_SHADER.contains("perm_lookup"));
        assert!(FBM_SHADER.contains("octaves"));
        assert!(FBM_SHADER.contains("perlin_2d"));
    }

    #[test]
    fn perm_table_contains_all_values() {
        let mut seen = [false; 256];
        for &v in &PERM_TABLE {
            assert!(v < 256, "perm table value {v} >= 256");
            seen[v as usize] = true;
        }
        for (i, &s) in seen.iter().enumerate() {
            assert!(s, "perm table missing value {i}");
        }
    }

    #[test]
    fn perlin_3d_cpu_in_range() {
        for i in 0..10_i32 {
            for j in 0..10_i32 {
                for k in 0..10_i32 {
                    let v =
                        perlin_3d_cpu(f64::from(i) * 0.3, f64::from(j) * 0.3, f64::from(k) * 0.3);
                    assert!((-2.0..=2.0).contains(&v), "3D value {v} out of range");
                }
            }
        }
    }

    #[test]
    fn perlin_3d_at_integer_coords_is_zero() {
        for x in 0..5_i32 {
            for y in 0..5_i32 {
                for z in 0..5_i32 {
                    let v = perlin_3d_cpu(f64::from(x), f64::from(y), f64::from(z));
                    assert!(
                        v.abs() < 1e-10,
                        "3D perlin at integer ({x},{y},{z}) should be ~0, got {v}"
                    );
                }
            }
        }
    }

    #[test]
    fn perlin_3d_is_coherent() {
        let a = perlin_3d_cpu(0.5, 0.5, 0.5);
        let b = perlin_3d_cpu(0.501, 0.501, 0.501);
        assert!((a - b).abs() < 0.1, "nearby 3D samples should be similar");
    }

    #[test]
    fn perlin_2d_f32_in_range() {
        for i in 0..50_i32 {
            for j in 0..50_i32 {
                let v = perlin_2d_cpu_f32(i as f32 * 0.1, j as f32 * 0.1);
                assert!((-2.0..=2.0).contains(&v), "f32 value {v} out of range");
            }
        }
    }

    #[test]
    fn perlin_2d_f32_at_integer_is_zero() {
        let v = perlin_2d_cpu_f32(1.0, 1.0);
        assert!(
            v.abs() < 1e-5,
            "f32 perlin at integer coords should be ~0, got {v}"
        );
    }

    #[test]
    fn perlin_2d_f32_matches_f64() {
        for i in 0..20_i32 {
            let x = (i as f64).mul_add(0.37, 0.1);
            let y = (i as f64).mul_add(0.41, 0.2);
            let v64 = perlin_2d_cpu(x, y);
            let v32 = perlin_2d_cpu_f32(x as f32, y as f32);
            assert!(
                (v64 as f32 - v32).abs() < 0.01,
                "f32/f64 mismatch at ({x},{y}): f64={v64}, f32={v32}"
            );
        }
    }

    #[test]
    fn f32_shader_source_valid() {
        assert!(PERLIN_F32_SHADER.contains("perlin"));
        assert!(PERLIN_F32_SHADER.contains("perm_lookup"));
        assert!(
            !PERLIN_F32_SHADER.contains("enable f64;"),
            "f32 shader must not use the f64 WGSL extension"
        );
    }
}