Skip to content

Commit e54382a

Browse files
committed
remove Tlas/Blas destroy methods
`Tlas.destroy` didn't check if the `Tlas` is used in a bind group of an active submission. The only reason we need the `destroy` methods for textures and buffers is because they allow users to eagerly release memory in browser implementations. I think we can remove the destroy methods on the acceleration structures for now as they complicate the picture without any gain. If they will be needed for Firefox we can add them back.
1 parent a8109f1 commit e54382a

File tree

13 files changed

+4
-249
lines changed

13 files changed

+4
-249
lines changed

player/src/lib.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -451,18 +451,12 @@ impl GlobalPlay for wgc::global::Global {
451451
Action::CreateBlas { id, desc, sizes } => {
452452
self.device_create_blas(device, &desc, sizes, Some(id));
453453
}
454-
Action::FreeBlas(id) => {
455-
self.blas_destroy(id).unwrap();
456-
}
457454
Action::DestroyBlas(id) => {
458455
self.blas_drop(id);
459456
}
460457
Action::CreateTlas { id, desc } => {
461458
self.device_create_tlas(device, &desc, Some(id));
462459
}
463-
Action::FreeTlas(id) => {
464-
self.tlas_destroy(id).unwrap();
465-
}
466460
Action::DestroyTlas(id) => {
467461
self.tlas_drop(id);
468462
}

wgpu-core/src/device/life.rs

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::{
99
};
1010
use smallvec::SmallVec;
1111

12-
use crate::resource::{Blas, Tlas};
1312
use std::sync::Arc;
1413
use thiserror::Error;
1514

@@ -101,26 +100,6 @@ impl ActiveSubmission {
101100

102101
false
103102
}
104-
105-
pub fn contains_blas(&self, blas: &Blas) -> bool {
106-
for encoder in &self.encoders {
107-
if encoder.trackers.blas_s.contains(blas) {
108-
return true;
109-
}
110-
}
111-
112-
false
113-
}
114-
115-
pub fn contains_tlas(&self, tlas: &Tlas) -> bool {
116-
for encoder in &self.encoders {
117-
if encoder.trackers.tlas_s.contains(tlas) {
118-
return true;
119-
}
120-
}
121-
122-
false
123-
}
124103
}
125104

126105
#[derive(Clone, Debug, Error)]
@@ -230,34 +209,6 @@ impl LifetimeTracker {
230209
})
231210
}
232211

233-
/// Returns the submission index of the most recent submission that uses the
234-
/// given blas.
235-
pub fn get_blas_latest_submission_index(&self, blas: &Blas) -> Option<SubmissionIndex> {
236-
// We iterate in reverse order, so that we can bail out early as soon
237-
// as we find a hit.
238-
self.active.iter().rev().find_map(|submission| {
239-
if submission.contains_blas(blas) {
240-
Some(submission.index)
241-
} else {
242-
None
243-
}
244-
})
245-
}
246-
247-
/// Returns the submission index of the most recent submission that uses the
248-
/// given tlas.
249-
pub fn get_tlas_latest_submission_index(&self, tlas: &Tlas) -> Option<SubmissionIndex> {
250-
// We iterate in reverse order, so that we can bail out early as soon
251-
// as we find a hit.
252-
self.active.iter().rev().find_map(|submission| {
253-
if submission.contains_tlas(tlas) {
254-
Some(submission.index)
255-
} else {
256-
None
257-
}
258-
})
259-
}
260-
261212
/// Returns the submission index of the most recent submission that uses the
262213
/// given texture.
263214
pub fn get_texture_latest_submission_index(

wgpu-core/src/device/queue.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use crate::{
2828

2929
use smallvec::SmallVec;
3030

31-
use crate::resource::DestroyedAccelerationStructure;
3231
use crate::scratch::ScratchBuffer;
3332
use std::{
3433
iter,
@@ -252,7 +251,6 @@ pub enum TempResource {
252251
ScratchBuffer(ScratchBuffer),
253252
DestroyedBuffer(DestroyedBuffer),
254253
DestroyedTexture(DestroyedTexture),
255-
DestroyedAccelerationStructure(DestroyedAccelerationStructure),
256254
}
257255

258256
/// A series of raw [`CommandBuffer`]s that have been submitted to a

wgpu-core/src/device/ray_tracing.rs

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ use std::sync::Arc;
44
use crate::api_log;
55
#[cfg(feature = "trace")]
66
use crate::device::trace;
7-
use crate::lock::{rank, Mutex};
7+
use crate::lock::rank;
88
use crate::resource::{Fallible, TrackingData};
99
use crate::snatch::Snatchable;
10-
use crate::weak_vec::WeakVec;
1110
use crate::{
1211
device::{Device, DeviceError},
1312
global::Global,
@@ -166,7 +165,6 @@ impl Device {
166165
label: desc.label.to_string(),
167166
max_instance_count: desc.max_instances,
168167
tracking_data: TrackingData::new(self.tracker_indices.tlas_s.clone()),
169-
bind_groups: Mutex::new(rank::TLAS_BIND_GROUPS, WeakVec::new()),
170168
}))
171169
}
172170
}
@@ -247,20 +245,6 @@ impl Global {
247245
(id, Some(error))
248246
}
249247

250-
pub fn blas_destroy(&self, blas_id: BlasId) -> Result<(), resource::DestroyError> {
251-
profiling::scope!("Blas::destroy");
252-
api_log!("Blas::destroy {blas_id:?}");
253-
254-
let blas = self.hub.blas_s.get(blas_id).get()?;
255-
256-
#[cfg(feature = "trace")]
257-
if let Some(trace) = blas.device.trace.lock().as_mut() {
258-
trace.add(trace::Action::FreeBlas(blas_id));
259-
}
260-
261-
blas.destroy()
262-
}
263-
264248
pub fn blas_drop(&self, blas_id: BlasId) {
265249
profiling::scope!("Blas::drop");
266250
api_log!("Blas::drop {blas_id:?}");
@@ -275,20 +259,6 @@ impl Global {
275259
}
276260
}
277261

278-
pub fn tlas_destroy(&self, tlas_id: TlasId) -> Result<(), resource::DestroyError> {
279-
profiling::scope!("Tlas::destroy");
280-
api_log!("Tlas::destroy {tlas_id:?}");
281-
282-
let tlas = self.hub.tlas_s.get(tlas_id).get()?;
283-
284-
#[cfg(feature = "trace")]
285-
if let Some(trace) = tlas.device.trace.lock().as_mut() {
286-
trace.add(trace::Action::FreeTlas(tlas_id));
287-
}
288-
289-
tlas.destroy()
290-
}
291-
292262
pub fn tlas_drop(&self, tlas_id: TlasId) {
293263
profiling::scope!("Tlas::drop");
294264
api_log!("Tlas::drop {tlas_id:?}");

wgpu-core/src/device/trace.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,11 @@ pub enum Action<'a> {
132132
desc: crate::resource::BlasDescriptor<'a>,
133133
sizes: wgt::BlasGeometrySizeDescriptors,
134134
},
135-
FreeBlas(id::BlasId),
136135
DestroyBlas(id::BlasId),
137136
CreateTlas {
138137
id: id::TlasId,
139138
desc: crate::resource::TlasDescriptor<'a>,
140139
},
141-
FreeTlas(id::TlasId),
142140
DestroyTlas(id::TlasId),
143141
}
144142

wgpu-core/src/lock/rank.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ define_lock_ranks! {
148148
rank BLAS_BUILT_INDEX "Blas::built_index" followed by { }
149149
rank TLAS_BUILT_INDEX "Tlas::built_index" followed by { }
150150
rank TLAS_DEPENDENCIES "Tlas::dependencies" followed by { }
151-
rank TLAS_BIND_GROUPS "Tlas::bind_groups" followed by { }
152151

153152
#[cfg(test)]
154153
rank PAWN "pawn" followed by { ROOK, BISHOP }

wgpu-core/src/resource.rs

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,42 +1898,6 @@ impl AccelerationStructure for Blas {
18981898
}
18991899
}
19001900

1901-
impl Blas {
1902-
pub(crate) fn destroy(self: &Arc<Self>) -> Result<(), DestroyError> {
1903-
let device = &self.device;
1904-
1905-
let temp = {
1906-
let mut snatch_guard = device.snatchable_lock.write();
1907-
1908-
let raw = match self.raw.snatch(&mut snatch_guard) {
1909-
Some(raw) => raw,
1910-
None => {
1911-
return Err(DestroyError::AlreadyDestroyed);
1912-
}
1913-
};
1914-
1915-
drop(snatch_guard);
1916-
1917-
queue::TempResource::DestroyedAccelerationStructure(DestroyedAccelerationStructure {
1918-
raw: ManuallyDrop::new(raw),
1919-
device: Arc::clone(&self.device),
1920-
label: self.label().to_owned(),
1921-
bind_groups: WeakVec::new(),
1922-
})
1923-
};
1924-
1925-
if let Some(queue) = device.get_queue() {
1926-
let mut life_lock = queue.lock_life();
1927-
let last_submit_index = life_lock.get_blas_latest_submission_index(self);
1928-
if let Some(last_submit_index) = last_submit_index {
1929-
life_lock.schedule_resource_destruction(temp, last_submit_index);
1930-
}
1931-
}
1932-
1933-
Ok(())
1934-
}
1935-
}
1936-
19371901
crate::impl_resource_type!(Blas);
19381902
crate::impl_labeled!(Blas);
19391903
crate::impl_parent_device!(Blas);
@@ -1954,7 +1918,6 @@ pub struct Tlas {
19541918
/// The `label` from the descriptor used to create the resource.
19551919
pub(crate) label: String,
19561920
pub(crate) tracking_data: TrackingData,
1957-
pub(crate) bind_groups: Mutex<WeakVec<BindGroup>>,
19581921
}
19591922

19601923
impl Drop for Tlas {
@@ -1987,71 +1950,3 @@ crate::impl_labeled!(Tlas);
19871950
crate::impl_parent_device!(Tlas);
19881951
crate::impl_storage_item!(Tlas);
19891952
crate::impl_trackable!(Tlas);
1990-
1991-
impl Tlas {
1992-
pub(crate) fn destroy(self: &Arc<Self>) -> Result<(), DestroyError> {
1993-
let device = &self.device;
1994-
1995-
let temp = {
1996-
let mut snatch_guard = device.snatchable_lock.write();
1997-
1998-
let raw = match self.raw.snatch(&mut snatch_guard) {
1999-
Some(raw) => raw,
2000-
None => {
2001-
return Err(DestroyError::AlreadyDestroyed);
2002-
}
2003-
};
2004-
2005-
drop(snatch_guard);
2006-
2007-
queue::TempResource::DestroyedAccelerationStructure(DestroyedAccelerationStructure {
2008-
raw: ManuallyDrop::new(raw),
2009-
device: Arc::clone(&self.device),
2010-
label: self.label().to_owned(),
2011-
bind_groups: mem::take(&mut self.bind_groups.lock()),
2012-
})
2013-
};
2014-
2015-
if let Some(queue) = device.get_queue() {
2016-
let mut life_lock = queue.lock_life();
2017-
let last_submit_index = life_lock.get_tlas_latest_submission_index(self);
2018-
if let Some(last_submit_index) = last_submit_index {
2019-
life_lock.schedule_resource_destruction(temp, last_submit_index);
2020-
}
2021-
}
2022-
2023-
Ok(())
2024-
}
2025-
}
2026-
2027-
#[derive(Debug)]
2028-
pub struct DestroyedAccelerationStructure {
2029-
raw: ManuallyDrop<Box<dyn hal::DynAccelerationStructure>>,
2030-
device: Arc<Device>,
2031-
label: String,
2032-
// only filled if the acceleration structure is a TLAS
2033-
bind_groups: WeakVec<BindGroup>,
2034-
}
2035-
2036-
impl DestroyedAccelerationStructure {
2037-
pub fn label(&self) -> &dyn Debug {
2038-
&self.label
2039-
}
2040-
}
2041-
2042-
impl Drop for DestroyedAccelerationStructure {
2043-
fn drop(&mut self) {
2044-
let mut deferred = self.device.deferred_destroy.lock();
2045-
deferred.push(DeferredDestroy::BindGroups(mem::take(
2046-
&mut self.bind_groups,
2047-
)));
2048-
drop(deferred);
2049-
2050-
resource_log!("Destroy raw Buffer (destroyed) {:?}", self.label());
2051-
// SAFETY: We are in the Drop impl and we don't use self.raw anymore after this point.
2052-
let raw = unsafe { ManuallyDrop::take(&mut self.raw) };
2053-
unsafe {
2054-
hal::DynDevice::destroy_acceleration_structure(self.device.raw(), raw);
2055-
}
2056-
}
2057-
}

wgpu-core/src/track/ray_tracing.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ impl<T: AccelerationStructure> AccelerationStructureTracker<T> {
4646
}
4747
}
4848

49-
/// Returns true if the given buffer is tracked.
50-
pub fn contains(&self, acceleration_structure: &T) -> bool {
51-
self.metadata
52-
.contains(acceleration_structure.tracker_index().as_usize())
53-
}
54-
5549
/// Inserts a single resource into the resource tracker.
5650
pub fn set_single(&mut self, resource: Arc<T>) {
5751
let index: usize = resource.tracker_index().as_usize();

wgpu/src/api/blas.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::context::{Context, DynContext};
1+
use crate::context::Context;
22
use crate::{Buffer, Data, Label, C};
33
use std::sync::Arc;
44
use std::thread;
@@ -35,8 +35,7 @@ static_assertions::assert_impl_all!(CreateBlasDescriptor<'_>: Send, Sync);
3535
///
3636
/// Each one contains:
3737
/// - A reference to a BLAS, this ***must*** be interacted with using [TlasInstance::new] or [TlasInstance::set_blas], a
38-
/// TlasInstance that references a BLAS keeps that BLAS from being dropped, but if the BLAS is explicitly destroyed (e.g.
39-
/// using [Blas::destroy]) the TlasInstance becomes invalid
38+
/// TlasInstance that references a BLAS keeps that BLAS from being dropped
4039
/// - A user accessible transformation matrix
4140
/// - A user accessible mask
4241
/// - A user accessible custom index
@@ -171,10 +170,6 @@ impl Blas {
171170
pub fn handle(&self) -> Option<u64> {
172171
self.handle
173172
}
174-
/// Destroy the associated native resources as soon as possible.
175-
pub fn destroy(&self) {
176-
DynContext::blas_destroy(&*self.shared.context, self.shared.data.as_ref());
177-
}
178173
}
179174

180175
impl Drop for BlasShared {

wgpu/src/api/tlas.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::api::blas::{ContextTlasInstance, DynContextTlasInstance, TlasInstance};
2-
use crate::context::{Context, DynContext};
2+
use crate::context::Context;
33
use crate::{BindingResource, Buffer, Data, Label, C};
44
use std::ops::{Index, IndexMut, Range};
55
use std::sync::Arc;
@@ -27,13 +27,6 @@ pub struct Tlas {
2727
}
2828
static_assertions::assert_impl_all!(Tlas: WasmNotSendSync);
2929

30-
impl Tlas {
31-
/// Destroy the associated native resources as soon as possible.
32-
pub fn destroy(&self) {
33-
DynContext::tlas_destroy(&*self.context, self.data.as_ref());
34-
}
35-
}
36-
3730
impl Drop for Tlas {
3831
fn drop(&mut self) {
3932
if !thread::panicking() {

wgpu/src/backend/webgpu.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3487,18 +3487,10 @@ impl crate::context::Context for ContextWebGpu {
34873487
unimplemented!("Raytracing not implemented for web");
34883488
}
34893489

3490-
fn blas_destroy(&self, _blas_data: &Self::BlasData) {
3491-
unimplemented!("Raytracing not implemented for web");
3492-
}
3493-
34943490
fn blas_drop(&self, _blas_data: &Self::BlasData) {
34953491
unimplemented!("Raytracing not implemented for web");
34963492
}
34973493

3498-
fn tlas_destroy(&self, _tlas_data: &Self::TlasData) {
3499-
unimplemented!("Raytracing not implemented for web");
3500-
}
3501-
35023494
fn tlas_drop(&self, _tlas_data: &Self::TlasData) {
35033495
unimplemented!("Raytracing not implemented for web");
35043496
}

0 commit comments

Comments
 (0)