Skip to content

Commit 60487f5

Browse files
committed
Document the wgpu_hal::CommandEncoder trait.
1 parent 665c075 commit 60487f5

File tree

1 file changed

+87
-8
lines changed

1 file changed

+87
-8
lines changed

wgpu-hal/src/lib.rs

Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ pub trait Device<A: Api>: WasmNotSendSync {
329329
unsafe fn create_sampler(&self, desc: &SamplerDescriptor) -> Result<A::Sampler, DeviceError>;
330330
unsafe fn destroy_sampler(&self, sampler: A::Sampler);
331331

332+
/// Create a fresh [`CommandEncoder`].
333+
///
334+
/// The new `CommandEncoder` is in the "closed" state.
332335
unsafe fn create_command_encoder(
333336
&self,
334337
desc: &CommandEncoderDescriptor<A>,
@@ -429,19 +432,95 @@ pub trait Queue<A: Api>: WasmNotSendSync {
429432
unsafe fn get_timestamp_period(&self) -> f32;
430433
}
431434

432-
/// Encoder for commands in command buffers.
433-
/// Serves as a parent for all the encoded command buffers.
434-
/// Works in bursts of action: one or more command buffers are recorded,
435-
/// then submitted to a queue, and then it needs to be `reset_all()`.
435+
/// Encoder and allocation pool for `CommandBuffer`.
436+
///
437+
/// The life cycle of a `CommandBuffer` is as follows:
438+
///
439+
/// - Call [`Device::create_command_encoder`] to create a new
440+
/// `CommandEncoder`, in the "closed" state.
441+
///
442+
/// - Call `begin_encoding` on a closed `CommandEncoder` to begin
443+
/// recording commands. This puts the `CommandEncoder` in the
444+
/// "recording" state.
445+
///
446+
/// - Call methods like `copy_buffer_to_buffer`, `begin_render_pass`,
447+
/// etc. on a "recording" `CommandEncoder` to add commands to the
448+
/// list.
449+
///
450+
/// - Call `end_encoding` on a recording `CommandEncoder` to close the
451+
/// encoder and construct a fresh `CommandBuffer` consisting of the
452+
/// list of commands recorded up to that point.
453+
///
454+
/// - Call `discard_encoding` on a recording `CommandEncoder` to drop
455+
/// the commands recorded thus far and close the encoder.
456+
///
457+
/// - Call `reset_all` on a closed `CommandEncoder`, passing all the
458+
/// live `CommandBuffers` built from it. All the `CommandBuffer`s
459+
/// are destroyed, and their resources are freed.
460+
///
461+
/// # Safety
462+
///
463+
/// - The `CommandEncoder` must be in the states described above to
464+
/// make the given calls.
465+
///
466+
/// - A `CommandBuffer` that has been submitted for execution on the
467+
/// GPU must live until its execution is complete.
468+
///
469+
/// - A `CommandBuffer` must not outlive the `CommandEncoder` that
470+
/// built it.
471+
///
472+
/// - A `CommandEncoder` must not outlive its `Device`.
436473
pub trait CommandEncoder<A: Api>: WasmNotSendSync + fmt::Debug {
437474
/// Begin encoding a new command buffer.
475+
///
476+
/// This puts this `CommandEncoder` in the "recording" state.
477+
///
478+
/// # Safety
479+
///
480+
/// This `CommandEncoder` must be in the "closed" state.
438481
unsafe fn begin_encoding(&mut self, label: Label) -> Result<(), DeviceError>;
439-
/// Discard currently recorded list, if any.
482+
483+
/// Discard the command list under construction, if any.
484+
///
485+
/// This puts this `CommandEncoder` in the "closed" state.
486+
///
487+
/// # Safety
488+
///
489+
/// This `CommandEncoder` must be in the "recording" state.
440490
unsafe fn discard_encoding(&mut self);
491+
492+
/// Return a fresh [`CommandBuffer`] holding the recorded commands.
493+
///
494+
/// The returned [`CommandBuffer`] holds all the commands recorded
495+
/// on this `CommandEncoder` since the last call to
496+
/// [`begin_encoding`].
497+
///
498+
/// This puts this `CommandEncoder` in the "closed" state.
499+
///
500+
/// # Safety
501+
///
502+
/// This `CommandEncoder` must be in the "recording" state.
503+
///
504+
/// The returned [`CommandBuffer`] must not outlive this
505+
/// `CommandEncoder`. Implementations are allowed to build
506+
/// `CommandBuffer`s that depend on storage owned by this
507+
/// `CommandEncoder`.
508+
///
509+
/// [`CommandBuffer`]: Api::CommandBuffer
510+
/// [`begin_encoding`]: CommandEncoder::begin_encoding
441511
unsafe fn end_encoding(&mut self) -> Result<A::CommandBuffer, DeviceError>;
442-
/// Reclaims all resources that are allocated for this encoder.
443-
/// Must get all of the produced command buffers back,
444-
/// and they must not be used by GPU at this moment.
512+
513+
/// Reclaim all resources belonging to this `CommandEncoder`.
514+
///
515+
/// # Safety
516+
///
517+
/// This `CommandEncoder` must be in the "closed" state.
518+
///
519+
/// The `command_buffers` iterator must produce all the live
520+
/// [`CommandBuffer`]s built using this `CommandEncoder` --- that
521+
/// is, every extant `CommandBuffer` returned from `end_encoding`.
522+
///
523+
/// [`CommandBuffer`]: Api::CommandBuffer
445524
unsafe fn reset_all<I>(&mut self, command_buffers: I)
446525
where
447526
I: Iterator<Item = A::CommandBuffer>;

0 commit comments

Comments
 (0)