Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Stop comparing a sky's pixels every frame when deciding whether the water mirror can be reused (part of #388)

Nguyen Xuan Ane742f5b7eb0a
diff --git a/src/water-reflection.js b/src/water-reflection.js
index c41f6b78..5329a43a 100644
--- a/src/water-reflection.js
+++ b/src/water-reflection.js
@@ -204,6 +204,25 @@ function mirroredFrame(frame, height, target) {
}
const SNAPSHOT_DEPTH = 4;
+// A typed array longer than this is a texture or map payload (a sky's pixels run to millions of values). Copying and comparing it
+// every frame cost about 170 ms a frame in the container port, so a frame snapshot keeps it by reference and a frame matches it by identity.
+const LARGE_ARRAY_LENGTH = 65536;
+
+function isTypedArray(value) {
+ return ArrayBuffer.isView(value) && !(value instanceof DataView);
+}
+
+function sameTypedArray(first, second) {
+ if (first.constructor !== second.constructor || first.length !== second.length || first.length > LARGE_ARRAY_LENGTH) {
+ return false;
+ }
+ for (let index = 0; index < first.length; index++) {
+ if (first[index] !== second[index]) {
+ return false;
+ }
+ }
+ return true;
+}
// A copy of the frame's values (arrays, typed arrays and plain objects are copied, anything else is kept by reference), so a later
// frame can be compared with what this one held even when the caller changes the same arrays in place.
@@ -212,7 +231,7 @@ function snapshotOf(value, depth = 0) {
return value;
}
if (ArrayBuffer.isView(value)) {
- return value.slice();
+ return isTypedArray(value) && value.length > LARGE_ARRAY_LENGTH ? value : value.slice();
}
if (Array.isArray(value)) {
return value.map(item => snapshotOf(item, depth + 1));
@@ -230,6 +249,9 @@ function sameValue(first, second, depth = 0) {
if (depth >= SNAPSHOT_DEPTH || first === null || second === null || typeof first !== 'object' || typeof second !== 'object') {
return false;
}
+ if (isTypedArray(first) && isTypedArray(second)) {
+ return sameTypedArray(first, second);
+ }
const firstKeys = Object.keys(first);
if (Object.getPrototypeOf(first) !== Object.getPrototypeOf(second) || firstKeys.length !== Object.keys(second).length) {
return false;
diff --git a/tests/water-reflection.test.js b/tests/water-reflection.test.js
index 591947c1..3f3df5bb 100644
--- a/tests/water-reflection.test.js
+++ b/tests/water-reflection.test.js
@@ -262,6 +262,15 @@ test('canReuseMirrorImage needs a drawn picture, the same frame and parts, or an
assert.equal(canReuseMirrorImage({ ...base, updateEvery: 2, framesSinceDraw: 1 }, { ...drawnFrame, time: 2 }), false);
});
+test('canReuseMirrorImage compares small typed arrays by value and a large one, a sky payload, by identity', () => {
+ const sky = new Float32Array(2_000_000);
+ const drawnFrame = { eye: new Float32Array([0, 20, 100]), environment: { map: { pixels: sky } } };
+ const base = { ready: true, drawnFrame, partsRevision: 1, drawnRevision: 1, framesSinceDraw: 0, updateEvery: 1 };
+ assert.equal(canReuseMirrorImage(base, { eye: new Float32Array([0, 20, 100]), environment: { map: { pixels: sky } } }), true);
+ assert.equal(canReuseMirrorImage(base, { eye: new Float32Array([0, 21, 100]), environment: { map: { pixels: sky } } }), false);
+ assert.equal(canReuseMirrorImage(base, { eye: new Float32Array([0, 20, 100]), environment: { map: { pixels: new Float32Array(2_000_000) } } }), false);
+});
+
test('only the mirror draw carries the mirror culling: parts under the plane, coarser levels and small far parts', () => {
const context = new Proxy({}, { get: (_, name) => (name === 'checkFramebufferStatus' ? () => 1 : name === 'FRAMEBUFFER_COMPLETE' ? 1 : typeof name === 'string' && name === name.toUpperCase() ? 0 : () => ({})) });
const cullingSeen = [];