Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable effect to be persistent #16080

Merged
merged 5 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/dev/core/src/Engines/abstractEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2554,6 +2554,11 @@ export abstract class AbstractEngine {
*/
public readonly onDisposeObservable = new Observable<AbstractEngine>();

/**
* An event triggered when a global cleanup of all effects is required
*/
public readonly onReleaseEffectsObservable = new Observable<AbstractEngine>();

/**
* Dispose and release all associated resources
*/
Expand Down
7 changes: 1 addition & 6 deletions packages/dev/core/src/Engines/thinEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4041,13 +4041,8 @@ export class ThinEngine extends AbstractEngine {
* Force the engine to release all cached effects. This means that next effect compilation will have to be done completely even if a similar effect was already compiled
*/
public releaseEffects() {
const keys = Object.keys(this._compiledEffects);
for (const name of keys) {
const effect = this._compiledEffects[name];
effect.dispose(true);
}

this._compiledEffects = {};
this.onReleaseEffectsObservable.notifyObservers(this);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/dev/core/src/Engines/webgpuEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2202,6 +2202,8 @@ export class WebGPUEngine extends ThinWebGPUEngine {
}

this._compiledEffects = {};

this.onReleaseEffectsObservable.notifyObservers(this);
}

public _deletePipelineContext(pipelineContext: IPipelineContext): void {
Expand Down
17 changes: 17 additions & 0 deletions packages/dev/core/src/Materials/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ export class Effect implements IDisposable {
*/
public static LogShaderCodeOnCompilationError = true;

/**
* Gets or sets a boolean indicating that effect ref counting is disabled
* If true, the effect will persist in memory until engine is disposed
*/
public static PersistentMode: boolean = false;

/**
* Use this with caution
* See ClearCodeCache function comments
Expand Down Expand Up @@ -384,6 +390,14 @@ export class Effect implements IDisposable {
(this._pipelineContext as any).program.__SPECTOR_rebuildProgram = this._rebuildProgram.bind(this);
}
}

this._engine.onReleaseEffectsObservable.addOnce(() => {
if (this.isDisposed) {
return;
}

this.dispose(true);
});
}

/** @internal */
Expand Down Expand Up @@ -1485,6 +1499,9 @@ export class Effect implements IDisposable {
if (force) {
this._refCount = 0;
} else {
if (Effect.PersistentMode) {
return;
}
this._refCount--;
}

Expand Down
5 changes: 2 additions & 3 deletions packages/dev/core/src/Particles/webgl2ParticleSystem.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { VertexBuffer, Buffer } from "../Buffers/buffer";
import type { ThinEngine } from "../Engines/thinEngine";
import type { IEffectCreationOptions } from "../Materials/effect";
import { Effect } from "../Materials/effect";
import type { Effect, IEffectCreationOptions } from "../Materials/effect";
import type { IGPUParticleSystemPlatform } from "./IGPUParticleSystemPlatform";

import { CustomParticleEmitter } from "./EmitterTypes/customParticleEmitter";
Expand Down Expand Up @@ -146,7 +145,7 @@ export class WebGL2ParticleSystem implements IGPUParticleSystemPlatform {
}

this._updateEffectOptions.defines = defines;
this._updateEffect = new Effect("gpuUpdateParticles", this._updateEffectOptions, this._engine);
this._updateEffect = this._engine.createEffect("gpuUpdateParticles", this._updateEffectOptions, this._engine);

return new UniformBufferEffectCommonAccessor(this._updateEffect);
}
Expand Down