use std::mem::{offset_of, size_of};
use crate::{Engine, Error, Event, Matrix, Result, runtime::EngineHandle};
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Detection {
pub center_x: f32,
pub center_y: f32,
pub width: f32,
pub height: f32,
pub confidence: f32,
pub class_id: u32,
pub color_rgba: u32,
pub track_id: u32,
}
const _: () = assert!(size_of::<Detection>() == 32);
const _: () = assert!(offset_of!(Detection, color_rgba) == 24);
const _: () = assert!(offset_of!(Detection, track_id) == 28);
pub struct DetectionBuffer {
engine: EngineHandle,
storage: Matrix,
consumer: Option<Event>,
count: u32,
capacity: u32,
}
impl DetectionBuffer {
pub fn create_host_upload(capacity: u32) -> Result<Self> {
Self::create_host_upload_on(&Engine::current()?, capacity)
}
pub fn create_host_upload_on(engine: &Engine, capacity: u32) -> Result<Self> {
if capacity == 0 {
return Err(Error::invalid_argument(
"oa::vision::DetectionBuffer::create_host_upload: capacity must be non-zero",
));
}
let byte_len = usize::try_from(capacity)
.ok()
.and_then(|count| count.checked_mul(size_of::<Detection>()))
.ok_or_else(|| {
Error::invalid_argument(
"oa::vision::DetectionBuffer::create_host_upload: capacity byte extent overflows usize",
)
})?;
let storage = Matrix::new_from_slice_on(engine, [byte_len], &vec![0_u8; byte_len])?;
Ok(Self {
engine: engine.handle(),
storage,
consumer: None,
count: 0,
capacity,
})
}
pub fn upload(&mut self, detections: &[Detection]) -> Result<()> {
if !self.is_ready()? {
return Err(Error::not_ready(
"oa::vision::DetectionBuffer::upload: buffer is still consumed by the GPU",
));
}
if detections.len() > usize::try_from(self.capacity).unwrap_or(usize::MAX) {
return Err(Error::resource_exhausted(
"oa::vision::DetectionBuffer::upload: detection count exceeds capacity",
));
}
let used = std::mem::size_of_val(detections);
let mut bytes = vec![0_u8; self.storage.num_elements()];
if used > 0 {
let source = unsafe { std::slice::from_raw_parts(detections.as_ptr().cast::<u8>(), used) };
bytes[..used].copy_from_slice(source);
}
self.storage.write_values::<u8>(&bytes)?;
self.count = u32::try_from(detections.len())
.map_err(|_| Error::invalid_argument("detection count exceeds u32"))?;
self.consumer = None;
Ok(())
}
pub fn mark_consumed(&mut self, completion: &Event) -> Result<()> {
if !self.engine.owns_event(completion) {
return Err(Error::invalid_argument(
"oa::vision::DetectionBuffer::mark_consumed requires an event from its engine",
));
}
self.consumer = if completion.is_complete()? {
None
} else {
Some(completion.clone())
};
Ok(())
}
pub fn is_ready(&self) -> Result<bool> {
match &self.consumer {
Some(completion) => completion.is_complete(),
None => Ok(true),
}
}
pub const fn count(&self) -> u32 {
self.count
}
pub const fn capacity(&self) -> u32 {
self.capacity
}
pub(crate) fn bindless_index(&self) -> Result<u32> {
self.engine.storage_descriptor_index(self.storage.storage())
}
pub(crate) const fn storage(&self) -> &Matrix {
&self.storage
}
pub(crate) const fn engine_handle(&self) -> &EngineHandle {
&self.engine
}
}