storageBuffer.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * This class is a small wrapper around a native buffer that can be read and/or written
  3. */
  4. export class StorageBuffer {
  5. /**
  6. * Creates a new storage buffer instance
  7. * @param engine The engine the buffer will be created inside
  8. * @param size The size of the buffer in bytes
  9. * @param creationFlags flags to use when creating the buffer (see undefined). The BUFFER_CREATIONFLAG_STORAGE flag will be automatically added.
  10. * @param label defines the label of the buffer (for debug purpose)
  11. */
  12. constructor(engine, size, creationFlags = 3, label) {
  13. this._engine = engine;
  14. this._label = label;
  15. this._engine._storageBuffers.push(this);
  16. this._create(size, creationFlags);
  17. }
  18. _create(size, creationFlags) {
  19. this._bufferSize = size;
  20. this._creationFlags = creationFlags;
  21. this._buffer = this._engine.createStorageBuffer(size, creationFlags, this._label);
  22. }
  23. /** @internal */
  24. _rebuild() {
  25. this._create(this._bufferSize, this._creationFlags);
  26. }
  27. /**
  28. * Gets underlying native buffer
  29. * @returns underlying native buffer
  30. */
  31. getBuffer() {
  32. return this._buffer;
  33. }
  34. /**
  35. * Updates the storage buffer
  36. * @param data the data used to update the storage buffer
  37. * @param byteOffset the byte offset of the data (optional)
  38. * @param byteLength the byte length of the data (optional)
  39. */
  40. update(data, byteOffset, byteLength) {
  41. if (!this._buffer) {
  42. return;
  43. }
  44. this._engine.updateStorageBuffer(this._buffer, data, byteOffset, byteLength);
  45. }
  46. /**
  47. * Reads data from the storage buffer
  48. * @param offset The offset in the storage buffer to start reading from (default: 0)
  49. * @param size The number of bytes to read from the storage buffer (default: capacity of the buffer)
  50. * @param buffer The buffer to write the data we have read from the storage buffer to (optional)
  51. * @param noDelay If true, a call to flushFramebuffer will be issued so that the data can be read back immediately. This can speed up data retrieval, at the cost of a small perf penalty (default: false).
  52. * @returns If not undefined, returns the (promise) buffer (as provided by the 4th parameter) filled with the data, else it returns a (promise) Uint8Array with the data read from the storage buffer
  53. */
  54. read(offset, size, buffer, noDelay) {
  55. return this._engine.readFromStorageBuffer(this._buffer, offset, size, buffer, noDelay);
  56. }
  57. /**
  58. * Disposes the storage buffer
  59. */
  60. dispose() {
  61. const storageBuffers = this._engine._storageBuffers;
  62. const index = storageBuffers.indexOf(this);
  63. if (index !== -1) {
  64. storageBuffers[index] = storageBuffers[storageBuffers.length - 1];
  65. storageBuffers.pop();
  66. }
  67. this._engine._releaseBuffer(this._buffer);
  68. this._buffer = null;
  69. }
  70. }
  71. //# sourceMappingURL=storageBuffer.js.map