From a7dd3c433f6e1a0d714e22829706f35b6745b8ae Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Thu, 27 Sep 2018 19:03:48 -0400 Subject: [PATCH] wgn: queue submit --- wgpu-native/src/command/mod.rs | 2 +- wgpu-native/src/device.rs | 49 +++++++++++++++++++++++++++++----- wgpu-native/src/lib.rs | 1 + wgpu-native/src/registry.rs | 31 +++++++++++++++++---- 4 files changed, 71 insertions(+), 12 deletions(-) diff --git a/wgpu-native/src/command/mod.rs b/wgpu-native/src/command/mod.rs index 492321e89..73abf9a83 100644 --- a/wgpu-native/src/command/mod.rs +++ b/wgpu-native/src/command/mod.rs @@ -70,7 +70,7 @@ pub struct TextureCopyView { } pub struct CommandBuffer { - raw: B::CommandBuffer, + pub(crate) raw: B::CommandBuffer, fence: B::Fence, recorded_thread_id: ThreadId, } diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 1021d43f2..3e881a793 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -1,15 +1,16 @@ -use hal::{self, Device as _Device, QueueGroup}; +use hal::{self, Device as _Device}; +use hal::queue::RawCommandQueue; use {command, conv, memory, pipeline, resource}; use registry::{self, Registry}; -use {BufferId, CommandBufferId, DeviceId, ShaderModuleId}; +use {BufferId, CommandBufferId, DeviceId, QueueId, ShaderModuleId}; -use std::slice; +use std::{iter, slice}; pub struct Device { device: B::Device, - queue_group: QueueGroup, + queue_group: hal::QueueGroup, mem_allocator: memory::SmartAllocator, com_allocator: command::CommandAllocator, } @@ -17,7 +18,7 @@ pub struct Device { impl Device { pub(crate) fn new( device: B::Device, - queue_group: QueueGroup, + queue_group: hal::QueueGroup, mem_props: hal::MemoryProperties, ) -> Self { Device { @@ -50,9 +51,45 @@ pub extern "C" fn wgpu_device_create_shader_module( #[no_mangle] pub extern "C" fn wgpu_device_create_command_buffer( device_id: DeviceId, - desc: command::CommandBufferDescriptor, + _desc: command::CommandBufferDescriptor, ) -> CommandBufferId { let device = registry::DEVICE_REGISTRY.get_mut(device_id); let cmd_buf = device.com_allocator.allocate(&device.device); registry::COMMAND_BUFFER_REGISTRY.register(cmd_buf) } + +#[no_mangle] +pub extern "C" fn wgpu_device_get_queue( + device_id: DeviceId, +) -> QueueId { + device_id +} + +#[no_mangle] +pub extern "C" fn wgpu_queue_submit( + queue_id: QueueId, + command_buffer_ptr: *const CommandBufferId, + command_buffer_count: usize, +) { + let mut device = registry::DEVICE_REGISTRY.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()` + for &cmb_id in command_buffer_ids { + let cmd_buf = registry::COMMAND_BUFFER_REGISTRY.take(cmb_id); + { + let submission = hal::queue::RawSubmission { + cmd_buffers: iter::once(&cmd_buf.raw), + wait_semaphores: &[], + signal_semaphores: &[], + }; + unsafe { + device.queue_group.queues[0] + .as_raw_mut() + .submit_raw(submission, None); + } + } + device.com_allocator.submit(cmd_buf); + } +} diff --git a/wgpu-native/src/lib.rs b/wgpu-native/src/lib.rs index 78f4de26f..bbae752c2 100644 --- a/wgpu-native/src/lib.rs +++ b/wgpu-native/src/lib.rs @@ -75,6 +75,7 @@ pub type AdapterId = Id; type AdapterHandle = hal::Adapter; pub type DeviceId = Id; type DeviceHandle = Device; +pub type QueueId = Id; pub type BufferId = Id; // Resource diff --git a/wgpu-native/src/registry.rs b/wgpu-native/src/registry.rs index a0552b2db..a57bb6f08 100644 --- a/wgpu-native/src/registry.rs +++ b/wgpu-native/src/registry.rs @@ -1,11 +1,12 @@ +#[cfg(not(feature = "remote"))] use std::marker::PhantomData; +#[cfg(not(feature = "remote"))] use std::os::raw::c_void; + #[cfg(feature = "remote")] use std::sync::Arc; #[cfg(feature = "remote")] use parking_lot::{Mutex, MutexGuard, MappedMutexGuard}; -#[cfg(feature = "remote")] -use std::{borrow, cmp, fmt, ops, ptr}; #[cfg(feature = "remote")] use hal::backend::FastHashMap; @@ -27,6 +28,7 @@ pub(crate) trait Registry { fn new() -> Self; fn register(&self, handle: T) -> Id; fn get_mut(&self, id: Id) -> RegistryItem; + fn take(&self, id: Id) -> T; } #[cfg(not(feature = "remote"))] @@ -43,18 +45,25 @@ impl Registry for LocalRegistry { } fn register(&self, handle: T) -> Id { - ::std::boxed::Box::into_raw(Box::new(handle)) as *mut _ as *mut c_void + Box::into_raw(Box::new(handle)) as *mut _ as *mut c_void } fn get_mut(&self, id: Id) -> RegistryItem { unsafe { (id as *mut T).as_mut() }.unwrap() } + + fn take(&self, id: Id) -> T { + unsafe { + *Box::from_raw(id as *mut T) + } + } } #[cfg(feature = "remote")] struct Registrations { next_id: Id, tracked: FastHashMap, + free: Vec, } #[cfg(feature = "remote")] @@ -63,6 +72,7 @@ impl Registrations { Registrations { next_id: 0, tracked: FastHashMap::default(), + free: Vec::new(), } } } @@ -82,15 +92,26 @@ impl Registry for RemoteRegistry { fn register(&self, handle: T) -> Id { let mut registrations = self.registrations.lock(); - let id = registrations.next_id; + let id = match registrations.free.pop() { + Some(id) => id, + None => { + registrations.next_id += 1; + registrations.next_id - 1 + } + }; registrations.tracked.insert(id, handle); - registrations.next_id += 1; id } fn get_mut(&self, id: Id) -> RegistryItem { MutexGuard::map(self.registrations.lock(), |r| r.tracked.get_mut(&id).unwrap()) } + + 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"))]