Add remote feature to control ID type

This commit is contained in:
Joshua Groves 2018-09-23 23:05:12 -06:00
parent 211189a090
commit 7d35607ec1
7 changed files with 88 additions and 21 deletions

View File

@ -13,6 +13,7 @@ path = "hello_triangle_rust/main.rs"
[features]
default = []
remote = ["wgpu-native/remote"]
metal = ["wgpu-native/metal"]
dx12 = ["wgpu-native/dx12"]
vulkan = ["wgpu-native/vulkan"]

View File

@ -7,19 +7,27 @@ fn main() {
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")),
enumeration: cbindgen::EnumConfig {
prefix_with_name: true,
..Default::default()
},
export: cbindgen::ExportConfig {
prefix: Some(String::from("WGPU")),
exclude: vec![
// We manually define `Id` is with an `#ifdef`, so exclude it here
String::from("Id"),
],
..Default::default()
},
language: cbindgen::Language::C,
..Default::default()
};
cbindgen::Builder::new()
.with_crate(crate_dir)
.with_config(config)
.with_language(cbindgen::Language::C)
.with_item_prefix("WGPU")
.generate()
.expect("Unable to generate bindings")
.unwrap()
.write_to_file("wgpu.h");
}

View File

@ -1,3 +1,9 @@
#ifdef WGPU_REMOTE
typedef uint32_t WGPUId;
#else
typedef void *WGPUId;
#endif
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
@ -8,8 +14,6 @@ typedef enum {
WGPUPowerPreference_HighPerformance = 2,
} WGPUPowerPreference;
typedef uint32_t WGPUId;
typedef WGPUId WGPUDeviceId;
typedef WGPUId WGPUAdapterId;

View File

@ -11,6 +11,7 @@ crate-type = ["lib", "cdylib", "staticlib"]
[features]
default = []
remote = []
metal = ["gfx-backend-metal"]
dx12 = ["gfx-backend-dx12"]
vulkan = ["gfx-backend-vulkan"]

View File

@ -1,7 +1,7 @@
use hal::{self, Device as _Device, QueueGroup};
use {conv, memory, pipeline, resource};
use registry;
use registry::{self, Registry};
use {BufferId, CommandBufferId, DeviceId, ShaderModuleId};
#[repr(C)]

View File

@ -1,6 +1,6 @@
use hal::{self, Instance as _Instance, PhysicalDevice as _PhysicalDevice};
use registry;
use registry::{self, Registry};
use {AdapterId, Device, DeviceId, InstanceId};
#[repr(C)]

View File

@ -1,47 +1,100 @@
use std::marker::PhantomData;
use std::os::raw::c_void;
use std::sync::{Arc, Mutex};
use std::{borrow, cmp, fmt, ops, ptr};
use hal::backend::FastHashMap;
use {AdapterHandle, DeviceHandle, InstanceHandle, ShaderModuleHandle};
#[cfg(not(feature = "remote"))]
pub(crate) type Id = *mut c_void;
#[cfg(feature = "remote")]
pub(crate) type Id = u32;
pub(crate) struct Registry<T> {
pub(crate) trait Registry<T> {
fn new() -> Self;
fn register(&mut self, handle: T) -> Id;
fn get(&self, id: Id) -> Option<&T>;
fn get_mut(&mut self, id: Id) -> Option<&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 register(&mut self, handle: T) -> Id {
::std::boxed::Box::into_raw(Box::new(handle)) as *mut _ as *mut c_void
}
fn get(&self, id: Id) -> Option<&T> {
unsafe { (id as *const T).as_ref() }
}
fn get_mut(&mut self, id: Id) -> Option<&mut T> {
unsafe { (id as *mut T).as_mut() }
}
}
#[cfg(feature = "remote")]
pub(crate) struct RemoteRegistry<T> {
next_id: Id,
tracked: FastHashMap<Id, T>,
}
impl<T> Registry<T> {
#[cfg(feature = "remote")]
impl<T> Registry<T> for RemoteRegistry<T> {
fn new() -> Self {
Registry {
RemoteRegistry {
next_id: 0,
tracked: FastHashMap::default(),
}
}
pub(crate) fn register(&mut self, handle: T) -> Id {
fn register(&mut self, handle: T) -> Id {
let id = self.next_id;
self.tracked.insert(id, handle);
self.next_id += 1;
id
}
pub(crate) fn get(&self, id: Id) -> Option<&T> {
fn get(&self, id: Id) -> Option<&T> {
self.tracked.get(&id)
}
pub(crate) fn get_mut(&mut self, id: Id) -> Option<&mut T> {
fn get_mut(&mut self, id: Id) -> Option<&mut T> {
self.tracked.get_mut(&id)
}
}
#[cfg(not(feature = "remote"))]
lazy_static! {
pub(crate) static ref ADAPTER_REGISTRY: Arc<Mutex<Registry<AdapterHandle>>> =
Arc::new(Mutex::new(Registry::new()));
pub(crate) static ref DEVICE_REGISTRY: Arc<Mutex<Registry<DeviceHandle>>> =
Arc::new(Mutex::new(Registry::new()));
pub(crate) static ref INSTANCE_REGISTRY: Arc<Mutex<Registry<InstanceHandle>>> =
Arc::new(Mutex::new(Registry::new()));
pub(crate) static ref SHADER_MODULE_REGISTRY: Arc<Mutex<Registry<ShaderModuleHandle>>> =
Arc::new(Mutex::new(Registry::new()));
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());
}
#[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()));
}