Browse Source

WebGLUniformsGroups: Fix and improve UBOs array caching system (#27374)

* fix and improve UBOs array caching system

* simplify cache check
Renaud Rohlinger 1 year ago
parent
commit
37cb4ae8dd
1 changed files with 15 additions and 40 deletions
  1. 15 40
      src/renderers/webgl/WebGLUniformsGroups.js

+ 15 - 40
src/renderers/webgl/WebGLUniformsGroups.js

@@ -102,7 +102,7 @@ function WebGLUniformsGroups( gl, info, capabilities, state ) {
 
 				const uniform = uniformArray[ j ];
 
-				if ( hasUniformChanged( uniform, i, cache ) === true ) {
+				if ( hasUniformChanged( uniform, i, j, cache ) === true ) {
 
 					const offset = uniform.__offset;
 
@@ -110,9 +110,9 @@ function WebGLUniformsGroups( gl, info, capabilities, state ) {
 
 					let arrayOffset = 0;
 
-					for ( let i = 0; i < values.length; i ++ ) {
+					for ( let k = 0; k < values.length; k ++ ) {
 
-						const value = values[ i ];
+						const value = values[ k ];
 
 						const info = getUniformSize( value );
 
@@ -161,31 +161,22 @@ function WebGLUniformsGroups( gl, info, capabilities, state ) {
 
 	}
 
-	function hasUniformChanged( uniform, index, cache ) {
+	function hasUniformChanged( uniform, index, indexArray, cache ) {
 
 		const value = uniform.value;
+		const indexString = index + '_' + indexArray;
 
-		if ( cache[ index ] === undefined ) {
+		if ( cache[ indexString ] === undefined ) {
 
 			// cache entry does not exist so far
 
 			if ( typeof value === 'number' || typeof value === 'boolean' ) {
 
-				cache[ index ] = value;
+				cache[ indexString ] = value;
 
 			} else {
 
-				const values = Array.isArray( value ) ? value : [ value ];
-
-				const tempValues = [];
-
-				for ( let i = 0; i < values.length; i ++ ) {
-
-					tempValues.push( values[ i ].clone() );
-
-				}
-
-				cache[ index ] = tempValues;
+				cache[ indexString ] = value.clone();
 
 			}
 
@@ -193,41 +184,25 @@ function WebGLUniformsGroups( gl, info, capabilities, state ) {
 
 		} else {
 
+			const cachedObject = cache[ indexString ];
+
 			// compare current value with cached entry
 
 			if ( typeof value === 'number' || typeof value === 'boolean' ) {
 
-				if ( cache[ index ] !== value ) {
+				if ( cachedObject !== value ) {
 
-					cache[ index ] = value;
+					cache[ indexString ] = value;
 					return true;
 
 				}
 
 			} else {
 
-				const cachedObjects = Array.isArray( cache[ index ] ) ? cache[ index ] : [ cache[ index ] ];
-				const values = Array.isArray( value ) ? value : [ value ];
-
-				for ( let i = 0; i < cachedObjects.length; i ++ ) {
-
-					const cachedObject = cachedObjects[ i ];
+				if ( cachedObject.equals( value ) === false ) {
 
-					if ( typeof cachedObject === 'number' || typeof cachedObject === 'boolean' ) {
-
-						if ( cachedObject !== values[ i ] ) {
-
-							cachedObjects[ i ] = values[ i ];
-							return true;
-
-						}
-
-					} else if ( cachedObject.equals( values[ i ] ) === false ) {
-
-						cachedObject.copy( values[ i ] );
-						return true;
-
-					}
+					cachedObject.copy( value );
+					return true;
 
 				}