darkly 0.5.0

A GPU-native paint engine on wgpu: brushes, layers, blend modes, masks, selections, and undo.
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
//! Noise void — domain-warped fractional Brownian motion.
//!
//! The cloudy / marbled / lightning-like procedural field described in the
//! README as the "de-facto Void". Pure-GPU: the shader generates the layer's
//! pixels from a handful of uniforms; nothing is stored on disk except the
//! params themselves.
//!
//! The FBM primitive lives in `shaders/lib/fbm.wgsl` and is concatenated
//! ahead of this void's fragment shader at pipeline-creation time. A future
//! warp veil will reuse the same helper as a displacement map.

use crate::gpu::effect::{
    create_blit_bind_group, create_blit_pipeline, EffectCache, EffectPipeline,
};
use crate::gpu::void::{DirtyFlag, ParamDef, ParamValue, Void, VoidRegistration};
use std::cell::Cell;
use std::sync::Arc;

/// Procedural-render downscale factor. The FBM shader runs into an aux
/// texture sized at `canvas / AUX_DOWNSCALE` (floored at 64) and a bilinear
/// blit pass upsamples to the void's destination. At default `size = 200`
/// FBM features are ~200 canvas pixels wide, so half-resolution is
/// visually indistinguishable while quartering the per-pixel cost of the
/// expensive 3D-FBM shader.
const AUX_DOWNSCALE: u32 = 2;
const AUX_MIN_DIM: u32 = 64;
const AUX_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8Unorm;

/// Side length of the 3D noise volume sampled by `fbm_value_noise3` in the
/// shader. 64³ × 4 bytes ≈ 1 MiB per void instance; small enough to live
/// in L2 cache on most GPUs, large enough that the FBM domain (which scales
/// up to ~16× the base coordinate at 5 octaves) crosses many texture
/// periods per pixel, masking any cycling pattern.
const NOISE3D_DIM: u32 = 64;

pub const TYPE_ID: &str = "noise";

const PARAMS: &[ParamDef] = &[
    // Seed indexes the procedural field — every integer produces a different
    // noise pattern, so a randomize button (or just typing a number) gives
    // the "infinite combinations of entropy" the README promises.
    ParamDef::Int {
        name: "seed",
        min: 0,
        max: i32::MAX,
        default: 42,
    },
    // Octave count of the underlying FBM. More octaves = more detail; cost
    // scales linearly. 5 is a good cloud-like default.
    ParamDef::Int {
        name: "octaves",
        min: 1,
        max: 8,
        default: 5,
    },
    // Feature size in canvas pixels. Higher = larger blobs; lower =
    // grainier. The default is tuned for 1k–2k canvases producing visible
    // cloud structure without going either flat or noisy. Converted to
    // a frequency multiplier (1 / size) at uniform-write time.
    ParamDef::Float {
        name: "size",
        min: 20.0,
        max: 2000.0,
        default: 200.0,
    },
    // Domain-warp strength. 0 = pure FBM, increasing values produce more
    // marbled / swirly deformation per Quilez's warp.
    ParamDef::Float {
        name: "warp",
        min: 0.0,
        max: 3.0,
        default: 1.5,
    },
    // Darkness / tonal contrast. Applied as `pow(value, 1.0 + darkness)`
    // in the shader. 0 = linear (washed-out grayscale); higher values
    // push midtones toward black, giving a Watery-style deep base with
    // brighter peaks. Range tuned so the default looks like a moodier
    // cloud field, not a flat gray ramp.
    ParamDef::Float {
        name: "darkness",
        min: 0.0,
        max: 3.0,
        default: 1.0,
    },
    // Time slider — z-coordinate into the 3D noise volume. Each value
    // produces a different cross-section of the same FBM field; scrub to
    // explore variations of the current seed without changing pattern
    // identity. Range chosen so the full slider covers many full noise-cell
    // crossings at the default Z scale (Z_SCALE = 0.15 in the shader).
    ParamDef::Float {
        name: "time",
        min: 0.0,
        max: 100.0,
        default: 0.0,
    },
];

pub fn register() -> VoidRegistration {
    VoidRegistration {
        type_id: TYPE_ID,
        display_name: "Noise",
        params: PARAMS,
        icon: "tabler:galaxy",
        supports_preview: true,
        supports_live_transform: true,
        // Purely procedural — no external capture, identity seed transform.
        capture_kind: None,
        default_transform: |_, _| crate::transform::Transform::identity(),
        create_pipeline,
        from_params: |params, shared| Box::new(Noise::from_params(params, shared)),
    }
}

#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct NoiseUniforms {
    seed: u32,
    octaves: i32,
    frequency: f32,
    warp: f32,
    darkness: f32,
    time: f32,
    // Multiplier from render-target px → canvas px (the aux buffer may be
    // smaller than the canvas). Baked at `create_cache` and cached on the
    // struct, so every uniform write rebuilds the whole struct from state.
    canvas_scale: f32,
    _pad0: f32,
    // Inverse of the user transform's affine — applied to the canvas-space
    // sampling coordinate so the field pans / scales / rotates with the gizmo.
    inv_row0: [f32; 4],
    inv_row1: [f32; 4],
}

#[derive(Debug)]
pub struct Noise {
    pub seed: i32,
    pub octaves: i32,
    /// Feature size in canvas pixels. Converted to a frequency multiplier
    /// (`1.0 / size`) when written to the GPU uniform.
    pub size: f32,
    pub warp: f32,
    pub darkness: f32,
    /// Z-axis offset into the 3D noise volume. User-controlled slider; a
    /// scrub control for exploring different cross-sections of the field
    /// without changing the seed.
    pub time: f32,
    /// User transform (gizmo affine). The shader samples the field through its
    /// inverse, so the noise pattern pans / scales / rotates under the gizmo.
    transform: crate::transform::Transform,
    /// Render-target→canvas px scale, baked at `create_cache`. `Cell` because
    /// the trait hands `&self` there; cached so uniform writes need no extra
    /// argument and never clobber it.
    canvas_scale: Cell<f32>,
    shared: Arc<EffectPipeline>,
    dirty: DirtyFlag,
}

impl Clone for Noise {
    fn clone(&self) -> Self {
        // Clones come up via `clone_boxed` (used by undo / clone_subtree).
        // The clone owns a fresh `EffectCache` from `ensure_void_layer`, so
        // start dirty to force a first encode.
        Noise {
            seed: self.seed,
            octaves: self.octaves,
            size: self.size,
            warp: self.warp,
            darkness: self.darkness,
            time: self.time,
            transform: self.transform,
            canvas_scale: Cell::new(self.canvas_scale.get()),
            shared: self.shared.clone(),
            dirty: DirtyFlag::new_dirty(),
        }
    }
}

impl Noise {
    fn from_params(params: &[ParamValue], shared: Arc<EffectPipeline>) -> Self {
        let seed = match params.first() {
            Some(ParamValue::Int(v)) => *v,
            _ => 42,
        };
        let octaves = match params.get(1) {
            Some(ParamValue::Int(v)) => *v,
            _ => 5,
        };
        let size = match params.get(2) {
            Some(ParamValue::Float(v)) => *v,
            _ => 200.0,
        };
        let warp = match params.get(3) {
            Some(ParamValue::Float(v)) => *v,
            _ => 1.5,
        };
        let darkness = match params.get(4) {
            Some(ParamValue::Float(v)) => *v,
            _ => 1.0,
        };
        let time = match params.get(5) {
            Some(ParamValue::Float(v)) => *v,
            _ => 0.0,
        };
        Noise {
            seed,
            octaves,
            size,
            warp,
            darkness,
            time,
            transform: crate::transform::Transform::identity(),
            canvas_scale: Cell::new(1.0),
            shared,
            dirty: DirtyFlag::new_dirty(),
        }
    }

    fn uniforms(&self) -> NoiseUniforms {
        let fwd = self.transform.to_affine();
        let inv = crate::transform::affine_inverse(&fwd).unwrap_or(crate::transform::IDENTITY);
        NoiseUniforms {
            seed: self.seed as u32,
            octaves: self.octaves,
            frequency: 1.0 / self.size.max(1.0),
            warp: self.warp,
            darkness: self.darkness,
            time: self.time,
            canvas_scale: self.canvas_scale.get(),
            _pad0: 0.0,
            inv_row0: [inv[0], inv[1], inv[2], 0.0],
            inv_row1: [inv[3], inv[4], inv[5], 0.0],
        }
    }
}

impl Void for Noise {
    fn type_id(&self) -> &'static str {
        TYPE_ID
    }

    fn clone_boxed(&self) -> Box<dyn Void> {
        Box::new(self.clone())
    }

    fn param_values(&self) -> Vec<ParamValue> {
        vec![
            ParamValue::Int(self.seed),
            ParamValue::Int(self.octaves),
            ParamValue::Float(self.size),
            ParamValue::Float(self.warp),
            ParamValue::Float(self.darkness),
            ParamValue::Float(self.time),
        ]
    }

    fn take_dirty(&mut self) -> bool {
        self.dirty.take()
    }

    fn mark_dirty(&mut self) {
        self.dirty.mark();
    }

    fn update_params(&mut self, queue: &wgpu::Queue, cache: &EffectCache, params: &[ParamValue]) {
        self.seed = match params.first() {
            Some(ParamValue::Int(v)) => *v,
            _ => self.seed,
        };
        self.octaves = match params.get(1) {
            Some(ParamValue::Int(v)) => *v,
            _ => self.octaves,
        };
        self.size = match params.get(2) {
            Some(ParamValue::Float(v)) => *v,
            _ => self.size,
        };
        self.warp = match params.get(3) {
            Some(ParamValue::Float(v)) => *v,
            _ => self.warp,
        };
        self.darkness = match params.get(4) {
            Some(ParamValue::Float(v)) => *v,
            _ => self.darkness,
        };
        self.time = match params.get(5) {
            Some(ParamValue::Float(v)) => *v,
            _ => self.time,
        };
        // Full write — `canvas_scale` is cached on the struct, so rebuilding
        // the whole uniform can't clobber it.
        if let Some(buf) = cache.uniform_bufs.first() {
            queue.write_buffer(buf, 0, bytemuck::bytes_of(&self.uniforms()));
        }
        self.dirty.mark();
    }

    fn set_transform(
        &mut self,
        queue: &wgpu::Queue,
        cache: &EffectCache,
        transform: &crate::transform::Transform,
    ) {
        self.transform = *transform;
        if let Some(buf) = cache.uniform_bufs.first() {
            queue.write_buffer(buf, 0, bytemuck::bytes_of(&self.uniforms()));
        }
        self.dirty.mark();
    }

    fn create_cache(
        &self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        _dst_view: &wgpu::TextureView,
        sampler: &wgpu::Sampler,
        render_width: u32,
        render_height: u32,
    ) -> EffectCache {
        let aux_w = (render_width / AUX_DOWNSCALE).max(AUX_MIN_DIM);
        let aux_h = (render_height / AUX_DOWNSCALE).max(AUX_MIN_DIM);
        self.canvas_scale.set(render_width as f32 / aux_w as f32);

        let uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("void-noise-uniforms"),
            size: std::mem::size_of::<NoiseUniforms>() as u64,
            usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        queue.write_buffer(&uniform_buf, 0, bytemuck::bytes_of(&self.uniforms()));

        // 3D noise volume: filled with PCG-hashed bytes, sampled with
        // hardware trilinear filtering by `fbm_value_noise3`. One volume
        // per void instance (~1 MiB at 64³ RGBA8).
        let noise_tex = device.create_texture(&wgpu::TextureDescriptor {
            label: Some("void-noise-volume"),
            size: wgpu::Extent3d {
                width: NOISE3D_DIM,
                height: NOISE3D_DIM,
                depth_or_array_layers: NOISE3D_DIM,
            },
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D3,
            format: wgpu::TextureFormat::Rgba8Unorm,
            usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
            view_formats: &[],
        });
        let noise_bytes = seed_noise_volume(NOISE3D_DIM, self.seed as u32);
        queue.write_texture(
            wgpu::TexelCopyTextureInfo {
                texture: &noise_tex,
                mip_level: 0,
                origin: wgpu::Origin3d::ZERO,
                aspect: wgpu::TextureAspect::All,
            },
            &noise_bytes,
            wgpu::TexelCopyBufferLayout {
                offset: 0,
                bytes_per_row: Some(NOISE3D_DIM * 4),
                rows_per_image: Some(NOISE3D_DIM),
            },
            wgpu::Extent3d {
                width: NOISE3D_DIM,
                height: NOISE3D_DIM,
                depth_or_array_layers: NOISE3D_DIM,
            },
        );
        let noise_view = noise_tex.create_view(&Default::default());

        // Dedicated sampler with Repeat addressing so the seed-offset
        // wrap in the shader works cleanly. Linear filter gives hardware
        // trilinear interpolation.
        let noise_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
            label: Some("void-noise-volume-sampler"),
            address_mode_u: wgpu::AddressMode::Repeat,
            address_mode_v: wgpu::AddressMode::Repeat,
            address_mode_w: wgpu::AddressMode::Repeat,
            mag_filter: wgpu::FilterMode::Linear,
            min_filter: wgpu::FilterMode::Linear,
            mipmap_filter: wgpu::MipmapFilterMode::Nearest,
            ..Default::default()
        });

        // FBM-pass bind groups. Layout from the shared pipeline:
        //   binding 0: uniform buffer
        //   binding 1: 3D noise texture
        //   binding 2: noise sampler
        // Duplicated to keep the [BindGroup; 2] cache shape — voids don't
        // ping-pong but the cache layout is shared with veils.
        let fbm_bg = |label: &str| {
            device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some(label),
                layout: &self.shared.bind_group_layout,
                entries: &[
                    wgpu::BindGroupEntry {
                        binding: 0,
                        resource: uniform_buf.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 1,
                        resource: wgpu::BindingResource::TextureView(&noise_view),
                    },
                    wgpu::BindGroupEntry {
                        binding: 2,
                        resource: wgpu::BindingResource::Sampler(&noise_sampler),
                    },
                ],
            })
        };
        let fbm_bgs = [fbm_bg("void-noise-fbm-bg-0"), fbm_bg("void-noise-fbm-bg-1")];

        // Aux texture: the FBM shader renders here, then a bilinear blit
        // pass upsamples to the void's destination. Format matches the
        // void's destination so a single blit pipeline serves both.
        let aux_tex = device.create_texture(&wgpu::TextureDescriptor {
            label: Some("void-noise-aux"),
            size: wgpu::Extent3d {
                width: aux_w,
                height: aux_h,
                depth_or_array_layers: 1,
            },
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            format: AUX_FORMAT,
            usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::RENDER_ATTACHMENT,
            view_formats: &[],
        });
        let aux_view = aux_tex.create_view(&Default::default());

        let blit = create_blit_pipeline(device, AUX_FORMAT, "void-noise-blit");
        let blit_bg = |label: &str| {
            create_blit_bind_group(device, &blit.bind_group_layout, &aux_view, sampler, label)
        };
        let blit_bgs = [
            blit_bg("void-noise-blit-bg-0"),
            blit_bg("void-noise-blit-bg-1"),
        ];

        EffectCache {
            uniform_bufs: vec![uniform_buf],
            bind_groups: vec![fbm_bgs, blit_bgs],
            aux_textures: vec![aux_tex],
            aux_views: vec![aux_view],
            aux_pipelines: vec![blit.pipeline],
        }
    }

    fn encode(
        &self,
        encoder: &mut wgpu::CommandEncoder,
        cache: &EffectCache,
        dst_view: &wgpu::TextureView,
    ) {
        // Pass 1: 3D-FBM into the low-res aux texture.
        {
            let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                label: Some("void-noise-fbm"),
                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                    view: &cache.aux_views[0],
                    resolve_target: None,
                    depth_slice: None,
                    ops: wgpu::Operations {
                        load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
                        store: wgpu::StoreOp::Store,
                    },
                })],
                ..Default::default()
            });
            rpass.set_pipeline(&self.shared.pipeline);
            rpass.set_bind_group(0, &cache.bind_groups[0][0], &[]);
            rpass.draw(0..3, 0..1);
        }
        // Pass 2: bilinear blit aux → destination.
        {
            let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                label: Some("void-noise-blit"),
                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                    view: dst_view,
                    resolve_target: None,
                    depth_slice: None,
                    ops: wgpu::Operations {
                        load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
                        store: wgpu::StoreOp::Store,
                    },
                })],
                ..Default::default()
            });
            rpass.set_pipeline(&cache.aux_pipelines[0]);
            rpass.set_bind_group(0, &cache.bind_groups[1][0], &[]);
            rpass.draw(0..3, 0..1);
        }
    }
}

fn create_pipeline(device: &wgpu::Device, _format: wgpu::TextureFormat) -> EffectPipeline {
    // The FBM pass renders into the aux texture (AUX_FORMAT), not directly
    // to the void's destination, so the pipeline's target format is fixed.
    // The bilinear-blit pass handles converting to whatever destination
    // format the compositor allocated.
    let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
        label: Some("void-noise-bgl"),
        entries: &[
            wgpu::BindGroupLayoutEntry {
                binding: 0,
                visibility: wgpu::ShaderStages::FRAGMENT,
                ty: wgpu::BindingType::Buffer {
                    ty: wgpu::BufferBindingType::Uniform,
                    has_dynamic_offset: false,
                    min_binding_size: None,
                },
                count: None,
            },
            wgpu::BindGroupLayoutEntry {
                binding: 1,
                visibility: wgpu::ShaderStages::FRAGMENT,
                ty: wgpu::BindingType::Texture {
                    sample_type: wgpu::TextureSampleType::Float { filterable: true },
                    view_dimension: wgpu::TextureViewDimension::D3,
                    multisampled: false,
                },
                count: None,
            },
            wgpu::BindGroupLayoutEntry {
                binding: 2,
                visibility: wgpu::ShaderStages::FRAGMENT,
                ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                count: None,
            },
        ],
    });

    let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
        label: Some("void-noise-pipeline-layout"),
        bind_group_layouts: &[Some(&bind_group_layout)],
        immediate_size: 0,
    });

    // WGSL has no native #include — concatenate the shared FBM helper ahead
    // of this void's shader. A future warp veil will assemble the same way.
    let fbm_src = include_str!("../../../shaders/lib/fbm.wgsl");
    let void_src = include_str!("../../../shaders/voids/noise.wgsl");
    let full_src = format!("{fbm_src}\n{void_src}");

    let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
        label: Some("void-noise-shader"),
        source: wgpu::ShaderSource::Wgsl(full_src.into()),
    });

    let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
        label: Some("void-noise-pipeline"),
        layout: Some(&pipeline_layout),
        vertex: wgpu::VertexState {
            module: &shader,
            entry_point: Some("vs_main"),
            buffers: &[],
            compilation_options: Default::default(),
        },
        fragment: Some(wgpu::FragmentState {
            module: &shader,
            entry_point: Some("fs_main"),
            targets: &[Some(wgpu::ColorTargetState {
                format: AUX_FORMAT,
                blend: None,
                write_mask: wgpu::ColorWrites::ALL,
            })],
            compilation_options: Default::default(),
        }),
        primitive: wgpu::PrimitiveState {
            topology: wgpu::PrimitiveTopology::TriangleList,
            ..Default::default()
        },
        depth_stencil: None,
        multisample: wgpu::MultisampleState::default(),
        multiview_mask: None,
        cache: None,
    });

    EffectPipeline {
        pipeline,
        bind_group_layout,
    }
}

/// PCG-hashed bytes for the 3D noise volume. RGBA8 layout matches the
/// `Rgba8Unorm` texture format; each channel is an independent
/// pseudo-random byte so callers that read different channels get
/// decorrelated noise (currently only `.x` is read, but the others are
/// reserved for future use). The seed makes per-instance volumes distinct
/// — if two voids have the same `seed` param they share an identical
/// volume layout, which is the user-visible determinism contract.
fn seed_noise_volume(dim: u32, seed: u32) -> Vec<u8> {
    let count = (dim * dim * dim) as usize;
    let mut bytes = vec![0u8; count * 4];
    let mut s = seed.wrapping_mul(747796405).wrapping_add(2891336453);
    for b in &mut bytes {
        s = pcg_hash(s);
        *b = (s >> 24) as u8;
    }
    bytes
}

/// PCG hash matching the GPU-side `fbm_pcg` in `shaders/lib/fbm.wgsl`.
/// Used to seed the 3D noise volume on the CPU.
fn pcg_hash(n: u32) -> u32 {
    let mut h = n.wrapping_mul(747796405).wrapping_add(2891336453);
    h = ((h >> ((h >> 28) + 4)) ^ h).wrapping_mul(277803737);
    (h >> 22) ^ h
}