goxjs / gl

Go cross-platform OpenGL bindings.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JS TexImage2D produces TypeError

nytehauq opened this issue · comments

TypeError: Argument 9 of WebGLRenderingContext.texImage2D does not implement interface ArrayBufferViewOrNull on Firefox 42.0 and Uncaught TypeError: Failed to execute 'texImage2D' on 'WebGLRenderingContext': parameter 9 is not of type 'ArrayBufferView' on Chrome 48.0.2564.109.

AFAIK the data parameter of gl.TexImage2D is a []byte which becomes a Uint8Array in GopherJS... which should satisfy the WebGL standard which calls for an ArrayBufferView.

For comparison, gl.BufferData takes a []byte and works fine. The only difference I can see is that the WebGL spec for bufferData specifies a BufferDataSource? (which is defined as an ArrayBufferView or an ArrayBuffer in the spec) and not an ArrayBufferView? type for the data parameter.

Thanks for reporting.

AFAIK the data parameter of gl.TexImage2D is a []byte which becomes a Uint8Array in GopherJS... which should satisfy the WebGL standard which calls for an ArrayBufferView.

Yeah, that sounds right.

Have you tried it in other browsers, like Chrome?

Can you post your usage code?

I certainly use and rely on that func in my projects and I haven't had such issues. For example, see here for one usage example.

Thanks for all the work!

Chrome also throws a TypeError. Even this code (gist) produces the error. Grabbed the latest gopherjs/gopherjs, goxjs/gl, and goxjs/glfw just now and the result is the same. Doesn't seem to matter what format is specified or whether nil or []byte{} or someByteSlice=[]byte{} are passed as data.

Oh wow. This works:

gl.TexImage2D(gl.TEXTURE_2D, 0, 10, 10, gl.RGBA, gl.UNSIGNED_BYTE, []byte{0})

These do not:

gl.TexImage2D(gl.TEXTURE_2D, 0, 10, 10, gl.RGBA, gl.UNSIGNED_BYTE, []byte{})
gl.TexImage2D(gl.TEXTURE_2D, 0, 10, 10, gl.RGBA, gl.UNSIGNED_BYTE, nil)

Seems that GopherJS's nil/empty slices are not ArrayBuffers nor are they null. For reference, all three cases work on Native.

An orthogonal question, but is it valid to call gl.TexImage2D with a nil data? In what situations would one want to do that?

From https://www.khronos.org/registry/webgl/specs/1.0/#TEXIMAGE2D:

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

So it appears to be a valid operation.

Thanks again for reporting this issue @nytehauq. It should be fixed in ceadefa now. Please let me know if you notice anything else.

Great, thanks for the quick fix. Passing a nil data is useful for fullscreen renderbuffer textures for color-based picking (or any situation where you're going to be rendering to a texture that begins empty). As an aside, ReadPixels works for at least this limited use-case.