Skip to content

Commit c5b8cac

Browse files
committed
Add more overview documentation to Buffer and usages.
1 parent e7cdfc4 commit c5b8cac

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

wgpu-types/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5151,6 +5151,10 @@ bitflags::bitflags! {
51515151
/// The usages determine what kind of memory the buffer is allocated from and what
51525152
/// actions the buffer can partake in.
51535153
///
5154+
/// Specifying only usages the application will actually perform may increase performance.
5155+
/// Additionally, on the WebGL backend, there are restrictions on [`BufferUsages::INDEX`];
5156+
/// see [`DownlevelFlags::UNRESTRICTED_INDEX_BUFFER`] for more information.
5157+
///
51545158
/// Corresponds to [WebGPU `GPUBufferUsageFlags`](
51555159
/// https://gpuweb.github.io/gpuweb/#typedefdef-gpubufferusageflags).
51565160
#[repr(transparent)]
@@ -5269,6 +5273,10 @@ pub struct BufferDescriptor<L> {
52695273
pub size: BufferAddress,
52705274
/// Usages of a buffer. If the buffer is used in any way that isn't specified here, the operation
52715275
/// will panic.
5276+
///
5277+
/// Specifying only usages the application will actually perform may increase performance.
5278+
/// Additionally, on the WebGL backend, there are restrictions on [`BufferUsages::INDEX`];
5279+
/// see [`DownlevelFlags::UNRESTRICTED_INDEX_BUFFER`] for more information.
52725280
pub usage: BufferUsages,
52735281
/// Allows a buffer to be mapped immediately after they are made. It does not have to be [`BufferUsages::MAP_READ`] or
52745282
/// [`BufferUsages::MAP_WRITE`], all buffers are allowed to be mapped at creation.

wgpu/src/api/buffer.rs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,63 @@ use crate::*;
99

1010
/// Handle to a GPU-accessible buffer.
1111
///
12-
/// Created with [`Device::create_buffer`] or
13-
/// [`DeviceExt::create_buffer_init`](util::DeviceExt::create_buffer_init).
14-
///
15-
/// Corresponds to [WebGPU `GPUBuffer`](https://gpuweb.github.io/gpuweb/#buffer-interface).
12+
/// A `Buffer` is a memory allocation for use by the GPU, somewhat analogous to
13+
/// <code>[Box]&lt;[\[u8\]][primitive@slice]&gt;</code> in Rust.
14+
/// The contents of buffers are untyped bytes; it is up to the application to
15+
/// specify the interpretation of the bytes when the buffer is used, in ways
16+
/// such as [`VertexBufferLayout`].
17+
/// A single buffer can be used to hold multiple independent pieces of data at
18+
/// different offsets (e.g. both vertices and indices for one or more meshes).
1619
///
1720
/// A `Buffer`'s bytes have "interior mutability": functions like
1821
/// [`Queue::write_buffer`] or [mapping] a buffer for writing only require a
1922
/// `&Buffer`, not a `&mut Buffer`, even though they modify its contents. `wgpu`
2023
/// prevents simultaneous reads and writes of buffer contents using run-time
2124
/// checks.
2225
///
26+
/// Created with [`Device::create_buffer()`] or
27+
/// [`DeviceExt::create_buffer_init()`].
28+
///
29+
/// Corresponds to [WebGPU `GPUBuffer`](https://gpuweb.github.io/gpuweb/#buffer-interface).
30+
///
2331
/// [mapping]: Buffer#mapping-buffers
2432
///
33+
/// # How to get your data into a buffer
34+
///
35+
/// Every `Buffer` starts with all bytes zeroed.
36+
/// There are many ways to load data into a `Buffer`:
37+
///
38+
/// - When creating a buffer, you may set the [`mapped_at_creation`][mac] flag,
39+
/// then write to its [`get_mapped_range_mut()`][Buffer::get_mapped_range_mut].
40+
/// This only works when the buffer is created and has not yet been used by
41+
/// the GPU, but it is all you need for buffers whose contents do not change
42+
/// after creation.
43+
/// - You may use [`DeviceExt::create_buffer_init()`] as a convenient way to
44+
/// do that and copy data from a `&[u8]` you provide.
45+
/// - After creation, you may use [`Buffer::map_async()`] to map it again;
46+
/// however, you then need to wait until the GPU is no longer using the buffer
47+
/// before you begin writing.
48+
/// - You may use [`CommandEncoder::copy_buffer_to_buffer()`] to copy data into
49+
/// this buffer from another buffer.
50+
/// - You may use [`Queue::write_buffer()`] to copy data into the buffer from a
51+
/// `&[u8]`. This uses a temporary “staging” buffer managed by `wgpu` to hold
52+
/// the data.
53+
/// - [`Queue::write_buffer_with()`] allows you to write directly into temporary
54+
/// storage instead of providing a slice you already prepared, which may
55+
/// allow *your* code to save the allocation of a [`Vec`] or such.
56+
/// - You may use [`util::StagingBelt`] to manage a set of temporary buffers.
57+
/// This may be more efficient than [`Queue::write_buffer_with()`] when you
58+
/// have many small copies to perform, but requires more steps to use, and
59+
/// tuning of the belt buffer size.
60+
/// - You may write your own staging buffer management customized to your
61+
/// application, based on mapped buffers and
62+
/// [`CommandEncoder::copy_buffer_to_buffer()`].
63+
/// - A GPU computation’s results can be stored in a buffer:
64+
/// - A [compute shader][ComputePipeline] may write to a buffer bound as a
65+
/// [storage buffer][BufferBindingType::Storage].
66+
/// - A render pass may render to a texture which is then copied to a buffer
67+
/// using [`CommandEncoder::copy_texture_to_buffer()`].
68+
///
2569
/// # Mapping buffers
2670
///
2771
/// If a `Buffer` is created with the appropriate [`usage`], it can be *mapped*:
@@ -172,6 +216,7 @@ use crate::*;
172216
/// [mac]: BufferDescriptor::mapped_at_creation
173217
/// [`MAP_READ`]: BufferUsages::MAP_READ
174218
/// [`MAP_WRITE`]: BufferUsages::MAP_WRITE
219+
/// [`DeviceExt::create_buffer_init()`]: util::DeviceExt::create_buffer_init
175220
#[derive(Debug, Clone)]
176221
pub struct Buffer {
177222
pub(crate) inner: dispatch::DispatchBuffer,

0 commit comments

Comments
 (0)