remove Option around A::SurfaceTexture

We can rely on the snatching mechanism to take the surface texture.
This commit is contained in:
teoxoy 2024-08-07 15:15:32 +02:00 committed by Teodor Tanasoaia
parent 19843c9c5f
commit ce9c9b76f6
5 changed files with 29 additions and 50 deletions

View File

@ -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);

View File

@ -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 {

View File

@ -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");
}

View File

@ -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<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(),
}
}
}
@ -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>(
@ -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>>,

View File

@ -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() }