1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#[macro_use]
extern crate log;
#[macro_use]
extern crate serde;
#[macro_use]
pub extern crate wgpu_native as wgpu;
use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use servo_config::pref;
use wgpu::adapter_get_info;
#[derive(Debug, Deserialize, Serialize)]
pub enum WebGPUResponse {
RequestAdapter(String, WebGPUAdapter),
RequestDevice,
}
pub type WebGPUResponseResult = Result<WebGPUResponse, String>;
#[derive(Debug, Deserialize, Serialize)]
pub enum WebGPURequest {
RequestAdapter(
IpcSender<WebGPUResponseResult>,
wgpu::RequestAdapterOptions,
wgpu::AdapterId,
),
RequestDevice,
Exit(IpcSender<()>),
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct WebGPU(pub IpcSender<WebGPURequest>);
impl WebGPU {
pub fn new() -> Option<Self> {
if !pref!(dom.webgpu.enabled) {
return None;
}
let (sender, receiver) = match ipc::channel() {
Ok(sender_and_receiver) => sender_and_receiver,
Err(e) => {
warn!(
"Failed to create sender and receiciver for WGPU thread ({})",
e
);
return None;
},
};
if let Err(e) = std::thread::Builder::new()
.name("WGPU".to_owned())
.spawn(move || {
WGPU::new(receiver).run();
})
{
warn!("Failed to spwan WGPU thread ({})", e);
return None;
}
Some(WebGPU(sender))
}
pub fn exit(&self, sender: IpcSender<()>) -> Result<(), &'static str> {
self.0
.send(WebGPURequest::Exit(sender))
.map_err(|_| "Failed to send Exit message")
}
}
struct WGPU {
receiver: IpcReceiver<WebGPURequest>,
global: wgpu::Global,
adapters: Vec<WebGPUAdapter>,
// Track invalid adapters https://gpuweb.github.io/gpuweb/#invalid
_invalid_adapters: Vec<WebGPUAdapter>,
}
impl WGPU {
fn new(receiver: IpcReceiver<WebGPURequest>) -> Self {
WGPU {
receiver,
global: wgpu::Global::new("webgpu-native"),
adapters: Vec::new(),
_invalid_adapters: Vec::new(),
}
}
fn deinit(self) {
self.global.delete()
}
fn run(mut self) {
while let Ok(msg) = self.receiver.recv() {
match msg {
WebGPURequest::RequestAdapter(sender, options, id) => {
let adapter_id = match wgpu::request_adapter(&self.global, &options, &[id]) {
Some(id) => id,
None => {
if let Err(e) =
sender.send(Err("Failed to get webgpu adapter".to_string()))
{
warn!(
"Failed to send response to WebGPURequest::RequestAdapter ({})",
e
)
}
return;
},
};
let adapter = WebGPUAdapter(adapter_id);
self.adapters.push(adapter);
let info =
gfx_select!(adapter_id => adapter_get_info(&self.global, adapter_id));
if let Err(e) =
sender.send(Ok(WebGPUResponse::RequestAdapter(info.name, adapter)))
{
warn!(
"Failed to send response to WebGPURequest::RequestAdapter ({})",
e
)
}
},
WebGPURequest::RequestDevice => {},
WebGPURequest::Exit(sender) => {
self.deinit();
if let Err(e) = sender.send(()) {
warn!("Failed to send response to WebGPURequest::Exit ({})", e)
}
return;
},
}
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub struct WebGPUAdapter(pub wgpu::AdapterId);
impl MallocSizeOf for WebGPUAdapter {
fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
0
}
}
|