Home Reference Source

viewer/buffersetpool.js

  1. /**
  2. * Keeps track of a collections of buffersets that can be reused. The main reason is that we don't have to allocate new memory for each new bufferset
  3. */
  4. export class BufferSetPool {
  5. constructor(maxPoolSize, stats) {
  6. this.maxPoolSize = maxPoolSize;
  7. this.stats = stats;
  8. this.currentPoolSize = 0;
  9. this.available = new Set();
  10. this.used = new Set();
  11. window.buffersetpool = this;
  12. }
  13. lease(bufferManager, hasTransparency, color, sizes) {
  14. if (this.currentPoolSize >= this.maxPoolSize) {
  15. throw "Maximum pool size exceeded";
  16. }
  17. if (this.available.size > 0) {
  18. var bufferSet = this.available.keys().next().value;
  19. this.used.add(bufferSet);
  20. this.available.delete(bufferSet);
  21. this.stats.setParameter("BufferSet pool", "Used", this.used.size);
  22. this.stats.setParameter("BufferSet pool", "Available", this.available.size);
  23. this.stats.setParameter("BufferSet pool", "Total memory", this.currentPoolSize * bufferManager.getDefaultByteSize());
  24. return bufferSet;
  25. }
  26. var newBufferSet = bufferManager.createBufferSet(hasTransparency, color, sizes);
  27. this.currentPoolSize++;
  28. this.used.add(newBufferSet);
  29. this.stats.setParameter("BufferSet pool", "Used", this.used.size);
  30. this.stats.setParameter("BufferSet pool", "Available", this.available.size);
  31. this.stats.setParameter("BufferSet pool", "Total memory", this.currentPoolSize * bufferManager.getDefaultByteSize());
  32. return newBufferSet;
  33. }
  34. release(bufferSet) {
  35. this.used.delete(bufferSet);
  36. this.available.add(bufferSet);
  37. this.stats.setParameter("BufferSet pool", "Used", this.used.size);
  38. this.stats.setParameter("BufferSet pool", "Available", this.available.size);
  39. bufferSet.positionsIndex = 0;
  40. bufferSet.normalsIndex = 0;
  41. bufferSet.indicesIndex = 0;
  42. bufferSet.nrIndices = 0;
  43. bufferSet.colorsIndex = 0;
  44. }
  45. cleanup() {
  46. this.available.clear();
  47. this.used.clear();
  48. this.currentPoolSize = 0;
  49. this.stats.setParameter("BufferSet pool", "Used", this.used.size);
  50. this.stats.setParameter("BufferSet pool", "Available", this.available.size);
  51. this.stats.setParameter("BufferSet pool", "Total memory", 0);
  52. }
  53. }