use core::mem::MaybeUninit;
use ruda_core::bytes::{AllocationController, AllocationProperty};
use ruda::runtime::memory_management::ManagedMemoryBinding;
use wgpu::BufferView;
pub struct WgpuAllocController {
view: Option<BufferView>,
buffer: wgpu::Buffer,
_binding: ManagedMemoryBinding,
}
impl Drop for WgpuAllocController {
fn drop(&mut self) {
drop(self.view.take());
self.buffer.unmap();
}
}
impl AllocationController for WgpuAllocController {
fn alloc_align(&self) -> usize {
wgpu::COPY_BUFFER_ALIGNMENT as usize
}
fn property(&self) -> AllocationProperty {
AllocationProperty::Pinned
}
unsafe fn memory_mut(&mut self) -> &mut [MaybeUninit<u8>] {
let bytes: &[u8] = self.view.as_ref().unwrap();
unsafe {
std::slice::from_raw_parts_mut(bytes.as_ptr() as *mut MaybeUninit<u8>, bytes.len())
}
}
fn memory(&self) -> &[std::mem::MaybeUninit<u8>] {
let bytes: &[u8] = self.view.as_ref().unwrap();
unsafe { std::slice::from_raw_parts(bytes.as_ptr() as *const MaybeUninit<u8>, bytes.len()) }
}
}
impl WgpuAllocController {
pub fn init(binding: ManagedMemoryBinding, buffer: wgpu::Buffer) -> Self {
let buf_view = buffer.get_mapped_range(..);
Self {
view: Some(buf_view),
buffer,
_binding: binding,
}
}
}