Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions lib/webgl2-context.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
"native-gles": "^0.7.1"
},
"devDependencies": {
"three": "^0.184.0"
"three": "^0.187.0"
}
}
10 changes: 10 additions & 0 deletions test/test-compressed-textures.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
34 changes: 30 additions & 4 deletions test/test-compressed-threejs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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')

Expand Down