Skip to content

Commit 629fb8d

Browse files
committed
Fix shader data invalidation bug
Addresses #689 (comment)
1 parent eee5e30 commit 629fb8d

2 files changed

Lines changed: 100 additions & 16 deletions

File tree

‎src/passes/EffectPass.ts‎

Lines changed: 86 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { WebGLRenderer, Material, Texture } from "three";
1+
import { Event as Event3, WebGLRenderer, Material, Texture } from "three";
22
import { Pass } from "../core/Pass.js";
33
import { Effect } from "../effects/Effect.js";
44
import { GBuffer } from "../enums/GBuffer.js";
@@ -23,23 +23,31 @@ export class EffectPass extends Pass<EffectMaterial> {
2323
private readonly effectMaterialManager: EffectMaterialManager;
2424

2525
/**
26-
* An event listener that calls {@link updateMaterial} and invalidates the material cache.
26+
* An event listener that calls {@link handleEffectEvent}.
2727
*/
2828

29-
private readonly effectChangeListener: () => void;
29+
private readonly effectListener: (e: Event3) => void;
3030

3131
/**
32-
* An event listener that calls {@link updateMaterial} without invalidating the material cache.
32+
* An event listener that calls {@link handleToggleEvent}.
3333
*/
3434

35-
private readonly effectToggleListener: () => void;
35+
private readonly gBufferConfigListener: (e: Event3) => void;
3636

3737
/**
3838
* Keeps track of the previous G-Buffer configuration.
3939
*/
4040

4141
private previousGBufferConfig: GBufferConfig | null;
4242

43+
/**
44+
* Indicates whether this pass has been disposed.
45+
*
46+
* If true, resources will be refreshed before the next render operation.
47+
*/
48+
49+
private disposed: boolean;
50+
4351
/**
4452
* An animation time scale.
4553
*/
@@ -58,10 +66,11 @@ export class EffectPass extends Pass<EffectMaterial> {
5866

5967
this.output.defaultBuffer = this.createFramebuffer();
6068
this.effectMaterialManager = new EffectMaterialManager(this.input);
61-
this.effectChangeListener = () => this.updateMaterial(true);
62-
this.effectToggleListener = () => this.updateMaterial(false);
69+
this.effectListener = (e: Event3) => this.handleEffectEvent(e);
70+
this.gBufferConfigListener = (e: Event3) => this.handleGBufferConfigEvent(e);
6371
this.previousGBufferConfig = null;
6472
this.effects = effects;
73+
this.disposed = false;
6574
this.timeScale = 1.0;
6675

6776
}
@@ -76,8 +85,8 @@ export class EffectPass extends Pass<EffectMaterial> {
7685

7786
for(const effect of super.subpasses) {
7887

79-
effect.removeEventListener(Pass.EVENT_CHANGE, this.effectChangeListener);
80-
effect.removeEventListener(Pass.EVENT_TOGGLE, this.effectToggleListener);
88+
effect.removeEventListener(Pass.EVENT_CHANGE, this.effectListener);
89+
effect.removeEventListener(Pass.EVENT_TOGGLE, this.effectListener);
8190

8291
}
8392

@@ -92,12 +101,13 @@ export class EffectPass extends Pass<EffectMaterial> {
92101

93102
}
94103

95-
effect.addEventListener(Pass.EVENT_CHANGE, this.effectChangeListener);
96-
effect.addEventListener(Pass.EVENT_TOGGLE, this.effectToggleListener);
104+
effect.addEventListener(Pass.EVENT_CHANGE, this.effectListener);
105+
effect.addEventListener(Pass.EVENT_TOGGLE, this.effectListener);
97106

98107
}
99108

100109
this.updateMaterial(true);
110+
this.disposed = false;
101111

102112
}
103113

@@ -213,6 +223,51 @@ export class EffectPass extends Pass<EffectMaterial> {
213223

214224
}
215225

226+
// #region Event Handlers
227+
228+
/**
229+
* Handles {@link Effect} events.
230+
*
231+
* @param event - An event.
232+
*/
233+
234+
private handleEffectEvent(e: Event3): void {
235+
236+
switch(e.type) {
237+
238+
case Pass.EVENT_CHANGE:
239+
this.effectMaterialManager.invalidateShaderData(e.target as Effect);
240+
this.updateMaterial(true);
241+
break;
242+
243+
case Pass.EVENT_TOGGLE:
244+
this.updateMaterial(false);
245+
break;
246+
247+
}
248+
249+
}
250+
251+
/**
252+
* Handles {@link GBufferConfig} events.
253+
*
254+
* @param event - An event.
255+
*/
256+
257+
private handleGBufferConfigEvent(e: Event3): void {
258+
259+
switch(e.type) {
260+
261+
case GBufferConfig.EVENT_CHANGE:
262+
this.updateMaterial(true);
263+
break;
264+
265+
}
266+
267+
}
268+
269+
// #endregion
270+
216271
protected override onInputChange(): void {
217272

218273
this.updateGBufferStruct();
@@ -237,13 +292,13 @@ export class EffectPass extends Pass<EffectMaterial> {
237292

238293
if(this.previousGBufferConfig !== null) {
239294

240-
this.previousGBufferConfig.removeEventListener(GBufferConfig.EVENT_CHANGE, this.effectChangeListener);
295+
this.previousGBufferConfig.removeEventListener(GBufferConfig.EVENT_CHANGE, this.gBufferConfigListener);
241296

242297
}
243298

244299
if(this.input.gBufferConfig !== null) {
245300

246-
this.input.gBufferConfig.addEventListener(GBufferConfig.EVENT_CHANGE, this.effectChangeListener);
301+
this.input.gBufferConfig.addEventListener(GBufferConfig.EVENT_CHANGE, this.gBufferConfigListener);
247302

248303
}
249304

@@ -280,6 +335,7 @@ export class EffectPass extends Pass<EffectMaterial> {
280335

281336
override async compile(): Promise<void> {
282337

338+
// Make sure all materials are created prior to compilation.
283339
this.updateMaterial(false);
284340
return super.compile();
285341

@@ -289,14 +345,22 @@ export class EffectPass extends Pass<EffectMaterial> {
289345

290346
for(const effect of this.effects) {
291347

292-
effect.removeEventListener(Pass.EVENT_CHANGE, this.effectChangeListener);
293-
effect.removeEventListener(Pass.EVENT_TOGGLE, this.effectToggleListener);
348+
effect.removeEventListener(Pass.EVENT_CHANGE, this.effectListener);
349+
effect.removeEventListener(Pass.EVENT_TOGGLE, this.effectListener);
350+
351+
}
352+
353+
if(this.input.gBufferConfig !== null) {
354+
355+
this.input.gBufferConfig.removeEventListener(GBufferConfig.EVENT_CHANGE, this.gBufferConfigListener);
294356

295357
}
296358

297359
this.effectMaterialManager.dispose();
298360
super.dispose();
299361

362+
this.disposed = true;
363+
300364
}
301365

302366
override render(): void {
@@ -307,6 +371,13 @@ export class EffectPass extends Pass<EffectMaterial> {
307371

308372
}
309373

374+
if(this.disposed) {
375+
376+
// Restore resources and event listeners.
377+
this.effects = [...this.effects];
378+
379+
}
380+
310381
for(const effect of this.effects) {
311382

312383
if(effect.enabled) {

‎src/utils/EffectMaterialManager.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ export class EffectMaterialManager implements Disposable {
123123

124124
if(this._gBufferConfig !== value) {
125125

126-
this.dispose();
126+
this.invalidateMaterialCache();
127+
this.effectShaderDataCache.clear();
127128
this._gBufferConfig = value;
128129

129130
}
@@ -466,6 +467,18 @@ export class EffectMaterialManager implements Disposable {
466467

467468
}
468469

470+
/**
471+
* Invalidates the shader data associated with the given effect.
472+
*
473+
* @param effect - The effect for which the shader data should be deleted.
474+
*/
475+
476+
invalidateShaderData(effect: Effect): void {
477+
478+
this.effectShaderDataCache.delete(effect);
479+
480+
}
481+
469482
dispose(): void {
470483

471484
this.invalidateMaterialCache();

0 commit comments

Comments
 (0)