forked from gfx-rs/wgpu
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[rs] prototype of async/await for buffer mapping
- Loading branch information
1 parent
1968eb8
commit a85f95d
Showing
6 changed files
with
206 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,8 @@ Cargo.lock | |
# Other | ||
.DS_Store | ||
|
||
# VSCode project | ||
.vscode | ||
|
||
# Output from capture example | ||
red.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,3 +46,4 @@ log = "0.4" | |
png = "0.15" | ||
winit = "0.20.0-alpha4" | ||
zerocopy = "0.2" | ||
futures = "0.3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::sync::{Arc, Mutex}; | ||
use std::task::{Context, Poll, Waker}; | ||
|
||
struct GpuFutureInner<T> { | ||
id: wgc::id::DeviceId, | ||
result: Option<T>, | ||
waker: Option<Waker>, | ||
} | ||
|
||
/// A Future that can poll the wgpu::Device | ||
pub struct GpuFuture<T> { | ||
inner: Arc<Mutex<GpuFutureInner<T>>>, | ||
} | ||
|
||
/// A completion handle to set the result on a GpuFuture | ||
pub struct GpuFutureCompletion<T> { | ||
inner: Arc<Mutex<GpuFutureInner<T>>>, | ||
} | ||
|
||
impl<T> Future for GpuFuture<T> | ||
{ | ||
type Output = T; | ||
|
||
fn poll(self: Pin<&mut Self>, context: &mut Context) -> Poll<Self::Output> { | ||
// grab a clone of the Arc | ||
let arc = Arc::clone(&Pin::into_inner(self).inner); | ||
|
||
// grab the device id and set the waker, but release the lock, so that the native callback can write to it | ||
let device_id = { | ||
let mut inner = arc.lock().unwrap(); | ||
inner.waker.replace(context.waker().clone()); | ||
inner.id | ||
}; | ||
|
||
// polling the device should trigger the callback | ||
wgn::wgpu_device_poll(device_id, true); | ||
|
||
// now take the lock again, and check whether the future is complete | ||
let mut inner = arc.lock().unwrap(); | ||
match inner.result.take() { | ||
Some(value) => Poll::Ready(value), | ||
_ => Poll::Pending, | ||
} | ||
} | ||
} | ||
|
||
impl<T> GpuFutureCompletion<T> { | ||
pub fn complete(self, value: T) { | ||
let mut inner = self.inner.lock().unwrap(); | ||
inner.result.replace(value); | ||
if let Some(waker) = &inner.waker { | ||
waker.wake_by_ref(); | ||
} | ||
} | ||
} | ||
|
||
pub(crate) fn new_gpu_future<T>(id: wgc::id::DeviceId) -> (GpuFuture<T>, GpuFutureCompletion<T>) { | ||
let inner = Arc::new(Mutex::new(GpuFutureInner { | ||
id, | ||
result: None, | ||
waker: None, | ||
})); | ||
|
||
( | ||
GpuFuture { | ||
inner: inner.clone(), | ||
}, | ||
GpuFutureCompletion { inner }, | ||
) | ||
} |
Oops, something went wrong.