xgpui-shader 0.5.0

Shader and paint extension for xgpui with gradients, brushes, and portable WGSL shaders
Documentation
use super::*;
use crate::{Gradient, GradientStop, MeshGradient, MeshGrid, SolidColor};
use gpui::{CustomContent, size};
use std::sync::Arc;

fn context() -> MetalCustomContext {
    let device = MTLCreateSystemDefaultDevice().expect("Metal device required");
    MetalCustomContext::new(
        device.clone(),
        device
            .newCommandQueue()
            .expect("Metal queue allocation failed"),
    )
}
fn extent(w: i32, h: i32) -> Size<DevicePixels> {
    size(DevicePixels(w), DevicePixels(h))
}
fn generate(
    ctx: &MetalCustomContext,
    extent: Size<DevicePixels>,
) -> Result<(Texture, CommandBuffer)> {
    let desc = TextureDescriptor::new();
    desc.setPixelFormat(MTLPixelFormat::BGRA8Unorm);
    unsafe { desc.setWidth(extent.width.0 as usize) };
    unsafe { desc.setHeight(extent.height.0 as usize) };
    desc.setStorageMode(MTLStorageMode::Shared);
    desc.setUsage(MTLTextureUsage::RenderTarget | MTLTextureUsage::ShaderRead);
    let texture = ctx
        .device
        .newTextureWithDescriptor(&desc)
        .expect("Metal texture allocation failed");
    let command = ctx.record(|_| Ok(())).unwrap();
    let pass = RenderPassDescriptor::new();
    let attachment = unsafe { pass.colorAttachments().objectAtIndexedSubscript(0) };
    attachment.setTexture(Some(&texture));
    attachment.setLoadAction(MTLLoadAction::Clear);
    attachment.setStoreAction(MTLStoreAction::Store);
    attachment.setClearColor(MTLClearColor {
        red: 1.,
        green: 0.,
        blue: 0.,
        alpha: 1.,
    });
    command
        .renderCommandEncoderWithDescriptor(&pass)
        .expect("Metal render encoder allocation failed")
        .endEncoding();
    Ok((texture, command))
}
fn prepare(
    cache: &OutputCache,
    ctx: &MetalCustomContext,
    size: Size<DevicePixels>,
) -> (MetalCustomOutput, bool) {
    cache.prepare(ctx, size, || generate(ctx, size)).unwrap()
}
fn submit(output: &MetalCustomOutput) {
    for command in &output.commands {
        if command.status() == MTLCommandBufferStatus::NotEnqueued {
            command.commit();
        }
        command.waitUntilCompleted();
        assert_eq!(command.status(), MTLCommandBufferStatus::Completed);
    }
}
fn red_pixel(texture: &TextureRef) {
    let mut pixel = [0u8; 4];
    unsafe {
        texture.getBytes_bytesPerRow_fromRegion_mipmapLevel(
            std::ptr::NonNull::new(pixel.as_mut_ptr().cast()).unwrap(),
            4,
            MTLRegion {
                origin: MTLOrigin { x: 0, y: 0, z: 0 },
                size: MTLSize {
                    width: 1,
                    height: 1,
                    depth: 1,
                },
            },
            0,
        )
    };
    assert_eq!(pixel, [0, 0, 255, 255]);
}

#[test]
fn pending_command_is_shared_until_host_submission() {
    let ctx = context();
    let cache = OutputCache::default();
    let (first, generated) = prepare(&cache, &ctx, extent(4, 4));
    assert!(generated);
    let (pending, generated) = cache
        .prepare(&ctx, extent(4, 4), || panic!("cache miss"))
        .unwrap();
    assert!(!generated);
    assert_eq!(pending.commands.len(), 1);
    assert_eq!(
        pending.commands[0].status(),
        MTLCommandBufferStatus::NotEnqueued
    );
    assert!(std::ptr::eq::<CommandBufferRef>(
        &*first.commands[0],
        &*pending.commands[0]
    ));
    assert!(std::ptr::eq::<TextureRef>(
        &*first.texture,
        &*pending.texture
    ));
    // Host deduplicates shared pending commands and submits once.
    submit(&first);
    let (ready, generated) = prepare(&cache, &ctx, extent(4, 4));
    assert!(!generated);
    assert!(ready.commands.is_empty());
    red_pixel(&ready.texture);
}

#[test]
fn eviction_removes_only_lru_entry_and_retained_pending_output_survives() {
    let ctx = context();
    let cache = OutputCache {
        budget: 128,
        ..Default::default()
    };
    let (_, _) = prepare(&cache, &ctx, extent(4, 4));
    let (evicted, _) = prepare(&cache, &ctx, extent(8, 2));
    assert!(!prepare(&cache, &ctx, extent(4, 4)).1); // A is most recent.
    assert!(prepare(&cache, &ctx, extent(2, 8)).1); // Evict only B.
    assert_eq!(cache.texture_info().unwrap().1, 128);
    assert_eq!(cache.state.lock().unwrap().entries.len(), 2);
    assert!(!prepare(&cache, &ctx, extent(4, 4)).1); // A survives, not clear-all.
    assert!(prepare(&cache, &ctx, extent(8, 2)).1); // B must regenerate.
    // Evicting cache ownership cannot cancel a previously returned command/texture.
    submit(&evicted);
    red_pixel(&evicted.texture);
}

#[test]
fn cache_miss_failure_preserves_entries_and_oversized_output_bypasses_cache() {
    let ctx = context();
    let cache = OutputCache {
        budget: 64,
        ..Default::default()
    };
    prepare(&cache, &ctx, extent(4, 4));
    assert!(
        cache
            .prepare(&ctx, extent(8, 2), || anyhow::bail!("encode failed"))
            .is_err()
    );
    assert!(!prepare(&cache, &ctx, extent(4, 4)).1);
    assert!(prepare(&cache, &ctx, extent(8, 8)).1);
    assert!(!prepare(&cache, &ctx, extent(4, 4)).1);
    assert_eq!(cache.texture_info().unwrap().1, 64);
    for size in [
        extent(0, 4),
        extent(-1, 4),
        extent(16385, 1),
        extent(8192, 8192),
    ] {
        assert!(
            cache
                .prepare(&ctx, size, || panic!("invalid allocation"))
                .is_err()
        );
    }
}

struct Probe {
    name: &'static str,
    producer: Arc<dyn CustomContent>,
    generations: Box<dyn Fn() -> usize>,
}
fn producers() -> Vec<Probe> {
    let solid = Arc::new(SolidColor::new([1., 0., 0., 1.]).unwrap());
    let gradient = Arc::new(
        Gradient::linear(
            45.,
            vec![
                GradientStop::new(0., [1., 0., 0., 1.]),
                GradientStop::new(1., [0., 0., 1., 1.]),
            ],
        )
        .unwrap(),
    );
    let mesh = Arc::new(
        MeshGradient::new(MeshGrid::regular(2, 2, vec![[1., 0., 0.]; 4]).unwrap(), 18).unwrap(),
    );
    vec![
        Probe {
            name: "solid",
            producer: solid.clone(),
            generations: Box::new(move || solid.preparations()),
        },
        Probe {
            name: "gradient",
            producer: gradient.clone(),
            generations: Box::new(move || gradient.preparations()),
        },
        Probe {
            name: "mesh",
            producer: mesh.clone(),
            generations: Box::new(move || mesh.preparations()),
        },
    ]
}
fn output(
    probe: &Probe,
    ctx: &MetalCustomContext,
    extent: Size<DevicePixels>,
) -> MetalCustomOutput {
    *probe
        .producer
        .prepare(ctx, extent)
        .unwrap()
        .downcast::<MetalCustomOutput>()
        .unwrap()
}

#[test]
fn all_producers_reuse_alternating_sizes_and_preserve_pending_commands() {
    let ctx = context();
    for probe in producers() {
        let a = output(&probe, &ctx, extent(320, 240));
        let b = output(&probe, &ctx, extent(80, 20));
        let pending = output(&probe, &ctx, extent(320, 240));
        assert_eq!((probe.generations)(), 2, "{}", probe.name);
        assert!(std::ptr::eq::<CommandBufferRef>(
            &*a.commands[0],
            &*pending.commands[0]
        ));
        submit(&a);
        submit(&b);
        for _ in 0..4 {
            assert!(output(&probe, &ctx, extent(320, 240)).commands.is_empty());
            assert!(output(&probe, &ctx, extent(80, 20)).commands.is_empty());
        }
        assert_eq!((probe.generations)(), 2, "{}", probe.name);
    }
}

#[test]
fn all_producers_separate_queues_on_the_same_device() {
    let a = context();
    let b = MetalCustomContext::new(
        a.device.clone(),
        a.device
            .newCommandQueue()
            .expect("Metal queue allocation failed"),
    );
    assert_ne!(a.cache_key(), b.cache_key());
    for probe in producers() {
        let first = output(&probe, &a, extent(16, 16));
        let second = output(&probe, &b, extent(16, 16));
        assert!(!std::ptr::eq::<TextureRef>(
            &*first.texture,
            &*second.texture
        ));
        assert!(!std::ptr::eq::<CommandBufferRef>(
            &*first.commands[0],
            &*second.commands[0]
        ));
        let again = output(&probe, &a, extent(16, 16));
        assert!(std::ptr::eq::<CommandBufferRef>(
            &*first.commands[0],
            &*again.commands[0]
        ));
        submit(&first);
        // Completing queue A cannot mark queue B's output submitted.
        assert_eq!(output(&probe, &b, extent(16, 16)).commands.len(), 1);
        assert!(output(&probe, &a, extent(16, 16)).commands.is_empty());
        submit(&second);
        assert!(output(&probe, &b, extent(16, 16)).commands.is_empty());
        assert_eq!((probe.generations)(), 2, "{}", probe.name);
    }
}