computeShader.d.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import type { UniformBuffer } from "../Materials/uniformBuffer";
  2. import type { Scene } from "../scene";
  3. import type { Nullable } from "../types";
  4. import type { ComputeEffect, IComputeShaderPath } from "./computeEffect";
  5. import type { ComputeBindingMapping } from "../Engines/Extensions/engine.computeShader";
  6. import type { BaseTexture } from "../Materials/Textures/baseTexture";
  7. import type { StorageBuffer } from "../Buffers/storageBuffer";
  8. import { TextureSampler } from "../Materials/Textures/textureSampler";
  9. import type { DataBuffer } from "../Buffers/dataBuffer.js";
  10. import type { ExternalTexture } from "../Materials/Textures/externalTexture.js";
  11. import type { VideoTexture } from "../Materials/Textures/videoTexture.js";
  12. import { WebGPUPerfCounter } from "../Engines/WebGPU/webgpuPerfCounter.js";
  13. import type { AbstractEngine } from "../Engines/abstractEngine.js";
  14. /**
  15. * Defines the options associated with the creation of a compute shader.
  16. */
  17. export interface IComputeShaderOptions {
  18. /**
  19. * list of bindings mapping (key is property name, value is binding location)
  20. * Must be provided because browsers don't support reflection for wgsl shaders yet (so there's no way to query the binding/group from a variable name)
  21. * TODO: remove this when browsers support reflection for wgsl shaders
  22. */
  23. bindingsMapping: ComputeBindingMapping;
  24. /**
  25. * The list of defines used in the shader
  26. */
  27. defines?: string[];
  28. /**
  29. * The name of the entry point in the shader source (default: "main")
  30. */
  31. entryPoint?: string;
  32. /**
  33. * If provided, will be called with the shader code so that this code can be updated before it is compiled by the GPU
  34. */
  35. processFinalCode?: Nullable<(code: string) => string>;
  36. }
  37. /**
  38. * The ComputeShader object lets you execute a compute shader on your GPU (if supported by the engine)
  39. */
  40. export declare class ComputeShader {
  41. private _engine;
  42. private _shaderPath;
  43. private _options;
  44. private _effect;
  45. private _cachedDefines;
  46. private _bindings;
  47. private _samplers;
  48. private _context;
  49. private _contextIsDirty;
  50. /**
  51. * Gets the unique id of the compute shader
  52. */
  53. readonly uniqueId: number;
  54. /**
  55. * The name of the shader
  56. */
  57. name: string;
  58. /**
  59. * The options used to create the shader
  60. */
  61. get options(): IComputeShaderOptions;
  62. /**
  63. * The shaderPath used to create the shader
  64. */
  65. get shaderPath(): string | IComputeShaderPath;
  66. /**
  67. * When set to true, dispatch won't call isReady anymore and won't check if the underlying GPU resources should be (re)created because of a change in the inputs (texture, uniform buffer, etc.)
  68. * If you know that your inputs did not change since last time dispatch was called and that isReady() returns true, set this flag to true to improve performance
  69. */
  70. fastMode: boolean;
  71. /**
  72. * Callback triggered when the shader is compiled
  73. */
  74. onCompiled: Nullable<(effect: ComputeEffect) => void>;
  75. /**
  76. * Callback triggered when an error occurs
  77. */
  78. onError: Nullable<(effect: ComputeEffect, errors: string) => void>;
  79. /**
  80. * Gets the GPU time spent running the compute shader for the last frame rendered (in nanoseconds).
  81. * You have to enable the "timestamp-query" extension in the engine constructor options and set engine.enableGPUTimingMeasurements = true.
  82. */
  83. readonly gpuTimeInFrame?: WebGPUPerfCounter;
  84. /**
  85. * Instantiates a new compute shader.
  86. * @param name Defines the name of the compute shader in the scene
  87. * @param engine Defines the engine the compute shader belongs to
  88. * @param shaderPath Defines the route to the shader code in one of three ways:
  89. * * object: \{ compute: "custom" \}, used with ShaderStore.ShadersStoreWGSL["customComputeShader"]
  90. * * object: \{ computeElement: "HTMLElementId" \}, used with shader code in script tags
  91. * * object: \{ computeSource: "compute shader code string" \}, where the string contains the shader code
  92. * * string: try first to find the code in ShaderStore.ShadersStoreWGSL[shaderPath + "ComputeShader"]. If not, assumes it is a file with name shaderPath.compute.fx in index.html folder.
  93. * @param options Define the options used to create the shader
  94. */
  95. constructor(name: string, engine: AbstractEngine, shaderPath: IComputeShaderPath | string, options?: Partial<IComputeShaderOptions>);
  96. /**
  97. * Gets the current class name of the material e.g. "ComputeShader"
  98. * Mainly use in serialization.
  99. * @returns the class name
  100. */
  101. getClassName(): string;
  102. /**
  103. * Binds a texture to the shader
  104. * @param name Binding name of the texture
  105. * @param texture Texture to bind
  106. * @param bindSampler Bind the sampler corresponding to the texture (default: true). The sampler will be bound just before the binding index of the texture
  107. */
  108. setTexture(name: string, texture: BaseTexture, bindSampler?: boolean): void;
  109. /**
  110. * Binds a storage texture to the shader
  111. * @param name Binding name of the texture
  112. * @param texture Texture to bind
  113. */
  114. setStorageTexture(name: string, texture: BaseTexture): void;
  115. /**
  116. * Binds an external texture to the shader
  117. * @param name Binding name of the texture
  118. * @param texture Texture to bind
  119. */
  120. setExternalTexture(name: string, texture: ExternalTexture): void;
  121. /**
  122. * Binds a video texture to the shader (by binding the external texture attached to this video)
  123. * @param name Binding name of the texture
  124. * @param texture Texture to bind
  125. * @returns true if the video texture was successfully bound, else false. false will be returned if the current engine does not support external textures
  126. */
  127. setVideoTexture(name: string, texture: VideoTexture): boolean;
  128. /**
  129. * Binds a uniform buffer to the shader
  130. * @param name Binding name of the buffer
  131. * @param buffer Buffer to bind
  132. */
  133. setUniformBuffer(name: string, buffer: UniformBuffer | DataBuffer): void;
  134. /**
  135. * Binds a storage buffer to the shader
  136. * @param name Binding name of the buffer
  137. * @param buffer Buffer to bind
  138. */
  139. setStorageBuffer(name: string, buffer: StorageBuffer | DataBuffer): void;
  140. /**
  141. * Binds a texture sampler to the shader
  142. * @param name Binding name of the sampler
  143. * @param sampler Sampler to bind
  144. */
  145. setTextureSampler(name: string, sampler: TextureSampler): void;
  146. /**
  147. * Specifies that the compute shader is ready to be executed (the compute effect and all the resources are ready)
  148. * @returns true if the compute shader is ready to be executed
  149. */
  150. isReady(): boolean;
  151. /**
  152. * Dispatches (executes) the compute shader
  153. * @param x Number of workgroups to execute on the X dimension
  154. * @param y Number of workgroups to execute on the Y dimension (default: 1)
  155. * @param z Number of workgroups to execute on the Z dimension (default: 1)
  156. * @returns True if the dispatch could be done, else false (meaning either the compute effect or at least one of the bound resources was not ready)
  157. */
  158. dispatch(x: number, y?: number, z?: number): boolean;
  159. /**
  160. * Dispatches (executes) the compute shader.
  161. * @param buffer Buffer containing the number of workgroups to execute on the X, Y and Z dimensions
  162. * @param offset Offset in the buffer where the workgroup counts are stored (default: 0)
  163. * @returns True if the dispatch could be done, else false (meaning either the compute effect or at least one of the bound resources was not ready)
  164. */
  165. dispatchIndirect(buffer: StorageBuffer | DataBuffer, offset?: number): boolean;
  166. private _checkContext;
  167. /**
  168. * Waits for the compute shader to be ready and executes it
  169. * @param x Number of workgroups to execute on the X dimension
  170. * @param y Number of workgroups to execute on the Y dimension (default: 1)
  171. * @param z Number of workgroups to execute on the Z dimension (default: 1)
  172. * @param delay Delay between the retries while the shader is not ready (in milliseconds - 10 by default)
  173. * @returns A promise that is resolved once the shader has been sent to the GPU. Note that it does not mean that the shader execution itself is finished!
  174. */
  175. dispatchWhenReady(x: number, y?: number, z?: number, delay?: number): Promise<void>;
  176. /**
  177. * Serializes this compute shader in a JSON representation
  178. * @returns the serialized compute shader object
  179. */
  180. serialize(): any;
  181. /**
  182. * Creates a compute shader from parsed compute shader data
  183. * @param source defines the JSON representation of the compute shader
  184. * @param scene defines the hosting scene
  185. * @param rootUrl defines the root URL to use to load textures and relative dependencies
  186. * @returns a new compute shader
  187. */
  188. static Parse(source: any, scene: Scene, rootUrl: string): ComputeShader;
  189. protected static _BufferIsDataBuffer(buffer: UniformBuffer | StorageBuffer | DataBuffer): buffer is DataBuffer;
  190. }