All files / engine/Source/Scene ResourceCacheStatistics.js

100% Statements 44/44
100% Branches 12/12
100% Functions 5/5
100% Lines 44/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150                                      14x               14x       14x 14x               1x 1251x 1251x   1251x 1251x                             1x   6388x     6387x     6387x 1x     6386x   6386x 6386x   6386x   6386x 6190x     6386x 336x     6386x 6386x                       1x   310x     309x     309x 1x     308x 308x 308x 308x                       1x   17700x     17699x 17699x 17699x   17699x 6318x     17699x 17699x   17699x 291x          
import Check from "../Core/Check.js";
import defined from "../Core/defined.js";
 
/**
 * Statistics for the GPU and CPU memory used by the models loaded through the
 * {@link ResourceCache}.
 *
 * @alias ResourceCacheStatistics
 * @constructor
 *
 * @private
 */
function ResourceCacheStatistics() {
  /**
   * The size of vertex buffers and index buffers loaded in the cache in bytes.
   *
   * @type {number}
   * @private
   */
  this.geometryByteLength = 0;
 
  /**
   * The size of all textures loaded in the cache in bytes
   *
   * @type {number}
   * @private
   */
  this.texturesByteLength = 0;
 
  // Track the sizes of resources by cache key. This is important so
  // removeLoader() can decrement the counts correctly.
  this._geometrySizes = {};
  this._textureSizes = {};
}
 
/**
 * Reset the memory counts
 *
 * @private
 */
ResourceCacheStatistics.prototype.clear = function () {
  this.geometryByteLength = 0;
  this.texturesByteLength = 0;
 
  this._geometrySizes = {};
  this._textureSizes = {};
};
 
/**
 * 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.
 * 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
 *
 * @private
 */
ResourceCacheStatistics.prototype.addGeometryLoader = function (loader) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("loader", loader);
  //>>includeEnd('debug');
 
  const cacheKey = loader.cacheKey;
 
  // Don't double count the same resource.
  if (this._geometrySizes.hasOwnProperty(cacheKey)) {
    return;
  }
 
  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;
};
 
/**
 * 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.
 *
 * @param {GltfTextureLoader} loader The texture loader with resources to track
 *
 * @private
 */
ResourceCacheStatistics.prototype.addTextureLoader = function (loader) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("loader", loader);
  //>>includeEnd('debug');
 
  const cacheKey = loader.cacheKey;
 
  // Don't double count the same resource.
  if (this._textureSizes.hasOwnProperty(cacheKey)) {
    return;
  }
 
  this._textureSizes[cacheKey] = 0;
  const totalSize = loader.texture.sizeInBytes;
  this.texturesByteLength += loader.texture.sizeInBytes;
  this._textureSizes[cacheKey] = totalSize;
};
 
/**
 * 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
 *
 * @private
 */
ResourceCacheStatistics.prototype.removeLoader = function (loader) {
  //>>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)) {
    this.geometryByteLength -= geometrySize;
  }
 
  const textureSize = this._textureSizes[cacheKey];
  delete this._textureSizes[cacheKey];
 
  if (defined(textureSize)) {
    this.texturesByteLength -= textureSize;
  }
};
 
export default ResourceCacheStatistics;