|
1 | 1 | use crate::{
|
2 |
| - util::align_to, Buffer, BufferAddress, BufferDescriptor, BufferSize, BufferUsages, |
| 2 | + util::align_to, Buffer, BufferAddress, BufferDescriptor, BufferSize, BufferSlice, BufferUsages, |
3 | 3 | BufferViewMut, CommandEncoder, Device, MapMode,
|
4 | 4 | };
|
5 | 5 | use std::fmt;
|
@@ -77,50 +77,46 @@ impl StagingBelt {
|
77 | 77 | size: BufferSize,
|
78 | 78 | device: &Device,
|
79 | 79 | ) -> BufferViewMut<'_> {
|
80 |
| - let (mapped, belt_buffer, offset_in_belt_buffer) = self.allocate( |
| 80 | + let slice_of_belt = self.allocate( |
81 | 81 | size,
|
82 | 82 | const { BufferSize::new(crate::COPY_BUFFER_ALIGNMENT).unwrap() },
|
83 | 83 | device,
|
84 | 84 | );
|
85 | 85 | encoder.copy_buffer_to_buffer(
|
86 |
| - belt_buffer, |
87 |
| - offset_in_belt_buffer, |
| 86 | + slice_of_belt.buffer(), |
| 87 | + slice_of_belt.offset(), |
88 | 88 | target,
|
89 | 89 | offset,
|
90 | 90 | size.get(),
|
91 | 91 | );
|
92 |
| - mapped |
| 92 | + slice_of_belt.get_mapped_range_mut() |
93 | 93 | }
|
94 | 94 |
|
95 | 95 | /// Allocate a staging belt slice with the given `size` and `alignment` and return it.
|
96 | 96 | ///
|
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. |
100 | 106 | ///
|
101 | 107 | /// If the `size` is greater than the space available in any free internal buffer, a new buffer
|
102 | 108 | /// will be allocated for it. Therefore, the `chunk_size` passed to [`StagingBelt::new()`]
|
103 | 109 | /// should ideally be larger than every such size.
|
104 | 110 | ///
|
105 | 111 | /// The chosen slice will be positioned within the buffer at a multiple of `alignment`,
|
106 | 112 | /// 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`]. |
118 | 114 | pub fn allocate(
|
119 | 115 | &mut self,
|
120 | 116 | size: BufferSize,
|
121 | 117 | alignment: BufferSize,
|
122 | 118 | device: &Device,
|
123 |
| - ) -> (BufferViewMut<'_>, &Buffer, BufferAddress) { |
| 119 | + ) -> BufferSlice<'_> { |
124 | 120 | assert!(
|
125 | 121 | alignment.get().is_power_of_two(),
|
126 | 122 | "alignment must be a power of two, not {alignment}"
|
@@ -161,14 +157,9 @@ impl StagingBelt {
|
161 | 157 | self.active_chunks.push(chunk);
|
162 | 158 | let chunk = self.active_chunks.last().unwrap();
|
163 | 159 |
|
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()) |
172 | 163 | }
|
173 | 164 |
|
174 | 165 | /// Prepare currently mapped buffers for use in a submission.
|
|
0 commit comments