From 43066b9c746759e80784000349137d37d7770abc Mon Sep 17 00:00:00 2001 From: sunag Date: Tue, 18 Jun 2024 16:48:00 -0300 Subject: [PATCH 1/2] fix uniform update tests --- .../jsm/renderers/common/UniformsGroup.js | 98 +++++++++++++------ examples/webgpu_sandbox.html | 3 + 2 files changed, 71 insertions(+), 30 deletions(-) diff --git a/examples/jsm/renderers/common/UniformsGroup.js b/examples/jsm/renderers/common/UniformsGroup.js index a73b85de32114a..77571f1c1f7e92 100644 --- a/examples/jsm/renderers/common/UniformsGroup.js +++ b/examples/jsm/renderers/common/UniformsGroup.js @@ -9,6 +9,8 @@ class UniformsGroup extends UniformBuffer { this.isUniformsGroup = true; + this._values = null; + // the order of uniforms in this array must match the order of uniforms in the shader this.uniforms = []; @@ -37,6 +39,18 @@ class UniformsGroup extends UniformBuffer { } + get values() { + + if ( this._values === null ) { + + this._values = Array.from( this.buffer ); + + } + + return this._values; + + } + get buffer() { let buffer = this._buffer; @@ -132,13 +146,15 @@ class UniformsGroup extends UniformBuffer { let updated = false; - const a = this.buffer; + const a = this.values; const v = uniform.getValue(); const offset = uniform.offset; if ( a[ offset ] !== v ) { - a[ offset ] = v; + const b = this.buffer; + + b[ offset ] = a[ offset ] = v; updated = true; } @@ -151,14 +167,16 @@ class UniformsGroup extends UniformBuffer { let updated = false; - const a = this.buffer; + const a = this.values; const v = uniform.getValue(); const offset = uniform.offset; if ( a[ offset + 0 ] !== v.x || a[ offset + 1 ] !== v.y ) { - a[ offset + 0 ] = v.x; - a[ offset + 1 ] = v.y; + const b = this.buffer; + + b[ offset + 0 ] = a[ offset + 0 ] = v.x; + b[ offset + 1 ] = a[ offset + 1 ] = v.y; updated = true; @@ -172,15 +190,17 @@ class UniformsGroup extends UniformBuffer { let updated = false; - const a = this.buffer; + const a = this.values; const v = uniform.getValue(); const offset = uniform.offset; if ( a[ offset + 0 ] !== v.x || a[ offset + 1 ] !== v.y || a[ offset + 2 ] !== v.z ) { - a[ offset + 0 ] = v.x; - a[ offset + 1 ] = v.y; - a[ offset + 2 ] = v.z; + const b = this.buffer; + + b[ offset + 0 ] = a[ offset + 0 ] = v.x; + b[ offset + 1 ] = a[ offset + 1 ] = v.y; + b[ offset + 2 ] = a[ offset + 2 ] = v.z; updated = true; @@ -194,16 +214,18 @@ class UniformsGroup extends UniformBuffer { let updated = false; - const a = this.buffer; + const a = this.values; const v = uniform.getValue(); const offset = uniform.offset; if ( a[ offset + 0 ] !== v.x || a[ offset + 1 ] !== v.y || a[ offset + 2 ] !== v.z || a[ offset + 4 ] !== v.w ) { - a[ offset + 0 ] = v.x; - a[ offset + 1 ] = v.y; - a[ offset + 2 ] = v.z; - a[ offset + 3 ] = v.w; + const b = this.buffer; + + b[ offset + 0 ] = a[ offset + 0 ] = v.x; + b[ offset + 1 ] = a[ offset + 1 ] = v.y; + b[ offset + 2 ] = a[ offset + 2 ] = v.z; + b[ offset + 3 ] = a[ offset + 3 ] = v.w; updated = true; @@ -217,15 +239,17 @@ class UniformsGroup extends UniformBuffer { let updated = false; - const a = this.buffer; + const a = this.values; const c = uniform.getValue(); const offset = uniform.offset; if ( a[ offset + 0 ] !== c.r || a[ offset + 1 ] !== c.g || a[ offset + 2 ] !== c.b ) { - a[ offset + 0 ] = c.r; - a[ offset + 1 ] = c.g; - a[ offset + 2 ] = c.b; + const b = this.buffer; + + b[ offset + 0 ] = a[ offset + 0 ] = c.r; + b[ offset + 1 ] = a[ offset + 1 ] = c.g; + b[ offset + 2 ] = a[ offset + 2 ] = c.b; updated = true; @@ -239,7 +263,7 @@ class UniformsGroup extends UniformBuffer { let updated = false; - const a = this.buffer; + const a = this.values; const e = uniform.getValue().elements; const offset = uniform.offset; @@ -247,15 +271,17 @@ class UniformsGroup extends UniformBuffer { a[ offset + 4 ] !== e[ 3 ] || a[ offset + 5 ] !== e[ 4 ] || a[ offset + 6 ] !== e[ 5 ] || a[ offset + 8 ] !== e[ 6 ] || a[ offset + 9 ] !== e[ 7 ] || a[ offset + 10 ] !== e[ 8 ] ) { - a[ offset + 0 ] = e[ 0 ]; - a[ offset + 1 ] = e[ 1 ]; - a[ offset + 2 ] = e[ 2 ]; - a[ offset + 4 ] = e[ 3 ]; - a[ offset + 5 ] = e[ 4 ]; - a[ offset + 6 ] = e[ 5 ]; - a[ offset + 8 ] = e[ 6 ]; - a[ offset + 9 ] = e[ 7 ]; - a[ offset + 10 ] = e[ 8 ]; + const b = this.buffer; + + b[ offset + 0 ] = a[ offset + 0 ] = e[ 0 ]; + b[ offset + 1 ] = a[ offset + 1 ] = e[ 1 ]; + b[ offset + 2 ] = a[ offset + 2 ] = e[ 2 ]; + b[ offset + 4 ] = a[ offset + 4 ] = e[ 3 ]; + b[ offset + 5 ] = a[ offset + 5 ] = e[ 4 ]; + b[ offset + 6 ] = a[ offset + 6 ] = e[ 5 ]; + b[ offset + 8 ] = a[ offset + 8 ] = e[ 6 ]; + b[ offset + 9 ] = a[ offset + 9 ] = e[ 7 ]; + b[ offset + 10 ] = a[ offset + 10 ] = e[ 8 ]; updated = true; @@ -269,13 +295,15 @@ class UniformsGroup extends UniformBuffer { let updated = false; - const a = this.buffer; + const a = this.values; const e = uniform.getValue().elements; const offset = uniform.offset; if ( arraysEqual( a, e, offset ) === false ) { - a.set( e, offset ); + const b = this.buffer; + b.set( e, offset ); + setArray( a, e, offset ); updated = true; } @@ -286,6 +314,16 @@ class UniformsGroup extends UniformBuffer { } +function setArray( a, b, offset ) { + + for ( let i = 0, l = b.length; i < l; i ++ ) { + + a[ offset + i ] = b[ i ]; + + } + +} + function arraysEqual( a, b, offset ) { for ( let i = 0, l = b.length; i < l; i ++ ) { diff --git a/examples/webgpu_sandbox.html b/examples/webgpu_sandbox.html index b34fa9cd9b1ea8..d133bded6919db 100644 --- a/examples/webgpu_sandbox.html +++ b/examples/webgpu_sandbox.html @@ -201,6 +201,9 @@ } + window.upCount = 0; + console.log( 'frame' ); + renderer.render( scene, camera ); } From 449fc404948a4c861cf2d6bf5af05de176be3788 Mon Sep 17 00:00:00 2001 From: sunag Date: Tue, 18 Jun 2024 16:49:48 -0300 Subject: [PATCH 2/2] cleanup --- examples/webgpu_sandbox.html | 3 --- 1 file changed, 3 deletions(-) diff --git a/examples/webgpu_sandbox.html b/examples/webgpu_sandbox.html index d133bded6919db..b34fa9cd9b1ea8 100644 --- a/examples/webgpu_sandbox.html +++ b/examples/webgpu_sandbox.html @@ -201,9 +201,6 @@ } - window.upCount = 0; - console.log( 'frame' ); - renderer.render( scene, camera ); }