#![cfg(feature = "gpu")]
use emblema::vulkan::gpu::{
ColorAttachment, PassTarget, PixelFormat as GpuFormat, StorageMode, Texture, TextureDescriptor,
TextureType,
};
use emblema::vulkan::VulkanContext;
use emblema::{
BackendPreference, Canvas, Color, Context, Extent2D, Image, Paint, PixelFormat, Rect, Sampling,
};
const SIDE: u32 = 16;
const QUADRANTS: [[f32; 4]; 4] = [
[1.0, 0.0, 0.0, 1.0],
[0.0, 1.0, 0.0, 1.0],
[0.0, 0.0, 1.0, 1.0],
[1.0, 1.0, 0.0, 1.0],
];
fn context() -> Option<Context> {
match Context::new(BackendPreference::Vulkan) {
Ok(ctx) => Some(ctx),
Err(e) => {
eprintln!("skipping: no Vulkan context ({e})");
None
}
}
}
fn vulkan(ctx: &mut Context) -> &mut VulkanContext {
#[allow(unreachable_patterns, clippy::infallible_destructuring_match)]
match ctx {
Context::Vulkan(vulkan) => vulkan,
_ => unreachable!("a Vulkan context was asked for"),
}
}
fn half(value: f32) -> [u8; 2] {
if value == 0.0 { 0u16 } else { 0x3c00 }.to_le_bytes()
}
fn texel(format: GpuFormat, color: [f32; 4]) -> Vec<u8> {
let byte = |v: f32| (v * 255.0) as u8;
let [r, g, b, a] = color;
match format {
GpuFormat::R8G8B8A8Unorm | GpuFormat::R8G8B8A8UnormSrgb => {
vec![byte(r), byte(g), byte(b), byte(a)]
}
GpuFormat::B8G8R8A8Unorm | GpuFormat::B8G8R8A8UnormSrgb => {
vec![byte(b), byte(g), byte(r), byte(a)]
}
GpuFormat::R16G16B16A16Float => color.iter().flat_map(|v| half(*v)).collect(),
GpuFormat::R32G32B32A32Float => color.iter().flat_map(|v| v.to_le_bytes()).collect(),
GpuFormat::R32Float => r.to_le_bytes().to_vec(),
other => panic!("{other:?} is not a format this test writes"),
}
}
fn quadrants(format: GpuFormat) -> Vec<u8> {
let mut bytes = Vec::new();
for y in 0..SIDE {
for x in 0..SIDE {
let quadrant = usize::from(x >= SIDE / 2) + 2 * usize::from(y >= SIDE / 2);
bytes.extend(texel(format, QUADRANTS[quadrant]));
}
}
bytes
}
fn sampled(format: GpuFormat) -> TextureDescriptor {
TextureDescriptor::sampled(format, SIDE, SIDE)
}
fn drawn(ctx: &mut Context, image: &Image, side: u32) -> Vec<u8> {
drawn_at(ctx, image, side, Sampling::Nearest)
}
fn drawn_at(ctx: &mut Context, image: &Image, side: u32, sampling: Sampling) -> Vec<u8> {
let size = Extent2D::new(side, side);
let whole = Rect::from_size(side as f32, side as f32);
let mut canvas = Canvas::new(size);
canvas.clear(Color::linear(0.0, 0.0, 0.0, 0.0));
canvas
.draw_rect(
whole,
&Paint::image(0, whole)
.with_sampling(sampling)
.with_anti_alias(false),
)
.expect("rect");
let mut surface = ctx
.create_surface(size, PixelFormat::Rgba8Unorm)
.expect("surface");
ctx.draw_with_images(&mut surface, &canvas.finish(), &[image])
.expect("draw the image");
let pixels = ctx.read(&mut surface).expect("read");
ctx.destroy_surface(surface);
pixels
}
fn at(pixels: &[u8], side: u32, x: u32, y: u32) -> [u8; 4] {
let i = ((y * side + x) * 4) as usize;
[pixels[i], pixels[i + 1], pixels[i + 2], pixels[i + 3]]
}
fn bytes_of(color: [f32; 4]) -> [u8; 4] {
color.map(|v| (v * 255.0) as u8)
}
fn image_of(ctx: &mut Context, texture: &Texture) -> Image {
Image::Vulkan(
vulkan(ctx)
.gpu_texture_image(texture)
.expect("an image of the texture"),
)
}
#[test]
fn a_texture_written_from_bytes_is_drawn_as_what_it_holds() {
let Some(mut ctx) = context() else {
return;
};
let mut drew = 0;
for format in [
GpuFormat::R8G8B8A8Unorm,
GpuFormat::R8G8B8A8UnormSrgb,
GpuFormat::B8G8R8A8Unorm,
GpuFormat::B8G8R8A8UnormSrgb,
GpuFormat::R16G16B16A16Float,
GpuFormat::R32G32B32A32Float,
GpuFormat::R32Float,
] {
if !vulkan(&mut ctx).gpu_format_support(format).sampled {
eprintln!("skipping {format:?}: this device does not sample it");
continue;
}
let texture = vulkan(&mut ctx)
.gpu_create_texture(&sampled(format))
.expect("a texture");
vulkan(&mut ctx)
.gpu_write_texture(&texture, 0, 0, &quadrants(format))
.expect("written");
let image = image_of(&mut ctx, &texture);
let pixels = drawn(&mut ctx, &image, SIDE);
for (quadrant, (x, y)) in [(4, 4), (12, 4), (4, 12), (12, 12)].into_iter().enumerate() {
let mut want = bytes_of(QUADRANTS[quadrant]);
if format == GpuFormat::R32Float {
want = [want[0], 0, 0, 255];
}
assert_eq!(at(&pixels, SIDE, x, y), want, "{format:?} at ({x}, {y})");
}
ctx.destroy_image(image);
vulkan(&mut ctx).gpu_destroy_texture(texture);
drew += 1;
}
assert!(drew > 0, "no format was drawn");
}
#[test]
fn an_srgb_texture_is_drawn_through_its_curve() {
let Some(mut ctx) = context() else {
return;
};
let mut gray = |format: GpuFormat| {
let texture = vulkan(&mut ctx)
.gpu_create_texture(&sampled(format))
.expect("a texture");
vulkan(&mut ctx)
.gpu_write_texture(
&texture,
0,
0,
&[128, 128, 128, 255].repeat((SIDE * SIDE) as usize),
)
.expect("written");
let image = image_of(&mut ctx, &texture);
let pixel = at(&drawn(&mut ctx, &image, SIDE), SIDE, 8, 8);
ctx.destroy_image(image);
vulkan(&mut ctx).gpu_destroy_texture(texture);
pixel
};
for (plain, curved) in [
(GpuFormat::R8G8B8A8Unorm, GpuFormat::R8G8B8A8UnormSrgb),
(GpuFormat::B8G8R8A8Unorm, GpuFormat::B8G8R8A8UnormSrgb),
] {
assert_eq!(gray(plain), [128, 128, 128, 255], "{plain:?}");
assert_eq!(gray(curved), [55, 55, 55, 255], "{curved:?}");
}
}
#[test]
fn an_image_of_a_texture_is_not_written_or_read_by_the_renderer() {
let Some(mut ctx) = context() else {
return;
};
let format = GpuFormat::R8G8B8A8Unorm;
let texture = vulkan(&mut ctx)
.gpu_create_texture(&sampled(format))
.expect("a texture");
vulkan(&mut ctx)
.gpu_write_texture(&texture, 0, 0, &quadrants(format))
.expect("written");
let mut image = image_of(&mut ctx, &texture);
assert!(ctx.write_image(&mut image, &quadrants(format)).is_err());
assert!(ctx.read_image(&mut image).is_err());
let pixels = drawn(&mut ctx, &image, SIDE);
assert_eq!(at(&pixels, SIDE, 12, 12), bytes_of(QUADRANTS[3]));
ctx.destroy_image(image);
vulkan(&mut ctx).gpu_destroy_texture(texture);
}
#[test]
fn a_texture_a_pass_drew_is_drawn_as_the_pass_left_it() {
let Some(mut ctx) = context() else {
return;
};
let target = vulkan(&mut ctx)
.gpu_create_texture(&{
let mut d = sampled(GpuFormat::R8G8B8A8Unorm);
d.render_target = true;
d
})
.expect("a target");
let cleared = |ctx: &mut Context, color: [f32; 4]| {
let vulkan = vulkan(ctx);
let mut encoder = vulkan.gpu_begin().expect("an encoder");
vulkan
.gpu_begin_pass(
&mut encoder,
&PassTarget {
colors: vec![ColorAttachment::cleared(&target, color)],
depth_stencil: None,
},
)
.expect("a pass");
vulkan.gpu_end_pass(&mut encoder).expect("ended");
let submission = vulkan.gpu_submit_deferred(encoder).expect("submitted");
drop(submission);
};
assert!(
vulkan(&mut ctx).gpu_texture_image(&target).is_err(),
"nothing has drawn it"
);
{
let vulkan = vulkan(&mut ctx);
let mut encoder = vulkan.gpu_begin().expect("an encoder");
vulkan
.gpu_begin_pass(
&mut encoder,
&PassTarget {
colors: vec![ColorAttachment::cleared(&target, QUADRANTS[0])],
depth_stencil: None,
},
)
.expect("a pass");
vulkan.gpu_end_pass(&mut encoder).expect("ended");
assert!(
vulkan.gpu_texture_image(&target).is_err(),
"a recording not submitted has drawn nothing"
);
vulkan.gpu_discard(encoder);
assert!(
vulkan.gpu_texture_image(&target).is_err(),
"a recording given up has drawn nothing"
);
}
for color in [QUADRANTS[1], QUADRANTS[2]] {
cleared(&mut ctx, color);
let image = image_of(&mut ctx, &target);
let pixels = drawn(&mut ctx, &image, SIDE);
assert_eq!(at(&pixels, SIDE, 8, 8), bytes_of(color));
ctx.destroy_image(image);
}
let image = image_of(&mut ctx, &target);
cleared(&mut ctx, QUADRANTS[0]);
let pixels = drawn(&mut ctx, &image, SIDE);
assert_eq!(at(&pixels, SIDE, 8, 8), bytes_of(QUADRANTS[0]));
ctx.destroy_image(image);
cleared(&mut ctx, QUADRANTS[3]);
let held = vulkan(&mut ctx)
.gpu_read_texture(&target, 0, 0)
.expect("read back");
assert_eq!(&held[..4], bytes_of(QUADRANTS[3]));
assert!(vulkan(&mut ctx)
.gpu_settle(std::time::Duration::from_secs(10))
.expect("settled"));
vulkan(&mut ctx).gpu_destroy_texture(target);
}
#[test]
fn a_texture_is_drawn_from_the_levels_that_were_written() {
let Some(mut ctx) = context() else {
return;
};
let format = GpuFormat::R8G8B8A8Unorm;
let texture = vulkan(&mut ctx)
.gpu_create_texture(&{
let mut d = sampled(format);
d.mip_levels = 2;
d
})
.expect("a texture of two levels");
let solid = |color: [f32; 4], side: u32| texel(format, color).repeat((side * side) as usize);
vulkan(&mut ctx)
.gpu_write_texture(&texture, 0, 0, &solid(QUADRANTS[0], SIDE))
.expect("the first level");
let image = image_of(&mut ctx, &texture);
let pixels = drawn_at(&mut ctx, &image, SIDE / 4, Sampling::Mipmap);
assert_eq!(at(&pixels, SIDE / 4, 2, 2), bytes_of(QUADRANTS[0]));
ctx.destroy_image(image);
vulkan(&mut ctx)
.gpu_write_texture(&texture, 1, 0, &solid(QUADRANTS[2], SIDE / 2))
.expect("the second level");
let image = image_of(&mut ctx, &texture);
let pixels = drawn_at(&mut ctx, &image, SIDE / 4, Sampling::Mipmap);
assert_eq!(at(&pixels, SIDE / 4, 2, 2), bytes_of(QUADRANTS[2]));
ctx.destroy_image(image);
vulkan(&mut ctx).gpu_destroy_texture(texture);
}
#[test]
fn an_image_of_a_zeroed_texture_has_every_level() {
let Some(mut ctx) = context() else {
return;
};
let format = GpuFormat::R8G8B8A8Unorm;
let solid = |color: [f32; 4], side: u32| texel(format, color).repeat((side * side) as usize);
for zeroed in [false, true] {
let texture = vulkan(&mut ctx)
.gpu_create_texture(&{
let mut d = sampled(format);
d.mip_levels = 2;
d.zeroed = zeroed;
d
})
.expect("a texture of two levels");
if zeroed {
let image = image_of(&mut ctx, &texture);
let pixels = drawn(&mut ctx, &image, SIDE);
assert_eq!(at(&pixels, SIDE, 2, 2), [0; 4], "zeroed, unwritten");
ctx.destroy_image(image);
}
vulkan(&mut ctx)
.gpu_write_texture(&texture, 0, 0, &solid(QUADRANTS[0], SIDE))
.expect("the first level");
let image = image_of(&mut ctx, &texture);
let pixels = drawn_at(&mut ctx, &image, SIDE / 4, Sampling::Mipmap);
let want = if zeroed {
[0; 4]
} else {
bytes_of(QUADRANTS[0])
};
assert_eq!(at(&pixels, SIDE / 4, 2, 2), want, "zeroed: {zeroed}");
ctx.destroy_image(image);
vulkan(&mut ctx).gpu_destroy_texture(texture);
}
}
#[test]
fn a_texture_the_renderer_cannot_sample_is_refused() {
let Some(mut ctx) = context() else {
return;
};
let vulkan = vulkan(&mut ctx);
let rgba = GpuFormat::R8G8B8A8Unorm;
let mut refused = |what: &str, desc: TextureDescriptor, written: bool| {
let Ok(texture) = vulkan.gpu_create_texture(&desc) else {
eprintln!("skipping {what}: this device makes no such texture");
return;
};
if written {
let bytes = desc
.format
.level_size(desc.width, desc.height)
.expect("a format that is written") as usize;
vulkan
.gpu_write_texture(&texture, 0, 0, &vec![0; bytes])
.expect("written");
}
assert!(
vulkan.gpu_texture_image(&texture).is_err(),
"{what} was given an image"
);
vulkan.gpu_destroy_texture(texture);
};
refused("a texture nothing has written", sampled(rgba), false);
refused(
"a texture no shader reads",
{
let mut d = sampled(rgba);
d.render_target = true;
d.shader_read = false;
d
},
false,
);
refused(
"a texture of four samples",
{
let mut d = sampled(rgba);
d.storage = StorageMode::DevicePrivate;
d.sample_count = 4;
d.ty = TextureType::Texture2DMultisample;
d.render_target = true;
d
},
false,
);
refused(
"a cube",
{
let mut d = sampled(rgba);
d.ty = TextureType::Cube;
d
},
true,
);
refused("two channels", sampled(GpuFormat::R8G8Unorm), true);
refused(
"one channel of eight bits",
sampled(GpuFormat::R8Unorm),
true,
);
}