Struct vulkano::buffer::device_local::DeviceLocalBuffer
source · [−]pub struct DeviceLocalBuffer<T, A = PotentialDedicatedAllocation<StandardMemoryPoolAlloc>>where
T: BufferContents + ?Sized,{ /* private fields */ }
Expand description
Buffer whose content is in device-local memory.
This buffer type is useful in order to store intermediary data. For example you execute a compute shader that writes to this buffer, then read the content of the buffer in a following compute or graphics pipeline.
The DeviceLocalBuffer
will be in device-local memory, unless the device doesn’t provide any
device-local memory.
Usage
Since a DeviceLocalBuffer
can only be directly accessed by the GPU, data cannot be transfered between
the host process and the buffer alone. One must use additional buffers which are accessible to the CPU as
staging areas, then use command buffers to execute the necessary data transfers.
Despite this, if one knows in advance that a buffer will not need to be frequently accessed by the host,
then there may be significant performance gains by using a DeviceLocalBuffer
over a buffer type which
allows host access.
Example
The following example outlines the general strategy one may take when initializing a DeviceLocalBuffer
.
use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer, DeviceLocalBuffer};
use vulkano::command_buffer::{AutoCommandBufferBuilder, CommandBufferUsage, CopyBufferInfo, PrimaryCommandBuffer};
use vulkano::sync::GpuFuture;
// Simple iterator to construct test data.
let data = (0..10_000).map(|i| i as f32);
// Create a CPU accessible buffer initialized with the data.
let temporary_accessible_buffer = CpuAccessibleBuffer::from_iter(
device.clone(),
BufferUsage { transfer_src: true, ..BufferUsage::empty() }, // Specify this buffer will be used as a transfer source.
false,
data,
)
.unwrap();
// Create a buffer array on the GPU with enough space for `10_000` floats.
let device_local_buffer = DeviceLocalBuffer::<[f32]>::array(
device.clone(),
10_000 as vulkano::DeviceSize,
BufferUsage {
storage_buffer: true,
transfer_dst: true,
..BufferUsage::empty()
}, // Specify use as a storage buffer and transfer destination.
device.active_queue_family_indices().iter().copied(),
)
.unwrap();
// Create a one-time command to copy between the buffers.
let mut cbb = AutoCommandBufferBuilder::primary(
device.clone(),
queue.queue_family_index(),
CommandBufferUsage::OneTimeSubmit,
)
.unwrap();
cbb.copy_buffer(CopyBufferInfo::buffers(
temporary_accessible_buffer,
device_local_buffer.clone(),
))
.unwrap();
let cb = cbb.build().unwrap();
// Execute copy command and wait for completion before proceeding.
cb.execute(queue.clone())
.unwrap()
.then_signal_fence_and_flush()
.unwrap()
.wait(None /* timeout */)
.unwrap()
Implementations
sourceimpl<T> DeviceLocalBuffer<T>where
T: BufferContents,
impl<T> DeviceLocalBuffer<T>where
T: BufferContents,
sourcepub fn new(
device: Arc<Device>,
usage: BufferUsage,
queue_family_indices: impl IntoIterator<Item = u32>
) -> Result<Arc<DeviceLocalBuffer<T>>, DeviceMemoryError>
pub fn new(
device: Arc<Device>,
usage: BufferUsage,
queue_family_indices: impl IntoIterator<Item = u32>
) -> Result<Arc<DeviceLocalBuffer<T>>, DeviceMemoryError>
sourceimpl<T> DeviceLocalBuffer<T>where
T: BufferContents + ?Sized,
impl<T> DeviceLocalBuffer<T>where
T: BufferContents + ?Sized,
sourcepub fn from_buffer<B>(
source: Arc<B>,
usage: BufferUsage,
queue: Arc<Queue>
) -> Result<(Arc<DeviceLocalBuffer<T>>, CommandBufferExecFuture<NowFuture, PrimaryAutoCommandBuffer>), DeviceLocalBufferCreationError>where
B: TypedBufferAccess<Content = T> + 'static,
pub fn from_buffer<B>(
source: Arc<B>,
usage: BufferUsage,
queue: Arc<Queue>
) -> Result<(Arc<DeviceLocalBuffer<T>>, CommandBufferExecFuture<NowFuture, PrimaryAutoCommandBuffer>), DeviceLocalBufferCreationError>where
B: TypedBufferAccess<Content = T> + 'static,
Builds a DeviceLocalBuffer
that copies its data from another buffer.
This function returns two objects: the newly-created buffer, and a future representing
the initial upload operation. In order to be allowed to use the DeviceLocalBuffer
, you must
either submit your operation after this future, or execute this future and wait for it to
be finished before submitting your own operation.
sourceimpl<T> DeviceLocalBuffer<T>where
T: BufferContents,
impl<T> DeviceLocalBuffer<T>where
T: BufferContents,
sourcepub fn from_data(
data: T,
usage: BufferUsage,
queue: Arc<Queue>
) -> Result<(Arc<DeviceLocalBuffer<T>>, CommandBufferExecFuture<NowFuture, PrimaryAutoCommandBuffer>), DeviceLocalBufferCreationError>
pub fn from_data(
data: T,
usage: BufferUsage,
queue: Arc<Queue>
) -> Result<(Arc<DeviceLocalBuffer<T>>, CommandBufferExecFuture<NowFuture, PrimaryAutoCommandBuffer>), DeviceLocalBufferCreationError>
Builds an DeviceLocalBuffer
from some data.
This function builds a memory-mapped intermediate buffer, writes the data to it, builds a command buffer that copies from this intermediate buffer to the final buffer, and finally submits the command buffer as a future.
This function returns two objects: the newly-created buffer, and a future representing
the initial upload operation. In order to be allowed to use the DeviceLocalBuffer
, you must
either submit your operation after this future, or execute this future and wait for it to
be finished before submitting your own operation.
Panics
- Panics if
T
has zero size.
sourceimpl<T> DeviceLocalBuffer<[T]>where
[T]: BufferContents,
impl<T> DeviceLocalBuffer<[T]>where
[T]: BufferContents,
sourcepub fn from_iter<D>(
data: D,
usage: BufferUsage,
queue: Arc<Queue>
) -> Result<(Arc<DeviceLocalBuffer<[T]>>, CommandBufferExecFuture<NowFuture, PrimaryAutoCommandBuffer>), DeviceLocalBufferCreationError>where
D: IntoIterator<Item = T>,
D::IntoIter: ExactSizeIterator,
pub fn from_iter<D>(
data: D,
usage: BufferUsage,
queue: Arc<Queue>
) -> Result<(Arc<DeviceLocalBuffer<[T]>>, CommandBufferExecFuture<NowFuture, PrimaryAutoCommandBuffer>), DeviceLocalBufferCreationError>where
D: IntoIterator<Item = T>,
D::IntoIter: ExactSizeIterator,
Panics
- Panics if
T
has zero size. - Panics if
data
is empty.
sourceimpl<T> DeviceLocalBuffer<[T]>where
[T]: BufferContents,
impl<T> DeviceLocalBuffer<[T]>where
[T]: BufferContents,
sourcepub fn array(
device: Arc<Device>,
len: DeviceSize,
usage: BufferUsage,
queue_family_indices: impl IntoIterator<Item = u32>
) -> Result<Arc<DeviceLocalBuffer<[T]>>, DeviceMemoryError>
pub fn array(
device: Arc<Device>,
len: DeviceSize,
usage: BufferUsage,
queue_family_indices: impl IntoIterator<Item = u32>
) -> Result<Arc<DeviceLocalBuffer<[T]>>, DeviceMemoryError>
Builds a new buffer. Can be used for arrays.
Panics
- Panics if
T
has zero size. - Panics if
len
is zero.
sourceimpl<T> DeviceLocalBuffer<T>where
T: BufferContents + ?Sized,
impl<T> DeviceLocalBuffer<T>where
T: BufferContents + ?Sized,
sourcepub unsafe fn raw(
device: Arc<Device>,
size: DeviceSize,
usage: BufferUsage,
queue_family_indices: impl IntoIterator<Item = u32>
) -> Result<Arc<DeviceLocalBuffer<T>>, DeviceMemoryError>
pub unsafe fn raw(
device: Arc<Device>,
size: DeviceSize,
usage: BufferUsage,
queue_family_indices: impl IntoIterator<Item = u32>
) -> Result<Arc<DeviceLocalBuffer<T>>, DeviceMemoryError>
sourcepub unsafe fn raw_with_exportable_fd(
device: Arc<Device>,
size: DeviceSize,
usage: BufferUsage,
queue_family_indices: impl IntoIterator<Item = u32>
) -> Result<Arc<DeviceLocalBuffer<T>>, DeviceMemoryError>
pub unsafe fn raw_with_exportable_fd(
device: Arc<Device>,
size: DeviceSize,
usage: BufferUsage,
queue_family_indices: impl IntoIterator<Item = u32>
) -> Result<Arc<DeviceLocalBuffer<T>>, DeviceMemoryError>
Same as raw
but with exportable fd option for the allocated memory on Linux/BSD
Panics
- Panics if
size
is zero.
sourcepub fn export_posix_fd(&self) -> Result<File, DeviceMemoryError>
pub fn export_posix_fd(&self) -> Result<File, DeviceMemoryError>
Exports posix file descriptor for the allocated memory
requires khr_external_memory_fd
and khr_external_memory
extensions to be loaded.
Only works on Linux/BSD.
sourceimpl<T, A> DeviceLocalBuffer<T, A>where
T: BufferContents + ?Sized,
impl<T, A> DeviceLocalBuffer<T, A>where
T: BufferContents + ?Sized,
sourcepub fn queue_family_indices(&self) -> &[u32]
pub fn queue_family_indices(&self) -> &[u32]
Returns the queue families this buffer can be used on.
Trait Implementations
sourceimpl<T, A> BufferAccess for DeviceLocalBuffer<T, A>where
T: BufferContents + ?Sized,
A: Send + Sync,
impl<T, A> BufferAccess for DeviceLocalBuffer<T, A>where
T: BufferContents + ?Sized,
A: Send + Sync,
sourcefn inner(&self) -> BufferInner<'_>
fn inner(&self) -> BufferInner<'_>
sourcefn size(&self) -> DeviceSize
fn size(&self) -> DeviceSize
sourcefn usage(&self) -> &BufferUsage
fn usage(&self) -> &BufferUsage
sourcefn into_buffer_slice(self: &Arc<Self>) -> Arc<BufferSlice<Self::Content, Self>>where
Self: Sized + TypedBufferAccess,
fn into_buffer_slice(self: &Arc<Self>) -> Arc<BufferSlice<Self::Content, Self>>where
Self: Sized + TypedBufferAccess,
BufferSlice
covering the whole buffer.sourcefn slice<T>(
self: &Arc<Self>,
range: Range<DeviceSize>
) -> Option<Arc<BufferSlice<[T], Self>>>where
Self: Sized + TypedBufferAccess<Content = [T]>,
fn slice<T>(
self: &Arc<Self>,
range: Range<DeviceSize>
) -> Option<Arc<BufferSlice<[T], Self>>>where
Self: Sized + TypedBufferAccess<Content = [T]>,
BufferSlice
for a subrange of elements in the buffer. Returns None
if
out of range. Read more