uniformBuffer.d.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. import type { Nullable, FloatArray } from "../types";
  2. import type { IMatrixLike, IVector3Like, IVector4Like, IColor3Like, IColor4Like } from "../Maths/math.like";
  3. import type { Effect } from "./effect";
  4. import type { ThinTexture } from "../Materials/Textures/thinTexture";
  5. import type { DataBuffer } from "../Buffers/dataBuffer";
  6. import type { InternalTexture } from "./Textures/internalTexture";
  7. import "../Engines/Extensions/engine.uniformBuffer";
  8. import type { AbstractEngine } from "../Engines/abstractEngine.js";
  9. /**
  10. * Uniform buffer objects.
  11. *
  12. * Handles blocks of uniform on the GPU.
  13. *
  14. * If WebGL 2 is not available, this class falls back on traditional setUniformXXX calls.
  15. *
  16. * For more information, please refer to :
  17. * https://www.khronos.org/opengl/wiki/Uniform_Buffer_Object
  18. */
  19. export declare class UniformBuffer {
  20. /** @internal */
  21. static _UpdatedUbosInFrame: {
  22. [name: string]: number;
  23. };
  24. private _engine;
  25. private _buffer;
  26. private _buffers;
  27. private _bufferIndex;
  28. private _createBufferOnWrite;
  29. private _data;
  30. private _bufferData;
  31. private _dynamic?;
  32. private _uniformLocations;
  33. private _uniformSizes;
  34. private _uniformArraySizes;
  35. private _uniformLocationPointer;
  36. private _needSync;
  37. private _noUBO;
  38. private _currentEffect;
  39. private _currentEffectName;
  40. private _name;
  41. private _currentFrameId;
  42. private static _MAX_UNIFORM_SIZE;
  43. private static _TempBuffer;
  44. private static _TempBufferInt32View;
  45. private static _TempBufferUInt32View;
  46. /**
  47. * Lambda to Update a 3x3 Matrix in a uniform buffer.
  48. * This is dynamic to allow compat with webgl 1 and 2.
  49. * You will need to pass the name of the uniform as well as the value.
  50. */
  51. updateMatrix3x3: (name: string, matrix: Float32Array) => void;
  52. /**
  53. * Lambda to Update a 2x2 Matrix in a uniform buffer.
  54. * This is dynamic to allow compat with webgl 1 and 2.
  55. * You will need to pass the name of the uniform as well as the value.
  56. */
  57. updateMatrix2x2: (name: string, matrix: Float32Array) => void;
  58. /**
  59. * Lambda to Update a single float in a uniform buffer.
  60. * This is dynamic to allow compat with webgl 1 and 2.
  61. * You will need to pass the name of the uniform as well as the value.
  62. */
  63. updateFloat: (name: string, x: number) => void;
  64. /**
  65. * Lambda to Update a vec2 of float in a uniform buffer.
  66. * This is dynamic to allow compat with webgl 1 and 2.
  67. * You will need to pass the name of the uniform as well as the value.
  68. */
  69. updateFloat2: (name: string, x: number, y: number, suffix?: string) => void;
  70. /**
  71. * Lambda to Update a vec3 of float in a uniform buffer.
  72. * This is dynamic to allow compat with webgl 1 and 2.
  73. * You will need to pass the name of the uniform as well as the value.
  74. */
  75. updateFloat3: (name: string, x: number, y: number, z: number, suffix?: string) => void;
  76. /**
  77. * Lambda to Update a vec4 of float in a uniform buffer.
  78. * This is dynamic to allow compat with webgl 1 and 2.
  79. * You will need to pass the name of the uniform as well as the value.
  80. */
  81. updateFloat4: (name: string, x: number, y: number, z: number, w: number, suffix?: string) => void;
  82. /**
  83. * Lambda to Update an array of float in a uniform buffer.
  84. * This is dynamic to allow compat with webgl 1 and 2.
  85. * You will need to pass the name of the uniform as well as the value.
  86. */
  87. updateFloatArray: (name: string, array: Float32Array) => void;
  88. /**
  89. * Lambda to Update an array of number in a uniform buffer.
  90. * This is dynamic to allow compat with webgl 1 and 2.
  91. * You will need to pass the name of the uniform as well as the value.
  92. */
  93. updateArray: (name: string, array: number[]) => void;
  94. /**
  95. * Lambda to Update an array of number in a uniform buffer.
  96. * This is dynamic to allow compat with webgl 1 and 2.
  97. * You will need to pass the name of the uniform as well as the value.
  98. */
  99. updateIntArray: (name: string, array: Int32Array) => void;
  100. /**
  101. * Lambda to Update an array of number in a uniform buffer.
  102. * This is dynamic to allow compat with webgl 1 and 2.
  103. * You will need to pass the name of the uniform as well as the value.
  104. */
  105. updateUIntArray: (name: string, array: Uint32Array) => void;
  106. /**
  107. * Lambda to Update a 4x4 Matrix in a uniform buffer.
  108. * This is dynamic to allow compat with webgl 1 and 2.
  109. * You will need to pass the name of the uniform as well as the value.
  110. */
  111. updateMatrix: (name: string, mat: IMatrixLike) => void;
  112. /**
  113. * Lambda to Update an array of 4x4 Matrix in a uniform buffer.
  114. * This is dynamic to allow compat with webgl 1 and 2.
  115. * You will need to pass the name of the uniform as well as the value.
  116. */
  117. updateMatrices: (name: string, mat: Float32Array) => void;
  118. /**
  119. * Lambda to Update vec3 of float from a Vector in a uniform buffer.
  120. * This is dynamic to allow compat with webgl 1 and 2.
  121. * You will need to pass the name of the uniform as well as the value.
  122. */
  123. updateVector3: (name: string, vector: IVector3Like) => void;
  124. /**
  125. * Lambda to Update vec4 of float from a Vector in a uniform buffer.
  126. * This is dynamic to allow compat with webgl 1 and 2.
  127. * You will need to pass the name of the uniform as well as the value.
  128. */
  129. updateVector4: (name: string, vector: IVector4Like) => void;
  130. /**
  131. * Lambda to Update vec3 of float from a Color in a uniform buffer.
  132. * This is dynamic to allow compat with webgl 1 and 2.
  133. * You will need to pass the name of the uniform as well as the value.
  134. */
  135. updateColor3: (name: string, color: IColor3Like, suffix?: string) => void;
  136. /**
  137. * Lambda to Update vec4 of float from a Color in a uniform buffer.
  138. * This is dynamic to allow compat with webgl 1 and 2.
  139. * You will need to pass the name of the uniform as well as the value.
  140. */
  141. updateColor4: (name: string, color: IColor3Like, alpha: number, suffix?: string) => void;
  142. /**
  143. * Lambda to Update vec4 of float from a Color in a uniform buffer.
  144. * This is dynamic to allow compat with webgl 1 and 2.
  145. * You will need to pass the name of the uniform as well as the value.
  146. */
  147. updateDirectColor4: (name: string, color: IColor4Like, suffix?: string) => void;
  148. /**
  149. * Lambda to Update a int a uniform buffer.
  150. * This is dynamic to allow compat with webgl 1 and 2.
  151. * You will need to pass the name of the uniform as well as the value.
  152. */
  153. updateInt: (name: string, x: number, suffix?: string) => void;
  154. /**
  155. * Lambda to Update a vec2 of int in a uniform buffer.
  156. * This is dynamic to allow compat with webgl 1 and 2.
  157. * You will need to pass the name of the uniform as well as the value.
  158. */
  159. updateInt2: (name: string, x: number, y: number, suffix?: string) => void;
  160. /**
  161. * Lambda to Update a vec3 of int in a uniform buffer.
  162. * This is dynamic to allow compat with webgl 1 and 2.
  163. * You will need to pass the name of the uniform as well as the value.
  164. */
  165. updateInt3: (name: string, x: number, y: number, z: number, suffix?: string) => void;
  166. /**
  167. * Lambda to Update a vec4 of int in a uniform buffer.
  168. * This is dynamic to allow compat with webgl 1 and 2.
  169. * You will need to pass the name of the uniform as well as the value.
  170. */
  171. updateInt4: (name: string, x: number, y: number, z: number, w: number, suffix?: string) => void;
  172. /**
  173. * Lambda to Update a unsigned int a uniform buffer.
  174. * This is dynamic to allow compat with webgl 1 and 2.
  175. * You will need to pass the name of the uniform as well as the value.
  176. */
  177. updateUInt: (name: string, x: number, suffix?: string) => void;
  178. /**
  179. * Lambda to Update a vec2 of unsigned int in a uniform buffer.
  180. * This is dynamic to allow compat with webgl 1 and 2.
  181. * You will need to pass the name of the uniform as well as the value.
  182. */
  183. updateUInt2: (name: string, x: number, y: number, suffix?: string) => void;
  184. /**
  185. * Lambda to Update a vec3 of unsigned int in a uniform buffer.
  186. * This is dynamic to allow compat with webgl 1 and 2.
  187. * You will need to pass the name of the uniform as well as the value.
  188. */
  189. updateUInt3: (name: string, x: number, y: number, z: number, suffix?: string) => void;
  190. /**
  191. * Lambda to Update a vec4 of unsigned int in a uniform buffer.
  192. * This is dynamic to allow compat with webgl 1 and 2.
  193. * You will need to pass the name of the uniform as well as the value.
  194. */
  195. updateUInt4: (name: string, x: number, y: number, z: number, w: number, suffix?: string) => void;
  196. /**
  197. * Instantiates a new Uniform buffer objects.
  198. *
  199. * Handles blocks of uniform on the GPU.
  200. *
  201. * If WebGL 2 is not available, this class falls back on traditional setUniformXXX calls.
  202. *
  203. * For more information, please refer to :
  204. * @see https://www.khronos.org/opengl/wiki/Uniform_Buffer_Object
  205. * @param engine Define the engine the buffer is associated with
  206. * @param data Define the data contained in the buffer
  207. * @param dynamic Define if the buffer is updatable
  208. * @param name to assign to the buffer (debugging purpose)
  209. * @param forceNoUniformBuffer define that this object must not rely on UBO objects
  210. */
  211. constructor(engine: AbstractEngine, data?: number[], dynamic?: boolean, name?: string, forceNoUniformBuffer?: boolean);
  212. /**
  213. * Indicates if the buffer is using the WebGL2 UBO implementation,
  214. * or just falling back on setUniformXXX calls.
  215. */
  216. get useUbo(): boolean;
  217. /**
  218. * Indicates if the WebGL underlying uniform buffer is in sync
  219. * with the javascript cache data.
  220. */
  221. get isSync(): boolean;
  222. /**
  223. * Indicates if the WebGL underlying uniform buffer is dynamic.
  224. * Also, a dynamic UniformBuffer will disable cache verification and always
  225. * update the underlying WebGL uniform buffer to the GPU.
  226. * @returns if Dynamic, otherwise false
  227. */
  228. isDynamic(): boolean;
  229. /**
  230. * The data cache on JS side.
  231. * @returns the underlying data as a float array
  232. */
  233. getData(): Float32Array;
  234. /**
  235. * The underlying WebGL Uniform buffer.
  236. * @returns the webgl buffer
  237. */
  238. getBuffer(): Nullable<DataBuffer>;
  239. /**
  240. * std140 layout specifies how to align data within an UBO structure.
  241. * See https://khronos.org/registry/OpenGL/specs/gl/glspec45.core.pdf#page=159
  242. * for specs.
  243. * @param size
  244. */
  245. private _fillAlignment;
  246. /**
  247. * Adds an uniform in the buffer.
  248. * Warning : the subsequents calls of this function must be in the same order as declared in the shader
  249. * for the layout to be correct ! The addUniform function only handles types like float, vec2, vec3, vec4, mat4,
  250. * meaning size=1,2,3,4 or 16. It does not handle struct types.
  251. * @param name Name of the uniform, as used in the uniform block in the shader.
  252. * @param size Data size, or data directly.
  253. * @param arraySize The number of elements in the array, 0 if not an array.
  254. */
  255. addUniform(name: string, size: number | number[], arraySize?: number): void;
  256. /**
  257. * Adds a Matrix 4x4 to the uniform buffer.
  258. * @param name Name of the uniform, as used in the uniform block in the shader.
  259. * @param mat A 4x4 matrix.
  260. */
  261. addMatrix(name: string, mat: IMatrixLike): void;
  262. /**
  263. * Adds a vec2 to the uniform buffer.
  264. * @param name Name of the uniform, as used in the uniform block in the shader.
  265. * @param x Define the x component value of the vec2
  266. * @param y Define the y component value of the vec2
  267. */
  268. addFloat2(name: string, x: number, y: number): void;
  269. /**
  270. * Adds a vec3 to the uniform buffer.
  271. * @param name Name of the uniform, as used in the uniform block in the shader.
  272. * @param x Define the x component value of the vec3
  273. * @param y Define the y component value of the vec3
  274. * @param z Define the z component value of the vec3
  275. */
  276. addFloat3(name: string, x: number, y: number, z: number): void;
  277. /**
  278. * Adds a vec3 to the uniform buffer.
  279. * @param name Name of the uniform, as used in the uniform block in the shader.
  280. * @param color Define the vec3 from a Color
  281. */
  282. addColor3(name: string, color: IColor3Like): void;
  283. /**
  284. * Adds a vec4 to the uniform buffer.
  285. * @param name Name of the uniform, as used in the uniform block in the shader.
  286. * @param color Define the rgb components from a Color
  287. * @param alpha Define the a component of the vec4
  288. */
  289. addColor4(name: string, color: IColor3Like, alpha: number): void;
  290. /**
  291. * Adds a vec3 to the uniform buffer.
  292. * @param name Name of the uniform, as used in the uniform block in the shader.
  293. * @param vector Define the vec3 components from a Vector
  294. */
  295. addVector3(name: string, vector: IVector3Like): void;
  296. /**
  297. * Adds a Matrix 3x3 to the uniform buffer.
  298. * @param name Name of the uniform, as used in the uniform block in the shader.
  299. */
  300. addMatrix3x3(name: string): void;
  301. /**
  302. * Adds a Matrix 2x2 to the uniform buffer.
  303. * @param name Name of the uniform, as used in the uniform block in the shader.
  304. */
  305. addMatrix2x2(name: string): void;
  306. /**
  307. * Effectively creates the WebGL Uniform Buffer, once layout is completed with `addUniform`.
  308. */
  309. create(): void;
  310. private _getNames;
  311. /** @internal */
  312. _rebuild(): void;
  313. /** @internal */
  314. _rebuildAfterContextLost(): void;
  315. /** @internal */
  316. get _numBuffers(): number;
  317. /** @internal */
  318. get _indexBuffer(): number;
  319. /** Gets the name of this buffer */
  320. get name(): string;
  321. /** Gets the current effect */
  322. get currentEffect(): Nullable<Effect>;
  323. private _buffersEqual;
  324. private _copyBuffer;
  325. /**
  326. * Updates the WebGL Uniform Buffer on the GPU.
  327. * If the `dynamic` flag is set to true, no cache comparison is done.
  328. * Otherwise, the buffer will be updated only if the cache differs.
  329. */
  330. update(): void;
  331. private _createNewBuffer;
  332. private _checkNewFrame;
  333. /**
  334. * Updates the value of an uniform. The `update` method must be called afterwards to make it effective in the GPU.
  335. * @param uniformName Define the name of the uniform, as used in the uniform block in the shader.
  336. * @param data Define the flattened data
  337. * @param size Define the size of the data.
  338. */
  339. updateUniform(uniformName: string, data: FloatArray, size: number): void;
  340. /**
  341. * Updates the value of an uniform. The `update` method must be called afterwards to make it effective in the GPU.
  342. * @param uniformName Define the name of the uniform, as used in the uniform block in the shader.
  343. * @param data Define the flattened data
  344. * @param size Define the size of the data.
  345. */
  346. updateUniformArray(uniformName: string, data: FloatArray, size: number): void;
  347. private _valueCache;
  348. private _cacheMatrix;
  349. private _updateMatrix3x3ForUniform;
  350. private _updateMatrix3x3ForEffect;
  351. private _updateMatrix2x2ForEffect;
  352. private _updateMatrix2x2ForUniform;
  353. private _updateFloatForEffect;
  354. private _updateFloatForUniform;
  355. private _updateFloat2ForEffect;
  356. private _updateFloat2ForUniform;
  357. private _updateFloat3ForEffect;
  358. private _updateFloat3ForUniform;
  359. private _updateFloat4ForEffect;
  360. private _updateFloat4ForUniform;
  361. private _updateFloatArrayForEffect;
  362. private _updateFloatArrayForUniform;
  363. private _updateArrayForEffect;
  364. private _updateArrayForUniform;
  365. private _updateIntArrayForEffect;
  366. private _updateIntArrayForUniform;
  367. private _updateUIntArrayForEffect;
  368. private _updateUIntArrayForUniform;
  369. private _updateMatrixForEffect;
  370. private _updateMatrixForUniform;
  371. private _updateMatricesForEffect;
  372. private _updateMatricesForUniform;
  373. private _updateVector3ForEffect;
  374. private _updateVector3ForUniform;
  375. private _updateVector4ForEffect;
  376. private _updateVector4ForUniform;
  377. private _updateColor3ForEffect;
  378. private _updateColor3ForUniform;
  379. private _updateColor4ForEffect;
  380. private _updateDirectColor4ForEffect;
  381. private _updateColor4ForUniform;
  382. private _updateDirectColor4ForUniform;
  383. private _updateIntForEffect;
  384. private _updateIntForUniform;
  385. private _updateInt2ForEffect;
  386. private _updateInt2ForUniform;
  387. private _updateInt3ForEffect;
  388. private _updateInt3ForUniform;
  389. private _updateInt4ForEffect;
  390. private _updateInt4ForUniform;
  391. private _updateUIntForEffect;
  392. private _updateUIntForUniform;
  393. private _updateUInt2ForEffect;
  394. private _updateUInt2ForUniform;
  395. private _updateUInt3ForEffect;
  396. private _updateUInt3ForUniform;
  397. private _updateUInt4ForEffect;
  398. private _updateUInt4ForUniform;
  399. /**
  400. * Sets a sampler uniform on the effect.
  401. * @param name Define the name of the sampler.
  402. * @param texture Define the texture to set in the sampler
  403. */
  404. setTexture(name: string, texture: Nullable<ThinTexture>): void;
  405. /**
  406. * Sets a sampler uniform on the effect.
  407. * @param name Define the name of the sampler.
  408. * @param texture Define the (internal) texture to set in the sampler
  409. */
  410. bindTexture(name: string, texture: Nullable<InternalTexture>): void;
  411. /**
  412. * Directly updates the value of the uniform in the cache AND on the GPU.
  413. * @param uniformName Define the name of the uniform, as used in the uniform block in the shader.
  414. * @param data Define the flattened data
  415. */
  416. updateUniformDirectly(uniformName: string, data: FloatArray): void;
  417. /**
  418. * Associates an effect to this uniform buffer
  419. * @param effect Define the effect to associate the buffer to
  420. * @param name Name of the uniform block in the shader.
  421. */
  422. bindToEffect(effect: Effect, name: string): void;
  423. /**
  424. * Binds the current (GPU) buffer to the effect
  425. */
  426. bindUniformBuffer(): void;
  427. /**
  428. * Dissociates the current effect from this uniform buffer
  429. */
  430. unbindEffect(): void;
  431. /**
  432. * Sets the current state of the class (_bufferIndex, _buffer) to point to the data buffer passed in parameter if this buffer is one of the buffers handled by the class (meaning if it can be found in the _buffers array)
  433. * This method is meant to be able to update a buffer at any time: just call setDataBuffer to set the class in the right state, call some updateXXX methods and then call udpate() => that will update the GPU buffer on the graphic card
  434. * @param dataBuffer buffer to look for
  435. * @returns true if the buffer has been found and the class internal state points to it, else false
  436. */
  437. setDataBuffer(dataBuffer: DataBuffer): boolean;
  438. /**
  439. * Disposes the uniform buffer.
  440. */
  441. dispose(): void;
  442. }