From 5eb6f190438746dacd22b509d1b4873738eb56f9 Mon Sep 17 00:00:00 2001 From: Nicolas Silva Date: Mon, 5 Feb 2024 15:24:40 +0100 Subject: [PATCH] WIP --- wgpu-hal/src/lib.rs | 94 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 86 insertions(+), 8 deletions(-) diff --git a/wgpu-hal/src/lib.rs b/wgpu-hal/src/lib.rs index 561024dd98..bfef581d10 100644 --- a/wgpu-hal/src/lib.rs +++ b/wgpu-hal/src/lib.rs @@ -80,12 +80,7 @@ pub mod api { } use std::{ - borrow::{Borrow, Cow}, - fmt, - num::NonZeroU32, - ops::{Range, RangeInclusive}, - ptr::NonNull, - sync::Arc, + any::Any, borrow::{Borrow, Cow}, fmt, num::NonZeroU32, ops::{Range, RangeInclusive}, ptr::NonNull, sync::Arc }; use bitflags::bitflags; @@ -293,7 +288,9 @@ pub trait Adapter: WasmNotSendSync { unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp; } -pub trait Device: WasmNotSendSync { +pub trait Device: WasmNotSendSync { + type Api: Api; + /// Exit connection to this logical device. unsafe fn exit(self, queue: A::Queue); /// Creates a new buffer. @@ -304,7 +301,7 @@ pub trait Device: WasmNotSendSync { //TODO: clarify if zero-sized mapping is allowed unsafe fn map_buffer( &self, - buffer: &A::Buffer, + buffer: &Self::Api::Buffer, range: MemoryRange, ) -> Result; unsafe fn unmap_buffer(&self, buffer: &A::Buffer) -> Result<(), DeviceError>; @@ -1608,3 +1605,84 @@ bitflags::bitflags! { pub struct AccelerationStructureBarrier { pub usage: Range, } + +pub trait Resource: WasmNotSendSync + 'static { + fn as_any(&self) -> &dyn Any; +} + +pub trait DynInstance: Resource {} +pub trait DynQueue: Resource {} +pub trait DynAdapater: Resource {} +pub trait DynSurface: Resource {} +pub trait DynCommandEncoder: Resource {} +pub trait DynBuffer: Resource {} +pub trait DynTexture: Resource {} +pub trait DynTextureView: Resource {} +pub trait DynSurfaceTexture: Resource {} +pub trait DynBindGroup: Resource {} +pub trait DynBindGroupLayout: Resource {} +pub trait DynPipelineLayout: Resource {} +pub trait DynShaderModule: Resource {} +pub trait DynRenderPipeline: Resource {} +pub trait DynComputePipeline: Resource {} +pub trait DynCommandBuffer: Resource {} +pub trait DynSampler: Resource {} +pub trait DynQuerySet: Resource {} +pub trait DynFence: Resource {} +pub trait DynAccelerationStructure: Resource {} + +pub fn downcast(res: &dyn Resource) -> &T { + res.as_any().downcast_ref().unwrap() +} + +pub trait DynDevice: Resource { + unsafe fn map_buffer( + &self, + buffer: &dyn DynBuffer, + range: MemoryRange, + ) -> Result; + unsafe fn unmap_buffer(&self, buffer: &dyn DynBuffer) -> Result<(), DeviceError>; + unsafe fn flush_mapped_ranges(&self, buffer: &dyn DynBuffer, ranges: &[MemoryRange]); + unsafe fn invalidate_mapped_ranges(&self, buffer: &dyn DynBuffer, ranges: &[MemoryRange]); + unsafe fn start_capture(&self) -> bool; + unsafe fn stop_capture(&self); + unsafe fn get_fence_value(&self, fence: &dyn DynFence) -> Result; + unsafe fn wait(&self, fence: &dyn DynFence, value: FenceValue, timeout_ms: u32) -> Result; +} + +impl DynDevice for T { + unsafe fn map_buffer( + &self, + buffer: &dyn DynBuffer, + range: MemoryRange, + ) -> Result { + let buffer: &A::Buffer = downcast(buffer); + self.map_buffer(buffer, range) + } + unsafe fn unmap_buffer(&self, buffer: &dyn DynBuffer) -> Result<(), DeviceError> { + let buffer: &A::Buffer = downcast(buffer); + self.unmap_buffer(buffer) + } + unsafe fn flush_mapped_ranges(&self, buffer: &dyn DynBuffer, ranges: &[MemoryRange]) { + let buffer: &A::Buffer = downcast(buffer); + self.flush_mapped_ranges(buffer, ranges.iter().cloned()) + } + unsafe fn invalidate_mapped_ranges(&self, buffer: &dyn DynBuffer, ranges: &[MemoryRange]) { + let buffer: &A::Buffer = downcast(buffer); + self.invalidate_mapped_ranges(buffer, ranges.iter().cloned()) + } + unsafe fn start_capture(&self) -> bool { + self.start_capture() + } + unsafe fn stop_capture(&self) { + self.start_capture() + } + unsafe fn get_fence_value(&self, fence: &dyn DynFence) -> Result { + let fence: &A::Fence = downcast(fence); + self.get_fence_value(fence) + } + unsafe fn wait(&self, fence: &dyn DynFence, value: FenceValue, timeout_ms: u32) -> Result { + let fence: &A::Fence = downcast(fence); + self.wait(fence, value, timeout_ms) + } +} \ No newline at end of file