pub fn f16_to_f32(bits: u16) -> f32 {
let sign = if bits & 0x8000 != 0 { -1.0f32 } else { 1.0 };
let exponent = ((bits >> 10) & 0x1f) as i32;
let mantissa = (bits & 0x3ff) as u32;
match exponent {
0 => sign * mantissa as f32 * 5.960_464_5e-8,
0x1f => {
if mantissa == 0 {
sign * f32::INFINITY
} else {
f32::NAN
}
}
_ => {
let s = u32::from(bits & 0x8000 != 0) << 31;
f32::from_bits(s | (((exponent + 127 - 15) as u32) << 23) | (mantissa << 13))
}
}
}
pub fn f32_to_f16(value: f32) -> u16 {
let bits = value.to_bits();
let sign = ((bits >> 16) & 0x8000) as u16;
let exp = ((bits >> 23) & 0xff) as i32 - 127 + 15;
let mantissa = bits & 0x7f_ffff;
if exp >= 0x1f {
let payload = if ((bits >> 23) & 0xff) == 0xff && mantissa != 0 {
0x200
} else {
0
};
return sign | 0x7c00 | payload;
}
if exp <= 0 {
if exp < -10 {
return sign;
}
let m = mantissa | 0x80_0000;
let shift = (14 - exp) as u32;
let half = (m >> shift) as u16;
let round = (m >> (shift - 1)) & 1;
return sign | (half + round as u16);
}
let half = ((exp as u32) << 10) as u16 | (mantissa >> 13) as u16;
let round = (mantissa >> 12) & 1;
let sticky = (mantissa & 0xfff != 0) as u32;
let up = round & (sticky | (half as u32 & 1));
sign | (half + up as u16)
}
pub fn padded_bytes_per_row(width: u32, format: wgpu::TextureFormat) -> u32 {
let unpadded = width * bytes_per_texel(format);
let align = wgpu::COPY_BYTES_PER_ROW_ALIGNMENT;
unpadded.div_ceil(align) * align
}
pub fn bytes_per_texel(format: wgpu::TextureFormat) -> u32 {
format
.block_copy_size(None)
.unwrap_or_else(|| panic!("{format:?} has no fixed texel size"))
}
pub fn copy_texture_to_buffer(
encoder: &mut wgpu::CommandEncoder,
texture: &wgpu::Texture,
buffer: &wgpu::Buffer,
) {
let (width, height) = (texture.width(), texture.height());
encoder.copy_texture_to_buffer(
wgpu::TexelCopyTextureInfo {
texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
wgpu::TexelCopyBufferInfo {
buffer,
layout: wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(padded_bytes_per_row(width, texture.format())),
rows_per_image: Some(height),
},
},
wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
);
}
pub fn staging_buffer(device: &wgpu::Device, texture: &wgpu::Texture) -> wgpu::Buffer {
let size =
padded_bytes_per_row(texture.width(), texture.format()) as u64 * texture.height() as u64;
device.create_buffer(&wgpu::BufferDescriptor {
label: Some("threers readback"),
size,
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
mapped_at_creation: false,
})
}
pub fn unpad(padded: &[u8], width: u32, height: u32, format: wgpu::TextureFormat) -> Vec<u8> {
let row = (width * bytes_per_texel(format)) as usize;
let stride = padded_bytes_per_row(width, format) as usize;
let mut out = Vec::with_capacity(row * height as usize);
for y in 0..height as usize {
out.extend_from_slice(&padded[y * stride..y * stride + row]);
}
out
}
#[cfg(not(target_arch = "wasm32"))]
pub fn texture_bytes(
device: &wgpu::Device,
queue: &wgpu::Queue,
texture: &wgpu::Texture,
) -> Vec<u8> {
let buffer = staging_buffer(device, texture);
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("threers readback copy"),
});
copy_texture_to_buffer(&mut encoder, texture, &buffer);
queue.submit([encoder.finish()]);
let slice = buffer.slice(..);
slice.map_async(wgpu::MapMode::Read, |_| {});
let _ = device.poll(wgpu::PollType::wait_indefinitely());
let mapped = slice
.get_mapped_range()
.expect("buffer range is mapped after a blocking poll");
let out = unpad(&mapped, texture.width(), texture.height(), texture.format());
drop(mapped);
buffer.unmap();
out
}
pub fn as_f32(bytes: &[u8]) -> Vec<f32> {
bytes
.chunks_exact(2)
.map(|c| f16_to_f32(u16::from_le_bytes([c[0], c[1]])))
.collect()
}
#[cfg(not(target_arch = "wasm32"))]
pub fn texture_channel_f32(
device: &wgpu::Device,
queue: &wgpu::Queue,
texture: &wgpu::Texture,
channel: usize,
) -> Vec<f32> {
let bytes = texture_bytes(device, queue, texture);
if texture.format() == wgpu::TextureFormat::Rg11b10Ufloat {
assert!(channel < 3, "channel {channel} of a 3-channel format");
return bytes
.chunks_exact(4)
.map(|t| {
let word = u32::from_le_bytes([t[0], t[1], t[2], t[3]]);
packed_float_to_f32(word, channel)
})
.collect();
}
let channels = (bytes_per_texel(texture.format()) / 2) as usize;
assert!(
channel < channels,
"channel {channel} of a {channels}-channel format"
);
bytes
.chunks_exact(channels * 2)
.map(|texel| {
let at = channel * 2;
f16_to_f32(u16::from_le_bytes([texel[at], texel[at + 1]]))
})
.collect()
}
pub fn packed_float_to_f32(word: u32, channel: usize) -> f32 {
let (bits, mantissa_bits) = match channel {
0 => (word & 0x7ff, 6),
1 => ((word >> 11) & 0x7ff, 6),
_ => ((word >> 22) & 0x3ff, 5),
};
let exponent = bits >> mantissa_bits;
let mantissa = bits & ((1 << mantissa_bits) - 1);
let half = (exponent << 10) | (mantissa << (10 - mantissa_bits));
f16_to_f32(half as u16)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn packed_floats_decode_exactly() {
let one = 15u32 << 6 | (15u32 << 6) << 11 | (15u32 << 5) << 22;
for c in 0..3 {
assert_eq!(packed_float_to_f32(one, c), 1.0, "channel {c}");
}
assert_eq!(packed_float_to_f32(0, 0), 0.0);
let step = (15u32 << 6 | 1) | ((15u32 << 5 | 1) << 22);
assert_eq!(packed_float_to_f32(step, 0), 1.0 + 1.0 / 64.0);
assert_eq!(packed_float_to_f32(step, 2), 1.0 + 1.0 / 32.0);
assert_eq!(packed_float_to_f32(step, 1), 0.0);
}
#[test]
fn half_floats_decode_exactly() {
let cases: &[(u16, f32)] = &[
(0x0000, 0.0),
(0x8000, -0.0),
(0x3c00, 1.0),
(0xbc00, -1.0),
(0x3555, 0.333_251_95),
(0x7bff, 65504.0), (0x0400, 6.103_515_6e-5), (0x0001, 5.960_464_5e-8), (0x03ff, 6.097_555e-5), ];
for &(bits, expected) in cases {
let got = f16_to_f32(bits);
assert!(
(got - expected).abs() <= expected.abs() * 1e-6,
"{bits:#06x}: {got} != {expected}"
);
}
assert!(f16_to_f32(0x7c00).is_infinite());
assert!(f16_to_f32(0xfc00).is_infinite() && f16_to_f32(0xfc00) < 0.0);
assert!(f16_to_f32(0x7e00).is_nan());
}
#[test]
fn every_half_float_round_trips() {
for bits in 0..=u16::MAX {
let v = f16_to_f32(bits);
if v.is_nan() {
assert!(nan_half(f32_to_f16(v)), "{bits:#06x} lost its NaN");
continue;
}
let back = f32_to_f16(v);
assert_eq!(back, bits, "{bits:#06x} -> {v} -> {back:#06x}");
}
}
fn nan_half(bits: u16) -> bool {
(bits & 0x7c00) == 0x7c00 && (bits & 0x3ff) != 0
}
#[test]
fn rows_are_padded_to_the_alignment_and_unpadded_back() {
let fmt = wgpu::TextureFormat::Rgba8Unorm;
assert_eq!(padded_bytes_per_row(100, fmt), 512);
assert_eq!(padded_bytes_per_row(64, fmt), 256);
let (w, h) = (100u32, 3u32);
let stride = padded_bytes_per_row(w, fmt) as usize;
let mut padded = vec![0u8; stride * h as usize];
for y in 0..h as usize {
for x in 0..(w * 4) as usize {
padded[y * stride + x] = (y * 7 + x) as u8;
}
}
let tight = unpad(&padded, w, h, fmt);
assert_eq!(tight.len(), (w * 4 * h) as usize);
for y in 0..h as usize {
for x in 0..(w * 4) as usize {
assert_eq!(tight[y * (w * 4) as usize + x], (y * 7 + x) as u8);
}
}
}
#[test]
fn texel_sizes_cover_what_gets_read_back() {
use wgpu::TextureFormat as F;
assert_eq!(bytes_per_texel(F::R8Unorm), 1);
assert_eq!(bytes_per_texel(F::Rgba8Unorm), 4);
assert_eq!(bytes_per_texel(F::Rgba8UnormSrgb), 4);
assert_eq!(bytes_per_texel(F::R16Float), 2);
assert_eq!(bytes_per_texel(F::Rg16Float), 4);
assert_eq!(bytes_per_texel(F::Rgba16Float), 8);
assert_eq!(bytes_per_texel(F::Rgba32Float), 16);
}
}
pub struct AsyncReadback {
buffer: wgpu::Buffer,
ready: std::sync::Arc<std::sync::atomic::AtomicBool>,
}
impl AsyncReadback {
pub fn new(device: &wgpu::Device, size: u64, label: &str) -> Self {
let buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some(label),
size,
usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
Self { buffer, ready: Default::default() }
}
pub fn buffer(&self) -> &wgpu::Buffer {
&self.buffer
}
pub fn map(&self, wake: std::sync::Arc<dyn Fn() + Send + Sync>) {
let ready = self.ready.clone();
self.buffer.slice(..).map_async(wgpu::MapMode::Read, move |r| {
if r.is_ok() {
ready.store(true, std::sync::atomic::Ordering::Release);
}
wake();
});
}
pub fn is_ready(&self) -> bool {
self.ready.load(std::sync::atomic::Ordering::Acquire)
}
pub fn read(&self) -> Option<Vec<u8>> {
if !self.ready.swap(false, std::sync::atomic::Ordering::AcqRel) {
return None;
}
let bytes = self.buffer.slice(..).get_mapped_range().ok()?.to_vec();
self.buffer.unmap();
Some(bytes)
}
}