mirror of
https://github.com/gfx-rs/wgpu.git
synced 2024-11-21 22:33:49 +00:00
Move mutex inside registry
This commit is contained in:
parent
7d35607ec1
commit
7e6765108b
@ -2,12 +2,20 @@ extern crate cbindgen;
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
const HEADER: &str = "
|
||||
#ifdef WGPU_REMOTE
|
||||
typedef uint32_t WGPUId;
|
||||
#else
|
||||
typedef void *WGPUId;
|
||||
#endif
|
||||
";
|
||||
|
||||
fn main() {
|
||||
let mut crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
crate_dir.push("../wgpu-native");
|
||||
|
||||
let config = cbindgen::Config {
|
||||
header: Some(String::from("#ifdef WGPU_REMOTE\n typedef uint32_t WGPUId;\n#else\n typedef void *WGPUId;\n#endif")),
|
||||
header: Some(String::from(HEADER.trim())),
|
||||
enumeration: cbindgen::EnumConfig {
|
||||
prefix_with_name: true,
|
||||
..Default::default()
|
||||
@ -15,7 +23,7 @@ fn main() {
|
||||
export: cbindgen::ExportConfig {
|
||||
prefix: Some(String::from("WGPU")),
|
||||
exclude: vec![
|
||||
// We manually define `Id` is with an `#ifdef`, so exclude it here
|
||||
// We manually define `Id` is within the header, so exclude it here
|
||||
String::from("Id"),
|
||||
],
|
||||
..Default::default()
|
||||
|
@ -36,15 +36,11 @@ pub extern "C" fn wgpu_device_create_shader_module(
|
||||
device_id: DeviceId,
|
||||
desc: pipeline::ShaderModuleDescriptor,
|
||||
) -> ShaderModuleId {
|
||||
let device_registry = registry::DEVICE_REGISTRY.lock().unwrap();
|
||||
let device = device_registry.get(device_id).unwrap();
|
||||
let device = registry::DEVICE_REGISTRY.get(device_id).unwrap();
|
||||
let shader = device
|
||||
.device
|
||||
.create_shader_module(unsafe {
|
||||
::std::slice::from_raw_parts(desc.code.bytes, desc.code.length)
|
||||
}).unwrap();
|
||||
registry::SHADER_MODULE_REGISTRY
|
||||
.lock()
|
||||
.unwrap()
|
||||
.register(ShaderModule { raw: shader })
|
||||
registry::SHADER_MODULE_REGISTRY.register(ShaderModule { raw: shader })
|
||||
}
|
||||
|
@ -33,9 +33,8 @@ pub extern "C" fn wgpu_create_instance() -> InstanceId {
|
||||
feature = "gfx-backend-metal"
|
||||
))]
|
||||
{
|
||||
let mut registry = registry::INSTANCE_REGISTRY.lock().unwrap();
|
||||
let inst = ::back::Instance::create("wgpu", 1);
|
||||
registry.register(inst)
|
||||
registry::INSTANCE_REGISTRY.register(inst)
|
||||
}
|
||||
#[cfg(not(any(
|
||||
feature = "gfx-backend-vulkan",
|
||||
@ -52,8 +51,7 @@ pub extern "C" fn wgpu_instance_get_adapter(
|
||||
instance_id: InstanceId,
|
||||
desc: AdapterDescriptor,
|
||||
) -> AdapterId {
|
||||
let instance_registry = registry::INSTANCE_REGISTRY.lock().unwrap();
|
||||
let instance = instance_registry.get(instance_id).unwrap();
|
||||
let instance = registry::INSTANCE_REGISTRY.get(instance_id).unwrap();
|
||||
let (mut low, mut high, mut other) = (None, None, None);
|
||||
for adapter in instance.enumerate_adapters() {
|
||||
match adapter.info.device_type {
|
||||
@ -67,10 +65,7 @@ pub extern "C" fn wgpu_instance_get_adapter(
|
||||
PowerPreference::LowPower => low.or(high),
|
||||
PowerPreference::HighPerformance | PowerPreference::Default => high.or(low),
|
||||
};
|
||||
registry::ADAPTER_REGISTRY
|
||||
.lock()
|
||||
.unwrap()
|
||||
.register(some.or(other).unwrap())
|
||||
registry::ADAPTER_REGISTRY.register(some.or(other).unwrap())
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@ -78,12 +73,8 @@ pub extern "C" fn wgpu_adapter_create_device(
|
||||
adapter_id: AdapterId,
|
||||
desc: DeviceDescriptor,
|
||||
) -> DeviceId {
|
||||
let mut adapter_registry = registry::ADAPTER_REGISTRY.lock().unwrap();
|
||||
let adapter = adapter_registry.get_mut(adapter_id).unwrap();
|
||||
let adapter = registry::ADAPTER_REGISTRY.get_mut(adapter_id).unwrap();
|
||||
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()
|
||||
.unwrap()
|
||||
.register(Device::new(device, queue_group, mem_props))
|
||||
registry::DEVICE_REGISTRY.register(Device::new(device, queue_group, mem_props))
|
||||
}
|
||||
|
@ -13,9 +13,9 @@ pub(crate) type Id = u32;
|
||||
|
||||
pub(crate) trait Registry<T> {
|
||||
fn new() -> Self;
|
||||
fn register(&mut self, handle: T) -> Id;
|
||||
fn register(&self, handle: T) -> Id;
|
||||
fn get(&self, id: Id) -> Option<&T>;
|
||||
fn get_mut(&mut self, id: Id) -> Option<&mut T>;
|
||||
fn get_mut(&self, id: Id) -> Option<&mut T>;
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "remote"))]
|
||||
@ -31,7 +31,7 @@ impl<T> Registry<T> for LocalRegistry<T> {
|
||||
}
|
||||
}
|
||||
|
||||
fn register(&mut self, handle: T) -> Id {
|
||||
fn register(&self, handle: T) -> Id {
|
||||
::std::boxed::Box::into_raw(Box::new(handle)) as *mut _ as *mut c_void
|
||||
}
|
||||
|
||||
@ -39,62 +39,71 @@ impl<T> Registry<T> for LocalRegistry<T> {
|
||||
unsafe { (id as *const T).as_ref() }
|
||||
}
|
||||
|
||||
fn get_mut(&mut self, id: Id) -> Option<&mut T> {
|
||||
fn get_mut(&self, id: Id) -> Option<&mut T> {
|
||||
unsafe { (id as *mut T).as_mut() }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "remote")]
|
||||
pub(crate) struct RemoteRegistry<T> {
|
||||
struct Registrations<T> {
|
||||
next_id: Id,
|
||||
tracked: FastHashMap<Id, T>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "remote")]
|
||||
impl<T> Registrations<T> {
|
||||
fn new() -> Self {
|
||||
Registrations {
|
||||
next_id: 0,
|
||||
tracked: FastHashMap::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "remote")]
|
||||
pub(crate) struct RemoteRegistry<T> {
|
||||
registrations: Arc<Mutex<Registrations<T>>>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "remote")]
|
||||
impl<T> Registry<T> for RemoteRegistry<T> {
|
||||
fn new() -> Self {
|
||||
RemoteRegistry {
|
||||
next_id: 0,
|
||||
tracked: FastHashMap::default(),
|
||||
registrations: Arc::new(Mutex::new(Registrations::new())),
|
||||
}
|
||||
}
|
||||
|
||||
fn register(&mut self, handle: T) -> Id {
|
||||
let id = self.next_id;
|
||||
self.tracked.insert(id, handle);
|
||||
self.next_id += 1;
|
||||
fn register(&self, handle: T) -> Id {
|
||||
let mut registrations = self.registrations.lock().unwrap();
|
||||
let id = registrations.next_id;
|
||||
registrations.tracked.insert(id, handle);
|
||||
registrations.next_id += 1;
|
||||
id
|
||||
}
|
||||
|
||||
fn get(&self, id: Id) -> Option<&T> {
|
||||
self.tracked.get(&id)
|
||||
let registrations = self.registrations.lock().unwrap();
|
||||
registrations.tracked.get(&id)
|
||||
}
|
||||
|
||||
fn get_mut(&mut self, id: Id) -> Option<&mut T> {
|
||||
self.tracked.get_mut(&id)
|
||||
fn get_mut(&self, id: Id) -> Option<&mut T> {
|
||||
let registrations = self.registrations.lock().unwrap();
|
||||
registrations.tracked.get_mut(&id)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "remote"))]
|
||||
lazy_static! {
|
||||
pub(crate) static ref ADAPTER_REGISTRY: Mutex<LocalRegistry<AdapterHandle>> =
|
||||
Mutex::new(LocalRegistry::new());
|
||||
pub(crate) static ref DEVICE_REGISTRY: Mutex<LocalRegistry<DeviceHandle>> =
|
||||
Mutex::new(LocalRegistry::new());
|
||||
pub(crate) static ref INSTANCE_REGISTRY: Mutex<LocalRegistry<InstanceHandle>> =
|
||||
Mutex::new(LocalRegistry::new());
|
||||
pub(crate) static ref SHADER_MODULE_REGISTRY: Mutex<LocalRegistry<ShaderModuleHandle>> =
|
||||
Mutex::new(LocalRegistry::new());
|
||||
pub(crate) static ref ADAPTER_REGISTRY: LocalRegistry<AdapterHandle> = LocalRegistry::new();
|
||||
pub(crate) static ref DEVICE_REGISTRY: LocalRegistry<DeviceHandle> = LocalRegistry::new();
|
||||
pub(crate) static ref INSTANCE_REGISTRY: LocalRegistry<InstanceHandle> = LocalRegistry::new();
|
||||
pub(crate) static ref SHADER_MODULE_REGISTRY: LocalRegistry<ShaderModuleHandle> = LocalRegistry::new();
|
||||
}
|
||||
|
||||
#[cfg(feature = "remote")]
|
||||
lazy_static! {
|
||||
pub(crate) static ref ADAPTER_REGISTRY: Arc<Mutex<RemoteRegistry<AdapterHandle>>> =
|
||||
Arc::new(Mutex::new(RemoteRegistry::new()));
|
||||
pub(crate) static ref DEVICE_REGISTRY: Arc<Mutex<RemoteRegistry<DeviceHandle>>> =
|
||||
Arc::new(Mutex::new(RemoteRegistry::new()));
|
||||
pub(crate) static ref INSTANCE_REGISTRY: Arc<Mutex<RemoteRegistry<InstanceHandle>>> =
|
||||
Arc::new(Mutex::new(RemoteRegistry::new()));
|
||||
pub(crate) static ref SHADER_MODULE_REGISTRY: Arc<Mutex<RemoteRegistry<ShaderModuleHandle>>> =
|
||||
Arc::new(Mutex::new(RemoteRegistry::new()));
|
||||
pub(crate) static ref ADAPTER_REGISTRY: RemoteRegistry<AdapterHandle> = RemoteRegistry::new();
|
||||
pub(crate) static ref DEVICE_REGISTRY: RemoteRegistry<DeviceHandle> = RemoteRegistry::new();
|
||||
pub(crate) static ref INSTANCE_REGISTRY: RemoteRegistry<InstanceHandle> = RemoteRegistry::new();
|
||||
pub(crate) static ref SHADER_MODULE_REGISTRY: RemoteRegistry<ShaderModuleHandle> = RemoteRegistry::new();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user