Skip to content

Commit 33d313c

Browse files
authored
Document some wgpu-core resource tracking types (#2960)
1 parent 6e99cd3 commit 33d313c

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

wgpu-core/src/device/queue.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ pub struct WrappedSubmissionIndex {
8686
pub index: SubmissionIndex,
8787
}
8888

89+
/// A texture or buffer to be freed soon.
90+
///
91+
/// This is just a tagged raw texture or buffer, generally about to be added to
92+
/// some other more specific container like:
93+
///
94+
/// - `PendingWrites::temp_resources`: resources used by queue writes and
95+
/// unmaps, waiting to be folded in with the next queue submission
96+
///
97+
/// - `ActiveSubmission::last_resources`: temporary resources used by a queue
98+
/// submission, to be freed when it completes
99+
///
100+
/// - `LifetimeTracker::free_resources`: resources to be freed in the next
101+
/// `maintain` call, no longer used anywhere
89102
#[derive(Debug)]
90103
pub enum TempResource<A: hal::Api> {
91104
Buffer(A::Buffer),
@@ -105,6 +118,19 @@ impl<A: hal::Api> EncoderInFlight<A> {
105118
}
106119
}
107120

121+
/// Writes made directly on the device or queue, not as part of a wgpu command buffer.
122+
///
123+
/// Operations like `buffer_unmap`, `queue_write_buffer`, and
124+
/// `queue_write_texture` need to copy data to the GPU. This must be
125+
/// done by encoding and submitting commands at the hal level, but these
126+
/// operations are not associated with any specific wgpu command buffer.
127+
///
128+
/// Instead, `Device::pending_writes` owns one of these values, which has its
129+
/// own hal command encoder and resource lists. The commands accumulated here
130+
/// are automatically submitted to the queue the next time the user submits a
131+
/// wgpu command buffer, ahead of the user's commands.
132+
///
133+
/// All uses of [`StagingBuffer`]s end up here.
108134
#[derive(Debug)]
109135
pub(crate) struct PendingWrites<A: hal::Api> {
110136
pub command_encoder: A::CommandEncoder,
@@ -1082,6 +1108,8 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
10821108
Err(WaitIdleError::WrongSubmissionIndex(..)) => unreachable!(),
10831109
};
10841110

1111+
// pending_write_resources has been drained, so it's empty, but we
1112+
// want to retain its heap allocation.
10851113
device.pending_writes.temp_resources = pending_write_resources;
10861114
device.temp_suspected.clear();
10871115
device.lock_life(&mut token).post_submit();

wgpu-core/src/resource.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,25 @@ impl<A: hal::Api> Resource for Buffer<A> {
224224
}
225225
}
226226

227+
/// A temporary buffer, consumed by the command that uses it.
228+
///
229+
/// A [`StagingBuffer`] is designed for one-shot uploads of data to the GPU. It
230+
/// is always created mapped, and the command that uses it destroys the buffer
231+
/// when it is done.
232+
///
233+
/// [`StagingBuffer`]s can be created with [`queue_create_staging_buffer`] and
234+
/// used with [`queue_write_staging_buffer`]. They are also used internally by
235+
/// operations like [`queue_write_texture`] that need to upload data to the GPU,
236+
/// but that don't belong to any particular wgpu command buffer.
237+
///
238+
/// Used `StagingBuffer`s are accumulated in [`Device::pending_writes`], to be
239+
/// freed once their associated operation's queue submission has finished
240+
/// execution.
241+
///
242+
/// [`queue_create_staging_buffer`]: Global::queue_create_staging_buffer
243+
/// [`queue_write_staging_buffer`]: Global::queue_write_staging_buffer
244+
/// [`queue_write_texture`]: Global::queue_write_texture
245+
/// [`Device::pending_writes`]: crate::device::Device
227246
pub struct StagingBuffer<A: hal::Api> {
228247
pub(crate) raw: A::Buffer,
229248
pub(crate) size: wgt::BufferAddress,

0 commit comments

Comments
 (0)