Skip to main content

wgpu/api/
command_buffer.rs

1use crate::{
2    api::{impl_deferred_command_buffer_actions, SharedDeferredCommandBufferActions},
3    *,
4};
5
6/// Handle to a command buffer on the GPU.
7///
8/// A `CommandBuffer` represents a complete sequence of commands that may be submitted to a command
9/// queue with [`Queue::submit`]. A `CommandBuffer` is obtained by recording a series of commands to
10/// a [`CommandEncoder`] and then calling [`CommandEncoder::finish`].
11///
12/// Corresponds to [WebGPU `GPUCommandBuffer`](https://gpuweb.github.io/gpuweb/#command-buffer).
13#[derive(Debug)]
14pub struct CommandBuffer {
15    pub(crate) buffer: dispatch::DispatchCommandBuffer,
16    /// Deferred actions recorded at encode time, to run at Queue::submit.
17    pub(crate) actions: SharedDeferredCommandBufferActions,
18}
19#[cfg(send_sync)]
20static_assertions::assert_impl_all!(CommandBuffer: Send, Sync);
21
22impl CommandBuffer {
23    #[cfg(custom)]
24    /// Returns custom implementation of CommandBuffer (if custom backend and is internally T)
25    pub fn as_custom<T: custom::CommandBufferInterface>(&self) -> Option<&T> {
26        self.buffer.as_custom()
27    }
28
29    // Expose map_buffer_on_submit/on_submitted_work_done on CommandBuffer as well,
30    // so callers can schedule after finishing encoding.
31    impl_deferred_command_buffer_actions!();
32}
33
34#[cfg(wgpu_core)]
35impl CommandBuffer {
36    /// Create a new command buffer of wgpu from a wgpu-core command buffer.
37    ///
38    /// # Arguments
39    ///
40    /// - `core_buffer` - wgpu-core command buffer.
41    pub fn from_core(core_buffer: alloc::sync::Arc<wgc::command::CommandBuffer>) -> Self {
42        Self {
43            buffer: crate::backend::wgpu_core::CoreCommandBuffer::from_core(core_buffer).into(),
44            actions: SharedDeferredCommandBufferActions::default(),
45        }
46    }
47
48    /// Returns the underlying wgpu-core command buffer if this `CommandBuffer` is on the wgpu-core backend, otherwise `None`.
49    pub fn as_core(&self) -> Option<alloc::sync::Arc<wgc::command::CommandBuffer>> {
50        self.buffer.as_core_opt().map(|cb| cb.as_core())
51    }
52}