stackgl / headless-gl

๐ŸŽƒ Windowless WebGL for node.js
1.76k stars 170 forks source link

Exception thrown when calling texImage2D with null image #205

Closed erikmchut closed 3 years ago

erikmchut commented 3 years ago

The OpenGL and WebGL process for allocating texture memory without actually uploading data is to call texImage2D with a null for the final parameter. The current code in headless-gl tries to dereference the null and throws an exception.

@dhritzkiv โ€“ the fix is easy (just null check and don't dereference pixels). I'd be happy to send a PR, but I'm curious about how to go about updating the gl-conformance package that tests this functionality. It seems the unit tests for WebGL support are not in the headless-gl package. Do you want the fix first or the conformance test first?

exception thrown: TypeError: Cannot read property '_image' of null,TypeError: Cannot read property '_image' of null
    at WebGLRenderingContext.ctx.texImage2D (node_modules/node-canvas-webgl/lib/canvas.js:108:19)
dhritzkiv commented 3 years ago

Hey @erikmchut.

Yeah, a PR would be great!

In terms of tests, I'd keep this out of the gl-conformance package (unless you believe it's in the scope of GL spec compliance), and simply add a test file/ suite in the tests directory.

erikmchut commented 3 years ago

I'll start there. But, yes โ€“ it is part of the spec:

From the WebGL1 Spec: https://www.khronos.org/registry/webgl/specs/latest/1.0/#TEXIMAGE2D

void texImage2D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, [AllowShared] ArrayBufferView? pixels) (OpenGL ES 2.0 ยง3.7.1, man page)

If pixels is null, a buffer of sufficient size initialized to 0 is passed.

If pixels is non-null, the type of pixels must match the type of the data to be read. If it is UNSIGNED_BYTE, a Uint8Array or Uint8ClampedArray must be supplied; if it is UNSIGNED_SHORT_5_6_5, UNSIGNED_SHORT_4_4_4_4, or UNSIGNED_SHORT_5_5_5_1, a Uint16Array must be supplied. If the types do not match, an INVALID_OPERATION error is generated.

If an attempt is made to call this function with no WebGLTexture bound (see above), an INVALID_OPERATION error is generated.

See Pixel Storage Parameters for WebGL-specific pixel storage parameters that affect the behavior of this function.

If pixels is non-null but its size is less than what is required by the specified width, height, format, type, and pixel storage parameters, generates an INVALID_OPERATION error.

erikmchut commented 3 years ago

My apologies โ€“ this source that needs fixing is actually in the node-canvas-webgl repo. https://github.com/akira-cn/node-canvas-webgl.

Moving this issue there.