xgpui-shader 0.5.0

Shader and paint extension for xgpui with gradients, brushes, and portable WGSL shaders
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
use super::*;
use crate::brush::text::TextBrush;
use gpui::{
    AtlasKey, DevicePixels, FontId, GlyphId, MonochromeSprite, RenderGlyphParams,
    TransformationMatrix,
};
use std::borrow::Cow;

pub(super) fn glyph(tile: gpui::AtlasTile, x: f32, scale: f32) -> MonochromeSprite {
    MonochromeSprite {
        order: 0,
        pad: 0,
        bounds: Bounds::new(
            point(
                gpui::ScaledPixels(x * scale),
                gpui::ScaledPixels(8. * scale),
            ),
            size(
                gpui::ScaledPixels(tile.bounds.size.width.0 as f32),
                gpui::ScaledPixels(tile.bounds.size.height.0 as f32),
            ),
        ),
        content_mask: ContentMask {
            bounds: Bounds::new(point(px(0.), px(0.)), size(px(56.), px(32.))).scale(scale),
        },
        color: gpui::white(),
        tile,
        transformation: TransformationMatrix::unit(),
    }
}
pub(super) fn masked_draw(brush: &TextBrush, glyph: MonochromeSprite, scale: f32) -> ShaderDraw {
    let parameters = brush
        .parameters(
            &glyph,
            Bounds::new(point(px(8.), px(8.)), size(px(64.), px(16.))),
            scale,
        )
        .unwrap();
    ShaderDraw {
        order: 0,
        bounds: glyph.bounds,
        content_mask: glyph.content_mask,
        opacity: 0.5,
        scale_factor: scale,
        program: brush.program.scene_program(),
        path_mask: None,
        glyph_mask: Some(glyph.tile),
        data: brush.program.pack(&parameters).unwrap(),
    }
}

#[test]
fn text_programs_translate_share_identity_and_keep_instance_size() {
    let base = Brush::linear(90., stops()).unwrap();
    let a = TextBrush::new(base.clone()).unwrap();
    let b = TextBrush::new(base).unwrap();
    assert_eq!(a.program.id(), b.program.id());
    assert!(a.program.scene_program().uses_glyph_mask());
    assert_eq!(std::mem::size_of::<BrushInstance>(), 128);
    for kind in [
        crate::NoiseKind::Simplex,
        crate::NoiseKind::Worley,
        crate::NoiseKind::WorleyF2,
        crate::NoiseKind::Dither,
    ] {
        for mode in [
            crate::BlendMode::Normal,
            crate::BlendMode::Multiply,
            crate::BlendMode::Screen,
            crate::BlendMode::Overlay,
            crate::BlendMode::ColorDodge,
            crate::BlendMode::SoftLight,
            crate::BlendMode::LinearBurn,
        ] {
            let noise = crate::Noise {
                kind,
                frequency: if kind == crate::NoiseKind::Dither {
                    1.
                } else {
                    0.04
                },
                octaves: 1,
                strength: 0.5,
            };
            TextBrush::new(
                Brush::solid([1.; 4])
                    .unwrap()
                    .with_noise(noise, mode)
                    .unwrap(),
            )
            .unwrap();
        }
    }
}

#[cfg(feature = "wgpu")]
#[test]
fn glyph_atlas_mask_multiplies_alpha_and_uses_full_text_bounds() {
    use gpui::{PlatformHeadlessRenderer, Quad};
    let mut renderers: Vec<Box<dyn PlatformHeadlessRenderer>> =
        vec![Box::new(gpui_wgpu::WgpuHeadlessRenderer::new().unwrap())];
    #[cfg(target_os = "macos")]
    renderers.push(Box::new(
        gpui_apple::metal_renderer::MetalHeadlessRenderer::new()
            .expect("Metal renderer initialization"),
    ));
    #[cfg(target_os = "windows")]
    if std::env::var("XGPUI_BRUSH_NATIVE").as_deref() == Ok("1") {
        renderers.push(Box::new(gpui_windows::Dx11HeadlessRenderer::new().unwrap()));
    }
    let stops = stops();
    let brush = TextBrush::new(Brush::linear(90., stops.clone()).unwrap()).unwrap();
    for renderer in &mut renderers {
        for scale in [1., 1.5, 2.] {
            let n = (16. * scale) as i32;
            let bytes: Vec<u8> = (0..n * n)
                .map(|i| [0, 64, 128, 255][((i % n) % 4) as usize])
                .collect();
            let key = AtlasKey::Glyph(RenderGlyphParams {
                font_id: FontId(0),
                glyph_id: GlyphId(n as u32),
                font_size: px(16.),
                subpixel_variant: point(0, 0),
                scale_factor: scale,
                is_emoji: false,
                subpixel_rendering: false,
                dilation: 0,
            });
            let tile = renderer
                .sprite_atlas()
                .get_or_insert_with(key, &mut || {
                    Ok(Some((
                        size(DevicePixels(n), DevicePixels(n)),
                        Cow::Owned(bytes.clone()),
                    )))
                })
                .unwrap()
                .unwrap();
            let mut scene = Scene::default();
            let full = Bounds::new(point(px(0.), px(0.)), size(px(96.), px(32.))).scale(scale);
            scene.insert_primitive(Quad {
                bounds: full,
                content_mask: ContentMask { bounds: full },
                background: gpui::black().into(),
                ..Default::default()
            });
            scene.insert_primitive(Primitive::Shader(masked_draw(
                &brush,
                glyph(tile, 8., scale),
                scale,
            )));
            scene.insert_primitive(Primitive::Shader(masked_draw(
                &brush,
                glyph(tile, 48., scale),
                scale,
            )));
            scene.finish();
            let image = renderer
                .render_scene_to_image(
                    &scene,
                    size(
                        DevicePixels((96. * scale) as i32),
                        DevicePixels((32. * scale) as i32),
                    ),
                )
                .unwrap();
            for origin in [8., 48.] {
                for local_x in 0..8 {
                    let x = (origin * scale) as u32 + local_x;
                    let y = (12. * scale) as u32;
                    let coverage = [0, 64, 128, 255][local_x as usize % 4] as f32 / 255.;
                    let t = ((x as f32 + 0.5) / scale - 8.) / 64.;
                    let expected = reference(&stops, t, 0.5 * coverage);
                    let pixel = image.get_pixel(x, y).0;
                    for i in 0..3 {
                        assert!(
                            pixel[i].abs_diff(expected[i]) <= 2,
                            "glyph scale={scale} x={x}: {pixel:?}, expected={expected:?}"
                        );
                    }
                }
            }
            assert_eq!(
                &image
                    .get_pixel((60. * scale) as u32, (12. * scale) as u32)
                    .0[..3],
                &[0, 0, 0],
                "clip must not remap glyph brush coordinates"
            );
            assert!(scene.custom_draws.is_empty());
            assert!(scene.monochrome_sprites.is_empty());
            assert_eq!(
                scene
                    .batches()
                    .filter(|b| matches!(b, PrimitiveBatch::Shaders { .. }))
                    .count(),
                1
            );
            let mut replay = Scene::default();
            replay.replay(0..scene.len(), &scene);
            replay.finish();
            let repeated = renderer
                .render_scene_to_image(
                    &replay,
                    size(
                        DevicePixels((96. * scale) as i32),
                        DevicePixels((32. * scale) as i32),
                    ),
                )
                .unwrap();
            assert_eq!(
                image, repeated,
                "replay must retain glyph atlas coverage and paint snapshot"
            );
            // Same program, different atlas textures cannot form one batch.
            let mut second = masked_draw(&brush, glyph(tile, 48., scale), scale);
            second.glyph_mask.as_mut().unwrap().texture_id.index += 1;
            let mut split = Scene::default();
            split.insert_primitive(Primitive::Shader(masked_draw(
                &brush,
                glyph(tile, 8., scale),
                scale,
            )));
            split.insert_primitive(Primitive::Shader(second));
            split.finish();
            assert_eq!(split.batches().count(), 2);
        }
    }
}

#[cfg(all(feature = "wgpu", target_os = "macos"))]
#[test]
fn real_text_wrapping_nested_and_deferred_scopes_leave_emoji_and_plain_text_native() {
    use gpui::*;
    use std::sync::{Arc, Mutex};
    #[derive(Default)]
    struct Captured {
        shaders: Vec<ShaderDraw>,
        mono: usize,
        emoji: Vec<gpui::PolychromeSprite>,
        custom: usize,
    }
    struct Recorder {
        inner: Box<dyn PlatformHeadlessRenderer>,
        captured: Arc<Mutex<Captured>>,
    }
    impl PlatformHeadlessRenderer for Recorder {
        fn sprite_atlas(&self) -> Arc<dyn PlatformAtlas> {
            self.inner.sprite_atlas()
        }
        fn render_scene(&mut self, scene: &Scene, size: Size<DevicePixels>) -> Result<()> {
            self.inner.render_scene(scene, size)
        }
        fn render_scene_to_image(
            &mut self,
            scene: &Scene,
            size: Size<DevicePixels>,
        ) -> Result<image::RgbaImage> {
            *self.captured.lock().unwrap() = Captured {
                shaders: scene.shader_draws.clone(),
                mono: scene.monochrome_sprites.len(),
                emoji: scene.polychrome_sprites.clone(),
                custom: scene.custom_draws.len(),
            };
            self.inner.render_scene_to_image(scene, size)
        }
    }
    struct CachedText {
        renders: Arc<std::sync::atomic::AtomicUsize>,
    }
    impl Render for CachedText {
        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
            self.renders
                .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            div().child("Cached text")
        }
    }
    struct View {
        cached: Entity<CachedText>,
        brush: Brush,
    }
    impl Render for View {
        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
            div()
                .size_full()
                .bg(black())
                .text_color(white())
                .font_family(".SystemUIFont")
                .text_size(px(20.))
                .flex()
                .flex_col()
                .child(
                    crate::painted()
                        .w(px(128.))
                        .h(px(100.))
                        .text_brush(Brush::linear(90., stops()).unwrap())
                        .child("Wrapped brushed text 🙂")
                        .child(
                            crate::painted()
                                .w(px(100.))
                                .h(px(24.))
                                .text_brush(Brush::solid([0., 1., 0., 1.]).unwrap())
                                .child("Nested"),
                        ),
                )
                .child(
                    crate::painted()
                        .w(px(128.))
                        .h(px(32.))
                        .text_brush(Brush::solid([0.125, 0.75, 0.25, 1.]).unwrap())
                        .child(deferred("Deferred")),
                )
                .child("Ordinary 🙂")
                .child(
                    crate::painted()
                        .w(px(128.))
                        .h(px(24.))
                        .text_brush(self.brush.clone())
                        .child(
                            self.cached
                                .clone()
                                .cached(StyleRefinement::default().w(px(128.)).h(px(24.))),
                        ),
                )
        }
    }
    for native in [false, true] {
        let captured = Arc::new(Mutex::new(Captured::default()));
        let output = captured.clone();
        let mut cx = HeadlessAppContext::with_platform(
            Arc::new(gpui_apple::AppleTextSystem::new()),
            Arc::new(()),
            move || {
                let inner: Box<dyn PlatformHeadlessRenderer> = if native {
                    Box::new(
                        gpui_apple::metal_renderer::MetalHeadlessRenderer::new()
                            .expect("Metal renderer initialization"),
                    )
                } else {
                    Box::new(gpui_wgpu::WgpuHeadlessRenderer::new()?)
                };
                Ok(Some(Box::new(Recorder {
                    inner,
                    captured: output.clone(),
                })))
            },
        );
        let renders = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let count = renders.clone();
        let window = cx
            .open_window(size(px(256.), px(240.)), |_, cx| {
                cx.new(|cx| View {
                    cached: cx.new(|_| CachedText { renders: count }),
                    brush: Brush::solid([0.6, 0.1, 0.3, 1.]).unwrap(),
                })
            })
            .unwrap();
        cx.update_window(window.into(), |_, window, cx| window.draw(cx).clear(cx))
            .unwrap();
        let scale = cx
            .update_window(window.into(), |_, window, _| window.scale_factor())
            .unwrap();
        let image = cx.capture_screenshot(window.into()).unwrap();
        assert!(
            image
                .enumerate_pixels()
                .any(|(x, y, p)| x < (128. * scale) as u32
                    && y < (90. * scale) as u32
                    && (p[0].abs_diff(p[1]) > 8 || p[2].abs_diff(p[1]) > 8)),
            "brushed glyphs must render visible color"
        );
        let result = captured.lock().unwrap();
        assert!(
            result.shaders.len() > 10,
            "real glyphs must use shader draws"
        );
        assert!(result.shaders.iter().all(|s| s.glyph_mask.is_some()));
        assert!(
            result.shaders.iter().any(|s| s.data[0] == [0., 1., 0., 1.]),
            "nested override"
        );
        assert!(
            result
                .shaders
                .iter()
                .any(|s| s.data[0] == [0.125, 0.75, 0.25, 1.]),
            "deferred scope must retain its brush"
        );
        assert!(result.mono > 0, "ordinary sibling text must remain native");
        assert!(
            result
                .emoji
                .iter()
                .any(|s| s.bounds.origin.y.0 < 100. * scale),
            "emoji inside the brushed scope must remain native color sprites"
        );
        assert_eq!(result.custom, 0);
        drop(result);
        let first = renders.load(std::sync::atomic::Ordering::Relaxed);
        window.update(&mut cx, |_, _, cx| cx.notify()).unwrap();
        cx.update_window(window.into(), |_, window, cx| window.draw(cx).clear(cx))
            .unwrap();
        assert_eq!(
            renders.load(std::sync::atomic::Ordering::Relaxed),
            first,
            "unchanged brush snapshots should preserve cached child paint"
        );
        window
            .update(&mut cx, |view, _, cx| {
                view.brush = Brush::solid([1., 0.5, 0., 1.]).unwrap();
                cx.notify();
            })
            .unwrap();
        cx.update_window(window.into(), |_, window, cx| window.draw(cx).clear(cx))
            .unwrap();
        let _ = cx.capture_screenshot(window.into()).unwrap();
        assert!(
            renders.load(std::sync::atomic::Ordering::Relaxed) > first,
            "changing inherited brush must invalidate cached children"
        );
        let result = captured.lock().unwrap();
        assert!(
            result
                .shaders
                .iter()
                .any(|s| s.data[0] == [1., 0.5, 0., 1.])
        );
        assert!(
            !result
                .shaders
                .iter()
                .any(|s| s.data[0] == [0.6, 0.1, 0.3, 1.]),
            "cached glyphs must not keep the old material"
        );
    }
}