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

@ -22,6 +22,7 @@ use smallvec::SmallVec;
use std::borrow::Cow;
use std::collections::HashMap;
use std::ffi::CString;
use std::num::NonZeroU64;
use std::ptr;
use std::rc::Rc;
use std::slice;
@ -41,6 +42,7 @@ use wgpu::{
resource::{BufferMapAsyncStatus, BufferMapOperation},
};
pub type ErrorScopeId = NonZeroU64;
const DEVICE_POLL_INTERVAL: u64 = 100;
pub const PRESENTATION_BUFFER_COUNT: usize = 10;
@ -68,7 +70,6 @@ pub enum WebGPURequest {
sender: IpcSender<WebGPUResponseResult>,
buffer_id: id::BufferId,
device_id: id::DeviceId,
scope_id: Option<u64>,
host_map: HostMap,
map_range: std::ops::Range<u64>,
},
@ -76,14 +77,12 @@ pub enum WebGPURequest {
CommandEncoderFinish {
command_encoder_id: id::CommandEncoderId,
device_id: id::DeviceId,
scope_id: Option<u64>,
// TODO(zakorgy): Serialize CommandBufferDescriptor in wgpu-core
// wgpu::command::CommandBufferDescriptor,
},
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,
@ -93,7 +92,6 @@ 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,
@ -101,7 +99,6 @@ pub enum WebGPURequest {
CopyTextureToBuffer {
command_encoder_id: id::CommandEncoderId,
device_id: id::DeviceId,
scope_id: Option<u64>,
source: TextureCopyView,
destination: BufferCopyView,
copy_size: wgt::Extent3d,
@ -109,33 +106,27 @@ pub enum WebGPURequest {
CopyTextureToTexture {
command_encoder_id: id::CommandEncoderId,
device_id: id::DeviceId,
scope_id: Option<u64>,
source: TextureCopyView,
destination: TextureCopyView,
copy_size: wgt::Extent3d,
},
CreateBindGroup {
device_id: id::DeviceId,
// TODO: Consider using NonZeroU64 to reduce enum size
scope_id: Option<u64>,
bind_group_id: id::BindGroupId,
descriptor: BindGroupDescriptor<'static>,
},
CreateBindGroupLayout {
device_id: id::DeviceId,
scope_id: Option<u64>,
bind_group_layout_id: id::BindGroupLayoutId,
descriptor: wgt::BindGroupLayoutDescriptor<'static>,
},
CreateBuffer {
device_id: id::DeviceId,
scope_id: Option<u64>,
buffer_id: id::BufferId,
descriptor: wgt::BufferDescriptor<Option<String>>,
},
CreateCommandEncoder {
device_id: id::DeviceId,
scope_id: Option<u64>,
// TODO(zakorgy): Serialize CommandEncoderDescriptor in wgpu-core
// wgpu::command::CommandEncoderDescriptor,
command_encoder_id: id::CommandEncoderId,
@ -143,32 +134,27 @@ pub enum WebGPURequest {
},
CreateComputePipeline {
device_id: id::DeviceId,
scope_id: Option<u64>,
compute_pipeline_id: id::ComputePipelineId,
descriptor: ComputePipelineDescriptor<'static>,
},
CreateContext(IpcSender<webrender_api::ExternalImageId>),
CreatePipelineLayout {
device_id: id::DeviceId,
scope_id: Option<u64>,
pipeline_layout_id: id::PipelineLayoutId,
descriptor: wgt::PipelineLayoutDescriptor<'static, id::BindGroupLayoutId>,
},
CreateRenderPipeline {
device_id: id::DeviceId,
scope_id: Option<u64>,
render_pipeline_id: id::RenderPipelineId,
descriptor: RenderPipelineDescriptor<'static>,
},
CreateSampler {
device_id: id::DeviceId,
scope_id: Option<u64>,
sampler_id: id::SamplerId,
descriptor: wgt::SamplerDescriptor<Option<String>>,
},
CreateShaderModule {
device_id: id::DeviceId,
scope_id: Option<u64>,
program_id: id::ShaderModuleId,
program: Vec<u32>,
},
@ -182,7 +168,6 @@ pub enum WebGPURequest {
},
CreateTexture {
device_id: id::DeviceId,
scope_id: Option<u64>,
texture_id: id::TextureId,
descriptor: wgt::TextureDescriptor<Option<String>>,
},
@ -190,7 +175,6 @@ pub enum WebGPURequest {
texture_id: id::TextureId,
texture_view_id: id::TextureViewId,
device_id: id::DeviceId,
scope_id: Option<u64>,
descriptor: wgt::TextureViewDescriptor<Option<String>>,
},
DestroyBuffer(id::BufferId),
@ -206,7 +190,6 @@ pub enum WebGPURequest {
descriptor: wgt::RenderBundleDescriptor<Option<String>>,
render_bundle_id: id::RenderBundleId,
device_id: id::DeviceId,
scope_id: Option<u64>,
},
RequestAdapter {
sender: IpcSender<WebGPUResponseResult>,
@ -224,18 +207,15 @@ 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 {
@ -246,7 +226,6 @@ 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,
@ -259,14 +238,12 @@ 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,
@ -283,7 +260,7 @@ struct BufferMapInfo<'a, T> {
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct WebGPU(pub IpcSender<WebGPURequest>);
pub struct WebGPU(pub IpcSender<(Option<ErrorScopeId>, WebGPURequest)>);
impl WebGPU {
pub fn new(
@ -341,14 +318,14 @@ impl WebGPU {
pub fn exit(&self, sender: IpcSender<()>) -> Result<(), &'static str> {
self.0
.send(WebGPURequest::Exit(sender))
.send((None, WebGPURequest::Exit(sender)))
.map_err(|_| "Failed to send Exit message")
}
}
struct WGPU<'a> {
receiver: IpcReceiver<WebGPURequest>,
sender: IpcSender<WebGPURequest>,
receiver: IpcReceiver<(Option<ErrorScopeId>, WebGPURequest)>,
sender: IpcSender<(Option<ErrorScopeId>, WebGPURequest)>,
script_sender: IpcSender<WebGPUMsg>,
global: wgpu::hub::Global<IdentityRecyclerFactory>,
adapters: Vec<WebGPUAdapter>,
@ -358,7 +335,8 @@ struct WGPU<'a> {
// Buffers with pending mapping
buffer_maps: HashMap<id::BufferId, Rc<BufferMapInfo<'a, WebGPUResponseResult>>>,
// Presentation Buffers with pending mapping
present_buffer_maps: HashMap<id::BufferId, Rc<BufferMapInfo<'a, WebGPURequest>>>,
present_buffer_maps:
HashMap<id::BufferId, Rc<BufferMapInfo<'a, (Option<ErrorScopeId>, WebGPURequest)>>>,
webrender_api: webrender_api::RenderApi,
webrender_document: webrender_api::DocumentId,
external_images: Arc<Mutex<WebrenderExternalImageRegistry>>,
@ -368,8 +346,8 @@ struct WGPU<'a> {
impl<'a> WGPU<'a> {
fn new(
receiver: IpcReceiver<WebGPURequest>,
sender: IpcSender<WebGPURequest>,
receiver: IpcReceiver<(Option<ErrorScopeId>, WebGPURequest)>,
sender: IpcSender<(Option<ErrorScopeId>, WebGPURequest)>,
script_sender: IpcSender<WebGPUMsg>,
webrender_api_sender: webrender_api::RenderApiSender,
webrender_document: webrender_api::DocumentId,
@ -404,13 +382,12 @@ impl<'a> WGPU<'a> {
let _ = self.global.poll_all_devices(false);
self.last_poll = Instant::now();
}
if let Ok(msg) = self.receiver.try_recv() {
if let Ok((scope_id, msg)) = self.receiver.try_recv() {
match msg {
WebGPURequest::BufferMapAsync {
sender,
buffer_id,
device_id,
scope_id,
host_map,
map_range,
} => {
@ -473,7 +450,6 @@ impl<'a> WGPU<'a> {
WebGPURequest::CommandEncoderFinish {
command_encoder_id,
device_id,
scope_id,
} => {
let global = &self.global;
let result = gfx_select!(command_encoder_id => global.command_encoder_finish(
@ -485,7 +461,6 @@ impl<'a> WGPU<'a> {
WebGPURequest::CopyBufferToBuffer {
command_encoder_id,
device_id,
scope_id,
source_id,
source_offset,
destination_id,
@ -506,7 +481,6 @@ impl<'a> WGPU<'a> {
WebGPURequest::CopyBufferToTexture {
command_encoder_id,
device_id,
scope_id,
source,
destination,
copy_size,
@ -523,7 +497,6 @@ impl<'a> WGPU<'a> {
WebGPURequest::CopyTextureToBuffer {
command_encoder_id,
device_id,
scope_id,
source,
destination,
copy_size,
@ -540,7 +513,6 @@ impl<'a> WGPU<'a> {
WebGPURequest::CopyTextureToTexture {
command_encoder_id,
device_id,
scope_id,
source,
destination,
copy_size,
@ -556,7 +528,6 @@ impl<'a> WGPU<'a> {
},
WebGPURequest::CreateBindGroup {
device_id,
scope_id,
bind_group_id,
descriptor,
} => {
@ -567,7 +538,6 @@ impl<'a> WGPU<'a> {
},
WebGPURequest::CreateBindGroupLayout {
device_id,
scope_id,
bind_group_layout_id,
descriptor,
} => {
@ -578,7 +548,6 @@ impl<'a> WGPU<'a> {
},
WebGPURequest::CreateBuffer {
device_id,
scope_id,
buffer_id,
descriptor,
} => {
@ -597,7 +566,6 @@ impl<'a> WGPU<'a> {
},
WebGPURequest::CreateCommandEncoder {
device_id,
scope_id,
command_encoder_id,
label,
} => {
@ -617,7 +585,6 @@ impl<'a> WGPU<'a> {
},
WebGPURequest::CreateComputePipeline {
device_id,
scope_id,
compute_pipeline_id,
descriptor,
} => {
@ -638,7 +605,6 @@ impl<'a> WGPU<'a> {
},
WebGPURequest::CreatePipelineLayout {
device_id,
scope_id,
pipeline_layout_id,
descriptor,
} => {
@ -650,7 +616,6 @@ impl<'a> WGPU<'a> {
//TODO: consider https://github.com/gfx-rs/wgpu/issues/684
WebGPURequest::CreateRenderPipeline {
device_id,
scope_id,
render_pipeline_id,
descriptor,
} => {
@ -661,7 +626,6 @@ impl<'a> WGPU<'a> {
},
WebGPURequest::CreateSampler {
device_id,
scope_id,
sampler_id,
descriptor,
} => {
@ -683,7 +647,6 @@ impl<'a> WGPU<'a> {
},
WebGPURequest::CreateShaderModule {
device_id,
scope_id,
program_id,
program,
} => {
@ -738,7 +701,6 @@ impl<'a> WGPU<'a> {
},
WebGPURequest::CreateTexture {
device_id,
scope_id,
texture_id,
descriptor,
} => {
@ -762,7 +724,6 @@ impl<'a> WGPU<'a> {
texture_id,
texture_view_id,
device_id,
scope_id,
descriptor,
} => {
let global = &self.global;
@ -840,7 +801,6 @@ impl<'a> WGPU<'a> {
descriptor,
render_bundle_id,
device_id,
scope_id,
} => {
let global = &self.global;
let st;
@ -941,7 +901,6 @@ impl<'a> WGPU<'a> {
WebGPURequest::RunComputePass {
command_encoder_id,
device_id,
scope_id,
compute_pass,
} => {
let global = &self.global;
@ -954,7 +913,6 @@ impl<'a> WGPU<'a> {
WebGPURequest::RunRenderPass {
command_encoder_id,
device_id,
scope_id,
render_pass,
} => {
let global = &self.global;
@ -966,7 +924,6 @@ impl<'a> WGPU<'a> {
},
WebGPURequest::Submit {
queue_id,
scope_id,
command_buffers,
} => {
let global = &self.global;
@ -1111,7 +1068,6 @@ impl<'a> WGPU<'a> {
WebGPURequest::UnmapBuffer {
buffer_id,
device_id,
scope_id,
array_buffer,
is_map_read,
offset,
@ -1166,7 +1122,6 @@ impl<'a> WGPU<'a> {
},
WebGPURequest::WriteBuffer {
queue_id,
scope_id,
buffer_id,
buffer_offset,
data,
@ -1183,7 +1138,6 @@ impl<'a> WGPU<'a> {
},
WebGPURequest::WriteTexture {
queue_id,
scope_id,
texture_cv,
data_layout,
size,
@ -1208,7 +1162,7 @@ impl<'a> WGPU<'a> {
fn send_result<U, T: std::fmt::Debug>(
&self,
device_id: id::DeviceId,
scope_id: Option<u64>,
scope_id: Option<ErrorScopeId>,
result: Result<U, T>,
) {
let &pipeline_id = self.devices.get(&WebGPUDevice(device_id)).unwrap();