Skip to content

Commit

Permalink
Update headers again
Browse files Browse the repository at this point in the history
Replaces *EnumerateFeatures with *GetFeatures.

Also fixes CI due to fix in headers.
  • Loading branch information
PJB3005 committed Sep 27, 2024
1 parent 8f34e48 commit b36e558
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 23 deletions.
13 changes: 5 additions & 8 deletions examples/texture_arrays/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion ffi/webgpu-headers
55 changes: 41 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit b36e558

Please sign in to comment.