Skip to content

Commit bd0de53

Browse files
committed
First bits of wgpu-remote
1 parent 1504542 commit bd0de53

File tree

9 files changed

+311
-1
lines changed

9 files changed

+311
-1
lines changed

Cargo.lock

Lines changed: 172 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
members = [
33
"wgpu-native",
44
"wgpu-bindings",
5+
"wgpu-remote",
56
"wgpu-rs",
67
"examples",
78
"gfx-examples",

wgpu-native/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ crate-type = ["lib", "cdylib", "staticlib"]
1818
[features]
1919
default = []
2020
local = ["winit", "gfx-backend-empty/winit"]
21+
remote = ["serde"]
2122
metal-auto-capture = ["gfx-backend-metal/auto-capture"]
2223

2324
[dependencies]
@@ -32,4 +33,5 @@ gfx-backend-dx11 = { version = "0.1.0", optional = true }
3233
gfx-backend-dx12 = { version = "0.1.0", optional = true }
3334
gfx-backend-metal = { version = "0.1.0", optional = true }
3435
#rendy-memory = { git = "https://github.com/rustgd/rendy", rev = "ce7dd7f", features = ["gfx-hal"] }
36+
serde = { version = "1.0", features = ["serde_derive"], optional = true }
3537
winit = { version = "0.18", optional = true }

wgpu-native/src/hub.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ impl<T> Registry<T> {
107107
}
108108
}
109109

110+
#[cfg(feature = "remote")]
111+
impl<T> Registry<T> {
112+
pub fn register(&self, id: Id, value: T) {
113+
let old = self.data.write().map.insert(id, value);
114+
assert!(old.is_none());
115+
}
116+
}
117+
110118
#[derive(Default)]
111119
pub struct Hub {
112120
pub(crate) instances: Arc<Registry<InstanceHandle>>,

wgpu-native/src/instance.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,35 @@ use crate::{
77
#[cfg(feature = "local")]
88
use crate::{DeviceId, SurfaceId};
99

10+
#[cfg(feature = "remote")]
11+
use serde::{Serialize, Deserialize};
12+
1013
use hal::{self, Instance as _Instance, PhysicalDevice as _PhysicalDevice};
1114

1215

1316
#[repr(C)]
1417
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
18+
#[cfg_attr(feature = "remote", derive(Serialize, Deserialize))]
1519
pub enum PowerPreference {
1620
Default = 0,
1721
LowPower = 1,
1822
HighPerformance = 2,
1923
}
2024

2125
#[repr(C)]
26+
#[cfg_attr(feature = "remote", derive(Clone, Serialize, Deserialize))]
2227
pub struct AdapterDescriptor {
2328
pub power_preference: PowerPreference,
2429
}
2530

2631
#[repr(C)]
32+
#[cfg_attr(feature = "remote", derive(Clone, Serialize, Deserialize))]
2733
pub struct Extensions {
2834
pub anisotropic_filtering: bool,
2935
}
3036

3137
#[repr(C)]
38+
#[cfg_attr(feature = "remote", derive(Clone, Serialize, Deserialize))]
3239
pub struct DeviceDescriptor {
3340
pub extensions: Extensions,
3441
}

wgpu-native/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub use self::pipeline::*;
3939
pub use self::resource::*;
4040
pub use self::swap_chain::*;
4141
#[cfg(not(feature = "local"))]
42-
pub use self::hub::{Id, IdentityManager};
42+
pub use self::hub::{HUB, Id, IdentityManager, Registry};
4343

4444
use std::ptr;
4545
use std::sync::atomic::{AtomicUsize, Ordering};

wgpu-remote/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "wgpu-remote"
3+
version = "0.1.0"
4+
authors = [
5+
"Dzmitry Malyshau <kvark@mozilla.com>",
6+
"Joshua Groves <josh@joshgroves.com>",
7+
]
8+
edition = "2018"
9+
10+
[features]
11+
default = []
12+
13+
[dependencies]
14+
wgpu-native = { path = "../wgpu-native", features = ["remote"] }
15+
ipc-channel = "0.11"
16+
log = "0.4"
17+
parking_lot = { version = "0.7" }
18+
serde = { version = "1.0", features = ["serde_derive"] }

wgpu-remote/src/lib.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use ipc_channel::ipc::IpcSender;
2+
use parking_lot::Mutex;
3+
use serde::{Serialize, Deserialize};
4+
5+
use wgpu_native as wgn;
6+
7+
8+
#[derive(Serialize, Deserialize)]
9+
pub enum InstanceMessage {
10+
InstanceGetAdapter(wgn::InstanceId, wgn::AdapterDescriptor, wgn::AdapterId),
11+
AdapterCreateDevice(wgn::AdapterId, wgn::DeviceDescriptor, wgn::DeviceId),
12+
}
13+
14+
/// A message on the timeline of devices, queues, and resources.
15+
#[derive(Serialize, Deserialize)]
16+
pub enum GlobalMessage {
17+
Instance(InstanceMessage),
18+
//Device(DeviceMessage),
19+
//Queue(QueueMessage),
20+
//Texture(TextureMessage),
21+
//Command(CommandMessage),
22+
}
23+
24+
#[derive(Default)]
25+
pub struct IdentityHub {
26+
adapters: wgn::IdentityManager,
27+
devices: wgn::IdentityManager,
28+
}
29+
30+
pub struct Client {
31+
channel: IpcSender<GlobalMessage>,
32+
identity: Mutex<IdentityHub>,
33+
}
34+
35+
impl Client {
36+
pub fn new(channel: IpcSender<GlobalMessage>) -> Self {
37+
Client {
38+
channel,
39+
identity: Mutex::new(IdentityHub::default()),
40+
}
41+
}
42+
}
43+
44+
45+
#[no_mangle]
46+
pub extern "C" fn wgpu_instance_get_adapter(
47+
client: &Client,
48+
instance_id: wgn::InstanceId,
49+
desc: &wgn::AdapterDescriptor,
50+
) -> wgn::AdapterId {
51+
let id = client.identity.lock().adapters.alloc();
52+
let msg = GlobalMessage::Instance(InstanceMessage::InstanceGetAdapter(instance_id, desc.clone(), id));
53+
client.channel.send(msg).unwrap();
54+
id
55+
}
56+
57+
#[no_mangle]
58+
pub extern "C" fn wgpu_adapter_create_device(
59+
client: &Client,
60+
adapter_id: wgn::AdapterId,
61+
desc: &wgn::DeviceDescriptor,
62+
) -> wgn::DeviceId {
63+
let id = client.identity.lock().devices.alloc();
64+
let msg = GlobalMessage::Instance(InstanceMessage::AdapterCreateDevice(adapter_id, desc.clone(), id));
65+
client.channel.send(msg).unwrap();
66+
id
67+
}

wgpu-remote/src/server.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use crate::{GlobalMessage, InstanceMessage}
2+
3+
use ipc_channel::ipc::IpcReceiver;
4+
5+
use wgn;
6+
7+
8+
struct Server {
9+
channel: IpcReceiver<GlobalMessage>,
10+
instance_id: wgn::IntanceId,
11+
}
12+
13+
impl Server {
14+
pub fn new(channel: IpcReceiver<GlobalMessage>) -> Self {
15+
Server {
16+
channel,
17+
instance_id: wgn::wgpu_create_instance(),
18+
}
19+
}
20+
}
21+
22+
pub fn process(message: GlobalMessage) {
23+
match message {
24+
GlobalMessage::Instance(msg) => match msg {
25+
InstanceMessage::InstanceGetAdapter(instance_id, ref desc, id) => {
26+
let adapter = wgn::instance_get_adapter(instance_id, desc);
27+
wgn::HUB.adapters.register(id, adapter);
28+
}
29+
InstanceMessage::AdapterCreateDevice(adapter_id, ref desc, id) => {
30+
let device = wgn::adapter_create_device(adapter_id, desc);
31+
wgn::HUB.devices.register(id, device);
32+
}
33+
},
34+
}
35+
}

0 commit comments

Comments
 (0)