webgpuPipelineContext.ts 17 KB

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