KhronosGroup/WebGL

texImage2D, source image not decodable: unspecified behavior

junov opened this issue · 5 comments

junov commented

The spec does not say what texImage2d is supposed to do when the source image is not fully decodable, for example when it is and HTMLImageElement whose image resource is a corrupted image file.

Since it is necessary to attempt to decode the image in order to determine whether it is "fully decodable", it would be best if the error handling behavior were asynchronous (e.g. texture is blank). Any synchronous error handling (e.g. throwing an exception) would be bad because it would force implementations to decode images synchronously, thus preventing image decode from being done on another thread/process.

The intent in this scenario was to synchronously generate an INVALID_VALUE OpenGL error. Conceptually, the image is an invalid source for the texture upload operation. I thought there was specification text defining that if the image isn't fully loaded yet, regardless of whether that load / decode would succeed, that implementations must generate INVALID_OPERATION. However I just checked the version history of specs/latest/1.0/index.html and can't find this. In my opinion this should be added to the WebGL specification.

Unfortunately it's infeasible to make this an asynchronous error because texImage2D has synchronous semantics. The recommended upload path for texture data that would otherwise come from HTMLImageElement is to create an ImageBitmap from a Blob and upload that instead. I think this should be added to https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/WebGL_best_practices but recall some disagreement about that advice.

CC @kdashg for feedback.

Doesn't the "4.1 Resource Restrictions" section imply that the uploaded image is either fully-decoded (no error) or zeros (also no error)?

Not deliberately. The semantic for years has been that incomplete images generate INVALID_VALUE. Here's a test case.
test-image.zip

Upon the first load from localhost, the upload of the not-yet-loaded image generates INVALID_VALUE. Reloading, though, it usually works.

GL error before texImage calls: 0x0
GL error after texImage2D with img with no src: 0x501
GL error after texImage2D with (hopefully) not-yet-loaded img: 0x501
GL error after texImage2D with complete img: 0x0

Rather than going through ImageBitmap, it should be sufficient to use img.decode(): https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/decode