Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Reset the mipmapped texture filter before post stages and bloom read

Nguyen Xuan An73a051696f76
diff --git a/changes/578.md b/changes/578.md
new file mode 100644
index 00000000..4d26c12c
--- /dev/null
+++ b/changes/578.md
@@ -0,0 +1 @@
+- Motion blur and bloom no longer leave a ghost of the previous frame after a seek (#578)
diff --git a/changes/578.size.md b/changes/578.size.md
new file mode 100644
index 00000000..8fb63ea3
--- /dev/null
+++ b/changes/578.size.md
@@ -0,0 +1,3 @@
+- goal:lit-box: 50 bytes gzip for the one-method fix that stops a ghost of the previous frame after a seek (#578)
+- goal:gltf-box: 58 bytes gzip for the same fix (#578)
+- goal:agent-scene: 63 bytes gzip for the same fix (#578)
diff --git a/src/renderer.js b/src/renderer.js
index d7f03a1a..b17ffb44 100644
--- a/src/renderer.js
+++ b/src/renderer.js
@@ -1770,11 +1770,22 @@ export class Renderer67 {
return variant;
}
+ // The fog step leaves the last frame's texture on a mipmapped filter. The stages and bloom read before this frame's
+ // mip chain exists, so on that filter they would sample the previous frame's blurred levels: a ghost after a seek.
+ restoreSingleLevelFiltering() {
+ if (!this.mipmappedTexture) return;
+ const context = this.context;
+ context.bindTexture(context.TEXTURE_2D, this.mipmappedTexture);
+ context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MIN_FILTER, context.LINEAR);
+ this.mipmappedTexture = null;
+ }
+
resolveFrame(frame, effects, environment, volume) {
const context = this.context;
context.bindFramebuffer(context.READ_FRAMEBUFFER, this.multisampleFramebuffer);
context.bindFramebuffer(context.DRAW_FRAMEBUFFER, this.resolveFramebuffer);
context.blitFramebuffer(0, 0, this.canvas.width, this.canvas.height, 0, 0, this.canvas.width, this.canvas.height, context.COLOR_BUFFER_BIT | (this.depth ? context.DEPTH_BUFFER_BIT : 0), context.NEAREST);
+ this.restoreSingleLevelFiltering();
let sourceTexture = this.frameTexture;
for (const stage of this.postStages) sourceTexture = stage.apply(this, frame, effects, sourceTexture) ?? sourceTexture;
if (effects.bloom.strength > 0) this.drawBloom(effects.bloom, sourceTexture);
diff --git a/tests/seek-mipmap-history.test.js b/tests/seek-mipmap-history.test.js
new file mode 100644
index 00000000..b0a875a5
--- /dev/null
+++ b/tests/seek-mipmap-history.test.js
@@ -0,0 +1,51 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+import { Renderer67 } from '../src/renderer.js';
+
+const LINEAR = 'LINEAR';
+const LINEAR_MIPMAP_LINEAR = 'LINEAR_MIPMAP_LINEAR';
+
+function fakeContext() {
+ const filters = new Map();
+ let boundTexture = null;
+ return {
+ filters,
+ READ_FRAMEBUFFER: 1,
+ DRAW_FRAMEBUFFER: 2,
+ COLOR_BUFFER_BIT: 4,
+ DEPTH_BUFFER_BIT: 8,
+ NEAREST: 9,
+ TEXTURE_2D: 3,
+ TEXTURE_MIN_FILTER: 5,
+ LINEAR,
+ bindFramebuffer() {},
+ blitFramebuffer() {},
+ bindTexture(target, texture) { boundTexture = texture; },
+ texParameteri(target, name, value) { filters.set(boundTexture, value); },
+ };
+}
+
+test('stages and bloom never read a texture left on a mipmapped filter by the previous frame (#578)', () => {
+ const context = fakeContext();
+ const frameTexture = { name: 'frame' };
+ context.filters.set(frameTexture, LINEAR_MIPMAP_LINEAR);
+ const stageReached = new Error('stage reached');
+ let filterSeenByStage = null;
+ const renderer = {
+ context,
+ canvas: { width: 4, height: 4 },
+ depth: true,
+ frameTexture,
+ mipmappedTexture: frameTexture,
+ postStages: [{
+ apply() {
+ filterSeenByStage = context.filters.get(frameTexture);
+ throw stageReached;
+ },
+ }],
+ restoreSingleLevelFiltering: Renderer67.prototype.restoreSingleLevelFiltering,
+ };
+ assert.throws(() => Renderer67.prototype.resolveFrame.call(renderer, {}, {}, {}, {}), stageReached);
+ assert.equal(filterSeenByStage, LINEAR);
+ assert.equal(renderer.mipmappedTexture, null);
+});