Skip to content

TSL: Fix dispose() when using reflector(). #30933

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

Merged
merged 1 commit into from
Apr 15, 2025
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
30 changes: 28 additions & 2 deletions src/nodes/utils/ReflectorNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ class ReflectorNode extends TextureNode {

}

/**
* Frees internal resources. Should be called when the node is no longer in use.
*/
dispose() {

super.dispose();

this._reflectorBaseNode.dispose();

}

}

/**
Expand Down Expand Up @@ -269,9 +280,9 @@ class ReflectorBaseNode extends Node {
/**
* Weak map for managing render targets.
*
* @type {WeakMap<Camera, RenderTarget>}
* @type {Map<Camera, RenderTarget>}
*/
this.renderTargets = new WeakMap();
this.renderTargets = new Map();

/**
* Force render even if reflector is facing away from camera.
Expand Down Expand Up @@ -308,6 +319,21 @@ class ReflectorBaseNode extends Node {

}

/**
* Frees internal resources. Should be called when the node is no longer in use.
*/
dispose() {

super.dispose();

for ( const renderTarget of this.renderTargets.values() ) {

renderTarget.dispose();

}

}

/**
* Returns a virtual camera for the given camera. The virtual camera is used to
* render the scene from the reflector's view so correct reflections can be produced.
Expand Down
14 changes: 9 additions & 5 deletions src/renderers/common/Textures.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,6 @@ class Textures extends DataMap {

this._destroyTexture( texture );

this.info.memory.textures --;

};

texture.addEventListener( 'dispose', onDispose );
Expand Down Expand Up @@ -445,10 +443,16 @@ class Textures extends DataMap {
*/
_destroyTexture( texture ) {

this.backend.destroySampler( texture );
this.backend.destroyTexture( texture );
if ( this.has( texture ) === true ) {
Copy link
Collaborator Author

@Mugen87 Mugen87 Apr 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to make sure the following does not break the dispose logic:

renderTarget.dispose();
renderTarget.texture.dispose();

Since calling dispose() on the render target disposes all of its textures, it's important to ensure dispose() directly called on a texture does not trigger redundant operations.


this.backend.destroySampler( texture );
this.backend.destroyTexture( texture );

this.delete( texture );

this.delete( texture );
this.info.memory.textures --;

}

}

Expand Down