computeEffect.d.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import type { Nullable } from "../types";
  2. import { Observable } from "../Misc/observable";
  3. import type { IComputePipelineContext } from "./IComputePipelineContext";
  4. import type { AbstractEngine } from "../Engines/abstractEngine";
  5. /**
  6. * Defines the route to the shader code. The priority is as follows:
  7. * * object: `{ computeSource: "compute shader code string"}` for directly passing the shader code
  8. * * object: `{ computeElement: "vertexShaderCode" }`, used with shader code in script tags
  9. * * object: `{ compute: "custom" }`, used with `Effect.ShadersStore["customVertexShader"]` and `Effect.ShadersStore["customFragmentShader"]`
  10. * * string: `"./COMMON_NAME"`, used with external files COMMON_NAME.vertex.fx and COMMON_NAME.fragment.fx in index.html folder.
  11. */
  12. export type IComputeShaderPath = {
  13. /**
  14. * Directly pass the shader code
  15. */
  16. computeSource?: string;
  17. /**
  18. * Used with Effect.ShadersStore. If the `vertex` is set to `"custom`, then
  19. * Babylon.js will read from Effect.ShadersStore["customVertexShader"]
  20. */
  21. compute?: string;
  22. /**
  23. * Used with shader code in script tags
  24. */
  25. computeElement?: string;
  26. };
  27. /**
  28. * Options to be used when creating a compute effect.
  29. */
  30. export interface IComputeEffectCreationOptions {
  31. /**
  32. * Define statements that will be set in the shader.
  33. */
  34. defines: any;
  35. /**
  36. * The name of the entry point in the shader source (default: "main")
  37. */
  38. entryPoint?: string;
  39. /**
  40. * Callback that will be called when the shader is compiled.
  41. */
  42. onCompiled: Nullable<(effect: ComputeEffect) => void>;
  43. /**
  44. * Callback that will be called if an error occurs during shader compilation.
  45. */
  46. onError: Nullable<(effect: ComputeEffect, errors: string) => void>;
  47. /**
  48. * If provided, will be called with the shader code so that this code can be updated before it is compiled by the GPU
  49. */
  50. processFinalCode?: Nullable<(code: string) => string>;
  51. }
  52. /**
  53. * Effect wrapping a compute shader and let execute (dispatch) the shader
  54. */
  55. export declare class ComputeEffect {
  56. private static _UniqueIdSeed;
  57. /**
  58. * Enable logging of the shader code when a compilation error occurs
  59. */
  60. static LogShaderCodeOnCompilationError: boolean;
  61. /**
  62. * Name of the effect.
  63. */
  64. name: IComputeShaderPath | string;
  65. /**
  66. * String container all the define statements that should be set on the shader.
  67. */
  68. defines: string;
  69. /**
  70. * Callback that will be called when the shader is compiled.
  71. */
  72. onCompiled: Nullable<(effect: ComputeEffect) => void>;
  73. /**
  74. * Callback that will be called if an error occurs during shader compilation.
  75. */
  76. onError: Nullable<(effect: ComputeEffect, errors: string) => void>;
  77. /**
  78. * Unique ID of the effect.
  79. */
  80. uniqueId: number;
  81. /**
  82. * Observable that will be called when the shader is compiled.
  83. * It is recommended to use executeWhenCompile() or to make sure that scene.isReady() is called to get this observable raised.
  84. */
  85. onCompileObservable: Observable<ComputeEffect>;
  86. /**
  87. * Observable that will be called if an error occurs during shader compilation.
  88. */
  89. onErrorObservable: Observable<ComputeEffect>;
  90. /**
  91. * Observable that will be called when effect is bound.
  92. */
  93. onBindObservable: Observable<ComputeEffect>;
  94. /**
  95. * @internal
  96. * Specifies if the effect was previously ready
  97. */
  98. _wasPreviouslyReady: boolean;
  99. private _engine;
  100. private _isReady;
  101. private _compilationError;
  102. /** @internal */
  103. _key: string;
  104. private _computeSourceCodeOverride;
  105. /** @internal */
  106. _pipelineContext: Nullable<IComputePipelineContext>;
  107. /** @internal */
  108. _computeSourceCode: string;
  109. private _rawComputeSourceCode;
  110. private _entryPoint;
  111. private _shaderLanguage;
  112. private _shaderStore;
  113. private _shaderRepository;
  114. private _includeShaderStore;
  115. /**
  116. * Creates a compute effect that can be used to execute a compute shader
  117. * @param baseName Name of the effect
  118. * @param options Set of all options to create the effect
  119. * @param engine The engine the effect is created for
  120. * @param key Effect Key identifying uniquely compiled shader variants
  121. */
  122. constructor(baseName: IComputeShaderPath | string, options: IComputeEffectCreationOptions, engine: AbstractEngine, key?: string);
  123. private _useFinalCode;
  124. /**
  125. * Unique key for this effect
  126. */
  127. get key(): string;
  128. /**
  129. * If the effect has been compiled and prepared.
  130. * @returns if the effect is compiled and prepared.
  131. */
  132. isReady(): boolean;
  133. private _isReadyInternal;
  134. /**
  135. * The engine the effect was initialized with.
  136. * @returns the engine.
  137. */
  138. getEngine(): AbstractEngine;
  139. /**
  140. * The pipeline context for this effect
  141. * @returns the associated pipeline context
  142. */
  143. getPipelineContext(): Nullable<IComputePipelineContext>;
  144. /**
  145. * The error from the last compilation.
  146. * @returns the error string.
  147. */
  148. getCompilationError(): string;
  149. /**
  150. * Adds a callback to the onCompiled observable and call the callback immediately if already ready.
  151. * @param func The callback to be used.
  152. */
  153. executeWhenCompiled(func: (effect: ComputeEffect) => void): void;
  154. private _checkIsReady;
  155. private _loadShader;
  156. /**
  157. * Gets the compute shader source code of this effect
  158. */
  159. get computeSourceCode(): string;
  160. /**
  161. * Gets the compute shader source code before it has been processed by the preprocessor
  162. */
  163. get rawComputeSourceCode(): string;
  164. /**
  165. * Prepares the effect
  166. * @internal
  167. */
  168. _prepareEffect(): void;
  169. private _processCompilationErrors;
  170. /**
  171. * Release all associated resources.
  172. **/
  173. dispose(): void;
  174. /**
  175. * This function will add a new compute shader to the shader store
  176. * @param name the name of the shader
  177. * @param computeShader compute shader content
  178. */
  179. static RegisterShader(name: string, computeShader: string): void;
  180. }