From c8a60c780c6f8c669c1f5276d5696ccc330529bd Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Fri, 28 Sep 2018 00:31:09 -0600 Subject: [PATCH] Refactor after rebase --- wgpu-bindings/wgpu.h | 12 +++++++----- wgpu-native/src/device.rs | 18 ++++++++++++------ wgpu-native/src/registry.rs | 25 ++++++++++++------------- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/wgpu-bindings/wgpu.h b/wgpu-bindings/wgpu.h index bad2c2e057..8a1b918fba 100644 --- a/wgpu-bindings/wgpu.h +++ b/wgpu-bindings/wgpu.h @@ -128,9 +128,6 @@ typedef WGPUId WGPUCommandBufferId; typedef WGPUId WGPUInstanceId; -typedef struct { - -} WGPUCommandBufferDescriptor; typedef WGPUId WGPUAttachmentStateId; typedef struct { @@ -170,6 +167,10 @@ typedef struct { WGPUColorWriteFlags write_mask; } WGPUBlendStateDescriptor; +typedef struct { + +} WGPUCommandBufferDescriptor; + typedef WGPUId WGPUDepthStencilStateId; typedef struct { @@ -239,8 +240,6 @@ WGPURenderPassId wgpu_command_buffer_begin_render_pass(WGPUCommandBufferId _comm WGPUInstanceId wgpu_create_instance(void); -WGPUCommandBufferId wgpu_device_create_command_buffer(WGPUDeviceId device_id, - WGPUCommandBufferDescriptor desc); WGPUAttachmentStateId wgpu_device_create_attachment_state(WGPUDeviceId device_id, WGPUAttachmentStateDescriptor desc); @@ -250,6 +249,9 @@ WGPUBindGroupLayoutId wgpu_device_create_bind_group_layout(WGPUDeviceId device_i WGPUBlendStateId wgpu_device_create_blend_state(WGPUDeviceId _device_id, WGPUBlendStateDescriptor desc); +WGPUCommandBufferId wgpu_device_create_command_buffer(WGPUDeviceId device_id, + WGPUCommandBufferDescriptor _desc); + WGPUDepthStencilStateId wgpu_device_create_depth_stencil_state(WGPUDeviceId device_id, WGPUDepthStencilStateDescriptor desc); diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index b3fed9c22b..31bdeeb2d3 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -1,11 +1,11 @@ use hal::{self, Device as _Device}; use hal::queue::RawCommandQueue; -use {binding_model, command, conv, memory, pipeline, resource}; +use {back, binding_model, command, conv, memory, pipeline}; -use std::{iter, slice}; +use std::{ffi, iter, slice}; use registry::{self, Items, Registry}; use { - AttachmentStateId, BindGroupLayoutId, BlendStateId, BufferId, CommandBufferId, DepthStencilStateId, DeviceId, + AttachmentStateId, BindGroupLayoutId, BlendStateId, CommandBufferId, DepthStencilStateId, DeviceId, PipelineLayoutId, QueueId, RenderPipelineId, ShaderModuleId, }; @@ -119,7 +119,8 @@ pub extern "C" fn wgpu_device_create_command_buffer( device_id: DeviceId, _desc: command::CommandBufferDescriptor, ) -> CommandBufferId { - let device = registry::DEVICE_REGISTRY.get_mut(device_id); + let mut device_guard = registry::DEVICE_REGISTRY.lock(); + let device = device_guard.get_mut(device_id); let cmd_buf = device.com_allocator.allocate(&device.device); registry::COMMAND_BUFFER_REGISTRY.register(cmd_buf) } @@ -137,13 +138,15 @@ pub extern "C" fn wgpu_queue_submit( command_buffer_ptr: *const CommandBufferId, command_buffer_count: usize, ) { - let mut device = registry::DEVICE_REGISTRY.get_mut(queue_id); + 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) }; //TODO: submit at once, requires `get_all()` + let mut command_buffer_guard = registry::COMMAND_BUFFER_REGISTRY.lock(); for &cmb_id in command_buffer_ids { - let cmd_buf = registry::COMMAND_BUFFER_REGISTRY.take(cmb_id); + let cmd_buf = command_buffer_guard.take(cmb_id); { let submission = hal::queue::RawSubmission { cmd_buffers: iter::once(&cmd_buf.raw), @@ -158,6 +161,9 @@ pub extern "C" fn wgpu_queue_submit( } device.com_allocator.submit(cmd_buf); } +} + +#[no_mangle] pub extern "C" fn wgpu_device_create_attachment_state( device_id: DeviceId, desc: pipeline::AttachmentStateDescriptor, diff --git a/wgpu-native/src/registry.rs b/wgpu-native/src/registry.rs index d94aa29c4c..0016363b6a 100644 --- a/wgpu-native/src/registry.rs +++ b/wgpu-native/src/registry.rs @@ -37,7 +37,7 @@ pub(crate) trait Registry { pub(crate) trait Items { fn get(&self, id: Id) -> Item; fn get_mut(&mut self, id: Id) -> ItemMut; - fn take(&self, id: Id) -> T; + fn take(&mut self, id: Id) -> T; } #[cfg(not(feature = "remote"))] @@ -54,6 +54,12 @@ impl Items for LocalItems { fn get_mut(&mut self, id: Id) -> ItemMut { unsafe { (id as *mut T).as_mut() }.unwrap() } + + fn take(&mut self, id: Id) -> T { + unsafe { + *Box::from_raw(id as *mut T) + } + } } #[cfg(not(feature = "remote"))] @@ -78,12 +84,6 @@ impl Registry for LocalRegistry { marker: PhantomData, } } - - fn take(&self, id: Id) -> T { - unsafe { - *Box::from_raw(id as *mut T) - } - } } #[cfg(feature = "remote")] @@ -113,6 +113,11 @@ impl Items for RemoteItems { fn get_mut(&mut self, id: Id) -> ItemMut { self.tracked.get_mut(&id).unwrap() } + + fn take(&mut self, id: Id) -> T { + self.free.push(id); + self.tracked.remove(&id).unwrap() + } } #[cfg(feature = "remote")] @@ -144,12 +149,6 @@ impl Registry for RemoteRegistry { fn lock(&self) -> ItemsGuard { self.items.lock() } - - fn take(&self, id: Id) -> T { - let mut registrations = self.registrations.lock(); - registrations.free.push(id); - registrations.tracked.remove(&id).unwrap() - } } #[cfg(not(feature = "remote"))]