diff options
Diffstat (limited to 'flowy/src/main.rs')
-rw-r--r-- | flowy/src/main.rs | 197 |
1 files changed, 27 insertions, 170 deletions
diff --git a/flowy/src/main.rs b/flowy/src/main.rs index 19084ef..7755ab8 100644 --- a/flowy/src/main.rs +++ b/flowy/src/main.rs @@ -1,15 +1,15 @@ -use std::io::{stdin, stdout, Read, Write}; +pub mod motion; +use motion::{enc::MotionEncoder, CommonBuffers, Params}; use pollster::FutureExt; +use std::io::{stdin, stdout, Read, Write}; use wgpu::{ - include_wgsl, Backends, BindGroupDescriptor, BindGroupEntry, BindGroupLayoutDescriptor, - BindGroupLayoutEntry, BindingType, CommandEncoder, ComputePipelineDescriptor, DeviceDescriptor, - Extent3d, Features, ImageCopyTexture, Instance, InstanceDescriptor, InstanceFlags, Limits, - MaintainBase, Origin3d, PipelineLayoutDescriptor, PowerPreference, RequestAdapterOptions, - ShaderStages, TextureDescriptor, TextureDimension, TextureFormat, TextureSampleType, - TextureUsages, TextureViewDimension, + Backends, DeviceDescriptor, Extent3d, Features, Instance, InstanceDescriptor, Limits, + MaintainBase, PowerPreference, RequestAdapterOptions, }; +use crate::motion::RoundParams; + fn main() { let instance = Instance::new(InstanceDescriptor { backends: Backends::all(), @@ -38,189 +38,46 @@ fn main() { .unwrap(); let (width, height) = (1920, 1080); - let extent = Extent3d { - width: width as u32, - height: height as u32, - depth_or_array_layers: 1, + let params = Params { + width, + height, + extent: Extent3d { + width: width as u32, + height: height as u32, + depth_or_array_layers: 1, + }, + block_width: 8, + block_height: 8, + blocks: (width / 8) * (height / 8), }; - let textures = [(), ()].map(|_| { - device.create_texture(&TextureDescriptor { - label: None, - size: extent, - mip_level_count: 1, - sample_count: 1, - dimension: TextureDimension::D2, - format: TextureFormat::Bgra8Unorm, - usage: TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST, - view_formats: &[], - }) - }); - let out_tex = device.create_texture(&TextureDescriptor { - label: None, - size: extent, - mip_level_count: 1, - sample_count: 1, - dimension: TextureDimension::D2, - format: TextureFormat::Bgra8Unorm, - usage: TextureUsages::STORAGE_BINDING | TextureUsages::COPY_SRC, - view_formats: &[], - }); + let bufs = CommonBuffers::create(&device, ¶ms); + let menc = MotionEncoder::create(&device, ¶ms, &bufs); let mut buffer = vec![0u8; width * height * 4]; - let download_buffer = device.create_buffer(&wgpu::BufferDescriptor { - label: None, - size: buffer.len() as u64, - usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ, - mapped_at_creation: false, - }); - - let bind_group_layout = device.create_bind_group_layout(&BindGroupLayoutDescriptor { - label: None, - entries: &[ - BindGroupLayoutEntry { - binding: 0, - count: None, - ty: wgpu::BindingType::StorageTexture { - access: wgpu::StorageTextureAccess::WriteOnly, - format: TextureFormat::Bgra8Unorm, - view_dimension: wgpu::TextureViewDimension::D2, - }, - visibility: ShaderStages::COMPUTE, - }, - BindGroupLayoutEntry { - binding: 1, - count: None, - ty: BindingType::Texture { - sample_type: TextureSampleType::Float { filterable: false }, - view_dimension: TextureViewDimension::D2, - multisampled: false, - }, - visibility: ShaderStages::COMPUTE, - }, - BindGroupLayoutEntry { - binding: 2, - count: None, - ty: BindingType::Texture { - sample_type: TextureSampleType::Float { filterable: false }, - view_dimension: TextureViewDimension::D2, - multisampled: false, - }, - visibility: ShaderStages::COMPUTE, - }, - ], - }); - let bind_groups = [false, true].map(|i| { - device.create_bind_group(&BindGroupDescriptor { - label: None, - layout: &bind_group_layout, - entries: &[ - BindGroupEntry { - binding: 0, - resource: wgpu::BindingResource::TextureView( - &out_tex.create_view(&Default::default()), - ), - }, - BindGroupEntry { - binding: 1, - resource: wgpu::BindingResource::TextureView( - &(if i { &textures[0] } else { &textures[1] }) - .create_view(&Default::default()), - ), - }, - BindGroupEntry { - binding: 2, - resource: wgpu::BindingResource::TextureView( - &(if i { &textures[1] } else { &textures[0] }) - .create_view(&Default::default()), - ), - }, - ], - }) - }); - - let pipeline_layout = device.create_pipeline_layout(&PipelineLayoutDescriptor { - label: None, - bind_group_layouts: &[&bind_group_layout], - push_constant_ranges: &[], - }); - - let module = device.create_shader_module(include_wgsl!("shader.wgsl")); - let pipeline = device.create_compute_pipeline(&ComputePipelineDescriptor { - label: None, - layout: Some(&pipeline_layout), - module: &module, - entry_point: "main", - }); - let mut i = 0; loop { + let rp = RoundParams { swap: i }; eprintln!("read"); stdin().read_exact(&mut buffer).unwrap(); eprintln!("upload"); - queue.write_texture( - ImageCopyTexture { - aspect: wgpu::TextureAspect::All, - mip_level: 0, - origin: Origin3d::ZERO, - texture: &textures[i], - }, - &buffer, - wgpu::ImageDataLayout { - offset: 0, - bytes_per_row: Some(extent.width * 4), - rows_per_image: Some(extent.height), - }, - extent, - ); + bufs.upload(&queue, ¶ms, &rp, &buffer); eprintln!("compute"); - let mut encoder = device.create_command_encoder(&Default::default()); - { - let mut cpass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor { - label: None, - timestamp_writes: None, - }); - cpass.set_pipeline(&pipeline); - cpass.set_bind_group(0, &bind_groups[i], &[]); - cpass.dispatch_workgroups(width as u32, height as u32, 1); - } + let mut encoder = device.create_command_encoder(&Default::default()); - encoder.copy_texture_to_buffer( - wgpu::ImageCopyTexture { - texture: &out_tex, - mip_level: 0, - origin: wgpu::Origin3d::ZERO, - aspect: wgpu::TextureAspect::All, - }, - wgpu::ImageCopyBuffer { - buffer: &download_buffer, - layout: wgpu::ImageDataLayout { - offset: 0, - bytes_per_row: Some(extent.width * 4), - rows_per_image: Some(extent.height), - }, - }, - extent, - ); + menc.pass(&mut encoder, ¶ms, &rp); + bufs.prepare_texture_download(&mut encoder, ¶ms, &rp); queue.submit(Some(encoder.finish())); device.poll(MaintainBase::Wait); eprintln!("download"); - let buffer_slice = download_buffer.slice(..); - let (sender, receiver) = oneshot::channel(); - buffer_slice.map_async(wgpu::MapMode::Read, move |r| sender.send(r).unwrap()); - device.poll(wgpu::Maintain::Wait); - receiver.recv().unwrap().unwrap(); - { - let view = buffer_slice.get_mapped_range(); - buffer.copy_from_slice(&view[..]); - } - download_buffer.unmap(); + bufs.download(&device, &mut buffer); + eprintln!("write"); stdout().write_all(&buffer).unwrap(); i += 1; |