3D engine that AI agents drive
Share the sampler setup as a function and cover preloadTextures in the hero swap test (#779)
Nguyen Xuan An731515cfbce7
diff --git a/changes/779.size.md b/changes/779.size.mdindex e659636f..2ec312f1 100644--- a/changes/779.size.md+++ b/changes/779.size.md@@ -7,3 +7,4 @@- 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 imports+- entry:texture-update: the entry bundles the renderer, which now carries preloadTextures and the shared sampler setup; the sliced upload itself loads on demanddiff --git a/src/renderer.js b/src/renderer.jsindex 8b26b6c8..2b29b865 100644--- a/src/renderer.js+++ b/src/renderer.js@@ -569,6 +569,19 @@ export function groupWorld(previous, parts, groundPart, batching, retainedUnchanreturn { batches, state: grouped.state };}+// Sampler state and mipmaps of the texture bound to TEXTURE_2D, once its level 0 is uploaded. Needs only the renderer's context and applyAnisotropy.+export function configureTexture(renderer, info) {+ const context = renderer.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);+ renderer.applyAnisotropy(context.TEXTURE_2D);+ }+}+export function textureDescriptorInfo(map, maximumSize = Infinity) {const width = map.image?.naturalWidth ?? map.image?.width;const height = map.image?.naturalHeight ?? map.image?.height;@@ -1040,19 +1053,6 @@ 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@@ -1080,7 +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);- this.configureTexture(info);+ configureTexture(this, 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.jsindex 5d73cca6..01388d75 100644--- a/src/texture-upload.js+++ b/src/texture-upload.js@@ -1,4 +1,4 @@-import { textureDescriptorInfo } from './renderer.js';+import { configureTexture, 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.@@ -99,7 +99,7 @@ export async function preloadRendererTextures(renderer, descriptors, { maximumBy}const previousBinding = context.getParameter(context.TEXTURE_BINDING_2D);context.bindTexture(context.TEXTURE_2D, texture);- renderer.configureTexture(info);+ configureTexture(renderer, info);context.bindTexture(context.TEXTURE_2D, previousBinding);renderer.textures.set(map, texture);} catch (error) {diff --git a/tests/hero-progressive.test.js b/tests/hero-progressive.test.jsindex 5745c3d9..b1dda276 100644--- a/tests/hero-progressive.test.js+++ b/tests/hero-progressive.test.js@@ -278,6 +278,7 @@ test('a first view that fails to build is replaced by the full world, and a fullfunction fakeRenderer(calls) {return {prepareEnvironmentInSlices: async () => { calls.push('environment'); },+ preloadTextures: async () => { calls.push('preloadTextures'); },replaceParts: () => { calls.push('replaceParts'); },prepareFrame: async () => { calls.push('prepareFrame'); },};@@ -288,11 +289,15 @@ const fakeParts = [{ geometry: { indices: [0, 1, 2] } }, { geometry: { indices:test('the swap prefilters the sky before it replaces the parts, and stops when the world is freed on the way', async () => {const calls = [];assert.equal(await replaceRendererWorld(fakeRenderer(calls), fakeParts, {}, () => false), true);- assert.equal(calls[0], 'environment');- assert.ok(calls.length > 1 && calls.slice(1).every(name => name === 'replaceParts'));+ assert.deepEqual(calls.slice(0, 2), ['environment', 'preloadTextures'], 'large textures upload in bands before the parts arrive');+ assert.ok(calls.length > 2 && calls.slice(2).every(name => name === 'replaceParts'));const freedCalls = [];assert.equal(await replaceRendererWorld(fakeRenderer(freedCalls), fakeParts, {}, () => true), false);assert.deepEqual(freedCalls, ['environment']);+ const freedDuringUploadCalls = [];+ const answers = [false, true];+ assert.equal(await replaceRendererWorld(fakeRenderer(freedDuringUploadCalls), fakeParts, {}, () => answers.shift() ?? true), false);+ assert.deepEqual(freedDuringUploadCalls, ['environment', 'preloadTextures']);});test('the swap prepares a draw at the last pose, and none when the world was never drawn', async () => {