@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,757 @@
1
+ import type { Nullable } from "core/types.js";
2
+ import { Logger } from "core/Misc/logger.js";
3
+
4
+ import type { ConnectionPoint } from "../connection/connectionPoint.js";
5
+ import type { ShaderBinding } from "../runtime/shaderRuntime.js";
6
+ import type { InputBlock } from "../blockFoundation/inputBlock.js";
7
+ import type { BaseBlock } from "../blockFoundation/baseBlock.js";
8
+ import { SmartFilter } from "../smartFilter.js";
9
+ import { ConnectionPointType } from "../connection/connectionPointType.js";
10
+ import { ShaderBlock } from "../blockFoundation/shaderBlock.js";
11
+ import { IsTextureInputBlock } from "../blockFoundation/inputBlock.js";
12
+ import { OptimizedShaderBlock } from "./optimizedShaderBlock.js";
13
+ import { AutoDisableMainInputColorName, DecorateChar, DecorateSymbol, GetShaderFragmentCode, UndecorateSymbol } from "../utils/shaderCodeUtils.js";
14
+ import { DependencyGraph } from "./dependencyGraph.js";
15
+ import { DisableableShaderBlock, BlockDisableStrategy } from "../blockFoundation/disableableShaderBlock.js";
16
+ import { TextureOptionsMatch, type OutputTextureOptions } from "../blockFoundation/textureOptions.js";
17
+
18
+ const GetDefineRegEx = /^\S*#define\s+(\w+).*$/; // Matches a #define statement line, capturing its decorated or undecorated name
19
+ const ShowDebugData = false;
20
+
21
+ /**
22
+ * @internal
23
+ */
24
+ type RemappedSymbol = {
25
+ /**
26
+ * The type of the symbol.
27
+ */
28
+ type: "uniform" | "const" | "sampler" | "function" | "define";
29
+
30
+ /**
31
+ * The name of the symbol.
32
+ */
33
+ name: string;
34
+
35
+ /**
36
+ * The name of the symbol after it has been remapped.
37
+ */
38
+ remappedName: string;
39
+
40
+ /**
41
+ * For "function", this is the parameter list to differentiate between overloads.
42
+ */
43
+ params?: string;
44
+
45
+ /**
46
+ * The declaration of the symbol. For "function" it is the function code.
47
+ */
48
+ declaration: string;
49
+
50
+ /**
51
+ * The ShaderBlock(s) that owns the symbol.
52
+ */
53
+ owners: ShaderBlock[];
54
+
55
+ /**
56
+ * The InputBlock that owns the texture. Only used for type="sampler".
57
+ */
58
+ inputBlock: InputBlock<ConnectionPointType.Texture> | undefined;
59
+ };
60
+
61
+ /**
62
+ * @internal
63
+ */
64
+ export type StackItem = {
65
+ /**
66
+ * The connection points to which to connect the output of the optimized block, once the optimized block has been created.
67
+ */
68
+ inputsToConnectTo: ConnectionPoint[];
69
+
70
+ /**
71
+ * The connection point to process.
72
+ */
73
+ outputConnectionPoint: ConnectionPoint;
74
+ };
75
+
76
+ /**
77
+ * Options for the smart filter optimizer.
78
+ */
79
+ export interface ISmartFilterOptimizerOptions {
80
+ /**
81
+ * The maximum number of samplers allowed in the fragment shader. Default: 8
82
+ */
83
+ maxSamplersInFragmentShader?: number;
84
+
85
+ /**
86
+ * If true, the optimizer will remove the disabled blocks from the optimized smart filter. Default: false
87
+ * It allows more aggressive optimizations, but removed blocks will no longer be available in the optimized smart filter.
88
+ */
89
+ removeDisabledBlocks?: boolean;
90
+ }
91
+
92
+ /**
93
+ * Optimizes a smart filter by aggregating blocks whenever possible, to reduce the number of draw calls.
94
+ */
95
+ export class SmartFilterOptimizer {
96
+ private _sourceSmartFilter: SmartFilter;
97
+ private _options: ISmartFilterOptimizerOptions;
98
+ private _blockStack: StackItem[] = [];
99
+ private _blockToStackItem: Map<BaseBlock, StackItem> = new Map();
100
+ private _savedBlockStack: StackItem[] = [];
101
+ private _savedBlockToStackItem: Map<BaseBlock, StackItem> = new Map();
102
+
103
+ private _symbolOccurrences: { [name: string]: number } = {};
104
+ private _remappedSymbols: Array<RemappedSymbol> = [];
105
+ private _blockToMainFunctionName: Map<BaseBlock, string> = new Map();
106
+ private _mainFunctionNameToCode: Map<string, string> = new Map();
107
+ private _dependencyGraph: DependencyGraph<string> = new DependencyGraph<string>();
108
+ private _vertexShaderCode: string | undefined;
109
+ private _currentOutputTextureOptions: OutputTextureOptions | undefined;
110
+ private _forceUnoptimized: boolean = false;
111
+
112
+ /**
113
+ * Creates a new smart filter optimizer
114
+ * @param smartFilter - The smart filter to optimize
115
+ * @param options - Options for the optimizer
116
+ */
117
+ constructor(smartFilter: SmartFilter, options?: ISmartFilterOptimizerOptions) {
118
+ this._sourceSmartFilter = smartFilter;
119
+ this._options = {
120
+ maxSamplersInFragmentShader: 8,
121
+ ...options,
122
+ };
123
+ }
124
+
125
+ /**
126
+ * Optimizes the smart filter by aggregating blocks whenever possible, to lower the number of rendering passes
127
+ * @returns The optimized smart filter, or null if the optimization failed
128
+ */
129
+ public optimize(): Nullable<SmartFilter> {
130
+ this._blockStack = [];
131
+ this._blockToStackItem = new Map();
132
+
133
+ let newSmartFilter: Nullable<SmartFilter> = null;
134
+
135
+ this._sourceSmartFilter._workWithAggregateFreeGraph(() => {
136
+ if (this._sourceSmartFilter.output.connectedTo && !IsTextureInputBlock(this._sourceSmartFilter.output.connectedTo.ownerBlock)) {
137
+ const connectionsToReconnect: [ConnectionPoint, ConnectionPoint][] = [];
138
+
139
+ if (this._options.removeDisabledBlocks) {
140
+ // Need to propagate runtime data to ensure we can tell if a block is disabled
141
+ this._sourceSmartFilter.output.ownerBlock.propagateRuntimeData();
142
+
143
+ const alreadyVisitedBlocks = new Set<BaseBlock>();
144
+ this._disconnectDisabledBlocks(this._sourceSmartFilter.output.connectedTo.ownerBlock, alreadyVisitedBlocks, connectionsToReconnect);
145
+ }
146
+
147
+ newSmartFilter = new SmartFilter(this._sourceSmartFilter.name + " - optimized");
148
+
149
+ // We must recheck isTextureInputBlock because all shader blocks may have been disconnected by the previous code
150
+ if (!IsTextureInputBlock(this._sourceSmartFilter.output.connectedTo.ownerBlock)) {
151
+ // Make sure all the connections in the graph have a runtimeData associated to them
152
+ // Note that the value of the runtimeData may not be set yet, we just need the objects to be created and propagated correctly
153
+ this._sourceSmartFilter.output.ownerBlock.prepareForRuntime();
154
+ this._sourceSmartFilter.output.ownerBlock.propagateRuntimeData();
155
+
156
+ const item: StackItem = {
157
+ inputsToConnectTo: [newSmartFilter.output],
158
+ outputConnectionPoint: this._sourceSmartFilter.output.connectedTo,
159
+ };
160
+
161
+ this._blockStack.push(item);
162
+ this._blockToStackItem.set(item.outputConnectionPoint.ownerBlock, item);
163
+
164
+ while (this._blockStack.length > 0) {
165
+ const { inputsToConnectTo, outputConnectionPoint } = this._blockStack.pop()!;
166
+
167
+ const newBlock = this._processBlock(newSmartFilter, outputConnectionPoint);
168
+
169
+ if (newBlock) {
170
+ for (const inputToConnectTo of inputsToConnectTo) {
171
+ inputToConnectTo.connectTo(newBlock.output);
172
+ }
173
+ }
174
+ }
175
+ } else {
176
+ newSmartFilter.output.connectTo(this._sourceSmartFilter.output.connectedTo);
177
+ }
178
+
179
+ if (this._options.removeDisabledBlocks) {
180
+ // We must reconnect the connections that were reconnected differently by the disconnect process, so that the original graph is left unmodified
181
+ for (const [input, connectedTo] of connectionsToReconnect) {
182
+ input.connectTo(connectedTo);
183
+ }
184
+ }
185
+ }
186
+ });
187
+
188
+ return newSmartFilter;
189
+ }
190
+
191
+ private _disconnectDisabledBlocks(block: BaseBlock, alreadyVisitedBlocks: Set<BaseBlock>, inputsToReconnect: [ConnectionPoint, ConnectionPoint][]) {
192
+ if (alreadyVisitedBlocks.has(block)) {
193
+ return;
194
+ }
195
+
196
+ alreadyVisitedBlocks.add(block);
197
+
198
+ for (const input of block.inputs) {
199
+ if (!input.connectedTo || input.type !== ConnectionPointType.Texture) {
200
+ continue;
201
+ }
202
+
203
+ this._disconnectDisabledBlocks(input.connectedTo.ownerBlock, alreadyVisitedBlocks, inputsToReconnect);
204
+ }
205
+
206
+ if (block instanceof DisableableShaderBlock && block.disabled.runtimeData.value) {
207
+ block.disconnectFromGraph(inputsToReconnect);
208
+ }
209
+ }
210
+
211
+ private _initialize() {
212
+ this._symbolOccurrences = {};
213
+ this._remappedSymbols = [];
214
+ this._blockToMainFunctionName = new Map();
215
+ this._mainFunctionNameToCode = new Map();
216
+ this._dependencyGraph = new DependencyGraph();
217
+ this._vertexShaderCode = undefined;
218
+ this._currentOutputTextureOptions = undefined;
219
+ this._forceUnoptimized = false;
220
+ }
221
+
222
+ private _makeSymbolUnique(symbolName: string): string {
223
+ let newVarName = symbolName;
224
+ if (!this._symbolOccurrences[symbolName]) {
225
+ this._symbolOccurrences[symbolName] = 1;
226
+ } else {
227
+ this._symbolOccurrences[symbolName]++;
228
+ newVarName += "_" + this._symbolOccurrences[symbolName];
229
+ }
230
+
231
+ return newVarName;
232
+ }
233
+
234
+ private _processDefines(block: ShaderBlock, code: string): string {
235
+ const defines = block.getShaderProgram().fragment.defines;
236
+ if (!defines) {
237
+ return code;
238
+ }
239
+
240
+ for (const define of defines) {
241
+ const match = define.match(GetDefineRegEx);
242
+ const defName = match?.[1];
243
+
244
+ if (!match || !defName) {
245
+ continue;
246
+ }
247
+
248
+ // See if we have already processed this define for this block type
249
+ const existingRemapped = this._remappedSymbols.find((s) => s.type === "define" && s.name === defName && s.owners[0] && s.owners[0].blockType === block.blockType);
250
+
251
+ let newDefName: string;
252
+ if (existingRemapped) {
253
+ newDefName = existingRemapped.remappedName;
254
+ } else {
255
+ // Add the new define to the remapped symbols list
256
+ newDefName = DecorateSymbol(this._makeSymbolUnique(UndecorateSymbol(defName)));
257
+
258
+ this._remappedSymbols.push({
259
+ type: "define",
260
+ name: defName,
261
+ remappedName: newDefName,
262
+ declaration: define.replace(defName, newDefName), // No need to reconstruct the declaration
263
+ owners: [block],
264
+ inputBlock: undefined,
265
+ });
266
+ }
267
+
268
+ // Replace the define name in the main shader code
269
+ code = code.replace(defName, newDefName);
270
+ }
271
+
272
+ return code;
273
+ }
274
+
275
+ private _processHelperFunctions(block: ShaderBlock, code: string): string {
276
+ const functions = block.getShaderProgram().fragment.functions;
277
+
278
+ if (functions.length === 1) {
279
+ // There's only the main function, so we don't need to do anything
280
+ return code;
281
+ }
282
+
283
+ const replaceFuncNames: Array<[RegExp, string]> = [];
284
+
285
+ for (const func of functions) {
286
+ let funcName = func.name;
287
+
288
+ if (funcName === block.getShaderProgram().fragment.mainFunctionName) {
289
+ continue;
290
+ }
291
+
292
+ funcName = UndecorateSymbol(funcName);
293
+
294
+ const regexFindCurName = new RegExp(DecorateSymbol(funcName), "g");
295
+
296
+ const existingFunctionExactOverload = this._remappedSymbols.find(
297
+ (s) => s.type === "function" && s.name === funcName && s.params === func.params && s.owners[0] && s.owners[0].blockType === block.blockType
298
+ );
299
+
300
+ const existingFunction = this._remappedSymbols.find((s) => s.type === "function" && s.name === funcName && s.owners[0] && s.owners[0].blockType === block.blockType);
301
+
302
+ // Get or create the remapped name, ignoring the parameter list
303
+ const newVarName = existingFunction?.remappedName ?? DecorateSymbol(this._makeSymbolUnique(funcName));
304
+
305
+ // If the function name, regardless of params, wasn't found, add the rename mapping to our list
306
+ if (!existingFunction) {
307
+ replaceFuncNames.push([regexFindCurName, newVarName]);
308
+ }
309
+
310
+ // If this exact overload wasn't found, add it to the list of remapped symbols so it'll be emitted in
311
+ // the final shader.
312
+ if (!existingFunctionExactOverload) {
313
+ let funcCode = func.code;
314
+ for (const [regex, replacement] of replaceFuncNames) {
315
+ funcCode = funcCode.replace(regex, replacement);
316
+ }
317
+
318
+ this._remappedSymbols.push({
319
+ type: "function",
320
+ name: funcName,
321
+ remappedName: newVarName,
322
+ params: func.params,
323
+ declaration: funcCode,
324
+ owners: [block],
325
+ inputBlock: undefined,
326
+ });
327
+ }
328
+
329
+ code = code.replace(regexFindCurName, newVarName);
330
+ }
331
+
332
+ return code;
333
+ }
334
+
335
+ private _processVariables(
336
+ block: ShaderBlock,
337
+ code: string,
338
+ varDecl: "const" | "uniform",
339
+ declarations?: string,
340
+ hasValue = false,
341
+ forceSingleInstance = false
342
+ ): [string, Array<string>] {
343
+ if (!declarations) {
344
+ return [code, []];
345
+ }
346
+
347
+ let rex = `${varDecl}\\s+(\\S+)\\s+${DecorateChar}(\\w+)${DecorateChar}\\s*`;
348
+ if (hasValue) {
349
+ rex += "=\\s*(.+);";
350
+ } else {
351
+ rex += ";";
352
+ }
353
+
354
+ const samplerList = [];
355
+ const rx = new RegExp(rex, "g");
356
+
357
+ let match = rx.exec(declarations);
358
+ while (match !== null) {
359
+ const singleInstance = forceSingleInstance || varDecl === "const";
360
+ const varType = match[1]!;
361
+ const varName = match[2]!;
362
+ const varValue = hasValue ? match[3]! : null;
363
+
364
+ let newVarName: Nullable<string> = null;
365
+
366
+ if (varType === "sampler2D") {
367
+ samplerList.push(DecorateSymbol(varName));
368
+ } else {
369
+ const existingRemapped = this._remappedSymbols.find((s) => s.type === varDecl && s.name === varName && s.owners[0] && s.owners[0].blockType === block.blockType);
370
+ if (existingRemapped && singleInstance) {
371
+ newVarName = existingRemapped.remappedName;
372
+ if (varDecl === "uniform") {
373
+ existingRemapped.owners.push(block);
374
+ }
375
+ } else {
376
+ newVarName = DecorateSymbol(this._makeSymbolUnique(varName));
377
+
378
+ this._remappedSymbols.push({
379
+ type: varDecl,
380
+ name: varName,
381
+ remappedName: newVarName,
382
+ declaration: `${varDecl} ${varType} ${newVarName}${hasValue ? " = " + varValue : ""};`,
383
+ owners: [block],
384
+ inputBlock: undefined,
385
+ });
386
+ }
387
+ }
388
+
389
+ if (newVarName) {
390
+ code = code.replace(new RegExp(DecorateSymbol(varName), "g"), newVarName);
391
+ }
392
+
393
+ match = rx.exec(declarations);
394
+ }
395
+
396
+ return [code, samplerList];
397
+ }
398
+
399
+ private _processSampleTexture(block: ShaderBlock, code: string, sampler: string, samplers: string[], inputTextureBlock?: InputBlock<ConnectionPointType.Texture>): string {
400
+ const rx = new RegExp(`__sampleTexture\\s*\\(\\s*${DecorateChar}${sampler}${DecorateChar}\\s*,\\s*(.*?)\\s*\\)`);
401
+
402
+ let newSamplerName = sampler;
403
+
404
+ const existingRemapped = this._remappedSymbols.find((s) => s.type === "sampler" && s.inputBlock && s.inputBlock === inputTextureBlock);
405
+ if (existingRemapped) {
406
+ // The texture is shared by multiple blocks. We must reuse the same sampler name
407
+ newSamplerName = existingRemapped.remappedName;
408
+ } else {
409
+ newSamplerName = DecorateSymbol(this._makeSymbolUnique(newSamplerName));
410
+
411
+ this._remappedSymbols.push({
412
+ type: "sampler",
413
+ name: sampler,
414
+ remappedName: newSamplerName,
415
+ declaration: `uniform sampler2D ${newSamplerName};`,
416
+ owners: [block],
417
+ inputBlock: inputTextureBlock,
418
+ });
419
+ }
420
+
421
+ if (samplers.indexOf(newSamplerName) === -1) {
422
+ samplers.push(newSamplerName);
423
+ }
424
+
425
+ let match = rx.exec(code);
426
+ while (match !== null) {
427
+ const uv = match[1]!;
428
+
429
+ code = code.substring(0, match.index) + `texture2D(${newSamplerName}, ${uv})` + code.substring(match.index + match[0]!.length);
430
+
431
+ match = rx.exec(code);
432
+ }
433
+
434
+ return code;
435
+ }
436
+
437
+ private _canBeOptimized(block: BaseBlock): boolean {
438
+ if (block.disableOptimization) {
439
+ return false;
440
+ }
441
+
442
+ if (block instanceof ShaderBlock) {
443
+ if (block.getShaderProgram().vertex !== this._vertexShaderCode) {
444
+ return false;
445
+ }
446
+
447
+ if (!TextureOptionsMatch(block.outputTextureOptions, this._currentOutputTextureOptions)) {
448
+ return false;
449
+ }
450
+ }
451
+
452
+ return true;
453
+ }
454
+
455
+ // Processes a block given one of its output connection point
456
+ // Returns the name of the main function in the shader code
457
+ private _optimizeBlock(optimizedBlock: OptimizedShaderBlock, outputConnectionPoint: ConnectionPoint, samplers: string[]): string {
458
+ const block = outputConnectionPoint.ownerBlock;
459
+
460
+ if (block instanceof ShaderBlock) {
461
+ if (this._currentOutputTextureOptions === undefined) {
462
+ this._currentOutputTextureOptions = block.outputTextureOptions;
463
+ }
464
+
465
+ const shaderProgram = block.getShaderProgram();
466
+
467
+ if (!shaderProgram) {
468
+ throw new Error(`Shader program not found for block "${block.name}"!`);
469
+ }
470
+
471
+ // We get the shader code of the main function only
472
+ let code = GetShaderFragmentCode(shaderProgram, true);
473
+
474
+ this._vertexShaderCode = this._vertexShaderCode ?? shaderProgram.vertex;
475
+
476
+ // Generates a unique name for the fragment main function (if not already generated)
477
+ const shaderFuncName = shaderProgram.fragment.mainFunctionName;
478
+
479
+ let newShaderFuncName = this._blockToMainFunctionName.get(block);
480
+
481
+ if (!newShaderFuncName) {
482
+ newShaderFuncName = UndecorateSymbol(shaderFuncName);
483
+ newShaderFuncName = DecorateSymbol(this._makeSymbolUnique(newShaderFuncName));
484
+
485
+ this._blockToMainFunctionName.set(block, newShaderFuncName);
486
+ this._dependencyGraph.addElement(newShaderFuncName);
487
+ }
488
+
489
+ // Replaces the main function name by the new one
490
+ code = code.replace(shaderFuncName, newShaderFuncName);
491
+
492
+ // Removes the vUV declaration if it exists
493
+ code = code.replace(/varying\s+vec2\s+vUV\s*;/g, "");
494
+
495
+ // Replaces the texture2D calls by __sampleTexture for easier processing
496
+ code = code.replace(/(?<!\w)texture2D\s*\(/g, " __sampleTexture(");
497
+
498
+ // Processes the defines to make them unique
499
+ code = this._processDefines(block, code);
500
+
501
+ // Processes the functions other than the main function
502
+ code = this._processHelperFunctions(block, code);
503
+
504
+ // Processes the constants to make them unique
505
+ code = this._processVariables(block, code, "const", shaderProgram.fragment.const, true)[0];
506
+
507
+ // Processes the uniform inputs to make them unique. Also extract the list of samplers
508
+ let samplerList: string[] = [];
509
+ [code, samplerList] = this._processVariables(block, code, "uniform", shaderProgram.fragment.uniform, false);
510
+
511
+ let additionalSamplers = [];
512
+ [code, additionalSamplers] = this._processVariables(block, code, "uniform", shaderProgram.fragment.uniformSingle, false, true);
513
+
514
+ samplerList.push(...additionalSamplers);
515
+
516
+ // Processes the texture inputs
517
+ for (const sampler of samplerList) {
518
+ const samplerName = UndecorateSymbol(sampler);
519
+
520
+ const input = block.findInput(samplerName);
521
+ if (!input) {
522
+ // No connection point found corresponding to this texture: it must be a texture used internally by the filter (here we are assuming that the shader code is not bugged!)
523
+ code = this._processSampleTexture(block, code, samplerName, samplers);
524
+ continue;
525
+ }
526
+
527
+ // input found. Is it connected?
528
+ if (!input.connectedTo) {
529
+ throw `The connection point corresponding to the input named "${samplerName}" in block named "${block.name}" is not connected!`;
530
+ }
531
+
532
+ // If we are using the AutoSample strategy, we must preprocess the code that samples the texture
533
+ if (block instanceof DisableableShaderBlock && block.blockDisableStrategy === BlockDisableStrategy.AutoSample) {
534
+ code = this._applyAutoSampleStrategy(code, sampler);
535
+ }
536
+
537
+ const parentBlock = input.connectedTo.ownerBlock;
538
+
539
+ if (IsTextureInputBlock(parentBlock)) {
540
+ // input is connected to an InputBlock of type "Texture": we must directly sample a texture
541
+ code = this._processSampleTexture(block, code, samplerName, samplers, parentBlock);
542
+ } else if (this._forceUnoptimized || !this._canBeOptimized(parentBlock)) {
543
+ // the block connected to this input cannot be optimized: we must directly sample its output texture
544
+ code = this._processSampleTexture(block, code, samplerName, samplers);
545
+ let stackItem = this._blockToStackItem.get(parentBlock);
546
+ if (!stackItem) {
547
+ stackItem = {
548
+ inputsToConnectTo: [],
549
+ outputConnectionPoint: input.connectedTo,
550
+ };
551
+ this._blockStack.push(stackItem);
552
+ this._blockToStackItem.set(parentBlock, stackItem);
553
+ }
554
+ // creates a new input connection point for the texture in the optimized block
555
+ const connectionPoint = optimizedBlock._registerInput(samplerName, ConnectionPointType.Texture);
556
+ stackItem.inputsToConnectTo.push(connectionPoint);
557
+ } else {
558
+ let parentFuncName: string;
559
+
560
+ if (this._blockToMainFunctionName.has(parentBlock)) {
561
+ // The parent block has already been processed. We can directly use the main function name
562
+ parentFuncName = this._blockToMainFunctionName.get(parentBlock)!;
563
+ } else {
564
+ // Recursively processes the block connected to this input to get the main function name of the parent block
565
+ parentFuncName = this._optimizeBlock(optimizedBlock, input.connectedTo, samplers);
566
+ this._dependencyGraph.addDependency(newShaderFuncName, parentFuncName);
567
+ }
568
+
569
+ // The texture samplerName is not used anymore by the block, as it is replaced by a call to the main function of the parent block
570
+ // We remap it to an non existent sampler name, because the code that binds the texture still exists in the ShaderBinding.bind function.
571
+ // We don't want this code to have any effect, as it could overwrite (and remove) the texture binding of another block using this same sampler name!
572
+ this._remappedSymbols.push({
573
+ type: "sampler",
574
+ name: samplerName,
575
+ remappedName: "L(° O °L)",
576
+ declaration: ``,
577
+ owners: [block],
578
+ inputBlock: undefined,
579
+ });
580
+
581
+ // We have to replace the call(s) to __sampleTexture by a call to the main function of the parent block
582
+ const rx = new RegExp(`__sampleTexture\\s*\\(\\s*${sampler}\\s*,\\s*(.*?)\\s*\\)`);
583
+
584
+ let match = rx.exec(code);
585
+ while (match !== null) {
586
+ const uv = match[1];
587
+
588
+ code = code.substring(0, match.index) + `${parentFuncName}(${uv})` + code.substring(match.index + match[0]!.length);
589
+ match = rx.exec(code);
590
+ }
591
+ }
592
+ }
593
+
594
+ this._mainFunctionNameToCode.set(newShaderFuncName, code);
595
+
596
+ return newShaderFuncName;
597
+ }
598
+
599
+ throw `Unhandled block type! blockType=${block.blockType}`;
600
+ }
601
+
602
+ private _saveBlockStackState(): void {
603
+ this._savedBlockStack = this._blockStack.slice();
604
+ this._savedBlockToStackItem = new Map();
605
+
606
+ for (const [key, value] of this._blockToStackItem) {
607
+ value.inputsToConnectTo = value.inputsToConnectTo.slice();
608
+ this._savedBlockToStackItem.set(key, value);
609
+ }
610
+ }
611
+
612
+ private _restoreBlockStackState(): void {
613
+ this._blockStack.length = 0;
614
+ this._blockStack.push(...this._savedBlockStack);
615
+
616
+ this._blockToStackItem.clear();
617
+ for (const [key, value] of this._savedBlockToStackItem) {
618
+ this._blockToStackItem.set(key, value);
619
+ }
620
+ }
621
+
622
+ private _processBlock(newSmartFilter: SmartFilter, outputConnectionPoint: ConnectionPoint): Nullable<ShaderBlock> {
623
+ this._saveBlockStackState();
624
+ this._initialize();
625
+
626
+ let optimizedBlock = new OptimizedShaderBlock(newSmartFilter, "optimized");
627
+
628
+ const samplers: string[] = [];
629
+ let mainFuncName = this._optimizeBlock(optimizedBlock, outputConnectionPoint, samplers);
630
+
631
+ if (samplers.length > this._options.maxSamplersInFragmentShader!) {
632
+ // Too many samplers for the optimized block.
633
+ // We must force the unoptimized mode and regenerate the block, which will be unoptimized this time
634
+ newSmartFilter.removeBlock(optimizedBlock);
635
+
636
+ this._initialize();
637
+
638
+ optimizedBlock = new OptimizedShaderBlock(newSmartFilter, "unoptimized");
639
+
640
+ this._forceUnoptimized = true;
641
+ samplers.length = 0;
642
+
643
+ this._restoreBlockStackState();
644
+
645
+ mainFuncName = this._optimizeBlock(optimizedBlock, outputConnectionPoint, samplers);
646
+ }
647
+
648
+ // Collects all the shader code
649
+ let code = "";
650
+ this._dependencyGraph.walk((element: string) => {
651
+ code += this._mainFunctionNameToCode.get(element)! + "\n";
652
+ });
653
+
654
+ // Sets the remapping of the shader variables
655
+ const blockOwnerToShaderBinding = new Map<ShaderBlock, ShaderBinding>();
656
+
657
+ const codeDefines = [];
658
+ let codeUniforms = "";
659
+ let codeConsts = "";
660
+ let codeFunctions = "";
661
+
662
+ for (const s of this._remappedSymbols) {
663
+ switch (s.type) {
664
+ case "define":
665
+ codeDefines.push(s.declaration);
666
+ break;
667
+ case "const":
668
+ codeConsts += s.declaration + "\n";
669
+ break;
670
+ case "uniform":
671
+ case "sampler":
672
+ codeUniforms += s.declaration + "\n";
673
+ break;
674
+ case "function":
675
+ codeFunctions += s.declaration + "\n";
676
+ break;
677
+ }
678
+
679
+ for (const block of s.owners) {
680
+ let shaderBinding = blockOwnerToShaderBinding.get(block);
681
+ if (!shaderBinding) {
682
+ shaderBinding = block.getShaderBinding();
683
+ blockOwnerToShaderBinding.set(block, shaderBinding);
684
+ }
685
+
686
+ switch (s.type) {
687
+ case "uniform":
688
+ case "sampler":
689
+ shaderBinding.addShaderVariableRemapping(DecorateSymbol(s.name), s.remappedName);
690
+ break;
691
+ }
692
+ }
693
+ }
694
+
695
+ // Builds and sets the final shader code
696
+ code = codeFunctions + code;
697
+ if (ShowDebugData) {
698
+ code = code.replace(/^ {16}/gm, "");
699
+ code = code!.replace(/\r/g, "");
700
+ code = code!.replace(/\n(\n)*/g, "\n");
701
+
702
+ Logger.Log(`=================== BLOCK (forceUnoptimized=${this._forceUnoptimized}) ===================`);
703
+ Logger.Log(codeDefines.join("\n"));
704
+ Logger.Log(codeUniforms);
705
+ Logger.Log(codeConsts);
706
+ Logger.Log(code);
707
+ Logger.Log(`remappedSymbols=${this._remappedSymbols}`);
708
+ Logger.Log(`samplers=${samplers}`);
709
+ }
710
+
711
+ optimizedBlock.setShaderProgram({
712
+ vertex: this._vertexShaderCode,
713
+ fragment: {
714
+ defines: codeDefines,
715
+ const: codeConsts,
716
+ uniform: codeUniforms,
717
+ mainFunctionName: mainFuncName,
718
+ functions: [
719
+ {
720
+ name: mainFuncName,
721
+ params: "",
722
+ code,
723
+ },
724
+ ],
725
+ },
726
+ });
727
+
728
+ if (this._currentOutputTextureOptions !== undefined) {
729
+ optimizedBlock.outputTextureOptions = this._currentOutputTextureOptions;
730
+ }
731
+
732
+ optimizedBlock.setShaderBindings(Array.from(blockOwnerToShaderBinding.values()));
733
+
734
+ return optimizedBlock;
735
+ }
736
+
737
+ /**
738
+ * If this block used DisableStrategy.AutoSample, find all the __sampleTexture calls which just pass the vUV,
739
+ * skip the first one, and for all others replace with the local variable created by the DisableStrategy.AutoSample
740
+ *
741
+ * @param code - The shader code to process
742
+ * @param sampler - The name of the sampler
743
+ *
744
+ * @returns The processed code
745
+ */
746
+ private _applyAutoSampleStrategy(code: string, sampler: string): string {
747
+ let isFirstMatch = true;
748
+ const rx = new RegExp(`__sampleTexture\\s*\\(\\s*${sampler}\\s*,\\s*vUV\\s*\\)`, "g");
749
+ return code.replace(rx, (match) => {
750
+ if (isFirstMatch) {
751
+ isFirstMatch = false;
752
+ return match;
753
+ }
754
+ return DecorateSymbol(AutoDisableMainInputColorName);
755
+ });
756
+ }
757
+ }