diff options
Diffstat (limited to '')
-rw-r--r-- | third_party/rust/wgpu-core/src/resource.rs | 74 |
1 files changed, 62 insertions, 12 deletions
diff --git a/third_party/rust/wgpu-core/src/resource.rs b/third_party/rust/wgpu-core/src/resource.rs index aca077caab..d3cd2968bf 100644 --- a/third_party/rust/wgpu-core/src/resource.rs +++ b/third_party/rust/wgpu-core/src/resource.rs @@ -8,8 +8,12 @@ use crate::{ }, global::Global, hal_api::HalApi, - id::{AdapterId, BufferId, DeviceId, Id, Marker, SurfaceId, TextureId}, + id::{ + AdapterId, BufferId, CommandEncoderId, DeviceId, Id, Marker, SurfaceId, TextureId, + TextureViewId, + }, init_tracker::{BufferInitTracker, TextureInitTracker}, + lock::{Mutex, RwLock}, resource, resource_log, snatch::{ExclusiveSnatchGuard, SnatchGuard, Snatchable}, track::{SharedTrackerIndexAllocator, TextureSelector, TrackerIndex}, @@ -18,7 +22,6 @@ use crate::{ }; use hal::CommandEncoder; -use parking_lot::{Mutex, RwLock}; use smallvec::SmallVec; use thiserror::Error; use wgt::WasmNotSendSync; @@ -55,7 +58,7 @@ use std::{ /// [`Device`]: crate::device::resource::Device /// [`Buffer`]: crate::resource::Buffer #[derive(Debug)] -pub struct ResourceInfo<T: Resource> { +pub(crate) struct ResourceInfo<T: Resource> { id: Option<Id<T::Marker>>, tracker_index: TrackerIndex, tracker_indices: Option<Arc<SharedTrackerIndexAllocator>>, @@ -141,7 +144,7 @@ impl<T: Resource> ResourceInfo<T> { pub(crate) type ResourceType = &'static str; -pub trait Resource: 'static + Sized + WasmNotSendSync { +pub(crate) trait Resource: 'static + Sized + WasmNotSendSync { type Marker: Marker; const TYPE: ResourceType; fn as_info(&self) -> &ResourceInfo<Self>; @@ -370,10 +373,10 @@ pub type BufferAccessResult = Result<(), BufferAccessError>; #[derive(Debug)] pub(crate) struct BufferPendingMapping<A: HalApi> { - pub range: Range<wgt::BufferAddress>, - pub op: BufferMapOperation, + pub(crate) range: Range<wgt::BufferAddress>, + pub(crate) op: BufferMapOperation, // hold the parent alive while the mapping is active - pub _parent_buffer: Arc<Buffer<A>>, + pub(crate) _parent_buffer: Arc<Buffer<A>>, } pub type BufferDescriptor<'a> = wgt::BufferDescriptor<Label<'a>>; @@ -734,7 +737,7 @@ pub(crate) enum TextureInner<A: HalApi> { } impl<A: HalApi> TextureInner<A> { - pub fn raw(&self) -> Option<&A::Texture> { + pub(crate) fn raw(&self) -> Option<&A::Texture> { match self { Self::Native { raw } => Some(raw), Self::Surface { raw: Some(tex), .. } => Some(tex.borrow()), @@ -924,11 +927,11 @@ impl Global { /// # Safety /// /// - The raw texture handle must not be manually destroyed - pub unsafe fn texture_as_hal<A: HalApi, F: FnOnce(Option<&A::Texture>)>( + pub unsafe fn texture_as_hal<A: HalApi, F: FnOnce(Option<&A::Texture>) -> R, R>( &self, id: TextureId, hal_texture_callback: F, - ) { + ) -> R { profiling::scope!("Texture::as_hal"); let hub = A::hub(self); @@ -937,7 +940,26 @@ impl Global { let snatch_guard = texture.device.snatchable_lock.read(); let hal_texture = texture.raw(&snatch_guard); - hal_texture_callback(hal_texture); + hal_texture_callback(hal_texture) + } + + /// # Safety + /// + /// - The raw texture view handle must not be manually destroyed + pub unsafe fn texture_view_as_hal<A: HalApi, F: FnOnce(Option<&A::TextureView>) -> R, R>( + &self, + id: TextureViewId, + hal_texture_view_callback: F, + ) -> R { + profiling::scope!("TextureView::as_hal"); + + let hub = A::hub(self); + let texture_view_opt = { hub.texture_views.try_get(id).ok().flatten() }; + let texture_view = texture_view_opt.as_ref().unwrap(); + let snatch_guard = texture_view.device.snatchable_lock.read(); + let hal_texture_view = texture_view.raw(&snatch_guard); + + hal_texture_view_callback(hal_texture_view) } /// # Safety @@ -1001,10 +1023,38 @@ impl Global { profiling::scope!("Surface::as_hal"); let surface = self.surfaces.get(id).ok(); - let hal_surface = surface.as_ref().and_then(|surface| A::get_surface(surface)); + let hal_surface = surface + .as_ref() + .and_then(|surface| A::surface_as_hal(surface)); hal_surface_callback(hal_surface) } + + /// # Safety + /// + /// - The raw command encoder handle must not be manually destroyed + pub unsafe fn command_encoder_as_hal_mut< + A: HalApi, + F: FnOnce(Option<&mut A::CommandEncoder>) -> R, + R, + >( + &self, + id: CommandEncoderId, + hal_command_encoder_callback: F, + ) -> R { + profiling::scope!("CommandEncoder::as_hal"); + + let hub = A::hub(self); + let cmd_buf = hub + .command_buffers + .get(id.into_command_buffer_id()) + .unwrap(); + let mut cmd_buf_data = cmd_buf.data.lock(); + let cmd_buf_data = cmd_buf_data.as_mut().unwrap(); + let cmd_buf_raw = cmd_buf_data.encoder.open().ok(); + + hal_command_encoder_callback(cmd_buf_raw) + } } /// A texture that has been marked as destroyed and is staged for actual deletion soon. |