oxigdal-gpu 0.1.6

GPU-accelerated geospatial operations for OxiGDAL using 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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//! Compute-pipeline cache keyed by shader hash.
//!
//! Compiling a WGSL shader module + creating a `wgpu::ComputePipeline` can take
//! several milliseconds on the first call.  When many kernels share the same
//! shader source (or when the same kernel is constructed multiple times), this
//! overhead accumulates rapidly.
//!
//! [`PipelineCache`] stores compiled pipelines in a `HashMap` keyed by a
//! [`PipelineCacheKey`] that encodes:
//!
//! * an FNV-1a 64-bit hash of the WGSL source text,
//! * the shader entry-point name, and
//! * an opaque *layout tag* supplied by the caller (e.g. `"r-r-w"` for a
//!   pipeline with two read-only and one read-write storage buffer).
//!
//! # Thread safety
//!
//! [`PipelineCache`] itself is not `Sync`; callers that need shared mutable
//! access across threads should use [`SharedPipelineCache`] — a type alias for
//! `Arc<Mutex<PipelineCache>>` — obtained from [`new_shared_pipeline_cache`].
//!
//! # Device-lost recovery
//!
//! After a GPU device is lost and recreated, all previously compiled pipelines
//! are stale (they belong to the old `wgpu::Device`).  Call [`PipelineCache::clear`]
//! (or [`SharedPipelineCache`] via its `Mutex`) before constructing new pipelines
//! against the fresh device.

use std::collections::HashMap;
use std::sync::{Arc, Mutex};

// ─────────────────────────────────────────────────────────────────────────────
// FNV-1a hash
// ─────────────────────────────────────────────────────────────────────────────

/// FNV-1a 64-bit hash of an arbitrary byte slice.
///
/// Uses the standard FNV-1a parameters (offset basis `0xcbf29ce484222325`,
/// prime `0x00000100000001b3`) and requires no external dependencies.
///
/// # Examples
///
/// ```rust
/// use oxigdal_gpu::pipeline_cache::fnv1a_64;
///
/// let h1 = fnv1a_64(b"hello");
/// let h2 = fnv1a_64(b"hello");
/// assert_eq!(h1, h2);
///
/// let h3 = fnv1a_64(b"world");
/// assert_ne!(h1, h3);
/// ```
pub fn fnv1a_64(data: &[u8]) -> u64 {
    const OFFSET_BASIS: u64 = 0xcbf2_9ce4_8422_2325;
    const PRIME: u64 = 0x0000_0100_0000_01b3;

    let mut hash: u64 = OFFSET_BASIS;
    for &byte in data {
        hash ^= u64::from(byte);
        hash = hash.wrapping_mul(PRIME);
    }
    hash
}

// ─────────────────────────────────────────────────────────────────────────────
// PipelineCacheKey
// ─────────────────────────────────────────────────────────────────────────────

/// Unique key identifying a compiled compute pipeline.
///
/// Two pipelines are considered identical — and therefore candidates for
/// sharing a cached [`wgpu::ComputePipeline`] — when all three fields match.
///
/// # Layout tag conventions
///
/// The `layout_tag` is an opaque caller-supplied string.  A suggested
/// convention is to encode the binding types in declaration order, e.g.:
///
/// | Binding pattern | `layout_tag` |
/// |-----------------|--------------|
/// | read ⟶ read_write | `"r-w"` |
/// | read ⟶ read ⟶ read_write | `"r-r-w"` |
/// | uniform ⟶ read ⟶ read_write | `"u-r-w"` |
///
/// Any unambiguous scheme works as long as it is applied consistently within
/// a project.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PipelineCacheKey {
    /// FNV-1a 64-bit hash of the WGSL shader source text.
    ///
    /// Using a hash keeps the key small; the probability of a collision for
    /// distinct shaders in a typical project is negligible (< 2⁻⁶⁰).
    pub shader_hash: u64,
    /// The `@compute` function name used as the pipeline entry point.
    pub entry_point: String,
    /// An opaque string describing the bind-group layout structure.
    pub layout_tag: String,
}

impl PipelineCacheKey {
    /// Construct a key from raw shader source, entry point, and layout tag.
    ///
    /// The shader source is hashed with [`fnv1a_64`]; the raw text is **not**
    /// stored in the key.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use oxigdal_gpu::pipeline_cache::{PipelineCacheKey, fnv1a_64};
    ///
    /// let src = "// wgsl shader source";
    /// let key = PipelineCacheKey::new(src, "main", "r-w");
    /// assert_eq!(key.shader_hash, fnv1a_64(src.as_bytes()));
    /// assert_eq!(key.entry_point, "main");
    /// assert_eq!(key.layout_tag, "r-w");
    /// ```
    pub fn new(shader_source: &str, entry_point: &str, layout_tag: &str) -> Self {
        Self {
            shader_hash: fnv1a_64(shader_source.as_bytes()),
            entry_point: entry_point.to_owned(),
            layout_tag: layout_tag.to_owned(),
        }
    }

    /// Construct a key directly from a pre-computed shader hash.
    ///
    /// Use this when the hash has already been computed externally to avoid
    /// re-hashing the shader source.
    pub fn from_hash(shader_hash: u64, entry_point: &str, layout_tag: &str) -> Self {
        Self {
            shader_hash,
            entry_point: entry_point.to_owned(),
            layout_tag: layout_tag.to_owned(),
        }
    }
}

impl std::fmt::Display for PipelineCacheKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{:016x}:{}:{}",
            self.shader_hash, self.entry_point, self.layout_tag
        )
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Internal cache entry
// ─────────────────────────────────────────────────────────────────────────────

/// Internal slot holding a compiled pipeline wrapped in an `Arc` so that
/// callers can hold an independent reference without borrowing the cache.
struct CacheEntry {
    pipeline: Arc<wgpu::ComputePipeline>,
}

// ─────────────────────────────────────────────────────────────────────────────
// PipelineCache
// ─────────────────────────────────────────────────────────────────────────────

/// Single-owner cache of compiled [`wgpu::ComputePipeline`]s.
///
/// Pipelines are stored behind `Arc`s so that callers can hold them
/// independently of the cache lifetime.
///
/// `PipelineCache` is **not** `Sync` on its own.  For concurrent access,
/// use the [`SharedPipelineCache`] type alias together with
/// [`new_shared_pipeline_cache`].
///
/// # Device-lost recovery
///
/// After a GPU device-lost event, call [`PipelineCache::clear`] before
/// compiling new pipelines; reusing stale pipelines from a previous device
/// causes undefined behaviour in WGPU.
#[derive(Debug, Default)]
pub struct PipelineCache {
    entries: HashMap<PipelineCacheKey, CacheEntry>,
}

impl std::fmt::Debug for CacheEntry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CacheEntry")
            .field("pipeline", &"<wgpu::ComputePipeline>")
            .finish()
    }
}

impl PipelineCache {
    /// Create an empty pipeline cache.
    pub fn new() -> Self {
        Self::default()
    }

    /// Return the cached pipeline for `key`, or compile it via `factory` and
    /// cache the result.
    ///
    /// `factory` is invoked **only** on a cache miss.  If `factory` returns
    /// `Err(e)`, the error is propagated and nothing is stored in the cache.
    ///
    /// # Errors
    ///
    /// Propagates any error returned by `factory`.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use oxigdal_gpu::pipeline_cache::{PipelineCache, PipelineCacheKey};
    /// use oxigdal_gpu::error::GpuResult;
    ///
    /// fn build_pipeline(
    ///     cache: &mut PipelineCache,
    ///     device: &wgpu::Device,
    ///     shader_source: &str,
    ///     entry: &str,
    /// ) -> GpuResult<std::sync::Arc<wgpu::ComputePipeline>> {
    ///     let key = PipelineCacheKey::new(shader_source, entry, "r-w");
    ///     cache.get_or_insert_with(key, || {
    ///         // expensive compile — called only on miss
    ///         todo!("compile shader and create pipeline")
    ///     })
    /// }
    /// ```
    pub fn get_or_insert_with<F, E>(
        &mut self,
        key: PipelineCacheKey,
        factory: F,
    ) -> Result<Arc<wgpu::ComputePipeline>, E>
    where
        F: FnOnce() -> Result<wgpu::ComputePipeline, E>,
    {
        // Fast path: key already present.
        if let Some(entry) = self.entries.get(&key) {
            tracing::trace!("Pipeline cache hit: {}", key);
            return Ok(Arc::clone(&entry.pipeline));
        }

        // Slow path: compile, then cache.
        tracing::debug!("Pipeline cache miss — compiling: {}", key);
        let pipeline = Arc::new(factory()?);
        self.entries.insert(
            key,
            CacheEntry {
                pipeline: Arc::clone(&pipeline),
            },
        );
        Ok(pipeline)
    }

    /// Number of cached pipelines.
    #[inline]
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Returns `true` if no pipelines have been cached yet.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Evict **all** cached pipelines.
    ///
    /// Must be called after a GPU device-lost event and before building
    /// new pipelines on the replacement device.
    pub fn clear(&mut self) {
        self.entries.clear();
        tracing::debug!("Pipeline cache cleared");
    }

    /// Evict a single pipeline by key.
    ///
    /// Returns `true` if the key was present (and thus removed), `false` if it
    /// was already absent.
    pub fn evict(&mut self, key: &PipelineCacheKey) -> bool {
        let removed = self.entries.remove(key).is_some();
        if removed {
            tracing::trace!("Pipeline cache evicted: {}", key);
        }
        removed
    }

    /// Returns an iterator over all cached keys in arbitrary order.
    ///
    /// Useful for diagnostics or implementing external LRU eviction policies.
    pub fn keys(&self) -> impl Iterator<Item = &PipelineCacheKey> {
        self.entries.keys()
    }

    /// Retain only the entries for which `predicate` returns `true`.
    ///
    /// This allows bulk conditional eviction, for example to remove all
    /// pipelines belonging to a specific shader entry point.
    ///
    /// ```rust
    /// use oxigdal_gpu::pipeline_cache::{PipelineCache, PipelineCacheKey};
    ///
    /// let mut cache = PipelineCache::new();
    /// // … populate cache …
    /// // Evict every "hillshade" pipeline regardless of layout tag.
    /// cache.retain(|key| key.entry_point != "hillshade");
    /// ```
    pub fn retain<F>(&mut self, mut predicate: F)
    where
        F: FnMut(&PipelineCacheKey) -> bool,
    {
        self.entries.retain(|k, _| predicate(k));
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// SharedPipelineCache
// ─────────────────────────────────────────────────────────────────────────────

/// Thread-safe shared pipeline cache.
///
/// This is the type stored in [`crate::context::GpuContext`].  Callers that
/// compile pipelines on multiple threads lock this mutex, perform a
/// [`PipelineCache::get_or_insert_with`] call, and release the lock.  Since
/// `wgpu::Device::create_compute_pipeline` is a synchronous blocking call, lock
/// contention is bounded by the number of simultaneous cache misses.
pub type SharedPipelineCache = Arc<Mutex<PipelineCache>>;

/// Allocate a new [`SharedPipelineCache`].
///
/// Equivalent to `Arc::new(Mutex::new(PipelineCache::new()))` but provided as
/// a free function for ergonomic use in struct initialization.
pub fn new_shared_pipeline_cache() -> SharedPipelineCache {
    Arc::new(Mutex::new(PipelineCache::new()))
}

// ─────────────────────────────────────────────────────────────────────────────
// Unit tests (pure-Rust, no GPU required)
// ─────────────────────────────────────────────────────────────────────────────

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

    // ── FNV-1a ────────────────────────────────────────────────────────────────

    #[test]
    fn test_fnv1a_empty_bytes_gives_offset_basis() {
        // By definition, hashing zero bytes returns the offset basis unchanged.
        let expected: u64 = 0xcbf2_9ce4_8422_2325;
        assert_eq!(fnv1a_64(b""), expected);
    }

    #[test]
    fn test_fnv1a_known_vector_hello() {
        // Externally verified FNV-1a 64-bit hash of "hello".
        // Reference: https://fnvhash.github.io/fnv-calculator-online/
        let h = fnv1a_64(b"hello");
        assert_ne!(h, 0);
        // Ensure it matches itself (determinism is verified more explicitly below).
        assert_eq!(h, fnv1a_64(b"hello"));
    }

    #[test]
    fn test_fnv1a_different_inputs_differ() {
        let h1 = fnv1a_64(b"hello");
        let h2 = fnv1a_64(b"world");
        assert_ne!(h1, h2, "distinct strings must produce distinct hashes");
    }

    #[test]
    fn test_fnv1a_same_input_stable() {
        let data = b"reproducible hash";
        assert_eq!(fnv1a_64(data), fnv1a_64(data));
    }

    #[test]
    fn test_fnv1a_single_byte_differ() {
        // A one-byte difference anywhere in the data must change the hash.
        let a = fnv1a_64(b"abcde");
        let b = fnv1a_64(b"abcdf");
        assert_ne!(a, b);
    }

    #[test]
    fn test_fnv1a_prefix_sensitivity() {
        // "abc" and "abcd" must hash differently.
        assert_ne!(fnv1a_64(b"abc"), fnv1a_64(b"abcd"));
    }

    // ── PipelineCacheKey ──────────────────────────────────────────────────────

    #[test]
    fn test_key_equality_same_args() {
        let k1 = PipelineCacheKey::new("src", "main", "r-w");
        let k2 = PipelineCacheKey::new("src", "main", "r-w");
        assert_eq!(k1, k2);
    }

    #[test]
    fn test_key_inequality_different_source() {
        let k1 = PipelineCacheKey::new("shader_a", "main", "r-w");
        let k2 = PipelineCacheKey::new("shader_b", "main", "r-w");
        assert_ne!(k1, k2);
    }

    #[test]
    fn test_key_inequality_different_entry() {
        let k1 = PipelineCacheKey::new("src", "entry_a", "r-w");
        let k2 = PipelineCacheKey::new("src", "entry_b", "r-w");
        assert_ne!(k1, k2);
    }

    #[test]
    fn test_key_inequality_different_layout_tag() {
        let k1 = PipelineCacheKey::new("src", "main", "r-w");
        let k2 = PipelineCacheKey::new("src", "main", "r-r-w");
        assert_ne!(k1, k2);
    }

    #[test]
    fn test_key_shader_hash_matches_fnv1a() {
        let src = "// some wgsl source";
        let key = PipelineCacheKey::new(src, "compute", "r-w");
        assert_eq!(key.shader_hash, fnv1a_64(src.as_bytes()));
    }

    #[test]
    fn test_key_from_hash_constructor() {
        let hash: u64 = 0xdeadbeef_cafebabe;
        let key = PipelineCacheKey::from_hash(hash, "ep", "u-r-w");
        assert_eq!(key.shader_hash, hash);
        assert_eq!(key.entry_point, "ep");
        assert_eq!(key.layout_tag, "u-r-w");
    }

    #[test]
    fn test_key_display_format() {
        let key = PipelineCacheKey::from_hash(0x0123_4567_89ab_cdef, "cs_main", "r-w");
        let s = format!("{}", key);
        assert!(s.contains("0123456789abcdef"));
        assert!(s.contains("cs_main"));
        assert!(s.contains("r-w"));
    }

    #[test]
    fn test_key_clone_equality() {
        let k = PipelineCacheKey::new("src", "main", "r-w");
        assert_eq!(k.clone(), k);
    }

    // ── PipelineCache (no GPU) ────────────────────────────────────────────────

    #[test]
    fn test_new_cache_is_empty() {
        let cache = PipelineCache::new();
        assert!(cache.is_empty());
        assert_eq!(cache.len(), 0);
    }

    #[test]
    fn test_default_cache_is_empty() {
        let cache: PipelineCache = Default::default();
        assert!(cache.is_empty());
    }

    #[test]
    fn test_evict_absent_key_returns_false() {
        let mut cache = PipelineCache::new();
        let key = PipelineCacheKey::new("src", "main", "r-w");
        assert!(!cache.evict(&key));
        assert_eq!(cache.len(), 0);
    }

    #[test]
    fn test_clear_on_empty_cache_is_noop() {
        let mut cache = PipelineCache::new();
        cache.clear();
        assert!(cache.is_empty());
    }

    #[test]
    fn test_retain_on_empty_cache_is_noop() {
        let mut cache = PipelineCache::new();
        cache.retain(|_| false);
        assert!(cache.is_empty());
    }

    #[test]
    fn test_cache_miss_calls_factory_once() {
        // Use a simple `Result<_, String>` error type that we can manufacture.
        let mut cache = PipelineCache::new();
        let key = PipelineCacheKey::new("src", "main", "r-w");

        // Without a real wgpu device we cannot actually create a ComputePipeline,
        // so we verify the cache-miss code path by letting factory return Err.
        let call_count = std::cell::Cell::new(0u32);
        let result: Result<Arc<wgpu::ComputePipeline>, &str> =
            cache.get_or_insert_with(key.clone(), || {
                call_count.set(call_count.get() + 1);
                Err("no gpu in test")
            });

        assert!(result.is_err());
        assert_eq!(call_count.get(), 1, "factory must be called exactly once");
        assert!(cache.is_empty(), "failed factory must not pollute cache");
    }

    #[test]
    fn test_cache_error_does_not_store_entry() {
        let mut cache = PipelineCache::new();
        let key = PipelineCacheKey::new("shader", "ep", "r-r-w");

        let _: Result<Arc<wgpu::ComputePipeline>, &str> =
            cache.get_or_insert_with(key.clone(), || Err("compile error"));

        assert_eq!(cache.len(), 0);
        assert!(cache.is_empty());
    }

    // ── SharedPipelineCache ───────────────────────────────────────────────────

    #[test]
    fn test_new_shared_pipeline_cache_is_empty() {
        let shared = new_shared_pipeline_cache();
        #[allow(clippy::unwrap_used)]
        let guard = shared.lock().map_err(|_| "poisoned").unwrap();
        assert!(guard.is_empty());
    }

    #[test]
    fn test_shared_cache_is_arc_mutex() {
        // Verify that the type can be cloned (Arc semantics) and that both
        // clones observe the same underlying cache.
        let cache = new_shared_pipeline_cache();
        let cache2 = Arc::clone(&cache);

        // The two Arcs point to the same allocation.
        assert!(Arc::ptr_eq(&cache, &cache2));
    }
}