webgpuPipelineContext.ts 15 KB

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