threers 0.1.0

Drop-in three.js replacement for Rust and WebAssembly (wgpu) — THREE.* API, scene graph, PBR, post-fx, mesh BVH, CSG, an OpenSCAD front end with a watertight exact-CSG kernel, headless render, and native media codecs
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
//! The GPU side: the textures the fluid lives in, the uniform block every pass
//! reads, and the pipelines that write them.
//!
//! Nothing here is specific to a fluid — it is the machinery for running a
//! sequence of fullscreen fragment passes over ping-ponged float targets.
//! [`super::FluidSim`] is what sequences them.
//!
//! The uniform block and its constants are shared with the Metal backend and
//! build without wgpu (`metal-fluid`); the pipelines below them are wgpu's.

#[cfg(feature = "fluid")]
pub use wgpu_side::*;

/// The uniform block, mirroring `Params` in [`super::shader`].
///
/// Field order is the layout: WGSL puts a `vec4` on a 16-byte boundary, so the
/// four loose scalars sit between the two `vec2` pairs and the two `vec4`s
/// rather than after them. `params_layout_matches_wgsl` pins the result.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct Params {
    pub texel_size: [f32; 2],
    pub point: [f32; 2],
    pub dye_texel_size: [f32; 2],
    pub dt: f32,
    pub dissipation: f32,
    pub curl: f32,
    pub aspect_ratio: f32,
    /// WGSL puts the next `vec4` on a 16-byte boundary and `repr(C)` would
    /// not; without this the two layouts diverge at `color` and every pass
    /// reads the wrong eight bytes.
    pub pad0: [f32; 2],
    pub color: [f32; 4],
    pub curve: [f32; 4],
    pub radius: f32,
    pub value: f32,
    pub flags: u32,
    pub pad: u32,
}

pub(crate) const FLAG_SHADING: u32 = 1;
pub(crate) const FLAG_BLOOM: u32 = 2;
pub(crate) const FLAG_SUNRAYS: u32 = 4;
pub(crate) const FLAG_ENCODE: u32 = 8;

#[cfg(feature = "fluid")]
/// Bytes per uniform block. WGSL rounds the struct up to its own alignment.
pub(crate) const PARAMS_SIZE: u64 = 96;

/// Splats in one pass. Two `vec4`s each, so the block is 1 KiB — well inside
/// the 64 KiB a uniform buffer is guaranteed, and more than a hand has
/// fingers or a burst has blobs.
pub(crate) const MAX_BATCH: usize = 32;

#[cfg(feature = "fluid")]
/// Bytes in the splat batch: `array<vec4<f32>, 64>`.
pub(crate) const BATCH_SIZE: u64 = (MAX_BATCH as u64) * 2 * 16;

#[cfg(feature = "fluid")]
mod wgpu_side {
    use super::*;
    use crate::renderer::fullscreen::FormatPipelines;

    /// Which float formats the fields are stored in.
    ///
    /// The simulation needs one, two and four channel float targets that can be
    /// both rendered to and filtered. WebGPU guarantees all three; WebGL2 through
    /// wgpu's GL backend guarantees none of them without `EXT_color_buffer_float`,
    /// and a device that has only the four-channel one still runs the whole
    /// simulation — it just carries three unused channels in the pressure field.
    /// So the narrow formats are a saving, not a requirement, and
    /// [`FluidFormats::for_adapter`] gives up each one independently.
    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    pub struct FluidFormats {
        /// Dye — three colour channels and nothing in alpha.
        pub dye: wgpu::TextureFormat,
        /// The *volume's* dye, which can be packed tighter than the plane's.
        ///
        /// Three channels of 11-bit float in one word: half the memory and half
        /// the bandwidth of four channels of 16, and the dye grid is both the
        /// largest field in a volume and the one the heaviest pass reads and
        /// writes. It is unsigned, which the plane's dye could not be — a
        /// MacCormack correction is an extrapolation and can undershoot — but
        /// nothing in the volume subtracts from the dye.
        ///
        /// `rg11b10ufloat` is only a render target where the device has that
        /// feature, so this falls back to [`dye`](Self::dye).
        pub volume_dye: wgpu::TextureFormat,
        /// Velocity — two channels.
        pub velocity: wgpu::TextureFormat,
        /// Pressure, divergence, curl, sunrays — one channel.
        pub scalar: wgpu::TextureFormat,
    }

    impl Default for FluidFormats {
        /// What WebGPU's baseline guarantees.
        fn default() -> Self {
            Self {
                dye: wgpu::TextureFormat::Rgba16Float,
                // Not the packed one: it is a render target only where the
                // device asked for the feature, and this is the baseline.
                // `FluidSim3d` upgrades it when the device it is handed can.
                volume_dye: wgpu::TextureFormat::Rgba16Float,
                velocity: wgpu::TextureFormat::Rg16Float,
                scalar: wgpu::TextureFormat::R16Float,
            }
        }
    }

    impl FluidFormats {
        /// The formats a volume will *actually* allocate, given its
        /// `compact_dye`.
        ///
        /// [`volume_dye`](Self::volume_dye) is what the device would allow, not
        /// what the simulation asked for, and those are different things: the
        /// packed format is off by default. Sizing a volume against the first
        /// while it allocates with the second underestimates the dye — which is
        /// most of the memory — by a factor of two. Resolve before predicting.
        pub fn resolve(self, compact_dye: bool) -> Self {
            Self {
                volume_dye: if compact_dye {
                    self.volume_dye
                } else {
                    self.dye
                },
                ..self
            }
        }

        /// The narrowest formats this adapter will render to and filter, widening
        /// one channel count at a time: `r16float` → `rg16float` → `rgba16float`.
        /// Returns `None` if even `rgba16float` is unusable, which means the
        /// simulation cannot run on this device at all.
        pub fn for_adapter(adapter: &wgpu::Adapter) -> Option<Self> {
            use wgpu::TextureFormat as F;
            let usable = |f: F| {
                let feats = adapter.get_texture_format_features(f);
                feats
                    .allowed_usages
                    .contains(wgpu::TextureUsages::RENDER_ATTACHMENT)
                    && feats
                        .flags
                        .contains(wgpu::TextureFormatFeatureFlags::FILTERABLE)
            };
            if !usable(F::Rgba16Float) {
                return None;
            }
            Some(Self {
                dye: F::Rgba16Float,
                volume_dye: if usable(F::Rg11b10Ufloat) {
                    F::Rg11b10Ufloat
                } else {
                    F::Rgba16Float
                },
                velocity: if usable(F::Rg16Float) {
                    F::Rg16Float
                } else {
                    F::Rgba16Float
                },
                scalar: if usable(F::R16Float) {
                    F::R16Float
                } else if usable(F::Rg16Float) {
                    F::Rg16Float
                } else {
                    F::Rgba16Float
                },
            })
        }
    }

    /// Blend `src + dst * (1 - src.a)` — the dye compositing over the background.
    const OVER: wgpu::BlendState = wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING;

    /// Blend `src + dst`, for the bloom chain's upsample.
    const ADD: wgpu::BlendState = wgpu::BlendState {
        color: wgpu::BlendComponent {
            src_factor: wgpu::BlendFactor::One,
            dst_factor: wgpu::BlendFactor::One,
            operation: wgpu::BlendOperation::Add,
        },
        alpha: wgpu::BlendComponent {
            src_factor: wgpu::BlendFactor::One,
            dst_factor: wgpu::BlendFactor::One,
            operation: wgpu::BlendOperation::Add,
        },
    };

    /// Everything compiled once: the layouts, the module, and one pipeline per
    /// (stage, target format, blend) the fixed part of the frame needs.
    pub(crate) struct Pipelines {
        pub bgl: wgpu::BindGroupLayout,
        layout: wgpu::PipelineLayout,
        module: wgpu::ShaderModule,

        pub copy_dye: wgpu::RenderPipeline,
        pub copy_velocity: wgpu::RenderPipeline,
        pub clear: wgpu::RenderPipeline,
        pub fill_velocity: wgpu::RenderPipeline,
        pub splat_velocity: wgpu::RenderPipeline,
        pub splat_dye: wgpu::RenderPipeline,
        pub advect_velocity: wgpu::RenderPipeline,
        pub advect_dye: wgpu::RenderPipeline,
        pub correct_velocity: wgpu::RenderPipeline,
        pub correct_dye: wgpu::RenderPipeline,
        pub divergence: wgpu::RenderPipeline,
        pub curl: wgpu::RenderPipeline,
        pub vorticity: wgpu::RenderPipeline,
        pub pressure: wgpu::RenderPipeline,
        pub gradient: wgpu::RenderPipeline,
        pub residual: wgpu::RenderPipeline,
        pub restrict: wgpu::RenderPipeline,
        pub prolong: wgpu::RenderPipeline,
        pub bloom_prefilter: wgpu::RenderPipeline,
        pub bloom_blur: wgpu::RenderPipeline,
        pub bloom_blur_add: wgpu::RenderPipeline,
        pub bloom_final: wgpu::RenderPipeline,
        pub sunrays_mask: wgpu::RenderPipeline,
        pub sunrays: wgpu::RenderPipeline,
        pub blur: wgpu::RenderPipeline,

        /// Display, background and checkerboard, keyed by the format of whatever
        /// the caller is rendering into. A surface, an offscreen target and a
        /// readback buffer are three different formats, and the fluid should not
        /// care which it was handed.
        out: FormatPipelines<OutputPipes>,
    }

    /// The three pipelines whose target format is the caller's.
    pub(crate) struct OutputPipes {
        pub display_over: wgpu::RenderPipeline,
        pub display_replace: wgpu::RenderPipeline,
        pub color: wgpu::RenderPipeline,
        pub checkerboard: wgpu::RenderPipeline,
        /// The frame-rate read-out, composited over the finished picture.
        pub overlay: wgpu::RenderPipeline,
    }

    impl Pipelines {
        pub fn new(device: &wgpu::Device, formats: FluidFormats) -> Self {
            let bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("threers fluid bgl"),
                entries: &[
                    wgpu::BindGroupLayoutEntry {
                        binding: 0,
                        visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
                        ty: wgpu::BindingType::Buffer {
                            ty: wgpu::BufferBindingType::Uniform,
                            has_dynamic_offset: false,
                            min_binding_size: wgpu::BufferSize::new(PARAMS_SIZE),
                        },
                        count: None,
                    },
                    tex_entry(1),
                    tex_entry(2),
                    tex_entry(3),
                    wgpu::BindGroupLayoutEntry {
                        binding: 4,
                        visibility: wgpu::ShaderStages::FRAGMENT,
                        ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                        count: None,
                    },
                    // The splat batch. Bound for every pass, because one layout
                    // serves them all; the others never read it.
                    wgpu::BindGroupLayoutEntry {
                        binding: 5,
                        visibility: wgpu::ShaderStages::FRAGMENT,
                        ty: wgpu::BindingType::Buffer {
                            ty: wgpu::BufferBindingType::Uniform,
                            has_dynamic_offset: false,
                            min_binding_size: wgpu::BufferSize::new(BATCH_SIZE),
                        },
                        count: None,
                    },
                ],
            });
            let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
                label: Some("threers fluid layout"),
                bind_group_layouts: &[Some(&bgl)],
                immediate_size: 0,
            });
            let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
                label: Some("threers fluid"),
                source: wgpu::ShaderSource::Wgsl(crate::fluid::shader::wgsl().into()),
            });

            // The plane does not use `volume_dye`; see its docs for why the
            // volume can pack its dye and this cannot.
            let FluidFormats {
                dye,
                velocity,
                scalar,
                volume_dye: _,
            } = formats;
            let p = |fs: &str, format, blend| {
                build(device, &layout, &module, "vs", fs, format, blend, fs)
            };
            Self {
                copy_dye: p("fs_copy", dye, None),
                copy_velocity: p("fs_copy", velocity, None),
                clear: p("fs_clear", scalar, None),
                fill_velocity: p("fs_color", velocity, None),
                splat_velocity: p("fs_splat", velocity, None),
                splat_dye: p("fs_splat", dye, None),
                advect_velocity: p("fs_advection", velocity, None),
                advect_dye: p("fs_advection", dye, None),
                correct_velocity: p("fs_advect_correct", velocity, None),
                correct_dye: p("fs_advect_correct", dye, None),
                divergence: p("fs_divergence", scalar, None),
                curl: p("fs_curl", scalar, None),
                vorticity: p("fs_vorticity", velocity, None),
                pressure: p("fs_pressure", scalar, None),
                gradient: p("fs_gradient", velocity, None),
                residual: p("fs_residual", scalar, None),
                restrict: p("fs_restriction", scalar, None),
                prolong: p("fs_prolong", scalar, None),
                bloom_prefilter: p("fs_bloom_prefilter", dye, None),
                bloom_blur: p("fs_bloom_blur", dye, None),
                bloom_blur_add: p("fs_bloom_blur", dye, Some(ADD)),
                bloom_final: p("fs_bloom_final", dye, None),
                // The mask is written into the dye's spare buffer, so it is in the
                // dye's format; the rays themselves are one channel.
                sunrays_mask: p("fs_sunrays_mask", dye, None),
                sunrays: p("fs_sunrays", scalar, None),
                blur: build(
                    device, &layout, &module, "vs_blur", "fs_blur", scalar, None, "fs_blur",
                ),
                bgl,
                layout,
                module,
                out: FormatPipelines::new(),
            }
        }

        /// The output pipelines for a target format, compiled on first sight.
        pub fn output(
            &mut self,
            device: &wgpu::Device,
            format: wgpu::TextureFormat,
        ) -> &OutputPipes {
            self.out.get_or_build(format, || {
                let p = |fs: &str, blend| {
                    build(
                        device,
                        &self.layout,
                        &self.module,
                        "vs",
                        fs,
                        format,
                        blend,
                        fs,
                    )
                };
                OutputPipes {
                    display_over: p("fs_display", Some(OVER)),
                    display_replace: p("fs_display", None),
                    color: p("fs_color", None),
                    checkerboard: p("fs_checkerboard", None),
                    overlay: p("fs_overlay", Some(wgpu::BlendState::ALPHA_BLENDING)),
                }
            })
        }
    }

    fn tex_entry(binding: u32) -> wgpu::BindGroupLayoutEntry {
        wgpu::BindGroupLayoutEntry {
            binding,
            visibility: wgpu::ShaderStages::FRAGMENT,
            ty: wgpu::BindingType::Texture {
                sample_type: wgpu::TextureSampleType::Float { filterable: true },
                view_dimension: wgpu::TextureViewDimension::D2,
                multisampled: false,
            },
            count: None,
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn build(
        device: &wgpu::Device,
        layout: &wgpu::PipelineLayout,
        module: &wgpu::ShaderModule,
        vs: &str,
        fs: &str,
        format: wgpu::TextureFormat,
        blend: Option<wgpu::BlendState>,
        label: &str,
    ) -> wgpu::RenderPipeline {
        device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
            label: Some(label),
            layout: Some(layout),
            vertex: wgpu::VertexState {
                module,
                entry_point: Some(vs),
                buffers: &[],
                compilation_options: Default::default(),
            },
            fragment: Some(wgpu::FragmentState {
                module,
                entry_point: Some(fs),
                targets: &[Some(wgpu::ColorTargetState {
                    format,
                    blend,
                    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,
        })
    }

    /// One uniform block, in a buffer of its own.
    ///
    /// A pass's parameters have to be visible to the GPU when that pass runs, and
    /// `Queue::write_buffer` only orders writes against *submits* — every write
    /// issued before a submit lands before all of its passes. Two passes in one
    /// submit therefore cannot share a buffer, so each takes its own. They are
    /// eighty bytes; there are about thirty of them.
    pub(crate) fn ubo(device: &wgpu::Device, label: &str) -> wgpu::Buffer {
        sized_ubo(device, label, PARAMS_SIZE)
    }

    /// The splat batch's buffer, or the zeroed stand-in every other pass binds.
    pub(crate) fn batch_ubo(device: &wgpu::Device, label: &str) -> wgpu::Buffer {
        sized_ubo(device, label, BATCH_SIZE)
    }

    fn sized_ubo(device: &wgpu::Device, label: &str, size: u64) -> wgpu::Buffer {
        device.create_buffer(&wgpu::BufferDescriptor {
            label: Some(label),
            size,
            usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        })
    }

    /// A bind group over one uniform block and up to three textures. Passes that
    /// read fewer get the same view in the unused slots — see [`super::shader`].
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn bind(
        device: &wgpu::Device,
        bgl: &wgpu::BindGroupLayout,
        sampler: &wgpu::Sampler,
        params: &wgpu::Buffer,
        batch: &wgpu::Buffer,
        a: &wgpu::TextureView,
        b: Option<&wgpu::TextureView>,
        c: Option<&wgpu::TextureView>,
    ) -> wgpu::BindGroup {
        device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("threers fluid bind"),
            layout: bgl,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: params.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: wgpu::BindingResource::TextureView(a),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: wgpu::BindingResource::TextureView(b.unwrap_or(a)),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: wgpu::BindingResource::TextureView(c.unwrap_or(a)),
                },
                wgpu::BindGroupEntry {
                    binding: 4,
                    resource: wgpu::BindingResource::Sampler(sampler),
                },
                wgpu::BindGroupEntry {
                    binding: 5,
                    resource: batch.as_entire_binding(),
                },
            ],
        })
    }
}

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

    /// The Rust block and the WGSL one have to agree byte for byte, and nothing
    /// in either language will say so if they drift — the GPU reads whatever is
    /// at the offset and the picture goes subtly wrong. The offsets below are
    /// WGSL's, derived from its alignment rules; this holds Rust to them.
    #[test]
    fn params_layout_matches_wgsl() {
        assert_eq!(std::mem::size_of::<Params>() as u64, PARAMS_SIZE);
        let p = Params::default();
        let base = &p as *const Params as usize;
        let at = |f: *const ()| f as usize - base;
        assert_eq!(at(&p.texel_size as *const _ as *const ()), 0);
        assert_eq!(at(&p.point as *const _ as *const ()), 8);
        assert_eq!(at(&p.dye_texel_size as *const _ as *const ()), 16);
        assert_eq!(at(&p.dt as *const _ as *const ()), 24);
        assert_eq!(at(&p.dissipation as *const _ as *const ()), 28);
        assert_eq!(at(&p.curl as *const _ as *const ()), 32);
        assert_eq!(at(&p.aspect_ratio as *const _ as *const ()), 36);
        // The two vec4s, which are why the scalars sit where they do.
        assert_eq!(at(&p.color as *const _ as *const ()), 48);
        assert_eq!(at(&p.curve as *const _ as *const ()), 64);
        assert_eq!(at(&p.radius as *const _ as *const ()), 80);
        assert_eq!(at(&p.value as *const _ as *const ()), 84);
        assert_eq!(at(&p.flags as *const _ as *const ()), 88);
    }
}