@onerjs/smart-filters 8.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/license.md +21 -0
  2. package/package.json +52 -0
  3. package/readme.md +9 -0
  4. package/src/IDisposable.ts +9 -0
  5. package/src/blockFoundation/aggregateBlock.ts +148 -0
  6. package/src/blockFoundation/baseBlock.ts +339 -0
  7. package/src/blockFoundation/customAggregateBlock.ts +88 -0
  8. package/src/blockFoundation/customShaderBlock.ts +362 -0
  9. package/src/blockFoundation/disableableShaderBlock.ts +91 -0
  10. package/src/blockFoundation/index.ts +9 -0
  11. package/src/blockFoundation/inputBlock.deserializer.ts +72 -0
  12. package/src/blockFoundation/inputBlock.serialization.types.ts +126 -0
  13. package/src/blockFoundation/inputBlock.serializer.ts +150 -0
  14. package/src/blockFoundation/inputBlock.ts +181 -0
  15. package/src/blockFoundation/outputBlock.ts +144 -0
  16. package/src/blockFoundation/shaderBlock.ts +156 -0
  17. package/src/blockFoundation/textureOptions.ts +57 -0
  18. package/src/command/command.ts +59 -0
  19. package/src/command/commandBuffer.ts +71 -0
  20. package/src/command/commandBufferDebugger.ts +14 -0
  21. package/src/command/index.ts +7 -0
  22. package/src/connection/connectionPoint.ts +205 -0
  23. package/src/connection/connectionPointCompatibilityState.ts +31 -0
  24. package/src/connection/connectionPointDirection.ts +9 -0
  25. package/src/connection/connectionPointType.ts +45 -0
  26. package/src/connection/connectionPointWithDefault.ts +27 -0
  27. package/src/connection/index.ts +8 -0
  28. package/src/editorUtils/editableInPropertyPage.ts +106 -0
  29. package/src/editorUtils/index.ts +3 -0
  30. package/src/index.ts +16 -0
  31. package/src/optimization/dependencyGraph.ts +96 -0
  32. package/src/optimization/index.ts +1 -0
  33. package/src/optimization/optimizedShaderBlock.ts +131 -0
  34. package/src/optimization/smartFilterOptimizer.ts +757 -0
  35. package/src/runtime/index.ts +8 -0
  36. package/src/runtime/renderTargetGenerator.ts +222 -0
  37. package/src/runtime/shaderRuntime.ts +174 -0
  38. package/src/runtime/smartFilterRuntime.ts +112 -0
  39. package/src/runtime/strongRef.ts +18 -0
  40. package/src/serialization/importCustomBlockDefinition.ts +86 -0
  41. package/src/serialization/index.ts +10 -0
  42. package/src/serialization/serializedBlockDefinition.ts +12 -0
  43. package/src/serialization/serializedShaderBlockDefinition.ts +7 -0
  44. package/src/serialization/serializedSmartFilter.ts +6 -0
  45. package/src/serialization/smartFilterDeserializer.ts +190 -0
  46. package/src/serialization/smartFilterSerializer.ts +110 -0
  47. package/src/serialization/v1/defaultBlockSerializer.ts +21 -0
  48. package/src/serialization/v1/index.ts +4 -0
  49. package/src/serialization/v1/shaderBlockSerialization.types.ts +85 -0
  50. package/src/serialization/v1/smartFilterSerialization.types.ts +129 -0
  51. package/src/smartFilter.ts +255 -0
  52. package/src/utils/buildTools/buildShaders.ts +14 -0
  53. package/src/utils/buildTools/convertGlslIntoBlock.ts +370 -0
  54. package/src/utils/buildTools/convertGlslIntoShaderProgram.ts +173 -0
  55. package/src/utils/buildTools/convertShaders.ts +65 -0
  56. package/src/utils/buildTools/recordVersionNumber.js +24 -0
  57. package/src/utils/buildTools/shaderCode.types.ts +59 -0
  58. package/src/utils/buildTools/shaderConverter.ts +466 -0
  59. package/src/utils/buildTools/watchShaders.ts +44 -0
  60. package/src/utils/index.ts +4 -0
  61. package/src/utils/renderTargetUtils.ts +30 -0
  62. package/src/utils/shaderCodeUtils.ts +192 -0
  63. package/src/utils/textureLoaders.ts +31 -0
  64. package/src/utils/textureUtils.ts +28 -0
  65. package/src/utils/uniqueIdGenerator.ts +28 -0
  66. package/src/version.ts +4 -0
@@ -0,0 +1,255 @@
1
+ import type { ThinEngine } from "core/Engines/thinEngine.js";
2
+ import type { Nullable } from "core/types.js";
3
+ import type { SmartFilterRuntime } from "./runtime/smartFilterRuntime.js";
4
+ import type { BaseBlock } from "./blockFoundation/baseBlock.js";
5
+ import type { ConnectionPointType } from "./connection/connectionPointType.js";
6
+ import type { ConnectionPoint } from "./connection/connectionPoint.js";
7
+ import { OutputBlock } from "./blockFoundation/outputBlock.js";
8
+ import { InternalSmartFilterRuntime } from "./runtime/smartFilterRuntime.js";
9
+ import { RenderTargetGenerator } from "./runtime/renderTargetGenerator.js";
10
+ import { AggregateBlock } from "./blockFoundation/aggregateBlock.js";
11
+ import type { IEditorData } from "shared-ui-components/nodeGraphSystem/interfaces/nodeLocationInfo.js";
12
+ import type { IDisposable } from "./IDisposable.js";
13
+ import { ShaderBlock } from "./blockFoundation/shaderBlock.js";
14
+ import type { ThinRenderTargetTexture } from "core/Materials/Textures/thinRenderTargetTexture.js";
15
+ import { GetBlockOutputTextureSize } from "./utils/textureUtils.js";
16
+
17
+ /**
18
+ * How long to wait for shader compilation and texture loading to complete before erroring out.
19
+ */
20
+ const InitializationTimeout = 10000;
21
+
22
+ /**
23
+ * Data passed to the initialize function of the blocks.
24
+ */
25
+ export type InitializationData = {
26
+ /**
27
+ * The current smart filter runtime the block is being initialized for.
28
+ */
29
+ readonly runtime: InternalSmartFilterRuntime;
30
+
31
+ /**
32
+ * The output block of the smart filter.
33
+ * This is used to determine if a block is linked to the output block so that we can prevent an
34
+ * extra render pass.
35
+ */
36
+ readonly outputBlock: OutputBlock;
37
+
38
+ /**
39
+ * The list of promises to wait for during the initialization step.
40
+ */
41
+ readonly initializationPromises: Promise<void>[];
42
+
43
+ /**
44
+ * Resources that need to be disposed when the runtime is disposed.
45
+ */
46
+ readonly disposableResources: IDisposable[];
47
+ };
48
+
49
+ /**
50
+ * The smart filter class is the main class of the smart filter module.
51
+ *
52
+ * It is responsible for managing a graph of smart filter blocks.
53
+ *
54
+ * It is also responsible for creating the runtime associated to the current state of the filter.
55
+ */
56
+ export class SmartFilter {
57
+ /**
58
+ * The friendly name of the smart filter.
59
+ */
60
+ public readonly name: string;
61
+
62
+ /**
63
+ * The namespace of the smart filter.
64
+ */
65
+ public readonly namespace: Nullable<string>;
66
+
67
+ /**
68
+ * The smart filter output (input connection point of the output block...).
69
+ *
70
+ * This is where the smart filter final block should be connected to in order to be visible on screen.
71
+ */
72
+ public readonly output: ConnectionPoint<ConnectionPointType.Texture>;
73
+
74
+ /**
75
+ * The output block of the smart filter.
76
+ */
77
+ public readonly outputBlock: OutputBlock;
78
+
79
+ /**
80
+ * User defined comments to describe the current smart filter.
81
+ */
82
+ public comments: Nullable<string> = null;
83
+
84
+ /**
85
+ * Data used by the smart filter editor.
86
+ */
87
+ public editorData: Nullable<IEditorData> = null;
88
+
89
+ private readonly _attachedBlocks: Array<BaseBlock>;
90
+ /**
91
+ * Creates a new instance of a @see SmartFilter.
92
+ * @param name - The friendly name of the smart filter
93
+ * @param namespace - The namespace of the smart filter
94
+ */
95
+ constructor(name: string, namespace: Nullable<string> = null) {
96
+ this.name = name;
97
+ this.namespace = namespace;
98
+
99
+ this._attachedBlocks = new Array<BaseBlock>();
100
+ this.outputBlock = new OutputBlock(this);
101
+ this.output = this.outputBlock.input;
102
+ }
103
+
104
+ /**
105
+ * @returns the list of blocks attached to the smart filter.
106
+ */
107
+ public get attachedBlocks(): ReadonlyArray<BaseBlock> {
108
+ return this._attachedBlocks;
109
+ }
110
+
111
+ /**
112
+ * @returns The current class name of the smart filter.
113
+ */
114
+ public getClassName(): string {
115
+ return "SmartFilter";
116
+ }
117
+
118
+ /**
119
+ * Registers a block to be part of this smart filter.
120
+ * @param block - The block to register on the smart filter
121
+ * @throws if the block is already registered on another smart filter
122
+ * @remarks This function will not register the block if it is already registered on the smart filter.
123
+ */
124
+ public registerBlock(block: BaseBlock): void {
125
+ // It is impossible to attach a block from another filter
126
+ if (block.smartFilter !== this) {
127
+ throw new Error("Block is not part of this smart filter");
128
+ }
129
+
130
+ // No need to attach a block several times
131
+ if (this._attachedBlocks.indexOf(block) !== -1) {
132
+ return;
133
+ }
134
+
135
+ // Add the block to the list of attached blocks
136
+ this._attachedBlocks.push(block);
137
+ }
138
+
139
+ /**
140
+ * Removes the block from the smart filter.
141
+ * @param block - The block to remove from the smart filter
142
+ * @remarks This function will disconnect the block on removal.
143
+ * This Output block cannot be removed.
144
+ */
145
+ public removeBlock(block: BaseBlock): void {
146
+ const attachedBlockIndex = this._attachedBlocks.indexOf(block);
147
+
148
+ // The block can only be removed if it is not the output block
149
+ // and if it is attached to the smart filter
150
+ if (attachedBlockIndex > -1 && !block.isOutput) {
151
+ // Disconnects all the connections of the block
152
+ block.disconnect();
153
+
154
+ // Removes the block from the list of attached blocks
155
+ this._attachedBlocks.splice(attachedBlockIndex, 1);
156
+ }
157
+ }
158
+
159
+ private _generateCommandsAndGatherInitPromises(initializationData: InitializationData): void {
160
+ const outputBlock = this.outputBlock;
161
+
162
+ outputBlock.visit(initializationData, (block: BaseBlock, initializationData: InitializationData) => {
163
+ block.generateCommandsAndGatherInitPromises(initializationData, outputBlock.input.connectedTo?.ownerBlock === block);
164
+ });
165
+ }
166
+
167
+ /**
168
+ * Create a new runtime for the current state of the smart filter.
169
+ * @param engine - The Babylon.js engine to use for the runtime
170
+ * @param renderTargetGenerator - The render target generator to use to generate the RTTs for the shader blocks. If not provided, a default one will be created.
171
+ * @returns the runtime that can be used to render the smart filter
172
+ */
173
+ public async createRuntimeAsync(engine: ThinEngine, renderTargetGenerator?: RenderTargetGenerator): Promise<SmartFilterRuntime> {
174
+ const runtime = new InternalSmartFilterRuntime(engine);
175
+
176
+ const initializationData: InitializationData = {
177
+ runtime,
178
+ outputBlock: this.outputBlock,
179
+ initializationPromises: [],
180
+ disposableResources: [],
181
+ };
182
+
183
+ this._workWithAggregateFreeGraph(() => {
184
+ this.outputBlock.prepareForRuntime();
185
+
186
+ renderTargetGenerator = renderTargetGenerator ?? new RenderTargetGenerator(false);
187
+ renderTargetGenerator.setOutputTextures(this, initializationData);
188
+
189
+ this.outputBlock.propagateRuntimeData();
190
+
191
+ this._generateCommandsAndGatherInitPromises(initializationData);
192
+ });
193
+
194
+ // Wait for all the blocks to be initialized
195
+ if (initializationData.initializationPromises.length > 0) {
196
+ const timeoutPromise = new Promise((resolve) => setTimeout(resolve, InitializationTimeout, true));
197
+ // eslint-disable-next-line github/no-then
198
+ const initializationPromises = Promise.all(initializationData.initializationPromises).then(() => false);
199
+ const timedOut = await Promise.race([initializationPromises, timeoutPromise]);
200
+ if (timedOut) {
201
+ throw new Error("Initialization promises timed out");
202
+ }
203
+ }
204
+
205
+ // Register the resources to dispose when the runtime is disposed
206
+ initializationData.disposableResources.forEach((resource) => runtime.registerResource(resource));
207
+
208
+ return runtime;
209
+ }
210
+
211
+ /**
212
+ * Resizes any intermediate textures according to the new size of the render target
213
+ * @param engine - The engine used to render the smart filter
214
+ */
215
+ public resize(engine: ThinEngine): void {
216
+ this._workWithAggregateFreeGraph(() => {
217
+ this.outputBlock.visit({}, (block: BaseBlock) => {
218
+ if (!(block instanceof ShaderBlock)) {
219
+ return;
220
+ }
221
+
222
+ if (block.output.runtimeData?.value) {
223
+ const size = GetBlockOutputTextureSize(this, engine, block.outputTextureOptions);
224
+ (block.output.runtimeData.value as ThinRenderTargetTexture).resize(size);
225
+ }
226
+ });
227
+ });
228
+ }
229
+
230
+ /**
231
+ * @internal
232
+ * Merges all aggregate blocks into the smart filter graph, executes the passed-in work, then restores the aggregate blocks.
233
+ * @param work - The work to execute with the aggregate blocks merged
234
+ */
235
+ public _workWithAggregateFreeGraph(work: () => void): void {
236
+ const mergedAggregateBlocks: AggregateBlock[] = [];
237
+
238
+ // Merge all aggregate blocks
239
+ this.outputBlock.visit({}, (block: BaseBlock, _extraData: object) => {
240
+ if (block instanceof AggregateBlock) {
241
+ block._mergeIntoSmartFilter(mergedAggregateBlocks);
242
+ }
243
+ });
244
+
245
+ try {
246
+ // Do the passed in work
247
+ work();
248
+ } finally {
249
+ // Restore all aggregate blocks, even if work throws
250
+ for (const block of mergedAggregateBlocks) {
251
+ block._unmergeFromSmartFilter();
252
+ }
253
+ }
254
+ }
255
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Builds all .glsl files under <shaderPath>.
3
+ * @param shaderPath - The path to the shaders to watch
4
+ * @param smartFiltersCorePath - The path to import the Smart Filters core from
5
+ * @param babylonCorePath - The path to import the Babylon core from (optional)
6
+ * @example node buildShaders.js <shaderPath> @babylonjs/smart-filters
7
+ */
8
+
9
+ import { ConvertShaders } from "./convertShaders.js";
10
+
11
+ const ExternalArguments = process.argv.slice(2);
12
+ if (ExternalArguments.length >= 2 && ExternalArguments[0] && ExternalArguments[1]) {
13
+ ConvertShaders(ExternalArguments[0], ExternalArguments[1], ExternalArguments[2]);
14
+ }
@@ -0,0 +1,370 @@
1
+ import * as fs from "fs";
2
+ import { ExtractShaderProgramFromGlsl } from "./convertGlslIntoShaderProgram.js";
3
+ import { ConnectionPointType } from "../../connection/connectionPointType.js";
4
+ import { BlockDisableStrategy } from "../../blockFoundation/disableableShaderBlock.js";
5
+
6
+ // eslint-disable-next-line @typescript-eslint/naming-convention
7
+ const EXTRA_IMPORTS = "@EXTRA_IMPORTS@";
8
+ // eslint-disable-next-line @typescript-eslint/naming-convention
9
+ const BABYLON_CORE_PATH = "@BABYLON_CORE_PATH@";
10
+ // eslint-disable-next-line @typescript-eslint/naming-convention
11
+ const SMART_FILTER_CORE_IMPORT = "@SMART_FILTER_CORE_IMPORT@";
12
+ const SHADER_PROGRAM = "@SHADER_PROGRAM@";
13
+ const BLOCK_NAME = "@BLOCK_NAME@";
14
+ const NAMESPACE = "@NAMESPACE@";
15
+ // eslint-disable-next-line @typescript-eslint/naming-convention
16
+ const SHADER_BINDING_PRIVATE_VARIABLES = "@SHADER_BINDING_PRIVATE_VARIABLES@";
17
+ const CAMEL_CASE_UNIFORM = "@CAMEL_CASE_UNIFORM@";
18
+ const CONNECTION_POINT_TYPE = "@CONNECTION_POINT_TYPE@";
19
+ const CONNECTION_POINT_DEFAULT_VALUE = "@CONNECTION_POINT_DEFAULT_VALUE@";
20
+ const SHADER_BINDING_EXTENDS = "@SHADER_BINDING_EXTENDS@";
21
+ const SHADER_BINDING_CTOR_DOCSTRING_PARAMS = "@SHADER_BINDING_CTOR_DOCSTRING_PARAMS@";
22
+ const SHADER_BINDING_CTOR_PARAMS = "@SHADER_CTOR_PARAMS@";
23
+ const SHADER_BINDING_SUPER_PARAMS = "@SHADER_BINDING_SUPER_PARAMS@";
24
+ const SHADER_BINDING_CTOR = "@SHADER_BINDING_CTOR@";
25
+ const SHADER_BINDING_BIND = "@SHADER_BINDING_BIND@";
26
+ const SHADER_BLOCK_EXTENDS = "@SHADER_BLOCK_EXTENDS@";
27
+ // eslint-disable-next-line @typescript-eslint/naming-convention
28
+ const BLOCK_INPUT_PROPERTIES = "@BLOCK_INPUT_PROPERTIES@";
29
+ // eslint-disable-next-line @typescript-eslint/naming-convention
30
+ const BLOCK_DISABLE_OPTIMIZATION = "@BLOCK_DISABLE_OPTIMIZATION@";
31
+ const BLOCK_DISABLE_STRATEGY = "@BLOCK_DISABLE_STRATEGY@";
32
+ const BLOCK_GET_SHADER_BINDING_VARS = "@BLOCK_SHADER_BINDING_BIND_VARS@";
33
+ const BLOCK_GET_SHADER_PARAM_LIST = "@BLOCK_GET_SHADER_PARAM_LIST@";
34
+ const EFFECT_SETTER = "@EFFECT_SETTER@";
35
+ const EFFECT_VALUE = "@EFFECT_VALUE@";
36
+ const EXTRA_BIND_DOCSTRING = "@EXTRA_BIND_DOCSTRING@";
37
+ const EXTRA_BIND_PARAMS = "@EXTRA_BIND_PARAMS@";
38
+
39
+ const ShaderBindingPrivateVariablesTemplate = ` private readonly _${CAMEL_CASE_UNIFORM}: RuntimeData<ConnectionPointType.${CONNECTION_POINT_TYPE}>;`;
40
+ const ShaderBindingCtorDocstringParams = ` * @param ${CAMEL_CASE_UNIFORM} - The ${CAMEL_CASE_UNIFORM} runtime value`;
41
+ const ShaderBindingCtorParams = ` ${CAMEL_CASE_UNIFORM}: RuntimeData<ConnectionPointType.${CONNECTION_POINT_TYPE}>`;
42
+ const ShaderBindingCtor = ` this._${CAMEL_CASE_UNIFORM} = ${CAMEL_CASE_UNIFORM};`;
43
+ const ShaderBindingBind = ` effect.${EFFECT_SETTER}(this.getRemappedName(Uniforms.${CAMEL_CASE_UNIFORM}), ${EFFECT_VALUE});`;
44
+ const ShaderBindingBindRegularValue = `this._${CAMEL_CASE_UNIFORM}.value`;
45
+
46
+ const BlockInputProperty = ` /**
47
+ * The ${CAMEL_CASE_UNIFORM} connection point.
48
+ */
49
+ public readonly ${CAMEL_CASE_UNIFORM} = this._registerInput(Uniforms.${CAMEL_CASE_UNIFORM}, ConnectionPointType.${CONNECTION_POINT_TYPE});
50
+ `;
51
+ const BlockInputOptionalProperty = ` /**
52
+ /**
53
+ * The ${CAMEL_CASE_UNIFORM} connection point.
54
+ */
55
+ public readonly ${CAMEL_CASE_UNIFORM} = this._registerOptionalInput(
56
+ "${CAMEL_CASE_UNIFORM}",
57
+ ConnectionPointType.${CONNECTION_POINT_TYPE},
58
+ createStrongRef(${CONNECTION_POINT_DEFAULT_VALUE})
59
+ );
60
+ `;
61
+ const BlockGetShaderBindingVars = ` const ${CAMEL_CASE_UNIFORM} = this._confirmRuntimeDataSupplied(this.${CAMEL_CASE_UNIFORM});`;
62
+
63
+ const FileTemplate = `/* eslint-disable prettier/prettier */
64
+ // ************************************************************
65
+ // Note: this file is auto-generated, do not modify it directly
66
+ // ************************************************************
67
+
68
+ // It was generated by convertGlslIntoBlock() from
69
+ // an annotated .glsl file. Modify the .glsl file to make changes
70
+ // to the block. This file will get overwritten when the build
71
+ // is run or during a watch when the .glsl file is updated.
72
+
73
+ import type { Effect } from "${BABYLON_CORE_PATH}/Materials/effect.js";
74
+
75
+ import {
76
+ ${SHADER_BINDING_EXTENDS},
77
+ type RuntimeData,
78
+ ConnectionPointType,
79
+ type SmartFilter,
80
+ ${SHADER_BLOCK_EXTENDS},
81
+ type ShaderProgram,
82
+ ${EXTRA_IMPORTS}} from "${SMART_FILTER_CORE_IMPORT}";${SHADER_PROGRAM}
83
+ /**
84
+ * The shader binding for the ${BLOCK_NAME}, used by the runtime
85
+ */
86
+ class ${BLOCK_NAME}ShaderBinding extends ${SHADER_BINDING_EXTENDS} {
87
+ ${SHADER_BINDING_PRIVATE_VARIABLES}
88
+
89
+ /**
90
+ * Creates a new shader binding instance for the block.
91
+ ${SHADER_BINDING_CTOR_DOCSTRING_PARAMS}
92
+ */
93
+ constructor(
94
+ ${SHADER_BINDING_CTOR_PARAMS}
95
+ ) {
96
+ super(${SHADER_BINDING_SUPER_PARAMS});
97
+ ${SHADER_BINDING_CTOR}
98
+ }
99
+
100
+ /**
101
+ * Binds all the required data to the shader when rendering.
102
+ * @param effect - defines the effect to bind the data to${EXTRA_BIND_DOCSTRING}
103
+ */
104
+ public override bind(effect: Effect${EXTRA_BIND_PARAMS}): void {
105
+ ${SHADER_BINDING_BIND}
106
+ }
107
+ }
108
+
109
+ /**
110
+ * The implementation of the ${BLOCK_NAME}
111
+ */
112
+ export class ${BLOCK_NAME} extends ${SHADER_BLOCK_EXTENDS} {
113
+ /**
114
+ * The class name of the block.
115
+ */
116
+ public static override ClassName = "${BLOCK_NAME}";
117
+
118
+ /**
119
+ * The namespace of the block.
120
+ */
121
+ public static override Namespace = "${NAMESPACE}";
122
+
123
+ ${BLOCK_INPUT_PROPERTIES}
124
+ /**
125
+ * The shader program (vertex and fragment code) to use to render the block
126
+ */
127
+ public static override ShaderCode = BlockShaderProgram;
128
+
129
+ /**
130
+ * Instantiates a new ${BLOCK_NAME}.
131
+ * @param smartFilter - The smart filter this block belongs to
132
+ * @param name - The friendly name of the block
133
+ */
134
+ constructor(smartFilter: SmartFilter, name: string) {
135
+ super(smartFilter, name, ${BLOCK_DISABLE_OPTIMIZATION}${BLOCK_DISABLE_STRATEGY});
136
+ }
137
+
138
+ /**
139
+ * Get the class instance that binds all the required data to the shader (effect) when rendering.
140
+ * @returns The class instance that binds the data to the effect
141
+ */
142
+ public getShaderBinding(): ${SHADER_BINDING_EXTENDS} {
143
+ ${BLOCK_GET_SHADER_BINDING_VARS}
144
+
145
+ return new ${BLOCK_NAME}ShaderBinding(${BLOCK_GET_SHADER_PARAM_LIST});
146
+ }
147
+ }
148
+
149
+ `;
150
+
151
+ /**
152
+ * Converts a single shader to a .ts file which exports a Smart Filter block
153
+ * @param fragmentShaderPath - The path to the fragment file for the shader
154
+ * @param smartFiltersCorePath - The path to import the Smart Filters core from
155
+ * @param babylonCorePath - The path to import the Babylon core from (optional)
156
+
157
+ */
158
+ export function ConvertGlslIntoBlock(fragmentShaderPath: string, smartFiltersCorePath: string, babylonCorePath?: string): void {
159
+ const extraImports: string[] = [];
160
+
161
+ const { shaderProgramCode, fragmentShaderInfo } = ExtractShaderProgramFromGlsl(fragmentShaderPath, smartFiltersCorePath, false, false);
162
+
163
+ // Validation
164
+ if (!fragmentShaderInfo.blockType) {
165
+ throw new Error("The glsl file must contain a header comment with a smartFilterBlockType value");
166
+ }
167
+
168
+ // Generate shader binding private variables
169
+ const shaderBindingPrivateVariables = fragmentShaderInfo.uniforms
170
+ .map((uniform) => {
171
+ return uniform.properties?.autoBind
172
+ ? null
173
+ : ShaderBindingPrivateVariablesTemplate.replace(CAMEL_CASE_UNIFORM, uniform.name).replace(CONNECTION_POINT_TYPE, GetConnectionPointTypeString(uniform.type));
174
+ })
175
+ .filter((line) => line !== null);
176
+
177
+ // Generate the shader binding constructor docstring params
178
+ const shaderBindingCtorDocstringParams = fragmentShaderInfo.uniforms
179
+ .map((uniform) => {
180
+ return uniform.properties?.autoBind ? null : ShaderBindingCtorDocstringParams.replace(new RegExp(CAMEL_CASE_UNIFORM, "g"), uniform.name);
181
+ })
182
+ .filter((param) => param !== null);
183
+
184
+ // Generate the shader binding constructor params
185
+ const shaderBindingCtorParams = fragmentShaderInfo.uniforms
186
+ .map((uniform) => {
187
+ return uniform.properties?.autoBind
188
+ ? null
189
+ : ShaderBindingCtorParams.replace(CAMEL_CASE_UNIFORM, uniform.name).replace(CONNECTION_POINT_TYPE, GetConnectionPointTypeString(uniform.type));
190
+ })
191
+ .filter((param) => param !== null);
192
+
193
+ // Generate the shader binding constructor
194
+ const shaderBindingCtor = fragmentShaderInfo.uniforms
195
+ .map((uniform) => {
196
+ return uniform.properties?.autoBind ? null : ShaderBindingCtor.replace(new RegExp(CAMEL_CASE_UNIFORM, "g"), uniform.name);
197
+ })
198
+ .filter((line) => line !== null);
199
+
200
+ // Generate the shader binding bind
201
+ let needWidthHeightParams = false;
202
+ const shaderBindingBind = fragmentShaderInfo.uniforms.map((uniform) => {
203
+ let effectValue: string;
204
+ let effectSetter = GetEffectSetter(uniform.type);
205
+
206
+ switch (uniform.properties?.autoBind) {
207
+ case "outputResolution":
208
+ {
209
+ effectValue = "width, height";
210
+ effectSetter = "setFloat2";
211
+ needWidthHeightParams = true;
212
+ }
213
+ break;
214
+ case "outputAspectRatio":
215
+ {
216
+ effectValue = "width / height, height / width";
217
+ effectSetter = "setFloat2";
218
+ needWidthHeightParams = true;
219
+ }
220
+ break;
221
+ default:
222
+ effectValue = ShaderBindingBindRegularValue;
223
+ }
224
+ return ShaderBindingBind.replace(EFFECT_VALUE, effectValue).replace(new RegExp(CAMEL_CASE_UNIFORM, "g"), uniform.name).replace(EFFECT_SETTER, effectSetter);
225
+ });
226
+
227
+ // Add extra params to the bind method if needed
228
+ let extraBindParams = "";
229
+ let extraBindDocstring = "";
230
+ if (needWidthHeightParams) {
231
+ extraBindDocstring = `
232
+ * @param width - defines the width of the output
233
+ * @param height - defines the height of the output`;
234
+ extraBindParams = `, width: number, height: number`;
235
+ }
236
+
237
+ // Generate the block input properties
238
+ const blockInputProperties = fragmentShaderInfo.uniforms
239
+ .map((uniform) => {
240
+ if (uniform.properties?.autoBind) {
241
+ return null;
242
+ } else if (uniform.properties?.default !== undefined) {
243
+ if (extraImports.indexOf(" createStrongRef") === -1) {
244
+ extraImports.push(" createStrongRef");
245
+ }
246
+ return BlockInputOptionalProperty.replace(new RegExp(CAMEL_CASE_UNIFORM, "g"), uniform.name)
247
+ .replace(CONNECTION_POINT_TYPE, GetConnectionPointTypeString(uniform.type))
248
+ .replace(CONNECTION_POINT_DEFAULT_VALUE, uniform.properties.default);
249
+ } else {
250
+ return BlockInputProperty.replace(new RegExp(CAMEL_CASE_UNIFORM, "g"), uniform.name).replace(CONNECTION_POINT_TYPE, GetConnectionPointTypeString(uniform.type));
251
+ }
252
+ })
253
+ .filter((property) => property !== null);
254
+
255
+ // Generate the block get shader binding vars
256
+ const blockGetShaderBindingVars = fragmentShaderInfo.uniforms
257
+ .map((uniform) => {
258
+ return uniform.properties?.autoBind ? null : BlockGetShaderBindingVars.replace(new RegExp(CAMEL_CASE_UNIFORM, "g"), uniform.name);
259
+ })
260
+ .filter((line) => line !== null);
261
+
262
+ // Handle the disable optimization flag
263
+ const disableOptimization = fragmentShaderInfo.disableOptimization === true ? "true" : "false";
264
+
265
+ // Generate the block get shader param list
266
+ const blockGetShaderParamList = fragmentShaderInfo.uniforms
267
+ .map((uniform) => {
268
+ return uniform.properties?.autoBind ? null : uniform.name;
269
+ })
270
+ .filter((param) => param !== null);
271
+
272
+ // Decide if this is a disableable block or not
273
+ let shaderBlockExtends = "ShaderBlock";
274
+ let shaderBindingExtends = "ShaderBinding";
275
+ let blockDisableStrategy = "";
276
+ let shaderBindingSuperParams = "";
277
+ if (fragmentShaderInfo.blockDisableStrategy) {
278
+ shaderBlockExtends = "DisableableShaderBlock";
279
+ shaderBindingExtends = "DisableableShaderBinding";
280
+ blockDisableStrategy = `, BlockDisableStrategy.${BlockDisableStrategy[fragmentShaderInfo.blockDisableStrategy]}`;
281
+ shaderBindingSuperParams = "parentBlock";
282
+
283
+ shaderBindingCtorDocstringParams.unshift(" * @param parentBlock - IDisableableBlock");
284
+ shaderBindingCtorParams.unshift(" parentBlock: IDisableableBlock");
285
+ blockGetShaderParamList.unshift("this");
286
+
287
+ extraImports.push(" type IDisableableBlock");
288
+ extraImports.push(" BlockDisableStrategy");
289
+
290
+ shaderBindingBind.unshift(" super.bind(effect);");
291
+ }
292
+
293
+ // Additional validation
294
+ if (fragmentShaderInfo.blockDisableStrategy !== undefined && fragmentShaderInfo.blockDisableStrategy !== BlockDisableStrategy.Manual) {
295
+ if (fragmentShaderInfo.uniforms.findIndex((uniform) => uniform.name === "disabled") !== -1) {
296
+ throw new Error("A block that uses a BlockDisableStrategy other than Manual should not declare its own 'disabled' uniform");
297
+ }
298
+ }
299
+
300
+ // Generate final contents
301
+ const finalContents = FileTemplate.replace(SHADER_PROGRAM, shaderProgramCode)
302
+ .replace(EXTRA_IMPORTS, extraImports.join(",\n"))
303
+ .replace(BABYLON_CORE_PATH, babylonCorePath || "@babylonjs/core")
304
+ .replace(SMART_FILTER_CORE_IMPORT, smartFiltersCorePath)
305
+ .replace(new RegExp(BLOCK_NAME, "g"), fragmentShaderInfo.blockType)
306
+ .replace(NAMESPACE, fragmentShaderInfo.namespace || "Other")
307
+ .replace(new RegExp(SHADER_BINDING_EXTENDS, "g"), shaderBindingExtends)
308
+ .replace(SHADER_BINDING_PRIVATE_VARIABLES, shaderBindingPrivateVariables.join("\n"))
309
+ .replace(SHADER_BINDING_CTOR_DOCSTRING_PARAMS, shaderBindingCtorDocstringParams.join("\n"))
310
+ .replace(SHADER_BINDING_CTOR_PARAMS, shaderBindingCtorParams.join(",\n"))
311
+ .replace(SHADER_BINDING_CTOR, shaderBindingCtor.join("\n"))
312
+ .replace(SHADER_BINDING_SUPER_PARAMS, shaderBindingSuperParams)
313
+ .replace(SHADER_BINDING_BIND, shaderBindingBind.join("\n"))
314
+ .replace(EXTRA_BIND_DOCSTRING, extraBindDocstring)
315
+ .replace(EXTRA_BIND_PARAMS, extraBindParams)
316
+ .replace(new RegExp(SHADER_BLOCK_EXTENDS, "g"), shaderBlockExtends)
317
+ .replace(BLOCK_INPUT_PROPERTIES, blockInputProperties.join("\n"))
318
+ .replace(BLOCK_DISABLE_OPTIMIZATION, disableOptimization)
319
+ .replace(BLOCK_DISABLE_STRATEGY, blockDisableStrategy)
320
+ .replace(BLOCK_GET_SHADER_BINDING_VARS, blockGetShaderBindingVars.join("\n"))
321
+ .replace(BLOCK_GET_SHADER_PARAM_LIST, blockGetShaderParamList.join(","));
322
+
323
+ // Write the block class TS file
324
+ const outputFullPathAndFileName = fragmentShaderPath.replace(".glsl", ".ts");
325
+ fs.writeFileSync(outputFullPathAndFileName, finalContents);
326
+ }
327
+
328
+ /**
329
+ * Get the string representation of a connection point type
330
+ * @param type - The connection point type
331
+ * @returns - The string representation of the connection point type
332
+ */
333
+ function GetConnectionPointTypeString(type: ConnectionPointType): string {
334
+ switch (type) {
335
+ case ConnectionPointType.Float:
336
+ return "Float";
337
+ case ConnectionPointType.Texture:
338
+ return "Texture";
339
+ case ConnectionPointType.Color3:
340
+ return "Color3";
341
+ case ConnectionPointType.Color4:
342
+ return "Color4";
343
+ case ConnectionPointType.Vector2:
344
+ return "Vector2";
345
+ case ConnectionPointType.Boolean:
346
+ return "Boolean";
347
+ }
348
+ }
349
+
350
+ /**
351
+ * Get the effect setter for a connection point type
352
+ * @param type - The connection point type
353
+ * @returns - The effect setter for the connection point type
354
+ */
355
+ function GetEffectSetter(type: ConnectionPointType): string {
356
+ switch (type) {
357
+ case ConnectionPointType.Float:
358
+ return "setFloat";
359
+ case ConnectionPointType.Texture:
360
+ return "setTexture";
361
+ case ConnectionPointType.Color3:
362
+ return "setColor3";
363
+ case ConnectionPointType.Color4:
364
+ return "setDirectColor4";
365
+ case ConnectionPointType.Vector2:
366
+ return "setVector2";
367
+ case ConnectionPointType.Boolean:
368
+ return "setBool";
369
+ }
370
+ }