From 0ec9a77ef03a5f72f06d2da2404d9e4217973cee Mon Sep 17 00:00:00 2001 From: sunag Date: Mon, 15 Dec 2025 17:24:20 -0300 Subject: [PATCH] UniformsGroup: Add range cache and fix clear old update ranges (#32561) --- src/renderers/common/Bindings.js | 6 ++ src/renderers/common/Uniform.js | 8 +++ src/renderers/common/UniformsGroup.js | 60 ++++++++++++++++--- .../webgpu/utils/WebGPUBindingUtils.js | 2 - 4 files changed, 67 insertions(+), 9 deletions(-) diff --git a/src/renderers/common/Bindings.js b/src/renderers/common/Bindings.js index f7675a18046e38..6a79971512bf8c 100644 --- a/src/renderers/common/Bindings.js +++ b/src/renderers/common/Bindings.js @@ -364,6 +364,12 @@ class Bindings extends DataMap { } + if ( binding.isBuffer && binding.updateRanges.length > 0 ) { + + binding.clearUpdateRanges(); + + } + } if ( needsBindingsUpdate === true ) { diff --git a/src/renderers/common/Uniform.js b/src/renderers/common/Uniform.js index ad0d3d6a752c7e..aa8c6093b2e035 100644 --- a/src/renderers/common/Uniform.js +++ b/src/renderers/common/Uniform.js @@ -61,6 +61,14 @@ class Uniform { */ this.offset = 0; + /** + * This property is set by {@link UniformsGroup} and marks + * the index position in the uniform array. + * + * @type {number} + */ + this.index = - 1; + } /** diff --git a/src/renderers/common/UniformsGroup.js b/src/renderers/common/UniformsGroup.js index 84628d35bd26c6..164b48e832c0c8 100644 --- a/src/renderers/common/UniformsGroup.js +++ b/src/renderers/common/UniformsGroup.js @@ -47,6 +47,51 @@ class UniformsGroup extends UniformBuffer { */ this.uniforms = []; + /** + * A cache for the uniform update ranges. + * + * @private + * @type {Map} + */ + this._updateRangeCache = new Map(); + + } + + /** + * Adds a uniform's update range to this buffer. + * + * @param {Uniform} uniform - The uniform. + */ + addUniformUpdateRange( uniform ) { + + const index = uniform.index; + + if ( this._updateRangeCache.has( index ) !== true ) { + + const updateRanges = this.updateRanges; + + const start = uniform.offset; + const count = uniform.itemSize; + + const range = { start, count }; + + updateRanges.push( range ); + + this._updateRangeCache.set( index, range ); + + } + + } + + /** + * Clears all update ranges of this buffer. + */ + clearUpdateRanges() { + + this._updateRangeCache.clear(); + + super.clearUpdateRanges(); + } /** @@ -156,6 +201,7 @@ class UniformsGroup extends UniformBuffer { } uniform.offset = offset / bytesPerElement; + uniform.index = i; offset += itemSize; @@ -235,7 +281,7 @@ class UniformsGroup extends UniformBuffer { b[ offset ] = a[ offset ] = v; updated = true; - this.addUpdateRange( offset, 1 ); + this.addUniformUpdateRange( uniform ); } @@ -267,7 +313,7 @@ class UniformsGroup extends UniformBuffer { updated = true; - this.addUpdateRange( offset, 2 ); + this.addUniformUpdateRange( uniform ); } @@ -300,7 +346,7 @@ class UniformsGroup extends UniformBuffer { updated = true; - this.addUpdateRange( offset, 3 ); + this.addUniformUpdateRange( uniform ); } @@ -334,7 +380,7 @@ class UniformsGroup extends UniformBuffer { updated = true; - this.addUpdateRange( offset, 4 ); + this.addUniformUpdateRange( uniform ); } @@ -366,7 +412,7 @@ class UniformsGroup extends UniformBuffer { updated = true; - this.addUpdateRange( offset, 3 ); + this.addUniformUpdateRange( uniform ); } @@ -406,7 +452,7 @@ class UniformsGroup extends UniformBuffer { updated = true; - this.addUpdateRange( offset, 12 ); + this.addUniformUpdateRange( uniform ); } @@ -435,7 +481,7 @@ class UniformsGroup extends UniformBuffer { setArray( a, e, offset ); updated = true; - this.addUpdateRange( offset, 16 ); + this.addUniformUpdateRange( uniform ); } diff --git a/src/renderers/webgpu/utils/WebGPUBindingUtils.js b/src/renderers/webgpu/utils/WebGPUBindingUtils.js index e1d3257b9a14e9..3f67a361e479db 100644 --- a/src/renderers/webgpu/utils/WebGPUBindingUtils.js +++ b/src/renderers/webgpu/utils/WebGPUBindingUtils.js @@ -224,8 +224,6 @@ class WebGPUBindingUtils { } - binding.clearUpdateRanges(); - } }