Skip to content

Commit 0ec9a77

Browse files
authored
UniformsGroup: Add range cache and fix clear old update ranges (#32561)
1 parent 81f1bca commit 0ec9a77

4 files changed

Lines changed: 67 additions & 9 deletions

File tree

src/renderers/common/Bindings.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,12 @@ class Bindings extends DataMap {
364364

365365
}
366366

367+
if ( binding.isBuffer && binding.updateRanges.length > 0 ) {
368+
369+
binding.clearUpdateRanges();
370+
371+
}
372+
367373
}
368374

369375
if ( needsBindingsUpdate === true ) {

src/renderers/common/Uniform.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ class Uniform {
6161
*/
6262
this.offset = 0;
6363

64+
/**
65+
* This property is set by {@link UniformsGroup} and marks
66+
* the index position in the uniform array.
67+
*
68+
* @type {number}
69+
*/
70+
this.index = - 1;
71+
6472
}
6573

6674
/**

src/renderers/common/UniformsGroup.js

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,51 @@ class UniformsGroup extends UniformBuffer {
4747
*/
4848
this.uniforms = [];
4949

50+
/**
51+
* A cache for the uniform update ranges.
52+
*
53+
* @private
54+
* @type {Map<number, {start: number, count: number}>}
55+
*/
56+
this._updateRangeCache = new Map();
57+
58+
}
59+
60+
/**
61+
* Adds a uniform's update range to this buffer.
62+
*
63+
* @param {Uniform} uniform - The uniform.
64+
*/
65+
addUniformUpdateRange( uniform ) {
66+
67+
const index = uniform.index;
68+
69+
if ( this._updateRangeCache.has( index ) !== true ) {
70+
71+
const updateRanges = this.updateRanges;
72+
73+
const start = uniform.offset;
74+
const count = uniform.itemSize;
75+
76+
const range = { start, count };
77+
78+
updateRanges.push( range );
79+
80+
this._updateRangeCache.set( index, range );
81+
82+
}
83+
84+
}
85+
86+
/**
87+
* Clears all update ranges of this buffer.
88+
*/
89+
clearUpdateRanges() {
90+
91+
this._updateRangeCache.clear();
92+
93+
super.clearUpdateRanges();
94+
5095
}
5196

5297
/**
@@ -156,6 +201,7 @@ class UniformsGroup extends UniformBuffer {
156201
}
157202

158203
uniform.offset = offset / bytesPerElement;
204+
uniform.index = i;
159205

160206
offset += itemSize;
161207

@@ -235,7 +281,7 @@ class UniformsGroup extends UniformBuffer {
235281
b[ offset ] = a[ offset ] = v;
236282
updated = true;
237283

238-
this.addUpdateRange( offset, 1 );
284+
this.addUniformUpdateRange( uniform );
239285

240286
}
241287

@@ -267,7 +313,7 @@ class UniformsGroup extends UniformBuffer {
267313

268314
updated = true;
269315

270-
this.addUpdateRange( offset, 2 );
316+
this.addUniformUpdateRange( uniform );
271317

272318
}
273319

@@ -300,7 +346,7 @@ class UniformsGroup extends UniformBuffer {
300346

301347
updated = true;
302348

303-
this.addUpdateRange( offset, 3 );
349+
this.addUniformUpdateRange( uniform );
304350

305351
}
306352

@@ -334,7 +380,7 @@ class UniformsGroup extends UniformBuffer {
334380

335381
updated = true;
336382

337-
this.addUpdateRange( offset, 4 );
383+
this.addUniformUpdateRange( uniform );
338384

339385
}
340386

@@ -366,7 +412,7 @@ class UniformsGroup extends UniformBuffer {
366412

367413
updated = true;
368414

369-
this.addUpdateRange( offset, 3 );
415+
this.addUniformUpdateRange( uniform );
370416

371417
}
372418

@@ -406,7 +452,7 @@ class UniformsGroup extends UniformBuffer {
406452

407453
updated = true;
408454

409-
this.addUpdateRange( offset, 12 );
455+
this.addUniformUpdateRange( uniform );
410456

411457
}
412458

@@ -435,7 +481,7 @@ class UniformsGroup extends UniformBuffer {
435481
setArray( a, e, offset );
436482
updated = true;
437483

438-
this.addUpdateRange( offset, 16 );
484+
this.addUniformUpdateRange( uniform );
439485

440486
}
441487

src/renderers/webgpu/utils/WebGPUBindingUtils.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,6 @@ class WebGPUBindingUtils {
224224

225225
}
226226

227-
binding.clearUpdateRanges();
228-
229227
}
230228

231229
}

0 commit comments

Comments
 (0)