From e039b70cbb3b6297ea29dc878804a89ba296fc9e Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Tue, 11 Jun 2019 20:52:09 -0400 Subject: [PATCH 1/2] [rs] Add native surface creation --- wgpu/src/lib.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index f48ceb9ac7..562d113991 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -502,10 +502,29 @@ impl Instance { } } - #[cfg(feature = "metal")] - pub fn create_surface_with_metal_layer(&self, window: *mut std::ffi::c_void) -> Surface { + pub fn create_surface_from_xlib( + &self, + display: *mut *const std::ffi::c_void, + window: u64, + ) -> Surface { + Surface { + id: wgn::wgpu_instance_create_surface_from_xlib(self.id, display, window), + } + } + + pub fn create_surface_from_macos_layer(&self, layer: *mut std::ffi::c_void) -> Surface { + Surface { + id: wgn::wgpu_instance_create_surface_from_macos_layer(self.id, layer), + } + } + + pub fn create_surface_from_windows_hwnd( + &self, + hinstance: *mut std::ffi::c_void, + hwnd: *mut std::ffi::c_void, + ) -> Surface { Surface { - id: wgn::wgpu_instance_create_surface_from_macos_layer(self.id, window), + id: wgn::wgpu_instance_create_surface_from_windows_hwnd(self.id, hinstance, hwnd), } } } From 8653786e83b71688ebea5ef45d5e5abb42aa1352 Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Tue, 11 Jun 2019 21:02:30 -0400 Subject: [PATCH 2/2] [rs] Run cargo fmt --- wgpu/examples/cube/main.rs | 14 ++++--- wgpu/examples/framework.rs | 5 +-- wgpu/examples/hello-compute/main.rs | 2 +- wgpu/examples/hello-triangle/main.rs | 2 +- wgpu/examples/shadow/main.rs | 61 ++++++++++++---------------- wgpu/src/lib.rs | 39 ++++++++++++------ wgpu/tests/multithreaded_compute.rs | 46 +++++++++++---------- 7 files changed, 88 insertions(+), 81 deletions(-) diff --git a/wgpu/examples/cube/main.rs b/wgpu/examples/cube/main.rs index 98b39ab8c5..5e055407f1 100644 --- a/wgpu/examples/cube/main.rs +++ b/wgpu/examples/cube/main.rs @@ -63,7 +63,7 @@ fn create_vertices() -> (Vec, Vec) { fn create_texels(size: usize) -> Vec { use std::iter; - (0..size * size) + (0 .. size * size) .flat_map(|id| { // get high five for recognizing this ;) let cx = 3.0 * (id % size) as f32 / (size - 1) as f32 - 2.0; @@ -216,7 +216,7 @@ impl framework::Example for Example { binding: 0, resource: wgpu::BindingResource::Buffer { buffer: &uniform_buf, - range: 0..64, + range: 0 .. 64, }, }, wgpu::Binding { @@ -231,8 +231,12 @@ impl framework::Example for Example { }); // Create the render pipeline - let vs_bytes = framework::load_glsl(include_str!("shader.vert"), framework::ShaderStage::Vertex); - let fs_bytes = framework::load_glsl(include_str!("shader.frag"), framework::ShaderStage::Fragment); + let vs_bytes = + framework::load_glsl(include_str!("shader.vert"), framework::ShaderStage::Vertex); + let fs_bytes = framework::load_glsl( + include_str!("shader.frag"), + framework::ShaderStage::Fragment, + ); let vs_module = device.create_shader_module(&vs_bytes); let fs_module = device.create_shader_module(&fs_bytes); @@ -335,7 +339,7 @@ impl framework::Example for Example { rpass.set_bind_group(0, &self.bind_group, &[]); rpass.set_index_buffer(&self.index_buf, 0); rpass.set_vertex_buffers(&[(&self.vertex_buf, 0)]); - rpass.draw_indexed(0..self.index_count as u32, 0, 0..1); + rpass.draw_indexed(0 .. self.index_count as u32, 0, 0 .. 1); } device.get_queue().submit(&[encoder.finish()]); diff --git a/wgpu/examples/framework.rs b/wgpu/examples/framework.rs index 67534c6007..f945b84526 100644 --- a/wgpu/examples/framework.rs +++ b/wgpu/examples/framework.rs @@ -62,10 +62,7 @@ pub fn run(title: &str) { let window = Window::new(&events_loop).unwrap(); window.set_title(title); let hidpi_factor = window.get_hidpi_factor(); - let size = window - .get_inner_size() - .unwrap() - .to_physical(hidpi_factor); + let size = window.get_inner_size().unwrap().to_physical(hidpi_factor); let surface = instance.create_surface(&window); diff --git a/wgpu/examples/hello-compute/main.rs b/wgpu/examples/hello-compute/main.rs index d5c41a08f9..a56c557ae9 100644 --- a/wgpu/examples/hello-compute/main.rs +++ b/wgpu/examples/hello-compute/main.rs @@ -58,7 +58,7 @@ fn main() { binding: 0, resource: wgpu::BindingResource::Buffer { buffer: &storage_buffer, - range: 0..size, + range: 0 .. size, }, }], }); diff --git a/wgpu/examples/hello-triangle/main.rs b/wgpu/examples/hello-triangle/main.rs index 2892d36700..ceacf4e930 100644 --- a/wgpu/examples/hello-triangle/main.rs +++ b/wgpu/examples/hello-triangle/main.rs @@ -150,7 +150,7 @@ fn main() { }); rpass.set_pipeline(&render_pipeline); rpass.set_bind_group(0, &bind_group, &[]); - rpass.draw(0..3, 0..1); + rpass.draw(0 .. 3, 0 .. 1); } device.get_queue().submit(&[encoder.finish()]); diff --git a/wgpu/examples/shadow/main.rs b/wgpu/examples/shadow/main.rs index 6c752a5c45..fba01979d6 100644 --- a/wgpu/examples/shadow/main.rs +++ b/wgpu/examples/shadow/main.rs @@ -1,8 +1,4 @@ -use std::{ - mem, - ops::Range, - rc::Rc, -}; +use std::{mem, ops::Range, rc::Rc}; #[path = "../framework.rs"] mod framework; @@ -232,7 +228,7 @@ impl framework::Example for Example { binding: 0, resource: wgpu::BindingResource::Buffer { buffer: &plane_uniform_buf, - range: 0..entity_uniform_size, + range: 0 .. entity_uniform_size, }, }], }); @@ -306,7 +302,7 @@ impl framework::Example for Example { binding: 0, resource: wgpu::BindingResource::Buffer { buffer: &uniform_buf, - range: 0..entity_uniform_size, + range: 0 .. entity_uniform_size, }, }], }), @@ -338,7 +334,7 @@ impl framework::Example for Example { }); let shadow_view = shadow_texture.create_default_view(); - let mut shadow_target_views = (0..2) + let mut shadow_target_views = (0 .. 2) .map(|i| { Some(shadow_texture.create_view(&wgpu::TextureViewDescriptor { format: Self::SHADOW_FORMAT, @@ -361,7 +357,7 @@ impl framework::Example for Example { a: 1.0, }, fov: 60.0, - depth: 1.0..20.0, + depth: 1.0 .. 20.0, target_view: shadow_target_views[0].take().unwrap(), }, Light { @@ -373,11 +369,12 @@ impl framework::Example for Example { a: 1.0, }, fov: 45.0, - depth: 1.0..20.0, + depth: 1.0 .. 20.0, target_view: shadow_target_views[1].take().unwrap(), }, ]; - let light_uniform_size = (Self::MAX_LIGHTS * mem::size_of::()) as wgpu::BufferAddress; + let light_uniform_size = + (Self::MAX_LIGHTS * mem::size_of::()) as wgpu::BufferAddress; let light_uniform_buf = device.create_buffer(&wgpu::BufferDescriptor { size: light_uniform_size, usage: wgpu::BufferUsage::UNIFORM @@ -429,20 +426,16 @@ impl framework::Example for Example { binding: 0, resource: wgpu::BindingResource::Buffer { buffer: &uniform_buf, - range: 0..uniform_size, + range: 0 .. uniform_size, }, }], }); // Create the render pipeline - let vs_bytes = framework::load_glsl( - include_str!("bake.vert"), - framework::ShaderStage::Vertex, - ); - let fs_bytes = framework::load_glsl( - include_str!("bake.frag"), - framework::ShaderStage::Fragment, - ); + let vs_bytes = + framework::load_glsl(include_str!("bake.vert"), framework::ShaderStage::Vertex); + let fs_bytes = + framework::load_glsl(include_str!("bake.frag"), framework::ShaderStage::Fragment); let vs_module = device.create_shader_module(&vs_bytes); let fs_module = device.create_shader_module(&fs_bytes); @@ -493,14 +486,12 @@ impl framework::Example for Example { bindings: &[ wgpu::BindGroupLayoutBinding { binding: 0, // global - visibility: wgpu::ShaderStage::VERTEX - | wgpu::ShaderStage::FRAGMENT, + visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, ty: wgpu::BindingType::UniformBuffer, }, wgpu::BindGroupLayoutBinding { binding: 1, // lights - visibility: wgpu::ShaderStage::VERTEX - | wgpu::ShaderStage::FRAGMENT, + visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, ty: wgpu::BindingType::UniformBuffer, }, wgpu::BindGroupLayoutBinding { @@ -540,14 +531,14 @@ impl framework::Example for Example { binding: 0, resource: wgpu::BindingResource::Buffer { buffer: &uniform_buf, - range: 0..uniform_size, + range: 0 .. uniform_size, }, }, wgpu::Binding { binding: 1, resource: wgpu::BindingResource::Buffer { buffer: &light_uniform_buf, - range: 0..light_uniform_size, + range: 0 .. light_uniform_size, }, }, wgpu::Binding { @@ -562,10 +553,8 @@ impl framework::Example for Example { }); // Create the render pipeline - let vs_bytes = framework::load_glsl( - include_str!("forward.vert"), - framework::ShaderStage::Vertex, - ); + let vs_bytes = + framework::load_glsl(include_str!("forward.vert"), framework::ShaderStage::Vertex); let fs_bytes = framework::load_glsl( include_str!("forward.frag"), framework::ShaderStage::Fragment, @@ -683,8 +672,8 @@ impl framework::Example for Example { { let size = mem::size_of::() as wgpu::BufferAddress; - let temp_buf_data = device - .create_buffer_mapped(self.entities.len(), wgpu::BufferUsage::TRANSFER_SRC); + let temp_buf_data = + device.create_buffer_mapped(self.entities.len(), wgpu::BufferUsage::TRANSFER_SRC); for (i, entity) in self.entities.iter_mut().enumerate() { if entity.rotation_speed != 0.0 { @@ -719,8 +708,8 @@ impl framework::Example for Example { if self.lights_are_dirty { self.lights_are_dirty = false; let size = (self.lights.len() * mem::size_of::()) as wgpu::BufferAddress; - let temp_buf_data = device - .create_buffer_mapped(self.lights.len(), wgpu::BufferUsage::TRANSFER_SRC); + let temp_buf_data = + device.create_buffer_mapped(self.lights.len(), wgpu::BufferUsage::TRANSFER_SRC); for (i, light) in self.lights.iter().enumerate() { temp_buf_data.data[i] = light.to_raw(); } @@ -763,7 +752,7 @@ impl framework::Example for Example { pass.set_bind_group(1, &entity.bind_group, &[]); pass.set_index_buffer(&entity.index_buf, 0); pass.set_vertex_buffers(&[(&entity.vertex_buf, 0)]); - pass.draw_indexed(0..entity.index_count as u32, 0, 0..1); + pass.draw_indexed(0 .. entity.index_count as u32, 0, 0 .. 1); } } @@ -799,7 +788,7 @@ impl framework::Example for Example { pass.set_bind_group(1, &entity.bind_group, &[]); pass.set_index_buffer(&entity.index_buf, 0); pass.set_vertex_buffers(&[(&entity.vertex_buf, 0)]); - pass.draw_indexed(0..entity.index_count as u32, 0, 0..1); + pass.draw_indexed(0 .. entity.index_count as u32, 0, 0 .. 1); } } diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 562d113991..624320caa4 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -468,7 +468,7 @@ impl Instance { #[cfg(feature = "gl")] pub fn new(windowed_context: wgn::glutin::WindowedContext) -> Self { Instance { - id: wgn::wgpu_create_gl_instance(windowed_context) + id: wgn::wgpu_create_gl_instance(windowed_context), } } @@ -653,16 +653,17 @@ impl Device { module: desc.vertex_stage.module.id, entry_point: vertex_entry_point.as_ptr(), }; - let (_fragment_entry_point, fragment_stage) = if let Some(fragment_stage) = &desc.fragment_stage { - let fragment_entry_point = CString::new(fragment_stage.entry_point).unwrap(); - let fragment_stage = wgn::PipelineStageDescriptor { - module: fragment_stage.module.id, - entry_point: fragment_entry_point.as_ptr(), + let (_fragment_entry_point, fragment_stage) = + if let Some(fragment_stage) = &desc.fragment_stage { + let fragment_entry_point = CString::new(fragment_stage.entry_point).unwrap(); + let fragment_stage = wgn::PipelineStageDescriptor { + module: fragment_stage.module.id, + entry_point: fragment_entry_point.as_ptr(), + }; + (fragment_entry_point, Some(fragment_stage)) + } else { + (CString::default(), None) }; - (fragment_entry_point, Some(fragment_stage)) - } else { - (CString::default(), None) - }; let temp_color_states = desc.color_states.to_vec(); let temp_vertex_buffers = desc @@ -682,7 +683,9 @@ impl Device { &wgn::RenderPipelineDescriptor { layout: desc.layout.id, vertex_stage, - fragment_stage: fragment_stage.as_ref().map_or(ptr::null(), |fs| fs as *const _), + fragment_stage: fragment_stage + .as_ref() + .map_or(ptr::null(), |fs| fs as *const _), rasterization_state: desc.rasterization_state.clone(), primitive_topology: desc.primitive_topology, color_states: temp_color_states.as_ptr(), @@ -1101,7 +1104,12 @@ impl CommandEncoder { impl<'a> RenderPass<'a> { /// Sets the active bind group for a given bind group index. - pub fn set_bind_group(&mut self, index: u32, bind_group: &BindGroup, offsets: &[BufferAddress]) { + pub fn set_bind_group( + &mut self, + index: u32, + bind_group: &BindGroup, + offsets: &[BufferAddress], + ) { wgn::wgpu_render_pass_set_bind_group( self.id, index, @@ -1193,7 +1201,12 @@ impl<'a> Drop for RenderPass<'a> { impl<'a> ComputePass<'a> { /// Sets the active bind group for a given bind group index. - pub fn set_bind_group(&mut self, index: u32, bind_group: &BindGroup, offsets: &[BufferAddress]) { + pub fn set_bind_group( + &mut self, + index: u32, + bind_group: &BindGroup, + offsets: &[BufferAddress], + ) { wgn::wgpu_compute_pass_set_bind_group( self.id, index, diff --git a/wgpu/tests/multithreaded_compute.rs b/wgpu/tests/multithreaded_compute.rs index d0ac55e316..d09bc21ea9 100644 --- a/wgpu/tests/multithreaded_compute.rs +++ b/wgpu/tests/multithreaded_compute.rs @@ -1,17 +1,17 @@ #[test] #[cfg(any(feature = "vulkan", feature = "metal", feature = "dx12"))] fn multithreaded_compute() { + use std::sync::mpsc; use std::thread; use std::time::Duration; - use std::sync::mpsc; let thread_count = 8; let (tx, rx) = mpsc::channel(); - for _ in 0..thread_count { + for _ in 0 .. thread_count { let tx = tx.clone(); thread::spawn(move || { - let numbers = vec!(100, 100, 100); + let numbers = vec![100, 100, 100]; let size = (numbers.len() * std::mem::size_of::()) as wgpu::BufferAddress; @@ -45,13 +45,14 @@ fn multithreaded_compute() { | wgpu::BufferUsage::TRANSFER_SRC, }); - let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - bindings: &[wgpu::BindGroupLayoutBinding { - binding: 0, - visibility: wgpu::ShaderStage::COMPUTE, - ty: wgpu::BindingType::StorageBuffer, - }], - }); + let bind_group_layout = + device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + bindings: &[wgpu::BindGroupLayoutBinding { + binding: 0, + visibility: wgpu::ShaderStage::COMPUTE, + ty: wgpu::BindingType::StorageBuffer, + }], + }); let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { layout: &bind_group_layout, @@ -59,7 +60,7 @@ fn multithreaded_compute() { binding: 0, resource: wgpu::BindingResource::Buffer { buffer: &storage_buffer, - range: 0..size, + range: 0 .. size, }, }], }); @@ -68,15 +69,17 @@ fn multithreaded_compute() { bind_group_layouts: &[&bind_group_layout], }); - let compute_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { - layout: &pipeline_layout, - compute_stage: wgpu::PipelineStageDescriptor { - module: &cs_module, - entry_point: "main", - }, - }); + let compute_pipeline = + device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { + layout: &pipeline_layout, + compute_stage: wgpu::PipelineStageDescriptor { + module: &cs_module, + entry_point: "main", + }, + }); - let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 }); + let mut encoder = + device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 }); encoder.copy_buffer_to_buffer(&staging_buffer, 0, &storage_buffer, 0, size); { let mut cpass = encoder.begin_compute_pass(); @@ -95,7 +98,8 @@ fn multithreaded_compute() { }); } - for _ in 0..thread_count { - rx.recv_timeout(Duration::from_secs(10)).expect("A thread never completed."); + for _ in 0 .. thread_count { + rx.recv_timeout(Duration::from_secs(10)) + .expect("A thread never completed."); } }