From 4635f1eb07d5edb7849ff9a871462b622b14a2b0 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 29 Oct 2019 13:31:40 -0400 Subject: [PATCH 1/3] Extended remote example --- examples/remote/main.c | 34 ++++++++++++++++++++++++++++++---- ffi/wgpu-remote.h | 14 ++++++++++---- wgpu-remote/src/server.rs | 11 +++++++++-- 3 files changed, 49 insertions(+), 10 deletions(-) diff --git a/examples/remote/main.c b/examples/remote/main.c index fc0f93ab1..15103b0ce 100644 --- a/examples/remote/main.c +++ b/examples/remote/main.c @@ -6,23 +6,49 @@ int main() { WGPUInfrastructure infra = wgpu_client_new(); + WGPUClient *client = infra.client; - if (!infra.client || infra.error) { - printf("Cannot initialize WGPU client: %s", infra.error); + if (!client || infra.error) { + printf("Cannot initialize WGPU client: %s\n", infra.error); return 1; } WGPUGlobal* server = wgpu_server_new(); if (!server) { - printf("Cannot initialize WGPU client: %s", server); + printf("Cannot initialize WGPU client: %s\n", server); return 1; } + WGPUAdapterId adapterId = 0; + { + WGPUAdapterId ids[10]; + int count = wgpu_client_make_adapter_ids(client, ids, 10); + + WGPURequestAdapterOptions options = { + .power_preference = WGPUPowerPreference_LowPower, + .backends = 2 | 4 | 8, + }; + char index = wgpu_server_instance_request_adapter(server, &options, ids, count); + if (index < 0) { + printf("No available GPU adapters!\n"); + return 2; + } + + wgpu_client_kill_adapter_ids(client, ids, index); + wgpu_client_kill_adapter_ids(client, ids+index+1, count-index-1); + adapterId = ids[index]; + } + //TODO: do something meaningful + if (adapterId) { + //wgpu_server_destroy_adapter() + wgpu_client_kill_adapter_ids(client, &adapterId, 1); + } wgpu_server_delete(server); - wgpu_client_delete(infra.client); + wgpu_client_delete(client); + printf("Done\n"); return 0; } diff --git a/ffi/wgpu-remote.h b/ffi/wgpu-remote.h index d920eef42..59cae1103 100644 --- a/ffi/wgpu-remote.h +++ b/ffi/wgpu-remote.h @@ -108,11 +108,17 @@ void wgpu_server_device_destroy(const WGPUGlobal *aGlobal, WGPUDeviceId aSelfId) WGPU_FUNC; +/** + * Request an adapter according to the specified options. + * Provide the list of IDs to pick from. + * + * Returns the index in this list, or -1 if unable to pick. + */ WGPU_INLINE -WGPUAdapterId wgpu_server_instance_request_adapter(const WGPUGlobal *aGlobal, - const WGPURequestAdapterOptions *aDesc, - const WGPUAdapterId *aIds, - uintptr_t aIdLength) +int8_t wgpu_server_instance_request_adapter(const WGPUGlobal *aGlobal, + const WGPURequestAdapterOptions *aDesc, + const WGPUAdapterId *aIds, + uintptr_t aIdLength) WGPU_FUNC; WGPU_INLINE diff --git a/wgpu-remote/src/server.rs b/wgpu-remote/src/server.rs index 719591ee4..13dfcbf8f 100644 --- a/wgpu-remote/src/server.rs +++ b/wgpu-remote/src/server.rs @@ -17,15 +17,22 @@ pub extern "C" fn wgpu_server_delete(global: *mut wgn::Global) { log::info!("\t...done"); } +/// Request an adapter according to the specified options. +/// Provide the list of IDs to pick from. +/// +/// Returns the index in this list, or -1 if unable to pick. #[no_mangle] pub extern "C" fn wgpu_server_instance_request_adapter( global: &wgn::Global, desc: &wgn::RequestAdapterOptions, ids: *const wgn::AdapterId, id_length: usize, -) -> wgn::AdapterId { +) -> i8 { let ids = unsafe { slice::from_raw_parts(ids, id_length) }; - wgn::request_adapter(global, desc, ids).unwrap() + match wgn::request_adapter(global, desc, ids) { + Some(id) => ids.iter().position(|&i| i == id).unwrap() as i8, + None => -1, + } } #[no_mangle] From ff1adb34d9226cfba58788366608a8dc7541f5b0 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 29 Oct 2019 15:30:30 -0400 Subject: [PATCH 2/3] Fix destroy_instance warning --- wgpu-native/src/instance.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index 23ae6863a..e721a28d9 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -73,6 +73,7 @@ impl Instance { } #[cfg(any(target_os = "ios", target_os = "macos"))] { + let _ = surface; //self.metal.destroy_surface(surface.metal); } #[cfg(windows)] From 384de9ca52be63a4e40f88e9b7c362a44d0f269c Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 29 Oct 2019 15:45:48 -0400 Subject: [PATCH 3/3] Properly dispose of DescriptorAllocator --- wgpu-native/src/device.rs | 5 +++-- wgpu-native/src/hub.rs | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index cdbe40086..0f0bf913f 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -775,10 +775,11 @@ impl Device { } } - pub(crate) fn destroy_self(self) { + pub(crate) fn dispose(self) { self.com_allocator.destroy(&self.raw); + let desc_alloc = self.desc_allocator.into_inner(); unsafe { - self.desc_allocator.lock().cleanup(&self.raw); + desc_alloc.dispose(&self.raw); } } } diff --git a/wgpu-native/src/hub.rs b/wgpu-native/src/hub.rs index 0e21b512d..f22a682a6 100644 --- a/wgpu-native/src/hub.rs +++ b/wgpu-native/src/hub.rs @@ -445,7 +445,7 @@ impl Drop for Hub { // self.adapters for (_, (device, _)) in devices.map.drain() { - device.destroy_self(); + device.dispose(); } } }