Skip to content

Commit

Permalink
nullable args in requestAdapter & requestDevice (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
rajveermalviya authored Jan 30, 2023
1 parent b6a188c commit 169b516
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 133 deletions.
29 changes: 6 additions & 23 deletions examples/capture/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
29 changes: 6 additions & 23 deletions examples/compute/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
31 changes: 0 additions & 31 deletions examples/framework.h
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
28 changes: 5 additions & 23 deletions examples/triangle/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ int main(int argc, char *argv[]) {
return 1;
}

instance = wgpuCreateInstance(&(WGPUInstanceDescriptor) {.nextInChain = NULL});
instance = wgpuCreateInstance(&(WGPUInstanceDescriptor){.nextInChain = NULL});

WGPUSurface surface;

Expand Down Expand Up @@ -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);
Expand Down
81 changes: 48 additions & 33 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) =
Expand Down

0 comments on commit 169b516

Please sign in to comment.