cranpose-render-wgpu 0.9.21

GPU renderer for Cranpose scenes through wgpu
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
use std::{
    hash::Hasher,
    io::Write,
    path::{Path, PathBuf},
    sync::{
        Arc, Condvar, Mutex, MutexGuard, OnceLock, PoisonError,
        atomic::{AtomicBool, Ordering},
    },
};

use cranpose_ui_graphics::FxHasher;
use web_time::{Duration, Instant};

use crate::{
    debug_toggles::DebugToggle,
    pipeline_recorder::PipelineRecorder,
    pipeline_records::{self, PipelineRecords, Recorded, ShaderPipelineRecord},
};

static DISK_CACHE: DebugToggle = DebugToggle::new("CRANPOSE_PIPELINE_DISK_CACHE");

fn disk_cache_enabled() -> bool {
    !DISK_CACHE.equals("0")
}

static HOST_FILE: OnceLock<PathBuf> = OnceLock::new();

/// Names the file the driver's compiled pipelines are kept in between runs.
///
/// This crate renders and does not know where an application may write, so the
/// platform backend passes the path in once, before the first device is
/// created. Without it the cache lives and dies with the process, and every
/// launch pays the driver's compile again. A later call is ignored: the
/// loaded blob and the watcher that writes it back must name one file.
pub fn set_file_path(path: PathBuf) {
    let _ = HOST_FILE.set(path);
}

pub(crate) fn file_path() -> Option<PathBuf> {
    if !disk_cache_enabled() {
        return None;
    }
    match crate::debug_toggles::debug_toggle_os("CRANPOSE_PIPELINE_CACHE_FILE") {
        Some(path) if !path.is_empty() => Some(PathBuf::from(path)),
        _ => HOST_FILE.get().cloned(),
    }
}

/// What the last launches left for this one: the driver's compiled
/// pipelines, where the device keeps them, and the pipelines those launches
/// drew with.
pub(crate) struct Loaded {
    pub(crate) cache: Option<wgpu::PipelineCache>,
    pub(crate) records: Arc<PipelineRecords>,
    /// Whether another build wrote the records: an update, whose compiled
    /// pipelines are gone.
    pub(crate) updated: bool,
    /// Whether the driver cache was filled from this build's compiled
    /// pipelines, so a recorded pipeline builds from it as a cache hit.
    pub(crate) holds_recorded: bool,
}

pub(crate) fn load(device: &wgpu::Device) -> Loaded {
    let path = file_path();
    let file = path.as_deref().and_then(|path| match std::fs::read(path) {
        Ok(bytes) => Some(bytes),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => None,
        Err(error) => {
            log::warn!("[pipeline-cache] unreadable {path:?}: {error}");
            None
        }
    });
    let contents = file.as_deref().and_then(current_contents);
    let updated = contents
        .as_ref()
        .is_some_and(|contents| !contents.this_build);
    let data = contents
        .as_ref()
        .and_then(|contents| contents.blob)
        .filter(|blob| !blob.is_empty());
    let cache = driver_cache(device, data, file.as_deref(), updated);
    Loaded {
        holds_recorded: data.is_some() && cache.is_some(),
        cache,
        records: Arc::new(contents.map(Contents::into_records).unwrap_or_default()),
        updated,
    }
}

/// Starts the cache file at `path` from `seed`, a cache file an app ships
/// for its fresh installs, unless the file exists. A seed another GPU or
/// driver wrote counts as another build's: its records name what the app
/// draws, but its compiled pipelines are that driver's, so it is written
/// under a build key no build has and loads without them.
pub fn seed(path: &Path, mut bytes: Vec<u8>, same_driver: bool) {
    if path.exists() {
        return;
    }
    if !same_driver && let Some(build) = bytes.get_mut(..blob_key().len()) {
        build.fill(0);
    }
    if let Some(parent) = path.parent()
        && let Err(error) = std::fs::create_dir_all(parent)
    {
        log::warn!("[pipeline-cache] create_dir_all {parent:?}: {error}");
        return;
    }
    match write_file(path, &bytes) {
        Ok(()) => log::info!(
            "[pipeline-cache] seeded {} B from the app{}",
            bytes.len(),
            if same_driver { "" } else { ", records only" },
        ),
        Err(error) => log::warn!("[pipeline-cache] seeding {path:?}: {error}"),
    }
}

/// The device's pipeline cache, filled from `data` when the file had this
/// build's.
fn driver_cache(
    device: &wgpu::Device,
    data: Option<&[u8]>,
    file: Option<&[u8]>,
    updated: bool,
) -> Option<wgpu::PipelineCache> {
    if !device.features().contains(wgpu::Features::PIPELINE_CACHE) {
        log::info!(
            "[pipeline-cache] not offered by {:?}; compiled pipelines persist only as far \
             as the driver's own cache does",
            device.adapter_info().backend
        );
        return None;
    }
    // SAFETY: `data` is this build's own `get_data` output, and `fallback:
    // true` has wgpu validate the header and fall back to an empty cache.
    #[expect(unsafe_code)]
    let cache = unsafe {
        device.create_pipeline_cache(&wgpu::PipelineCacheDescriptor {
            label: Some("cranpose pipeline disk cache"),
            data,
            fallback: true,
        })
    };
    match (data, file) {
        (Some(data), _) => log::info!("[pipeline-cache] loaded {} B from disk", data.len()),
        (None, Some(file)) if updated => log::info!(
            "[pipeline-cache] cold: dropped {} B compiled from other shaders",
            file.len()
        ),
        (None, _) => log::info!("[pipeline-cache] cold (no blob on disk)"),
    }
    Some(cache)
}

const FILE_LAYOUT: u32 = 4;

/// Names what fills a file: its layout, the framework's WGSL sources, and
/// this crate's version, which changes with each release of the shader
/// rewrites, pipeline layouts and translator between those sources and the
/// driver, and with every pipeline key's layout.
///
/// The driver keeps every pipeline of the blob it loads in the cache it saves,
/// so a blob kept across shader changes only grows, and the driver holds all
/// of it resident. A file under another key loads cold and is replaced.
fn blob_key() -> [u8; 8] {
    let mut hasher = FxHasher::default();
    hasher.write_u32(FILE_LAYOUT);
    hasher.write_u64(cranpose_ui_graphics::framework_shaders::SOURCES_KEY);
    hasher.write(env!("CARGO_PKG_VERSION").as_bytes());
    hasher.finish().to_le_bytes()
}

struct Contents<'a> {
    first_screen: FirstScreenKeys<'a>,
    records: PipelineRecords,
    /// Whether this build wrote the file.
    this_build: bool,
    blob: Option<&'a [u8]>,
}

impl Contents<'_> {
    /// The records, the last first screen's shapes among them.
    fn into_records(self) -> PipelineRecords {
        let mut records = self.records;
        records.shapes.splice(
            0..0,
            self.first_screen.map(|key| Recorded {
                entry: key,
                age: 0,
                first_screen: true,
            }),
        );
        records
    }
}

#[derive(Clone)]
struct FirstScreenKeys<'a>(&'a [u8]);

impl Iterator for FirstScreenKeys<'_> {
    type Item = u64;

    fn next(&mut self) -> Option<u64> {
        let (key, rest) = self.0.split_first_chunk::<8>()?;
        self.0 = rest;
        Some(u64::from_le_bytes(*key))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.len(), Some(self.len()))
    }
}

impl ExactSizeIterator for FirstScreenKeys<'_> {
    fn len(&self) -> usize {
        self.0.len() / 8
    }
}

fn current_contents(file: &[u8]) -> Option<Contents<'_>> {
    let (build, rest) = file.split_first_chunk::<8>()?;
    let rest = rest.strip_prefix(crate::render::ShapePipelineKey::DISK_LAYOUT.as_slice())?;
    let (count, rest) = rest.split_first_chunk::<4>()?;
    let keys_len = usize::try_from(u32::from_le_bytes(*count))
        .ok()?
        .checked_mul(8)?;
    let (keys, rest) = rest.split_at_checked(keys_len)?;
    // The shape keys stand on their own: records that do not decode cost
    // only themselves and the driver blob behind them.
    let (records, blob) = pipeline_records::decode(rest).map_or_else(
        || (PipelineRecords::default(), None),
        |(records, blob)| (records, Some(blob)),
    );
    let this_build = *build == blob_key();
    Some(Contents {
        first_screen: FirstScreenKeys(keys),
        records,
        this_build,
        blob: blob.filter(|_| this_build),
    })
}

/// The file holding `shapes`, the first screen's ahead of the records, the
/// other records, and `blob`.
fn file_bytes<'a>(
    shapes: impl Iterator<Item = Recorded<u64>> + Clone,
    shaders: impl Iterator<Item = Recorded<&'a ShaderPipelineRecord>>,
    fixed: impl Iterator<Item = Recorded<&'a str>>,
    blob: &[u8],
) -> Option<Vec<u8>> {
    let mut bytes = Vec::with_capacity(4096 + blob.len());
    bytes.extend_from_slice(&blob_key());
    bytes.extend_from_slice(&crate::render::ShapePipelineKey::DISK_LAYOUT);
    let first_screen = shapes.clone().filter(|shape| shape.first_screen);
    bytes.extend_from_slice(
        &u32::try_from(first_screen.clone().count())
            .ok()?
            .to_le_bytes(),
    );
    for shape in first_screen {
        bytes.extend_from_slice(&shape.entry.to_le_bytes());
    }
    pipeline_records::encode(
        shapes.filter(|shape| !shape.first_screen),
        shaders,
        fixed,
        &mut bytes,
    )?;
    bytes.extend_from_slice(blob);
    Some(bytes)
}

fn write_file(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
    let mut file = std::fs::File::create(path)?;
    file.write_all(bytes)
}

pub(crate) fn persist(
    cache: Option<&wgpu::PipelineCache>,
    path: &Path,
    previous: &PipelineRecords,
    recorder: &PipelineRecorder,
) {
    let started = Instant::now();
    let blob = cache
        .and_then(wgpu::PipelineCache::get_data)
        .unwrap_or_default();
    let existing = std::fs::read(path).ok();
    // A launch closed before its first frame keeps the last launch's records.
    let (bytes, key_count) = if recorder.frame_drawn() {
        let drawn = recorder.drawn();
        let shapes = recent(
            drawn.shapes.iter().copied(),
            previous.shapes.iter().copied(),
        );
        let shaders = recent(
            drawn.shaders.iter().map(Recorded::as_ref),
            previous.shaders.iter().map(Recorded::as_ref),
        );
        let fixed = recent(
            drawn.fixed.iter().copied(),
            previous
                .fixed
                .iter()
                .map(|label| label.as_ref().map(String::as_str)),
        );
        let count = shapes.clone().count() + shaders.clone().count() + fixed.clone().count();
        (file_bytes(shapes, shaders, fixed, &blob), count)
    } else {
        (
            file_bytes(
                previous.shapes.iter().copied(),
                previous.shaders.iter().map(Recorded::as_ref),
                previous
                    .fixed
                    .iter()
                    .map(|label| label.as_ref().map(String::as_str)),
                &blob,
            ),
            previous.shapes.len() + previous.shaders.len() + previous.fixed.len(),
        )
    };
    let Some(bytes) = bytes else {
        return;
    };
    if existing.is_some_and(|existing| existing == bytes) {
        return;
    }
    if let Some(parent) = path.parent()
        && let Err(error) = std::fs::create_dir_all(parent)
    {
        log::warn!("[pipeline-cache] create_dir_all {parent:?}: {error}");
        return;
    }
    let tmp = path.with_extension("tmp");
    let written = write_file(&tmp, &bytes).and_then(|()| std::fs::rename(&tmp, path));
    match written {
        Ok(()) => log::info!(
            "[pipeline-cache] persisted {} B and {} recorded pipelines in {:.1} ms",
            blob.len(),
            key_count,
            crate::render::instant_ms(started, Instant::now()),
        ),
        Err(error) => log::warn!("[pipeline-cache] write {path:?}: {error}"),
    }
}

/// How many launches a pipeline stays recorded after the last that drew
/// with it.
const RECORDED_LAUNCHES: u8 = 4;
/// The most pipelines of each kind the file records.
const MAX_RECORDS: usize = 128;

/// This launch's pipelines, then those of the previous launches it did not
/// draw with, a launch older: each while it is younger than
/// [`RECORDED_LAUNCHES`], at most [`MAX_RECORDS`] of them.
fn recent<T: Copy + PartialEq>(
    session: impl Iterator<Item = Recorded<T>> + Clone,
    previous: impl Iterator<Item = Recorded<T>> + Clone,
) -> impl Iterator<Item = Recorded<T>> + Clone {
    let drawn = session.clone();
    session
        .chain(previous.filter_map(move |record| {
            let unseen = !drawn.clone().any(|drawn| drawn.entry == record.entry);
            (unseen && record.age + 1 < RECORDED_LAUNCHES).then_some(Recorded {
                age: record.age + 1,
                first_screen: false,
                ..record
            })
        }))
        .take(MAX_RECORDS)
}

/// How long pipeline builds must pause before the cache is written. A burst
/// of builds, such as a screen's first frames, is written once after its last
/// build, and a session closed a moment after launch still keeps what the
/// launch compiled, so the next launch skips those compiles.
const PERSIST_QUIET: Duration = Duration::from_millis(500);

static CHANGES: PersistSignal = PersistSignal::new();

/// Counts a change the cache file should keep, such as a pipeline build,
/// and wakes the watchers waiting to write it.
pub(crate) fn note_change() {
    CHANGES.note_change();
}

struct PersistSignal {
    revision: Mutex<u64>,
    changed: Condvar,
}

/// Why a watcher woke, with the build count at that moment.
#[derive(Debug, PartialEq, Eq)]
enum Wake {
    /// Builds past the written count paused for the quiet period.
    Quiet(u64),
    /// The watcher's owner let it go.
    Stopped(u64),
}

impl PersistSignal {
    const fn new() -> Self {
        Self {
            revision: Mutex::new(0),
            changed: Condvar::new(),
        }
    }

    fn lock(&self) -> MutexGuard<'_, u64> {
        self.revision.lock().unwrap_or_else(PoisonError::into_inner)
    }

    fn note_change(&self) {
        *self.lock() += 1;
        self.changed.notify_all();
    }

    fn stop(&self, stopped: &AtomicBool) {
        stopped.store(true, Ordering::Release);
        // Holding the lock orders the flag against a watcher between its
        // check and its wait, so the wake below cannot be lost.
        let _held = self.lock();
        self.changed.notify_all();
    }

    fn wait_for_quiet(&self, written: u64, quiet: Duration, stopped: &AtomicBool) -> Wake {
        let is_stopped = || stopped.load(Ordering::Acquire);
        let mut revision = self
            .changed
            .wait_while(self.lock(), |revision| {
                *revision == written && !is_stopped()
            })
            .unwrap_or_else(PoisonError::into_inner);
        loop {
            if is_stopped() {
                return Wake::Stopped(*revision);
            }
            let seen = *revision;
            let (next, waited) = self
                .changed
                .wait_timeout_while(revision, quiet, |revision| {
                    *revision == seen && !is_stopped()
                })
                .unwrap_or_else(PoisonError::into_inner);
            revision = next;
            if waited.timed_out() {
                return Wake::Quiet(*revision);
            }
        }
    }
}

/// Keeps the cache's watcher writing new pipelines to disk. Dropping it
/// writes what is still unwritten and ends the watcher.
pub(crate) struct PersistWatcher {
    stopped: Arc<AtomicBool>,
    thread: Option<std::thread::JoinHandle<()>>,
}

impl Drop for PersistWatcher {
    fn drop(&mut self) {
        CHANGES.stop(&self.stopped);
        if let Some(thread) = self.thread.take()
            && thread.join().is_err()
        {
            log::warn!("[pipeline-cache] persist thread panicked during shutdown");
        }
    }
}

pub(crate) fn spawn_persist_watcher(
    cache: Option<wgpu::PipelineCache>,
    previous: Arc<PipelineRecords>,
    recorder: PipelineRecorder,
) -> Option<PersistWatcher> {
    let path = file_path()?;
    let stopped = Arc::new(AtomicBool::new(false));
    let watcher_stopped = Arc::clone(&stopped);
    let mut written = *CHANGES.lock();
    let spawned = std::thread::Builder::new()
        .name("cranpose-pl-cache".into())
        .spawn(move || {
            loop {
                match CHANGES.wait_for_quiet(written, PERSIST_QUIET, &watcher_stopped) {
                    Wake::Quiet(revision) => {
                        persist(cache.as_ref(), &path, &previous, &recorder);
                        written = revision;
                    }
                    Wake::Stopped(revision) => {
                        if revision != written {
                            persist(cache.as_ref(), &path, &previous, &recorder);
                        }
                        return;
                    }
                }
            }
        });
    match spawned {
        Ok(thread) => Some(PersistWatcher {
            stopped,
            thread: Some(thread),
        }),
        Err(error) => {
            log::warn!("[pipeline-cache] persist thread failed to spawn: {error}");
            None
        }
    }
}

#[cfg(test)]
#[path = "tests/pipeline_disk_cache_tests.rs"]
mod tests;