Skip to content

Commit 584197b

Browse files
committed
Move syncDefaultBufferType to Pass
1 parent 33c5bd2 commit 584197b

2 files changed

Lines changed: 86 additions & 41 deletions

File tree

src/core/Pass.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ export abstract class Pass<TMaterial extends Material | null = null>
148148

149149
private _attached: boolean;
150150

151+
/**
152+
* @see {@link autoSyncDefaultBuffers}
153+
*/
154+
155+
private _autoSyncDefaultBuffers: boolean;
156+
151157
/**
152158
* @see {@link timer}
153159
*/
@@ -251,6 +257,7 @@ export abstract class Pass<TMaterial extends Material | null = null>
251257
this._name = name;
252258
this._enabled = true;
253259
this._attached = false;
260+
this._autoSyncDefaultBuffers = true;
254261
this._renderer = null;
255262
this._timer = null;
256263
this._scene = null;
@@ -339,6 +346,29 @@ export abstract class Pass<TMaterial extends Material | null = null>
339346

340347
}
341348

349+
/**
350+
* Controls whether the settings of the input and output default buffers should be synchronized.
351+
*
352+
* @defaultValue true
353+
*/
354+
355+
protected get autoSyncDefaultBuffers(): boolean {
356+
357+
return this._autoSyncDefaultBuffers;
358+
359+
}
360+
361+
protected set autoSyncDefaultBuffers(value: boolean) {
362+
363+
if(this._autoSyncDefaultBuffers !== value) {
364+
365+
this._autoSyncDefaultBuffers = value;
366+
this.syncDefaultBuffers();
367+
368+
}
369+
370+
}
371+
342372
/**
343373
* A list of subpasses.
344374
*
@@ -729,6 +759,59 @@ export abstract class Pass<TMaterial extends Material | null = null>
729759

730760
}
731761

762+
/**
763+
* Synchronizes the texture settings of the input and output default buffers.
764+
*
765+
* This method ensures that the output buffer uses adequate settings for storing values from the input buffer.
766+
*/
767+
768+
private syncDefaultBuffers(): void {
769+
770+
const renderer = this.renderer;
771+
const inputBuffer = this.input.defaultBuffer?.value ?? null;
772+
const outputBuffer = this.output.defaultBuffer?.value ?? null;
773+
774+
if(!this.autoSyncDefaultBuffers || renderer === null || inputBuffer === null || outputBuffer === null) {
775+
776+
return;
777+
778+
}
779+
780+
const texture = outputBuffer.texture;
781+
782+
texture.needsUpdate = (
783+
texture.format !== inputBuffer.format ||
784+
texture.internalFormat !== inputBuffer.internalFormat ||
785+
texture.type !== inputBuffer.type
786+
);
787+
788+
if(texture.needsUpdate) {
789+
790+
texture.format = inputBuffer.format;
791+
texture.internalFormat = inputBuffer.internalFormat;
792+
texture.type = inputBuffer.type;
793+
794+
}
795+
796+
// If the output buffer uses low precision, enable sRGB encoding to reduce information loss.
797+
const useSRGBFramebuffer = !this.output.frameBufferPrecisionHigh && renderer.outputColorSpace === SRGBColorSpace;
798+
799+
if(useSRGBFramebuffer && texture.colorSpace !== SRGBColorSpace) {
800+
801+
texture.colorSpace = SRGBColorSpace;
802+
texture.needsUpdate = true;
803+
804+
}
805+
806+
if(texture.needsUpdate) {
807+
808+
// Notify listeners.
809+
this.output.defaultBuffer!.texture.setChanged();
810+
811+
}
812+
813+
}
814+
732815
/**
733816
* Checks if this pass uses convolution shaders.
734817
*
@@ -1133,6 +1216,7 @@ export abstract class Pass<TMaterial extends Material | null = null>
11331216

11341217
case "change":
11351218
this.updateFullscreenMaterialsInput();
1219+
this.syncDefaultBuffers();
11361220
this.onInputChange();
11371221
break;
11381222

@@ -1159,6 +1243,7 @@ export abstract class Pass<TMaterial extends Material | null = null>
11591243
case "change":
11601244
this.updateOutputBufferSize();
11611245
this.updateFullscreenMaterialsOutput();
1246+
this.syncDefaultBuffers();
11621247
this.onOutputChange();
11631248
break;
11641249

src/core/io/IOManager.ts

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Material, SRGBColorSpace, WebGLRenderTarget } from "three";
1+
import { Material, WebGLRenderTarget } from "three";
22
import { GBuffer } from "../../enums/GBuffer.js";
33
import { ClearPass } from "../../passes/ClearPass.js";
44
import { GeometryPass } from "../../passes/GeometryPass.js";
@@ -247,8 +247,6 @@ export class IOManager {
247247
this.updateOutput(pipeline);
248248
this.updateInput(pipeline);
249249

250-
IOManager.syncDefaultBufferType(pipeline);
251-
252250
}
253251

254252
/**
@@ -276,44 +274,6 @@ export class IOManager {
276274

277275
}
278276

279-
/**
280-
* Synchronizes the texture type of input/output default buffers.
281-
*
282-
* @param pipeline - The pipeline to update.
283-
*/
284-
285-
private static syncDefaultBufferType(pipeline: RenderPipeline): void {
286-
287-
for(const pass of pipeline.passes.filter(x => x.enabled)) {
288-
289-
const inputBuffer = pass.input.defaultBuffer?.value ?? null;
290-
const outputBuffer = pass.output.defaultBuffer?.value ?? null;
291-
292-
if(inputBuffer === null || outputBuffer === null || pass instanceof GeometryPass) {
293-
294-
continue;
295-
296-
}
297-
298-
outputBuffer.texture.format = inputBuffer.format;
299-
outputBuffer.texture.internalFormat = inputBuffer.internalFormat;
300-
outputBuffer.texture.type = inputBuffer.type;
301-
outputBuffer.texture.needsUpdate = true;
302-
303-
if(!pass.input.frameBufferPrecisionHigh && pipeline.renderer?.outputColorSpace === SRGBColorSpace) {
304-
305-
// If the output buffer uses low precision, enable sRGB encoding to reduce information loss.
306-
outputBuffer.texture.colorSpace = SRGBColorSpace;
307-
308-
}
309-
310-
// Notify listeners that this texture has changed.
311-
pass.output.defaultBuffer!.texture.setChanged();
312-
313-
}
314-
315-
}
316-
317277
/**
318278
* Returns the main geometry pass of the given pipeline.
319279
*

0 commit comments

Comments
 (0)