wgpu_async/
async_queue.rs

1use crate::{async_device::AsyncDevice, WgpuFuture};
2use std::{ops::Deref, sync::Arc};
3use wgpu::{CommandBuffer, Queue};
4
5/// A wrapper around a [`wgpu::Queue`] which shadows some methods to allow for callback-and-poll
6/// methods to be made async, such as [`AsyncQueue::submit`].
7#[derive(Clone, Debug)]
8pub struct AsyncQueue {
9    device: AsyncDevice,
10    queue: Arc<Queue>,
11}
12
13impl AsyncQueue {
14    pub(crate) fn new(device: AsyncDevice, queue: Arc<Queue>) -> Self {
15        Self { device, queue }
16    }
17
18    /// This is an `async` version of [`wgpu::Queue::submit`].
19    ///
20    /// Just like [`wgpu::Queue::submit`], a call to this method starts the given work immediately,
21    /// however this method returns a future that can be awaited giving the completion of the submitted work.
22    pub fn submit<I: IntoIterator<Item = CommandBuffer>>(
23        &self,
24        command_buffers: I,
25    ) -> WgpuFuture<()> {
26        let queue_ref = Arc::clone(&self.queue);
27
28        queue_ref.submit(command_buffers);
29
30        self.device.do_async(move |callback| {
31            queue_ref.on_submitted_work_done(|| callback(()));
32        })
33    }
34
35    /// Gets the device associated with this queue.
36    pub fn device(&self) -> &AsyncDevice {
37        &self.device
38    }
39}
40impl Deref for AsyncQueue {
41    type Target = wgpu::Queue;
42
43    fn deref(&self) -> &Self::Target {
44        &self.queue
45    }
46}
47impl<T> AsRef<T> for AsyncQueue
48where
49    T: ?Sized,
50    <AsyncQueue as Deref>::Target: AsRef<T>,
51{
52    fn as_ref(&self) -> &T {
53        self.deref().as_ref()
54    }
55}