diff --git a/examples/hello_triangle_rust/main.rs b/examples/hello_triangle_rust/main.rs index 125771c30c..9985304b33 100644 --- a/examples/hello_triangle_rust/main.rs +++ b/examples/hello_triangle_rust/main.rs @@ -1,25 +1,20 @@ extern crate wgpu; fn main() { let instance = wgpu::Instance::new(); - let adapter = instance.get_adapter( - wgpu::AdapterDescriptor { - power_preference: wgpu::PowerPreference::LowPower, + let adapter = instance.get_adapter(wgpu::AdapterDescriptor { + power_preference: wgpu::PowerPreference::LowPower, + }); + let device = adapter.create_device(wgpu::DeviceDescriptor { + extensions: wgpu::Extensions { + anisotropic_filtering: false, }, - ); - let device = adapter.create_device( - wgpu::DeviceDescriptor { - extensions: wgpu::Extensions { - anisotropic_filtering: false, - }, - }, - ); + }); let vs_bytes = include_bytes!("./../data/hello_triangle.vert.spv"); let _vs = device.create_shader_module(vs_bytes); let fs_bytes = include_bytes!("./../data/hello_triangle.frag.spv"); let _fs = device.create_shader_module(fs_bytes); - let cmd_buf = device.create_command_buffer(wgpu::CommandBufferDescriptor { - }); + let cmd_buf = device.create_command_buffer(wgpu::CommandBufferDescriptor {}); let queue = device.get_queue(); queue.submit(&[cmd_buf]); } diff --git a/wgpu-native/src/command/allocator.rs b/wgpu-native/src/command/allocator.rs index ec5adbe348..cf7b05adf6 100644 --- a/wgpu-native/src/command/allocator.rs +++ b/wgpu-native/src/command/allocator.rs @@ -1,15 +1,14 @@ use super::CommandBuffer; -use hal::{self, Device}; use hal::command::RawCommandBuffer; use hal::pool::RawCommandPool; +use hal::{self, Device}; use std::collections::HashMap; //TODO: use `parking_lot::Mutex`? use std::sync::Mutex; use std::thread; - struct CommandPool { raw: B::CommandPool, available: Vec>, @@ -39,15 +38,13 @@ impl CommandAllocator { pub fn allocate(&self, device: &B::Device) -> CommandBuffer { let thread_id = thread::current().id(); let mut inner = self.inner.lock().unwrap(); - let pool = inner.pools - .entry(thread_id) - .or_insert_with(|| CommandPool { - raw: device.create_command_pool( - self.queue_family, - hal::pool::CommandPoolCreateFlags::RESET_INDIVIDUAL, - ), - available: Vec::new(), - }); + let pool = inner.pools.entry(thread_id).or_insert_with(|| CommandPool { + raw: device.create_command_pool( + self.queue_family, + hal::pool::CommandPoolCreateFlags::RESET_INDIVIDUAL, + ), + available: Vec::new(), + }); if let Some(cmd_buf) = pool.available.pop() { device.reset_fence(&cmd_buf.fence); @@ -65,11 +62,7 @@ impl CommandAllocator { } pub fn submit(&self, cmd_buf: CommandBuffer) { - self.inner - .lock() - .unwrap() - .pending - .push(cmd_buf); + self.inner.lock().unwrap().pending.push(cmd_buf); } pub fn recycle(&self, mut cmd_buf: CommandBuffer) { @@ -86,7 +79,7 @@ impl CommandAllocator { pub fn maintain(&self, device: &B::Device) { let mut inner = self.inner.lock().unwrap(); - for i in (0 .. inner.pending.len()).rev() { + for i in (0..inner.pending.len()).rev() { if device.get_fence_status(&inner.pending[i].fence) { let cmd_buf = inner.pending.swap_remove(i); inner diff --git a/wgpu-native/src/command/mod.rs b/wgpu-native/src/command/mod.rs index ee81ce088a..b0501094b6 100644 --- a/wgpu-native/src/command/mod.rs +++ b/wgpu-native/src/command/mod.rs @@ -15,7 +15,6 @@ use { use std::thread::ThreadId; - #[repr(C)] #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] pub enum LoadOp { diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index acf987cb0a..5b60aac87e 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -1,15 +1,14 @@ -use hal::{self, Device as _Device}; use hal::queue::RawCommandQueue; +use hal::{self, Device as _Device}; use {back, binding_model, command, conv, memory, pipeline}; -use std::{ffi, iter, slice}; use registry::{self, Items, Registry}; +use std::{ffi, iter, slice}; use { - AttachmentStateId, BindGroupLayoutId, BlendStateId, CommandBufferId, DepthStencilStateId, DeviceId, - PipelineLayoutId, QueueId, RenderPipelineId, ShaderModuleId, + AttachmentStateId, BindGroupLayoutId, BlendStateId, CommandBufferId, DepthStencilStateId, + DeviceId, PipelineLayoutId, QueueId, RenderPipelineId, ShaderModuleId, }; - pub struct Device { device: B::Device, queue_group: hal::QueueGroup, @@ -56,9 +55,11 @@ pub extern "C" fn wgpu_device_create_bind_group_layout( }), &[], ); - registry::BIND_GROUP_LAYOUT_REGISTRY.lock().register(binding_model::BindGroupLayout { - raw: descriptor_set_layout, - }) + registry::BIND_GROUP_LAYOUT_REGISTRY + .lock() + .register(binding_model::BindGroupLayout { + raw: descriptor_set_layout, + }) } #[no_mangle] @@ -76,9 +77,11 @@ pub extern "C" fn wgpu_device_create_pipeline_layout( let device = &device_guard.get(device_id).device; let pipeline_layout = device.create_pipeline_layout(descriptor_set_layouts.iter().map(|d| &d.raw), &[]); // TODO: push constants - registry::PIPELINE_LAYOUT_REGISTRY.lock().register(binding_model::PipelineLayout { - raw: pipeline_layout, - }) + registry::PIPELINE_LAYOUT_REGISTRY + .lock() + .register(binding_model::PipelineLayout { + raw: pipeline_layout, + }) } #[no_mangle] @@ -86,9 +89,11 @@ pub extern "C" fn wgpu_device_create_blend_state( _device_id: DeviceId, desc: pipeline::BlendStateDescriptor, ) -> BlendStateId { - registry::BLEND_STATE_REGISTRY.lock().register(pipeline::BlendState { - raw: conv::map_blend_state_descriptor(desc), - }) + registry::BLEND_STATE_REGISTRY + .lock() + .register(pipeline::BlendState { + raw: conv::map_blend_state_descriptor(desc), + }) } #[no_mangle] @@ -96,9 +101,11 @@ pub extern "C" fn wgpu_device_create_depth_stencil_state( device_id: DeviceId, desc: pipeline::DepthStencilStateDescriptor, ) -> DepthStencilStateId { - registry::DEPTH_STENCIL_STATE_REGISTRY.lock().register(pipeline::DepthStencilState { - raw: conv::map_depth_stencil_state(desc), - }) + registry::DEPTH_STENCIL_STATE_REGISTRY + .lock() + .register(pipeline::DepthStencilState { + raw: conv::map_depth_stencil_state(desc), + }) } #[no_mangle] @@ -111,7 +118,9 @@ pub extern "C" fn wgpu_device_create_shader_module( let shader = device .create_shader_module(unsafe { slice::from_raw_parts(desc.code.bytes, desc.code.length) }) .unwrap(); - registry::SHADER_MODULE_REGISTRY.lock().register(ShaderModule { raw: shader }) + registry::SHADER_MODULE_REGISTRY + .lock() + .register(ShaderModule { raw: shader }) } #[no_mangle] @@ -126,10 +135,8 @@ pub extern "C" fn wgpu_device_create_command_buffer( } #[no_mangle] -pub extern "C" fn wgpu_device_get_queue( - device_id: DeviceId, -) -> QueueId { - device_id +pub extern "C" fn wgpu_device_get_queue(device_id: DeviceId) -> QueueId { + device_id } #[no_mangle] @@ -140,9 +147,8 @@ pub extern "C" fn wgpu_queue_submit( ) { let mut device_guard = registry::DEVICE_REGISTRY.lock(); let device = device_guard.get_mut(queue_id); - let command_buffer_ids = unsafe { - slice::from_raw_parts(command_buffer_ptr, command_buffer_count) - }; + let command_buffer_ids = + unsafe { slice::from_raw_parts(command_buffer_ptr, command_buffer_count) }; //TODO: submit at once, requires `get_all()` let mut command_buffer_guard = registry::COMMAND_BUFFER_REGISTRY.lock(); for &cmb_id in command_buffer_ids { @@ -188,7 +194,9 @@ pub extern "C" fn wgpu_device_create_attachment_state( layouts: hal::image::Layout::Undefined..hal::image::Layout::Present, // TODO map } }).collect(); - registry::ATTACHMENT_STATE_REGISTRY.lock().register(pipeline::AttachmentState { raw: attachments }) + registry::ATTACHMENT_STATE_REGISTRY + .lock() + .register(pipeline::AttachmentState { raw: attachments }) } #[no_mangle] @@ -364,5 +372,7 @@ pub extern "C" fn wgpu_device_create_render_pipeline( .create_graphics_pipeline(&pipeline_desc, None) .unwrap(); - registry::RENDER_PIPELINE_REGISTRY.lock().register(pipeline::RenderPipeline { raw: pipeline }) + registry::RENDER_PIPELINE_REGISTRY + .lock() + .register(pipeline::RenderPipeline { raw: pipeline }) } diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index 5e57487db1..e534e47850 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -67,7 +67,9 @@ pub extern "C" fn wgpu_instance_get_adapter( PowerPreference::LowPower => low.or(high), PowerPreference::HighPerformance | PowerPreference::Default => high.or(low), }; - registry::ADAPTER_REGISTRY.lock().register(some.or(other).unwrap()) + registry::ADAPTER_REGISTRY + .lock() + .register(some.or(other).unwrap()) } #[no_mangle] @@ -79,5 +81,7 @@ pub extern "C" fn wgpu_adapter_create_device( let adapter = adapter_guard.get_mut(adapter_id); let (device, queue_group) = adapter.open_with::<_, hal::General>(1, |_qf| true).unwrap(); let mem_props = adapter.physical_device.memory_properties(); - registry::DEVICE_REGISTRY.lock().register(Device::new(device, queue_group, mem_props)) + registry::DEVICE_REGISTRY + .lock() + .register(Device::new(device, queue_group, mem_props)) } diff --git a/wgpu-native/src/lib.rs b/wgpu-native/src/lib.rs index d065a0e838..7046536423 100644 --- a/wgpu-native/src/lib.rs +++ b/wgpu-native/src/lib.rs @@ -40,7 +40,6 @@ pub use self::resource::*; use back::Backend as B; use registry::Id; - #[repr(C)] pub struct Color { pub r: f32, diff --git a/wgpu-native/src/registry.rs b/wgpu-native/src/registry.rs index 44184997d3..eb56750dff 100644 --- a/wgpu-native/src/registry.rs +++ b/wgpu-native/src/registry.rs @@ -10,9 +10,9 @@ use std::os::raw::c_void; use std::sync::Arc; use { - AdapterHandle, AttachmentStateHandle, BindGroupLayoutHandle, BlendStateHandle, CommandBufferHandle, - DepthStencilStateHandle, DeviceHandle, InstanceHandle, PipelineLayoutHandle, - RenderPipelineHandle, ShaderModuleHandle, + AdapterHandle, AttachmentStateHandle, BindGroupLayoutHandle, BlendStateHandle, + CommandBufferHandle, DepthStencilStateHandle, DeviceHandle, InstanceHandle, + PipelineLayoutHandle, RenderPipelineHandle, ShaderModuleHandle, }; #[cfg(not(feature = "remote"))] @@ -60,9 +60,7 @@ impl Items for LocalItems { } fn take(&mut self, id: Id) -> T { - unsafe { - *Box::from_raw(id as *mut T) - } + unsafe { *Box::from_raw(id as *mut T) } } } @@ -167,7 +165,8 @@ lazy_static! { pub(crate) static ref DEPTH_STENCIL_STATE_REGISTRY: ConcreteRegistry = ConcreteRegistry::new(); pub(crate) static ref DEVICE_REGISTRY: ConcreteRegistry = ConcreteRegistry::new(); - pub(crate) static ref COMMAND_BUFFER_REGISTRY: ConcreteRegistry = ConcreteRegistry::new(); + pub(crate) static ref COMMAND_BUFFER_REGISTRY: ConcreteRegistry = + ConcreteRegistry::new(); pub(crate) static ref INSTANCE_REGISTRY: ConcreteRegistry = ConcreteRegistry::new(); pub(crate) static ref PIPELINE_LAYOUT_REGISTRY: ConcreteRegistry = diff --git a/wgpu-rs/src/lib.rs b/wgpu-rs/src/lib.rs index b9b448092f..0497e52aaa 100644 --- a/wgpu-rs/src/lib.rs +++ b/wgpu-rs/src/lib.rs @@ -1,12 +1,10 @@ extern crate wgpu_native as wgn; pub use wgn::{ - Color, Origin3d, Extent3d, - AdapterDescriptor, Extensions, DeviceDescriptor, PowerPreference, - ShaderModuleDescriptor, CommandBufferDescriptor, + AdapterDescriptor, Color, CommandBufferDescriptor, DeviceDescriptor, Extensions, Extent3d, + Origin3d, PowerPreference, ShaderModuleDescriptor, }; - pub struct Instance { id: wgn::InstanceId, } @@ -31,7 +29,6 @@ pub struct Queue { id: wgn::QueueId, } - impl Instance { pub fn new() -> Self { Instance { @@ -56,7 +53,7 @@ impl Adapter { impl Device { pub fn create_shader_module(&self, spv: &[u8]) -> ShaderModule { - let desc = wgn::ShaderModuleDescriptor{ + let desc = wgn::ShaderModuleDescriptor { code: wgn::ByteArray { bytes: spv.as_ptr(), length: spv.len(),