Skip to content

Commit

Permalink
WebGLUniformsGroups: Fix and improve UBOs array caching system (#27374)
Browse files Browse the repository at this point in the history
* fix and improve UBOs array caching system

* simplify cache check
  • Loading branch information
RenaudRohlinger authored Dec 14, 2023
1 parent fff4f00 commit 37cb4ae
Showing 1 changed file with 15 additions and 40 deletions.
55 changes: 15 additions & 40 deletions src/renderers/webgl/WebGLUniformsGroups.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,17 @@ 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;

const values = Array.isArray( uniform.value ) ? uniform.value : [ uniform.value ];

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 );

Expand Down Expand Up @@ -161,73 +161,48 @@ 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();

}

return true;

} 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;

}

Expand Down

0 comments on commit 37cb4ae

Please sign in to comment.