Skip to content

Renderers: Initial support for Float16Array. #31305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2025
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
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"esprima": "readonly",
"jsonlint": "readonly",
"VideoFrame": "readonly",
"VideoDecoder": "readonly"
"VideoDecoder": "readonly",
"Float16Array": "readonly"
},
"rules": {
"no-throw-literal": [
Expand Down
3 changes: 2 additions & 1 deletion examples/webgl_geometry_colors.html
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/core/BufferAttribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
4 changes: 4 additions & 0 deletions src/renderers/webgl-fallback/utils/WebGLAttributeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
4 changes: 4 additions & 0 deletions src/renderers/webgl/WebGLAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
6 changes: 6 additions & 0 deletions src/renderers/webgpu/utils/WebGPUAttributeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ const typedArraysToVertexFormatPrefix = new Map( [
[ Float32Array, [ 'float32', ]],
] );

if ( typeof Float16Array !== 'undefined' ) {

typedArraysToVertexFormatPrefix.set( Float16Array, [ 'float16' ] );

}

const typedAttributeToVertexFormatPrefix = new Map( [
[ Float16BufferAttribute, [ 'float16', ]],
] );
Expand Down