wgpu_core/device/life.rs
1use alloc::{sync::Arc, vec::Vec};
2
3use smallvec::SmallVec;
4use thiserror::Error;
5
6use crate::{
7 device::{
8 queue::{EncoderInFlight, SubmittedWorkDoneClosure, TempResource},
9 DeviceError,
10 },
11 ray_tracing::BlasCompactReadyPendingClosure,
12 resource::{Blas, Buffer, Texture, Trackable},
13 snatch::SnatchGuard,
14 SubmissionIndex,
15};
16
17/// A command submitted to the GPU for execution.
18///
19/// ## Keeping resources alive while the GPU is using them
20///
21/// [`wgpu_hal`] requires that, when a command is submitted to a queue, all the
22/// resources it uses must remain alive until it has finished executing.
23///
24/// [`wgpu_hal`]: hal
25struct ActiveSubmission {
26 /// The index of the submission we track.
27 ///
28 /// When `Device::fence`'s value is greater than or equal to this, our queue
29 /// submission has completed.
30 index: SubmissionIndex,
31
32 /// Buffers to be mapped once this submission has completed.
33 mapped: Vec<Arc<Buffer>>,
34
35 /// BLASes to have their compacted size read back once this submission has completed.
36 compact_read_back: Vec<Arc<Blas>>,
37
38 /// Command buffers used by this submission, and the encoder that owns them.
39 ///
40 /// [`wgpu_hal::Queue::submit`] requires the submitted command buffers to
41 /// remain alive until the submission has completed execution. Command
42 /// encoders double as allocation pools for command buffers, so holding them
43 /// here and cleaning them up in [`LifetimeTracker::triage_submissions`]
44 /// satisfies that requirement.
45 ///
46 /// Once this submission has completed, the command buffers are reset and
47 /// the command encoder is recycled.
48 ///
49 /// [`wgpu_hal::Queue::submit`]: hal::Queue::submit
50 encoders: Vec<EncoderInFlight>,
51
52 /// List of queue "on_submitted_work_done" closures to be called once this
53 /// submission has completed.
54 work_done_closures: SmallVec<[SubmittedWorkDoneClosure; 1]>,
55}
56
57impl ActiveSubmission {
58 /// Returns true if this submission contains the given buffer.
59 ///
60 /// This only uses constant-time operations.
61 pub fn contains_buffer(&self, buffer: &Buffer) -> bool {
62 for encoder in &self.encoders {
63 // The ownership location of buffers depends on where the command encoder
64 // came from. If it is the staging command encoder on the queue, it is
65 // in the pending buffer list. If it came from a user command encoder,
66 // it is in the tracker.
67
68 if encoder.trackers.buffers.contains(buffer) {
69 return true;
70 }
71
72 if encoder
73 .pending_buffers
74 .contains_key(&buffer.tracker_index())
75 {
76 return true;
77 }
78 }
79
80 false
81 }
82
83 /// Returns true if this submission contains the given texture.
84 ///
85 /// This only uses constant-time operations.
86 pub fn contains_texture(&self, texture: &Texture) -> bool {
87 for encoder in &self.encoders {
88 // The ownership location of textures depends on where the command encoder
89 // came from. If it is the staging command encoder on the queue, it is
90 // in the pending buffer list. If it came from a user command encoder,
91 // it is in the tracker.
92
93 if encoder.trackers.textures.contains(texture) {
94 return true;
95 }
96
97 if encoder
98 .pending_textures
99 .contains_key(&texture.tracker_index())
100 {
101 return true;
102 }
103 }
104
105 false
106 }
107
108 /// Returns true if this submission contains the given blas.
109 ///
110 /// This only uses constant-time operations.
111 pub fn contains_blas(&self, blas: &Blas) -> bool {
112 for encoder in &self.encoders {
113 if encoder.trackers.blas_s.contains(blas) {
114 return true;
115 }
116
117 if encoder.pending_blas_s.contains_key(&blas.tracker_index()) {
118 return true;
119 }
120 }
121
122 false
123 }
124}
125
126#[derive(Clone, Debug, Error)]
127#[non_exhaustive]
128pub enum WaitIdleError {
129 #[error(transparent)]
130 Device(#[from] DeviceError),
131 #[error("Tried to wait using a submission index ({0}) that has not been returned by a successful submission (last successful submission: {1})")]
132 WrongSubmissionIndex(SubmissionIndex, SubmissionIndex),
133 #[error("Timed out trying to wait for the given submission index.")]
134 Timeout,
135}
136
137impl WaitIdleError {
138 pub fn to_poll_error(&self) -> Option<wgt::PollError> {
139 match self {
140 WaitIdleError::Timeout => Some(wgt::PollError::Timeout),
141 _ => None,
142 }
143 }
144}
145
146/// Resource tracking for a device.
147///
148/// ## Host mapping buffers
149///
150/// A buffer cannot be mapped until all active queue submissions that use it
151/// have completed. To that end:
152///
153/// - Each buffer's `ResourceInfo::submission_index` records the index of the
154/// most recent queue submission that uses that buffer.
155///
156/// - When the device is polled, the following `LifetimeTracker` methods decide
157/// what should happen next:
158///
159/// 1) `triage_submissions` moves entries in `self.active[i]` for completed
160/// submissions to `self.ready_to_map`. At this point, both
161/// `self.active` and `self.ready_to_map` are up to date with the given
162/// submission index.
163///
164/// 2) `handle_mapping` drains `self.ready_to_map` and actually maps the
165/// buffers, collecting a list of notification closures to call.
166///
167/// Only calling `Global::buffer_map_async` clones a new `Arc` for the
168/// buffer. This new `Arc` is only dropped by `handle_mapping`.
169pub(crate) struct LifetimeTracker {
170 /// Resources used by queue submissions still in flight. One entry per
171 /// submission, with older submissions appearing before younger.
172 ///
173 /// Entries are added by `track_submission` and drained by
174 /// `LifetimeTracker::triage_submissions`. Lots of methods contribute data
175 /// to particular entries.
176 active: Vec<ActiveSubmission>,
177
178 /// Buffers the user has asked us to map, and which are not used by any
179 /// queue submission still in flight.
180 ready_to_map: Vec<Arc<Buffer>>,
181
182 /// BLASes the user has asked us to prepare to compact, and which are not used by any
183 /// queue submission still in flight.
184 ready_to_compact: Vec<Arc<Blas>>,
185
186 /// Queue "on_submitted_work_done" closures that were initiated for while there is no
187 /// currently pending submissions. These cannot be immediately invoked as they
188 /// must happen _after_ all mapped buffer callbacks are mapped, so we defer them
189 /// here until the next time the device is maintained.
190 work_done_closures: SmallVec<[SubmittedWorkDoneClosure; 1]>,
191}
192
193impl LifetimeTracker {
194 pub fn new() -> Self {
195 Self {
196 active: Vec::new(),
197 ready_to_map: Vec::new(),
198 ready_to_compact: Vec::new(),
199 work_done_closures: SmallVec::new(),
200 }
201 }
202
203 /// Return true if there are no queue submissions still in flight.
204 pub fn queue_empty(&self) -> bool {
205 self.active.is_empty()
206 }
207
208 /// Start tracking resources associated with a new queue submission.
209 pub fn track_submission(&mut self, index: SubmissionIndex, encoders: Vec<EncoderInFlight>) {
210 self.active.push(ActiveSubmission {
211 index,
212 mapped: Vec::new(),
213 compact_read_back: Vec::new(),
214 encoders,
215 work_done_closures: SmallVec::new(),
216 });
217 }
218
219 pub(crate) fn map(&mut self, buffer: &Arc<Buffer>) -> Option<SubmissionIndex> {
220 // Determine which buffers are ready to map, and which must wait for the GPU.
221 let submission = self
222 .active
223 .iter_mut()
224 .rev()
225 .find(|a| a.contains_buffer(buffer));
226
227 let maybe_submission_index = submission.as_ref().map(|s| s.index);
228
229 submission
230 .map_or(&mut self.ready_to_map, |a| &mut a.mapped)
231 .push(buffer.clone());
232
233 maybe_submission_index
234 }
235
236 pub(crate) fn prepare_compact(&mut self, blas: &Arc<Blas>) -> Option<SubmissionIndex> {
237 // Determine which BLASes are ready to map, and which must wait for the GPU.
238 let submission = self.active.iter_mut().rev().find(|a| a.contains_blas(blas));
239
240 let maybe_submission_index = submission.as_ref().map(|s| s.index);
241
242 submission
243 .map_or(&mut self.ready_to_compact, |a| &mut a.compact_read_back)
244 .push(blas.clone());
245
246 maybe_submission_index
247 }
248
249 /// Returns the submission index of the most recent submission that uses the
250 /// given buffer.
251 pub fn get_buffer_latest_submission_index(&self, buffer: &Buffer) -> Option<SubmissionIndex> {
252 // We iterate in reverse order, so that we can bail out early as soon
253 // as we find a hit.
254 self.active.iter().rev().find_map(|submission| {
255 if submission.contains_buffer(buffer) {
256 Some(submission.index)
257 } else {
258 None
259 }
260 })
261 }
262
263 /// Returns the submission index of the most recent submission that uses the
264 /// given texture.
265 pub fn get_texture_latest_submission_index(
266 &self,
267 texture: &Texture,
268 ) -> Option<SubmissionIndex> {
269 // We iterate in reverse order, so that we can bail out early as soon
270 // as we find a hit.
271 self.active.iter().rev().find_map(|submission| {
272 if submission.contains_texture(texture) {
273 Some(submission.index)
274 } else {
275 None
276 }
277 })
278 }
279
280 /// Sort out the consequences of completed submissions.
281 ///
282 /// Assume that all submissions up through `last_done` have completed.
283 ///
284 /// - Buffers used by those submissions are now ready to map, if requested.
285 /// Add any buffers in the submission's [`mapped`] list to
286 /// [`self.ready_to_map`], where [`LifetimeTracker::handle_mapping`]
287 /// will find them.
288 ///
289 /// Return a list of [`SubmittedWorkDoneClosure`]s to run.
290 ///
291 /// [`mapped`]: ActiveSubmission::mapped
292 /// [`self.ready_to_map`]: LifetimeTracker::ready_to_map
293 /// [`SubmittedWorkDoneClosure`]: crate::device::queue::SubmittedWorkDoneClosure
294 #[must_use]
295 pub fn triage_submissions(
296 &mut self,
297 last_done: SubmissionIndex,
298 ) -> SmallVec<[SubmittedWorkDoneClosure; 1]> {
299 profiling::scope!("triage_submissions");
300
301 //TODO: enable when `is_sorted_by_key` is stable
302 //debug_assert!(self.active.is_sorted_by_key(|a| a.index));
303 let done_count = self
304 .active
305 .iter()
306 .position(|a| a.index > last_done)
307 .unwrap_or(self.active.len());
308
309 let mut work_done_closures: SmallVec<_> = self.work_done_closures.drain(..).collect();
310 for a in self.active.drain(..done_count) {
311 self.ready_to_map.extend(a.mapped);
312 self.ready_to_compact.extend(a.compact_read_back);
313 for encoder in a.encoders {
314 // This involves actually decrementing the ref count of all command buffer
315 // resources, so can be _very_ expensive.
316 profiling::scope!("drop command buffer trackers");
317 drop(encoder);
318 }
319 work_done_closures.extend(a.work_done_closures);
320 }
321 work_done_closures
322 }
323
324 pub fn schedule_resource_destruction(
325 &mut self,
326 temp_resource: TempResource,
327 last_submit_index: SubmissionIndex,
328 ) {
329 let resources = self
330 .active
331 .iter_mut()
332 .find(|a| a.index == last_submit_index)
333 .map(|a| {
334 // Because this resource's `last_submit_index` matches `a.index`,
335 // we know that we must have done something with the resource,
336 // so `a.encoders` should not be empty.
337 &mut a.encoders.last_mut().unwrap().temp_resources
338 });
339 if let Some(resources) = resources {
340 resources.push(temp_resource);
341 }
342 }
343
344 pub fn add_work_done_closure(
345 &mut self,
346 closure: SubmittedWorkDoneClosure,
347 ) -> Option<SubmissionIndex> {
348 match self.active.last_mut() {
349 Some(active) => {
350 active.work_done_closures.push(closure);
351 Some(active.index)
352 }
353 // We must defer the closure until all previously occurring map_async closures
354 // have fired. This is required by the spec.
355 None => {
356 self.work_done_closures.push(closure);
357 None
358 }
359 }
360 }
361
362 /// Map the buffers in `self.ready_to_map`.
363 ///
364 /// Return a list of mapping notifications to send.
365 ///
366 /// See the documentation for [`LifetimeTracker`] for details.
367 #[must_use]
368 pub(crate) fn handle_mapping(
369 &mut self,
370 snatch_guard: &SnatchGuard,
371 ) -> Vec<super::BufferMapPendingClosure> {
372 if self.ready_to_map.is_empty() {
373 return Vec::new();
374 }
375 let mut pending_callbacks: Vec<super::BufferMapPendingClosure> =
376 Vec::with_capacity(self.ready_to_map.len());
377
378 for buffer in self.ready_to_map.drain(..) {
379 match buffer.map(snatch_guard) {
380 Some(cb) => pending_callbacks.push(cb),
381 None => continue,
382 }
383 }
384 pending_callbacks
385 }
386 /// Read back compact sizes from the BLASes in `self.ready_to_compact`.
387 ///
388 /// Return a list of mapping notifications to send.
389 ///
390 /// See the documentation for [`LifetimeTracker`] for details.
391 #[must_use]
392 pub(crate) fn handle_compact_read_back(&mut self) -> Vec<BlasCompactReadyPendingClosure> {
393 if self.ready_to_compact.is_empty() {
394 return Vec::new();
395 }
396 let mut pending_callbacks: Vec<BlasCompactReadyPendingClosure> =
397 Vec::with_capacity(self.ready_to_compact.len());
398
399 for blas in self.ready_to_compact.drain(..) {
400 match blas.read_back_compact_size() {
401 Some(cb) => pending_callbacks.push(cb),
402 None => continue,
403 }
404 }
405 pending_callbacks
406 }
407}