diff --git a/examples/capture/main.c b/examples/capture/main.c index c24f6beb..a725d7ff 100644 --- a/examples/capture/main.c +++ b/examples/capture/main.c @@ -28,33 +28,16 @@ int main(int argc, char *argv[]) { int width = 100; int height = 200; - WGPUInstance instance = wgpuCreateInstance(&(WGPUInstanceDescriptor) {.nextInChain = NULL}); + WGPUInstance instance = + wgpuCreateInstance(&(WGPUInstanceDescriptor){.nextInChain = NULL}); WGPUAdapter adapter; - wgpuInstanceRequestAdapter(instance, - &(WGPURequestAdapterOptions){ - .nextInChain = NULL, - .compatibleSurface = NULL, - }, - request_adapter_callback, (void *)&adapter); + wgpuInstanceRequestAdapter(instance, NULL, request_adapter_callback, + (void *)&adapter); WGPUDevice device; - wgpuAdapterRequestDevice(adapter, - &(WGPUDeviceDescriptor){ - .nextInChain = NULL, - .label = "Device", - .requiredLimits = - &(WGPURequiredLimits){ - .nextInChain = NULL, - .limits = WGPULimits_DEFAULT, - }, - .defaultQueue = - (WGPUQueueDescriptor){ - .nextInChain = NULL, - .label = NULL, - }, - }, - request_device_callback, (void *)&device); + wgpuAdapterRequestDevice(adapter, NULL, request_device_callback, + (void *)&device); wgpuDeviceSetUncapturedErrorCallback(device, handle_uncaptured_error, NULL); wgpuDeviceSetDeviceLostCallback(device, handle_device_lost, NULL); diff --git a/examples/compute/main.c b/examples/compute/main.c index 6baa9292..1f17c9ac 100644 --- a/examples/compute/main.c +++ b/examples/compute/main.c @@ -31,33 +31,16 @@ int main(int argc, char *argv[]) { initializeLog(); - WGPUInstance instance = wgpuCreateInstance(&(WGPUInstanceDescriptor) {.nextInChain = NULL}); + WGPUInstance instance = + wgpuCreateInstance(&(WGPUInstanceDescriptor){.nextInChain = NULL}); WGPUAdapter adapter; - wgpuInstanceRequestAdapter(instance, - &(WGPURequestAdapterOptions){ - .nextInChain = NULL, - .compatibleSurface = NULL, - }, - request_adapter_callback, (void *)&adapter); + wgpuInstanceRequestAdapter(instance, NULL, request_adapter_callback, + (void *)&adapter); WGPUDevice device; - wgpuAdapterRequestDevice(adapter, - &(WGPUDeviceDescriptor){ - .nextInChain = NULL, - .label = "Device", - .requiredLimits = - &(WGPURequiredLimits){ - .nextInChain = NULL, - .limits = WGPULimits_DEFAULT, - }, - .defaultQueue = - (WGPUQueueDescriptor){ - .nextInChain = NULL, - .label = NULL, - }, - }, - request_device_callback, (void *)&device); + wgpuAdapterRequestDevice(adapter, NULL, request_device_callback, + (void *)&device); wgpuDeviceSetUncapturedErrorCallback(device, handle_uncaptured_error, NULL); wgpuDeviceSetDeviceLostCallback(device, handle_device_lost, NULL); diff --git a/examples/framework.h b/examples/framework.h index 30bf4c1d..bf2a2d63 100644 --- a/examples/framework.h +++ b/examples/framework.h @@ -1,36 +1,5 @@ #include "wgpu.h" -#define WGPULimits_DEFAULT \ - (WGPULimits) { \ - .maxBindGroups = WGPU_LIMIT_U32_UNDEFINED, \ - .maxTextureDimension1D = WGPU_LIMIT_U32_UNDEFINED, \ - .maxTextureDimension2D = WGPU_LIMIT_U32_UNDEFINED, \ - .maxTextureDimension3D = WGPU_LIMIT_U32_UNDEFINED, \ - .maxTextureArrayLayers = WGPU_LIMIT_U32_UNDEFINED, \ - .maxDynamicUniformBuffersPerPipelineLayout = WGPU_LIMIT_U32_UNDEFINED, \ - .maxDynamicStorageBuffersPerPipelineLayout = WGPU_LIMIT_U32_UNDEFINED, \ - .maxSampledTexturesPerShaderStage = WGPU_LIMIT_U32_UNDEFINED, \ - .maxSamplersPerShaderStage = WGPU_LIMIT_U32_UNDEFINED, \ - .maxStorageBuffersPerShaderStage = WGPU_LIMIT_U32_UNDEFINED, \ - .maxStorageTexturesPerShaderStage = WGPU_LIMIT_U32_UNDEFINED, \ - .maxUniformBuffersPerShaderStage = WGPU_LIMIT_U32_UNDEFINED, \ - .maxUniformBufferBindingSize = WGPU_LIMIT_U64_UNDEFINED, \ - .maxStorageBufferBindingSize = WGPU_LIMIT_U64_UNDEFINED, \ - .minUniformBufferOffsetAlignment = WGPU_LIMIT_U32_UNDEFINED, \ - .minStorageBufferOffsetAlignment = WGPU_LIMIT_U32_UNDEFINED, \ - .maxVertexBuffers = WGPU_LIMIT_U32_UNDEFINED, \ - .maxBufferSize = WGPU_LIMIT_U64_UNDEFINED, \ - .maxVertexAttributes = WGPU_LIMIT_U32_UNDEFINED, \ - .maxVertexBufferArrayStride = WGPU_LIMIT_U32_UNDEFINED, \ - .maxInterStageShaderComponents = WGPU_LIMIT_U32_UNDEFINED, \ - .maxComputeWorkgroupStorageSize = WGPU_LIMIT_U32_UNDEFINED, \ - .maxComputeInvocationsPerWorkgroup = WGPU_LIMIT_U32_UNDEFINED, \ - .maxComputeWorkgroupSizeX = WGPU_LIMIT_U32_UNDEFINED, \ - .maxComputeWorkgroupSizeY = WGPU_LIMIT_U32_UNDEFINED, \ - .maxComputeWorkgroupSizeZ = WGPU_LIMIT_U32_UNDEFINED, \ - .maxComputeWorkgroupsPerDimension = WGPU_LIMIT_U32_UNDEFINED, \ - } - WGPUShaderModuleDescriptor load_wgsl(const char *name); void request_adapter_callback(WGPURequestAdapterStatus status, diff --git a/examples/triangle/main.c b/examples/triangle/main.c index b8a25b30..a0118fb4 100644 --- a/examples/triangle/main.c +++ b/examples/triangle/main.c @@ -75,7 +75,7 @@ int main(int argc, char *argv[]) { return 1; } - instance = wgpuCreateInstance(&(WGPUInstanceDescriptor) {.nextInChain = NULL}); + instance = wgpuCreateInstance(&(WGPUInstanceDescriptor){.nextInChain = NULL}); WGPUSurface surface; @@ -171,32 +171,14 @@ int main(int argc, char *argv[]) { #endif WGPUAdapter adapter; - wgpuInstanceRequestAdapter(instance, - &(WGPURequestAdapterOptions){ - .nextInChain = NULL, - .compatibleSurface = surface, - }, - request_adapter_callback, (void *)&adapter); + wgpuInstanceRequestAdapter(instance, NULL, request_adapter_callback, + (void *)&adapter); printAdapterFeatures(adapter); WGPUDevice device; - wgpuAdapterRequestDevice(adapter, - &(WGPUDeviceDescriptor){ - .nextInChain = NULL, - .label = "Device", - .requiredLimits = - &(WGPURequiredLimits){ - .nextInChain = NULL, - .limits = WGPULimits_DEFAULT, - }, - .defaultQueue = - (WGPUQueueDescriptor){ - .nextInChain = NULL, - .label = NULL, - }, - }, - request_device_callback, (void *)&device); + wgpuAdapterRequestDevice(adapter, NULL, request_device_callback, + (void *)&device); wgpuDeviceSetUncapturedErrorCallback(device, handle_uncaptured_error, NULL); wgpuDeviceSetDeviceLostCallback(device, handle_device_lost, NULL); diff --git a/src/device.rs b/src/device.rs index 4e12394d..0d10569a 100644 --- a/src/device.rs +++ b/src/device.rs @@ -18,42 +18,53 @@ use wgc::gfx_select; #[no_mangle] pub unsafe extern "C" fn wgpuInstanceRequestAdapter( instance: native::WGPUInstance, - options: &native::WGPURequestAdapterOptions, + options: Option<&native::WGPURequestAdapterOptions>, callback: native::WGPURequestAdapterCallback, userdata: *mut std::os::raw::c_void, ) { let instance = instance.as_ref().expect("invalid instance"); let context = &instance.context; - let (compatible_surface, given_backend) = follow_chain!( - map_adapter_options(options, - WGPUSType_AdapterExtras => native::WGPUAdapterExtras) - ); - let power_preference = match options.powerPreference { - native::WGPUPowerPreference_LowPower => wgt::PowerPreference::LowPower, - native::WGPUPowerPreference_HighPerformance => wgt::PowerPreference::HighPerformance, - _ => wgt::PowerPreference::default(), - }; - let backend_bits = match given_backend { - native::WGPUBackendType_Null => wgt::Backends::all(), - native::WGPUBackendType_Vulkan => wgt::Backends::VULKAN, - native::WGPUBackendType_Metal => wgt::Backends::METAL, - native::WGPUBackendType_D3D12 => wgt::Backends::DX12, - native::WGPUBackendType_D3D11 => wgt::Backends::DX11, - native::WGPUBackendType_OpenGL => wgt::Backends::GL, - _ => panic!("Invalid backend {}", given_backend), - }; + let (desc, inputs) = match options { + Some(options) => { + let (compatible_surface, given_backend) = follow_chain!( + map_adapter_options(options, + WGPUSType_AdapterExtras => native::WGPUAdapterExtras) + ); - let compatible_surface = compatible_surface.as_ref().map(|surface| surface.id); + ( + wgt::RequestAdapterOptions { + power_preference: match options.powerPreference { + native::WGPUPowerPreference_LowPower => wgt::PowerPreference::LowPower, + native::WGPUPowerPreference_HighPerformance => { + wgt::PowerPreference::HighPerformance + } + _ => wgt::PowerPreference::default(), + }, + force_fallback_adapter: options.forceFallbackAdapter, + compatible_surface: compatible_surface.as_ref().map(|surface| surface.id), + }, + wgc::instance::AdapterInputs::Mask( + match given_backend { + native::WGPUBackendType_Null => wgt::Backends::all(), + native::WGPUBackendType_Vulkan => wgt::Backends::VULKAN, + native::WGPUBackendType_Metal => wgt::Backends::METAL, + native::WGPUBackendType_D3D12 => wgt::Backends::DX12, + native::WGPUBackendType_D3D11 => wgt::Backends::DX11, + native::WGPUBackendType_OpenGL => wgt::Backends::GL, + _ => panic!("Invalid backend {}", given_backend), + }, + |_| (), + ), + ) + } + None => ( + wgt::RequestAdapterOptions::default(), + wgc::instance::AdapterInputs::Mask(wgt::Backends::all(), |_| ()), + ), + }; - match context.request_adapter( - &wgt::RequestAdapterOptions { - power_preference, - compatible_surface, - force_fallback_adapter: options.forceFallbackAdapter, - }, - wgc::instance::AdapterInputs::Mask(backend_bits, |_| ()), - ) { + match context.request_adapter(&desc, inputs) { Ok(adapter) => { (callback.unwrap())( native::WGPURequestAdapterStatus_Success, @@ -90,16 +101,20 @@ pub unsafe extern "C" fn wgpuInstanceRequestAdapter( #[no_mangle] pub unsafe extern "C" fn wgpuAdapterRequestDevice( adapter: native::WGPUAdapter, - descriptor: &native::WGPUDeviceDescriptor, + descriptor: Option<&native::WGPUDeviceDescriptor>, callback: native::WGPURequestDeviceCallback, userdata: *mut std::os::raw::c_void, ) { let (adapter, context) = adapter.unwrap_handle(); - let (desc, trace_str) = follow_chain!( - map_device_descriptor(descriptor, - WGPUSType_DeviceExtras => native::WGPUDeviceExtras) - ); + let (desc, trace_str) = match descriptor { + Some(descriptor) => follow_chain!( + map_device_descriptor(descriptor, + WGPUSType_DeviceExtras => native::WGPUDeviceExtras) + ), + None => (wgt::DeviceDescriptor::default(), None), + }; + let trace_path = trace_str.as_ref().map(Path::new); let (device, err) =