Report errors from void returning operations

This commit is contained in:
Kunal Mohan 2020-08-01 16:32:37 +05:30
parent 6499367fe2
commit 8cb5fad828
10 changed files with 140 additions and 38 deletions

6
Cargo.lock generated
View file

@ -3701,7 +3701,7 @@ checksum = "0419348c027fa7be448d2ae7ea0e4e04c2334c31dc4e74ab29f00a2a7ca69204"
[[package]]
name = "naga"
version = "0.1.0"
source = "git+https://github.com/gfx-rs/naga?rev=94802078c3bc5d138f497419ea3e7a869f10916d#94802078c3bc5d138f497419ea3e7a869f10916d"
source = "git+https://github.com/gfx-rs/naga?rev=1eb637038dd15fc1dad770eca8e6943424dbc122#1eb637038dd15fc1dad770eca8e6943424dbc122"
dependencies = [
"bitflags",
"fxhash",
@ -6911,7 +6911,7 @@ dependencies = [
[[package]]
name = "wgpu-core"
version = "0.5.0"
source = "git+https://github.com/gfx-rs/wgpu#9e4839eb049707629fa8a91e3603085433f352a4"
source = "git+https://github.com/gfx-rs/wgpu#872a6c4c2bab5591838219c34e0cbf5fa9c2ec85"
dependencies = [
"arrayvec 0.5.1",
"bitflags",
@ -6938,7 +6938,7 @@ dependencies = [
[[package]]
name = "wgpu-types"
version = "0.5.0"
source = "git+https://github.com/gfx-rs/wgpu#9e4839eb049707629fa8a91e3603085433f352a4"
source = "git+https://github.com/gfx-rs/wgpu#872a6c4c2bab5591838219c34e0cbf5fa9c2ec85"
dependencies = [
"bitflags",
"serde",

View file

@ -8,10 +8,11 @@ use crate::dom::bindings::codegen::Bindings::GPUMapModeBinding::GPUMapModeConsta
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::reflector::DomObject;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::USVString;
use crate::dom::globalscope::GlobalScope;
use crate::dom::gpu::{response_async, AsyncWGPUListener};
use crate::dom::gpudevice::GPUDevice;
use crate::dom::promise::Promise;
use crate::realms::InRealm;
use crate::script_runtime::JSContext;
@ -25,9 +26,7 @@ use std::ffi::c_void;
use std::ops::Range;
use std::ptr::NonNull;
use std::rc::Rc;
use webgpu::{
wgpu::device::HostMap, WebGPU, WebGPUBuffer, WebGPUDevice, WebGPURequest, WebGPUResponse,
};
use webgpu::{wgpu::device::HostMap, WebGPU, WebGPUBuffer, WebGPURequest, WebGPUResponse};
const RANGE_OFFSET_ALIGN_MASK: u64 = 8;
const RANGE_SIZE_ALIGN_MASK: u64 = 4;
@ -61,7 +60,7 @@ pub struct GPUBuffer {
label: DomRefCell<Option<USVString>>,
state: Cell<GPUBufferState>,
buffer: WebGPUBuffer,
device: WebGPUDevice,
device: Dom<GPUDevice>,
size: GPUSize64,
#[ignore_malloc_size_of = "promises are hard"]
map_promise: DomRefCell<Option<Rc<Promise>>>,
@ -72,7 +71,7 @@ impl GPUBuffer {
fn new_inherited(
channel: WebGPU,
buffer: WebGPUBuffer,
device: WebGPUDevice,
device: &GPUDevice,
state: GPUBufferState,
size: GPUSize64,
map_info: DomRefCell<Option<GPUBufferMapInfo>>,
@ -83,7 +82,7 @@ impl GPUBuffer {
channel,
label: DomRefCell::new(label),
state: Cell::new(state),
device,
device: Dom::from_ref(device),
buffer,
map_promise: DomRefCell::new(None),
size,
@ -96,7 +95,7 @@ impl GPUBuffer {
global: &GlobalScope,
channel: WebGPU,
buffer: WebGPUBuffer,
device: WebGPUDevice,
device: &GPUDevice,
state: GPUBufferState,
size: GPUSize64,
map_info: DomRefCell<Option<GPUBufferMapInfo>>,
@ -145,6 +144,8 @@ impl GPUBufferMethods for GPUBuffer {
let m_range = m_info.mapping_range.clone();
if let Err(e) = self.channel.0.send(WebGPURequest::UnmapBuffer {
buffer_id: self.id().0,
device_id: self.device.id().0,
scope_id: self.device.use_current_scope(),
array_buffer: IpcSharedMemory::from_bytes(m_info.mapping.borrow().as_slice()),
is_map_read: m_info.map_mode == Some(GPUMapModeConstants::READ),
offset: m_range.start,

View file

@ -29,7 +29,7 @@ use std::borrow::Cow;
use std::cell::Cell;
use std::collections::HashSet;
use webgpu::wgpu::command as wgpu_com;
use webgpu::{self, wgt, WebGPU, WebGPURequest};
use webgpu::{self, identity::WebGPUOpResult, wgt, WebGPU, WebGPURequest};
// https://gpuweb.github.io/gpuweb/#enumdef-encoder-state
#[derive(MallocSizeOf, PartialEq)]
@ -103,6 +103,10 @@ impl GPUCommandEncoder {
*self.state.borrow_mut() = GPUCommandEncoderState::Closed;
}
}
pub fn device(&self) -> &GPUDevice {
&*self.device
}
}
impl GPUCommandEncoderMethods for GPUCommandEncoder {
@ -254,9 +258,15 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
size: GPUSize64,
) {
let valid = *self.state.borrow() == GPUCommandEncoderState::Open;
let scope_id = self.device.use_current_scope();
if !valid {
// TODO: Record an error in the current scope.
self.device.handle_server_msg(
scope_id,
WebGPUOpResult::ValidationError(String::from(
"CommandEncoder is not in Open State",
)),
);
self.valid.set(false);
return;
}
@ -269,6 +279,8 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
.0
.send(WebGPURequest::CopyBufferToBuffer {
command_encoder_id: self.encoder.0,
device_id: self.device.id().0,
scope_id,
source_id: source.id().0,
source_offset,
destination_id: destination.id().0,
@ -286,9 +298,15 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
copy_size: GPUExtent3D,
) {
let valid = *self.state.borrow() == GPUCommandEncoderState::Open;
let scope_id = self.device.use_current_scope();
if !valid {
// TODO: Record an error in the current scope.
self.device.handle_server_msg(
scope_id,
WebGPUOpResult::ValidationError(String::from(
"CommandEncoder is not in Open State",
)),
);
self.valid.set(false);
return;
}
@ -301,6 +319,8 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
.0
.send(WebGPURequest::CopyBufferToTexture {
command_encoder_id: self.encoder.0,
device_id: self.device.id().0,
scope_id,
source: convert_buffer_cv(source),
destination: convert_texture_cv(destination),
copy_size: convert_texture_size_to_wgt(&convert_texture_size_to_dict(&copy_size)),
@ -316,9 +336,15 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
copy_size: GPUExtent3D,
) {
let valid = *self.state.borrow() == GPUCommandEncoderState::Open;
let scope_id = self.device.use_current_scope();
if !valid {
// TODO: Record an error in the current scope.
self.device.handle_server_msg(
scope_id,
WebGPUOpResult::ValidationError(String::from(
"CommandEncoder is not in Open State",
)),
);
self.valid.set(false);
return;
}
@ -331,6 +357,8 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
.0
.send(WebGPURequest::CopyTextureToBuffer {
command_encoder_id: self.encoder.0,
device_id: self.device.id().0,
scope_id,
source: convert_texture_cv(source),
destination: convert_buffer_cv(destination),
copy_size: convert_texture_size_to_wgt(&convert_texture_size_to_dict(&copy_size)),
@ -346,9 +374,15 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
copy_size: GPUExtent3D,
) {
let valid = *self.state.borrow() == GPUCommandEncoderState::Open;
let scope_id = self.device.use_current_scope();
if !valid {
// TODO: Record an error in the current scope.
self.device.handle_server_msg(
scope_id,
WebGPUOpResult::ValidationError(String::from(
"CommandEncoder is not in Open State",
)),
);
self.valid.set(false);
return;
}
@ -357,6 +391,8 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
.0
.send(WebGPURequest::CopyTextureToTexture {
command_encoder_id: self.encoder.0,
device_id: self.device.id().0,
scope_id,
source: convert_texture_cv(source),
destination: convert_texture_cv(destination),
copy_size: convert_texture_size_to_wgt(&convert_texture_size_to_dict(&copy_size)),

View file

@ -93,6 +93,8 @@ impl GPUComputePassEncoderMethods for GPUComputePassEncoder {
.0
.send(WebGPURequest::RunComputePass {
command_encoder_id: self.command_encoder.id().0,
device_id: self.command_encoder.device().id().0,
scope_id: self.command_encoder.device().use_current_scope(),
compute_pass,
})
.expect("Failed to send RunComputePass");

View file

@ -148,12 +148,14 @@ impl GPUDevice {
label: Option<String>,
) -> DomRoot<Self> {
let queue = GPUQueue::new(global, channel.clone(), queue);
reflect_dom_object(
let device = reflect_dom_object(
Box::new(GPUDevice::new_inherited(
channel, adapter, extensions, limits, device, &queue, label,
)),
global,
)
);
queue.set_device(&*device);
device
}
}
@ -341,7 +343,7 @@ impl GPUDeviceMethods for GPUDevice {
&self.global(),
self.channel.clone(),
buffer,
self.device,
&self,
state,
descriptor.size,
map_info,

View file

@ -11,24 +11,25 @@ use crate::dom::bindings::codegen::Bindings::GPUQueueBinding::GPUQueueMethods;
use crate::dom::bindings::codegen::Bindings::GPUTextureBinding::GPUExtent3D;
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::USVString;
use crate::dom::globalscope::GlobalScope;
use crate::dom::gpubuffer::{GPUBuffer, GPUBufferState};
use crate::dom::gpucommandbuffer::GPUCommandBuffer;
use crate::dom::gpucommandencoder::{convert_texture_cv, convert_texture_data_layout};
use crate::dom::gpudevice::{convert_texture_size_to_dict, convert_texture_size_to_wgt};
use crate::dom::gpudevice::{convert_texture_size_to_dict, convert_texture_size_to_wgt, GPUDevice};
use dom_struct::dom_struct;
use ipc_channel::ipc::IpcSharedMemory;
use js::rust::CustomAutoRooterGuard;
use js::typedarray::ArrayBuffer;
use webgpu::{wgt, WebGPU, WebGPUQueue, WebGPURequest};
use webgpu::{identity::WebGPUOpResult, wgt, WebGPU, WebGPUQueue, WebGPURequest};
#[dom_struct]
pub struct GPUQueue {
reflector_: Reflector,
#[ignore_malloc_size_of = "defined in webgpu"]
channel: WebGPU,
device: DomRefCell<Option<Dom<GPUDevice>>>,
label: DomRefCell<Option<USVString>>,
queue: WebGPUQueue,
}
@ -38,6 +39,7 @@ impl GPUQueue {
GPUQueue {
channel,
reflector_: Reflector::new(),
device: DomRefCell::new(None),
label: DomRefCell::new(None),
queue,
}
@ -48,6 +50,12 @@ impl GPUQueue {
}
}
impl GPUQueue {
pub fn set_device(&self, device: &GPUDevice) {
*self.device.borrow_mut() = Some(Dom::from_ref(device));
}
}
impl GPUQueueMethods for GPUQueue {
/// https://gpuweb.github.io/gpuweb/#dom-gpuobjectbase-label
fn GetLabel(&self) -> Option<USVString> {
@ -67,8 +75,14 @@ impl GPUQueueMethods for GPUQueue {
_ => false,
})
});
let scope_id = self.device.borrow().as_ref().unwrap().use_current_scope();
if !valid {
// TODO: Generate error to the ErrorScope
self.device.borrow().as_ref().unwrap().handle_server_msg(
scope_id,
WebGPUOpResult::ValidationError(String::from(
"Referenced GPUBuffer(s) are not Unmapped",
)),
);
return;
}
let command_buffers = command_buffers.iter().map(|cb| cb.id().0).collect();
@ -76,6 +90,7 @@ impl GPUQueueMethods for GPUQueue {
.0
.send(WebGPURequest::Submit {
queue_id: self.queue.0,
scope_id,
command_buffers,
})
.unwrap();
@ -111,6 +126,7 @@ impl GPUQueueMethods for GPUQueue {
);
if let Err(e) = self.channel.0.send(WebGPURequest::WriteBuffer {
queue_id: self.queue.0,
scope_id: self.device.borrow().as_ref().unwrap().use_current_scope(),
buffer_id: buffer.id().0,
buffer_offset,
data: final_data,
@ -144,6 +160,7 @@ impl GPUQueueMethods for GPUQueue {
if let Err(e) = self.channel.0.send(WebGPURequest::WriteTexture {
queue_id: self.queue.0,
scope_id: self.device.borrow().as_ref().unwrap().use_current_scope(),
texture_cv,
data_layout: texture_layout,
size: write_size,

View file

@ -165,6 +165,8 @@ impl GPURenderPassEncoderMethods for GPURenderPassEncoder {
.0
.send(WebGPURequest::RunRenderPass {
command_encoder_id: self.command_encoder.id().0,
device_id: self.command_encoder.device().id().0,
scope_id: self.command_encoder.device().use_current_scope(),
render_pass,
})
.expect("Failed to send RunRenderPass");

View file

@ -5,8 +5,7 @@
// https://gpuweb.github.io/gpuweb/#gpu-interface
[Exposed=(Window, DedicatedWorker), Pref="dom.webgpu.enabled"]
interface GPU {
// May reject with DOMException // TODO: DOMException("OperationError")?
Promise<GPUAdapter> requestAdapter(optional GPURequestAdapterOptions options = {});
Promise<GPUAdapter?> requestAdapter(optional GPURequestAdapterOptions options = {});
};
// https://gpuweb.github.io/gpuweb/#dictdef-gpurequestadapteroptions

View file

@ -9,8 +9,7 @@ interface GPUAdapter {
readonly attribute object extensions;
//readonly attribute GPULimits limits; Dont expose higher limits for now.
// May reject with DOMException // TODO: DOMException("OperationError")?
Promise<GPUDevice> requestDevice(optional GPUDeviceDescriptor descriptor = {});
Promise<GPUDevice?> requestDevice(optional GPUDeviceDescriptor descriptor = {});
};
dictionary GPUDeviceDescriptor : GPUObjectDescriptorBase {

View file

@ -80,6 +80,8 @@ pub enum WebGPURequest {
},
CopyBufferToBuffer {
command_encoder_id: id::CommandEncoderId,
device_id: id::DeviceId,
scope_id: Option<u64>,
source_id: id::BufferId,
source_offset: wgt::BufferAddress,
destination_id: id::BufferId,
@ -88,18 +90,24 @@ pub enum WebGPURequest {
},
CopyBufferToTexture {
command_encoder_id: id::CommandEncoderId,
device_id: id::DeviceId,
scope_id: Option<u64>,
source: BufferCopyView,
destination: TextureCopyView,
copy_size: wgt::Extent3d,
},
CopyTextureToBuffer {
command_encoder_id: id::CommandEncoderId,
device_id: id::DeviceId,
scope_id: Option<u64>,
source: TextureCopyView,
destination: BufferCopyView,
copy_size: wgt::Extent3d,
},
CopyTextureToTexture {
command_encoder_id: id::CommandEncoderId,
device_id: id::DeviceId,
scope_id: Option<u64>,
source: TextureCopyView,
destination: TextureCopyView,
copy_size: wgt::Extent3d,
@ -213,14 +221,19 @@ pub enum WebGPURequest {
},
RunComputePass {
command_encoder_id: id::CommandEncoderId,
device_id: id::DeviceId,
scope_id: Option<u64>,
compute_pass: ComputePass,
},
RunRenderPass {
command_encoder_id: id::CommandEncoderId,
device_id: id::DeviceId,
scope_id: Option<u64>,
render_pass: RenderPass,
},
Submit {
queue_id: id::QueueId,
scope_id: Option<u64>,
command_buffers: Vec<id::CommandBufferId>,
},
SwapChainPresent {
@ -230,6 +243,8 @@ pub enum WebGPURequest {
},
UnmapBuffer {
buffer_id: id::BufferId,
device_id: id::DeviceId,
scope_id: Option<u64>,
array_buffer: IpcSharedMemory,
is_map_read: bool,
offset: u64,
@ -242,12 +257,14 @@ pub enum WebGPURequest {
},
WriteBuffer {
queue_id: id::QueueId,
scope_id: Option<u64>,
buffer_id: id::BufferId,
buffer_offset: u64,
data: IpcSharedMemory,
},
WriteTexture {
queue_id: id::QueueId,
scope_id: Option<u64>,
texture_cv: TextureCopyView,
data_layout: wgt::TextureDataLayout,
size: wgt::Extent3d,
@ -455,6 +472,8 @@ impl<'a> WGPU<'a> {
},
WebGPURequest::CopyBufferToBuffer {
command_encoder_id,
device_id,
scope_id,
source_id,
source_offset,
destination_id,
@ -462,7 +481,7 @@ impl<'a> WGPU<'a> {
size,
} => {
let global = &self.global;
let _ = gfx_select!(command_encoder_id => global.command_encoder_copy_buffer_to_buffer(
let result = gfx_select!(command_encoder_id => global.command_encoder_copy_buffer_to_buffer(
command_encoder_id,
source_id,
source_offset,
@ -470,48 +489,58 @@ impl<'a> WGPU<'a> {
destination_offset,
size
));
self.send_result(device_id, scope_id, result);
},
WebGPURequest::CopyBufferToTexture {
command_encoder_id,
device_id,
scope_id,
source,
destination,
copy_size,
} => {
let global = &self.global;
let _ = gfx_select!(command_encoder_id => global.command_encoder_copy_buffer_to_texture(
let result = gfx_select!(command_encoder_id => global.command_encoder_copy_buffer_to_texture(
command_encoder_id,
&source,
&destination,
&copy_size
));
self.send_result(device_id, scope_id, result);
},
WebGPURequest::CopyTextureToBuffer {
command_encoder_id,
device_id,
scope_id,
source,
destination,
copy_size,
} => {
let global = &self.global;
let _ = gfx_select!(command_encoder_id => global.command_encoder_copy_texture_to_buffer(
let result = gfx_select!(command_encoder_id => global.command_encoder_copy_texture_to_buffer(
command_encoder_id,
&source,
&destination,
&copy_size
));
self.send_result(device_id, scope_id, result);
},
WebGPURequest::CopyTextureToTexture {
command_encoder_id,
device_id,
scope_id,
source,
destination,
copy_size,
} => {
let global = &self.global;
let _ = gfx_select!(command_encoder_id => global.command_encoder_copy_texture_to_texture(
let result = gfx_select!(command_encoder_id => global.command_encoder_copy_texture_to_texture(
command_encoder_id,
&source,
&destination,
&copy_size
));
self.send_result(device_id, scope_id, result);
},
WebGPURequest::CreateBindGroup {
device_id,
@ -890,30 +919,38 @@ impl<'a> WGPU<'a> {
},
WebGPURequest::RunComputePass {
command_encoder_id,
device_id,
scope_id,
compute_pass,
} => {
let global = &self.global;
let _ = gfx_select!(command_encoder_id => global.command_encoder_run_compute_pass(
let result = gfx_select!(command_encoder_id => global.command_encoder_run_compute_pass(
command_encoder_id,
&compute_pass
));
self.send_result(device_id, scope_id, result);
},
WebGPURequest::RunRenderPass {
command_encoder_id,
device_id,
scope_id,
render_pass,
} => {
let global = &self.global;
let _ = gfx_select!(command_encoder_id => global.command_encoder_run_render_pass(
let result = gfx_select!(command_encoder_id => global.command_encoder_run_render_pass(
command_encoder_id,
&render_pass
));
self.send_result(device_id, scope_id, result);
},
WebGPURequest::Submit {
queue_id,
scope_id,
command_buffers,
} => {
let global = &self.global;
let _ = gfx_select!(queue_id => global.queue_submit(queue_id, &command_buffers));
let result = gfx_select!(queue_id => global.queue_submit(queue_id, &command_buffers));
self.send_result(queue_id, scope_id, result);
},
WebGPURequest::SwapChainPresent {
external_id,
@ -1052,6 +1089,8 @@ impl<'a> WGPU<'a> {
},
WebGPURequest::UnmapBuffer {
buffer_id,
device_id,
scope_id,
array_buffer,
is_map_read,
offset,
@ -1068,7 +1107,8 @@ impl<'a> WGPU<'a> {
unsafe { slice::from_raw_parts_mut(map_ptr, size as usize) }
.copy_from_slice(&array_buffer);
}
let _ = gfx_select!(buffer_id => global.buffer_unmap(buffer_id));
let result = gfx_select!(buffer_id => global.buffer_unmap(buffer_id));
self.send_result(device_id, scope_id, result);
},
WebGPURequest::UpdateWebRenderData {
buffer_id,
@ -1105,21 +1145,24 @@ impl<'a> WGPU<'a> {
},
WebGPURequest::WriteBuffer {
queue_id,
scope_id,
buffer_id,
buffer_offset,
data,
} => {
let global = &self.global;
//TODO: Report result to content process
let _ = gfx_select!(queue_id => global.queue_write_buffer(
let result = gfx_select!(queue_id => global.queue_write_buffer(
queue_id,
buffer_id,
buffer_offset as wgt::BufferAddress,
&data
));
self.send_result(queue_id, scope_id, result);
},
WebGPURequest::WriteTexture {
queue_id,
scope_id,
texture_cv,
data_layout,
size,
@ -1127,20 +1170,21 @@ impl<'a> WGPU<'a> {
} => {
let global = &self.global;
//TODO: Report result to content process
let _ = gfx_select!(queue_id => global.queue_write_texture(
let result = gfx_select!(queue_id => global.queue_write_texture(
queue_id,
&texture_cv,
&data,
&data_layout,
&size
));
self.send_result(queue_id, scope_id, result);
},
}
}
}
}
fn send_result<U: id::TypedId, T: std::fmt::Debug>(
fn send_result<U, T: std::fmt::Debug>(
&self,
device_id: id::DeviceId,
scope_id: Option<u64>,