From b6fd86599343e07688302e7c2d2b890fdbfb1244 Mon Sep 17 00:00:00 2001 From: Michael Herzog Date: Tue, 24 Jun 2025 09:38:22 +0200 Subject: [PATCH] Renderers: Initial support for `Float16Array`. (#31305) --- .eslintrc.json | 3 ++- examples/webgl_geometry_colors.html | 3 ++- src/core/BufferAttribute.js | 4 ++-- src/renderers/webgl-fallback/utils/WebGLAttributeUtils.js | 4 ++++ src/renderers/webgl/WebGLAttributes.js | 4 ++++ src/renderers/webgpu/utils/WebGPUAttributeUtils.js | 6 ++++++ 6 files changed, 20 insertions(+), 4 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index b4089e0aa9eb07..bdb1813613c210 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -42,7 +42,8 @@ "esprima": "readonly", "jsonlint": "readonly", "VideoFrame": "readonly", - "VideoDecoder": "readonly" + "VideoDecoder": "readonly", + "Float16Array": "readonly" }, "rules": { "no-throw-literal": [ diff --git a/examples/webgl_geometry_colors.html b/examples/webgl_geometry_colors.html index c916370e675d07..4c7880c47e3e05 100644 --- a/examples/webgl_geometry_colors.html +++ b/examples/webgl_geometry_colors.html @@ -103,7 +103,8 @@ const geometry1 = new THREE.IcosahedronGeometry( radius, 1 ); const count = geometry1.attributes.position.count; - geometry1.setAttribute( 'color', new THREE.BufferAttribute( new Float32Array( count * 3 ), 3 ) ); + const arrayType = ( typeof Float16Array !== 'undefined' ) ? Float16Array : Float32Array; + geometry1.setAttribute( 'color', new THREE.BufferAttribute( new arrayType( count * 3 ), 3 ) ); const geometry2 = geometry1.clone(); const geometry3 = geometry1.clone(); diff --git a/src/core/BufferAttribute.js b/src/core/BufferAttribute.js index 07fd3dc5c15108..3e1b10fe68f3f5 100644 --- a/src/core/BufferAttribute.js +++ b/src/core/BufferAttribute.js @@ -841,8 +841,8 @@ class Uint32BufferAttribute extends BufferAttribute { * Convenient class that can be used when creating a `Float16` buffer attribute with * a plain `Array` instance. * - * This class automatically converts to and from FP16 since `Float16Array` is not - * natively supported in JavaScript. + * This class automatically converts to and from FP16 via `Uint16Array` since `Float16Array` + * browser support is still problematic. * * @augments BufferAttribute */ diff --git a/src/renderers/webgl-fallback/utils/WebGLAttributeUtils.js b/src/renderers/webgl-fallback/utils/WebGLAttributeUtils.js index 4082a9d5aefb44..2b4453fba77c93 100644 --- a/src/renderers/webgl-fallback/utils/WebGLAttributeUtils.js +++ b/src/renderers/webgl-fallback/utils/WebGLAttributeUtils.js @@ -114,6 +114,10 @@ class WebGLAttributeUtils { type = gl.FLOAT; + } else if ( typeof Float16Array !== 'undefined' && array instanceof Float16Array ) { + + type = gl.HALF_FLOAT; + } else if ( array instanceof Uint16Array ) { if ( attribute.isFloat16BufferAttribute ) { diff --git a/src/renderers/webgl/WebGLAttributes.js b/src/renderers/webgl/WebGLAttributes.js index bb530218266c46..c022775224d9fa 100644 --- a/src/renderers/webgl/WebGLAttributes.js +++ b/src/renderers/webgl/WebGLAttributes.js @@ -21,6 +21,10 @@ function WebGLAttributes( gl ) { type = gl.FLOAT; + } else if ( typeof Float16Array !== 'undefined' && array instanceof Float16Array ) { + + type = gl.HALF_FLOAT; + } else if ( array instanceof Uint16Array ) { if ( attribute.isFloat16BufferAttribute ) { diff --git a/src/renderers/webgpu/utils/WebGPUAttributeUtils.js b/src/renderers/webgpu/utils/WebGPUAttributeUtils.js index be81c0c33fe1ba..32799497b33814 100644 --- a/src/renderers/webgpu/utils/WebGPUAttributeUtils.js +++ b/src/renderers/webgpu/utils/WebGPUAttributeUtils.js @@ -12,6 +12,12 @@ const typedArraysToVertexFormatPrefix = new Map( [ [ Float32Array, [ 'float32', ]], ] ); +if ( typeof Float16Array !== 'undefined' ) { + + typedArraysToVertexFormatPrefix.set( Float16Array, [ 'float16' ] ); + +} + const typedAttributeToVertexFormatPrefix = new Map( [ [ Float16BufferAttribute, [ 'float16', ]], ] );