-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #24708 - szeged:webgpu-base, r=gterzian,kvark
Initial implementation of WebGPU API <!-- Please describe your changes on the following line: --> - Added the WebIDL bindings for GPU and GPUadapter interfaces. - Created a background thread for WebGPU api calls. - Established the async communication between the background thread and the WebGPU interfaces. - Implemented the `requesAdapter` function of `navigator.gpu` `./mach test-tidy` reports an error due to the different `arrayvec` version used in `servo` and `webgpu`, so added it to the ignore list in `servo-tidy.toml` --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [ ] `./mach test-tidy` does not report any errors - [ ] These changes addresses a part of ##24706 <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> cc @jdm, cc @kvark <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/24708) <!-- Reviewable:end -->
- Loading branch information
Showing
31 changed files
with
853 additions
and
7 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -159,6 +159,9 @@ mod gen { | |
}, | ||
}, | ||
dom: { | ||
webgpu: { | ||
enabled: bool, | ||
}, | ||
bluetooth: { | ||
enabled: bool, | ||
testing: { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* 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/. */ | ||
|
||
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::error::Error; | ||
use crate::dom::bindings::refcounted::{Trusted, TrustedPromise}; | ||
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector}; | ||
use crate::dom::bindings::root::DomRoot; | ||
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 dom_struct::dom_struct; | ||
use ipc_channel::ipc::{self, IpcSender}; | ||
use ipc_channel::router::ROUTER; | ||
use js::jsapi::Heap; | ||
use std::rc::Rc; | ||
use webgpu::wgpu; | ||
use webgpu::{WebGPU, WebGPURequest, WebGPUResponse, WebGPUResponseResult}; | ||
|
||
#[dom_struct] | ||
pub struct GPU { | ||
reflector_: Reflector, | ||
} | ||
|
||
impl GPU { | ||
pub fn new_inherited() -> GPU { | ||
GPU { | ||
reflector_: Reflector::new(), | ||
} | ||
} | ||
|
||
pub fn new(global: &GlobalScope) -> DomRoot<GPU> { | ||
reflect_dom_object(Box::new(GPU::new_inherited()), global, GPUBinding::Wrap) | ||
} | ||
|
||
fn wgpu_channel(&self) -> Option<WebGPU> { | ||
self.global().as_window().webgpu_channel() | ||
} | ||
} | ||
|
||
pub trait AsyncWGPUListener { | ||
fn handle_response(&self, response: WebGPUResponse, promise: &Rc<Promise>); | ||
} | ||
|
||
struct WGPUResponse<T: AsyncWGPUListener + DomObject> { | ||
trusted: TrustedPromise, | ||
receiver: Trusted<T>, | ||
} | ||
|
||
impl<T: AsyncWGPUListener + DomObject> WGPUResponse<T> { | ||
#[allow(unrooted_must_root)] | ||
fn response(self, response: WebGPUResponseResult) { | ||
let promise = self.trusted.root(); | ||
match response { | ||
Ok(response) => self.receiver.root().handle_response(response, &promise), | ||
Err(error) => promise.reject_error(Error::Type(format!( | ||
"Received error from WebGPU thread: {}", | ||
error | ||
))), | ||
} | ||
} | ||
} | ||
|
||
pub fn response_async<T: AsyncWGPUListener + DomObject + 'static>( | ||
promise: &Rc<Promise>, | ||
receiver: &T, | ||
) -> IpcSender<WebGPUResponseResult> { | ||
let (action_sender, action_receiver) = ipc::channel().unwrap(); | ||
let (task_source, canceller) = receiver | ||
.global() | ||
.as_window() | ||
.task_manager() | ||
.dom_manipulation_task_source_with_canceller(); | ||
let mut trusted = Some(TrustedPromise::new(promise.clone())); | ||
let trusted_receiver = Trusted::new(receiver); | ||
ROUTER.add_route( | ||
action_receiver.to_opaque(), | ||
Box::new(move |message| { | ||
let trusted = if let Some(trusted) = trusted.take() { | ||
trusted | ||
} else { | ||
error!("WebGPU callback called twice!"); | ||
return; | ||
}; | ||
|
||
let context = WGPUResponse { | ||
trusted, | ||
receiver: trusted_receiver.clone(), | ||
}; | ||
let result = task_source.queue_with_canceller( | ||
task!(process_webgpu_task: move|| { | ||
context.response(message.to().unwrap()); | ||
}), | ||
&canceller, | ||
); | ||
if let Err(err) = result { | ||
error!("Failed to queue GPU listener-task: {:?}", err); | ||
} | ||
}), | ||
); | ||
action_sender | ||
} | ||
|
||
impl GPUMethods for GPU { | ||
// https://gpuweb.github.io/gpuweb/#dom-gpu-requestadapter | ||
fn RequestAdapter( | ||
&self, | ||
options: &GPURequestAdapterOptions, | ||
comp: InCompartment, | ||
) -> Rc<Promise> { | ||
let promise = Promise::new_in_current_compartment(&self.global(), comp); | ||
let sender = response_async(&promise, self); | ||
let power_preference = match options.powerPreference { | ||
Some(GPUPowerPreference::Low_power) => wgpu::PowerPreference::LowPower, | ||
Some(GPUPowerPreference::High_performance) => wgpu::PowerPreference::HighPerformance, | ||
None => wgpu::PowerPreference::Default, | ||
}; | ||
|
||
match self.wgpu_channel() { | ||
Some(channel) => { | ||
channel | ||
.0 | ||
.send(WebGPURequest::RequestAdapter( | ||
sender, | ||
wgpu::RequestAdapterOptions { power_preference }, | ||
)) | ||
.unwrap(); | ||
}, | ||
None => promise.reject_error(Error::Type("No WebGPU thread...".to_owned())), | ||
}; | ||
promise | ||
} | ||
} | ||
|
||
impl AsyncWGPUListener for GPU { | ||
fn handle_response(&self, response: WebGPUResponse, promise: &Rc<Promise>) { | ||
match response { | ||
WebGPUResponse::RequestAdapter(name, adapter) => { | ||
let adapter = GPUAdapter::new( | ||
&self.global(), | ||
DOMString::from(name), | ||
Heap::default(), | ||
adapter, | ||
); | ||
promise.resolve_native(&adapter); | ||
}, | ||
response => promise.reject_error(Error::Type(format!( | ||
"Wrong response received for GPU from WebGPU thread {:?}", | ||
response, | ||
))), | ||
} | ||
} | ||
} |
Oops, something went wrong.