3D engine that AI agents drive
Test the sliced replaceParts wiring and drop the banded texture upload (#779)
Nguyen Xuan Anff5c6f144540
diff --git a/bench/incremental-grouping/run.mjs b/bench/incremental-grouping/run.mjsnew file mode 100644index 00000000..013b6d11--- /dev/null+++ b/bench/incremental-grouping/run.mjs@@ -0,0 +1,63 @@+// Measures the grouping step of replaceParts for a world that arrives in growing prefixes (issue 779):+// groupParts over every prefix against groupPartsIncrementally with the state kept between slices.+// Usage: node bench/incremental-grouping/run.mjs [--runs 5]+// Times depend on machine load, so the report gives the range over the runs.+import { groupParts, groupPartsIncrementally } from '../../src/renderer.js';++const runs = Number(process.argv[process.argv.indexOf('--runs') + 1]) || 5;+const partCount = 100000;+const geometryCount = 2000;+const sliceCount = 20;++const identity = () => new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);+const geometries = Array.from({ length: geometryCount }, () => ({+ positions: new Float32Array([0, 0, 0, 1, 0, 0, 0, 1, 0]),+ normals: new Float32Array([0, 0, 1, 0, 0, 1, 0, 0, 1]),+ indices: new Uint32Array([0, 1, 2]),+}));+const materials = Array.from({ length: 8 }, (_, index) => ({ color: [index / 8, 0.5, 0.5], metallic: 0, roughness: 0.5, emission: 0 }));+const partsPerGeometry = partCount / geometryCount;+const parts = Array.from({ length: partCount }, (_, index) => ({+ name: `part ${index}`,+ geometry: geometries[Math.floor(index / partsPerGeometry)],+ matrix: identity(),+ material: materials[index % materials.length],+}));+const sliceSize = partCount / sliceCount;++function timeSlices(groupPrefix) {+ const durations = [];+ for (let end = sliceSize; end <= partCount; end += sliceSize) {+ const prefix = parts.slice(0, end);+ const start = performance.now();+ groupPrefix(prefix);+ durations.push(performance.now() - start);+ }+ return { totalMs: durations.reduce((sum, value) => sum + value, 0), worstSliceMs: Math.max(...durations) };+}++function range(samples, key) {+ const values = samples.map(sample => Math.round(sample[key]));+ return `${Math.min(...values)} to ${Math.max(...values)}`;+}++const full = [];+const incremental = [];+for (let run = 0; run < runs; run++) {+ full.push(timeSlices(prefix => groupParts(prefix)));+ let state = null;+ incremental.push(timeSlices((prefix) => {+ const grouped = groupPartsIncrementally(state, prefix);+ state = grouped.state;+ }));+}+console.log(JSON.stringify({+ parts: partCount,+ geometries: geometryCount,+ slices: sliceCount,+ runs,+ fullTotalMs: range(full, 'totalMs'),+ fullWorstSliceMs: range(full, 'worstSliceMs'),+ incrementalTotalMs: range(incremental, 'totalMs'),+ incrementalWorstSliceMs: range(incremental, 'worstSliceMs'),+}, null, 2));diff --git a/changes/779-incremental-grouping.md b/changes/779-incremental-grouping.mdindex 382dd832..45fec4a9 100644--- a/changes/779-incremental-grouping.md+++ b/changes/779-incremental-grouping.md@@ -1,2 +1 @@-- `replaceParts(parts, { retainedUnchanged: true })` groups only the parts appended since the last call when a world grows by whole geometries, so loading a large world in slices no longer regroups the whole prefix on every call (#779)-- An unflipped texture image over 1,048,576 pixels uploads in row bands of at most that size instead of one `texImage2D`; the pixels are identical (#779)+- `replaceParts(parts, { retainedUnchanged: true })` groups only the parts appended since the last call when a world grows by whole geometries, so loading a large world in slices no longer regroups the whole prefix on every call; a floor that shares a geometry with a world part still groups with it (#779)diff --git a/changes/779-incremental-grouping.size.md b/changes/779-incremental-grouping.size.mddeleted file mode 100644index b4ef6cc6..00000000--- a/changes/779-incremental-grouping.size.md+++ /dev/null@@ -1,9 +0,0 @@-- entry:core: the incremental grouping for sliced replaceParts and the banded texture upload, about 340 bytes gzip, for a measured drop in grouping time from 1.5 to 2.9 s to 0.16 to 0.30 s over a 100,000-part world loaded in 20 slices-- scene:lit-box: same code as entry:core-- scene:gltf-box: same code as entry:core-- scene:gltf-box+lazy: same code as entry:core-- scene:agent-scene: same code as entry:core-- scene:agent-scene+lazy: same code as entry:core-- goal:lit-box: the growth is the sliced grouping and banded upload, not scene features; it takes the regroup of a 100,000-part world from 11 to 23 ms at worst per slice, and the lead accepts it as a functional gain-- goal:gltf-box: the growth is the sliced grouping and banded upload, not scene features; same measured gain-- goal:agent-scene: the growth is the sliced grouping and banded upload, not scene features; same measured gaindiff --git a/docs/gltf.md b/docs/gltf.mdindex 5c29cf4b..7ccc4f2b 100644--- a/docs/gltf.md+++ b/docs/gltf.md@@ -115,8 +115,7 @@ renderer.replaceParts(asset.parts);`programsReady()` polls `KHR_parallel_shader_compile` and then finishes construction; a renderer made with `deferProgramLink` must not render before it resolves.`await renderer.prepareFrame(frame)` is the matching last step before a hidden renderer is shown: it draws `frame` once without freezing the page for the programs a first draw links (the post pass of a fog or sky frame, liquids, ambient occlusion, the reversed-depth copy). A draw that meets a program the driver is still building stops, the page waits in short timers, and the draw runs again until one completes; it resolves `true`. After `timeoutMilliseconds` (6000 by default) it draws the frame in one block and resolves `false`, so a slow driver costs a stall, never a hang. Start the next scene's `prepareFrame` while the current scene plays and begin the fade when it resolves.Starting on `shaderFeatureRepresentatives` matters: a scene whose first parts lack a feature recompiles the program inside `replaceParts`, which blocks again.-When a world arrives in growing prefixes that end on a geometry boundary, call `renderer.replaceParts(prefix, { retainedUnchanged: true })`: only the parts appended since the last call are grouped and checked, and the batches built before are kept as they are. A prefix that changes, reorders or removes earlier parts, or an appended part that shares a geometry with them, groups the whole list as before. Measured on 100,000 parts of 2,000 geometries in 20 prefixes, the grouping step took 1.5 to 2.9 s in total and 134 to 303 ms at worst without it, and 0.16 to 0.30 s in total and 11 to 23 ms at worst with it.-A texture image over 1,048,576 pixels (more than 4 MiB of RGBA) that is not flipped on upload goes to the GPU in row bands of at most that size, so no single `texImage2D` stalls the driver; the result equals one upload. A flipped image stays one upload.+When a world arrives in growing prefixes that end on a geometry boundary, call `renderer.replaceParts(prefix, { retainedUnchanged: true })`: only the parts appended since the last call are grouped and checked, and the batches built before are kept as they are. A prefix that changes, reorders or removes earlier parts, or an appended part that shares a geometry with them, groups the whole list as before. `node bench/incremental-grouping/run.mjs` times it for 100,000 parts of 2,000 geometries in 20 prefixes; the grouping step is about ten times shorter in total and in its longest slice, and the numbers move with machine load.## Animation and ownershipdiff --git a/src/renderer.js b/src/renderer.jsindex cd27410e..c11b05c6 100644--- a/src/renderer.js+++ b/src/renderer.js@@ -45,28 +45,6 @@ const MAP_FLAGS = ['hasBaseMap', 'hasNormalMap', 'hasMetallicRoughnessMap', 'hasconst DEFAULT_NORMAL_SCALE = [1, 1];// The most texture taps a mipmapped texture may take along a grazing axis (16 is the usual hardware ceiling, 8 costs less).const MAX_TEXTURE_ANISOTROPY = 8;-// One texImage2D of a large image stalls the driver for tens of milliseconds, so an image over this many pixels goes up in bands.-export const MAX_UPLOAD_PIXELS = 1 << 20;--/** Uploads `image` to the bound TEXTURE_2D. An unflipped image over MAX_UPLOAD_PIXELS goes up in row bands of at most that size; the pixels are the same. */-export function uploadImage(context, image, width, height, internalFormat, flipY) {- // Browsers disagree on whether a flip applies before or after the skipped rows, so a flipped image stays one upload.- if (flipY || width * height <= MAX_UPLOAD_PIXELS) {- context.texImage2D(context.TEXTURE_2D, 0, internalFormat, context.RGBA, context.UNSIGNED_BYTE, image);- return;- }- context.texImage2D(context.TEXTURE_2D, 0, internalFormat, width, height, 0, context.RGBA, context.UNSIGNED_BYTE, null);- const bandRows = Math.max(1, Math.floor(MAX_UPLOAD_PIXELS / width));- try {- for (let firstRow = 0; firstRow < height; firstRow += bandRows) {- const rows = Math.min(bandRows, height - firstRow);- context.pixelStorei(context.UNPACK_SKIP_ROWS, firstRow);- context.texSubImage2D(context.TEXTURE_2D, 0, 0, firstRow, width, rows, context.RGBA, context.UNSIGNED_BYTE, image);- }- } finally {- context.pixelStorei(context.UNPACK_SKIP_ROWS, 0);- }-}const PROCEDURAL_MATERIAL_TAGS = new Set(['water', 'foliage', 'grass', 'terrain', 'floor', 'fire', 'smoke', 'liquid', 'foam']);// Tags whose vertices sway in the wind; the shadow pass must be redrawn when time moves.@@ -540,6 +518,20 @@ export function groupPartsIncrementally(previous, parts, batching = true) {return { batches, state: { parts: parts.slice(), geometries: new Set(parts.map(part => part.geometry)), batches } };}+/**+ * Groups the world and its floor for replaceParts. Without `retainedUnchanged` this is groupParts over the parts and the floor, with no state kept.+ * With it, only the appended slice is grouped; the floor is one more batch unless it shares a geometry with a world part,+ * where groupParts would have merged them, so that case groups everything.+ */+export function groupWorld(previous, parts, groundPart, batching, retainedUnchanged) {+ if (!retainedUnchanged) return { batches: groupParts(groundPart ? [...parts, groundPart] : parts, batching), state: null };+ const grouped = groupPartsIncrementally(previous, parts, batching);+ if (!groundPart) return grouped;+ const floorMerges = grouped.state.geometries.has(groundPart.geometry);+ const batches = floorMerges ? groupParts([...parts, groundPart], batching) : [...grouped.batches, ...groupParts([groundPart], batching)];+ return { batches, state: grouped.state };+}+export function textureDescriptorInfo(map, maximumSize = Infinity) {const width = map.image?.naturalWidth ?? map.image?.width;const height = map.image?.naturalHeight ?? map.image?.height;@@ -1023,7 +1015,7 @@ export class Renderer67 {if (map) {const info = textureDescriptorInfo(map, context.getParameter(context.MAX_TEXTURE_SIZE));if (map.image.compressed) map.image.compressed.upload(context);- else uploadImage(context, map.image, info.width, info.height, map.colorSpace === 'linear' ? context.RGBA8 : context.SRGB8_ALPHA8, map.flipY ?? false);+ else context.texImage2D(context.TEXTURE_2D, 0, map.colorSpace === 'linear' ? context.RGBA8 : context.SRGB8_ALPHA8, context.RGBA, context.UNSIGNED_BYTE, map.image);context.texParameteri(context.TEXTURE_2D, context.TEXTURE_WRAP_S, info.wrapS);context.texParameteri(context.TEXTURE_2D, context.TEXTURE_WRAP_T, info.wrapT);context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MIN_FILTER, info.minFilter);@@ -1317,16 +1309,9 @@ export class Renderer67 {const renderParts = this.groundPart ? [...parts, this.groundPart] : parts;this.fitParts = renderParts;this.fitBounds = undefined;- let groups;- if (retainedUnchanged) {- // A world that grows in slices groups only the slice that arrived; the floor is one more batch of its own geometry.- const grouped = groupPartsIncrementally(this.incrementalGrouping, parts, this.batching);- this.incrementalGrouping = grouped.state;- groups = this.groundPart ? [...grouped.batches, ...groupParts([this.groundPart], this.batching)] : grouped.batches;- } else {- this.incrementalGrouping = null;- groups = groupParts(renderParts, this.batching);- }+ const grouped = groupWorld(retainedUnchanged ? this.incrementalGrouping : null, parts, this.groundPart, this.batching, retainedUnchanged);+ this.incrementalGrouping = grouped.state;+ const groups = grouped.batches;const geometries = new Set(renderParts.flatMap(partGeometries));// Uploaded geometry was validated when it arrived; rechecking a large static scene every frame dominated streamed playback.for (const geometry of geometries) {diff --git a/tests/helpers/dev-server-browser.js b/tests/helpers/dev-server-browser.jsnew file mode 100644index 00000000..d4ee0a9f--- /dev/null+++ b/tests/helpers/dev-server-browser.js@@ -0,0 +1,58 @@+// Starts the Vite dev server and headless Chrome for the tests that need a real WebGL context.+import { spawn } from 'node:child_process';+import { freePort } from './free-port.js';++const CHROME_ARGUMENTS = ['--use-angle=swiftshader', '--enable-unsafe-swiftshader', '--enable-webgl', '--ignore-gpu-blocklist'];++async function startServer(port) {+ const server = spawn(process.execPath, ['node_modules/vite/bin/vite.js', '--host', '127.0.0.1', '--port', String(port), '--strictPort'], { windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });+ await new Promise((resolve, reject) => {+ const timeout = setTimeout(() => reject(new Error('The dev server did not start.')), 30000);+ server.stdout.on('data', (data) => {+ if (/ready in/.test(data.toString())) {+ clearTimeout(timeout);+ resolve();+ }+ });+ server.on('exit', (code) => {+ clearTimeout(timeout);+ reject(new Error(`The dev server exited ${code}.`));+ });+ });+ return server;+}++/**+ * Runs `body(page)` on the dev server's page in headless Chrome and cleans up.+ * Where Chrome or playwright is missing the test skips on a developer machine and fails on CI, where Chrome is required.+ */+export async function withDevServerPage(testContext, body) {+ const skipOrThrow = (message) => {+ if (process.env.CI) throw new Error(message);+ testContext.skip(message);+ };+ let chromium;+ try {+ ({ chromium } = await import('playwright'));+ } catch {+ skipOrThrow('playwright is not installed');+ return;+ }+ const port = await freePort();+ const server = await startServer(port);+ let browser;+ try {+ try {+ browser = await chromium.launch({ headless: true, args: CHROME_ARGUMENTS });+ } catch (error) {+ skipOrThrow(`Chrome did not start: ${error.message.split('\n')[0]}`);+ return;+ }+ const page = await browser.newPage({ viewport: { width: 128, height: 128 } });+ await page.goto(`http://127.0.0.1:${port}/`);+ await body(page);+ } finally {+ await browser?.close();+ server.kill();+ }+}diff --git a/tests/incremental-grouping.test.js b/tests/incremental-grouping.test.jsindex bc9e3704..47add8ec 100644--- a/tests/incremental-grouping.test.js+++ b/tests/incremental-grouping.test.js@@ -1,6 +1,6 @@import assert from 'node:assert/strict';import test from 'node:test';-import { groupParts, groupPartsIncrementally } from '../src/renderer.js';+import { groupParts, groupPartsIncrementally, groupWorld } from '../src/renderer.js';const identity = () => new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);const makeGeometry = () => ({@@ -52,3 +52,42 @@ test('an invalid appended part still throws', () => {const broken = { ...makePart(200, geometries[5]), matrix: new Float32Array(3) };assert.throws(() => groupPartsIncrementally(grouped.state, [...worldParts.slice(0, 5), broken]));});++const floorPart = { name: 'floor', geometry: makeGeometry(), matrix: identity(), material: materials[0] };+const sharingFloor = { name: 'shared floor', geometry: geometries[2], matrix: identity(), material: materials[0] };++test('groupWorld with a floor and growing prefixes builds the batches of one full grouping', () => {+ let state = null;+ for (let end = 5; end <= worldParts.length; end += 5) {+ const prefix = worldParts.slice(0, end);+ const grouped = groupWorld(state, prefix, floorPart, true, true);+ assert.ok(sameBatches(grouped.batches, groupParts([...prefix, floorPart])), `prefix of ${end} parts`);+ state = grouped.state;+ }+ assert.equal(state.parts.includes(floorPart), false, 'the floor is not part of the kept state');+});++test('groupWorld puts a floor that shares a geometry with a world part into that batch, as before', () => {+ let state = null;+ for (let end = 5; end <= worldParts.length; end += 5) {+ const prefix = worldParts.slice(0, end);+ const grouped = groupWorld(state, prefix, sharingFloor, true, true);+ assert.ok(sameBatches(grouped.batches, groupParts([...prefix, sharingFloor])), `prefix of ${end} parts`);+ state = grouped.state;+ }+});++test('groupWorld without retainedUnchanged groups the whole list and keeps no state', () => {+ const kept = groupWorld(null, worldParts.slice(0, 10), floorPart, true, true);+ const whole = groupWorld(kept.state, worldParts.slice(0, 20), floorPart, true, false);+ assert.ok(sameBatches(whole.batches, groupParts([...worldParts.slice(0, 20), floorPart])));+ assert.equal(whole.state, null);+ const withoutFloor = groupWorld(kept.state, worldParts.slice(0, 20), null, true, false);+ assert.ok(sameBatches(withoutFloor.batches, groupParts(worldParts.slice(0, 20))));+});++test('groupWorld without a floor returns the incremental grouping as it is', () => {+ const first = groupWorld(null, worldParts.slice(0, 10), null, true, true);+ const next = groupWorld(first.state, worldParts.slice(0, 20), null, true, true);+ assert.ok(sameBatches(next.batches, groupParts(worldParts.slice(0, 20))));+});diff --git a/tests/replace-parts-incremental.test.js b/tests/replace-parts-incremental.test.jsnew file mode 100644index 00000000..02fe45e7--- /dev/null+++ b/tests/replace-parts-incremental.test.js@@ -0,0 +1,64 @@+// Renderer67.replaceParts with retainedUnchanged builds the same batches as a full replaceParts (issue 779).+// It runs in headless Chrome from the Vite dev server; it skips locally when Chrome cannot start and fails on CI.+import test from 'node:test';+import assert from 'node:assert/strict';+import { withDevServerPage } from './helpers/dev-server-browser.js';++async function compareSlicedAndFullBatches(page) {+ const task = async () => {+ const { box } = await import('/src/geometry.js');+ const { Renderer67 } = await import('/src/renderer.js');+ const identity = () => new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);+ const materials = [0, 1].map(index => ({ tag: 'surface', color: [index, 0.5, 0.5], metallic: 0, roughness: 0.5, emission: 0 }));+ const geometries = [1, 2, 3, 4].map(size => box(0, size));+ const makeWorld = () => Array.from({ length: 24 }, (_, index) => ({ name: `part ${index}`, geometry: geometries[Math.floor(index / 6)], matrix: identity(), material: materials[index % 2] }));+ const layout = (renderer) => renderer.batches.map(batch => batch.parts.map(part => part.name));+ const makeRenderer = (world, ground) => {+ const canvas = document.createElement('canvas');+ canvas.width = 32;+ canvas.height = 32;+ document.body.append(canvas);+ return new Renderer67(canvas, world.slice(0, 6), { ground, samples: 1, pixelRatio: 1 });+ };+ const results = {};+ const floorOwnGeometry = { name: 'floor', geometry: box(0, 9), matrix: identity(), material: materials[0] };+ const floorSharedGeometry = { name: 'floor', geometry: geometries[2], matrix: identity(), material: materials[0] };+ const cases = { noGround: () => false, ownGeometry: () => floorOwnGeometry, sharedGeometry: () => floorSharedGeometry };+ for (const [name, ground] of Object.entries(cases)) {+ const world = makeWorld();+ const sliced = makeRenderer(world, ground());+ const full = makeRenderer(world, ground());+ const matches = [];+ for (let end = 12; end <= world.length; end += 6) {+ sliced.replaceParts(world.slice(0, end), { retainedUnchanged: true });+ full.replaceParts(world.slice(0, end));+ matches.push(JSON.stringify(layout(sliced)) === JSON.stringify(layout(full)));+ }+ const keptState = Boolean(sliced.incrementalGrouping);+ sliced.replaceParts(world.slice(0, 18));+ full.replaceParts(world.slice(0, 18));+ results[name] = {+ matches,+ keptState,+ clearedByPlainCall: sliced.incrementalGrouping === null,+ plainCallMatches: JSON.stringify(layout(sliced)) === JSON.stringify(layout(full)),+ batchCount: sliced.batches.length,+ };+ }+ return results;+ };+ return page.evaluate(task);+}++test('replaceParts with retainedUnchanged matches a full replaceParts, with and without a floor', async (context) => {+ await withDevServerPage(context, async (page) => {+ const results = await compareSlicedAndFullBatches(page);+ for (const [name, result] of Object.entries(results)) {+ assert.deepEqual(result.matches, [true, true, true], `${name}: sliced batches differ from the full grouping`);+ assert.equal(result.keptState, true, `${name}: the grouping state is kept between slices`);+ assert.equal(result.clearedByPlainCall, true, `${name}: a call without retainedUnchanged clears the state`);+ assert.equal(result.plainCallMatches, true, `${name}: a plain call groups the whole list`);+ }+ assert.ok(results.ownGeometry.batchCount > results.noGround.batchCount, 'a floor on its own geometry is one more batch');+ });+});diff --git a/tests/texture-upload-bands.test.js b/tests/texture-upload-bands.test.jsdeleted file mode 100644index 49e8a320..00000000--- a/tests/texture-upload-bands.test.js+++ /dev/null@@ -1,152 +0,0 @@-// A large texture goes up in bounded row bands, and the pixels equal a single upload (issue 779).-// The pixel check runs in headless Chrome (SwiftShader) from the Vite dev server and skips when Chrome cannot start.-import test from 'node:test';-import assert from 'node:assert/strict';-import { spawn } from 'node:child_process';-import { freePort } from './helpers/free-port.js';-import { MAX_UPLOAD_PIXELS, uploadImage } from '../src/renderer.js';--function recordingContext() {- const calls = [];- const context = {- TEXTURE_2D: 3553, RGBA: 6408, UNSIGNED_BYTE: 5121, RGBA8: 32856, UNPACK_SKIP_ROWS: 3314,- texImage2D: (...args) => calls.push(['texImage2D', ...args]),- texSubImage2D: (...args) => calls.push(['texSubImage2D', ...args]),- pixelStorei: (name, value) => calls.push(['pixelStorei', name, value]),- };- return { context, calls };-}--test('an image within the limit goes up in one call', () => {- const { context, calls } = recordingContext();- uploadImage(context, {}, 1024, 1024, context.RGBA8, false);- assert.equal(calls.length, 1);- assert.equal(calls[0][0], 'texImage2D');-});--test('a larger image goes up in bands that each stay within the limit and cover every row once', () => {- const { context, calls } = recordingContext();- const width = 3000;- const height = 1500;- uploadImage(context, {}, width, height, context.RGBA8, false);- const bands = calls.filter(call => call[0] === 'texSubImage2D');- assert.ok(bands.length > 1);- let nextRow = 0;- for (const [, , , , destinationRow, bandWidth, rows] of bands) {- assert.equal(destinationRow, nextRow);- assert.equal(bandWidth, width);- assert.ok(bandWidth * rows <= MAX_UPLOAD_PIXELS);- nextRow += rows;- }- assert.equal(nextRow, height);- assert.equal(calls.at(-1)[2], 0, 'the skip-rows state is restored');-});--test('a flipped image stays one upload', () => {- const { context, calls } = recordingContext();- uploadImage(context, {}, 2000, 1000, context.RGBA8, true);- assert.deepEqual(calls.map(call => call[0]), ['texImage2D']);-});--async function startServer(port) {- const server = spawn(process.execPath, ['node_modules/vite/bin/vite.js', '--host', '127.0.0.1', '--port', String(port), '--strictPort'], { windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });- await new Promise((resolve, reject) => {- const timeout = setTimeout(() => reject(new Error('The dev server did not start.')), 30000);- server.stdout.on('data', (data) => {- if (/ready in/.test(data.toString())) {- clearTimeout(timeout);- resolve();- }- });- server.on('exit', (code) => {- clearTimeout(timeout);- reject(new Error(`The dev server exited ${code}.`));- });- });- return server;-}--async function differingBytes(page) {- const task = async () => {- const { box } = await import('/src/geometry.js');- const { Renderer67, MAX_UPLOAD_PIXELS: limit } = await import('/src/renderer.js');- const canvas = document.createElement('canvas');- canvas.width = 64;- canvas.height = 64;- document.body.append(canvas);- const part = { name: 'p', geometry: box(0, 1), matrix: new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]), material: { tag: 'surface', color: [1, 1, 1], metallic: 0, roughness: 0.5, emission: 0 } };- const renderer = new Renderer67(canvas, [part], { ground: false, samples: 1, pixelRatio: 1 });- const gl = renderer.context;- const width = 1280;- const height = Math.ceil(limit / width) * 2 + 7;- const source = document.createElement('canvas');- source.width = width;- source.height = height;- const sourceContext = source.getContext('2d');- const imageData = sourceContext.createImageData(width, height);- for (let row = 0; row < height; row++) {- for (let column = 0; column < width; column++) {- const offset = (row * width + column) * 4;- imageData.data.set([column % 251, row % 241, (column + row) % 239, 255], offset);- }- }- sourceContext.putImageData(imageData, 0, 0);- const read = (texture) => {- const framebuffer = gl.createFramebuffer();- gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);- gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);- const pixels = new Uint8Array(width * height * 4);- gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels);- gl.bindFramebuffer(gl.FRAMEBUFFER, null);- gl.deleteFramebuffer(framebuffer);- return pixels;- };- const results = {};- for (const flipY of [false, true]) {- const banded = renderer.createTexture({ image: source, colorSpace: 'linear', flipY, minFilter: 9729 });- const whole = gl.createTexture();- gl.bindTexture(gl.TEXTURE_2D, whole);- gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY);- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, gl.RGBA, gl.UNSIGNED_BYTE, source);- gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);- const bandedPixels = read(banded);- const wholePixels = read(whole);- let differing = 0;- for (let index = 0; index < bandedPixels.length; index++) if (bandedPixels[index] !== wholePixels[index]) differing++;- results[flipY ? 'flipped' : 'upright'] = { differing, bytes: bandedPixels.length, error: gl.getError() };- }- return results;- };- return page.evaluate(task);-}--test('banded upload gives the same pixels as one upload, upright, and flipped stays whole', async (context) => {- let chromium;- try {- ({ chromium } = await import('playwright'));- } catch {- context.skip('playwright is not installed');- return;- }- const port = await freePort();- const server = await startServer(port);- let browser;- try {- try {- browser = await chromium.launch({ headless: true, args: ['--use-angle=swiftshader', '--enable-unsafe-swiftshader', '--enable-webgl', '--ignore-gpu-blocklist'] });- } catch (error) {- context.skip(`Chrome did not start: ${error.message.split('\n')[0]}`);- return;- }- const page = await browser.newPage({ viewport: { width: 128, height: 128 } });- await page.goto(`http://127.0.0.1:${port}/`);- const results = await differingBytes(page);- for (const [name, result] of Object.entries(results)) {- assert.equal(result.error, 0, `${name} upload raised a GL error`);- assert.equal(result.differing, 0, `${name}: ${result.differing} of ${result.bytes} bytes differ`);- }- } finally {- await browser?.close();- server.kill();- }-});