sgl-3d 0.4.2

Caller-driven 3D rendering for SGL games, native and in the browser (WebGPU)
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
//! What the library counts on each thread while the `diagnostics` feature is
//! on (`diagnostics::counters`): its uploads by call site, the buffers it
//! creates, the time each step of building a model and each
//! instance BVH takes, the ray source's and geometry slabs' growths, the
//! static-edit boxes recorded and merged, and the hardware path's
//! acceleration-structure builds and compactions. Without the feature every
//! function here is a pass-through and nothing is kept.

/// A step of preparing or placing a model, or of building an instance BVH,
/// which `Counters::steps` times. Preparing a model (`PreparedModel::new`)
/// runs `Validate`, `Pack`, `RayBvh`, `Ranges` and `Clusters` on the
/// thread that prepares it; `Scene::add_model` and `set_model` run `Place`
/// and `Write` on theirs.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BuildStep {
    /// Checking a mesh's indices, positions and deformation.
    Validate,
    /// Packing a model's ray-source words (mesh records, vertices, indices,
    /// a deforming model's influences and morph targets) and its meshes'
    /// shadow-caster positions.
    Pack,
    /// Building a model's BVH.
    RayBvh,
    /// A mesh's culling hierarchy.
    Ranges,
    /// A mesh's local-light shadow caster clusters.
    Clusters,
    /// Placing a prepared model: checking it against the scene and the
    /// device, allocating its ranges (growing a buffer if it must) and
    /// rebasing the words that address them.
    Place,
    /// Copying a placed model's words and geometry to the queue.
    Write,
    /// Building the static instance BVH (after a static edit).
    StaticInstanceBvh,
    /// Building the moving instance BVH (every traced frame).
    MovingInstanceBvh,
}

/// One call site's uploads: what the library wrote to the GPU from that
/// line through the queue or a buffer created with contents. A site is its
/// file and line, which move as the library is edited: compare versions by
/// `Counters::uploaded_bytes` or by sums over a file, not by line.
#[cfg(any(test, feature = "diagnostics"))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UploadSite {
    pub file: &'static str,
    pub line: u32,
    pub bytes: u64,
    pub writes: u64,
}

/// One build step's calls and time; in the browser, the time is
/// `performance.now()`'s, which the browser coarsens.
#[cfg(any(test, feature = "diagnostics"))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct StepTime {
    pub step: BuildStep,
    pub calls: u64,
    pub nanoseconds: u64,
}

/// What this thread counted since it started (`diagnostics::counters`).
/// Subtract two of them for what happened between.
#[cfg(any(test, feature = "diagnostics"))]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Counters {
    /// Uploads by call site, in the order the sites first uploaded.
    pub uploads: Vec<UploadSite>,
    /// Buffers created, with contents or without.
    pub buffers_created: u64,
    /// Build steps, in the order they first ran.
    pub steps: Vec<StepTime>,
    /// Growths of the ray source, each a copy of it whole.
    pub ray_source_growths: u64,
    /// Growths of a geometry slab, each a copy of it whole.
    pub geometry_growths: u64,
    /// Static-edit boxes recorded, and those merged into another: the scene
    /// keeps at most 1024 pending, and past them halves the list, merging
    /// 512 pairs at once.
    pub static_edit_boxes: u64,
    pub static_edit_boxes_merged: u64,
    /// The hardware path's builds recorded into frames: models' BLASes and
    /// the vertices they hold, deforming instances' BLASes and TLASes. A
    /// frame abandoned before it was submitted records its builds again.
    pub blas_builds: u64,
    pub blas_build_vertices: u64,
    pub deformed_blas_builds: u64,
    pub tlas_builds: u64,
    /// Models' BLASes compacted, and the vertices they hold.
    pub blas_compactions: u64,
    pub blas_compacted_vertices: u64,
}

#[cfg(any(test, feature = "diagnostics"))]
impl Counters {
    /// The bytes uploaded from every site.
    pub fn uploaded_bytes(&self) -> u64 {
        self.uploads.iter().map(|site| site.bytes).sum()
    }

    /// `step`'s calls and time.
    pub fn step(&self, step: BuildStep) -> StepTime {
        self.steps
            .iter()
            .find(|time| time.step == step)
            .copied()
            .unwrap_or(StepTime {
                step,
                calls: 0,
                nanoseconds: 0,
            })
    }

    /// What was counted after `earlier`, a snapshot of the same thread.
    pub fn since(&self, earlier: &Counters) -> Counters {
        let uploads = self
            .uploads
            .iter()
            .filter_map(|site| {
                let before = earlier
                    .uploads
                    .iter()
                    .find(|old| old.line == site.line && old.file == site.file);
                let bytes = site.bytes - before.map_or(0, |old| old.bytes);
                let writes = site.writes - before.map_or(0, |old| old.writes);
                (writes > 0).then(|| UploadSite {
                    bytes,
                    writes,
                    ..site.clone()
                })
            })
            .collect();
        let steps = self
            .steps
            .iter()
            .map(|time| {
                let before = earlier.step(time.step);
                StepTime {
                    calls: time.calls - before.calls,
                    nanoseconds: time.nanoseconds - before.nanoseconds,
                    ..*time
                }
            })
            .filter(|time| time.calls > 0)
            .collect();
        Counters {
            uploads,
            buffers_created: self.buffers_created - earlier.buffers_created,
            steps,
            ray_source_growths: self.ray_source_growths - earlier.ray_source_growths,
            geometry_growths: self.geometry_growths - earlier.geometry_growths,
            static_edit_boxes: self.static_edit_boxes - earlier.static_edit_boxes,
            static_edit_boxes_merged: self.static_edit_boxes_merged
                - earlier.static_edit_boxes_merged,
            blas_builds: self.blas_builds - earlier.blas_builds,
            blas_build_vertices: self.blas_build_vertices - earlier.blas_build_vertices,
            deformed_blas_builds: self.deformed_blas_builds - earlier.deformed_blas_builds,
            tlas_builds: self.tlas_builds - earlier.tlas_builds,
            blas_compactions: self.blas_compactions - earlier.blas_compactions,
            blas_compacted_vertices: self.blas_compacted_vertices - earlier.blas_compacted_vertices,
        }
    }
}

#[cfg(any(test, feature = "diagnostics"))]
thread_local! {
    static COUNTERS: std::cell::RefCell<Counters> = std::cell::RefCell::default();
}

/// Counts `bytes` written by one upload at the caller's call site.
#[cfg(any(test, feature = "diagnostics"))]
#[cfg_attr(any(test, feature = "diagnostics"), track_caller)]
fn upload(bytes: usize) {
    let at = std::panic::Location::caller();
    COUNTERS.with_borrow_mut(|counters| {
        let at_site = |site: &UploadSite| site.line == at.line() && site.file == at.file();
        if !counters.uploads.iter().any(at_site) {
            counters.uploads.push(UploadSite {
                file: at.file(),
                line: at.line(),
                bytes: 0,
                writes: 0,
            });
        }
        let site = counters
            .uploads
            .iter_mut()
            .find(|site| at_site(site))
            .unwrap();
        site.bytes += bytes as u64;
        site.writes += 1;
    });
}

/// `queue.write_buffer`, counted.
#[cfg_attr(any(test, feature = "diagnostics"), track_caller)]
pub(crate) fn write_buffer(queue: &wgpu::Queue, buffer: &wgpu::Buffer, offset: u64, data: &[u8]) {
    #[cfg(any(test, feature = "diagnostics"))]
    upload(data.len());
    queue.write_buffer(buffer, offset, data);
}

/// `queue.write_texture`, counted.
#[cfg_attr(any(test, feature = "diagnostics"), track_caller)]
pub(crate) fn write_texture(
    queue: &wgpu::Queue,
    texture: wgpu::TexelCopyTextureInfo<'_>,
    data: &[u8],
    layout: wgpu::TexelCopyBufferLayout,
    size: wgpu::Extent3d,
) {
    #[cfg(any(test, feature = "diagnostics"))]
    upload(data.len());
    queue.write_texture(texture, data, layout, size);
}

/// `device.create_buffer`, counted as a buffer created.
#[cfg_attr(any(test, feature = "diagnostics"), track_caller)]
pub(crate) fn buffer(
    device: &wgpu::Device,
    descriptor: &wgpu::BufferDescriptor<'_>,
) -> wgpu::Buffer {
    #[cfg(any(test, feature = "diagnostics"))]
    COUNTERS.with_borrow_mut(|counters| counters.buffers_created += 1);
    device.create_buffer(descriptor)
}

/// `device.create_buffer_init`, counted as an upload and a buffer created.
#[cfg_attr(any(test, feature = "diagnostics"), track_caller)]
pub(crate) fn buffer_init(
    device: &wgpu::Device,
    descriptor: &wgpu::util::BufferInitDescriptor<'_>,
) -> wgpu::Buffer {
    use wgpu::util::DeviceExt;
    #[cfg(any(test, feature = "diagnostics"))]
    {
        upload(descriptor.contents.len());
        COUNTERS.with_borrow_mut(|counters| counters.buffers_created += 1);
    }
    device.create_buffer_init(descriptor)
}

/// `device.create_texture_with_data`, counted as an upload.
#[cfg_attr(any(test, feature = "diagnostics"), track_caller)]
pub(crate) fn texture_init(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    descriptor: &wgpu::TextureDescriptor<'_>,
    order: wgpu::util::TextureDataOrder,
    data: &[u8],
) -> wgpu::Texture {
    use wgpu::util::DeviceExt;
    #[cfg(any(test, feature = "diagnostics"))]
    upload(data.len());
    device.create_texture_with_data(queue, descriptor, order, data)
}

/// A moment on the diagnostics clock, which `elapsed_ms` measures from:
/// `Instant` natively and the page's `performance.now()` in the browser,
/// with the feature; nothing without it, where every measure is zero.
#[derive(Clone, Copy)]
pub(crate) struct Moment {
    #[cfg(all(any(test, feature = "diagnostics"), not(target_arch = "wasm32")))]
    at: std::time::Instant,
    #[cfg(all(feature = "diagnostics", target_arch = "wasm32"))]
    at: f64,
}

impl Moment {
    pub fn now() -> Self {
        Self {
            #[cfg(all(any(test, feature = "diagnostics"), not(target_arch = "wasm32")))]
            at: std::time::Instant::now(),
            #[cfg(all(feature = "diagnostics", target_arch = "wasm32"))]
            at: performance_now(),
        }
    }

    /// Milliseconds since this moment.
    pub fn elapsed_ms(self) -> f64 {
        #[cfg(all(any(test, feature = "diagnostics"), not(target_arch = "wasm32")))]
        let elapsed = self.at.elapsed().as_secs_f64() * 1e3;
        #[cfg(all(feature = "diagnostics", target_arch = "wasm32"))]
        let elapsed = performance_now() - self.at;
        #[cfg(not(any(
            all(any(test, feature = "diagnostics"), not(target_arch = "wasm32")),
            all(feature = "diagnostics", target_arch = "wasm32")
        )))]
        let elapsed = {
            let _ = self;
            0.
        };
        elapsed
    }
}

/// The page's or worker's `performance.now()`, in milliseconds; zero where
/// the global has none.
#[cfg(all(feature = "diagnostics", target_arch = "wasm32"))]
fn performance_now() -> f64 {
    use js_sys::wasm_bindgen::{JsCast, JsValue};
    thread_local! {
        static NOW: Option<(JsValue, js_sys::Function)> = {
            let performance = js_sys::Reflect::get(&js_sys::global(), &"performance".into()).ok();
            performance.and_then(|performance| {
                let now = js_sys::Reflect::get(&performance, &"now".into()).ok()?;
                Some((performance, now.dyn_into().ok()?))
            })
        };
    }
    NOW.with(|now| {
        now.as_ref()
            .and_then(|(performance, now)| now.call0(performance).ok()?.as_f64())
            .unwrap_or(0.)
    })
}

/// Runs `work`, one `step` of building, counting its calls and its time.
pub(crate) fn step<T>(step: BuildStep, work: impl FnOnce() -> T) -> T {
    #[cfg(any(test, feature = "diagnostics"))]
    {
        let started = Moment::now();
        let result = work();
        let nanoseconds = (started.elapsed_ms() * 1e6) as u64;
        COUNTERS.with_borrow_mut(|counters| {
            if !counters.steps.iter().any(|time| time.step == step) {
                counters.steps.push(StepTime {
                    step,
                    calls: 0,
                    nanoseconds: 0,
                });
            }
            let time = counters
                .steps
                .iter_mut()
                .find(|time| time.step == step)
                .unwrap();
            time.calls += 1;
            time.nanoseconds += nanoseconds;
        });
        result
    }
    #[cfg(not(any(test, feature = "diagnostics")))]
    {
        let _ = step;
        work()
    }
}

/// Counts a growth of the ray source, which copies it whole.
pub(crate) fn ray_source_growth() {
    #[cfg(any(test, feature = "diagnostics"))]
    COUNTERS.with_borrow_mut(|counters| counters.ray_source_growths += 1);
}

/// Counts a growth of a geometry slab, which copies it whole.
pub(crate) fn geometry_growth() {
    #[cfg(any(test, feature = "diagnostics"))]
    COUNTERS.with_borrow_mut(|counters| counters.geometry_growths += 1);
}

/// Counts a static-edit box recorded, and the `merged` pairs of pending
/// boxes that recording it merged.
pub(crate) fn static_edit(merged: usize) {
    #[cfg(any(test, feature = "diagnostics"))]
    COUNTERS.with_borrow_mut(|counters| {
        counters.static_edit_boxes += 1;
        counters.static_edit_boxes_merged += merged as u64;
    });
    #[cfg(not(any(test, feature = "diagnostics")))]
    let _ = merged;
}

/// Counts a model's BLAS of `vertices` built into a frame.
pub(crate) fn blas_build(vertices: u32) {
    #[cfg(any(test, feature = "diagnostics"))]
    COUNTERS.with_borrow_mut(|counters| {
        counters.blas_builds += 1;
        counters.blas_build_vertices += u64::from(vertices);
    });
    #[cfg(not(any(test, feature = "diagnostics")))]
    let _ = vertices;
}

/// Counts a deforming instance's BLAS built into a frame.
pub(crate) fn deformed_blas_build() {
    #[cfg(any(test, feature = "diagnostics"))]
    COUNTERS.with_borrow_mut(|counters| counters.deformed_blas_builds += 1);
}

/// Counts a TLAS built into a frame.
pub(crate) fn tlas_build() {
    #[cfg(any(test, feature = "diagnostics"))]
    COUNTERS.with_borrow_mut(|counters| counters.tlas_builds += 1);
}

/// Counts a model's BLAS of `vertices` compacted.
pub(crate) fn blas_compaction(vertices: u32) {
    #[cfg(any(test, feature = "diagnostics"))]
    COUNTERS.with_borrow_mut(|counters| {
        counters.blas_compactions += 1;
        counters.blas_compacted_vertices += u64::from(vertices);
    });
    #[cfg(not(any(test, feature = "diagnostics")))]
    let _ = vertices;
}

/// This thread's counters so far.
#[cfg(any(test, feature = "diagnostics"))]
pub(crate) fn snapshot() -> Counters {
    COUNTERS.with_borrow(Clone::clone)
}