3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Allocate a hero world's frame targets in a task of their own (#779)
Nguyen Xuan Anb637f609e69a
diff --git a/changes/779.md b/changes/779.mdindex a8aab3ba..4a8833c4 100644--- a/changes/779.md+++ b/changes/779.md@@ -1 +1 @@-- The home hero no longer allocates its bloom targets inside the first draw of a world: `prepareFrame` creates them in a task of their own, which removes a 55 to 75 ms main-thread block at every world switch, and `measure-hero-load.mjs` names the functions and build phases behind each long task (`--profile`) (#779)+- The home hero no longer allocates its bloom targets inside the first draw of a world: `prepareFrame` creates them in a task of their own, which removes a 55 to 75 ms main-thread block at every world switch,; `programsReady` also takes the size plan and frame targets in a task of their own, which removes the 35 ms resize block of the build; `measure-hero-load.mjs` names the functions and build phases behind each long task (`--profile`) (#779)diff --git a/src/renderer.js b/src/renderer.jsindex 0b38b6b1..2a3cdec3 100644--- a/src/renderer.js+++ b/src/renderer.js@@ -902,7 +902,7 @@ export class CrucibleRenderer {// The second half of construction, which reads the link results. Reading a result waits for the driver, so a page that// builds a heavy renderer passes deferProgramLink and awaits programsReady() instead of freezing for the whole compile.- finishConstruction() {+ finishConstruction({ deferFrameTargets = false } = {}) {if (this.disposed || !this.pendingConstruction) return;const { pending, parts, environment } = this.pendingConstruction;this.pendingConstruction = null;@@ -945,13 +945,23 @@ export class CrucibleRenderer {this.initializeEnvironment(environment.map);this.createShadowMap();if (this.reversedDepthMode !== false && !benchmark) installReversedDepth(this, this.reversedDepthMode);+ if (!deferFrameTargets) this.allocateInitialTargets();+ }++ // The size plan and the frame targets: the largest driver calls of construction, so a page that awaits programsReady() takes them in a task of their own.+ allocateInitialTargets() {+ if (this.disposed) return;this.resize();if (!this.benchmark && !this.multisampleFramebuffer) this.allocateFrameTargets(this.canvas.width, this.canvas.height);}/** Resolves once every program started by the constructor has linked, polling without blocking the main thread, then finishes construction. Needs deferProgramLink. After dispose() it resolves at once without finishing construction. */async programsReady() {- if (this.disposed || !this.pendingConstruction) return;+ if (this.disposed) return;+ if (!this.pendingConstruction) {+ await this.initialTargetsPending;+ return;+ }const context = this.context;const parallelCompile = context.getExtension('KHR_parallel_shader_compile');const { pending } = this.pendingConstruction;@@ -960,7 +970,11 @@ export class CrucibleRenderer {while (!this.disposed && parallelCompile && programs.some(program => !context.getProgramParameter(program, parallelCompile.COMPLETION_STATUS_KHR))) {await new Promise(resolve => setTimeout(resolve, 8));}- this.finishConstruction();+ if (this.pendingConstruction) {+ this.finishConstruction({ deferFrameTargets: true });+ this.initialTargetsPending = new Promise(resolve => setTimeout(resolve, 0)).then(() => this.allocateInitialTargets());+ }+ await this.initialTargetsPending;}/**diff --git a/tests/programs-ready-frame-targets.test.js b/tests/programs-ready-frame-targets.test.jsnew file mode 100644index 00000000..ff76dce2--- /dev/null+++ b/tests/programs-ready-frame-targets.test.js@@ -0,0 +1,50 @@+import { test } from 'node:test';+import assert from 'node:assert/strict';+import { CrucibleRenderer } from '../src/renderer.js';++function fakeRenderer() {+ const calls = [];+ return {+ calls,+ disposed: false,+ pendingConstruction: { pending: {} },+ pendingEnvironmentPrograms: null,+ initialTargetsPending: null,+ context: { getExtension: () => null },+ finishConstruction(options) {+ calls.push(options?.deferFrameTargets ? 'finishConstruction:deferred' : 'finishConstruction');+ this.pendingConstruction = null;+ },+ allocateInitialTargets() { calls.push('allocateInitialTargets'); },+ };+}++const programsReady = renderer => CrucibleRenderer.prototype.programsReady.call(renderer);++test('programsReady allocates the frame targets in a task of their own after construction finishes (#779)', async () => {+ const renderer = fakeRenderer();+ const ready = programsReady(renderer);+ for (let turn = 0; turn < 5; turn++) await Promise.resolve();+ assert.deepEqual(renderer.calls, ['finishConstruction:deferred'], 'the targets wait for a timer task');+ await ready;+ assert.deepEqual(renderer.calls, ['finishConstruction:deferred', 'allocateInitialTargets']);+});++test('a second programsReady waits for the targets the first one is still allocating', async () => {+ const renderer = fakeRenderer();+ const first = programsReady(renderer);+ await Promise.resolve();+ const second = programsReady(renderer);+ await second;+ assert.deepEqual(renderer.calls, ['finishConstruction:deferred', 'allocateInitialTargets']);+ await first;+});++test('programsReady on a renderer disposed during the wait allocates nothing', async () => {+ const renderer = fakeRenderer();+ renderer.allocateInitialTargets = CrucibleRenderer.prototype.allocateInitialTargets;+ const ready = programsReady(renderer);+ renderer.disposed = true;+ await ready;+ assert.deepEqual(renderer.calls, ['finishConstruction:deferred']);+});