Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new safe Buffer and Image types containing bound memory #2050

Merged
merged 5 commits into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 55 additions & 57 deletions vulkano/src/buffer/cpu_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
//! or write and write simultaneously will block.

use super::{
sys::UnsafeBuffer, BufferAccess, BufferAccessObject, BufferContents, BufferCreationError,
BufferInner, BufferUsage,
sys::{Buffer, BufferMemory, RawBuffer},
BufferAccess, BufferAccessObject, BufferContents, BufferError, BufferInner, BufferUsage,
};
use crate::{
buffer::{sys::UnsafeBufferCreateInfo, TypedBufferAccess},
buffer::{sys::BufferCreateInfo, TypedBufferAccess},
device::{Device, DeviceOwned},
memory::{
allocator::{
AllocationCreateInfo, AllocationCreationError, AllocationType, MemoryAlloc,
AllocationCreateInfo, AllocationCreationError, AllocationType,
MemoryAllocatePreference, MemoryAllocator, MemoryUsage,
},
DedicatedAllocation,
Expand Down Expand Up @@ -56,16 +56,7 @@ pub struct CpuAccessibleBuffer<T>
where
T: BufferContents + ?Sized,
{
// Inner content.
inner: Arc<UnsafeBuffer>,

// The memory held by the buffer.
memory: MemoryAlloc,

// Queue families allowed to access this buffer.
queue_family_indices: SmallVec<[u32; 4]>,

// Necessary to make it compile.
inner: Arc<Buffer>,
marker: PhantomData<Box<T>>,
}

Expand Down Expand Up @@ -217,11 +208,11 @@ where
) -> Result<Arc<CpuAccessibleBuffer<T>>, AllocationCreationError> {
let queue_family_indices: SmallVec<[_; 4]> = queue_family_indices.into_iter().collect();

let buffer = UnsafeBuffer::new(
let raw_buffer = RawBuffer::new(
allocator.device().clone(),
UnsafeBufferCreateInfo {
BufferCreateInfo {
sharing: if queue_family_indices.len() >= 2 {
Sharing::Concurrent(queue_family_indices.clone())
Sharing::Concurrent(queue_family_indices)
} else {
Sharing::Exclusive
},
Expand All @@ -231,11 +222,11 @@ where
},
)
.map_err(|err| match err {
BufferCreationError::AllocError(err) => err,
BufferError::AllocError(err) => err,
// We don't use sparse-binding, therefore the other errors can't happen.
_ => unreachable!(),
})?;
let requirements = buffer.memory_requirements();
let requirements = *raw_buffer.memory_requirements();
let create_info = AllocationCreateInfo {
requirements,
allocation_type: AllocationType::Linear,
Expand All @@ -245,7 +236,7 @@ where
MemoryUsage::Upload
},
allocate_preference: MemoryAllocatePreference::Unknown,
dedicated_allocation: Some(DedicatedAllocation::Buffer(&buffer)),
dedicated_allocation: Some(DedicatedAllocation::Buffer(&raw_buffer)),
..Default::default()
};

Expand All @@ -257,12 +248,14 @@ where
// easier to invalidate and flush the whole buffer. It does not affect the
// allocation in any way.
alloc.shrink(size);
buffer.bind_memory(alloc.device_memory(), alloc.offset())?;
let inner = Arc::new(
raw_buffer
.bind_memory_unchecked(alloc)
.map_err(|(err, _, _)| err)?,
);

Ok(Arc::new(CpuAccessibleBuffer {
inner: buffer,
memory: alloc,
queue_family_indices,
inner,
marker: PhantomData,
}))
}
Expand All @@ -271,16 +264,6 @@ where
}
}

impl<T> CpuAccessibleBuffer<T>
where
T: BufferContents + ?Sized,
{
/// Returns the queue families this buffer can be used on.
pub fn queue_family_indices(&self) -> &[u32] {
&self.queue_family_indices
}
}

impl<T> CpuAccessibleBuffer<T>
where
T: BufferContents + ?Sized,
Expand All @@ -295,12 +278,17 @@ where
/// that uses it in exclusive mode will fail. You can still submit this buffer for non-exclusive
/// accesses (ie. reads).
pub fn read(&self) -> Result<ReadLock<'_, T>, ReadLockError> {
let allocation = match self.inner.memory() {
BufferMemory::Normal(a) => a,
BufferMemory::Sparse => unreachable!(),
};

let range = self.inner().offset..self.inner().offset + self.size();
let mut state = self.inner.state();
let buffer_range = self.inner().offset..self.inner().offset + self.size();

unsafe {
state.check_cpu_read(buffer_range.clone())?;
state.cpu_read_lock(buffer_range.clone());
state.check_cpu_read(range.clone())?;
state.cpu_read_lock(range.clone());
}

let bytes = unsafe {
Expand All @@ -309,13 +297,13 @@ where
// lock, so there will no new data and this call will do nothing.
// TODO: probably still more efficient to call it only if we're the first to acquire a
// read lock, but the number of CPU locks isn't currently tracked anywhere.
self.memory.invalidate_range(0..self.size()).unwrap();
self.memory.mapped_slice().unwrap()
allocation.invalidate_range(0..self.size()).unwrap();
allocation.mapped_slice().unwrap()
};

Ok(ReadLock {
inner: self,
buffer_range,
buffer: self,
range,
data: T::from_bytes(bytes).unwrap(),
})
}
Expand All @@ -329,22 +317,27 @@ where
/// After this function successfully locks the buffer, any attempt to submit a command buffer
/// that uses it and any attempt to call `read()` will return an error.
pub fn write(&self) -> Result<WriteLock<'_, T>, WriteLockError> {
let allocation = match self.inner.memory() {
BufferMemory::Normal(a) => a,
BufferMemory::Sparse => unreachable!(),
};

let range = self.inner().offset..self.inner().offset + self.size();
let mut state = self.inner.state();
let buffer_range = self.inner().offset..self.inner().offset + self.size();

unsafe {
state.check_cpu_write(buffer_range.clone())?;
state.cpu_write_lock(buffer_range.clone());
state.check_cpu_write(range.clone())?;
state.cpu_write_lock(range.clone());
}

let bytes = unsafe {
self.memory.invalidate_range(0..self.size()).unwrap();
self.memory.write(0..self.size()).unwrap()
allocation.invalidate_range(0..self.size()).unwrap();
allocation.write(0..self.size()).unwrap()
};

Ok(WriteLock {
inner: self,
buffer_range,
buffer: self,
range,
data: T::from_bytes_mut(bytes).unwrap(),
})
}
Expand Down Expand Up @@ -421,8 +414,8 @@ pub struct ReadLock<'a, T>
where
T: BufferContents + ?Sized,
{
inner: &'a CpuAccessibleBuffer<T>,
buffer_range: Range<DeviceSize>,
buffer: &'a CpuAccessibleBuffer<T>,
range: Range<DeviceSize>,
data: &'a T,
}

Expand All @@ -432,8 +425,8 @@ where
{
fn drop(&mut self) {
unsafe {
let mut state = self.inner.inner.state();
state.cpu_read_unlock(self.buffer_range.clone());
let mut state = self.buffer.inner.state();
state.cpu_read_unlock(self.range.clone());
}
}
}
Expand All @@ -458,8 +451,8 @@ pub struct WriteLock<'a, T>
where
T: BufferContents + ?Sized,
{
inner: &'a CpuAccessibleBuffer<T>,
buffer_range: Range<DeviceSize>,
buffer: &'a CpuAccessibleBuffer<T>,
range: Range<DeviceSize>,
data: &'a mut T,
}

Expand All @@ -468,11 +461,16 @@ where
T: BufferContents + ?Sized + 'a,
{
fn drop(&mut self) {
let allocation = match self.buffer.inner.memory() {
BufferMemory::Normal(a) => a,
BufferMemory::Sparse => unreachable!(),
};

unsafe {
self.inner.memory.flush_range(0..self.inner.size()).unwrap();
allocation.flush_range(0..self.buffer.size()).unwrap();

let mut state = self.inner.inner.state();
state.cpu_write_unlock(self.buffer_range.clone());
let mut state = self.buffer.inner.state();
state.cpu_write_unlock(self.range.clone());
}
}
}
Expand Down
53 changes: 32 additions & 21 deletions vulkano/src/buffer/cpu_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@
// according to those terms.

use super::{
sys::{UnsafeBuffer, UnsafeBufferCreateInfo},
BufferAccess, BufferAccessObject, BufferContents, BufferCreationError, BufferInner,
BufferUsage, TypedBufferAccess,
sys::{Buffer, BufferCreateInfo, RawBuffer},
BufferAccess, BufferAccessObject, BufferContents, BufferError, BufferInner, BufferUsage,
TypedBufferAccess,
};
use crate::{
buffer::sys::BufferMemory,
device::{Device, DeviceOwned},
memory::{
allocator::{
AllocationCreateInfo, AllocationCreationError, AllocationType, MemoryAlloc,
AllocationCreateInfo, AllocationCreationError, AllocationType,
MemoryAllocatePreference, MemoryAllocator, MemoryUsage, StandardMemoryAllocator,
},
DedicatedAllocation,
},
DeviceSize,
DeviceSize, VulkanError,
};
use std::{
hash::{Hash, Hasher},
Expand Down Expand Up @@ -118,11 +119,7 @@ where
// One buffer of the pool.
#[derive(Debug)]
struct ActualBuffer {
// Inner content.
inner: Arc<UnsafeBuffer>,

// The memory held by the buffer.
memory: MemoryAlloc,
inner: Arc<Buffer>,

// List of the chunks that are reserved.
chunks_in_use: Mutex<Vec<ActualBufferChunk>>,
Expand Down Expand Up @@ -426,29 +423,33 @@ where
) -> Result<(), AllocationCreationError> {
let size = match (size_of::<T>() as DeviceSize).checked_mul(capacity) {
Some(s) => s,
None => return Err(AllocationCreationError::OutOfDeviceMemory),
None => {
return Err(AllocationCreationError::VulkanError(
VulkanError::OutOfDeviceMemory,
))
}
};

let buffer = UnsafeBuffer::new(
let raw_buffer = RawBuffer::new(
self.device().clone(),
UnsafeBufferCreateInfo {
BufferCreateInfo {
size,
usage: self.buffer_usage,
..Default::default()
},
)
.map_err(|err| match err {
BufferCreationError::AllocError(err) => err,
BufferError::AllocError(err) => err,
// We don't use sparse-binding, therefore the other errors can't happen.
_ => unreachable!(),
})?;
let requirements = buffer.memory_requirements();
let requirements = *raw_buffer.memory_requirements();
let create_info = AllocationCreateInfo {
requirements,
allocation_type: AllocationType::Linear,
usage: self.memory_usage,
allocate_preference: MemoryAllocatePreference::Unknown,
dedicated_allocation: Some(DedicatedAllocation::Buffer(&buffer)),
dedicated_allocation: Some(DedicatedAllocation::Buffer(&raw_buffer)),
..Default::default()
};

Expand All @@ -457,11 +458,16 @@ where
debug_assert!(alloc.offset() % requirements.alignment == 0);
debug_assert!(alloc.size() == requirements.size);
alloc.shrink(size);
unsafe { buffer.bind_memory(alloc.device_memory(), alloc.offset()) }?;
let inner = unsafe {
Arc::new(
raw_buffer
.bind_memory_unchecked(alloc)
.map_err(|(err, _, _)| err)?,
)
};

**cur_buf_mutex = Some(Arc::new(ActualBuffer {
inner: buffer,
memory: alloc,
inner,
chunks_in_use: Mutex::new(vec![]),
next_index: AtomicU64::new(0),
capacity,
Expand Down Expand Up @@ -588,7 +594,12 @@ where
let range = (index * size_of::<T>() as DeviceSize + align_offset)
..((index + requested_len) * size_of::<T>() as DeviceSize + align_offset);

let bytes = current_buffer.memory.write(range.clone()).unwrap();
let allocation = match current_buffer.inner.memory() {
BufferMemory::Normal(a) => a,
BufferMemory::Sparse => unreachable!(),
};

let bytes = allocation.write(range.clone()).unwrap();
let mapping = <[T]>::from_bytes_mut(bytes).unwrap();

let mut written = 0;
Expand All @@ -597,7 +608,7 @@ where
written += 1;
}

current_buffer.memory.flush_range(range).unwrap();
allocation.flush_range(range).unwrap();

assert_eq!(
written, requested_len,
Expand Down
Loading