createUniformArray.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. import Cartesian2 from '../Core/Cartesian2.js';
  2. import Cartesian3 from '../Core/Cartesian3.js';
  3. import Cartesian4 from '../Core/Cartesian4.js';
  4. import Color from '../Core/Color.js';
  5. import defined from '../Core/defined.js';
  6. import DeveloperError from '../Core/DeveloperError.js';
  7. import Matrix2 from '../Core/Matrix2.js';
  8. import Matrix3 from '../Core/Matrix3.js';
  9. import Matrix4 from '../Core/Matrix4.js';
  10. import RuntimeError from '../Core/RuntimeError.js';
  11. /**
  12. * @private
  13. */
  14. function createUniformArray(gl, activeUniform, uniformName, locations) {
  15. switch (activeUniform.type) {
  16. case gl.FLOAT:
  17. return new UniformArrayFloat(gl, activeUniform, uniformName, locations);
  18. case gl.FLOAT_VEC2:
  19. return new UniformArrayFloatVec2(gl, activeUniform, uniformName, locations);
  20. case gl.FLOAT_VEC3:
  21. return new UniformArrayFloatVec3(gl, activeUniform, uniformName, locations);
  22. case gl.FLOAT_VEC4:
  23. return new UniformArrayFloatVec4(gl, activeUniform, uniformName, locations);
  24. case gl.SAMPLER_2D:
  25. case gl.SAMPLER_CUBE:
  26. return new UniformArraySampler(gl, activeUniform, uniformName, locations);
  27. case gl.INT:
  28. case gl.BOOL:
  29. return new UniformArrayInt(gl, activeUniform, uniformName, locations);
  30. case gl.INT_VEC2:
  31. case gl.BOOL_VEC2:
  32. return new UniformArrayIntVec2(gl, activeUniform, uniformName, locations);
  33. case gl.INT_VEC3:
  34. case gl.BOOL_VEC3:
  35. return new UniformArrayIntVec3(gl, activeUniform, uniformName, locations);
  36. case gl.INT_VEC4:
  37. case gl.BOOL_VEC4:
  38. return new UniformArrayIntVec4(gl, activeUniform, uniformName, locations);
  39. case gl.FLOAT_MAT2:
  40. return new UniformArrayMat2(gl, activeUniform, uniformName, locations);
  41. case gl.FLOAT_MAT3:
  42. return new UniformArrayMat3(gl, activeUniform, uniformName, locations);
  43. case gl.FLOAT_MAT4:
  44. return new UniformArrayMat4(gl, activeUniform, uniformName, locations);
  45. default:
  46. throw new RuntimeError('Unrecognized uniform type: ' + activeUniform.type + ' for uniform "' + uniformName + '".');
  47. }
  48. }
  49. /**
  50. * @private
  51. */
  52. function UniformArrayFloat(gl, activeUniform, uniformName, locations) {
  53. var length = locations.length;
  54. /**
  55. * @type {String}
  56. * @readonly
  57. */
  58. this.name = uniformName;
  59. this.value = new Array(length);
  60. this._value = new Float32Array(length);
  61. this._gl = gl;
  62. this._location = locations[0];
  63. }
  64. UniformArrayFloat.prototype.set = function() {
  65. var value = this.value;
  66. var length = value.length;
  67. var arraybuffer = this._value;
  68. var changed = false;
  69. for (var i = 0; i < length; ++i) {
  70. var v = value[i];
  71. if (v !== arraybuffer[i]) {
  72. arraybuffer[i] = v;
  73. changed = true;
  74. }
  75. }
  76. if (changed) {
  77. this._gl.uniform1fv(this._location, arraybuffer);
  78. }
  79. };
  80. ///////////////////////////////////////////////////////////////////////////
  81. /**
  82. * @private
  83. */
  84. function UniformArrayFloatVec2(gl, activeUniform, uniformName, locations) {
  85. var length = locations.length;
  86. /**
  87. * @type {String}
  88. * @readonly
  89. */
  90. this.name = uniformName;
  91. this.value = new Array(length);
  92. this._value = new Float32Array(length * 2);
  93. this._gl = gl;
  94. this._location = locations[0];
  95. }
  96. UniformArrayFloatVec2.prototype.set = function() {
  97. var value = this.value;
  98. var length = value.length;
  99. var arraybuffer = this._value;
  100. var changed = false;
  101. var j = 0;
  102. for (var i = 0; i < length; ++i) {
  103. var v = value[i];
  104. if (!Cartesian2.equalsArray(v, arraybuffer, j)) {
  105. Cartesian2.pack(v, arraybuffer, j);
  106. changed = true;
  107. }
  108. j += 2;
  109. }
  110. if (changed) {
  111. this._gl.uniform2fv(this._location, arraybuffer);
  112. }
  113. };
  114. ///////////////////////////////////////////////////////////////////////////
  115. /**
  116. * @private
  117. */
  118. function UniformArrayFloatVec3(gl, activeUniform, uniformName, locations) {
  119. var length = locations.length;
  120. /**
  121. * @type {String}
  122. * @readonly
  123. */
  124. this.name = uniformName;
  125. this.value = new Array(length);
  126. this._value = new Float32Array(length * 3);
  127. this._gl = gl;
  128. this._location = locations[0];
  129. }
  130. UniformArrayFloatVec3.prototype.set = function() {
  131. var value = this.value;
  132. var length = value.length;
  133. var arraybuffer = this._value;
  134. var changed = false;
  135. var j = 0;
  136. for (var i = 0; i < length; ++i) {
  137. var v = value[i];
  138. if (defined(v.red)) {
  139. if ((v.red !== arraybuffer[j]) ||
  140. (v.green !== arraybuffer[j + 1]) ||
  141. (v.blue !== arraybuffer[j + 2])) {
  142. arraybuffer[j] = v.red;
  143. arraybuffer[j + 1] = v.green;
  144. arraybuffer[j + 2] = v.blue;
  145. changed = true;
  146. }
  147. } else if (defined(v.x)) {
  148. if (!Cartesian3.equalsArray(v, arraybuffer, j)) {
  149. Cartesian3.pack(v, arraybuffer, j);
  150. changed = true;
  151. }
  152. } else {
  153. //>>includeStart('debug', pragmas.debug);
  154. throw new DeveloperError('Invalid vec3 value.');
  155. //>>includeEnd('debug');
  156. }
  157. j += 3;
  158. }
  159. if (changed) {
  160. this._gl.uniform3fv(this._location, arraybuffer);
  161. }
  162. };
  163. ///////////////////////////////////////////////////////////////////////////
  164. /**
  165. * @private
  166. */
  167. function UniformArrayFloatVec4(gl, activeUniform, uniformName, locations) {
  168. var length = locations.length;
  169. /**
  170. * @type {String}
  171. * @readonly
  172. */
  173. this.name = uniformName;
  174. this.value = new Array(length);
  175. this._value = new Float32Array(length * 4);
  176. this._gl = gl;
  177. this._location = locations[0];
  178. }
  179. UniformArrayFloatVec4.prototype.set = function() {
  180. // PERFORMANCE_IDEA: if it is a common case that only a few elements
  181. // in a uniform array change, we could use heuristics to determine
  182. // when it is better to call uniform4f for each element that changed
  183. // vs. call uniform4fv once to set the entire array. This applies
  184. // to all uniform array types, not just vec4. We might not care
  185. // once we have uniform buffers since that will be the fast path.
  186. // PERFORMANCE_IDEA: Micro-optimization (I bet it works though):
  187. // As soon as changed is true, break into a separate loop that
  188. // does the copy without the equals check.
  189. var value = this.value;
  190. var length = value.length;
  191. var arraybuffer = this._value;
  192. var changed = false;
  193. var j = 0;
  194. for (var i = 0; i < length; ++i) {
  195. var v = value[i];
  196. if (defined(v.red)) {
  197. if (!Color.equalsArray(v, arraybuffer, j)) {
  198. Color.pack(v, arraybuffer, j);
  199. changed = true;
  200. }
  201. } else if (defined(v.x)) {
  202. if (!Cartesian4.equalsArray(v, arraybuffer, j)) {
  203. Cartesian4.pack(v, arraybuffer, j);
  204. changed = true;
  205. }
  206. } else {
  207. //>>includeStart('debug', pragmas.debug);
  208. throw new DeveloperError('Invalid vec4 value.');
  209. //>>includeEnd('debug');
  210. }
  211. j += 4;
  212. }
  213. if (changed) {
  214. this._gl.uniform4fv(this._location, arraybuffer);
  215. }
  216. };
  217. ///////////////////////////////////////////////////////////////////////////
  218. /**
  219. * @private
  220. */
  221. function UniformArraySampler(gl, activeUniform, uniformName, locations) {
  222. var length = locations.length;
  223. /**
  224. * @type {String}
  225. * @readonly
  226. */
  227. this.name = uniformName;
  228. this.value = new Array(length);
  229. this._value = new Float32Array(length);
  230. this._gl = gl;
  231. this._locations = locations;
  232. this.textureUnitIndex = undefined;
  233. }
  234. UniformArraySampler.prototype.set = function() {
  235. var gl = this._gl;
  236. var textureUnitIndex = gl.TEXTURE0 + this.textureUnitIndex;
  237. var value = this.value;
  238. var length = value.length;
  239. for (var i = 0; i < length; ++i) {
  240. var v = value[i];
  241. gl.activeTexture(textureUnitIndex + i);
  242. gl.bindTexture(v._target, v._texture);
  243. }
  244. };
  245. UniformArraySampler.prototype._setSampler = function(textureUnitIndex) {
  246. this.textureUnitIndex = textureUnitIndex;
  247. var locations = this._locations;
  248. var length = locations.length;
  249. for (var i = 0; i < length; ++i) {
  250. var index = textureUnitIndex + i;
  251. this._gl.uniform1i(locations[i], index);
  252. }
  253. return textureUnitIndex + length;
  254. };
  255. ///////////////////////////////////////////////////////////////////////////
  256. /**
  257. * @private
  258. */
  259. function UniformArrayInt(gl, activeUniform, uniformName, locations) {
  260. var length = locations.length;
  261. /**
  262. * @type {String}
  263. * @readonly
  264. */
  265. this.name = uniformName;
  266. this.value = new Array(length);
  267. this._value = new Int32Array(length);
  268. this._gl = gl;
  269. this._location = locations[0];
  270. }
  271. UniformArrayInt.prototype.set = function() {
  272. var value = this.value;
  273. var length = value.length;
  274. var arraybuffer = this._value;
  275. var changed = false;
  276. for (var i = 0; i < length; ++i) {
  277. var v = value[i];
  278. if (v !== arraybuffer[i]) {
  279. arraybuffer[i] = v;
  280. changed = true;
  281. }
  282. }
  283. if (changed) {
  284. this._gl.uniform1iv(this._location, arraybuffer);
  285. }
  286. };
  287. ///////////////////////////////////////////////////////////////////////////
  288. /**
  289. * @private
  290. */
  291. function UniformArrayIntVec2(gl, activeUniform, uniformName, locations) {
  292. var length = locations.length;
  293. /**
  294. * @type {String}
  295. * @readonly
  296. */
  297. this.name = uniformName;
  298. this.value = new Array(length);
  299. this._value = new Int32Array(length * 2);
  300. this._gl = gl;
  301. this._location = locations[0];
  302. }
  303. UniformArrayIntVec2.prototype.set = function() {
  304. var value = this.value;
  305. var length = value.length;
  306. var arraybuffer = this._value;
  307. var changed = false;
  308. var j = 0;
  309. for (var i = 0; i < length; ++i) {
  310. var v = value[i];
  311. if (!Cartesian2.equalsArray(v, arraybuffer, j)) {
  312. Cartesian2.pack(v, arraybuffer, j);
  313. changed = true;
  314. }
  315. j += 2;
  316. }
  317. if (changed) {
  318. this._gl.uniform2iv(this._location, arraybuffer);
  319. }
  320. };
  321. ///////////////////////////////////////////////////////////////////////////
  322. /**
  323. * @private
  324. */
  325. function UniformArrayIntVec3(gl, activeUniform, uniformName, locations) {
  326. var length = locations.length;
  327. /**
  328. * @type {String}
  329. * @readonly
  330. */
  331. this.name = uniformName;
  332. this.value = new Array(length);
  333. this._value = new Int32Array(length * 3);
  334. this._gl = gl;
  335. this._location = locations[0];
  336. }
  337. UniformArrayIntVec3.prototype.set = function() {
  338. var value = this.value;
  339. var length = value.length;
  340. var arraybuffer = this._value;
  341. var changed = false;
  342. var j = 0;
  343. for (var i = 0; i < length; ++i) {
  344. var v = value[i];
  345. if (!Cartesian3.equalsArray(v, arraybuffer, j)) {
  346. Cartesian3.pack(v, arraybuffer, j);
  347. changed = true;
  348. }
  349. j += 3;
  350. }
  351. if (changed) {
  352. this._gl.uniform3iv(this._location, arraybuffer);
  353. }
  354. };
  355. ///////////////////////////////////////////////////////////////////////////
  356. /**
  357. * @private
  358. */
  359. function UniformArrayIntVec4(gl, activeUniform, uniformName, locations) {
  360. var length = locations.length;
  361. /**
  362. * @type {String}
  363. * @readonly
  364. */
  365. this.name = uniformName;
  366. this.value = new Array(length);
  367. this._value = new Int32Array(length * 4);
  368. this._gl = gl;
  369. this._location = locations[0];
  370. }
  371. UniformArrayIntVec4.prototype.set = function() {
  372. var value = this.value;
  373. var length = value.length;
  374. var arraybuffer = this._value;
  375. var changed = false;
  376. var j = 0;
  377. for (var i = 0; i < length; ++i) {
  378. var v = value[i];
  379. if (!Cartesian4.equalsArray(v, arraybuffer, j)) {
  380. Cartesian4.pack(v, arraybuffer, j);
  381. changed = true;
  382. }
  383. j += 4;
  384. }
  385. if (changed) {
  386. this._gl.uniform4iv(this._location, arraybuffer);
  387. }
  388. };
  389. ///////////////////////////////////////////////////////////////////////////
  390. /**
  391. * @private
  392. */
  393. function UniformArrayMat2(gl, activeUniform, uniformName, locations) {
  394. var length = locations.length;
  395. /**
  396. * @type {String}
  397. * @readonly
  398. */
  399. this.name = uniformName;
  400. this.value = new Array(length);
  401. this._value = new Float32Array(length * 4);
  402. this._gl = gl;
  403. this._location = locations[0];
  404. }
  405. UniformArrayMat2.prototype.set = function() {
  406. var value = this.value;
  407. var length = value.length;
  408. var arraybuffer = this._value;
  409. var changed = false;
  410. var j = 0;
  411. for (var i = 0; i < length; ++i) {
  412. var v = value[i];
  413. if (!Matrix2.equalsArray(v, arraybuffer, j)) {
  414. Matrix2.pack(v, arraybuffer, j);
  415. changed = true;
  416. }
  417. j += 4;
  418. }
  419. if (changed) {
  420. this._gl.uniformMatrix2fv(this._location, false, arraybuffer);
  421. }
  422. };
  423. ///////////////////////////////////////////////////////////////////////////
  424. /**
  425. * @private
  426. */
  427. function UniformArrayMat3(gl, activeUniform, uniformName, locations) {
  428. var length = locations.length;
  429. /**
  430. * @type {String}
  431. * @readonly
  432. */
  433. this.name = uniformName;
  434. this.value = new Array(length);
  435. this._value = new Float32Array(length * 9);
  436. this._gl = gl;
  437. this._location = locations[0];
  438. }
  439. UniformArrayMat3.prototype.set = function() {
  440. var value = this.value;
  441. var length = value.length;
  442. var arraybuffer = this._value;
  443. var changed = false;
  444. var j = 0;
  445. for (var i = 0; i < length; ++i) {
  446. var v = value[i];
  447. if (!Matrix3.equalsArray(v, arraybuffer, j)) {
  448. Matrix3.pack(v, arraybuffer, j);
  449. changed = true;
  450. }
  451. j += 9;
  452. }
  453. if (changed) {
  454. this._gl.uniformMatrix3fv(this._location, false, arraybuffer);
  455. }
  456. };
  457. ///////////////////////////////////////////////////////////////////////////
  458. /**
  459. * @private
  460. */
  461. function UniformArrayMat4(gl, activeUniform, uniformName, locations) {
  462. var length = locations.length;
  463. /**
  464. * @type {String}
  465. * @readonly
  466. */
  467. this.name = uniformName;
  468. this.value = new Array(length);
  469. this._value = new Float32Array(length * 16);
  470. this._gl = gl;
  471. this._location = locations[0];
  472. }
  473. UniformArrayMat4.prototype.set = function() {
  474. var value = this.value;
  475. var length = value.length;
  476. var arraybuffer = this._value;
  477. var changed = false;
  478. var j = 0;
  479. for (var i = 0; i < length; ++i) {
  480. var v = value[i];
  481. if (!Matrix4.equalsArray(v, arraybuffer, j)) {
  482. Matrix4.pack(v, arraybuffer, j);
  483. changed = true;
  484. }
  485. j += 16;
  486. }
  487. if (changed) {
  488. this._gl.uniformMatrix4fv(this._location, false, arraybuffer);
  489. }
  490. };
  491. export default createUniformArray;