Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Link programs while textures and buffers upload (part of #406)

Nguyen Xuan Anbb33456e9366
diff --git a/changes/406-upload.md b/changes/406-upload.md
new file mode 100644
index 00000000..a3725806
--- /dev/null
+++ b/changes/406-upload.md
@@ -0,0 +1 @@
+- The renderer starts every program, then uploads textures and buffers, then reads the link results, so the driver links while the upload runs (issue #406). On the AIS port film this hides about 0.25 s; with `ImageBitmap` images (64 ms to upload a 24-megapixel texture against 300 ms from an `Image`) the page loads in 2.8 to 3.4 s instead of 3.6 to 3.7 s. Pixels are unchanged. `docs/terrain.md` has the bitmap loading line and a migration snippet for per-vertex mask samplers.
diff --git a/docs/terrain.md b/docs/terrain.md
index aef1bb4c..1f9d5dbe 100644
--- a/docs/terrain.md
+++ b/docs/terrain.md
@@ -99,7 +99,23 @@ In a recipe, the same ground is the `terrain` geometry (`size`, `resolution`, `s
Combine the sampled arrays into `heights` in your own loop (for a sea mask: lower the vertex by the mask share), then build the geometry. A test pins the output against the per-vertex build it replaces: positions, normals, uvs and indices are bit-equal.
-Measured on the AIS port film (4.9 M vertices over three tiers, 10 m core mesh, sea mask averaged over each cell), page open to `stageReady`, RTX 3060, headless Chrome on ANGLE D3D11, four runs while other sessions loaded the machine: 5.5 to 13.1 s before (5.5 to 6.7 s in the quiet runs), 3.6 to 9.6 s after (3.6 to 3.7 s in three of four runs), against 1.0 to 1.4 s for the three.js build of the same scene. The terrain code fell from about 3.4 s of CPU to about 0.6 s; the frame at film second 1 is pixel-identical (max difference 0 of 255). What stays: image decode and mask readback (about 0.6 s), texture upload (about 0.7 s) and shader compile (about 0.6 s). The three.js build does the shoreline per pixel in a shader, so it needs no mask pass.
+Measured on the AIS port film (4.9 M vertices over three tiers, 10 m core mesh, sea mask averaged over each cell), page open to `stageReady`, RTX 3060, headless Chrome on ANGLE D3D11, four runs while other sessions loaded the machine: 5.5 to 13.1 s before (5.5 to 6.7 s in the quiet runs), 3.6 to 9.6 s after (3.6 to 3.7 s in three of four runs), against 1.0 to 1.4 s for the three.js build of the same scene. The terrain code fell from about 3.4 s of CPU to about 0.6 s; the frame at film second 1 is pixel-identical (max difference 0 of 255). With bitmaps for the images and the programs linking during the uploads, the same page loads in 2.8 to 6.6 s (2.8 to 3.4 s in the two quietest runs; three.js 1.1 to 2.1 s in the same runs), and the frame is still pixel-identical. What stays: mask readback (about 0.3 s), texture upload (about 0.35 s), the two raster passes (about 0.5 s) and building meshes in the port's own code. The three.js build does the shoreline per pixel in a shader, so it needs no mask pass.
+
+### Moving a per-vertex mask sampler over
+
+If you sample a mask yourself (a bilinear read or a cell mean for every vertex, through a closure), switch to `sampleRasterGrid` and `createHeightfieldGeometry`. The values and the mesh stay the same, and the work drops from a call per sample to two passes over the pixels:
+
+```js
+// before: 16 bilinear reads and a closure call for every vertex
+const heightAt = (east, north) => groundHeight(bilinear(heights, east, north), cellAverage(mask, east, north, spacing));
+const geometry = gridGeometry({ columns, rows, eastAt, northAt, heightAt, uvBox });
+// after: the whole grid at once, then the mesh
+const lines = { columns, rows, eastAt, northAt };
+const share = sampleRasterGrid(mask, { ...lines, cellSize: spacing, samplesPerSide: 4 }), raw = sampleRasterGrid(heights, lines);
+const geometry = createHeightfieldGeometry({ ...lines, heights: Float32Array.from(raw, (value, vertex) => groundHeight(value, share[vertex])), uvBox });
+```
+
+Load images as `createImageBitmap(await (await fetch(url)).blob(), { colorSpaceConversion: 'none', premultiplyAlpha: 'none' })` and pass the bitmap as a texture `image`. The renderer accepts it, it decodes off the main thread, and `texImage2D` from a bitmap takes about 0.2 of the time it takes from an `Image` (measured on 24-megapixel JPEGs: 64 ms against 300 ms), with identical pixels. The renderer also starts every program before it uploads textures and buffers, so the driver links them while the upload runs.
## Navigation
diff --git a/src/renderer.js b/src/renderer.js
index fc234915..e466c8ba 100644
--- a/src/renderer.js
+++ b/src/renderer.js
@@ -608,6 +608,8 @@ export class Renderer67 {
pending.bloom = startProgram(this.context, fullscreenVertex, bloomFragment);
if (environment.map) this.startEnvironmentPrograms();
}
+ // The driver links the programs on its own threads while the textures and buffers upload; only then is the link result read.
+ groups.forEach((group) => this.batches.push(this.uploadBatch(group)));
this.finishMainProgram(pending.main);
this.depthProgram = finishProgram(this.context, pending.depth);
this.depthSkinUniforms = Object.fromEntries(SKIN_UNIFORMS.map(name => [name, this.context.getUniformLocation(this.depthProgram, name)]));
@@ -644,7 +646,6 @@ export class Renderer67 {
}
if (this.shaderFeatures.has('splat')) requireSplatSupport().initializePlaceholders(this);
this.initializeEnvironment(environment.map);
- groups.forEach((group) => this.batches.push(this.uploadBatch(group)));
this.createShadowMap();
this.resize();
if (!this.benchmark && !this.multisampleFramebuffer) this.allocateFrameTargets(this.canvas.width, this.canvas.height);