-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
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
WebGPURenderer: Introduce TimestampQueryPool #30359
Merged
sunag
merged 13 commits into
mrdoob:dev
from
RenaudRohlinger:utsubo/feat/timestamp-pool
Jan 21, 2025
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1aa1be9
WebGPURenderer: Introduce renderer.resolveAllTimestampsAsync(type)
RenaudRohlinger 69e6abe
cleanup
RenaudRohlinger 9610f07
update some examples and cleanup
RenaudRohlinger 64aaac1
white space
RenaudRohlinger 8043b31
test bird screenshot
RenaudRohlinger baefb56
fix bird e2e
RenaudRohlinger d0f6182
revert bird screenshot
RenaudRohlinger 97fa3e0
feedbacks
RenaudRohlinger 17c66fd
better to not use async as it affects the loop and slow CPU/frames
RenaudRohlinger 332b16a
dispose timestampPools, fix dispose of webglstate and fix webgl conte…
RenaudRohlinger 2efb229
fix CI
RenaudRohlinger f66fb97
Update Backend.js
Mugen87 dcc1255
Update Backend.js
Mugen87 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
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -28,7 +28,8 @@ | |
"three": "../build/three.webgpu.js", | ||
"three/webgpu": "../build/three.webgpu.js", | ||
"three/tsl": "../build/three.tsl.js", | ||
"three/addons/": "./jsm/" | ||
"three/addons/": "./jsm/", | ||
"stats-gl": "https://cdn.jsdelivr.net/npm/[email protected]/dist/main.js" | ||
} | ||
} | ||
</script> | ||
|
@@ -40,7 +41,7 @@ | |
|
||
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; | ||
|
||
import Stats from 'three/addons/libs/stats.module.js'; | ||
import Stats from 'stats-gl'; | ||
import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; | ||
|
||
let container, stats; | ||
|
@@ -168,7 +169,7 @@ | |
|
||
// | ||
|
||
renderer = new THREE.WebGPURenderer( { antialias: true } ); | ||
renderer = new THREE.WebGPURenderer( { antialias: true, forceWebGL: false } ); | ||
renderer.setPixelRatio( window.devicePixelRatio ); | ||
renderer.setSize( window.innerWidth, window.innerHeight ); | ||
renderer.setAnimationLoop( animate ); | ||
|
@@ -310,30 +311,29 @@ | |
// Destructure uniforms | ||
const { alignment, separation, cohesion, deltaTime, rayOrigin, rayDirection } = effectController; | ||
|
||
const zoneRadius = separation.add( alignment ).add( cohesion ); | ||
const separationThresh = separation.div( zoneRadius ); | ||
const alignmentThresh = ( separation.add( alignment ) ).div( zoneRadius ); | ||
const zoneRadiusSq = zoneRadius.mul( zoneRadius ); | ||
const zoneRadius = separation.add( alignment ).add( cohesion ).toConst(); | ||
const separationThresh = separation.div( zoneRadius ).toConst(); | ||
const alignmentThresh = ( separation.add( alignment ) ).div( zoneRadius ).toConst(); | ||
const zoneRadiusSq = zoneRadius.mul( zoneRadius ).toConst(); | ||
|
||
const position = positionStorage.element( instanceIndex ); | ||
const velocity = velocityStorage.element( instanceIndex ); | ||
|
||
// Add influence of pointer position to velocity. | ||
const directionToRay = rayOrigin.sub( position ); | ||
const projectionLength = dot( directionToRay, rayDirection ); | ||
|
||
const closestPoint = rayOrigin.sub( rayDirection.mul( projectionLength ) ); | ||
// Cache current bird's position and velocity outside the loop | ||
const birdIndex = instanceIndex.toConst( 'birdIndex' ); | ||
const position = positionStorage.element( birdIndex ).toVar(); | ||
const velocity = velocityStorage.element( birdIndex ).toVar(); | ||
|
||
const directionToClosestPoint = closestPoint.sub( position ); | ||
const distanceToClosestPoint = length( directionToClosestPoint ); | ||
const distanceToClosestPointSq = distanceToClosestPoint.mul( distanceToClosestPoint ); | ||
// Add influence of pointer position to velocity using cached position | ||
const directionToRay = rayOrigin.sub( position ).toConst(); | ||
const projectionLength = dot( directionToRay, rayDirection ).toConst(); | ||
const closestPoint = rayOrigin.sub( rayDirection.mul( projectionLength ) ).toConst(); | ||
const directionToClosestPoint = closestPoint.sub( position ).toConst(); | ||
const distanceToClosestPoint = length( directionToClosestPoint ).toConst(); | ||
const distanceToClosestPointSq = distanceToClosestPoint.mul( distanceToClosestPoint ).toConst(); | ||
|
||
const rayRadius = float( 150.0 ); | ||
const rayRadiusSq = rayRadius.mul( rayRadius ); | ||
const rayRadius = float( 150.0 ).toConst(); | ||
const rayRadiusSq = rayRadius.mul( rayRadius ).toConst(); | ||
|
||
If( distanceToClosestPointSq.lessThan( rayRadiusSq ), () => { | ||
|
||
// Scale bird velocity inversely with distance from prey radius center. | ||
const velocityAdjust = ( distanceToClosestPointSq.div( rayRadiusSq ).sub( 1.0 ) ).mul( deltaTime ).mul( 100.0 ); | ||
velocity.addAssign( normalize( directionToClosestPoint ).mul( velocityAdjust ) ); | ||
limit.addAssign( 5.0 ); | ||
|
@@ -347,11 +347,18 @@ | |
|
||
Loop( { start: uint( 0 ), end: uint( BIRDS ), type: 'uint', condition: '<' }, ( { i } ) => { | ||
|
||
If( i.equal( birdIndex ), () => { | ||
|
||
Continue(); | ||
|
||
} ); | ||
|
||
// Cache bird's position and velocity | ||
|
||
const birdPosition = positionStorage.element( i ); | ||
const dirToBird = birdPosition.sub( position ); | ||
const distToBird = length( dirToBird ); | ||
|
||
// Don't apply any changes to velocity if the distance to this bird is negligible. | ||
If( distToBird.lessThan( 0.0001 ), () => { | ||
|
||
Continue(); | ||
|
@@ -413,8 +420,10 @@ | |
|
||
} ); | ||
|
||
} )().compute( BIRDS ); | ||
// Write back the final velocity to storage | ||
velocityStorage.element( birdIndex ).assign( velocity ); | ||
|
||
} )().compute( BIRDS ); | ||
computePosition = Fn( () => { | ||
|
||
const { deltaTime } = effectController; | ||
|
@@ -430,7 +439,13 @@ | |
|
||
scene.add( birdMesh ); | ||
|
||
stats = new Stats(); | ||
stats = new Stats( { | ||
precision: 3, | ||
horizontal: false, | ||
trackGPU: true, | ||
trackCPT: true | ||
} ); | ||
stats.init( renderer ); | ||
container.appendChild( stats.dom ); | ||
|
||
container.style.touchAction = 'none'; | ||
|
@@ -468,6 +483,7 @@ | |
function animate() { | ||
|
||
render(); | ||
renderer.resolveTimestampsAsync(); | ||
stats.update(); | ||
|
||
} | ||
|
@@ -489,6 +505,7 @@ | |
|
||
renderer.compute( computeVelocity ); | ||
renderer.compute( computePosition ); | ||
renderer.resolveTimestampsAsync( 'compute' ); | ||
renderer.render( scene, camera ); | ||
|
||
// Move pointer away so we only affect birds when moving the mouse | ||
|
This file contains 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 |
---|---|---|
|
@@ -14,11 +14,11 @@ | |
<script type="importmap"> | ||
{ | ||
"imports": { | ||
"three": "../build/three.webgpu.js", | ||
"three/webgpu": "../build/three.webgpu.js", | ||
"three": "../src/Three.WebGPU.js", | ||
"three/webgpu": "../src/Three.WebGPU.js", | ||
"three/tsl": "../build/three.tsl.js", | ||
"three/addons/": "./jsm/", | ||
"stats-gl": "https://cdn.jsdelivr.net/npm/stats-gl@2.2.8/dist/main.js" | ||
"stats-gl": "https://cdn.jsdelivr.net/npm/stats-gl@3.6.0/dist/main.js" | ||
} | ||
} | ||
</script> | ||
|
@@ -273,7 +273,9 @@ | |
|
||
stats = new Stats( { | ||
precision: 3, | ||
horizontal: false | ||
horizontal: false, | ||
trackGPU: true, | ||
trackCPT: true | ||
} ); | ||
stats.init( renderer ); | ||
document.body.appendChild( stats.dom ); | ||
|
@@ -343,11 +345,12 @@ | |
|
||
scene.overrideMaterial = collisionPosMaterial; | ||
renderer.setRenderTarget( collisionPosRT ); | ||
await renderer.renderAsync( scene, collisionCamera ); | ||
renderer.render( scene, collisionCamera ); | ||
|
||
// compute | ||
|
||
await renderer.computeAsync( computeParticles ); | ||
renderer.compute( computeParticles ); | ||
renderer.resolveTimestampsAsync( 'compute' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better to have a static definition for this like |
||
|
||
// result | ||
|
||
|
@@ -356,6 +359,7 @@ | |
|
||
await postProcessing.renderAsync(); | ||
|
||
renderer.resolveTimestampsAsync(); | ||
stats.update(); | ||
|
||
} | ||
|
This file contains 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 |
---|---|---|
|
@@ -17,19 +17,24 @@ | |
"three": "../build/three.webgpu.js", | ||
"three/webgpu": "../build/three.webgpu.js", | ||
"three/tsl": "../build/three.tsl.js", | ||
"three/addons/": "./jsm/" | ||
"three/addons/": "./jsm/", | ||
"stats-gl": "https://cdn.jsdelivr.net/npm/[email protected]/dist/main.js" | ||
} | ||
} | ||
</script> | ||
|
||
<script type="module"> | ||
|
||
import * as THREE from 'three'; | ||
|
||
import Stats from 'stats-gl'; | ||
|
||
|
||
import { Fn, uniform, instancedArray, float, vec2, vec3, color, instanceIndex } from 'three/tsl'; | ||
|
||
import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; | ||
|
||
let camera, scene, renderer; | ||
let camera, scene, renderer, stats; | ||
let computeNode; | ||
|
||
const pointerVector = new THREE.Vector2( - 10.0, - 10.0 ); // Out of bounds first | ||
|
@@ -120,6 +125,19 @@ | |
renderer.setAnimationLoop( animate ); | ||
document.body.appendChild( renderer.domElement ); | ||
|
||
stats = new Stats( { | ||
precision: 4, | ||
horizontal: false, | ||
trackGPU: true, | ||
trackCPT: true, | ||
logsPerSecond: 10, | ||
graphsPerSecond: 60, | ||
samplesGraph: 30, | ||
} ); | ||
stats.init( renderer ); | ||
document.body.appendChild( stats.dom ); | ||
stats.dom.style.position = 'absolute'; | ||
|
||
window.addEventListener( 'resize', onWindowResize ); | ||
window.addEventListener( 'mousemove', onMouseMove ); | ||
|
||
|
@@ -158,8 +176,17 @@ | |
function animate() { | ||
|
||
renderer.compute( computeNode ); | ||
renderer.resolveTimestampsAsync( 'compute' ); | ||
|
||
renderer.render( scene, camera ); | ||
|
||
|
||
renderer.resolveTimestampsAsync().then( () => { | ||
|
||
stats.update(); | ||
|
||
} ); | ||
|
||
} | ||
|
||
</script> | ||
|
This file contains 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 |
---|---|---|
|
@@ -20,7 +20,9 @@ | |
"imports": { | ||
"three": "../build/three.webgpu.js", | ||
"three/webgpu": "../build/three.webgpu.js", | ||
"three/addons/": "./jsm/" | ||
"three/addons/": "./jsm/", | ||
"stats-gl": "https://cdn.jsdelivr.net/npm/[email protected]/dist/main.js" | ||
|
||
} | ||
} | ||
</script> | ||
|
@@ -29,7 +31,7 @@ | |
|
||
import * as THREE from 'three'; | ||
|
||
import Stats from 'three/addons/libs/stats.module.js'; | ||
import Stats from 'stats-gl'; | ||
|
||
import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; | ||
|
||
|
@@ -71,7 +73,7 @@ | |
scene = new THREE.Scene(); | ||
|
||
|
||
renderer = new THREE.WebGPURenderer( { antialias: true, forceWebGL: false } ); | ||
renderer = new THREE.WebGPURenderer( { antialias: true } ); | ||
renderer.setPixelRatio( window.devicePixelRatio ); | ||
renderer.setSize( window.innerWidth, window.innerHeight ); | ||
renderer.toneMapping = THREE.ACESFilmicToneMapping; | ||
|
@@ -82,7 +84,13 @@ | |
|
||
// | ||
|
||
stats = new Stats(); | ||
stats = new Stats( { | ||
precision: 3, | ||
horizontal: false, | ||
trackGPU: true, | ||
} ); | ||
stats.init( renderer ); | ||
|
||
document.body.appendChild( stats.dom ); | ||
|
||
new RGBELoader() | ||
|
@@ -149,9 +157,10 @@ | |
|
||
// | ||
|
||
function render() { | ||
async function render() { | ||
|
||
renderer.renderAsync( scene, camera ); | ||
await renderer.renderAsync( scene, camera ); | ||
renderer.resolveTimestampsAsync( 'render' ); | ||
|
||
stats.update(); | ||
|
||
|
This file contains 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 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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These break in
gh-pages
:https://threejs.org/examples/?q=snow#webgpu_compute_particles_snow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#30432