ResourceCacheStatistics.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import Check from "../Core/Check.js";
  2. import defined from "../Core/defined.js";
  3. /**
  4. * Statistics for the GPU and CPU memory used by the models loaded through the
  5. * {@link ResourceCache}.
  6. *
  7. * @alias ResourceCacheStatistics
  8. * @constructor
  9. *
  10. * @private
  11. */
  12. function ResourceCacheStatistics() {
  13. /**
  14. * The size of vertex buffers and index buffers loaded in the cache in bytes.
  15. *
  16. * @type {number}
  17. * @private
  18. */
  19. this.geometryByteLength = 0;
  20. /**
  21. * The size of all textures loaded in the cache in bytes
  22. *
  23. * @type {number}
  24. * @private
  25. */
  26. this.texturesByteLength = 0;
  27. // Track the sizes of resources by cache key. This is important so
  28. // removeLoader() can decrement the counts correctly.
  29. this._geometrySizes = {};
  30. this._textureSizes = {};
  31. }
  32. /**
  33. * Reset the memory counts
  34. *
  35. * @private
  36. */
  37. ResourceCacheStatistics.prototype.clear = function () {
  38. this.geometryByteLength = 0;
  39. this.texturesByteLength = 0;
  40. this._geometrySizes = {};
  41. this._textureSizes = {};
  42. };
  43. /**
  44. * Track the resources for a vertex or index buffer loader. This should be called after a loader is ready; that
  45. * is it has been loaded and processed.
  46. * This method handles the following cases gracefully:
  47. * <ul>
  48. * <li>If the loader is added twice, its resources will not be double-counted</li>
  49. * <li>If the geometry has a CPU copy of the GPU buffer, it will be added to the count</li>
  50. * </ul>
  51. * @param {GltfVertexBufferLoader|GltfIndexBufferLoader} loader The geometry buffer with resources to track
  52. *
  53. * @private
  54. */
  55. ResourceCacheStatistics.prototype.addGeometryLoader = function (loader) {
  56. //>>includeStart('debug', pragmas.debug);
  57. Check.typeOf.object("loader", loader);
  58. //>>includeEnd('debug');
  59. const cacheKey = loader.cacheKey;
  60. // Don't double count the same resource.
  61. if (this._geometrySizes.hasOwnProperty(cacheKey)) {
  62. return;
  63. }
  64. this._geometrySizes[cacheKey] = 0;
  65. const buffer = loader.buffer;
  66. const typedArray = loader.typedArray;
  67. let totalSize = 0;
  68. if (defined(buffer)) {
  69. totalSize += buffer.sizeInBytes;
  70. }
  71. if (defined(typedArray)) {
  72. totalSize += typedArray.byteLength;
  73. }
  74. this.geometryByteLength += totalSize;
  75. this._geometrySizes[cacheKey] = totalSize;
  76. };
  77. /**
  78. * Track the resources for a texture loader. This should be called after a loader is ready; that
  79. * is it has been loaded and processed.
  80. * If the loader is added twice, its resources will not be double-counted.
  81. *
  82. * @param {GltfTextureLoader} loader The texture loader with resources to track
  83. *
  84. * @private
  85. */
  86. ResourceCacheStatistics.prototype.addTextureLoader = function (loader) {
  87. //>>includeStart('debug', pragmas.debug);
  88. Check.typeOf.object("loader", loader);
  89. //>>includeEnd('debug');
  90. const cacheKey = loader.cacheKey;
  91. // Don't double count the same resource.
  92. if (this._textureSizes.hasOwnProperty(cacheKey)) {
  93. return;
  94. }
  95. this._textureSizes[cacheKey] = 0;
  96. const totalSize = loader.texture.sizeInBytes;
  97. this.texturesByteLength += loader.texture.sizeInBytes;
  98. this._textureSizes[cacheKey] = totalSize;
  99. };
  100. /**
  101. * Remove a loader's resources from the memory count. The loader's cache key
  102. * is used to determine information about the resource, so this method can
  103. * be used both for geometry and textures. If the loader does not have any
  104. * tracked resources, this is a no-op.
  105. * @param {ResourceLoader} loader The resource loader to remove from the cache
  106. *
  107. * @private
  108. */
  109. ResourceCacheStatistics.prototype.removeLoader = function (loader) {
  110. //>>includeStart('debug', pragmas.debug);
  111. Check.typeOf.object("loader", loader);
  112. //>>includeEnd('debug');
  113. const cacheKey = loader.cacheKey;
  114. const geometrySize = this._geometrySizes[cacheKey];
  115. delete this._geometrySizes[cacheKey];
  116. if (defined(geometrySize)) {
  117. this.geometryByteLength -= geometrySize;
  118. }
  119. const textureSize = this._textureSizes[cacheKey];
  120. delete this._textureSizes[cacheKey];
  121. if (defined(textureSize)) {
  122. this.texturesByteLength -= textureSize;
  123. }
  124. };
  125. export default ResourceCacheStatistics;