cesium/packages/engine/Source/Scene/ResourceCacheStatistics.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

150 lines
4.0 KiB
JavaScript
Raw Permalink Normal View History

2022-06-01 01:23:02 +08:00
import Check from "../Core/Check.js";
import defined from "../Core/defined.js";
2022-06-01 01:23:02 +08:00
/**
2022-06-07 04:07:34 +08:00
* Statistics for the GPU and CPU memory used by the models loaded through the
2022-06-01 01:23:02 +08:00
* {@link ResourceCache}.
*
* @alias ResourceCacheStatistics
* @constructor
*
* @private
*/
2022-08-11 06:22:27 +08:00
function ResourceCacheStatistics() {
2022-06-01 01:23:02 +08:00
/**
* The size of vertex buffers and index buffers loaded in the cache in bytes.
*
* @type {number}
2022-06-01 01:23:02 +08:00
* @private
*/
this.geometryByteLength = 0;
2022-06-01 01:23:02 +08:00
/**
* The size of all textures loaded in the cache in bytes
*
* @type {number}
2022-06-01 01:23:02 +08:00
* @private
*/
this.texturesByteLength = 0;
2022-06-01 01:23:02 +08:00
// Track the sizes of resources by cache key. This is important so
2022-06-08 03:23:39 +08:00
// removeLoader() can decrement the counts correctly.
this._geometrySizes = {};
this._textureSizes = {};
}
2022-06-01 01:23:02 +08:00
/**
* Reset the memory counts
*
* @private
*/
ResourceCacheStatistics.prototype.clear = function () {
this.geometryByteLength = 0;
this.texturesByteLength = 0;
this._geometrySizes = {};
this._textureSizes = {};
};
2022-06-01 01:23:02 +08:00
/**
* Track the resources for a vertex or index buffer loader. This should be called after a loader is ready; that
* is it has been loaded and processed.
2022-06-01 01:23:02 +08:00
* This method handles the following cases gracefully:
* <ul>
* <li>If the loader is added twice, its resources will not be double-counted</li>
* <li>If the geometry has a CPU copy of the GPU buffer, it will be added to the count</li>
* </ul>
* @param {GltfVertexBufferLoader|GltfIndexBufferLoader} loader The geometry buffer with resources to track
2022-06-09 03:31:26 +08:00
*
* @private
2022-06-01 01:23:02 +08:00
*/
ResourceCacheStatistics.prototype.addGeometryLoader = function (loader) {
2022-06-01 01:23:02 +08:00
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("loader", loader);
//>>includeEnd('debug');
2022-05-28 04:49:10 +08:00
const cacheKey = loader.cacheKey;
2022-05-31 23:29:31 +08:00
// Don't double count the same resource.
if (this._geometrySizes.hasOwnProperty(cacheKey)) {
return;
}
2022-05-31 23:29:31 +08:00
this._geometrySizes[cacheKey] = 0;
const buffer = loader.buffer;
const typedArray = loader.typedArray;
let totalSize = 0;
if (defined(buffer)) {
totalSize += buffer.sizeInBytes;
}
if (defined(typedArray)) {
totalSize += typedArray.byteLength;
}
this.geometryByteLength += totalSize;
this._geometrySizes[cacheKey] = totalSize;
};
2022-06-01 01:23:02 +08:00
/**
* Track the resources for a texture loader. This should be called after a loader is ready; that
* is it has been loaded and processed.
* If the loader is added twice, its resources will not be double-counted.
*
2022-06-01 01:23:02 +08:00
* @param {GltfTextureLoader} loader The texture loader with resources to track
2022-06-09 03:31:26 +08:00
*
* @private
2022-06-01 01:23:02 +08:00
*/
2023-03-18 02:20:46 +08:00
ResourceCacheStatistics.prototype.addTextureLoader = function (loader) {
2022-06-01 01:23:02 +08:00
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("loader", loader);
//>>includeEnd('debug');
2022-05-28 04:49:10 +08:00
const cacheKey = loader.cacheKey;
2022-05-31 23:29:31 +08:00
// Don't double count the same resource.
if (this._textureSizes.hasOwnProperty(cacheKey)) {
return;
}
2022-05-28 04:49:10 +08:00
this._textureSizes[cacheKey] = 0;
const totalSize = loader.texture.sizeInBytes;
this.texturesByteLength += loader.texture.sizeInBytes;
this._textureSizes[cacheKey] = totalSize;
};
2022-06-01 01:23:02 +08:00
/**
* Remove a loader's resources from the memory count. The loader's cache key
* is used to determine information about the resource, so this method can
* be used both for geometry and textures. If the loader does not have any
* tracked resources, this is a no-op.
* @param {ResourceLoader} loader The resource loader to remove from the cache
2022-06-09 03:31:26 +08:00
*
* @private
2022-06-01 01:23:02 +08:00
*/
ResourceCacheStatistics.prototype.removeLoader = function (loader) {
2022-06-01 01:23:02 +08:00
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("loader", loader);
//>>includeEnd('debug');
const cacheKey = loader.cacheKey;
const geometrySize = this._geometrySizes[cacheKey];
delete this._geometrySizes[cacheKey];
if (defined(geometrySize)) {
2022-05-31 23:29:31 +08:00
this.geometryByteLength -= geometrySize;
}
const textureSize = this._textureSizes[cacheKey];
delete this._textureSizes[cacheKey];
if (defined(textureSize)) {
2022-05-31 23:29:31 +08:00
this.texturesByteLength -= textureSize;
}
};
2022-08-11 06:22:27 +08:00
export default ResourceCacheStatistics;