-
-
Notifications
You must be signed in to change notification settings - Fork 36.1k
TSL: ShadowNode: prevent memory leaks. Add webgpu_test_memory #32395
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
+378
−2
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7cf8870
LST: prevent memory leaks caused by LightNodes
querielo f2e4ee7
Revert "LST: prevent memory leaks caused by LightNodes"
querielo cc56561
TSL: dispose shadow node to prevent memory. Add webgpu_test_memory
querielo 145a0ac
Review comments
querielo b10a05b
Review Comments: https://github.com/mrdoob/three.js/pull/32395#discus…
querielo ba41e15
webgpu_test_memory: fix memory leak in the test
querielo d3eb1b7
Review comment: https://github.com/mrdoob/three.js/pull/32395#discuss…
querielo c816045
Review comment: https://github.com/mrdoob/three.js/pull/32395#discuss…
querielo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,301 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <title>three.js webgpu - memory test I</title> | ||
| <meta charset="utf-8"> | ||
| <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> | ||
| <link type="text/css" rel="stylesheet" href="example.css"> | ||
| </head> | ||
|
|
||
| <body> | ||
| <div id="info" class="invert"> | ||
| <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a> | ||
|
|
||
| <div class="title-wrapper"> | ||
| <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>WebGPU Memory Test I</span> | ||
| </div> | ||
|
|
||
| <small> | ||
| This example tests memory management with WebGPU renderer. | ||
| <br /> Spheres are created, rendered, and disposed in each frame. | ||
| </small> | ||
| </div> | ||
|
|
||
| <script type="importmap"> | ||
| { | ||
| "imports": { | ||
| "three": "../build/three.webgpu.js", | ||
| "three/tsl": "../build/three.tsl.js", | ||
| "three/webgpu": "../build/three.webgpu.js", | ||
| "three/addons/": "./jsm/" | ||
| } | ||
| } | ||
| </script> | ||
|
|
||
| <script type="module"> | ||
|
|
||
| import * as THREE from 'three/webgpu'; | ||
| import { pass, mrt, directionToColor, normalView, screenUV, context, sample, colorToDirection } from 'three/tsl'; | ||
| import { outline } from 'three/addons/tsl/display/OutlineNode.js'; | ||
| import { ao } from 'three/addons/tsl/display/GTAONode.js'; | ||
|
|
||
| import { Inspector } from 'three/addons/inspector/Inspector.js'; | ||
|
|
||
| let camera, scene, renderer, light; | ||
| let generateMeshes = true; | ||
| let mesh; | ||
| let postProcessing; | ||
| let aoNode, outlineNode, scenePass, prePass; | ||
| const selectedObjects = []; | ||
|
|
||
| const params = { | ||
| castShadow: true, | ||
| enablePP: false, | ||
| enableOutline: true, | ||
| enableAO: true, | ||
| recreateLight: () => { | ||
|
|
||
| // Remove existing light | ||
| if ( light ) { | ||
|
|
||
| scene.remove( light ); | ||
| light.dispose(); | ||
| light = null; | ||
|
|
||
| } | ||
|
|
||
| // Create new directional light | ||
| light = new THREE.DirectionalLight( 0xffffff, 1 ); | ||
| light.position.set( Math.random() * 200 - 100, 100, Math.random() * 200 - 100 ); | ||
| light.castShadow = params.castShadow; | ||
| scene.add( light ); | ||
|
|
||
| }, | ||
| start: () => { | ||
|
|
||
| generateMeshes = true; | ||
|
|
||
| }, | ||
| stop: () => { | ||
|
|
||
| generateMeshes = false; | ||
|
|
||
| } | ||
| }; | ||
|
|
||
| init(); | ||
|
|
||
| async function init() { | ||
|
|
||
| const container = document.createElement( 'div' ); | ||
| document.body.appendChild( container ); | ||
|
|
||
| camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 10000 ); | ||
| camera.position.z = 200; | ||
|
|
||
| scene = new THREE.Scene(); | ||
| scene.background = new THREE.Color( 0xffffff ); | ||
|
|
||
| renderer = new THREE.WebGPURenderer(); | ||
| renderer.shadowMap.enabled = true; | ||
| renderer.setPixelRatio( window.devicePixelRatio ); | ||
| renderer.setSize( window.innerWidth, window.innerHeight ); | ||
| renderer.setAnimationLoop( animate ); | ||
| renderer.inspector = new Inspector(); | ||
| container.appendChild( renderer.domElement ); | ||
|
|
||
| await renderer.init(); | ||
|
|
||
| light = new THREE.DirectionalLight( 0xffffff, 1 ); | ||
| light.position.set( 0, 100, 0 ); | ||
| light.castShadow = true; | ||
| scene.add( light ); | ||
|
|
||
| const planeGeometry = new THREE.PlaneGeometry( 1000, 1000 ); | ||
| const planeMaterial = new THREE.MeshLambertMaterial( { color: 0xcccccc } ); | ||
| const plane = new THREE.Mesh( planeGeometry, planeMaterial ); | ||
| plane.rotation.x = - Math.PI / 2; | ||
| plane.position.y = - 100; | ||
| plane.receiveShadow = true; | ||
| scene.add( plane ); | ||
|
|
||
| // Inspector UI | ||
| const gui = renderer.inspector.createParameters( 'Settings' ); | ||
|
|
||
| gui.add( params, 'castShadow' ).name( 'Cast Shadow' ).onChange( ( value ) => { | ||
|
|
||
| if ( light ) light.castShadow = value; | ||
|
|
||
| } ); | ||
|
|
||
| const ppFolder = gui.addFolder( 'Post Processing' ); | ||
| ppFolder.add( params, 'enablePP' ).name( 'Enable' ).onChange( updatePostProcessing ); | ||
| ppFolder.add( params, 'enableOutline' ).name( 'Outline' ).onChange( updatePostProcessing ); | ||
| ppFolder.add( params, 'enableAO' ).name( 'AO' ).onChange( updatePostProcessing ); | ||
|
|
||
| gui.add( params, 'recreateLight' ).name( 'Recreate Directional Light' ); | ||
| gui.add( params, 'start' ).name( 'Start Creating Meshes' ); | ||
| gui.add( params, 'stop' ).name( 'Stop Creating Meshes' ); | ||
|
|
||
| window.addEventListener( 'resize', onWindowResize ); | ||
|
|
||
| } | ||
|
|
||
| function updatePostProcessing() { | ||
|
|
||
| if ( postProcessing ) { | ||
|
|
||
| postProcessing.dispose(); | ||
| postProcessing = null; | ||
|
|
||
| } | ||
|
|
||
| if ( scenePass ) { | ||
|
|
||
| scenePass.dispose(); | ||
| scenePass = null; | ||
|
|
||
| } | ||
|
|
||
| if ( prePass ) { | ||
|
|
||
| prePass.dispose(); | ||
| prePass = null; | ||
|
|
||
| } | ||
|
|
||
| if ( aoNode ) { | ||
|
|
||
| aoNode.dispose(); | ||
| aoNode = null; | ||
|
|
||
| } | ||
|
|
||
| if ( outlineNode ) { | ||
|
|
||
| outlineNode.dispose(); | ||
| outlineNode = null; | ||
|
|
||
| } | ||
|
|
||
| if ( params.enablePP ) { | ||
|
|
||
| postProcessing = new THREE.PostProcessing( renderer ); | ||
|
|
||
| scenePass = pass( scene, camera ); | ||
| let colorNode = scenePass; | ||
|
|
||
| if ( params.enableAO ) { | ||
|
|
||
| prePass = pass( scene, camera ); | ||
| prePass.setMRT( mrt( { | ||
| output: directionToColor( normalView ) | ||
| } ) ); | ||
|
|
||
| const prePassNormal = sample( ( uv ) => { | ||
|
|
||
| return colorToDirection( prePass.getTextureNode().sample( uv ) ); | ||
|
|
||
| } ); | ||
| const prePassDepth = prePass.getTextureNode( 'depth' ); | ||
|
|
||
| aoNode = ao( prePassDepth, prePassNormal, camera ); | ||
|
|
||
| scenePass.contextNode = context( { | ||
| ao: aoNode.getTextureNode().sample( screenUV ).r | ||
| } ); | ||
|
|
||
| } | ||
|
|
||
| if ( params.enableOutline ) { | ||
|
|
||
| outlineNode = outline( scene, camera, { | ||
| selectedObjects: selectedObjects | ||
| } ); | ||
| colorNode = colorNode.add( outlineNode ); | ||
|
|
||
| } | ||
|
|
||
| postProcessing.outputNode = colorNode; | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| function onWindowResize() { | ||
|
|
||
| const width = window.innerWidth; | ||
| const height = window.innerHeight; | ||
|
|
||
| camera.aspect = width / height; | ||
| camera.updateProjectionMatrix(); | ||
|
|
||
| renderer.setSize( width, height ); | ||
|
|
||
| } | ||
|
|
||
| function createImage() { | ||
|
|
||
| const canvas = document.createElement( 'canvas' ); | ||
| canvas.width = 256; | ||
| canvas.height = 256; | ||
|
|
||
| const canvas2DContext = canvas.getContext( '2d' ); | ||
| canvas2DContext.fillStyle = 'rgb(' + Math.floor( Math.random() * 256 ) + ',' + Math.floor( Math.random() * 256 ) + ',' + Math.floor( Math.random() * 256 ) + ')'; | ||
| canvas2DContext.fillRect( 0, 0, 256, 256 ); | ||
|
|
||
| return canvas; | ||
|
|
||
| } | ||
|
|
||
| // | ||
|
|
||
| function animate() { | ||
|
|
||
| if ( generateMeshes ) { | ||
|
|
||
| if ( mesh ) { | ||
|
|
||
| scene.remove( mesh ); | ||
|
|
||
| mesh.geometry.dispose(); | ||
| mesh.material.map.dispose(); | ||
| mesh.material.dispose(); | ||
|
|
||
| } | ||
|
|
||
| const geometry = new THREE.SphereGeometry( 50, Math.random() * 64, Math.random() * 32 ); | ||
|
|
||
| const texture = new THREE.CanvasTexture( createImage() ); | ||
|
|
||
| const material = new THREE.MeshLambertMaterial( { map: texture } ); | ||
|
|
||
| mesh = new THREE.Mesh( geometry, material ); | ||
| mesh.castShadow = true; | ||
|
|
||
| scene.add( mesh ); | ||
|
|
||
| if ( outlineNode ) { | ||
|
|
||
| selectedObjects[ 0 ] = mesh; | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| if ( postProcessing ) { | ||
|
|
||
| postProcessing.render(); | ||
|
|
||
| } else { | ||
|
|
||
| renderer.render( scene, camera ); | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| </script> | ||
|
|
||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,6 +82,8 @@ class DirectionalLight extends Light { | |
|
|
||
| this.shadow.dispose(); | ||
|
|
||
| super.dispose(); | ||
|
|
||
| } | ||
|
|
||
| copy( source ) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.