add new Instance::as_hal fn

This commit is contained in:
teoxoy authored and Teodor Tanasoaia committed 2024-10-14 13:24:44 +02:00
1 parent f2faf609af
commit 40206ce4d7
2 files changed
+18 -17

No files matched your search

+1 -7
View File
@@ -61,13 +61,7 @@ impl Global {
///
/// - The raw instance handle returned must not be manually destroyed.
pub unsafe fn instance_as_hal<A: HalApi>(&self) -> Option<&A::Instance> {
self.instance.raw(A::VARIANT).map(|instance| {
instance
.as_any()
.downcast_ref()
// This should be impossible. It would mean that backend instance and enum type are mismatching.
.expect("Stored instance is not of the correct type")
})
unsafe { self.instance.as_hal::<A>() }
}
/// # Safety
+17 -10
View File
@@ -121,6 +121,19 @@ impl Instance {
(*instance_backend == backend).then(|| instance.as_ref())
})
}
/// # Safety
///
/// - The raw instance handle returned must not be manually destroyed.
pub unsafe fn as_hal<A: HalApi>(&self) -> Option<&A::Instance> {
self.raw(A::VARIANT).map(|instance| {
instance
.as_any()
.downcast_ref()
// This should be impossible. It would mean that backend instance and enum type are mismatching.
.expect("Stored instance is not of the correct type")
})
}
}
pub struct Surface {
@@ -463,11 +476,8 @@ impl Global {
) -> Result<SurfaceId, CreateSurfaceError> {
profiling::scope!("Instance::create_surface_metal");
let instance = self
.instance
.raw(Backend::Metal)
let instance = unsafe { self.instance.as_hal::<hal::api::Metal>() }
.ok_or(CreateSurfaceError::BackendNotEnabled(Backend::Metal))?;
let instance_metal: &hal::metal::Instance = instance.as_any().downcast_ref().unwrap();
let layer = layer.cast();
// SAFETY: We do this cast and deref. (rather than using `metal` to get the
@@ -483,7 +493,7 @@ impl Global {
// lexical scope.
let layer = unsafe { &*layer };
let raw_surface: Box<dyn hal::DynSurface> =
Box::new(instance_metal.create_surface_from_layer(layer));
Box::new(instance.create_surface_from_layer(layer));
let surface = Surface {
presentation: Mutex::new(rank::SURFACE_PRESENTATION, None),
@@ -500,12 +510,9 @@ impl Global {
id_in: Option<SurfaceId>,
create_surface_func: impl FnOnce(&hal::dx12::Instance) -> hal::dx12::Surface,
) -> Result<SurfaceId, CreateSurfaceError> {
let instance = self
.instance
.raw(Backend::Dx12)
let instance = unsafe { self.instance.as_hal::<hal::api::Dx12>() }
.ok_or(CreateSurfaceError::BackendNotEnabled(Backend::Dx12))?;
let instance_dx12 = instance.as_any().downcast_ref().unwrap();
let surface: Box<dyn hal::DynSurface> = Box::new(create_surface_func(instance_dx12));
let surface: Box<dyn hal::DynSurface> = Box::new(create_surface_func(instance));
let surface = Surface {
presentation: Mutex::new(rank::SURFACE_PRESENTATION, None),