Skip to content

Commit

Permalink
Allow constructing custom
Browse files Browse the repository at this point in the history
Signed-off-by: sagudev <[email protected]>
  • Loading branch information
sagudev committed Jan 3, 2025
1 parent 2001ddf commit f52082a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 114 deletions.
140 changes: 41 additions & 99 deletions wgpu/src/backend/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,22 @@ use crate::dispatch::{

use std::{ops::Range, pin::Pin, sync::Arc};

#[derive(Debug)]
pub struct DynContext(Arc<dyn InstanceInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynContext => .0);

impl DynContext {
pub(crate) fn new<T: InstanceInterface>(instance: T) -> Self {
Self(Arc::new(instance))
}

#[cfg(wgpu_core)]
pub fn from_core(instance: super::ContextWgpuCore) -> Self {
Self(Arc::new(instance))
}

#[cfg(webgpu)]
pub(crate) fn from_webgpu(instance: super::ContextWebGpu) -> Self {
Self(Arc::new(instance))
}
macro_rules! dyn_type {
(pub struct $name:ident(dyn $interface:tt)) => {
#[derive(Debug)]
pub struct $name(Arc<dyn $interface>);
crate::cmp::impl_eq_ord_hash_arc_address!($name => .0);

impl $name {
pub(crate) fn new<T: $interface>(t: T) -> Self {
Self(Arc::new(t))
}
}
};
}

dyn_type!(pub struct DynContext(dyn InstanceInterface));

impl InstanceInterface for DynContext {
fn new(_desc: crate::InstanceDescriptor) -> Self
where
Expand Down Expand Up @@ -63,9 +59,7 @@ impl InstanceInterface for DynContext {
}
}

#[derive(Debug)]
pub struct DynAdapter(Arc<dyn AdapterInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynAdapter => .0);
dyn_type!(pub struct DynAdapter(dyn AdapterInterface));

impl AdapterInterface for DynAdapter {
fn request_device(
Expand Down Expand Up @@ -108,9 +102,7 @@ impl AdapterInterface for DynAdapter {
}
}

#[derive(Debug)]
pub struct DynDevice(Arc<dyn DeviceInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynDevice => .0);
dyn_type!(pub struct DynDevice(dyn DeviceInterface));

impl DeviceInterface for DynDevice {
fn features(&self) -> crate::Features {
Expand Down Expand Up @@ -261,9 +253,7 @@ impl DeviceInterface for DynDevice {
}
}

#[derive(Debug)]
pub struct DynQueue(Arc<dyn QueueInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynQueue => .0);
dyn_type!(pub struct DynQueue(dyn QueueInterface));

impl QueueInterface for DynQueue {
fn write_buffer(
Expand Down Expand Up @@ -336,43 +326,31 @@ impl QueueInterface for DynQueue {
}
}

#[derive(Debug)]
pub struct DynShaderModule(Arc<dyn ShaderModuleInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynShaderModule => .0);
dyn_type!(pub struct DynShaderModule(dyn ShaderModuleInterface));

impl ShaderModuleInterface for DynShaderModule {
fn get_compilation_info(&self) -> Pin<Box<dyn dispatch::ShaderCompilationInfoFuture>> {
self.0.get_compilation_info()
}
}

#[derive(Debug)]
pub struct DynBindGroupLayout(Arc<dyn BindGroupLayoutInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynBindGroupLayout => .0);
dyn_type!(pub struct DynBindGroupLayout(dyn BindGroupLayoutInterface));

impl BindGroupLayoutInterface for DynBindGroupLayout {}

#[derive(Debug)]
pub struct DynBindGroup(Arc<dyn BindGroupInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynBindGroup => .0);
dyn_type!(pub struct DynBindGroup(dyn BindGroupInterface));

impl BindGroupInterface for DynBindGroup {}

#[derive(Debug)]
pub struct DynTextureView(Arc<dyn TextureViewInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynTextureView => .0);
dyn_type!(pub struct DynTextureView(dyn TextureViewInterface));

impl TextureViewInterface for DynTextureView {}

#[derive(Debug)]
pub struct DynSampler(Arc<dyn SamplerInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynSampler => .0);
dyn_type!(pub struct DynSampler(dyn SamplerInterface));

impl SamplerInterface for DynSampler {}

#[derive(Debug)]
pub struct DynBuffer(Arc<dyn BufferInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynBuffer => .0);
dyn_type!(pub struct DynBuffer(dyn BufferInterface));

impl BufferInterface for DynBuffer {
fn map_async(
Expand Down Expand Up @@ -408,9 +386,7 @@ impl BufferInterface for DynBuffer {
}
}

#[derive(Debug)]
pub struct DynTexture(Arc<dyn TextureInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynTexture => .0);
dyn_type!(pub struct DynTexture(dyn TextureInterface));

impl TextureInterface for DynTexture {
fn create_view(
Expand All @@ -425,71 +401,55 @@ impl TextureInterface for DynTexture {
}
}

#[derive(Debug)]
pub struct DynBlas(Arc<dyn BlasInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynBlas => .0);
dyn_type!(pub struct DynBlas(dyn BlasInterface));

impl BlasInterface for DynBlas {
fn destroy(&self) {
self.0.destroy();
}
}

#[derive(Debug)]
pub struct DynTlas(Arc<dyn TlasInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynTlas => .0);
dyn_type!(pub struct DynTlas(dyn TlasInterface));

impl TlasInterface for DynTlas {
fn destroy(&self) {
self.0.destroy();
}
}

#[derive(Debug)]
pub struct DynQuerySet(Arc<dyn QuerySetInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynQuerySet => .0);
dyn_type!(pub struct DynQuerySet(dyn QuerySetInterface));

impl QuerySetInterface for DynQuerySet {}

#[derive(Debug)]
pub struct DynPipelineLayout(Arc<dyn PipelineLayoutInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynPipelineLayout => .0);
dyn_type!(pub struct DynPipelineLayout(dyn PipelineLayoutInterface));

impl PipelineLayoutInterface for DynPipelineLayout {}

#[derive(Debug)]
pub struct DynRenderPipeline(Arc<dyn RenderPipelineInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynRenderPipeline => .0);
dyn_type!(pub struct DynRenderPipeline(dyn RenderPipelineInterface));

impl RenderPipelineInterface for DynRenderPipeline {
fn get_bind_group_layout(&self, index: u32) -> DispatchBindGroupLayout {
self.0.get_bind_group_layout(index)
}
}

#[derive(Debug)]
pub struct DynComputePipeline(Arc<dyn ComputePipelineInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynComputePipeline => .0);
dyn_type!(pub struct DynComputePipeline(dyn ComputePipelineInterface));

impl ComputePipelineInterface for DynComputePipeline {
fn get_bind_group_layout(&self, index: u32) -> DispatchBindGroupLayout {
self.0.get_bind_group_layout(index)
}
}

#[derive(Debug)]
pub struct DynPipelineCache(Arc<dyn PipelineCacheInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynPipelineCache => .0);
dyn_type!(pub struct DynPipelineCache(dyn PipelineCacheInterface));

impl PipelineCacheInterface for DynPipelineCache {
fn get_data(&self) -> Option<Vec<u8>> {
self.0.get_data()
}
}

#[derive(Debug)]
pub struct DynCommandEncoder(Arc<dyn CommandEncoderInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynCommandEncoder => .0);
dyn_type!(pub struct DynCommandEncoder(dyn CommandEncoderInterface));

impl CommandEncoderInterface for DynCommandEncoder {
fn copy_buffer_to_buffer(
Expand Down Expand Up @@ -624,9 +584,7 @@ impl CommandEncoderInterface for DynCommandEncoder {
}
}

#[derive(Debug)]
pub struct DynComputePass(Arc<dyn ComputePassInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynComputePass => .0);
dyn_type!(pub struct DynComputePass(dyn ComputePassInterface));

impl ComputePassInterface for DynComputePass {
fn set_pipeline(&mut self, pipeline: &DispatchComputePipeline) {
Expand Down Expand Up @@ -705,9 +663,7 @@ impl ComputePassInterface for DynComputePass {
}
}

#[derive(Debug)]
pub struct DynRenderPass(Arc<dyn RenderPassInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynRenderPass => .0);
dyn_type!(pub struct DynRenderPass(dyn RenderPassInterface));

impl RenderPassInterface for DynRenderPass {
fn set_pipeline(&mut self, pipeline: &DispatchRenderPipeline) {
Expand Down Expand Up @@ -935,15 +891,11 @@ impl RenderPassInterface for DynRenderPass {
}
}

#[derive(Debug)]
pub struct DynCommandBuffer(Arc<dyn CommandBufferInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynCommandBuffer => .0);
dyn_type!(pub struct DynCommandBuffer(dyn CommandBufferInterface));

impl CommandBufferInterface for DynCommandBuffer {}

#[derive(Debug)]
pub struct DynRenderBundleEncoder(Arc<dyn RenderBundleEncoderInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynRenderBundleEncoder => .0);
dyn_type!(pub struct DynRenderBundleEncoder(dyn RenderBundleEncoderInterface));

impl RenderBundleEncoderInterface for DynRenderBundleEncoder {
fn set_pipeline(&mut self, pipeline: &DispatchRenderPipeline) {
Expand Down Expand Up @@ -1029,15 +981,11 @@ impl RenderBundleEncoderInterface for DynRenderBundleEncoder {
}
}

#[derive(Debug)]
pub struct DynRenderBundle(Arc<dyn RenderBundleInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynRenderBundle => .0);
dyn_type!(pub struct DynRenderBundle(dyn RenderBundleInterface));

impl RenderBundleInterface for DynRenderBundle {}

#[derive(Debug)]
pub struct DynSurface(Arc<dyn SurfaceInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynSurface => .0);
dyn_type!(pub struct DynSurface(dyn SurfaceInterface));

impl SurfaceInterface for DynSurface {
fn get_capabilities(&self, adapter: &dispatch::DispatchAdapter) -> wgt::SurfaceCapabilities {
Expand All @@ -1059,9 +1007,7 @@ impl SurfaceInterface for DynSurface {
}
}

#[derive(Debug)]
pub struct DynSurfaceOutputDetail(Arc<dyn SurfaceOutputDetailInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynSurfaceOutputDetail => .0);
dyn_type!(pub struct DynSurfaceOutputDetail(dyn SurfaceOutputDetailInterface));

impl SurfaceOutputDetailInterface for DynSurfaceOutputDetail {
fn present(&self) {
Expand All @@ -1073,9 +1019,7 @@ impl SurfaceOutputDetailInterface for DynSurfaceOutputDetail {
}
}

#[derive(Debug)]
pub struct DynQueueWriteBuffer(Arc<dyn QueueWriteBufferInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynQueueWriteBuffer => .0);
dyn_type!(pub struct DynQueueWriteBuffer(dyn QueueWriteBufferInterface));

impl QueueWriteBufferInterface for DynQueueWriteBuffer {
fn slice(&self) -> &[u8] {
Expand All @@ -1087,9 +1031,7 @@ impl QueueWriteBufferInterface for DynQueueWriteBuffer {
}
}

#[derive(Debug)]
pub struct DynBufferMappedRange(Arc<dyn BufferMappedRangeInterface>);
crate::cmp::impl_eq_ord_hash_arc_address!(DynBufferMappedRange => .0);
dyn_type!(pub struct DynBufferMappedRange(dyn BufferMappedRangeInterface));

impl BufferMappedRangeInterface for DynBufferMappedRange {
fn slice(&self) -> &[u8] {
Expand Down
28 changes: 13 additions & 15 deletions wgpu/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,19 @@ macro_rules! dispatch_types {
_ => None,
}
}

//#[cfg(custom)]
#[inline]
#[allow(unused)]
pub fn into_dyn(self) -> <$custom_context as InterfaceTypes>::$subtype {
match self {
#[cfg(wgpu_core)]
Self::Core(value) => <$custom_context as InterfaceTypes>::$subtype::new(value),
#[cfg(webgpu)]
Self::WebGPU(value) => <$custom_context as InterfaceTypes>::$subtype::new(value),
Self::Custom(value) => value,
}
}
}

#[cfg(wgpu_core)]
Expand Down Expand Up @@ -714,21 +727,6 @@ macro_rules! dispatch_types {
};
}

impl DispatchInstance {
//#[cfg(custom)]
#[inline]
#[allow(unused)]
pub fn into_dyn(self) -> backend::DynContext {
match self {
#[cfg(wgpu_core)]
Self::Core(value) => backend::DynContext::from_core(value),
#[cfg(webgpu)]
Self::WebGPU(value) => backend::DynContext::from_webgpu(value),
Self::Custom(value) => value,
}
}
}

dispatch_types! {
wgpu_core = backend::ContextWgpuCore;
webgpu = backend::ContextWebGpu;
Expand Down

0 comments on commit f52082a

Please sign in to comment.