nativePipelineContext.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. export class NativePipelineContext {
  2. get isReady() {
  3. if (this.compilationError) {
  4. const message = this.compilationError.message;
  5. throw new Error("SHADER ERROR" + (typeof message === "string" ? "\n" + message : ""));
  6. }
  7. return this.isCompiled;
  8. }
  9. _getVertexShaderCode() {
  10. return null;
  11. }
  12. _getFragmentShaderCode() {
  13. return null;
  14. }
  15. constructor(engine, isAsync) {
  16. this.isCompiled = false;
  17. this._valueCache = {};
  18. this._engine = engine;
  19. this.isAsync = isAsync;
  20. }
  21. _fillEffectInformation(effect, uniformBuffersNames, uniformsNames, uniforms, samplerList, samplers, attributesNames, attributes) {
  22. const engine = this._engine;
  23. if (engine.supportsUniformBuffers) {
  24. for (const name in uniformBuffersNames) {
  25. effect.bindUniformBlock(name, uniformBuffersNames[name]);
  26. }
  27. }
  28. const effectAvailableUniforms = this._engine.getUniforms(this, uniformsNames);
  29. effectAvailableUniforms.forEach((uniform, index) => {
  30. uniforms[uniformsNames[index]] = uniform;
  31. });
  32. this._uniforms = uniforms;
  33. let index;
  34. for (index = 0; index < samplerList.length; index++) {
  35. const sampler = effect.getUniform(samplerList[index]);
  36. if (sampler == null) {
  37. samplerList.splice(index, 1);
  38. index--;
  39. }
  40. }
  41. samplerList.forEach((name, index) => {
  42. samplers[name] = index;
  43. });
  44. attributes.push(...engine.getAttributes(this, attributesNames));
  45. }
  46. /**
  47. * Release all associated resources.
  48. **/
  49. dispose() {
  50. this._uniforms = {};
  51. }
  52. /**
  53. * @internal
  54. */
  55. _cacheMatrix(uniformName, matrix) {
  56. const cache = this._valueCache[uniformName];
  57. const flag = matrix.updateFlag;
  58. if (cache !== undefined && cache === flag) {
  59. return false;
  60. }
  61. this._valueCache[uniformName] = flag;
  62. return true;
  63. }
  64. /**
  65. * @internal
  66. */
  67. _cacheFloat2(uniformName, x, y) {
  68. let cache = this._valueCache[uniformName];
  69. if (!cache) {
  70. cache = [x, y];
  71. this._valueCache[uniformName] = cache;
  72. return true;
  73. }
  74. let changed = false;
  75. if (cache[0] !== x) {
  76. cache[0] = x;
  77. changed = true;
  78. }
  79. if (cache[1] !== y) {
  80. cache[1] = y;
  81. changed = true;
  82. }
  83. return changed;
  84. }
  85. /**
  86. * @internal
  87. */
  88. _cacheFloat3(uniformName, x, y, z) {
  89. let cache = this._valueCache[uniformName];
  90. if (!cache) {
  91. cache = [x, y, z];
  92. this._valueCache[uniformName] = cache;
  93. return true;
  94. }
  95. let changed = false;
  96. if (cache[0] !== x) {
  97. cache[0] = x;
  98. changed = true;
  99. }
  100. if (cache[1] !== y) {
  101. cache[1] = y;
  102. changed = true;
  103. }
  104. if (cache[2] !== z) {
  105. cache[2] = z;
  106. changed = true;
  107. }
  108. return changed;
  109. }
  110. /**
  111. * @internal
  112. */
  113. _cacheFloat4(uniformName, x, y, z, w) {
  114. let cache = this._valueCache[uniformName];
  115. if (!cache) {
  116. cache = [x, y, z, w];
  117. this._valueCache[uniformName] = cache;
  118. return true;
  119. }
  120. let changed = false;
  121. if (cache[0] !== x) {
  122. cache[0] = x;
  123. changed = true;
  124. }
  125. if (cache[1] !== y) {
  126. cache[1] = y;
  127. changed = true;
  128. }
  129. if (cache[2] !== z) {
  130. cache[2] = z;
  131. changed = true;
  132. }
  133. if (cache[3] !== w) {
  134. cache[3] = w;
  135. changed = true;
  136. }
  137. return changed;
  138. }
  139. /**
  140. * Sets an integer value on a uniform variable.
  141. * @param uniformName Name of the variable.
  142. * @param value Value to be set.
  143. */
  144. setInt(uniformName, value) {
  145. const cache = this._valueCache[uniformName];
  146. if (cache !== undefined && cache === value) {
  147. return;
  148. }
  149. if (this._engine.setInt(this._uniforms[uniformName], value)) {
  150. this._valueCache[uniformName] = value;
  151. }
  152. }
  153. /**
  154. * Sets a int2 on a uniform variable.
  155. * @param uniformName Name of the variable.
  156. * @param x First int in int2.
  157. * @param y Second int in int2.
  158. */
  159. setInt2(uniformName, x, y) {
  160. if (this._cacheFloat2(uniformName, x, y)) {
  161. if (!this._engine.setInt2(this._uniforms[uniformName], x, y)) {
  162. this._valueCache[uniformName] = null;
  163. }
  164. }
  165. }
  166. /**
  167. * Sets a int3 on a uniform variable.
  168. * @param uniformName Name of the variable.
  169. * @param x First int in int3.
  170. * @param y Second int in int3.
  171. * @param z Third int in int3.
  172. */
  173. setInt3(uniformName, x, y, z) {
  174. if (this._cacheFloat3(uniformName, x, y, z)) {
  175. if (!this._engine.setInt3(this._uniforms[uniformName], x, y, z)) {
  176. this._valueCache[uniformName] = null;
  177. }
  178. }
  179. }
  180. /**
  181. * Sets a int4 on a uniform variable.
  182. * @param uniformName Name of the variable.
  183. * @param x First int in int4.
  184. * @param y Second int in int4.
  185. * @param z Third int in int4.
  186. * @param w Fourth int in int4.
  187. */
  188. setInt4(uniformName, x, y, z, w) {
  189. if (this._cacheFloat4(uniformName, x, y, z, w)) {
  190. if (!this._engine.setInt4(this._uniforms[uniformName], x, y, z, w)) {
  191. this._valueCache[uniformName] = null;
  192. }
  193. }
  194. }
  195. /**
  196. * Sets an int array on a uniform variable.
  197. * @param uniformName Name of the variable.
  198. * @param array array to be set.
  199. */
  200. setIntArray(uniformName, array) {
  201. this._valueCache[uniformName] = null;
  202. this._engine.setIntArray(this._uniforms[uniformName], array);
  203. }
  204. /**
  205. * Sets an int array 2 on a uniform variable. (Array is specified as single array eg. [1,2,3,4] will result in [[1,2],[3,4]] in the shader)
  206. * @param uniformName Name of the variable.
  207. * @param array array to be set.
  208. */
  209. setIntArray2(uniformName, array) {
  210. this._valueCache[uniformName] = null;
  211. this._engine.setIntArray2(this._uniforms[uniformName], array);
  212. }
  213. /**
  214. * Sets an int array 3 on a uniform variable. (Array is specified as single array eg. [1,2,3,4,5,6] will result in [[1,2,3],[4,5,6]] in the shader)
  215. * @param uniformName Name of the variable.
  216. * @param array array to be set.
  217. */
  218. setIntArray3(uniformName, array) {
  219. this._valueCache[uniformName] = null;
  220. this._engine.setIntArray3(this._uniforms[uniformName], array);
  221. }
  222. /**
  223. * Sets an int array 4 on a uniform variable. (Array is specified as single array eg. [1,2,3,4,5,6,7,8] will result in [[1,2,3,4],[5,6,7,8]] in the shader)
  224. * @param uniformName Name of the variable.
  225. * @param array array to be set.
  226. */
  227. setIntArray4(uniformName, array) {
  228. this._valueCache[uniformName] = null;
  229. this._engine.setIntArray4(this._uniforms[uniformName], array);
  230. }
  231. /**
  232. * Sets an unsigned integer value on a uniform variable.
  233. * @param uniformName Name of the variable.
  234. * @param value Value to be set.
  235. */
  236. setUInt(uniformName, value) {
  237. const cache = this._valueCache[uniformName];
  238. if (cache !== undefined && cache === value) {
  239. return;
  240. }
  241. if (this._engine.setUInt(this._uniforms[uniformName], value)) {
  242. this._valueCache[uniformName] = value;
  243. }
  244. }
  245. /**
  246. * Sets a unsigned int2 on a uniform variable.
  247. * @param uniformName Name of the variable.
  248. * @param x First unsigned int in uint2.
  249. * @param y Second unsigned int in uint2.
  250. */
  251. setUInt2(uniformName, x, y) {
  252. if (this._cacheFloat2(uniformName, x, y)) {
  253. if (!this._engine.setUInt2(this._uniforms[uniformName], x, y)) {
  254. this._valueCache[uniformName] = null;
  255. }
  256. }
  257. }
  258. /**
  259. * Sets a unsigned int3 on a uniform variable.
  260. * @param uniformName Name of the variable.
  261. * @param x First unsigned int in uint3.
  262. * @param y Second unsigned int in uint3.
  263. * @param z Third unsigned int in uint3.
  264. */
  265. setUInt3(uniformName, x, y, z) {
  266. if (this._cacheFloat3(uniformName, x, y, z)) {
  267. if (!this._engine.setUInt3(this._uniforms[uniformName], x, y, z)) {
  268. this._valueCache[uniformName] = null;
  269. }
  270. }
  271. }
  272. /**
  273. * Sets a unsigned int4 on a uniform variable.
  274. * @param uniformName Name of the variable.
  275. * @param x First unsigned int in uint4.
  276. * @param y Second unsigned int in uint4.
  277. * @param z Third unsigned int in uint4.
  278. * @param w Fourth unsigned int in uint4.
  279. */
  280. setUInt4(uniformName, x, y, z, w) {
  281. if (this._cacheFloat4(uniformName, x, y, z, w)) {
  282. if (!this._engine.setUInt4(this._uniforms[uniformName], x, y, z, w)) {
  283. this._valueCache[uniformName] = null;
  284. }
  285. }
  286. }
  287. /**
  288. * Sets an unsigned int array on a uniform variable.
  289. * @param uniformName Name of the variable.
  290. * @param array array to be set.
  291. */
  292. setUIntArray(uniformName, array) {
  293. this._valueCache[uniformName] = null;
  294. this._engine.setUIntArray(this._uniforms[uniformName], array);
  295. }
  296. /**
  297. * Sets an unsigned int array 2 on a uniform variable. (Array is specified as single array eg. [1,2,3,4] will result in [[1,2],[3,4]] in the shader)
  298. * @param uniformName Name of the variable.
  299. * @param array array to be set.
  300. */
  301. setUIntArray2(uniformName, array) {
  302. this._valueCache[uniformName] = null;
  303. this._engine.setUIntArray2(this._uniforms[uniformName], array);
  304. }
  305. /**
  306. * Sets an unsigned int array 3 on a uniform variable. (Array is specified as single array eg. [1,2,3,4,5,6] will result in [[1,2,3],[4,5,6]] in the shader)
  307. * @param uniformName Name of the variable.
  308. * @param array array to be set.
  309. */
  310. setUIntArray3(uniformName, array) {
  311. this._valueCache[uniformName] = null;
  312. this._engine.setUIntArray3(this._uniforms[uniformName], array);
  313. }
  314. /**
  315. * Sets an unsigned int array 4 on a uniform variable. (Array is specified as single array eg. [1,2,3,4,5,6,7,8] will result in [[1,2,3,4],[5,6,7,8]] in the shader)
  316. * @param uniformName Name of the variable.
  317. * @param array array to be set.
  318. */
  319. setUIntArray4(uniformName, array) {
  320. this._valueCache[uniformName] = null;
  321. this._engine.setUIntArray4(this._uniforms[uniformName], array);
  322. }
  323. /**
  324. * Sets an float array on a uniform variable.
  325. * @param uniformName Name of the variable.
  326. * @param array array to be set.
  327. */
  328. setFloatArray(uniformName, array) {
  329. this._valueCache[uniformName] = null;
  330. this._engine.setFloatArray(this._uniforms[uniformName], array);
  331. }
  332. /**
  333. * Sets an float array 2 on a uniform variable. (Array is specified as single array eg. [1,2,3,4] will result in [[1,2],[3,4]] in the shader)
  334. * @param uniformName Name of the variable.
  335. * @param array array to be set.
  336. */
  337. setFloatArray2(uniformName, array) {
  338. this._valueCache[uniformName] = null;
  339. this._engine.setFloatArray2(this._uniforms[uniformName], array);
  340. }
  341. /**
  342. * Sets an float array 3 on a uniform variable. (Array is specified as single array eg. [1,2,3,4,5,6] will result in [[1,2,3],[4,5,6]] in the shader)
  343. * @param uniformName Name of the variable.
  344. * @param array array to be set.
  345. */
  346. setFloatArray3(uniformName, array) {
  347. this._valueCache[uniformName] = null;
  348. this._engine.setFloatArray3(this._uniforms[uniformName], array);
  349. }
  350. /**
  351. * Sets an float array 4 on a uniform variable. (Array is specified as single array eg. [1,2,3,4,5,6,7,8] will result in [[1,2,3,4],[5,6,7,8]] in the shader)
  352. * @param uniformName Name of the variable.
  353. * @param array array to be set.
  354. */
  355. setFloatArray4(uniformName, array) {
  356. this._valueCache[uniformName] = null;
  357. this._engine.setFloatArray4(this._uniforms[uniformName], array);
  358. }
  359. /**
  360. * Sets an array on a uniform variable.
  361. * @param uniformName Name of the variable.
  362. * @param array array to be set.
  363. */
  364. setArray(uniformName, array) {
  365. this._valueCache[uniformName] = null;
  366. this._engine.setArray(this._uniforms[uniformName], array);
  367. }
  368. /**
  369. * Sets an array 2 on a uniform variable. (Array is specified as single array eg. [1,2,3,4] will result in [[1,2],[3,4]] in the shader)
  370. * @param uniformName Name of the variable.
  371. * @param array array to be set.
  372. */
  373. setArray2(uniformName, array) {
  374. this._valueCache[uniformName] = null;
  375. this._engine.setArray2(this._uniforms[uniformName], array);
  376. }
  377. /**
  378. * Sets an array 3 on a uniform variable. (Array is specified as single array eg. [1,2,3,4,5,6] will result in [[1,2,3],[4,5,6]] in the shader)
  379. * @param uniformName Name of the variable.
  380. * @param array array to be set.
  381. */
  382. setArray3(uniformName, array) {
  383. this._valueCache[uniformName] = null;
  384. this._engine.setArray3(this._uniforms[uniformName], array);
  385. }
  386. /**
  387. * Sets an array 4 on a uniform variable. (Array is specified as single array eg. [1,2,3,4,5,6,7,8] will result in [[1,2,3,4],[5,6,7,8]] in the shader)
  388. * @param uniformName Name of the variable.
  389. * @param array array to be set.
  390. */
  391. setArray4(uniformName, array) {
  392. this._valueCache[uniformName] = null;
  393. this._engine.setArray4(this._uniforms[uniformName], array);
  394. }
  395. /**
  396. * Sets matrices on a uniform variable.
  397. * @param uniformName Name of the variable.
  398. * @param matrices matrices to be set.
  399. */
  400. setMatrices(uniformName, matrices) {
  401. if (!matrices) {
  402. return;
  403. }
  404. this._valueCache[uniformName] = null;
  405. this._engine.setMatrices(this._uniforms[uniformName], matrices);
  406. }
  407. /**
  408. * Sets matrix on a uniform variable.
  409. * @param uniformName Name of the variable.
  410. * @param matrix matrix to be set.
  411. */
  412. setMatrix(uniformName, matrix) {
  413. if (this._cacheMatrix(uniformName, matrix)) {
  414. if (!this._engine.setMatrices(this._uniforms[uniformName], matrix.asArray())) {
  415. this._valueCache[uniformName] = null;
  416. }
  417. }
  418. }
  419. /**
  420. * Sets a 3x3 matrix on a uniform variable. (Specified as [1,2,3,4,5,6,7,8,9] will result in [1,2,3][4,5,6][7,8,9] matrix)
  421. * @param uniformName Name of the variable.
  422. * @param matrix matrix to be set.
  423. */
  424. setMatrix3x3(uniformName, matrix) {
  425. this._valueCache[uniformName] = null;
  426. this._engine.setMatrix3x3(this._uniforms[uniformName], matrix);
  427. }
  428. /**
  429. * Sets a 2x2 matrix on a uniform variable. (Specified as [1,2,3,4] will result in [1,2][3,4] matrix)
  430. * @param uniformName Name of the variable.
  431. * @param matrix matrix to be set.
  432. */
  433. setMatrix2x2(uniformName, matrix) {
  434. this._valueCache[uniformName] = null;
  435. this._engine.setMatrix2x2(this._uniforms[uniformName], matrix);
  436. }
  437. /**
  438. * Sets a float on a uniform variable.
  439. * @param uniformName Name of the variable.
  440. * @param value value to be set.
  441. */
  442. setFloat(uniformName, value) {
  443. const cache = this._valueCache[uniformName];
  444. if (cache !== undefined && cache === value) {
  445. return;
  446. }
  447. if (this._engine.setFloat(this._uniforms[uniformName], value)) {
  448. this._valueCache[uniformName] = value;
  449. }
  450. }
  451. /**
  452. * Sets a boolean on a uniform variable.
  453. * @param uniformName Name of the variable.
  454. * @param bool value to be set.
  455. */
  456. setBool(uniformName, bool) {
  457. const cache = this._valueCache[uniformName];
  458. if (cache !== undefined && cache === bool) {
  459. return;
  460. }
  461. if (this._engine.setInt(this._uniforms[uniformName], bool ? 1 : 0)) {
  462. this._valueCache[uniformName] = bool ? 1 : 0;
  463. }
  464. }
  465. /**
  466. * Sets a Vector2 on a uniform variable.
  467. * @param uniformName Name of the variable.
  468. * @param vector2 vector2 to be set.
  469. */
  470. setVector2(uniformName, vector2) {
  471. if (this._cacheFloat2(uniformName, vector2.x, vector2.y)) {
  472. if (!this._engine.setFloat2(this._uniforms[uniformName], vector2.x, vector2.y)) {
  473. this._valueCache[uniformName] = null;
  474. }
  475. }
  476. }
  477. /**
  478. * Sets a float2 on a uniform variable.
  479. * @param uniformName Name of the variable.
  480. * @param x First float in float2.
  481. * @param y Second float in float2.
  482. */
  483. setFloat2(uniformName, x, y) {
  484. if (this._cacheFloat2(uniformName, x, y)) {
  485. if (!this._engine.setFloat2(this._uniforms[uniformName], x, y)) {
  486. this._valueCache[uniformName] = null;
  487. }
  488. }
  489. }
  490. /**
  491. * Sets a Vector3 on a uniform variable.
  492. * @param uniformName Name of the variable.
  493. * @param vector3 Value to be set.
  494. */
  495. setVector3(uniformName, vector3) {
  496. if (this._cacheFloat3(uniformName, vector3.x, vector3.y, vector3.z)) {
  497. if (!this._engine.setFloat3(this._uniforms[uniformName], vector3.x, vector3.y, vector3.z)) {
  498. this._valueCache[uniformName] = null;
  499. }
  500. }
  501. }
  502. /**
  503. * Sets a float3 on a uniform variable.
  504. * @param uniformName Name of the variable.
  505. * @param x First float in float3.
  506. * @param y Second float in float3.
  507. * @param z Third float in float3.
  508. */
  509. setFloat3(uniformName, x, y, z) {
  510. if (this._cacheFloat3(uniformName, x, y, z)) {
  511. if (!this._engine.setFloat3(this._uniforms[uniformName], x, y, z)) {
  512. this._valueCache[uniformName] = null;
  513. }
  514. }
  515. }
  516. /**
  517. * Sets a Vector4 on a uniform variable.
  518. * @param uniformName Name of the variable.
  519. * @param vector4 Value to be set.
  520. */
  521. setVector4(uniformName, vector4) {
  522. if (this._cacheFloat4(uniformName, vector4.x, vector4.y, vector4.z, vector4.w)) {
  523. if (!this._engine.setFloat4(this._uniforms[uniformName], vector4.x, vector4.y, vector4.z, vector4.w)) {
  524. this._valueCache[uniformName] = null;
  525. }
  526. }
  527. }
  528. /**
  529. * Sets a Quaternion on a uniform variable.
  530. * @param uniformName Name of the variable.
  531. * @param quaternion Value to be set.
  532. */
  533. setQuaternion(uniformName, quaternion) {
  534. if (this._cacheFloat4(uniformName, quaternion.x, quaternion.y, quaternion.z, quaternion.w)) {
  535. if (!this._engine.setFloat4(this._uniforms[uniformName], quaternion.x, quaternion.y, quaternion.z, quaternion.w)) {
  536. this._valueCache[uniformName] = null;
  537. }
  538. }
  539. }
  540. /**
  541. * Sets a float4 on a uniform variable.
  542. * @param uniformName Name of the variable.
  543. * @param x First float in float4.
  544. * @param y Second float in float4.
  545. * @param z Third float in float4.
  546. * @param w Fourth float in float4.
  547. */
  548. setFloat4(uniformName, x, y, z, w) {
  549. if (this._cacheFloat4(uniformName, x, y, z, w)) {
  550. if (!this._engine.setFloat4(this._uniforms[uniformName], x, y, z, w)) {
  551. this._valueCache[uniformName] = null;
  552. }
  553. }
  554. }
  555. /**
  556. * Sets a Color3 on a uniform variable.
  557. * @param uniformName Name of the variable.
  558. * @param color3 Value to be set.
  559. */
  560. setColor3(uniformName, color3) {
  561. if (this._cacheFloat3(uniformName, color3.r, color3.g, color3.b)) {
  562. if (!this._engine.setFloat3(this._uniforms[uniformName], color3.r, color3.g, color3.b)) {
  563. this._valueCache[uniformName] = null;
  564. }
  565. }
  566. }
  567. /**
  568. * Sets a Color4 on a uniform variable.
  569. * @param uniformName Name of the variable.
  570. * @param color3 Value to be set.
  571. * @param alpha Alpha value to be set.
  572. */
  573. setColor4(uniformName, color3, alpha) {
  574. if (this._cacheFloat4(uniformName, color3.r, color3.g, color3.b, alpha)) {
  575. if (!this._engine.setFloat4(this._uniforms[uniformName], color3.r, color3.g, color3.b, alpha)) {
  576. this._valueCache[uniformName] = null;
  577. }
  578. }
  579. }
  580. /**
  581. * Sets a Color4 on a uniform variable
  582. * @param uniformName defines the name of the variable
  583. * @param color4 defines the value to be set
  584. */
  585. setDirectColor4(uniformName, color4) {
  586. if (this._cacheFloat4(uniformName, color4.r, color4.g, color4.b, color4.a)) {
  587. if (!this._engine.setFloat4(this._uniforms[uniformName], color4.r, color4.g, color4.b, color4.a)) {
  588. this._valueCache[uniformName] = null;
  589. }
  590. }
  591. }
  592. }
  593. //# sourceMappingURL=nativePipelineContext.js.map