Skip to content

The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.

1.6 KB
Raw Blame
//! Host API without a page: shared device, offscreen window target, frame bookkeeping, readback.
use threert_webgpu as webgpu;

#[test]
fn host_frame_without_page() {
    let gpu = match webgpu::init(&webgpu::GpuOptions::default()) {
        Ok(g) => g,
        Err(e) => {
            eprintln!("skipping: {e}");
            return;
        }
    };
    assert!(std::ptr::eq(webgpu::device(), &gpu.device), "one shared device");
    assert!(gpu.js_features.contains(&"core-features-and-limits"));

    let off = webgpu::Offscreen::new(8, 4, wgpu::TextureFormat::Bgra8Unorm);
    // the host draws into the target itself (as the UI renderer would) …
    let view = off.texture().create_view(&Default::default());
    let mut enc = webgpu::device().create_command_encoder(&Default::default());
    enc.begin_render_pass(&wgpu::RenderPassDescriptor {
        label: None,
        color_attachments: &[Some(wgpu::RenderPassColorAttachment {
            view: &view,
            resolve_target: None,
            ops: wgpu::Operations { load: wgpu::LoadOp::Clear(wgpu::Color { r: 1.0, g: 0.0, b: 0.0, a: 1.0 }), store: wgpu::StoreOp::Store },
        })],
        ..Default::default()
    });
    webgpu::queue().submit([enc.finish()]);

    // … and a frame in which no page drew leaves it alone
    webgpu::set_window_target(off.texture(), 8.0, 4.0);
    let frame = webgpu::end_window_frame();
    assert_eq!(frame, webgpu::WindowFrame::default());
    let px = off.read_rgba(true).unwrap();
    assert_eq!(px.len(), 8 * 4 * 4);
    assert_eq!(&px[..4], &[255, 0, 0, 255], "BGRA read back as RGBA");
    assert_eq!(webgpu::window_canvas_size(), None, "no page, no window canvas");
}