1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-11 00:21:05 -05:00

refactor(op_crates/webgpu): simplify gfx_select macro & add gfx_put and gfx_ok macros (#10044)

This commit is contained in:
crowlKats 2021-05-03 16:42:59 +02:00 committed by GitHub
parent 3a37444cb5
commit 7bc03523d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 76 additions and 151 deletions

View file

@ -195,17 +195,11 @@ pub fn op_webgpu_create_bind_group_layout(
entries: Cow::from(entries),
};
let (bind_group_layout, maybe_err) = gfx_select!(device => instance.device_create_bind_group_layout(
gfx_put!(device => instance.device_create_bind_group_layout(
device,
&descriptor,
std::marker::PhantomData
));
let rid = state
.resource_table
.add(WebGpuBindGroupLayout(bind_group_layout));
Ok(WebGpuResult::rid_err(rid, maybe_err))
) => state, WebGpuBindGroupLayout)
}
#[derive(Deserialize)]
@ -244,17 +238,11 @@ pub fn op_webgpu_create_pipeline_layout(
push_constant_ranges: Default::default(),
};
let (pipeline_layout, maybe_err) = gfx_select!(device => instance.device_create_pipeline_layout(
gfx_put!(device => instance.device_create_pipeline_layout(
device,
&descriptor,
std::marker::PhantomData
));
let rid = state
.resource_table
.add(super::pipeline::WebGpuPipelineLayout(pipeline_layout));
Ok(WebGpuResult::rid_err(rid, maybe_err))
) => state, super::pipeline::WebGpuPipelineLayout)
}
#[derive(Deserialize)]
@ -340,13 +328,9 @@ pub fn op_webgpu_create_bind_group(
entries: Cow::from(entries),
};
let (bind_group, maybe_err) = gfx_select!(device => instance.device_create_bind_group(
gfx_put!(device => instance.device_create_bind_group(
device,
&descriptor,
std::marker::PhantomData
));
let rid = state.resource_table.add(WebGpuBindGroup(bind_group));
Ok(WebGpuResult::rid_err(rid, maybe_err))
) => state, WebGpuBindGroup)
}

View file

@ -60,15 +60,11 @@ pub fn op_webgpu_create_buffer(
mapped_at_creation: args.mapped_at_creation.unwrap_or(false),
};
let (buffer, maybe_err) = gfx_select!(device => instance.device_create_buffer(
gfx_put!(device => instance.device_create_buffer(
device,
&descriptor,
std::marker::PhantomData
));
let rid = state.resource_table.add(WebGpuBuffer(buffer));
Ok(WebGpuResult::rid_err(rid, maybe_err))
) => state, WebGpuBuffer)
}
#[derive(Deserialize)]
@ -233,7 +229,5 @@ pub fn op_webgpu_buffer_unmap(
slice.copy_from_slice(&buffer);
}
let maybe_err = gfx_select!(buffer => instance.buffer_unmap(buffer)).err();
Ok(WebGpuResult::maybe_err(maybe_err))
gfx_ok!(buffer => instance.buffer_unmap(buffer))
}

View file

@ -109,17 +109,13 @@ pub fn op_webgpu_render_bundle_encoder_finish(
.into_inner();
let instance = state.borrow::<super::Instance>();
let (render_bundle, maybe_err) = gfx_select!(render_bundle_encoder.parent() => instance.render_bundle_encoder_finish(
gfx_put!(render_bundle_encoder.parent() => instance.render_bundle_encoder_finish(
render_bundle_encoder,
&wgpu_core::command::RenderBundleDescriptor {
label: args.label.map(Cow::from),
},
std::marker::PhantomData
));
let rid = state.resource_table.add(WebGpuRenderBundle(render_bundle));
Ok(WebGpuResult::rid_err(rid, maybe_err))
) => state, WebGpuRenderBundle)
}
#[derive(Deserialize)]

View file

@ -61,17 +61,11 @@ pub fn op_webgpu_create_command_encoder(
label: args.label.map(Cow::from),
};
let (command_encoder, maybe_err) = gfx_select!(device => instance.device_create_command_encoder(
gfx_put!(device => instance.device_create_command_encoder(
device,
&descriptor,
std::marker::PhantomData
));
let rid = state
.resource_table
.add(WebGpuCommandEncoder(command_encoder));
Ok(WebGpuResult::rid_err(rid, maybe_err))
) => state, WebGpuCommandEncoder)
}
#[derive(Deserialize)]
@ -302,16 +296,14 @@ pub fn op_webgpu_command_encoder_copy_buffer_to_buffer(
.ok_or_else(bad_resource_id)?;
let destination_buffer = destination_buffer_resource.0;
let maybe_err = gfx_select!(command_encoder => instance.command_encoder_copy_buffer_to_buffer(
gfx_ok!(command_encoder => instance.command_encoder_copy_buffer_to_buffer(
command_encoder,
source_buffer,
args.source_offset,
destination_buffer,
args.destination_offset,
args.size
)).err();
Ok(WebGpuResult::maybe_err(maybe_err))
))
}
#[derive(Deserialize)]
@ -389,7 +381,7 @@ pub fn op_webgpu_command_encoder_copy_buffer_to_texture(
z: origin.z.unwrap_or(0),
}),
};
let maybe_err = gfx_select!(command_encoder => instance.command_encoder_copy_buffer_to_texture(
gfx_ok!(command_encoder => instance.command_encoder_copy_buffer_to_texture(
command_encoder,
&source,
&destination,
@ -398,9 +390,7 @@ pub fn op_webgpu_command_encoder_copy_buffer_to_texture(
height: args.copy_size.height.unwrap_or(1),
depth: args.copy_size.depth.unwrap_or(1),
}
)).err();
Ok(WebGpuResult::maybe_err(maybe_err))
))
}
#[derive(Deserialize)]
@ -451,7 +441,7 @@ pub fn op_webgpu_command_encoder_copy_texture_to_buffer(
rows_per_image: args.destination.rows_per_image.unwrap_or(0),
},
};
let maybe_err = gfx_select!(command_encoder => instance.command_encoder_copy_texture_to_buffer(
gfx_ok!(command_encoder => instance.command_encoder_copy_texture_to_buffer(
command_encoder,
&source,
&destination,
@ -460,9 +450,7 @@ pub fn op_webgpu_command_encoder_copy_texture_to_buffer(
height: args.copy_size.height.unwrap_or(1),
depth: args.copy_size.depth.unwrap_or(1),
}
)).err();
Ok(WebGpuResult::maybe_err(maybe_err))
))
}
#[derive(Deserialize)]
@ -517,7 +505,7 @@ pub fn op_webgpu_command_encoder_copy_texture_to_texture(
z: origin.z.unwrap_or(0),
}),
};
let maybe_err = gfx_select!(command_encoder => instance.command_encoder_copy_texture_to_texture(
gfx_ok!(command_encoder => instance.command_encoder_copy_texture_to_texture(
command_encoder,
&source,
&destination,
@ -526,9 +514,7 @@ pub fn op_webgpu_command_encoder_copy_texture_to_texture(
height: args.copy_size.height.unwrap_or(1),
depth: args.copy_size.depth.unwrap_or(1),
}
)).err();
Ok(WebGpuResult::maybe_err(maybe_err))
))
}
#[derive(Deserialize)]
@ -550,11 +536,8 @@ pub fn op_webgpu_command_encoder_push_debug_group(
.ok_or_else(bad_resource_id)?;
let command_encoder = command_encoder_resource.0;
let maybe_err = gfx_select!(command_encoder => instance
gfx_ok!(command_encoder => instance
.command_encoder_push_debug_group(command_encoder, &args.group_label))
.err();
Ok(WebGpuResult::maybe_err(maybe_err))
}
#[derive(Deserialize)]
@ -575,9 +558,7 @@ pub fn op_webgpu_command_encoder_pop_debug_group(
.ok_or_else(bad_resource_id)?;
let command_encoder = command_encoder_resource.0;
let maybe_err = gfx_select!(command_encoder => instance.command_encoder_pop_debug_group(command_encoder)).err();
Ok(WebGpuResult::maybe_err(maybe_err))
gfx_ok!(command_encoder => instance.command_encoder_pop_debug_group(command_encoder))
}
#[derive(Deserialize)]
@ -599,12 +580,10 @@ pub fn op_webgpu_command_encoder_insert_debug_marker(
.ok_or_else(bad_resource_id)?;
let command_encoder = command_encoder_resource.0;
let maybe_err = gfx_select!(command_encoder => instance.command_encoder_insert_debug_marker(
gfx_ok!(command_encoder => instance.command_encoder_insert_debug_marker(
command_encoder,
&args.marker_label
)).err();
Ok(WebGpuResult::maybe_err(maybe_err))
))
}
#[derive(Deserialize)]
@ -631,15 +610,11 @@ pub fn op_webgpu_command_encoder_write_timestamp(
.get::<super::WebGpuQuerySet>(args.query_set)
.ok_or_else(bad_resource_id)?;
let maybe_err =
gfx_select!(command_encoder => instance.command_encoder_write_timestamp(
command_encoder,
query_set_resource.0,
args.query_index
))
.err();
Ok(WebGpuResult::maybe_err(maybe_err))
gfx_ok!(command_encoder => instance.command_encoder_write_timestamp(
command_encoder,
query_set_resource.0,
args.query_index
))
}
#[derive(Deserialize)]
@ -673,18 +648,14 @@ pub fn op_webgpu_command_encoder_resolve_query_set(
.get::<super::buffer::WebGpuBuffer>(args.destination)
.ok_or_else(bad_resource_id)?;
let maybe_err =
gfx_select!(command_encoder => instance.command_encoder_resolve_query_set(
command_encoder,
query_set_resource.0,
args.first_query,
args.query_count,
destination_resource.0,
args.destination_offset
))
.err();
Ok(WebGpuResult::maybe_err(maybe_err))
gfx_ok!(command_encoder => instance.command_encoder_resolve_query_set(
command_encoder,
query_set_resource.0,
args.first_query,
args.query_count,
destination_resource.0,
args.destination_offset
))
}
#[derive(Deserialize)]
@ -710,14 +681,8 @@ pub fn op_webgpu_command_encoder_finish(
label: args.label.map(Cow::from),
};
let (command_buffer, maybe_err) = gfx_select!(command_encoder => instance.command_encoder_finish(
gfx_put!(command_encoder => instance.command_encoder_finish(
command_encoder,
&descriptor
));
let rid = state
.resource_table
.add(WebGpuCommandBuffer(command_buffer));
Ok(WebGpuResult::rid_err(rid, maybe_err))
) => state, WebGpuCommandBuffer)
}

View file

@ -227,14 +227,10 @@ pub fn op_webgpu_compute_pass_end_pass(
let compute_pass = &compute_pass_resource.0.borrow();
let instance = state.borrow::<super::Instance>();
let maybe_err =
gfx_select!(command_encoder => instance.command_encoder_run_compute_pass(
command_encoder,
compute_pass
))
.err();
Ok(WebGpuResult::maybe_err(maybe_err))
gfx_ok!(command_encoder => instance.command_encoder_run_compute_pass(
command_encoder,
compute_pass
))
}
#[derive(Deserialize)]

View file

@ -28,20 +28,35 @@ mod macros {
macro_rules! gfx_select {
($id:expr => $global:ident.$method:ident( $($param:expr),* )) => {
match $id.backend() {
#[cfg(all(not(target_arch = "wasm32"), not(any(target_os = "ios", target_os = "macos"))))]
#[cfg(not(target_os = "macos"))]
wgpu_types::Backend::Vulkan => $global.$method::<wgpu_core::backend::Vulkan>( $($param),* ),
#[cfg(all(not(target_arch = "wasm32"), any(target_os = "ios", target_os = "macos")))]
#[cfg(target_os = "macos")]
wgpu_types::Backend::Metal => $global.$method::<wgpu_core::backend::Metal>( $($param),* ),
#[cfg(all(not(target_arch = "wasm32"), windows))]
#[cfg(windows)]
wgpu_types::Backend::Dx12 => $global.$method::<wgpu_core::backend::Dx12>( $($param),* ),
#[cfg(all(not(target_arch = "wasm32"), windows))]
#[cfg(windows)]
wgpu_types::Backend::Dx11 => $global.$method::<wgpu_core::backend::Dx11>( $($param),* ),
#[cfg(any(target_arch = "wasm32", all(unix, not(any(target_os = "ios", target_os = "macos")))))]
#[cfg(all(unix, not(target_os = "macos")))]
wgpu_types::Backend::Gl => $global.$method::<wgpu_core::backend::Gl>( $($param),+ ),
other => panic!("Unexpected backend {:?}", other),
}
};
}
macro_rules! gfx_put {
($id:expr => $global:ident.$method:ident( $($param:expr),* ) => $state:expr, $rc:expr) => {{
let (val, maybe_err) = gfx_select!($id => $global.$method($($param),*));
let rid = $state.resource_table.add($rc(val));
Ok(WebGpuResult::rid_err(rid, maybe_err))
}};
}
macro_rules! gfx_ok {
($id:expr => $global:ident.$method:ident( $($param:expr),* )) => {{
let maybe_err = gfx_select!($id => $global.$method($($param),*)).err();
Ok(WebGpuResult::maybe_err(maybe_err))
}};
}
}
pub mod binding;
@ -534,15 +549,11 @@ pub fn op_webgpu_create_query_set(
count: args.count,
};
let (query_set, maybe_err) = gfx_select!(device => instance.device_create_query_set(
gfx_put!(device => instance.device_create_query_set(
device,
&descriptor,
std::marker::PhantomData
));
let rid = state.resource_table.add(WebGpuQuerySet(query_set));
Ok(WebGpuResult::rid_err(rid, maybe_err))
) => state, WebGpuQuerySet)
}
fn declare_webgpu_ops() -> Vec<(&'static str, Box<OpFn>)> {

View file

@ -142,7 +142,7 @@ pub fn op_webgpu_write_texture(
rows_per_image: args.data_layout.rows_per_image.unwrap_or(0),
};
let maybe_err = gfx_select!(queue => instance.queue_write_texture(
gfx_ok!(queue => instance.queue_write_texture(
queue,
&destination,
&*zero_copy,
@ -153,7 +153,4 @@ pub fn op_webgpu_write_texture(
depth: args.size.depth.unwrap_or(1),
}
))
.err();
Ok(WebGpuResult::maybe_err(maybe_err))
}

View file

@ -306,9 +306,7 @@ pub fn op_webgpu_render_pass_end_pass(
let render_pass = &render_pass_resource.0.borrow();
let instance = state.borrow::<super::Instance>();
let maybe_err = gfx_select!(command_encoder => instance.command_encoder_run_render_pass(command_encoder, render_pass)).err();
Ok(WebGpuResult::maybe_err(maybe_err))
gfx_ok!(command_encoder => instance.command_encoder_run_render_pass(command_encoder, render_pass))
}
#[derive(Deserialize)]

View file

@ -113,13 +113,9 @@ pub fn op_webgpu_create_sampler(
border_color: None, // native-only
};
let (sampler, maybe_err) = gfx_select!(device => instance.device_create_sampler(
gfx_put!(device => instance.device_create_sampler(
device,
&descriptor,
std::marker::PhantomData
));
let rid = state.resource_table.add(WebGpuSampler(sampler));
Ok(WebGpuResult::rid_err(rid, maybe_err))
) => state, WebGpuSampler)
}

View file

@ -66,14 +66,10 @@ pub fn op_webgpu_create_shader_module(
flags,
};
let (shader_module, maybe_err) = gfx_select!(device => instance.device_create_shader_module(
gfx_put!(device => instance.device_create_shader_module(
device,
&descriptor,
source,
std::marker::PhantomData
));
let rid = state.resource_table.add(WebGpuShaderModule(shader_module));
Ok(WebGpuResult::rid_err(rid, maybe_err))
) => state, WebGpuShaderModule)
}

View file

@ -176,15 +176,11 @@ pub fn op_webgpu_create_texture(
usage: wgpu_types::TextureUsage::from_bits(args.usage).unwrap(),
};
let (texture, maybe_err) = gfx_select!(device => instance.device_create_texture(
gfx_put!(device => instance.device_create_texture(
device,
&descriptor,
std::marker::PhantomData
));
let rid = state.resource_table.add(WebGpuTexture(texture));
Ok(WebGpuResult::rid_err(rid, maybe_err))
) => state, WebGpuTexture)
}
#[derive(Deserialize)]
@ -237,13 +233,9 @@ pub fn op_webgpu_create_texture_view(
),
};
let (texture_view, maybe_err) = gfx_select!(texture => instance.texture_create_view(
gfx_put!(texture => instance.texture_create_view(
texture,
&descriptor,
std::marker::PhantomData
));
let rid = state.resource_table.add(WebGpuTextureView(texture_view));
Ok(WebGpuResult::rid_err(rid, maybe_err))
) => state, WebGpuTextureView)
}