diff --git a/Cargo.lock b/Cargo.lock index b7ff4d4cc..08efbdbcf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1338,7 +1338,6 @@ name = "wgpu-remote" version = "0.1.0" dependencies = [ "ipc-channel 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/examples/hello_remote_c/main.c b/examples/hello_remote_c/main.c index f0e4a61ad..581961551 100644 --- a/examples/hello_remote_c/main.c +++ b/examples/hello_remote_c/main.c @@ -4,14 +4,17 @@ int main() { WGPUInfrastructure infra = wgpu_initialize(); - if (!infra.client || !infra.server || infra.error) { + if (!infra.factory || !infra.server || infra.error) { printf("Cannot initialize WGPU: %s", infra.error); return 1; } + WGPUClient* client = wgpu_client_create(infra.factory); + //TODO: do something meaningful - wgpu_client_terminate(infra.client); + wgpu_client_destroy(infra.factory, client); + wgpu_terminate(infra.factory); return 0; } diff --git a/ffi/wgpu-remote.h b/ffi/wgpu-remote.h index 408d95ebe..fe38e949e 100644 --- a/ffi/wgpu-remote.h +++ b/ffi/wgpu-remote.h @@ -15,6 +15,8 @@ typedef enum { typedef struct WGPUClient WGPUClient; +typedef struct WGPUClientFactory WGPUClientFactory; + typedef struct WGPUServer WGPUServer; typedef struct WGPUTrackPermit WGPUTrackPermit; @@ -45,7 +47,7 @@ typedef struct { } WGPUAdapterDescriptor; typedef struct { - WGPUClient *client; + WGPUClientFactory *factory; WGPUServer *server; const uint8_t *error; } WGPUInfrastructure; @@ -58,10 +60,14 @@ WGPUDeviceId wgpu_client_adapter_create_device(const WGPUClient *client, WGPUAdapterId adapter_id, const WGPUDeviceDescriptor *desc); -WGPUAdapterId wgpu_client_get_adapter(const WGPUClient *client, const WGPUAdapterDescriptor *desc); +WGPUClient *wgpu_client_create(const WGPUClientFactory *factory); -void wgpu_client_terminate(WGPUClient *client); +void wgpu_client_destroy(const WGPUClientFactory *factory, WGPUClient *client); + +WGPUAdapterId wgpu_client_get_adapter(const WGPUClient *client, const WGPUAdapterDescriptor *desc); WGPUInfrastructure wgpu_initialize(void); void wgpu_server_process(const WGPUServer *server); + +void wgpu_terminate(WGPUClientFactory *factory); diff --git a/ffi/wgpu.h b/ffi/wgpu.h index f88466a86..b37a23f2d 100644 --- a/ffi/wgpu.h +++ b/ffi/wgpu.h @@ -264,11 +264,9 @@ typedef struct { WGPUEpoch _1; } WGPUId; -typedef WGPUId WGPUAdapterId; +typedef WGPUId WGPUDeviceId; -typedef struct { - WGPUPowerPreference power_preference; -} WGPUAdapterDescriptor; +typedef WGPUId WGPUAdapterId; typedef struct { bool anisotropic_filtering; @@ -278,16 +276,6 @@ typedef struct { WGPUExtensions extensions; } WGPUDeviceDescriptor; -#if defined(WGPU_REMOTE) -typedef struct { - WGPUAdapterId adapter; - WGPUAdapterDescriptor adapter_desc; - WGPUDeviceDescriptor device_desc; -} WGPUForcedExports; -#endif - -typedef WGPUId WGPUDeviceId; - typedef WGPUId WGPUBindGroupId; typedef WGPUId WGPUBufferId; @@ -614,6 +602,10 @@ typedef struct { typedef WGPUDeviceId WGPUQueueId; +typedef struct { + WGPUPowerPreference power_preference; +} WGPUAdapterDescriptor; + typedef struct { WGPUTextureId texture_id; WGPUTextureViewId view_id; @@ -638,10 +630,6 @@ typedef struct { -#if defined(WGPU_REMOTE) -void forced_exports(WGPUForcedExports fe); -#endif - WGPUDeviceId wgpu_adapter_create_device(WGPUAdapterId adapter_id, const WGPUDeviceDescriptor *desc); void wgpu_bind_group_destroy(WGPUBindGroupId bind_group_id); diff --git a/wgpu-native/src/conv.rs b/wgpu-native/src/conv.rs index b74144cd8..3451c0042 100644 --- a/wgpu-native/src/conv.rs +++ b/wgpu-native/src/conv.rs @@ -452,6 +452,7 @@ pub fn map_texture_dimension_size( } } +#[cfg(feature = "local")] pub fn map_texture_view_dimension( dimension: resource::TextureViewDimension, ) -> hal::image::ViewKind { @@ -467,6 +468,7 @@ pub fn map_texture_view_dimension( } } +#[cfg(feature = "local")] pub fn map_texture_aspect_flags(aspect: resource::TextureAspectFlags) -> hal::format::Aspects { use crate::resource::TextureAspectFlags as Taf; use hal::format::Aspects; diff --git a/wgpu-native/src/resource.rs b/wgpu-native/src/resource.rs index ab15d706a..046007806 100644 --- a/wgpu-native/src/resource.rs +++ b/wgpu-native/src/resource.rs @@ -173,6 +173,7 @@ pub struct TextureDescriptor { } pub(crate) enum TexturePlacement { + #[cfg_attr(feature = "remote", allow(unused))] SwapChain(SwapChainLink>), Memory(B::Memory), Void, diff --git a/wgpu-remote/Cargo.toml b/wgpu-remote/Cargo.toml index c2bef8fef..1ff445b50 100644 --- a/wgpu-remote/Cargo.toml +++ b/wgpu-remote/Cargo.toml @@ -20,7 +20,6 @@ metal = ["wgpu-native/gfx-backend-metal"] [dependencies] wgpu-native = { path = "../wgpu-native", version = "0.2", features = ["remote"] } ipc-channel = "0.11" -lazy_static = "1" log = "0.4" parking_lot = { version = "0.7" } serde = { version = "1.0", features = ["serde_derive"] } diff --git a/wgpu-remote/src/lib.rs b/wgpu-remote/src/lib.rs index c4e4daa1c..5aca46615 100644 --- a/wgpu-remote/src/lib.rs +++ b/wgpu-remote/src/lib.rs @@ -4,7 +4,6 @@ extern crate wgpu_native as wgn; use crate::server::Server; use ipc_channel::ipc; -use lazy_static::lazy_static; use log::error; use parking_lot::Mutex; use serde::{Deserialize, Serialize}; @@ -14,15 +13,12 @@ use std::ptr; mod server; -lazy_static! { - static ref INSTANCE_IDENTITIES: Mutex = Mutex::new(wgn::IdentityManager::default()); -} - #[derive(Serialize, Deserialize)] enum InstanceMessage { + Create(wgn::InstanceId), InstanceGetAdapter(wgn::InstanceId, wgn::AdapterDescriptor, wgn::AdapterId), AdapterCreateDevice(wgn::AdapterId, wgn::DeviceDescriptor, wgn::DeviceId), - Terminate, + Destroy(wgn::InstanceId), } /// A message on the timeline of devices, queues, and resources. @@ -33,6 +29,7 @@ enum GlobalMessage { //Queue(QueueMessage), //Texture(TextureMessage), //Command(CommandMessage), + Terminate, } #[derive(Default)] @@ -47,22 +44,14 @@ pub struct Client { identity: Mutex, } -impl Client { - fn new( - channel: ipc::IpcSender, - instance_id: wgn::InstanceId, - ) -> Self { - Client { - channel, - instance_id, - identity: Mutex::new(IdentityHub::default()), - } - } +pub struct ClientFactory { + channel: ipc::IpcSender, + instance_identities: Mutex, } #[repr(C)] pub struct Infrastructure { - pub client: *mut Client, + pub factory: *mut ClientFactory, pub server: *mut Server, pub error: *const u8, } @@ -71,11 +60,13 @@ pub struct Infrastructure { pub extern "C" fn wgpu_initialize() -> Infrastructure { match ipc::channel() { Ok((sender, receiver)) => { - let instance_id = INSTANCE_IDENTITIES.lock().alloc(); - let client = Client::new(sender, instance_id); - let server = Server::new(receiver, instance_id); + let factory = ClientFactory { + channel: sender, + instance_identities: Mutex::new(wgn::IdentityManager::default()), + }; + let server = Server::new(receiver); Infrastructure { - client: Box::into_raw(Box::new(client)), + factory: Box::into_raw(Box::new(factory)), server: Box::into_raw(Box::new(server)), error: ptr::null(), } @@ -83,22 +74,43 @@ pub extern "C" fn wgpu_initialize() -> Infrastructure { Err(e) => { error!("WGPU initialize failed: {:?}", e); Infrastructure { - client: ptr::null_mut(), + factory: ptr::null_mut(), server: ptr::null_mut(), error: ptr::null(), //TODO_remote_ } } } - } #[no_mangle] -pub extern "C" fn wgpu_client_terminate(client: *mut Client) { +pub extern "C" fn wgpu_terminate(factory: *mut ClientFactory) { + let factory = unsafe { + Box::from_raw(factory) + }; + let _ = factory.channel.send(GlobalMessage::Terminate); +} + +#[no_mangle] +pub extern "C" fn wgpu_client_create(factory: &ClientFactory) -> *mut Client { + let instance_id = factory.instance_identities.lock().alloc(); + let msg = GlobalMessage::Instance(InstanceMessage::Create(instance_id)); + factory.channel.send(msg).unwrap(); + let client = Client { + channel: factory.channel.clone(), + instance_id, + identity: Mutex::new(IdentityHub::default()), + }; + Box::into_raw(Box::new(client)) +} + +#[no_mangle] +pub extern "C" fn wgpu_client_destroy(factory: &ClientFactory, client: *mut Client) { let client = unsafe { Box::from_raw(client) }; - INSTANCE_IDENTITIES.lock().free(client.instance_id); - let _ = client.channel.send(GlobalMessage::Instance(InstanceMessage::Terminate)); + factory.instance_identities.lock().free(client.instance_id); + let msg = GlobalMessage::Instance(InstanceMessage::Destroy(client.instance_id)); + client.channel.send(msg).unwrap(); } #[no_mangle] diff --git a/wgpu-remote/src/server.rs b/wgpu-remote/src/server.rs index d505f79db..96a359a6d 100644 --- a/wgpu-remote/src/server.rs +++ b/wgpu-remote/src/server.rs @@ -9,9 +9,7 @@ pub struct Server { } impl Server { - pub(crate) fn new(channel: IpcReceiver, instance_id: wgn::InstanceId) -> Self { - let instance = wgn::create_instance(); - wgn::HUB.instances.register(instance_id, instance); + pub(crate) fn new(channel: IpcReceiver) -> Self { Server { channel, } @@ -26,6 +24,10 @@ enum ControlFlow { fn process(message: GlobalMessage) -> ControlFlow { match message { GlobalMessage::Instance(msg) => match msg { + InstanceMessage::Create(instance_id) => { + let instance = wgn::create_instance(); + wgn::HUB.instances.register(instance_id, instance); + } InstanceMessage::InstanceGetAdapter(instance_id, ref desc, id) => { let adapter = wgn::instance_get_adapter(instance_id, desc); wgn::HUB.adapters.register(id, adapter); @@ -34,8 +36,11 @@ fn process(message: GlobalMessage) -> ControlFlow { let device = wgn::adapter_create_device(adapter_id, desc); wgn::HUB.devices.register(id, device); } - InstanceMessage::Terminate => return ControlFlow::Terminate, + InstanceMessage::Destroy(instance_id) => { + wgn::HUB.instances.unregister(instance_id); + } }, + GlobalMessage::Terminate => return ControlFlow::Terminate, } ControlFlow::Continue