webgpuPipelineContext.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. import { UniformBuffer } from "../../Materials/uniformBuffer.js";
  2. import { WebGPUShaderProcessor } from "./webgpuShaderProcessor.js";
  3. /** @internal */
  4. export class WebGPUPipelineContext {
  5. get isAsync() {
  6. return false;
  7. }
  8. get isReady() {
  9. if (this.stages) {
  10. return true;
  11. }
  12. return false;
  13. }
  14. constructor(shaderProcessingContext, engine) {
  15. // The field is indexed by textureState. See @WebGPUMaterialContext.textureState for more information.
  16. this.bindGroupLayouts = {};
  17. this._name = "unnamed";
  18. this.shaderProcessingContext = shaderProcessingContext;
  19. this._leftOverUniformsByName = {};
  20. this.engine = engine;
  21. this.vertexBufferKindToType = {};
  22. }
  23. _handlesSpectorRebuildCallback() {
  24. // Nothing to do yet for spector.
  25. }
  26. _fillEffectInformation(effect, uniformBuffersNames, uniformsNames, uniforms, samplerList, samplers, attributesNames, attributes) {
  27. const engine = this.engine;
  28. if (engine._doNotHandleContextLost) {
  29. effect._fragmentSourceCode = "";
  30. effect._vertexSourceCode = "";
  31. }
  32. const foundSamplers = this.shaderProcessingContext.availableTextures;
  33. let index;
  34. for (index = 0; index < samplerList.length; index++) {
  35. const name = samplerList[index];
  36. const sampler = foundSamplers[samplerList[index]];
  37. if (sampler == null || sampler == undefined) {
  38. samplerList.splice(index, 1);
  39. index--;
  40. }
  41. else {
  42. samplers[name] = index;
  43. }
  44. }
  45. for (const attr of engine.getAttributes(this, attributesNames)) {
  46. attributes.push(attr);
  47. }
  48. // Build the uniform layout for the left over uniforms.
  49. this.buildUniformLayout();
  50. const attributeNamesFromEffect = [];
  51. const attributeLocationsFromEffect = [];
  52. for (index = 0; index < attributesNames.length; index++) {
  53. const location = attributes[index];
  54. if (location >= 0) {
  55. attributeNamesFromEffect.push(attributesNames[index]);
  56. attributeLocationsFromEffect.push(location);
  57. }
  58. }
  59. this.shaderProcessingContext.attributeNamesFromEffect = attributeNamesFromEffect;
  60. this.shaderProcessingContext.attributeLocationsFromEffect = attributeLocationsFromEffect;
  61. }
  62. /** @internal */
  63. /**
  64. * Build the uniform buffer used in the material.
  65. */
  66. buildUniformLayout() {
  67. if (!this.shaderProcessingContext.leftOverUniforms.length) {
  68. return;
  69. }
  70. this.uniformBuffer = new UniformBuffer(this.engine, undefined, undefined, "leftOver-" + this._name);
  71. for (const leftOverUniform of this.shaderProcessingContext.leftOverUniforms) {
  72. const type = leftOverUniform.type.replace(/^(.*?)(<.*>)?$/, "$1");
  73. const size = WebGPUShaderProcessor.UniformSizes[type];
  74. this.uniformBuffer.addUniform(leftOverUniform.name, size, leftOverUniform.length);
  75. this._leftOverUniformsByName[leftOverUniform.name] = leftOverUniform.type;
  76. }
  77. this.uniformBuffer.create();
  78. }
  79. /**
  80. * Release all associated resources.
  81. **/
  82. dispose() {
  83. if (this.uniformBuffer) {
  84. this.uniformBuffer.dispose();
  85. }
  86. }
  87. /**
  88. * Sets an integer value on a uniform variable.
  89. * @param uniformName Name of the variable.
  90. * @param value Value to be set.
  91. */
  92. setInt(uniformName, value) {
  93. if (!this.uniformBuffer || !this._leftOverUniformsByName[uniformName]) {
  94. return;
  95. }
  96. this.uniformBuffer.updateInt(uniformName, value);
  97. }
  98. /**
  99. * Sets an int2 value on a uniform variable.
  100. * @param uniformName Name of the variable.
  101. * @param x First int in int2.
  102. * @param y Second int in int2.
  103. */
  104. setInt2(uniformName, x, y) {
  105. if (!this.uniformBuffer || !this._leftOverUniformsByName[uniformName]) {
  106. return;
  107. }
  108. this.uniformBuffer.updateInt2(uniformName, x, y);
  109. }
  110. /**
  111. * Sets an int3 value on a uniform variable.
  112. * @param uniformName Name of the variable.
  113. * @param x First int in int3.
  114. * @param y Second int in int3.
  115. * @param z Third int in int3.
  116. */
  117. setInt3(uniformName, x, y, z) {
  118. if (!this.uniformBuffer || !this._leftOverUniformsByName[uniformName]) {
  119. return;
  120. }
  121. this.uniformBuffer.updateInt3(uniformName, x, y, z);
  122. }
  123. /**
  124. * Sets an int4 value on a uniform variable.
  125. * @param uniformName Name of the variable.
  126. * @param x First int in int4.
  127. * @param y Second int in int4.
  128. * @param z Third int in int4.
  129. * @param w Fourth int in int4.
  130. */
  131. setInt4(uniformName, x, y, z, w) {
  132. if (!this.uniformBuffer || !this._leftOverUniformsByName[uniformName]) {
  133. return;
  134. }
  135. this.uniformBuffer.updateInt4(uniformName, x, y, z, w);
  136. }
  137. /**
  138. * Sets an int array on a uniform variable.
  139. * @param uniformName Name of the variable.
  140. * @param array array to be set.
  141. */
  142. setIntArray(uniformName, array) {
  143. if (!this.uniformBuffer || !this._leftOverUniformsByName[uniformName]) {
  144. return;
  145. }
  146. this.uniformBuffer.updateIntArray(uniformName, array);
  147. }
  148. /**
  149. * 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)
  150. * @param uniformName Name of the variable.
  151. * @param array array to be set.
  152. */
  153. setIntArray2(uniformName, array) {
  154. this.setIntArray(uniformName, array);
  155. }
  156. /**
  157. * 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)
  158. * @param uniformName Name of the variable.
  159. * @param array array to be set.
  160. */
  161. setIntArray3(uniformName, array) {
  162. this.setIntArray(uniformName, array);
  163. }
  164. /**
  165. * 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)
  166. * @param uniformName Name of the variable.
  167. * @param array array to be set.
  168. */
  169. setIntArray4(uniformName, array) {
  170. this.setIntArray(uniformName, array);
  171. }
  172. /**
  173. * Sets an unsigned integer value on a uniform variable.
  174. * @param uniformName Name of the variable.
  175. * @param value Value to be set.
  176. */
  177. setUInt(uniformName, value) {
  178. if (!this.uniformBuffer || !this._leftOverUniformsByName[uniformName]) {
  179. return;
  180. }
  181. this.uniformBuffer.updateUInt(uniformName, value);
  182. }
  183. /**
  184. * Sets an unsigned int2 value on a uniform variable.
  185. * @param uniformName Name of the variable.
  186. * @param x First unsigned int in uint2.
  187. * @param y Second unsigned int in uint2.
  188. */
  189. setUInt2(uniformName, x, y) {
  190. if (!this.uniformBuffer || !this._leftOverUniformsByName[uniformName]) {
  191. return;
  192. }
  193. this.uniformBuffer.updateUInt2(uniformName, x, y);
  194. }
  195. /**
  196. * Sets an unsigned int3 value on a uniform variable.
  197. * @param uniformName Name of the variable.
  198. * @param x First unsigned int in uint3.
  199. * @param y Second unsigned int in uint3.
  200. * @param z Third unsigned int in uint3.
  201. */
  202. setUInt3(uniformName, x, y, z) {
  203. if (!this.uniformBuffer || !this._leftOverUniformsByName[uniformName]) {
  204. return;
  205. }
  206. this.uniformBuffer.updateUInt3(uniformName, x, y, z);
  207. }
  208. /**
  209. * Sets an unsigned int4 value on a uniform variable.
  210. * @param uniformName Name of the variable.
  211. * @param x First unsigned int in uint4.
  212. * @param y Second unsigned int in uint4.
  213. * @param z Third unsigned int in uint4.
  214. * @param w Fourth unsigned int in uint4.
  215. */
  216. setUInt4(uniformName, x, y, z, w) {
  217. if (!this.uniformBuffer || !this._leftOverUniformsByName[uniformName]) {
  218. return;
  219. }
  220. this.uniformBuffer.updateUInt4(uniformName, x, y, z, w);
  221. }
  222. /**
  223. * Sets an unsigned int array on a uniform variable.
  224. * @param uniformName Name of the variable.
  225. * @param array array to be set.
  226. */
  227. setUIntArray(uniformName, array) {
  228. if (!this.uniformBuffer || !this._leftOverUniformsByName[uniformName]) {
  229. return;
  230. }
  231. this.uniformBuffer.updateUIntArray(uniformName, array);
  232. }
  233. /**
  234. * 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)
  235. * @param uniformName Name of the variable.
  236. * @param array array to be set.
  237. */
  238. setUIntArray2(uniformName, array) {
  239. this.setUIntArray(uniformName, array);
  240. }
  241. /**
  242. * 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)
  243. * @param uniformName Name of the variable.
  244. * @param array array to be set.
  245. */
  246. setUIntArray3(uniformName, array) {
  247. this.setUIntArray(uniformName, array);
  248. }
  249. /**
  250. * 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)
  251. * @param uniformName Name of the variable.
  252. * @param array array to be set.
  253. */
  254. setUIntArray4(uniformName, array) {
  255. this.setUIntArray(uniformName, array);
  256. }
  257. /**
  258. * Sets an array on a uniform variable.
  259. * @param uniformName Name of the variable.
  260. * @param array array to be set.
  261. */
  262. setArray(uniformName, array) {
  263. if (!this.uniformBuffer || !this._leftOverUniformsByName[uniformName]) {
  264. return;
  265. }
  266. this.uniformBuffer.updateArray(uniformName, array);
  267. }
  268. /**
  269. * 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)
  270. * @param uniformName Name of the variable.
  271. * @param array array to be set.
  272. */
  273. setArray2(uniformName, array) {
  274. this.setArray(uniformName, array);
  275. }
  276. /**
  277. * 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)
  278. * @param uniformName Name of the variable.
  279. * @param array array to be set.
  280. */
  281. setArray3(uniformName, array) {
  282. this.setArray(uniformName, array);
  283. }
  284. /**
  285. * 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)
  286. * @param uniformName Name of the variable.
  287. * @param array array to be set.
  288. */
  289. setArray4(uniformName, array) {
  290. this.setArray(uniformName, array);
  291. }
  292. /**
  293. * Sets matrices on a uniform variable.
  294. * @param uniformName Name of the variable.
  295. * @param matrices matrices to be set.
  296. */
  297. setMatrices(uniformName, matrices) {
  298. if (!this.uniformBuffer || !this._leftOverUniformsByName[uniformName]) {
  299. return;
  300. }
  301. this.uniformBuffer.updateMatrices(uniformName, matrices);
  302. }
  303. /**
  304. * Sets matrix on a uniform variable.
  305. * @param uniformName Name of the variable.
  306. * @param matrix matrix to be set.
  307. */
  308. setMatrix(uniformName, matrix) {
  309. if (!this.uniformBuffer || !this._leftOverUniformsByName[uniformName]) {
  310. return;
  311. }
  312. this.uniformBuffer.updateMatrix(uniformName, matrix);
  313. }
  314. /**
  315. * 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)
  316. * @param uniformName Name of the variable.
  317. * @param matrix matrix to be set.
  318. */
  319. setMatrix3x3(uniformName, matrix) {
  320. if (!this.uniformBuffer || !this._leftOverUniformsByName[uniformName]) {
  321. return;
  322. }
  323. this.uniformBuffer.updateMatrix3x3(uniformName, matrix);
  324. }
  325. /**
  326. * Sets a 2x2 matrix on a uniform variable. (Specified as [1,2,3,4] will result in [1,2][3,4] matrix)
  327. * @param uniformName Name of the variable.
  328. * @param matrix matrix to be set.
  329. */
  330. setMatrix2x2(uniformName, matrix) {
  331. if (!this.uniformBuffer || !this._leftOverUniformsByName[uniformName]) {
  332. return;
  333. }
  334. this.uniformBuffer.updateMatrix2x2(uniformName, matrix);
  335. }
  336. /**
  337. * Sets a float on a uniform variable.
  338. * @param uniformName Name of the variable.
  339. * @param value value to be set.
  340. */
  341. setFloat(uniformName, value) {
  342. if (!this.uniformBuffer || !this._leftOverUniformsByName[uniformName]) {
  343. return;
  344. }
  345. this.uniformBuffer.updateFloat(uniformName, value);
  346. }
  347. /**
  348. * Sets a Vector2 on a uniform variable.
  349. * @param uniformName Name of the variable.
  350. * @param vector2 vector2 to be set.
  351. */
  352. setVector2(uniformName, vector2) {
  353. this.setFloat2(uniformName, vector2.x, vector2.y);
  354. }
  355. /**
  356. * Sets a float2 on a uniform variable.
  357. * @param uniformName Name of the variable.
  358. * @param x First float in float2.
  359. * @param y Second float in float2.
  360. */
  361. setFloat2(uniformName, x, y) {
  362. if (!this.uniformBuffer || !this._leftOverUniformsByName[uniformName]) {
  363. return;
  364. }
  365. this.uniformBuffer.updateFloat2(uniformName, x, y);
  366. }
  367. /**
  368. * Sets a Vector3 on a uniform variable.
  369. * @param uniformName Name of the variable.
  370. * @param vector3 Value to be set.
  371. */
  372. setVector3(uniformName, vector3) {
  373. this.setFloat3(uniformName, vector3.x, vector3.y, vector3.z);
  374. }
  375. /**
  376. * Sets a float3 on a uniform variable.
  377. * @param uniformName Name of the variable.
  378. * @param x First float in float3.
  379. * @param y Second float in float3.
  380. * @param z Third float in float3.
  381. */
  382. setFloat3(uniformName, x, y, z) {
  383. if (!this.uniformBuffer || !this._leftOverUniformsByName[uniformName]) {
  384. return;
  385. }
  386. this.uniformBuffer.updateFloat3(uniformName, x, y, z);
  387. }
  388. /**
  389. * Sets a Vector4 on a uniform variable.
  390. * @param uniformName Name of the variable.
  391. * @param vector4 Value to be set.
  392. */
  393. setVector4(uniformName, vector4) {
  394. this.setFloat4(uniformName, vector4.x, vector4.y, vector4.z, vector4.w);
  395. }
  396. /**
  397. * Sets a Quaternion on a uniform variable.
  398. * @param uniformName Name of the variable.
  399. * @param quaternion Value to be set.
  400. */
  401. setQuaternion(uniformName, quaternion) {
  402. this.setFloat4(uniformName, quaternion.x, quaternion.y, quaternion.z, quaternion.w);
  403. }
  404. /**
  405. * Sets a float4 on a uniform variable.
  406. * @param uniformName Name of the variable.
  407. * @param x First float in float4.
  408. * @param y Second float in float4.
  409. * @param z Third float in float4.
  410. * @param w Fourth float in float4.
  411. */
  412. setFloat4(uniformName, x, y, z, w) {
  413. if (!this.uniformBuffer || !this._leftOverUniformsByName[uniformName]) {
  414. return;
  415. }
  416. this.uniformBuffer.updateFloat4(uniformName, x, y, z, w);
  417. }
  418. /**
  419. * Sets a Color3 on a uniform variable.
  420. * @param uniformName Name of the variable.
  421. * @param color3 Value to be set.
  422. */
  423. setColor3(uniformName, color3) {
  424. this.setFloat3(uniformName, color3.r, color3.g, color3.b);
  425. }
  426. /**
  427. * Sets a Color4 on a uniform variable.
  428. * @param uniformName Name of the variable.
  429. * @param color3 Value to be set.
  430. * @param alpha Alpha value to be set.
  431. */
  432. setColor4(uniformName, color3, alpha) {
  433. this.setFloat4(uniformName, color3.r, color3.g, color3.b, alpha);
  434. }
  435. /**
  436. * Sets a Color4 on a uniform variable
  437. * @param uniformName defines the name of the variable
  438. * @param color4 defines the value to be set
  439. */
  440. setDirectColor4(uniformName, color4) {
  441. this.setFloat4(uniformName, color4.r, color4.g, color4.b, color4.a);
  442. }
  443. _getVertexShaderCode() {
  444. return this.sources?.vertex;
  445. }
  446. _getFragmentShaderCode() {
  447. return this.sources?.fragment;
  448. }
  449. }
  450. //# sourceMappingURL=webgpuPipelineContext.js.map