webgpuPipelineContext.ts 15 KB

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