oxigdal-gpu 0.1.7

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
//! Integration tests for the WGSL push-constants (immediates) helper module.
//!
//! Pure-Rust tests (range/layout validation, buffer writes, shader-source
//! inspection) run unconditionally.  GPU-dependent tests use the
//! `try_gpu_context` / `catch_unwind` pattern standard in this test suite so
//! that the whole suite still passes in environments without a GPU.

// Allow unwrap in tests and relax doc requirements.
#![allow(clippy::unwrap_used, clippy::expect_used, missing_docs)]

use oxigdal_gpu::{
    GpuContext,
    push_constants::{
        MAX_PUSH_CONSTANTS_SIZE_BYTES, PushConstantRange, PushConstantsBuffer, PushConstantsLayout,
        make_push_constants_shader_source, max_push_constants_size, supports_push_constants,
    },
};

// ─────────────────────────────────────────────────────────────────────────────
// Helper: try to create a GPU context without panicking.
// ─────────────────────────────────────────────────────────────────────────────

/// Attempt to obtain a `GpuContext`, returning `None` when no GPU backend is
/// compiled in or no adapter is present on the machine.
///
/// `wgpu` panics synchronously when no backend feature flag is enabled, so we
/// wrap the future creation inside `catch_unwind`.
fn try_gpu_context() -> Option<GpuContext> {
    use std::panic::AssertUnwindSafe;
    let result =
        std::panic::catch_unwind(AssertUnwindSafe(|| pollster::block_on(GpuContext::new())));
    match result {
        Ok(Ok(ctx)) => Some(ctx),
        _ => None,
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// PushConstantRange tests — pure Rust
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn test_push_constant_range_compute_default() {
    let r = PushConstantRange::compute(32);
    assert_eq!(r.start, 0, "start must be 0 for compute() constructor");
    assert_eq!(r.end, 32, "end must equal the requested size");
    assert!(
        r.validate().is_ok(),
        "PushConstantRange::compute(32) must pass validation"
    );
}

#[test]
fn test_push_constant_range_validate_rejects_zero_size() {
    let r = PushConstantRange {
        stages: wgpu::ShaderStages::COMPUTE,
        start: 0,
        end: 0,
    };
    assert!(
        r.validate().is_err(),
        "start == end must be rejected as zero-size range"
    );
}

#[test]
fn test_push_constant_range_validate_rejects_start_ge_end() {
    let r = PushConstantRange {
        stages: wgpu::ShaderStages::COMPUTE,
        start: 16,
        end: 4,
    };
    assert!(r.validate().is_err(), "start > end must be rejected");
}

#[test]
fn test_push_constant_range_validate_rejects_over_128_bytes() {
    let r = PushConstantRange {
        stages: wgpu::ShaderStages::COMPUTE,
        start: 0,
        end: MAX_PUSH_CONSTANTS_SIZE_BYTES + 4,
    };
    assert!(
        r.validate().is_err(),
        "size > MAX_PUSH_CONSTANTS_SIZE_BYTES must be rejected"
    );
}

#[test]
fn test_push_constant_range_validate_accepts_max_size() {
    let r = PushConstantRange {
        stages: wgpu::ShaderStages::COMPUTE,
        start: 0,
        end: MAX_PUSH_CONSTANTS_SIZE_BYTES,
    };
    assert!(
        r.validate().is_ok(),
        "exactly MAX_PUSH_CONSTANTS_SIZE_BYTES must be accepted"
    );
}

#[test]
fn test_push_constant_range_validate_rejects_unaligned_end() {
    // end = 6 is not 4-byte aligned
    let r = PushConstantRange {
        stages: wgpu::ShaderStages::COMPUTE,
        start: 0,
        end: 6,
    };
    assert!(
        r.validate().is_err(),
        "end not 4-byte aligned must be rejected"
    );
}

#[test]
fn test_push_constant_range_size_helper() {
    let r = PushConstantRange::compute(64);
    assert_eq!(r.size(), 64);
}

// ─────────────────────────────────────────────────────────────────────────────
// PushConstantsLayout tests — pure Rust
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn test_push_constants_layout_compute_only_structure() {
    let l = PushConstantsLayout::compute_only(64);
    assert_eq!(l.total_size, 64, "total_size must equal the requested size");
    assert_eq!(l.ranges.len(), 1, "exactly one range expected");
    assert!(
        l.validate().is_ok(),
        "PushConstantsLayout::compute_only(64) must pass validation"
    );
}

#[test]
fn test_push_constants_layout_to_wgpu_ranges_single() {
    let l = PushConstantsLayout::compute_only(16);
    let ranges = l.to_wgpu_ranges();
    assert_eq!(ranges.len(), 1, "one range descriptor expected");
    assert_eq!(ranges[0].range(), 0..16, "range must span [0, 16)");
}

#[test]
fn test_push_constants_layout_to_wgpu_ranges_stages() {
    let l = PushConstantsLayout::compute_only(32);
    let ranges = l.to_wgpu_ranges();
    assert_eq!(ranges[0].stages, wgpu::ShaderStages::COMPUTE);
}

#[test]
fn test_push_constants_layout_validate_rejects_unaligned_total_size() {
    let mut l = PushConstantsLayout::compute_only(16);
    l.total_size = 7; // not 4-byte aligned
    assert!(
        l.validate().is_err(),
        "unaligned total_size must be rejected"
    );
}

#[test]
fn test_push_constants_layout_validate_rejects_too_large() {
    let mut l = PushConstantsLayout::compute_only(16);
    l.total_size = MAX_PUSH_CONSTANTS_SIZE_BYTES + 4;
    assert!(
        l.validate().is_err(),
        "oversized total_size must be rejected"
    );
}

#[test]
fn test_push_constants_layout_immediate_size_for_wgpu() {
    let l = PushConstantsLayout::compute_only(48);
    assert_eq!(l.immediate_size_for_wgpu(), 48);
}

// ─────────────────────────────────────────────────────────────────────────────
// PushConstantsBuffer tests — pure Rust
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn test_push_constants_buffer_new_zero_initialised() {
    let layout = PushConstantsLayout::compute_only(16);
    let buf = PushConstantsBuffer::new(layout);
    assert_eq!(buf.size(), 16);
    assert!(
        buf.as_bytes().iter().all(|&b| b == 0),
        "buffer must be zero-initialised"
    );
}

#[test]
fn test_push_constants_buffer_write_u32_roundtrip() {
    let layout = PushConstantsLayout::compute_only(16);
    let mut buf = PushConstantsBuffer::new(layout);
    buf.write_u32(0, 42).expect("write_u32 at offset 0 failed");
    let bytes = buf.as_bytes();
    let val = u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]);
    assert_eq!(val, 42, "round-trip u32 read failed");
}

#[test]
fn test_push_constants_buffer_write_f32_roundtrip() {
    let layout = PushConstantsLayout::compute_only(16);
    let mut buf = PushConstantsBuffer::new(layout);
    buf.write_f32(4, std::f32::consts::PI)
        .expect("write_f32 at offset 4 failed");
    let bytes = buf.as_bytes();
    let val = f32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]);
    assert!(
        (val - std::f32::consts::PI).abs() < 1e-6,
        "round-trip f32 read failed: expected π ≈ 3.14159, got {val}"
    );
}

#[test]
fn test_push_constants_buffer_write_vec4_f32_roundtrip() {
    let layout = PushConstantsLayout::compute_only(16);
    let mut buf = PushConstantsBuffer::new(layout);
    let data: [f32; 4] = [1.0, 2.0, 3.0, 4.0];
    buf.write_vec4_f32(0, data).expect("write_vec4_f32 failed");
    let bytes = buf.as_bytes();
    for (i, expected) in data.iter().enumerate() {
        let off = i * 4;
        let val = f32::from_le_bytes([bytes[off], bytes[off + 1], bytes[off + 2], bytes[off + 3]]);
        assert!(
            (val - expected).abs() < 1e-6,
            "vec4 element {i}: expected {expected}, got {val}"
        );
    }
}

#[test]
fn test_push_constants_buffer_write_multiple_fields() {
    // Simulate a real use case: write 3 fields at different offsets.
    let layout = PushConstantsLayout::compute_only(16);
    let mut buf = PushConstantsBuffer::new(layout);
    buf.write_u32(0, 1024).expect("write width failed"); // offset 0
    buf.write_u32(4, 768).expect("write height failed"); // offset 4
    buf.write_f32(8, 0.5).expect("write scale failed"); // offset 8

    let bytes = buf.as_bytes();
    let width = u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]);
    let height = u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]);
    let scale = f32::from_le_bytes([bytes[8], bytes[9], bytes[10], bytes[11]]);

    assert_eq!(width, 1024, "width mismatch");
    assert_eq!(height, 768, "height mismatch");
    assert!((scale - 0.5).abs() < 1e-6, "scale mismatch: got {scale}");
}

#[test]
fn test_push_constants_buffer_offset_overflow_errors() {
    let layout = PushConstantsLayout::compute_only(4);
    let mut buf = PushConstantsBuffer::new(layout);
    // Offset 4 + 4 bytes = 8 > 4-byte buffer → must fail.
    let result = buf.write_u32(4, 1);
    assert!(
        result.is_err(),
        "write past buffer end must return an error"
    );
    // Also verify writing exactly at the start succeeds.
    assert!(
        buf.write_u32(0, 99).is_ok(),
        "write at offset 0 must succeed"
    );
}

#[test]
fn test_push_constants_buffer_clear_resets_to_zero() {
    let layout = PushConstantsLayout::compute_only(16);
    let mut buf = PushConstantsBuffer::new(layout);
    buf.write_u32(0, 0xDEAD_BEEF).expect("write failed");
    buf.clear();
    assert!(
        buf.as_bytes().iter().all(|&b| b == 0),
        "clear() must reset all bytes to zero"
    );
}

// ─────────────────────────────────────────────────────────────────────────────
// Shader source generation tests — pure Rust
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn test_make_push_constants_shader_source_contains_var_immediate() {
    let src = make_push_constants_shader_source(
        "struct PushConstantsBlock { val: u32 }",
        "let x = pc.val;",
    );
    assert!(
        src.contains("var<immediate>"),
        "shader source must use var<immediate> (wgpu 29+ naming); got:\n{src}"
    );
}

#[test]
fn test_make_push_constants_shader_source_includes_struct_def() {
    let src = make_push_constants_shader_source("struct PushConstantsBlock { val: u32 }", "");
    assert!(
        src.contains("struct PushConstantsBlock"),
        "shader source must embed the struct definition"
    );
}

#[test]
fn test_make_push_constants_shader_source_has_compute_entry_point() {
    let src = make_push_constants_shader_source("struct PushConstantsBlock { val: u32 }", "");
    assert!(
        src.contains("@compute"),
        "shader source must declare a @compute entry point"
    );
}

#[test]
fn test_make_push_constants_shader_source_body_included() {
    let body = "let result = pc.val * 2u;";
    let src = make_push_constants_shader_source("struct PushConstantsBlock { val: u32 }", body);
    assert!(
        src.contains(body),
        "shader body must appear verbatim in the generated source"
    );
}

#[test]
fn test_make_push_constants_shader_source_references_pc_variable() {
    let src = make_push_constants_shader_source(
        "struct PushConstantsBlock { width: u32, height: u32 }",
        "let w = pc.width;",
    );
    assert!(
        src.contains("pc: PushConstantsBlock"),
        "the immediate variable must be declared as 'pc: PushConstantsBlock'"
    );
}

// ─────────────────────────────────────────────────────────────────────────────
// GPU-dependent tests — wrapped in catch_unwind / try_gpu_context
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn test_supports_push_constants_gpu_query_does_not_panic() {
    // This test only asserts that the function runs without panicking.
    // The actual bool result depends on the hardware/driver.
    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        let ctx = match try_gpu_context() {
            Some(c) => c,
            None => return,
        };
        let _supported = supports_push_constants(&ctx);
        // No assertion — just proving the call succeeds.
    }));
    let _ = result;
}

#[test]
fn test_max_push_constants_size_gpu_query_does_not_panic() {
    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        let ctx = match try_gpu_context() {
            Some(c) => c,
            None => return,
        };
        let max_size = max_push_constants_size(&ctx);
        // When IMMEDIATES is not enabled, the size is 0 — that is valid.
        // When it is enabled, we expect at least the spec minimum.
        if supports_push_constants(&ctx) {
            assert!(
                max_size >= MAX_PUSH_CONSTANTS_SIZE_BYTES,
                "max_push_constants_size should be >= {} when IMMEDIATES is supported, got {}",
                MAX_PUSH_CONSTANTS_SIZE_BYTES,
                max_size
            );
        }
    }));
    let _ = result;
}

#[test]
fn test_build_push_constants_pipeline_requires_feature_or_errors() {
    // When built without the IMMEDIATES feature, build_push_constants_pipeline
    // must return a meaningful error (not panic).  This test verifies the
    // error path is reachable without requiring actual push-constant support.
    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        let ctx = match try_gpu_context() {
            Some(c) => c,
            None => return,
        };

        // A context created with GpuContext::new() does NOT request IMMEDIATES,
        // so build_push_constants_pipeline must return UnsupportedOperation.
        if supports_push_constants(&ctx) {
            // Hardware supports it but we did not request it during context
            // creation — the device was still created without the feature, so
            // the API-level check should still fire.  Skip asserting the error.
            return;
        }

        use oxigdal_gpu::push_constants::build_push_constants_pipeline;
        let layout = PushConstantsLayout::compute_only(16);
        let src = make_push_constants_shader_source(
            "struct PushConstantsBlock { value: u32, _p0: u32, _p1: u32, _p2: u32 }",
            "let v = pc.value;",
        );
        let err = build_push_constants_pipeline(&ctx, &src, "main", &layout);
        assert!(
            err.is_err(),
            "build_push_constants_pipeline must return Err when IMMEDIATES not enabled"
        );
    }));
    let _ = result;
}