Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Load the float output module on demand and keep the core growth small (issue #611)

Nguyen Xuan An0196c3d0db3f
diff --git a/changes/611-render-output.size.md b/changes/611-render-output.size.md
index b288b24d..f0ada8d1 100644
--- a/changes/611-render-output.size.md
+++ b/changes/611-render-output.size.md
@@ -1,6 +1,4 @@
-- entry:render-passes: the pass target can be half float (173 bytes gzip), which 16-bit PNG and EXR passes read back instead of 8-bit bytes.
-- scene:agent-scene: the preview page carries the transparent flag, the half-float output target and its readback (about 400 bytes gzip), the compositing output of issue 611.
-- scene:agent-scene+lazy: the same 400 bytes, nothing else lazy changed.
-- goal:lit-box: 10 bytes gzip for one `frame.outputFramebuffer ?? null` in the final pass, the only core change that lets a render job draw the finished frame into a float target; there is no addon seam for it.
+- entry:render-passes: a pass target can be half float (128 bytes gzip, a static addon, not lazy), which 16-bit PNG and EXR passes read back instead of 8-bit bytes.
+- goal:lit-box: 9 bytes gzip for one `frame.outputFramebuffer ?? null` in the final pass, the only core change that lets a render job draw the finished frame into a float target.
- goal:gltf-box: the same 11 bytes in the core renderer.
-- goal:agent-scene: the same 411 bytes, a visible capability (alpha, 16-bit and EXR render output) for agent jobs.
+- goal:agent-scene: 25 bytes gzip for the transparent flag and the lazy hook; the float target and its readback load on demand (`src/float-output.js`), and the encoders run in Node.
diff --git a/scripts/agent-preview-browser.js b/scripts/agent-preview-browser.js
index 222edeff..a8da47a7 100644
--- a/scripts/agent-preview-browser.js
+++ b/scripts/agent-preview-browser.js
@@ -24,7 +24,6 @@ import { hasPatternTextures } from './agent-preview-textures.js';
import { TEXTURE_SEQUENCE_LIMITS } from '../src/texture-sequence-limits.js';
import { registerCatalogEnvironment, registerCatalogLook } from '../src/look-catalog.js';
import { parseRadianceHdr } from '../src/hdri.js';
-import { createFloatOutput, readFloatPixels } from '../src/float-output.js';
import { parseCube } from '../src/view-transform.js';
import { weightTracksFromGltfAnimation } from '../src/export-skeleton.js';
import { clipFromGltfAnimation } from '../src/skeleton-pose.js';
@@ -84,16 +83,6 @@ function candidateCamera(record, operations) {
return createAgentScene({ format: '67js.scene', schemaVersion: 1, geometries: [], materials: [], nodes: [], camera }).camera;
}
-// The half-float target a floatOutput frame draws into; rebuilt when the renderer or the canvas size changes.
-function floatOutputFor(record) {
- const cached = record.floatOutput;
- if (cached && cached.renderer === record.renderer && cached.output.width === canvas.width && cached.output.height === canvas.height) return cached.output;
- cached?.output.dispose();
- const output = createFloatOutput(record.renderer.context, canvas.width, canvas.height);
- record.floatOutput = { renderer: record.renderer, output };
- return output;
-}
-
function renderInfo(renderer) {
const context = renderer.context;
const debug = context.getExtension('WEBGL_debug_renderer_info');
@@ -769,7 +758,7 @@ window.__agentPreview = {
const shotEffects = depthOfFieldOverrides
? { ...record.scene.effects, depthOfField: { ...record.scene.effects.depthOfField, ...depthOfFieldOverrides } }
: undefined;
- const outputFramebuffer = floatOutput && !pass ? floatOutputFor(record).framebuffer : undefined;
+ const outputFramebuffer = floatOutput && !pass ? record.floatModule.outputFor(record, canvas).framebuffer : undefined;
draw(record, rendererTime, shotCamera, { pass, passDepthRange, passFloat, previousCamera, outputFramebuffer,
...(brightness === 1 ? {} : { exposure }), ...(shotEffects ? { effects: shotEffects } : {}) });
const info = renderInfo(record.renderer);
@@ -777,12 +766,13 @@ window.__agentPreview = {
},
// Reads the last frame as RGBA float32, rows bottom to top, cropped to `region` (pixels from the top left): the lit image
// when it was drawn with floatOutput, a pass when it was drawn with passFloat. Base64 of the float bytes.
+ // 16-bit PNG and EXR output load their module here, so a scene that never asks for them does not carry it.
+ async prepareFloatOutput({ sessionId }) {
+ requireSession(sessionId).floatModule = await import('../src/float-output.js');
+ },
readFloat({ sessionId, source, region }) {
const record = requireSession(sessionId);
- const context = record.renderer.context;
- const framebuffer = source === 'pass' ? record.renderer.passSupport.framebuffer : floatOutputFor(record).framebuffer;
- const pixels = readFloatPixels(context, framebuffer, canvas.width, canvas.height, region);
- return { width: region?.width ?? canvas.width, height: region?.height ?? canvas.height, data: bytesToBase64(new Uint8Array(pixels.buffer)) };
+ return record.floatModule.readFrame(record, canvas, source, region);
},
// Offline path trace of the scene at one instant. The tracer loads here, on first use, and not with the preview.
async pathTrace({ sessionId, animation, motionTime, options }) {
diff --git a/scripts/agent-runtime.mjs b/scripts/agent-runtime.mjs
index 79be1921..04c421d0 100644
--- a/scripts/agent-runtime.mjs
+++ b/scripts/agent-runtime.mjs
@@ -440,6 +440,7 @@ export function createAgentRuntime({ kernelWorkers = true, backend = 'gpu', brow
await jobPage.setViewportSize({ width, height });
const session = await jobPage.context().newCDPSession(jobPage);
const region = settings.region;
+ if (settings.format && settings.format !== 'png') await jobPage.evaluate(prepareInput => window.__agentPreview.prepareFloatOutput(prepareInput), { sessionId: jobId });
// A memory budget can shrink the frame below the size the region was chosen for.
if (region && (region.x + region.width > width || region.y + region.height > height)) {
throw new Error(`The region lies outside the rendered frame, which the memory budget reduced to ${width}x${height}.`);
diff --git a/src/agent-scene-capabilities.js b/src/agent-scene-capabilities.js
index f50e5333..87d4cb55 100644
--- a/src/agent-scene-capabilities.js
+++ b/src/agent-scene-capabilities.js
@@ -209,13 +209,6 @@ export const AGENT_SCENE_CAPABILITIES = Object.freeze({
materialPatterns: MATERIAL_PATTERN_TYPES, maxMaterialPatterns: MAX_MATERIAL_PATTERNS,
materialMappings: MATERIAL_MAPPINGS,
renderPasses: Object.freeze({ names: RENDER_PASS_NAMES, files: 'render-<pass>.png or frame-N-<pass>.png, raw and unantialiased; opaque and cutout geometry only', depth: `16 bits in red and green over ${RENDER_PASS_DEPTH_RANGE} m`, objectId: '24 bits in RGB; the job result lists the part names' }),
- renderOutput: Object.freeze({
- transparent: 'true writes the lit image with alpha (PNG, 16-bit PNG or EXR); image and stills only. The sky, environment background and automatic ground are not drawn, so the scene needs its own ground or a shadowCatcher; volume fog is refused.',
- format: `${RENDER_OUTPUT_FORMATS.join(', ')}: png is 8-bit (default); png16 is 16-bit from a half-float frame; exr holds the same display colour as the PNG in half floats (alpha stays premultiplied); its depth pass is metres in one float32 channel Z and objectId one float32 channel id. image and stills only.`,
- region: '{x, y, width, height} pixels from the top left, at least 16 each; renders the whole frame and keeps the crop. Works for image, stills and video (even sizes).',
- fps: `1 to ${MAXIMUM_JOB_FPS}; a video is at most 900 frames`,
- pathTracer: 'renderer jobs write the whole opaque 8-bit PNG: transparent, format and region are refused.',
- }),
// Recipe timeline (src/film-timeline.js), rendered with settings.timeline; the rest is in docs/agent-render-jobs.md.
filmTimeline: Object.freeze({
maxShots: 64, maxTotalSeconds: 60, maxShotSeconds: 30,
diff --git a/src/float-output.js b/src/float-output.js
index c4c72e47..d7c564da 100644
--- a/src/float-output.js
+++ b/src/float-output.js
@@ -52,3 +52,23 @@ export function readFloatPixels(context, framebuffer, frameWidth, frameHeight, r
context.bindFramebuffer(context.READ_FRAMEBUFFER, null);
return pixels;
}
+
+/** The target a floatOutput frame draws into, kept on the session record and rebuilt when the renderer or the size changes. */
+export function outputFor(record, canvas) {
+ const cached = record.floatOutput;
+ if (cached && cached.renderer === record.renderer && cached.output.width === canvas.width && cached.output.height === canvas.height) return cached.output;
+ cached?.output.dispose();
+ const output = createFloatOutput(record.renderer.context, canvas.width, canvas.height);
+ record.floatOutput = { renderer: record.renderer, output };
+ return output;
+}
+
+/** The last frame, or its pass, as base64 RGBA float32 (rows bottom to top) cropped to `region`. */
+export function readFrame(record, canvas, source, region) {
+ const framebuffer = source === 'pass' ? record.renderer.passSupport.framebuffer : outputFor(record, canvas).framebuffer;
+ const pixels = readFloatPixels(record.renderer.context, framebuffer, canvas.width, canvas.height, region);
+ const bytes = new Uint8Array(pixels.buffer);
+ let text = '';
+ for (let offset = 0; offset < bytes.length; offset += 0x8000) text += String.fromCharCode(...bytes.subarray(offset, offset + 0x8000));
+ return { width: region?.width ?? canvas.width, height: region?.height ?? canvas.height, data: btoa(text) };
+}
diff --git a/src/render-passes.js b/src/render-passes.js
index db58c1f5..1671b560 100644
--- a/src/render-passes.js
+++ b/src/render-passes.js
@@ -1,5 +1,4 @@
import { compileProgram, fullscreenVertex } from './gl-program.js';
-import { requireFloatTargets } from './float-output.js';
// Render passes draw raw material data (albedo, normal, emission, depth, object ID) instead of the lit image.
// The shader branch lives in the main fragment shader behind the `renderPass` feature, which compiles on the first
@@ -20,7 +19,7 @@ void main() { outputColor = vec4(texelFetch(sourceTexture, ivec2(gl_FragCoord.xy
// A float target keeps the pass values as half floats (frame.passFloat) for 16-bit PNG and EXR output; the shader writes the same values.
function createPassTarget(context, width, height, float = false) {
- if (float) requireFloatTargets(context);
+ if (float && !context.getExtension('EXT_color_buffer_float')) throw new Error('16-bit PNG and EXR output need EXT_color_buffer_float. Use format png.');
const texture = context.createTexture();
context.bindTexture(context.TEXTURE_2D, texture);
if (float) context.texImage2D(context.TEXTURE_2D, 0, context.RGBA16F, width, height, 0, context.RGBA, context.HALF_FLOAT, null);