3D engine that AI agents drive
Slice large texture uploads and measure long tasks after first paint (#779)
Nguyen Xuan An00cb3a10f816
diff --git a/changes/779.md b/changes/779.mdnew file mode 100644index 00000000..4f2b627e--- /dev/null+++ b/changes/779.md@@ -0,0 +1 @@+- `renderer.preloadTextures(parts)` uploads large textures in bands of at most 1 MiB with a yield after each, so a world switch no longer hands the driver a whole image in one call, and `measure-hero-load.mjs --watch-seconds` counts the main-thread tasks over 50 ms after first paint (#779)diff --git a/changes/779.size.md b/changes/779.size.mdnew file mode 100644index 00000000..e659636f--- /dev/null+++ b/changes/779.size.md@@ -0,0 +1,9 @@+- entry:core: about 0.8 KB gzip buy texture uploads in bounded bands with a yield after each, so a world switch no longer hands the driver a whole image in one call; no smaller form keeps the upload bounded+- scene:lit-box: the same code; the size bundle inlines the on-demand import although only a scene that calls preloadTextures loads it+- scene:gltf-box: the same code, counted the same way+- scene:gltf-box+lazy: the same code, counted the same way+- scene:agent-scene: the same code, counted the same way+- scene:agent-scene+lazy: the same code, counted the same way+- goal:lit-box: the code loads on demand at run time and counts here only because the size bundle inlines dynamic imports+- goal:gltf-box: the code loads on demand at run time and counts here only because the size bundle inlines dynamic imports+- goal:agent-scene: the code loads on demand at run time and counts here only because the size bundle inlines dynamic importsdiff --git a/docs/gltf.md b/docs/gltf.mdindex 13f0cb05..79826087 100644--- a/docs/gltf.md+++ b/docs/gltf.md@@ -116,6 +116,7 @@ renderer.replaceParts(asset.parts);`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, 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. An appended part joins the batch that already has its geometry and material, or starts a new batch after the others, exactly where grouping the whole list would put it. A prefix that changes, reorders or removes earlier parts groups the whole list as before. Shader status and logs are read only when a program fails to link, and the shadow framebuffer and the anisotropy limit are asked for once, because each read is a round trip to the GPU process that waits for all the work queued before it (30 to 70 ms each on a busy card).+Before `replaceParts`, `await renderer.preloadTextures(parts)` uploads the textures that are larger than `maximumBytes` (1 MiB by default) a band of rows at a time, with `checkpoint` (an async function that yields, `createSlicer()` by default) after each band, so one upload never hands the driver a whole image. The pixels equal a single upload, including `flipY`; `replaceParts` then finds the textures ready. Compressed and small textures are left to the call that needs them, and the constructor still uploads synchronously.`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/scripts/measure-hero-load.mjs b/scripts/measure-hero-load.mjsindex 20895f65..982336c2 100644--- a/scripts/measure-hero-load.mjs+++ b/scripts/measure-hero-load.mjs@@ -3,6 +3,7 @@// node scripts/measure-hero-load.mjs --dist <built site folder> [--label name] [--runs 3] [--conditions fast4g,slow,none]// [--viewports desktop,phone] [--out directory]// [--cpu 4] CPU slowdown factor (emulated by the DevTools protocol, not a slow device)+// [--watch-seconds N] keep each page open N seconds after the swap (the loop changes worlds) and report the main-thread tasks over 50 ms after first paint// [--origin <url>] measure a deployed site instead of serving --dist (it then needs no folder)// node scripts/measure-hero-load.mjs --dist <folder> --stills <directory> stills before and after the swap, with PSNR// node scripts/measure-hero-load.mjs --dist <folder> --video <file.mp4> CDP screencast of the first 8 s on Fast 4G@@ -39,7 +40,7 @@ const CONTENT_TYPES = { '.html': 'text/html', '.js': 'text/javascript', '.css':const TEXT_TYPES = new Set(['.html', '.js', '.css', '.json', '.svg']);function parseArguments(argv) {- const options = { dist: null, label: 'build', runs: 3, conditions: ['fast4g', 'slow', 'none'], viewports: ['desktop', 'phone'], out: null, cpu: 1, remoteOrigin: null, stills: null, video: null, timeoutSeconds: 120 };+ const options = { dist: null, label: 'build', runs: 3, conditions: ['fast4g', 'slow', 'none'], viewports: ['desktop', 'phone'], out: null, cpu: 1, remoteOrigin: null, stills: null, video: null, timeoutSeconds: 120, watchSeconds: 0 };for (let index = 0; index < argv.length; index++) {const flag = argv[index];const value = () => argv[++index];@@ -54,6 +55,7 @@ function parseArguments(argv) {else if (flag === '--stills') options.stills = resolve(value());else if (flag === '--video') options.video = resolve(value());else if (flag === '--timeout') options.timeoutSeconds = Number(value());+ else if (flag === '--watch-seconds') options.watchSeconds = Number(value());else throw new Error(`Unknown argument ${flag}.`);}if (!options.dist && options.remoteOrigin) return options;@@ -103,6 +105,11 @@ function installFrameTimer(marker) {for (const entry of list.getEntries()) if (!entry.hadRecentInput) window.__layoutShift += entry.value;}).observe({ type: 'layout-shift', buffered: true });// Gaps between animation frames over 50 ms: the main thread was blocked. The swap to the full world should leave none that is long.+ // Main-thread tasks over 50 ms (the Long Tasks API), kept with their start so a run can count the ones after the first paint.+ window.__longTasks = [];+ new PerformanceObserver(list => {+ for (const entry of list.getEntries()) window.__longTasks.push([Math.round(entry.startTime), Math.round(entry.duration)]);+ }).observe({ type: 'longtask', buffered: true });window.__frameGaps = [];let previousFrameAt = performance.now();const watchFrameGaps = now => {@@ -170,7 +177,8 @@ async function measureOnce(browser, options, viewportName, conditionName) {const failed = events.find(event => event.kind === 'failed');const swapEnd = detailed?.at ?? Infinity;const swapGaps = (window.__frameGaps ?? []).filter(([at, gap]) => at + gap > swapEnd - (detailed?.ms ?? 0) - 100 && at < swapEnd + 100);- return { lcp: window.__largestContentfulPaint, layoutShift: window.__layoutShift, first: window.__firstWorldDraw, detailed: detailed?.at ?? null, swapMs: detailed?.ms ?? null, swapHeldMs: (window.__phases ?? []).filter(phase => phase[0] === "swap").reduce((total, phase) => total + phase[3] - phase[2], 0), swapLongestGapMs: swapGaps.length ? Math.max(...swapGaps.map(([, gap]) => gap)) : 0, failed: failed?.message ?? null, state: window.__loop?.detail?.() ?? null, mode: window.__heroMode };+ const longTasksAfterFirstPaint = (window.__longTasks ?? []).filter(([start]) => window.__firstWorldDraw !== null && start >= window.__firstWorldDraw);+ return { longTaskCount: longTasksAfterFirstPaint.length, longestTaskMs: longTasksAfterFirstPaint.reduce((longest, [, duration]) => Math.max(longest, duration), 0), longTasks: longTasksAfterFirstPaint, lcp: window.__largestContentfulPaint, layoutShift: window.__layoutShift, first: window.__firstWorldDraw, detailed: detailed?.at ?? null, swapMs: detailed?.ms ?? null, swapHeldMs: (window.__phases ?? []).filter(phase => phase[0] === "swap").reduce((total, phase) => total + phase[3] - phase[2], 0), swapLongestGapMs: swapGaps.length ? Math.max(...swapGaps.map(([, gap]) => gap)) : 0, failed: failed?.message ?? null, state: window.__loop?.detail?.() ?? null, mode: window.__heroMode };});const deadline = Date.now() + options.timeoutSeconds * 1000;let status = await finished();@@ -178,6 +186,10 @@ async function measureOnce(browser, options, viewportName, conditionName) {await new Promise(resolveWait => setTimeout(resolveWait, 100));status = await finished();}+ if (options.watchSeconds > 0) {+ await new Promise(resolveWait => setTimeout(resolveWait, options.watchSeconds * 1000));+ status = await finished();+ }const poster = await page.evaluate(() => {const entry = performance.getEntriesByType('resource').find(resource => resource.name.includes('poster-b-port'));return entry ? Math.round(entry.responseEnd) : null;@@ -195,6 +207,9 @@ async function measureOnce(browser, options, viewportName, conditionName) {detailMs,swapMs: status.swapMs,swapLongestGapMs: status.swapLongestGapMs,+ longTaskCount: status.longTaskCount,+ longestTaskMs: status.longestTaskMs,+ longTasks: status.longTasks,swapHeldMs: status.swapHeldMs,bytesBeforeFirstFrame: bytes.atFirstFrame,bytesAtDetail: detailMs === null ? null : bytesAt(bytes, detailMs),@@ -231,6 +246,8 @@ async function runMatrix(browser, options) {swapMs: median(runs.map(run => run.swapMs)),swapHeldMs: median(runs.map(run => run.swapHeldMs)),swapLongestGapMs: median(runs.map(run => run.swapLongestGapMs)),+ longTasksOver50AfterFirstPaint: runs.map(run => run.longTaskCount),+ longestTaskMs: Math.max(...runs.map(run => run.longestTaskMs)),detailRangeMs: [Math.min(...runs.map(run => run.detailMs ?? Infinity)), Math.max(...runs.map(run => run.detailMs ?? 0))],bytesBeforeFirstFrame: median(runs.map(run => run.bytesBeforeFirstFrame)),bytesAtDetail: median(runs.map(run => run.bytesAtDetail)),diff --git a/src/hero-lab/loop/world.js b/src/hero-lab/loop/world.jsindex 52687fd0..348d99e4 100644--- a/src/hero-lab/loop/world.js+++ b/src/hero-lab/loop/world.js@@ -176,6 +176,9 @@ export async function replaceRendererWorld(renderer, parts, environmentMap, isFrawait renderer.prepareEnvironmentInSlices(environmentMap, createSlicer(SLICE_MILLISECONDS));recordSwapPhase('prefilter', prefilterStart);if (isFreed()) return false;+ // Large textures go to the driver in bands between slices, so replaceParts finds them uploaded.+ await renderer.preloadTextures(parts, { checkpoint: createSlicer(SLICE_MILLISECONDS) });+ if (isFreed()) return false;const replaceStart = performance.now();renderer.replaceParts(parts);recordSwapPhase('replaceParts', replaceStart);diff --git a/src/renderer.js b/src/renderer.jsindex 0833f9ff..8b26b6c8 100644--- a/src/renderer.js+++ b/src/renderer.js@@ -1040,6 +1040,32 @@ export class Renderer67 {context.texParameterf(target, extension.TEXTURE_MAX_ANISOTROPY_EXT, this.supportedAnisotropy);}+ // Sampler state and mipmaps of the texture bound to TEXTURE_2D, once its level 0 is uploaded.+ configureTexture(info) {+ const context = this.context;+ 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);+ context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MAG_FILTER, info.magFilter);+ if (info.mipmaps) {+ context.generateMipmap(context.TEXTURE_2D);+ this.applyAnisotropy(context.TEXTURE_2D);+ }+ }++ /**+ * Uploads the large textures of `source` (parts, or texture descriptors) in bands of at most `maximumBytes`, yielding to+ * the page after each band, so a later replaceParts or createTexture finds them ready instead of handing the driver every+ * image in one call. Pixels equal a single upload. Compressed and small textures are left to createTexture.+ * `checkpoint` is an async function that yields (createSlicer from cooperative.js).+ */+ async preloadTextures(source, options = {}) {+ const descriptors = source.flatMap(item => (item?.material ? materialMapState(item.material).slice(0, 5) : [item]));+ // The slicing code loads on demand, so a renderer that never preloads does not carry it.+ const { preloadRendererTextures } = await import('./texture-upload.js');+ await preloadRendererTextures(this, descriptors, options);+ }+createTexture(map) {if (this.textures.has(map)) return this.textures.get(map);const context = this.context;@@ -1054,14 +1080,7 @@ export class Renderer67 {const info = textureDescriptorInfo(map, context.getParameter(context.MAX_TEXTURE_SIZE));if (map.image.compressed) map.image.compressed.upload(context);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);- context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MAG_FILTER, info.magFilter);- if (info.mipmaps) {- context.generateMipmap(context.TEXTURE_2D);- this.applyAnisotropy(context.TEXTURE_2D);- }+ this.configureTexture(info);} else {context.texImage2D(context.TEXTURE_2D, 0, context.RGBA8, 1, 1, 0, context.RGBA, context.UNSIGNED_BYTE, new Uint8Array([255, 255, 255, 255]));context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MIN_FILTER, context.LINEAR);diff --git a/src/texture-upload.js b/src/texture-upload.jsnew file mode 100644index 00000000..5d73cca6--- /dev/null+++ b/src/texture-upload.js@@ -0,0 +1,110 @@+import { textureDescriptorInfo } from './renderer.js';+import { createSlicer } from './cooperative.js';++// Uploads a large texture a band of rows at a time, so no single texImage2D hands the driver the whole image at once.+// The plan needs no GL context, so tests can reach it directly.++/** Largest band of texture data one texSubImage2D call carries. */+export const TEXTURE_SLICE_BYTES = 1 << 20;++/**+ * Splits `height` rows of `bytesPerRow` into bands of at most `maximumBytes` (at least one row each).+ * Returns [{ firstRow, rowCount }] that cover every row once, in order.+ */+export function planRowSlices(height, bytesPerRow, maximumBytes = TEXTURE_SLICE_BYTES) {+ if (!Number.isInteger(height) || height <= 0) throw new RangeError('planRowSlices height must be a positive integer.');+ if (!(bytesPerRow > 0) || !(maximumBytes > 0)) throw new RangeError('planRowSlices bytesPerRow and maximumBytes must be positive.');+ const rowsPerSlice = Math.max(1, Math.floor(maximumBytes / bytesPerRow));+ const slices = [];+ for (let firstRow = 0; firstRow < height; firstRow += rowsPerSlice) {+ slices.push({ firstRow, rowCount: Math.min(rowsPerSlice, height - firstRow) });+ }+ return slices;+}++/**+ * The texture row a band lands on. WebGL flips a sub-rectangle inside itself when UNPACK_FLIP_Y_WEBGL is on, so with+ * `flipY` the band goes to the mirrored rows and the whole image comes out as one flipped upload would make it.+ */+export function sliceDestinationRow(slice, height, flipY) {+ return flipY ? height - slice.firstRow - slice.rowCount : slice.firstRow;+}++/** A band of an image source, cut without changing its pixels. RGBA bytes ({ width, height, data }) are viewed in place. */+async function bandOf(image, width, slice, flipY) {+ if (image.data) return image.data.subarray(slice.firstRow * width * 4, (slice.firstRow + slice.rowCount) * width * 4);+ return createImageBitmap(image, 0, slice.firstRow, width, slice.rowCount, {+ premultiplyAlpha: 'none',+ colorSpaceConversion: 'none',+ imageOrientation: flipY ? 'flipY' : 'none',+ });+}++/** WebGL ignores UNPACK_FLIP_Y_WEBGL for an ImageBitmap, whose orientation was fixed when it was made. */+const ignoresFlip = image => typeof ImageBitmap === 'function' && image instanceof ImageBitmap;++/**+ * Fills level 0 of `texture` (RGBA, UNSIGNED_BYTE) from `image` in bands of at most `maximumBytes`, calling `checkpoint()`+ * after each so the page can draw. The caller sets the texture parameters and mipmaps afterwards. `image` is an image+ * source or { width, height, data } of RGBA bytes. Each band sets and restores the pixel store and binding it touches,+ * because the page draws between bands.+ */+export async function uploadTextureInSlices(context, texture, internalFormat, image, { flipY: requestedFlipY = false, maximumBytes = TEXTURE_SLICE_BYTES, checkpoint = async () => {} } = {}) {+ const width = image.naturalWidth ?? image.width;+ const height = image.naturalHeight ?? image.height;+ const flipY = requestedFlipY && !ignoresFlip(image);+ const target = context.TEXTURE_2D;+ const previousAllocationBinding = context.getParameter(context.TEXTURE_BINDING_2D);+ context.bindTexture(target, texture);+ context.texImage2D(target, 0, internalFormat, width, height, 0, context.RGBA, context.UNSIGNED_BYTE, null);+ context.bindTexture(target, previousAllocationBinding);+ for (const slice of planRowSlices(height, width * 4, maximumBytes)) {+ const band = await bandOf(image, width, slice, flipY);+ const previousBinding = context.getParameter(context.TEXTURE_BINDING_2D);+ const previousFlip = context.getParameter(context.UNPACK_FLIP_Y_WEBGL);+ const previousPremultiply = context.getParameter(context.UNPACK_PREMULTIPLY_ALPHA_WEBGL);+ const previousColorSpace = context.getParameter(context.UNPACK_COLORSPACE_CONVERSION_WEBGL);+ try {+ context.bindTexture(target, texture);+ context.pixelStorei(context.UNPACK_FLIP_Y_WEBGL, flipY);+ context.pixelStorei(context.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);+ context.pixelStorei(context.UNPACK_COLORSPACE_CONVERSION_WEBGL, context.NONE);+ context.texSubImage2D(target, 0, 0, sliceDestinationRow(slice, height, flipY), width, slice.rowCount, context.RGBA, context.UNSIGNED_BYTE, band);+ } finally {+ context.pixelStorei(context.UNPACK_FLIP_Y_WEBGL, previousFlip);+ context.pixelStorei(context.UNPACK_PREMULTIPLY_ALPHA_WEBGL, previousPremultiply);+ context.pixelStorei(context.UNPACK_COLORSPACE_CONVERSION_WEBGL, previousColorSpace);+ context.bindTexture(target, previousBinding);+ band.close?.();+ }+ await checkpoint();+ }+}++/** Renderer67.preloadTextures: uploads each descriptor larger than maximumBytes in bands, then records it where createTexture looks. */+export async function preloadRendererTextures(renderer, descriptors, { maximumBytes = TEXTURE_SLICE_BYTES, checkpoint = createSlicer() } = {}) {+ const context = renderer.context;+ const maximumSize = context.getParameter(context.MAX_TEXTURE_SIZE);+ for (const map of new Set(descriptors)) {+ if (!map || renderer.textures.has(map) || map.image?.compressed) continue;+ const info = textureDescriptorInfo(map, maximumSize);+ if (info.width * info.height * 4 <= maximumBytes) continue;+ const texture = context.createTexture();+ try {+ await uploadTextureInSlices(context, texture, map.colorSpace === 'linear' ? context.RGBA8 : context.SRGB8_ALPHA8, map.image, { flipY: map.flipY ?? false, maximumBytes, checkpoint });+ // Another call may have uploaded the same descriptor while this one yielded.+ if (renderer.textures.has(map)) {+ context.deleteTexture(texture);+ continue;+ }+ const previousBinding = context.getParameter(context.TEXTURE_BINDING_2D);+ context.bindTexture(context.TEXTURE_2D, texture);+ renderer.configureTexture(info);+ context.bindTexture(context.TEXTURE_2D, previousBinding);+ renderer.textures.set(map, texture);+ } catch (error) {+ context.deleteTexture(texture);+ throw error;+ }+ }+}diff --git a/tests/incremental-grouping-edits.test.js b/tests/incremental-grouping-edits.test.jsnew file mode 100644index 00000000..1005a91c--- /dev/null+++ b/tests/incremental-grouping-edits.test.js@@ -0,0 +1,52 @@+import assert from 'node:assert/strict';+import test from 'node:test';+import { groupParts, 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 = () => ({+ 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]),+});++function seededRandom(seed) {+ let state = seed >>> 0;+ return () => {+ state = (state * 1664525 + 1013904223) >>> 0;+ return state / 2 ** 32;+ };+}++const sameBatches = (left, right) => left.length === right.length && left.every((batch, index) => batch.length === right[index].length && batch.every((part, position) => part === right[index][position]));++// replaceParts({ retainedUnchanged: true }) may be given any edit of the previous list: appended, removed, replaced or reordered parts.+// Whatever the edit, the batches must equal one full regroup of the same list.+test('random edits of a world group exactly as one full regroup does, for 30 seeds', () => {+ for (let seed = 1; seed <= 30; seed++) {+ const random = seededRandom(seed * 104729);+ const geometries = Array.from({ length: 6 }, makeGeometry);+ const materials = Array.from({ length: 4 }, (_, index) => ({ color: [index / 4, 0.5, 0.5], metallic: 0, roughness: 0.5, emission: 0 }));+ let counter = 0;+ const newPart = () => ({+ name: `part ${counter++}`,+ geometry: geometries[Math.floor(random() * geometries.length)],+ matrix: identity(),+ material: materials[Math.floor(random() * materials.length)],+ castShadow: random() < 0.8,+ });+ let parts = Array.from({ length: 5 + Math.floor(random() * 20) }, newPart);+ const groundPart = random() < 0.5 ? { name: 'ground', geometry: random() < 0.5 ? geometries[0] : makeGeometry(), matrix: identity(), material: { ...materials[0], tag: 'floor' } } : null;+ let state = null;+ for (let step = 0; step < 25; step++) {+ const edit = random();+ if (edit < 0.5) parts = [...parts, ...Array.from({ length: 1 + Math.floor(random() * 6) }, newPart)];+ else if (edit < 0.65 && parts.length > 2) parts = parts.filter(() => random() < 0.8);+ else if (edit < 0.8 && parts.length > 0) parts = parts.map(part => (random() < 0.2 ? newPart() : part));+ else if (edit < 0.9) parts = [...parts].sort(() => random() - 0.5);+ const grouped = groupWorld(state, parts, groundPart, true, true);+ state = grouped.state;+ const expected = groupParts(groundPart ? [...parts, groundPart] : parts, true);+ assert.ok(sameBatches(grouped.batches, expected), `seed ${seed} step ${step}`);+ }+ }+});diff --git a/tests/texture-upload.test.js b/tests/texture-upload.test.jsnew file mode 100644index 00000000..ee6af4f4--- /dev/null+++ b/tests/texture-upload.test.js@@ -0,0 +1,173 @@+import assert from 'node:assert/strict';+import test from 'node:test';+import { planRowSlices, sliceDestinationRow, uploadTextureInSlices, TEXTURE_SLICE_BYTES } from '../src/texture-upload.js';+import { withDevServerPage } from './helpers/dev-server-browser.js';++function seededRandom(seed) {+ let state = seed >>> 0;+ return () => {+ state = (state * 1664525 + 1013904223) >>> 0;+ return state / 2 ** 32;+ };+}++test('row slices cover every row once, in order, each within the byte bound', () => {+ const random = seededRandom(779);+ for (let trial = 0; trial < 200; trial++) {+ const height = 1 + Math.floor(random() * 5000);+ const bytesPerRow = 4 * (1 + Math.floor(random() * 4096));+ const maximumBytes = 1 + Math.floor(random() * 4 * TEXTURE_SLICE_BYTES);+ const slices = planRowSlices(height, bytesPerRow, maximumBytes);+ let nextRow = 0;+ for (const slice of slices) {+ assert.equal(slice.firstRow, nextRow);+ assert.ok(slice.rowCount >= 1);+ assert.ok(slice.rowCount * bytesPerRow <= Math.max(maximumBytes, bytesPerRow), 'a band stays within the bound unless one row alone exceeds it');+ nextRow += slice.rowCount;+ }+ assert.equal(nextRow, height);+ }+});++test('a texture that fits the bound is one band, and bad input is refused', () => {+ assert.deepEqual(planRowSlices(64, 256, TEXTURE_SLICE_BYTES), [{ firstRow: 0, rowCount: 64 }]);+ assert.deepEqual(planRowSlices(5, 1000, 1), [0, 1, 2, 3, 4].map(firstRow => ({ firstRow, rowCount: 1 })));+ assert.throws(() => planRowSlices(0, 4), RangeError);+ assert.throws(() => planRowSlices(4, 0), RangeError);+});++test('with flipY the bands land on the mirrored rows', () => {+ assert.equal(sliceDestinationRow({ firstRow: 0, rowCount: 3 }, 10, false), 0);+ assert.equal(sliceDestinationRow({ firstRow: 0, rowCount: 3 }, 10, true), 7);+ assert.equal(sliceDestinationRow({ firstRow: 3, rowCount: 3 }, 10, true), 4);+ assert.equal(sliceDestinationRow({ firstRow: 9, rowCount: 1 }, 10, true), 0);+});++// A context that records texSubImage2D into a byte grid, flipping each band inside itself as WebGL does.+function recordingContext(width, height) {+ const grid = new Uint8Array(width * height * 4);+ const calls = [];+ const state = { flipY: false };+ const context = {+ TEXTURE_2D: 1, TEXTURE_BINDING_2D: 2, UNPACK_FLIP_Y_WEBGL: 3, UNPACK_PREMULTIPLY_ALPHA_WEBGL: 4, UNPACK_COLORSPACE_CONVERSION_WEBGL: 5, NONE: 0, RGBA: 6, UNSIGNED_BYTE: 7,+ getParameter: name => (name === 3 ? state.flipY : null),+ bindTexture() {}, texImage2D() {},+ pixelStorei(name, value) { if (name === 3) state.flipY = value; },+ texSubImage2D(target, level, x, y, bandWidth, bandHeight, format, type, pixels) {+ calls.push(pixels.byteLength);+ for (let row = 0; row < bandHeight; row++) {+ const sourceRow = state.flipY ? bandHeight - 1 - row : row;+ grid.set(pixels.subarray(sourceRow * bandWidth * 4, (sourceRow + 1) * bandWidth * 4), ((y + row) * width + x) * 4);+ }+ },+ };+ return { context, grid, calls };+}++for (const flipY of [false, true]) {+ test(`sliced upload of raw bytes equals one upload (flipY ${flipY}), with every call bounded and a yield after each`, async () => {+ const width = 37;+ const height = 101;+ const data = new Uint8Array(width * height * 4).map((_, index) => (index * 7919) % 251);+ const { context, grid, calls } = recordingContext(width, height);+ let checkpoints = 0;+ await uploadTextureInSlices(context, {}, 0, { width, height, data }, { flipY, maximumBytes: 5000, checkpoint: async () => { checkpoints++; } });+ const expected = new Uint8Array(data.length);+ for (let row = 0; row < height; row++) {+ const sourceRow = flipY ? height - 1 - row : row;+ expected.set(data.subarray(sourceRow * width * 4, (sourceRow + 1) * width * 4), row * width * 4);+ }+ assert.deepEqual(grid, expected);+ assert.ok(calls.length > 1);+ assert.ok(calls.every(bytes => bytes <= 5000));+ assert.equal(checkpoints, calls.length);+ });+}++test('in Chrome a sliced upload reads back the pixels a single upload gives, flipped or not, with alpha', async (testContext) => {+ await withDevServerPage(testContext, async (page) => {+ const result = await page.evaluate(async () => {+ const { uploadTextureInSlices } = await import('/src/texture-upload.js');+ const width = 64;+ const height = 90;+ const canvas = document.createElement('canvas');+ canvas.width = width;+ canvas.height = height;+ const painter = canvas.getContext('2d');+ for (let row = 0; row < height; row++) {+ painter.fillStyle = `rgba(${row * 2}, ${255 - row * 2}, ${(row * 37) % 256}, ${0.2 + (row % 5) * 0.2})`;+ painter.fillRect(row % width, row, width - (row % width), 1);+ }+ // A glTF texture arrives as an unpremultiplied ImageBitmap, whose rows are cut without rounding.+ const bitmap = await createImageBitmap(canvas, { premultiplyAlpha: 'none', colorSpaceConversion: 'none' });+ // A canvas source is premultiplied, so only an opaque one reads back the same through both paths.+ const opaque = document.createElement('canvas');+ opaque.width = width;+ opaque.height = height;+ const opaquePainter = opaque.getContext('2d');+ for (let row = 0; row < height; row++) {+ opaquePainter.fillStyle = `rgb(${row * 2}, ${255 - row * 2}, ${(row * 37) % 256})`;+ opaquePainter.fillRect(row % width, row, width - (row % width), 1);+ }+ const gl = document.createElement('canvas').getContext('webgl2');+ const framebuffer = gl.createFramebuffer();+ const read = (texture) => {+ 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);+ return Array.from(pixels);+ };+ const outcome = {};+ for (const [kind, image] of [['bitmap', bitmap], ['canvas', opaque]]) for (const flipY of [false, true]) {+ const whole = gl.createTexture();+ gl.bindTexture(gl.TEXTURE_2D, whole);+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY);+ gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE);+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, gl.RGBA, gl.UNSIGNED_BYTE, image);+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);+ const sliced = gl.createTexture();+ await uploadTextureInSlices(gl, sliced, gl.RGBA8, image, { flipY, maximumBytes: 64 * 4 * 7 });+ outcome[kind + flipY] = { identical: JSON.stringify(read(whole)) === JSON.stringify(read(sliced)), restoredFlip: gl.getParameter(gl.UNPACK_FLIP_Y_WEBGL) === false };+ }+ return outcome;+ });+ for (const key of ['bitmapfalse', 'bitmaptrue', 'canvasfalse', 'canvastrue']) {+ assert.equal(result[key].identical, true, `pixels differ for ${key}`);+ assert.equal(result[key].restoredFlip, true);+ }+ });+});++test('preloadTextures uploads a large texture in bands and createTexture then reuses it, while a small one is left alone', async (testContext) => {+ await withDevServerPage(testContext, async (page) => {+ const result = await page.evaluate(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 material = { tag: 'surface', color: [0.5, 0.5, 0.5], metallic: 0, roughness: 0.5, emission: 0 };+ const canvas = document.createElement('canvas');+ canvas.width = 32;+ canvas.height = 32;+ document.body.append(canvas);+ const renderer = new Renderer67(canvas, [{ name: 'box', geometry: box(0, 1), matrix: identity, material }], { samples: 1, pixelRatio: 1 });+ const imageOf = size => Object.assign(document.createElement('canvas'), { width: size, height: size });+ const large = { image: imageOf(256) };+ const small = { image: imageOf(8) };+ let bands = 0;+ const texSubImage2D = renderer.context.texSubImage2D.bind(renderer.context);+ renderer.context.texSubImage2D = (...parameters) => {+ bands++;+ return texSubImage2D(...parameters);+ };+ await renderer.preloadTextures([{ material: { ...material, map: large } }, { material: { ...material, map: small } }], { maximumBytes: 256 * 4 * 16 });+ const uploaded = renderer.textures.get(large);+ return { bands, largeUploaded: Boolean(uploaded), smallLeftAlone: !renderer.textures.has(small), reused: renderer.createTexture(large) === uploaded, error: renderer.context.getError() };+ });+ assert.equal(result.bands, 16);+ assert.equal(result.largeUploaded, true);+ assert.equal(result.smallLeftAlone, true);+ assert.equal(result.reused, true);+ assert.equal(result.error, 0);+ });+});