Skip to content

Commit 9c0fa29

Browse files
authored
Use BufferSlice in StagingBelt::allocate(). (#7179)
* Use `BufferSlice` in `StagingBelt::allocate()`. * Make `StagingBelt::allocate()` return just the slice.
1 parent 14bb855 commit 9c0fa29

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

wgpu/src/util/belt.rs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
util::align_to, Buffer, BufferAddress, BufferDescriptor, BufferSize, BufferUsages,
2+
util::align_to, Buffer, BufferAddress, BufferDescriptor, BufferSize, BufferSlice, BufferUsages,
33
BufferViewMut, CommandEncoder, Device, MapMode,
44
};
55
use std::fmt;
@@ -77,50 +77,46 @@ impl StagingBelt {
7777
size: BufferSize,
7878
device: &Device,
7979
) -> BufferViewMut<'_> {
80-
let (mapped, belt_buffer, offset_in_belt_buffer) = self.allocate(
80+
let slice_of_belt = self.allocate(
8181
size,
8282
const { BufferSize::new(crate::COPY_BUFFER_ALIGNMENT).unwrap() },
8383
device,
8484
);
8585
encoder.copy_buffer_to_buffer(
86-
belt_buffer,
87-
offset_in_belt_buffer,
86+
slice_of_belt.buffer(),
87+
slice_of_belt.offset(),
8888
target,
8989
offset,
9090
size.get(),
9191
);
92-
mapped
92+
slice_of_belt.get_mapped_range_mut()
9393
}
9494

9595
/// Allocate a staging belt slice with the given `size` and `alignment` and return it.
9696
///
97-
/// This allows you to do whatever you want with the slice after, such as
98-
/// copying it to a texture or executing a compute shader that reads it, whereas
99-
/// [`StagingBelt::write_buffer()`] can only write to other buffers.
97+
/// To use this slice, call [`BufferSlice::get_mapped_range_mut()`] and write your data into
98+
/// that [`BufferViewMut`].
99+
/// (The view must be dropped before [`StagingBelt::finish()`] is called.)
100+
///
101+
/// You can then record your own GPU commands to perform with the slice,
102+
/// such as copying it to a texture or executing a compute shader that reads it (whereas
103+
/// [`StagingBelt::write_buffer()`] can only write to other buffers).
104+
/// All commands involving this slice must be submitted after
105+
/// [`StagingBelt::finish()`] is called and before [`StagingBelt::recall()`] is called.
100106
///
101107
/// If the `size` is greater than the space available in any free internal buffer, a new buffer
102108
/// will be allocated for it. Therefore, the `chunk_size` passed to [`StagingBelt::new()`]
103109
/// should ideally be larger than every such size.
104110
///
105111
/// The chosen slice will be positioned within the buffer at a multiple of `alignment`,
106112
/// which may be used to meet alignment requirements for the operation you wish to perform
107-
/// with the slice. This does not necessarily affect the alignment of the mapping.
108-
///
109-
/// Three values are returned:
110-
///
111-
/// * The mapped buffer view which you should write into from the CPU side (Rust code).
112-
/// * The buffer containing the slice, for you to use in GPU commands.
113-
/// All commands involving this slice must be submitted after
114-
/// [`StagingBelt::finish()`] is called and before [`StagingBelt::recall()`] is called.
115-
/// * The offset within the buffer at which the slice starts.
116-
/// This offset should be used for all GPU commands, and should not be used with
117-
/// the mapped buffer view.
113+
/// with the slice. This does not necessarily affect the alignment of the [`BufferViewMut`].
118114
pub fn allocate(
119115
&mut self,
120116
size: BufferSize,
121117
alignment: BufferSize,
122118
device: &Device,
123-
) -> (BufferViewMut<'_>, &Buffer, BufferAddress) {
119+
) -> BufferSlice<'_> {
124120
assert!(
125121
alignment.get().is_power_of_two(),
126122
"alignment must be a power of two, not {alignment}"
@@ -161,14 +157,9 @@ impl StagingBelt {
161157
self.active_chunks.push(chunk);
162158
let chunk = self.active_chunks.last().unwrap();
163159

164-
(
165-
chunk
166-
.buffer
167-
.slice(allocation_offset..allocation_offset + size.get())
168-
.get_mapped_range_mut(),
169-
&chunk.buffer,
170-
allocation_offset,
171-
)
160+
chunk
161+
.buffer
162+
.slice(allocation_offset..allocation_offset + size.get())
172163
}
173164

174165
/// Prepare currently mapped buffers for use in a submission.

0 commit comments

Comments
 (0)