use mln_builtins::conformance::{
Check, assert_checks_pass, custom_layers, foreign_ids, imported_textures, mipmapped_targets, mipmapped_targets_of,
mipmapped_writes, wall_quads,
};
use mln_gfx::Size;
use mln_gfx::gpu::{Gpu, TextureDesc, TextureFormat, TextureRegion, TextureUsage};
use crate::{CustomLayerEncoder, CustomLayerPass, Device, WebGpu};
const CHECKS: [(&str, Check<WebGpu>); 2] = [
("wall_quads", wall_quads),
("mipmapped_targets of a format that shaders can't write, whose levels a pass draws", |gpu| {
mipmapped_targets_of(gpu, TextureFormat::Bgra8Unorm)
}),
];
#[test]
fn the_backend_passes_the_conformance_checks() {
let Some(device) = super::device() else {
return;
};
assert_checks_pass(&CHECKS, || Some(WebGpu::new(&device)));
}
#[test]
fn a_backend_rejects_another_backends_ids() {
let Some(device) = super::device() else {
return;
};
foreign_ids(&mut WebGpu::new(&device), &mut WebGpu::new(&device)).expect("the valid calls");
}
#[test]
fn a_device_without_storage_textures_draws_each_mip_level() {
let Some(host) = super::device() else {
return;
};
let adapter = host.adapter().expect("the library's adapter");
let limits = wgpu::Limits { max_storage_textures_per_shader_stage: 0, ..host.device().limits() };
let (device, queue) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor {
label: Some("host device"),
required_limits: limits,
..Default::default()
}))
.expect("device");
let device = Device::from_wgpu(host.instance().cloned(), device, queue, None);
mipmapped_targets(&mut WebGpu::new(&device)).expect("the frames");
mipmapped_writes(&mut WebGpu::new(&device)).expect("the written texture's frames");
}
#[test]
fn a_readback_copies_into_the_last_ones_buffer() {
let Some(device) = super::device() else {
return;
};
let mut gpu = WebGpu::new(&device);
let size = Size { width: 4, height: 2 };
let texture = gpu
.create_texture(&TextureDesc {
label: "read back",
size,
format: TextureFormat::Rgba8Unorm,
usage: TextureUsage { sampled: true, copy_src: true, ..TextureUsage::default() },
mipmapped: false,
})
.expect("a texture");
let whole = TextureRegion::whole(size);
gpu.write_texture(texture, whole, &[1; 4 * 4 * 2]).expect("the first texels");
let first = gpu.read_texture(texture, whole).expect("a readback");
let buffer = gpu.readback_buffer(first).cloned();
assert_eq!(gpu.take_readback(first, true).expect("the texels"), Some(vec![1; 4 * 4 * 2]));
gpu.write_texture(texture, whole, &[2; 4 * 4 * 2]).expect("the second texels");
let second = gpu.read_texture(texture, whole).expect("a readback");
assert!(buffer.is_some() && gpu.readback_buffer(second) == buffer.as_ref(), "the first readback's buffer");
assert_eq!(gpu.take_readback(second, true).expect("the texels"), Some(vec![2; 4 * 4 * 2]));
}
#[test]
fn custom_layers_get_the_backends_encoder_and_pass() {
let Some(device) = super::device() else {
return;
};
custom_layers(
&mut WebGpu::new(&device),
|lent| assert!(lent.is::<CustomLayerEncoder>()),
|lent| {
let lent = lent.downcast_mut::<CustomLayerPass>().expect("a WebGPU pass");
assert_eq!(lent.color_format, wgpu::TextureFormat::Rgba8Unorm);
},
)
.expect("the frames");
}
#[test]
fn imported_textures_draw_like_the_backends_own() {
let Some(device) = super::device() else {
return;
};
imported_textures(&mut WebGpu::new(&device), |gpu, texture| {
let native = gpu.native_texture(texture).expect("a texture of the backend's").clone();
Ok(gpu.import_texture(&native))
})
.expect("the frames");
}