Skip to content

fix(WebGPU): keep finite inputs in range on half-float uploads - #3750

Open
PaulHax wants to merge 1 commit into
masterfrom
webgpu-finite-uint16-sampling
Open

PaulHax wants to merge 1 commit into
masterfrom
webgpu-finite-uint16-sampling

Conversation

@PaulHax

@PaulHax PaulHax commented Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator

Context

Uint16 values from 65520 through 65535 round to infinity in a half-float texture, and finite floating-point inputs beyond ±65504 can overflow too. A non-finite texel can affect interpolation with its neighbors. WebGPU uses half-float storage when preferSizeOverAccuracy is set or when the device lacks float32-filterable.

HalfFloat.toHalf also mishandled overflow. JavaScript && returns its operand rather than 0 or 1, so the overflow branch ORed the float mantissa into the result: 70000 converted to -Infinity, 1e38 to NaN, and NaN to +Infinity.

Results

  • With half-float storage, 65535 uploads and samples as 65504. A partial update containing -70000 and 70000 stores -65504 and 65504.
  • A texture logs one warning the first time an upload clamps a value.
  • toHalf returns infinity of the same sign for finite values beyond the range and keeps NaN. This also applies to the OpenGL half-float upload path, where NaN previously uploaded as +Infinity.

Changes

  • HalfFloat.toHalf: fix the sign of overflowing values and keep NaN.
  • HalfFloat.toHalfClamped (new): like toHalf, but converts finite values beyond the range to ±65504. Infinity and NaN convert unchanged.
  • HalfFloat.needsHalfFloatClamp (new): whether toHalfClamped clamps a value.
  • HalfFloat.MAX_FINITE_HALF_FLOAT (new): 65504.
  • WebGPU Texture: full and partial half-float uploads use toHalfClamped and warn once per texture when they clamp.

PR Checklist

  • GitHub Actions CI passed: semantic-release commit messages, lint, and tests
  • Test coverage added
  • Documentation and TypeScript definitions are updated to match these changes

@PaulHax
PaulHax added this pull request to stack #3751 November 7, 2026 19:06
@PaulHax
PaulHax force-pushed the webgpu-finite-uint16-sampling branch from b511d0a to c18aec4 Compare November 7, 2026 19:13
@PaulHax
PaulHax force-pushed the webgpu-finite-uint16-sampling branch 2 times, most recently from 2bdf985 to b5d305e Compare November 11, 2026 13:20
Base automatically changed from webgpu-filterable-image-scalars to master November 12, 2026 13:41
@PaulHax
PaulHax force-pushed the webgpu-finite-uint16-sampling branch from b5d305e to 328c147 Compare November 12, 2026 13:41
Comment on lines +14 to +25
// Prevent finite inputs such as Uint16 65535 from becoming infinite texels,
// which can contaminate interpolation with neighboring values.
function toHalfForUpload(value) {
let uploadValue = value;
if (Number.isFinite(value)) {
uploadValue = Math.max(
-MAX_FINITE_HALF_FLOAT,
Math.min(value, MAX_FINITE_HALF_FLOAT)
);
}
return HalfFloat.toHalf(uploadValue);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the clamp, finite values no longer reach HalfFloat.toHalf's overflow branch. Could we fix that in HalfFloat and move this clamp there as well? #3707 adds an identical toHalfForUpload for OpenGL, so both backends could share one helper.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toHalf's overflow branch now returns infinity of the same sign and keeps NaN. There's a new HalfFloat.toHalfClamped that clamps finite values to ±65504 and passes Infinity/NaN through, plus needsHalfFloatClamp for detecting when it clamps. WebGPU uses both, and #3707 can switch its toHalfForUpload to them once this lands.

outArray[outOffset + i] = HalfFloat.toHalf(
inputArray[inOffset + i]
);
outArray[outOffset + i] = toHalfForUpload(inputArray[inOffset + i]);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we warn once per texture when a value is clamped?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@PaulHax
PaulHax force-pushed the webgpu-finite-uint16-sampling branch 3 times, most recently from f409333 to 3b895e7 Compare November 17, 2026 16:56
Clamp finite values beyond the half float range to the largest finite half
float when uploading to 16float textures, instead of letting them become
infinity. Warn the first time a texture clamps a value, since devices without
float32-filterable use half float without being asked to.

Fix HalfFloat.toHalf so finite values beyond the half float range become
infinity of the same sign and NaN stays NaN. JavaScript && returns its operand
rather than 0 or 1, so the overflow branch ORed the whole float mantissa into
the result. Add HalfFloat.toHalfClamped and needsHalfFloatClamp so texture
uploads can share the clamp.
@PaulHax
PaulHax force-pushed the webgpu-finite-uint16-sampling branch from 3b895e7 to 9d25aae Compare November 17, 2026 20:02

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants