webgpuPipelineContext.ts 16 KB

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