1
use easygpu::wgpu;
2
use image::{GenericImageView, Rgba};
3
use tracing::Level;
4
use winit::window::Theme;
5

            
6
use crate::frame_renderer::FrameRenderer;
7
use crate::prelude::*;
8
use crate::sprite::Srgb;
9

            
10
1
#[tokio::test]
11
#[allow(clippy::semicolon_if_nothing_returned)] // false positive from tokio::test
12
1
async fn offscreen_render_test() {
13
1
    tracing_subscriber::fmt()
14
1
        .with_max_level(Level::TRACE)
15
1
        .init();
16
1
    let (scene_sender, scene_receiver) = flume::unbounded();
17
1

            
18
1
    let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::default());
19
1
    let adapter = instance
20
1
        .request_adapter(&wgpu::RequestAdapterOptions {
21
1
            power_preference: wgpu::PowerPreference::default(),
22
1
            compatible_surface: None,
23
1
            force_fallback_adapter: false,
24
1
        })
25
        .await
26
1
        .expect("No wgpu adapter found");
27
1
    let renderer = easygpu::renderer::Renderer::offscreen(&adapter, 4)
28
        .await
29
1
        .expect("error creating renderer");
30
1

            
31
1
    let mut target = Target::from(Scene::new(scene_sender, Theme::Light));
32
1
    target.scene_mut().unwrap().set_size(Size::new(64, 64));
33
1
    target.scene_mut().unwrap().start_frame();
34
1

            
35
1
    Shape::<Scaled>::circle(Point::new(16., 16.), Figure::new(16.))
36
1
        .fill(Fill::new(Color::RED))
37
1
        .render(&target);
38
1

            
39
1
    Shape::<Scaled>::circle(Point::new(48., 16.), Figure::new(16.))
40
1
        .fill(Fill::new(Color::LIME))
41
1
        .render(&target);
42
1

            
43
1
    Shape::<Scaled>::circle(Point::new(16., 48.), Figure::new(16.))
44
1
        .fill(Fill::new(Color::BLUE))
45
1
        .render(&target);
46
1

            
47
1
    target.scene_mut().unwrap().end_frame();
48
1

            
49
1
    let image = FrameRenderer::<Srgb>::render_one_frame(renderer, scene_receiver)
50
1
        .expect("Error rendering offscreen");
51
1

            
52
1
    assert_eq!(image.width(), 64);
53
1
    assert_eq!(image.height(), 64);
54

            
55
1
    assert_eq!(image.get_pixel(16, 16), Rgba([255, 0, 0, 255]));
56
1
    assert_eq!(image.get_pixel(48, 16), Rgba([0, 255, 0, 255]));
57
1
    assert_eq!(image.get_pixel(16, 48), Rgba([0, 0, 255, 255]));
58
1
    assert_eq!(image.get_pixel(48, 48), Rgba([0, 0, 0, 0]));
59
}