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            &WaitIdleError::WrongSubmissionIndex(a, b) => {
142                Some(wgt::PollError::WrongSubmissionIndex(a, b))
143            }
144            _ => None,
145        }
146    }
147}
148
149/// Resource tracking for a device.
150///
151/// ## Host mapping buffers
152///
153/// A buffer cannot be mapped until all active queue submissions that use it
154/// have completed. To that end:
155///
156/// -   Each buffer's `ResourceInfo::submission_index` records the index of the
157///     most recent queue submission that uses that buffer.
158///
159/// -   When the device is polled, the following `LifetimeTracker` methods decide
160///     what should happen next:
161///
162///     1)  `triage_submissions` moves entries in `self.active[i]` for completed
163///         submissions to `self.ready_to_map`.  At this point, both
164///         `self.active` and `self.ready_to_map` are up to date with the given
165///         submission index.
166///
167///     2)  `handle_mapping` drains `self.ready_to_map` and actually maps the
168///         buffers, collecting a list of notification closures to call.
169///
170/// Only calling `Global::buffer_map_async` clones a new `Arc` for the
171/// buffer. This new `Arc` is only dropped by `handle_mapping`.
172pub(crate) struct LifetimeTracker {
173    /// Resources used by queue submissions still in flight. One entry per
174    /// submission, with older submissions appearing before younger.
175    ///
176    /// Entries are added by `track_submission` and drained by
177    /// `LifetimeTracker::triage_submissions`. Lots of methods contribute data
178    /// to particular entries.
179    active: Vec<ActiveSubmission>,
180
181    /// Buffers the user has asked us to map, and which are not used by any
182    /// queue submission still in flight.
183    ready_to_map: Vec<Arc<Buffer>>,
184
185    /// BLASes the user has asked us to prepare to compact, and which are not used by any
186    /// queue submission still in flight.
187    ready_to_compact: Vec<Arc<Blas>>,
188
189    /// Queue "on_submitted_work_done" closures that were initiated for while there is no
190    /// currently pending submissions. These cannot be immediately invoked as they
191    /// must happen _after_ all mapped buffer callbacks are mapped, so we defer them
192    /// here until the next time the device is maintained.
193    work_done_closures: SmallVec<[SubmittedWorkDoneClosure; 1]>,
194}
195
196impl LifetimeTracker {
197    pub fn new() -> Self {
198        Self {
199            active: Vec::new(),
200            ready_to_map: Vec::new(),
201            ready_to_compact: Vec::new(),
202            work_done_closures: SmallVec::new(),
203        }
204    }
205
206    /// Return true if there are no queue submissions still in flight.
207    pub fn queue_empty(&self) -> bool {
208        self.active.is_empty()
209    }
210
211    /// Start tracking resources associated with a new queue submission.
212    pub fn track_submission(&mut self, index: SubmissionIndex, encoders: Vec<EncoderInFlight>) {
213        self.active.push(ActiveSubmission {
214            index,
215            mapped: Vec::new(),
216            compact_read_back: Vec::new(),
217            encoders,
218            work_done_closures: SmallVec::new(),
219        });
220    }
221
222    /// Schedule a buffer for mapping.
223    ///
224    /// The buffer will be added either to a pending submission, or to `self.ready_to_map`.
225    /// If it is added to a pending submission, returns the index of that submission.
226    pub(crate) fn map(&mut self, buffer: &Arc<Buffer>) -> Option<SubmissionIndex> {
227        let submission = self
228            .active
229            .iter_mut()
230            .rev()
231            .find(|a| a.contains_buffer(buffer));
232
233        let maybe_submission_index = submission.as_ref().map(|s| s.index);
234
235        submission
236            .map_or(&mut self.ready_to_map, |a| &mut a.mapped)
237            .push(buffer.clone());
238
239        maybe_submission_index
240    }
241
242    pub(crate) fn prepare_compact(&mut self, blas: &Arc<Blas>) -> Option<SubmissionIndex> {
243        // Determine which BLASes are ready to map, and which must wait for the GPU.
244        let submission = self.active.iter_mut().rev().find(|a| a.contains_blas(blas));
245
246        let maybe_submission_index = submission.as_ref().map(|s| s.index);
247
248        submission
249            .map_or(&mut self.ready_to_compact, |a| &mut a.compact_read_back)
250            .push(blas.clone());
251
252        maybe_submission_index
253    }
254
255    /// Returns the submission index of the most recent submission that uses the
256    /// given buffer.
257    pub fn get_buffer_latest_submission_index(&self, buffer: &Buffer) -> Option<SubmissionIndex> {
258        // We iterate in reverse order, so that we can bail out early as soon
259        // as we find a hit.
260        self.active.iter().rev().find_map(|submission| {
261            if submission.contains_buffer(buffer) {
262                Some(submission.index)
263            } else {
264                None
265            }
266        })
267    }
268
269    /// Returns the submission index of the most recent submission that uses the
270    /// given texture.
271    pub fn get_texture_latest_submission_index(
272        &self,
273        texture: &Texture,
274    ) -> Option<SubmissionIndex> {
275        // We iterate in reverse order, so that we can bail out early as soon
276        // as we find a hit.
277        self.active.iter().rev().find_map(|submission| {
278            if submission.contains_texture(texture) {
279                Some(submission.index)
280            } else {
281                None
282            }
283        })
284    }
285
286    /// Sort out the consequences of completed submissions.
287    ///
288    /// Assume that all submissions up through `last_done` have completed.
289    ///
290    /// -   Buffers used by those submissions are now ready to map, if requested.
291    ///     Add any buffers in the submission's [`mapped`] list to
292    ///     [`self.ready_to_map`], where [`LifetimeTracker::handle_mapping`]
293    ///     will find them.
294    ///
295    /// Return a list of [`SubmittedWorkDoneClosure`]s to run.
296    ///
297    /// [`mapped`]: ActiveSubmission::mapped
298    /// [`self.ready_to_map`]: LifetimeTracker::ready_to_map
299    /// [`SubmittedWorkDoneClosure`]: crate::device::queue::SubmittedWorkDoneClosure
300    #[must_use]
301    pub fn triage_submissions(
302        &mut self,
303        last_done: SubmissionIndex,
304    ) -> SmallVec<[SubmittedWorkDoneClosure; 1]> {
305        profiling::scope!("triage_submissions");
306
307        debug_assert!(self.active.is_sorted_by_key(|a| a.index));
308        let done_count = self
309            .active
310            .iter()
311            .position(|a| a.index > last_done)
312            .unwrap_or(self.active.len());
313
314        let mut work_done_closures: SmallVec<_> = self.work_done_closures.drain(..).collect();
315        for a in self.active.drain(..done_count) {
316            self.ready_to_map.extend(a.mapped);
317            self.ready_to_compact.extend(a.compact_read_back);
318            for encoder in a.encoders {
319                // This involves actually decrementing the ref count of all command buffer
320                // resources, so can be _very_ expensive.
321                profiling::scope!("drop command buffer trackers");
322                drop(encoder);
323            }
324            work_done_closures.extend(a.work_done_closures);
325        }
326        work_done_closures
327    }
328
329    pub fn schedule_resource_destruction(
330        &mut self,
331        temp_resource: TempResource,
332        last_submit_index: SubmissionIndex,
333    ) {
334        let resources = self
335            .active
336            .iter_mut()
337            .find(|a| a.index == last_submit_index)
338            .map(|a| {
339                // Because this resource's `last_submit_index` matches `a.index`,
340                // we know that we must have done something with the resource,
341                // so `a.encoders` should not be empty.
342                &mut a.encoders.last_mut().unwrap().temp_resources
343            });
344        if let Some(resources) = resources {
345            resources.push(temp_resource);
346        }
347    }
348
349    pub fn add_work_done_closure(
350        &mut self,
351        closure: SubmittedWorkDoneClosure,
352    ) -> Option<SubmissionIndex> {
353        match self.active.last_mut() {
354            Some(active) => {
355                active.work_done_closures.push(closure);
356                Some(active.index)
357            }
358            // We must defer the closure until all previously occurring map_async closures
359            // have fired. This is required by the spec.
360            None => {
361                self.work_done_closures.push(closure);
362                None
363            }
364        }
365    }
366
367    /// Map the buffers in `self.ready_to_map`.
368    ///
369    /// Return a list of mapping notifications to send.
370    ///
371    /// See the documentation for [`LifetimeTracker`] for details.
372    #[must_use]
373    pub(crate) fn handle_mapping(
374        &mut self,
375        snatch_guard: &SnatchGuard,
376    ) -> Vec<super::BufferMapPendingClosure> {
377        if self.ready_to_map.is_empty() {
378            return Vec::new();
379        }
380        let mut pending_callbacks: Vec<super::BufferMapPendingClosure> =
381            Vec::with_capacity(self.ready_to_map.len());
382
383        for buffer in self.ready_to_map.drain(..) {
384            match buffer.map(snatch_guard) {
385                Some(cb) => pending_callbacks.push(cb),
386                None => continue,
387            }
388        }
389        pending_callbacks
390    }
391    /// Read back compact sizes from the BLASes in `self.ready_to_compact`.
392    ///
393    /// Return a list of mapping notifications to send.
394    ///
395    /// See the documentation for [`LifetimeTracker`] for details.
396    #[must_use]
397    pub(crate) fn handle_compact_read_back(&mut self) -> Vec<BlasCompactReadyPendingClosure> {
398        if self.ready_to_compact.is_empty() {
399            return Vec::new();
400        }
401        let mut pending_callbacks: Vec<BlasCompactReadyPendingClosure> =
402            Vec::with_capacity(self.ready_to_compact.len());
403
404        for blas in self.ready_to_compact.drain(..) {
405            match blas.read_back_compact_size() {
406                Some(cb) => pending_callbacks.push(cb),
407                None => continue,
408            }
409        }
410        pending_callbacks
411    }
412}