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
//! `PortValue` — the runtime values flowing along DAG edges.
use std::sync::Arc;
use crate::buf::{OpaqueValue, RasterBuf, ScalarField};
use crate::port::PortKind;
/// One value flowing along an edge. Cloning is cheap (Arc / Copy).
#[derive(Debug, Clone)]
pub enum PortValue {
Features(OpaqueValue),
Raster(Arc<RasterBuf>),
Sprite(Arc<RasterBuf>),
Brush(OpaqueValue),
/// Label placement candidates or decisions (see [`PortKind::Labels`]).
Labels(OpaqueValue),
Scalar(ScalarValue),
ScalarField(Arc<ScalarField>),
}
impl PortValue {
pub fn kind(&self) -> PortKind {
match self {
PortValue::Features(_) => PortKind::Features,
PortValue::Raster(_) => PortKind::Raster,
PortValue::Sprite(_) => PortKind::Sprite,
PortValue::Brush(_) => PortKind::Brush,
PortValue::Labels(_) => PortKind::Labels,
PortValue::Scalar(_) => PortKind::Scalar,
PortValue::ScalarField(_) => PortKind::ScalarField,
}
}
pub fn as_scalar_field(&self) -> Option<&Arc<ScalarField>> {
if let PortValue::ScalarField(f) = self {
Some(f)
} else {
None
}
}
pub fn as_raster(&self) -> Option<&Arc<RasterBuf>> {
if let PortValue::Raster(r) = self {
Some(r)
} else {
None
}
}
pub fn as_sprite(&self) -> Option<&Arc<RasterBuf>> {
if let PortValue::Sprite(s) = self {
Some(s)
} else {
None
}
}
/// Approximate heap bytes held by this value's payload.
///
/// Exact for the pixel-carrying variants (raster, sprite, scalar
/// field) — the ones that dominate render-time memory. Type-erased
/// payloads (features, brushes, labels) report `0` because their
/// concrete types live in other crates; treat the number as "bytes
/// in pixel buffers", which is what memory reports care about.
/// The interned blank raster reports `0`: it is one allocation shared
/// by every holder, so charging it per holder would badly overstate
/// what a render or a cache is really costing.
pub fn approx_bytes(&self) -> usize {
match self {
PortValue::Raster(r) | PortValue::Sprite(r) => {
if RasterBuf::is_interned_blank(r) {
0
} else {
r.pixels.len()
}
}
PortValue::ScalarField(f) => f.values.len() * std::mem::size_of::<f32>(),
_ => 0,
}
}
pub fn as_scalar(&self) -> Option<&ScalarValue> {
if let PortValue::Scalar(s) = self {
Some(s)
} else {
None
}
}
}
/// A constant value carried on a `Scalar` port.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ScalarValue {
/// Straight (non-premultiplied) sRGB-encoded RGBA, components in
/// `[0, 1]` — the same convention as a parsed `#rrggbb[aa]`
/// literal. Consumers linearize / premultiply as needed.
Color([f32; 4]),
Number(f64),
Bool(bool),
}
impl ScalarValue {
pub fn as_color(&self) -> Option<[f32; 4]> {
if let ScalarValue::Color(c) = self {
Some(*c)
} else {
None
}
}
pub fn as_number(&self) -> Option<f64> {
if let ScalarValue::Number(n) = self {
Some(*n)
} else {
None
}
}
pub fn as_bool(&self) -> Option<bool> {
if let ScalarValue::Bool(b) = self {
Some(*b)
} else {
None
}
}
/// Short kind name for error messages (`number` / `color` / `bool`).
pub fn kind_name(&self) -> &'static str {
match self {
ScalarValue::Color(_) => "color",
ScalarValue::Number(_) => "number",
ScalarValue::Bool(_) => "bool",
}
}
/// Feed this value into a cache-key hasher. Stable across runs.
pub fn hash_into(&self, h: &mut xxhash_rust::xxh3::Xxh3) {
match self {
ScalarValue::Color(c) => {
h.update(b"C");
for ch in c {
h.update(&ch.to_le_bytes());
}
}
ScalarValue::Number(n) => {
h.update(b"N");
h.update(&n.to_le_bytes());
}
ScalarValue::Bool(b) => {
h.update(b"B");
h.update(&[*b as u8]);
}
}
}
}