Skip to content

Commit 4779b3a

Browse files
author
Jean Dumoulin
committed
Defers batch destruction until next update
1 parent 802d8fe commit 4779b3a

File tree

3 files changed

+16
-93
lines changed

3 files changed

+16
-93
lines changed

packages/engine/Source/DataSources/StaticGeometryColorBatch.js

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,8 @@ function Batch(
4747
this.subscriptions = new AssociativeArray();
4848
this.showsUpdated = new AssociativeArray();
4949
this.itemsToRemove = [];
50-
this.invalidated = false;
51-
52-
let removeMaterialSubscription;
53-
if (defined(depthFailMaterialProperty)) {
54-
removeMaterialSubscription =
55-
depthFailMaterialProperty.definitionChanged.addEventListener(
56-
Batch.prototype.onMaterialChanged,
57-
this,
58-
);
59-
}
60-
this.removeMaterialSubscription = removeMaterialSubscription;
6150
}
6251

63-
Batch.prototype.onMaterialChanged = function () {
64-
this.invalidated = true;
65-
};
66-
6752
Batch.prototype.isMaterial = function (updater) {
6853
const material = this.depthFailMaterialProperty;
6954
const updaterMaterial = updater.depthFailMaterialProperty;
@@ -391,9 +376,6 @@ Batch.prototype.destroy = function () {
391376
if (defined(oldPrimitive)) {
392377
primitives.remove(oldPrimitive);
393378
}
394-
if (defined(this.removeMaterialSubscription)) {
395-
this.removeMaterialSubscription();
396-
}
397379
};
398380

399381
/**
@@ -451,12 +433,9 @@ StaticGeometryColorBatch.prototype.add = function (time, updater) {
451433
function removeItem(items, updater) {
452434
const length = items.length;
453435
for (let i = length - 1; i >= 0; i--) {
454-
const item = items[i];
455-
if (item.remove(updater)) {
456-
if (item.updaters.length === 0) {
457-
items.splice(i, 1);
458-
item.destroy();
459-
}
436+
if (items[i].remove(updater)) {
437+
// If the item is now empty, delete it (deferred until the next update,
438+
// in case a new updater is added to the same item first).
460439
return true;
461440
}
462441
}
@@ -493,22 +472,13 @@ function updateItems(batch, items, time, isUpdated) {
493472
let i;
494473
for (i = length - 1; i >= 0; i--) {
495474
const item = items[i];
496-
if (item.invalidated) {
475+
if (item.updaters.length === 0) {
497476
items.splice(i, 1);
498-
const updaters = item.updaters.values;
499-
const updatersLength = updaters.length;
500-
for (let h = 0; h < updatersLength; h++) {
501-
batch.add(time, updaters[h]);
502-
}
503477
item.destroy();
504478
}
505479
}
506480

507-
length = items.length;
508-
for (i = 0; i < length; ++i) {
509-
isUpdated = items[i].update(time) && isUpdated;
510-
}
511-
return isUpdated;
481+
return items.every(item => item.update(time));
512482
}
513483

514484
StaticGeometryColorBatch.prototype.update = function (time) {

packages/engine/Source/DataSources/StaticGeometryPerMaterialBatch.js

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,10 @@ function Batch(
4343
this.depthFailMaterial = undefined;
4444
this.updatersWithAttributes = new AssociativeArray();
4545
this.attributes = new AssociativeArray();
46-
this.invalidated = false;
47-
this.removeMaterialSubscription =
48-
materialProperty.definitionChanged.addEventListener(
49-
Batch.prototype.onMaterialChanged,
50-
this,
51-
);
5246
this.subscriptions = new AssociativeArray();
5347
this.showsUpdated = new AssociativeArray();
5448
}
5549

56-
Batch.prototype.onMaterialChanged = function () {
57-
this.invalidated = true;
58-
};
59-
6050
Batch.prototype.isMaterial = function (updater) {
6151
const material = this.materialProperty;
6252
const updaterMaterial = updater.fillMaterialProperty;
@@ -378,7 +368,6 @@ Batch.prototype.destroy = function () {
378368
if (defined(oldPrimitive)) {
379369
primitives.remove(oldPrimitive);
380370
}
381-
this.removeMaterialSubscription();
382371
};
383372

384373
/**
@@ -426,40 +415,27 @@ StaticGeometryPerMaterialBatch.prototype.remove = function (updater) {
426415
const items = this._items;
427416
const length = items.length;
428417
for (let i = length - 1; i >= 0; i--) {
429-
const item = items[i];
430-
if (item.remove(updater)) {
431-
if (item.updaters.length === 0) {
432-
items.splice(i, 1);
433-
item.destroy();
434-
}
418+
if (items[i].remove(updater)) {
419+
// If the item is now empty, delete it (deferred until the next update,
420+
// in case a new updater is added to the same item first).
435421
break;
436422
}
437423
}
438424
};
439425

440426
StaticGeometryPerMaterialBatch.prototype.update = function (time) {
441-
let i;
442427
const items = this._items;
443428
const length = items.length;
444429

445-
for (i = length - 1; i >= 0; i--) {
430+
for (let i = length - 1; i >= 0; i--) {
446431
const item = items[i];
447-
if (item.invalidated) {
432+
if (item.updaters.length === 0) {
448433
items.splice(i, 1);
449-
const updaters = item.updaters.values;
450-
const updatersLength = updaters.length;
451-
for (let h = 0; h < updatersLength; h++) {
452-
this.add(time, updaters[h]);
453-
}
454434
item.destroy();
455435
}
456436
}
457437

458-
let isUpdated = true;
459-
for (i = 0; i < items.length; i++) {
460-
isUpdated = items[i].update(time) && isUpdated;
461-
}
462-
return isUpdated;
438+
return items.every(item => item.update(time));
463439
};
464440

465441
StaticGeometryPerMaterialBatch.prototype.getBoundingSphere = function (

packages/engine/Source/DataSources/StaticGroundPolylinePerMaterialBatch.js

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,13 @@ function Batch(
4444
this.material = undefined;
4545
this.updatersWithAttributes = new AssociativeArray();
4646
this.attributes = new AssociativeArray();
47-
this.invalidated = false;
48-
this.removeMaterialSubscription =
49-
materialProperty.definitionChanged.addEventListener(
50-
Batch.prototype.onMaterialChanged,
51-
this,
52-
);
5347
this.subscriptions = new AssociativeArray();
5448
this.showsUpdated = new AssociativeArray();
5549
this.zIndex = zIndex;
5650

5751
this._asynchronous = asynchronous;
5852
}
5953

60-
Batch.prototype.onMaterialChanged = function () {
61-
this.invalidated = true;
62-
};
63-
6454
// Check if the given updater's material is compatible with this batch
6555
Batch.prototype.isMaterial = function (updater) {
6656
const material = this.materialProperty;
@@ -325,7 +315,6 @@ Batch.prototype.destroy = function () {
325315
if (defined(oldPrimitive)) {
326316
orderedGroundPrimitives.remove(oldPrimitive);
327317
}
328-
this.removeMaterialSubscription();
329318
};
330319

331320
/**
@@ -371,12 +360,9 @@ StaticGroundPolylinePerMaterialBatch.prototype.remove = function (updater) {
371360
const items = this._items;
372361
const length = items.length;
373362
for (let i = length - 1; i >= 0; i--) {
374-
const item = items[i];
375-
if (item.remove(updater)) {
376-
if (item.updaters.length === 0) {
377-
items.splice(i, 1);
378-
item.destroy();
379-
}
363+
if (items[i].remove(updater)) {
364+
// If the item is now empty, delete it (deferred until the next update,
365+
// in case a new updater is added to the same item first).
380366
break;
381367
}
382368
}
@@ -389,22 +375,13 @@ StaticGroundPolylinePerMaterialBatch.prototype.update = function (time) {
389375

390376
for (i = length - 1; i >= 0; i--) {
391377
const item = items[i];
392-
if (item.invalidated) {
378+
if (item.updaters.length === 0) {
393379
items.splice(i, 1);
394-
const updaters = item.updaters.values;
395-
const updatersLength = updaters.length;
396-
for (let h = 0; h < updatersLength; h++) {
397-
this.add(time, updaters[h]);
398-
}
399380
item.destroy();
400381
}
401382
}
402383

403-
let isUpdated = true;
404-
for (i = 0; i < items.length; i++) {
405-
isUpdated = items[i].update(time) && isUpdated;
406-
}
407-
return isUpdated;
384+
return items.every(item => item.update(time));
408385
};
409386

410387
StaticGroundPolylinePerMaterialBatch.prototype.getBoundingSphere = function (

0 commit comments

Comments
 (0)