darkly 0.5.0

A GPU-native paint engine on wgpu: brushes, layers, blend modes, masks, selections, and undo.
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
//! Switch node — routes one of two inputs through to the output based on
//! a static `select` flag, or removes the wire entirely so downstream falls
//! back to its own port default.
//!
//! Generalises a gate: wire only `in_0_X`, leave `in_1_X` unconnected, and
//! the switch behaves as an enable-on toggle (when `select` flips to 1 the
//! chosen side is unconnected, so downstream sees no wire). Wire both sides
//! and it becomes a 2-way mux.
//!
//! `select` is a Bool input port (not a `ParamDef`) so the brush author can
//! expose it to the brush user as a toggle in BrushOptions. Per-instance
//! `label`/`description` overrides (set via `Graph::set_port_label` /
//! `set_port_description`) let the author rename the toggle for the brush
//! user without inventing a parallel mechanism.
//!
//! The switch is dispatched at graph-rewrite time, before compilation, by
//! [`apply_to`]: that walks the graph and, for every switch whose `select`
//! port is *unconnected* (i.e. driven by its own default), splices the
//! chosen upstream directly into the downstream, drops the other side, and
//! removes the switch node. Switches whose `select` is dynamically wired
//! are left in place and rejected by [`SwitchEvaluator::compile_wgsl`] in
//! v1 — a runtime WGSL `select(...)` fallback is a follow-up if anyone
//! actually wires it dynamically.

use crate::brush::eval::{BrushNodeEvaluator, EvalContext};
use crate::brush::node::BrushNodeRegistration;
use crate::brush::wgsl::{CompileWgslCtx, NodeWgsl};
use crate::brush::wire::{BrushWireType, ScalarValue};
use crate::nodegraph::{Connection, Graph, NodeRegistration, PortDef, PortDir, PortRef};

pub const TYPE_ID: &str = "switch";

/// `(in_0, in_1, out)` port-name triplet per wire-type. The switch declares
/// one triplet for each [`BrushWireType`]; the brush author wires whichever
/// triplets they need and leaves the others unconnected.
const TRIPLETS: &[(&str, &str, &str)] = &[
    ("in_0_scalar", "in_1_scalar", "out_scalar"),
    ("in_0_int", "in_1_int", "out_int"),
    ("in_0_bool", "in_1_bool", "out_bool"),
    ("in_0_vec2", "in_1_vec2", "out_vec2"),
    ("in_0_vec4", "in_1_vec4", "out_vec4"),
];

const SELECT_PORT: &str = "select";

pub fn register() -> BrushNodeRegistration {
    BrushNodeRegistration::compute(
        NodeRegistration {
            type_id: TYPE_ID,
            category: "math",
            display_name: "Switch",
            description: "Toggle that picks one of two inputs. Wire one side only and it acts as an on/off switch for that branch.",
            ports: vec![
                PortDef::input("in_0_scalar", BrushWireType::Scalar)
                    .with_description("Routed to out_scalar when select=0"),
                PortDef::input("in_1_scalar", BrushWireType::Scalar)
                    .with_description("Routed to out_scalar when select=1"),
                PortDef::output("out_scalar", BrushWireType::Scalar)
                    .with_description("Selected scalar"),
                PortDef::input("in_0_int", BrushWireType::Int)
                    .with_description("Routed to out_int when select=0"),
                PortDef::input("in_1_int", BrushWireType::Int)
                    .with_description("Routed to out_int when select=1"),
                PortDef::output("out_int", BrushWireType::Int).with_description("Selected int"),
                PortDef::input("in_0_bool", BrushWireType::Bool)
                    .with_description("Routed to out_bool when select=0"),
                PortDef::input("in_1_bool", BrushWireType::Bool)
                    .with_description("Routed to out_bool when select=1"),
                PortDef::output("out_bool", BrushWireType::Bool).with_description("Selected bool"),
                PortDef::input("in_0_vec2", BrushWireType::Vec2)
                    .with_description("Routed to out_vec2 when select=0"),
                PortDef::input("in_1_vec2", BrushWireType::Vec2)
                    .with_description("Routed to out_vec2 when select=1"),
                PortDef::output("out_vec2", BrushWireType::Vec2).with_description("Selected vec2"),
                PortDef::input("in_0_vec4", BrushWireType::Vec4)
                    .with_description("Routed to out_vec4 when select=0"),
                PortDef::input("in_1_vec4", BrushWireType::Vec4)
                    .with_description("Routed to out_vec4 when select=1"),
                PortDef::output("out_vec4", BrushWireType::Vec4).with_description("Selected vec4"),
                PortDef::input(SELECT_PORT, BrushWireType::Bool)
                    .with_range(0.0, 1.0, 0.0)
                    .with_step(1.0)
                    .with_label("Select")
                    .with_description(
                        "Routes one of two inputs to the output. If the \
                         chosen input is unconnected, downstream falls back \
                         to its own port default.",
                    ),
            ],
            params: &[],
            is_gpu: false,
            is_terminal: false,
            supports_erase: true,
        },
        || Box::new(SwitchEvaluator),
    )
}

pub struct SwitchEvaluator;

impl BrushNodeEvaluator for SwitchEvaluator {
    /// Safety net only. [`apply_to`] removes Switch nodes before the
    /// runner ever sees them; if one survives (dynamic `select` wiring),
    /// just route the chosen side so a CPU eval doesn't produce garbage.
    fn evaluate_cpu(&self, ctx: &EvalContext) -> Vec<(String, ScalarValue)> {
        let select_to_1 = ctx.input(SELECT_PORT).as_f32() >= 0.5;
        let mut out = Vec::with_capacity(TRIPLETS.len());
        for (in_0, in_1, out_name) in TRIPLETS {
            let chosen = if select_to_1 { *in_1 } else { *in_0 };
            out.push(((*out_name).to_string(), ctx.input(chosen)));
        }
        out
    }

    fn compile_wgsl(&self, _cctx: &CompileWgslCtx) -> Result<NodeWgsl, String> {
        // Reaching this means `apply_to` left the switch in place — which
        // only happens when `select` is dynamically wired. v1 rejects.
        Err(
            "Switch.select must be a static value; dynamic wiring is not \
             yet supported. Disconnect the wire on the `select` port and \
             toggle it via its exposed slider/value instead."
                .into(),
        )
    }
}

/// Rewrite every Switch node in `graph` in place: splice the chosen input
/// through to all downstreams (or drop the wire entirely if the chosen
/// input is unconnected), then remove the Switch.
///
/// Switches whose `select` port has an incoming connection are left alone
/// — those are dynamic and handled (rejected, in v1) by
/// [`SwitchEvaluator::compile_wgsl`].
///
/// Called by [`crate::brush::compile_graph`] on a throwaway clone of the
/// user's graph — the persisted graph is never mutated.
pub(crate) fn apply_to(graph: &mut Graph<BrushWireType>) {
    let switch_ids: Vec<_> = graph
        .nodes()
        .iter()
        .filter(|(_, n)| n.type_id == TYPE_ID)
        .map(|(id, _)| *id)
        .collect();

    for switch_id in switch_ids {
        // Dynamic select → leave for WGSL compile to reject.
        let select_wired = graph
            .connections
            .iter()
            .any(|c| c.to.node == switch_id && c.to.port == SELECT_PORT);
        if select_wired {
            continue;
        }

        // Read the (possibly per-instance overridden) default of `select`.
        let select_to_1 = {
            let Some(node) = graph.nodes().get(&switch_id) else {
                continue;
            };
            node.ports
                .iter()
                .find(|p| p.name == SELECT_PORT && p.dir == PortDir::Input)
                .map(|p| p.default >= 0.5)
                .unwrap_or(false)
        };

        for (in_0, in_1, out_name) in TRIPLETS {
            let chosen_in = if select_to_1 { *in_1 } else { *in_0 };

            // Snapshot the chosen upstream and every downstream first so
            // we can mutate `graph.connections` without overlapping borrows.
            let upstream: Option<PortRef> = graph
                .connections
                .iter()
                .find(|c| c.to.node == switch_id && c.to.port == chosen_in)
                .map(|c| c.from.clone());
            let downstreams: Vec<PortRef> = graph
                .connections
                .iter()
                .filter(|c| c.from.node == switch_id && c.from.port == *out_name)
                .map(|c| c.to.clone())
                .collect();

            // Drop every edge touching this triplet's switch ports
            // (including the unchosen `in_X`, which now goes nowhere).
            graph.connections.retain(|c| {
                !(c.to.node == switch_id && (c.to.port == *in_0 || c.to.port == *in_1))
                    && !(c.from.node == switch_id && c.from.port == *out_name)
            });

            // Splice upstream → downstreams. If no upstream, the
            // downstreams are now unconnected and fall back to their
            // declared port defaults via `EvalContext::input` /
            // `CompileWgslCtx::input`.
            if let Some(up) = upstream {
                for down in downstreams {
                    graph.connections.push(Connection {
                        from: up.clone(),
                        to: down,
                    });
                }
            }
        }

        // remove_node also drops any stray edges still touching the switch
        // (e.g. anyone wiring `out_X` after we cleared it — shouldn't
        // happen, but cheap insurance).
        let _ = graph.remove_node(switch_id);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::brush;
    use crate::gpu::params::ParamValue;
    use crate::nodegraph::{Connection, Graph, NodeId, PortRef};

    /// Helper: instantiate a node of `type_id` from the live registry,
    /// using the registration's default params.
    fn add_node(graph: &mut Graph<BrushWireType>, type_id: &str) -> NodeId {
        let registry = brush::registry();
        let reg = registry
            .get(type_id)
            .unwrap_or_else(|| panic!("no registration for {type_id}"));
        let params: Vec<ParamValue> = reg.params.iter().map(|p| p.default_value()).collect();
        graph.add_node(type_id, reg.ports.clone(), params)
    }

    fn pr(node: NodeId, port: &str) -> PortRef {
        PortRef {
            node,
            port: port.into(),
        }
    }

    /// `paint_color → switch.in_0_vec4 → switch.out_vec4 → stamp.color`,
    /// select=0 → expect a direct `paint_color → stamp.color` after rewrite.
    #[test]
    fn enabled_default_splices_through() {
        let mut graph = Graph::<BrushWireType>::new();
        let paint_color = add_node(&mut graph, "paint_color");
        let switch_id = add_node(&mut graph, TYPE_ID);
        let stamp = add_node(&mut graph, "stamp");

        graph
            .connect(pr(paint_color, "color"), pr(switch_id, "in_0_vec4"))
            .unwrap();
        graph
            .connect(pr(switch_id, "out_vec4"), pr(stamp, "color"))
            .unwrap();

        apply_to(&mut graph);

        // Switch removed.
        assert!(
            graph.nodes().get(&switch_id).is_none(),
            "switch node should be removed after rewrite"
        );
        // Direct edge spliced through.
        assert!(
            graph
                .connections
                .iter()
                .any(|c| c.from == pr(paint_color, "color") && c.to == pr(stamp, "color")),
            "expected paint_color → stamp.color after splice; got {:?}",
            graph.connections
        );
    }

    /// Same wiring, `select=1` → chosen side (`in_1_vec4`) is unconnected,
    /// so `stamp.color` ends up disconnected after rewrite.
    #[test]
    fn disabled_drops_wire_so_downstream_uses_default() {
        let mut graph = Graph::<BrushWireType>::new();
        let paint_color = add_node(&mut graph, "paint_color");
        let switch_id = add_node(&mut graph, TYPE_ID);
        let stamp = add_node(&mut graph, "stamp");

        graph
            .connect(pr(paint_color, "color"), pr(switch_id, "in_0_vec4"))
            .unwrap();
        graph
            .connect(pr(switch_id, "out_vec4"), pr(stamp, "color"))
            .unwrap();

        // Flip select to 1 — `in_1_vec4` is unconnected so the wire is dropped.
        graph.set_port_default(switch_id, SELECT_PORT, 1.0).unwrap();

        apply_to(&mut graph);

        assert!(graph.nodes().get(&switch_id).is_none());
        assert!(
            !graph.connections.iter().any(|c| c.to == pr(stamp, "color")),
            "stamp.color should be unconnected after rewrite; got {:?}",
            graph.connections
        );
    }

    /// Two inputs wired; toggling `select` routes one through and drops the other.
    #[test]
    fn mux_mode_routes_chosen_input_and_drops_unchosen() {
        // select=0 → in_0 wired through.
        {
            let mut graph = Graph::<BrushWireType>::new();
            let paint_color = add_node(&mut graph, "paint_color");
            let switch_id = add_node(&mut graph, TYPE_ID);
            let stamp = add_node(&mut graph, "stamp");

            // Both sides wired; in_0 from paint_color, in_1 from another
            // unrelated source. We don't have a "constant color" node handy
            // so we fake it by leaving in_1 connected to a second paint_color.
            let paint_color_b = add_node(&mut graph, "paint_color");
            graph
                .connect(pr(paint_color, "color"), pr(switch_id, "in_0_vec4"))
                .unwrap();
            graph
                .connect(pr(paint_color_b, "color"), pr(switch_id, "in_1_vec4"))
                .unwrap();
            graph
                .connect(pr(switch_id, "out_vec4"), pr(stamp, "color"))
                .unwrap();

            apply_to(&mut graph);

            assert!(graph.nodes().get(&switch_id).is_none());
            assert!(
                graph
                    .connections
                    .iter()
                    .any(|c| c.from == pr(paint_color, "color") && c.to == pr(stamp, "color")),
                "select=0 should route paint_color (in_0) → stamp.color"
            );
            assert!(
                !graph
                    .connections
                    .iter()
                    .any(|c| c.from == pr(paint_color_b, "color")),
                "paint_color_b (in_1) should be disconnected"
            );
        }

        // select=1 → in_1 wired through.
        {
            let mut graph = Graph::<BrushWireType>::new();
            let paint_color = add_node(&mut graph, "paint_color");
            let switch_id = add_node(&mut graph, TYPE_ID);
            let stamp = add_node(&mut graph, "stamp");
            let paint_color_b = add_node(&mut graph, "paint_color");
            graph
                .connect(pr(paint_color, "color"), pr(switch_id, "in_0_vec4"))
                .unwrap();
            graph
                .connect(pr(paint_color_b, "color"), pr(switch_id, "in_1_vec4"))
                .unwrap();
            graph
                .connect(pr(switch_id, "out_vec4"), pr(stamp, "color"))
                .unwrap();

            graph.set_port_default(switch_id, SELECT_PORT, 1.0).unwrap();

            apply_to(&mut graph);

            assert!(graph.nodes().get(&switch_id).is_none());
            assert!(
                graph
                    .connections
                    .iter()
                    .any(|c| c.from == pr(paint_color_b, "color") && c.to == pr(stamp, "color")),
                "select=1 should route paint_color_b (in_1) → stamp.color"
            );
            assert!(
                !graph
                    .connections
                    .iter()
                    .any(|c| c.from == pr(paint_color, "color")),
                "paint_color (in_0) should be disconnected"
            );
        }
    }

    /// A wire into the `select` port keeps the switch in the graph so the
    /// WGSL compile can reject it. There's no other Bool-output node in the
    /// registry today (the switch is the first), so we synthesize the
    /// connection directly — `apply_to` doesn't re-validate wire types.
    #[test]
    fn dynamic_select_leaves_switch_in_place() {
        let mut graph = Graph::<BrushWireType>::new();
        let paint_color = add_node(&mut graph, "paint_color");
        let switch_id = add_node(&mut graph, TYPE_ID);
        let stamp = add_node(&mut graph, "stamp");
        graph
            .connect(pr(paint_color, "color"), pr(switch_id, "in_0_vec4"))
            .unwrap();
        graph
            .connect(pr(switch_id, "out_vec4"), pr(stamp, "color"))
            .unwrap();
        // Synthetic dynamic-select wire (no real Bool source exists yet).
        graph.connections.push(Connection {
            from: pr(paint_color, "color"),
            to: pr(switch_id, SELECT_PORT),
        });

        apply_to(&mut graph);

        assert!(
            graph.nodes().get(&switch_id).is_some(),
            "switch with dynamic select should NOT be rewritten away"
        );
        assert!(
            graph
                .connections
                .iter()
                .any(|c| c.to == pr(switch_id, SELECT_PORT)),
            "dynamic select wire should still be present"
        );
    }
}