Skip to content

Commit 5a2875e

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 d098812 commit 5a2875e

File tree

13 files changed

+3
-247
lines changed

13 files changed

+3
-247
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,
@@ -303,7 +302,6 @@ pub enum TempResource {
303302
ScratchBuffer(ScratchBuffer),
304303
DestroyedBuffer(DestroyedBuffer),
305304
DestroyedTexture(DestroyedTexture),
306-
DestroyedAccelerationStructure(DestroyedAccelerationStructure),
307305
}
308306

309307
/// 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
@@ -2010,42 +2010,6 @@ impl AccelerationStructure for Blas {
20102010
}
20112011
}
20122012

2013-
impl Blas {
2014-
pub(crate) fn destroy(self: &Arc<Self>) -> Result<(), DestroyError> {
2015-
let device = &self.device;
2016-
2017-
let temp = {
2018-
let mut snatch_guard = device.snatchable_lock.write();
2019-
2020-
let raw = match self.raw.snatch(&mut snatch_guard) {
2021-
Some(raw) => raw,
2022-
None => {
2023-
return Err(DestroyError::AlreadyDestroyed);
2024-
}
2025-
};
2026-
2027-
drop(snatch_guard);
2028-
2029-
queue::TempResource::DestroyedAccelerationStructure(DestroyedAccelerationStructure {
2030-
raw: ManuallyDrop::new(raw),
2031-
device: Arc::clone(&self.device),
2032-
label: self.label().to_owned(),
2033-
bind_groups: WeakVec::new(),
2034-
})
2035-
};
2036-
2037-
if let Some(queue) = device.get_queue() {
2038-
let mut life_lock = queue.lock_life();
2039-
let last_submit_index = life_lock.get_blas_latest_submission_index(self);
2040-
if let Some(last_submit_index) = last_submit_index {
2041-
life_lock.schedule_resource_destruction(temp, last_submit_index);
2042-
}
2043-
}
2044-
2045-
Ok(())
2046-
}
2047-
}
2048-
20492013
crate::impl_resource_type!(Blas);
20502014
crate::impl_labeled!(Blas);
20512015
crate::impl_parent_device!(Blas);
@@ -2066,7 +2030,6 @@ pub struct Tlas {
20662030
/// The `label` from the descriptor used to create the resource.
20672031
pub(crate) label: String,
20682032
pub(crate) tracking_data: TrackingData,
2069-
pub(crate) bind_groups: Mutex<WeakVec<BindGroup>>,
20702033
}
20712034

20722035
impl Drop for Tlas {
@@ -2099,71 +2062,3 @@ crate::impl_labeled!(Tlas);
20992062
crate::impl_parent_device!(Tlas);
21002063
crate::impl_storage_item!(Tlas);
21012064
crate::impl_trackable!(Tlas);
2102-
2103-
impl Tlas {
2104-
pub(crate) fn destroy(self: &Arc<Self>) -> Result<(), DestroyError> {
2105-
let device = &self.device;
2106-
2107-
let temp = {
2108-
let mut snatch_guard = device.snatchable_lock.write();
2109-
2110-
let raw = match self.raw.snatch(&mut snatch_guard) {
2111-
Some(raw) => raw,
2112-
None => {
2113-
return Err(DestroyError::AlreadyDestroyed);
2114-
}
2115-
};
2116-
2117-
drop(snatch_guard);
2118-
2119-
queue::TempResource::DestroyedAccelerationStructure(DestroyedAccelerationStructure {
2120-
raw: ManuallyDrop::new(raw),
2121-
device: Arc::clone(&self.device),
2122-
label: self.label().to_owned(),
2123-
bind_groups: mem::take(&mut self.bind_groups.lock()),
2124-
})
2125-
};
2126-
2127-
if let Some(queue) = device.get_queue() {
2128-
let mut life_lock = queue.lock_life();
2129-
let last_submit_index = life_lock.get_tlas_latest_submission_index(self);
2130-
if let Some(last_submit_index) = last_submit_index {
2131-
life_lock.schedule_resource_destruction(temp, last_submit_index);
2132-
}
2133-
}
2134-
2135-
Ok(())
2136-
}
2137-
}
2138-
2139-
#[derive(Debug)]
2140-
pub struct DestroyedAccelerationStructure {
2141-
raw: ManuallyDrop<Box<dyn hal::DynAccelerationStructure>>,
2142-
device: Arc<Device>,
2143-
label: String,
2144-
// only filled if the acceleration structure is a TLAS
2145-
bind_groups: WeakVec<BindGroup>,
2146-
}
2147-
2148-
impl DestroyedAccelerationStructure {
2149-
pub fn label(&self) -> &dyn Debug {
2150-
&self.label
2151-
}
2152-
}
2153-
2154-
impl Drop for DestroyedAccelerationStructure {
2155-
fn drop(&mut self) {
2156-
let mut deferred = self.device.deferred_destroy.lock();
2157-
deferred.push(DeferredDestroy::BindGroups(mem::take(
2158-
&mut self.bind_groups,
2159-
)));
2160-
drop(deferred);
2161-
2162-
resource_log!("Destroy raw Buffer (destroyed) {:?}", self.label());
2163-
// SAFETY: We are in the Drop impl and we don't use self.raw anymore after this point.
2164-
let raw = unsafe { ManuallyDrop::take(&mut self.raw) };
2165-
unsafe {
2166-
hal::DynDevice::destroy_acceleration_structure(self.device.raw(), raw);
2167-
}
2168-
}
2169-
}

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: 1 addition & 5 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;
@@ -171,10 +171,6 @@ impl Blas {
171171
pub fn handle(&self) -> Option<u64> {
172172
self.handle
173173
}
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-
}
178174
}
179175

180176
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
@@ -3425,18 +3425,10 @@ impl crate::context::Context for ContextWebGpu {
34253425
unimplemented!("Raytracing not implemented for web");
34263426
}
34273427

3428-
fn blas_destroy(&self, _blas_data: &Self::BlasData) {
3429-
unimplemented!("Raytracing not implemented for web");
3430-
}
3431-
34323428
fn blas_drop(&self, _blas_data: &Self::BlasData) {
34333429
unimplemented!("Raytracing not implemented for web");
34343430
}
34353431

3436-
fn tlas_destroy(&self, _tlas_data: &Self::TlasData) {
3437-
unimplemented!("Raytracing not implemented for web");
3438-
}
3439-
34403432
fn tlas_drop(&self, _tlas_data: &Self::TlasData) {
34413433
unimplemented!("Raytracing not implemented for web");
34423434
}

0 commit comments

Comments
 (0)