Skip to content

Fix uniform index queries and accept more texImage2D sources - #4

Merged
monteslu merged 2 commits into
mainfrom
fix-uniform-queries-and-texture-sources
Sep 16, 2026
Merged

monteslu merged 2 commits into
mainfrom
fix-uniform-queries-and-texture-sources

Conversation

@monteslu

@monteslu monteslu commented Sep 16, 2026 •

Copy link
Copy Markdown
Owner

Three fixes to WebGL2 calls that any twgl-based library hits.

getUniformIndices / getActiveUniforms: argument arity mismatch

The JS wrappers passed a count argument that the native bindings do not take:

  • _getUniformIndices reads (program, namesArray, outIndices) and derives the count from the array itself, but the wrapper passed 4 arguments.
  • _getActiveUniformsiv reads (program, indices, pname, outParams), but the wrapper passed 5.

Every argument shifted along, so the addon rejected the call with Invalid argument. twgl calls both while building any program that has uniforms. It reproduces in a few lines:

const idx = gl.getUniformIndices(prog, ['a', 'b', 'c']);
// before: Error: Invalid argument
// after:  [ 0, 1, 2 ]
gl.getActiveUniforms(prog, idx, gl.UNIFORM_TYPE);
// after:  [ 5126, 5126, 5126 ]   (GL_FLOAT)

texImage2D: coerce undefined/null width and height

WebIDL coerces the numeric arguments, so a browser turns an undefined or null width/height into 0 and allocates an empty texture. The native binding demanded real numbers and threw A number was expected.

Allocating an empty texture this way is common: twgl's setEmptyTexture() calls exactly like this whenever a texture is created with no src and no explicit size.

texImage2D: accept ImageData as a source

ImageData is a valid texImage2D source per the WebGL spec. The 6-arg path only handled Canvas and Image, so it threw Value is not one of these types.

Its pixels are already RGBA in the expected layout, so they now upload directly instead of routing through a temp canvas, which rejects ImageData anyway. That is also one less copy.

Verification

Existing suite: 204 passed, 0 failed.

…names

glGetIntegerv writes as many values as the pname defines, and the driver
cannot know how large the destination is. The default branch of
getParameter() passed a one-element Int32Array, so any array pname that is
not special-cased above it overflowed the heap.

GL_COMPRESSED_TEXTURE_FORMATS is the case that bit: a desktop Mesa stack
reports 51 formats, so the driver wrote ~200 bytes past the end of a 4-byte
buffer. The corruption landed in whatever allocated next -- usually V8 --
and surfaced as an unrelated crash much later, with backtraces pointing at
the wasm compiler rather than here.

Over-allocate a 64-int scratch and return the first element. Scalars are
unchanged; an unlisted array pname now costs a slightly larger temporary
instead of corrupting the heap. Adds getParameterArray() for callers that
want the whole array.

npm test: 204 passed, 0 failed.
Three fixes to WebGL2 calls that any twgl-based library hits.

getUniformIndices / getActiveUniforms: argument arity mismatch

The JS wrappers passed a count argument the native bindings do not take.
_getUniformIndices reads (program, namesArray, outIndices) and derives the
count from the array itself; _getActiveUniformsiv reads (program, indices,
pname, outParams). Passing the extra count shifted every argument along, so
the addon rejected the call with "Invalid argument". twgl calls both while
building any program that has uniforms.

texImage2D: coerce undefined/null width and height

WebIDL coerces the numeric arguments, so a browser turns an undefined or null
width/height into 0 and allocates an empty texture. The native binding
demanded real numbers and threw "A number was expected". Allocating an empty
texture this way is common: twgl's setEmptyTexture() calls exactly like this
whenever a texture is created with no src and no explicit size.

texImage2D: accept ImageData as a source

ImageData is a valid texImage2D source per the WebGL spec. The 6-arg path only
handled Canvas and Image, so it threw "Value is not one of these types". Its
pixels are already RGBA in the expected layout, so they upload directly rather
than going through a temp canvas, which rejects ImageData anyway.

Verified with the existing suite: 204 passed, 0 failed.
@monteslu
monteslu force-pushed the fix-uniform-queries-and-texture-sources branch from ad13221 to 9ab3a45 Compare September 16, 2026 01:58
@monteslu
monteslu merged commit 7368f72 into main Sep 16, 2026
2 checks passed
@monteslu
monteslu deleted the fix-uniform-queries-and-texture-sources branch September 16, 2026 02:03
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.

1 participant