diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index 835b0262f1..6831bed36c 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -4,10 +4,9 @@ use crate::{ AdapterHandle, AdapterId, DeviceHandle, + InstanceId, SurfaceHandle, }; -#[cfg(not(feature = "gfx-backend-gl"))] -use crate::{InstanceId}; #[cfg(feature = "local")] use crate::{DeviceId, SurfaceId}; @@ -76,6 +75,13 @@ pub extern "C" fn wgpu_create_instance() -> InstanceId { HUB.instances.register_local(inst) } +#[cfg(all(feature = "local", feature = "gfx-backend-gl"))] +pub fn wgpu_create_gl_instance(windowed_context: back::glutin::WindowedContext) -> InstanceId { + let raw = back::Surface::from_window(windowed_context); + let surface = SurfaceHandle::new(raw); + HUB.surfaces.register_local(surface) +} + #[cfg(all(feature = "window-winit", not(feature = "gfx-backend-gl")))] #[no_mangle] pub extern "C" fn wgpu_instance_create_surface_from_winit( @@ -175,10 +181,25 @@ pub extern "C" fn wgpu_instance_create_surface_from_windows_hwnd( HUB.surfaces.register_local(surface) } -fn get_adapter( - adapters: Vec>, - desc: &AdapterDescriptor -) -> AdapterHandle { +#[cfg(all(feature = "local", feature = "gfx-backend-gl"))] +pub fn wgpu_instance_get_gl_surface(instance_id: InstanceId) -> SurfaceId { + instance_id +} + +pub fn instance_get_adapter(instance_id: InstanceId, desc: &AdapterDescriptor) -> AdapterHandle { + #[cfg(not(feature = "gfx-backend-gl"))] + let adapters = { + let instance_guard = HUB.instances.read(); + let instance = &instance_guard[instance_id]; + instance.enumerate_adapters() + }; + #[cfg(feature = "gfx-backend-gl")] + let adapters = { + let surface_guard = HUB.surfaces.read(); + let surface = &surface_guard[instance_id]; + surface.raw.enumerate_adapters() + }; + let (mut low, mut high, mut other) = (None, None, None); for adapter in adapters { match adapter.info.device_type { @@ -197,14 +218,7 @@ fn get_adapter( .expect("No adapters found. Please enable the feature for one of the graphics backends: vulkan, metal, dx12, dx11, gl") } -#[cfg(not(feature = "gfx-backend-gl"))] -pub fn instance_get_adapter(instance_id: InstanceId, desc: &AdapterDescriptor) -> AdapterHandle { - let instance_guard = HUB.instances.read(); - let instance = &instance_guard[instance_id]; - get_adapter(instance.enumerate_adapters(), desc) -} - -#[cfg(all(feature = "local", not(feature = "gfx-backend-gl")))] +#[cfg(feature = "local")] #[no_mangle] pub extern "C" fn wgpu_instance_get_adapter( instance_id: InstanceId, @@ -215,26 +229,6 @@ pub extern "C" fn wgpu_instance_get_adapter( HUB.adapters.register_local(adapter) } -#[cfg(all(feature = "local", feature = "gfx-backend-gl"))] -pub fn wgpu_gl_create_surface(windowed_context: back::glutin::WindowedContext) -> SurfaceId { - let raw = back::Surface::from_window(windowed_context); - let surface = SurfaceHandle::new(raw); - HUB.surfaces.register_local(surface) -} - -#[cfg(feature = "gfx-backend-gl")] -#[no_mangle] -pub extern "C" fn wgpu_gl_surface_get_adapter( - surface_id: SurfaceId, - desc: &AdapterDescriptor -) -> AdapterId { - let surface_guard = HUB.surfaces.read(); - let surface = &surface_guard[surface_id]; - let adapter = get_adapter(surface.raw.enumerate_adapters(), desc); - info!("Adapter {:?}", adapter.info); - HUB.adapters.register_local(adapter) -} - pub fn adapter_create_device(adapter_id: AdapterId, _desc: &DeviceDescriptor) -> DeviceHandle { let adapter_guard = HUB.adapters.read(); let adapter = &adapter_guard[adapter_id]; diff --git a/wgpu-native/src/lib.rs b/wgpu-native/src/lib.rs index b3465fcf13..42e6bc4cb1 100644 --- a/wgpu-native/src/lib.rs +++ b/wgpu-native/src/lib.rs @@ -244,6 +244,8 @@ macro_rules! typed_id { define_id!(InstanceId); #[cfg(not(feature = "gfx-backend-gl"))] type InstanceHandle = back::Instance; +#[cfg(feature = "gfx-backend-gl")] +pub type InstanceId = SurfaceId; define_id!(AdapterId); type AdapterHandle = hal::Adapter;