Skip to content

Commit

Permalink
introduce DeviceTextureTracker which holds weak references to textures
Browse files Browse the repository at this point in the history
  • Loading branch information
teoxoy committed Jul 9, 2024
1 parent ae33a88 commit 7ba4498
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 79 deletions.
6 changes: 3 additions & 3 deletions wgpu-core/src/command/clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
Texture, TextureClearMode,
},
snatch::SnatchGuard,
track::{TextureSelector, TextureTracker},
track::{TextureSelector, TextureTrackerSetSingle},
};

use hal::CommandEncoder as _;
Expand Down Expand Up @@ -269,11 +269,11 @@ impl Global {
}
}

pub(crate) fn clear_texture<A: HalApi>(
pub(crate) fn clear_texture<A: HalApi, T: TextureTrackerSetSingle<A>>(
dst_texture: &Arc<Texture<A>>,
range: TextureInitRange,
encoder: &mut A::CommandEncoder,
texture_tracker: &mut TextureTracker<A>,
texture_tracker: &mut T,
alignments: &hal::Alignments,
zero_buffer: &A::Buffer,
snatch_guard: &SnatchGuard<'_>,
Expand Down
9 changes: 3 additions & 6 deletions wgpu-core/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,12 +434,9 @@ impl<A: HalApi> CommandBuffer<A> {
.buffers
.set_from_tracker_and_drain_transitions(&head.buffers, snatch_guard);

base.textures.set_from_tracker(&head.textures);
let (transitions, textures) = base.textures.drain_transitions(snatch_guard);
let texture_barriers = transitions
.into_iter()
.enumerate()
.map(|(i, p)| p.into_hal(textures[i].unwrap().raw().unwrap()));
let texture_barriers = base
.textures
.set_from_tracker_and_drain_transitions(&head.textures, snatch_guard);

unsafe {
raw.transition_buffers(buffer_barriers);
Expand Down
26 changes: 10 additions & 16 deletions wgpu-core/src/device/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1343,15 +1343,12 @@ impl Global {
))
.map_err(DeviceError::from)?
};
trackers
let texture_barriers = trackers
.textures
.set_from_usage_scope(&used_surface_textures);
let (transitions, textures) =
trackers.textures.drain_transitions(&snatch_guard);
let texture_barriers = transitions
.into_iter()
.enumerate()
.map(|(i, p)| p.into_hal(textures[i].unwrap().raw().unwrap()));
.set_from_usage_scope_and_drain_transitions(
&used_surface_textures,
&snatch_guard,
);
let present = unsafe {
baked.encoder.transition_textures(texture_barriers);
baked.encoder.end_encoding().unwrap()
Expand Down Expand Up @@ -1401,15 +1398,12 @@ impl Global {
if !used_surface_textures.is_empty() {
let mut trackers = device.trackers.lock();

trackers
let texture_barriers = trackers
.textures
.set_from_usage_scope(&used_surface_textures);
let (transitions, textures) =
trackers.textures.drain_transitions(&snatch_guard);
let texture_barriers = transitions
.into_iter()
.enumerate()
.map(|(i, p)| p.into_hal(textures[i].unwrap().raw().unwrap()));
.set_from_usage_scope_and_drain_transitions(
&used_surface_textures,
&snatch_guard,
);
unsafe {
pending_writes
.command_encoder
Expand Down
4 changes: 3 additions & 1 deletion wgpu-core/src/device/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3554,7 +3554,9 @@ impl<A: HalApi> Device<A> {
}
}
for texture in trackers.textures.used_resources() {
let _ = texture.destroy();
if let Some(texture) = Weak::upgrade(&texture) {
let _ = texture.destroy();
}
}
}

Expand Down
7 changes: 4 additions & 3 deletions wgpu-core/src/track/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ pub(crate) use buffer::{
use metadata::{ResourceMetadata, ResourceMetadataProvider};
pub(crate) use stateless::{StatelessBindGroupState, StatelessTracker};
pub(crate) use texture::{
TextureBindGroupState, TextureSelector, TextureTracker, TextureUsageScope,
DeviceTextureTracker, TextureBindGroupState, TextureSelector, TextureTracker,
TextureTrackerSetSingle, TextureUsageScope,
};
use wgt::strict_assert_ne;

Expand Down Expand Up @@ -603,14 +604,14 @@ impl<'a, A: HalApi> UsageScope<'a, A> {
/// A tracker used by Device.
pub(crate) struct DeviceTracker<A: HalApi> {
pub buffers: DeviceBufferTracker<A>,
pub textures: TextureTracker<A>,
pub textures: DeviceTextureTracker<A>,
}

impl<A: HalApi> DeviceTracker<A> {
pub fn new() -> Self {
Self {
buffers: DeviceBufferTracker::new(),
textures: TextureTracker::new(),
textures: DeviceTextureTracker::new(),
}
}
}
Expand Down
Loading

0 comments on commit 7ba4498

Please sign in to comment.