Skip to content

Commit

Permalink
Support worker for WebGPU
Browse files Browse the repository at this point in the history
To create resource ids for the WebGPU we need to access the `IdentityHub`.
In order to access the `IdentityHub` from a `Window` or a `Worker` as well, I've moved the `IdentityHub` to the `GlobalScope` from the `Navigator`.
  • Loading branch information
Istvan Miklos committed Jan 23, 2020
1 parent ee3fb92 commit dbed5f1
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 122 deletions.
36 changes: 35 additions & 1 deletion components/script/dom/globalscope.rs
Expand Up @@ -26,6 +26,7 @@ use crate::dom::eventsource::EventSource;
use crate::dom::eventtarget::EventTarget;
use crate::dom::file::File;
use crate::dom::htmlscriptelement::ScriptId;
use crate::dom::identityhub::Identities;
use crate::dom::messageevent::MessageEvent;
use crate::dom::messageport::MessagePort;
use crate::dom::paintworkletglobalscope::PaintWorkletGlobalScope;
Expand Down Expand Up @@ -79,8 +80,9 @@ use script_traits::{
};
use script_traits::{TimerEventId, TimerSchedulerMsg, TimerSource};
use servo_url::{MutableOrigin, ServoUrl};
use smallvec::SmallVec;
use std::borrow::Cow;
use std::cell::Cell;
use std::cell::{Cell, RefCell};
use std::collections::hash_map::Entry;
use std::collections::{HashMap, VecDeque};
use std::ffi::CString;
Expand All @@ -91,6 +93,10 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use time::{get_time, Timespec};
use uuid::Uuid;
use webgpu::wgpu::{
id::{AdapterId, BindGroupLayoutId, BufferId, DeviceId, PipelineLayoutId},
Backend,
};

#[derive(JSTraceable)]
pub struct AutoCloseWorker(Arc<AtomicBool>);
Expand Down Expand Up @@ -206,6 +212,9 @@ pub struct GlobalScope {

/// An optional string allowing the user agent to be set for testing.
user_agent: Cow<'static, str>,

#[ignore_malloc_size_of = "defined in wgpu"]
gpu_id_hub: RefCell<Identities>,
}

/// A wrapper for glue-code between the ipc router and the event-loop.
Expand Down Expand Up @@ -421,6 +430,7 @@ impl GlobalScope {
consumed_rejections: Default::default(),
is_headless,
user_agent,
gpu_id_hub: RefCell::new(Identities::new()),
}
}

Expand Down Expand Up @@ -1970,6 +1980,30 @@ impl GlobalScope {
// TODO: Worker and Worklet global scopes.
None
}

pub fn wgpu_create_adapter_ids(&self) -> SmallVec<[AdapterId; 4]> {
self.gpu_id_hub.borrow_mut().create_adapter_ids()
}

pub fn wgpu_create_bind_group_layout_id(&self, backend: Backend) -> BindGroupLayoutId {
self.gpu_id_hub
.borrow_mut()
.create_bind_group_layout_id(backend)
}

pub fn wgpu_create_buffer_id(&self, backend: Backend) -> BufferId {
self.gpu_id_hub.borrow_mut().create_buffer_id(backend)
}

pub fn wgpu_create_device_id(&self, backend: Backend) -> DeviceId {
self.gpu_id_hub.borrow_mut().create_device_id(backend)
}

pub fn wgpu_create_pipeline_layout_id(&self, backend: Backend) -> PipelineLayoutId {
self.gpu_id_hub
.borrow_mut()
.create_pipeline_layout_id(backend)
}
}

fn timestamp_in_ms(time: Timespec) -> u64 {
Expand Down
12 changes: 5 additions & 7 deletions components/script/dom/gpu.rs
Expand Up @@ -5,7 +5,6 @@
use crate::compartments::InCompartment;
use crate::dom::bindings::codegen::Bindings::GPUBinding::GPURequestAdapterOptions;
use crate::dom::bindings::codegen::Bindings::GPUBinding::{self, GPUMethods, GPUPowerPreference};
use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowBinding::WindowMethods;
use crate::dom::bindings::error::Error;
use crate::dom::bindings::refcounted::{Trusted, TrustedPromise};
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
Expand All @@ -14,7 +13,7 @@ use crate::dom::bindings::str::DOMString;
use crate::dom::globalscope::GlobalScope;
use crate::dom::gpuadapter::GPUAdapter;
use crate::dom::promise::Promise;
use crate::task_source::TaskSource;
use crate::task_source::{TaskSource, TaskSourceName};
use dom_struct::dom_struct;
use ipc_channel::ipc::{self, IpcSender};
use ipc_channel::router::ROUTER;
Expand Down Expand Up @@ -69,11 +68,10 @@ pub fn response_async<T: AsyncWGPUListener + DomObject + 'static>(
receiver: &T,
) -> IpcSender<WebGPUResponseResult> {
let (action_sender, action_receiver) = ipc::channel().unwrap();
let (task_source, canceller) = receiver
let task_source = receiver.global().dom_manipulation_task_source();
let canceller = receiver
.global()
.as_window()
.task_manager()
.dom_manipulation_task_source_with_canceller();
.task_canceller(TaskSourceName::DOMManipulation);
let mut trusted = Some(TrustedPromise::new(promise.clone()));
let trusted_receiver = Trusted::new(receiver);
ROUTER.add_route(
Expand Down Expand Up @@ -121,7 +119,7 @@ impl GPUMethods for GPU {
},
None => wgpu::instance::PowerPreference::Default,
};
let ids = global.as_window().Navigator().create_adapter_ids();
let ids = global.wgpu_create_adapter_ids();

let script_to_constellation_chan = global.script_to_constellation_chan();
if script_to_constellation_chan
Expand Down
27 changes: 10 additions & 17 deletions components/script/dom/gpuadapter.rs
Expand Up @@ -6,9 +6,7 @@ use crate::compartments::InCompartment;
use crate::dom::bindings::codegen::Bindings::GPUAdapterBinding::{
self, GPUAdapterMethods, GPUDeviceDescriptor,
};
use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowBinding::WindowMethods;
use crate::dom::bindings::error::Error;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
Expand All @@ -17,7 +15,6 @@ use crate::dom::gpu::response_async;
use crate::dom::gpu::AsyncWGPUListener;
use crate::dom::gpudevice::GPUDevice;
use crate::dom::promise::Promise;
use crate::dom::window::Window;
use crate::script_runtime::JSContext as SafeJSContext;
use dom_struct::dom_struct;
use js::jsapi::{Heap, JSObject};
Expand Down Expand Up @@ -92,21 +89,17 @@ impl GPUAdapterMethods for GPUAdapter {
max_bind_groups: descriptor.limits.maxBindGroups,
},
};
if let Some(window) = self.global().downcast::<Window>() {
let id = window
.Navigator()
.create_device_id(self.adapter.0.backend());
if self
.channel
.0
.send(WebGPURequest::RequestDevice(sender, self.adapter, desc, id))
.is_err()
{
promise.reject_error(Error::Operation);
}
} else {
let id = self
.global()
.wgpu_create_device_id(self.adapter.0.backend());
if self
.channel
.0
.send(WebGPURequest::RequestDevice(sender, self.adapter, desc, id))
.is_err()
{
promise.reject_error(Error::Operation);
};
}
promise
}
}
Expand Down
108 changes: 47 additions & 61 deletions components/script/dom/gpudevice.rs
Expand Up @@ -12,8 +12,6 @@ use crate::dom::bindings::codegen::Bindings::GPUBindGroupLayoutBinding::{
use crate::dom::bindings::codegen::Bindings::GPUBufferBinding::GPUBufferDescriptor;
use crate::dom::bindings::codegen::Bindings::GPUDeviceBinding::{self, GPUDeviceMethods};
use crate::dom::bindings::codegen::Bindings::GPUPipelineLayoutBinding::GPUPipelineLayoutDescriptor;
use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowBinding::WindowMethods;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
Expand All @@ -23,12 +21,11 @@ use crate::dom::gpuadapter::GPUAdapter;
use crate::dom::gpubindgrouplayout::GPUBindGroupLayout;
use crate::dom::gpubuffer::{GPUBuffer, GPUBufferState};
use crate::dom::gpupipelinelayout::GPUPipelineLayout;
use crate::dom::window::Window;
use crate::script_runtime::JSContext as SafeJSContext;
use dom_struct::dom_struct;
use ipc_channel::ipc;
use js::jsapi::{Heap, JSObject};
use js::jsval::{JSVal, ObjectValue, UndefinedValue};
use js::jsval::{JSVal, ObjectValue};
use js::typedarray::{ArrayBuffer, CreateWith};
use std::collections::{HashMap, HashSet};
use std::ptr::{self, NonNull};
Expand Down Expand Up @@ -179,20 +176,16 @@ impl GPUDeviceMethods for GPUDevice {
fn CreateBuffer(&self, descriptor: &GPUBufferDescriptor) -> DomRoot<GPUBuffer> {
let (valid, wgpu_descriptor) = self.validate_buffer_descriptor(descriptor);
let (sender, receiver) = ipc::channel().unwrap();
if let Some(window) = self.global().downcast::<Window>() {
let id = window.Navigator().create_buffer_id(self.device.0.backend());
self.channel
.0
.send(WebGPURequest::CreateBuffer(
sender,
self.device,
id,
wgpu_descriptor,
))
.expect("Failed to create WebGPU buffer");
} else {
unimplemented!()
};
let id = self.global().wgpu_create_buffer_id(self.device.0.backend());
self.channel
.0
.send(WebGPURequest::CreateBuffer(
sender,
self.device,
id,
wgpu_descriptor,
))
.expect("Failed to create WebGPU buffer");

let buffer = receiver.recv().unwrap();

Expand All @@ -216,21 +209,16 @@ impl GPUDeviceMethods for GPUDevice {
) -> Vec<JSVal> {
let (valid, wgpu_descriptor) = self.validate_buffer_descriptor(descriptor);
let (sender, receiver) = ipc::channel().unwrap();
rooted!(in(*cx) let js_val = UndefinedValue());
if let Some(window) = self.global().downcast::<Window>() {
let id = window.Navigator().create_buffer_id(self.device.0.backend());
self.channel
.0
.send(WebGPURequest::CreateBufferMapped(
sender,
self.device,
id,
wgpu_descriptor.clone(),
))
.expect("Failed to create WebGPU buffer");
} else {
return vec![js_val.get()];
};
let id = self.global().wgpu_create_buffer_id(self.device.0.backend());
self.channel
.0
.send(WebGPURequest::CreateBufferMapped(
sender,
self.device,
id,
wgpu_descriptor.clone(),
))
.expect("Failed to create WebGPU buffer");

let (buffer, array_buffer) = receiver.recv().unwrap();

Expand Down Expand Up @@ -381,20 +369,19 @@ impl GPUDeviceMethods for GPUDevice {
max_dynamic_storage_buffers_per_pipeline_layout >= 0;

let (sender, receiver) = ipc::channel().unwrap();
if let Some(window) = self.global().downcast::<Window>() {
let id = window
.Navigator()
.create_bind_group_layout_id(self.device.0.backend());
self.channel
.0
.send(WebGPURequest::CreateBindGroupLayout(
sender,
self.device,
id,
bindings.clone(),
))
.expect("Failed to create WebGPU BindGroupLayout");
}
let id = self
.global()
.wgpu_create_bind_group_layout_id(self.device.0.backend());
self.channel
.0
.send(WebGPURequest::CreateBindGroupLayout(
sender,
self.device,
id,
bindings.clone(),
))
.expect("Failed to create WebGPU BindGroupLayout");

let bgl = receiver.recv().unwrap();

let binds = descriptor
Expand Down Expand Up @@ -460,20 +447,19 @@ impl GPUDeviceMethods for GPUDevice {
max_dynamic_storage_buffers_per_pipeline_layout >= 0;

let (sender, receiver) = ipc::channel().unwrap();
if let Some(window) = self.global().downcast::<Window>() {
let id = window
.Navigator()
.create_pipeline_layout_id(self.device.0.backend());
self.channel
.0
.send(WebGPURequest::CreatePipelineLayout(
sender,
self.device,
id,
bgl_ids,
))
.expect("Failed to create WebGPU PipelineLayout");
}
let id = self
.global()
.wgpu_create_pipeline_layout_id(self.device.0.backend());
self.channel
.0
.send(WebGPURequest::CreatePipelineLayout(
sender,
self.device,
id,
bgl_ids,
))
.expect("Failed to create WebGPU PipelineLayout");

let pipeline_layout = receiver.recv().unwrap();
GPUPipelineLayout::new(&self.global(), bind_group_layouts, pipeline_layout, valid)
}
Expand Down
36 changes: 0 additions & 36 deletions components/script/dom/navigator.rs
Expand Up @@ -12,7 +12,6 @@ use crate::dom::bindings::str::DOMString;
use crate::dom::bluetooth::Bluetooth;
use crate::dom::gamepadlist::GamepadList;
use crate::dom::gpu::GPU;
use crate::dom::identityhub::Identities;
use crate::dom::mediadevices::MediaDevices;
use crate::dom::mediasession::MediaSession;
use crate::dom::mimetypearray::MimeTypeArray;
Expand All @@ -24,13 +23,7 @@ use crate::dom::serviceworkercontainer::ServiceWorkerContainer;
use crate::dom::window::Window;
use crate::dom::xr::XR;
use dom_struct::dom_struct;
use smallvec::SmallVec;
use std::cell::RefCell;
use std::rc::Rc;
use webgpu::wgpu::{
id::{AdapterId, BindGroupLayoutId, BufferId, DeviceId, PipelineLayoutId},
Backend,
};

#[dom_struct]
pub struct Navigator {
Expand All @@ -45,8 +38,6 @@ pub struct Navigator {
permissions: MutNullableDom<Permissions>,
mediasession: MutNullableDom<MediaSession>,
gpu: MutNullableDom<GPU>,
#[ignore_malloc_size_of = "Defined in wgpu"]
gpu_id_hub: RefCell<Identities>,
}

impl Navigator {
Expand All @@ -63,7 +54,6 @@ impl Navigator {
permissions: Default::default(),
mediasession: Default::default(),
gpu: Default::default(),
gpu_id_hub: RefCell::new(Identities::new()),
}
}

Expand All @@ -76,32 +66,6 @@ impl Navigator {
}
}

impl Navigator {
pub fn create_adapter_ids(&self) -> SmallVec<[AdapterId; 4]> {
self.gpu_id_hub.borrow_mut().create_adapter_ids()
}

pub fn create_device_id(&self, backend: Backend) -> DeviceId {
self.gpu_id_hub.borrow_mut().create_device_id(backend)
}

pub fn create_buffer_id(&self, backend: Backend) -> BufferId {
self.gpu_id_hub.borrow_mut().create_buffer_id(backend)
}

pub fn create_bind_group_layout_id(&self, backend: Backend) -> BindGroupLayoutId {
self.gpu_id_hub
.borrow_mut()
.create_bind_group_layout_id(backend)
}

pub fn create_pipeline_layout_id(&self, backend: Backend) -> PipelineLayoutId {
self.gpu_id_hub
.borrow_mut()
.create_pipeline_layout_id(backend)
}
}

impl NavigatorMethods for Navigator {
// https://html.spec.whatwg.org/multipage/#dom-navigator-product
fn Product(&self) -> DOMString {
Expand Down

0 comments on commit dbed5f1

Please sign in to comment.