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
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
//! GpuPaintTarget and readback GPU integration tests.
//!
//! Tests compositing, alpha blending, erasing, masking, and GPU readback.
//! Run with: `cargo test -p darkly --test paint_target`

use darkly::gpu::paint_target::{GpuPaintTarget, PaintPipelines};
use darkly::gpu::readback;
use darkly::gpu::test_utils::*;

fn encoder(device: &wgpu::Device) -> wgpu::CommandEncoder {
    device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
        label: Some("test"),
    })
}

fn submit(queue: &wgpu::Queue, encoder: wgpu::CommandEncoder) {
    queue.submit([encoder.finish()]);
}

// ============================================================================
// GpuPaintTarget tests
// ============================================================================

/// composite_circle: paint red circle, verify center is red and corners are transparent.
#[test]
fn paint_target_composite_circle() {
    let (device, queue) = test_device();
    let (w, h) = (128, 128);
    let fmt = wgpu::TextureFormat::Rgba8Unorm;

    let (tex, view) = create_test_texture(&device, &queue, w, h, &vec![0u8; (w * h * 4) as usize]);
    let pipelines = PaintPipelines::new(
        &device,
        &queue,
        &darkly::gpu::selection::selection_mask_bgl(&device),
    );
    let target = GpuPaintTarget::from_canvas_texture(
        &tex,
        &view,
        fmt,
        darkly::coord::CanvasRect::from_xywh(0, 0, w, h),
    );

    let mut enc = encoder(&device);
    target.composite_circle(
        &mut enc,
        &pipelines,
        &queue,
        64.0,
        64.0,
        10.0,
        [255, 0, 0, 255],
        1.0,
    );
    submit(&queue, enc);

    let pixels = readback_texture(&device, &queue, &tex, fmt, w, h);

    // Center pixel (64, 64) should be red.
    let c = ((64 * w + 64) * 4) as usize;
    assert_eq!(pixels[c], 255, "center R={}, expected 255", pixels[c]);
    assert_eq!(pixels[c + 1], 0, "center G={}, expected 0", pixels[c + 1]);
    assert_eq!(pixels[c + 2], 0, "center B={}, expected 0", pixels[c + 2]);
    assert_eq!(
        pixels[c + 3],
        255,
        "center A={}, expected 255",
        pixels[c + 3]
    );

    // Corner (0, 0) should be transparent.
    assert_eq!(pixels[3], 0, "corner alpha={}, expected 0", pixels[3]);

    // Pixel inside circle (64, 57) — 7px from center, radius 10.
    let inside = ((57 * w + 64) * 4) as usize;
    assert!(
        pixels[inside + 3] > 0,
        "inside circle should be non-transparent"
    );

    // Pixel outside circle (64, 50) — 14px from center, radius 10.
    let outside = ((50 * w + 64) * 4) as usize;
    assert_eq!(
        pixels[outside + 3],
        0,
        "outside circle should be transparent, got A={}",
        pixels[outside + 3]
    );
}

/// Alpha blending: composite circle on semi-transparent background.
#[test]
fn paint_target_alpha_blending() {
    let (device, queue) = test_device();
    let (w, h) = (128, 128);
    let fmt = wgpu::TextureFormat::Rgba8Unorm;

    let bg: Vec<u8> = (0..w * h).flat_map(|_| [0u8, 0, 255, 128]).collect();
    let (tex, view) = create_test_texture(&device, &queue, w, h, &bg);
    let pipelines = PaintPipelines::new(
        &device,
        &queue,
        &darkly::gpu::selection::selection_mask_bgl(&device),
    );
    let target = GpuPaintTarget::from_canvas_texture(
        &tex,
        &view,
        fmt,
        darkly::coord::CanvasRect::from_xywh(0, 0, w, h),
    );

    // Paint red circle at center with 50% alpha.
    let mut enc = encoder(&device);
    target.composite_circle(
        &mut enc,
        &pipelines,
        &queue,
        64.0,
        64.0,
        10.0,
        [255, 0, 0, 128],
        1.0,
    );
    submit(&queue, enc);

    let pixels = readback_texture(&device, &queue, &tex, fmt, w, h);
    let c = ((64 * w + 64) * 4) as usize;
    let (r, g, b, a) = (pixels[c], pixels[c + 1], pixels[c + 2], pixels[c + 3]);

    // Source-over: out.a = src.a + dst.a * (1 - src.a) > 128
    assert!(r > 0, "blended pixel should have red, got R={r}");
    assert!(b > 0, "blended pixel should have blue, got B={b}");
    assert!(a > 128, "blended alpha should be > 128, got A={a}");
    assert_eq!(g, 0, "green should remain 0, got G={g}");
}

/// R8 mask target: paint black on fully-revealed mask.
#[test]
fn paint_target_r8_mask() {
    let (device, queue) = test_device();
    let (w, h) = (128, 128);
    let fmt = wgpu::TextureFormat::R8Unorm;

    let white: Vec<u8> = vec![255u8; (w * h) as usize];
    let (tex, view) = create_test_texture_with_format(&device, &queue, w, h, &white, fmt);
    let pipelines = PaintPipelines::new(
        &device,
        &queue,
        &darkly::gpu::selection::selection_mask_bgl(&device),
    );
    let target = GpuPaintTarget::from_canvas_texture(
        &tex,
        &view,
        fmt,
        darkly::coord::CanvasRect::from_xywh(0, 0, w, h),
    );

    // Composite black → luminance 0 → mask toward 0.
    let mut enc = encoder(&device);
    target.composite_circle(
        &mut enc,
        &pipelines,
        &queue,
        64.0,
        64.0,
        10.0,
        [0, 0, 0, 255],
        1.0,
    );
    submit(&queue, enc);

    let pixels = readback_texture(&device, &queue, &tex, fmt, w, h);

    let center = (64 * w + 64) as usize;
    assert!(
        pixels[center] < 10,
        "center mask should be near 0, got {}",
        pixels[center]
    );
    assert_eq!(
        pixels[0], 255,
        "corner mask should be 255, got {}",
        pixels[0]
    );
}

/// Selection masking: left half selected, right half not.
#[test]
fn paint_target_selection_masking() {
    let (device, queue) = test_device();
    let (w, h) = (128, 128);
    let fmt = wgpu::TextureFormat::Rgba8Unorm;

    let (tex, view) = create_test_texture(&device, &queue, w, h, &vec![0u8; (w * h * 4) as usize]);
    let pipelines = PaintPipelines::new(
        &device,
        &queue,
        &darkly::gpu::selection::selection_mask_bgl(&device),
    );

    // Selection mask: left half = 255, right half = 0.
    let mut sel_data = vec![0u8; (w * h) as usize];
    for y in 0..h {
        for x in 0..w / 2 {
            sel_data[(y * w + x) as usize] = 255;
        }
    }
    let (_sel_tex, sel_view) = create_test_texture_with_format(
        &device,
        &queue,
        w,
        h,
        &sel_data,
        wgpu::TextureFormat::R8Unorm,
    );

    let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
        label: Some("test-sampler"),
        mag_filter: wgpu::FilterMode::Linear,
        min_filter: wgpu::FilterMode::Linear,
        ..Default::default()
    });
    let sel_bind_group = pipelines.create_selection_bind_group(&device, &sel_view, &sampler);

    let target = GpuPaintTarget::from_canvas_texture(
        &tex,
        &view,
        fmt,
        darkly::coord::CanvasRect::from_xywh(0, 0, w, h),
    );

    let mut enc = encoder(&device);
    target.composite_circle_with_selection(
        &mut enc,
        &pipelines,
        &queue,
        64.0,
        64.0,
        30.0,
        [255, 0, 0, 255],
        1.0,
        &sel_bind_group,
    );
    submit(&queue, enc);

    let pixels = readback_texture(&device, &queue, &tex, fmt, w, h);

    // Left side, within circle (48, 64) — 16px from center, well within r=30.
    let left = ((64 * w + 48) * 4) as usize;
    assert!(
        pixels[left + 3] > 0,
        "left (selected) should have paint, A={}",
        pixels[left + 3]
    );

    // Right side, within circle (80, 64) — 16px from center, within r=30 but unselected.
    let right = ((64 * w + 80) * 4) as usize;
    assert_eq!(
        pixels[right + 3],
        0,
        "right (unselected) should be transparent, A={}",
        pixels[right + 3]
    );
}

// ============================================================================
// Readback tests
// ============================================================================

/// Readback round-trip: known pattern → readback → exact match.
#[test]
fn readback_round_trip() {
    let (device, queue) = test_device();
    let (w, h) = (128, 128);
    let fmt = wgpu::TextureFormat::Rgba8Unorm;

    let mut data = vec![0u8; (w * h * 4) as usize];
    for y in 0..h {
        for x in 0..w {
            let i = ((y * w + x) * 4) as usize;
            data[i] = y as u8;
            data[i + 1] = x as u8;
            data[i + 2] = 42;
            data[i + 3] = 255;
        }
    }
    let (tex, _view) = create_test_texture(&device, &queue, w, h, &data);

    let readback = readback_texture(&device, &queue, &tex, fmt, w, h);
    assert_eq!(readback, data, "readback should exactly match input");
}

/// Readback sub-rect: read top-left 64×64 of a 128×128 texture.
#[test]
fn readback_sub_rect() {
    let (device, queue) = test_device();
    let (w, h) = (128, 128);
    let fmt = wgpu::TextureFormat::Rgba8Unorm;

    // Quadrant colors.
    let mut data = vec![0u8; (w * h * 4) as usize];
    for y in 0..h {
        for x in 0..w {
            let i = ((y * w + x) * 4) as usize;
            let (r, g, b) = match (x < 64, y < 64) {
                (true, true) => (255, 0, 0),
                (false, true) => (0, 255, 0),
                (true, false) => (0, 0, 255),
                (false, false) => (255, 255, 0),
            };
            data[i] = r;
            data[i + 1] = g;
            data[i + 2] = b;
            data[i + 3] = 255;
        }
    }
    let (tex, _view) = create_test_texture(&device, &queue, w, h, &data);

    // Read only top-left 64×64.
    let mut enc = encoder(&device);
    let request = readback::request_readback(
        &device,
        &mut enc,
        &tex,
        fmt,
        darkly::coord::LayerRect::from_xywh(0, 0, 64, 64),
    );
    submit(&queue, enc);
    let pixels = request.blocking_read(&device);

    assert_eq!(pixels.len(), (64 * 64 * 4) as usize);

    // All pixels should be red.
    for y in 0..64u32 {
        for x in 0..64u32 {
            let i = ((y * 64 + x) * 4) as usize;
            assert_eq!(
                &pixels[i..i + 4],
                &[255, 0, 0, 255],
                "pixel ({x},{y}) should be red, got {:?}",
                &pixels[i..i + 4]
            );
        }
    }
}

/// Painting on an offset paste-extent layer: the painted pixels must land at
/// the canvas position requested, not at (canvas_pos − offset).
#[test]
fn paint_target_composite_circle_on_offset_layer() {
    let (device, queue) = test_device();
    let canvas_w: u32 = 256;
    let canvas_h: u32 = 256;
    // Layer's (0,0) sits at canvas (-100, -100). Painting at canvas (50, 50)
    // should land at layer-local (150, 150).
    let layer_off_x: i32 = -100;
    let layer_off_y: i32 = -100;
    let (lw, lh) = (400u32, 400u32);
    let fmt = wgpu::TextureFormat::Rgba8Unorm;

    let (tex, view) =
        create_test_texture(&device, &queue, lw, lh, &vec![0u8; (lw * lh * 4) as usize]);
    let pipelines = PaintPipelines::new(
        &device,
        &queue,
        &darkly::gpu::selection::selection_mask_bgl(&device),
    );
    let target = GpuPaintTarget::from_extent(
        &tex,
        &view,
        fmt,
        darkly::coord::CanvasRect::from_xywh(layer_off_x, layer_off_y, lw, lh),
        darkly::coord::CanvasRect::from_xywh(0, 0, canvas_w, canvas_h),
    );

    let mut enc = encoder(&device);
    target.composite_circle(
        &mut enc,
        &pipelines,
        &queue,
        50.0,
        50.0,
        10.0,
        [0, 255, 0, 255],
        1.0,
    );
    submit(&queue, enc);

    let pixels = readback_texture(&device, &queue, &tex, fmt, lw, lh);

    // Canvas (50, 50) → layer-local (50 - (-100), 50 - (-100)) = (150, 150).
    let cx_local = (50 - layer_off_x) as u32;
    let cy_local = (50 - layer_off_y) as u32;
    let c = ((cy_local * lw + cx_local) * 4) as usize;
    assert_eq!(pixels[c], 0);
    assert_eq!(
        pixels[c + 1],
        255,
        "expected green at layer-local (150,150)"
    );
    assert_eq!(pixels[c + 2], 0);
    assert_eq!(pixels[c + 3], 255);

    // The OLD buggy mapping would have painted at canvas (50,50) interpreted
    // as layer-local — i.e. at layer-local (50, 50). That position must be
    // empty.
    let bug = ((50u32 * lw + 50) * 4) as usize;
    assert_eq!(
        pixels[bug + 3],
        0,
        "layer-local (50,50) should be untouched (would be wrong-place paint)"
    );
}

/// fill_rect on an offset paste-extent layer: rect input is canvas-space.
/// A canvas-space rect at (canvas_x, canvas_y) lands at layer-local
/// (canvas_x − offset_x, canvas_y − offset_y). Regression for the
/// `clear_rect`/`fill_rect` canvas-space API contract (P1c).
#[test]
fn paint_target_fill_rect_canvas_space_on_offset_layer() {
    let (device, queue) = test_device();
    let canvas_w: u32 = 256;
    let canvas_h: u32 = 256;
    let (lw, lh) = (300u32, 300u32);
    let fmt = wgpu::TextureFormat::Rgba8Unorm;
    let off_x: i32 = -50;
    let off_y: i32 = -50;
    let (tex, view) =
        create_test_texture(&device, &queue, lw, lh, &vec![0u8; (lw * lh * 4) as usize]);
    let pipelines = PaintPipelines::new(
        &device,
        &queue,
        &darkly::gpu::selection::selection_mask_bgl(&device),
    );
    let target = GpuPaintTarget::from_extent(
        &tex,
        &view,
        fmt,
        darkly::coord::CanvasRect::from_xywh(off_x, off_y, lw, lh),
        darkly::coord::CanvasRect::from_xywh(0, 0, canvas_w, canvas_h),
    );

    // Canvas-space rect at (10, 10) size (20, 20). Maps to layer-local
    // (10 - (-50), 10 - (-50)) = (60, 60).
    let mut enc = encoder(&device);
    target.fill_rect(
        &mut enc,
        &pipelines,
        &queue,
        darkly::coord::CanvasRect::from_xywh(10, 10, 20, 20),
        [0, 0, 255, 255],
    );
    submit(&queue, enc);

    let pixels = readback_texture(&device, &queue, &tex, fmt, lw, lh);
    // Layer-local (65, 65) — interior of the rect.
    let lx = (15 - off_x) as u32;
    let ly = (15 - off_y) as u32;
    let c = ((ly * lw + lx) * 4) as usize;
    assert_eq!(
        pixels[c + 2],
        255,
        "rect interior should be blue at layer-local ({lx},{ly})"
    );
    assert_eq!(pixels[c + 3], 255);
    // Layer-local (50, 50) — would be the OLD (target-local) interpretation;
    // must be transparent under the canvas-space contract.
    let outside = ((50u32 * lw + 50) * 4) as usize;
    assert_eq!(
        pixels[outside + 3],
        0,
        "layer-local (50,50) should be untouched (canvas-space input lands at layer-local (60,60))"
    );
}

/// Canvas-negative origin: a fill_rect at canvas (-30, 10) on a layer with
/// offset (-50, -50) should land at layer-local (20, 60). Confirms the
/// `[i32; 4]` rect contract handles negative canvas coordinates.
#[test]
fn paint_target_fill_rect_canvas_negative_origin_on_offset_layer() {
    let (device, queue) = test_device();
    let canvas_w: u32 = 128;
    let canvas_h: u32 = 128;
    let (lw, lh) = (200u32, 200u32);
    let fmt = wgpu::TextureFormat::Rgba8Unorm;
    let off_x: i32 = -50;
    let off_y: i32 = -50;
    let (tex, view) =
        create_test_texture(&device, &queue, lw, lh, &vec![0u8; (lw * lh * 4) as usize]);
    let pipelines = PaintPipelines::new(
        &device,
        &queue,
        &darkly::gpu::selection::selection_mask_bgl(&device),
    );
    let target = GpuPaintTarget::from_extent(
        &tex,
        &view,
        fmt,
        darkly::coord::CanvasRect::from_xywh(off_x, off_y, lw, lh),
        darkly::coord::CanvasRect::from_xywh(0, 0, canvas_w, canvas_h),
    );

    // Canvas-space rect at (-30, 10) size (10, 10). Maps to layer-local (20, 60).
    let mut enc = encoder(&device);
    target.fill_rect(
        &mut enc,
        &pipelines,
        &queue,
        darkly::coord::CanvasRect::from_xywh(-30, 10, 10, 10),
        [0, 255, 0, 255],
    );
    submit(&queue, enc);

    let pixels = readback_texture(&device, &queue, &tex, fmt, lw, lh);
    // Layer-local (25, 65) — interior.
    let lx = (-25 - off_x) as u32;
    let ly = (15 - off_y) as u32;
    let c = ((ly * lw + lx) * 4) as usize;
    assert_eq!(
        pixels[c + 1],
        255,
        "rect interior should be green at layer-local ({lx},{ly})"
    );
}