webgpuPipelineContext.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. import { IPipelineContext } from '../IPipelineContext';
  2. import { Nullable } from '../../types';
  3. import { WebGPUEngine } from '../webgpuEngine';
  4. import { InternalTexture } from '../../Materials/Textures/internalTexture';
  5. import { Effect } from '../../Materials/effect';
  6. import { WebGPUShaderProcessingContext } from './webgpuShaderProcessingContext';
  7. import { UniformBuffer } from "../../Materials/uniformBuffer";
  8. import { IMatrixLike, IVector2Like, IVector3Like, IVector4Like, IColor3Like, IColor4Like } from '../../Maths/math.like';
  9. const _uniformSizes: { [type: string]: number } = {
  10. "float": 1,
  11. "vec2": 2,
  12. "vec3": 3,
  13. "vec4": 4,
  14. "mat2": 4,
  15. "mat3": 9,
  16. "mat4": 16
  17. };
  18. /** @hidden */
  19. export interface IWebGPUPipelineContextSamplerCache {
  20. setIndex: number;
  21. textureBinding: number;
  22. samplerBinding: number;
  23. texture: InternalTexture;
  24. }
  25. /** @hidden */
  26. export interface IWebGPUPipelineContextVertexInputsCache {
  27. indexBuffer: Nullable<GPUBuffer>;
  28. indexOffset: number;
  29. vertexStartSlot: number;
  30. vertexBuffers: GPUBuffer[];
  31. vertexOffsets: number[];
  32. }
  33. /** @hidden */
  34. export class WebGPUPipelineContext implements IPipelineContext {
  35. public engine: WebGPUEngine;
  36. public availableAttributes: { [key: string]: number };
  37. public availableUBOs: { [key: string]: { setIndex: number, bindingIndex: number} };
  38. public availableSamplers: { [key: string]: { setIndex: number, bindingIndex: number} };
  39. public orderedAttributes: string[];
  40. public orderedUBOsAndSamplers: { name: string, isSampler: boolean }[][];
  41. public leftOverUniforms: { name: string, type: string, length: number }[];
  42. public leftOverUniformsByName: { [name: string]: string };
  43. public sources: {
  44. vertex: string
  45. fragment: string,
  46. };
  47. public stages: Nullable<GPURenderPipelineStageDescriptor>;
  48. public samplers: { [name: string]: Nullable<IWebGPUPipelineContextSamplerCache> } = { };
  49. public vertexInputs: IWebGPUPipelineContextVertexInputsCache;
  50. public bindGroupLayouts: (GPUBindGroupLayout | undefined)[];
  51. public bindGroups: GPUBindGroup[];
  52. public renderPipeline: GPURenderPipeline;
  53. /**
  54. * Stores the uniform buffer
  55. */
  56. public uniformBuffer: Nullable<UniformBuffer>;
  57. // Default implementation.
  58. public onCompiled?: () => void;
  59. public get isAsync() {
  60. return false;
  61. }
  62. public get isReady(): boolean {
  63. if (this.stages) {
  64. return true;
  65. }
  66. return false;
  67. }
  68. constructor(shaderProcessingContext: WebGPUShaderProcessingContext, engine: WebGPUEngine) {
  69. if (shaderProcessingContext) {
  70. this.availableAttributes = shaderProcessingContext.availableAttributes;
  71. this.availableUBOs = shaderProcessingContext.availableUBOs;
  72. this.availableSamplers = shaderProcessingContext.availableSamplers;
  73. this.orderedAttributes = shaderProcessingContext.orderedAttributes;
  74. this.orderedUBOsAndSamplers = shaderProcessingContext.orderedUBOsAndSamplers;
  75. this.leftOverUniforms = shaderProcessingContext.leftOverUniforms;
  76. this.leftOverUniformsByName = {};
  77. }
  78. }
  79. public _handlesSpectorRebuildCallback(onCompiled: (program: any) => void): void {
  80. // Nothing to do yet for spector.
  81. }
  82. public _fillEffectInformation(effect: Effect, uniformBuffersNames: { [key: string]: number }, uniformsNames: string[], uniforms: { [key: string]: Nullable<WebGLUniformLocation> }, samplerList: string[], samplers: { [key: string]: number }, attributesNames: string[], attributes: number[]) {
  83. const engine = this.engine;
  84. // TODO WEBGPU. Cleanup SEB on this entire function. Should not need anything in here or almost.
  85. let effectAvailableUniforms = engine.getUniforms(this, uniformsNames);
  86. effectAvailableUniforms.forEach((uniform, index) => {
  87. uniforms[uniformsNames[index]] = uniform;
  88. });
  89. // Prevent Memory Leak by reducing the number of string, refer to the string instead of copy.
  90. effect._fragmentSourceCode = "";
  91. effect._vertexSourceCode = "";
  92. // this._fragmentSourceCodeOverride = "";
  93. // this._vertexSourceCodeOverride = "";
  94. const foundSamplers = this.availableSamplers;
  95. let index: number;
  96. for (index = 0; index < samplerList.length; index++) {
  97. const name = samplerList[index];
  98. const sampler = foundSamplers[samplerList[index]];
  99. if (sampler == null || sampler == undefined) {
  100. samplerList.splice(index, 1);
  101. index--;
  102. }
  103. else {
  104. samplers[name] = index;
  105. }
  106. }
  107. attributes.push(...engine.getAttributes(this, attributesNames));
  108. // Build the uniform layout for the left over uniforms.
  109. this.buildUniformLayout();
  110. }
  111. /**
  112. * Build the uniform buffer used in the material.
  113. */
  114. public buildUniformLayout(): void {
  115. if (!this.leftOverUniforms.length) {
  116. return;
  117. }
  118. this.uniformBuffer = new UniformBuffer(this.engine);
  119. for (let leftOverUniform of this.leftOverUniforms) {
  120. const size = _uniformSizes[leftOverUniform.type];
  121. this.uniformBuffer.addUniform(leftOverUniform.name, size, leftOverUniform.length);
  122. // TODO WEBGPU. Replace with info from uniform buffer class
  123. this.leftOverUniformsByName[leftOverUniform.name] = leftOverUniform.type;
  124. }
  125. this.uniformBuffer.create();
  126. }
  127. /**
  128. * Release all associated resources.
  129. **/
  130. public dispose() {
  131. if (this.uniformBuffer) {
  132. this.uniformBuffer.dispose();
  133. }
  134. // TODO WEBGPU. Dispose all resources.
  135. }
  136. /**
  137. * Sets an interger value on a uniform variable.
  138. * @param uniformName Name of the variable.
  139. * @param value Value to be set.
  140. */
  141. public setInt(uniformName: string, value: number): void {
  142. throw "setInt not Supported in LeftOver UBO.";
  143. }
  144. /**
  145. * Sets an int array on a uniform variable.
  146. * @param uniformName Name of the variable.
  147. * @param array array to be set.
  148. */
  149. public setIntArray(uniformName: string, array: Int32Array): void {
  150. throw "setIntArray not Supported in LeftOver UBO.";
  151. }
  152. /**
  153. * 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)
  154. * @param uniformName Name of the variable.
  155. * @param array array to be set.
  156. */
  157. public setIntArray2(uniformName: string, array: Int32Array): void {
  158. throw "setIntArray2 not Supported in LeftOver UBO.";
  159. }
  160. /**
  161. * 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)
  162. * @param uniformName Name of the variable.
  163. * @param array array to be set.
  164. */
  165. public setIntArray3(uniformName: string, array: Int32Array): void {
  166. throw "setIntArray3 not Supported in LeftOver UBO.";
  167. }
  168. /**
  169. * 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)
  170. * @param uniformName Name of the variable.
  171. * @param array array to be set.
  172. */
  173. public setIntArray4(uniformName: string, array: Int32Array): void {
  174. throw "setIntArray4 not Supported in LeftOver UBO.";
  175. }
  176. /**
  177. * Sets an float array on a uniform variable.
  178. * @param uniformName Name of the variable.
  179. * @param array array to be set.
  180. */
  181. public setFloatArray(uniformName: string, array: Float32Array): void {
  182. if (!this.uniformBuffer || !this.leftOverUniformsByName[uniformName]) {
  183. return;
  184. }
  185. this.uniformBuffer.updateFloatArray(uniformName, array);
  186. }
  187. /**
  188. * 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)
  189. * @param uniformName Name of the variable.
  190. * @param array array to be set.
  191. */
  192. public setFloatArray2(uniformName: string, array: Float32Array): void {
  193. throw "setFloatArray2 not Supported in LeftOver UBO.";
  194. }
  195. /**
  196. * 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)
  197. * @param uniformName Name of the variable.
  198. * @param array array to be set.
  199. */
  200. public setFloatArray3(uniformName: string, array: Float32Array): void {
  201. throw "setFloatArray3 not Supported in LeftOver UBO.";
  202. }
  203. /**
  204. * 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)
  205. * @param uniformName Name of the variable.
  206. * @param array array to be set.
  207. */
  208. public setFloatArray4(uniformName: string, array: Float32Array): void {
  209. throw "setFloatArray4 not Supported in LeftOver UBO.";
  210. }
  211. /**
  212. * Sets an array on a uniform variable.
  213. * @param uniformName Name of the variable.
  214. * @param array array to be set.
  215. */
  216. public setArray(uniformName: string, array: number[]): void {
  217. // TODO WEBGPU.
  218. throw "setArray not Supported in LeftOver UBO.";
  219. }
  220. /**
  221. * 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)
  222. * @param uniformName Name of the variable.
  223. * @param array array to be set.
  224. */
  225. public setArray2(uniformName: string, array: number[]): void {
  226. // TODO WEBGPU.
  227. throw "setArray2 not Supported in LeftOver UBO.";
  228. }
  229. /**
  230. * 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)
  231. * @param uniformName Name of the variable.
  232. * @param array array to be set.
  233. * @returns this effect.
  234. */
  235. public setArray3(uniformName: string, array: number[]): void {
  236. // TODO WEBGPU.
  237. throw "setArray3 not Supported in LeftOver UBO.";
  238. }
  239. /**
  240. * 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)
  241. * @param uniformName Name of the variable.
  242. * @param array array to be set.
  243. */
  244. public setArray4(uniformName: string, array: number[]): void {
  245. // TODO WEBGPU.
  246. throw "setArray4 not Supported in LeftOver UBO.";
  247. }
  248. /**
  249. * Sets matrices on a uniform variable.
  250. * @param uniformName Name of the variable.
  251. * @param matrices matrices to be set.
  252. */
  253. public setMatrices(uniformName: string, matrices: Float32Array): void {
  254. if (!this.uniformBuffer || !this.leftOverUniformsByName[uniformName]) {
  255. return;
  256. }
  257. this.uniformBuffer.updateMatrices(uniformName, matrices);
  258. }
  259. /**
  260. * Sets matrix on a uniform variable.
  261. * @param uniformName Name of the variable.
  262. * @param matrix matrix to be set.
  263. */
  264. public setMatrix(uniformName: string, matrix: IMatrixLike): void {
  265. if (!this.uniformBuffer || !this.leftOverUniformsByName[uniformName]) {
  266. return;
  267. }
  268. this.uniformBuffer.updateMatrix(uniformName, matrix);
  269. }
  270. /**
  271. * Sets a 3x3 matrix on a uniform variable. (Speicified as [1,2,3,4,5,6,7,8,9] will result in [1,2,3][4,5,6][7,8,9] matrix)
  272. * @param uniformName Name of the variable.
  273. * @param matrix matrix to be set.
  274. */
  275. public setMatrix3x3(uniformName: string, matrix: Float32Array): void {
  276. if (!this.uniformBuffer || !this.leftOverUniformsByName[uniformName]) {
  277. return;
  278. }
  279. this.uniformBuffer.updateMatrix3x3(uniformName, matrix);
  280. }
  281. /**
  282. * Sets a 2x2 matrix on a uniform variable. (Speicified as [1,2,3,4] will result in [1,2][3,4] matrix)
  283. * @param uniformName Name of the variable.
  284. * @param matrix matrix to be set.
  285. */
  286. public setMatrix2x2(uniformName: string, matrix: Float32Array): void {
  287. if (!this.uniformBuffer || !this.leftOverUniformsByName[uniformName]) {
  288. return;
  289. }
  290. this.uniformBuffer.updateMatrix2x2(uniformName, matrix);
  291. }
  292. /**
  293. * Sets a float on a uniform variable.
  294. * @param uniformName Name of the variable.
  295. * @param value value to be set.
  296. * @returns this effect.
  297. */
  298. public setFloat(uniformName: string, value: number): void {
  299. if (!this.uniformBuffer || !this.leftOverUniformsByName[uniformName]) {
  300. return;
  301. }
  302. this.uniformBuffer.updateFloat(uniformName, value);
  303. }
  304. /**
  305. * Sets a boolean on a uniform variable.
  306. * @param uniformName Name of the variable.
  307. * @param bool value to be set.
  308. */
  309. public setBool(uniformName: string, bool: boolean): void {
  310. // this.engine.setBool(this._uniforms[uniformName], bool ? 1 : 0);
  311. // TODO WEBGPU.
  312. throw "setBool not Supported in LeftOver UBO.";
  313. }
  314. /**
  315. * Sets a Vector2 on a uniform variable.
  316. * @param uniformName Name of the variable.
  317. * @param vector2 vector2 to be set.
  318. */
  319. public setVector2(uniformName: string, vector2: IVector2Like): void {
  320. this.setFloat2(uniformName, vector2.x, vector2.y);
  321. }
  322. /**
  323. * Sets a float2 on a uniform variable.
  324. * @param uniformName Name of the variable.
  325. * @param x First float in float2.
  326. * @param y Second float in float2.
  327. */
  328. public setFloat2(uniformName: string, x: number, y: number): void {
  329. if (!this.uniformBuffer || !this.leftOverUniformsByName[uniformName]) {
  330. return;
  331. }
  332. this.uniformBuffer.updateFloat2(uniformName, x, y);
  333. }
  334. /**
  335. * Sets a Vector3 on a uniform variable.
  336. * @param uniformName Name of the variable.
  337. * @param vector3 Value to be set.
  338. */
  339. public setVector3(uniformName: string, vector3: IVector3Like): void {
  340. this.setFloat3(uniformName, vector3.x, vector3.y, vector3.z);
  341. }
  342. /**
  343. * Sets a float3 on a uniform variable.
  344. * @param uniformName Name of the variable.
  345. * @param x First float in float3.
  346. * @param y Second float in float3.
  347. * @param z Third float in float3.
  348. */
  349. public setFloat3(uniformName: string, x: number, y: number, z: number): void {
  350. if (!this.uniformBuffer || !this.leftOverUniformsByName[uniformName]) {
  351. return;
  352. }
  353. this.uniformBuffer.updateFloat3(uniformName, x, y, z);
  354. }
  355. /**
  356. * Sets a Vector4 on a uniform variable.
  357. * @param uniformName Name of the variable.
  358. * @param vector4 Value to be set.
  359. */
  360. public setVector4(uniformName: string, vector4: IVector4Like): void {
  361. this.setFloat4(uniformName, vector4.x, vector4.y, vector4.z, vector4.w);
  362. }
  363. /**
  364. * Sets a float4 on a uniform variable.
  365. * @param uniformName Name of the variable.
  366. * @param x First float in float4.
  367. * @param y Second float in float4.
  368. * @param z Third float in float4.
  369. * @param w Fourth float in float4.
  370. * @returns this effect.
  371. */
  372. public setFloat4(uniformName: string, x: number, y: number, z: number, w: number): void {
  373. if (!this.uniformBuffer || !this.leftOverUniformsByName[uniformName]) {
  374. return;
  375. }
  376. this.uniformBuffer.updateFloat4(uniformName, x, y, z, w);
  377. }
  378. /**
  379. * Sets a Color3 on a uniform variable.
  380. * @param uniformName Name of the variable.
  381. * @param color3 Value to be set.
  382. */
  383. public setColor3(uniformName: string, color3: IColor3Like): void {
  384. this.setFloat3(uniformName, color3.b, color3.g, color3.b);
  385. }
  386. /**
  387. * Sets a Color4 on a uniform variable.
  388. * @param uniformName Name of the variable.
  389. * @param color3 Value to be set.
  390. * @param alpha Alpha value to be set.
  391. */
  392. public setColor4(uniformName: string, color3: IColor3Like, alpha: number): void {
  393. this.setFloat4(uniformName, color3.b, color3.g, color3.b, alpha);
  394. }
  395. /**
  396. * Sets a Color4 on a uniform variable
  397. * @param uniformName defines the name of the variable
  398. * @param color4 defines the value to be set
  399. */
  400. public setDirectColor4(uniformName: string, color4: IColor4Like): void {
  401. this.setFloat4(uniformName, color4.r, color4.g, color4.b, color4.a);
  402. }
  403. }