17: Registry major refactor and compute passes r=grovesNL a=kvark



Co-authored-by: Dzmitry Malyshau <kvark@mozilla.com>
This commit is contained in:
bors[bot] 2018-10-02 19:08:47 +00:00
commit 15883ab21c
11 changed files with 319 additions and 254 deletions

View File

@ -1,7 +1,37 @@
use hal;
//use {CommandBuffer, CommandBufferId, ComputePassId};
use registry::{HUB, Items, Registry};
use {
Stored,
CommandBufferId, ComputePassId
};
pub struct ComputePass<B: hal::Backend> {
raw: B::CommandBuffer,
cmb_id: Stored<CommandBufferId>,
}
impl<B: hal::Backend> ComputePass<B> {
pub fn new(raw: B::CommandBuffer, cmb_id: CommandBufferId) -> Self {
ComputePass {
raw,
cmb_id: Stored(cmb_id),
}
}
}
#[no_mangle]
pub extern "C" fn wgpu_compute_pass_end_pass(
pass_id: ComputePassId,
) -> CommandBufferId {
let pass = HUB.compute_passes
.lock()
.take(pass_id);
HUB.command_buffers
.lock()
.get_mut(pass.cmb_id.0)
.raw = Some(pass.raw);
pass.cmb_id.0
}

View File

@ -12,7 +12,7 @@ use {
Color, Origin3d, Stored,
BufferId, CommandBufferId, ComputePassId, DeviceId, RenderPassId, TextureId, TextureViewId,
};
use registry::{self, Items, Registry};
use registry::{HUB, Items, Registry};
use std::thread::ThreadId;
@ -86,13 +86,13 @@ pub extern "C" fn wgpu_command_buffer_begin_render_pass(
command_buffer_id: CommandBufferId,
_descriptor: RenderPassDescriptor<TextureViewId>,
) -> RenderPassId {
let mut cmb_guard = registry::COMMAND_BUFFER_REGISTRY.lock();
let mut cmb = cmb_guard.get_mut(command_buffer_id);
let mut cmb_guard = HUB.command_buffers.lock();
let cmb = cmb_guard.get_mut(command_buffer_id);
let raw = cmb.raw.take().unwrap();
let mut device_guard = registry::DEVICE_REGISTRY.lock();
let device = &device_guard.get(cmb.device_id.0).raw;
let device_guard = HUB.devices.lock();
let _device = &device_guard.get(cmb.device_id.0).raw;
//let render_pass = device.create_render_pass();
//let framebuffer = device.create_framebuffer();
@ -106,12 +106,21 @@ pub extern "C" fn wgpu_command_buffer_begin_render_pass(
hal::SubpassContents::Inline,
);*/
registry::RENDER_PASS_REGISTRY
HUB.render_passes
.lock()
.register(RenderPass::new(raw, command_buffer_id))
}
#[no_mangle]
pub extern "C" fn wgpu_command_buffer_begin_compute_pass() -> ComputePassId {
unimplemented!()
pub extern "C" fn wgpu_command_buffer_begin_compute_pass(
command_buffer_id: CommandBufferId,
) -> ComputePassId {
let mut cmb_guard = HUB.command_buffers.lock();
let cmb = cmb_guard.get_mut(command_buffer_id);
let raw = cmb.raw.take().unwrap();
HUB.compute_passes
.lock()
.register(ComputePass::new(raw, command_buffer_id))
}

View File

@ -1,21 +1,18 @@
use hal;
use hal::command::RawCommandBuffer;
use registry::{self, Items, Registry};
use registry::{HUB, Items, Registry};
use {
Stored,
CommandBufferId, RenderPassId,
};
pub struct RenderPass<B: hal::Backend> {
raw: B::CommandBuffer,
cmb_id: Stored<CommandBufferId>,
}
// This is needed for `cmb_id` - would be great to remove.
#[cfg(not(feature = "remote"))]
unsafe impl<B: hal::Backend> Sync for RenderPass<B> {}
impl<B: hal::Backend> RenderPass<B> {
pub fn new(raw: B::CommandBuffer, cmb_id: CommandBufferId) -> Self {
RenderPass {
@ -27,16 +24,16 @@ impl<B: hal::Backend> RenderPass<B> {
#[no_mangle]
pub extern "C" fn wgpu_render_pass_end_pass(
render_pass_id: RenderPassId,
pass_id: RenderPassId,
) -> CommandBufferId {
let mut rp = registry::RENDER_PASS_REGISTRY
let mut pass = HUB.render_passes
.lock()
.take(render_pass_id);
rp.raw.end_render_pass();
.take(pass_id);
pass.raw.end_render_pass();
registry::COMMAND_BUFFER_REGISTRY
HUB.command_buffers
.lock()
.get_mut(rp.cmb_id.0)
.raw = Some(rp.raw);
rp.cmb_id.0
.get_mut(pass.cmb_id.0)
.raw = Some(pass.raw);
pass.cmb_id.0
}

View File

@ -1,15 +1,17 @@
use hal::command::RawCommandBuffer;
use hal::queue::RawCommandQueue;
use hal::{self, Device as _Device};
use {back, binding_model, command, conv, memory, pipeline};
use registry::{self, Items, Registry};
use std::{ffi, iter, slice};
use registry::{HUB, Items, Registry};
use {
AttachmentStateId, BindGroupLayoutId, BlendStateId, CommandBufferId, DepthStencilStateId,
DeviceId, PipelineLayoutId, QueueId, RenderPipelineId, ShaderModuleId,
};
use hal::command::RawCommandBuffer;
use hal::queue::RawCommandQueue;
use hal::{self, Device as _Device};
use std::{ffi, slice};
pub struct Device<B: hal::Backend> {
pub(crate) raw: B::Device,
queue_group: hal::QueueGroup<B, hal::General>,
@ -42,7 +44,7 @@ pub extern "C" fn wgpu_device_create_bind_group_layout(
desc: binding_model::BindGroupLayoutDescriptor,
) -> BindGroupLayoutId {
let bindings = unsafe { slice::from_raw_parts(desc.bindings, desc.bindings_length) };
let device_guard = registry::DEVICE_REGISTRY.lock();
let device_guard = HUB.devices.lock();
let device = device_guard.get(device_id);
let descriptor_set_layout = device.raw.create_descriptor_set_layout(
bindings.iter().map(|binding| {
@ -56,7 +58,7 @@ pub extern "C" fn wgpu_device_create_bind_group_layout(
}),
&[],
);
registry::BIND_GROUP_LAYOUT_REGISTRY
HUB.bind_group_layouts
.lock()
.register(binding_model::BindGroupLayout {
raw: descriptor_set_layout,
@ -68,17 +70,17 @@ pub extern "C" fn wgpu_device_create_pipeline_layout(
device_id: DeviceId,
desc: binding_model::PipelineLayoutDescriptor,
) -> PipelineLayoutId {
let bind_group_layout_guard = registry::BIND_GROUP_LAYOUT_REGISTRY.lock();
let bind_group_layout_guard = HUB.bind_group_layouts.lock();
let descriptor_set_layouts =
unsafe { slice::from_raw_parts(desc.bind_group_layouts, desc.bind_group_layouts_length) }
.iter()
.map(|id| bind_group_layout_guard.get(id.clone()))
.collect::<Vec<_>>();
let device_guard = registry::DEVICE_REGISTRY.lock();
let device_guard = HUB.devices.lock();
let device = &device_guard.get(device_id).raw;
let pipeline_layout =
device.create_pipeline_layout(descriptor_set_layouts.iter().map(|d| &d.raw), &[]); // TODO: push constants
registry::PIPELINE_LAYOUT_REGISTRY
HUB.pipeline_layouts
.lock()
.register(binding_model::PipelineLayout {
raw: pipeline_layout,
@ -90,7 +92,7 @@ pub extern "C" fn wgpu_device_create_blend_state(
_device_id: DeviceId,
desc: pipeline::BlendStateDescriptor,
) -> BlendStateId {
registry::BLEND_STATE_REGISTRY
HUB.blend_states
.lock()
.register(pipeline::BlendState {
raw: conv::map_blend_state_descriptor(desc),
@ -102,7 +104,7 @@ pub extern "C" fn wgpu_device_create_depth_stencil_state(
_device_id: DeviceId,
desc: pipeline::DepthStencilStateDescriptor,
) -> DepthStencilStateId {
registry::DEPTH_STENCIL_STATE_REGISTRY
HUB.depth_stencil_states
.lock()
.register(pipeline::DepthStencilState {
raw: conv::map_depth_stencil_state(desc),
@ -114,12 +116,12 @@ pub extern "C" fn wgpu_device_create_shader_module(
device_id: DeviceId,
desc: pipeline::ShaderModuleDescriptor,
) -> ShaderModuleId {
let device_guard = registry::DEVICE_REGISTRY.lock();
let device_guard = HUB.devices.lock();
let device = &device_guard.get(device_id).raw;
let shader = device
.create_shader_module(unsafe { slice::from_raw_parts(desc.code.bytes, desc.code.length) })
.unwrap();
registry::SHADER_MODULE_REGISTRY
HUB.shader_modules
.lock()
.register(ShaderModule { raw: shader })
}
@ -129,14 +131,14 @@ pub extern "C" fn wgpu_device_create_command_buffer(
device_id: DeviceId,
_desc: command::CommandBufferDescriptor,
) -> CommandBufferId {
let mut device_guard = registry::DEVICE_REGISTRY.lock();
let mut device_guard = HUB.devices.lock();
let device = device_guard.get_mut(device_id);
let mut cmd_buf = device.com_allocator.allocate(device_id, &device.raw);
cmd_buf.raw.as_mut().unwrap().begin(
hal::command::CommandBufferFlags::ONE_TIME_SUBMIT,
hal::command::CommandBufferInheritanceInfo::default(),
);
registry::COMMAND_BUFFER_REGISTRY.lock().register(cmd_buf)
HUB.command_buffers.lock().register(cmd_buf)
}
#[no_mangle]
@ -150,28 +152,48 @@ pub extern "C" fn wgpu_queue_submit(
command_buffer_ptr: *const CommandBufferId,
command_buffer_count: usize,
) {
let mut device_guard = registry::DEVICE_REGISTRY.lock();
let mut device_guard = HUB.devices.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();
let mut command_buffer_guard = HUB.command_buffers.lock();
let command_buffer_ids = unsafe {
slice::from_raw_parts(command_buffer_ptr, command_buffer_count)
};
// finish all the command buffers first
for &cmb_id in command_buffer_ids {
let mut cmd_buf = command_buffer_guard.take(cmb_id);
{
let mut raw = cmd_buf.raw.as_mut().unwrap();
raw.finish();
let submission = hal::queue::RawSubmission {
cmd_buffers: iter::once(raw),
wait_semaphores: &[],
signal_semaphores: &[],
};
unsafe {
device.queue_group.queues[0]
.as_raw_mut()
.submit_raw(submission, None);
}
command_buffer_guard
.get_mut(cmb_id)
.raw
.as_mut()
.unwrap()
.finish();
}
// now prepare the GPU submission
{
let submission = hal::queue::RawSubmission {
cmd_buffers: command_buffer_ids
.iter()
.map(|&cmb_id| {
command_buffer_guard
.get(cmb_id)
.raw
.as_ref()
.unwrap()
}),
wait_semaphores: &[],
signal_semaphores: &[],
};
unsafe {
device.queue_group.queues[0]
.as_raw_mut()
.submit_raw(submission, None);
}
}
// finally, return the command buffers to the allocator
for &cmb_id in command_buffer_ids {
let cmd_buf = command_buffer_guard.take(cmb_id);
device.com_allocator.submit(cmd_buf);
}
}
@ -181,7 +203,7 @@ pub extern "C" fn wgpu_device_create_attachment_state(
device_id: DeviceId,
desc: pipeline::AttachmentStateDescriptor,
) -> AttachmentStateId {
let device_guard = registry::DEVICE_REGISTRY.lock();
let device_guard = HUB.devices.lock();
let device = &device_guard.get(device_id).raw;
let color_formats = unsafe {
@ -224,7 +246,7 @@ pub extern "C" fn wgpu_device_create_attachment_state(
depth_stencil_format,
};
registry::ATTACHMENT_STATE_REGISTRY
HUB.attachment_states
.lock()
.register(at_state)
}
@ -240,12 +262,12 @@ pub extern "C" fn wgpu_device_create_render_pipeline(
height: 100,
};
let device_guard = registry::DEVICE_REGISTRY.lock();
let device_guard = HUB.devices.lock();
let device = &device_guard.get(device_id).raw;
let pipeline_layout_guard = registry::PIPELINE_LAYOUT_REGISTRY.lock();
let pipeline_layout_guard = HUB.pipeline_layouts.lock();
let layout = &pipeline_layout_guard.get(desc.layout).raw;
let pipeline_stages = unsafe { slice::from_raw_parts(desc.stages, desc.stages_length) };
let shader_module_guard = registry::SHADER_MODULE_REGISTRY.lock();
let shader_module_guard = HUB.shader_modules.lock();
let shaders = {
let mut vertex = None;
let mut fragment = None;
@ -303,7 +325,7 @@ pub extern "C" fn wgpu_device_create_render_pipeline(
primitive_restart: hal::pso::PrimitiveRestart::Disabled, // TODO
};
let blend_state_guard = registry::BLEND_STATE_REGISTRY.lock();
let blend_state_guard = HUB.blend_states.lock();
let blend_state = unsafe { slice::from_raw_parts(desc.blend_state, desc.blend_state_length) }
.iter()
.map(|id| blend_state_guard.get(id.clone()).raw)
@ -314,7 +336,7 @@ pub extern "C" fn wgpu_device_create_render_pipeline(
targets: blend_state,
};
let depth_stencil_state_guard = registry::DEPTH_STENCIL_STATE_REGISTRY.lock();
let depth_stencil_state_guard = HUB.depth_stencil_states.lock();
let depth_stencil = depth_stencil_state_guard.get(desc.depth_stencil_state).raw;
// TODO
@ -341,7 +363,7 @@ pub extern "C" fn wgpu_device_create_render_pipeline(
depth_bounds: None,
};
let attachment_state_guard = registry::ATTACHMENT_STATE_REGISTRY.lock();
let attachment_state_guard = HUB.attachment_states.lock();
let attachment_state = attachment_state_guard.get(desc.attachment_state);
// TODO
@ -377,7 +399,7 @@ pub extern "C" fn wgpu_device_create_render_pipeline(
.create_graphics_pipeline(&pipeline_desc, None)
.unwrap();
registry::RENDER_PIPELINE_REGISTRY
HUB.render_pipelines
.lock()
.register(pipeline::RenderPipeline { raw: pipeline })
}

View File

@ -1,6 +1,6 @@
use hal::{self, Instance as _Instance, PhysicalDevice as _PhysicalDevice};
use registry::{self, Items, Registry};
use registry::{HUB, Items, Registry};
use {AdapterId, Device, DeviceId, InstanceId};
#[repr(C)]
@ -35,7 +35,7 @@ pub extern "C" fn wgpu_create_instance() -> InstanceId {
))]
{
let inst = ::back::Instance::create("wgpu", 1);
registry::INSTANCE_REGISTRY.lock().register(inst)
HUB.instances.lock().register(inst)
}
#[cfg(not(any(
feature = "gfx-backend-vulkan",
@ -52,7 +52,7 @@ pub extern "C" fn wgpu_instance_get_adapter(
instance_id: InstanceId,
desc: AdapterDescriptor,
) -> AdapterId {
let instance_guard = registry::INSTANCE_REGISTRY.lock();
let instance_guard = HUB.instances.lock();
let instance = instance_guard.get(instance_id);
let (mut low, mut high, mut other) = (None, None, None);
for adapter in instance.enumerate_adapters() {
@ -67,7 +67,7 @@ pub extern "C" fn wgpu_instance_get_adapter(
PowerPreference::LowPower => low.or(high),
PowerPreference::HighPerformance | PowerPreference::Default => high.or(low),
};
registry::ADAPTER_REGISTRY
HUB.adapters
.lock()
.register(some.or(other).unwrap())
}
@ -77,11 +77,11 @@ pub extern "C" fn wgpu_adapter_create_device(
adapter_id: AdapterId,
_desc: DeviceDescriptor,
) -> DeviceId {
let mut adapter_guard = registry::ADAPTER_REGISTRY.lock();
let mut adapter_guard = HUB.adapters.lock();
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
HUB.devices
.lock()
.register(Device::new(device, queue_group, mem_props))
}

View File

@ -126,3 +126,4 @@ type CommandBufferHandle = CommandBuffer<B>;
pub type RenderPassId = Id;
type RenderPassHandle = RenderPass<B>;
pub type ComputePassId = Id;
type ComputePassHandle = ComputePass<B>;

View File

@ -1,181 +0,0 @@
#[cfg(feature = "remote")]
use hal::backend::FastHashMap;
#[cfg(feature = "remote")]
use parking_lot::{Mutex, MutexGuard};
#[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;
use {
AdapterHandle, AttachmentStateHandle, BindGroupLayoutHandle, BlendStateHandle,
CommandBufferHandle, DepthStencilStateHandle, DeviceHandle, InstanceHandle,
RenderPassHandle,
PipelineLayoutHandle, RenderPipelineHandle, ShaderModuleHandle,
};
#[cfg(not(feature = "remote"))]
pub(crate) type Id = *mut c_void;
#[cfg(feature = "remote")]
pub(crate) type Id = u32;
type Item<'a, T> = &'a T;
type ItemMut<'a, T> = &'a mut T;
#[cfg(not(feature = "remote"))]
type ItemsGuard<'a, T> = LocalItems<T>;
#[cfg(feature = "remote")]
type ItemsGuard<'a, T> = MutexGuard<'a, RemoteItems<T>>;
pub(crate) trait Registry<T> {
fn new() -> Self;
fn lock(&self) -> ItemsGuard<T>;
}
pub(crate) trait Items<T> {
fn register(&mut self, handle: T) -> Id;
fn get(&self, id: Id) -> Item<T>;
fn get_mut(&mut self, id: Id) -> ItemMut<T>;
fn take(&mut self, id: Id) -> T;
}
#[cfg(not(feature = "remote"))]
pub(crate) struct LocalItems<T> {
marker: PhantomData<T>,
}
#[cfg(not(feature = "remote"))]
impl<T> Items<T> for LocalItems<T> {
fn register(&mut self, handle: T) -> Id {
Box::into_raw(Box::new(handle)) as *mut _ as *mut c_void
}
fn get(&self, id: Id) -> Item<T> {
unsafe { (id as *mut T).as_ref() }.unwrap()
}
fn get_mut(&mut self, id: Id) -> ItemMut<T> {
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"))]
pub(crate) struct LocalRegistry<T> {
marker: PhantomData<T>,
}
#[cfg(not(feature = "remote"))]
impl<T> Registry<T> for LocalRegistry<T> {
fn new() -> Self {
LocalRegistry {
marker: PhantomData,
}
}
fn lock(&self) -> ItemsGuard<T> {
LocalItems {
marker: PhantomData,
}
}
}
#[cfg(feature = "remote")]
pub(crate) struct RemoteItems<T> {
next_id: Id,
tracked: FastHashMap<Id, T>,
free: Vec<Id>,
}
#[cfg(feature = "remote")]
impl<T> RemoteItems<T> {
fn new() -> Self {
RemoteItems {
next_id: 0,
tracked: FastHashMap::default(),
free: Vec::new(),
}
}
}
#[cfg(feature = "remote")]
impl<T> Items<T> for RemoteItems<T> {
fn register(&mut self, handle: T) -> Id {
let id = match self.free.pop() {
Some(id) => id,
None => {
self.next_id += 1;
self.next_id - 1
}
};
self.tracked.insert(id, handle);
id
}
fn get(&self, id: Id) -> Item<T> {
self.tracked.get(&id).unwrap()
}
fn get_mut(&mut self, id: Id) -> ItemMut<T> {
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")]
pub(crate) struct RemoteRegistry<T> {
items: Arc<Mutex<RemoteItems<T>>>,
}
#[cfg(feature = "remote")]
impl<T> Registry<T> for RemoteRegistry<T> {
fn new() -> Self {
RemoteRegistry {
items: Arc::new(Mutex::new(RemoteItems::new())),
}
}
fn lock(&self) -> ItemsGuard<T> {
self.items.lock()
}
}
#[cfg(not(feature = "remote"))]
type ConcreteRegistry<T> = LocalRegistry<T>;
#[cfg(feature = "remote")]
type ConcreteRegistry<T> = RemoteRegistry<T>;
lazy_static! {
pub(crate) static ref ADAPTER_REGISTRY: ConcreteRegistry<AdapterHandle> =
ConcreteRegistry::new();
pub(crate) static ref ATTACHMENT_STATE_REGISTRY: ConcreteRegistry<AttachmentStateHandle> =
ConcreteRegistry::new();
pub(crate) static ref BIND_GROUP_LAYOUT_REGISTRY: ConcreteRegistry<BindGroupLayoutHandle> =
ConcreteRegistry::new();
pub(crate) static ref BLEND_STATE_REGISTRY: ConcreteRegistry<BlendStateHandle> =
ConcreteRegistry::new();
pub(crate) static ref DEPTH_STENCIL_STATE_REGISTRY: ConcreteRegistry<DepthStencilStateHandle> =
ConcreteRegistry::new();
pub(crate) static ref DEVICE_REGISTRY: ConcreteRegistry<DeviceHandle> = ConcreteRegistry::new();
pub(crate) static ref COMMAND_BUFFER_REGISTRY: ConcreteRegistry<CommandBufferHandle> =
ConcreteRegistry::new();
pub(crate) static ref INSTANCE_REGISTRY: ConcreteRegistry<InstanceHandle> =
ConcreteRegistry::new();
pub(crate) static ref PIPELINE_LAYOUT_REGISTRY: ConcreteRegistry<PipelineLayoutHandle> =
ConcreteRegistry::new();
pub(crate) static ref RENDER_PIPELINE_REGISTRY: ConcreteRegistry<RenderPipelineHandle> =
ConcreteRegistry::new();
pub(crate) static ref SHADER_MODULE_REGISTRY: ConcreteRegistry<ShaderModuleHandle> =
ConcreteRegistry::new();
pub(crate) static ref RENDER_PASS_REGISTRY: ConcreteRegistry<RenderPassHandle> =
ConcreteRegistry::new();
}

View File

@ -0,0 +1,48 @@
use std::marker::PhantomData;
use std::os::raw::c_void;
pub type Id = *mut c_void;
pub type ItemsGuard<'a, T> = Items<T>;
pub struct Items<T> {
marker: PhantomData<T>,
}
impl<T> super::Items<T> for Items<T> {
fn register(&mut self, handle: T) -> Id {
Box::into_raw(Box::new(handle)) as *mut _ as *mut c_void
}
fn get(&self, id: Id) -> super::Item<T> {
unsafe { (id as *mut T).as_ref() }.unwrap()
}
fn get_mut(&mut self, id: Id) -> super::ItemMut<T> {
unsafe { (id as *mut T).as_mut() }.unwrap()
}
fn take(&mut self, id: Id) -> T {
unsafe { *Box::from_raw(id as *mut T) }
}
}
pub struct Registry<T> {
marker: PhantomData<T>,
}
impl<T> Default for Registry<T> {
fn default() -> Self {
Registry {
marker: PhantomData,
}
}
}
impl<T> super::Registry<T> for Registry<T> {
fn lock(&self) -> ItemsGuard<T> {
Items {
marker: PhantomData,
}
}
}

View File

@ -0,0 +1,52 @@
#[cfg(not(feature = "remote"))]
mod local;
#[cfg(feature = "remote")]
mod remote;
#[cfg(not(feature = "remote"))]
pub use self::local::{Id, ItemsGuard, Registry as ConcreteRegistry};
#[cfg(feature = "remote")]
pub use self::remote::{Id, ItemsGuard, Registry as ConcreteRegistry};
use {
AdapterHandle, AttachmentStateHandle, BindGroupLayoutHandle, BlendStateHandle,
CommandBufferHandle, DepthStencilStateHandle, DeviceHandle, InstanceHandle,
RenderPassHandle, ComputePassHandle,
PipelineLayoutHandle, RenderPipelineHandle, ShaderModuleHandle,
};
type Item<'a, T> = &'a T;
type ItemMut<'a, T> = &'a mut T;
pub trait Registry<T>: Default {
fn lock(&self) -> ItemsGuard<T>;
}
pub trait Items<T> {
fn register(&mut self, handle: T) -> Id;
fn get(&self, id: Id) -> Item<T>;
fn get_mut(&mut self, id: Id) -> ItemMut<T>;
fn take(&mut self, id: Id) -> T;
}
#[derive(Default)]
pub struct Hub {
pub(crate) instances: ConcreteRegistry<InstanceHandle>,
pub(crate) adapters: ConcreteRegistry<AdapterHandle>,
pub(crate) devices: ConcreteRegistry<DeviceHandle>,
pub(crate) pipeline_layouts: ConcreteRegistry<PipelineLayoutHandle>,
pub(crate) bind_group_layouts: ConcreteRegistry<BindGroupLayoutHandle>,
pub(crate) attachment_states: ConcreteRegistry<AttachmentStateHandle>,
pub(crate) blend_states: ConcreteRegistry<BlendStateHandle>,
pub(crate) depth_stencil_states: ConcreteRegistry<DepthStencilStateHandle>,
pub(crate) shader_modules: ConcreteRegistry<ShaderModuleHandle>,
pub(crate) command_buffers: ConcreteRegistry<CommandBufferHandle>,
pub(crate) render_pipelines: ConcreteRegistry<RenderPipelineHandle>,
pub(crate) render_passes: ConcreteRegistry<RenderPassHandle>,
pub(crate) compute_passes: ConcreteRegistry<ComputePassHandle>,
}
lazy_static! {
pub static ref HUB: Hub = Hub::default();
}

View File

@ -0,0 +1,68 @@
use hal::backend::FastHashMap;
use parking_lot::{Mutex, MutexGuard};
use std::sync::Arc;
pub type Id = u32;
pub type ItemsGuard<'a, T> = MutexGuard<'a, Items<T>>;
pub struct Items<T> {
next_id: Id,
tracked: FastHashMap<Id, T>,
free: Vec<Id>,
}
impl<T> Items<T> {
fn new() -> Self {
Items {
next_id: 0,
tracked: FastHashMap::default(),
free: Vec::new(),
}
}
}
impl<T> super::Items<T> for Items<T> {
fn register(&mut self, handle: T) -> Id {
let id = match self.free.pop() {
Some(id) => id,
None => {
self.next_id += 1;
self.next_id - 1
}
};
self.tracked.insert(id, handle);
id
}
fn get(&self, id: Id) -> super::Item<T> {
self.tracked.get(&id).unwrap()
}
fn get_mut(&mut self, id: Id) -> super::ItemMut<T> {
self.tracked.get_mut(&id).unwrap()
}
fn take(&mut self, id: Id) -> T {
self.free.push(id);
self.tracked.remove(&id).unwrap()
}
}
pub struct Registry<T> {
items: Arc<Mutex<Items<T>>>,
}
impl<T> Default for Registry<T> {
fn default() -> Self {
Registry {
items: Arc::new(Mutex::new(Items::new())),
}
}
}
impl<T> super::Registry<T> for Registry<T> {
fn lock(&self) -> ItemsGuard<T> {
self.items.lock()
}
}

View File

@ -68,6 +68,11 @@ pub struct RenderPass<'a> {
parent: &'a mut CommandBuffer,
}
pub struct ComputePass<'a> {
id: wgn::ComputePassId,
parent: &'a mut CommandBuffer,
}
pub struct Queue {
id: wgn::QueueId,
}
@ -247,6 +252,13 @@ impl CommandBuffer {
parent: self,
}
}
pub fn begin_compute_pass(&mut self) -> ComputePass {
ComputePass {
id: wgn::wgpu_command_buffer_begin_compute_pass(self.id),
parent: self,
}
}
}
impl<'a> RenderPass<'a> {
@ -256,6 +268,13 @@ impl<'a> RenderPass<'a> {
}
}
impl<'a> ComputePass<'a> {
pub fn end_pass(self) -> &'a mut CommandBuffer {
wgn::wgpu_compute_pass_end_pass(self.id);
self.parent
}
}
impl Queue {
pub fn submit(&self, command_buffers: &[CommandBuffer]) {
wgn::wgpu_queue_submit(