Skip to content

Commit 2bdf4be

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

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-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: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,62 @@ 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+
/// `Box<[u8]>` in Rust. The contents of buffers are untyped bytes;
14+
/// it is up to the application to specify the interpretation of the bytes when
15+
/// the buffer is used, in ways such as [`VertexBufferLayout`].
16+
/// A single buffer can be used to hold multiple independent pieces of data at
17+
/// different offsets (e.g. both vertices and indices for one or more meshes).
1618
///
1719
/// A `Buffer`'s bytes have "interior mutability": functions like
1820
/// [`Queue::write_buffer`] or [mapping] a buffer for writing only require a
1921
/// `&Buffer`, not a `&mut Buffer`, even though they modify its contents. `wgpu`
2022
/// prevents simultaneous reads and writes of buffer contents using run-time
2123
/// checks.
2224
///
25+
/// Created with [`Device::create_buffer`] or
26+
/// [`DeviceExt::create_buffer_init`].
27+
///
28+
/// Corresponds to [WebGPU `GPUBuffer`](https://gpuweb.github.io/gpuweb/#buffer-interface).
29+
///
2330
/// [mapping]: Buffer#mapping-buffers
2431
///
32+
/// # How to get your data into a buffer
33+
///
34+
/// Every `Buffer` starts with all bytes zero.
35+
/// There are many ways to load data into a `Buffer`:
36+
///
37+
/// * When creating a buffer, you may set the [`mapped_at_creation`][mac] flag,
38+
/// then write to its [`get_mapped_range_mut()`][Buffer::get_mapped_range_mut].
39+
/// This only works when the buffer is created and has not yet been used by
40+
/// the GPU, but it is all you need for buffers whose contents do not change
41+
/// after creation.
42+
/// * You may use [`DeviceExt::create_buffer_init`] as a convenient way to do
43+
/// that and copy data from a `&[u8]` you provide.
44+
/// * After creation, you may use [`Buffer::map_async()`] to map it again;
45+
/// however, you then need to wait until the GPU is no longer using the buffer
46+
/// before you begin writing.
47+
/// * You may use [`CommandEncoder::copy_buffer_to_buffer()`] to copy data into
48+
/// this buffer from another buffer.
49+
/// * You may use [`Queue::write_buffer()`] to copy data into the buffer from a
50+
/// `&[u8]`. This uses a temporary “staging” buffer managed by `wgpu` to hold
51+
/// the data.
52+
/// * [`Queue::write_buffer_with()`] allows you to write directly into temporary
53+
/// storage instead of providing a slice you already prepared, which may
54+
/// allow *your* code to save the allocation of a [`Vec`] or such.
55+
/// * You may use [`util::StagingBelt`] to manage a set of temporary buffers.
56+
/// This may be more efficient than [`Queue::write_buffer_with()`] when you
57+
/// have many small copies to perform, but requires more steps to use, and
58+
/// tuning of the belt buffer size.
59+
/// * You may write your own staging buffer management customized to your
60+
/// application, based on mapped buffers and
61+
/// [`CommandEncoder::copy_buffer_to_buffer()`].
62+
/// * A GPU computation’s results can be stored in a buffer:
63+
/// * A [compute shader][ComputePipeline] may write to a buffer bound as a
64+
/// [storage buffer][BufferBindingType::Storage].
65+
/// * A render pass may render to a texture which is then copied to a buffer
66+
/// using [`CommandEncoder::copy_texture_to_buffer()`].
67+
///
2568
/// # Mapping buffers
2669
///
2770
/// If a `Buffer` is created with the appropriate [`usage`], it can be *mapped*:
@@ -172,6 +215,7 @@ use crate::*;
172215
/// [mac]: BufferDescriptor::mapped_at_creation
173216
/// [`MAP_READ`]: BufferUsages::MAP_READ
174217
/// [`MAP_WRITE`]: BufferUsages::MAP_WRITE
218+
/// [`DeviceExt::create_buffer_init`]: util::DeviceExt::create_buffer_init
175219
#[derive(Debug, Clone)]
176220
pub struct Buffer {
177221
pub(crate) inner: dispatch::DispatchBuffer,

0 commit comments

Comments
 (0)