buffer.d.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. import type { Nullable, DataArray, FloatArray } from "../types";
  2. import type { AbstractEngine } from "../Engines/abstractEngine";
  3. import { DataBuffer } from "./dataBuffer";
  4. /**
  5. * Class used to store data that will be store in GPU memory
  6. */
  7. export declare class Buffer {
  8. private _engine;
  9. private _buffer;
  10. /** @internal */
  11. _data: Nullable<DataArray>;
  12. private _updatable;
  13. private _instanced;
  14. private _divisor;
  15. private _isAlreadyOwned;
  16. private _isDisposed;
  17. private _label?;
  18. /**
  19. * Gets a boolean indicating if the Buffer is disposed
  20. */
  21. get isDisposed(): boolean;
  22. /**
  23. * Gets the byte stride.
  24. */
  25. readonly byteStride: number;
  26. /**
  27. * Constructor
  28. * @param engine the engine
  29. * @param data the data to use for this buffer
  30. * @param updatable whether the data is updatable
  31. * @param stride the stride (optional)
  32. * @param postponeInternalCreation whether to postpone creating the internal WebGL buffer (optional)
  33. * @param instanced whether the buffer is instanced (optional)
  34. * @param useBytes set to true if the stride in in bytes (optional)
  35. * @param divisor sets an optional divisor for instances (1 by default)
  36. * @param label defines the label of the buffer (for debug purpose)
  37. */
  38. constructor(engine: AbstractEngine, data: DataArray | DataBuffer, updatable: boolean, stride?: number, postponeInternalCreation?: boolean, instanced?: boolean, useBytes?: boolean, divisor?: number, label?: string);
  39. /**
  40. * Create a new VertexBuffer based on the current buffer
  41. * @param kind defines the vertex buffer kind (position, normal, etc.)
  42. * @param offset defines offset in the buffer (0 by default)
  43. * @param size defines the size in floats of attributes (position is 3 for instance)
  44. * @param stride defines the stride size in floats in the buffer (the offset to apply to reach next value when data is interleaved)
  45. * @param instanced defines if the vertex buffer contains indexed data
  46. * @param useBytes defines if the offset and stride are in bytes *
  47. * @param divisor sets an optional divisor for instances (1 by default)
  48. * @returns the new vertex buffer
  49. */
  50. createVertexBuffer(kind: string, offset: number, size: number, stride?: number, instanced?: boolean, useBytes?: boolean, divisor?: number): VertexBuffer;
  51. /**
  52. * Gets a boolean indicating if the Buffer is updatable?
  53. * @returns true if the buffer is updatable
  54. */
  55. isUpdatable(): boolean;
  56. /**
  57. * Gets current buffer's data
  58. * @returns a DataArray or null
  59. */
  60. getData(): Nullable<DataArray>;
  61. /**
  62. * Gets underlying native buffer
  63. * @returns underlying native buffer
  64. */
  65. getBuffer(): Nullable<DataBuffer>;
  66. /**
  67. * Gets the stride in float32 units (i.e. byte stride / 4).
  68. * May not be an integer if the byte stride is not divisible by 4.
  69. * @returns the stride in float32 units
  70. * @deprecated Please use byteStride instead.
  71. */
  72. getStrideSize(): number;
  73. /**
  74. * Store data into the buffer. Creates the buffer if not used already.
  75. * If the buffer was already used, it will be updated only if it is updatable, otherwise it will do nothing.
  76. * @param data defines the data to store
  77. */
  78. create(data?: Nullable<DataArray>): void;
  79. /** @internal */
  80. _rebuild(): void;
  81. /**
  82. * Update current buffer data
  83. * @param data defines the data to store
  84. */
  85. update(data: DataArray): void;
  86. /**
  87. * Updates the data directly.
  88. * @param data the new data
  89. * @param offset the new offset
  90. * @param vertexCount the vertex count (optional)
  91. * @param useBytes set to true if the offset is in bytes
  92. */
  93. updateDirectly(data: DataArray, offset: number, vertexCount?: number, useBytes?: boolean): void;
  94. /** @internal */
  95. _increaseReferences(): void;
  96. /**
  97. * Release all resources
  98. */
  99. dispose(): void;
  100. }
  101. /**
  102. * Options to be used when creating a vertex buffer
  103. */
  104. export interface IVertexBufferOptions {
  105. /**
  106. * whether the data is updatable (default: false)
  107. */
  108. updatable?: boolean;
  109. /**
  110. * whether to postpone creating the internal WebGL buffer (default: false)
  111. */
  112. postponeInternalCreation?: boolean;
  113. /**
  114. * the stride (will be automatically computed from the kind parameter if not specified)
  115. */
  116. stride?: number;
  117. /**
  118. * whether the buffer is instanced (default: false)
  119. */
  120. instanced?: boolean;
  121. /**
  122. * the offset of the data (default: 0)
  123. */
  124. offset?: number;
  125. /**
  126. * the number of components (will be automatically computed from the kind parameter if not specified)
  127. */
  128. size?: number;
  129. /**
  130. * the type of the component (will be deduce from the data parameter if not specified)
  131. */
  132. type?: number;
  133. /**
  134. * whether the data contains normalized data (default: false)
  135. */
  136. normalized?: boolean;
  137. /**
  138. * set to true if stride and offset are in bytes (default: false)
  139. */
  140. useBytes?: boolean;
  141. /**
  142. * defines the instance divisor to use (default: 1, only used if instanced is true)
  143. */
  144. divisor?: number;
  145. /**
  146. * defines if the buffer should be released when the vertex buffer is disposed (default: false)
  147. */
  148. takeBufferOwnership?: boolean;
  149. /**
  150. * label to use for this vertex buffer (debugging purpose)
  151. */
  152. label?: string;
  153. }
  154. /**
  155. * Specialized buffer used to store vertex data
  156. */
  157. export declare class VertexBuffer {
  158. private static _Counter;
  159. /** @internal */
  160. _buffer: Buffer;
  161. /** @internal */
  162. _validOffsetRange: boolean;
  163. private _kind;
  164. private _size;
  165. /** @internal */
  166. _ownsBuffer: boolean;
  167. private _instanced;
  168. private _instanceDivisor;
  169. /** @internal */
  170. _isDisposed: boolean;
  171. /** @internal */
  172. _label?: string;
  173. /**
  174. * The byte type.
  175. */
  176. static readonly BYTE: number;
  177. /**
  178. * The unsigned byte type.
  179. */
  180. static readonly UNSIGNED_BYTE: number;
  181. /**
  182. * The short type.
  183. */
  184. static readonly SHORT: number;
  185. /**
  186. * The unsigned short type.
  187. */
  188. static readonly UNSIGNED_SHORT: number;
  189. /**
  190. * The integer type.
  191. */
  192. static readonly INT: number;
  193. /**
  194. * The unsigned integer type.
  195. */
  196. static readonly UNSIGNED_INT: number;
  197. /**
  198. * The float type.
  199. */
  200. static readonly FLOAT: number;
  201. /**
  202. * Gets a boolean indicating if the Buffer is disposed
  203. */
  204. get isDisposed(): boolean;
  205. /**
  206. * Gets or sets the instance divisor when in instanced mode
  207. */
  208. get instanceDivisor(): number;
  209. set instanceDivisor(value: number);
  210. /**
  211. * Gets the byte stride.
  212. */
  213. readonly byteStride: number;
  214. /**
  215. * Gets the byte offset.
  216. */
  217. readonly byteOffset: number;
  218. /**
  219. * Gets whether integer data values should be normalized into a certain range when being casted to a float.
  220. */
  221. readonly normalized: boolean;
  222. /**
  223. * Gets the data type of each component in the array.
  224. */
  225. readonly type: number;
  226. /**
  227. * Gets the unique id of this vertex buffer
  228. */
  229. readonly uniqueId: number;
  230. /**
  231. * Gets a hash code representing the format (type, normalized, size, instanced, stride) of this buffer
  232. * All buffers with the same format will have the same hash code
  233. */
  234. readonly hashCode: number;
  235. /**
  236. * Gets the engine associated with the buffer
  237. */
  238. readonly engine: AbstractEngine;
  239. /**
  240. * Gets the max possible amount of vertices stored within the current vertex buffer.
  241. * We do not have the end offset or count so this will be too big for concatenated vertex buffers.
  242. * @internal
  243. */
  244. get _maxVerticesCount(): number;
  245. /**
  246. * Constructor
  247. * @param engine the engine
  248. * @param data the data to use for this vertex buffer
  249. * @param kind the vertex buffer kind
  250. * @param updatable whether the data is updatable
  251. * @param postponeInternalCreation whether to postpone creating the internal WebGL buffer (optional)
  252. * @param stride the stride (optional)
  253. * @param instanced whether the buffer is instanced (optional)
  254. * @param offset the offset of the data (optional)
  255. * @param size the number of components (optional)
  256. * @param type the type of the component (optional)
  257. * @param normalized whether the data contains normalized data (optional)
  258. * @param useBytes set to true if stride and offset are in bytes (optional)
  259. * @param divisor defines the instance divisor to use (1 by default)
  260. * @param takeBufferOwnership defines if the buffer should be released when the vertex buffer is disposed
  261. */
  262. constructor(engine: AbstractEngine, data: DataArray | Buffer | DataBuffer, kind: string, updatable: boolean, postponeInternalCreation?: boolean, stride?: number, instanced?: boolean, offset?: number, size?: number, type?: number, normalized?: boolean, useBytes?: boolean, divisor?: number, takeBufferOwnership?: boolean);
  263. /**
  264. * Constructor
  265. * @param engine the engine
  266. * @param data the data to use for this vertex buffer
  267. * @param kind the vertex buffer kind
  268. * @param options defines the rest of the options used to create the buffer
  269. */
  270. constructor(engine: AbstractEngine, data: DataArray | Buffer | DataBuffer, kind: string, options?: IVertexBufferOptions);
  271. private _computeHashCode;
  272. /** @internal */
  273. _rebuild(): void;
  274. /**
  275. * Returns the kind of the VertexBuffer (string)
  276. * @returns a string
  277. */
  278. getKind(): string;
  279. /**
  280. * Gets a boolean indicating if the VertexBuffer is updatable?
  281. * @returns true if the buffer is updatable
  282. */
  283. isUpdatable(): boolean;
  284. /**
  285. * Gets current buffer's data
  286. * @returns a DataArray or null
  287. */
  288. getData(): Nullable<DataArray>;
  289. /**
  290. * Gets current buffer's data as a float array. Float data is constructed if the vertex buffer data cannot be returned directly.
  291. * @param totalVertices number of vertices in the buffer to take into account
  292. * @param forceCopy defines a boolean indicating that the returned array must be cloned upon returning it
  293. * @returns a float array containing vertex data
  294. */
  295. getFloatData(totalVertices: number, forceCopy?: boolean): Nullable<FloatArray>;
  296. /**
  297. * Gets underlying native buffer
  298. * @returns underlying native buffer
  299. */
  300. getBuffer(): Nullable<DataBuffer>;
  301. /**
  302. * Gets the Buffer instance that wraps the native GPU buffer
  303. * @returns the wrapper buffer
  304. */
  305. getWrapperBuffer(): Buffer;
  306. /**
  307. * Gets the stride in float32 units (i.e. byte stride / 4).
  308. * May not be an integer if the byte stride is not divisible by 4.
  309. * @returns the stride in float32 units
  310. * @deprecated Please use byteStride instead.
  311. */
  312. getStrideSize(): number;
  313. /**
  314. * Returns the offset as a multiple of the type byte length.
  315. * @returns the offset in bytes
  316. * @deprecated Please use byteOffset instead.
  317. */
  318. getOffset(): number;
  319. /**
  320. * Returns the number of components or the byte size per vertex attribute
  321. * @param sizeInBytes If true, returns the size in bytes or else the size in number of components of the vertex attribute (default: false)
  322. * @returns the number of components
  323. */
  324. getSize(sizeInBytes?: boolean): number;
  325. /**
  326. * Gets a boolean indicating is the internal buffer of the VertexBuffer is instanced
  327. * @returns true if this buffer is instanced
  328. */
  329. getIsInstanced(): boolean;
  330. /**
  331. * Returns the instancing divisor, zero for non-instanced (integer).
  332. * @returns a number
  333. */
  334. getInstanceDivisor(): number;
  335. /**
  336. * Store data into the buffer. If the buffer was already used it will be either recreated or updated depending on isUpdatable property
  337. * @param data defines the data to store
  338. */
  339. create(data?: DataArray): void;
  340. /**
  341. * Updates the underlying buffer according to the passed numeric array or Float32Array.
  342. * This function will create a new buffer if the current one is not updatable
  343. * @param data defines the data to store
  344. */
  345. update(data: DataArray): void;
  346. /**
  347. * Updates directly the underlying WebGLBuffer according to the passed numeric array or Float32Array.
  348. * Returns the directly updated WebGLBuffer.
  349. * @param data the new data
  350. * @param offset the new offset
  351. * @param useBytes set to true if the offset is in bytes
  352. */
  353. updateDirectly(data: DataArray, offset: number, useBytes?: boolean): void;
  354. /**
  355. * Disposes the VertexBuffer and the underlying WebGLBuffer.
  356. */
  357. dispose(): void;
  358. /**
  359. * Enumerates each value of this vertex buffer as numbers.
  360. * @param count the number of values to enumerate
  361. * @param callback the callback function called for each value
  362. */
  363. forEach(count: number, callback: (value: number, index: number) => void): void;
  364. /**
  365. * Positions
  366. */
  367. static readonly PositionKind: string;
  368. /**
  369. * Normals
  370. */
  371. static readonly NormalKind: string;
  372. /**
  373. * Tangents
  374. */
  375. static readonly TangentKind: string;
  376. /**
  377. * Texture coordinates
  378. */
  379. static readonly UVKind: string;
  380. /**
  381. * Texture coordinates 2
  382. */
  383. static readonly UV2Kind: string;
  384. /**
  385. * Texture coordinates 3
  386. */
  387. static readonly UV3Kind: string;
  388. /**
  389. * Texture coordinates 4
  390. */
  391. static readonly UV4Kind: string;
  392. /**
  393. * Texture coordinates 5
  394. */
  395. static readonly UV5Kind: string;
  396. /**
  397. * Texture coordinates 6
  398. */
  399. static readonly UV6Kind: string;
  400. /**
  401. * Colors
  402. */
  403. static readonly ColorKind: string;
  404. /**
  405. * Instance Colors
  406. */
  407. static readonly ColorInstanceKind: string;
  408. /**
  409. * Matrix indices (for bones)
  410. */
  411. static readonly MatricesIndicesKind: string;
  412. /**
  413. * Matrix weights (for bones)
  414. */
  415. static readonly MatricesWeightsKind: string;
  416. /**
  417. * Additional matrix indices (for bones)
  418. */
  419. static readonly MatricesIndicesExtraKind: string;
  420. /**
  421. * Additional matrix weights (for bones)
  422. */
  423. static readonly MatricesWeightsExtraKind: string;
  424. /**
  425. * Deduces the stride given a kind.
  426. * @param kind The kind string to deduce
  427. * @returns The deduced stride
  428. */
  429. static DeduceStride(kind: string): number;
  430. /**
  431. * Gets the vertex buffer type of the given data array.
  432. * @param data the data array
  433. * @returns the vertex buffer type
  434. */
  435. static GetDataType(data: DataArray): number;
  436. /**
  437. * Gets the byte length of the given type.
  438. * @param type the type
  439. * @returns the number of bytes
  440. */
  441. static GetTypeByteLength(type: number): number;
  442. /**
  443. * Enumerates each value of the given parameters as numbers.
  444. * @param data the data to enumerate
  445. * @param byteOffset the byte offset of the data
  446. * @param byteStride the byte stride of the data
  447. * @param componentCount the number of components per element
  448. * @param componentType the type of the component
  449. * @param count the number of values to enumerate
  450. * @param normalized whether the data is normalized
  451. * @param callback the callback function called for each value
  452. */
  453. static ForEach(data: DataArray, byteOffset: number, byteStride: number, componentCount: number, componentType: number, count: number, normalized: boolean, callback: (value: number, index: number) => void): void;
  454. private static _GetFloatValue;
  455. /**
  456. * Gets the given data array as a float array. Float data is constructed if the data array cannot be returned directly.
  457. * @param data the input data array
  458. * @param size the number of components
  459. * @param type the component type
  460. * @param byteOffset the byte offset of the data
  461. * @param byteStride the byte stride of the data
  462. * @param normalized whether the data is normalized
  463. * @param totalVertices number of vertices in the buffer to take into account
  464. * @param forceCopy defines a boolean indicating that the returned array must be cloned upon returning it
  465. * @returns a float array containing vertex data
  466. */
  467. static GetFloatData(data: DataArray, size: number, type: number, byteOffset: number, byteStride: number, normalized: boolean, totalVertices: number, forceCopy?: boolean): FloatArray;
  468. }