Skip to content

Commit

Permalink
Add memory usage based heurisitic for unloading tiles
Browse files Browse the repository at this point in the history
Adding a maximumMemoryUsage limiter which behaves similar to the
maximumNumberOfLoadedTiles property. When the cache exceeds usage, all
unneeded tiles are unloaded.
  • Loading branch information
shehzan10 committed Apr 26, 2017
1 parent f9409dd commit 70be1d0
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion Source/Scene/Cesium3DTileset.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ define([

this._maximumScreenSpaceError = defaultValue(options.maximumScreenSpaceError, 16);
this._maximumNumberOfLoadedTiles = defaultValue(options.maximumNumberOfLoadedTiles, 256);
this._maximumMemoryUsage = defaultValue(options.maximumMemoryUsage, 256);
this._styleEngine = new Cesium3DTileStyleEngine();

/**
Expand Down Expand Up @@ -469,6 +470,7 @@ define([
* });
*
* @see Cesium3DTileset#maximumNumberOfLoadedTiles
* @see Cesium3DTileset#maximumMemoryUsage
* @see Cesium3DTileset#trimLoadedTiles
*/
this.tileUnload = new Event();
Expand Down Expand Up @@ -848,6 +850,43 @@ define([
}
},

/**
* The maximum amount of memory in MB that can be used by the tileset.
* Tiles not in view are unloaded to enforce this.
* <p>
* If decreasing this value results in unloading tiles, the tiles are unloaded the next frame.
* </p>
* <p>
* If more tiles than <code>maximumMemoryUsage</code> are needed
* to meet the desired screen-space error, determined by {@link Cesium3DTileset#maximumScreenSpaceError},
* for the current view than the number of tiles loaded will exceed
* <code>maximumMemoryUsage</code>. For example, if the maximum is 256 MB, but
* 300 MB of tiles are needed to meet the screen-space error, then 300 MB of tiles may be loaded. When
* these tiles go out of view, they will be unloaded.
* </p>
*
* @memberof Cesium3DTileset.prototype
*
* @type {Number}
* @default 256
*
* @exception {DeveloperError} <code>maximumMemoryUsage</code> must be greater than or equal to zero.
*/
maximumMemoryUsage : {
get : function() {
return this._maximumMemoryUsage;
},
set : function(value) {
//>>includeStart('debug', pragmas.debug);
if (value < 0) {
throw new DeveloperError('maximumMemoryUsage must be greater than or equal to zero');
}
//>>includeEnd('debug');

this._maximumMemoryUsage = value;
}
},

/**
* The tileset's bounding volume.
*
Expand Down Expand Up @@ -1977,13 +2016,16 @@ define([
var replacementList = tileset._replacementList;
var tileUnload = tileset.tileUnload;

var totalMemoryUsage = stats.textureMemorySizeInBytes + stats.vertexMemorySizeInBytes + stats.batchTableMemorySizeInBytes;
var maximumMemoryUsageInBytes = tileset._maximumMemoryUsage * 1024 * 1024;

// Traverse the list only to the sentinel since tiles/nodes to the
// right of the sentinel were used this frame.
//
// The sub-list to the left of the sentinel is ordered from LRU to MRU.
var sentinel = tileset._replacementSentinel;
var node = replacementList.head;
while ((node !== sentinel) && ((replacementList.length > maximumNumberOfLoadedTiles) || trimTiles)) {
while ((node !== sentinel) && ((replacementList.length > maximumNumberOfLoadedTiles) || trimTiles || (totalMemoryUsage > maximumMemoryUsageInBytes))) {
var tile = node.item;

decrementPointAndFeatureLoadCounts(tileset, tile.content);
Expand All @@ -1995,6 +2037,7 @@ define([
replacementList.remove(currentNode);

--stats.numberContentReady;
totalMemoryUsage = tileset._statistics.textureMemorySizeInBytes + tileset._statistics.vertexMemorySizeInBytes + tileset._statistics.batchTableMemorySizeInBytes;
}
}

Expand Down

0 comments on commit 70be1d0

Please sign in to comment.