diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index e7c3d1127..171da90ae 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -126,85 +126,23 @@ pub struct Instance { impl Instance { pub fn new( name: &str, - mut instance_desc: wgt::InstanceDescriptor, + instance_desc: wgt::InstanceDescriptor, telemetry: Option, ) -> Arc { - let mut this = Self { - _name: name.to_owned(), - instance_per_backend: Vec::new(), - requested_backends: instance_desc.backends, - supported_backends: Backends::empty(), - flags: instance_desc.flags, - // HACK: We must take ownership of the field here, without being able to pass it into - // try_add_hal(). Remove it from the mutable descriptor instead, while try_add_hal() - // borrows the handle from `this.display` instead. - display: instance_desc.display.take(), - devices: InstanceDevices::new(), - }; + let mut this = InstanceBuilder::new(name, instance_desc, telemetry); #[cfg(all(vulkan, not(target_os = "netbsd")))] - this.try_add_hal(hal::api::Vulkan, &instance_desc, telemetry); + this.try_add_hal(hal::api::Vulkan); #[cfg(metal)] - this.try_add_hal(hal::api::Metal, &instance_desc, telemetry); + this.try_add_hal(hal::api::Metal); #[cfg(dx12)] - this.try_add_hal(hal::api::Dx12, &instance_desc, telemetry); + this.try_add_hal(hal::api::Dx12); #[cfg(gles)] - this.try_add_hal(hal::api::Gles, &instance_desc, telemetry); + this.try_add_hal(hal::api::Gles); #[cfg(feature = "noop")] - this.try_add_hal(hal::api::Noop, &instance_desc, telemetry); + this.try_add_hal(hal::api::Noop); - Arc::new(this) - } - - /// Helper for `Instance::new()`; attempts to add a single `wgpu-hal` backend to this instance. - fn try_add_hal( - &mut self, - _: A, - instance_desc: &wgt::InstanceDescriptor, - telemetry: Option, - ) { - // Whether or not the backend was requested, and whether or not it succeeds, - // note that we *could* try it. - self.supported_backends |= A::VARIANT.into(); - - if !instance_desc.backends.contains(A::VARIANT.into()) { - log::trace!("Instance::new: backend {:?} not requested", A::VARIANT); - return; - } - - // If this was Some, it was moved into self - assert!(instance_desc.display.is_none()); - - let hal_desc = hal::InstanceDescriptor { - name: "wgpu", - flags: self.flags, - memory_budget_thresholds: instance_desc.memory_budget_thresholds, - backend_options: instance_desc.backend_options.clone(), - telemetry, - // Pass a borrow, the core instance here keeps the owned handle alive already - // WARNING: Using self here, not instance_desc! - display: self.display.as_ref().map(|hdh| { - hdh.display_handle() - .expect("Implementation did not provide a DisplayHandle") - }), - }; - - use hal::Instance as _; - // SAFETY: ??? - match unsafe { A::Instance::init(&hal_desc) } { - Ok(instance) => { - log::debug!("Instance::new: created {:?} backend", A::VARIANT); - self.instance_per_backend - .push((A::VARIANT, Box::new(instance))); - } - Err(err) => { - log::debug!( - "Instance::new: failed to create {:?} backend: {:?}", - A::VARIANT, - err - ); - } - } + this.build() } pub fn from_hal_instance( @@ -759,6 +697,98 @@ impl Instance { } } +pub struct InstanceBuilder { + instance: Instance, + instance_desc: wgt::InstanceDescriptor, + telemetry: Option, +} + +impl InstanceBuilder { + pub fn new( + name: &str, + mut desc: wgt::InstanceDescriptor, + telemetry: Option, + ) -> Self { + Self { + instance: Instance { + _name: name.to_owned(), + instance_per_backend: Vec::new(), + requested_backends: desc.backends, + supported_backends: Backends::empty(), + flags: desc.flags, + // HACK: We must take ownership of the field here, without being able to pass it into + // try_add_hal(). Remove it from the mutable descriptor instead, while try_add_hal() + // borrows the handle from `this.display` instead. + display: desc.display.take(), + devices: InstanceDevices::new(), + }, + instance_desc: desc, + telemetry, + } + } + + pub fn try_add_hal(&mut self, api: A) { + use hal::Instance as _; + self.try_add_custom_hal(api, |hal_desc| unsafe { A::Instance::init(hal_desc) }); + } + + pub fn try_add_custom_hal( + &mut self, + _: A, + f: impl FnOnce(&hal::InstanceDescriptor) -> Result, + ) { + if self.instance.supported_backends.contains(A::VARIANT.into()) { + return; + } + + // Whether or not the backend was requested, and whether or not it succeeds, + // note that we *could* try it. + self.instance.supported_backends |= A::VARIANT.into(); + + if !self.instance_desc.backends.contains(A::VARIANT.into()) { + log::trace!("Instance::new: backend {:?} not requested", A::VARIANT); + return; + } + + // If this was Some, it was moved into self + assert!(self.instance_desc.display.is_none()); + + let hal_desc = hal::InstanceDescriptor { + name: "wgpu", + flags: self.instance.flags, + memory_budget_thresholds: self.instance_desc.memory_budget_thresholds, + backend_options: self.instance_desc.backend_options.clone(), + telemetry: self.telemetry, + // Pass a borrow, the core instance here keeps the owned handle alive already + // WARNING: Using self here, not instance_desc! + display: self.instance.display.as_ref().map(|hdh| { + hdh.display_handle() + .expect("Implementation did not provide a DisplayHandle") + }), + }; + + match f(&hal_desc) { + Ok(instance) => { + log::debug!("Instance::new: created {:?} backend", A::VARIANT); + self.instance + .instance_per_backend + .push((A::VARIANT, Box::new(instance))); + } + Err(err) => { + log::debug!( + "Instance::new: failed to create {:?} backend: {:?}", + A::VARIANT, + err + ); + } + } + } + + pub fn build(self) -> Arc { + Arc::new(self.instance) + } +} + pub struct Surface { pub(crate) presentation: Mutex>, pub surface_per_backend: HashMap>,