Struct vulkano::buffer::cpu_pool::CpuBufferPool
source · pub struct CpuBufferPool<T, A = Arc<StdMemoryPool>>where
A: MemoryPool,{ /* private fields */ }
Expand description
Ring buffer from which “sub-buffers” can be individually allocated.
This buffer is especially suitable when you want to upload or download some data regularly (for example, at each frame for a video game).
Usage
A CpuBufferPool
is similar to a ring buffer. You start by creating an empty pool, then you
grab elements from the pool and use them, and if the pool is full it will automatically grow
in size.
Contrary to a Vec
, elements automatically free themselves when they are dropped (ie. usually
when you call cleanup_finished()
on a future, or when you drop that future).
Arc-like
The CpuBufferPool
struct internally contains an Arc
. You can clone the CpuBufferPool
for
a cheap cost, and all the clones will share the same underlying buffer.
Example
use vulkano::buffer::CpuBufferPool;
use vulkano::command_buffer::AutoCommandBufferBuilder;
use vulkano::command_buffer::CommandBuffer;
use vulkano::sync::GpuFuture;
// Create the ring buffer.
let buffer = CpuBufferPool::upload(device.clone());
for n in 0 .. 25u32 {
// Each loop grabs a new entry from that ring buffer and stores ` data` in it.
let data: [f32; 4] = [1.0, 0.5, n as f32 / 24.0, 0.0];
let sub_buffer = buffer.next(data).unwrap();
// You can then use `sub_buffer` as if it was an entirely separate buffer.
AutoCommandBufferBuilder::primary_one_time_submit(device.clone(), queue.family())
.unwrap()
// For the sake of the example we just call `update_buffer` on the buffer, even though
// it is pointless to do that.
.update_buffer(sub_buffer.clone(), [0.2, 0.3, 0.4, 0.5])
.unwrap()
.build().unwrap()
.execute(queue.clone())
.unwrap()
.then_signal_fence_and_flush()
.unwrap();
}
Implementations
sourceimpl<T> CpuBufferPool<T>
impl<T> CpuBufferPool<T>
sourcepub fn new(device: Arc<Device>, usage: BufferUsage) -> CpuBufferPool<T>
pub fn new(device: Arc<Device>, usage: BufferUsage) -> CpuBufferPool<T>
Builds a CpuBufferPool
.
sourcepub fn upload(device: Arc<Device>) -> CpuBufferPool<T>
pub fn upload(device: Arc<Device>) -> CpuBufferPool<T>
Builds a CpuBufferPool
meant for simple uploads.
Shortcut for a pool that can only be used as transfer source and with exclusive queue family accesses.
sourcepub fn download(device: Arc<Device>) -> CpuBufferPool<T>
pub fn download(device: Arc<Device>) -> CpuBufferPool<T>
Builds a CpuBufferPool
meant for simple downloads.
Shortcut for a pool that can only be used as transfer destination and with exclusive queue family accesses.
sourcepub fn uniform_buffer(device: Arc<Device>) -> CpuBufferPool<T>
pub fn uniform_buffer(device: Arc<Device>) -> CpuBufferPool<T>
Builds a CpuBufferPool
meant for usage as a uniform buffer.
Shortcut for a pool that can only be used as uniform buffer and with exclusive queue family accesses.
sourcepub fn vertex_buffer(device: Arc<Device>) -> CpuBufferPool<T>
pub fn vertex_buffer(device: Arc<Device>) -> CpuBufferPool<T>
Builds a CpuBufferPool
meant for usage as a vertex buffer.
Shortcut for a pool that can only be used as vertex buffer and with exclusive queue family accesses.
sourcepub fn indirect_buffer(device: Arc<Device>) -> CpuBufferPool<T>
pub fn indirect_buffer(device: Arc<Device>) -> CpuBufferPool<T>
Builds a CpuBufferPool
meant for usage as a indirect buffer.
Shortcut for a pool that can only be used as indirect buffer and with exclusive queue family accesses.
sourceimpl<T, A> CpuBufferPool<T, A>where
A: MemoryPool,
impl<T, A> CpuBufferPool<T, A>where
A: MemoryPool,
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the current capacity of the pool, in number of elements.
sourcepub fn reserve(&self, capacity: usize) -> Result<(), DeviceMemoryAllocError>
pub fn reserve(&self, capacity: usize) -> Result<(), DeviceMemoryAllocError>
Makes sure that the capacity is at least capacity
. Allocates memory if it is not the
case.
Since this can involve a memory allocation, an OomError
can happen.
sourcepub fn next(
&self,
data: T
) -> Result<CpuBufferPoolSubbuffer<T, A>, DeviceMemoryAllocError>
pub fn next(
&self,
data: T
) -> Result<CpuBufferPoolSubbuffer<T, A>, DeviceMemoryAllocError>
Grants access to a new subbuffer and puts data
in it.
If no subbuffer is available (because they are still in use by the GPU), a new buffer will automatically be allocated.
Note: You can think of it like a
Vec
. If you insert an element and theVec
is not large enough, a new chunk of memory is automatically allocated.
sourcepub fn chunk<I>(
&self,
data: I
) -> Result<CpuBufferPoolChunk<T, A>, DeviceMemoryAllocError>where
I: IntoIterator<Item = T>,
I::IntoIter: ExactSizeIterator,
pub fn chunk<I>(
&self,
data: I
) -> Result<CpuBufferPoolChunk<T, A>, DeviceMemoryAllocError>where
I: IntoIterator<Item = T>,
I::IntoIter: ExactSizeIterator,
Grants access to a new subbuffer and puts data
in it.
If no subbuffer is available (because they are still in use by the GPU), a new buffer will automatically be allocated.
Note: You can think of it like a
Vec
. If you insert elements and theVec
is not large enough, a new chunk of memory is automatically allocated.
Panic
Panics if the length of the iterator didn’t match the actual number of element.
sourcepub fn try_next(&self, data: T) -> Option<CpuBufferPoolSubbuffer<T, A>>
pub fn try_next(&self, data: T) -> Option<CpuBufferPoolSubbuffer<T, A>>
Grants access to a new subbuffer and puts data
in it.
Returns None
if no subbuffer is available.
A CpuBufferPool
is always empty the first time you use it, so you shouldn’t use
try_next
the first time you use it.