babylon.uniformBuffer.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. module BABYLON {
  2. export class UniformBuffer {
  3. private _engine: Engine;
  4. private _buffer: WebGLBuffer;
  5. private _data: number[];
  6. private _bufferData: Float32Array;
  7. private _dynamic: boolean;
  8. private _uniformName: string;
  9. private _uniformLocations: { [key:string]:number; };
  10. private _uniformSizes: { [key:string]:number; };
  11. private _uniformLocationPointer: number;
  12. private _needSync: boolean;
  13. private _cache: Float32Array;
  14. private _noUBO: boolean;
  15. private _currentEffect: Effect;
  16. // Pool for avoiding memory leaks
  17. private static _MAX_UNIFORM_SIZE = 256;
  18. private static _tempBuffer = new Float32Array(UniformBuffer._MAX_UNIFORM_SIZE);
  19. /**
  20. * Wrapper for updateUniform.
  21. * @method updateMatrix3x3
  22. * @param {string} name Name of the uniform, as used in the uniform block in the shader.
  23. * @param {Float32Array} matrix
  24. */
  25. public updateMatrix3x3: (name: string, matrix: Float32Array) => void;
  26. /**
  27. * Wrapper for updateUniform.
  28. * @param {string} name Name of the uniform, as used in the uniform block in the shader.
  29. * @param {Float32Array} matrix
  30. */
  31. public updateMatrix2x2: (name: string, matrix: Float32Array) => void;
  32. /**
  33. * Wrapper for updateUniform.
  34. * @param {string} name Name of the uniform, as used in the uniform block in the shader.
  35. * @param {number} x
  36. */
  37. public updateFloat: (name: string, x: number) => void;
  38. /**
  39. * Wrapper for updateUniform.
  40. * @param {string} name Name of the uniform, as used in the uniform block in the shader.
  41. * @param {number} x
  42. * @param {number} y
  43. * @param {string} [suffix] Suffix to add to the uniform name.
  44. */
  45. public updateFloat2: (name: string, x: number, y: number, suffix?: string) => void;
  46. /**
  47. * Wrapper for updateUniform.
  48. * @param {string} name Name of the uniform, as used in the uniform block in the shader.
  49. * @param {number} x
  50. * @param {number} y
  51. * @param {number} z
  52. * @param {string} [suffix] Suffix to add to the uniform name.
  53. */
  54. public updateFloat3: (name: string, x: number, y: number, z: number, suffix?: string) => void;
  55. /**
  56. * Wrapper for updateUniform.
  57. * @param {string} name Name of the uniform, as used in the uniform block in the shader.
  58. * @param {number} x
  59. * @param {number} y
  60. * @param {number} z
  61. * @param {number} w
  62. * @param {string} [suffix] Suffix to add to the uniform name.
  63. */
  64. public updateFloat4: (name: string, x: number, y: number, z: number, w: number, suffix?: string) => void;
  65. /**
  66. * Wrapper for updateUniform.
  67. * @param {string} name Name of the uniform, as used in the uniform block in the shader.
  68. * @param {Matrix} A 4x4 matrix.
  69. */
  70. public updateMatrix: (name: string, mat: Matrix) => void;
  71. /**
  72. * Wrapper for updateUniform.
  73. * @param {string} name Name of the uniform, as used in the uniform block in the shader.
  74. * @param {Vector3} vector
  75. */
  76. public updateVector3: (name: string, vector: Vector3) => void;
  77. /**
  78. * Wrapper for updateUniform.
  79. * @param {string} name Name of the uniform, as used in the uniform block in the shader.
  80. * @param {Vector4} vector
  81. */
  82. public updateVector4: (name: string, vector: Vector4) => void;
  83. /**
  84. * Wrapper for updateUniform.
  85. * @param {string} name Name of the uniform, as used in the uniform block in the shader.
  86. * @param {Color3} color
  87. * @param {string} [suffix] Suffix to add to the uniform name.
  88. */
  89. public updateColor3: (name: string, color: Color3, suffix?: string) => void;
  90. /**
  91. * Wrapper for updateUniform.
  92. * @param {string} name Name of the uniform, as used in the uniform block in the shader.
  93. * @param {Color3} color
  94. * @param {number} alpha
  95. * @param {string} [suffix] Suffix to add to the uniform name.
  96. */
  97. public updateColor4: (name: string, color: Color3, alpha: number, suffix?: string) => void;
  98. /**
  99. * Uniform buffer objects.
  100. *
  101. * Handles blocks of uniform on the GPU.
  102. *
  103. * If WebGL 2 is not available, this class falls back on traditionnal setUniformXXX calls.
  104. *
  105. * For more information, please refer to :
  106. * https://www.khronos.org/opengl/wiki/Uniform_Buffer_Object
  107. */
  108. constructor(engine: Engine, data?: number[], dynamic?: boolean) {
  109. this._engine = engine;
  110. this._noUBO = !engine.supportsUniformBuffers;
  111. this._dynamic = dynamic;
  112. this._data = data || [];
  113. this._uniformLocations = {};
  114. this._uniformSizes = {};
  115. this._uniformLocationPointer = 0;
  116. this._needSync = false;
  117. if (this._noUBO) {
  118. this.updateMatrix3x3 = this._updateMatrix3x3ForEffect;
  119. this.updateMatrix2x2 = this._updateMatrix2x2ForEffect;
  120. this.updateFloat = this._updateFloatForEffect;
  121. this.updateFloat2 = this._updateFloat2ForEffect;
  122. this.updateFloat3 = this._updateFloat3ForEffect;
  123. this.updateFloat4 = this._updateFloat4ForEffect;
  124. this.updateMatrix = this._updateMatrixForEffect;
  125. this.updateVector3 = this._updateVector3ForEffect;
  126. this.updateVector4 = this._updateVector4ForEffect;
  127. this.updateColor3 = this._updateColor3ForEffect;
  128. this.updateColor4 = this._updateColor4ForEffect;
  129. } else {
  130. this._engine._uniformBuffers.push(this);
  131. this.updateMatrix3x3 = this._updateMatrix3x3ForUniform;
  132. this.updateMatrix2x2 = this._updateMatrix2x2ForUniform;
  133. this.updateFloat = this._updateFloatForUniform;
  134. this.updateFloat2 = this._updateFloat2ForUniform;
  135. this.updateFloat3 = this._updateFloat3ForUniform;
  136. this.updateFloat4 = this._updateFloat4ForUniform;
  137. this.updateMatrix = this._updateMatrixForUniform;
  138. this.updateVector3 = this._updateVector3ForUniform;
  139. this.updateVector4 = this._updateVector4ForUniform;
  140. this.updateColor3 = this._updateColor3ForUniform;
  141. this.updateColor4 = this._updateColor4ForUniform;
  142. }
  143. }
  144. // Properties
  145. /**
  146. * Indicates if the buffer is using the WebGL2 UBO implementation,
  147. * or just falling back on setUniformXXX calls.
  148. */
  149. public get useUbo(): boolean {
  150. return !this._noUBO;
  151. }
  152. /**
  153. * Indicates if the WebGL underlying uniform buffer is in sync
  154. * with the javascript cache data.
  155. */
  156. public get isSync(): boolean {
  157. return !this._needSync;
  158. }
  159. /**
  160. * Indicates if the WebGL underlying uniform buffer is dynamic.
  161. * Also, a dynamic UniformBuffer will disable cache verification and always
  162. * update the underlying WebGL uniform buffer to the GPU.
  163. */
  164. public isDynamic(): boolean {
  165. return this._dynamic;
  166. }
  167. /**
  168. * The data cache on JS side.
  169. */
  170. public getData(): Float32Array {
  171. return this._bufferData;
  172. }
  173. /**
  174. * The underlying WebGL Uniform buffer.
  175. */
  176. public getBuffer(): WebGLBuffer {
  177. return this._buffer;
  178. }
  179. /**
  180. * std140 layout specifies how to align data within an UBO structure.
  181. * See https://khronos.org/registry/OpenGL/specs/gl/glspec45.core.pdf#page=159
  182. * for specs.
  183. */
  184. private _fillAlignment(size: number) {
  185. // This code has been simplified because we only use floats, vectors of 1, 2, 3, 4 components
  186. // and 4x4 matrices
  187. // TODO : change if other types are used
  188. var alignment;
  189. if (size <= 2) {
  190. alignment = size;
  191. } else {
  192. alignment = 4;
  193. }
  194. if ((this._uniformLocationPointer % alignment) !== 0) {
  195. var oldPointer = this._uniformLocationPointer;
  196. this._uniformLocationPointer += alignment - (this._uniformLocationPointer % alignment);
  197. var diff = this._uniformLocationPointer - oldPointer;
  198. for (var i = 0; i < diff; i++) {
  199. this._data.push(0);
  200. }
  201. }
  202. }
  203. /**
  204. * Adds an uniform in the buffer.
  205. * Warning : the subsequents calls of this function must be in the same order as declared in the shader
  206. * for the layout to be correct !
  207. * @param {string} name Name of the uniform, as used in the uniform block in the shader.
  208. * @param {number|number[]} size Data size, or data directly.
  209. */
  210. public addUniform(name: string, size: number | number[]) {
  211. if (this._noUBO) {
  212. return;
  213. }
  214. if (this._uniformLocations[name] !== undefined) {
  215. // Already existing uniform
  216. return;
  217. }
  218. // This function must be called in the order of the shader layout !
  219. // size can be the size of the uniform, or data directly
  220. var data;
  221. if (size instanceof Array) {
  222. data = size;
  223. size = data.length;
  224. } else {
  225. size = <number>size;
  226. data = [];
  227. // Fill with zeros
  228. for (var i = 0; i < size; i++) {
  229. data.push(0);
  230. }
  231. }
  232. this._fillAlignment(<number>size);
  233. this._uniformSizes[name] = <number>size;
  234. this._uniformLocations[name] = this._uniformLocationPointer;
  235. this._uniformLocationPointer += <number>size;
  236. for (var i = 0; i < size; i++) {
  237. this._data.push(data[i]);
  238. }
  239. this._needSync = true;
  240. }
  241. /**
  242. * Wrapper for addUniform.
  243. * @param {string} name Name of the uniform, as used in the uniform block in the shader.
  244. * @param {Matrix} mat A 4x4 matrix.
  245. */
  246. public addMatrix(name: string, mat: Matrix) {
  247. this.addUniform(name, Array.prototype.slice.call(mat.toArray()));
  248. }
  249. /**
  250. * Wrapper for addUniform.
  251. * @param {string} name Name of the uniform, as used in the uniform block in the shader.
  252. * @param {number} x
  253. * @param {number} y
  254. */
  255. public addFloat2(name: string, x: number, y: number) {
  256. var temp = [x, y];
  257. this.addUniform(name, temp);
  258. }
  259. /**
  260. * Wrapper for addUniform.
  261. * @param {string} name Name of the uniform, as used in the uniform block in the shader.
  262. * @param {number} x
  263. * @param {number} y
  264. * @param {number} z
  265. */
  266. public addFloat3(name: string, x: number, y: number, z: number) {
  267. var temp = [x, y, z];
  268. this.addUniform(name, temp);
  269. }
  270. /**
  271. * Wrapper for addUniform.
  272. * @param {string} name Name of the uniform, as used in the uniform block in the shader.
  273. * @param {Color3} color
  274. */
  275. public addColor3(name: string, color: Color3) {
  276. var temp = [];
  277. color.toArray(temp);
  278. this.addUniform(name, temp);
  279. }
  280. /**
  281. * Wrapper for addUniform.
  282. * @param {string} name Name of the uniform, as used in the uniform block in the shader.
  283. * @param {Color3} color
  284. * @param {number} alpha
  285. */
  286. public addColor4(name: string, color: Color3, alpha: number) {
  287. var temp = [];
  288. color.toArray(temp);
  289. temp.push(alpha);
  290. this.addUniform(name, temp);
  291. }
  292. /**
  293. * Wrapper for addUniform.
  294. * @param {string} name Name of the uniform, as used in the uniform block in the shader.
  295. * @param {Vector3} vector
  296. */
  297. public addVector3(name: string, vector: Vector3) {
  298. var temp = [];
  299. vector.toArray(temp);
  300. this.addUniform(name, temp);
  301. }
  302. /**
  303. * Wrapper for addUniform.
  304. * @param {string} name Name of the uniform, as used in the uniform block in the shader.
  305. */
  306. public addMatrix3x3(name: string) {
  307. this.addUniform(name, 12);
  308. }
  309. /**
  310. * Wrapper for addUniform.
  311. * @param {string} name Name of the uniform, as used in the uniform block in the shader.
  312. */
  313. public addMatrix2x2(name: string) {
  314. this.addUniform(name, 8);
  315. }
  316. /**
  317. * Effectively creates the WebGL Uniform Buffer, once layout is completed with `addUniform`.
  318. */
  319. public create(): void {
  320. if (this._noUBO) {
  321. return;
  322. }
  323. if (this._buffer) {
  324. return; // nothing to do
  325. }
  326. // See spec, alignment must be filled as a vec4
  327. this._fillAlignment(4);
  328. this._bufferData = new Float32Array(this._data);
  329. this._rebuild();
  330. this._needSync = true;
  331. }
  332. public _rebuild(): void {
  333. if (this._noUBO) {
  334. return;
  335. }
  336. if (this._dynamic) {
  337. this._buffer = this._engine.createDynamicUniformBuffer(this._bufferData);
  338. } else {
  339. this._buffer = this._engine.createUniformBuffer(this._bufferData);
  340. }
  341. }
  342. /**
  343. * Updates the WebGL Uniform Buffer on the GPU.
  344. * If the `dynamic` flag is set to true, no cache comparison is done.
  345. * Otherwise, the buffer will be updated only if the cache differs.
  346. */
  347. public update(): void {
  348. if (!this._buffer) {
  349. this.create();
  350. return;
  351. }
  352. if (!this._dynamic && !this._needSync) {
  353. return;
  354. }
  355. this._engine.updateUniformBuffer(this._buffer, this._bufferData);
  356. this._needSync = false;
  357. }
  358. /**
  359. * Updates the value of an uniform. The `update` method must be called afterwards to make it effective in the GPU.
  360. * @param {string} uniformName Name of the uniform, as used in the uniform block in the shader.
  361. * @param {number[]|Float32Array} data Flattened data
  362. * @param {number} size Size of the data.
  363. */
  364. public updateUniform(uniformName: string, data: number[] | Float32Array, size: number) {
  365. var location = this._uniformLocations[uniformName];
  366. if (location === undefined) {
  367. if (this._buffer) {
  368. // Cannot add an uniform if the buffer is already created
  369. Tools.Error("Cannot add an uniform after UBO has been created.");
  370. return;
  371. }
  372. this.addUniform(uniformName, size);
  373. location = this._uniformLocations[uniformName];
  374. }
  375. if (!this._buffer) {
  376. this.create();
  377. }
  378. if (!this._dynamic) {
  379. // Cache for static uniform buffers
  380. var changed = false;
  381. for (var i = 0; i < size; i++) {
  382. if (this._bufferData[location + i] !== data[i]) {
  383. changed = true;
  384. this._bufferData[location + i] = data[i];
  385. }
  386. }
  387. this._needSync = this._needSync || changed;
  388. } else {
  389. // No cache for dynamic
  390. for (var i = 0; i < size; i++) {
  391. this._bufferData[location + i] = data[i];
  392. }
  393. }
  394. }
  395. // Update methods
  396. private _updateMatrix3x3ForUniform(name: string, matrix: Float32Array): void {
  397. // To match std140, matrix must be realigned
  398. for (var i = 0; i < 3; i++) {
  399. UniformBuffer._tempBuffer[i * 4] = matrix[i * 3];
  400. UniformBuffer._tempBuffer[i * 4 + 1] = matrix[i * 3 + 1];
  401. UniformBuffer._tempBuffer[i * 4 + 2] = matrix[i * 3 + 2];
  402. UniformBuffer._tempBuffer[i * 4 + 3] = 0.0;
  403. }
  404. this.updateUniform(name, UniformBuffer._tempBuffer, 12);
  405. }
  406. private _updateMatrix3x3ForEffect(name: string, matrix: Float32Array): void {
  407. this._currentEffect.setMatrix3x3(name, matrix);
  408. }
  409. private _updateMatrix2x2ForEffect(name: string, matrix: Float32Array): void {
  410. this._currentEffect.setMatrix2x2(name, matrix);
  411. }
  412. private _updateMatrix2x2ForUniform(name: string, matrix: Float32Array): void {
  413. // To match std140, matrix must be realigned
  414. for (var i = 0; i < 2; i++) {
  415. UniformBuffer._tempBuffer[i * 4] = matrix[i * 2];
  416. UniformBuffer._tempBuffer[i * 4 + 1] = matrix[i * 2 + 1];
  417. UniformBuffer._tempBuffer[i * 4 + 2] = 0.0;
  418. UniformBuffer._tempBuffer[i * 4 + 3] = 0.0;
  419. }
  420. this.updateUniform(name, UniformBuffer._tempBuffer, 8);
  421. }
  422. private _updateFloatForEffect(name: string, x: number) {
  423. this._currentEffect.setFloat(name, x);
  424. }
  425. private _updateFloatForUniform(name: string, x: number) {
  426. UniformBuffer._tempBuffer[0] = x;
  427. this.updateUniform(name, UniformBuffer._tempBuffer, 1);
  428. }
  429. private _updateFloat2ForEffect(name: string, x: number, y: number, suffix = "") {
  430. this._currentEffect.setFloat2(name + suffix, x, y);
  431. }
  432. private _updateFloat2ForUniform(name: string, x: number, y: number, suffix = "") {
  433. UniformBuffer._tempBuffer[0] = x;
  434. UniformBuffer._tempBuffer[1] = y;
  435. this.updateUniform(name, UniformBuffer._tempBuffer, 2);
  436. }
  437. private _updateFloat3ForEffect(name: string, x: number, y: number, z: number, suffix = "") {
  438. this._currentEffect.setFloat3(name + suffix, x, y, z);
  439. }
  440. private _updateFloat3ForUniform(name: string, x: number, y: number, z: number, suffix = "") {
  441. UniformBuffer._tempBuffer[0] = x;
  442. UniformBuffer._tempBuffer[1] = y;
  443. UniformBuffer._tempBuffer[2] = z;
  444. this.updateUniform(name, UniformBuffer._tempBuffer, 3);
  445. }
  446. private _updateFloat4ForEffect(name: string, x: number, y: number, z: number, w: number, suffix = "") {
  447. this._currentEffect.setFloat4(name + suffix, x, y, z, w);
  448. }
  449. private _updateFloat4ForUniform(name: string, x: number, y: number, z: number, w: number, suffix = "") {
  450. UniformBuffer._tempBuffer[0] = x;
  451. UniformBuffer._tempBuffer[1] = y;
  452. UniformBuffer._tempBuffer[2] = z;
  453. UniformBuffer._tempBuffer[3] = w;
  454. this.updateUniform(name, UniformBuffer._tempBuffer, 4);
  455. }
  456. private _updateMatrixForEffect(name: string, mat: Matrix) {
  457. this._currentEffect.setMatrix(name, mat);
  458. }
  459. private _updateMatrixForUniform(name: string, mat: Matrix) {
  460. this.updateUniform(name, mat.toArray(), 16);
  461. }
  462. private _updateVector3ForEffect(name: string, vector: Vector3) {
  463. this._currentEffect.setVector3(name, vector);
  464. }
  465. private _updateVector3ForUniform(name: string, vector: Vector3) {
  466. vector.toArray(UniformBuffer._tempBuffer);
  467. this.updateUniform(name, UniformBuffer._tempBuffer, 3);
  468. }
  469. private _updateVector4ForEffect(name: string, vector: Vector4) {
  470. this._currentEffect.setVector4(name, vector);
  471. }
  472. private _updateVector4ForUniform(name: string, vector: Vector4) {
  473. vector.toArray(UniformBuffer._tempBuffer);
  474. this.updateUniform(name, UniformBuffer._tempBuffer, 4);
  475. }
  476. private _updateColor3ForEffect(name: string, color: Color3, suffix = "") {
  477. this._currentEffect.setColor3(name + suffix, color);
  478. }
  479. private _updateColor3ForUniform(name: string, color: Color3, suffix = "") {
  480. color.toArray(UniformBuffer._tempBuffer);
  481. this.updateUniform(name, UniformBuffer._tempBuffer, 3);
  482. }
  483. private _updateColor4ForEffect(name: string, color: Color3, alpha: number, suffix = "") {
  484. this._currentEffect.setColor4(name + suffix, color, alpha);
  485. }
  486. private _updateColor4ForUniform(name: string, color: Color3, alpha: number, suffix = "") {
  487. color.toArray(UniformBuffer._tempBuffer);
  488. UniformBuffer._tempBuffer[3] = alpha;
  489. this.updateUniform(name, UniformBuffer._tempBuffer, 4);
  490. }
  491. /**
  492. * Sets a sampler uniform on the effect.
  493. * @param {string} name Name of the sampler.
  494. * @param {Texture} texture
  495. */
  496. public setTexture(name: string, texture: BaseTexture) {
  497. this._currentEffect.setTexture(name, texture);
  498. }
  499. /**
  500. * Directly updates the value of the uniform in the cache AND on the GPU.
  501. * @param {string} uniformName Name of the uniform, as used in the uniform block in the shader.
  502. * @param {number[]|Float32Array} data Flattened data
  503. */
  504. public updateUniformDirectly(uniformName: string, data: number[] | Float32Array) {
  505. this.updateUniform(uniformName, data, data.length);
  506. this.update();
  507. }
  508. /**
  509. * Binds this uniform buffer to an effect.
  510. * @param {Effect} effect
  511. * @param {string} name Name of the uniform block in the shader.
  512. */
  513. public bindToEffect(effect: Effect, name: string): void {
  514. this._currentEffect = effect;
  515. if (this._noUBO) {
  516. return;
  517. }
  518. effect.bindUniformBuffer(this._buffer, name);
  519. }
  520. /**
  521. * Disposes the uniform buffer.
  522. */
  523. public dispose(): void {
  524. if (this._noUBO) {
  525. return;
  526. }
  527. let index = this._engine._uniformBuffers.indexOf(this);
  528. if (index !== -1) {
  529. this._engine._uniformBuffers.splice(index, 1);
  530. }
  531. if (!this._buffer) {
  532. return;
  533. }
  534. if (this._engine._releaseBuffer(this._buffer)) {
  535. this._buffer = null;
  536. }
  537. }
  538. }
  539. }