Skip to main content

CommandEncoder

Trait CommandEncoder 

Source
pub trait CommandEncoder: Debug + HalThreadSafe {
Show 34 methods // Required methods fn begin_debug_label(&mut self, label: &str); fn end_debug_label(&mut self); fn insert_debug_marker(&mut self, label: &str); fn pipeline_barrier(&mut self, barriers: &Barriers<'_>); fn copy_buffer_to_buffer(&mut self, copy: &BufferCopy); fn copy_buffer_to_image(&mut self, copy: &BufferImageCopy); fn copy_image_to_buffer(&mut self, copy: &BufferImageCopy); fn copy_image_to_image(&mut self, copy: &ImageCopy); fn clear_buffer(&mut self, buffer: BufferHandle, offset: u64, size: u64); fn begin_render_pass(&mut self, desc: &RenderPassDesc<'_>); fn end_render_pass(&mut self); fn set_viewport(&mut self, viewport: &Viewport); fn set_scissor(&mut self, rect: &Rect2d); fn set_stencil_reference(&mut self, reference: u32); fn bind_graphics_pipeline(&mut self, pipeline: GraphicsPipelineHandle); fn bind_index_buffer( &mut self, buffer: BufferHandle, offset: u64, format: IndexFormat, ); fn bind_group( &mut self, slot: u32, group: BindGroupHandle, dynamic_offsets: &[u32], layout: PipelineLayoutHandle, ); fn push_constants( &mut self, stages: ShaderStages, offset: u32, data: &[u8], layout: PipelineLayoutHandle, ); fn draw(&mut self, vertices: Range<u32>, instances: Range<u32>); fn draw_indexed( &mut self, indices: Range<u32>, base_vertex: i32, instances: Range<u32>, ); fn draw_indirect(&mut self, draw: &DrawIndirect); fn draw_indexed_indirect(&mut self, draw: &DrawIndirect); fn draw_indirect_count(&mut self, draw: &DrawIndirectCount); fn draw_indexed_indirect_count(&mut self, draw: &DrawIndirectCount); fn draw_mesh_tasks(&mut self, x: u32, y: u32, z: u32); fn draw_mesh_tasks_indirect(&mut self, draw: &DrawIndirect); fn begin_compute_pass(&mut self, desc: &ComputePassDesc<'_>); fn end_compute_pass(&mut self); fn bind_compute_pipeline(&mut self, pipeline: ComputePipelineHandle); fn dispatch(&mut self, x: u32, y: u32, z: u32); fn dispatch_indirect(&mut self, args: BufferHandle, offset: u64); fn reset_query_set(&mut self, set: QuerySetHandle, range: Range<u32>); fn resolve_query_set( &mut self, set: QuerySetHandle, range: Range<u32>, dst: BufferHandle, dst_offset: u64, ); fn finish(self: Box<Self>) -> Result<CommandBufferHandle, HalError>;
}
Expand description

Records commands into a command buffer.

Obtained from Device::create_command_encoder and consumed by finish. One encoder produces one command buffer.

§Scope rules

  • Draw commands, set_viewport, set_scissor and bind_graphics_pipeline are legal only between begin_render_pass and end_render_pass.
  • dispatch and bind_compute_pipeline are legal only inside a compute pass.
  • Copies, barriers and query resets/resolves are legal only outside any pass — Vulkan forbids them inside dynamic rendering, and the render graph places them between passes anyway. Timestamps are the exception that proves the rule: they are not a call at all, but PassTimestampWrites on the pass descriptor.
  • Passes do not nest.

A backend may assume these hold. crate::null checks them and records a validation error, which is how the graph’s unit suite catches a violation without a GPU.

§Thread safety

Send + Sync on native, through HalThreadSafe — an encoder may be moved to a worker thread and recorded there, which the P8 job system will do.

Vulkan’s external-synchronisation rule for command buffers (“two threads may not record into one”) is discharged by &mut self on every recording method, not by dropping Sync: two threads cannot hold &mut to the same encoder, and &self alone can record nothing. An earlier version of this doc claimed Send but not Sync, which contradicted the trait’s own bound — every implementor has always had to be Sync.

On wasm32 the bound is vacuous, because wgpu’s web types are !Send and the browser is single-threaded.

Required Methods§

Source

fn begin_debug_label(&mut self, label: &str)

Opens a named region in capture tools and profilers.

Accepted and dropped when Features::DEBUG_MARKERS is absent — debug instrumentation degrades, it does not break the build (topic 10’s stated policy).

Source

fn end_debug_label(&mut self)

Closes the most recent begin_debug_label.

Source

fn insert_debug_marker(&mut self, label: &str)

Inserts a zero-width marker.

Source

fn pipeline_barrier(&mut self, barriers: &Barriers<'_>)

Records a batch of state transitions.

The only way to synchronise inside a command buffer. See the module docs for the division of labour: this seam expresses barriers, the render graph decides where they go.

Source

fn copy_buffer_to_buffer(&mut self, copy: &BufferCopy)

Buffer-to-buffer copy.

Source

fn copy_buffer_to_image(&mut self, copy: &BufferImageCopy)

Buffer-to-image copy — the texture upload path.

Source

fn copy_image_to_buffer(&mut self, copy: &BufferImageCopy)

Image-to-buffer copy — the readback path (crcbl screenshot, the delayed debug ring).

Source

fn copy_image_to_image(&mut self, copy: &ImageCopy)

Image-to-image copy.

Source

fn clear_buffer(&mut self, buffer: BufferHandle, offset: u64, size: u64)

Zeroes a buffer range.

Zero and nothing else, which is why it is a clear rather than a fill. This took a value: u32 until 2026-08-19 and promised “a repeating 32-bit value”, which two of the four backends could never keep: Metal’s fillBuffer:range:value: repeats a byte, so only a word with four equal bytes has an encoding, and WebGPU offers clearBuffer and no valued fill at all. A parameter three backends had to refuse most values of is not a seam, and Capability::BufferFillRepeatedByte and BufferFillWord existed only to describe how badly each one could keep the promise.

The name follows what the backends call it — clearBuffer on WebGPU, clear_buffer on wgpu — rather than what one of them could do.

Zeroing an indirect count at the top of a frame is the use this was written for, and it is not what the renderer does: a clear is legal only outside a pass, and a render-graph frame is passes end to end. See crcbl_shaders::clear_counters, which zeroes from inside the graph and is what let those counters become device-local.

Source

fn begin_render_pass(&mut self, desc: &RenderPassDesc<'_>)

Opens a render pass. Attachments must already be in the right state.

Source

fn end_render_pass(&mut self)

Closes the current render pass.

Source

fn set_viewport(&mut self, viewport: &Viewport)

Sets the viewport transform. See Viewport on the depth range and reversed-Z.

Source

fn set_scissor(&mut self, rect: &Rect2d)

Sets the scissor rectangle.

Source

fn set_stencil_reference(&mut self, reference: u32)

Sets the stencil reference value for subsequent draws — the value each StencilFaceState::compare tests against and StencilOp::Replace writes.

The only channel there is. StencilState carries no reference of its own, because two of the APIs behind this seam have no pipeline field to put one in. The rule every backend implements, and which Capability::StencilReference stands for:

  • The reference is pass state: a pass opens holding stencil::INITIAL_REFERENCE and keeps whatever this call last set until it ends.
  • Binding a graphics pipeline does not disturb it. A call made before a bind is still in force for the draws after it.
Source

fn bind_graphics_pipeline(&mut self, pipeline: GraphicsPipelineHandle)

Binds a graphics pipeline.

Source

fn bind_index_buffer( &mut self, buffer: BufferHandle, offset: u64, format: IndexFormat, )

Binds an index buffer.

There is no bind_vertex_buffer: geometry is pulled from storage buffers. See crate::pipeline.

Source

fn bind_group( &mut self, slot: u32, group: BindGroupHandle, dynamic_offsets: &[u32], layout: PipelineLayoutHandle, )

Binds a bind group at slot, with dynamic offsets for any dynamic-offset bindings in its layout, in binding order.

dynamic_offsets is the substitute for push constants on a device without them — see BindingKind::UniformBuffer.

Source

fn push_constants( &mut self, stages: ShaderStages, offset: u32, data: &[u8], layout: PipelineLayoutHandle, )

Writes push constants.

Requires Features::PUSH_CONSTANTS. A backend without it must fail loudly at pipeline-layout creation rather than silently dropping the write here.

Source

fn draw(&mut self, vertices: Range<u32>, instances: Range<u32>)

Non-indexed draw. For full-screen triangles and bring-up; the geometry path is indirect.

Source

fn draw_indexed( &mut self, indices: Range<u32>, base_vertex: i32, instances: Range<u32>, )

Indexed draw.

Source

fn draw_indirect(&mut self, draw: &DrawIndirect)

Indirect non-indexed draw with a CPU-known draw count.

Source

fn draw_indexed_indirect(&mut self, draw: &DrawIndirect)

Indirect indexed draw with a CPU-known draw count. GeometryPath::IndirectPerBatch’s draw path, one call per bucket.

Source

fn draw_indirect_count(&mut self, draw: &DrawIndirectCount)

Indirect non-indexed draw with a GPU-read draw count.

Source

fn draw_indexed_indirect_count(&mut self, draw: &DrawIndirectCount)

Indirect indexed draw with a GPU-read draw count. GeometryPath::IndirectCount’s steady-state draw call — one per pass, regardless of scene size.

Source

fn draw_mesh_tasks(&mut self, x: u32, y: u32, z: u32)

Launches a mesh-shading workload, in workgroups — GeometryPath::MeshShader’s draw call.

x, y and z count workgroups of the first stage the bound pipeline has: the task stage when MeshPipelineDesc::task was Some, and the mesh stage otherwise. That is Vulkan’s rule for vkCmdDrawMeshTasksEXT, D3D12’s for DispatchMesh, and Metal’s for drawMeshThreadgroups:, so it is the seam’s rule too.

This is a draw, not a dispatch: it belongs inside a render pass with a mesh pipeline bound, and its output is fragments rather than buffer writes. The three-dimensional count is why it does not reuse draw’s vertex/instance ranges — there are no vertices to range over.

Requires Features::MESH_SHADER, which create_mesh_pipeline has already refused without.

Source

fn draw_mesh_tasks_indirect(&mut self, draw: &DrawIndirect)

draw_mesh_tasks with the workgroup counts read from GPU memory — the shape a culling pass’s output needs to reduce work rather than only output.

§The argument layout

One argument structure is three consecutive 32-bit words: the x, y and z workgroup counts, in that order, of the same stage draw_mesh_tasks counts groups of. The layout is not this seam’s to choose — it is VkDrawMeshTasksIndirectCommandEXT, D3D12_DISPATCH_MESH_ARGUMENTS and Metal’s MTLDispatchThreadgroupsIndirectArguments, which are the same three words in the same order, because a driver reads these bytes directly.

DrawIndirect::stride is therefore 12 for a tightly packed buffer, and DrawIndirect::offset must be a multiple of 4. draw_count > 1 requires Features::MULTI_DRAW_INDIRECT, exactly as it does for the indexed form.

DrawIndirect::args must be in ResourceState::IndirectArgument — a buffer the same pass also reads as a storage buffer is in one state or the other, never both, so a caller that needs both wants two buffers.

§Capability

Requires Features::MESH_SHADER and nothing more. There is no separate flag because none of the three APIs has one: VK_EXT_mesh_shader defines vkCmdDrawMeshTasksIndirectEXT alongside vkCmdDrawMeshTasksEXT, D3D12 mesh shader tier 1 admits D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH_MESH in a command signature, and a Metal device with mesh functions has drawMeshThreadgroupsWithIndirectBuffer:. A device that can launch this stage at all can launch it from memory.

Source

fn begin_compute_pass(&mut self, desc: &ComputePassDesc<'_>)

Opens a compute pass.

Source

fn end_compute_pass(&mut self)

Closes the current compute pass.

Source

fn bind_compute_pipeline(&mut self, pipeline: ComputePipelineHandle)

Binds a compute pipeline.

Source

fn dispatch(&mut self, x: u32, y: u32, z: u32)

Dispatches a compute workload, in workgroups.

Source

fn dispatch_indirect(&mut self, args: BufferHandle, offset: u64)

Dispatches with the workgroup counts read from GPU memory — the shape a variable-size culling or compaction pass needs.

Source

fn reset_query_set(&mut self, set: QuerySetHandle, range: Range<u32>)

Resets a query range so it can be written again.

Required on Vulkan before every write; a no-op on backends that reset implicitly. Callers always call it, so the Vulkan path is never the special case.

What is written between a reset and a resolve is RenderPassDesc::timestamp_writes, not a call of its own: see PassTimestampWrites for why the seam has no free-standing timestamp.

Source

fn resolve_query_set( &mut self, set: QuerySetHandle, range: Range<u32>, dst: BufferHandle, dst_offset: u64, )

Copies query results into a buffer, as the device wrote them.

The counterpart of Device::query_results and the one place the two read paths differ: that one reports a QueryKind::Timestamp in nanoseconds, and this one cannot. It records a GPU-side copy — vkCmdCopyQueryPoolResults, ResolveQueryData, GPUCommandEncoder.resolveQuerySet — so the values never pass through the CPU and there is nowhere to apply a scale. A caller reading the destination gets the device’s own clock units, and what those are worth is a question only the backend can answer.

So this is for feeding a delta back to the GPU — a shader that compares two readings, which needs no unit — and Device::query_results is for a number a human or a HUD is going to read.

Source

fn finish(self: Box<Self>) -> Result<CommandBufferHandle, HalError>

Ends recording and produces a submittable command buffer.

Takes Box<Self> rather than self so the method stays object-safe: Box<dyn CommandEncoder> can call it, a bare self receiver could not.

§Errors

Returns HalError if the backend rejected the recorded stream — an unclosed pass, an out-of-range value, or an out-of-memory condition in the command pool.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§