From dae57d414616f813836ebb7897c0495571312e86 Mon Sep 17 00:00:00 2001 From: Luis Montes Date: Wed, 11 Nov 2026 10:55:39 -0700 Subject: [PATCH] ASTC: WEBGL_compressed_texture_astc has getSupportedProfiles() (['ldr'], plus 'hdr' when the driver has GL_KHR_texture_compression_astc_hdr), as the WebGL spec defines; three's KTX2Loader.detectSupport() calls it and threw on any driver that reports ASTC The compressed-texture test now calls KTX2Loader.detectSupport() (the call apps make at startup) on three r186 and r187, and fails without this change with the reporter's error. three is a devDependency at ^0.187.0. The driver's extension list is read once (_nativeExtensions) and shared by getSupportedExtensions and getExtension. --- lib/webgl2-context.mjs | 36 ++++++++++++++++++++++++------- package-lock.json | 8 +++---- package.json | 2 +- test/test-compressed-textures.mjs | 10 +++++++++ test/test-compressed-threejs.mjs | 34 +++++++++++++++++++++++++---- 5 files changed, 73 insertions(+), 17 deletions(-) diff --git a/lib/webgl2-context.mjs b/lib/webgl2-context.mjs index 7d32456..9d9d3ff 100644 --- a/lib/webgl2-context.mjs +++ b/lib/webgl2-context.mjs @@ -1316,17 +1316,28 @@ export class WebGL2RenderingContext { // ─── Extensions ─────────────────────────────────────────────────────── + // the driver's own extension names, read once + _nativeExtensions() { + if (!this._nativeExts) { + const set = new Set() + try { + const numExts = new Int32Array(1) + this._gl.glGetIntegerv(0x821D /* GL_NUM_EXTENSIONS */, numExts) + for (let i = 0; i < numExts[0]; i++) { + const name = this._gl.glGetStringi(GL.EXTENSIONS, i) + if (name) set.add(name) + } + } catch (e) {} + this._nativeExts = set + } + return this._nativeExts + } + getSupportedExtensions() { const exts = [] exts.push('WEBGL_debug_renderer_info') try { - const numExts = new Int32Array(1) - this._gl.glGetIntegerv(0x821D /* GL_NUM_EXTENSIONS */, numExts) - const nativeExts = new Set() - for (let i = 0; i < numExts[0]; i++) { - const name = this._gl.glGetStringi(GL.EXTENSIONS, i) - if (name) nativeExts.add(name) - } + const nativeExts = this._nativeExtensions() if (nativeExts.has('GL_EXT_color_buffer_float')) exts.push('EXT_color_buffer_float') if (nativeExts.has('GL_OES_texture_float_linear')) exts.push('OES_texture_float_linear') if (nativeExts.has('GL_EXT_color_buffer_half_float')) exts.push('EXT_color_buffer_half_float') @@ -1348,7 +1359,16 @@ export class WebGL2RenderingContext { } const supported = this.getSupportedExtensions() const compressed = COMPRESSED_EXTENSIONS[name] - if (compressed) return supported.includes(name) ? { ...compressed.constants } : null + if (compressed) { + if (!supported.includes(name)) return null + const ext = { ...compressed.constants } + if (name === 'WEBGL_compressed_texture_astc') { + const profiles = ['ldr'] + if (this._nativeExtensions().has('GL_KHR_texture_compression_astc_hdr')) profiles.push('hdr') + ext.getSupportedProfiles = () => [...profiles] + } + return ext + } if (supported.includes(name)) return {} return null } diff --git a/package-lock.json b/package-lock.json index 6010382..72af717 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,7 @@ "native-gles": "^0.7.1" }, "devDependencies": { - "three": "^0.184.0" + "three": "^0.187.0" } }, "../native-gles": { @@ -97,9 +97,9 @@ } }, "node_modules/three": { - "version": "0.184.0", - "resolved": "https://registry.npmjs.org/three/-/three-0.184.0.tgz", - "integrity": "sha512-wtTRjG92pM5eUg/KuUnHsqSAlPM296brTOcLgMRqEeylYTh/CdtvKUvCyyCQTzFuStieWxvZb8mVTMvdPyUpxg==", + "version": "0.187.0", + "resolved": "https://registry.npmjs.org/three/-/three-0.187.0.tgz", + "integrity": "sha512-Qiu7R+Cmh8CstZsY1uJOSWnVlfo7ZtNLV7mF8Jltk3LB3AtDu9cRZlcnRKkAk8Y4z6KKw35z7Y7Urd7jWY8etA==", "dev": true, "license": "MIT" }, diff --git a/package.json b/package.json index f8c400d..f823740 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,6 @@ "native-gles": "^0.7.1" }, "devDependencies": { - "three": "^0.184.0" + "three": "^0.187.0" } } diff --git a/test/test-compressed-textures.mjs b/test/test-compressed-textures.mjs index 4e7de53..2c36886 100644 --- a/test/test-compressed-textures.mjs +++ b/test/test-compressed-textures.mjs @@ -39,6 +39,16 @@ for (const [nativeName, webName, constants] of expectations) { check(gl.getExtension(webName) === null, `${webName} is not returned by getExtension`) } } +{ + // WEBGL_compressed_texture_astc is the one extension object with a method (the WebGL spec): getSupportedProfiles() + const astc = gl.getExtension('WEBGL_compressed_texture_astc') + if (native.includes('GL_KHR_texture_compression_astc_ldr')) { + const profiles = astc && typeof astc.getSupportedProfiles === 'function' ? astc.getSupportedProfiles() : null + check(Array.isArray(profiles) && profiles.includes('ldr'), 'getSupportedProfiles() returns an array that includes ldr') + check(Array.isArray(profiles) && profiles.includes('hdr') === native.includes('GL_KHR_texture_compression_astc_hdr'), + "getSupportedProfiles() lists hdr exactly when the driver has GL_KHR_texture_compression_astc_hdr") + } +} check(supported.includes('WEBGL_compressed_texture_etc'), 'WEBGL_compressed_texture_etc is always advertised (ETC2 is core in GLES 3.0)') const etc = gl.getExtension('WEBGL_compressed_texture_etc') check(!!etc && etc.COMPRESSED_RGB8_ETC2 === 0x9274 && etc.COMPRESSED_RGBA8_ETC2_EAC === 0x9278 && etc.COMPRESSED_R11_EAC === 0x9270, diff --git a/test/test-compressed-threejs.mjs b/test/test-compressed-threejs.mjs index 1254691..eb8dc5b 100644 --- a/test/test-compressed-threejs.mjs +++ b/test/test-compressed-threejs.mjs @@ -3,12 +3,12 @@ // with compressedTex*, and samples it), not just the raw context calls. import { createWebGL2Context } from '../index.mjs' -// three.js r150 and later render through WebGL2 only, so this needs a recent three (a devDependency). -// THREE_MODULE overrides the import (a file path or URL) to try another build. +// three.js r150 and later render through WebGL2 only (this repo's three is a devDependency, kept current). console.log('--- test-compressed-threejs ---') -let THREE +let THREE, KTX2Loader try { - THREE = await import(process.env.THREE_MODULE ? new URL(process.env.THREE_MODULE, 'file://' + process.cwd() + '/').href : 'three') + THREE = await import('three') + ;({ KTX2Loader } = await import('three/examples/jsm/loaders/KTX2Loader.js')) } catch (e) { console.log(`FAIL: three.js could not be imported (${e.code || e.message}); run npm install`) process.exit(1) @@ -23,6 +23,32 @@ const renderer = new THREE.WebGLRenderer({ canvas, context: gl }) let failed = false const check = (ok, message) => { console.log(`${ok ? 'PASS' : 'FAIL'}: ${message}`); if (!ok) failed = true } +// the loader apps call at startup: it asks every compressed extension what it supports, and calls +// getSupportedProfiles() on ASTC, so an extension object without it throws here +const driverExts = new Set() +{ + const num = new Int32Array(1) + gl._gl.glGetIntegerv(0x821D /* NUM_EXTENSIONS */, num) + for (let i = 0; i < num[0]; i++) driverExts.add(gl._gl.glGetStringi(0x1F03, i)) +} +let support = null +try { + support = new KTX2Loader().detectSupport(renderer).workerConfig + check(true, 'KTX2Loader.detectSupport(renderer) does not throw') +} catch (e) { + check(false, `KTX2Loader.detectSupport(renderer) throws: ${e.message}`) +} +if (support) { + // three disables ASTC / ETC1 / ETC2 on Linux when the driver also reports BPTC and S3TC (Mesa emulates them in + // software), so detectSupport's astcSupported and etc2Supported are not the driver's answer; the extension + // queries it makes are + check(renderer.extensions.has('WEBGL_compressed_texture_astc') === driverExts.has('GL_KHR_texture_compression_astc_ldr'), 'three finds WEBGL_compressed_texture_astc exactly when the driver has ASTC LDR') + check(renderer.extensions.has('WEBGL_compressed_texture_etc') === true, 'three finds WEBGL_compressed_texture_etc (ETC2 is core in GLES 3.0)') + check(support.astcHDRSupported === driverExts.has('GL_KHR_texture_compression_astc_hdr'), `detectSupport astcHDRSupported (${support.astcHDRSupported}) matches the driver`) + check(support.dxtSupported === driverExts.has('GL_EXT_texture_compression_s3tc'), `detectSupport dxtSupported (${support.dxtSupported}) matches the driver`) + check(support.bptcSupported === driverExts.has('GL_EXT_texture_compression_bptc'), `detectSupport bptcSupported (${support.bptcSupported}) matches the driver`) +} + check(renderer.extensions.has('WEBGL_compressed_texture_s3tc') === gl.getSupportedExtensions().includes('WEBGL_compressed_texture_s3tc'), 'three sees the same S3TC support the context advertises')