Skip to content

Commit fb3b33d

Browse files
committed
[wgpu] Clarify documentation for Queue transfer methods.
Explain more clearly that the `write_buffer`, `write_buffer_with`, and `write_texture` methods on `wgpu::Queue` do not immediately submit the transfers for execution. Provide sample code for flushing them.
1 parent e5201a7 commit fb3b33d

File tree

1 file changed

+54
-16
lines changed

1 file changed

+54
-16
lines changed

wgpu/src/lib.rs

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4991,11 +4991,24 @@ impl<'a> Drop for QueueWriteBufferView<'a> {
49914991
impl Queue {
49924992
/// Schedule a data write into `buffer` starting at `offset`.
49934993
///
4994-
/// This method is intended to have low performance costs.
4995-
/// As such, the write is not immediately submitted, and instead enqueued
4996-
/// internally to happen at the start of the next `submit()` call.
4997-
///
49984994
/// This method fails if `data` overruns the size of `buffer` starting at `offset`.
4995+
///
4996+
/// This does *not* submit the transfer to the GPU immediately. Calls to
4997+
/// `write_buffer` begin execution only on the next call to
4998+
/// [`Queue::submit`]. To get a set of scheduled transfers started
4999+
/// immediately, it's fine to call `submit` with no command buffers at all:
5000+
///
5001+
/// ```no_run
5002+
/// # let queue: wgpu::Queue = todo!();
5003+
/// queue.submit([]);
5004+
/// ```
5005+
///
5006+
/// However, `data` will be immediately copied into staging memory, so the
5007+
/// caller may discard it any time after this call completes.
5008+
///
5009+
/// If possible, consider using [`Queue::write_buffer_with`] instead. That
5010+
/// method avoids an intermediate copy and is often able to transfer data
5011+
/// more efficiently than this one.
49995012
pub fn write_buffer(&self, buffer: &Buffer, offset: BufferAddress, data: &[u8]) {
50005013
DynContext::queue_write_buffer(
50015014
&*self.context,
@@ -5008,14 +5021,32 @@ impl Queue {
50085021
)
50095022
}
50105023

5011-
/// Schedule a data write into `buffer` starting at `offset` via the returned
5012-
/// [`QueueWriteBufferView`].
5024+
/// Write to a buffer via a directly mapped staging buffer.
5025+
///
5026+
/// Return a [`QueueWriteBufferView`] which, when dropped, schedules a copy
5027+
/// of its contents into `buffer` at `offset`. The returned view
5028+
/// dereferences to a `size`-byte long `&mut [u8]`, in which you should
5029+
/// store the data you would like written to `buffer`.
5030+
///
5031+
/// This method may perform transfers faster than [`Queue::write_buffer`],
5032+
/// because the returned [`QueueWriteBufferView`] is actually the staging
5033+
/// buffer for the write, mapped into the caller's address space. Writing
5034+
/// your data directly into this staging buffer avoids the temporary
5035+
/// CPU-side buffer needed by `write_buffer`.
50135036
///
5014-
/// Reading from this buffer is slow and will not yield the actual contents of the buffer.
5037+
/// Reading from the returned view is slow, and will not yield the current
5038+
/// contents of `buffer`.
50155039
///
5016-
/// This method is intended to have low performance costs.
5017-
/// As such, the write is not immediately submitted, and instead enqueued
5018-
/// internally to happen at the start of the next `submit()` call.
5040+
/// Note that dropping the [`QueueWriteBufferView`] does *not* submit the
5041+
/// transfer to the GPU immediately. The transfer begins only on the next
5042+
/// call to [`Queue::submit`] after the view is dropped. To get a set of
5043+
/// scheduled transfers started immediately, it's fine to call `submit` with
5044+
/// no command buffers at all:
5045+
///
5046+
/// ```no_run
5047+
/// # let queue: wgpu::Queue = todo!();
5048+
/// queue.submit([]);
5049+
/// ```
50195050
///
50205051
/// This method fails if `size` is greater than the size of `buffer` starting at `offset`.
50215052
#[must_use]
@@ -5059,13 +5090,20 @@ impl Queue {
50595090
/// texture (coordinate offset, mip level) that will be overwritten.
50605091
/// * `size` is the size, in texels, of the region to be written.
50615092
///
5062-
/// This method is intended to have low performance costs.
5063-
/// As such, the write is not immediately submitted, and instead enqueued
5064-
/// internally to happen at the start of the next `submit()` call.
5065-
/// However, `data` will be immediately copied into staging memory; so the caller may
5066-
/// discard it any time after this call completes.
5067-
///
50685093
/// This method fails if `size` overruns the size of `texture`, or if `data` is too short.
5094+
///
5095+
/// This does *not* submit the transfer to the GPU immediately. Calls to
5096+
/// `write_texture` begin execution only on the next call to
5097+
/// [`Queue::submit`]. To get a set of scheduled transfers started
5098+
/// immediately, it's fine to call `submit` with no command buffers at all:
5099+
///
5100+
/// ```no_run
5101+
/// # let queue: wgpu::Queue = todo!();
5102+
/// queue.submit([]);
5103+
/// ```
5104+
///
5105+
/// However, `data` will be immediately copied into staging memory, so the
5106+
/// caller may discard it any time after this call completes.
50695107
pub fn write_texture(
50705108
&self,
50715109
texture: ImageCopyTexture<'_>,

0 commit comments

Comments
 (0)