Skip to content

Commit 2c90f8b

Browse files
committed
perf: skip temporal accumulation when disabled
- Gate accumulation target clearing: only clear when accumulate=true - Skip accumulation render pass when accumulate=false (default) - Point composite directly to blur output when accumulate disabled - Fix floating point precision tests with toBeCloseTo() Saves 1 render pass + 2 clears per frame (~2M-8M pixel ops)
1 parent 6c52e9b commit 2c90f8b

3 files changed

Lines changed: 37 additions & 19 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
node_modules/
22
dist/
3-
.DS_Store
3+
.DS_Store

src/N8AONode.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,9 @@ export class N8AONode extends TempNode {
702702
} else {
703703
this.frame = 0;
704704
this.needsFrame = false;
705-
this.clearAccumulationTargets(renderer);
705+
if (this.configuration.accumulate) {
706+
this.clearAccumulationTargets(renderer);
707+
}
706708
}
707709

708710
this.lastViewMatrix.copy(this.camera.matrixWorldInverse);
@@ -757,20 +759,24 @@ export class N8AONode extends TempNode {
757759
writeTarget = nextWriteTarget;
758760
}
759761

760-
this.accumulationCurrentTextureNode.value = readTarget.texture;
761-
this.accumulationPreviousTextureNode.value =
762-
this.accumulationTargetA.texture;
763-
renderer.setRenderTarget(this.accumulationTargetB);
764-
this.quadMesh.material = this.accumulationMaterial;
765-
this.quadMesh.name = "N8AO.Accumulation";
766-
this.quadMesh.render(renderer as any);
762+
if (this.configuration.accumulate) {
763+
this.accumulationCurrentTextureNode.value = readTarget.texture;
764+
this.accumulationPreviousTextureNode.value =
765+
this.accumulationTargetA.texture;
766+
renderer.setRenderTarget(this.accumulationTargetB);
767+
this.quadMesh.material = this.accumulationMaterial;
768+
this.quadMesh.name = "N8AO.Accumulation";
769+
this.quadMesh.render(renderer as any);
767770

768-
const previousAccumulationTarget = this.accumulationTargetA;
769-
this.accumulationTargetA = this.accumulationTargetB;
770-
this.accumulationTargetB = previousAccumulationTarget;
771-
this.accumulationPreviousTextureNode.value =
772-
this.accumulationTargetA.texture;
773-
this.compositeAoTextureNode.value = this.accumulationTargetA.texture;
771+
const previousAccumulationTarget = this.accumulationTargetA;
772+
this.accumulationTargetA = this.accumulationTargetB;
773+
this.accumulationTargetB = previousAccumulationTarget;
774+
this.accumulationPreviousTextureNode.value =
775+
this.accumulationTargetA.texture;
776+
this.compositeAoTextureNode.value = this.accumulationTargetA.texture;
777+
} else {
778+
this.compositeAoTextureNode.value = readTarget.texture;
779+
}
774780
}
775781

776782
renderer.setRenderTarget(this.outputTarget);

src/math.test.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ import {
77
resolveDisplayMode,
88
} from "./math.js";
99

10+
const expectCloseTo = (
11+
actual: number[][],
12+
expected: number[][],
13+
precision = 10,
14+
) => {
15+
expect(actual).toHaveLength(expected.length);
16+
actual.forEach((row, i) => {
17+
expect(row).toHaveLength(expected[i].length);
18+
row.forEach((val, j) => {
19+
expect(val).toBeCloseTo(expected[i][j], precision);
20+
});
21+
});
22+
};
23+
1024
describe("n8ao webgpu math", () => {
1125
it("matches upstream n8ao configuration defaults", () => {
1226
expect(createDefaultN8AOConfiguration()).toMatchObject({
@@ -36,8 +50,7 @@ describe("n8ao webgpu math", () => {
3650

3751
it("generates the same hemisphere sample distribution as upstream n8ao", () => {
3852
const samples = generateHemisphereSamples(4);
39-
40-
expect(samples.map((sample) => sample.toArray())).toEqual([
53+
expectCloseTo(samples.map((s) => s.toArray()), [
4154
[0.3535533905932738, 0, 0.9354143466934853],
4255
[-0.4515442808474507, 0.4136517405184686, 0.7905694150420949],
4356
[0.06911574220702117, -0.7875423888141976, 0.6123724356957944],
@@ -47,8 +60,7 @@ describe("n8ao webgpu math", () => {
4760

4861
it("generates the same denoise poisson pattern as upstream n8ao", () => {
4962
const samples = generateDenoiseSamples(4, 11);
50-
51-
expect(samples.map((sample) => sample.toArray())).toEqual([
63+
expectCloseTo(samples.map((s) => s.toArray()), [
5264
[0.35355339059327373, 0],
5365
[-1.4567267349998804e-15, -0.5946035575013605],
5466
[-0.8059274488676564, 3.948903589373758e-15],

0 commit comments

Comments
 (0)