Skip to content

Commit

Permalink
introduce DynAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed Aug 10, 2024
1 parent 99d1fcb commit 719f9b0
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 5 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 @@ -88,6 +88,7 @@ impl crate::Api for Api {
}

crate::impl_dyn_resource!(
Adapter,
AccelerationStructure,
BindGroup,
BindGroupLayout,
Expand Down
56 changes: 56 additions & 0 deletions wgpu-hal/src/dynamic/adapter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use crate::{Adapter, DeviceError, SurfaceCapabilities, TextureFormatCapabilities};

use super::{DynDevice, DynQueue, DynResource, DynResourceExt, DynSurface};

pub struct DynOpenDevice {
pub device: Box<dyn DynDevice>,
pub queue: Box<dyn DynQueue>,
}

pub trait DynAdapter: DynResource {
unsafe fn open(
&self,
features: wgt::Features,
limits: &wgt::Limits,
memory_hints: &wgt::MemoryHints,
) -> Result<DynOpenDevice, DeviceError>;

unsafe fn texture_format_capabilities(
&self,
format: wgt::TextureFormat,
) -> TextureFormatCapabilities;

unsafe fn surface_capabilities(&self, surface: &dyn DynSurface) -> Option<SurfaceCapabilities>;

unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp;
}

impl<A: Adapter + DynResource> DynAdapter for A {
unsafe fn open(
&self,
features: wgt::Features,
limits: &wgt::Limits,
memory_hints: &wgt::MemoryHints,
) -> Result<DynOpenDevice, DeviceError> {
unsafe { A::open(self, features, limits, memory_hints) }.map(|open_device| DynOpenDevice {
device: Box::new(open_device.device),
queue: Box::new(open_device.queue),
})
}

unsafe fn texture_format_capabilities(
&self,
format: wgt::TextureFormat,
) -> TextureFormatCapabilities {
unsafe { A::texture_format_capabilities(self, format) }
}

unsafe fn surface_capabilities(&self, surface: &dyn DynSurface) -> Option<SurfaceCapabilities> {
let surface = surface.expect_downcast_ref();
unsafe { A::surface_capabilities(self, surface) }
}

unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp {
unsafe { A::get_presentation_timestamp(self) }
}
}
2 changes: 2 additions & 0 deletions wgpu-hal/src/dynamic/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
mod adapter;
mod command;
mod device;
mod queue;
mod surface;

pub use adapter::{DynAdapter, DynOpenDevice};
pub use command::DynCommandEncoder;
pub use device::DynDevice;
pub use queue::DynQueue;
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 @@ -165,6 +165,7 @@ impl crate::Api for Api {
}

crate::impl_dyn_resource!(
Adapter,
AccelerationStructure,
BindGroup,
BindGroupLayout,
Expand Down
11 changes: 6 additions & 5 deletions wgpu-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,11 @@ mod dynamic;

pub(crate) use dynamic::impl_dyn_resource;
pub use dynamic::{
DynAccelerationStructure, DynAcquiredSurfaceTexture, DynBindGroup, DynBindGroupLayout,
DynBuffer, DynCommandBuffer, DynCommandEncoder, DynComputePipeline, DynDevice, DynFence,
DynPipelineCache, DynPipelineLayout, DynQuerySet, DynQueue, DynRenderPipeline, DynResource,
DynSampler, DynShaderModule, DynSurface, DynSurfaceTexture, DynTexture, DynTextureView,
DynAccelerationStructure, DynAcquiredSurfaceTexture, DynAdapter, DynBindGroup,
DynBindGroupLayout, DynBuffer, DynCommandBuffer, DynCommandEncoder, DynComputePipeline,
DynDevice, DynFence, DynOpenDevice, DynPipelineCache, DynPipelineLayout, DynQuerySet, DynQueue,
DynRenderPipeline, DynResource, DynSampler, DynShaderModule, DynSurface, DynSurfaceTexture,
DynTexture, DynTextureView,
};

use std::{
Expand Down Expand Up @@ -391,7 +392,7 @@ impl InstanceError {
pub trait Api: Clone + fmt::Debug + Sized {
type Instance: Instance<A = Self>;
type Surface: DynSurface + Surface<A = Self>;
type Adapter: Adapter<A = Self>;
type Adapter: DynAdapter + Adapter<A = Self>;
type Device: DynDevice + Device<A = Self>;

type Queue: DynQueue + Queue<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 @@ -72,6 +72,7 @@ impl crate::Api for Api {
}

crate::impl_dyn_resource!(
Adapter,
AccelerationStructure,
BindGroup,
BindGroupLayout,
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 @@ -79,6 +79,7 @@ impl crate::Api for Api {
}

crate::impl_dyn_resource!(
Adapter,
AccelerationStructure,
BindGroup,
BindGroupLayout,
Expand Down

0 comments on commit 719f9b0

Please sign in to comment.