@babylonjs/smart-filters 0.1.0-alpha
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.
- package/dist/IDisposable.d.ts +10 -0
- package/dist/IDisposable.d.ts.map +1 -0
- package/dist/IDisposable.js +2 -0
- package/dist/IDisposable.js.map +1 -0
- package/dist/blocks/aggregateBlock.d.ts +50 -0
- package/dist/blocks/aggregateBlock.d.ts.map +1 -0
- package/dist/blocks/aggregateBlock.js +103 -0
- package/dist/blocks/aggregateBlock.js.map +1 -0
- package/dist/blocks/baseBlock.d.ts +160 -0
- package/dist/blocks/baseBlock.d.ts.map +1 -0
- package/dist/blocks/baseBlock.js +256 -0
- package/dist/blocks/baseBlock.js.map +1 -0
- package/dist/blocks/copyBlock.d.ts +59 -0
- package/dist/blocks/copyBlock.d.ts.map +1 -0
- package/dist/blocks/copyBlock.js +84 -0
- package/dist/blocks/copyBlock.js.map +1 -0
- package/dist/blocks/disableableBlock.d.ts +30 -0
- package/dist/blocks/disableableBlock.d.ts.map +1 -0
- package/dist/blocks/disableableBlock.js +22 -0
- package/dist/blocks/disableableBlock.js.map +1 -0
- package/dist/blocks/inputBlock.d.ts +64 -0
- package/dist/blocks/inputBlock.d.ts.map +1 -0
- package/dist/blocks/inputBlock.js +74 -0
- package/dist/blocks/inputBlock.js.map +1 -0
- package/dist/blocks/outputBlock.d.ts +42 -0
- package/dist/blocks/outputBlock.d.ts.map +1 -0
- package/dist/blocks/outputBlock.js +74 -0
- package/dist/blocks/outputBlock.js.map +1 -0
- package/dist/blocks/shaderBlock.d.ts +68 -0
- package/dist/blocks/shaderBlock.d.ts.map +1 -0
- package/dist/blocks/shaderBlock.js +101 -0
- package/dist/blocks/shaderBlock.js.map +1 -0
- package/dist/command/command.d.ts +49 -0
- package/dist/command/command.d.ts.map +1 -0
- package/dist/command/command.js +15 -0
- package/dist/command/command.js.map +1 -0
- package/dist/command/commandBuffer.d.ts +40 -0
- package/dist/command/commandBuffer.d.ts.map +1 -0
- package/dist/command/commandBuffer.js +58 -0
- package/dist/command/commandBuffer.js.map +1 -0
- package/dist/command/commandBufferDebugger.d.ts +7 -0
- package/dist/command/commandBufferDebugger.d.ts.map +1 -0
- package/dist/command/commandBufferDebugger.js +12 -0
- package/dist/command/commandBufferDebugger.js.map +1 -0
- package/dist/connection/connectionPoint.d.ts +110 -0
- package/dist/connection/connectionPoint.d.ts.map +1 -0
- package/dist/connection/connectionPoint.js +153 -0
- package/dist/connection/connectionPoint.js.map +1 -0
- package/dist/connection/connectionPointCompatibilityState.d.ts +20 -0
- package/dist/connection/connectionPointCompatibilityState.d.ts.map +1 -0
- package/dist/connection/connectionPointCompatibilityState.js +32 -0
- package/dist/connection/connectionPointCompatibilityState.js.map +1 -0
- package/dist/connection/connectionPointDirection.d.ts +10 -0
- package/dist/connection/connectionPointDirection.d.ts.map +1 -0
- package/dist/connection/connectionPointDirection.js +11 -0
- package/dist/connection/connectionPointDirection.js.map +1 -0
- package/dist/connection/connectionPointType.d.ts +22 -0
- package/dist/connection/connectionPointType.d.ts.map +1 -0
- package/dist/connection/connectionPointType.js +17 -0
- package/dist/connection/connectionPointType.js.map +1 -0
- package/dist/connection/connectionPointWithDefault.d.ts +23 -0
- package/dist/connection/connectionPointWithDefault.d.ts.map +1 -0
- package/dist/connection/connectionPointWithDefault.js +19 -0
- package/dist/connection/connectionPointWithDefault.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/optimization/dependencyGraph.d.ts +31 -0
- package/dist/optimization/dependencyGraph.d.ts.map +1 -0
- package/dist/optimization/dependencyGraph.js +77 -0
- package/dist/optimization/dependencyGraph.js.map +1 -0
- package/dist/optimization/optimizedShaderBlock.d.ts +75 -0
- package/dist/optimization/optimizedShaderBlock.d.ts.map +1 -0
- package/dist/optimization/optimizedShaderBlock.js +105 -0
- package/dist/optimization/optimizedShaderBlock.js.map +1 -0
- package/dist/optimization/smartFilterOptimizer.d.ts +72 -0
- package/dist/optimization/smartFilterOptimizer.d.ts.map +1 -0
- package/dist/optimization/smartFilterOptimizer.js +482 -0
- package/dist/optimization/smartFilterOptimizer.js.map +1 -0
- package/dist/runtime/renderTargetGenerator.d.ts +35 -0
- package/dist/runtime/renderTargetGenerator.d.ts.map +1 -0
- package/dist/runtime/renderTargetGenerator.js +153 -0
- package/dist/runtime/renderTargetGenerator.js.map +1 -0
- package/dist/runtime/shaderRuntime.d.ts +95 -0
- package/dist/runtime/shaderRuntime.d.ts.map +1 -0
- package/dist/runtime/shaderRuntime.js +120 -0
- package/dist/runtime/shaderRuntime.js.map +1 -0
- package/dist/runtime/smartFilterRuntime.d.ts +69 -0
- package/dist/runtime/smartFilterRuntime.d.ts.map +1 -0
- package/dist/runtime/smartFilterRuntime.js +57 -0
- package/dist/runtime/smartFilterRuntime.js.map +1 -0
- package/dist/runtime/strongRef.d.ts +16 -0
- package/dist/runtime/strongRef.d.ts.map +1 -0
- package/dist/runtime/strongRef.js +9 -0
- package/dist/runtime/strongRef.js.map +1 -0
- package/dist/smartFilter.d.ts +100 -0
- package/dist/smartFilter.d.ts.map +1 -0
- package/dist/smartFilter.js +154 -0
- package/dist/smartFilter.js.map +1 -0
- package/dist/utils/shaderCodeUtils.d.ts +131 -0
- package/dist/utils/shaderCodeUtils.d.ts.map +1 -0
- package/dist/utils/shaderCodeUtils.js +115 -0
- package/dist/utils/shaderCodeUtils.js.map +1 -0
- package/dist/utils/textureLoaders.d.ts +14 -0
- package/dist/utils/textureLoaders.d.ts.map +1 -0
- package/dist/utils/textureLoaders.js +22 -0
- package/dist/utils/textureLoaders.js.map +1 -0
- package/license.md +21 -0
- package/package.json +47 -0
- package/readme.md +165 -0
- package/src/IDisposable.ts +9 -0
- package/src/blocks/aggregateBlock.ts +121 -0
- package/src/blocks/baseBlock.ts +341 -0
- package/src/blocks/copyBlock.ts +103 -0
- package/src/blocks/disableableBlock.ts +40 -0
- package/src/blocks/inputBlock.ts +114 -0
- package/src/blocks/outputBlock.ts +97 -0
- package/src/blocks/shaderBlock.ts +145 -0
- package/src/command/command.ts +60 -0
- package/src/command/commandBuffer.ts +71 -0
- package/src/command/commandBufferDebugger.ts +13 -0
- package/src/connection/connectionPoint.ts +212 -0
- package/src/connection/connectionPointCompatibilityState.ts +31 -0
- package/src/connection/connectionPointDirection.ts +9 -0
- package/src/connection/connectionPointType.ts +30 -0
- package/src/connection/connectionPointWithDefault.ts +35 -0
- package/src/index.ts +36 -0
- package/src/optimization/dependencyGraph.ts +94 -0
- package/src/optimization/optimizedShaderBlock.ts +133 -0
- package/src/optimization/smartFilterOptimizer.ts +706 -0
- package/src/runtime/renderTargetGenerator.ts +204 -0
- package/src/runtime/shaderRuntime.ts +155 -0
- package/src/runtime/smartFilterRuntime.ts +104 -0
- package/src/runtime/strongRef.ts +18 -0
- package/src/smartFilter.ts +227 -0
- package/src/utils/shaderCodeUtils.ts +242 -0
- package/src/utils/textureLoaders.ts +28 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import type { ThinTexture } from "@babylonjs/core/Materials/Textures/thinTexture";
|
|
2
|
+
import type { Nullable } from "@babylonjs/core/types";
|
|
3
|
+
import { ThinRenderTargetTexture } from "@babylonjs/core/Materials/Textures/thinRenderTargetTexture.js";
|
|
4
|
+
|
|
5
|
+
import type { BaseBlock } from "../blocks/baseBlock";
|
|
6
|
+
import type { InitializationData, SmartFilter } from "../smartFilter";
|
|
7
|
+
import type { InternalSmartFilterRuntime } from "./smartFilterRuntime";
|
|
8
|
+
import { ShaderBlock } from "../blocks/shaderBlock.js";
|
|
9
|
+
import { createStrongRef } from "./strongRef.js";
|
|
10
|
+
import { ConnectionPointType } from "../connection/connectionPointType.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
interface RefCountedTexture {
|
|
16
|
+
/**
|
|
17
|
+
* The texture.
|
|
18
|
+
*/
|
|
19
|
+
texture: ThinTexture;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The reference count.
|
|
23
|
+
*/
|
|
24
|
+
refCount: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The render target generator is responsible for creating and assigning render targets to ShaderBlocks.
|
|
29
|
+
*/
|
|
30
|
+
export class RenderTargetGenerator {
|
|
31
|
+
private _optimize: boolean;
|
|
32
|
+
private _renderTargetPool: Map<number, Set<RefCountedTexture>>;
|
|
33
|
+
private _numTargetsCreated;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Creates a new render target generator.
|
|
37
|
+
* @param optimize - If true, the render target generator will try to reuse render targets as much as possible.
|
|
38
|
+
*/
|
|
39
|
+
constructor(optimize = true) {
|
|
40
|
+
this._optimize = optimize;
|
|
41
|
+
this._renderTargetPool = new Map();
|
|
42
|
+
this._numTargetsCreated = 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Returns the number of render targets created by the process
|
|
47
|
+
*/
|
|
48
|
+
public get numTargetsCreated() {
|
|
49
|
+
return this._numTargetsCreated;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Sets the output textures for the ShaderBlocks of the smart filter.
|
|
54
|
+
* @param smart - The smart filter to generate the render targets for.
|
|
55
|
+
* @param initializationData - The initialization data to use.
|
|
56
|
+
*/
|
|
57
|
+
public setOutputTextures(smart: SmartFilter, initializationData: InitializationData) {
|
|
58
|
+
smart.output.ownerBlock.visit(
|
|
59
|
+
initializationData,
|
|
60
|
+
(block: BaseBlock, initializationData: InitializationData) => {
|
|
61
|
+
if (!(block instanceof ShaderBlock)) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
let refCountedTexture: Nullable<RefCountedTexture> = null;
|
|
66
|
+
|
|
67
|
+
// We assign a texture to the output of the block only if this is not the last block in the chain,
|
|
68
|
+
// i.e. not the block connected to the smart output block (in which case the output of the block is to the canvas and not a texture).
|
|
69
|
+
if (!block.output.endpoints.some((cp) => cp.ownerBlock === smart.output.ownerBlock)) {
|
|
70
|
+
refCountedTexture = this._getTexture(initializationData.runtime, block.textureRatio);
|
|
71
|
+
|
|
72
|
+
if (!block.output.runtimeData) {
|
|
73
|
+
const runtimeOutput = createStrongRef(refCountedTexture.texture);
|
|
74
|
+
block.output.runtimeData = runtimeOutput;
|
|
75
|
+
} else {
|
|
76
|
+
block.output.runtimeData.value = refCountedTexture.texture;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (this._optimize) {
|
|
81
|
+
if (refCountedTexture !== null) {
|
|
82
|
+
for (const output of block.outputs) {
|
|
83
|
+
output.endpoints.forEach((endpoint) => {
|
|
84
|
+
if (endpoint.connectedTo) {
|
|
85
|
+
refCountedTexture!.refCount++;
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
for (const input of block.inputs) {
|
|
92
|
+
if (!input.connectedTo || input.connectedTo.type !== ConnectionPointType.Texture) {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const connectedBlock = input.connectedTo.ownerBlock;
|
|
96
|
+
if (connectedBlock instanceof ShaderBlock && connectedBlock.output.runtimeData) {
|
|
97
|
+
this._releaseTexture(connectedBlock.output.runtimeData.value, connectedBlock.textureRatio);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
);
|
|
103
|
+
this._renderTargetPool.clear();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private _findAvailableTexture(ratio: number): Nullable<RefCountedTexture> {
|
|
107
|
+
const refCountedTextures = this._renderTargetPool.get(ratio);
|
|
108
|
+
if (!refCountedTextures) {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
for (const refCountedTexture of refCountedTextures) {
|
|
113
|
+
if (refCountedTexture.refCount === 0) {
|
|
114
|
+
return refCountedTexture;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
private _getTexture(runtime: InternalSmartFilterRuntime, ratio: number): RefCountedTexture {
|
|
122
|
+
if (!this._optimize) {
|
|
123
|
+
this._numTargetsCreated++;
|
|
124
|
+
return {
|
|
125
|
+
texture: this._createTexture(runtime, ratio),
|
|
126
|
+
refCount: 0,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
let refCountedTextures = this._renderTargetPool.get(ratio);
|
|
131
|
+
if (!refCountedTextures) {
|
|
132
|
+
refCountedTextures = new Set();
|
|
133
|
+
this._renderTargetPool.set(ratio, refCountedTextures);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
let refCountedTexture = this._findAvailableTexture(ratio);
|
|
137
|
+
if (!refCountedTexture) {
|
|
138
|
+
refCountedTexture = {
|
|
139
|
+
texture: this._createTexture(runtime, ratio),
|
|
140
|
+
refCount: 0,
|
|
141
|
+
};
|
|
142
|
+
refCountedTextures.add(refCountedTexture);
|
|
143
|
+
this._numTargetsCreated++;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return refCountedTexture;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
private _releaseTexture(texture: ThinTexture, ratio: number) {
|
|
150
|
+
if (!this._optimize) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const refCountedTextures = this._renderTargetPool.get(ratio);
|
|
155
|
+
if (!refCountedTextures) {
|
|
156
|
+
throw new Error(`_releaseTextureToPool: Trying to add a texture to a non existing pool ${ratio}!`);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
for (const refCountedTexture of refCountedTextures) {
|
|
160
|
+
if (refCountedTexture.texture === texture) {
|
|
161
|
+
refCountedTexture.refCount--;
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
throw new Error(`_releaseTextureToPool: Can't find the texture in the pool ${ratio}!`);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Creates an offscreen texture to hold on the result of the block rendering.
|
|
171
|
+
* @param runtime - The current runtime we create the texture for
|
|
172
|
+
* @param ratio - The ratio of the texture to create compared to the final output
|
|
173
|
+
* @returns The render target texture
|
|
174
|
+
*/
|
|
175
|
+
private _createTexture(runtime: InternalSmartFilterRuntime, ratio: number): ThinRenderTargetTexture {
|
|
176
|
+
const engine = runtime.engine;
|
|
177
|
+
|
|
178
|
+
// We are only rendering full screen post process without depth or stencil information
|
|
179
|
+
const setup = {
|
|
180
|
+
generateDepthBuffer: false,
|
|
181
|
+
generateStencilBuffer: false,
|
|
182
|
+
generateMipMaps: false,
|
|
183
|
+
samplingMode: 2, // Babylon Constants.TEXTURE_LINEAR_LINEAR,
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
// The size of the output is by default the current rendering size of the engine
|
|
187
|
+
const width = engine.getRenderWidth(true);
|
|
188
|
+
const height = engine.getRenderHeight(true);
|
|
189
|
+
const size = {
|
|
190
|
+
width: Math.floor(width * ratio),
|
|
191
|
+
height: Math.floor(height * ratio),
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
// Creates frame buffers for effects
|
|
195
|
+
const finalRenderTarget = new ThinRenderTargetTexture(engine, size, setup);
|
|
196
|
+
runtime.registerResource(finalRenderTarget);
|
|
197
|
+
|
|
198
|
+
// Babylon Constants.TEXTURE_CLAMP_ADDRESSMODE; NPOT Friendly
|
|
199
|
+
finalRenderTarget.wrapU = 0;
|
|
200
|
+
finalRenderTarget.wrapV = 0;
|
|
201
|
+
|
|
202
|
+
return finalRenderTarget;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import type { EffectRenderer } from "@babylonjs/core/Materials/effectRenderer";
|
|
2
|
+
import type { RenderTargetWrapper } from "@babylonjs/core/Engines/renderTargetWrapper";
|
|
3
|
+
import type { AbstractEngine } from "@babylonjs/core/Engines/abstractEngine";
|
|
4
|
+
import type { Effect } from "@babylonjs/core/Materials/effect";
|
|
5
|
+
import { EffectWrapper } from "@babylonjs/core/Materials/effectRenderer.js";
|
|
6
|
+
|
|
7
|
+
import type { IDisposable } from "../IDisposable";
|
|
8
|
+
import type { ShaderProgram } from "../utils/shaderCodeUtils";
|
|
9
|
+
import { createStrongRef, type StrongRef } from "./strongRef.js";
|
|
10
|
+
import type { IDisableableBlock } from "../blocks/disableableBlock";
|
|
11
|
+
import { decorateSymbol, getShaderCreateOptions } from "../utils/shaderCodeUtils.js";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The shader bindings for a ShaderBlock that can't be disabled.
|
|
15
|
+
*/
|
|
16
|
+
export abstract class Binding {
|
|
17
|
+
/**
|
|
18
|
+
* Binds all the required data to the shader when rendering.
|
|
19
|
+
* @param effect - defines the effect to bind the data to
|
|
20
|
+
* @param width - defines the width of the output
|
|
21
|
+
* @param height - defines the height of the output
|
|
22
|
+
*/
|
|
23
|
+
public abstract bind(effect: Effect, width?: number, height?: number): void;
|
|
24
|
+
|
|
25
|
+
private _remappedShaderVariables: { [key: string]: string } = {};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Gets the remapped shader variable name.
|
|
29
|
+
* @param variableName - The variable name
|
|
30
|
+
* @returns The remapped variable name
|
|
31
|
+
*/
|
|
32
|
+
public getRemappedName(variableName: string) {
|
|
33
|
+
variableName = decorateSymbol(variableName);
|
|
34
|
+
return this._remappedShaderVariables[variableName] ?? variableName;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Sets the remapped shader variables.
|
|
39
|
+
* @param variableName - defines the variable name to remap
|
|
40
|
+
* @param remappedName - defines the remapped name
|
|
41
|
+
*/
|
|
42
|
+
public addShaderVariableRemapping(variableName: string, remappedName: string) {
|
|
43
|
+
this._remappedShaderVariables[variableName] = remappedName;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The shader bindings for a disableable ShaderBlock.
|
|
49
|
+
*/
|
|
50
|
+
export abstract class ShaderBinding extends Binding {
|
|
51
|
+
private _disabled: StrongRef<boolean>;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Construct a ShaderBinding instance.
|
|
55
|
+
* @param parentBlock - The parent block
|
|
56
|
+
*/
|
|
57
|
+
constructor(parentBlock: IDisableableBlock) {
|
|
58
|
+
super();
|
|
59
|
+
this._disabled = parentBlock.disabled?.runtimeData || createStrongRef(false);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Binds all the required data to the shader when rendering.
|
|
64
|
+
* @param effect - defines the effect to bind the data to
|
|
65
|
+
* @param _width - defines the width of the output
|
|
66
|
+
* @param _height - defines the height of the output
|
|
67
|
+
*/
|
|
68
|
+
public bind(effect: Effect, _width?: number, _height?: number): void {
|
|
69
|
+
effect.setBool(this.getRemappedName("disabled"), this._disabled.value);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The shader runtime is the base for any runtime associated with a @see ShaderBlock.
|
|
75
|
+
*
|
|
76
|
+
* It encapsulates the basic needs to render a full screen effect mainly the effect wrapper holding on the shader program.
|
|
77
|
+
*
|
|
78
|
+
* It is able to either render to a texture or directly to the main canvas.
|
|
79
|
+
*
|
|
80
|
+
* It also manages the disposal of the effect wrapper.
|
|
81
|
+
*/
|
|
82
|
+
export class ShaderRuntime implements IDisposable {
|
|
83
|
+
/**
|
|
84
|
+
* Promise that resolves when the effect is ready to be used.
|
|
85
|
+
*/
|
|
86
|
+
public readonly onReadyAsync: Promise<void>;
|
|
87
|
+
|
|
88
|
+
private readonly _engine: AbstractEngine;
|
|
89
|
+
private readonly _effectRenderer: EffectRenderer;
|
|
90
|
+
private readonly _effectWrapper: EffectWrapper;
|
|
91
|
+
private readonly _shaderBinding: Binding;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Creates a new @see ShaderRuntime.
|
|
95
|
+
* @param effectRenderer - defines the effect renderer to use to render the full screen effect
|
|
96
|
+
* @param shaderProgram - defines the shader code associated with this runtime
|
|
97
|
+
* @param shaderBinding - defines the shader bindings associated with this runtime
|
|
98
|
+
*/
|
|
99
|
+
constructor(effectRenderer: EffectRenderer, shaderProgram: ShaderProgram, shaderBinding: Binding) {
|
|
100
|
+
this._engine = effectRenderer.engine;
|
|
101
|
+
this._effectRenderer = effectRenderer;
|
|
102
|
+
this._shaderBinding = shaderBinding;
|
|
103
|
+
this._effectWrapper = new EffectWrapper({
|
|
104
|
+
engine: this._engine,
|
|
105
|
+
...getShaderCreateOptions(shaderProgram),
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// Wraps the effect readiness in a promise to expose it as a public property.
|
|
109
|
+
this.onReadyAsync = new Promise<void>((resolve, reject) => {
|
|
110
|
+
this._effectWrapper.effect.executeWhenCompiled(() => {
|
|
111
|
+
resolve();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
this._effectWrapper.effect.onErrorObservable.addOnce((error) => {
|
|
115
|
+
reject(error);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Renders the full screen effect into a texture.
|
|
122
|
+
* @param renderTargetWrapper - The render target wrapper to render into
|
|
123
|
+
*/
|
|
124
|
+
public renderToTexture(renderTargetWrapper: RenderTargetWrapper): void {
|
|
125
|
+
this._engine.bindFramebuffer(renderTargetWrapper);
|
|
126
|
+
this._draw(renderTargetWrapper.width, renderTargetWrapper.height);
|
|
127
|
+
this._engine.unBindFramebuffer(renderTargetWrapper);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Renders the full screen effect into the main canvas.
|
|
132
|
+
*/
|
|
133
|
+
public renderToCanvas(): void {
|
|
134
|
+
this._effectRenderer.setViewport();
|
|
135
|
+
this._draw(this._engine.getRenderWidth(), this._engine.getRenderHeight());
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* "Draws" the full screen effect into the currently bound output.
|
|
140
|
+
* @param width - defines the width of the output
|
|
141
|
+
* @param height - defines the height of the output
|
|
142
|
+
*/
|
|
143
|
+
private _draw(width: number, height: number): void {
|
|
144
|
+
this._effectRenderer.applyEffectWrapper(this._effectWrapper);
|
|
145
|
+
this._shaderBinding.bind(this._effectWrapper.effect, width, height);
|
|
146
|
+
this._effectRenderer.draw();
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Disposes the runtime resources.
|
|
151
|
+
*/
|
|
152
|
+
public dispose(): void {
|
|
153
|
+
this._effectWrapper.dispose();
|
|
154
|
+
}
|
|
155
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { EffectRenderer } from "@babylonjs/core/Materials/effectRenderer.js";
|
|
2
|
+
import type { ThinEngine } from "@babylonjs/core/Engines/thinEngine";
|
|
3
|
+
import { CommandBuffer } from "../command/commandBuffer.js";
|
|
4
|
+
import type { IDisposable } from "../IDisposable";
|
|
5
|
+
import type { Command } from "../command/command";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A runtime is a snapshot of a smart filter containing all the
|
|
9
|
+
* required data to render it as well as the entire command buffer.
|
|
10
|
+
*/
|
|
11
|
+
export type SmartFilterRuntime = {
|
|
12
|
+
/**
|
|
13
|
+
* The command buffer containing all the commands to execute during a frame.
|
|
14
|
+
*/
|
|
15
|
+
readonly commandBuffer: Readonly<CommandBuffer>;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Renders one frame of the smart filter.
|
|
19
|
+
*/
|
|
20
|
+
render(): void;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Dispose the runtime and all its associated resources
|
|
24
|
+
*/
|
|
25
|
+
dispose(): void;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The internal runtime implementation exposing more information than @see SmartFilterRuntime.
|
|
30
|
+
* This is used internally to render the smart filter.
|
|
31
|
+
*
|
|
32
|
+
* It is not fully exposed publicly to prevent any misuse of the runtime.
|
|
33
|
+
*/
|
|
34
|
+
export class InternalSmartFilterRuntime implements SmartFilterRuntime {
|
|
35
|
+
/**
|
|
36
|
+
* The engine used by the smart filter.
|
|
37
|
+
*/
|
|
38
|
+
public readonly engine: ThinEngine;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The effect renderer used by the smart filter.
|
|
42
|
+
*/
|
|
43
|
+
public readonly effectRenderer: EffectRenderer;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The command buffer containing all the commands to execute during a frame.
|
|
47
|
+
*/
|
|
48
|
+
public readonly commandBuffer: CommandBuffer;
|
|
49
|
+
|
|
50
|
+
private readonly _resources: IDisposable[];
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Instantiates a new smart filter runtime for one given engine.
|
|
54
|
+
* @param engine - the engine to use to render the smart filter
|
|
55
|
+
*/
|
|
56
|
+
constructor(engine: ThinEngine) {
|
|
57
|
+
this.engine = engine;
|
|
58
|
+
|
|
59
|
+
this._resources = [];
|
|
60
|
+
|
|
61
|
+
this.commandBuffer = new CommandBuffer();
|
|
62
|
+
|
|
63
|
+
this.effectRenderer = new EffectRenderer(engine);
|
|
64
|
+
this.registerResource(this.effectRenderer);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Reference a resource to be disposed when the runtime is disposed.
|
|
69
|
+
* @param resource - defines the resource to dispose once the runtime is disposed
|
|
70
|
+
*/
|
|
71
|
+
public registerResource(resource: IDisposable): void {
|
|
72
|
+
this._resources.push(resource);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Registers a command to be executed during the render loop.
|
|
77
|
+
* @param command - defines the command to execute
|
|
78
|
+
*/
|
|
79
|
+
public registerCommand(command: Command): void {
|
|
80
|
+
this.commandBuffer.push(command);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Renders the smart filter.
|
|
85
|
+
* This function will execute all the commands contained int the command buffer.
|
|
86
|
+
*/
|
|
87
|
+
public render(): void {
|
|
88
|
+
try {
|
|
89
|
+
this.commandBuffer.execute();
|
|
90
|
+
} catch (e) {
|
|
91
|
+
// eslint-disable-next-line no-debugger
|
|
92
|
+
debugger;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Dispose the runtime and all its associated resources
|
|
98
|
+
*/
|
|
99
|
+
public dispose(): void {
|
|
100
|
+
for (const resource of this._resources) {
|
|
101
|
+
resource.dispose();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This describes a strong reference to any data type.
|
|
3
|
+
*/
|
|
4
|
+
export type StrongRef<T> = {
|
|
5
|
+
/**
|
|
6
|
+
* The value of the strong reference.
|
|
7
|
+
*/
|
|
8
|
+
value: T;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Create a strong reference to the given value.
|
|
13
|
+
* @param value - the value to wrap in a strong reference
|
|
14
|
+
* @returns the strong reference containing the value
|
|
15
|
+
*/
|
|
16
|
+
export function createStrongRef<T>(value: T): StrongRef<T> {
|
|
17
|
+
return { value };
|
|
18
|
+
}
|