use crate::gpu::{GpuInputBuffer, PlotReadbackError, ScalarType};
use futures::channel::oneshot;
use runmat_accelerate_api::allocation::catch_allocation_cleanup_unwind;
use runmat_accelerate_api::WgpuDeviceAccess;
#[cfg(all(test, not(target_arch = "wasm32")))]
#[path = "util/tests.rs"]
mod tests;
#[cfg(all(test, target_arch = "wasm32"))]
#[path = "util/browser_tests.rs"]
mod browser_tests;
struct Mapping<'a> {
access: &'a WgpuDeviceAccess,
buffer: &'a wgpu::Buffer,
registered: bool,
}
impl Mapping<'_> {
fn finish(&mut self) -> Result<(), PlotReadbackError> {
if !self.registered {
return Ok(());
}
self.registered = false;
catch_allocation_cleanup_unwind(|| {
self.access
.with_cleanup(|cleanup| cleanup.unmap_buffer(self.buffer))
})
.map_err(|_| PlotReadbackError::UnmapPanicked)?
.map_err(PlotReadbackError::CleanupAccess)
}
}
impl Drop for Mapping<'_> {
fn drop(&mut self) {
if let Err(error) = self.finish() {
log::error!("{error}");
}
}
}
pub(crate) async fn readback_bytes(
access: &WgpuDeviceAccess,
buffer: &wgpu::Buffer,
byte_len: usize,
) -> Result<Vec<u8>, String> {
readback_mapped(access, buffer, byte_len, |bytes| Ok(bytes.to_vec()))
.await
.map_err(|error| error.to_string())
}
async fn readback_mapped<T>(
access: &WgpuDeviceAccess,
buffer: &wgpu::Buffer,
byte_len: usize,
decode: impl FnOnce(&[u8]) -> Result<T, PlotReadbackError>,
) -> Result<T, PlotReadbackError> {
if byte_len == 0 {
return decode(&[]);
}
let byte_len_u64 = u64::try_from(byte_len).map_err(|_| PlotReadbackError::ByteSizeOverflow)?;
if byte_len_u64 > buffer.size() {
return Err(PlotReadbackError::BufferTooSmall {
requested: byte_len,
available: buffer.size(),
});
}
let slice = buffer.slice(0..byte_len_u64);
let (tx, rx) = oneshot::channel();
let mut mapping = Mapping {
access,
buffer,
registered: false,
};
let result = async {
access
.with_shared(|_| {
mapping.registered = true;
slice.map_async(wgpu::MapMode::Read, move |result| {
let _ = tx.send(result);
});
})
.map_err(PlotReadbackError::Access)?;
#[cfg(not(target_arch = "wasm32"))]
access.poll(wgpu::Maintain::Wait);
#[cfg(target_arch = "wasm32")]
access.poll(wgpu::Maintain::Poll);
rx.await
.map_err(|_| PlotReadbackError::MapCallbackDropped)?
.map_err(PlotReadbackError::Map)?;
access
.with_shared(|_| {
let data = slice.get_mapped_range();
if data.len() < byte_len {
return Err(PlotReadbackError::BufferTooSmall {
requested: byte_len,
available: data.len() as u64,
});
}
decode(&data[..byte_len])
})
.map_err(PlotReadbackError::Access)?
}
.await;
combine_cleanup(result, mapping.finish())
}
fn combine_cleanup<T>(
result: Result<T, PlotReadbackError>,
cleanup: Result<(), PlotReadbackError>,
) -> Result<T, PlotReadbackError> {
match (result, cleanup) {
(result, Ok(())) => result,
(Ok(_), Err(cleanup)) => Err(cleanup),
(Err(error), Err(cleanup)) => Err(PlotReadbackError::WithCleanup {
primary: Box::new(error),
cleanup: Box::new(cleanup),
}),
}
}
pub async fn readback_u32(access: &WgpuDeviceAccess, buffer: &wgpu::Buffer) -> Result<u32, String> {
readback_mapped(access, buffer, 4, |bytes| {
Ok(u32::from_le_bytes(bytes.try_into().map_err(|_| {
PlotReadbackError::Decode("invalid u32 readback extent")
})?))
})
.await
.map_err(|error| error.to_string())
}
pub async fn readback_f32(access: &WgpuDeviceAccess, buffer: &wgpu::Buffer) -> Result<f32, String> {
Ok(f32::from_bits(readback_u32(access, buffer).await?))
}
pub async fn readback_f32_buffer(
access: &WgpuDeviceAccess,
buffer: &wgpu::Buffer,
element_count: usize,
) -> Result<Vec<f32>, String> {
let byte_len = element_count
.checked_mul(4)
.ok_or("readback byte size overflow")?;
readback_mapped(access, buffer, byte_len, |bytes| {
Ok(bytes
.chunks_exact(4)
.map(|bytes| f32::from_le_bytes(bytes.try_into().expect("four-byte chunk")))
.collect())
})
.await
.map_err(|error| error.to_string())
}
pub async fn copy_readback_bytes(
access: &WgpuDeviceAccess,
buffer: &GpuInputBuffer,
byte_len: usize,
) -> Result<Vec<u8>, String> {
copy_readback_bytes_typed(access, buffer, byte_len)
.await
.map_err(|error| error.to_string())
}
pub(crate) async fn copy_readback_bytes_typed(
access: &WgpuDeviceAccess,
buffer: &GpuInputBuffer,
byte_len: usize,
) -> Result<Vec<u8>, PlotReadbackError> {
copy_readback_mapped(access, buffer, byte_len, |bytes| Ok(bytes.to_vec())).await
}
pub async fn copy_readback_u32_buffer(
access: &WgpuDeviceAccess,
buffer: &GpuInputBuffer,
element_count: usize,
) -> Result<Vec<u32>, PlotReadbackError> {
let byte_len = element_count
.checked_mul(4)
.ok_or(PlotReadbackError::ByteSizeOverflow)?;
copy_readback_mapped(access, buffer, byte_len, |bytes| {
Ok(bytes
.chunks_exact(4)
.map(|bytes| u32::from_le_bytes(bytes.try_into().expect("four-byte chunk")))
.collect())
})
.await
}
async fn copy_readback_mapped<T>(
access: &WgpuDeviceAccess,
buffer: &GpuInputBuffer,
byte_len: usize,
decode: impl FnOnce(&[u8]) -> Result<T, PlotReadbackError>,
) -> Result<T, PlotReadbackError> {
if byte_len == 0 {
access
.with_shared(|emission| {
buffer
.buffer(emission)
.map(|_| ())
.map_err(PlotReadbackError::Access)
})
.map_err(PlotReadbackError::Access)??;
return decode(&[]);
}
let staging = access
.with_shared(|emission| -> Result<_, PlotReadbackError> {
let source = buffer.buffer(emission).map_err(PlotReadbackError::Access)?;
let byte_size =
u64::try_from(byte_len).map_err(|_| PlotReadbackError::ByteSizeOverflow)?;
if byte_size > source.size() {
return Err(PlotReadbackError::CopyBufferTooSmall {
requested: byte_len,
available: source.size(),
});
}
let staging = emission.device().create_buffer(&wgpu::BufferDescriptor {
label: Some("runmat-plot-readback-staging"),
size: byte_size,
usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let commands = emission.encode("runmat-plot-readback-encoder", |encoder| {
encoder.copy_buffer_to_buffer(source, 0, &staging, 0, byte_size);
});
emission.queue().submit(Some(commands));
Ok(staging)
})
.map_err(PlotReadbackError::Access)??;
readback_mapped(access, &staging, byte_len, decode).await
}
pub async fn readback_scalar_buffer_f64(
access: &WgpuDeviceAccess,
buffer: &GpuInputBuffer,
element_count: usize,
scalar: ScalarType,
) -> Result<Vec<f64>, String> {
readback_scalar_buffer_f64_typed(access, buffer, element_count, scalar)
.await
.map_err(|error| error.to_string())
}
pub(crate) async fn readback_scalar_buffer_f64_typed(
access: &WgpuDeviceAccess,
buffer: &GpuInputBuffer,
element_count: usize,
scalar: ScalarType,
) -> Result<Vec<f64>, PlotReadbackError> {
let width = match scalar {
ScalarType::F32 => 4,
ScalarType::F64 => 8,
};
let byte_len = element_count
.checked_mul(width)
.ok_or(PlotReadbackError::ByteSizeOverflow)?;
copy_readback_mapped(access, buffer, byte_len, |bytes| match scalar {
ScalarType::F32 => Ok(bytes
.chunks_exact(4)
.map(|bytes| {
f64::from(f32::from_le_bytes(
bytes.try_into().expect("four-byte chunk"),
))
})
.collect()),
ScalarType::F64 => Ok(bytes
.chunks_exact(8)
.map(|bytes| f64::from_le_bytes(bytes.try_into().expect("eight-byte chunk")))
.collect()),
})
.await
}