use std::sync::mpsc;
use crate::canvas::gpu::Gpu;
pub(crate) const REQUIRE_GPU_ENV: &str = "SGL_REQUIRE_GPU";
fn unavailable<T>(reason: &str) -> Option<T> {
let required = std::env::var(REQUIRE_GPU_ENV).is_ok_and(|v| !v.is_empty() && v != "0");
assert!(
!required,
"GPU test cannot run but {REQUIRE_GPU_ENV} is set: {reason}"
);
eprintln!("skipping GPU test: {reason}");
None
}
pub(crate) fn gpu() -> Option<Gpu> {
match Gpu::headless() {
Ok(gpu) => Some(gpu),
Err(err) => unavailable(&err.to_string()),
}
}
pub(crate) fn device() -> Option<(wgpu::Device, wgpu::Queue)> {
gpu().map(|gpu| (gpu.device, gpu.queue))
}
pub(crate) fn read_texture(
device: &wgpu::Device,
queue: &wgpu::Queue,
texture: &wgpu::Texture,
width: u32,
height: u32,
bytes_per_pixel: u32,
) -> Vec<u8> {
let unpadded = bytes_per_pixel * width;
let align = wgpu::COPY_BYTES_PER_ROW_ALIGNMENT;
let padded = unpadded.div_ceil(align) * align;
let buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("test readback buffer"),
size: u64::from(padded) * u64::from(height),
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
mapped_at_creation: false,
});
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("test readback encoder"),
});
encoder.copy_texture_to_buffer(
wgpu::TexelCopyTextureInfo {
texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
wgpu::TexelCopyBufferInfo {
buffer: &buffer,
layout: wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(padded),
rows_per_image: Some(height),
},
},
wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
);
queue.submit(Some(encoder.finish()));
let (tx, rx) = mpsc::channel();
buffer.slice(..).map_async(wgpu::MapMode::Read, move |r| {
let _ = tx.send(r);
});
device
.poll(wgpu::PollType::wait_indefinitely())
.expect("poll for readback");
rx.recv()
.expect("map callback")
.expect("map readback buffer");
let data = buffer
.slice(..)
.get_mapped_range()
.expect("mapped readback buffer");
let mut out = Vec::with_capacity((unpadded * height) as usize);
for row in 0..height {
let start = (row * padded) as usize;
out.extend_from_slice(&data[start..start + unpadded as usize]);
}
out
}
pub(crate) fn f16_to_f32(bits: u16) -> f32 {
let sign = if bits & 0x8000 == 0 { 1.0 } else { -1.0 };
let exp = i32::from((bits >> 10) & 0x1f);
let mant = f32::from(bits & 0x3ff);
let magnitude = match exp {
0 => mant * 2f32.powi(-24),
0x1f if mant == 0.0 => f32::INFINITY,
0x1f => f32::NAN,
_ => (1.0 + mant / 1024.0) * 2f32.powi(exp - 15),
};
sign * magnitude
}
pub(crate) fn rgba16f_to_f32(bytes: &[u8]) -> Vec<f32> {
bytes
.chunks_exact(2)
.map(|b| f16_to_f32(u16::from_le_bytes([b[0], b[1]])))
.collect()
}
#[cfg(test)]
mod tests {
use super::f16_to_f32;
#[test]
fn f16_decodes_known_encodings() {
assert_eq!(f16_to_f32(0x3C00), 1.0);
assert_eq!(f16_to_f32(0x3800), 0.5);
assert_eq!(f16_to_f32(0xC000), -2.0);
assert_eq!(f16_to_f32(0x3A00), 0.75);
assert_eq!(f16_to_f32(0x0001), 2f32.powi(-24));
assert_eq!(f16_to_f32(0x0000), 0.0);
}
}