The occlusion probe survives a restored context, a depth reader that throws, and queries that never answer

A source's contextGeneration tells the probe the same context was lost and
restored; it then makes its GPU objects again instead of drawing with dead
ones. A depth reader that throws turns the test off for a while instead of
throwing out of add(). Batches whose control point never answers count as
failed ones, so a query path that never answers stands down and is rebuilt
on the retry.
This commit is contained in:
Sameh Khamis committed 2026-11-10 22:03:36 -08:00
1 parent 730dff53dd
commit cd44d5c4d8
2 files changed
+87 -2

No files matched your search

+32 -2
View File
@@ -41,7 +41,7 @@ import {
* gl: WebGLRenderingContext|WebGL2RenderingContext, webgl2: boolean,
* width: number, height: number, framebuffers: WebGLFramebuffer[],
* textures: WebGLTexture[],
* encoding?: 'packed' | 'depth',
* encoding?: 'packed' | 'depth', contextGeneration?: number,
* frustums: Array<{near: number, far: number}>, logDepth: boolean,
* view: Float64Array, tanHalfFovX: number, tanHalfFovY: number,
* }} DepthSource
@@ -50,6 +50,9 @@ import {
* packed four bytes to a texel, and `framebuffers` the framebuffers they are
* attached to (the WebGL 1 fallback reads them). `'depth'`: depth textures,
* read on the GPU only.
* - `contextGeneration`: changes when the same context is lost and restored,
* which leaves every GPU object made on it before dead; the probe then
* makes its own again.
* - `logDepth`: depth is `log2(1 + d - near) / log2(far - near + 1)` of the
* view distance `d`; otherwise standard perspective depth.
* - `view`: the view matrix in ECEF, column-major; `tanHalfFovX`/`Y` the
@@ -187,6 +190,7 @@ export class DepthOcclusionProbe {
this._snapshot = null;
this._snapshotAt = Number.NEGATIVE_INFINITY;
this._spent = 0;
this._contextGeneration = undefined;
}
/**
@@ -343,7 +347,13 @@ export class DepthOcclusionProbe {
_openSource() {
this._source = null;
if (this._now < this._retryAt) return;
const source = this._readSource(this._scene, this._view);
let source;
try {
source = this._readSource(this._scene, this._view);
} catch {
this._fail();
return;
}
if (!source) {
this.mode = 'off';
return;
@@ -352,7 +362,18 @@ export class DepthOcclusionProbe {
// A different context (a rebuilt viewer) orphans whatever was in flight.
this.destroy();
this._gl = source.gl;
} else if (
source.contextGeneration !== undefined &&
source.contextGeneration !== this._contextGeneration
) {
// The same context lost and restored: everything made on it before is
// gone, and nothing is left to delete.
this.reset();
this._queryPool.length = 0;
this._resources = null;
this._unsupportedGl = null;
}
this._contextGeneration = source.contextGeneration;
try {
if (source.gl.isContextLost?.()) {
// Everything made on the lost context went with it.
@@ -648,6 +669,9 @@ export class DepthOcclusionProbe {
for (let i = batches.length - 1; i >= 0; i--) {
const batch = batches[i];
if (batch.frame >= overdue) continue;
// A batch whose control point never answered counts as a failed one:
// a query path that never answers stands down like one that says no.
if (batch.cursor === 0) this._controlFailures++;
for (let k = batch.cursor; k < batch.queries.length; k++)
gl.deleteQuery(batch.queries[k]);
for (
@@ -658,6 +682,12 @@ export class DepthOcclusionProbe {
batch.contacts[k].pending = false;
batches.splice(i, 1);
}
if (this._controlFailures >= PROBE_MAX_CONTROL_FAILURES) {
// Rebuild from scratch on the retry: the GPU objects may be what
// stopped answering.
this.destroy();
this._fail();
}
} catch {
for (const batch of batches) this._release(batch.contacts);
batches.length = 0;
+55
View File
@@ -93,3 +93,58 @@ test('with no target nothing is tested', () => {
assert.equal(test.add(point(), { x: 0, y: 0, z: -500 }), false);
assert.equal(test.mode, 'off');
});
test('a depth reader that throws turns the test off instead of throwing', () => {
const test = createDepthOcclusionTest({
readSource: () => {
throw new Error('no eye');
},
target: () => ({}),
});
test.beginFrame(0);
assert.equal(test.add(point(), { x: 0, y: 0, z: -500 }), false);
assert.equal(test.mode, 'off');
test.endFrame();
});
test('after the context is lost and restored, the probe makes its GPU objects again', () => {
const gl = createFakeGl();
let programs = 0;
const createProgram = gl.createProgram;
gl.createProgram = () => {
programs++;
return createProgram();
};
let generation = 1;
const test = createDepthOcclusionTest({
readSource: () => ({ ...depthSource(gl), contextGeneration: generation }),
target: () => ({}),
});
const run = (now) => {
test.beginFrame(now);
test.add(point(), { x: -250, y: 0, z: -500 });
test.endFrame();
};
run(0);
run(1);
assert.equal(programs, 1);
generation = 2;
run(2);
assert.equal(programs, 2, 'made again on the restored context');
assert.equal(test.mode, 'async');
});
test('batches that never answer stand the probe down', () => {
const gl = createFakeGl();
const test = createDepthOcclusionTest({
readSource: () => depthSource(gl),
target: () => ({}),
});
// No finishGpu: nothing ever answers.
for (let frame = 0; frame < 200 && test.mode !== 'off'; frame++) {
test.beginFrame(frame);
test.add(point(), { x: -250, y: 0, z: -500 });
test.endFrame();
}
assert.equal(test.mode, 'off');
});