From ce9c9b76f63b90c94e982d1b1d6195fc2aa502da Mon Sep 17 00:00:00 2001 From: teoxoy <28601907+teoxoy@users.noreply.github.com> Date: Wed, 7 Aug 2024 15:15:32 +0200 Subject: [PATCH] remove `Option` around `A::SurfaceTexture` We can rely on the snatching mechanism to take the surface texture. --- wgpu-core/src/command/mod.rs | 2 +- wgpu-core/src/device/queue.rs | 29 +++++++++++++---------------- wgpu-core/src/present.rs | 22 ++++++++-------------- wgpu-core/src/resource.rs | 21 +++++++-------------- wgpu-core/src/snatch.rs | 5 ----- 5 files changed, 29 insertions(+), 50 deletions(-) diff --git a/wgpu-core/src/command/mod.rs b/wgpu-core/src/command/mod.rs index 7d4d86673f..7314e8f04c 100644 --- a/wgpu-core/src/command/mod.rs +++ b/wgpu-core/src/command/mod.rs @@ -409,7 +409,7 @@ impl CommandBuffer { let texture_barriers = transitions .into_iter() .enumerate() - .map(|(i, p)| p.into_hal(textures[i].unwrap().raw().unwrap())); + .map(|(i, p)| p.into_hal(textures[i].unwrap().raw())); unsafe { raw.transition_buffers(buffer_barriers); diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index 8c076644f9..27f13e2f46 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -1143,12 +1143,10 @@ impl Global { for texture in cmd_buf_trackers.textures.used_resources() { let should_extend = match texture.try_inner(&snatch_guard)? { TextureInner::Native { .. } => false, - TextureInner::Surface { ref raw, .. } => { - if raw.is_some() { - // Compare the Arcs by pointer as Textures don't implement Eq. - submit_surface_textures_owned - .insert(Arc::as_ptr(&texture), texture.clone()); - } + TextureInner::Surface { .. } => { + // Compare the Arcs by pointer as Textures don't implement Eq. + submit_surface_textures_owned + .insert(Arc::as_ptr(&texture), texture.clone()); true } @@ -1242,12 +1240,10 @@ impl Global { for texture in pending_writes.dst_textures.values() { match texture.try_inner(&snatch_guard)? { TextureInner::Native { .. } => {} - TextureInner::Surface { ref raw, .. } => { - if raw.is_some() { - // Compare the Arcs by pointer as Textures don't implement Eq - submit_surface_textures_owned - .insert(Arc::as_ptr(texture), texture.clone()); - } + TextureInner::Surface { .. } => { + // Compare the Arcs by pointer as Textures don't implement Eq + submit_surface_textures_owned + .insert(Arc::as_ptr(texture), texture.clone()); unsafe { used_surface_textures @@ -1291,10 +1287,11 @@ impl Global { SmallVec::<[_; 2]>::with_capacity(submit_surface_textures_owned.len()); for texture in submit_surface_textures_owned.values() { - submit_surface_textures.extend(match texture.inner.get(&snatch_guard) { - Some(TextureInner::Surface { raw, .. }) => raw.as_ref(), - _ => None, - }); + let raw = match texture.inner.get(&snatch_guard) { + Some(TextureInner::Surface { raw, .. }) => raw, + _ => unreachable!(), + }; + submit_surface_textures.push(raw); } unsafe { diff --git a/wgpu-core/src/present.rs b/wgpu-core/src/present.rs index 9c4c9115fd..38828f7643 100644 --- a/wgpu-core/src/present.rs +++ b/wgpu-core/src/present.rs @@ -208,7 +208,7 @@ impl Global { let texture = resource::Texture::new( &device, resource::TextureInner::Surface { - raw: Some(ast.texture), + raw: ast.texture, parent_id: surface_id, }, hal_usage, @@ -306,21 +306,15 @@ impl Global { .lock() .textures .remove(texture.tracker_index()); - let mut exclusive_snatch_guard = device.snatchable_lock.write(); let suf = A::surface_as_hal(&surface); - let mut inner = texture.inner_mut(&mut exclusive_snatch_guard); - let inner = inner.as_mut().unwrap(); - - match *inner { - resource::TextureInner::Surface { - ref mut raw, - ref parent_id, - } => { - if surface_id != *parent_id { + let exclusive_snatch_guard = device.snatchable_lock.write(); + match texture.inner.snatch(exclusive_snatch_guard).unwrap() { + resource::TextureInner::Surface { raw, parent_id } => { + if surface_id != parent_id { log::error!("Presented frame is from a different surface"); Err(hal::SurfaceError::Lost) } else { - unsafe { queue.raw().present(suf.unwrap(), raw.take().unwrap()) } + unsafe { queue.raw().present(suf.unwrap(), raw) } } } _ => unreachable!(), @@ -390,9 +384,9 @@ impl Global { let suf = A::surface_as_hal(&surface); let exclusive_snatch_guard = device.snatchable_lock.write(); match texture.inner.snatch(exclusive_snatch_guard).unwrap() { - resource::TextureInner::Surface { mut raw, parent_id } => { + resource::TextureInner::Surface { raw, parent_id } => { if surface_id == parent_id { - unsafe { suf.unwrap().discard_texture(raw.take().unwrap()) }; + unsafe { suf.unwrap().discard_texture(raw) }; } else { log::warn!("Surface texture is outdated"); } diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index 25c351549e..d23290789a 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -12,7 +12,7 @@ use crate::{ init_tracker::{BufferInitTracker, TextureInitTracker}, lock::{rank, Mutex, RwLock}, resource_log, - snatch::{ExclusiveSnatchGuard, SnatchGuard, Snatchable}, + snatch::{SnatchGuard, Snatchable}, track::{SharedTrackerIndexAllocator, TextureSelector, TrackerIndex}, Label, LabelHelpers, }; @@ -953,17 +953,16 @@ pub(crate) enum TextureInner { raw: A::Texture, }, Surface { - raw: Option, + raw: A::SurfaceTexture, parent_id: SurfaceId, }, } impl TextureInner { - pub(crate) fn raw(&self) -> Option<&A::Texture> { + pub(crate) fn raw(&self) -> &A::Texture { match self { - Self::Native { raw } => Some(raw), - Self::Surface { raw: Some(tex), .. } => Some(tex.borrow()), - _ => None, + Self::Native { raw } => raw, + Self::Surface { raw, .. } => raw.borrow(), } } } @@ -1104,7 +1103,7 @@ impl Texture { } pub(crate) fn raw<'a>(&'a self, snatch_guard: &'a SnatchGuard) -> Option<&'a A::Texture> { - self.inner.get(snatch_guard)?.raw() + Some(self.inner.get(snatch_guard)?.raw()) } pub(crate) fn try_raw<'a>( @@ -1113,16 +1112,10 @@ impl Texture { ) -> Result<&'a A::Texture, DestroyedResourceError> { self.inner .get(guard) - .and_then(|t| t.raw()) + .map(|t| t.raw()) .ok_or_else(|| DestroyedResourceError(self.error_ident())) } - pub(crate) fn inner_mut<'a>( - &'a self, - guard: &'a mut ExclusiveSnatchGuard, - ) -> Option<&'a mut TextureInner> { - self.inner.get_mut(guard) - } pub(crate) fn get_clear_view<'a>( clear_mode: &'a TextureClearMode, desc: &'a wgt::TextureDescriptor<(), Vec>, diff --git a/wgpu-core/src/snatch.rs b/wgpu-core/src/snatch.rs index 6f60f45d85..9866b77723 100644 --- a/wgpu-core/src/snatch.rs +++ b/wgpu-core/src/snatch.rs @@ -37,11 +37,6 @@ impl Snatchable { unsafe { (*self.value.get()).as_ref() } } - /// Get write access to the value. Requires a the snatchable lock's write guard. - pub fn get_mut<'a>(&'a self, _guard: &'a mut ExclusiveSnatchGuard) -> Option<&'a mut T> { - unsafe { (*self.value.get()).as_mut() } - } - /// Take the value. Requires a the snatchable lock's write guard. pub fn snatch(&self, _guard: ExclusiveSnatchGuard) -> Option { unsafe { (*self.value.get()).take() }