Skip to content

Commit 7d84965

Browse files
authored
fix: Don't track buffers and textures just to dispose of them (#1769)
1 parent 48ddfbd commit 7d84965

File tree

3 files changed

+7
-23
lines changed

3 files changed

+7
-23
lines changed

apps/typegpu-docs/src/content/docs/fundamentals/roots.mdx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ To unwrap a `TgpuVertexLayout` make sure to explicitly mark each of its attribut
6565

6666
## Destroying resources
6767

68-
Calling `root.destroy()` will destroy all resources created with it.
69-
It will also destroy the underlying WebGPU device, if it wasn't originally passed in via the `initFromDevice` function.
68+
Calling `root.destroy()` will call `device.destroy()` to let the browser know that it can free up all the resources. However, if an existing device is passed in via `tgpu.initFromDevice()`, this method does nothing.
7069

7170
```ts
7271
root.destroy(); // <- frees up all the resources

packages/typegpu/src/core/root/init.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,6 @@ class WithFragmentImpl implements WithFragment {
249249
}
250250
}
251251

252-
interface Disposable {
253-
destroy(): void;
254-
}
255-
256252
/**
257253
* Holds all data that is necessary to facilitate CPU and GPU communication.
258254
* Programs that share a root can interact via GPU buffers.
@@ -261,8 +257,6 @@ class TgpuRootImpl extends WithBindingImpl
261257
implements TgpuRoot, ExperimentalTgpuRoot {
262258
'~unstable': Omit<ExperimentalTgpuRoot, keyof TgpuRoot>;
263259

264-
private _disposables: Disposable[] = [];
265-
266260
private _unwrappedBindGroupLayouts = new WeakMemo(
267261
(key: TgpuBindGroupLayout) => key.unwrap(this),
268262
);
@@ -307,9 +301,7 @@ class TgpuRootImpl extends WithBindingImpl
307301
typeSchema: TData,
308302
initialOrBuffer?: Infer<TData> | GPUBuffer,
309303
): TgpuBuffer<TData> {
310-
const buffer = INTERNAL_createBuffer(this, typeSchema, initialOrBuffer);
311-
this._disposables.push(buffer);
312-
return buffer;
304+
return INTERNAL_createBuffer(this, typeSchema, initialOrBuffer);
313305
}
314306

315307
createUniform<TData extends AnyWgslData>(
@@ -319,7 +311,6 @@ class TgpuRootImpl extends WithBindingImpl
319311
const buffer = INTERNAL_createBuffer(this, typeSchema, initialOrBuffer)
320312
// biome-ignore lint/suspicious/noExplicitAny: i'm sure it's fine
321313
.$usage('uniform' as any);
322-
this._disposables.push(buffer);
323314

324315
return new TgpuBufferShorthandImpl('uniform', buffer);
325316
}
@@ -331,7 +322,6 @@ class TgpuRootImpl extends WithBindingImpl
331322
const buffer = INTERNAL_createBuffer(this, typeSchema, initialOrBuffer)
332323
// biome-ignore lint/suspicious/noExplicitAny: i'm sure it's fine
333324
.$usage('storage' as any);
334-
this._disposables.push(buffer);
335325

336326
return new TgpuBufferShorthandImpl('mutable', buffer);
337327
}
@@ -343,7 +333,6 @@ class TgpuRootImpl extends WithBindingImpl
343333
const buffer = INTERNAL_createBuffer(this, typeSchema, initialOrBuffer)
344334
// biome-ignore lint/suspicious/noExplicitAny: i'm sure it's fine
345335
.$usage('storage' as any);
346-
this._disposables.push(buffer);
347336

348337
return new TgpuBufferShorthandImpl('readonly', buffer);
349338
}
@@ -369,10 +358,6 @@ class TgpuRootImpl extends WithBindingImpl
369358
}
370359

371360
destroy() {
372-
for (const disposable of this._disposables) {
373-
disposable.destroy();
374-
}
375-
376361
clearTextureUtilsCache(this.device);
377362

378363
if (this._ownDevice) {
@@ -413,7 +398,6 @@ class TgpuRootImpl extends WithBindingImpl
413398
>
414399
> {
415400
const texture = INTERNAL_createTexture(props, this);
416-
this._disposables.push(texture);
417401
// biome-ignore lint/suspicious/noExplicitAny: <too much type wrangling>
418402
return texture as any;
419403
}

packages/typegpu/tests/root.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('TgpuRoot', () => {
6060
});
6161

6262
describe('.destroy', () => {
63-
it('should call .destroy on all buffers created with it', ({ root }) => {
63+
it('should call .destroy on the device it created', ({ root }) => {
6464
const buffer1 = root.createBuffer(d.f32);
6565
const buffer2 = root.createBuffer(d.i32);
6666
const buffer3 = root.createBuffer(d.u32);
@@ -71,9 +71,10 @@ describe('TgpuRoot', () => {
7171

7272
root.destroy();
7373

74-
expect(buffer1DestroySpy).toHaveBeenCalledOnce();
75-
expect(buffer2DestroySpy).toHaveBeenCalledOnce();
76-
expect(buffer3DestroySpy).toHaveBeenCalledOnce();
74+
expect(root.device.destroy).toHaveBeenCalledOnce();
75+
expect(buffer1DestroySpy).not.toHaveBeenCalled();
76+
expect(buffer2DestroySpy).not.toHaveBeenCalled();
77+
expect(buffer3DestroySpy).not.toHaveBeenCalled();
7778
});
7879
});
7980

0 commit comments

Comments
 (0)