Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Background thread for automatic device polling #1891

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions player/src/bin/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ fn main() {
adapter,
&desc,
None,
None,
id
));
if let Some(e) = error {
Expand Down
1 change: 1 addition & 0 deletions player/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ impl Test<'_> {
limits: wgt::Limits::default(),
},
None,
None,
device
));
if let Some(e) = error {
Expand Down
22 changes: 21 additions & 1 deletion wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ use smallvec::SmallVec;
use thiserror::Error;
use wgt::{BufferAddress, TextureFormat, TextureViewDimension};

use std::{borrow::Cow, iter, marker::PhantomData, mem, ops::Range, ptr, sync::atomic::Ordering};
use std::{
borrow::Cow,
iter,
marker::PhantomData,
mem,
ops::Range,
ptr,
sync::{atomic::Ordering, Arc},
};

mod life;
pub mod queue;
Expand Down Expand Up @@ -261,10 +269,13 @@ pub struct Device<A: hal::Api> {
//TODO: move this behind another mutex. This would allow several methods to switch
// to borrow Device immutably, such as `write_buffer`, `write_texture`, and `buffer_unmap`.
pending_writes: queue::PendingWrites<A>,
buffer_map_notifier: Option<BufferMapNotifier>,
#[cfg(feature = "trace")]
pub(crate) trace: Option<Mutex<trace::Trace>>,
}

pub type BufferMapNotifier = Arc<dyn Fn() + Send + Sync>;

#[derive(Clone, Debug, Error)]
pub enum CreateDeviceError {
#[error("not enough memory left")]
Expand Down Expand Up @@ -300,6 +311,7 @@ impl<A: HalApi> Device<A> {
alignments: hal::Alignments,
downlevel: wgt::DownlevelCapabilities,
desc: &DeviceDescriptor,
buffer_map_notifier: Option<BufferMapNotifier>,
trace_path: Option<&std::path::Path>,
) -> Result<Self, CreateDeviceError> {
#[cfg(not(feature = "trace"))]
Expand Down Expand Up @@ -347,6 +359,7 @@ impl<A: HalApi> Device<A> {
features: desc.features,
downlevel,
pending_writes,
buffer_map_notifier,
})
}

Expand Down Expand Up @@ -407,6 +420,12 @@ impl<A: HalApi> Device<A> {
})
}

fn notify_buffer_map(&self) {
if let Some(notifier) = self.buffer_map_notifier.as_ref() {
notifier();
}
}

fn untrack<'this, 'token: 'this, G: GlobalIdentityHandlerFactory>(
&'this mut self,
hub: &Hub<A, G>,
Expand Down Expand Up @@ -4655,6 +4674,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
device
.lock_life(&mut token)
.map(id::Valid(buffer_id), ref_count);
device.notify_buffer_map();

Ok(())
}
Expand Down
33 changes: 22 additions & 11 deletions wgpu-core/src/instance.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
device::{Device, DeviceDescriptor},
device::{BufferMapNotifier, Device, DeviceDescriptor},
hub::{Global, GlobalIdentityHandlerFactory, HalApi, Input, Token},
id::{AdapterId, DeviceId, SurfaceId, Valid},
present::Presentation,
Expand Down Expand Up @@ -275,6 +275,7 @@ impl<A: HalApi> Adapter<A> {
self_id: AdapterId,
open: hal::OpenDevice<A>,
desc: &DeviceDescriptor,
buffer_map_notifier: Option<BufferMapNotifier>,
trace_path: Option<&std::path::Path>,
) -> Result<Device<A>, RequestDeviceError> {
// Verify all features were exposed by the adapter
Expand Down Expand Up @@ -331,6 +332,7 @@ impl<A: HalApi> Adapter<A> {
caps.alignments.clone(),
caps.downlevel.clone(),
desc,
buffer_map_notifier,
trace_path,
)
.or(Err(RequestDeviceError::OutOfMemory))
Expand All @@ -340,14 +342,15 @@ impl<A: HalApi> Adapter<A> {
&self,
self_id: AdapterId,
desc: &DeviceDescriptor,
buffer_map_notifier: Option<BufferMapNotifier>,
trace_path: Option<&std::path::Path>,
) -> Result<Device<A>, RequestDeviceError> {
let open = unsafe { self.raw.adapter.open(desc.features) }.map_err(|err| match err {
hal::DeviceError::Lost => RequestDeviceError::DeviceLost,
hal::DeviceError::OutOfMemory => RequestDeviceError::OutOfMemory,
})?;

self.create_device_from_hal(self_id, open, desc, trace_path)
self.create_device_from_hal(self_id, open, desc, buffer_map_notifier, trace_path)
}
}

Expand Down Expand Up @@ -820,6 +823,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
&self,
adapter_id: AdapterId,
desc: &DeviceDescriptor,
buffer_map_notifier: Option<BufferMapNotifier>,
trace_path: Option<&std::path::Path>,
id_in: Input<G, DeviceId>,
) -> (DeviceId, Option<RequestDeviceError>) {
Expand All @@ -835,10 +839,11 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
Ok(adapter) => adapter,
Err(_) => break RequestDeviceError::InvalidAdapter,
};
let device = match adapter.create_device(adapter_id, desc, trace_path) {
Ok(device) => device,
Err(e) => break e,
};
let device =
match adapter.create_device(adapter_id, desc, buffer_map_notifier, trace_path) {
Ok(device) => device,
Err(e) => break e,
};
let id = fid.assign(device, &mut token);
return (id.0, None);
};
Expand All @@ -856,6 +861,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
adapter_id: AdapterId,
hal_device: hal::OpenDevice<A>,
desc: &DeviceDescriptor,
buffer_map_notifier: Option<BufferMapNotifier>,
trace_path: Option<&std::path::Path>,
id_in: Input<G, DeviceId>,
) -> (DeviceId, Option<RequestDeviceError>) {
Expand All @@ -871,11 +877,16 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
Ok(adapter) => adapter,
Err(_) => break RequestDeviceError::InvalidAdapter,
};
let device =
match adapter.create_device_from_hal(adapter_id, hal_device, desc, trace_path) {
Ok(device) => device,
Err(e) => break e,
};
let device = match adapter.create_device_from_hal(
adapter_id,
hal_device,
desc,
buffer_map_notifier,
trace_path,
) {
Ok(device) => device,
Err(e) => break e,
};
let id = fid.assign(device, &mut token);
return (id.0, None);
};
Expand Down
78 changes: 36 additions & 42 deletions wgpu/examples/capture/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::env;
use std::fs::File;
use std::io::Write;
use std::mem::size_of;
use wgpu::{Buffer, Device};
use wgpu::Buffer;

async fn run(png_output_path: &str) {
let args: Vec<_> = env::args().collect();
Expand All @@ -20,14 +20,14 @@ async fn run(png_output_path: &str) {
return;
}
};
let (device, buffer, buffer_dimensions) = create_red_image_with_dimensions(width, height).await;
create_png(png_output_path, device, buffer, &buffer_dimensions).await;
let (buffer, buffer_dimensions) = create_red_image_with_dimensions(width, height).await;
create_png(png_output_path, buffer, &buffer_dimensions).await;
}

async fn create_red_image_with_dimensions(
width: usize,
height: usize,
) -> (Device, Buffer, BufferDimensions) {
) -> (Buffer, BufferDimensions) {
let adapter = wgpu::Instance::new(wgpu::Backends::PRIMARY)
.request_adapter(&wgpu::RequestAdapterOptions::default())
.await
Expand All @@ -41,6 +41,7 @@ async fn create_red_image_with_dimensions(
limits: wgpu::Limits::downlevel_defaults(),
},
None,
true,
)
.await
.unwrap();
Expand Down Expand Up @@ -113,58 +114,52 @@ async fn create_red_image_with_dimensions(
};

queue.submit(Some(command_buffer));
(device, output_buffer, buffer_dimensions)
(output_buffer, buffer_dimensions)
}

async fn create_png(
png_output_path: &str,
device: Device,
output_buffer: Buffer,
buffer_dimensions: &BufferDimensions,
) {
// Note that we're not calling `.await` here.
let buffer_slice = output_buffer.slice(..);
let buffer_future = buffer_slice.map_async(wgpu::MapMode::Read);
if buffer_slice.map_async(wgpu::MapMode::Read).await.is_err() {
return;
}

// Poll the device in a blocking manner so that our future resolves.
// In an actual application, `device.poll(...)` should
// be called in an event loop or on another thread.
device.poll(wgpu::Maintain::Wait);
// If a file system is available, write the buffer as a PNG
let has_file_system_available = cfg!(not(target_arch = "wasm32"));
if !has_file_system_available {
return;
}

if let Ok(()) = buffer_future.await {
let padded_buffer = buffer_slice.get_mapped_range();

let mut png_encoder = png::Encoder::new(
File::create(png_output_path).unwrap(),
buffer_dimensions.width as u32,
buffer_dimensions.height as u32,
);
png_encoder.set_depth(png::BitDepth::Eight);
png_encoder.set_color(png::ColorType::RGBA);
let mut png_writer = png_encoder
.write_header()
.unwrap()
.into_stream_writer_with_size(buffer_dimensions.unpadded_bytes_per_row);

// from the padded_buffer we write just the unpadded bytes into the image
for chunk in padded_buffer.chunks(buffer_dimensions.padded_bytes_per_row) {
png_writer
.write_all(&chunk[..buffer_dimensions.unpadded_bytes_per_row])
.unwrap();
}
png_writer.finish().unwrap();
let padded_buffer = buffer_slice.get_mapped_range();

let mut png_encoder = png::Encoder::new(
File::create(png_output_path).unwrap(),
buffer_dimensions.width as u32,
buffer_dimensions.height as u32,
);
png_encoder.set_depth(png::BitDepth::Eight);
png_encoder.set_color(png::ColorType::RGBA);
let mut png_writer = png_encoder
.write_header()
.unwrap()
.into_stream_writer_with_size(buffer_dimensions.unpadded_bytes_per_row);

// from the padded_buffer we write just the unpadded bytes into the image
for chunk in padded_buffer.chunks(buffer_dimensions.padded_bytes_per_row) {
png_writer
.write_all(&chunk[..buffer_dimensions.unpadded_bytes_per_row])
.unwrap();
}
png_writer.finish().unwrap();

// With the current interface, we have to make sure all mapped views are
// dropped before we unmap the buffer.
drop(padded_buffer);
// With the current interface, we have to make sure all mapped views are
// dropped before we unmap the buffer.
drop(padded_buffer);

output_buffer.unmap();
}
output_buffer.unmap();
}

struct BufferDimensions {
Expand Down Expand Up @@ -218,9 +213,8 @@ mod tests {
let (device, output_buffer, dimensions) =
create_red_image_with_dimensions(100usize, 200usize).await;
let buffer_slice = output_buffer.slice(..);
let buffer_future = buffer_slice.map_async(wgpu::MapMode::Read);
device.poll(wgpu::Maintain::Wait);
buffer_future
buffer_slice
.map_async(wgpu::MapMode::Read)
.await
.expect("failed to map buffer slice for capture test");
let padded_buffer = buffer_slice.get_mapped_range();
Expand Down
1 change: 1 addition & 0 deletions wgpu/examples/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ async fn setup<E: Example>(title: &str) -> Setup {
limits: needed_limits,
},
trace_dir.ok().as_ref().map(std::path::Path::new),
true,
)
.await
.expect("Unable to find a suitable GPU adapter!");
Expand Down
14 changes: 3 additions & 11 deletions wgpu/examples/hello-compute/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ async fn execute_gpu(numbers: &[u32]) -> Option<Vec<u32>> {
limits: wgpu::Limits::downlevel_defaults(),
},
None,
true,
)
.await
.unwrap();
Expand Down Expand Up @@ -145,18 +146,9 @@ async fn execute_gpu_inner(
// Submits command encoder for processing
queue.submit(Some(encoder.finish()));

// Note that we're not calling `.await` here.
let buffer_slice = staging_buffer.slice(..);
// Gets the future representing when `staging_buffer` can be read from
let buffer_future = buffer_slice.map_async(wgpu::MapMode::Read);

// Poll the device in a blocking manner so that our future resolves.
// In an actual application, `device.poll(...)` should
// be called in an event loop or on another thread.
device.poll(wgpu::Maintain::Wait);

// Awaits until `buffer_future` can be read from
if let Ok(()) = buffer_future.await {
// Awaits until `buffer_slice` can be read from
if let Ok(()) = buffer_slice.map_async(wgpu::MapMode::Read).await {
// Gets contents of buffer
let data = buffer_slice.get_mapped_range();
// Since contents are got in bytes, this converts these bytes back to u32
Expand Down
1 change: 1 addition & 0 deletions wgpu/examples/hello-triangle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ async fn run(event_loop: EventLoop<()>, window: Window) {
limits: wgpu::Limits::downlevel_defaults().using_resolution(adapter.limits()),
},
None,
true,
)
.await
.expect("Failed to create device");
Expand Down
1 change: 1 addition & 0 deletions wgpu/examples/hello-windows/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ async fn run(event_loop: EventLoop<()>, viewports: Vec<(Window, wgpu::Color)>) {
limits: wgpu::Limits::downlevel_defaults(),
},
None,
true,
)
.await
.expect("Failed to create device");
Expand Down
Loading