buffer.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { Nullable, DataArray } from "types";
  2. import { Engine } from "Engine";
  3. import { Mesh, VertexBuffer } from "Mesh";
  4. /**
  5. * Class used to store data that will be store in GPU memory
  6. */
  7. export class Buffer {
  8. private _engine: Engine;
  9. private _buffer: Nullable<WebGLBuffer>;
  10. /** @hidden */
  11. public _data: Nullable<DataArray>;
  12. private _updatable: boolean;
  13. private _instanced: boolean;
  14. /**
  15. * Gets the byte stride.
  16. */
  17. public readonly byteStride: number;
  18. /**
  19. * Constructor
  20. * @param engine the engine
  21. * @param data the data to use for this buffer
  22. * @param updatable whether the data is updatable
  23. * @param stride the stride (optional)
  24. * @param postponeInternalCreation whether to postpone creating the internal WebGL buffer (optional)
  25. * @param instanced whether the buffer is instanced (optional)
  26. * @param useBytes set to true if the stride in in bytes (optional)
  27. */
  28. constructor(engine: any, data: DataArray, updatable: boolean, stride = 0, postponeInternalCreation = false, instanced = false, useBytes = false) {
  29. if (engine instanceof Mesh) { // old versions of VertexBuffer accepted 'mesh' instead of 'engine'
  30. this._engine = engine.getScene().getEngine();
  31. }
  32. else {
  33. this._engine = engine;
  34. }
  35. this._updatable = updatable;
  36. this._instanced = instanced;
  37. this._data = data;
  38. this.byteStride = useBytes ? stride : stride * Float32Array.BYTES_PER_ELEMENT;
  39. if (!postponeInternalCreation) { // by default
  40. this.create();
  41. }
  42. }
  43. /**
  44. * Create a new VertexBuffer based on the current buffer
  45. * @param kind defines the vertex buffer kind (position, normal, etc.)
  46. * @param offset defines offset in the buffer (0 by default)
  47. * @param size defines the size in floats of attributes (position is 3 for instance)
  48. * @param stride defines the stride size in floats in the buffer (the offset to apply to reach next value when data is interleaved)
  49. * @param instanced defines if the vertex buffer contains indexed data
  50. * @param useBytes defines if the offset and stride are in bytes
  51. * @returns the new vertex buffer
  52. */
  53. public createVertexBuffer(kind: string, offset: number, size: number, stride?: number, instanced?: boolean, useBytes = false): VertexBuffer {
  54. const byteOffset = useBytes ? offset : offset * Float32Array.BYTES_PER_ELEMENT;
  55. const byteStride = stride ? (useBytes ? stride : stride * Float32Array.BYTES_PER_ELEMENT) : this.byteStride;
  56. // a lot of these parameters are ignored as they are overriden by the buffer
  57. return new VertexBuffer(this._engine, this, kind, this._updatable, true, byteStride, instanced === undefined ? this._instanced : instanced, byteOffset, size, undefined, undefined, true);
  58. }
  59. // Properties
  60. /**
  61. * Gets a boolean indicating if the Buffer is updatable?
  62. * @returns true if the buffer is updatable
  63. */
  64. public isUpdatable(): boolean {
  65. return this._updatable;
  66. }
  67. /**
  68. * Gets current buffer's data
  69. * @returns a DataArray or null
  70. */
  71. public getData(): Nullable<DataArray> {
  72. return this._data;
  73. }
  74. /**
  75. * Gets underlying native buffer
  76. * @returns underlying native buffer
  77. */
  78. public getBuffer(): Nullable<WebGLBuffer> {
  79. return this._buffer;
  80. }
  81. /**
  82. * Gets the stride in float32 units (i.e. byte stride / 4).
  83. * May not be an integer if the byte stride is not divisible by 4.
  84. * DEPRECATED. Use byteStride instead.
  85. * @returns the stride in float32 units
  86. */
  87. public getStrideSize(): number {
  88. return this.byteStride / Float32Array.BYTES_PER_ELEMENT;
  89. }
  90. // Methods
  91. /**
  92. * Store data into the buffer. If the buffer was already used it will be either recreated or updated depending on isUpdatable property
  93. * @param data defines the data to store
  94. */
  95. public create(data: Nullable<DataArray> = null): void {
  96. if (!data && this._buffer) {
  97. return; // nothing to do
  98. }
  99. data = data || this._data;
  100. if (!data) {
  101. return;
  102. }
  103. if (!this._buffer) { // create buffer
  104. if (this._updatable) {
  105. this._buffer = this._engine.createDynamicVertexBuffer(data);
  106. this._data = data;
  107. } else {
  108. this._buffer = this._engine.createVertexBuffer(data);
  109. }
  110. } else if (this._updatable) { // update buffer
  111. this._engine.updateDynamicVertexBuffer(this._buffer, data);
  112. this._data = data;
  113. }
  114. }
  115. /** @hidden */
  116. public _rebuild(): void {
  117. this._buffer = null;
  118. this.create(this._data);
  119. }
  120. /**
  121. * Update current buffer data
  122. * @param data defines the data to store
  123. */
  124. public update(data: DataArray): void {
  125. this.create(data);
  126. }
  127. /**
  128. * Updates the data directly.
  129. * @param data the new data
  130. * @param offset the new offset
  131. * @param vertexCount the vertex count (optional)
  132. * @param useBytes set to true if the offset is in bytes
  133. */
  134. public updateDirectly(data: DataArray, offset: number, vertexCount?: number, useBytes: boolean = false): void {
  135. if (!this._buffer) {
  136. return;
  137. }
  138. if (this._updatable) { // update buffer
  139. this._engine.updateDynamicVertexBuffer(this._buffer, data, useBytes ? offset : offset * Float32Array.BYTES_PER_ELEMENT, (vertexCount ? vertexCount * this.byteStride : undefined));
  140. this._data = null;
  141. }
  142. }
  143. /**
  144. * Release all resources
  145. */
  146. public dispose(): void {
  147. if (!this._buffer) {
  148. return;
  149. }
  150. if (this._engine._releaseBuffer(this._buffer)) {
  151. this._buffer = null;
  152. }
  153. }
  154. }