Skip to content

Commit ce9c9b7

Browse files
committed
remove Option around A::SurfaceTexture
We can rely on the snatching mechanism to take the surface texture.
1 parent 19843c9 commit ce9c9b7

File tree

5 files changed

+29
-50
lines changed

5 files changed

+29
-50
lines changed

wgpu-core/src/command/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ impl<A: HalApi> CommandBuffer<A> {
409409
let texture_barriers = transitions
410410
.into_iter()
411411
.enumerate()
412-
.map(|(i, p)| p.into_hal(textures[i].unwrap().raw().unwrap()));
412+
.map(|(i, p)| p.into_hal(textures[i].unwrap().raw()));
413413

414414
unsafe {
415415
raw.transition_buffers(buffer_barriers);

wgpu-core/src/device/queue.rs

+13-16
Original file line numberDiff line numberDiff line change
@@ -1143,12 +1143,10 @@ impl Global {
11431143
for texture in cmd_buf_trackers.textures.used_resources() {
11441144
let should_extend = match texture.try_inner(&snatch_guard)? {
11451145
TextureInner::Native { .. } => false,
1146-
TextureInner::Surface { ref raw, .. } => {
1147-
if raw.is_some() {
1148-
// Compare the Arcs by pointer as Textures don't implement Eq.
1149-
submit_surface_textures_owned
1150-
.insert(Arc::as_ptr(&texture), texture.clone());
1151-
}
1146+
TextureInner::Surface { .. } => {
1147+
// Compare the Arcs by pointer as Textures don't implement Eq.
1148+
submit_surface_textures_owned
1149+
.insert(Arc::as_ptr(&texture), texture.clone());
11521150

11531151
true
11541152
}
@@ -1242,12 +1240,10 @@ impl Global {
12421240
for texture in pending_writes.dst_textures.values() {
12431241
match texture.try_inner(&snatch_guard)? {
12441242
TextureInner::Native { .. } => {}
1245-
TextureInner::Surface { ref raw, .. } => {
1246-
if raw.is_some() {
1247-
// Compare the Arcs by pointer as Textures don't implement Eq
1248-
submit_surface_textures_owned
1249-
.insert(Arc::as_ptr(texture), texture.clone());
1250-
}
1243+
TextureInner::Surface { .. } => {
1244+
// Compare the Arcs by pointer as Textures don't implement Eq
1245+
submit_surface_textures_owned
1246+
.insert(Arc::as_ptr(texture), texture.clone());
12511247

12521248
unsafe {
12531249
used_surface_textures
@@ -1291,10 +1287,11 @@ impl Global {
12911287
SmallVec::<[_; 2]>::with_capacity(submit_surface_textures_owned.len());
12921288

12931289
for texture in submit_surface_textures_owned.values() {
1294-
submit_surface_textures.extend(match texture.inner.get(&snatch_guard) {
1295-
Some(TextureInner::Surface { raw, .. }) => raw.as_ref(),
1296-
_ => None,
1297-
});
1290+
let raw = match texture.inner.get(&snatch_guard) {
1291+
Some(TextureInner::Surface { raw, .. }) => raw,
1292+
_ => unreachable!(),
1293+
};
1294+
submit_surface_textures.push(raw);
12981295
}
12991296

13001297
unsafe {

wgpu-core/src/present.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ impl Global {
208208
let texture = resource::Texture::new(
209209
&device,
210210
resource::TextureInner::Surface {
211-
raw: Some(ast.texture),
211+
raw: ast.texture,
212212
parent_id: surface_id,
213213
},
214214
hal_usage,
@@ -306,21 +306,15 @@ impl Global {
306306
.lock()
307307
.textures
308308
.remove(texture.tracker_index());
309-
let mut exclusive_snatch_guard = device.snatchable_lock.write();
310309
let suf = A::surface_as_hal(&surface);
311-
let mut inner = texture.inner_mut(&mut exclusive_snatch_guard);
312-
let inner = inner.as_mut().unwrap();
313-
314-
match *inner {
315-
resource::TextureInner::Surface {
316-
ref mut raw,
317-
ref parent_id,
318-
} => {
319-
if surface_id != *parent_id {
310+
let exclusive_snatch_guard = device.snatchable_lock.write();
311+
match texture.inner.snatch(exclusive_snatch_guard).unwrap() {
312+
resource::TextureInner::Surface { raw, parent_id } => {
313+
if surface_id != parent_id {
320314
log::error!("Presented frame is from a different surface");
321315
Err(hal::SurfaceError::Lost)
322316
} else {
323-
unsafe { queue.raw().present(suf.unwrap(), raw.take().unwrap()) }
317+
unsafe { queue.raw().present(suf.unwrap(), raw) }
324318
}
325319
}
326320
_ => unreachable!(),
@@ -390,9 +384,9 @@ impl Global {
390384
let suf = A::surface_as_hal(&surface);
391385
let exclusive_snatch_guard = device.snatchable_lock.write();
392386
match texture.inner.snatch(exclusive_snatch_guard).unwrap() {
393-
resource::TextureInner::Surface { mut raw, parent_id } => {
387+
resource::TextureInner::Surface { raw, parent_id } => {
394388
if surface_id == parent_id {
395-
unsafe { suf.unwrap().discard_texture(raw.take().unwrap()) };
389+
unsafe { suf.unwrap().discard_texture(raw) };
396390
} else {
397391
log::warn!("Surface texture is outdated");
398392
}

wgpu-core/src/resource.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
init_tracker::{BufferInitTracker, TextureInitTracker},
1313
lock::{rank, Mutex, RwLock},
1414
resource_log,
15-
snatch::{ExclusiveSnatchGuard, SnatchGuard, Snatchable},
15+
snatch::{SnatchGuard, Snatchable},
1616
track::{SharedTrackerIndexAllocator, TextureSelector, TrackerIndex},
1717
Label, LabelHelpers,
1818
};
@@ -953,17 +953,16 @@ pub(crate) enum TextureInner<A: HalApi> {
953953
raw: A::Texture,
954954
},
955955
Surface {
956-
raw: Option<A::SurfaceTexture>,
956+
raw: A::SurfaceTexture,
957957
parent_id: SurfaceId,
958958
},
959959
}
960960

961961
impl<A: HalApi> TextureInner<A> {
962-
pub(crate) fn raw(&self) -> Option<&A::Texture> {
962+
pub(crate) fn raw(&self) -> &A::Texture {
963963
match self {
964-
Self::Native { raw } => Some(raw),
965-
Self::Surface { raw: Some(tex), .. } => Some(tex.borrow()),
966-
_ => None,
964+
Self::Native { raw } => raw,
965+
Self::Surface { raw, .. } => raw.borrow(),
967966
}
968967
}
969968
}
@@ -1104,7 +1103,7 @@ impl<A: HalApi> Texture<A> {
11041103
}
11051104

11061105
pub(crate) fn raw<'a>(&'a self, snatch_guard: &'a SnatchGuard) -> Option<&'a A::Texture> {
1107-
self.inner.get(snatch_guard)?.raw()
1106+
Some(self.inner.get(snatch_guard)?.raw())
11081107
}
11091108

11101109
pub(crate) fn try_raw<'a>(
@@ -1113,16 +1112,10 @@ impl<A: HalApi> Texture<A> {
11131112
) -> Result<&'a A::Texture, DestroyedResourceError> {
11141113
self.inner
11151114
.get(guard)
1116-
.and_then(|t| t.raw())
1115+
.map(|t| t.raw())
11171116
.ok_or_else(|| DestroyedResourceError(self.error_ident()))
11181117
}
11191118

1120-
pub(crate) fn inner_mut<'a>(
1121-
&'a self,
1122-
guard: &'a mut ExclusiveSnatchGuard,
1123-
) -> Option<&'a mut TextureInner<A>> {
1124-
self.inner.get_mut(guard)
1125-
}
11261119
pub(crate) fn get_clear_view<'a>(
11271120
clear_mode: &'a TextureClearMode<A>,
11281121
desc: &'a wgt::TextureDescriptor<(), Vec<wgt::TextureFormat>>,

wgpu-core/src/snatch.rs

-5
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ impl<T> Snatchable<T> {
3737
unsafe { (*self.value.get()).as_ref() }
3838
}
3939

40-
/// Get write access to the value. Requires a the snatchable lock's write guard.
41-
pub fn get_mut<'a>(&'a self, _guard: &'a mut ExclusiveSnatchGuard) -> Option<&'a mut T> {
42-
unsafe { (*self.value.get()).as_mut() }
43-
}
44-
4540
/// Take the value. Requires a the snatchable lock's write guard.
4641
pub fn snatch(&self, _guard: ExclusiveSnatchGuard) -> Option<T> {
4742
unsafe { (*self.value.get()).take() }

0 commit comments

Comments
 (0)