webgpu: Move errorscopes to WGPU thread (#32304)

* Prepare errorscopes logic in wgpu_thread

* remove scope_id from ipc

* new GPUErrors per spec

* remove cotent timeline error_scope

* fixup poperrorscope types

* device_scope -> gpu_error and nice errors

* Handle errors detection more elegantly

* good expectations

* new expectations

* Make error_scope.errors Vec as per spec
This commit is contained in:
Samson 2024-05-22 18:47:35 +02:00 committed by GitHub
parent 9f32809671
commit 794110ebe5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
37 changed files with 3401 additions and 725 deletions

View file

@ -2079,7 +2079,7 @@ where
options,
ids,
};
if webgpu_chan.0.send((None, adapter_request)).is_err() {
if webgpu_chan.0.send(adapter_request).is_err() {
warn!("Failed to send request adapter message on WebGPU channel");
}
},

View file

@ -232,7 +232,7 @@ class Descriptor(DescriptorProvider):
self.register = desc.get('register', True)
self.path = desc.get('path', pathDefault)
self.inRealmMethods = [name for name in desc.get('inRealms', [])]
self.bindingPath = f"crate::dom::bindings::codegen::Bindings::{ifaceName}Binding::{ifaceName}_Binding"
self.bindingPath = f"{getModuleFromObject(self.interface)}::{ifaceName}_Binding"
self.outerObjectHook = desc.get('outerObjectHook', 'None')
self.proxy = False
self.weakReferenceable = desc.get('weakReferenceable', False)

View file

@ -56,7 +56,7 @@ use script_traits::{
};
use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl};
use uuid::Uuid;
use webgpu::{ErrorScopeId, WebGPUDevice, WebGPUOpResult};
use webgpu::WebGPUDevice;
use super::bindings::trace::HashMapTracedValues;
use crate::dom::bindings::cell::{DomRefCell, RefMut};
@ -3097,17 +3097,12 @@ impl GlobalScope {
let _ = self.gpu_devices.borrow_mut().remove(&device);
}
pub fn handle_wgpu_msg(
&self,
device: WebGPUDevice,
scope: Option<ErrorScopeId>,
result: WebGPUOpResult,
) {
pub fn handle_uncaptured_gpu_error(&self, device: WebGPUDevice, error: webgpu::Error) {
self.gpu_devices
.borrow()
.get(&device)
.expect("GPUDevice not found")
.handle_server_msg(scope, result);
.fire_uncaptured_error(error);
}
pub fn handle_gamepad_event(&self, gamepad_event: GamepadEvent) {

View file

@ -91,7 +91,7 @@ impl Drop for GPUAdapter {
if let Err(e) = self
.channel
.0
.send((None, WebGPURequest::DropAdapter(self.adapter.0)))
.send(WebGPURequest::DropAdapter(self.adapter.0))
{
warn!(
"Failed to send WebGPURequest::DropAdapter({:?}) ({})",
@ -217,16 +217,13 @@ impl GPUAdapterMethods for GPUAdapter {
if self
.channel
.0
.send((
None,
WebGPURequest::RequestDevice {
.send(WebGPURequest::RequestDevice {
sender,
adapter_id: self.adapter,
descriptor: desc,
device_id: id,
pipeline_id,
},
))
})
.is_err()
{
promise.reject_error(Error::Operation);

View file

@ -73,7 +73,7 @@ impl Drop for GPUBindGroup {
if let Err(e) = self
.channel
.0
.send((None, WebGPURequest::DropBindGroup(self.bind_group.0)))
.send(WebGPURequest::DropBindGroup(self.bind_group.0))
{
warn!(
"Failed to send WebGPURequest::DropBindGroup({:?}) ({})",

View file

@ -62,10 +62,11 @@ impl GPUBindGroupLayout {
impl Drop for GPUBindGroupLayout {
fn drop(&mut self) {
if let Err(e) = self.channel.0.send((
None,
WebGPURequest::DropBindGroupLayout(self.bind_group_layout.0),
)) {
if let Err(e) = self
.channel
.0
.send(WebGPURequest::DropBindGroupLayout(self.bind_group_layout.0))
{
warn!(
"Failed to send WebGPURequest::DropBindGroupLayout({:?}) ({})",
self.bind_group_layout.0, e

View file

@ -12,9 +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, WebGPUOpResult, WebGPURequest, WebGPUResponse, WebGPUResponseResult,
};
use webgpu::{WebGPU, WebGPUBuffer, WebGPURequest, WebGPUResponse, WebGPUResponseResult};
use super::bindings::buffer_source::{create_new_external_array_buffer, HeapBufferSource};
use crate::dom::bindings::cell::DomRefCell;
@ -137,7 +135,7 @@ impl Drop for GPUBuffer {
if let Err(e) = self
.channel
.0
.send((None, WebGPURequest::DropBuffer(self.buffer.0)))
.send(WebGPURequest::DropBuffer(self.buffer.0))
{
warn!(
"Failed to send WebGPURequest::DropBuffer({:?}) ({})",
@ -166,17 +164,14 @@ impl GPUBufferMethods for GPUBuffer {
return Err(Error::Operation);
};
let m_range = m_info.mapping_range.clone();
if let Err(e) = self.channel.0.send((
self.device.use_current_scope(),
WebGPURequest::UnmapBuffer {
if let Err(e) = self.channel.0.send(WebGPURequest::UnmapBuffer {
buffer_id: self.id().0,
device_id: self.device.id().0,
array_buffer: IpcSharedMemory::from_bytes(&m_info.mapping.lock().unwrap()),
is_map_read: m_info.map_mode == Some(GPUMapModeConstants::READ),
offset: m_range.start,
size: m_range.end - m_range.start,
},
)) {
}) {
warn!("Failed to send Buffer unmap ({:?}) ({})", self.buffer.0, e);
}
// Step 3.3
@ -209,7 +204,7 @@ impl GPUBufferMethods for GPUBuffer {
if let Err(e) = self
.channel
.0
.send((None, WebGPURequest::DestroyBuffer(self.buffer.0)))
.send(WebGPURequest::DestroyBuffer(self.buffer.0))
{
warn!(
"Failed to send WebGPURequest::DestroyBuffer({:?}) ({})",
@ -237,12 +232,11 @@ impl GPUBufferMethods for GPUBuffer {
} else {
self.size - offset
};
let scope_id = self.device.use_current_scope();
if self.state.get() != GPUBufferState::Unmapped {
self.device.handle_server_msg(
scope_id,
WebGPUOpResult::ValidationError(String::from("Buffer is not Unmapped")),
);
self.device
.dispatch_error(webgpu::Error::Validation(String::from(
"Buffer is not Unmapped",
)));
promise.reject_error(Error::Abort);
return promise;
}
@ -250,10 +244,10 @@ impl GPUBufferMethods for GPUBuffer {
GPUMapModeConstants::READ => HostMap::Read,
GPUMapModeConstants::WRITE => HostMap::Write,
_ => {
self.device.handle_server_msg(
scope_id,
WebGPUOpResult::ValidationError(String::from("Invalid MapModeFlags")),
);
self.device
.dispatch_error(webgpu::Error::Validation(String::from(
"Invalid MapModeFlags",
)));
promise.reject_error(Error::Abort);
return promise;
},
@ -262,17 +256,14 @@ impl GPUBufferMethods for GPUBuffer {
let map_range = offset..offset + range_size;
let sender = response_async(&promise, self);
if let Err(e) = self.channel.0.send((
scope_id,
WebGPURequest::BufferMapAsync {
if let Err(e) = self.channel.0.send(WebGPURequest::BufferMapAsync {
sender,
buffer_id: self.buffer.0,
device_id: self.device.id().0,
host_map,
offset,
size: Some(range_size),
},
)) {
}) {
warn!(
"Failed to send BufferMapAsync ({:?}) ({})",
self.buffer.0, e

View file

@ -111,7 +111,7 @@ pub struct GPUCanvasContext {
impl GPUCanvasContext {
fn new_inherited(canvas: HTMLCanvasElementOrOffscreenCanvas, channel: WebGPU) -> Self {
let (sender, receiver) = ipc::channel().unwrap();
if let Err(e) = channel.0.send((None, WebGPURequest::CreateContext(sender))) {
if let Err(e) = channel.0.send(WebGPURequest::CreateContext(sender)) {
warn!("Failed to send CreateContext ({:?})", e);
}
let external_id = receiver.recv().unwrap();
@ -153,14 +153,11 @@ impl GPUCanvasContext {
.wgpu_id_hub()
.lock()
.create_command_encoder_id(texture_id.backend());
if let Err(e) = self.channel.0.send((
None,
WebGPURequest::SwapChainPresent {
if let Err(e) = self.channel.0.send(WebGPURequest::SwapChainPresent {
external_id: self.context_id.0,
texture_id,
encoder_id,
},
)) {
}) {
warn!(
"Failed to send UpdateWebrenderData({:?}) ({})",
self.context_id, e
@ -275,17 +272,14 @@ impl GPUCanvasContextMethods for GPUCanvasContext {
self.channel
.0
.send((
None,
WebGPURequest::CreateSwapChain {
.send(WebGPURequest::CreateSwapChain {
device_id: descriptor.device.id().0,
buffer_ids,
external_id: self.context_id.0,
sender,
image_desc,
image_data,
},
))
})
.expect("Failed to create WebGPU SwapChain");
self.texture
@ -298,13 +292,10 @@ impl GPUCanvasContextMethods for GPUCanvasContext {
/// <https://gpuweb.github.io/gpuweb/#dom-gpucanvascontext-unconfigure>
fn Unconfigure(&self) {
if let Some(image_key) = self.webrender_image.take() {
if let Err(e) = self.channel.0.send((
None,
WebGPURequest::DestroySwapChain {
if let Err(e) = self.channel.0.send(WebGPURequest::DestroySwapChain {
external_id: self.context_id.0,
image_key,
},
)) {
}) {
warn!(
"Failed to send DestroySwapChain-ImageKey({:?}) ({})",
image_key, e

View file

@ -72,10 +72,11 @@ impl GPUCommandBuffer {
impl Drop for GPUCommandBuffer {
fn drop(&mut self) {
if let Err(e) = self.channel.0.send((
None,
WebGPURequest::DropCommandBuffer(self.command_buffer.0),
)) {
if let Err(e) = self
.channel
.0
.send(WebGPURequest::DropCommandBuffer(self.command_buffer.0))
{
warn!(
"Failed to send DropCommandBuffer({:?}) ({})",
self.command_buffer.0, e

View file

@ -270,17 +270,14 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
.insert(DomRoot::from_ref(destination));
self.channel
.0
.send((
None,
WebGPURequest::CopyBufferToBuffer {
.send(WebGPURequest::CopyBufferToBuffer {
command_encoder_id: self.encoder.0,
source_id: source.id().0,
source_offset,
destination_id: destination.id().0,
destination_offset,
size,
},
))
})
.expect("Failed to send CopyBufferToBuffer");
}
@ -302,17 +299,12 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
self.channel
.0
.send((
None,
WebGPURequest::CopyBufferToTexture {
.send(WebGPURequest::CopyBufferToTexture {
command_encoder_id: self.encoder.0,
source: convert_ic_buffer(source),
destination: convert_ic_texture(destination),
copy_size: convert_texture_size_to_wgt(&convert_texture_size_to_dict(
&copy_size,
)),
},
))
copy_size: convert_texture_size_to_wgt(&convert_texture_size_to_dict(&copy_size)),
})
.expect("Failed to send CopyBufferToTexture");
}
@ -334,17 +326,12 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
self.channel
.0
.send((
None,
WebGPURequest::CopyTextureToBuffer {
.send(WebGPURequest::CopyTextureToBuffer {
command_encoder_id: self.encoder.0,
source: convert_ic_texture(source),
destination: convert_ic_buffer(destination),
copy_size: convert_texture_size_to_wgt(&convert_texture_size_to_dict(
&copy_size,
)),
},
))
copy_size: convert_texture_size_to_wgt(&convert_texture_size_to_dict(&copy_size)),
})
.expect("Failed to send CopyTextureToBuffer");
}
@ -362,17 +349,12 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
self.channel
.0
.send((
None,
WebGPURequest::CopyTextureToTexture {
.send(WebGPURequest::CopyTextureToTexture {
command_encoder_id: self.encoder.0,
source: convert_ic_texture(source),
destination: convert_ic_texture(destination),
copy_size: convert_texture_size_to_wgt(&convert_texture_size_to_dict(
&copy_size,
)),
},
))
copy_size: convert_texture_size_to_wgt(&convert_texture_size_to_dict(&copy_size)),
})
.expect("Failed to send CopyTextureToTexture");
}
@ -380,16 +362,13 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
fn Finish(&self, descriptor: &GPUCommandBufferDescriptor) -> DomRoot<GPUCommandBuffer> {
self.channel
.0
.send((
self.device.use_current_scope(),
WebGPURequest::CommandEncoderFinish {
.send(WebGPURequest::CommandEncoderFinish {
command_encoder_id: self.encoder.0,
device_id: self.device.id().0,
is_error: !self.valid.get(),
// TODO(zakorgy): We should use `_descriptor` here after it's not empty
// and the underlying wgpu-core struct is serializable
},
))
})
.expect("Failed to send Finish");
*self.state.borrow_mut() = GPUCommandEncoderState::Closed;

View file

@ -100,13 +100,10 @@ impl GPUComputePassEncoderMethods for GPUComputePassEncoder {
let compute_pass = self.compute_pass.borrow_mut().take();
self.channel
.0
.send((
None,
WebGPURequest::RunComputePass {
.send(WebGPURequest::RunComputePass {
command_encoder_id: self.command_encoder.id().0,
compute_pass,
},
))
})
.expect("Failed to send RunComputePass"); //TODO: handle error
self.command_encoder.set_state(

View file

@ -103,10 +103,11 @@ impl GPUComputePipelineMethods for GPUComputePipeline {
impl Drop for GPUComputePipeline {
fn drop(&mut self) {
if let Err(e) = self.channel.0.send((
None,
WebGPURequest::DropComputePipeline(self.compute_pipeline.0),
)) {
if let Err(e) = self
.channel
.0
.send(WebGPURequest::DropComputePipeline(self.compute_pipeline.0))
{
warn!(
"Failed to send WebGPURequest::DropComputePipeline({:?}) ({})",
self.compute_pipeline.0, e

View file

@ -17,19 +17,21 @@ 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, ErrorScopeId, WebGPU, WebGPUOpResult, WebGPURequest};
use webgpu::{self, wgt, PopError, WebGPU, WebGPURequest, WebGPUResponse, WebGPUResponseResult};
use super::bindings::codegen::UnionTypes::GPUPipelineLayoutOrGPUAutoLayoutMode;
use super::bindings::error::Fallible;
use super::gpu::AsyncWGPUListener;
use super::gpudevicelostinfo::GPUDeviceLostInfo;
use super::gpusupportedlimits::GPUSupportedLimits;
use super::types::GPUError;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::EventBinding::EventInit;
use crate::dom::bindings::codegen::Bindings::EventTargetBinding::EventTargetMethods;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
GPUBindGroupDescriptor, GPUBindGroupLayoutDescriptor, GPUBindingResource, GPUBufferBindingType,
GPUBufferDescriptor, GPUCommandEncoderDescriptor, GPUComputePipelineDescriptor,
GPUDeviceLostReason, GPUDeviceMethods, GPUError, GPUErrorFilter, GPUPipelineLayoutDescriptor,
GPUDeviceLostReason, GPUDeviceMethods, GPUErrorFilter, GPUPipelineLayoutDescriptor,
GPURenderBundleEncoderDescriptor, GPURenderPipelineDescriptor, GPUSamplerBindingType,
GPUSamplerDescriptor, GPUShaderModuleDescriptor, GPUStorageTextureAccess,
GPUSupportedLimitsMethods, GPUTextureDescriptor, GPUTextureDimension, GPUTextureSampleType,
@ -42,6 +44,7 @@ use crate::dom::bindings::str::{DOMString, USVString};
use crate::dom::bindings::trace::RootedTraceableBox;
use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::dom::gpu::response_async;
use crate::dom::gpuadapter::GPUAdapter;
use crate::dom::gpubindgroup::GPUBindGroup;
use crate::dom::gpubindgrouplayout::GPUBindGroupLayout;
@ -54,7 +57,6 @@ use crate::dom::gpuconvert::{
convert_texture_size_to_dict, convert_texture_size_to_wgt, convert_vertex_format,
convert_view_dimension,
};
use crate::dom::gpuoutofmemoryerror::GPUOutOfMemoryError;
use crate::dom::gpupipelinelayout::GPUPipelineLayout;
use crate::dom::gpuqueue::GPUQueue;
use crate::dom::gpurenderbundleencoder::GPURenderBundleEncoder;
@ -64,33 +66,9 @@ use crate::dom::gpushadermodule::GPUShaderModule;
use crate::dom::gpusupportedfeatures::GPUSupportedFeatures;
use crate::dom::gputexture::GPUTexture;
use crate::dom::gpuuncapturederrorevent::GPUUncapturedErrorEvent;
use crate::dom::gpuvalidationerror::GPUValidationError;
use crate::dom::promise::Promise;
use crate::realms::InRealm;
#[derive(JSTraceable, MallocSizeOf)]
struct ErrorScopeInfo {
op_count: u64,
#[ignore_malloc_size_of = "Because it is non-owning"]
error: Option<GPUError>,
#[ignore_malloc_size_of = "promises are hard"]
promise: Option<Rc<Promise>>,
}
#[derive(JSTraceable, MallocSizeOf)]
struct ErrorScopeMetadata {
id: ErrorScopeId,
filter: GPUErrorFilter,
popped: Cell<bool>,
}
#[derive(JSTraceable, MallocSizeOf)]
struct ScopeContext {
error_scopes: HashMap<ErrorScopeId, ErrorScopeInfo>,
scope_stack: Vec<ErrorScopeMetadata>,
next_scope_id: ErrorScopeId,
}
#[dom_struct]
pub struct GPUDevice {
eventtarget: EventTarget,
@ -106,7 +84,6 @@ pub struct GPUDevice {
#[no_trace]
device: webgpu::WebGPUDevice,
default_queue: Dom<GPUQueue>,
scope_context: DomRefCell<ScopeContext>,
#[ignore_malloc_size_of = "promises are hard"]
lost_promise: DomRefCell<Option<Rc<Promise>>>,
valid: Cell<bool>,
@ -134,11 +111,6 @@ impl GPUDevice {
label: DomRefCell::new(USVString::from(label)),
device,
default_queue: Dom::from_ref(queue),
scope_context: DomRefCell::new(ScopeContext {
error_scopes: HashMap::new(),
scope_stack: Vec::new(),
next_scope_id: ErrorScopeId::new(1).unwrap(),
}),
lost_promise: DomRefCell::new(None),
valid: Cell::new(true),
}
@ -179,110 +151,28 @@ impl GPUDevice {
self.channel.clone()
}
pub fn handle_server_msg(&self, scope: Option<ErrorScopeId>, result: WebGPUOpResult) {
let result = match result {
WebGPUOpResult::Success => Ok(()),
WebGPUOpResult::ValidationError(m) => {
let val_err = GPUValidationError::new(&self.global(), DOMString::from_string(m));
Err((
GPUError::GPUValidationError(val_err),
GPUErrorFilter::Validation,
))
},
WebGPUOpResult::OutOfMemoryError => {
let oom_err = GPUOutOfMemoryError::new(&self.global());
Err((
GPUError::GPUOutOfMemoryError(oom_err),
GPUErrorFilter::Out_of_memory,
))
},
};
if let Some(s_id) = scope {
if let Err((err, filter)) = result {
let scop = self
.scope_context
.borrow()
.scope_stack
.iter()
.rev()
.find(|meta| meta.id <= s_id && meta.filter == filter)
.map(|meta| meta.id);
if let Some(s) = scop {
self.handle_error(s, err);
} else {
self.fire_uncaptured_error(err);
}
}
self.try_remove_scope(s_id);
} else if let Err((err, _)) = result {
self.fire_uncaptured_error(err);
pub fn dispatch_error(&self, error: webgpu::Error) {
if let Err(e) = self.channel.0.send(WebGPURequest::DispatchError {
device_id: self.device.0,
error,
}) {
warn!("Failed to send WebGPURequest::DispatchError due to {e:?}");
}
}
fn handle_error(&self, scope: ErrorScopeId, error: GPUError) {
let mut context = self.scope_context.borrow_mut();
if let Some(err_scope) = context.error_scopes.get_mut(&scope) {
if err_scope.error.is_none() {
err_scope.error = Some(error);
}
} else {
warn!("Could not find ErrorScope with Id({})", scope);
}
}
fn try_remove_scope(&self, scope: ErrorScopeId) {
let mut context = self.scope_context.borrow_mut();
let remove = if let Some(err_scope) = context.error_scopes.get_mut(&scope) {
err_scope.op_count -= 1;
if let Some(ref promise) = err_scope.promise {
if !promise.is_fulfilled() {
if let Some(ref e) = err_scope.error {
promise.resolve_native(e);
} else if err_scope.op_count == 0 {
promise.resolve_native(&None::<GPUError>);
}
}
}
err_scope.op_count == 0 && err_scope.promise.is_some()
} else {
warn!("Could not find ErrorScope with Id({})", scope);
false
};
if remove {
let _ = context.error_scopes.remove(&scope);
context.scope_stack.retain(|meta| meta.id != scope);
}
}
fn fire_uncaptured_error(&self, err: GPUError) {
pub fn fire_uncaptured_error(&self, error: webgpu::Error) {
let error = GPUError::from_error(&self.global(), error);
let ev = GPUUncapturedErrorEvent::new(
&self.global(),
DOMString::from("uncapturederror"),
&GPUUncapturedErrorEventInit {
error: err,
error,
parent: EventInit::empty(),
},
);
let _ = self.eventtarget.DispatchEvent(ev.event());
}
pub fn use_current_scope(&self) -> Option<ErrorScopeId> {
let mut context = self.scope_context.borrow_mut();
let scope_id = context
.scope_stack
.iter()
.rev()
.find(|meta| !meta.popped.get())
.map(|meta| meta.id);
scope_id.and_then(|s_id| {
context.error_scopes.get_mut(&s_id).map(|scope| {
scope.op_count += 1;
s_id
})
})
}
fn get_pipeline_layout_data(
&self,
layout: &GPUPipelineLayoutOrGPUAutoLayoutMode,
@ -379,24 +269,19 @@ impl GPUDeviceMethods for GPUDevice {
.lock()
.create_buffer_id(self.device.0.backend());
let scope_id = self.use_current_scope();
if desc.is_none() {
self.handle_server_msg(
scope_id,
WebGPUOpResult::ValidationError(String::from("Invalid GPUBufferUsage")),
);
self.dispatch_error(webgpu::Error::Validation(String::from(
"Invalid GPUBufferUsage",
)));
}
self.channel
.0
.send((
scope_id,
WebGPURequest::CreateBuffer {
.send(WebGPURequest::CreateBuffer {
device_id: self.device.0,
buffer_id: id,
descriptor: desc,
},
))
})
.expect("Failed to create WebGPU buffer");
let buffer = webgpu::WebGPUBuffer(id);
@ -509,18 +394,15 @@ impl GPUDeviceMethods for GPUDevice {
})
.collect::<Vec<_>>();
let scope_id = self.use_current_scope();
let desc = if valid {
Some(wgpu_bind::BindGroupLayoutDescriptor {
label: convert_label(&descriptor.parent),
entries: Cow::Owned(entries),
})
} else {
self.handle_server_msg(
scope_id,
WebGPUOpResult::ValidationError(String::from("Invalid GPUShaderStage")),
);
self.dispatch_error(webgpu::Error::Validation(String::from(
"Invalid GPUShaderStage",
)));
None
};
@ -531,14 +413,11 @@ impl GPUDeviceMethods for GPUDevice {
.create_bind_group_layout_id(self.device.0.backend());
self.channel
.0
.send((
scope_id,
WebGPURequest::CreateBindGroupLayout {
.send(WebGPURequest::CreateBindGroupLayout {
device_id: self.device.0,
bind_group_layout_id,
descriptor: desc,
},
))
})
.expect("Failed to create WebGPU BindGroupLayout");
let bgl = webgpu::WebGPUBindGroupLayout(bind_group_layout_id);
@ -568,8 +447,6 @@ impl GPUDeviceMethods for GPUDevice {
push_constant_ranges: Cow::Owned(vec![]),
};
let scope_id = self.use_current_scope();
let pipeline_layout_id = self
.global()
.wgpu_id_hub()
@ -577,14 +454,11 @@ impl GPUDeviceMethods for GPUDevice {
.create_pipeline_layout_id(self.device.0.backend());
self.channel
.0
.send((
scope_id,
WebGPURequest::CreatePipelineLayout {
.send(WebGPURequest::CreatePipelineLayout {
device_id: self.device.0,
pipeline_layout_id,
descriptor: desc,
},
))
})
.expect("Failed to create WebGPU PipelineLayout");
let bgls = descriptor
@ -633,8 +507,6 @@ impl GPUDeviceMethods for GPUDevice {
entries: Cow::Owned(entries),
};
let scope_id = self.use_current_scope();
let bind_group_id = self
.global()
.wgpu_id_hub()
@ -642,14 +514,11 @@ impl GPUDeviceMethods for GPUDevice {
.create_bind_group_id(self.device.0.backend());
self.channel
.0
.send((
scope_id,
WebGPURequest::CreateBindGroup {
.send(WebGPURequest::CreateBindGroup {
device_id: self.device.0,
bind_group_id,
descriptor: desc,
},
))
})
.expect("Failed to create WebGPU BindGroup");
let bind_group = webgpu::WebGPUBindGroup(bind_group_id);
@ -675,18 +544,14 @@ impl GPUDeviceMethods for GPUDevice {
.lock()
.create_shader_module_id(self.device.0.backend());
let scope_id = self.use_current_scope();
self.channel
.0
.send((
scope_id,
WebGPURequest::CreateShaderModule {
.send(WebGPURequest::CreateShaderModule {
device_id: self.device.0,
program_id,
program: descriptor.code.0.clone(),
label: None,
},
))
})
.expect("Failed to create WebGPU ShaderModule");
let shader_module = webgpu::WebGPUShaderModule(program_id);
@ -709,7 +574,6 @@ impl GPUDeviceMethods for GPUDevice {
.lock()
.create_compute_pipeline_id(self.device.0.backend());
let scope_id = self.use_current_scope();
let (layout, implicit_ids, bgls) = self.get_pipeline_layout_data(&descriptor.parent.layout);
let desc = wgpu_pipe::ComputePipelineDescriptor {
@ -725,15 +589,12 @@ impl GPUDeviceMethods for GPUDevice {
self.channel
.0
.send((
scope_id,
WebGPURequest::CreateComputePipeline {
.send(WebGPURequest::CreateComputePipeline {
device_id: self.device.0,
compute_pipeline_id,
descriptor: desc,
implicit_ids,
},
))
})
.expect("Failed to create WebGPU ComputePipeline");
let compute_pipeline = webgpu::WebGPUComputePipeline(compute_pipeline_id);
@ -768,17 +629,13 @@ impl GPUDeviceMethods for GPUDevice {
.wgpu_id_hub()
.lock()
.create_command_encoder_id(self.device.0.backend());
let scope_id = self.use_current_scope();
self.channel
.0
.send((
scope_id,
WebGPURequest::CreateCommandEncoder {
.send(WebGPURequest::CreateCommandEncoder {
device_id: self.device.0,
command_encoder_id,
label: convert_label(&descriptor.parent),
},
))
})
.expect("Failed to create WebGPU command encoder");
let encoder = webgpu::WebGPUCommandEncoder(command_encoder_id);
@ -822,20 +679,16 @@ impl GPUDeviceMethods for GPUDevice {
.lock()
.create_texture_id(self.device.0.backend());
let scope_id = self.use_current_scope();
if desc.is_none() {
return Err(Error::Type(String::from("Invalid GPUTextureUsage")));
}
self.channel
.0
.send((
scope_id,
WebGPURequest::CreateTexture {
.send(WebGPURequest::CreateTexture {
device_id: self.device.0,
texture_id,
descriptor: desc,
},
))
})
.expect("Failed to create WebGPU Texture");
let texture = webgpu::WebGPUTexture(texture_id);
@ -880,17 +733,13 @@ impl GPUDeviceMethods for GPUDevice {
border_color: None,
};
let scope_id = self.use_current_scope();
self.channel
.0
.send((
scope_id,
WebGPURequest::CreateSampler {
.send(WebGPURequest::CreateSampler {
device_id: self.device.0,
sampler_id,
descriptor: desc,
},
))
})
.expect("Failed to create WebGPU sampler");
let sampler = webgpu::WebGPUSampler(sampler_id);
@ -910,7 +759,6 @@ impl GPUDeviceMethods for GPUDevice {
&self,
descriptor: &GPURenderPipelineDescriptor,
) -> DomRoot<GPURenderPipeline> {
let scope_id = self.use_current_scope();
let mut valid = true;
let (layout, implicit_ids, bgls) = self.get_pipeline_layout_data(&descriptor.parent.layout);
@ -1028,10 +876,9 @@ impl GPUDeviceMethods for GPUDevice {
multiview: None,
})
} else {
self.handle_server_msg(
scope_id,
WebGPUOpResult::ValidationError(String::from("Invalid GPUColorWriteFlags")),
);
self.dispatch_error(webgpu::Error::Validation(String::from(
"Invalid GPUColorWriteFlags",
)));
None
};
@ -1043,15 +890,12 @@ impl GPUDeviceMethods for GPUDevice {
self.channel
.0
.send((
scope_id,
WebGPURequest::CreateRenderPipeline {
.send(WebGPURequest::CreateRenderPipeline {
device_id: self.device.0,
render_pipeline_id,
descriptor: desc,
implicit_ids,
},
))
})
.expect("Failed to create WebGPU render pipeline");
let render_pipeline = webgpu::WebGPURenderPipeline(render_pipeline_id);
@ -1117,50 +961,33 @@ impl GPUDeviceMethods for GPUDevice {
/// <https://gpuweb.github.io/gpuweb/#dom-gpudevice-pusherrorscope>
fn PushErrorScope(&self, filter: GPUErrorFilter) {
let mut context = self.scope_context.borrow_mut();
let scope_id = context.next_scope_id;
context.next_scope_id = ErrorScopeId::new(scope_id.get() + 1).unwrap();
let err_scope = ErrorScopeInfo {
op_count: 0,
error: None,
promise: None,
};
let res = context.error_scopes.insert(scope_id, err_scope);
context.scope_stack.push(ErrorScopeMetadata {
id: scope_id,
filter,
popped: Cell::new(false),
});
assert!(res.is_none());
if self
.channel
.0
.send(WebGPURequest::PushErrorScope {
device_id: self.device.0,
filter: filter.to_webgpu(),
})
.is_err()
{
warn!("Failed sending WebGPURequest::PushErrorScope");
}
}
/// <https://gpuweb.github.io/gpuweb/#dom-gpudevice-poperrorscope>
fn PopErrorScope(&self, comp: InRealm) -> Rc<Promise> {
let mut context = self.scope_context.borrow_mut();
let promise = Promise::new_in_current_realm(comp);
let scope_id =
if let Some(meta) = context.scope_stack.iter().rev().find(|m| !m.popped.get()) {
meta.popped.set(true);
meta.id
} else {
promise.reject_error(Error::Operation);
return promise;
};
let remove = if let Some(err_scope) = context.error_scopes.get_mut(&scope_id) {
if let Some(ref e) = err_scope.error {
promise.resolve_native(e);
} else if err_scope.op_count == 0 {
promise.resolve_native(&None::<GPUError>);
}
err_scope.promise = Some(promise.clone());
err_scope.op_count == 0
} else {
error!("Could not find ErrorScope with Id({})", scope_id);
false
};
if remove {
let _ = context.error_scopes.remove(&scope_id);
context.scope_stack.retain(|meta| meta.id != scope_id);
let sender = response_async(&promise, self);
if self
.channel
.0
.send(WebGPURequest::PopErrorScope {
device_id: self.device.0,
sender,
})
.is_err()
{
warn!("Error when sending WebGPURequest::PopErrorScope");
}
promise
}
@ -1178,7 +1005,7 @@ impl GPUDeviceMethods for GPUDevice {
if let Err(e) = self
.channel
.0
.send((None, WebGPURequest::DestroyDevice(self.device.0)))
.send(WebGPURequest::DestroyDevice(self.device.0))
{
warn!("Failed to send DestroyDevice ({:?}) ({})", self.device.0, e);
}
@ -1186,19 +1013,35 @@ impl GPUDeviceMethods for GPUDevice {
}
}
impl AsyncWGPUListener for GPUDevice {
fn handle_response(&self, response: Option<WebGPUResponseResult>, promise: &Rc<Promise>) {
match response {
Some(Ok(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)) => {
let error = GPUError::from_error(&self.global(), error);
promise.resolve_native(&error);
},
},
_ => unreachable!("Wrong response recived on AsyncWGPUListener for GPUDevice"),
}
}
}
impl Drop for GPUDevice {
fn drop(&mut self) {
if let Err(e) = self
.channel
.0
.send((None, WebGPURequest::DestroyDevice(self.device.0)))
.send(WebGPURequest::DestroyDevice(self.device.0))
{
warn!("Failed to send DestroyDevice ({:?}) ({})", self.device.0, e);
}
if let Err(e) = self
.channel
.0
.send((None, WebGPURequest::DropDevice(self.device.0)))
.send(WebGPURequest::DropDevice(self.device.0))
{
warn!("Failed to send DropDevice ({:?}) ({})", self.device.0, e);
}

View file

@ -0,0 +1,90 @@
/* 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 dom_struct::dom_struct;
use js::rust::HandleObject;
use webgpu::{Error, ErrorFilter};
use super::types::{GPUInternalError, GPUOutOfMemoryError, GPUValidationError};
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{GPUErrorFilter, GPUErrorMethods};
use crate::dom::bindings::reflector::{reflect_dom_object_with_proto, Reflector};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::globalscope::GlobalScope;
#[dom_struct]
pub struct GPUError {
reflector_: Reflector,
message: DOMString,
}
impl GPUError {
pub fn new_inherited(message: DOMString) -> Self {
Self {
reflector_: Reflector::new(),
message,
}
}
#[allow(dead_code)]
pub fn new(global: &GlobalScope, message: DOMString) -> DomRoot<Self> {
Self::new_with_proto(global, None, message)
}
#[allow(dead_code)]
pub fn new_with_proto(
global: &GlobalScope,
proto: Option<HandleObject>,
message: DOMString,
) -> DomRoot<Self> {
reflect_dom_object_with_proto(Box::new(GPUError::new_inherited(message)), global, proto)
}
pub fn from_error(global: &GlobalScope, error: Error) -> DomRoot<Self> {
match error {
Error::Validation(msg) => DomRoot::upcast(GPUValidationError::new_with_proto(
global,
None,
DOMString::from_string(msg),
)),
Error::OutOfMemory(msg) => DomRoot::upcast(GPUOutOfMemoryError::new_with_proto(
global,
None,
DOMString::from_string(msg),
)),
Error::Internal(msg) => DomRoot::upcast(GPUInternalError::new_with_proto(
global,
None,
DOMString::from_string(msg),
)),
}
}
}
impl GPUErrorMethods for GPUError {
/// <https://gpuweb.github.io/gpuweb/#dom-gpuerror-message>
fn Message(&self) -> DOMString {
self.message.clone()
}
}
impl From<ErrorFilter> for GPUErrorFilter {
fn from(filter: ErrorFilter) -> Self {
match filter {
ErrorFilter::Validation => GPUErrorFilter::Validation,
ErrorFilter::OutOfMemory => GPUErrorFilter::Out_of_memory,
ErrorFilter::Internal => GPUErrorFilter::Internal,
}
}
}
impl GPUErrorFilter {
pub fn to_webgpu(&self) -> ErrorFilter {
match self {
GPUErrorFilter::Validation => ErrorFilter::Validation,
GPUErrorFilter::Out_of_memory => ErrorFilter::OutOfMemory,
GPUErrorFilter::Internal => ErrorFilter::Internal,
}
}
}

View file

@ -0,0 +1,43 @@
/* 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 dom_struct::dom_struct;
use js::rust::HandleObject;
use super::types::GPUError;
use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::globalscope::GlobalScope;
#[dom_struct]
pub struct GPUInternalError {
gpu_error: GPUError,
}
impl GPUInternalError {
fn new_inherited(message: DOMString) -> Self {
Self {
gpu_error: GPUError::new_inherited(message),
}
}
pub fn new_with_proto(
global: &GlobalScope,
proto: Option<HandleObject>,
message: DOMString,
) -> DomRoot<Self> {
reflect_dom_object_with_proto(Box::new(Self::new_inherited(message)), global, proto)
}
/// <https://gpuweb.github.io/gpuweb/#dom-GPUInternalError-GPUInternalError>
#[allow(non_snake_case)]
pub fn Constructor(
global: &GlobalScope,
proto: Option<HandleObject>,
message: DOMString,
) -> DomRoot<Self> {
Self::new_with_proto(global, proto, message)
}
}

View file

@ -5,37 +5,39 @@
use dom_struct::dom_struct;
use js::rust::HandleObject;
use crate::dom::bindings::reflector::{reflect_dom_object_with_proto, Reflector};
use super::types::GPUError;
use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::globalscope::GlobalScope;
#[dom_struct]
pub struct GPUOutOfMemoryError {
reflector_: Reflector,
gpu_error: GPUError,
}
impl GPUOutOfMemoryError {
fn new_inherited() -> Self {
fn new_inherited(message: DOMString) -> Self {
Self {
reflector_: Reflector::new(),
gpu_error: GPUError::new_inherited(message),
}
}
pub fn new(global: &GlobalScope) -> DomRoot<Self> {
Self::new_with_proto(global, None)
pub fn new_with_proto(
global: &GlobalScope,
proto: Option<HandleObject>,
message: DOMString,
) -> DomRoot<Self> {
reflect_dom_object_with_proto(Box::new(Self::new_inherited(message)), global, proto)
}
fn new_with_proto(global: &GlobalScope, proto: Option<HandleObject>) -> DomRoot<Self> {
reflect_dom_object_with_proto(
Box::new(GPUOutOfMemoryError::new_inherited()),
global,
proto,
)
}
/// <https://gpuweb.github.io/gpuweb/#dom-gpuoutofmemoryerror-gpuoutofmemoryerror>
/// <https://gpuweb.github.io/gpuweb/#dom-GPUOutOfMemoryError-GPUOutOfMemoryError>
#[allow(non_snake_case)]
pub fn Constructor(global: &GlobalScope, proto: Option<HandleObject>) -> DomRoot<Self> {
GPUOutOfMemoryError::new_with_proto(global, proto)
pub fn Constructor(
global: &GlobalScope,
proto: Option<HandleObject>,
message: DOMString,
) -> DomRoot<Self> {
Self::new_with_proto(global, proto, message)
}
}

View file

@ -84,10 +84,11 @@ impl GPUPipelineLayoutMethods for GPUPipelineLayout {
impl Drop for GPUPipelineLayout {
fn drop(&mut self) {
if let Err(e) = self.channel.0.send((
None,
WebGPURequest::DropPipelineLayout(self.pipeline_layout.0),
)) {
if let Err(e) = self
.channel
.0
.send(WebGPURequest::DropPipelineLayout(self.pipeline_layout.0))
{
warn!(
"Failed to send DropPipelineLayout ({:?}) ({})",
self.pipeline_layout.0, e

View file

@ -6,7 +6,7 @@ use std::rc::Rc;
use dom_struct::dom_struct;
use ipc_channel::ipc::IpcSharedMemory;
use webgpu::{wgt, WebGPU, WebGPUOpResult, WebGPUQueue, WebGPURequest, WebGPUResponse};
use webgpu::{wgt, WebGPU, WebGPUQueue, WebGPURequest, WebGPUResponse};
use super::bindings::codegen::Bindings::WebGPUBinding::{GPUImageCopyTexture, GPUImageDataLayout};
use super::gpu::{response_async, AsyncWGPUListener};
@ -81,26 +81,23 @@ impl GPUQueueMethods for GPUQueue {
.iter()
.all(|b| matches!(b.state(), GPUBufferState::Unmapped))
});
let scope_id = self.device.borrow().as_ref().unwrap().use_current_scope();
if !valid {
self.device.borrow().as_ref().unwrap().handle_server_msg(
scope_id,
WebGPUOpResult::ValidationError(String::from(
self.device
.borrow()
.as_ref()
.unwrap()
.dispatch_error(webgpu::Error::Validation(String::from(
"Referenced GPUBuffer(s) are not Unmapped",
)),
);
)));
return;
}
let command_buffers = command_buffers.iter().map(|cb| cb.id().0).collect();
self.channel
.0
.send((
scope_id,
WebGPURequest::Submit {
.send(WebGPURequest::Submit {
queue_id: self.queue.0,
command_buffers,
},
))
})
.unwrap();
}
@ -135,15 +132,12 @@ impl GPUQueueMethods for GPUQueue {
let final_data = IpcSharedMemory::from_bytes(
&bytes[data_offset as usize..(data_offset + content_size) as usize],
);
if let Err(e) = self.channel.0.send((
self.device.borrow().as_ref().unwrap().use_current_scope(),
WebGPURequest::WriteBuffer {
if let Err(e) = self.channel.0.send(WebGPURequest::WriteBuffer {
queue_id: self.queue.0,
buffer_id: buffer.id().0,
buffer_offset,
data: final_data,
},
)) {
}) {
warn!("Failed to send WriteBuffer({:?}) ({})", buffer.id(), e);
return Err(Error::Operation);
}
@ -174,16 +168,13 @@ impl GPUQueueMethods for GPUQueue {
let write_size = convert_texture_size_to_wgt(&convert_texture_size_to_dict(&size));
let final_data = IpcSharedMemory::from_bytes(&bytes);
if let Err(e) = self.channel.0.send((
self.device.borrow().as_ref().unwrap().use_current_scope(),
WebGPURequest::WriteTexture {
if let Err(e) = self.channel.0.send(WebGPURequest::WriteTexture {
queue_id: self.queue.0,
texture_cv,
data_layout: texture_layout,
size: write_size,
data: final_data,
},
)) {
}) {
warn!(
"Failed to send WriteTexture({:?}) ({})",
destination.texture.id().0,
@ -200,13 +191,14 @@ impl GPUQueueMethods for GPUQueue {
let global = self.global();
let promise = Promise::new(&global);
let sender = response_async(&promise, self);
if let Err(e) = self.channel.0.send((
self.device.borrow().as_ref().unwrap().use_current_scope(),
WebGPURequest::QueueOnSubmittedWorkDone {
if let Err(e) = self
.channel
.0
.send(WebGPURequest::QueueOnSubmittedWorkDone {
sender,
queue_id: self.queue.0,
},
)) {
})
{
warn!("QueueOnSubmittedWorkDone failed with {e}")
}
promise

View file

@ -83,7 +83,7 @@ impl Drop for GPURenderBundle {
if let Err(e) = self
.channel
.0
.send((None, WebGPURequest::DropRenderBundle(self.render_bundle.0)))
.send(WebGPURequest::DropRenderBundle(self.render_bundle.0))
{
warn!(
"Failed to send DropRenderBundle ({:?}) ({})",

View file

@ -209,15 +209,12 @@ impl GPURenderBundleEncoderMethods for GPURenderBundleEncoder {
self.channel
.0
.send((
self.device.use_current_scope(),
WebGPURequest::RenderBundleEncoderFinish {
.send(WebGPURequest::RenderBundleEncoderFinish {
render_bundle_encoder: encoder,
descriptor: desc,
render_bundle_id,
device_id: self.device.id().0,
},
))
})
.expect("Failed to send RenderBundleEncoderFinish");
let render_bundle = WebGPURenderBundle(render_bundle_id);

View file

@ -164,13 +164,10 @@ impl GPURenderPassEncoderMethods for GPURenderPassEncoder {
let render_pass = self.render_pass.borrow_mut().take();
self.channel
.0
.send((
None,
WebGPURequest::RunRenderPass {
.send(WebGPURequest::RunRenderPass {
command_encoder_id: self.command_encoder.id().0,
render_pass,
},
))
})
.expect("Failed to send RunRenderPass");
self.command_encoder.set_state(

View file

@ -100,10 +100,11 @@ impl GPURenderPipelineMethods for GPURenderPipeline {
impl Drop for GPURenderPipeline {
fn drop(&mut self) {
if let Err(e) = self.channel.0.send((
None,
WebGPURequest::DropRenderPipeline(self.render_pipeline.0),
)) {
if let Err(e) = self
.channel
.0
.send(WebGPURequest::DropRenderPipeline(self.render_pipeline.0))
{
warn!(
"Failed to send WebGPURequest::DropRenderPipeline({:?}) ({})",
self.render_pipeline.0, e

View file

@ -88,7 +88,7 @@ impl Drop for GPUSampler {
if let Err(e) = self
.channel
.0
.send((None, WebGPURequest::DropSampler(self.sampler.0)))
.send(WebGPURequest::DropSampler(self.sampler.0))
{
warn!("Failed to send DropSampler ({:?}) ({})", self.sampler.0, e);
}

View file

@ -82,7 +82,7 @@ impl Drop for GPUShaderModule {
if let Err(e) = self
.channel
.0
.send((None, WebGPURequest::DropShaderModule(self.shader_module.0)))
.send(WebGPURequest::DropShaderModule(self.shader_module.0))
{
warn!(
"Failed to send DropShaderModule ({:?}) ({})",

View file

@ -7,7 +7,7 @@ use std::string::String;
use dom_struct::dom_struct;
use webgpu::wgc::resource;
use webgpu::{wgt, WebGPU, WebGPUOpResult, WebGPURequest, WebGPUTexture, WebGPUTextureView};
use webgpu::{wgt, WebGPU, WebGPURequest, WebGPUTexture, WebGPUTextureView};
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
@ -114,7 +114,7 @@ impl Drop for GPUTexture {
if let Err(e) = self
.channel
.0
.send((None, WebGPURequest::DropTexture(self.texture.0)))
.send(WebGPURequest::DropTexture(self.texture.0))
{
warn!(
"Failed to send WebGPURequest::DropTexture({:?}) ({})",
@ -143,8 +143,6 @@ impl GPUTextureMethods for GPUTexture {
/// <https://gpuweb.github.io/gpuweb/#dom-gputexture-createview>
fn CreateView(&self, descriptor: &GPUTextureViewDescriptor) -> DomRoot<GPUTextureView> {
let scope_id = self.device.use_current_scope();
let desc = if !matches!(descriptor.mipLevelCount, Some(0)) &&
!matches!(descriptor.arrayLayerCount, Some(0))
{
@ -165,12 +163,10 @@ impl GPUTextureMethods for GPUTexture {
},
})
} else {
self.device.handle_server_msg(
scope_id,
WebGPUOpResult::ValidationError(String::from(
self.device
.dispatch_error(webgpu::Error::Validation(String::from(
"arrayLayerCount and mipLevelCount cannot be 0",
)),
);
)));
None
};
@ -182,15 +178,12 @@ impl GPUTextureMethods for GPUTexture {
self.channel
.0
.send((
scope_id,
WebGPURequest::CreateTextureView {
.send(WebGPURequest::CreateTextureView {
texture_id: self.texture.0,
texture_view_id,
device_id: self.device.id().0,
descriptor: desc,
},
))
})
.expect("Failed to create WebGPU texture view");
let texture_view = WebGPUTextureView(texture_view_id);
@ -209,13 +202,10 @@ impl GPUTextureMethods for GPUTexture {
if self.destroyed.get() {
return;
}
if let Err(e) = self.channel.0.send((
None,
WebGPURequest::DestroyTexture {
if let Err(e) = self.channel.0.send(WebGPURequest::DestroyTexture {
device_id: self.device.id().0,
texture_id: self.texture.0,
},
)) {
}) {
warn!(
"Failed to send WebGPURequest::DestroyTexture({:?}) ({})",
self.texture.0, e

View file

@ -83,7 +83,7 @@ impl Drop for GPUTextureView {
if let Err(e) = self
.channel
.0
.send((None, WebGPURequest::DropTextureView(self.texture_view.0)))
.send(WebGPURequest::DropTextureView(self.texture_view.0))
{
warn!(
"Failed to send DropTextureView ({:?}) ({})",

View file

@ -8,25 +8,26 @@ use servo_atoms::Atom;
use crate::dom::bindings::codegen::Bindings::EventBinding::Event_Binding::EventMethods;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
GPUError, GPUUncapturedErrorEventInit, GPUUncapturedErrorEventMethods,
GPUUncapturedErrorEventInit, GPUUncapturedErrorEventMethods,
};
use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::event::Event;
use crate::dom::globalscope::GlobalScope;
use crate::dom::gpuerror::GPUError;
#[dom_struct]
pub struct GPUUncapturedErrorEvent {
event: Event,
#[ignore_malloc_size_of = "Because it is non-owning"]
gpu_error: GPUError,
gpu_error: Dom<GPUError>,
}
impl GPUUncapturedErrorEvent {
fn new_inherited(init: &GPUUncapturedErrorEventInit) -> Self {
Self {
gpu_error: clone_gpu_error(&init.error),
gpu_error: Dom::from_ref(&init.error),
event: Event::new_inherited(),
}
}
@ -78,8 +79,8 @@ impl GPUUncapturedErrorEvent {
impl GPUUncapturedErrorEventMethods for GPUUncapturedErrorEvent {
/// <https://gpuweb.github.io/gpuweb/#dom-gpuuncapturederrorevent-error>
fn Error(&self) -> GPUError {
clone_gpu_error(&self.gpu_error)
fn Error(&self) -> DomRoot<GPUError> {
DomRoot::from_ref(&self.gpu_error)
}
/// <https://dom.spec.whatwg.org/#dom-event-istrusted>
@ -87,10 +88,3 @@ impl GPUUncapturedErrorEventMethods for GPUUncapturedErrorEvent {
self.event.IsTrusted()
}
}
fn clone_gpu_error(error: &GPUError) -> GPUError {
match *error {
GPUError::GPUValidationError(ref v) => GPUError::GPUValidationError(v.clone()),
GPUError::GPUOutOfMemoryError(ref w) => GPUError::GPUOutOfMemoryError(w.clone()),
}
}

View file

@ -5,41 +5,30 @@
use dom_struct::dom_struct;
use js::rust::HandleObject;
use super::bindings::error::Fallible;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::GPUValidationErrorMethods;
use crate::dom::bindings::reflector::{reflect_dom_object_with_proto, Reflector};
use super::types::GPUError;
use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::globalscope::GlobalScope;
#[dom_struct]
pub struct GPUValidationError {
reflector_: Reflector,
message: DOMString,
gpu_error: GPUError,
}
impl GPUValidationError {
fn new_inherited(message: DOMString) -> Self {
Self {
reflector_: Reflector::new(),
message,
gpu_error: GPUError::new_inherited(message),
}
}
pub fn new(global: &GlobalScope, message: DOMString) -> DomRoot<Self> {
Self::new_with_proto(global, None, message)
}
fn new_with_proto(
pub fn new_with_proto(
global: &GlobalScope,
proto: Option<HandleObject>,
message: DOMString,
) -> DomRoot<Self> {
reflect_dom_object_with_proto(
Box::new(GPUValidationError::new_inherited(message)),
global,
proto,
)
reflect_dom_object_with_proto(Box::new(Self::new_inherited(message)), global, proto)
}
/// <https://gpuweb.github.io/gpuweb/#dom-gpuvalidationerror-gpuvalidationerror>
@ -48,14 +37,7 @@ impl GPUValidationError {
global: &GlobalScope,
proto: Option<HandleObject>,
message: DOMString,
) -> Fallible<DomRoot<Self>> {
Ok(GPUValidationError::new_with_proto(global, proto, message))
}
}
impl GPUValidationErrorMethods for GPUValidationError {
/// <https://gpuweb.github.io/gpuweb/#dom-gpuvalidationerror-message>
fn Message(&self) -> DOMString {
self.message.clone()
) -> DomRoot<Self> {
Self::new_with_proto(global, proto, message)
}
}

View file

@ -340,6 +340,8 @@ pub mod gpucomputepipeline;
pub mod gpuconvert;
pub mod gpudevice;
pub mod gpudevicelostinfo;
pub mod gpuerror;
pub mod gpuinternalerror;
pub mod gpumapmode;
pub mod gpuoutofmemoryerror;
pub mod gpupipelinelayout;

View file

@ -1086,19 +1086,28 @@ partial interface GPUDevice {
readonly attribute Promise<GPUDeviceLostInfo> lost;
};
[Exposed=(Window, DedicatedWorker), Pref="dom.webgpu.enabled"]
interface GPUValidationError {
[Throws]
constructor(DOMString message);
[Exposed=(Window, Worker), Pref="dom.webgpu.enabled"]
interface GPUError {
readonly attribute DOMString message;
};
[Exposed=(Window, DedicatedWorker), Pref="dom.webgpu.enabled"]
interface GPUOutOfMemoryError {
constructor();
[Exposed=(Window, Worker), Pref="dom.webgpu.enabled"]
interface GPUValidationError
: GPUError {
constructor(DOMString message);
};
typedef (GPUOutOfMemoryError or GPUValidationError) GPUError;
[Exposed=(Window, Worker), Pref="dom.webgpu.enabled"]
interface GPUOutOfMemoryError
: GPUError {
constructor(DOMString message);
};
[Exposed=(Window, Worker), Pref="dom.webgpu.enabled"]
interface GPUInternalError
: GPUError {
constructor(DOMString message);
};
enum GPUErrorFilter {
"validation",
@ -1112,7 +1121,7 @@ partial interface GPUDevice {
Promise<GPUError?> popErrorScope();
};
[Exposed=(Window, DedicatedWorker), Pref="dom.webgpu.enabled"]
[Exposed=(Window, Worker), Pref="dom.webgpu.enabled"]
interface GPUUncapturedErrorEvent : Event {
constructor(
DOMString type,

View file

@ -2398,16 +2398,6 @@ impl ScriptThread {
WebGPUMsg::FreeTexture(id) => self.gpu_id_hub.lock().kill_texture_id(id),
WebGPUMsg::FreeTextureView(id) => self.gpu_id_hub.lock().kill_texture_view_id(id),
WebGPUMsg::Exit => *self.webgpu_port.borrow_mut() = None,
WebGPUMsg::WebGPUOpResult {
device,
scope_id,
pipeline_id,
result,
} => {
let global = self.documents.borrow().find_global(pipeline_id).unwrap();
let _ac = enter_realm(&*global);
global.handle_wgpu_msg(device, scope_id, result);
},
WebGPUMsg::CleanDevice {
pipeline_id,
device,
@ -2415,6 +2405,15 @@ impl ScriptThread {
let global = self.documents.borrow().find_global(pipeline_id).unwrap();
global.remove_gpu_device(device);
},
WebGPUMsg::UncapturedError {
device,
pipeline_id,
error,
} => {
let global = self.documents.borrow().find_global(pipeline_id).unwrap();
let _ac = enter_realm(&*global);
global.handle_uncaptured_gpu_error(device, error);
},
_ => {},
}
}

View file

@ -29,7 +29,7 @@ use wgc::resource::{
pub use {wgpu_core as wgc, wgpu_types as wgt};
use crate::identity::*;
use crate::{WebGPU, PRESENTATION_BUFFER_COUNT};
use crate::{Error, ErrorFilter, PopError, WebGPU, PRESENTATION_BUFFER_COUNT};
#[derive(Debug, Deserialize, Serialize)]
#[allow(clippy::large_enum_variant)]
@ -48,6 +48,7 @@ pub enum WebGPUResponse {
},
BufferMapAsync(IpcSharedMemory),
SubmittedWorkDone,
PoppedErrorScope(Result<Option<Error>, PopError>),
}
pub type WebGPUResponseResult = Result<WebGPUResponse, String>;
@ -251,4 +252,16 @@ pub enum WebGPURequest {
sender: IpcSender<Option<WebGPUResponseResult>>,
queue_id: id::QueueId,
},
PushErrorScope {
device_id: id::DeviceId,
filter: ErrorFilter,
},
DispatchError {
device_id: id::DeviceId,
error: Error,
},
PopErrorScope {
device_id: id::DeviceId,
sender: IpcSender<Option<WebGPUResponseResult>>,
},
}

View file

@ -0,0 +1,100 @@
/* 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/. */
//! Error scopes and GPUError types
use std::fmt;
use serde::{Deserialize, Serialize};
use crate::wgc;
/// <https://www.w3.org/TR/webgpu/#gpu-error-scope>
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub(crate) struct ErrorScope {
pub errors: Vec<Error>,
pub filter: ErrorFilter,
}
impl ErrorScope {
pub fn new(filter: ErrorFilter) -> Self {
Self {
filter,
errors: Vec::new(),
}
}
}
/// <https://www.w3.org/TR/webgpu/#enumdef-gpuerrorfilter>
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum ErrorFilter {
Validation,
OutOfMemory,
Internal,
}
/// <https://www.w3.org/TR/webgpu/#gpuerror>
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum Error {
Validation(String),
OutOfMemory(String),
Internal(String),
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.message())
}
}
impl Error {
pub fn filter(&self) -> ErrorFilter {
match self {
Error::Validation(_) => ErrorFilter::Validation,
Error::OutOfMemory(_) => ErrorFilter::OutOfMemory,
Error::Internal(_) => ErrorFilter::Internal,
}
}
pub fn message(&self) -> &str {
match self {
Error::Validation(m) => m,
Error::OutOfMemory(m) => m,
Error::Internal(m) => m,
}
}
// TODO: labels
// based on https://github.com/gfx-rs/wgpu/blob/trunk/wgpu/src/backend/wgpu_core.rs#L289
pub fn from_error<E: std::error::Error + 'static>(error: E) -> Self {
let mut source_opt: Option<&(dyn std::error::Error + 'static)> = Some(&error);
while let Some(source) = source_opt {
if let Some(wgc::device::DeviceError::OutOfMemory) =
source.downcast_ref::<wgc::device::DeviceError>()
{
return Self::OutOfMemory(error.to_string());
}
source_opt = source.source();
}
// TODO: This hack is needed because there are
// multiple OutOfMemory error variant in wgpu-core
// and even upstream does not handle them correctly
if format!("{error:?}").contains("OutOfMemory") {
return Self::OutOfMemory(error.to_string());
}
Self::Validation(error.to_string())
}
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub enum PopError {
Lost,
Empty,
}

View file

@ -13,11 +13,11 @@ mod wgpu_thread;
use std::borrow::Cow;
use std::collections::HashMap;
use std::num::NonZeroU64;
use std::sync::{Arc, Mutex};
use arrayvec::ArrayVec;
use euclid::default::Size2D;
pub use gpu_error::{Error, ErrorFilter, PopError};
use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
use serde::{Deserialize, Serialize};
use servo_config::pref;
@ -28,16 +28,15 @@ use webrender_traits::{
use wgc::id;
mod dom_messages;
mod gpu_error;
mod script_messages;
pub use dom_messages::*;
pub use identity::*;
pub use script_messages::*;
pub type ErrorScopeId = NonZeroU64;
pub use wgpu_thread::PRESENTATION_BUFFER_COUNT;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct WebGPU(pub IpcSender<(Option<ErrorScopeId>, WebGPURequest)>);
pub struct WebGPU(pub IpcSender<WebGPURequest>);
impl WebGPU {
pub fn new(
@ -95,7 +94,7 @@ impl WebGPU {
pub fn exit(&self, sender: IpcSender<()>) -> Result<(), &'static str> {
self.0
.send((None, WebGPURequest::Exit(sender)))
.send(WebGPURequest::Exit(sender))
.map_err(|_| "Failed to send Exit message")
}
}

View file

@ -7,20 +7,13 @@
use base::id::PipelineId;
use serde::{Deserialize, Serialize};
use crate::gpu_error::Error;
use crate::identity::WebGPUDevice;
use crate::wgc::id::{
AdapterId, BindGroupId, BindGroupLayoutId, BufferId, CommandBufferId, ComputePipelineId,
DeviceId, PipelineLayoutId, QuerySetId, RenderBundleId, RenderPipelineId, SamplerId,
ShaderModuleId, StagingBufferId, SurfaceId, TextureId, TextureViewId,
};
use crate::ErrorScopeId;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum WebGPUOpResult {
ValidationError(String),
OutOfMemoryError,
Success,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum WebGPUMsg {
@ -41,15 +34,14 @@ pub enum WebGPUMsg {
FreeRenderBundle(RenderBundleId),
FreeStagingBuffer(StagingBufferId),
FreeQuerySet(QuerySetId),
WebGPUOpResult {
device: WebGPUDevice,
scope_id: Option<ErrorScopeId>,
pipeline_id: PipelineId,
result: WebGPUOpResult,
},
CleanDevice {
device: WebGPUDevice,
pipeline_id: PipelineId,
},
UncapturedError {
device: WebGPUDevice,
pipeline_id: PipelineId,
error: Error,
},
Exit,
}

View file

@ -20,28 +20,53 @@ use webrender_traits::{WebrenderExternalImageRegistry, WebrenderImageHandlerType
use wgc::command::{ImageCopyBuffer, ImageCopyTexture};
use wgc::device::queue::SubmittedWorkDoneClosure;
use wgc::device::{DeviceDescriptor, HostMap, ImplicitPipelineIds};
use wgc::id::DeviceId;
use wgc::pipeline::ShaderModuleDescriptor;
use wgc::resource::{BufferMapCallback, BufferMapOperation};
use wgc::{gfx_select, id};
use wgt::InstanceDescriptor;
pub use {wgpu_core as wgc, wgpu_types as wgt};
use crate::gpu_error::ErrorScope;
use crate::poll_thread::Poller;
use crate::{
ErrorScopeId, PresentationData, Transmute, WebGPU, WebGPUAdapter, WebGPUDevice, WebGPUMsg,
WebGPUOpResult, WebGPUQueue, WebGPURequest, WebGPUResponse,
Error, PopError, PresentationData, Transmute, WebGPU, WebGPUAdapter, WebGPUDevice, WebGPUMsg,
WebGPUQueue, WebGPURequest, WebGPUResponse,
};
pub const PRESENTATION_BUFFER_COUNT: usize = 10;
#[derive(Eq, Hash, PartialEq)]
pub(crate) struct DeviceScope {
pub device_id: DeviceId,
pub pipeline_id: PipelineId,
/// <https://www.w3.org/TR/webgpu/#dom-gpudevice-errorscopestack-slot>
pub error_scope_stack: Vec<ErrorScope>,
// TODO:
// Queue for this device (to remove transmutes)
// queue_id: QueueId,
// Poller for this device
// poller: Poller,
}
impl DeviceScope {
pub fn new(device_id: DeviceId, pipeline_id: PipelineId) -> Self {
Self {
device_id,
pipeline_id,
error_scope_stack: Vec::new(),
}
}
}
#[allow(clippy::upper_case_acronyms)] // Name of the library
pub(crate) struct WGPU {
receiver: IpcReceiver<(Option<ErrorScopeId>, WebGPURequest)>,
sender: IpcSender<(Option<ErrorScopeId>, WebGPURequest)>,
receiver: IpcReceiver<WebGPURequest>,
sender: IpcSender<WebGPURequest>,
script_sender: IpcSender<WebGPUMsg>,
global: Arc<wgc::global::Global>,
adapters: Vec<WebGPUAdapter>,
devices: HashMap<WebGPUDevice, PipelineId>,
devices: HashMap<DeviceId, DeviceScope>,
// Track invalid adapters https://gpuweb.github.io/gpuweb/#invalid
_invalid_adapters: Vec<WebGPUAdapter>,
//TODO: Remove this (https://github.com/gfx-rs/wgpu/issues/867)
@ -55,8 +80,8 @@ pub(crate) struct WGPU {
impl WGPU {
pub(crate) fn new(
receiver: IpcReceiver<(Option<ErrorScopeId>, WebGPURequest)>,
sender: IpcSender<(Option<ErrorScopeId>, WebGPURequest)>,
receiver: IpcReceiver<WebGPURequest>,
sender: IpcSender<WebGPURequest>,
script_sender: IpcSender<WebGPUMsg>,
webrender_api_sender: RenderApiSender,
webrender_document: DocumentId,
@ -89,7 +114,7 @@ impl WGPU {
pub(crate) fn run(&mut self) {
loop {
if let Ok((scope_id, msg)) = self.receiver.recv() {
if let Ok(msg) = self.receiver.recv() {
match msg {
WebGPURequest::BufferMapAsync {
sender,
@ -151,7 +176,7 @@ impl WGPU {
warn!("Failed to send BufferMapAsync Response ({:?})", w);
}
}
self.send_result(device_id, scope_id, result);
self.maybe_dispatch_wgpu_error(device_id, result.err());
},
WebGPURequest::CommandEncoderFinish {
command_encoder_id,
@ -160,24 +185,28 @@ impl WGPU {
} => {
let global = &self.global;
let result = if is_error {
Err(String::from("Invalid GPUCommandEncoder"))
Err(Error::Internal(String::from("Invalid GPUCommandEncoder")))
} else if let Some(err) = self
.error_command_encoders
.borrow()
.get(&command_encoder_id)
{
Err(err.clone())
Err(Error::Internal(err.clone()))
} else {
tuple_to_result(
if let Some(error) =
gfx_select!(command_encoder_id => global.command_encoder_finish(
command_encoder_id,
&wgt::CommandBufferDescriptor::default()
)),
)
.map_err(|e| format!("{:?}", e))
))
.1
{
Err(Error::from_error(error))
} else {
Ok(())
}
};
self.encoder_record_error(command_encoder_id, &result);
self.send_result(device_id, scope_id, result);
self.maybe_dispatch_error(device_id, result.err());
},
WebGPURequest::CopyBufferToBuffer {
command_encoder_id,
@ -249,9 +278,9 @@ impl WGPU {
descriptor,
} => {
let global = &self.global;
let result = tuple_to_result(gfx_select!(bind_group_id =>
global.device_create_bind_group(device_id, &descriptor, Some(bind_group_id))));
self.send_result(device_id, scope_id, result);
let (_, error) = gfx_select!(bind_group_id =>
global.device_create_bind_group(device_id, &descriptor, Some(bind_group_id)));
self.maybe_dispatch_wgpu_error(device_id, error);
},
WebGPURequest::CreateBindGroupLayout {
device_id,
@ -260,10 +289,10 @@ impl WGPU {
} => {
let global = &self.global;
if let Some(desc) = descriptor {
let result = tuple_to_result(gfx_select!(bind_group_layout_id =>
global.device_create_bind_group_layout(device_id, &desc, Some(bind_group_layout_id))));
let (_, error) = gfx_select!(bind_group_layout_id =>
global.device_create_bind_group_layout(device_id, &desc, Some(bind_group_layout_id)));
self.send_result(device_id, scope_id, result);
self.maybe_dispatch_wgpu_error(device_id, error);
}
},
WebGPURequest::CreateBuffer {
@ -273,10 +302,10 @@ impl WGPU {
} => {
let global = &self.global;
if let Some(desc) = descriptor {
let result = tuple_to_result(gfx_select!(buffer_id =>
global.device_create_buffer(device_id, &desc, Some(buffer_id))));
let (_, error) = gfx_select!(buffer_id =>
global.device_create_buffer(device_id, &desc, Some(buffer_id)));
self.send_result(device_id, scope_id, result);
self.maybe_dispatch_wgpu_error(device_id, error);
}
},
WebGPURequest::CreateCommandEncoder {
@ -286,10 +315,10 @@ impl WGPU {
} => {
let global = &self.global;
let desc = wgt::CommandEncoderDescriptor { label };
let result = tuple_to_result(gfx_select!(command_encoder_id =>
global.device_create_command_encoder(device_id, &desc, Some(command_encoder_id))));
let (_, error) = gfx_select!(command_encoder_id =>
global.device_create_command_encoder(device_id, &desc, Some(command_encoder_id)));
self.send_result(device_id, scope_id, result);
self.maybe_dispatch_wgpu_error(device_id, error);
},
WebGPURequest::CreateComputePipeline {
device_id,
@ -310,15 +339,14 @@ impl WGPU {
root_id: Some(*layout),
group_ids: bgls.as_slice(),
});
let result = tuple_to_result(
gfx_select!(compute_pipeline_id => global.device_create_compute_pipeline(
let (_, error) = gfx_select!(compute_pipeline_id => global.device_create_compute_pipeline(
device_id,
&descriptor,
Some(compute_pipeline_id),
implicit
)),
)
);
self.send_result(device_id, scope_id, result);
self.maybe_dispatch_wgpu_error(device_id, error);
},
WebGPURequest::CreateContext(sender) => {
let id = self
@ -336,9 +364,9 @@ impl WGPU {
descriptor,
} => {
let global = &self.global;
let result = tuple_to_result(gfx_select!(pipeline_layout_id =>
global.device_create_pipeline_layout(device_id, &descriptor, Some(pipeline_layout_id))));
self.send_result(device_id, scope_id, result);
let (_, error) = gfx_select!(pipeline_layout_id =>
global.device_create_pipeline_layout(device_id, &descriptor, Some(pipeline_layout_id)));
self.maybe_dispatch_wgpu_error(device_id, error);
},
WebGPURequest::CreateRenderPipeline {
device_id,
@ -360,14 +388,14 @@ impl WGPU {
group_ids: bgls.as_slice(),
});
if let Some(desc) = descriptor {
let result = tuple_to_result(gfx_select!(render_pipeline_id =>
let (_, error) = gfx_select!(render_pipeline_id =>
global.device_create_render_pipeline(
device_id,
&desc,
Some(render_pipeline_id),
implicit)
));
self.send_result(device_id, scope_id, result);
);
self.maybe_dispatch_wgpu_error(device_id, error);
}
},
WebGPURequest::CreateSampler {
@ -376,14 +404,12 @@ impl WGPU {
descriptor,
} => {
let global = &self.global;
let result = tuple_to_result(
gfx_select!(sampler_id => global.device_create_sampler(
let (_, error) = gfx_select!(sampler_id => global.device_create_sampler(
device_id,
&descriptor,
Some(sampler_id)
)),
);
self.send_result(device_id, scope_id, result);
));
self.maybe_dispatch_wgpu_error(device_id, error);
},
WebGPURequest::CreateShaderModule {
device_id,
@ -399,9 +425,9 @@ impl WGPU {
label: label.map(|s| s.into()),
shader_bound_checks: wgt::ShaderBoundChecks::default(),
};
let result = tuple_to_result(gfx_select!(program_id =>
global.device_create_shader_module(device_id, &desc, source, Some(program_id))));
self.send_result(device_id, scope_id, result);
let (_, error) = gfx_select!(program_id =>
global.device_create_shader_module(device_id, &desc, source, Some(program_id)));
self.maybe_dispatch_wgpu_error(device_id, error);
},
WebGPURequest::CreateSwapChain {
device_id,
@ -454,14 +480,13 @@ impl WGPU {
} => {
let global = &self.global;
if let Some(desc) = descriptor {
let result = tuple_to_result(
gfx_select!(texture_id => global.device_create_texture(
let (_, error) = gfx_select!(texture_id => global.device_create_texture(
device_id,
&desc,
Some(texture_id)
)),
)
);
self.send_result(device_id, scope_id, result);
self.maybe_dispatch_wgpu_error(device_id, error);
}
},
WebGPURequest::CreateTextureView {
@ -472,15 +497,14 @@ impl WGPU {
} => {
let global = &self.global;
if let Some(desc) = descriptor {
let result = tuple_to_result(
gfx_select!(texture_view_id => global.texture_create_view(
let (_, error) = gfx_select!(texture_view_id => global.texture_create_view(
texture_id,
&desc,
Some(texture_view_id)
)),
)
);
self.send_result(device_id, scope_id, result);
self.maybe_dispatch_wgpu_error(device_id, error);
}
},
WebGPURequest::DestroyBuffer(buffer) => {
@ -526,7 +550,7 @@ impl WGPU {
} => {
let global = &self.global;
let result = gfx_select!(texture_id => global.texture_destroy(texture_id));
self.send_result(device_id, scope_id, result);
self.maybe_dispatch_wgpu_error(device_id, result.err());
},
WebGPURequest::Exit(sender) => {
if let Err(e) = sender.send(()) {
@ -546,7 +570,7 @@ impl WGPU {
},
WebGPURequest::DropDevice(device_id) => {
let device = WebGPUDevice(device_id);
let pipeline_id = self.devices.remove(&device).unwrap();
let pipeline_id = self.devices.remove(&device_id).unwrap().pipeline_id;
if let Err(e) = self.script_sender.send(WebGPUMsg::CleanDevice {
device,
pipeline_id,
@ -566,15 +590,14 @@ impl WGPU {
device_id,
} => {
let global = &self.global;
let result = tuple_to_result(
gfx_select!(render_bundle_id => global.render_bundle_encoder_finish(
let (_, error) = gfx_select!(render_bundle_id => global.render_bundle_encoder_finish(
render_bundle_encoder,
&descriptor,
Some(render_bundle_id)
)),
)
);
self.send_result(device_id, scope_id, result);
self.maybe_dispatch_wgpu_error(device_id, error);
},
WebGPURequest::RequestAdapter {
sender,
@ -652,7 +675,8 @@ impl WGPU {
};
let device = WebGPUDevice(device_id);
let queue = WebGPUQueue(queue_id);
self.devices.insert(device, pipeline_id);
self.devices
.insert(device_id, DeviceScope::new(device_id, pipeline_id));
if let Err(e) = sender.send(Some(Ok(WebGPUResponse::RequestDevice {
device_id: device,
queue_id: queue,
@ -705,12 +729,14 @@ impl WGPU {
.contains_key(&id.into_command_encoder_id())
});
let result = if cmd_id.is_some() {
Err(String::from("Invalid command buffer submitted"))
Err(Error::Internal(String::from(
"Invalid command buffer submitted",
)))
} else {
gfx_select!(queue_id => global.queue_submit(queue_id, &command_buffers))
.map_err(|e| format!("{:?}", e))
.map_err(|e| Error::from_error(e))
};
self.send_result(queue_id.transmute(), scope_id, result);
self.maybe_dispatch_error(queue_id.transmute(), result.err());
},
WebGPURequest::SwapChainPresent {
external_id,
@ -888,7 +914,7 @@ impl WGPU {
.copy_from_slice(&array_buffer);
}
let result = gfx_select!(buffer_id => global.buffer_unmap(buffer_id));
self.send_result(device_id, scope_id, result);
self.maybe_dispatch_wgpu_error(device_id, result.err());
},
WebGPURequest::WriteBuffer {
queue_id,
@ -904,7 +930,7 @@ impl WGPU {
buffer_offset as wgt::BufferAddress,
&data
));
self.send_result(queue_id.transmute(), scope_id, result);
self.maybe_dispatch_wgpu_error(queue_id.transmute(), result.err());
},
WebGPURequest::WriteTexture {
queue_id,
@ -922,7 +948,7 @@ impl WGPU {
&data_layout,
&size
));
self.send_result(queue_id.transmute(), scope_id, result);
self.maybe_dispatch_wgpu_error(queue_id.transmute(), result.err());
},
WebGPURequest::QueueOnSubmittedWorkDone { sender, queue_id } => {
let global = &self.global;
@ -936,7 +962,7 @@ impl WGPU {
}));
let result = gfx_select!(queue_id => global.queue_on_submitted_work_done(queue_id, callback));
self.poller.wake();
self.send_result(queue_id.transmute(), scope_id, result);
self.maybe_dispatch_wgpu_error(queue_id.transmute(), result.err());
},
WebGPURequest::DropTexture(id) => {
let global = &self.global;
@ -1031,6 +1057,48 @@ impl WGPU {
warn!("Unable to send FreeQuerySet({:?}) ({:?})", id, e);
};
},
WebGPURequest::PushErrorScope { device_id, filter } => {
// <https://www.w3.org/TR/webgpu/#dom-gpudevice-pusherrorscope>
let device_scope = self
.devices
.get_mut(&device_id)
.expect("Using invalid device");
device_scope.error_scope_stack.push(ErrorScope::new(filter));
},
WebGPURequest::DispatchError { device_id, error } => {
self.dispatch_error(device_id, error);
},
WebGPURequest::PopErrorScope { device_id, sender } => {
// <https://www.w3.org/TR/webgpu/#dom-gpudevice-poperrorscope>
if let Some(device_scope) = self.devices.get_mut(&device_id) {
if let Some(error_scope) = device_scope.error_scope_stack.pop() {
if let Err(e) =
sender.send(Some(Ok(WebGPUResponse::PoppedErrorScope(Ok(
// TODO: Do actual selection instead of selecting first error
error_scope.errors.first().cloned(),
)))))
{
warn!(
"Unable to send {:?} to poperrorscope: {e:?}",
error_scope.errors
);
}
} else {
if let Err(e) = sender.send(Some(Ok(
WebGPUResponse::PoppedErrorScope(Err(PopError::Empty)),
))) {
warn!("Unable to send PopError::Empty: {e:?}");
}
}
} else {
// device lost
if let Err(e) = sender.send(Some(Ok(WebGPUResponse::PoppedErrorScope(
Err(PopError::Lost),
)))) {
warn!("Unable to send PopError::Lost due {e:?}");
}
}
},
}
}
}
@ -1039,30 +1107,45 @@ impl WGPU {
}
}
fn send_result<U, T: std::fmt::Debug>(
&self,
fn maybe_dispatch_wgpu_error<E: std::error::Error + 'static>(
&mut self,
device_id: id::DeviceId,
scope_id: Option<ErrorScopeId>,
result: Result<U, T>,
error: Option<E>,
) {
let &pipeline_id = self.devices.get(&WebGPUDevice(device_id)).unwrap();
if let Err(w) = self.script_sender.send(WebGPUMsg::WebGPUOpResult {
self.maybe_dispatch_error(device_id, error.map(|e| Error::from_error(e)))
}
/// Dispatches error (if there is any)
fn maybe_dispatch_error(&mut self, device_id: id::DeviceId, error: Option<Error>) {
if let Some(error) = error {
self.dispatch_error(device_id, error);
}
}
/// <https://www.w3.org/TR/webgpu/#abstract-opdef-dispatch-error>
fn dispatch_error(&mut self, device_id: id::DeviceId, error: Error) {
if let Some(device_scope) = self.devices.get_mut(&device_id) {
if let Some(error_scope) = device_scope
.error_scope_stack
.iter_mut()
.rev()
.find(|error_scope| error_scope.filter == error.filter())
{
error_scope.errors.push(error);
} else {
if self
.script_sender
.send(WebGPUMsg::UncapturedError {
device: WebGPUDevice(device_id),
scope_id,
pipeline_id,
result: if let Err(e) = result {
let err = format!("{:?}", e);
if err.contains("OutOfMemory") {
WebGPUOpResult::OutOfMemoryError
} else {
WebGPUOpResult::ValidationError(err)
pipeline_id: device_scope.pipeline_id,
error: error.clone(),
})
.is_err()
{
warn!("Failed to send WebGPUMsg::UncapturedError: {error:?}");
}
} else {
WebGPUOpResult::Success
},
}) {
warn!("Failed to send WebGPUOpResult ({})", w);
}
} // else device is lost
}
fn encoder_record_error<U, T: std::fmt::Debug>(
@ -1078,11 +1161,3 @@ impl WGPU {
}
}
}
fn tuple_to_result<T, E>(res: (T, Option<E>)) -> Result<T, E> {
if let Some(err) = res.1 {
Err(err)
} else {
Ok(res.0)
}
}

File diff suppressed because it is too large Load diff