@@ -9,19 +9,62 @@ use crate::*;
9
9
10
10
/// Handle to a GPU-accessible buffer.
11
11
///
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).
16
18
///
17
19
/// A `Buffer`'s bytes have "interior mutability": functions like
18
20
/// [`Queue::write_buffer`] or [mapping] a buffer for writing only require a
19
21
/// `&Buffer`, not a `&mut Buffer`, even though they modify its contents. `wgpu`
20
22
/// prevents simultaneous reads and writes of buffer contents using run-time
21
23
/// checks.
22
24
///
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
+ ///
23
30
/// [mapping]: Buffer#mapping-buffers
24
31
///
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
+ ///
25
68
/// # Mapping buffers
26
69
///
27
70
/// If a `Buffer` is created with the appropriate [`usage`], it can be *mapped*:
@@ -172,6 +215,7 @@ use crate::*;
172
215
/// [mac]: BufferDescriptor::mapped_at_creation
173
216
/// [`MAP_READ`]: BufferUsages::MAP_READ
174
217
/// [`MAP_WRITE`]: BufferUsages::MAP_WRITE
218
+ /// [`DeviceExt::create_buffer_init`]: util::DeviceExt::create_buffer_init
175
219
#[ derive( Debug , Clone ) ]
176
220
pub struct Buffer {
177
221
pub ( crate ) inner : dispatch:: DispatchBuffer ,
0 commit comments