Skip to content
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 hash-based cache key #29479

Merged
merged 8 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
35 changes: 32 additions & 3 deletions examples/webgpu_performance.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,35 @@

import Stats from 'three/addons/libs/stats.module.js';

import { GUI } from 'three/addons/libs/lil-gui.module.min.js';

import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';

import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';

let camera, scene, renderer, stats;
let model;

const options = { static: true };

init();

function setStatic( object, value ) {

object.traverse( child => {

if ( child.isMesh ) {

child.static = value;

}

} );

}

function init() {

const container = document.createElement( 'div' );
Expand Down Expand Up @@ -83,26 +102,36 @@

loader.load( 'dungeon_warkarma.glb', async function ( gltf ) {

const model = gltf.scene;
model = gltf.scene;

// wait until the model can be added to the scene without blocking due to shader compilation

await renderer.compileAsync( model, camera, scene );

scene.add( model );

//

setStatic( model, options.static );

} );

} );



const controls = new OrbitControls( camera, renderer.domElement );
controls.minDistance = 2;
controls.maxDistance = 60;
controls.target.set( 0, 0, - 0.2 );
controls.update();

// gui

const gui = new GUI();
gui.add( options, 'static' ).onChange( () => {

setStatic( model, options.static );

} );

window.addEventListener( 'resize', onWindowResize );

Expand Down
7 changes: 4 additions & 3 deletions src/nodes/code/ScriptableNode.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Node from '../core/Node.js';
import { scriptableValue } from './ScriptableValueNode.js';
import { nodeProxy, float } from '../tsl/TSLBase.js';
import { hashArray, hashString } from '../core/NodeUtils.js';

class Resources extends Map {

Expand Down Expand Up @@ -445,15 +446,15 @@ class ScriptableNode extends Node {

getCacheKey( force ) {

const cacheKey = [ this.source, this.getDefaultOutputNode().getCacheKey( force ) ];
const values = [ hashString( this.source ), this.getDefaultOutputNode().getCacheKey( force ) ];

for ( const param in this.parameters ) {

cacheKey.push( this.parameters[ param ].getCacheKey( force ) );
values.push( this.parameters[ param ].getCacheKey( force ) );

}

return cacheKey.join( ',' );
return hashArray( values );

}

Expand Down
60 changes: 54 additions & 6 deletions src/nodes/core/NodeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,74 @@ import { Vector2 } from '../../math/Vector2.js';
import { Vector3 } from '../../math/Vector3.js';
import { Vector4 } from '../../math/Vector4.js';

// cyrb53 (c) 2018 bryc (github.com/bryc). License: Public domain. Attribution appreciated.
// A fast and simple 64-bit (or 53-bit) string hash function with decent collision resistance.
// Largely inspired by MurmurHash2/3, but with a focus on speed/simplicity.
// See https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript/52171480#52171480
// https://github.com/bryc/code/blob/master/jshash/experimental/cyrb53.js
export function hashString( str, seed = 0 ) {

let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;

for ( let i = 0, ch; i < str.length; i ++ ) {

ch = str.charCodeAt( i );
h1 = Math.imul( h1 ^ ch, 2654435761 );
h2 = Math.imul( h2 ^ ch, 1597334677 );

}

h1 = Math.imul( h1 ^ ( h1 >>> 16 ), 2246822507 );
h1 ^= Math.imul( h2 ^ ( h2 >>> 13 ), 3266489909 );
h2 = Math.imul( h2 ^ ( h2 >>> 16 ), 2246822507 );
h2 ^= Math.imul( h1 ^ ( h1 >>> 13 ), 3266489909 );

return 4294967296 * ( 2097151 & h2 ) + ( h1 >>> 0 );

}

export function hashArray( array ) {

const len = array.length;

let h1 = 0xdeadbeef ^ len, h2 = 0x41c6ce57 ^ len;

for ( let i = 0; i < len; i ++ ) {

const value = array[ i ];

h1 = Math.imul( h1 ^ value, 2654435761 );
h2 = Math.imul( h2 ^ value, 1597334677 );

}

h1 = Math.imul( h1 ^ ( h1 >>> 16 ), 2246822507 );
h1 ^= Math.imul( h2 ^ ( h2 >>> 13 ), 3266489909 );
h2 = Math.imul( h2 ^ ( h2 >>> 16 ), 2246822507 );
h2 ^= Math.imul( h1 ^ ( h1 >>> 13 ), 3266489909 );

return 4294967296 * ( 2097151 & h2 ) + ( h1 >>> 0 );

}

export function getCacheKey( object, force = false ) {

let cacheKey = '{';
const values = [];

if ( object.isNode === true ) {

cacheKey += object.id;
values.push( object.id );
object = object.getSelf();

}

for ( const { property, childNode } of getNodeChildren( object ) ) {

cacheKey += ',' + property.slice( 0, - 4 ) + ':' + childNode.getCacheKey( force );
values.push( values, hashString( property.slice( 0, - 4 ) ), childNode.getCacheKey( force ) );

}

cacheKey += '}';

return cacheKey;
return hashArray( values );

}

Expand Down
6 changes: 2 additions & 4 deletions src/nodes/display/ToneMappingNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { addMethodChaining, nodeObject, vec4 } from '../tsl/TSLCore.js';
import { rendererReference } from '../accessors/RendererReferenceNode.js';

import { NoToneMapping } from '../../constants.js';
import { hashArray } from '../core/NodeUtils.js';

class ToneMappingNode extends TempNode {

Expand All @@ -25,10 +26,7 @@ class ToneMappingNode extends TempNode {

getCacheKey() {

let cacheKey = super.getCacheKey();
cacheKey = '{toneMapping:' + this.toneMapping + ',nodes:' + cacheKey + '}';

return cacheKey;
return hashArray( [ super.getCacheKey(), this.toneMapping ] );

}

Expand Down
3 changes: 2 additions & 1 deletion src/nodes/lighting/AnalyticLightNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import { screenCoordinate } from '../display/ScreenNode.js';
import { HalfFloatType, LessCompare, RGFormat, VSMShadowMap, WebGPUCoordinateSystem } from '../../constants.js';
import { renderGroup } from '../core/UniformGroupNode.js';
import { hashArray } from '../core/NodeUtils.js';

const BasicShadowMap = Fn( ( { depthTexture, shadowCoord } ) => {

Expand Down Expand Up @@ -238,7 +239,7 @@

getCacheKey() {

return super.getCacheKey() + '-' + ( this.light.id + '-' + ( this.light.castShadow ? '1' : '0' ) );
return hashArray( super.getCacheKey(), this.light.id, this.light.castShadow ? 1 : 0 );
Fixed Show fixed Hide fixed

}

Expand Down
7 changes: 4 additions & 3 deletions src/renderers/common/ClippingContext.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Matrix3 } from '../../math/Matrix3.js';
import { Plane } from '../../math/Plane.js';
import { Vector4 } from '../../math/Vector4.js';
import { hashArray } from '../../nodes/core/NodeUtils.js';

const _plane = /*@__PURE__*/ new Plane();

Expand All @@ -20,7 +21,7 @@ class ClippingContext {

this.parentVersion = 0;
this.viewNormalMatrix = new Matrix3();
this.cacheKey = '';
this.cacheKey = 0;

}

Expand Down Expand Up @@ -95,7 +96,7 @@ class ClippingContext {
if ( update ) {

this.version ++;
this.cacheKey = `${ this.globalClippingCount }:${ this.localClippingEnabled === undefined ? false : this.localClippingEnabled }:`;
this.cacheKey = hashArray( [ this.globalClippingCount, this.localClippingEnabled === true ? 1 : 0 ] );

}

Expand Down Expand Up @@ -165,7 +166,7 @@ class ClippingContext {
if ( update ) {

this.version += parent.version;
this.cacheKey = parent.cacheKey + `:${ this.localClippingCount }:${ this.localClipIntersection === undefined ? false : this.localClipIntersection }`;
this.cacheKey = hashArray( [ parent.cacheKey, this.localClippingCount, this.localClipIntersection === true ? 1 : 0 ] );

}

Expand Down
9 changes: 4 additions & 5 deletions src/renderers/common/RenderContext.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Vector4 } from '../../math/Vector4.js';
import { hashArray } from '../../nodes/core/NodeUtils.js';

let id = 0;

Expand Down Expand Up @@ -50,17 +51,15 @@ export function getCacheKey( renderContext ) {

const { textures, activeCubeFace } = renderContext;

let key = '';
const values = [ activeCubeFace ];

for ( const texture of textures ) {

key += texture.id + ',';
values.push( texture.id );

}

key += activeCubeFace;

return key;
return hashArray( values );

}

Expand Down
15 changes: 12 additions & 3 deletions src/renderers/common/RenderObject.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { hashString } from '../../nodes/core/NodeUtils.js';
import ClippingContext from './ClippingContext.js';

let _id = 0;
Expand Down Expand Up @@ -369,7 +370,7 @@ export default class RenderObject {

}

return cacheKey;
return hashString( cacheKey );

}

Expand All @@ -383,13 +384,21 @@ export default class RenderObject {

// Environment Nodes Cache Key

return this.object.receiveShadow + ',' + this._nodes.getCacheKey( this.scene, this.lightsNode );
let cacheKey = this._nodes.getCacheKey( this.scene, this.lightsNode );

if ( this.object.receiveShadow ) {

cacheKey += 1;

}

return cacheKey;

}

getCacheKey() {

return this.getMaterialCacheKey() + ',' + this.getDynamicCacheKey();
return this.getMaterialCacheKey() + this.getDynamicCacheKey();

}

Expand Down
10 changes: 5 additions & 5 deletions src/renderers/common/nodes/Nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,15 @@ class Nodes extends DataMap {
const environmentNode = this.getEnvironmentNode( scene );
const fogNode = this.getFogNode( scene );

const cacheKey = [];
let cacheKey = 0;

if ( lightsNode ) cacheKey.push( lightsNode.getCacheKey( true ) );
if ( environmentNode ) cacheKey.push( environmentNode.getCacheKey() );
if ( fogNode ) cacheKey.push( fogNode.getCacheKey() );
if ( lightsNode ) cacheKey += lightsNode.getCacheKey( true );
if ( environmentNode ) cacheKey += environmentNode.getCacheKey();
if ( fogNode ) cacheKey += fogNode.getCacheKey();

cacheKeyData = {
callId,
cacheKey: cacheKey.join( ',' )
cacheKey
};

this.callHashCache.set( chain, cacheKeyData );
Expand Down