3D engine that AI agents drive
Move compressed upload into the glTF addon and reset size budgets
Nguyen Xuan An96f6b4220bc0
diff --git a/bench/size/budgets.json b/bench/size/budgets.jsonindex 71396b6c..94351c3f 100644--- a/bench/size/budgets.json+++ b/bench/size/budgets.json@@ -6,13 +6,13 @@"agent-scene": 100000},"budgets": {- "entry:core": 88533,+ "entry:core": 88015,"entry:model": 14400,"entry:nature": 13200,"entry:motion": 15260,"entry:canvas2d": 9400,- "entry:gltf": 57393,- "entry:gltf+lazy": 60721,+ "entry:gltf": 57607,+ "entry:gltf+lazy": 60935,"entry:modeling": 8700,"entry:sculpt": 2500,"entry:surface": 10100,@@ -45,10 +45,10 @@"entry:material-layer-shaders": 21400,"entry:light-linking": 19720,"entry:cascaded-shadows": 28530,- "scene:lit-box": 65552,- "scene:gltf-box": 89349,- "scene:gltf-box+lazy": 92676,- "scene:agent-scene": 109326,- "scene:agent-scene+lazy": 291904+ "scene:lit-box": 65012,+ "scene:gltf-box": 89295,+ "scene:gltf-box+lazy": 92622,+ "scene:agent-scene": 108829,+ "scene:agent-scene+lazy": 291407}}diff --git a/src/compressed-texture.js b/src/compressed-texture.jsindex 1ff9d5f7..e0f7a2d4 100644--- a/src/compressed-texture.js+++ b/src/compressed-texture.js@@ -1,5 +1,5 @@// GPU-compressed texture formats that a KTX2 loader can hand over. Only the glTF addon imports this file.-// It hands the renderer a generic image: { width, height, compressed: { internalFormat, extension, levels: [{ data, width, height }], bytes } }.+// It hands the renderer a generic image: { width, height, compressed: { levels, bytes, minFilter(filter), upload(context) } }.const BPTC = 'EXT_texture_compression_bptc';const S3TC = 'WEBGL_compressed_texture_s3tc';@@ -59,7 +59,23 @@ export function describeCompressedImage(glFormat, levels, field, colorSpace) {const srgb = colorSpace !== 'linear';const extension = srgb && info.srgbExtension ? info.srgbExtension : info.extension;const internalFormat = srgb ? info.srgbFormat : glFormat;- return { width: base.width, height: base.height, compressed: { internalFormat, extension, name: info.name, levels: usable, bytes } };+ return { width: base.width, height: base.height, compressed: { name: info.name, levels: usable, bytes, minFilter: minFilterFor(usable.length), upload: context => uploadLevels(context, usable, internalFormat, extension) } };+}++// A single level cannot be mip filtered. Odd filter enums are the linear ones.+function minFilterFor(levelCount) {+ return minFilter => (levelCount === 1 && minFilter >= 9984 ? 9728 + (minFilter & 1) : minFilter);+}++/** Upload every mip level into the bound TEXTURE_2D with immutable storage. */+function uploadLevels(context, levels, internalFormat, extension) {+ if (!context.getExtension(extension)) {+ throw new Error(`This context lacks ${extension}, which the compressed texture needs. Transcode KTX2 for this context with ktx2WorkerConfigFor(context) (see docs/gltf.md).`);+ }+ context.texStorage2D(context.TEXTURE_2D, levels.length, internalFormat, levels[0].width, levels[0].height);+ levels.forEach((level, index) => {+ context.compressedTexSubImage2D(context.TEXTURE_2D, index, 0, 0, level.width, level.height, internalFormat, level.data);+ });}/**diff --git a/src/renderer.js b/src/renderer.jsindex 885e9a98..dfc432ce 100644--- a/src/renderer.js+++ b/src/renderer.js@@ -338,11 +338,8 @@ export function textureDescriptorInfo(map, maximumSize = Infinity) {if (![10497, 33071, 33648].includes(wrapS) || ![10497, 33071, 33648].includes(wrapT)) throw new Error('Unsupported texture wrap mode.');if (![9728, 9729, 9984, 9985, 9986, 9987].includes(minFilter) || ![9728, 9729].includes(magFilter)) throw new Error('Unsupported texture filtering mode.');const compressed = map.image.compressed;- if (compressed) {- // Precompressed mips come with the image and cannot be generated. A single level drops mip filtering (odd enums are linear).- const singleLevel = compressed.levels.length === 1 && minFilter >= 9984;- return { width, height, wrapS, wrapT, minFilter: singleLevel ? 9728 + (minFilter & 1) : minFilter, magFilter, mipmaps: false, bytes: compressed.bytes };- }+ // Precompressed mips come with the image and are never generated. The image supplies its own filter and byte count.+ if (compressed) return { width, height, wrapS, wrapT, minFilter: compressed.minFilter(minFilter), magFilter, mipmaps: false, bytes: compressed.bytes };const mipmaps = minFilter >= 9984;return { width, height, wrapS, wrapT, minFilter, magFilter, mipmaps, bytes: estimateTextureBytes(width, height, { mipmaps }) };}@@ -672,14 +669,6 @@ export class Renderer67 {context.texParameterf(target, extension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min(MAX_TEXTURE_ANISOTROPY, supported));}- /** Upload precompressed mips ({ internalFormat, extension, levels }) into the bound texture. */- uploadCompressedLevels({ internalFormat, extension, levels }) {- const context = this.context;- if (!context.getExtension(extension)) throw new Error(`This context lacks ${extension}, which the compressed texture needs (see docs/gltf.md).`);- context.texStorage2D(context.TEXTURE_2D, levels.length, internalFormat, levels[0].width, levels[0].height);- levels.forEach((level, index) => context.compressedTexSubImage2D(context.TEXTURE_2D, index, 0, 0, level.width, level.height, internalFormat, level.data));- }-createTexture(map) {if (this.textures.has(map)) return this.textures.get(map);const context = this.context;@@ -692,7 +681,7 @@ export class Renderer67 {context.pixelStorei(context.UNPACK_COLORSPACE_CONVERSION_WEBGL, context.NONE);if (map) {const info = textureDescriptorInfo(map, context.getParameter(context.MAX_TEXTURE_SIZE));- if (map.image.compressed) this.uploadCompressedLevels(map.image.compressed);+ 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);diff --git a/tests/compressed-texture.test.js b/tests/compressed-texture.test.jsindex d59e1411..c765b92e 100644--- a/tests/compressed-texture.test.js+++ b/tests/compressed-texture.test.js@@ -21,7 +21,7 @@ function fakeContext(extensionNames) {}function uploadLevels(context, image) {- Renderer67.prototype.uploadCompressedLevels.call({ context }, image.compressed);+ image.compressed.upload(context);}function bc7Levels() {@@ -116,7 +116,7 @@ test('createTexture uploads a compressed image without generating mips', () => {compressedTexSubImage2D: () => calls.push('compressedTexSubImage2D'),};const map = { image: describeCompressedImage(BC7, bc7Levels(), 'map', 'srgb'), colorSpace: 'srgb' };- const renderer = { context, textures: new Map(), applyAnisotropy: () => calls.push('anisotropy'), uploadCompressedLevels: Renderer67.prototype.uploadCompressedLevels };+ const renderer = { context, textures: new Map(), applyAnisotropy: () => calls.push('anisotropy') };Renderer67.prototype.createTexture.call(renderer, map);assert.deepEqual(calls, ['texStorage2D', 'compressedTexSubImage2D', 'compressedTexSubImage2D', 'compressedTexSubImage2D', 'compressedTexSubImage2D']);});