Skip to content

Commit e19bfa3

Browse files
committed
Fix potential depth texture type mismatch
Closes #745
1 parent 4b22b8a commit e19bfa3

1 file changed

Lines changed: 70 additions & 93 deletions

File tree

‎src/core/EffectComposer.js‎

Lines changed: 70 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,6 @@ export class EffectComposer {
8686

8787
this.copyPass = new CopyPass();
8888

89-
/**
90-
* A depth texture.
91-
*
92-
* @type {DepthTexture}
93-
* @private
94-
*/
95-
96-
this.depthTexture = null;
97-
9889
/**
9990
* A render target that holds a stable copy of the scene depth.
10091
*
@@ -137,6 +128,19 @@ export class EffectComposer {
137128

138129
}
139130

131+
/**
132+
* A stable depth texture to be used by depth-aware passes.
133+
*
134+
* @type {DepthTexture}
135+
* @private
136+
*/
137+
138+
get stableDepthTexture() {
139+
140+
return this.depthRenderTarget?.depthTexture ?? null;
141+
142+
}
143+
140144
/**
141145
* The current amount of samples used for multisample anti-aliasing.
142146
*
@@ -159,33 +163,17 @@ export class EffectComposer {
159163

160164
set multisampling(value) {
161165

162-
const buffer = this.inputBuffer;
163-
const multisampling = this.multisampling;
164-
165-
if(multisampling > 0 && value > 0) {
166-
167-
this.inputBuffer.samples = value;
168-
this.outputBuffer.samples = value;
169-
this.inputBuffer.dispose();
170-
this.outputBuffer.dispose();
171-
172-
} else if(multisampling !== value) {
166+
if(this.multisampling === value) {
173167

174-
this.inputBuffer.dispose();
175-
this.outputBuffer.dispose();
176-
177-
// Enable or disable MSAA.
178-
this.inputBuffer = this.createBuffer(
179-
buffer.depthBuffer,
180-
buffer.stencilBuffer,
181-
buffer.texture.type,
182-
value
183-
);
184-
185-
this.outputBuffer = this.inputBuffer.clone();
168+
return;
186169

187170
}
188171

172+
this.inputBuffer.samples = value;
173+
this.outputBuffer.samples = value;
174+
this.inputBuffer.dispose();
175+
this.outputBuffer.dispose();
176+
189177
}
190178

191179
/**
@@ -288,42 +276,50 @@ export class EffectComposer {
288276
* Creates a depth texture attachment that will be provided to all passes.
289277
*
290278
* To prevent errors or incorrect behavior when the same depth buffer is attached to the input and output buffers,
291-
* a separate stable depth target is created alongside the ping-pong buffers. All passes receive the stable target's
279+
* a separate stable depth target is created alongside the ping-pong buffers. All passes receive the stable target's
292280
* depth texture, which is never used as a render output and therefore cannot create a feedback loop. The stable
293281
* texture is populated each frame via blitFramebuffer immediately before the first buffer swap.
294282
*
295283
* @private
296-
* @return {DepthTexture} The stable depth texture distributed to passes.
297284
*/
298285

299286
createDepthTexture() {
300287

301-
const inputBuffer = this.inputBuffer;
302-
const depthTexture = new DepthTexture();
303-
this.depthTexture = depthTexture;
288+
const inputDepthTexture = new DepthTexture();
289+
inputDepthTexture.name = "EffectComposer.InputDepth";
304290

305-
if(inputBuffer.stencilBuffer) {
291+
if(this.inputBuffer.stencilBuffer) {
306292

307-
depthTexture.format = DepthStencilFormat;
308-
depthTexture.type = UnsignedInt248Type;
293+
inputDepthTexture.format = DepthStencilFormat;
294+
inputDepthTexture.type = UnsignedInt248Type;
309295

310296
} else {
311297

312-
depthTexture.type = FloatType;
298+
inputDepthTexture.type = FloatType;
313299

314300
}
315301

316-
const stableDepthTexture = depthTexture.clone();
302+
const outputDepthTexture = inputDepthTexture.clone();
303+
outputDepthTexture.name = "EffectComposer.OutputDepth";
304+
305+
const stableDepthTexture = inputDepthTexture.clone();
317306
stableDepthTexture.name = "EffectComposer.StableDepth";
318307

319-
this.depthRenderTarget = new WebGLRenderTarget(inputBuffer.width, inputBuffer.height, {
308+
this.inputBuffer.depthTexture = inputDepthTexture;
309+
this.outputBuffer.depthTexture = outputDepthTexture;
310+
311+
// Force rebuild renderbuffers.
312+
this.inputBuffer.dispose();
313+
this.outputBuffer.dispose();
314+
315+
const { width, height } = this.inputBuffer;
316+
317+
this.depthRenderTarget = new WebGLRenderTarget(width, height, {
320318
depthBuffer: true,
321-
stencilBuffer: inputBuffer.stencilBuffer,
319+
stencilBuffer: this.inputBuffer.stencilBuffer,
322320
depthTexture: stableDepthTexture
323321
});
324322

325-
return stableDepthTexture;
326-
327323
}
328324

329325
/**
@@ -375,24 +371,28 @@ export class EffectComposer {
375371

376372
deleteDepthTexture() {
377373

378-
if(this.depthTexture !== null) {
379-
380-
this.depthTexture.dispose();
381-
this.depthTexture = null;
382-
this.depthRenderTarget.dispose();
383-
this.depthRenderTarget = null;
374+
const stableDepthTexture = this.stableDepthTexture;
384375

385-
this.inputBuffer.depthTexture = null;
386-
this.outputBuffer.depthTexture = null;
376+
for(const pass of this.passes) {
387377

388-
for(const pass of this.passes) {
378+
// Don't remove foreign depth textures.
379+
if(pass.getDepthTexture() === stableDepthTexture) {
389380

390381
pass.setDepthTexture(null);
391382

392383
}
393384

394385
}
395386

387+
this.depthRenderTarget?.dispose();
388+
this.depthRenderTarget = null;
389+
390+
this.inputBuffer.depthTexture?.dispose();
391+
this.inputBuffer.depthTexture = null;
392+
393+
this.outputBuffer.depthTexture?.dispose();
394+
this.outputBuffer.depthTexture = null;
395+
396396
}
397397

398398
/**
@@ -411,21 +411,14 @@ export class EffectComposer {
411411
const renderer = this.renderer;
412412
const size = (renderer === null) ? new Vector2() : renderer.getDrawingBufferSize(new Vector2());
413413

414-
const options = {
414+
const renderTarget = new WebGLRenderTarget(size.width, size.height, {
415415
minFilter: LinearFilter,
416416
magFilter: LinearFilter,
417+
samples: multisampling,
417418
stencilBuffer,
418419
depthBuffer,
419420
type
420-
};
421-
422-
const renderTarget = new WebGLRenderTarget(size.width, size.height, options);
423-
424-
if(multisampling > 0) {
425-
426-
renderTarget.samples = multisampling;
427-
428-
}
421+
});
429422

430423
if(type === UnsignedByteType && renderer !== null && renderer.outputColorSpace === SRGBColorSpace) {
431424

@@ -524,22 +517,21 @@ export class EffectComposer {
524517

525518
}
526519

527-
if(pass.needsDepthTexture || this.depthTexture !== null) {
520+
if(pass.needsDepthTexture || this.depthRenderTarget !== null) {
528521

529-
if(this.depthTexture === null) {
522+
if(this.depthRenderTarget === null) {
530523

531-
const stableDepthTexture = this.createDepthTexture();
524+
this.createDepthTexture();
532525

533-
for(pass of passes) {
526+
for(const existingPass of passes) {
534527

535-
pass.setDepthTexture(stableDepthTexture);
528+
existingPass.setDepthTexture(this.stableDepthTexture);
536529

537530
}
538531

539532
} else {
540533

541-
const stableDepthTexture = this.depthRenderTarget.depthTexture;
542-
pass.setDepthTexture(stableDepthTexture);
534+
pass.setDepthTexture(this.stableDepthTexture);
543535

544536
}
545537

@@ -562,16 +554,16 @@ export class EffectComposer {
562554

563555
if(removed) {
564556

565-
if(this.depthTexture !== null) {
557+
const stableDepthTexture = this.stableDepthTexture;
558+
559+
if(stableDepthTexture !== null) {
566560

567561
// Check if the depth texture is still required.
568562
const reducer = (a, b) => (a || b.needsDepthTexture);
569563
const depthTextureRequired = passes.reduce(reducer, false);
570564

571565
if(!depthTextureRequired) {
572566

573-
const stableDepthTexture = this.depthRenderTarget.depthTexture;
574-
575567
// Don't remove foreign depth textures.
576568
if(pass.getDepthTexture() === stableDepthTexture) {
577569

@@ -662,10 +654,6 @@ export class EffectComposer {
662654

663655
}
664656

665-
// Setup the depth texture (RenderPass renders into the inputBuffer).
666-
inputBuffer.depthTexture = this.depthTexture;
667-
outputBuffer.depthTexture = null;
668-
669657
pass.render(renderer, inputBuffer, outputBuffer, deltaTime, stencilTest);
670658

671659
if(pass.needsDepthBlit) {
@@ -783,23 +771,12 @@ export class EffectComposer {
783771

784772
}
785773

786-
this.passes = [];
787-
788-
if(this.inputBuffer !== null) {
789-
790-
this.inputBuffer.dispose();
791-
792-
}
793-
794-
if(this.outputBuffer !== null) {
795-
796-
this.outputBuffer.dispose();
797-
798-
}
799-
800774
this.deleteDepthTexture();
775+
this.inputBuffer.dispose();
776+
this.outputBuffer.dispose();
801777
this.copyPass.dispose();
802778
this.timer.dispose();
779+
this.passes = [];
803780

804781
Pass.fullscreenGeometry.dispose();
805782

0 commit comments

Comments
 (0)