diff --git a/src/conv.rs b/src/conv.rs index b2a8c319..4efe76df 100644 --- a/src/conv.rs +++ b/src/conv.rs @@ -4,35 +4,14 @@ use crate::{follow_chain, map_enum}; use std::num::{NonZeroU32, NonZeroU64}; use std::{borrow::Cow, ffi::CStr}; -// Ultimately map_load_op and map_store_op should be able to use the map_enum! -// macro, but not until wgc supports "Undefined" as a valid value. For now the -// "_raw_" map enum version is wrapped into a function that falls back to an -// arbitrary default value. -// (This is the easyfix of https://github.com/gfx-rs/wgpu-native/issues/290) +map_enum!(map_load_op, WGPULoadOp, wgc::command::LoadOp, Clear, Load); map_enum!( - _raw_map_load_op, - WGPULoadOp, - wgc::command::LoadOp, - //"Unknown load op", - Clear, - Load -); -map_enum!( - _raw_map_store_op, + map_store_op, WGPUStoreOp, wgc::command::StoreOp, - //"Unknown store op", Discard, Store ); -#[inline] -pub fn map_load_op(value: native::WGPULoadOp) -> wgc::command::LoadOp { - return _raw_map_load_op(value).unwrap_or(wgc::command::LoadOp::Clear); -} -#[inline] -pub fn map_store_op(value: native::WGPUStoreOp) -> wgc::command::StoreOp { - return _raw_map_store_op(value).unwrap_or(wgc::command::StoreOp::Discard); -} map_enum!( map_address_mode, WGPUAddressMode, @@ -426,11 +405,12 @@ pub fn write_limits_struct( limits.maxComputeWorkgroupsPerDimension = wgt_limits.max_compute_workgroups_per_dimension; supported_limits.limits = limits; - match unsafe { supported_limits.nextInChain.as_ref() } { - Some(native::WGPUChainedStructOut { - sType: native::WGPUSType_SupportedLimitsExtras, - .. - }) => unsafe { + if let Some(native::WGPUChainedStructOut { + sType: native::WGPUSType_SupportedLimitsExtras, + .. + }) = unsafe { supported_limits.nextInChain.as_ref() } + { + unsafe { let extras = std::mem::transmute::< *mut native::WGPUChainedStructOut, *mut native::WGPUSupportedLimitsExtras, @@ -439,8 +419,7 @@ pub fn write_limits_struct( maxPushConstantSize: wgt_limits.max_push_constant_size, maxNonSamplerBindings: wgt_limits.max_non_sampler_bindings, }; - }, - _ => {} + } }; } @@ -1266,8 +1245,8 @@ pub fn map_bind_group_entry<'a>( } #[inline] -pub fn map_bind_group_layout_entry<'a>( - entry: &'a native::WGPUBindGroupLayoutEntry, +pub fn map_bind_group_layout_entry( + entry: &native::WGPUBindGroupLayoutEntry, extras: Option<&native::WGPUBindGroupLayoutEntryExtras>, ) -> wgt::BindGroupLayoutEntry { let is_buffer = entry.buffer.type_ != native::WGPUBufferBindingType_Undefined; @@ -1368,7 +1347,7 @@ pub fn map_bind_group_layout_entry<'a>( binding: entry.binding, visibility: wgt::ShaderStages::from_bits(entry.visibility) .expect("invalid visibility for bind group layout entry"), - count: extras.map(|v| NonZeroU32::new(v.count)).flatten(), + count: extras.and_then(|v| NonZeroU32::new(v.count)), } } diff --git a/src/lib.rs b/src/lib.rs index 5d9f3e80..747ed76b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -194,9 +194,14 @@ impl Drop for WGPUPipelineLayoutImpl { } } +struct QuerySetData { + query_type: native::WGPUQueryType, + query_count: u32, +} pub struct WGPUQuerySetImpl { context: Arc, id: id::QuerySetId, + data: QuerySetData, } impl Drop for WGPUQuerySetImpl { fn drop(&mut self) { @@ -1023,25 +1028,21 @@ pub unsafe extern "C" fn wgpuCommandEncoderBeginComputePass( ) }; - let timestamp_writes = descriptor - .map(|descriptor| { - descriptor.timestampWrites.as_ref().map(|timestamp_write| { - wgc::command::ComputePassTimestampWrites { - query_set: timestamp_write - .querySet - .as_ref() - .expect("invalid query set in timestamp writes") - .id, - beginning_of_pass_write_index: map_query_set_index( - timestamp_write.beginningOfPassWriteIndex, - ), - end_of_pass_write_index: map_query_set_index( - timestamp_write.endOfPassWriteIndex, - ), - } - }) + let timestamp_writes = descriptor.and_then(|descriptor| { + descriptor.timestampWrites.as_ref().map(|timestamp_write| { + wgc::command::ComputePassTimestampWrites { + query_set: timestamp_write + .querySet + .as_ref() + .expect("invalid query set in timestamp writes") + .id, + beginning_of_pass_write_index: map_query_set_index( + timestamp_write.beginningOfPassWriteIndex, + ), + end_of_pass_write_index: map_query_set_index(timestamp_write.endOfPassWriteIndex), + } }) - .flatten(); + }); let desc = match descriptor { Some(descriptor) => wgc::command::ComputePassDescriptor { @@ -1081,14 +1082,17 @@ pub unsafe extern "C" fn wgpuCommandEncoderBeginRenderPass( .expect("invalid texture view for depth stencil attachment") .id, depth: wgc::command::PassChannel { - load_op: conv::map_load_op(desc.depthLoadOp), - store_op: conv::map_store_op(desc.depthStoreOp), + load_op: conv::map_load_op(desc.depthLoadOp).unwrap_or(wgc::command::LoadOp::Load), + store_op: conv::map_store_op(desc.depthStoreOp) + .unwrap_or(wgc::command::StoreOp::Store), clear_value: desc.depthClearValue, read_only: desc.depthReadOnly != 0, }, stencil: wgc::command::PassChannel { - load_op: conv::map_load_op(desc.stencilLoadOp), - store_op: conv::map_store_op(desc.stencilStoreOp), + load_op: conv::map_load_op(desc.stencilLoadOp) + .unwrap_or(wgc::command::LoadOp::Load), + store_op: conv::map_store_op(desc.stencilStoreOp) + .unwrap_or(wgc::command::StoreOp::Store), clear_value: desc.stencilClearValue, read_only: desc.stencilReadOnly != 0, }, @@ -1120,8 +1124,10 @@ pub unsafe extern "C" fn wgpuCommandEncoderBeginRenderPass( view: view.id, resolve_target: color_attachment.resolveTarget.as_ref().map(|v| v.id), channel: wgc::command::PassChannel { - load_op: conv::map_load_op(color_attachment.loadOp), - store_op: conv::map_store_op(color_attachment.storeOp), + load_op: conv::map_load_op(color_attachment.loadOp) + .expect("invalid load op for render pass color attachment"), + store_op: conv::map_store_op(color_attachment.storeOp) + .expect("invalid store op for render pass color attachment"), clear_value: conv::map_color(&color_attachment.clearValue), read_only: false, }, @@ -2010,6 +2016,10 @@ pub unsafe extern "C" fn wgpuDeviceCreateQuerySet( Arc::into_raw(Arc::new(WGPUQuerySetImpl { context: context.clone(), id: query_set_id, + data: QuerySetData { + query_type: descriptor.type_, + query_count: descriptor.count, + }, })) } @@ -2753,6 +2763,20 @@ pub unsafe extern "C" fn wgpuQuerySetDestroy(_query_set: native::WGPUQuerySet) { //TODO: needs to be implemented in wgpu-core } +#[no_mangle] +pub unsafe extern "C" fn wgpuQuerySetGetCount(query_set: native::WGPUQuerySet) -> u32 { + let query_set = query_set.as_ref().expect("invalid query set"); + query_set.data.query_count +} + +#[no_mangle] +pub unsafe extern "C" fn wgpuQuerySetGetType( + query_set: native::WGPUQuerySet, +) -> native::WGPUQueryType { + let query_set = query_set.as_ref().expect("invalid query set"); + query_set.data.query_type +} + #[no_mangle] pub unsafe extern "C" fn wgpuQuerySetReference(query_set: native::WGPUQuerySet) { assert!(!query_set.is_null(), "invalid query set"); diff --git a/src/unimplemented.rs b/src/unimplemented.rs index b8795720..cfabd764 100644 --- a/src/unimplemented.rs +++ b/src/unimplemented.rs @@ -110,16 +110,6 @@ pub extern "C" fn wgpuPipelineLayoutSetLabel( unimplemented!(); } -#[no_mangle] -pub extern "C" fn wgpuQuerySetGetCount(_query_set: native::WGPUQuerySet) -> u32 { - unimplemented!(); -} - -#[no_mangle] -pub extern "C" fn wgpuQuerySetGetType(_query_set: native::WGPUQuerySet) -> native::WGPUQueryType { - unimplemented!(); -} - #[no_mangle] pub extern "C" fn wgpuQuerySetSetLabel( _query_set: native::WGPUQuerySet,