Staging belt, use LocalSpawner for the framework

This commit is contained in:
Dzmitry Malyshau committed 2020-06-23 23:15:47 -04:00
1 parent 90307b6715
commit 4aa7abdff8
12 files changed
+207 -20

No files matched your search

+1 -2
View File
@@ -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 {
+1
View File
@@ -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 });
+45 -10
View File
@@ -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<E: Example>(event_loop: EventLoop<()>, window: Window) {
async fn run_async<E: Example, S: LocalSpawn + 'static + Sized>(
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<E: Example>(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<E: Example>(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<E: Example>(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<E: Example>(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<E: Example>(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<E: Example>(title: &str) {
wgpu::util::initialize_default_subscriber(chrome_tracing_dir.ok());
};
futures::executor::block_on(run_async::<E>(event_loop, window));
let mut local_pool = LocalPool::new();
let spawner = local_pool.spawner();
local_pool
.spawner()
.spawn_local(run_async::<E, LocalSpawner>(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<E: Example>(title: &str) {
.ok()
})
.expect("couldn't append canvas to document body");
wasm_bindgen_futures::spawn_local(run_async::<E>(event_loop, window));
wasm_bindgen_futures::spawn_local(run_async::<E, WebSpawner>(
event_loop,
window,
WebSpawner {},
));
}
}
+1
View File
@@ -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 });
+1
View File
@@ -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(
+1
View File
@@ -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() {
+16 -5
View File
@@ -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::<f32>::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 });
+1
View File
@@ -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"),
+1
View File
@@ -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;
+3 -3
View File
@@ -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,
+132
View File
@@ -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<Chunk>,
free_chunks: Vec<Chunk>,
sender: mpsc::Sender<Chunk>,
receiver: mpsc::Receiver<Chunk>,
}
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<Output = ()> + 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(|_| ())
}
}
+4
View File
@@ -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<Bytes: ?Sized>(pub Bytes);