use smallvec::SmallVec;
use std::error;
use std::fmt;
use std::iter;
use std::marker::PhantomData;
use std::mem;
use std::ops::Deref;
use std::ops::DerefMut;
use std::ptr;
use std::sync::Arc;
use std::sync::RwLock;
use std::sync::RwLockReadGuard;
use std::sync::RwLockWriteGuard;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use buffer::BufferUsage;
use buffer::sys::BufferCreationError;
use buffer::sys::SparseLevel;
use buffer::sys::UnsafeBuffer;
use buffer::traits::BufferAccess;
use buffer::traits::BufferInner;
use buffer::traits::TypedBufferAccess;
use device::Device;
use device::DeviceOwned;
use device::Queue;
use image::ImageAccess;
use instance::QueueFamily;
use memory::Content;
use memory::CpuAccess as MemCpuAccess;
use memory::DedicatedAlloc;
use memory::DeviceMemoryAllocError;
use memory::pool::AllocFromRequirementsFilter;
use memory::pool::AllocLayout;
use memory::pool::MappingRequirement;
use memory::pool::MemoryPool;
use memory::pool::MemoryPoolAlloc;
use memory::pool::PotentialDedicatedAllocation;
use memory::pool::StdMemoryPoolAlloc;
use sync::AccessError;
use sync::Sharing;
#[derive(Debug)]
pub struct CpuAccessibleBuffer<T: ?Sized, A = PotentialDedicatedAllocation<StdMemoryPoolAlloc>> {
inner: UnsafeBuffer,
memory: A,
access: RwLock<CurrentGpuAccess>,
queue_families: SmallVec<[u32; 4]>,
marker: PhantomData<Box<T>>,
}
#[derive(Debug)]
enum CurrentGpuAccess {
NonExclusive {
num: AtomicUsize,
},
Exclusive {
num: usize,
},
}
impl<T> CpuAccessibleBuffer<T> {
pub fn from_data(device: Arc<Device>, usage: BufferUsage, data: T)
-> Result<Arc<CpuAccessibleBuffer<T>>, DeviceMemoryAllocError>
where T: Content + 'static
{
unsafe {
let uninitialized =
CpuAccessibleBuffer::raw(device, mem::size_of::<T>(), usage, iter::empty())?;
{
let mut mapping = uninitialized.write().unwrap();
ptr::write::<T>(&mut *mapping, data)
}
Ok(uninitialized)
}
}
#[inline]
pub unsafe fn uninitialized(device: Arc<Device>, usage: BufferUsage)
-> Result<Arc<CpuAccessibleBuffer<T>>, DeviceMemoryAllocError> {
CpuAccessibleBuffer::raw(device, mem::size_of::<T>(), usage, iter::empty())
}
}
impl<T> CpuAccessibleBuffer<[T]> {
pub fn from_iter<I>(device: Arc<Device>, usage: BufferUsage, data: I)
-> Result<Arc<CpuAccessibleBuffer<[T]>>, DeviceMemoryAllocError>
where I: ExactSizeIterator<Item = T>,
T: Content + 'static
{
unsafe {
let uninitialized =
CpuAccessibleBuffer::uninitialized_array(device, data.len(), usage)?;
{
let mut mapping = uninitialized.write().unwrap();
for (i, o) in data.zip(mapping.iter_mut()) {
ptr::write(o, i);
}
}
Ok(uninitialized)
}
}
#[inline]
pub unsafe fn uninitialized_array(
device: Arc<Device>, len: usize, usage: BufferUsage)
-> Result<Arc<CpuAccessibleBuffer<[T]>>, DeviceMemoryAllocError> {
CpuAccessibleBuffer::raw(device, len * mem::size_of::<T>(), usage, iter::empty())
}
}
impl<T: ?Sized> CpuAccessibleBuffer<T> {
pub unsafe fn raw<'a, I>(device: Arc<Device>, size: usize, usage: BufferUsage,
queue_families: I)
-> Result<Arc<CpuAccessibleBuffer<T>>, DeviceMemoryAllocError>
where I: IntoIterator<Item = QueueFamily<'a>>
{
let queue_families = queue_families
.into_iter()
.map(|f| f.id())
.collect::<SmallVec<[u32; 4]>>();
let (buffer, mem_reqs) = {
let sharing = if queue_families.len() >= 2 {
Sharing::Concurrent(queue_families.iter().cloned())
} else {
Sharing::Exclusive
};
match UnsafeBuffer::new(device.clone(), size, usage, sharing, SparseLevel::none()) {
Ok(b) => b,
Err(BufferCreationError::AllocError(err)) => return Err(err),
Err(_) => unreachable!(),
}
};
let mem = MemoryPool::alloc_from_requirements(&Device::standard_pool(&device),
&mem_reqs,
AllocLayout::Linear,
MappingRequirement::Map,
DedicatedAlloc::Buffer(&buffer),
|_| AllocFromRequirementsFilter::Allowed)?;
debug_assert!((mem.offset() % mem_reqs.alignment) == 0);
debug_assert!(mem.mapped_memory().is_some());
buffer.bind_memory(mem.memory(), mem.offset())?;
Ok(Arc::new(CpuAccessibleBuffer {
inner: buffer,
memory: mem,
access: RwLock::new(CurrentGpuAccess::NonExclusive {
num: AtomicUsize::new(0),
}),
queue_families: queue_families,
marker: PhantomData,
}))
}
}
impl<T: ?Sized, A> CpuAccessibleBuffer<T, A> {
#[inline]
pub fn queue_families(&self) -> Vec<QueueFamily> {
self.queue_families
.iter()
.map(|&num| {
self.device()
.physical_device()
.queue_family_by_id(num)
.unwrap()
})
.collect()
}
}
impl<T: ?Sized, A> CpuAccessibleBuffer<T, A>
where T: Content + 'static,
A: MemoryPoolAlloc
{
#[inline]
pub fn read(&self) -> Result<ReadLock<T>, ReadLockError> {
let lock = match self.access.try_read() {
Ok(l) => l,
Err(_) => return Err(ReadLockError::CpuWriteLocked),
};
if let CurrentGpuAccess::Exclusive { .. } = *lock {
return Err(ReadLockError::GpuWriteLocked);
}
let offset = self.memory.offset();
let range = offset .. offset + self.inner.size();
Ok(ReadLock {
inner: unsafe { self.memory.mapped_memory().unwrap().read_write(range) },
lock: lock,
})
}
#[inline]
pub fn write(&self) -> Result<WriteLock<T>, WriteLockError> {
let lock = match self.access.try_write() {
Ok(l) => l,
Err(_) => return Err(WriteLockError::CpuLocked),
};
match *lock {
CurrentGpuAccess::NonExclusive { ref num } if num.load(Ordering::SeqCst) == 0 => (),
_ => return Err(WriteLockError::GpuLocked),
}
let offset = self.memory.offset();
let range = offset .. offset + self.inner.size();
Ok(WriteLock {
inner: unsafe { self.memory.mapped_memory().unwrap().read_write(range) },
lock: lock,
})
}
}
unsafe impl<T: ?Sized, A> BufferAccess for CpuAccessibleBuffer<T, A>
where T: 'static + Send + Sync
{
#[inline]
fn inner(&self) -> BufferInner {
BufferInner {
buffer: &self.inner,
offset: 0,
}
}
#[inline]
fn size(&self) -> usize {
self.inner.size()
}
#[inline]
fn conflicts_buffer(&self, other: &dyn BufferAccess) -> bool {
self.conflict_key() == other.conflict_key()
}
#[inline]
fn conflicts_image(&self, other: &dyn ImageAccess) -> bool {
false
}
#[inline]
fn conflict_key(&self) -> (u64, usize) {
(self.inner.key(), 0)
}
#[inline]
fn try_gpu_lock(&self, exclusive_access: bool, _: &Queue) -> Result<(), AccessError> {
if exclusive_access {
let mut lock = match self.access.try_write() {
Ok(lock) => lock,
Err(_) => return Err(AccessError::AlreadyInUse),
};
match *lock {
CurrentGpuAccess::NonExclusive { ref num } if num.load(Ordering::SeqCst) == 0 => (),
_ => return Err(AccessError::AlreadyInUse),
};
*lock = CurrentGpuAccess::Exclusive { num: 1 };
Ok(())
} else {
let lock = match self.access.try_read() {
Ok(lock) => lock,
Err(_) => return Err(AccessError::AlreadyInUse),
};
match *lock {
CurrentGpuAccess::Exclusive { .. } => return Err(AccessError::AlreadyInUse),
CurrentGpuAccess::NonExclusive { ref num } => {
num.fetch_add(1, Ordering::SeqCst)
},
};
Ok(())
}
}
#[inline]
unsafe fn increase_gpu_lock(&self) {
{
let read_lock = self.access.read().unwrap();
if let CurrentGpuAccess::NonExclusive { ref num } = *read_lock {
let prev = num.fetch_add(1, Ordering::SeqCst);
debug_assert!(prev >= 1);
return;
}
}
{
let mut write_lock = self.access.write().unwrap();
if let CurrentGpuAccess::Exclusive { ref mut num } = *write_lock {
*num += 1;
} else {
unreachable!()
}
}
}
#[inline]
unsafe fn unlock(&self) {
{
let read_lock = self.access.read().unwrap();
if let CurrentGpuAccess::NonExclusive { ref num } = *read_lock {
let prev = num.fetch_sub(1, Ordering::SeqCst);
debug_assert!(prev >= 1);
return;
}
}
{
let mut write_lock = self.access.write().unwrap();
if let CurrentGpuAccess::Exclusive { ref mut num } = *write_lock {
if *num != 1 {
*num -= 1;
return;
}
} else {
panic!()
}
*write_lock = CurrentGpuAccess::NonExclusive { num: AtomicUsize::new(0) };
}
}
}
unsafe impl<T: ?Sized, A> TypedBufferAccess for CpuAccessibleBuffer<T, A>
where T: 'static + Send + Sync
{
type Content = T;
}
unsafe impl<T: ?Sized, A> DeviceOwned for CpuAccessibleBuffer<T, A> {
#[inline]
fn device(&self) -> &Arc<Device> {
self.inner.device()
}
}
pub struct ReadLock<'a, T: ?Sized + 'a> {
inner: MemCpuAccess<'a, T>,
lock: RwLockReadGuard<'a, CurrentGpuAccess>,
}
impl<'a, T: ?Sized + 'a> ReadLock<'a, T> {
#[inline]
pub fn map<U: ?Sized + 'a, F>(self, f: F) -> ReadLock<'a, U>
where F: FnOnce(&mut T) -> &mut U
{
ReadLock {
inner: self.inner.map(|ptr| unsafe { f(&mut *ptr) as *mut _ }),
lock: self.lock,
}
}
}
impl<'a, T: ?Sized + 'a> Deref for ReadLock<'a, T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
self.inner.deref()
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ReadLockError {
CpuWriteLocked,
GpuWriteLocked,
}
impl error::Error for ReadLockError {
#[inline]
fn description(&self) -> &str {
match *self {
ReadLockError::CpuWriteLocked => {
"the buffer is already locked for write mode by the CPU"
},
ReadLockError::GpuWriteLocked => {
"the buffer is already locked for write mode by the GPU"
},
}
}
}
impl fmt::Display for ReadLockError {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(fmt, "{}", error::Error::description(self))
}
}
pub struct WriteLock<'a, T: ?Sized + 'a> {
inner: MemCpuAccess<'a, T>,
lock: RwLockWriteGuard<'a, CurrentGpuAccess>,
}
impl<'a, T: ?Sized + 'a> WriteLock<'a, T> {
#[inline]
pub fn map<U: ?Sized + 'a, F>(self, f: F) -> WriteLock<'a, U>
where F: FnOnce(&mut T) -> &mut U
{
WriteLock {
inner: self.inner.map(|ptr| unsafe { f(&mut *ptr) as *mut _ }),
lock: self.lock,
}
}
}
impl<'a, T: ?Sized + 'a> Deref for WriteLock<'a, T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
self.inner.deref()
}
}
impl<'a, T: ?Sized + 'a> DerefMut for WriteLock<'a, T> {
#[inline]
fn deref_mut(&mut self) -> &mut T {
self.inner.deref_mut()
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum WriteLockError {
CpuLocked,
GpuLocked,
}
impl error::Error for WriteLockError {
#[inline]
fn description(&self) -> &str {
match *self {
WriteLockError::CpuLocked => {
"the buffer is already locked by the CPU"
},
WriteLockError::GpuLocked => {
"the buffer is already locked by the GPU"
},
}
}
}
impl fmt::Display for WriteLockError {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(fmt, "{}", error::Error::description(self))
}
}
#[cfg(test)]
mod tests {
use buffer::{BufferUsage, CpuAccessibleBuffer};
#[test]
fn create_empty_buffer() {
let (device, queue) = gfx_dev_and_queue!();
const EMPTY: [i32; 0] = [];
let _ = CpuAccessibleBuffer::from_data(device, BufferUsage::all(), EMPTY.iter());
}
}