Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Texture snatching #4969

Merged
merged 2 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions wgpu-core/src/binding_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -847,11 +847,14 @@ impl<A: HalApi> Drop for BindGroup<A> {

impl<A: HalApi> BindGroup<A> {
pub(crate) fn raw(&self, guard: &SnatchGuard) -> Option<&A::BindGroup> {
// Clippy insist on writing it this way. The idea is to return None
// if any of the raw buffer is not valid anymore.
for buffer in &self.used_buffer_ranges {
// Clippy insist on writing it this way. The idea is to return None
// if any of the raw buffer is not valid anymore.
let _ = buffer.buffer.raw(guard)?;
}
for texture in &self.used_texture_ranges {
let _ = texture.texture.raw(guard)?;
}
self.raw.as_ref()
}
pub(crate) fn validate_dynamic_bindings(
Expand Down
27 changes: 1 addition & 26 deletions wgpu-core/src/device/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,32 +736,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.get_and_mark_destroyed(texture_id)
.map_err(|_| resource::DestroyError::Invalid)?;

let device = &texture.device;

#[cfg(feature = "trace")]
if let Some(ref mut trace) = *device.trace.lock() {
trace.add(trace::Action::FreeTexture(texture_id));
}

let last_submit_index = texture.info.submission_index();

let snatch_guard = texture.device.snatchable_lock.read();

if let Some(resource::TextureInner::Native { .. }) = texture.inner.get(&snatch_guard) {
let temp = queue::TempResource::Texture(texture.clone());
let mut guard = device.pending_writes.lock();
let pending_writes = guard.as_mut().unwrap();
if pending_writes.dst_textures.contains_key(&texture_id) {
pending_writes.temp_resources.push(temp);
} else {
drop(guard);
device
.lock_life()
.schedule_resource_destruction(temp, last_submit_index);
}
}

Ok(())
texture.destroy()
}

pub fn texture_drop<A: HalApi>(&self, texture_id: id::TextureId, wait: bool) {
Expand Down
43 changes: 41 additions & 2 deletions wgpu-core/src/device/life.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use crate::{
},
pipeline::{ComputePipeline, RenderPipeline},
resource::{
self, Buffer, DestroyedBuffer, QuerySet, Resource, Sampler, StagingBuffer, Texture,
TextureView,
self, Buffer, DestroyedBuffer, DestroyedTexture, QuerySet, Resource, Sampler,
StagingBuffer, Texture, TextureView,
},
track::{ResourceTracker, Tracker},
FastHashMap, SubmissionIndex,
Expand All @@ -43,6 +43,7 @@ pub(crate) struct ResourceMaps<A: HalApi> {
pub render_bundles: FastHashMap<RenderBundleId, Arc<RenderBundle<A>>>,
pub query_sets: FastHashMap<QuerySetId, Arc<QuerySet<A>>>,
pub destroyed_buffers: FastHashMap<BufferId, Arc<DestroyedBuffer<A>>>,
pub destroyed_textures: FastHashMap<TextureId, Arc<DestroyedTexture<A>>>,
}

impl<A: HalApi> ResourceMaps<A> {
Expand All @@ -61,6 +62,7 @@ impl<A: HalApi> ResourceMaps<A> {
render_bundles: FastHashMap::default(),
query_sets: FastHashMap::default(),
destroyed_buffers: FastHashMap::default(),
destroyed_textures: FastHashMap::default(),
}
}

Expand All @@ -79,6 +81,7 @@ impl<A: HalApi> ResourceMaps<A> {
render_bundles,
query_sets,
destroyed_buffers,
destroyed_textures,
} = self;
buffers.clear();
staging_buffers.clear();
Expand All @@ -93,6 +96,7 @@ impl<A: HalApi> ResourceMaps<A> {
render_bundles.clear();
query_sets.clear();
destroyed_buffers.clear();
destroyed_textures.clear();
}

pub(crate) fn extend(&mut self, mut other: Self) {
Expand All @@ -110,6 +114,7 @@ impl<A: HalApi> ResourceMaps<A> {
render_bundles,
query_sets,
destroyed_buffers,
destroyed_textures,
} = self;
buffers.extend(other.buffers.drain());
staging_buffers.extend(other.staging_buffers.drain());
Expand All @@ -124,6 +129,7 @@ impl<A: HalApi> ResourceMaps<A> {
render_bundles.extend(other.render_bundles.drain());
query_sets.extend(other.query_sets.drain());
destroyed_buffers.extend(other.destroyed_buffers.drain());
destroyed_textures.extend(other.destroyed_textures.drain());
}
}

Expand Down Expand Up @@ -298,6 +304,11 @@ impl<A: HalApi> LifetimeTracker<A> {
TempResource::Texture(raw) => {
last_resources.textures.insert(raw.as_info().id(), raw);
}
TempResource::DestroyedTexture(destroyed) => {
last_resources
.destroyed_textures
.insert(destroyed.id, destroyed);
}
}
}

Expand Down Expand Up @@ -404,6 +415,9 @@ impl<A: HalApi> LifetimeTracker<A> {
TempResource::Texture(raw) => {
resources.textures.insert(raw.as_info().id(), raw);
}
TempResource::DestroyedTexture(destroyed) => {
resources.destroyed_textures.insert(destroyed.id, destroyed);
}
}
}

Expand Down Expand Up @@ -680,6 +694,27 @@ impl<A: HalApi> LifetimeTracker<A> {
}
}

fn triage_suspected_destroyed_textures(
&mut self,
#[cfg(feature = "trace")] trace: &mut Option<&mut trace::Trace>,
) {
for (id, texture) in self.suspected_resources.destroyed_textures.drain() {
let submit_index = texture.submission_index;
if let Some(resources) = self.active.iter_mut().find(|a| a.index == submit_index) {
resources
.last_resources
.destroyed_textures
.insert(id, texture);
} else {
self.free_resources.destroyed_textures.insert(id, texture);
}
#[cfg(feature = "trace")]
if let Some(ref mut t) = *trace {
t.add(trace::Action::DestroyTexture(id));
}
}
}

fn triage_suspected_compute_pipelines(
&mut self,
trackers: &Mutex<Tracker<A>>,
Expand Down Expand Up @@ -913,6 +948,10 @@ impl<A: HalApi> LifetimeTracker<A> {
#[cfg(feature = "trace")]
&mut trace,
);
self.triage_suspected_destroyed_textures(
#[cfg(feature = "trace")]
&mut trace,
);
}

/// Determine which buffers are ready to map, and which must wait for the
Expand Down
5 changes: 3 additions & 2 deletions wgpu-core/src/device/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use crate::{
identity::{GlobalIdentityHandlerFactory, Input},
init_tracker::{has_copy_partial_init_tracker_coverage, TextureInitRange},
resource::{
Buffer, BufferAccessError, BufferMapState, DestroyedBuffer, Resource, ResourceInfo,
ResourceType, StagingBuffer, Texture, TextureInner,
Buffer, BufferAccessError, BufferMapState, DestroyedBuffer, DestroyedTexture, Resource,
ResourceInfo, ResourceType, StagingBuffer, Texture, TextureInner,
},
resource_log, track, FastHashMap, SubmissionIndex,
};
Expand Down Expand Up @@ -164,6 +164,7 @@ pub enum TempResource<A: HalApi> {
Buffer(Arc<Buffer<A>>),
StagingBuffer(Arc<StagingBuffer<A>>),
DestroyedBuffer(Arc<DestroyedBuffer<A>>),
DestroyedTexture(Arc<DestroyedTexture<A>>),
Texture(Arc<Texture<A>>),
}

Expand Down
76 changes: 76 additions & 0 deletions wgpu-core/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,50 @@ impl<A: HalApi> Texture<A> {
}
}
}

pub(crate) fn destroy(self: &Arc<Self>) -> Result<(), DestroyError> {
let device = &self.device;
let texture_id = self.info.id();

#[cfg(feature = "trace")]
if let Some(ref mut trace) = *device.trace.lock() {
trace.add(trace::Action::FreeTexture(texture_id));
}

let temp = {
let snatch_guard = device.snatchable_lock.write();
let raw = match self.inner.snatch(snatch_guard) {
Some(TextureInner::Native { raw }) => raw,
Some(TextureInner::Surface { .. }) => {
return Ok(());
}
None => {
return Err(resource::DestroyError::AlreadyDestroyed);
}
};

queue::TempResource::DestroyedTexture(Arc::new(DestroyedTexture {
raw: Some(raw),
device: Arc::clone(&self.device),
submission_index: self.info.submission_index(),
id: self.info.id.unwrap(),
label: self.info.label.clone(),
}))
};

let mut pending_writes = device.pending_writes.lock();
let pending_writes = pending_writes.as_mut().unwrap();
if pending_writes.dst_textures.contains_key(&texture_id) {
pending_writes.temp_resources.push(temp);
} else {
let last_submit_index = self.info.submission_index();
device
.lock_life()
.schedule_resource_destruction(temp, last_submit_index);
}

Ok(())
}
}

impl<G: GlobalIdentityHandlerFactory> Global<G> {
Expand Down Expand Up @@ -927,6 +971,38 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
}
}

/// A texture that has been marked as destroyed and is staged for actual deletion soon.
#[derive(Debug)]
pub struct DestroyedTexture<A: HalApi> {
raw: Option<A::Texture>,
device: Arc<Device<A>>,
label: String,
pub(crate) id: TextureId,
pub(crate) submission_index: u64,
}

impl<A: HalApi> DestroyedTexture<A> {
pub fn label(&self) -> &dyn Debug {
if !self.label.is_empty() {
return &self.label;
}

&self.id
}
}

impl<A: HalApi> Drop for DestroyedTexture<A> {
fn drop(&mut self) {
if let Some(raw) = self.raw.take() {
resource_log!("Deallocate raw Texture (destroyed) {:?}", self.label());
unsafe {
use hal::Device;
self.device.raw().destroy_texture(raw);
}
}
}
}

#[derive(Clone, Copy, Debug)]
pub enum TextureErrorDimension {
X,
Expand Down