Skip to content

Commit

Permalink
remove Option around A::SurfaceTexture
Browse files Browse the repository at this point in the history
We can rely on the snatching mechanism to take the surface texture.
  • Loading branch information
teoxoy committed Aug 12, 2024
1 parent 19843c9 commit ce9c9b7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 50 deletions.
2 changes: 1 addition & 1 deletion wgpu-core/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ impl<A: HalApi> CommandBuffer<A> {
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);
Expand Down
29 changes: 13 additions & 16 deletions wgpu-core/src/device/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 8 additions & 14 deletions wgpu-core/src/present.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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!(),
Expand Down Expand Up @@ -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");
}
Expand Down
21 changes: 7 additions & 14 deletions wgpu-core/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -953,17 +953,16 @@ pub(crate) enum TextureInner<A: HalApi> {
raw: A::Texture,
},
Surface {
raw: Option<A::SurfaceTexture>,
raw: A::SurfaceTexture,
parent_id: SurfaceId,
},
}

impl<A: HalApi> TextureInner<A> {
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(),
}
}
}
Expand Down Expand Up @@ -1104,7 +1103,7 @@ impl<A: HalApi> Texture<A> {
}

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>(
Expand All @@ -1113,16 +1112,10 @@ impl<A: HalApi> Texture<A> {
) -> 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<A>> {
self.inner.get_mut(guard)
}
pub(crate) fn get_clear_view<'a>(
clear_mode: &'a TextureClearMode<A>,
desc: &'a wgt::TextureDescriptor<(), Vec<wgt::TextureFormat>>,
Expand Down
5 changes: 0 additions & 5 deletions wgpu-core/src/snatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ impl<T> Snatchable<T> {
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<T> {
unsafe { (*self.value.get()).take() }
Expand Down

0 comments on commit ce9c9b7

Please sign in to comment.