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
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
//! Per-instance data, one array per property, uploaded as instanced vertex
//! buffers.
//!
//! A column is what a table column is: one value per row. Marks draw a row per
//! instance and read each property from its own buffer, so changing one
//! property re-uploads that property and nothing else, and changing a few rows
//! of it uploads just those rows ([`Column::update`]).
//!
//! Vertex buffers rather than storage buffers, because WebGL2 has none of the
//! latter and every mark has to draw there too.

use super::gpu;
use std::ops::Range;

/// What a column holds and how the GPU reads it.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ColumnFormat {
    /// `1..=4` floats a row, read as `f32` / `vecN<f32>`.
    Float(u32),
    /// Four bytes a row, read as a `vec4<f32>` in `0..=1` — colours.
    Unorm8x4,
    /// Six floats a row, read as two `vec3<f32>` attributes. Two values that
    /// travel together share a buffer this way, and WebGPU allows a pipeline
    /// only eight.
    Float3x2,
    /// Eight floats a row, read as two `vec4<f32>` attributes.
    Float4x2,
    /// Sixteen floats a row, read as four `vec4<f32>` attributes.
    Float4x4,
    /// An absolute `f64` position stored as `(hi, lo)` `f32` triples — see
    /// [`split_f64`](super::split_f64). Two shader attributes.
    PositionF64,
    /// A position relative to the layer's
    /// [`local_origin`](super::MarkCommon::local_origin), one `f32` triple:
    /// half the bytes of [`PositionF64`](Self::PositionF64), for data that
    /// arrives local — a tile's features relative to its corner. Takes the
    /// same two shader locations, so the columns after it do not move.
    PositionLocal,
}

impl ColumnFormat {
    /// Bytes a row.
    pub fn stride(self) -> u64 {
        match self {
            ColumnFormat::Float(n) => 4 * n.clamp(1, 4) as u64,
            ColumnFormat::Unorm8x4 => 4,
            ColumnFormat::PositionF64 | ColumnFormat::Float3x2 => 24,
            ColumnFormat::PositionLocal => 12,
            ColumnFormat::Float4x2 => 32,
            ColumnFormat::Float4x4 => 64,
        }
    }

    fn attributes(self, first_location: u32) -> Vec<wgpu::VertexAttribute> {
        match self {
            ColumnFormat::Float(n) => vec![wgpu::VertexAttribute {
                format: match n.clamp(1, 4) {
                    1 => wgpu::VertexFormat::Float32,
                    2 => wgpu::VertexFormat::Float32x2,
                    3 => wgpu::VertexFormat::Float32x3,
                    _ => wgpu::VertexFormat::Float32x4,
                },
                offset: 0,
                shader_location: first_location,
            }],
            ColumnFormat::PositionLocal => vec![wgpu::VertexAttribute {
                format: wgpu::VertexFormat::Float32x3,
                offset: 0,
                shader_location: first_location,
            }],
            ColumnFormat::Float4x2 => vec![
                wgpu::VertexAttribute {
                    format: wgpu::VertexFormat::Float32x4,
                    offset: 0,
                    shader_location: first_location,
                },
                wgpu::VertexAttribute {
                    format: wgpu::VertexFormat::Float32x4,
                    offset: 16,
                    shader_location: first_location + 1,
                },
            ],
            ColumnFormat::Float4x4 => (0..4)
                .map(|k| wgpu::VertexAttribute {
                    format: wgpu::VertexFormat::Float32x4,
                    offset: 16 * k as u64,
                    shader_location: first_location + k,
                })
                .collect(),
            ColumnFormat::Unorm8x4 => vec![wgpu::VertexAttribute {
                format: wgpu::VertexFormat::Unorm8x4,
                offset: 0,
                shader_location: first_location,
            }],
            ColumnFormat::PositionF64 | ColumnFormat::Float3x2 => vec![
                wgpu::VertexAttribute {
                    format: wgpu::VertexFormat::Float32x3,
                    offset: 0,
                    shader_location: first_location,
                },
                wgpu::VertexAttribute {
                    format: wgpu::VertexFormat::Float32x3,
                    offset: 12,
                    shader_location: first_location + 1,
                },
            ],
        }
    }

    /// Shader locations the column takes.
    pub(crate) fn locations(self) -> u32 {
        match self {
            ColumnFormat::PositionF64
            | ColumnFormat::Float3x2
            | ColumnFormat::Float4x2
            | ColumnFormat::PositionLocal => 2,
            ColumnFormat::Float4x4 => 4,
            _ => 1,
        }
    }
}

/// One property for every instance.
///
/// Rows past the end of the data take the column's default, so a layer can
/// leave a column empty to give every instance the same value — the same rule
/// as [`InstancedMesh::colors`](crate::InstancedMesh::colors).
#[derive(Debug)]
pub struct Column {
    format: ColumnFormat,
    bytes: Vec<u8>,
    default: Vec<u8>,
    /// Bytes changed since the last upload. `None` with a buffer present means
    /// the GPU copy is current.
    dirty: Option<Range<usize>>,
    /// Rows the GPU buffer was filled for — padding included.
    uploaded_rows: usize,
    pub(crate) buffer: Option<gpu::Buffer>,
}

impl Column {
    pub fn new(format: ColumnFormat) -> Self {
        let default = match format {
            ColumnFormat::Unorm8x4 => vec![255; 4],
            _ => vec![0; format.stride() as usize],
        };
        Self {
            format,
            bytes: Vec::new(),
            default,
            dirty: None,
            uploaded_rows: 0,
            buffer: None,
        }
    }

    /// `1..=4` floats a row.
    pub fn float(components: u32) -> Self {
        Self::new(ColumnFormat::Float(components))
    }

    pub fn format(&self) -> ColumnFormat {
        self.format
    }

    /// Rows of real data (not counting default padding).
    pub fn len(&self) -> usize {
        self.bytes.len() / self.format.stride() as usize
    }

    pub fn is_empty(&self) -> bool {
        self.bytes.is_empty()
    }

    /// The value rows past the end of the data take.
    pub fn set_default_f32(&mut self, value: &[f32]) {
        self.default = pack_f32(self.format, value);
        self.mark_all();
    }

    /// [`set_default_f32`](Self::set_default_f32) for an RGBA8 column.
    pub fn set_default_rgba(&mut self, rgba: [u8; 4]) {
        assert_eq!(self.format, ColumnFormat::Unorm8x4, "not a colour column");
        self.default = rgba.to_vec();
        self.mark_all();
    }

    /// Replace every row. `values` is row-major, `components` floats a row.
    pub fn set_f32(&mut self, values: &[f32]) {
        self.bytes = pack_f32(self.format, values);
        self.mark_all();
    }

    /// Replace every row of a colour column.
    pub fn set_rgba(&mut self, values: &[[u8; 4]]) {
        assert_eq!(self.format, ColumnFormat::Unorm8x4, "not a colour column");
        self.bytes = values.iter().flatten().copied().collect();
        self.mark_all();
    }

    /// Replace every row of a position column with absolute positions,
    /// making it [`PositionF64`](ColumnFormat::PositionF64) if it was local.
    pub fn set_positions(&mut self, values: &[[f64; 3]]) {
        if self.format == ColumnFormat::PositionLocal {
            self.format = ColumnFormat::PositionF64;
            self.default = vec![0; 24];
            self.buffer = None;
        }
        assert_eq!(
            self.format,
            ColumnFormat::PositionF64,
            "not a position column"
        );
        self.bytes = pack_positions(values);
        self.mark_all();
    }

    /// Overwrite rows `start..start + n` with `values` (floats, row-major),
    /// growing the column if they run past its end. Only those rows are
    /// uploaded next frame.
    pub fn update_f32(&mut self, start: usize, values: &[f32]) {
        let packed = pack_f32(self.format, values);
        self.splice(start, &packed);
    }

    /// [`update_f32`](Self::update_f32) for a colour column.
    pub fn update_rgba(&mut self, start: usize, values: &[[u8; 4]]) {
        assert_eq!(self.format, ColumnFormat::Unorm8x4, "not a colour column");
        let packed: Vec<u8> = values.iter().flatten().copied().collect();
        self.splice(start, &packed);
    }

    /// Replace every row with positions relative to the layer's local origin,
    /// making the column [`PositionLocal`](ColumnFormat::PositionLocal).
    pub fn set_local_positions(&mut self, values: &[[f32; 3]]) {
        assert!(
            matches!(
                self.format,
                ColumnFormat::PositionF64 | ColumnFormat::PositionLocal
            ),
            "not a position column"
        );
        if self.format != ColumnFormat::PositionLocal {
            self.format = ColumnFormat::PositionLocal;
            self.default = vec![0; 12];
            // The layout changed: the old buffer's rows are the wrong size.
            self.buffer = None;
        }
        self.bytes = bytemuck::cast_slice(values).to_vec();
        self.mark_all();
    }

    /// [`set_local_positions`](Self::set_local_positions) for some rows.
    pub fn update_local_positions(&mut self, start: usize, values: &[[f32; 3]]) {
        assert_eq!(
            self.format,
            ColumnFormat::PositionLocal,
            "not a local position column"
        );
        self.splice(start, bytemuck::cast_slice(values));
    }

    /// [`update_f32`](Self::update_f32) for a position column.
    pub fn update_positions(&mut self, start: usize, values: &[[f64; 3]]) {
        assert_eq!(
            self.format,
            ColumnFormat::PositionF64,
            "not a position column"
        );
        self.splice(start, &pack_positions(values));
    }

    /// A position column's rows as absolute `f64`s, `origin` added to local
    /// ones; `None` for any other column.
    pub fn positions_f64(&self, origin: Option<[f64; 3]>) -> Option<Vec<[f64; 3]>> {
        let f: Vec<f32> = bytemuck::pod_collect_to_vec(&self.bytes);
        match self.format {
            ColumnFormat::PositionF64 => Some(
                f.chunks(6)
                    .map(|r| {
                        [
                            r[0] as f64 + r[3] as f64,
                            r[1] as f64 + r[4] as f64,
                            r[2] as f64 + r[5] as f64,
                        ]
                    })
                    .collect(),
            ),
            ColumnFormat::PositionLocal => {
                let o = origin.unwrap_or_default();
                Some(
                    f.chunks(3)
                        .map(|r| [o[0] + r[0] as f64, o[1] + r[1] as f64, o[2] + r[2] as f64])
                        .collect(),
                )
            }
            _ => None,
        }
    }

    /// Row `i` of a float column, the default past the data.
    pub fn row_f32(&self, i: usize) -> Vec<f32> {
        let stride = self.format.stride() as usize;
        let bytes = self
            .bytes
            .get(i * stride..(i + 1) * stride)
            .unwrap_or(&self.default);
        bytemuck::pod_collect_to_vec(bytes)
    }

    /// Drop the GPU copy, keeping the data: the next draw uploads it again.
    pub fn release(&mut self) {
        self.buffer = None;
        self.uploaded_rows = 0;
        self.mark_all();
    }

    /// Bytes the GPU copy holds (its capacity), 0 when released.
    pub fn gpu_bytes(&self) -> u64 {
        self.buffer.as_ref().map_or(0, |b| b.size())
    }

    /// Raw bytes, for tests and for formats a typed setter does not cover.
    pub fn bytes(&self) -> &[u8] {
        &self.bytes
    }

    /// The byte range the next upload will write, if any. For tests.
    pub fn pending_upload(&self) -> Option<Range<usize>> {
        self.dirty.clone()
    }

    fn splice(&mut self, start: usize, packed: &[u8]) {
        let stride = self.format.stride() as usize;
        let begin = start * stride;
        let end = begin + packed.len();
        if self.bytes.len() < end {
            // Rows between the old end and `start` take the default, exactly
            // as they would have been drawn before the write.
            while self.bytes.len() < begin {
                self.bytes.extend_from_slice(&self.default);
            }
            self.bytes.resize(end, 0);
        }
        self.bytes[begin..end].copy_from_slice(packed);
        self.dirty = Some(match self.dirty.take() {
            Some(r) => r.start.min(begin)..r.end.max(end),
            None => begin..end,
        });
    }

    fn mark_all(&mut self) {
        // A full rewrite: drop the buffer's claim to be current.
        self.dirty = Some(0..usize::MAX);
    }

    /// Bring the GPU buffer up to date for `rows` instances.
    pub(crate) fn upload(&mut self, device: &gpu::Device, queue: &gpu::Queue, rows: usize) {
        let stride = self.format.stride() as usize;
        let rows = rows.max(1);
        let need = rows * stride;
        let fits = self
            .buffer
            .as_ref()
            .is_some_and(|b| b.size() as usize >= need);
        let full = !fits
            || self.uploaded_rows != rows
            || matches!(&self.dirty, Some(r) if r.end == usize::MAX);
        if full {
            let mut data = Vec::with_capacity(need);
            data.extend_from_slice(&self.bytes[..self.bytes.len().min(need)]);
            while data.len() < need {
                data.extend_from_slice(&self.default);
            }
            if !fits {
                // Grow by half again so a column appended to row by row is not
                // reallocated every frame.
                let cap = (need + need / 2).max(stride * 4);
                let cap = cap.div_ceil(4) * 4;
                self.buffer = Some(device.create_buffer(&wgpu::BufferDescriptor {
                    label: Some("threers mark column"),
                    size: cap as u64,
                    usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
                    mapped_at_creation: false,
                }));
            }
            queue.write_buffer(self.buffer.as_ref().unwrap(), 0, &data);
            self.uploaded_rows = rows;
        } else if let Some(r) = self.dirty.clone() {
            let end = r.end.min(need).min(self.bytes.len());
            if r.start < end {
                // `write_buffer` wants a multiple of four bytes; every format's
                // stride is one, so whole rows always are.
                queue.write_buffer(
                    self.buffer.as_ref().unwrap(),
                    r.start as u64,
                    &self.bytes[r.start..end],
                );
            }
        }
        self.dirty = None;
    }

    /// Pretend the GPU copy is current, so a test sees only later changes.
    #[cfg(test)]
    pub(crate) fn mark_uploaded(&mut self) {
        self.dirty = None;
    }

    pub(crate) fn layout(&self, first_location: u32) -> (u64, Vec<wgpu::VertexAttribute>) {
        (self.format.stride(), self.format.attributes(first_location))
    }
}

fn pack_f32(format: ColumnFormat, values: &[f32]) -> Vec<u8> {
    match format {
        ColumnFormat::Float(n) => {
            let n = n.clamp(1, 4) as usize;
            assert!(
                values.len().is_multiple_of(n),
                "{} floats is not a whole number of {n}-float rows",
                values.len()
            );
            bytemuck::cast_slice(values).to_vec()
        }
        ColumnFormat::PositionLocal => {
            assert!(
                values.len().is_multiple_of(3),
                "{} floats is not a whole number of positions",
                values.len()
            );
            bytemuck::cast_slice(values).to_vec()
        }
        ColumnFormat::Float4x2 => {
            assert!(
                values.len().is_multiple_of(8),
                "{} floats is not a whole number of 8-float rows",
                values.len()
            );
            bytemuck::cast_slice(values).to_vec()
        }
        ColumnFormat::Float4x4 => {
            assert!(
                values.len().is_multiple_of(16),
                "{} floats is not a whole number of 16-float rows",
                values.len()
            );
            bytemuck::cast_slice(values).to_vec()
        }
        ColumnFormat::Float3x2 => {
            assert!(
                values.len().is_multiple_of(6),
                "{} floats is not a whole number of 6-float rows",
                values.len()
            );
            bytemuck::cast_slice(values).to_vec()
        }
        ColumnFormat::Unorm8x4 => values
            .iter()
            .map(|v| (v.clamp(0.0, 1.0) * 255.0 + 0.5) as u8)
            .collect(),
        ColumnFormat::PositionF64 => {
            let rows: Vec<[f64; 3]> = values
                .chunks(3)
                .map(|c| [c[0] as f64, c[1] as f64, c[2] as f64])
                .collect();
            pack_positions(&rows)
        }
    }
}

fn pack_positions(values: &[[f64; 3]]) -> Vec<u8> {
    let mut out = Vec::with_capacity(values.len() * 24);
    for p in values {
        let (hi, lo) = super::split_f64x3(*p);
        for v in hi.iter().chain(lo.iter()) {
            out.extend_from_slice(&v.to_le_bytes());
        }
    }
    out
}

/// Vertex buffer layouts for `columns`, instanced, from shader location
/// `first_location` on.
pub(crate) fn instance_layouts(
    columns: &[&Column],
    first_location: u32,
) -> Vec<(u64, Vec<wgpu::VertexAttribute>)> {
    let mut loc = first_location;
    columns
        .iter()
        .map(|c| {
            let l = c.layout(loc);
            loc += c.format.locations();
            l
        })
        .collect()
}

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

    /// Bytes are not `f32`-aligned in a `Vec<u8>`, so copy rather than cast.
    fn floats(bytes: &[u8]) -> Vec<f32> {
        bytemuck::pod_collect_to_vec(bytes)
    }

    #[test]
    fn partial_update_marks_only_those_rows() {
        let mut c = Column::float(1);
        c.set_f32(&[1.0, 2.0, 3.0, 4.0]);
        // Pretend it went up.
        c.dirty = None;
        c.update_f32(2, &[9.0]);
        assert_eq!(c.pending_upload(), Some(8..12));
        assert_eq!(floats(c.bytes()), [1.0, 2.0, 9.0, 4.0]);
    }

    #[test]
    fn writing_past_the_end_fills_the_gap_with_the_default() {
        let mut c = Column::float(1);
        c.set_default_f32(&[7.0]);
        c.set_f32(&[1.0]);
        c.update_f32(3, &[5.0]);
        assert_eq!(floats(c.bytes()), [1.0, 7.0, 7.0, 5.0]);
    }

    #[test]
    fn positions_pack_as_hi_then_lo() {
        let mut c = Column::new(ColumnFormat::PositionF64);
        c.set_positions(&[[10_000_000.25, 0.0, -1.5]]);
        let f = floats(c.bytes());
        assert_eq!(f.len(), 6);
        assert_eq!(f[0] as f64 + f[3] as f64, 10_000_000.25);
        assert_eq!(f[2], -1.5);
        assert_eq!(f[5], 0.0);
    }

    #[test]
    fn layouts_number_locations_across_two_attribute_columns() {
        let a = Column::new(ColumnFormat::PositionF64);
        let b = Column::float(1);
        let l = instance_layouts(&[&a, &b], 3);
        assert_eq!(l[0].1[0].shader_location, 3);
        assert_eq!(l[0].1[1].shader_location, 4);
        assert_eq!(l[1].1[0].shader_location, 5);
    }
}