logo
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

Builds a new buffer. Only allowed for sized data.

Panics
  • Panics if T has zero size.

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.

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.
Panics
  • Panics if T has zero size.
  • Panics if data is empty.

Builds a new buffer. Can be used for arrays.

Panics
  • Panics if T has zero size.
  • Panics if len is zero.

Builds a new buffer without checking the size.

Safety
  • You must ensure that the size that you pass is correct for T.
Panics
  • Panics if size is zero.

Same as raw but with exportable fd option for the allocated memory on Linux/BSD

Panics
  • Panics if size is zero.

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.

Returns the queue families this buffer can be used on.

Trait Implementations

Returns the inner information about this buffer.
Returns the size of the buffer in bytes.
Returns the usage the buffer was created with.
Returns a BufferSlice covering the whole buffer.
Returns a BufferSlice for a subrange of elements in the buffer. Returns None if out of range. Read more
Returns a BufferSlice for a single element in the buffer. Returns None if out of range. Read more
Gets the device address for this buffer. Read more
Formats the value using the given formatter. Read more
Returns the device that owns Self.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
The type of the content.
Returns the length of the buffer in number of elements. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.