mirror of
https://github.com/gfx-rs/wgpu.git
synced 2024-11-21 22:33:49 +00:00
wgn: queue submit
This commit is contained in:
parent
212cc386f3
commit
a7dd3c433f
@ -70,7 +70,7 @@ pub struct TextureCopyView {
|
||||
}
|
||||
|
||||
pub struct CommandBuffer<B: hal::Backend> {
|
||||
raw: B::CommandBuffer,
|
||||
pub(crate) raw: B::CommandBuffer,
|
||||
fence: B::Fence,
|
||||
recorded_thread_id: ThreadId,
|
||||
}
|
||||
|
@ -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<B: hal::Backend> {
|
||||
device: B::Device,
|
||||
queue_group: QueueGroup<B, hal::General>,
|
||||
queue_group: hal::QueueGroup<B, hal::General>,
|
||||
mem_allocator: memory::SmartAllocator<B>,
|
||||
com_allocator: command::CommandAllocator<B>,
|
||||
}
|
||||
@ -17,7 +18,7 @@ pub struct Device<B: hal::Backend> {
|
||||
impl<B: hal::Backend> Device<B> {
|
||||
pub(crate) fn new(
|
||||
device: B::Device,
|
||||
queue_group: QueueGroup<B, hal::General>,
|
||||
queue_group: hal::QueueGroup<B, hal::General>,
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -75,6 +75,7 @@ pub type AdapterId = Id;
|
||||
type AdapterHandle = hal::Adapter<B>;
|
||||
pub type DeviceId = Id;
|
||||
type DeviceHandle = Device<B>;
|
||||
pub type QueueId = Id;
|
||||
pub type BufferId = Id;
|
||||
|
||||
// Resource
|
||||
|
@ -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<T> {
|
||||
fn new() -> Self;
|
||||
fn register(&self, handle: T) -> Id;
|
||||
fn get_mut(&self, id: Id) -> RegistryItem<T>;
|
||||
fn take(&self, id: Id) -> T;
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "remote"))]
|
||||
@ -43,18 +45,25 @@ impl<T> Registry<T> for LocalRegistry<T> {
|
||||
}
|
||||
|
||||
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<T> {
|
||||
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<T> {
|
||||
next_id: Id,
|
||||
tracked: FastHashMap<Id, T>,
|
||||
free: Vec<Id>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "remote")]
|
||||
@ -63,6 +72,7 @@ impl<T> Registrations<T> {
|
||||
Registrations {
|
||||
next_id: 0,
|
||||
tracked: FastHashMap::default(),
|
||||
free: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -82,15 +92,26 @@ impl<T> Registry<T> for RemoteRegistry<T> {
|
||||
|
||||
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<T> {
|
||||
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"))]
|
||||
|
Loading…
Reference in New Issue
Block a user