From 4f85e22d568f208e1a7d9f6e5e66d0ba2c0c9e67 Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Thu, 18 Jul 2024 18:23:20 +0200 Subject: [PATCH] Update headers (#392) --- examples/enumerate_adapters/main.c | 24 ++--- examples/framework/framework.c | 14 +++ examples/framework/framework.h | 1 + examples/triangle/main.c | 2 + ffi/webgpu-headers | 2 +- src/conv.rs | 51 ++++++++- src/lib.rs | 161 ++++++++++------------------- src/unimplemented.rs | 9 -- 8 files changed, 134 insertions(+), 130 deletions(-) diff --git a/examples/enumerate_adapters/main.c b/examples/enumerate_adapters/main.c index 5d57ebde..25b533d8 100644 --- a/examples/enumerate_adapters/main.c +++ b/examples/enumerate_adapters/main.c @@ -22,23 +22,23 @@ int main(int argc, char *argv[]) { WGPUAdapter adapter = adapters[i]; assert(adapter); - WGPUAdapterProperties props; - wgpuAdapterGetProperties(adapter, &props); + WGPUAdapterInfo info = {0}; + wgpuAdapterGetInfo(adapter, &info); printf("WGPUAdapter: %d\n", i); - printf("WGPUAdapterProperties {\n" - "\tvendorID: %" PRIu32 "\n" - "\tvendorName: %s\n" + printf("WGPUAdapterInfo {\n" + "\tvendor: %s\n" "\tarchitecture: %s\n" - "\tdeviceID: %" PRIu32 "\n" - "\tname: %s\n" - "\tdriverDescription: %s\n" - "\tadapterType: %#.8x\n" + "\tdevice: %s\n" + "\tdescription: %s\n" "\tbackendType: %#.8x\n" + "\tadapterType: %#.8x\n" + "\tvendorID: %" PRIu32 "\n" + "\tdeviceID: %" PRIu32 "\n" "}\n", - props.vendorID, props.vendorName, props.architecture, props.deviceID, - props.name, props.driverDescription, props.adapterType, - props.backendType); + info.vendor, info.architecture, info.device, info.description, + info.backendType, info.adapterType, info.vendorID, info.deviceID); + wgpuAdapterInfoFreeMembers(info); wgpuAdapterRelease(adapter); } diff --git a/examples/framework/framework.c b/examples/framework/framework.c index 9cee574e..f0729e51 100644 --- a/examples/framework/framework.c +++ b/examples/framework/framework.c @@ -164,3 +164,17 @@ void frmwrk_print_global_report(WGPUGlobalReport report) { } printf("}\n"); } + +void frmwrk_print_adapter_info(WGPUAdapter adapter) { + struct WGPUAdapterInfo info = {0}; + wgpuAdapterGetInfo(adapter, &info); + printf("description: %s\n", info.description); + printf("vendor: %s\n", info.vendor); + printf("architecture: %s\n", info.architecture); + printf("device: %s\n", info.device); + printf("backend type: %u\n", info.backendType); + printf("adapter type: %u\n", info.adapterType); + printf("vendorID: %x\n", info.vendorID); + printf("deviceID: %x\n", info.deviceID); + wgpuAdapterInfoFreeMembers(info); +} \ No newline at end of file diff --git a/examples/framework/framework.h b/examples/framework/framework.h index 8634b20b..c10084d7 100644 --- a/examples/framework/framework.h +++ b/examples/framework/framework.h @@ -22,5 +22,6 @@ WGPUShaderModule frmwrk_load_shader_module(WGPUDevice device, const char *name); void frmwrk_print_global_report(WGPUGlobalReport report); WGPUBuffer frmwrk_device_create_buffer_init( WGPUDevice device, const frmwrk_buffer_init_descriptor *descriptor); +void frmwrk_print_adapter_info(WGPUAdapter adapter); #endif // FRAMEWORK_H diff --git a/examples/triangle/main.c b/examples/triangle/main.c index 667bec6e..6bfb9b49 100644 --- a/examples/triangle/main.c +++ b/examples/triangle/main.c @@ -188,6 +188,8 @@ int main(int argc, char *argv[]) { handle_request_adapter, &demo); assert(demo.adapter); + frmwrk_print_adapter_info(demo.adapter); + wgpuAdapterRequestDevice(demo.adapter, NULL, handle_request_device, &demo); assert(demo.device); diff --git a/ffi/webgpu-headers b/ffi/webgpu-headers index aef5e428..043af6c7 160000 --- a/ffi/webgpu-headers +++ b/ffi/webgpu-headers @@ -1 +1 @@ -Subproject commit aef5e428a1fdab2ea770581ae7c95d8779984e0a +Subproject commit 043af6c77e566f707db36759d9c9f161ebb616fd diff --git a/src/conv.rs b/src/conv.rs index fccae55c..39d623e6 100644 --- a/src/conv.rs +++ b/src/conv.rs @@ -1,6 +1,6 @@ -use crate::native; use crate::utils::{make_slice, ptr_into_label, ptr_into_pathbuf}; use crate::{follow_chain, map_enum}; +use crate::{native, UncapturedErrorCallback}; use std::num::{NonZeroIsize, NonZeroU32, NonZeroU64}; use std::ptr::NonNull; use std::{borrow::Cow, ffi::CStr}; @@ -302,13 +302,14 @@ pub fn map_instance_descriptor( } #[inline] -pub fn map_device_descriptor<'a>( +pub(crate) fn map_device_descriptor<'a>( des: &native::WGPUDeviceDescriptor, base_limits: wgt::Limits, extras: Option<&native::WGPUDeviceExtras>, ) -> ( wgt::DeviceDescriptor>, *const std::ffi::c_char, + Option, ) { ( wgt::DeviceDescriptor { @@ -331,6 +332,13 @@ pub fn map_device_descriptor<'a>( Some(extras) => extras.tracePath, None => std::ptr::null(), }, + match des.uncapturedErrorCallbackInfo.callback { + None => None, + callback => Some(UncapturedErrorCallback { + callback, + userdata: des.uncapturedErrorCallbackInfo.userdata, + }), + }, ) } @@ -1523,6 +1531,24 @@ pub fn map_texture_usage_flags(flags: native::WGPUTextureUsage) -> wgt::TextureU temp } +#[inline] +pub fn to_native_texture_usage_flags(flags: wgt::TextureUsages) -> native::WGPUTextureUsage { + let mut flag = 0; + if flags.contains(wgt::TextureUsages::COPY_SRC) { + flag |= native::WGPUTextureUsage_CopySrc; + } + if flags.contains(wgt::TextureUsages::COPY_DST) { + flag |= native::WGPUTextureUsage_CopySrc; + } + if flags.contains(wgt::TextureUsages::TEXTURE_BINDING) { + flag |= native::WGPUTextureUsage_TextureBinding; + } + if flags.contains(wgt::TextureUsages::RENDER_ATTACHMENT) { + flag |= native::WGPUTextureUsage_RenderAttachment; + } + flag +} + pub enum CreateSurfaceParams { Raw( ( @@ -1633,3 +1659,24 @@ pub fn map_surface_configuration( }, } } + +pub fn map_backend_type(backend: wgt::Backend) -> native::WGPUBackendType { + match backend { + wgt::Backend::Empty => native::WGPUBackendType_Null, + wgt::Backend::Vulkan => native::WGPUBackendType_Vulkan, + wgt::Backend::Metal => native::WGPUBackendType_Metal, + wgt::Backend::Dx12 => native::WGPUBackendType_D3D12, + wgt::Backend::Gl => native::WGPUBackendType_OpenGL, + wgt::Backend::BrowserWebGpu => native::WGPUBackendType_WebGPU, + } +} + +pub fn map_adapter_type(device_type: wgt::DeviceType) -> native::WGPUAdapterType { + match device_type { + wgt::DeviceType::Other => native::WGPUAdapterType_Unknown, + wgt::DeviceType::IntegratedGpu => native::WGPUAdapterType_IntegratedGPU, + wgt::DeviceType::DiscreteGpu => native::WGPUAdapterType_DiscreteGPU, + wgt::DeviceType::VirtualGpu => native::WGPUAdapterType_CPU, // close enough? + wgt::DeviceType::Cpu => native::WGPUAdapterType_CPU, + } +} diff --git a/src/lib.rs b/src/lib.rs index 83b773a2..2a0befee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,9 @@ use conv::{ - map_bind_group_entry, map_bind_group_layout_entry, map_device_descriptor, - map_instance_backend_flags, map_instance_descriptor, map_pipeline_layout_descriptor, - map_primitive_state, map_query_set_descriptor, map_query_set_index, map_shader_module, - map_surface, map_surface_configuration, CreateSurfaceParams, + map_adapter_type, map_backend_type, map_bind_group_entry, map_bind_group_layout_entry, + map_device_descriptor, map_instance_backend_flags, map_instance_descriptor, + map_pipeline_layout_descriptor, map_primitive_state, map_query_set_descriptor, + map_query_set_index, map_shader_module, map_surface, map_surface_configuration, + CreateSurfaceParams, }; use parking_lot::Mutex; use smallvec::SmallVec; @@ -13,8 +14,8 @@ use std::{ fmt::Display, mem, num::NonZeroU64, + sync::atomic, sync::Arc, - sync::{atomic, OnceLock}, thread, }; use utils::{ @@ -42,21 +43,9 @@ const LABEL: &str = "label"; pub type Context = wgc::global::Global; -struct AdapterProperties { - vendor_id: u32, - vendor_name: CString, - architecture: CString, - device_id: u32, - name: CString, - driver_description: CString, - adapter_type: native::WGPUAdapterType, - backend_type: native::WGPUBackendType, -} - pub struct WGPUAdapterImpl { context: Arc, id: id::AdapterId, - properties: OnceLock, } impl Drop for WGPUAdapterImpl { fn drop(&mut self) { @@ -698,52 +687,33 @@ pub unsafe extern "C" fn wgpuAdapterGetLimits( } #[no_mangle] -pub unsafe extern "C" fn wgpuAdapterGetProperties( +pub unsafe extern "C" fn wgpuAdapterGetInfo( adapter: native::WGPUAdapter, - properties: Option<&mut native::WGPUAdapterProperties>, + info: Option<&mut native::WGPUAdapterInfo>, ) { let adapter = adapter.as_ref().expect("invalid adapter"); - let properties = properties.expect("invalid return pointer \"properties\""); + let info = info.expect("invalid return pointer \"info\""); let context = adapter.context.as_ref(); let adapter_id = adapter.id; - let props = adapter.properties.get_or_init(|| { - match gfx_select!(adapter_id => context.adapter_get_info(adapter_id)) { - Ok(info) => AdapterProperties { - vendor_id: info.vendor, - vendor_name: CString::new(info.driver).unwrap(), - architecture: CString::default(), // TODO - device_id: info.device, - name: CString::new(info.name).unwrap(), - driver_description: CString::new(info.driver_info).unwrap(), - adapter_type: match info.device_type { - wgt::DeviceType::Other => native::WGPUAdapterType_Unknown, - wgt::DeviceType::IntegratedGpu => native::WGPUAdapterType_IntegratedGPU, - wgt::DeviceType::DiscreteGpu => native::WGPUAdapterType_DiscreteGPU, - wgt::DeviceType::VirtualGpu => native::WGPUAdapterType_CPU, // close enough? - wgt::DeviceType::Cpu => native::WGPUAdapterType_CPU, - }, - backend_type: match info.backend { - wgt::Backend::Empty => native::WGPUBackendType_Null, - wgt::Backend::Vulkan => native::WGPUBackendType_Vulkan, - wgt::Backend::Metal => native::WGPUBackendType_Metal, - wgt::Backend::Dx12 => native::WGPUBackendType_D3D12, - wgt::Backend::Gl => native::WGPUBackendType_OpenGL, - wgt::Backend::BrowserWebGpu => native::WGPUBackendType_WebGPU, - }, - }, - Err(err) => handle_error_fatal(context, err, "wgpuAdapterGetProperties"), - } - }); + let result = gfx_select!(adapter_id => context.adapter_get_info(adapter_id)); + let result = match result { + Ok(info) => info, + Err(err) => handle_error_fatal(context, err, "wgpuAdapterGetInfo"), + }; - properties.vendorID = props.vendor_id; - properties.vendorName = props.vendor_name.as_ptr(); - properties.architecture = props.architecture.as_ptr(); - properties.deviceID = props.device_id; - properties.name = props.name.as_ptr(); - properties.driverDescription = props.driver_description.as_ptr(); - properties.adapterType = props.adapter_type; - properties.backendType = props.backend_type; + info.vendor = CString::new(format!("{:#x}", result.vendor)) + .unwrap() + .into_raw(); + info.architecture = CString::default().into_raw(); + info.device = CString::new(format!("{:#x}", result.device)) + .unwrap() + .into_raw(); + info.description = CString::new(result.name).unwrap().into_raw(); + info.backendType = map_backend_type(result.backend); + info.adapterType = map_adapter_type(result.device_type); + info.vendorID = result.vendor; + info.deviceID = result.device; } #[no_mangle] @@ -768,6 +738,22 @@ pub unsafe extern "C" fn wgpuAdapterHasFeature( adapter_features.contains(feature) as native::WGPUBool } +#[no_mangle] +pub unsafe extern "C" fn wgpuAdapterInfoFreeMembers(adapter_info: native::WGPUAdapterInfo) { + drop(CString::from_raw( + adapter_info.vendor as *mut std::ffi::c_char, + )); + drop(CString::from_raw( + adapter_info.architecture as *mut std::ffi::c_char, + )); + drop(CString::from_raw( + adapter_info.device as *mut std::ffi::c_char, + )); + drop(CString::from_raw( + adapter_info.description as *mut std::ffi::c_char, + )); +} + #[no_mangle] pub unsafe extern "C" fn wgpuAdapterRequestDevice( adapter: native::WGPUAdapter, @@ -796,9 +782,9 @@ pub unsafe extern "C" fn wgpuAdapterRequestDevice( }; let base_limits = get_base_device_limits_from_adapter_limits(&adapter_limits); - let (desc, trace_str, device_lost_handler) = match descriptor { + let (desc, trace_str, device_lost_handler, error_callback) = match descriptor { Some(descriptor) => { - let (desc, trace_str) = follow_chain!( + let (desc, trace_str, error_callback) = follow_chain!( map_device_descriptor((descriptor, base_limits), WGPUSType_DeviceExtras => native::WGPUDeviceExtras) ); @@ -806,7 +792,7 @@ pub unsafe extern "C" fn wgpuAdapterRequestDevice( callback: descriptor.deviceLostCallback, userdata: descriptor.deviceLostUserdata, }; - (desc, trace_str, device_lost_handler) + (desc, trace_str, device_lost_handler, error_callback) } None => ( wgt::DeviceDescriptor { @@ -815,6 +801,7 @@ pub unsafe extern "C" fn wgpuAdapterRequestDevice( }, std::ptr::null(), DEFAULT_DEVICE_LOST_HANDLER, + None, ), }; @@ -830,6 +817,11 @@ pub unsafe extern "C" fn wgpuAdapterRequestDevice( match err { None => { let message = CString::default(); + let mut error_sink = ErrorSinkRaw::new(device_lost_handler); + if let Some(error_callback) = error_callback { + error_sink.uncaptured_handler = error_callback; + } + callback( native::WGPURequestDeviceStatus_Success, Arc::into_raw(Arc::new(WGPUDeviceImpl { @@ -839,7 +831,7 @@ pub unsafe extern "C" fn wgpuAdapterRequestDevice( context: context.clone(), id: queue_id, }), - error_sink: Arc::new(Mutex::new(ErrorSinkRaw::new(device_lost_handler))), + error_sink: Arc::new(Mutex::new(error_sink)), })), message.as_ptr(), userdata, @@ -2622,17 +2614,6 @@ pub unsafe extern "C" fn wgpuDevicePushErrorScope( }); } -#[no_mangle] -pub unsafe extern "C" fn wgpuDeviceSetUncapturedErrorCallback( - device: native::WGPUDevice, - callback: native::WGPUErrorCallback, - userdata: *mut std::os::raw::c_void, -) { - let device = device.as_ref().expect("invalid device"); - let mut error_sink = device.error_sink.lock(); - error_sink.uncaptured_handler = UncapturedErrorCallback { callback, userdata }; -} - #[no_mangle] pub unsafe extern "C" fn wgpuDeviceReference(device: native::WGPUDevice) { assert!(!device.is_null(), "invalid device"); @@ -2726,7 +2707,6 @@ pub unsafe extern "C" fn wgpuInstanceRequestAdapter( Arc::into_raw(Arc::new(WGPUAdapterImpl { context: context.clone(), id: adapter_id, - properties: OnceLock::default(), })), message.as_ptr(), userdata, @@ -2782,7 +2762,6 @@ pub unsafe extern "C" fn wgpuInstanceEnumerateAdapters( temp[i] = Arc::into_raw(Arc::new(WGPUAdapterImpl { context: context.clone(), id: *id, - properties: OnceLock::default(), })); }); } else { @@ -3715,6 +3694,9 @@ pub unsafe extern "C" fn wgpuSurfaceGetCapabilities( Err(cause) => handle_error_fatal(context, cause, "wgpuSurfaceGetCapabilities"), }; + capabilities.usages = + conv::to_native_texture_usage_flags(caps.usages) as native::WGPUTextureUsageFlags; + let formats = caps .formats .iter() @@ -3821,39 +3803,6 @@ pub unsafe extern "C" fn wgpuSurfaceGetCurrentTexture( }; } -#[no_mangle] -pub unsafe extern "C" fn wgpuSurfaceGetPreferredFormat( - surface: native::WGPUSurface, - adapter: native::WGPUAdapter, -) -> native::WGPUTextureFormat { - let (adapter_id, context) = { - let adapter = adapter.as_ref().expect("invalid adapter"); - (adapter.id, &adapter.context) - }; - let surface_id = surface.as_ref().expect("invalid surface").id; - - let caps = match wgc::gfx_select!(adapter_id => context.surface_get_capabilities(surface_id, adapter_id)) - { - Ok(caps) => caps, - Err(wgc::instance::GetSurfaceSupportError::Unsupported) => { - wgt::SurfaceCapabilities::default() - } - Err(err) => handle_error_fatal(context, err, "wgpuSurfaceGetPreferredFormat"), - }; - - match caps - .formats - .first() - .and_then(|f| conv::to_native_texture_format(*f)) - { - Some(format) => format, - None => panic!( - "Error in wgpuSurfaceGetPreferredFormat: unsupported format.\n\ - Please report it to https://github.com/gfx-rs/wgpu-native" - ), - } -} - #[no_mangle] pub unsafe extern "C" fn wgpuSurfacePresent(surface: native::WGPUSurface) { let surface = surface.as_ref().expect("invalid surface"); diff --git a/src/unimplemented.rs b/src/unimplemented.rs index 3d5e84b6..f58045f8 100644 --- a/src/unimplemented.rs +++ b/src/unimplemented.rs @@ -1,14 +1,5 @@ use crate::native; -#[no_mangle] -pub extern "C" fn wgpuAdapterRequestAdapterInfo( - _adapter: native::WGPUAdapter, - _callback: native::WGPUAdapterRequestAdapterInfoCallback, - _userdata: *mut ::std::os::raw::c_void, -) { - unimplemented!(); -} - #[no_mangle] pub extern "C" fn wgpuGetProcAddress( _device: native::WGPUDevice,