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
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
use crate::gpu::effect::{EffectCache, EffectPipeline};
use crate::gpu::veil::{ParamDef, ParamValue, Veil, VeilRegistration};
use std::sync::Arc;

const PARAMS: &[ParamDef] = &[
    ParamDef::Int {
        name: "iterations",
        min: 1,
        max: 50,
        default: 5,
    },
    ParamDef::Float {
        name: "wetness",
        min: 0.0,
        max: 2.0,
        default: 0.5,
    },
];

/// Size of the generated RGBA noise texture used as a flow map.
const NOISE_SIZE: u32 = 256;

pub fn register() -> VeilRegistration {
    VeilRegistration {
        type_id: "watercolor",
        display_name: "Watercolor",
        params: PARAMS,
        create_pipeline: create_watercolor_pipeline,
        from_params: |params, shared| {
            let iterations = match params.first() {
                Some(ParamValue::Int(v)) => *v,
                _ => 20,
            };
            let wetness = match params.get(1) {
                Some(ParamValue::Float(v)) => *v,
                _ => 1.0,
            };
            Box::new(Watercolor::new(iterations, wetness, shared))
        },
    }
}

/// GPU uniforms for the watercolor shader.
/// `pass_type`: 0 = RGB→CMYK init, 1 = blur iteration, 2 = CMYK→RGB final.
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct WatercolorUniforms {
    pass_type: i32,
    wetness: f32,
    resolution_x: f32,
    resolution_y: f32,
}

#[derive(Clone, Debug)]
pub struct Watercolor {
    pub iterations: i32,
    pub wetness: f32,
    shared: Arc<EffectPipeline>,
}

impl Watercolor {
    pub fn new(iterations: i32, wetness: f32, shared: Arc<EffectPipeline>) -> Self {
        Watercolor {
            iterations: iterations.max(1),
            wetness,
            shared,
        }
    }

    fn make_uniform_buf(
        &self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        pass_type: i32,
        width: u32,
        height: u32,
        label: &str,
    ) -> wgpu::Buffer {
        let uniforms = WatercolorUniforms {
            pass_type,
            wetness: self.wetness,
            resolution_x: width as f32,
            resolution_y: height as f32,
        };
        let buf = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some(label),
            size: std::mem::size_of::<WatercolorUniforms>() as u64,
            usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        queue.write_buffer(&buf, 0, bytemuck::bytes_of(&uniforms));
        buf
    }
}

/// Generate a 256x256 RGBA noise texture. Uses a simple xorshift PRNG
/// seeded deterministically so every instance gets the same flow map.
fn generate_noise_data() -> Vec<u8> {
    let total = (NOISE_SIZE * NOISE_SIZE * 4) as usize;
    let mut data = Vec::with_capacity(total);
    let mut state: u32 = 0x12345678;
    for _ in 0..total {
        // xorshift32
        state ^= state << 13;
        state ^= state >> 17;
        state ^= state << 5;
        data.push((state >> 24) as u8);
    }
    data
}

fn create_noise_texture(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
) -> (wgpu::Texture, wgpu::TextureView) {
    let data = generate_noise_data();
    let tex = device.create_texture(&wgpu::TextureDescriptor {
        label: Some("watercolor-noise"),
        size: wgpu::Extent3d {
            width: NOISE_SIZE,
            height: NOISE_SIZE,
            depth_or_array_layers: 1,
        },
        mip_level_count: 1,
        sample_count: 1,
        dimension: wgpu::TextureDimension::D2,
        format: wgpu::TextureFormat::Rgba8Unorm,
        usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
        view_formats: &[],
    });
    queue.write_texture(
        wgpu::TexelCopyTextureInfo {
            texture: &tex,
            mip_level: 0,
            origin: wgpu::Origin3d::ZERO,
            aspect: wgpu::TextureAspect::All,
        },
        &data,
        wgpu::TexelCopyBufferLayout {
            offset: 0,
            bytes_per_row: Some(NOISE_SIZE * 4),
            rows_per_image: Some(NOISE_SIZE),
        },
        wgpu::Extent3d {
            width: NOISE_SIZE,
            height: NOISE_SIZE,
            depth_or_array_layers: 1,
        },
    );
    let view = tex.create_view(&wgpu::TextureViewDescriptor::default());
    (tex, view)
}

impl Veil for Watercolor {
    fn type_id(&self) -> &'static str {
        "watercolor"
    }

    fn clone_boxed(&self) -> Box<dyn Veil> {
        Box::new(self.clone())
    }

    fn param_values(&self) -> Vec<ParamValue> {
        vec![
            ParamValue::Int(self.iterations),
            ParamValue::Float(self.wetness),
        ]
    }

    fn create_cache(
        &self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        ping_pong_views: &[wgpu::TextureView; 2],
        sampler: &wgpu::Sampler,
        render_width: u32,
        render_height: u32,
    ) -> EffectCache {
        let layout = &self.shared.bind_group_layout;

        // --- Uniform buffers for each pass type ---
        let init_ub = self.make_uniform_buf(
            device,
            queue,
            0,
            render_width,
            render_height,
            "watercolor-ub-init",
        );
        let blur_ub = self.make_uniform_buf(
            device,
            queue,
            1,
            render_width,
            render_height,
            "watercolor-ub-blur",
        );
        let final_ub = self.make_uniform_buf(
            device,
            queue,
            2,
            render_width,
            render_height,
            "watercolor-ub-final",
        );

        // --- Noise texture + repeat sampler for flow-map bias ---
        let (noise_tex, noise_view) = create_noise_texture(device, queue);
        let noise_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
            label: Some("watercolor-noise-sampler"),
            address_mode_u: wgpu::AddressMode::Repeat,
            address_mode_v: wgpu::AddressMode::Repeat,
            mag_filter: wgpu::FilterMode::Linear,
            min_filter: wgpu::FilterMode::Linear,
            ..Default::default()
        });

        // --- Two aux textures for iterative ping-pong blur ---
        let tex_usage =
            wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING;
        let mut aux_textures = vec![noise_tex];
        let mut aux_views = Vec::with_capacity(3);
        // aux_views[0..2] = ping-pong blur targets, aux_views[2] = noise
        for i in 0..2 {
            let tex = device.create_texture(&wgpu::TextureDescriptor {
                label: Some(&format!("watercolor-aux-{i}")),
                size: wgpu::Extent3d {
                    width: render_width,
                    height: render_height,
                    depth_or_array_layers: 1,
                },
                mip_level_count: 1,
                sample_count: 1,
                dimension: wgpu::TextureDimension::D2,
                format: wgpu::TextureFormat::Rgba8Unorm,
                usage: tex_usage,
                view_formats: &[],
            });
            let view = tex.create_view(&wgpu::TextureViewDescriptor::default());
            aux_views.push(view);
            aux_textures.push(tex);
        }
        aux_views.push(noise_view);

        // Helper: build a bind group with the given input texture view + uniform buf.
        // All bind groups share the noise texture at binding 3.
        let make_bg = |label: &str, input_view: &wgpu::TextureView, ub: &wgpu::Buffer| {
            device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some(label),
                layout,
                entries: &[
                    wgpu::BindGroupEntry {
                        binding: 0,
                        resource: wgpu::BindingResource::TextureView(input_view),
                    },
                    wgpu::BindGroupEntry {
                        binding: 1,
                        resource: wgpu::BindingResource::Sampler(sampler),
                    },
                    wgpu::BindGroupEntry {
                        binding: 2,
                        resource: ub.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 3,
                        resource: wgpu::BindingResource::TextureView(&aux_views[2]),
                    },
                    wgpu::BindGroupEntry {
                        binding: 4,
                        resource: wgpu::BindingResource::Sampler(&noise_sampler),
                    },
                ],
            })
        };

        // [0] = init: reads ping_pong[i] → writes aux_views[0]
        let init_bgs: [wgpu::BindGroup; 2] = std::array::from_fn(|i| {
            make_bg(
                &format!("watercolor-init-{i}"),
                &ping_pong_views[i],
                &init_ub,
            )
        });

        // [1] = blur: [j] reads aux_views[j] (writes to aux_views[1-j])
        let blur_bgs: [wgpu::BindGroup; 2] = std::array::from_fn(|j| {
            make_bg(&format!("watercolor-blur-{j}"), &aux_views[j], &blur_ub)
        });

        // [2] = final: [j] reads aux_views[j] → writes to dst
        let final_bgs: [wgpu::BindGroup; 2] = std::array::from_fn(|j| {
            make_bg(&format!("watercolor-final-{j}"), &aux_views[j], &final_ub)
        });

        EffectCache {
            uniform_bufs: vec![init_ub, blur_ub, final_ub],
            bind_groups: vec![init_bgs, blur_bgs, final_bgs],
            aux_textures,
            aux_views,
            aux_pipelines: vec![],
        }
    }

    fn encode(
        &self,
        encoder: &mut wgpu::CommandEncoder,
        cache: &EffectCache,
        src_idx: usize,
        dst_view: &wgpu::TextureView,
    ) {
        let pipeline = &self.shared.pipeline;
        let n = self.iterations as usize;

        // Pass 0: RGB → CMYK — ping_pong[src] → aux[0]
        {
            let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                label: Some("watercolor-init"),
                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                    view: &cache.aux_views[0],
                    resolve_target: None,
                    depth_slice: None,
                    ops: wgpu::Operations {
                        load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
                        store: wgpu::StoreOp::Store,
                    },
                })],
                ..Default::default()
            });
            rpass.set_pipeline(pipeline);
            rpass.set_bind_group(0, &cache.bind_groups[0][src_idx], &[]);
            rpass.draw(0..3, 0..1);
        }

        // Passes 1..N: blur iterations — aux[src] → aux[dst], ping-ponging.
        // Iteration i reads aux[i%2], writes to aux[(i+1)%2].
        for i in 0..n {
            let read_idx = i % 2;
            let write_idx = (i + 1) % 2;
            let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                label: Some("watercolor-blur"),
                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                    view: &cache.aux_views[write_idx],
                    resolve_target: None,
                    depth_slice: None,
                    ops: wgpu::Operations {
                        load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
                        store: wgpu::StoreOp::Store,
                    },
                })],
                ..Default::default()
            });
            rpass.set_pipeline(pipeline);
            rpass.set_bind_group(0, &cache.bind_groups[1][read_idx], &[]);
            rpass.draw(0..3, 0..1);
        }

        // Final pass: CMYK → RGB — aux[last_written] → dst
        let last_written = n % 2;
        {
            let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                label: Some("watercolor-final"),
                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                    view: dst_view,
                    resolve_target: None,
                    depth_slice: None,
                    ops: wgpu::Operations {
                        load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
                        store: wgpu::StoreOp::Store,
                    },
                })],
                ..Default::default()
            });
            rpass.set_pipeline(pipeline);
            rpass.set_bind_group(0, &cache.bind_groups[2][last_written], &[]);
            rpass.draw(0..3, 0..1);
        }
    }
}

fn create_watercolor_pipeline(
    device: &wgpu::Device,
    _format: wgpu::TextureFormat,
) -> EffectPipeline {
    let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
        label: Some("watercolor-bgl"),
        entries: &[
            wgpu::BindGroupLayoutEntry {
                binding: 0,
                visibility: wgpu::ShaderStages::FRAGMENT,
                ty: wgpu::BindingType::Texture {
                    sample_type: wgpu::TextureSampleType::Float { filterable: true },
                    view_dimension: wgpu::TextureViewDimension::D2,
                    multisampled: false,
                },
                count: None,
            },
            wgpu::BindGroupLayoutEntry {
                binding: 1,
                visibility: wgpu::ShaderStages::FRAGMENT,
                ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                count: None,
            },
            wgpu::BindGroupLayoutEntry {
                binding: 2,
                visibility: wgpu::ShaderStages::FRAGMENT,
                ty: wgpu::BindingType::Buffer {
                    ty: wgpu::BufferBindingType::Uniform,
                    has_dynamic_offset: false,
                    min_binding_size: None,
                },
                count: None,
            },
            wgpu::BindGroupLayoutEntry {
                binding: 3,
                visibility: wgpu::ShaderStages::FRAGMENT,
                ty: wgpu::BindingType::Texture {
                    sample_type: wgpu::TextureSampleType::Float { filterable: true },
                    view_dimension: wgpu::TextureViewDimension::D2,
                    multisampled: false,
                },
                count: None,
            },
            wgpu::BindGroupLayoutEntry {
                binding: 4,
                visibility: wgpu::ShaderStages::FRAGMENT,
                ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                count: None,
            },
        ],
    });

    let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
        label: Some("watercolor-pipeline-layout"),
        bind_group_layouts: &[Some(&bind_group_layout)],
        immediate_size: 0,
    });

    let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
        label: Some("watercolor-shader"),
        source: wgpu::ShaderSource::Wgsl(
            include_str!("../../../shaders/veils/watercolor.wgsl").into(),
        ),
    });

    let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
        label: Some("watercolor-pipeline"),
        layout: Some(&pipeline_layout),
        vertex: wgpu::VertexState {
            module: &shader,
            entry_point: Some("vs_main"),
            buffers: &[],
            compilation_options: Default::default(),
        },
        fragment: Some(wgpu::FragmentState {
            module: &shader,
            entry_point: Some("fs_watercolor"),
            targets: &[Some(wgpu::ColorTargetState {
                format: wgpu::TextureFormat::Rgba8Unorm,
                blend: None,
                write_mask: wgpu::ColorWrites::ALL,
            })],
            compilation_options: Default::default(),
        }),
        primitive: wgpu::PrimitiveState {
            topology: wgpu::PrimitiveTopology::TriangleList,
            ..Default::default()
        },
        depth_stencil: None,
        multisample: wgpu::MultisampleState::default(),
        multiview_mask: None,
        cache: None,
    });

    EffectPipeline {
        pipeline,
        bind_group_layout,
    }
}