Change ErrorScopeId type to NonZeroU64

And extract it from WebGPURequest
This commit is contained in:
Kunal Mohan 2020-08-03 01:45:29 +05:30
parent 8eff1d74de
commit ce6e09a3aa
17 changed files with 277 additions and 250 deletions

View file

@ -138,6 +138,7 @@ use std::cell::{Cell, RefCell, UnsafeCell};
use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
use std::hash::{BuildHasher, Hash};
use std::mem;
use std::num::NonZeroU64;
use std::ops::{Deref, DerefMut, Range};
use std::path::PathBuf;
use std::rc::Rc;
@ -524,6 +525,7 @@ unsafe_no_jsmanaged_fields!(ActiveUniformBlockInfo);
unsafe_no_jsmanaged_fields!(bool, f32, f64, String, AtomicBool, AtomicUsize, Uuid, char);
unsafe_no_jsmanaged_fields!(usize, u8, u16, u32, u64);
unsafe_no_jsmanaged_fields!(isize, i8, i16, i32, i64);
unsafe_no_jsmanaged_fields!(NonZeroU64);
unsafe_no_jsmanaged_fields!(Error);
unsafe_no_jsmanaged_fields!(ServoUrl, ImmutableOrigin, MutableOrigin);
unsafe_no_jsmanaged_fields!(Image, ImageMetadata, dyn ImageCache, PendingImageId);

View file

@ -127,7 +127,7 @@ use std::sync::Arc;
use std::thread::JoinHandle;
use time::{get_time, Timespec};
use uuid::Uuid;
use webgpu::{identity::WebGPUOpResult, WebGPUDevice};
use webgpu::{identity::WebGPUOpResult, ErrorScopeId, WebGPUDevice};
#[derive(JSTraceable)]
pub struct AutoCloseWorker {
@ -3023,7 +3023,7 @@ impl GlobalScope {
pub fn handle_wgpu_msg(
&self,
device: WebGPUDevice,
scope: Option<u64>,
scope: Option<ErrorScopeId>,
result: WebGPUOpResult,
) {
self.gpu_devices

View file

@ -97,14 +97,17 @@ impl GPUAdapterMethods for GPUAdapter {
if self
.channel
.0
.send(WebGPURequest::RequestDevice {
sender,
adapter_id: self.adapter,
descriptor: desc,
device_id: id,
pipeline_id,
label: descriptor.parent.label.as_ref().map(|s| s.to_string()),
})
.send((
None,
WebGPURequest::RequestDevice {
sender,
adapter_id: self.adapter,
descriptor: desc,
device_id: id,
pipeline_id,
label: descriptor.parent.label.as_ref().map(|s| s.to_string()),
},
))
.is_err()
{
promise.reject_error(Error::Operation);

View file

@ -146,15 +146,19 @@ impl GPUBufferMethods for GPUBuffer {
let mut info = self.map_info.borrow_mut();
let m_info = info.as_mut().unwrap();
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,
size: m_range.end - m_range.start,
}) {
if let Err(e) = self.channel.0.send((
self.device.use_current_scope(),
WebGPURequest::UnmapBuffer {
buffer_id: self.id().0,
device_id: self.device.id().0,
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,
size: m_range.end - m_range.start,
},
)) {
warn!("Failed to send Buffer unmap ({:?}) ({})", self.buffer.0, e);
}
// Step 3.3
@ -185,7 +189,7 @@ impl GPUBufferMethods for GPUBuffer {
if let Err(e) = self
.channel
.0
.send(WebGPURequest::DestroyBuffer(self.buffer.0))
.send((None, WebGPURequest::DestroyBuffer(self.buffer.0)))
{
warn!(
"Failed to send WebGPURequest::DestroyBuffer({:?}) ({})",
@ -238,14 +242,16 @@ 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(WebGPURequest::BufferMapAsync {
sender,
buffer_id: self.buffer.0,
device_id: self.device.id().0,
if let Err(e) = self.channel.0.send((
scope_id,
host_map,
map_range: map_range.clone(),
}) {
WebGPURequest::BufferMapAsync {
sender,
buffer_id: self.buffer.0,
device_id: self.device.id().0,
host_map,
map_range: map_range.clone(),
},
)) {
warn!(
"Failed to send BufferMapAsync ({:?}) ({})",
self.buffer.0, e
@ -360,7 +366,7 @@ impl AsyncWGPUListener for GPUBuffer {
if let Err(e) = self
.channel
.0
.send(WebGPURequest::BufferMapComplete(self.buffer.0))
.send((None, WebGPURequest::BufferMapComplete(self.buffer.0)))
{
warn!(
"Failed to send BufferMapComplete({:?}) ({})",

View file

@ -45,7 +45,9 @@ pub struct GPUCanvasContext {
impl GPUCanvasContext {
fn new_inherited(canvas: &HTMLCanvasElement, size: Size2D<u32>, channel: WebGPU) -> Self {
let (sender, receiver) = ipc::channel().unwrap();
let _ = channel.0.send(WebGPURequest::CreateContext(sender));
if let Err(e) = channel.0.send((None, WebGPURequest::CreateContext(sender))) {
warn!("Failed to send CreateContext ({:?})", e);
}
let external_id = receiver.recv().unwrap();
Self {
reflector_: Reflector::new(),
@ -88,11 +90,14 @@ impl GPUCanvasContext {
.wgpu_id_hub()
.lock()
.create_command_encoder_id(texture_id.backend());
if let Err(e) = self.channel.0.send(WebGPURequest::SwapChainPresent {
external_id: self.context_id.0,
texture_id,
encoder_id,
}) {
if let Err(e) = self.channel.0.send((
None,
WebGPURequest::SwapChainPresent {
external_id: self.context_id.0,
texture_id,
encoder_id,
},
)) {
warn!(
"Failed to send UpdateWebrenderData({:?}) ({})",
self.context_id, e
@ -168,14 +173,17 @@ impl GPUCanvasContextMethods for GPUCanvasContext {
self.channel
.0
.send(WebGPURequest::CreateSwapChain {
device_id: descriptor.device.id().0,
buffer_ids,
external_id: self.context_id.0,
sender,
image_desc,
image_data,
})
.send((
None,
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");
let usage = if descriptor.usage % 2 == 0 {

View file

@ -277,16 +277,18 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
.insert(DomRoot::from_ref(destination));
self.channel
.0
.send(WebGPURequest::CopyBufferToBuffer {
command_encoder_id: self.encoder.0,
device_id: self.device.id().0,
.send((
scope_id,
source_id: source.id().0,
source_offset,
destination_id: destination.id().0,
destination_offset,
size,
})
WebGPURequest::CopyBufferToBuffer {
command_encoder_id: self.encoder.0,
device_id: self.device.id().0,
source_id: source.id().0,
source_offset,
destination_id: destination.id().0,
destination_offset,
size,
},
))
.expect("Failed to send CopyBufferToBuffer");
}
@ -317,14 +319,18 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
self.channel
.0
.send(WebGPURequest::CopyBufferToTexture {
command_encoder_id: self.encoder.0,
device_id: self.device.id().0,
.send((
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)),
})
WebGPURequest::CopyBufferToTexture {
command_encoder_id: self.encoder.0,
device_id: self.device.id().0,
source: convert_buffer_cv(source),
destination: convert_texture_cv(destination),
copy_size: convert_texture_size_to_wgt(&convert_texture_size_to_dict(
&copy_size,
)),
},
))
.expect("Failed to send CopyBufferToTexture");
}
@ -355,14 +361,18 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
self.channel
.0
.send(WebGPURequest::CopyTextureToBuffer {
command_encoder_id: self.encoder.0,
device_id: self.device.id().0,
.send((
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)),
})
WebGPURequest::CopyTextureToBuffer {
command_encoder_id: self.encoder.0,
device_id: self.device.id().0,
source: convert_texture_cv(source),
destination: convert_buffer_cv(destination),
copy_size: convert_texture_size_to_wgt(&convert_texture_size_to_dict(
&copy_size,
)),
},
))
.expect("Failed to send CopyTextureToBuffer");
}
@ -389,14 +399,18 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
self.channel
.0
.send(WebGPURequest::CopyTextureToTexture {
command_encoder_id: self.encoder.0,
device_id: self.device.id().0,
.send((
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)),
})
WebGPURequest::CopyTextureToTexture {
command_encoder_id: self.encoder.0,
device_id: self.device.id().0,
source: convert_texture_cv(source),
destination: convert_texture_cv(destination),
copy_size: convert_texture_size_to_wgt(&convert_texture_size_to_dict(
&copy_size,
)),
},
))
.expect("Failed to send CopyTextureToTexture");
}
@ -404,13 +418,15 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
fn Finish(&self, descriptor: &GPUCommandBufferDescriptor) -> DomRoot<GPUCommandBuffer> {
self.channel
.0
.send(WebGPURequest::CommandEncoderFinish {
command_encoder_id: self.encoder.0,
device_id: self.device.id().0,
scope_id: self.device.use_current_scope(),
// TODO(zakorgy): We should use `_descriptor` here after it's not empty
// and the underlying wgpu-core struct is serializable
})
.send((
self.device.use_current_scope(),
WebGPURequest::CommandEncoderFinish {
command_encoder_id: self.encoder.0,
device_id: self.device.id().0,
// 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

@ -91,12 +91,14 @@ impl GPUComputePassEncoderMethods for GPUComputePassEncoder {
if let Some(compute_pass) = self.compute_pass.borrow_mut().take() {
self.channel
.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,
})
.send((
self.command_encoder.device().use_current_scope(),
WebGPURequest::RunComputePass {
command_encoder_id: self.command_encoder.id().0,
device_id: self.command_encoder.device().id().0,
compute_pass,
},
))
.expect("Failed to send RunComputePass");
self.command_encoder.set_state(

View file

@ -71,9 +71,7 @@ use std::rc::Rc;
use webgpu::wgpu::{
binding_model as wgpu_bind, command::RenderBundleEncoder, pipeline as wgpu_pipe,
};
use webgpu::{self, identity::WebGPUOpResult, wgt, WebGPU, WebGPURequest};
type ErrorScopeId = u64;
use webgpu::{self, identity::WebGPUOpResult, wgt, ErrorScopeId, WebGPU, WebGPURequest};
#[derive(JSTraceable, MallocSizeOf)]
struct ErrorScopeInfo {
@ -131,7 +129,7 @@ impl GPUDevice {
scope_context: DomRefCell::new(ScopeContext {
error_scopes: HashMap::new(),
scope_stack: Vec::new(),
next_scope_id: 0,
next_scope_id: ErrorScopeId::new(1).unwrap(),
}),
lost_promise: DomRefCell::new(None),
}
@ -313,12 +311,14 @@ impl GPUDeviceMethods for GPUDevice {
let scope_id = self.use_current_scope();
self.channel
.0
.send(WebGPURequest::CreateBuffer {
device_id: self.device.0,
.send((
scope_id,
buffer_id: id,
descriptor: wgpu_descriptor,
})
WebGPURequest::CreateBuffer {
device_id: self.device.0,
buffer_id: id,
descriptor: wgpu_descriptor,
},
))
.expect("Failed to create WebGPU buffer");
let buffer = webgpu::WebGPUBuffer(id);
@ -451,12 +451,14 @@ impl GPUDeviceMethods for GPUDevice {
.create_bind_group_layout_id(self.device.0.backend());
self.channel
.0
.send(WebGPURequest::CreateBindGroupLayout {
device_id: self.device.0,
bind_group_layout_id,
.send((
scope_id,
descriptor: desc,
})
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);
@ -493,12 +495,14 @@ impl GPUDeviceMethods for GPUDevice {
.create_pipeline_layout_id(self.device.0.backend());
self.channel
.0
.send(WebGPURequest::CreatePipelineLayout {
device_id: self.device.0,
pipeline_layout_id,
descriptor: desc,
.send((
scope_id,
})
WebGPURequest::CreatePipelineLayout {
device_id: self.device.0,
pipeline_layout_id,
descriptor: desc,
},
))
.expect("Failed to create WebGPU PipelineLayout");
let pipeline_layout = webgpu::WebGPUPipelineLayout(pipeline_layout_id);
@ -553,12 +557,14 @@ impl GPUDeviceMethods for GPUDevice {
.create_bind_group_id(self.device.0.backend());
self.channel
.0
.send(WebGPURequest::CreateBindGroup {
device_id: self.device.0,
bind_group_id,
descriptor: desc,
.send((
scope_id,
})
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);
@ -592,12 +598,14 @@ impl GPUDeviceMethods for GPUDevice {
let scope_id = self.use_current_scope();
self.channel
.0
.send(WebGPURequest::CreateShaderModule {
device_id: self.device.0,
.send((
scope_id,
program_id,
program,
})
WebGPURequest::CreateShaderModule {
device_id: self.device.0,
program_id,
program,
},
))
.expect("Failed to create WebGPU ShaderModule");
let shader_module = webgpu::WebGPUShaderModule(program_id);
@ -631,12 +639,14 @@ impl GPUDeviceMethods for GPUDevice {
self.channel
.0
.send(WebGPURequest::CreateComputePipeline {
device_id: self.device.0,
.send((
scope_id,
compute_pipeline_id,
descriptor: desc,
})
WebGPURequest::CreateComputePipeline {
device_id: self.device.0,
compute_pipeline_id,
descriptor: desc,
},
))
.expect("Failed to create WebGPU ComputePipeline");
let compute_pipeline = webgpu::WebGPUComputePipeline(compute_pipeline_id);
@ -660,12 +670,14 @@ impl GPUDeviceMethods for GPUDevice {
let scope_id = self.use_current_scope();
self.channel
.0
.send(WebGPURequest::CreateCommandEncoder {
device_id: self.device.0,
.send((
scope_id,
command_encoder_id,
label: descriptor.parent.label.as_ref().map(|s| s.to_string()),
})
WebGPURequest::CreateCommandEncoder {
device_id: self.device.0,
command_encoder_id,
label: descriptor.parent.label.as_ref().map(|s| s.to_string()),
},
))
.expect("Failed to create WebGPU command encoder");
let encoder = webgpu::WebGPUCommandEncoder(command_encoder_id);
@ -710,12 +722,14 @@ impl GPUDeviceMethods for GPUDevice {
self.channel
.0
.send(WebGPURequest::CreateTexture {
device_id: self.device.0,
texture_id,
descriptor: desc,
.send((
scope_id,
})
WebGPURequest::CreateTexture {
device_id: self.device.0,
texture_id,
descriptor: desc,
},
))
.expect("Failed to create WebGPU Texture");
let texture = webgpu::WebGPUTexture(texture_id);
@ -761,12 +775,14 @@ impl GPUDeviceMethods for GPUDevice {
let scope_id = self.use_current_scope();
self.channel
.0
.send(WebGPURequest::CreateSampler {
device_id: self.device.0,
.send((
scope_id,
sampler_id,
descriptor: desc,
})
WebGPURequest::CreateSampler {
device_id: self.device.0,
sampler_id,
descriptor: desc,
},
))
.expect("Failed to create WebGPU sampler");
let sampler = webgpu::WebGPUSampler(sampler_id);
@ -903,12 +919,14 @@ impl GPUDeviceMethods for GPUDevice {
self.channel
.0
.send(WebGPURequest::CreateRenderPipeline {
device_id: self.device.0,
render_pipeline_id,
.send((
scope_id,
descriptor: desc,
})
WebGPURequest::CreateRenderPipeline {
device_id: self.device.0,
render_pipeline_id,
descriptor: desc,
},
))
.expect("Failed to create WebGPU render pipeline");
let render_pipeline = webgpu::WebGPURenderPipeline(render_pipeline_id);
@ -961,7 +979,7 @@ impl GPUDeviceMethods for GPUDevice {
fn PushErrorScope(&self, filter: GPUErrorFilter) {
let mut context = self.scope_context.borrow_mut();
let scope_id = context.next_scope_id;
context.next_scope_id += 1;
context.next_scope_id = ErrorScopeId::new(scope_id.get() + 1).unwrap();
let err_scope = ErrorScopeInfo {
op_count: 0,
error: None,

View file

@ -88,11 +88,13 @@ impl GPUQueueMethods for GPUQueue {
let command_buffers = command_buffers.iter().map(|cb| cb.id().0).collect();
self.channel
.0
.send(WebGPURequest::Submit {
queue_id: self.queue.0,
.send((
scope_id,
command_buffers,
})
WebGPURequest::Submit {
queue_id: self.queue.0,
command_buffers,
},
))
.unwrap();
}
@ -124,13 +126,15 @@ 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(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,
}) {
if let Err(e) = self.channel.0.send((
self.device.borrow().as_ref().unwrap().use_current_scope(),
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);
}
@ -158,14 +162,16 @@ 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(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,
data: final_data,
}) {
if let Err(e) = self.channel.0.send((
self.device.borrow().as_ref().unwrap().use_current_scope(),
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,

View file

@ -195,13 +195,15 @@ impl GPURenderBundleEncoderMethods for GPURenderBundleEncoder {
self.channel
.0
.send(WebGPURequest::RenderBundleEncoderFinish {
render_bundle_encoder: encoder,
descriptor: desc,
render_bundle_id,
device_id: self.device.id().0,
scope_id: self.device.use_current_scope(),
})
.send((
self.device.use_current_scope(),
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

@ -163,12 +163,14 @@ impl GPURenderPassEncoderMethods for GPURenderPassEncoder {
if let Some(render_pass) = self.render_pass.borrow_mut().take() {
self.channel
.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,
})
.send((
self.command_encoder.device().use_current_scope(),
WebGPURequest::RunRenderPass {
command_encoder_id: self.command_encoder.id().0,
device_id: self.command_encoder.device().id().0,
render_pass,
},
))
.expect("Failed to send RunRenderPass");
self.command_encoder.set_state(

View file

@ -57,10 +57,13 @@ impl GPUSwapChain {
impl GPUSwapChain {
pub fn destroy(&self, external_id: u64, image_key: webrender_api::ImageKey) {
if let Err(e) = self.channel.0.send(WebGPURequest::DestroySwapChain {
external_id,
image_key,
}) {
if let Err(e) = self.channel.0.send((
None,
WebGPURequest::DestroySwapChain {
external_id,
image_key,
},
)) {
warn!(
"Failed to send DestroySwapChain-ImageKey({:?}) ({})",
image_key, e

View file

@ -167,13 +167,15 @@ impl GPUTextureMethods for GPUTexture {
self.channel
.0
.send(WebGPURequest::CreateTextureView {
texture_id: self.texture.0,
texture_view_id,
device_id: self.device.id().0,
descriptor: desc,
.send((
scope_id,
})
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);
@ -191,7 +193,7 @@ impl GPUTextureMethods for GPUTexture {
if let Err(e) = self
.channel
.0
.send(WebGPURequest::DestroyTexture(self.texture.0))
.send((None, WebGPURequest::DestroyTexture(self.texture.0)))
{
warn!(
"Failed to send WebGPURequest::DestroyTexture({:?}) ({})",