Skip to content

Commit

Permalink
introduce DynInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed Aug 10, 2024
1 parent 719f9b0 commit eba1072
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 4 deletions.
1 change: 1 addition & 0 deletions wgpu-hal/src/dx12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ crate::impl_dyn_resource!(
ComputePipeline,
Device,
Fence,
Instance,
PipelineCache,
PipelineLayout,
QuerySet,
Expand Down
53 changes: 53 additions & 0 deletions wgpu-hal/src/dynamic/instance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Box casts are needed, alternative would be a temporaries which are more verbose and not more expressive.
#![allow(trivial_casts)]

use crate::{Capabilities, Instance, InstanceError};

use super::{DynAdapter, DynResource, DynResourceExt as _, DynSurface};

pub struct DynExposedAdapter {
pub adapter: Box<dyn DynAdapter>,
pub info: wgt::AdapterInfo,
pub features: wgt::Features,
pub capabilities: Capabilities,
}

pub trait DynInstance: DynResource {
unsafe fn create_surface(
&self,
display_handle: raw_window_handle::RawDisplayHandle,
window_handle: raw_window_handle::RawWindowHandle,
) -> Result<Box<dyn DynSurface>, InstanceError>;

unsafe fn enumerate_adapters(
&self,
surface_hint: Option<&dyn DynSurface>,
) -> Vec<DynExposedAdapter>;
}

impl<I: Instance + DynResource> DynInstance for I {
unsafe fn create_surface(
&self,
display_handle: raw_window_handle::RawDisplayHandle,
window_handle: raw_window_handle::RawWindowHandle,
) -> Result<Box<dyn DynSurface>, InstanceError> {
unsafe { I::create_surface(self, display_handle, window_handle) }
.map(|surface| Box::new(surface) as Box<dyn DynSurface>)
}

unsafe fn enumerate_adapters(
&self,
surface_hint: Option<&dyn DynSurface>,
) -> Vec<DynExposedAdapter> {
let surface_hint = surface_hint.map(|s| s.expect_downcast_ref());
unsafe { I::enumerate_adapters(self, surface_hint) }
.into_iter()
.map(|exposed| DynExposedAdapter {
adapter: Box::new(exposed.adapter),
info: exposed.info,
features: exposed.features,
capabilities: exposed.capabilities,
})
.collect()
}
}
2 changes: 2 additions & 0 deletions wgpu-hal/src/dynamic/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
mod adapter;
mod command;
mod device;
mod instance;
mod queue;
mod surface;

pub use adapter::{DynAdapter, DynOpenDevice};
pub use command::DynCommandEncoder;
pub use device::DynDevice;
pub use instance::{DynExposedAdapter, DynInstance};
pub use queue::DynQueue;
pub use surface::{DynAcquiredSurfaceTexture, DynSurface};

Expand Down
1 change: 1 addition & 0 deletions wgpu-hal/src/gles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ crate::impl_dyn_resource!(
ComputePipeline,
Device,
Fence,
Instance,
PipelineCache,
PipelineLayout,
QuerySet,
Expand Down
8 changes: 4 additions & 4 deletions wgpu-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@ pub(crate) use dynamic::impl_dyn_resource;
pub use dynamic::{
DynAccelerationStructure, DynAcquiredSurfaceTexture, DynAdapter, DynBindGroup,
DynBindGroupLayout, DynBuffer, DynCommandBuffer, DynCommandEncoder, DynComputePipeline,
DynDevice, DynFence, DynOpenDevice, DynPipelineCache, DynPipelineLayout, DynQuerySet, DynQueue,
DynRenderPipeline, DynResource, DynSampler, DynShaderModule, DynSurface, DynSurfaceTexture,
DynTexture, DynTextureView,
DynDevice, DynExposedAdapter, DynFence, DynInstance, DynOpenDevice, DynPipelineCache,
DynPipelineLayout, DynQuerySet, DynQueue, DynRenderPipeline, DynResource, DynSampler,
DynShaderModule, DynSurface, DynSurfaceTexture, DynTexture, DynTextureView,
};

use std::{
Expand Down Expand Up @@ -390,7 +390,7 @@ impl InstanceError {
}

pub trait Api: Clone + fmt::Debug + Sized {
type Instance: Instance<A = Self>;
type Instance: DynInstance + Instance<A = Self>;
type Surface: DynSurface + Surface<A = Self>;
type Adapter: DynAdapter + Adapter<A = Self>;
type Device: DynDevice + Device<A = Self>;
Expand Down
1 change: 1 addition & 0 deletions wgpu-hal/src/metal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ crate::impl_dyn_resource!(
ComputePipeline,
Device,
Fence,
Instance,
PipelineCache,
PipelineLayout,
QuerySet,
Expand Down
1 change: 1 addition & 0 deletions wgpu-hal/src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ crate::impl_dyn_resource!(
ComputePipeline,
Device,
Fence,
Instance,
PipelineCache,
PipelineLayout,
QuerySet,
Expand Down

0 comments on commit eba1072

Please sign in to comment.