diff --git a/examples/boids/main.rs b/examples/boids/main.rs index 2c38f1e45..2b4553e5f 100644 --- a/examples/boids/main.rs +++ b/examples/boids/main.rs @@ -1,8 +1,6 @@ // Flocking boids example with gpu compute update pass // adapted from https://github.com/austinEng/webgpu-samples/blob/master/src/examples/computeBoids.ts -extern crate rand; - #[path = "../framework.rs"] mod framework; @@ -257,6 +255,7 @@ impl framework::Example for Example { frame: &wgpu::SwapChainTexture, device: &wgpu::Device, _queue: &wgpu::Queue, + _spawner: &impl futures::task::LocalSpawn, ) -> wgpu::CommandBuffer { // create render pass descriptor let render_pass_descriptor = wgpu::RenderPassDescriptor { diff --git a/examples/cube/main.rs b/examples/cube/main.rs index 26e57a38d..3c1f30e47 100644 --- a/examples/cube/main.rs +++ b/examples/cube/main.rs @@ -318,6 +318,7 @@ impl framework::Example for Example { frame: &wgpu::SwapChainTexture, device: &wgpu::Device, _queue: &wgpu::Queue, + _spawner: &impl futures::task::LocalSpawn, ) -> wgpu::CommandBuffer { let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None }); diff --git a/examples/framework.rs b/examples/framework.rs index 519d0acd3..2f49613e2 100644 --- a/examples/framework.rs +++ b/examples/framework.rs @@ -1,4 +1,6 @@ -use std::time; +use futures::task::LocalSpawn; +#[cfg(not(target_arch = "wasm32"))] +use std::time::{Duration, Instant}; use winit::{ event::{self, WindowEvent}, event_loop::{ControlFlow, EventLoop}, @@ -49,10 +51,15 @@ pub trait Example: 'static + Sized { frame: &wgpu::SwapChainTexture, device: &wgpu::Device, queue: &wgpu::Queue, + spawner: &impl LocalSpawn, ) -> wgpu::CommandBuffer; } -async fn run_async(event_loop: EventLoop<()>, window: Window) { +async fn run_async( + event_loop: EventLoop<()>, + window: Window, + spawner: S, +) { log::info!("Initializing the surface..."); let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY); @@ -111,7 +118,7 @@ async fn run_async(event_loop: EventLoop<()>, window: Window) { } #[cfg(not(target_arch = "wasm32"))] - let mut last_update_inst = time::Instant::now(); + let mut last_update_inst = Instant::now(); log::info!("Entering render loop..."); event_loop.run(move |event, _, control_flow| { @@ -121,7 +128,7 @@ async fn run_async(event_loop: EventLoop<()>, window: Window) { } else { #[cfg(not(target_arch = "wasm32"))] { - ControlFlow::WaitUntil(time::Instant::now() + time::Duration::from_millis(10)) + ControlFlow::WaitUntil(Instant::now() + Duration::from_millis(10)) } #[cfg(target_arch = "wasm32")] { @@ -132,9 +139,9 @@ async fn run_async(event_loop: EventLoop<()>, window: Window) { event::Event::MainEventsCleared => { #[cfg(not(target_arch = "wasm32"))] { - if last_update_inst.elapsed() > time::Duration::from_millis(20) { + if last_update_inst.elapsed() > Duration::from_millis(20) { window.request_redraw(); - last_update_inst = time::Instant::now(); + last_update_inst = Instant::now(); } } @@ -178,7 +185,7 @@ async fn run_async(event_loop: EventLoop<()>, window: Window) { } }; - let command_buf = example.render(&frame.output, &device, &queue); + let command_buf = example.render(&frame.output, &device, &queue, &spawner); queue.submit(Some(command_buf)); } _ => {} @@ -199,6 +206,11 @@ pub fn run(title: &str) { #[cfg(not(target_arch = "wasm32"))] { + use futures::{ + executor::{LocalPool, LocalSpawner}, + task::LocalSpawnExt, + }; + env_logger::init(); #[cfg(feature = "subscriber")] @@ -207,13 +219,31 @@ pub fn run(title: &str) { wgpu::util::initialize_default_subscriber(chrome_tracing_dir.ok()); }; - futures::executor::block_on(run_async::(event_loop, window)); + let mut local_pool = LocalPool::new(); + let spawner = local_pool.spawner(); + local_pool + .spawner() + .spawn_local(run_async::(event_loop, window, spawner)) + .unwrap(); + local_pool.run(); } #[cfg(target_arch = "wasm32")] { + use futures::{future::LocalFutureObj, task::SpawnError}; + use winit::platform::web::WindowExtWebSys; + + struct WebSpawner {} + impl LocalSpawn for WebSpawner { + fn spawn_local_obj( + &self, + future: LocalFutureObj<'static, ()>, + ) -> Result<(), SpawnError> { + Ok(wasm_bindgen_futures::spawn_local(future)) + } + } + std::panic::set_hook(Box::new(console_error_panic_hook::hook)); console_log::init().expect("could not initialize logger"); - use winit::platform::web::WindowExtWebSys; // On wasm, append the canvas to the document body web_sys::window() .and_then(|win| win.document()) @@ -223,7 +253,12 @@ pub fn run(title: &str) { .ok() }) .expect("couldn't append canvas to document body"); - wasm_bindgen_futures::spawn_local(run_async::(event_loop, window)); + + wasm_bindgen_futures::spawn_local(run_async::( + event_loop, + window, + WebSpawner {}, + )); } } diff --git a/examples/mipmap/main.rs b/examples/mipmap/main.rs index da2c63490..62bf23326 100644 --- a/examples/mipmap/main.rs +++ b/examples/mipmap/main.rs @@ -405,6 +405,7 @@ impl framework::Example for Example { frame: &wgpu::SwapChainTexture, device: &wgpu::Device, _queue: &wgpu::Queue, + _spawner: &impl futures::task::LocalSpawn, ) -> wgpu::CommandBuffer { let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None }); diff --git a/examples/msaa-line/main.rs b/examples/msaa-line/main.rs index 482e74a9f..b4024ebfa 100644 --- a/examples/msaa-line/main.rs +++ b/examples/msaa-line/main.rs @@ -235,6 +235,7 @@ impl framework::Example for Example { frame: &wgpu::SwapChainTexture, device: &wgpu::Device, _queue: &wgpu::Queue, + _spawner: &impl futures::task::LocalSpawn, ) -> wgpu::CommandBuffer { if self.rebuild_bundle { self.bundle = Example::create_bundle( diff --git a/examples/shadow/main.rs b/examples/shadow/main.rs index c243e870a..036cba5ab 100644 --- a/examples/shadow/main.rs +++ b/examples/shadow/main.rs @@ -708,6 +708,7 @@ impl framework::Example for Example { frame: &wgpu::SwapChainTexture, device: &wgpu::Device, queue: &wgpu::Queue, + _spawner: &impl futures::task::LocalSpawn, ) -> wgpu::CommandBuffer { // update uniforms for entity in self.entities.iter_mut() { diff --git a/examples/skybox/main.rs b/examples/skybox/main.rs index 05cb66bfb..f0766fe26 100644 --- a/examples/skybox/main.rs +++ b/examples/skybox/main.rs @@ -1,3 +1,5 @@ +use futures::task::{LocalSpawn, LocalSpawnExt}; + #[path = "../framework.rs"] mod framework; @@ -19,6 +21,7 @@ pub struct Skybox { bind_group: wgpu::BindGroup, uniform_buf: wgpu::Buffer, uniforms: Uniforms, + staging_belt: wgpu::util::StagingBelt, } impl Skybox { @@ -234,6 +237,7 @@ impl framework::Example for Skybox { uniform_buf, aspect, uniforms, + staging_belt: wgpu::util::StagingBelt::new(0x100, device), }, None, ) @@ -261,15 +265,22 @@ impl framework::Example for Skybox { frame: &wgpu::SwapChainTexture, device: &wgpu::Device, queue: &wgpu::Queue, + spawner: &impl LocalSpawn, ) -> wgpu::CommandBuffer { // update rotation let rotation = cgmath::Matrix4::::from_angle_x(cgmath::Deg(0.25)); self.uniforms[1] = self.uniforms[1] * rotation; - queue.write_buffer( - &self.uniform_buf, - 0, - bytemuck::cast_slice(&raw_uniforms(&self.uniforms)), - ); + let raw_uniforms = raw_uniforms(&self.uniforms); + self.staging_belt + .write_buffer( + &self.uniform_buf, + 0, + wgpu::BufferSize::new((raw_uniforms.len() * 4) as wgpu::BufferAddress).unwrap(), + device, + ) + .copy_from_slice(bytemuck::cast_slice(&raw_uniforms)); + let upload_future = self.staging_belt.flush(queue, device); + spawner.spawn_local(upload_future).unwrap(); let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None }); diff --git a/examples/texture-arrays/main.rs b/examples/texture-arrays/main.rs index ce9b1b716..ca982bea3 100644 --- a/examples/texture-arrays/main.rs +++ b/examples/texture-arrays/main.rs @@ -362,6 +362,7 @@ impl framework::Example for Example { frame: &wgpu::SwapChainTexture, device: &wgpu::Device, _queue: &wgpu::Queue, + _spawner: &impl futures::task::LocalSpawn, ) -> wgpu::CommandBuffer { let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("primary"), diff --git a/examples/water/main.rs b/examples/water/main.rs index b280e248b..1de64f93a 100644 --- a/examples/water/main.rs +++ b/examples/water/main.rs @@ -680,6 +680,7 @@ impl framework::Example for Example { frame: &wgpu::SwapChainTexture, device: &wgpu::Device, queue: &wgpu::Queue, + _spawner: &impl futures::task::LocalSpawn, ) -> wgpu::CommandBuffer { // Increment frame count regardless of if we draw. self.current_frame += 1; diff --git a/src/lib.rs b/src/lib.rs index 87826c449..2defa601d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1433,7 +1433,7 @@ impl Buffer { } } -impl BufferSlice<'_> { +impl<'a> BufferSlice<'a> { //TODO: fn slice(&self) -> Self /// Map the buffer. Buffer is ready to map once the future is resolved. @@ -1473,7 +1473,7 @@ impl BufferSlice<'_> { /// Synchronously and immediately map a buffer for reading. If the buffer is not immediately mappable /// through [`BufferDescriptor::mapped_at_creation`] or [`BufferSlice::map_async`], will panic. - pub fn get_mapped_range(&self) -> BufferView { + pub fn get_mapped_range(&self) -> BufferView<'a> { let end = self.buffer.map_context.lock().add(self.offset, self.size); let data = Context::buffer_get_mapped_range( &*self.buffer.context, @@ -1485,7 +1485,7 @@ impl BufferSlice<'_> { /// Synchronously and immediately map a buffer for writing. If the buffer is not immediately mappable /// through [`BufferDescriptor::mapped_at_creation`] or [`BufferSlice::map_async`], will panic. - pub fn get_mapped_range_mut(&self) -> BufferViewMut { + pub fn get_mapped_range_mut(&self) -> BufferViewMut<'a> { let end = self.buffer.map_context.lock().add(self.offset, self.size); let data = Context::buffer_get_mapped_range_mut( &*self.buffer.context, diff --git a/src/util/belt.rs b/src/util/belt.rs new file mode 100644 index 000000000..c1a3fa30d --- /dev/null +++ b/src/util/belt.rs @@ -0,0 +1,132 @@ +use crate::{ + Buffer, BufferAddress, BufferDescriptor, BufferSize, BufferUsage, BufferViewMut, + CommandEncoder, CommandEncoderDescriptor, Device, MapMode, Queue, +}; +use futures::{future::join_all, FutureExt}; +use std::{future::Future, iter, mem, sync::mpsc}; + +struct Chunk { + buffer: Buffer, + size: BufferAddress, + offset: BufferAddress, +} + +/// Staging belt is a machine that uploads data. +/// +/// Internally it uses a ring-buffer of staging buffers that are sub-allocated. +/// It has an advantage over `Queue.write_buffer` in a way that it returns a mutable slice, +/// which you can fill to avoid an extra data copy. +pub struct StagingBelt { + chunk_size: BufferAddress, + encoder: CommandEncoder, + active_chunks: Vec, + free_chunks: Vec, + sender: mpsc::Sender, + receiver: mpsc::Receiver, +} + +impl StagingBelt { + /// Create a new staging belt. + /// + /// The `chunk_size` is the unit of internal buffer allocation. + /// It's better when it's big, but ideally still 1-4 times less than + /// the total amount of data uploaded per submission. + pub fn new(chunk_size: BufferAddress, device: &Device) -> Self { + let (sender, receiver) = mpsc::channel(); + StagingBelt { + chunk_size, + encoder: device.create_command_encoder(&CommandEncoderDescriptor::default()), + active_chunks: Vec::new(), + free_chunks: Vec::new(), + sender, + receiver, + } + } + + /// Allocate the staging belt slice of `size` to be uploaded into the `target` buffer + /// at the specified offset. + /// + /// The upload will only really be scheduled at the next `StagingBelt::flush` call. + pub fn write_buffer( + &mut self, + target: &Buffer, + offset: BufferAddress, + size: BufferSize, + device: &Device, + ) -> BufferViewMut { + assert_eq!( + size.get() % crate::COPY_BUFFER_ALIGNMENT, + 0, + "Size has to be aligned to the COPY_BUFFER_ALIGNMENT" + ); + + while let Ok(mut chunk) = self.receiver.try_recv() { + chunk.offset = 0; + self.free_chunks.push(chunk); + } + let mut chunk = if let Some(index) = self + .active_chunks + .iter() + .position(|chunk| chunk.offset + size.get() <= chunk.size) + { + self.active_chunks.swap_remove(index) + } else if let Some(index) = self + .free_chunks + .iter() + .position(|chunk| size.get() <= chunk.size) + { + self.free_chunks.swap_remove(index) + } else { + let size = self.chunk_size.max(size.get()); + //log::info!("Creating chunk of size {}", size); + Chunk { + buffer: device.create_buffer(&BufferDescriptor { + label: Some("staging"), + size, + usage: BufferUsage::MAP_WRITE | BufferUsage::COPY_SRC, + mapped_at_creation: true, + }), + size, + offset: 0, + } + }; + + self.encoder + .copy_buffer_to_buffer(&chunk.buffer, chunk.offset, target, offset, size.get()); + let old_offset = chunk.offset; + chunk.offset += size.get(); + self.active_chunks.push(chunk); + self.active_chunks + .last() + .unwrap() + .buffer + .slice(old_offset..old_offset + size.get()) + .get_mapped_range_mut() + } + + /// Schedule all the pending data uploads to the `queue`. + /// + /// At this point, all the partially used staging buffers are closed until + /// the GPU is done copying the data from them. + pub fn flush(&mut self, queue: &Queue, device: &Device) -> impl Future + Send { + //log::debug!("Flushing {} chunks", self.active_chunks.len()); + for chunk in self.active_chunks.iter() { + chunk.buffer.unmap(); + } + let current = mem::replace( + &mut self.encoder, + device.create_command_encoder(&CommandEncoderDescriptor::default()), + ); + queue.submit(iter::once(current.finish())); + let sender_template = &self.sender; + join_all(self.active_chunks.drain(..).map(|chunk| { + let sender = sender_template.clone(); + chunk + .buffer + .slice(..) + .map_async(MapMode::Write) + .inspect(move |_| sender.send(chunk).unwrap()) + })) + .map(|_| ()) + } +} diff --git a/src/util/mod.rs b/src/util/mod.rs index cc201a041..60dcb0515 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,6 +1,10 @@ +mod belt; + #[cfg(all(not(target_arch = "wasm32"), feature = "subscriber"))] pub use wgc::logging::subscriber::{initialize_default_subscriber, ChromeTracingLayer}; +pub use belt::StagingBelt; + /// Wrapper aligning contents to at least 4. #[repr(align(4))] pub struct WordAligned(pub Bytes);