From b36e558a4ee365916cc5abca9ea80cd0cdcf2f6c Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Fri, 27 Sep 2024 11:29:50 +0200 Subject: [PATCH] Update headers again Replaces *EnumerateFeatures with *GetFeatures. Also fixes CI due to fix in headers. --- examples/texture_arrays/main.c | 13 ++++---- ffi/webgpu-headers | 2 +- src/lib.rs | 55 +++++++++++++++++++++++++--------- 3 files changed, 47 insertions(+), 23 deletions(-) diff --git a/examples/texture_arrays/main.c b/examples/texture_arrays/main.c index 46d40503..7d27f1b3 100644 --- a/examples/texture_arrays/main.c +++ b/examples/texture_arrays/main.c @@ -233,16 +233,13 @@ int main(int argc, char *argv[]) { WGPUSurfaceCapabilities surface_capabilities = {0}; wgpuSurfaceGetCapabilities(demo.surface, demo.adapter, &surface_capabilities); - size_t adapter_feature_count = - wgpuAdapterEnumerateFeatures(demo.adapter, NULL); - WGPUFeatureName *adapter_features = (WGPUFeatureName *)malloc( - sizeof(WGPUFeatureName) * adapter_feature_count); - wgpuAdapterEnumerateFeatures(demo.adapter, adapter_features); + WGPUSupportedFeatures adapter_features = {0}; + wgpuAdapterGetFeatures(demo.adapter, &adapter_features); bool adapter_has_required_features = false; bool adapter_has_optional_features = false; - for (size_t i = 0; i < adapter_feature_count; i++) { - switch ((uint32_t)adapter_features[i]) { + for (size_t i = 0; i < adapter_features.featureCount; i++) { + switch ((uint32_t)adapter_features.features[i]) { case WGPUNativeFeature_TextureBindingArray: adapter_has_required_features = true; break; @@ -253,7 +250,7 @@ int main(int argc, char *argv[]) { } assert( adapter_has_required_features /* Adapter must support WGPUNativeFeature_TextureBindingArray feature for this example */); - free(adapter_features); + wgpuSupportedFeaturesFreeMembers(adapter_features); WGPUFeatureName required_device_features[2] = { (WGPUFeatureName)WGPUNativeFeature_TextureBindingArray, diff --git a/ffi/webgpu-headers b/ffi/webgpu-headers index 2b597475..4f7f7ed9 160000 --- a/ffi/webgpu-headers +++ b/ffi/webgpu-headers @@ -1 +1 @@ -Subproject commit 2b5974750be7b2545d842ecec02e585022081952 +Subproject commit 4f7f7ed94a0040b65283035831bdffc024668ff8 diff --git a/src/lib.rs b/src/lib.rs index bde9a98e..3513ffbc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ use conv::{ map_query_set_index, map_shader_module, map_surface, map_surface_configuration, CreateSurfaceParams, }; +use core::slice; use parking_lot::Mutex; use smallvec::SmallVec; use std::{ @@ -660,26 +661,33 @@ pub unsafe extern "C" fn wgpuCreateInstance( // Adapter methods #[no_mangle] -pub unsafe extern "C" fn wgpuAdapterEnumerateFeatures( +pub unsafe extern "C" fn wgpuAdapterGetFeatures( adapter: native::WGPUAdapter, - features: *mut native::WGPUFeatureName, -) -> usize { + features: Option<&mut native::WGPUSupportedFeatures>, +) -> native::WGPUStatus { let (adapter_id, context) = { let adapter = adapter.as_ref().expect("invalid adapter"); (adapter.id, &adapter.context) }; + let features = features.expect("invalid return pointer \"features\""); + let adapter_features = match gfx_select!(adapter_id => context.adapter_features(adapter_id)) { Ok(features) => features, Err(err) => handle_error_fatal(err, "wgpuAdapterEnumerateFeatures"), }; let temp = conv::features_to_native(adapter_features); + let mut temp = temp.into_boxed_slice(); - if !features.is_null() { - std::ptr::copy_nonoverlapping(temp.as_ptr(), features, temp.len()); - } + *features = native::WGPUSupportedFeatures { + nextInChain: std::ptr::null_mut(), + featureCount: temp.len(), + features: temp.as_mut_ptr(), + }; - temp.len() + mem::forget(temp); + + native::WGPUStatus_Success } #[no_mangle] @@ -2500,26 +2508,45 @@ pub extern "C" fn wgpuDeviceDestroy(_device: native::WGPUDevice) { } #[no_mangle] -pub unsafe extern "C" fn wgpuDeviceEnumerateFeatures( +pub unsafe extern "C" fn wgpuDeviceGetFeatures( device: native::WGPUDevice, - features: *mut native::WGPUFeatureName, -) -> usize { + features: Option<&mut native::WGPUSupportedFeatures>, +) -> native::WGPUStatus { let (device_id, context) = { let device = device.as_ref().expect("invalid device"); (device.id, &device.context) }; + let features = features.expect("invalid return pointer \"features\""); + let device_features = match gfx_select!(device_id => context.device_features(device_id)) { Ok(features) => features, Err(err) => handle_error_fatal(err, "wgpuDeviceEnumerateFeatures"), }; let temp = conv::features_to_native(device_features); + let mut temp = temp.into_boxed_slice(); - if !features.is_null() { - std::ptr::copy_nonoverlapping(temp.as_ptr(), features, temp.len()); - } + *features = native::WGPUSupportedFeatures { + nextInChain: std::ptr::null_mut(), + featureCount: temp.len(), + features: temp.as_mut_ptr(), + }; + + mem::forget(temp); + + native::WGPUStatus_Success +} - temp.len() +#[no_mangle] +pub unsafe extern "C" fn wgpuSupportedFeaturesFreeMembers( + supported_features: native::WGPUSupportedFeatures, +) { + if !supported_features.features.is_null() && supported_features.featureCount > 0 { + drop(Box::from_raw(slice::from_raw_parts_mut( + supported_features.features as *mut native::WGPUFeatureName, + supported_features.featureCount, + ))) + } } #[no_mangle]