diff --git a/examples/boids/main.rs b/examples/boids/main.rs index 2b4553e5f..a9304e18d 100644 --- a/examples/boids/main.rs +++ b/examples/boids/main.rs @@ -29,7 +29,7 @@ impl framework::Example for Example { sc_desc: &wgpu::SwapChainDescriptor, device: &wgpu::Device, _queue: &wgpu::Queue, - ) -> (Self, Option) { + ) -> Self { // load (and compile) shaders and create shader modules let boids_module = device.create_shader_module(wgpu::include_spirv!("boids.comp.spv")); @@ -219,18 +219,15 @@ impl framework::Example for Example { // returns Example struct and No encoder commands - ( - Example { - particle_bind_groups, - particle_buffers, - vertices_buffer, - compute_pipeline, - render_pipeline, - work_group_count, - frame_num: 0, - }, - None, - ) + Example { + particle_bind_groups, + particle_buffers, + vertices_buffer, + compute_pipeline, + render_pipeline, + work_group_count, + frame_num: 0, + } } /// update is called for any WindowEvent not handled by the framework @@ -254,9 +251,9 @@ impl framework::Example for Example { &mut self, frame: &wgpu::SwapChainTexture, device: &wgpu::Device, - _queue: &wgpu::Queue, + queue: &wgpu::Queue, _spawner: &impl futures::task::LocalSpawn, - ) -> wgpu::CommandBuffer { + ) { // create render pass descriptor let render_pass_descriptor = wgpu::RenderPassDescriptor { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { @@ -297,7 +294,7 @@ impl framework::Example for Example { self.frame_num += 1; // done - command_encoder.finish() + queue.submit(Some(command_encoder.finish())); } } diff --git a/examples/cube/main.rs b/examples/cube/main.rs index 3c1f30e47..f599c6935 100644 --- a/examples/cube/main.rs +++ b/examples/cube/main.rs @@ -116,7 +116,7 @@ impl framework::Example for Example { sc_desc: &wgpu::SwapChainDescriptor, device: &wgpu::Device, queue: &wgpu::Queue, - ) -> (Self, Option) { + ) -> Self { use std::mem; // Create the vertex and index buffers @@ -287,15 +287,14 @@ impl framework::Example for Example { }); // Done - let this = Example { + Example { vertex_buf, index_buf, index_count: index_data.len(), bind_group, uniform_buf, pipeline, - }; - (this, None) + } } fn update(&mut self, _event: winit::event::WindowEvent) { @@ -317,9 +316,9 @@ impl framework::Example for Example { &mut self, frame: &wgpu::SwapChainTexture, device: &wgpu::Device, - _queue: &wgpu::Queue, + queue: &wgpu::Queue, _spawner: &impl futures::task::LocalSpawn, - ) -> wgpu::CommandBuffer { + ) { let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None }); { @@ -349,7 +348,7 @@ impl framework::Example for Example { rpass.draw_indexed(0..self.index_count as u32, 0, 0..1); } - encoder.finish() + queue.submit(Some(encoder.finish())); } } diff --git a/examples/framework.rs b/examples/framework.rs index 2f49613e2..8e8a41e18 100644 --- a/examples/framework.rs +++ b/examples/framework.rs @@ -38,7 +38,7 @@ pub trait Example: 'static + Sized { sc_desc: &wgpu::SwapChainDescriptor, device: &wgpu::Device, queue: &wgpu::Queue, - ) -> (Self, Option); + ) -> Self; fn resize( &mut self, sc_desc: &wgpu::SwapChainDescriptor, @@ -52,7 +52,7 @@ pub trait Example: 'static + Sized { device: &wgpu::Device, queue: &wgpu::Queue, spawner: &impl LocalSpawn, - ) -> wgpu::CommandBuffer; + ); } async fn run_async( @@ -112,10 +112,7 @@ async fn run_async( let mut swap_chain = device.create_swap_chain(&surface, &sc_desc); log::info!("Initializing the example..."); - let (mut example, init_command_buf) = E::init(&sc_desc, &device, &queue); - if init_command_buf.is_some() { - queue.submit(init_command_buf); - } + let mut example = E::init(&sc_desc, &device, &queue); #[cfg(not(target_arch = "wasm32"))] let mut last_update_inst = Instant::now(); @@ -185,8 +182,7 @@ async fn run_async( } }; - let command_buf = example.render(&frame.output, &device, &queue, &spawner); - queue.submit(Some(command_buf)); + example.render(&frame.output, &device, &queue, &spawner); } _ => {} } diff --git a/examples/mipmap/main.rs b/examples/mipmap/main.rs index 62bf23326..ebf75cfa7 100644 --- a/examples/mipmap/main.rs +++ b/examples/mipmap/main.rs @@ -204,8 +204,8 @@ impl framework::Example for Example { fn init( sc_desc: &wgpu::SwapChainDescriptor, device: &wgpu::Device, - _queue: &wgpu::Queue, - ) -> (Self, Option) { + queue: &wgpu::Queue, + ) -> Self { use std::mem; let mut init_encoder = @@ -375,14 +375,14 @@ impl framework::Example for Example { // Done Self::generate_mipmaps(&mut init_encoder, &device, &texture, mip_level_count); + queue.submit(Some(init_encoder.finish())); - let this = Example { + Example { vertex_buf, bind_group, uniform_buf, draw_pipeline, - }; - (this, Some(init_encoder.finish())) + } } fn update(&mut self, _event: winit::event::WindowEvent) { @@ -404,9 +404,9 @@ impl framework::Example for Example { &mut self, frame: &wgpu::SwapChainTexture, device: &wgpu::Device, - _queue: &wgpu::Queue, + queue: &wgpu::Queue, _spawner: &impl futures::task::LocalSpawn, - ) -> wgpu::CommandBuffer { + ) { let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None }); { @@ -433,7 +433,7 @@ impl framework::Example for Example { rpass.draw(0..4, 0..1); } - encoder.finish() + queue.submit(Some(encoder.finish())); } } diff --git a/examples/msaa-line/main.rs b/examples/msaa-line/main.rs index b4024ebfa..f42c9f92f 100644 --- a/examples/msaa-line/main.rs +++ b/examples/msaa-line/main.rs @@ -132,7 +132,7 @@ impl framework::Example for Example { sc_desc: &wgpu::SwapChainDescriptor, device: &wgpu::Device, _queue: &wgpu::Queue, - ) -> (Self, Option) { + ) -> Self { log::info!("Press left/right arrow keys to change sample_count."); let sample_count = 4; @@ -179,7 +179,7 @@ impl framework::Example for Example { vertex_count, ); - let this = Example { + Example { bundle, vs_module, fs_module, @@ -190,8 +190,7 @@ impl framework::Example for Example { sample_count, rebuild_bundle: false, sc_desc: sc_desc.clone(), - }; - (this, None) + } } fn update(&mut self, event: winit::event::WindowEvent) { @@ -234,9 +233,9 @@ impl framework::Example for Example { &mut self, frame: &wgpu::SwapChainTexture, device: &wgpu::Device, - _queue: &wgpu::Queue, + queue: &wgpu::Queue, _spawner: &impl futures::task::LocalSpawn, - ) -> wgpu::CommandBuffer { + ) { if self.rebuild_bundle { self.bundle = Example::create_bundle( device, @@ -282,7 +281,7 @@ impl framework::Example for Example { .execute_bundles(iter::once(&self.bundle)); } - encoder.finish() + queue.submit(iter::once(encoder.finish())); } } diff --git a/examples/shadow/main.rs b/examples/shadow/main.rs index 036cba5ab..8da8837b4 100644 --- a/examples/shadow/main.rs +++ b/examples/shadow/main.rs @@ -1,4 +1,4 @@ -use std::{mem, ops::Range, rc::Rc}; +use std::{iter, mem, ops::Range, rc::Rc}; #[path = "../framework.rs"] mod framework; @@ -207,7 +207,7 @@ impl framework::Example for Example { sc_desc: &wgpu::SwapChainDescriptor, device: &wgpu::Device, _queue: &wgpu::Queue, - ) -> (Self, Option) { + ) -> Self { // Create the vertex and index buffers let vertex_size = mem::size_of::(); let (cube_vertex_data, cube_index_data) = create_cube(); @@ -656,7 +656,7 @@ impl framework::Example for Example { label: None, }); - let this = Example { + Example { entities, lights, lights_are_dirty: true, @@ -664,8 +664,7 @@ impl framework::Example for Example { forward_pass, forward_depth: depth_texture.create_default_view(), light_uniform_buf, - }; - (this, None) + } } fn update(&mut self, _event: winit::event::WindowEvent) { @@ -709,7 +708,7 @@ impl framework::Example for Example { device: &wgpu::Device, queue: &wgpu::Queue, _spawner: &impl futures::task::LocalSpawn, - ) -> wgpu::CommandBuffer { + ) { // update uniforms for entity in self.entities.iter_mut() { if entity.rotation_speed != 0.0 { @@ -811,7 +810,7 @@ impl framework::Example for Example { } } - encoder.finish() + queue.submit(iter::once(encoder.finish())); } } diff --git a/examples/skybox/main.rs b/examples/skybox/main.rs index f0766fe26..4a9b7836c 100644 --- a/examples/skybox/main.rs +++ b/examples/skybox/main.rs @@ -1,8 +1,8 @@ -use futures::task::{LocalSpawn, LocalSpawnExt}; - #[path = "../framework.rs"] mod framework; +use futures::task::{LocalSpawn, LocalSpawnExt}; + const SKYBOX_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8Unorm; type Uniform = cgmath::Matrix4; @@ -42,7 +42,7 @@ impl framework::Example for Skybox { sc_desc: &wgpu::SwapChainDescriptor, device: &wgpu::Device, queue: &wgpu::Queue, - ) -> (Self, Option) { + ) -> Self { let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { bindings: &[ wgpu::BindGroupLayoutEntry::new( @@ -230,17 +230,15 @@ impl framework::Example for Skybox { ], label: None, }); - ( - Self { - pipeline, - bind_group, - uniform_buf, - aspect, - uniforms, - staging_belt: wgpu::util::StagingBelt::new(0x100, device), - }, - None, - ) + + Skybox { + pipeline, + bind_group, + uniform_buf, + aspect, + uniforms, + staging_belt: wgpu::util::StagingBelt::new(0x100, device), + } } fn update(&mut self, _event: winit::event::WindowEvent) { @@ -266,7 +264,7 @@ impl framework::Example for Skybox { 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; @@ -279,8 +277,7 @@ impl framework::Example for Skybox { 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 transfer_comb = self.staging_belt.flush(device); let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None }); @@ -306,7 +303,11 @@ impl framework::Example for Skybox { rpass.set_bind_group(0, &self.bind_group, &[]); rpass.draw(0..3 as u32, 0..1); } - encoder.finish() + + queue.submit(vec![transfer_comb, encoder.finish()]); + + let belt_future = self.staging_belt.recall(); + spawner.spawn_local(belt_future).unwrap(); } } diff --git a/examples/texture-arrays/main.rs b/examples/texture-arrays/main.rs index ca982bea3..931bdba12 100644 --- a/examples/texture-arrays/main.rs +++ b/examples/texture-arrays/main.rs @@ -95,7 +95,7 @@ impl framework::Example for Example { sc_desc: &wgpu::SwapChainDescriptor, device: &wgpu::Device, queue: &wgpu::Queue, - ) -> (Self, Option) { + ) -> Self { let mut uniform_workaround = false; let vs_module = device.create_shader_module(wgpu::include_spirv!("shader.vert.spv")); let fs_bytes: Vec = match device.capabilities() { @@ -335,16 +335,13 @@ impl framework::Example for Example { alpha_to_coverage_enabled: false, }); - ( - Self { - vertex_buffer, - index_buffer, - bind_group, - pipeline, - uniform_workaround_data, - }, - None, - ) + Self { + vertex_buffer, + index_buffer, + bind_group, + pipeline, + uniform_workaround_data, + } } fn resize( &mut self, @@ -361,9 +358,9 @@ impl framework::Example for Example { &mut self, frame: &wgpu::SwapChainTexture, device: &wgpu::Device, - _queue: &wgpu::Queue, + queue: &wgpu::Queue, _spawner: &impl futures::task::LocalSpawn, - ) -> wgpu::CommandBuffer { + ) { let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("primary"), }); @@ -394,7 +391,7 @@ impl framework::Example for Example { drop(rpass); - encoder.finish() + queue.submit(Some(encoder.finish())); } } diff --git a/examples/water/main.rs b/examples/water/main.rs index 1de64f93a..050fe0ba1 100644 --- a/examples/water/main.rs +++ b/examples/water/main.rs @@ -4,7 +4,7 @@ mod framework; mod point_gen; use cgmath::Point3; -use std::mem; +use std::{iter, mem}; /// /// Radius of the terrain. @@ -269,7 +269,7 @@ impl framework::Example for Example { sc_desc: &wgpu::SwapChainDescriptor, device: &wgpu::Device, queue: &wgpu::Queue, - ) -> (Self, Option) { + ) -> Self { // Size of one water vertex let water_vertex_size = mem::size_of::(); @@ -609,7 +609,7 @@ impl framework::Example for Example { }); // Done - let this = Example { + Example { water_vertex_buf, water_vertex_count: water_vertices.len(), water_bind_group_layout, @@ -632,8 +632,7 @@ impl framework::Example for Example { current_frame: 0, active: Some(0), - }; - (this, None) + } } fn update(&mut self, _event: winit::event::WindowEvent) { @@ -681,7 +680,7 @@ impl framework::Example for Example { 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; let back_color = wgpu::Color { @@ -699,21 +698,21 @@ impl framework::Example for Example { bytemuck::cast_slice(&[water_sin, water_cos]), ); + // Only render valid frames. See resize method. + if let Some(active) = self.active { + if active >= self.current_frame { + return; + } + } else { + return; + } + // The encoder provides a way to turn our instructions here, into // a command buffer the GPU can understand. let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("Main Command Encoder"), }); - // Only render valid frames. See resize method. - if let Some(active) = self.active { - if active >= self.current_frame { - return encoder.finish(); - } - } else { - return encoder.finish(); - } - // First pass: render the reflection. { let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { @@ -792,7 +791,7 @@ impl framework::Example for Example { rpass.draw(0..self.water_vertex_count as u32, 0..1); } - encoder.finish() + queue.submit(iter::once(encoder.finish())); } } diff --git a/src/util/belt.rs b/src/util/belt.rs index c1a3fa30d..6d780785c 100644 --- a/src/util/belt.rs +++ b/src/util/belt.rs @@ -1,9 +1,9 @@ use crate::{ - Buffer, BufferAddress, BufferDescriptor, BufferSize, BufferUsage, BufferViewMut, - CommandEncoder, CommandEncoderDescriptor, Device, MapMode, Queue, + Buffer, BufferAddress, BufferDescriptor, BufferSize, BufferUsage, BufferViewMut, CommandBuffer, + CommandEncoder, CommandEncoderDescriptor, Device, MapMode, }; use futures::{future::join_all, FutureExt}; -use std::{future::Future, iter, mem, sync::mpsc}; +use std::{future::Future, mem, sync::mpsc}; struct Chunk { buffer: Buffer, @@ -19,7 +19,11 @@ struct Chunk { pub struct StagingBelt { chunk_size: BufferAddress, encoder: CommandEncoder, + /// Chunks that we are actively using for pending transfers at this moment. active_chunks: Vec, + /// Chunks that have scheduled transfers already. + closed_chunks: Vec, + /// Chunks that are back from the GPU and ready to be used. free_chunks: Vec, sender: mpsc::Sender, receiver: mpsc::Receiver, @@ -37,6 +41,7 @@ impl StagingBelt { chunk_size, encoder: device.create_command_encoder(&CommandEncoderDescriptor::default()), active_chunks: Vec::new(), + closed_chunks: Vec::new(), free_chunks: Vec::new(), sender, receiver, @@ -54,16 +59,6 @@ impl StagingBelt { 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() @@ -78,7 +73,8 @@ impl StagingBelt { self.free_chunks.swap_remove(index) } else { let size = self.chunk_size.max(size.get()); - //log::info!("Creating chunk of size {}", size); + #[cfg(not(target_arch = "wasm32"))] + wgc::span!(_guard, INFO, "Creating chunk of size {}", size); Chunk { buffer: device.create_buffer(&BufferDescriptor { label: Some("staging"), @@ -95,6 +91,11 @@ impl StagingBelt { .copy_buffer_to_buffer(&chunk.buffer, chunk.offset, target, offset, size.get()); let old_offset = chunk.offset; chunk.offset += size.get(); + let remainder = chunk.offset % crate::COPY_BUFFER_ALIGNMENT; + if remainder != 0 { + chunk.offset += crate::COPY_BUFFER_ALIGNMENT - remainder; + } + self.active_chunks.push(chunk); self.active_chunks .last() @@ -104,22 +105,37 @@ impl StagingBelt { .get_mapped_range_mut() } - /// Schedule all the pending data uploads to the `queue`. + /// Produce a command buffer with all the accumulated transfers. /// /// 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() { + pub fn flush(&mut self, device: &Device) -> CommandBuffer { + #[cfg(not(target_arch = "wasm32"))] + wgc::span!(_guard, DEBUG, "Flushing chunks"); + + for chunk in self.active_chunks.drain(..) { chunk.buffer.unmap(); + self.closed_chunks.push(chunk); } - let current = mem::replace( + + mem::replace( &mut self.encoder, device.create_command_encoder(&CommandEncoderDescriptor::default()), - ); - queue.submit(iter::once(current.finish())); + ) + .finish() + } + + /// Recall all of the closed buffers back for re-usal. + /// + /// This has to be called after the command buffer produced by `flush` is submitted! + pub fn recall(&mut self) -> impl Future + Send { + while let Ok(mut chunk) = self.receiver.try_recv() { + chunk.offset = 0; + self.free_chunks.push(chunk); + } + let sender_template = &self.sender; - join_all(self.active_chunks.drain(..).map(|chunk| { + join_all(self.closed_chunks.drain(..).map(|chunk| { let sender = sender_template.clone(); chunk .buffer