Expand description
Recording commands to execute on the device.
With Vulkan, to get the device to perform work, even relatively simple tasks, you must create a command buffer. A command buffer is a list of commands that will executed by the device. You must first record commands to a recording command buffer, then end the recording to turn it into an actual command buffer, and then it can be used. Depending on how a command buffer is created, it can be used only once, or reused many times.
§Command pools and allocators
Command buffers are allocated from command pools. A command pool holds memory that is used to record the sequence of commands in a command buffer. Command pools are not thread-safe, and therefore commands cannot be recorded to a single command buffer from multiple threads at a time.
Raw command pools are unsafe to use, so Vulkano uses [command buffer allocators] to manage
command buffers and pools, to ensure their memory is used efficiently, and to protect against
invalid usage. Vulkano provides the StandardCommandBufferAllocator
for this purpose, but
you can also create your own by implementing the CommandBufferAllocator
trait.
§Primary and secondary command buffers
There are two levels of command buffers:
- A primary command buffer can be executed on a queue, and is the main command buffer level. It cannot be executed within another command buffer.
- A secondary command buffer can only be executed within a primary command buffer, not directly on a queue.
Using secondary command buffers, there is slightly more overhead than using primary command buffers alone, but there are also advantages. A single command buffer cannot be recorded from multiple threads at a time, so if you want to divide the recording work among several threads, each thread must record its own command buffer. While it is possible for these to be all primary command buffers, there are limitations: a render pass or query cannot span multiple primary command buffers, while secondary command buffers can inherit this state from their parent primary command buffer. Therefore, to have a single render pass or query that is shared across all the command buffers, you must record secondary command buffers.
§Recording a command buffer
To record a new command buffer, the most direct way is to create a new
AutoCommandBufferBuilder
. You can then call methods on this object to record new commands
to the command buffer. When you are done recording, you call build
to finalise the command
buffer and turn it into either a PrimaryAutoCommandBuffer
or a
SecondaryAutoCommandBuffer
.
§Submitting a primary command buffer
Once a primary command buffer is recorded and built, you can submit the
PrimaryAutoCommandBuffer
to a queue. Submitting a command buffer returns an object that
implements the GpuFuture
trait and that represents the moment when the execution will end
on the GPU.
use vulkano::command_buffer::{
AutoCommandBufferBuilder, CommandBufferUsage, PrimaryCommandBufferAbstract,
SubpassContents,
};
let mut cb = AutoCommandBufferBuilder::primary(
command_buffer_allocator.clone(),
queue.queue_family_index(),
CommandBufferUsage::OneTimeSubmit,
)
.unwrap();
cb.begin_render_pass(render_pass_begin_info, Default::default())
.unwrap()
.bind_pipeline_graphics(graphics_pipeline.clone())
.unwrap()
.bind_vertex_buffers(0, vertex_buffer.clone())
.unwrap();
unsafe { cb.draw(vertex_buffer.len() as u32, 1, 0, 0) }.unwrap();
cb.end_render_pass(Default::default()).unwrap();
let cb = cb.build().unwrap();
let future = cb.execute(queue.clone());
Re-exports§
pub use self::auto::AutoCommandBufferBuilder;
pub use self::auto::PrimaryAutoCommandBuffer;
pub use self::auto::SecondaryAutoCommandBuffer;
Modules§
- allocator
- Traits and types for managing the allocation of command buffers and command pools.
- auto
- Contains
AutoCommandBufferBuilder
and the built typesPrimaryCommandBuffer
andSecondaryCommandBuffer
. - pool
- Memory and resource pool for recording command buffers.
Structs§
- Blit
Image Info - Parameters to blit image data.
- Buffer
Copy - A region of data to copy between buffers.
- Buffer
Image Copy - A region of data to copy between a buffer and an image.
- Clear
Color Image Info - Parameters to clear a color image.
- Clear
Depth Stencil Image Info - Parameters to clear a depth/stencil image.
- Clear
Rect - Specifies the clear region for the
clear_attachments
command. - Command
Buffer - A command buffer that has finished recording.
- Command
Buffer Begin Info - Parameters to begin recording a command buffer.
- Command
Buffer Exec Future - Represents a command buffer being executed by the GPU and the moment when the execution finishes.
- Command
Buffer Inheritance Info - The context that a secondary command buffer can inherit from the primary command buffer it’s executed in.
- Command
Buffer Inheritance Render Pass Info - The render pass context that a secondary command buffer is created for.
- Command
Buffer Inheritance Rendering Info - The dynamic rendering context that a secondary command buffer is created for.
- Command
Buffer State - Command
Buffer Submit Info - Parameters for a command buffer in a queue submit operation.
- Copy
Buffer Info - Parameters to copy data from a buffer to another buffer.
- Copy
Buffer Info Typed - Parameters to copy data from a buffer to another buffer, with type information.
- Copy
Buffer ToImage Info - Parameters to copy data from a buffer to an image.
- Copy
Image Info - Parameters to copy data from an image to another image.
- Copy
Image ToBuffer Info - Parameters to copy data from an image to a buffer.
- Dispatch
Indirect Command - Used as buffer contents to provide input for the
AutoCommandBufferBuilder::dispatch_indirect
command. - Draw
Indexed Indirect Command - Used as buffer contents to provide input for the
AutoCommandBufferBuilder::draw_indexed_indirect
command. - Draw
Indirect Command - Used as buffer contents to provide input for the
AutoCommandBufferBuilder::draw_indirect
command. - Draw
Mesh Tasks Indirect Command - Used as buffer contents to provide input for the
AutoCommandBufferBuilder::draw_mesh_tasks_indirect
command. - Image
Blit - A region of data to blit between images.
- Image
Copy - A region of data to copy between images.
- Image
Resolve - A region of data to resolve between images.
- Recording
Command Buffer - A command buffer in the recording state.
- Render
Pass Begin Info - Parameters to begin a new render pass.
- Rendering
Attachment Info - Parameters to specify properties of an attachment.
- Rendering
Attachment Resolve Info - Parameters to specify the resolve behavior of an attachment.
- Rendering
Info - Parameters to begin rendering.
- Resolve
Image Info - Parameters to resolve image data.
- Resource
UseRef - Secondary
Resource UseRef - Semaphore
Submit Info - Parameters for a semaphore signal or wait operation in a queue submit operation.
- Submit
Info - Parameters to submit command buffers to a queue.
- Subpass
Begin Info - Parameters to begin a new subpass within a render pass.
- Subpass
EndInfo - Parameters to end the current subpass within a render pass.
Enums§
- Clear
Attachment - Clear attachment type, used in
clear_attachments
command. - Command
Buffer Exec Error - Error that can happen when attempting to execute a command buffer.
- Command
Buffer Inheritance Render Pass Type - Selects the type of render pass for command buffer inheritance.
- Command
Buffer Level - Determines the kind of command buffer to create.
- Command
Buffer Usage - Usage flags to pass when creating a command buffer.
- Resource
InCommand - Subpass
Contents - Describes what a subpass in a command buffer will contain.