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