From 79ecded9fa0f5a20fb470e6a9c27caf0fa284a63 Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Wed, 8 Jan 2025 18:48:17 +0100 Subject: [PATCH] mod interface_types Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --- wgpu/src/api/render_bundle_encoder.rs | 2 +- wgpu/src/backend/custom.rs | 1079 ++----------------------- wgpu/src/backend/mod.rs | 1 - wgpu/src/backend/webgpu.rs | 59 +- wgpu/src/backend/wgpu_core.rs | 61 +- wgpu/src/dispatch.rs | 289 ++++--- 6 files changed, 288 insertions(+), 1203 deletions(-) diff --git a/wgpu/src/api/render_bundle_encoder.rs b/wgpu/src/api/render_bundle_encoder.rs index 23bd975ed4..bb0e5de7d5 100644 --- a/wgpu/src/api/render_bundle_encoder.rs +++ b/wgpu/src/api/render_bundle_encoder.rs @@ -58,7 +58,7 @@ impl<'a> RenderBundleEncoder<'a> { #[cfg(webgpu)] dispatch::DispatchRenderBundleEncoder::WebGPU(b) => b.finish(desc), //#[cfg(custom)] - dispatch::DispatchRenderBundleEncoder::Custom(b) => b.finish(desc), + dispatch::DispatchRenderBundleEncoder::Custom(_) => unimplemented!(), }; RenderBundle { inner: bundle } diff --git a/wgpu/src/backend/custom.rs b/wgpu/src/backend/custom.rs index a088c763e8..929c18961e 100644 --- a/wgpu/src/backend/custom.rs +++ b/wgpu/src/backend/custom.rs @@ -1,11 +1,9 @@ #![allow(ambiguous_wide_pointer_comparisons)] use crate::dispatch::{ - self, AdapterInterface, BindGroupInterface, BindGroupLayoutInterface, BlasInterface, - BufferInterface, BufferMappedRangeInterface, CommandBufferInterface, CommandEncoderInterface, - ComputePassInterface, ComputePipelineInterface, DeviceInterface, DispatchBindGroup, - DispatchBindGroupLayout, DispatchBuffer, DispatchCommandBuffer, DispatchComputePipeline, - DispatchQuerySet, DispatchRenderPipeline, DispatchTexture, InstanceInterface, InterfaceTypes, + AdapterInterface, BindGroupInterface, BindGroupLayoutInterface, BlasInterface, BufferInterface, + BufferMappedRangeInterface, CommandBufferInterface, CommandEncoderInterface, + ComputePassInterface, ComputePipelineInterface, DeviceInterface, InstanceInterface, PipelineCacheInterface, PipelineLayoutInterface, QuerySetInterface, QueueInterface, QueueWriteBufferInterface, RenderBundleEncoderInterface, RenderBundleInterface, RenderPassInterface, RenderPipelineInterface, SamplerInterface, ShaderModuleInterface, @@ -13,7 +11,7 @@ use crate::dispatch::{ TlasInterface, }; -use std::{ops::Range, pin::Pin, sync::Arc}; +use std::sync::Arc; macro_rules! dyn_type { // cloning of arc forbidden @@ -27,6 +25,22 @@ macro_rules! dyn_type { Self(Arc::new(t)) } } + + impl std::ops::Deref for $name { + type Target = dyn $interface; + + #[inline] + fn deref(&self) -> &Self::Target { + self.0.as_ref() + } + } + + impl std::ops::DerefMut for $name { + #[inline] + fn deref_mut(&mut self) -> &mut Self::Target { + Arc::get_mut(&mut self.0).expect("") + } + } }; // cloning of arc is allowed (pub ref struct $name:ident(dyn $interface:tt)) => { @@ -39,1077 +53,102 @@ macro_rules! dyn_type { Self(Arc::new(t)) } } - }; -} - -dyn_type!(pub ref struct DynContext(dyn InstanceInterface)); - -impl InstanceInterface for DynContext { - fn new(_desc: &crate::InstanceDescriptor) -> Self - where - Self: Sized, - { - unimplemented!("DynInstance cannot be created without knowing inner type") - // while this info could be provided via generics we do not want that - } - - unsafe fn create_surface( - &self, - target: crate::SurfaceTargetUnsafe, - ) -> Result { - unsafe { self.0.create_surface(target) } - } - fn request_adapter( - &self, - options: &crate::RequestAdapterOptions<'_, '_>, - ) -> Pin> { - self.0.request_adapter(options) - } + impl std::ops::Deref for $name { + type Target = dyn $interface; - fn poll_all_devices(&self, force_wait: bool) -> bool { - self.0.poll_all_devices(force_wait) - } + #[inline] + fn deref(&self) -> &Self::Target { + self.0.as_ref() + } + } + }; } +dyn_type!(pub ref struct DynContext(dyn InstanceInterface)); dyn_type!(pub ref struct DynAdapter(dyn AdapterInterface)); - -impl AdapterInterface for DynAdapter { - fn request_device( - &self, - desc: &crate::DeviceDescriptor<'_>, - trace_dir: Option<&std::path::Path>, - ) -> Pin> { - self.0.request_device(desc, trace_dir) - } - - fn is_surface_supported(&self, surface: &dispatch::DispatchSurface) -> bool { - self.0.is_surface_supported(surface) - } - - fn features(&self) -> crate::Features { - self.0.features() - } - - fn limits(&self) -> crate::Limits { - self.0.limits() - } - - fn downlevel_capabilities(&self) -> crate::DownlevelCapabilities { - self.0.downlevel_capabilities() - } - - fn get_info(&self) -> crate::AdapterInfo { - self.0.get_info() - } - - fn get_texture_format_features( - &self, - format: crate::TextureFormat, - ) -> crate::TextureFormatFeatures { - self.0.get_texture_format_features(format) - } - - fn get_presentation_timestamp(&self) -> crate::PresentationTimestamp { - self.0.get_presentation_timestamp() - } -} - dyn_type!(pub ref struct DynDevice(dyn DeviceInterface)); - -impl DeviceInterface for DynDevice { - fn features(&self) -> crate::Features { - self.0.features() - } - - fn limits(&self) -> crate::Limits { - self.0.limits() - } - - fn create_shader_module( - &self, - desc: crate::ShaderModuleDescriptor<'_>, - shader_runtime_checks: wgt::ShaderRuntimeChecks, - ) -> dispatch::DispatchShaderModule { - self.0.create_shader_module(desc, shader_runtime_checks) - } - - unsafe fn create_shader_module_spirv( - &self, - desc: &crate::ShaderModuleDescriptorSpirV<'_>, - ) -> dispatch::DispatchShaderModule { - unsafe { self.0.create_shader_module_spirv(desc) } - } - - fn create_bind_group_layout( - &self, - desc: &crate::BindGroupLayoutDescriptor<'_>, - ) -> dispatch::DispatchBindGroupLayout { - self.0.create_bind_group_layout(desc) - } - - fn create_bind_group( - &self, - desc: &crate::BindGroupDescriptor<'_>, - ) -> dispatch::DispatchBindGroup { - self.0.create_bind_group(desc) - } - - fn create_pipeline_layout( - &self, - desc: &crate::PipelineLayoutDescriptor<'_>, - ) -> dispatch::DispatchPipelineLayout { - self.0.create_pipeline_layout(desc) - } - - fn create_render_pipeline( - &self, - desc: &crate::RenderPipelineDescriptor<'_>, - ) -> dispatch::DispatchRenderPipeline { - self.0.create_render_pipeline(desc) - } - - fn create_compute_pipeline( - &self, - desc: &crate::ComputePipelineDescriptor<'_>, - ) -> dispatch::DispatchComputePipeline { - self.0.create_compute_pipeline(desc) - } - - unsafe fn create_pipeline_cache( - &self, - desc: &crate::PipelineCacheDescriptor<'_>, - ) -> dispatch::DispatchPipelineCache { - unsafe { self.0.create_pipeline_cache(desc) } - } - - fn create_buffer(&self, desc: &crate::BufferDescriptor<'_>) -> dispatch::DispatchBuffer { - self.0.create_buffer(desc) - } - - fn create_texture(&self, desc: &crate::TextureDescriptor<'_>) -> dispatch::DispatchTexture { - self.0.create_texture(desc) - } - - fn create_blas( - &self, - desc: &crate::CreateBlasDescriptor<'_>, - sizes: crate::BlasGeometrySizeDescriptors, - ) -> (Option, dispatch::DispatchBlas) { - self.0.create_blas(desc, sizes) - } - - fn create_tlas(&self, desc: &crate::CreateTlasDescriptor<'_>) -> dispatch::DispatchTlas { - self.0.create_tlas(desc) - } - - fn create_sampler(&self, desc: &crate::SamplerDescriptor<'_>) -> dispatch::DispatchSampler { - self.0.create_sampler(desc) - } - - fn create_query_set(&self, desc: &crate::QuerySetDescriptor<'_>) -> dispatch::DispatchQuerySet { - self.0.create_query_set(desc) - } - - fn create_command_encoder( - &self, - desc: &crate::CommandEncoderDescriptor<'_>, - ) -> dispatch::DispatchCommandEncoder { - self.0.create_command_encoder(desc) - } - - fn create_render_bundle_encoder( - &self, - desc: &crate::RenderBundleEncoderDescriptor<'_>, - ) -> dispatch::DispatchRenderBundleEncoder { - self.0.create_render_bundle_encoder(desc) - } - - fn set_device_lost_callback(&self, device_lost_callback: dispatch::BoxDeviceLostCallback) { - self.0.set_device_lost_callback(device_lost_callback); - } - - fn on_uncaptured_error(&self, handler: Box) { - self.0.on_uncaptured_error(handler); - } - - fn push_error_scope(&self, filter: crate::ErrorFilter) { - self.0.push_error_scope(filter); - } - - fn pop_error_scope(&self) -> Pin> { - self.0.pop_error_scope() - } - - fn start_capture(&self) { - self.0.start_capture(); - } - - fn stop_capture(&self) { - self.0.stop_capture(); - } - - fn poll(&self, maintain: crate::Maintain) -> crate::MaintainResult { - self.0.poll(maintain) - } - - fn get_internal_counters(&self) -> crate::InternalCounters { - self.0.get_internal_counters() - } - - fn generate_allocator_report(&self) -> Option { - self.0.generate_allocator_report() - } - - fn destroy(&self) { - self.0.destroy(); - } -} - dyn_type!(pub ref struct DynQueue(dyn QueueInterface)); - -impl QueueInterface for DynQueue { - fn write_buffer( - &self, - buffer: &dispatch::DispatchBuffer, - offset: crate::BufferAddress, - data: &[u8], - ) { - self.0.write_buffer(buffer, offset, data); - } - - fn create_staging_buffer( - &self, - size: crate::BufferSize, - ) -> Option { - self.0.create_staging_buffer(size) - } - - fn validate_write_buffer( - &self, - buffer: &dispatch::DispatchBuffer, - offset: wgt::BufferAddress, - size: wgt::BufferSize, - ) -> Option<()> { - self.0.validate_write_buffer(buffer, offset, size) - } - - fn write_staging_buffer( - &self, - buffer: &dispatch::DispatchBuffer, - offset: crate::BufferAddress, - staging_buffer: &dispatch::DispatchQueueWriteBuffer, - ) { - self.0.write_staging_buffer(buffer, offset, staging_buffer); - } - - fn write_texture( - &self, - texture: crate::TexelCopyTextureInfo<'_>, - data: &[u8], - data_layout: crate::TexelCopyBufferLayout, - size: crate::Extent3d, - ) { - self.0.write_texture(texture, data, data_layout, size); - } - - fn submit( - &self, - command_buffers: &mut dyn Iterator, - ) -> u64 { - self.0.submit(command_buffers) - } - - fn get_timestamp_period(&self) -> f32 { - self.0.get_timestamp_period() - } - - fn on_submitted_work_done(&self, callback: dispatch::BoxSubmittedWorkDoneCallback) { - self.0.on_submitted_work_done(callback); - } - - #[cfg(any(webgpu, webgl))] - fn copy_external_image_to_texture( - &self, - _source: &wgt::CopyExternalImageSourceInfo, - _dest: wgt::CopyExternalImageDestInfo<&crate::api::Texture>, - _size: crate::Extent3d, - ) { - unimplemented!() - } -} - dyn_type!(pub ref struct DynShaderModule(dyn ShaderModuleInterface)); - -impl ShaderModuleInterface for DynShaderModule { - fn get_compilation_info(&self) -> Pin> { - self.0.get_compilation_info() - } -} - dyn_type!(pub ref struct DynBindGroupLayout(dyn BindGroupLayoutInterface)); - -impl BindGroupLayoutInterface for DynBindGroupLayout {} - dyn_type!(pub ref struct DynBindGroup(dyn BindGroupInterface)); - -impl BindGroupInterface for DynBindGroup {} - dyn_type!(pub ref struct DynTextureView(dyn TextureViewInterface)); - -impl TextureViewInterface for DynTextureView {} - dyn_type!(pub ref struct DynSampler(dyn SamplerInterface)); - -impl SamplerInterface for DynSampler {} - dyn_type!(pub ref struct DynBuffer(dyn BufferInterface)); - -impl BufferInterface for DynBuffer { - fn map_async( - &self, - mode: crate::MapMode, - range: Range, - callback: dispatch::BufferMapCallback, - ) { - self.0.map_async(mode, range, callback); - } - - fn get_mapped_range( - &self, - sub_range: Range, - ) -> dispatch::DispatchBufferMappedRange { - self.0.get_mapped_range(sub_range) - } - - fn unmap(&self) { - self.0.unmap(); - } - - fn destroy(&self) { - self.0.destroy(); - } - - #[cfg(webgpu)] - fn get_mapped_range_as_array_buffer( - &self, - _sub_range: Range, - ) -> Option { - unimplemented!() - } -} - dyn_type!(pub ref struct DynTexture(dyn TextureInterface)); - -impl TextureInterface for DynTexture { - fn create_view( - &self, - desc: &crate::TextureViewDescriptor<'_>, - ) -> dispatch::DispatchTextureView { - self.0.create_view(desc) - } - - fn destroy(&self) { - self.0.destroy(); - } -} - dyn_type!(pub ref struct DynBlas(dyn BlasInterface)); - -impl BlasInterface for DynBlas { - fn destroy(&self) { - self.0.destroy(); - } -} - dyn_type!(pub ref struct DynTlas(dyn TlasInterface)); - -impl TlasInterface for DynTlas { - fn destroy(&self) { - self.0.destroy(); - } -} - dyn_type!(pub ref struct DynQuerySet(dyn QuerySetInterface)); - -impl QuerySetInterface for DynQuerySet {} - dyn_type!(pub ref struct DynPipelineLayout(dyn PipelineLayoutInterface)); - -impl PipelineLayoutInterface for DynPipelineLayout {} - dyn_type!(pub ref struct DynRenderPipeline(dyn RenderPipelineInterface)); - -impl RenderPipelineInterface for DynRenderPipeline { - fn get_bind_group_layout(&self, index: u32) -> DispatchBindGroupLayout { - self.0.get_bind_group_layout(index) - } -} - dyn_type!(pub ref struct DynComputePipeline(dyn ComputePipelineInterface)); - -impl ComputePipelineInterface for DynComputePipeline { - fn get_bind_group_layout(&self, index: u32) -> DispatchBindGroupLayout { - self.0.get_bind_group_layout(index) - } -} - dyn_type!(pub ref struct DynPipelineCache(dyn PipelineCacheInterface)); - -impl PipelineCacheInterface for DynPipelineCache { - fn get_data(&self) -> Option> { - self.0.get_data() - } -} - dyn_type!(pub mut struct DynCommandEncoder(dyn CommandEncoderInterface)); - -impl CommandEncoderInterface for DynCommandEncoder { - fn copy_buffer_to_buffer( - &self, - source: &DispatchBuffer, - source_offset: crate::BufferAddress, - destination: &DispatchBuffer, - destination_offset: crate::BufferAddress, - copy_size: crate::BufferAddress, - ) { - self.0.copy_buffer_to_buffer( - source, - source_offset, - destination, - destination_offset, - copy_size, - ); - } - - fn copy_buffer_to_texture( - &self, - source: crate::TexelCopyBufferInfo<'_>, - destination: crate::TexelCopyTextureInfo<'_>, - copy_size: crate::Extent3d, - ) { - self.0 - .copy_buffer_to_texture(source, destination, copy_size); - } - - fn copy_texture_to_buffer( - &self, - source: crate::TexelCopyTextureInfo<'_>, - destination: crate::TexelCopyBufferInfo<'_>, - copy_size: crate::Extent3d, - ) { - self.0 - .copy_texture_to_buffer(source, destination, copy_size); - } - - fn copy_texture_to_texture( - &self, - source: crate::TexelCopyTextureInfo<'_>, - destination: crate::TexelCopyTextureInfo<'_>, - copy_size: crate::Extent3d, - ) { - self.0 - .copy_texture_to_texture(source, destination, copy_size); - } - - fn begin_compute_pass( - &self, - desc: &crate::ComputePassDescriptor<'_>, - ) -> dispatch::DispatchComputePass { - self.0.begin_compute_pass(desc) - } - - fn begin_render_pass( - &self, - desc: &crate::RenderPassDescriptor<'_>, - ) -> dispatch::DispatchRenderPass { - self.0.begin_render_pass(desc) - } - - fn finish(&mut self) -> DispatchCommandBuffer { - Arc::get_mut(&mut self.0).unwrap().finish() - } - - fn clear_texture( - &self, - texture: &DispatchTexture, - subresource_range: &crate::ImageSubresourceRange, - ) { - self.0.clear_texture(texture, subresource_range); - } - - fn clear_buffer( - &self, - buffer: &DispatchBuffer, - offset: crate::BufferAddress, - size: Option, - ) { - self.0.clear_buffer(buffer, offset, size); - } - - fn insert_debug_marker(&self, label: &str) { - self.0.insert_debug_marker(label); - } - - fn push_debug_group(&self, label: &str) { - self.0.push_debug_group(label); - } - - fn pop_debug_group(&self) { - self.0.pop_debug_group(); - } - - fn write_timestamp(&self, query_set: &DispatchQuerySet, query_index: u32) { - self.0.write_timestamp(query_set, query_index); - } - - fn resolve_query_set( - &self, - query_set: &DispatchQuerySet, - first_query: u32, - query_count: u32, - destination: &DispatchBuffer, - destination_offset: crate::BufferAddress, - ) { - self.0.resolve_query_set( - query_set, - first_query, - query_count, - destination, - destination_offset, - ); - } - - fn build_acceleration_structures_unsafe_tlas<'a>( - &self, - blas: &mut dyn Iterator>, - tlas: &mut dyn Iterator>, - ) { - self.0.build_acceleration_structures_unsafe_tlas(blas, tlas); - } - - fn build_acceleration_structures<'a>( - &self, - blas: &mut dyn Iterator>, - tlas: &mut dyn Iterator, - ) { - self.0.build_acceleration_structures(blas, tlas); - } -} - dyn_type!(pub mut struct DynComputePass(dyn ComputePassInterface)); - -impl ComputePassInterface for DynComputePass { - fn set_pipeline(&mut self, pipeline: &DispatchComputePipeline) { - Arc::get_mut(&mut self.0).unwrap().set_pipeline(pipeline); - } - - fn set_bind_group( - &mut self, - index: u32, - bind_group: Option<&DispatchBindGroup>, - offsets: &[crate::DynamicOffset], - ) { - Arc::get_mut(&mut self.0) - .unwrap() - .set_bind_group(index, bind_group, offsets); - } - - fn set_push_constants(&mut self, offset: u32, data: &[u8]) { - Arc::get_mut(&mut self.0) - .unwrap() - .set_push_constants(offset, data); - } - - fn insert_debug_marker(&mut self, label: &str) { - Arc::get_mut(&mut self.0) - .unwrap() - .insert_debug_marker(label); - } - - fn push_debug_group(&mut self, group_label: &str) { - Arc::get_mut(&mut self.0) - .unwrap() - .push_debug_group(group_label); - } - - fn pop_debug_group(&mut self) { - Arc::get_mut(&mut self.0).unwrap().pop_debug_group(); - } - - fn write_timestamp(&mut self, query_set: &DispatchQuerySet, query_index: u32) { - Arc::get_mut(&mut self.0) - .unwrap() - .write_timestamp(query_set, query_index); - } - - fn begin_pipeline_statistics_query(&mut self, query_set: &DispatchQuerySet, query_index: u32) { - Arc::get_mut(&mut self.0) - .unwrap() - .begin_pipeline_statistics_query(query_set, query_index); - } - - fn end_pipeline_statistics_query(&mut self) { - Arc::get_mut(&mut self.0) - .unwrap() - .end_pipeline_statistics_query(); - } - - fn dispatch_workgroups(&mut self, x: u32, y: u32, z: u32) { - Arc::get_mut(&mut self.0) - .unwrap() - .dispatch_workgroups(x, y, z); - } - - fn dispatch_workgroups_indirect( - &mut self, - indirect_buffer: &DispatchBuffer, - indirect_offset: crate::BufferAddress, - ) { - Arc::get_mut(&mut self.0) - .unwrap() - .dispatch_workgroups_indirect(indirect_buffer, indirect_offset); - } - - fn end(&mut self) { - Arc::get_mut(&mut self.0).unwrap().end() - } -} - dyn_type!(pub mut struct DynRenderPass(dyn RenderPassInterface)); - -impl RenderPassInterface for DynRenderPass { - fn set_pipeline(&mut self, pipeline: &DispatchRenderPipeline) { - Arc::get_mut(&mut self.0).unwrap().set_pipeline(pipeline); - } - - fn set_bind_group( - &mut self, - index: u32, - bind_group: Option<&DispatchBindGroup>, - offsets: &[crate::DynamicOffset], - ) { - Arc::get_mut(&mut self.0) - .unwrap() - .set_bind_group(index, bind_group, offsets); - } - - fn set_index_buffer( - &mut self, - buffer: &DispatchBuffer, - index_format: crate::IndexFormat, - offset: crate::BufferAddress, - size: Option, - ) { - Arc::get_mut(&mut self.0) - .unwrap() - .set_index_buffer(buffer, index_format, offset, size); - } - - fn set_vertex_buffer( - &mut self, - slot: u32, - buffer: &DispatchBuffer, - offset: crate::BufferAddress, - size: Option, - ) { - Arc::get_mut(&mut self.0) - .unwrap() - .set_vertex_buffer(slot, buffer, offset, size); - } - - fn set_push_constants(&mut self, stages: crate::ShaderStages, offset: u32, data: &[u8]) { - Arc::get_mut(&mut self.0) - .unwrap() - .set_push_constants(stages, offset, data); - } - - fn set_blend_constant(&mut self, color: crate::Color) { - Arc::get_mut(&mut self.0).unwrap().set_blend_constant(color); - } - - fn set_scissor_rect(&mut self, x: u32, y: u32, width: u32, height: u32) { - Arc::get_mut(&mut self.0) - .unwrap() - .set_scissor_rect(x, y, width, height); - } - - fn set_viewport( - &mut self, - x: f32, - y: f32, - width: f32, - height: f32, - min_depth: f32, - max_depth: f32, - ) { - Arc::get_mut(&mut self.0) - .unwrap() - .set_viewport(x, y, width, height, min_depth, max_depth); - } - - fn set_stencil_reference(&mut self, reference: u32) { - Arc::get_mut(&mut self.0) - .unwrap() - .set_stencil_reference(reference); - } - - fn draw(&mut self, vertices: Range, instances: Range) { - Arc::get_mut(&mut self.0).unwrap().draw(vertices, instances); - } - - fn draw_indexed(&mut self, indices: Range, base_vertex: i32, instances: Range) { - Arc::get_mut(&mut self.0) - .unwrap() - .draw_indexed(indices, base_vertex, instances); - } - - fn draw_indirect( - &mut self, - indirect_buffer: &DispatchBuffer, - indirect_offset: crate::BufferAddress, - ) { - Arc::get_mut(&mut self.0) - .unwrap() - .draw_indirect(indirect_buffer, indirect_offset); - } - - fn draw_indexed_indirect( - &mut self, - indirect_buffer: &DispatchBuffer, - indirect_offset: crate::BufferAddress, - ) { - Arc::get_mut(&mut self.0) - .unwrap() - .draw_indexed_indirect(indirect_buffer, indirect_offset); - } - - fn multi_draw_indirect( - &mut self, - indirect_buffer: &DispatchBuffer, - indirect_offset: crate::BufferAddress, - count: u32, - ) { - Arc::get_mut(&mut self.0).unwrap().multi_draw_indirect( - indirect_buffer, - indirect_offset, - count, - ); - } - - fn multi_draw_indexed_indirect( - &mut self, - indirect_buffer: &DispatchBuffer, - indirect_offset: crate::BufferAddress, - count: u32, - ) { - Arc::get_mut(&mut self.0) - .unwrap() - .multi_draw_indexed_indirect(indirect_buffer, indirect_offset, count); - } - - fn multi_draw_indirect_count( - &mut self, - indirect_buffer: &DispatchBuffer, - indirect_offset: crate::BufferAddress, - count_buffer: &DispatchBuffer, - count_buffer_offset: crate::BufferAddress, - max_count: u32, - ) { - Arc::get_mut(&mut self.0) - .unwrap() - .multi_draw_indirect_count( - indirect_buffer, - indirect_offset, - count_buffer, - count_buffer_offset, - max_count, - ); - } - - fn multi_draw_indexed_indirect_count( - &mut self, - indirect_buffer: &DispatchBuffer, - indirect_offset: crate::BufferAddress, - count_buffer: &DispatchBuffer, - count_buffer_offset: crate::BufferAddress, - max_count: u32, - ) { - Arc::get_mut(&mut self.0) - .unwrap() - .multi_draw_indexed_indirect_count( - indirect_buffer, - indirect_offset, - count_buffer, - count_buffer_offset, - max_count, - ); - } - - fn insert_debug_marker(&mut self, label: &str) { - Arc::get_mut(&mut self.0) - .unwrap() - .insert_debug_marker(label); - } - - fn push_debug_group(&mut self, group_label: &str) { - Arc::get_mut(&mut self.0) - .unwrap() - .push_debug_group(group_label); - } - - fn pop_debug_group(&mut self) { - Arc::get_mut(&mut self.0).unwrap().pop_debug_group(); - } - - fn write_timestamp(&mut self, query_set: &DispatchQuerySet, query_index: u32) { - Arc::get_mut(&mut self.0) - .unwrap() - .write_timestamp(query_set, query_index); - } - - fn begin_occlusion_query(&mut self, query_index: u32) { - Arc::get_mut(&mut self.0) - .unwrap() - .begin_occlusion_query(query_index); - } - - fn end_occlusion_query(&mut self) { - Arc::get_mut(&mut self.0).unwrap().end_occlusion_query(); - } - - fn begin_pipeline_statistics_query(&mut self, query_set: &DispatchQuerySet, query_index: u32) { - Arc::get_mut(&mut self.0) - .unwrap() - .begin_pipeline_statistics_query(query_set, query_index); - } - - fn end_pipeline_statistics_query(&mut self) { - Arc::get_mut(&mut self.0) - .unwrap() - .end_pipeline_statistics_query(); - } - - fn execute_bundles( - &mut self, - render_bundles: &mut dyn Iterator, - ) { - Arc::get_mut(&mut self.0) - .unwrap() - .execute_bundles(render_bundles); - } - - fn end(&mut self) { - Arc::get_mut(&mut self.0).unwrap().end(); - } -} - dyn_type!(pub ref struct DynCommandBuffer(dyn CommandBufferInterface)); - -impl CommandBufferInterface for DynCommandBuffer {} - dyn_type!(pub mut struct DynRenderBundleEncoder(dyn RenderBundleEncoderInterface)); - -impl RenderBundleEncoderInterface for DynRenderBundleEncoder { - fn set_pipeline(&mut self, pipeline: &DispatchRenderPipeline) { - Arc::get_mut(&mut self.0).unwrap().set_pipeline(pipeline); - } - - fn set_bind_group( - &mut self, - index: u32, - bind_group: Option<&DispatchBindGroup>, - offsets: &[crate::DynamicOffset], - ) { - Arc::get_mut(&mut self.0) - .unwrap() - .set_bind_group(index, bind_group, offsets); - } - - fn set_index_buffer( - &mut self, - buffer: &DispatchBuffer, - index_format: crate::IndexFormat, - offset: crate::BufferAddress, - size: Option, - ) { - Arc::get_mut(&mut self.0) - .unwrap() - .set_index_buffer(buffer, index_format, offset, size); - } - - fn set_vertex_buffer( - &mut self, - slot: u32, - buffer: &DispatchBuffer, - offset: crate::BufferAddress, - size: Option, - ) { - Arc::get_mut(&mut self.0) - .unwrap() - .set_vertex_buffer(slot, buffer, offset, size); - } - - fn set_push_constants(&mut self, stages: crate::ShaderStages, offset: u32, data: &[u8]) { - Arc::get_mut(&mut self.0) - .unwrap() - .set_push_constants(stages, offset, data); - } - - fn draw(&mut self, vertices: Range, instances: Range) { - Arc::get_mut(&mut self.0).unwrap().draw(vertices, instances); - } - - fn draw_indexed(&mut self, indices: Range, base_vertex: i32, instances: Range) { - Arc::get_mut(&mut self.0) - .unwrap() - .draw_indexed(indices, base_vertex, instances); - } - - fn draw_indirect( - &mut self, - indirect_buffer: &DispatchBuffer, - indirect_offset: crate::BufferAddress, - ) { - Arc::get_mut(&mut self.0) - .unwrap() - .draw_indirect(indirect_buffer, indirect_offset); - } - - fn draw_indexed_indirect( - &mut self, - indirect_buffer: &DispatchBuffer, - indirect_offset: crate::BufferAddress, - ) { - Arc::get_mut(&mut self.0) - .unwrap() - .draw_indexed_indirect(indirect_buffer, indirect_offset); - } - - fn finish(self, _desc: &crate::RenderBundleDescriptor<'_>) -> dispatch::DispatchRenderBundle - where - Self: Sized, - { - unimplemented!("RenderBundleEncoderInterface is cannot dyn dispatch finish") - } -} - dyn_type!(pub ref struct DynRenderBundle(dyn RenderBundleInterface)); - -impl RenderBundleInterface for DynRenderBundle {} - dyn_type!(pub ref struct DynSurface(dyn SurfaceInterface)); - -impl SurfaceInterface for DynSurface { - fn get_capabilities(&self, adapter: &dispatch::DispatchAdapter) -> wgt::SurfaceCapabilities { - self.0.get_capabilities(adapter) - } - - fn configure(&self, device: &dispatch::DispatchDevice, config: &crate::SurfaceConfiguration) { - self.0.configure(device, config); - } - - fn get_current_texture( - &self, - ) -> ( - Option, - crate::SurfaceStatus, - dispatch::DispatchSurfaceOutputDetail, - ) { - self.0.get_current_texture() - } -} - dyn_type!(pub ref struct DynSurfaceOutputDetail(dyn SurfaceOutputDetailInterface)); - -impl SurfaceOutputDetailInterface for DynSurfaceOutputDetail { - fn present(&self) { - self.0.present(); - } - - fn texture_discard(&self) { - self.0.texture_discard(); - } -} - dyn_type!(pub mut struct DynQueueWriteBuffer(dyn QueueWriteBufferInterface)); - -impl QueueWriteBufferInterface for DynQueueWriteBuffer { - fn slice(&self) -> &[u8] { - self.0.slice() - } - - fn slice_mut(&mut self) -> &mut [u8] { - Arc::get_mut(&mut self.0).unwrap().slice_mut() - } -} - dyn_type!(pub mut struct DynBufferMappedRange(dyn BufferMappedRangeInterface)); -impl BufferMappedRangeInterface for DynBufferMappedRange { - fn slice(&self) -> &[u8] { - self.0.slice() - } - - fn slice_mut(&mut self) -> &mut [u8] { - Arc::get_mut(&mut self.0).unwrap().slice_mut() - } -} - -impl InterfaceTypes for DynContext { - type Instance = DynContext; +pub(crate) mod interface_types { + use super::*; + pub type Instance = DynContext; - type Adapter = DynAdapter; + pub type Adapter = DynAdapter; - type Device = DynDevice; + pub type Device = DynDevice; - type Queue = DynQueue; + pub type Queue = DynQueue; - type ShaderModule = DynShaderModule; + pub type ShaderModule = DynShaderModule; - type BindGroupLayout = DynBindGroupLayout; + pub type BindGroupLayout = DynBindGroupLayout; - type BindGroup = DynBindGroup; + pub type BindGroup = DynBindGroup; - type TextureView = DynTextureView; + pub type TextureView = DynTextureView; - type Sampler = DynSampler; + pub type Sampler = DynSampler; - type Buffer = DynBuffer; + pub type Buffer = DynBuffer; - type Texture = DynTexture; + pub type Texture = DynTexture; - type Blas = DynBlas; + pub type Blas = DynBlas; - type Tlas = DynTlas; + pub type Tlas = DynTlas; - type QuerySet = DynQuerySet; + pub type QuerySet = DynQuerySet; - type PipelineLayout = DynPipelineLayout; + pub type PipelineLayout = DynPipelineLayout; - type RenderPipeline = DynRenderPipeline; + pub type RenderPipeline = DynRenderPipeline; - type ComputePipeline = DynComputePipeline; + pub type ComputePipeline = DynComputePipeline; - type PipelineCache = DynPipelineCache; + pub type PipelineCache = DynPipelineCache; - type CommandEncoder = DynCommandEncoder; + pub type CommandEncoder = DynCommandEncoder; - type ComputePass = DynComputePass; + pub type ComputePass = DynComputePass; - type RenderPass = DynRenderPass; + pub type RenderPass = DynRenderPass; - type CommandBuffer = DynCommandBuffer; + pub type CommandBuffer = DynCommandBuffer; - type RenderBundleEncoder = DynRenderBundleEncoder; + pub type RenderBundleEncoder = DynRenderBundleEncoder; - type RenderBundle = DynRenderBundle; + pub type RenderBundle = DynRenderBundle; - type Surface = DynSurface; + pub type Surface = DynSurface; - type SurfaceOutputDetail = DynSurfaceOutputDetail; + pub type SurfaceOutputDetail = DynSurfaceOutputDetail; - type QueueWriteBuffer = DynQueueWriteBuffer; + pub type QueueWriteBuffer = DynQueueWriteBuffer; - type BufferMappedRange = DynBufferMappedRange; + pub type BufferMappedRange = DynBufferMappedRange; } diff --git a/wgpu/src/backend/mod.rs b/wgpu/src/backend/mod.rs index e8b705f49b..f2d89ae910 100644 --- a/wgpu/src/backend/mod.rs +++ b/wgpu/src/backend/mod.rs @@ -12,4 +12,3 @@ pub(crate) use wgpu_core::ContextWgpuCore; //#[cfg(custom)] pub mod custom; //#[cfg(custom)] -pub(crate) use custom::DynContext; diff --git a/wgpu/src/backend/webgpu.rs b/wgpu/src/backend/webgpu.rs index 6600a512da..3dbd93e87b 100644 --- a/wgpu/src/backend/webgpu.rs +++ b/wgpu/src/backend/webgpu.rs @@ -1429,35 +1429,36 @@ crate::cmp::impl_eq_ord_hash_proxy!(WebSurfaceOutputDetail => .ident); crate::cmp::impl_eq_ord_hash_proxy!(WebQueueWriteBuffer => .ident); crate::cmp::impl_eq_ord_hash_proxy!(WebBufferMappedRange => .ident); -impl dispatch::InterfaceTypes for ContextWebGpu { - type Instance = ContextWebGpu; - type Adapter = WebAdapter; - type Device = WebDevice; - type Queue = WebQueue; - type ShaderModule = WebShaderModule; - type BindGroupLayout = WebBindGroupLayout; - type BindGroup = WebBindGroup; - type TextureView = WebTextureView; - type Sampler = WebSampler; - type Buffer = WebBuffer; - type Texture = WebTexture; - type Blas = WebBlas; - type Tlas = WebTlas; - type QuerySet = WebQuerySet; - type PipelineLayout = WebPipelineLayout; - type RenderPipeline = WebRenderPipeline; - type ComputePipeline = WebComputePipeline; - type PipelineCache = WebPipelineCache; - type CommandEncoder = WebCommandEncoder; - type ComputePass = WebComputePassEncoder; - type RenderPass = WebRenderPassEncoder; - type CommandBuffer = WebCommandBuffer; - type RenderBundleEncoder = WebRenderBundleEncoder; - type RenderBundle = WebRenderBundle; - type Surface = WebSurface; - type SurfaceOutputDetail = WebSurfaceOutputDetail; - type QueueWriteBuffer = WebQueueWriteBuffer; - type BufferMappedRange = WebBufferMappedRange; +pub(crate) mod interface_types { + use super::*; + pub type Instance = ContextWebGpu; + pub type Adapter = WebAdapter; + pub type Device = WebDevice; + pub type Queue = WebQueue; + pub type ShaderModule = WebShaderModule; + pub type BindGroupLayout = WebBindGroupLayout; + pub type BindGroup = WebBindGroup; + pub type TextureView = WebTextureView; + pub type Sampler = WebSampler; + pub type Buffer = WebBuffer; + pub type Texture = WebTexture; + pub type Blas = WebBlas; + pub type Tlas = WebTlas; + pub type QuerySet = WebQuerySet; + pub type PipelineLayout = WebPipelineLayout; + pub type RenderPipeline = WebRenderPipeline; + pub type ComputePipeline = WebComputePipeline; + pub type PipelineCache = WebPipelineCache; + pub type CommandEncoder = WebCommandEncoder; + pub type ComputePass = WebComputePassEncoder; + pub type RenderPass = WebRenderPassEncoder; + pub type CommandBuffer = WebCommandBuffer; + pub type RenderBundleEncoder = WebRenderBundleEncoder; + pub type RenderBundle = WebRenderBundle; + pub type Surface = WebSurface; + pub type SurfaceOutputDetail = WebSurfaceOutputDetail; + pub type QueueWriteBuffer = WebQueueWriteBuffer; + pub type BufferMappedRange = WebBufferMappedRange; } impl dispatch::InstanceInterface for ContextWebGpu { diff --git a/wgpu/src/backend/wgpu_core.rs b/wgpu/src/backend/wgpu_core.rs index c49c65ab42..0df7ddd55f 100644 --- a/wgpu/src/backend/wgpu_core.rs +++ b/wgpu/src/backend/wgpu_core.rs @@ -1,6 +1,6 @@ use crate::{ api, - dispatch::{self, BufferMappedRangeInterface, InterfaceTypes}, + dispatch::{self, BufferMappedRangeInterface}, BindingResource, BufferBinding, BufferDescriptor, CompilationInfo, CompilationMessage, CompilationMessageType, ErrorSource, Features, Label, LoadOp, MapMode, Operations, ShaderSource, SurfaceTargetUnsafe, TextureDescriptor, @@ -735,35 +735,36 @@ crate::cmp::impl_eq_ord_hash_proxy!(CoreSurfaceOutputDetail => .surface_id); crate::cmp::impl_eq_ord_hash_proxy!(CoreQueueWriteBuffer => .mapping.ptr); crate::cmp::impl_eq_ord_hash_proxy!(CoreBufferMappedRange => .ptr); -impl InterfaceTypes for ContextWgpuCore { - type Instance = ContextWgpuCore; - type Adapter = CoreAdapter; - type Device = CoreDevice; - type Queue = CoreQueue; - type ShaderModule = CoreShaderModule; - type BindGroupLayout = CoreBindGroupLayout; - type BindGroup = CoreBindGroup; - type TextureView = CoreTextureView; - type Sampler = CoreSampler; - type Buffer = CoreBuffer; - type Texture = CoreTexture; - type Blas = CoreBlas; - type Tlas = CoreTlas; - type QuerySet = CoreQuerySet; - type PipelineLayout = CorePipelineLayout; - type RenderPipeline = CoreRenderPipeline; - type ComputePipeline = CoreComputePipeline; - type PipelineCache = CorePipelineCache; - type CommandEncoder = CoreCommandEncoder; - type ComputePass = CoreComputePass; - type RenderPass = CoreRenderPass; - type CommandBuffer = CoreCommandBuffer; - type RenderBundleEncoder = CoreRenderBundleEncoder; - type RenderBundle = CoreRenderBundle; - type Surface = CoreSurface; - type SurfaceOutputDetail = CoreSurfaceOutputDetail; - type QueueWriteBuffer = CoreQueueWriteBuffer; - type BufferMappedRange = CoreBufferMappedRange; +pub(crate) mod interface_types { + use super::*; + pub type Instance = ContextWgpuCore; + pub type Adapter = CoreAdapter; + pub type Device = CoreDevice; + pub type Queue = CoreQueue; + pub type ShaderModule = CoreShaderModule; + pub type BindGroupLayout = CoreBindGroupLayout; + pub type BindGroup = CoreBindGroup; + pub type TextureView = CoreTextureView; + pub type Sampler = CoreSampler; + pub type Buffer = CoreBuffer; + pub type Texture = CoreTexture; + pub type Blas = CoreBlas; + pub type Tlas = CoreTlas; + pub type QuerySet = CoreQuerySet; + pub type PipelineLayout = CorePipelineLayout; + pub type RenderPipeline = CoreRenderPipeline; + pub type ComputePipeline = CoreComputePipeline; + pub type PipelineCache = CorePipelineCache; + pub type CommandEncoder = CoreCommandEncoder; + pub type ComputePass = CoreComputePass; + pub type RenderPass = CoreRenderPass; + pub type CommandBuffer = CoreCommandBuffer; + pub type RenderBundleEncoder = CoreRenderBundleEncoder; + pub type RenderBundle = CoreRenderBundle; + pub type Surface = CoreSurface; + pub type SurfaceOutputDetail = CoreSurfaceOutputDetail; + pub type QueueWriteBuffer = CoreQueueWriteBuffer; + pub type BufferMappedRange = CoreBufferMappedRange; } impl dispatch::InstanceInterface for ContextWgpuCore { diff --git a/wgpu/src/dispatch.rs b/wgpu/src/dispatch.rs index 81937a58b1..cbf4232a5a 100644 --- a/wgpu/src/dispatch.rs +++ b/wgpu/src/dispatch.rs @@ -49,40 +49,6 @@ pub type BufferMapCallback = Box) // Common traits on all the interface traits trait_alias!(CommonTraits: Any + Debug + WasmNotSendSync); -// Non-object-safe traits that are added as a bound on InterfaceTypes. -trait_alias!(ComparisonTraits: PartialEq + Eq + PartialOrd + Ord + Hash); - -/// Types that represent a "Backend" for the wgpu API. -pub trait InterfaceTypes { - type Instance: InstanceInterface + ComparisonTraits; - type Adapter: AdapterInterface + ComparisonTraits; - type Device: DeviceInterface + ComparisonTraits; - type Queue: QueueInterface + ComparisonTraits; - type ShaderModule: ShaderModuleInterface + ComparisonTraits; - type BindGroupLayout: BindGroupLayoutInterface + ComparisonTraits; - type BindGroup: BindGroupInterface + ComparisonTraits; - type TextureView: TextureViewInterface + ComparisonTraits; - type Sampler: SamplerInterface + ComparisonTraits; - type Buffer: BufferInterface + ComparisonTraits; - type Texture: TextureInterface + ComparisonTraits; - type Blas: BlasInterface + ComparisonTraits; - type Tlas: TlasInterface + ComparisonTraits; - type QuerySet: QuerySetInterface + ComparisonTraits; - type PipelineLayout: PipelineLayoutInterface + ComparisonTraits; - type RenderPipeline: RenderPipelineInterface + ComparisonTraits; - type ComputePipeline: ComputePipelineInterface + ComparisonTraits; - type PipelineCache: PipelineCacheInterface + ComparisonTraits; - type CommandEncoder: CommandEncoderInterface + ComparisonTraits; - type ComputePass: ComputePassInterface + ComparisonTraits; - type RenderPass: RenderPassInterface + ComparisonTraits; - type CommandBuffer: CommandBufferInterface + ComparisonTraits; - type RenderBundleEncoder: RenderBundleEncoderInterface + ComparisonTraits; - type RenderBundle: RenderBundleInterface + ComparisonTraits; - type Surface: SurfaceInterface + ComparisonTraits; - type SurfaceOutputDetail: SurfaceOutputDetailInterface + ComparisonTraits; - type QueueWriteBuffer: QueueWriteBufferInterface + ComparisonTraits; - type BufferMappedRange: BufferMappedRangeInterface + ComparisonTraits; -} pub trait InstanceInterface: CommonTraits { fn new(desc: &wgt::InstanceDescriptor) -> Self @@ -557,26 +523,23 @@ pub trait BufferMappedRangeInterface: CommonTraits { /// In the future, we may want a truly generic backend, which could be extended from this enum. macro_rules! dispatch_types_inner { ( - wgpu_core = $wgpu_core_context:ty; - webgpu = $webgpu_context:ty; - custom = $custom_context:ty; - {ref type $name:ident = InterfaceTypes::$subtype:ident: $trait:ident}; + {ref type $name:ident = $subtype:ident: $trait:ident}; ) => { #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)] pub enum $name { #[cfg(wgpu_core)] - Core(Arc<<$wgpu_core_context as InterfaceTypes>::$subtype>), + Core(Arc), #[cfg(webgpu)] - WebGPU(Arc<<$webgpu_context as InterfaceTypes>::$subtype>), + WebGPU(Arc), //#[cfg(custom)] - Custom(<$custom_context as InterfaceTypes>::$subtype), + Custom(backend::custom::interface_types::$subtype), } impl $name { #[cfg(wgpu_core)] #[inline] #[allow(unused)] - pub fn as_core(&self) -> &<$wgpu_core_context as InterfaceTypes>::$subtype { + pub fn as_core(&self) -> &backend::wgpu_core::interface_types::$subtype { match self { Self::Core(value) => value, _ => panic!(concat!(stringify!($name), " is not core")), @@ -586,7 +549,7 @@ macro_rules! dispatch_types_inner { #[cfg(wgpu_core)] #[inline] #[allow(unused)] - pub fn as_core_opt(&self) -> Option<&<$wgpu_core_context as InterfaceTypes>::$subtype> { + pub fn as_core_opt(&self) -> Option<&backend::wgpu_core::interface_types::$subtype> { match self { Self::Core(value) => Some(value), _ => None, @@ -596,7 +559,7 @@ macro_rules! dispatch_types_inner { #[cfg(webgpu)] #[inline] #[allow(unused)] - pub fn as_webgpu(&self) -> &<$webgpu_context as InterfaceTypes>::$subtype { + pub fn as_webgpu(&self) -> &backend::webgpu::interface_types::$subtype { match self { Self::WebGPU(value) => value, _ => panic!(concat!(stringify!($name), " is not webgpu")), @@ -606,7 +569,7 @@ macro_rules! dispatch_types_inner { #[cfg(webgpu)] #[inline] #[allow(unused)] - pub fn as_webgpu_opt(&self) -> Option<&<$webgpu_context as InterfaceTypes>::$subtype> { + pub fn as_webgpu_opt(&self) -> Option<&backend::webgpu::interface_types::$subtype> { match self { Self::WebGPU(value) => Some(value), _ => None, @@ -615,17 +578,17 @@ macro_rules! dispatch_types_inner { } #[cfg(wgpu_core)] - impl From<<$wgpu_core_context as InterfaceTypes>::$subtype> for $name { + impl From for $name { #[inline] - fn from(value: <$wgpu_core_context as InterfaceTypes>::$subtype) -> Self { + fn from(value: backend::wgpu_core::interface_types::$subtype) -> Self { Self::Core(Arc::new(value)) } } #[cfg(webgpu)] - impl From<<$webgpu_context as InterfaceTypes>::$subtype> for $name { + impl From for $name { #[inline] - fn from(value: <$webgpu_context as InterfaceTypes>::$subtype) -> Self { + fn from(value: backend::webgpu::interface_types::$subtype) -> Self { Self::WebGPU(Arc::new(value)) } } @@ -641,32 +604,29 @@ macro_rules! dispatch_types_inner { #[cfg(webgpu)] Self::WebGPU(value) => value.as_ref(), //#[cfg(webgpu)] - Self::Custom(value) => value, + Self::Custom(value) => value.deref(), } } } }; ( - wgpu_core = $wgpu_core_context:ty; - webgpu = $webgpu_context:ty; - custom = $custom_context:ty; - {mut type $name:ident = InterfaceTypes::$subtype:ident: $trait:ident}; + {mut type $name:ident = $subtype:ident: $trait:ident}; ) => { #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum $name { #[cfg(wgpu_core)] - Core(<$wgpu_core_context as InterfaceTypes>::$subtype), + Core(backend::wgpu_core::interface_types::$subtype), #[cfg(webgpu)] - WebGPU(<$webgpu_context as InterfaceTypes>::$subtype), + WebGPU(backend::webgpu::interface_types::$subtype), //#[cfg(custom)] - Custom(<$custom_context as InterfaceTypes>::$subtype), + Custom(backend::custom::interface_types::$subtype), } impl $name { #[cfg(wgpu_core)] #[inline] #[allow(unused)] - pub fn as_core(&self) -> &<$wgpu_core_context as InterfaceTypes>::$subtype { + pub fn as_core(&self) -> &backend::wgpu_core::interface_types::$subtype { match self { Self::Core(value) => value, _ => panic!(concat!(stringify!($name), " is not core")), @@ -676,7 +636,7 @@ macro_rules! dispatch_types_inner { #[cfg(wgpu_core)] #[inline] #[allow(unused)] - pub fn as_core_mut(&mut self) -> &mut <$wgpu_core_context as InterfaceTypes>::$subtype { + pub fn as_core_mut(&mut self) -> &mut backend::wgpu_core::interface_types::$subtype { match self { Self::Core(value) => value, _ => panic!(concat!(stringify!($name), " is not core")), @@ -686,7 +646,7 @@ macro_rules! dispatch_types_inner { #[cfg(wgpu_core)] #[inline] #[allow(unused)] - pub fn as_core_opt(&self) -> Option<&<$wgpu_core_context as InterfaceTypes>::$subtype> { + pub fn as_core_opt(&self) -> Option<&backend::wgpu_core::interface_types::$subtype> { match self { Self::Core(value) => Some(value), _ => None, @@ -698,7 +658,7 @@ macro_rules! dispatch_types_inner { #[allow(unused)] pub fn as_core_mut_opt( &mut self, - ) -> Option<&mut <$wgpu_core_context as InterfaceTypes>::$subtype> { + ) -> Option<&mut backend::wgpu_core::interface_types::$subtype> { match self { Self::Core(value) => Some(value), _ => None, @@ -708,7 +668,7 @@ macro_rules! dispatch_types_inner { #[cfg(webgpu)] #[inline] #[allow(unused)] - pub fn as_webgpu(&self) -> &<$webgpu_context as InterfaceTypes>::$subtype { + pub fn as_webgpu(&self) -> &backend::webgpu::interface_types::$subtype { match self { Self::WebGPU(value) => value, _ => panic!(concat!(stringify!($name), " is not webgpu")), @@ -718,7 +678,7 @@ macro_rules! dispatch_types_inner { #[cfg(webgpu)] #[inline] #[allow(unused)] - pub fn as_webgpu_mut(&mut self) -> &mut <$webgpu_context as InterfaceTypes>::$subtype { + pub fn as_webgpu_mut(&mut self) -> &mut backend::webgpu::interface_types::$subtype { match self { Self::WebGPU(value) => value, _ => panic!(concat!(stringify!($name), " is not webgpu")), @@ -728,7 +688,7 @@ macro_rules! dispatch_types_inner { #[cfg(webgpu)] #[inline] #[allow(unused)] - pub fn as_webgpu_opt(&self) -> Option<&<$webgpu_context as InterfaceTypes>::$subtype> { + pub fn as_webgpu_opt(&self) -> Option<&backend::webgpu::interface_types::$subtype> { match self { Self::WebGPU(value) => Some(value), _ => None, @@ -740,7 +700,7 @@ macro_rules! dispatch_types_inner { #[allow(unused)] pub fn as_webgpu_mut_opt( &mut self, - ) -> Option<&mut <$webgpu_context as InterfaceTypes>::$subtype> { + ) -> Option<&mut backend::webgpu::interface_types::$subtype> { match self { Self::WebGPU(value) => Some(value), _ => None, @@ -749,17 +709,17 @@ macro_rules! dispatch_types_inner { } #[cfg(wgpu_core)] - impl From<<$wgpu_core_context as InterfaceTypes>::$subtype> for $name { + impl From for $name { #[inline] - fn from(value: <$wgpu_core_context as InterfaceTypes>::$subtype) -> Self { + fn from(value: backend::wgpu_core::interface_types::$subtype) -> Self { Self::Core(value) } } #[cfg(webgpu)] - impl From<<$webgpu_context as InterfaceTypes>::$subtype> for $name { + impl From for $name { #[inline] - fn from(value: <$webgpu_context as InterfaceTypes>::$subtype) -> Self { + fn from(value: backend::webgpu::interface_types::$subtype) -> Self { Self::WebGPU(value) } } @@ -775,7 +735,7 @@ macro_rules! dispatch_types_inner { #[cfg(webgpu)] Self::WebGPU(value) => value, //#[cfg(custom)] - Self::Custom(value) => value, + Self::Custom(value) => value.deref(), } } } @@ -789,65 +749,150 @@ macro_rules! dispatch_types_inner { #[cfg(webgpu)] Self::WebGPU(value) => value, //#[cfg(custom)] - Self::Custom(value) => value, + Self::Custom(value) => value.deref_mut(), } } } }; } -macro_rules! dispatch_types { - ( - wgpu_core = $wgpu_core_context:ty; - webgpu = $webgpu_context:ty; - custom = $custom_context:ty; - {$( - $type:tt; - )*} - ) => { - $( - dispatch_types_inner!{ - wgpu_core = $wgpu_core_context; - webgpu = $webgpu_context; - custom = $custom_context; - $type; - } - )* +dispatch_types_inner! { + { + ref type DispatchInstance = Instance:InstanceInterface }; } - -dispatch_types! { - wgpu_core = backend::ContextWgpuCore; - webgpu = backend::ContextWebGpu; - custom = backend::DynContext; +dispatch_types_inner! { + { + ref type DispatchAdapter = Adapter:AdapterInterface + }; +} +dispatch_types_inner! { + { + ref type DispatchDevice = Device:DeviceInterface + }; +} +dispatch_types_inner! { + { + ref type DispatchQueue = Queue:QueueInterface + }; +} +dispatch_types_inner! { { - {ref type DispatchInstance = InterfaceTypes::Instance: InstanceInterface}; - {ref type DispatchAdapter = InterfaceTypes::Adapter: AdapterInterface}; - {ref type DispatchDevice = InterfaceTypes::Device: DeviceInterface}; - {ref type DispatchQueue = InterfaceTypes::Queue: QueueInterface}; - {ref type DispatchShaderModule = InterfaceTypes::ShaderModule: ShaderModuleInterface}; - {ref type DispatchBindGroupLayout = InterfaceTypes::BindGroupLayout: BindGroupLayoutInterface}; - {ref type DispatchBindGroup = InterfaceTypes::BindGroup: BindGroupInterface}; - {ref type DispatchTextureView = InterfaceTypes::TextureView: TextureViewInterface}; - {ref type DispatchSampler = InterfaceTypes::Sampler: SamplerInterface}; - {ref type DispatchBuffer = InterfaceTypes::Buffer: BufferInterface}; - {ref type DispatchTexture = InterfaceTypes::Texture: TextureInterface}; - {ref type DispatchBlas = InterfaceTypes::Blas: BlasInterface}; - {ref type DispatchTlas = InterfaceTypes::Tlas: TlasInterface}; - {ref type DispatchQuerySet = InterfaceTypes::QuerySet: QuerySetInterface}; - {ref type DispatchPipelineLayout = InterfaceTypes::PipelineLayout: PipelineLayoutInterface}; - {ref type DispatchRenderPipeline = InterfaceTypes::RenderPipeline: RenderPipelineInterface}; - {ref type DispatchComputePipeline = InterfaceTypes::ComputePipeline: ComputePipelineInterface}; - {ref type DispatchPipelineCache = InterfaceTypes::PipelineCache: PipelineCacheInterface}; - {mut type DispatchCommandEncoder = InterfaceTypes::CommandEncoder: CommandEncoderInterface}; - {mut type DispatchComputePass = InterfaceTypes::ComputePass: ComputePassInterface}; - {mut type DispatchRenderPass = InterfaceTypes::RenderPass: RenderPassInterface}; - {ref type DispatchCommandBuffer = InterfaceTypes::CommandBuffer: CommandBufferInterface}; - {mut type DispatchRenderBundleEncoder = InterfaceTypes::RenderBundleEncoder: RenderBundleEncoderInterface}; - {ref type DispatchRenderBundle = InterfaceTypes::RenderBundle: RenderBundleInterface}; - {ref type DispatchSurface = InterfaceTypes::Surface: SurfaceInterface}; - {ref type DispatchSurfaceOutputDetail = InterfaceTypes::SurfaceOutputDetail: SurfaceOutputDetailInterface}; - {mut type DispatchQueueWriteBuffer = InterfaceTypes::QueueWriteBuffer: QueueWriteBufferInterface}; - {mut type DispatchBufferMappedRange = InterfaceTypes::BufferMappedRange: BufferMappedRangeInterface}; - } + ref type DispatchShaderModule = ShaderModule:ShaderModuleInterface + }; +} +dispatch_types_inner! { + { + ref type DispatchBindGroupLayout = BindGroupLayout:BindGroupLayoutInterface + }; +} +dispatch_types_inner! { + { + ref type DispatchBindGroup = BindGroup:BindGroupInterface + }; +} +dispatch_types_inner! { + { + ref type DispatchTextureView = TextureView:TextureViewInterface + }; +} +dispatch_types_inner! { + { + ref type DispatchSampler = Sampler:SamplerInterface + }; +} +dispatch_types_inner! { + { + ref type DispatchBuffer = Buffer:BufferInterface + }; +} +dispatch_types_inner! { + { + ref type DispatchTexture = Texture:TextureInterface + }; +} +dispatch_types_inner! { + { + ref type DispatchBlas = Blas:BlasInterface + }; +} +dispatch_types_inner! { + { + ref type DispatchTlas = Tlas:TlasInterface + }; +} +dispatch_types_inner! { + { + ref type DispatchQuerySet = QuerySet:QuerySetInterface + }; +} +dispatch_types_inner! { + { + ref type DispatchPipelineLayout = PipelineLayout:PipelineLayoutInterface + }; +} +dispatch_types_inner! { + { + ref type DispatchRenderPipeline = RenderPipeline:RenderPipelineInterface + }; +} +dispatch_types_inner! { + { + ref type DispatchComputePipeline = ComputePipeline:ComputePipelineInterface + }; +} +dispatch_types_inner! { + { + ref type DispatchPipelineCache = PipelineCache:PipelineCacheInterface + }; +} +dispatch_types_inner! { + { + mut type DispatchCommandEncoder = CommandEncoder:CommandEncoderInterface + }; +} +dispatch_types_inner! { + { + mut type DispatchComputePass = ComputePass:ComputePassInterface + }; +} +dispatch_types_inner! { + { + mut type DispatchRenderPass = RenderPass:RenderPassInterface + }; +} +dispatch_types_inner! { + { + ref type DispatchCommandBuffer = CommandBuffer:CommandBufferInterface + }; +} +dispatch_types_inner! { + { + mut type DispatchRenderBundleEncoder = RenderBundleEncoder:RenderBundleEncoderInterface + }; +} +dispatch_types_inner! { + { + ref type DispatchRenderBundle = RenderBundle:RenderBundleInterface + }; +} +dispatch_types_inner! { + { + ref type DispatchSurface = Surface:SurfaceInterface + }; +} +dispatch_types_inner! { + { + ref type DispatchSurfaceOutputDetail = SurfaceOutputDetail:SurfaceOutputDetailInterface + }; +} +dispatch_types_inner! { + { + mut type DispatchQueueWriteBuffer = QueueWriteBuffer:QueueWriteBufferInterface + }; +} +dispatch_types_inner! { + { + mut type DispatchBufferMappedRange = BufferMappedRange:BufferMappedRangeInterface + }; }