cranpose-ui-graphics 0.1.9

Pure math/data for drawing & units in Cranpose
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
//! Render effects that can be applied to graphics layers.
//!
//! Matches the Jetpack Compose `RenderEffect` API with extensions for custom
//! WGSL shaders (`RuntimeShader`).

use crate::LayerShape;
use std::sync::Arc;

/// Edge treatment for blur effects at the boundary of the blurred region.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum TileMode {
    /// Clamp to the edge pixel color.
    #[default]
    Clamp,
    /// Repeat the gradient/effect from start to end.
    Repeated,
    /// Mirror the gradient/effect every other repetition.
    Mirror,
    /// Treat pixels outside the boundary as transparent.
    Decal,
}

/// Controls blur behavior outside source bounds.
///
/// This mirrors Compose's `BlurredEdgeTreatment`:
/// - bounded treatment (`shape != None`) clips blur output and uses `TileMode::Clamp`
/// - unbounded treatment (`shape == None`) does not clip and uses `TileMode::Decal`
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BlurredEdgeTreatment {
    shape: Option<LayerShape>,
}

impl BlurredEdgeTreatment {
    /// Bounded treatment that clips to a rectangle.
    pub const RECTANGLE: Self = Self {
        shape: Some(LayerShape::Rectangle),
    };

    /// Unbounded treatment that does not clip blurred output.
    pub const UNBOUNDED: Self = Self { shape: None };

    /// Bounded treatment with a specific clip shape.
    pub const fn with_shape(shape: LayerShape) -> Self {
        Self { shape: Some(shape) }
    }

    pub fn shape(self) -> Option<LayerShape> {
        self.shape
    }

    pub fn clip(self) -> bool {
        self.shape.is_some()
    }

    pub fn tile_mode(self) -> TileMode {
        if self.clip() {
            TileMode::Clamp
        } else {
            TileMode::Decal
        }
    }
}

impl Default for BlurredEdgeTreatment {
    fn default() -> Self {
        Self::RECTANGLE
    }
}

/// A custom WGSL shader effect, analogous to Android's `RuntimeShader`.
///
/// The shader source must be a complete WGSL module that declares:
/// ```wgsl
/// @group(0) @binding(0) var input_texture: texture_2d<f32>;
/// @group(0) @binding(1) var input_sampler: sampler;
/// @group(1) @binding(0) var<uniform> u: array<vec4<f32>, 64>;
/// ```
///
/// Float uniforms are packed linearly into the `u` array. Access them in WGSL
/// as `u[index / 4][index % 4]` for individual floats, or `u[index / 4].xy`
/// for vec2, etc. User uniforms may use indices `0..248`; slots `248..256`
/// are reserved for renderer metadata.
///
/// RuntimeShader pipelines operate on premultiplied-alpha textures. Custom
/// shaders should preserve premultiplied output semantics.
#[derive(Clone, Debug)]
pub struct RuntimeShader {
    source: Arc<str>,
    source_hash: u64,
    uniforms: Vec<f32>,
}

/// Error returned when a shader uniform write targets renderer-owned storage.
#[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)]
pub enum RuntimeShaderUniformError {
    #[error(
        "uniform range starting at {index} with width {width} exceeds user uniform range 0..{max_user_uniforms}; slots {reserved_start}..{max_uniforms} are reserved for renderer data"
    )]
    OutOfUserRange {
        index: usize,
        width: usize,
        max_user_uniforms: usize,
        reserved_start: usize,
        max_uniforms: usize,
    },
}

impl RuntimeShader {
    /// Total uniform storage size in floats (64 vec4s = 256 floats).
    ///
    /// The final slots are reserved for renderer-managed data.
    pub const MAX_UNIFORMS: usize = 256;
    /// First renderer-reserved uniform slot.
    pub const RESERVED_UNIFORM_START: usize = 248;
    /// Maximum user-addressable uniform count.
    pub const MAX_USER_UNIFORMS: usize = Self::RESERVED_UNIFORM_START;
    const INITIAL_UNIFORM_CAPACITY: usize = 16;

    /// Create a new RuntimeShader from WGSL source code.
    pub fn new(wgsl_source: &str) -> Self {
        let source_hash = hash_shader_source(wgsl_source);
        Self {
            source: Arc::<str>::from(wgsl_source),
            source_hash,
            uniforms: Vec::with_capacity(Self::INITIAL_UNIFORM_CAPACITY),
        }
    }

    /// Create a RuntimeShader from shared WGSL source code.
    ///
    /// This avoids repeatedly copying large shader modules for animated effects
    /// that rebuild only their uniform payload every frame.
    pub fn from_shared_source(source: Arc<str>) -> Self {
        let source_hash = hash_shader_source(&source);
        Self {
            source,
            source_hash,
            uniforms: Vec::with_capacity(Self::INITIAL_UNIFORM_CAPACITY),
        }
    }

    /// Set a single float uniform at the given index.
    ///
    /// Invalid renderer-reserved ranges are ignored. Use [`Self::try_set_float`]
    /// when the caller needs to handle invalid uniform writes explicitly.
    pub fn set_float(&mut self, index: usize, value: f32) {
        let _ = self.try_set_float(index, value);
    }

    /// Set a single float uniform at the given index.
    pub fn try_set_float(
        &mut self,
        index: usize,
        value: f32,
    ) -> Result<(), RuntimeShaderUniformError> {
        self.try_ensure_capacity(index, 1)?;
        self.uniforms[index] = value;
        Ok(())
    }

    /// Set a vec2 uniform at the given index (consumes indices `[index, index+1]`).
    ///
    /// Invalid renderer-reserved ranges are ignored. Use [`Self::try_set_float2`]
    /// when the caller needs to handle invalid uniform writes explicitly.
    pub fn set_float2(&mut self, index: usize, x: f32, y: f32) {
        let _ = self.try_set_float2(index, x, y);
    }

    /// Set a vec2 uniform at the given index (consumes indices `[index, index+1]`).
    pub fn try_set_float2(
        &mut self,
        index: usize,
        x: f32,
        y: f32,
    ) -> Result<(), RuntimeShaderUniformError> {
        self.try_ensure_capacity(index, 2)?;
        self.uniforms[index] = x;
        self.uniforms[index + 1] = y;
        Ok(())
    }

    /// Set a vec4 uniform at the given index (consumes indices `[index..index+4]`).
    ///
    /// Invalid renderer-reserved ranges are ignored. Use [`Self::try_set_float4`]
    /// when the caller needs to handle invalid uniform writes explicitly.
    pub fn set_float4(&mut self, index: usize, x: f32, y: f32, z: f32, w: f32) {
        let _ = self.try_set_float4(index, x, y, z, w);
    }

    /// Set a vec4 uniform at the given index (consumes indices `[index..index+4]`).
    pub fn try_set_float4(
        &mut self,
        index: usize,
        x: f32,
        y: f32,
        z: f32,
        w: f32,
    ) -> Result<(), RuntimeShaderUniformError> {
        self.try_ensure_capacity(index, 4)?;
        self.uniforms[index] = x;
        self.uniforms[index + 1] = y;
        self.uniforms[index + 2] = z;
        self.uniforms[index + 3] = w;
        Ok(())
    }

    /// Get the WGSL source code.
    pub fn source(&self) -> &str {
        &self.source
    }

    /// Get the uniform data as a float slice (for uploading to GPU).
    pub fn uniforms(&self) -> &[f32] {
        &self.uniforms
    }

    /// Get the uniform data padded to full 256-float array (for GPU uniform buffer).
    pub fn uniforms_padded(&self) -> [f32; Self::MAX_UNIFORMS] {
        let mut padded = [0.0f32; Self::MAX_UNIFORMS];
        let len = self.uniforms.len().min(Self::MAX_UNIFORMS);
        padded[..len].copy_from_slice(&self.uniforms[..len]);
        padded
    }

    /// Compute a hash of the shader source for pipeline caching.
    pub fn source_hash(&self) -> u64 {
        self.source_hash
    }

    fn try_ensure_capacity(
        &mut self,
        index: usize,
        width: usize,
    ) -> Result<(), RuntimeShaderUniformError> {
        let min_len = index
            .checked_add(width)
            .ok_or_else(|| Self::uniform_range_error(index, width))?;
        if min_len > Self::MAX_USER_UNIFORMS {
            return Err(Self::uniform_range_error(index, width));
        }
        if self.uniforms.len() < min_len {
            self.uniforms.resize(min_len, 0.0);
        }
        Ok(())
    }

    fn uniform_range_error(index: usize, width: usize) -> RuntimeShaderUniformError {
        RuntimeShaderUniformError::OutOfUserRange {
            index,
            width,
            max_user_uniforms: Self::MAX_USER_UNIFORMS,
            reserved_start: Self::RESERVED_UNIFORM_START,
            max_uniforms: Self::MAX_UNIFORMS,
        }
    }
}

impl PartialEq for RuntimeShader {
    fn eq(&self, other: &Self) -> bool {
        self.source_hash == other.source_hash
            && (Arc::ptr_eq(&self.source, &other.source)
                || self.source.as_ref() == other.source.as_ref())
            && self.uniforms == other.uniforms
    }
}

fn hash_shader_source(source: &str) -> u64 {
    const FNV_OFFSET_BASIS: u64 = 0xcbf2_9ce4_8422_2325;
    const FNV_PRIME: u64 = 0x0000_0100_0000_01b3;

    source
        .as_bytes()
        .iter()
        .fold(FNV_OFFSET_BASIS, |hash, byte| {
            (hash ^ u64::from(*byte)).wrapping_mul(FNV_PRIME)
        })
}

/// A render effect applied to a graphics layer's rendered content.
///
/// Matches Jetpack Compose's `RenderEffect` sealed class hierarchy,
/// extended with `Shader` for custom WGSL effects.
#[derive(Clone, Debug, PartialEq)]
pub enum RenderEffect {
    /// Gaussian blur applied to the layer's rendered content.
    Blur {
        radius_x: f32,
        radius_y: f32,
        edge_treatment: TileMode,
    },
    /// Offset the rendered content by a fixed amount.
    Offset { offset_x: f32, offset_y: f32 },
    /// Apply a custom WGSL shader effect.
    Shader { shader: RuntimeShader },
    /// Chain two effects: apply `first`, then apply `second` to the result.
    Chain {
        first: Box<RenderEffect>,
        second: Box<RenderEffect>,
    },
}

impl RenderEffect {
    /// Create a blur effect with equal radius in both directions.
    pub fn blur(radius: f32) -> Self {
        Self::blur_with_edge_treatment(radius, TileMode::default())
    }

    /// Create a blur effect with equal radius in both directions and explicit
    /// edge treatment semantics.
    pub fn blur_with_edge_treatment(radius: f32, edge_treatment: TileMode) -> Self {
        Self::Blur {
            radius_x: radius,
            radius_y: radius,
            edge_treatment,
        }
    }

    /// Create a blur effect with separate horizontal and vertical radii.
    pub fn blur_xy(radius_x: f32, radius_y: f32, edge_treatment: TileMode) -> Self {
        Self::Blur {
            radius_x,
            radius_y,
            edge_treatment,
        }
    }

    /// Create an offset effect.
    pub fn offset(offset_x: f32, offset_y: f32) -> Self {
        Self::Offset { offset_x, offset_y }
    }

    /// Create a custom shader effect from a RuntimeShader.
    pub fn runtime_shader(shader: RuntimeShader) -> Self {
        Self::Shader { shader }
    }

    /// Chain this effect with another: `self` is applied first, then `other`.
    pub fn then(self, other: RenderEffect) -> Self {
        Self::Chain {
            first: Box::new(self),
            second: Box::new(other),
        }
    }

    /// Returns `true` if this effect or any chained sub-effect is a
    /// `RuntimeShader`. Animated shaders produce different output every frame,
    /// so layer surface caching is counterproductive for them.
    pub fn contains_runtime_shader(&self) -> bool {
        match self {
            RenderEffect::Shader { .. } => true,
            RenderEffect::Chain { first, second } => {
                first.contains_runtime_shader() || second.contains_runtime_shader()
            }
            _ => false,
        }
    }
}

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

    #[test]
    fn runtime_shader_set_uniforms() {
        let mut shader = RuntimeShader::new("// test");
        shader.set_float(0, 1.0);
        shader.set_float2(2, 3.0, 4.0);
        shader.set_float4(4, 5.0, 6.0, 7.0, 8.0);

        assert_eq!(shader.uniforms()[0], 1.0);
        assert_eq!(shader.uniforms()[1], 0.0); // gap
        assert_eq!(shader.uniforms()[2], 3.0);
        assert_eq!(shader.uniforms()[3], 4.0);
        assert_eq!(shader.uniforms()[4], 5.0);
        assert_eq!(shader.uniforms()[5], 6.0);
        assert_eq!(shader.uniforms()[6], 7.0);
        assert_eq!(shader.uniforms()[7], 8.0);
    }

    #[test]
    fn runtime_shader_padded() {
        let mut shader = RuntimeShader::new("// test");
        shader.set_float(0, 42.0);
        let padded = shader.uniforms_padded();
        assert_eq!(padded[0], 42.0);
        assert_eq!(padded[1], 0.0);
        assert_eq!(padded[255], 0.0);
    }

    #[test]
    fn runtime_shader_try_set_reports_reserved_uniform_slots() {
        let mut shader = RuntimeShader::new("// test");

        let err = shader
            .try_set_float(RuntimeShader::RESERVED_UNIFORM_START, 1.0)
            .unwrap_err();
        assert_eq!(
            err,
            RuntimeShaderUniformError::OutOfUserRange {
                index: RuntimeShader::RESERVED_UNIFORM_START,
                width: 1,
                max_user_uniforms: RuntimeShader::MAX_USER_UNIFORMS,
                reserved_start: RuntimeShader::RESERVED_UNIFORM_START,
                max_uniforms: RuntimeShader::MAX_UNIFORMS,
            }
        );
        assert!(shader.uniforms().is_empty());

        let err = shader
            .try_set_float4(RuntimeShader::MAX_USER_UNIFORMS - 3, 1.0, 2.0, 3.0, 4.0)
            .unwrap_err();
        assert_eq!(
            err,
            RuntimeShaderUniformError::OutOfUserRange {
                index: RuntimeShader::MAX_USER_UNIFORMS - 3,
                width: 4,
                max_user_uniforms: RuntimeShader::MAX_USER_UNIFORMS,
                reserved_start: RuntimeShader::RESERVED_UNIFORM_START,
                max_uniforms: RuntimeShader::MAX_UNIFORMS,
            }
        );
    }

    #[test]
    fn runtime_shader_setters_ignore_invalid_uniform_slots_without_panicking() {
        let mut shader = RuntimeShader::new("// test");
        shader.set_float(0, 7.0);

        shader.set_float(RuntimeShader::RESERVED_UNIFORM_START, 1.0);
        shader.set_float4(RuntimeShader::MAX_USER_UNIFORMS - 3, 1.0, 2.0, 3.0, 4.0);

        assert_eq!(shader.uniforms(), &[7.0]);
    }

    #[test]
    fn render_effect_chaining() {
        let blur = RenderEffect::blur(10.0);
        let offset = RenderEffect::offset(5.0, 5.0);
        let chained = blur.then(offset);
        match chained {
            RenderEffect::Chain { first, second } => {
                assert!(matches!(*first, RenderEffect::Blur { .. }));
                assert!(matches!(*second, RenderEffect::Offset { .. }));
            }
            _ => panic!("expected Chain"),
        }
    }

    #[test]
    fn blur_convenience() {
        let effect = RenderEffect::blur(15.0);
        match effect {
            RenderEffect::Blur {
                radius_x,
                radius_y,
                edge_treatment,
            } => {
                assert_eq!(radius_x, 15.0);
                assert_eq!(radius_y, 15.0);
                assert_eq!(edge_treatment, TileMode::Clamp);
            }
            _ => panic!("expected Blur"),
        }
    }

    #[test]
    fn blur_with_edge_treatment_uses_explicit_mode() {
        let effect = RenderEffect::blur_with_edge_treatment(6.0, TileMode::Decal);
        match effect {
            RenderEffect::Blur {
                radius_x,
                radius_y,
                edge_treatment,
            } => {
                assert_eq!(radius_x, 6.0);
                assert_eq!(radius_y, 6.0);
                assert_eq!(edge_treatment, TileMode::Decal);
            }
            _ => panic!("expected Blur"),
        }
    }

    #[test]
    fn source_hash_consistent() {
        let s1 = RuntimeShader::new("fn main() {}");
        let s2 = RuntimeShader::new("fn main() {}");
        assert_eq!(s1.source_hash(), s2.source_hash());
    }

    #[test]
    fn runtime_shader_from_shared_source_reuses_shared_source() {
        let source = Arc::<str>::from("fn fragment() -> vec4<f32> { return vec4<f32>(1.0); }");
        let s1 = RuntimeShader::from_shared_source(source.clone());
        let s2 = RuntimeShader::from_shared_source(source);

        assert!(Arc::ptr_eq(&s1.source, &s2.source));
        assert_eq!(s1.source_hash(), s2.source_hash());
    }

    #[test]
    fn runtime_shader_source_storage_has_no_process_global_interner() {
        let source = include_str!("render_effect.rs");
        let blocked_static = ["static ", "INTERNER"].concat();
        let blocked_type = ["ShaderSource", "Interner"].concat();

        assert!(
            !source.contains(&blocked_static) && !source.contains(&blocked_type),
            "RuntimeShader source sharing must be explicit via from_shared_source, not a process-global interner"
        );
    }

    #[test]
    fn blur_xy_preserves_tile_mode() {
        let effect = RenderEffect::blur_xy(3.0, 7.0, TileMode::Clamp);
        match effect {
            RenderEffect::Blur {
                radius_x,
                radius_y,
                edge_treatment,
            } => {
                assert_eq!(radius_x, 3.0);
                assert_eq!(radius_y, 7.0);
                assert_eq!(edge_treatment, TileMode::Clamp);
            }
            _ => panic!("expected Blur"),
        }
    }

    #[test]
    fn offset_constructor_sets_components() {
        let effect = RenderEffect::offset(11.0, -5.0);
        match effect {
            RenderEffect::Offset { offset_x, offset_y } => {
                assert_eq!(offset_x, 11.0);
                assert_eq!(offset_y, -5.0);
            }
            _ => panic!("expected Offset"),
        }
    }

    #[test]
    fn runtime_shader_equality_is_source_value_based() {
        let mut s1 = RuntimeShader::new("fn main() {}");
        let mut s2 = RuntimeShader::new("fn main() {}");
        s1.set_float(0, 1.0);
        s2.set_float(0, 1.0);
        assert_eq!(s1, s2);
    }

    #[test]
    fn blurred_edge_treatment_defaults_to_bounded_rectangle() {
        let treatment = BlurredEdgeTreatment::default();
        assert_eq!(treatment.shape(), Some(LayerShape::Rectangle));
        assert!(treatment.clip());
        assert_eq!(treatment.tile_mode(), TileMode::Clamp);
    }

    #[test]
    fn blurred_edge_treatment_unbounded_uses_decal_and_no_clip() {
        let treatment = BlurredEdgeTreatment::UNBOUNDED;
        assert_eq!(treatment.shape(), None);
        assert!(!treatment.clip());
        assert_eq!(treatment.tile_mode(), TileMode::Decal);
    }

    #[test]
    fn blurred_edge_treatment_with_shape_uses_bounded_mode() {
        let rounded = LayerShape::Rounded(RoundedCornerShape::uniform(8.0));
        let treatment = BlurredEdgeTreatment::with_shape(rounded);
        assert_eq!(treatment.shape(), Some(rounded));
        assert!(treatment.clip());
        assert_eq!(treatment.tile_mode(), TileMode::Clamp);
    }
}