Introduce ClientFactory

This commit is contained in:
Dzmitry Malyshau 2019-04-30 15:37:36 -04:00
parent 61fca00f34
commit 6ac666a5aa
9 changed files with 71 additions and 56 deletions

1
Cargo.lock generated
View File

@ -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)",

View File

@ -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;
}

View File

@ -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);

View File

@ -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);

View File

@ -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;

View File

@ -173,6 +173,7 @@ pub struct TextureDescriptor {
}
pub(crate) enum TexturePlacement<B: hal::Backend> {
#[cfg_attr(feature = "remote", allow(unused))]
SwapChain(SwapChainLink<Mutex<SwapImageEpoch>>),
Memory(B::Memory),
Void,

View File

@ -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"] }

View File

@ -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<wgn::IdentityManager> = 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<IdentityHub>,
}
impl Client {
fn new(
channel: ipc::IpcSender<GlobalMessage>,
instance_id: wgn::InstanceId,
) -> Self {
Client {
channel,
instance_id,
identity: Mutex::new(IdentityHub::default()),
}
}
pub struct ClientFactory {
channel: ipc::IpcSender<GlobalMessage>,
instance_identities: Mutex<wgn::IdentityManager>,
}
#[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]

View File

@ -9,9 +9,7 @@ pub struct Server {
}
impl Server {
pub(crate) fn new(channel: IpcReceiver<GlobalMessage>, instance_id: wgn::InstanceId) -> Self {
let instance = wgn::create_instance();
wgn::HUB.instances.register(instance_id, instance);
pub(crate) fn new(channel: IpcReceiver<GlobalMessage>) -> 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