Less nesting in webgpu response (#32799)

* Remove Option wrap of WebGPUResponse

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>

* Replace WebGPUResponseResult with WebGPUResponse

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>

---------

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
This commit is contained in:
Samson 2024-07-17 22:37:52 +02:00 committed by GitHub
parent 1223335547
commit 34eed29037
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 196 additions and 214 deletions

View file

@ -10,7 +10,7 @@ use ipc_channel::router::ROUTER;
use js::jsapi::Heap;
use script_traits::ScriptMsg;
use webgpu::wgt::PowerPreference;
use webgpu::{wgc, WebGPUResponse, WebGPUResponseResult};
use webgpu::{wgc, WebGPUResponse};
use super::bindings::codegen::Bindings::WebGPUBinding::GPUTextureFormat;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
@ -45,7 +45,7 @@ impl GPU {
}
pub trait AsyncWGPUListener {
fn handle_response(&self, response: Option<WebGPUResponseResult>, promise: &Rc<Promise>);
fn handle_response(&self, response: WebGPUResponse, promise: &Rc<Promise>);
}
struct WGPUResponse<T: AsyncWGPUListener + DomObject> {
@ -55,7 +55,7 @@ struct WGPUResponse<T: AsyncWGPUListener + DomObject> {
impl<T: AsyncWGPUListener + DomObject> WGPUResponse<T> {
#[allow(crown::unrooted_must_root)]
fn response(self, response: Option<WebGPUResponseResult>) {
fn response(self, response: WebGPUResponse) {
let promise = self.trusted.root();
self.receiver.root().handle_response(response, &promise);
}
@ -64,7 +64,7 @@ impl<T: AsyncWGPUListener + DomObject> WGPUResponse<T> {
pub fn response_async<T: AsyncWGPUListener + DomObject + 'static>(
promise: &Rc<Promise>,
receiver: &T,
) -> IpcSender<Option<WebGPUResponseResult>> {
) -> IpcSender<WebGPUResponse> {
let (action_sender, action_receiver) = ipc::channel().unwrap();
let task_source = receiver.global().dom_manipulation_task_source();
let canceller = receiver
@ -139,42 +139,34 @@ impl GPUMethods for GPU {
}
impl AsyncWGPUListener for GPU {
fn handle_response(&self, response: Option<WebGPUResponseResult>, promise: &Rc<Promise>) {
fn handle_response(&self, response: WebGPUResponse, promise: &Rc<Promise>) {
match response {
Some(response) => match response {
Ok(WebGPUResponse::RequestAdapter {
adapter_info,
adapter_id,
features,
limits,
channel,
}) => {
let adapter = GPUAdapter::new(
&self.global(),
channel,
DOMString::from(format!(
"{} ({:?})",
adapter_info.name,
adapter_id.0.backend()
)),
Heap::default(),
features,
limits,
adapter_info,
adapter_id,
);
promise.resolve_native(&adapter);
},
Err(e) => {
warn!("Could not get GPUAdapter ({:?})", e);
promise.resolve_native(&None::<GPUAdapter>);
},
Ok(_) => unreachable!("GPU received wrong WebGPUResponse"),
WebGPUResponse::Adapter(Ok(adapter)) => {
let adapter = GPUAdapter::new(
&self.global(),
adapter.channel,
DOMString::from(format!(
"{} ({:?})",
adapter.adapter_info.name,
adapter.adapter_id.0.backend()
)),
Heap::default(),
adapter.features,
adapter.limits,
adapter.adapter_info,
adapter.adapter_id,
);
promise.resolve_native(&adapter);
},
None => {
WebGPUResponse::Adapter(Err(e)) => {
warn!("Could not get GPUAdapter ({:?})", e);
promise.resolve_native(&None::<GPUAdapter>);
},
WebGPUResponse::None => {
warn!("Couldn't get a response, because WebGPU is disabled");
promise.resolve_native(&None::<GPUAdapter>);
},
_ => unreachable!("GPU received wrong WebGPUResponse"),
}
}
}

View file

@ -7,7 +7,7 @@ use std::rc::Rc;
use dom_struct::dom_struct;
use js::jsapi::{Heap, JSObject};
use webgpu::{wgt, WebGPU, WebGPUAdapter, WebGPURequest, WebGPUResponse, WebGPUResponseResult};
use webgpu::{wgt, WebGPU, WebGPUAdapter, WebGPURequest, WebGPUResponse};
use super::gpusupportedfeatures::GPUSupportedFeatures;
use super::types::{GPUAdapterInfo, GPUSupportedLimits};
@ -263,35 +263,30 @@ impl GPUAdapterMethods for GPUAdapter {
}
impl AsyncWGPUListener for GPUAdapter {
fn handle_response(&self, response: Option<WebGPUResponseResult>, promise: &Rc<Promise>) {
fn handle_response(&self, response: WebGPUResponse, promise: &Rc<Promise>) {
match response {
Some(response) => match response {
Ok(WebGPUResponse::RequestDevice {
device_id,
queue_id,
descriptor,
}) => {
let device = GPUDevice::new(
&self.global(),
self.channel.clone(),
self,
Heap::default(),
descriptor.required_features,
descriptor.required_limits,
device_id,
queue_id,
descriptor.label.unwrap_or_default(),
);
self.global().add_gpu_device(&device);
promise.resolve_native(&device);
},
Err(e) => {
warn!("Could not get GPUDevice({:?})", e);
promise.reject_error(Error::Operation);
},
Ok(_) => unreachable!("GPUAdapter received wrong WebGPUResponse"),
WebGPUResponse::Device(Ok(device)) => {
let descriptor = device.descriptor;
let device = GPUDevice::new(
&self.global(),
self.channel.clone(),
self,
Heap::default(),
descriptor.required_features,
descriptor.required_limits,
device.device_id,
device.queue_id,
descriptor.label.unwrap_or_default(),
);
self.global().add_gpu_device(&device);
promise.resolve_native(&device);
},
None => unreachable!("Failed to get a response for RequestDevice"),
WebGPUResponse::Device(Err(e)) => {
warn!("Could not get GPUDevice({:?})", e);
promise.reject_error(Error::Operation);
},
WebGPUResponse::None => unreachable!("Failed to get a response for RequestDevice"),
_ => unreachable!("GPUAdapter received wrong WebGPUResponse"),
}
}
}

View file

@ -12,7 +12,7 @@ use dom_struct::dom_struct;
use ipc_channel::ipc::IpcSharedMemory;
use js::typedarray::{ArrayBuffer, ArrayBufferU8};
use webgpu::wgc::device::HostMap;
use webgpu::{WebGPU, WebGPUBuffer, WebGPURequest, WebGPUResponse, WebGPUResponseResult};
use webgpu::{WebGPU, WebGPUBuffer, WebGPURequest, WebGPUResponse};
use super::bindings::buffer_source::{create_new_external_array_buffer, HeapBufferSource};
use crate::dom::bindings::cell::DomRefCell;
@ -350,29 +350,26 @@ impl GPUBufferMethods for GPUBuffer {
}
impl AsyncWGPUListener for GPUBuffer {
fn handle_response(&self, response: Option<WebGPUResponseResult>, promise: &Rc<Promise>) {
fn handle_response(&self, response: WebGPUResponse, promise: &Rc<Promise>) {
match response {
Some(response) => match response {
Ok(WebGPUResponse::BufferMapAsync(bytes)) => {
*self
.map_info
.borrow_mut()
.as_mut()
.unwrap()
.mapping
.lock()
.unwrap()
.as_mut() = bytes.to_vec();
promise.resolve_native(&());
self.state.set(GPUBufferState::Mapped);
},
Err(e) => {
warn!("Could not map buffer({:?})", e);
promise.reject_error(Error::Abort);
},
Ok(_) => unreachable!("GPUBuffer received wrong WebGPUResponse"),
WebGPUResponse::BufferMapAsync(Ok(bytes)) => {
*self
.map_info
.borrow_mut()
.as_mut()
.unwrap()
.mapping
.lock()
.unwrap()
.as_mut() = bytes.to_vec();
promise.resolve_native(&());
self.state.set(GPUBufferState::Mapped);
},
None => unreachable!("Failed to get a response for BufferMapAsync"),
WebGPUResponse::BufferMapAsync(Err(e)) => {
warn!("Could not map buffer({:?})", e);
promise.reject_error(Error::Abort);
},
_ => unreachable!("GPUBuffer received wrong WebGPUResponse"),
}
*self.map_promise.borrow_mut() = None;
}

View file

@ -17,7 +17,7 @@ use webgpu::wgc::id::{BindGroupLayoutId, PipelineLayoutId};
use webgpu::wgc::{
binding_model as wgpu_bind, command as wgpu_com, pipeline as wgpu_pipe, resource as wgpu_res,
};
use webgpu::{self, wgt, PopError, WebGPU, WebGPURequest, WebGPUResponse, WebGPUResponseResult};
use webgpu::{self, wgt, PopError, WebGPU, WebGPURequest, WebGPUResponse};
use super::bindings::codegen::UnionTypes::GPUPipelineLayoutOrGPUAutoLayoutMode;
use super::bindings::error::Fallible;
@ -1010,9 +1010,9 @@ impl GPUDeviceMethods for GPUDevice {
}
impl AsyncWGPUListener for GPUDevice {
fn handle_response(&self, response: Option<WebGPUResponseResult>, promise: &Rc<Promise>) {
fn handle_response(&self, response: WebGPUResponse, promise: &Rc<Promise>) {
match response {
Some(Ok(WebGPUResponse::PoppedErrorScope(result))) => match result {
WebGPUResponse::PoppedErrorScope(result) => match result {
Ok(None) | Err(PopError::Lost) => promise.resolve_native(&None::<Option<GPUError>>),
Err(PopError::Empty) => promise.reject_error(Error::Operation),
Ok(Some(error)) => {

View file

@ -206,13 +206,9 @@ impl GPUQueueMethods for GPUQueue {
}
impl AsyncWGPUListener for GPUQueue {
fn handle_response(
&self,
response: Option<Result<webgpu::WebGPUResponse, String>>,
promise: &Rc<Promise>,
) {
fn handle_response(&self, response: webgpu::WebGPUResponse, promise: &Rc<Promise>) {
match response {
Some(Ok(WebGPUResponse::SubmittedWorkDone)) => {
WebGPUResponse::SubmittedWorkDone => {
promise.resolve_native(&());
},
_ => {

View file

@ -5,7 +5,7 @@
use std::rc::Rc;
use dom_struct::dom_struct;
use webgpu::{WebGPU, WebGPURequest, WebGPUResponse, WebGPUResponseResult, WebGPUShaderModule};
use webgpu::{WebGPU, WebGPURequest, WebGPUResponse, WebGPUShaderModule};
use super::gpu::AsyncWGPUListener;
use super::gpucompilationinfo::GPUCompilationInfo;
@ -89,9 +89,9 @@ impl GPUShaderModuleMethods for GPUShaderModule {
}
impl AsyncWGPUListener for GPUShaderModule {
fn handle_response(&self, response: Option<WebGPUResponseResult>, promise: &Rc<Promise>) {
fn handle_response(&self, response: WebGPUResponse, promise: &Rc<Promise>) {
match response {
Some(Ok(WebGPUResponse::CompilationInfo(info))) => {
WebGPUResponse::CompilationInfo(info) => {
let info = GPUCompilationInfo::from(&self.global(), info);
promise.resolve_native(&info);
},