TypeScript-DOM-lib-generator icon indicating copy to clipboard operation
TypeScript-DOM-lib-generator copied to clipboard

WebGLRenderingContext function types incorrectly reject null resources

Open jameswilddev opened this issue 3 years ago • 2 comments

WebGL has a bit of a quirk in that the browser is able to destroy the rendering context at any time. It's not even something which can happen between event handlers; you can call any WebGL function, and the rendering context could be taken away from you before it returns.

When that happens, functions which create resources will return null. This is correctly modelled in TypeScript's types for functions such as createShader, createBuffer, etc.

However, Khronos' guidance specifically states that an application should NOT attempt to check whether created resources are null. In the event of context loss, all existing resources are effectively void and all methods on the WebGL context become no-ops until the browser decides to reinstate the rendering context (at which point an event is raised and the application is expected to recreate all of its resources).

TypeScript's types for a WebGLRenderingContext, however, seem to disallow null in place of a resource in all functions I've come across. For example, gl.shaderSource(null, "some source") is considered a type error.

Given that the only way to avoid a type error is to contradict the guidance of the standards body who invented the standard, this seems like a bug to me.

This was previously raised in https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/399, but only a small handful of functions were fixed. I could raise a PR to do some more if wanted?

jameswilddev avatar Feb 14 '22 20:02 jameswilddev

I'm not sure how much of the webgl types are generated from upstream WIDLs which might not note the nulls, but I think we'd be open to adding more nulls like #399

orta avatar Feb 16 '22 14:02 orta

#399 has been reverted because upstream IDL has added null to those methods. Now most WebGL APIs are directly generated from IDL (including shaderSource).

https://github.com/w3c/webref/blob/12358e007a8f4d60a302f84dde67a83407df47b7/ed/idl/webgl1.idl#L712-L715

    undefined uniform1fv(WebGLUniformLocation? location, Float32List v);
    undefined uniform2fv(WebGLUniformLocation? location, Float32List v);
    undefined uniform3fv(WebGLUniformLocation? location, Float32List v);
    undefined uniform4fv(WebGLUniformLocation? location, Float32List v);

https://github.com/w3c/webref/blob/12358e007a8f4d60a302f84dde67a83407df47b7/ed/idl/webgl1.idl#L642

    undefined shaderSource(WebGLShader shader, DOMString source);

gl.shaderSource(null, "some source") is actually a runtime error:

image

while gl.uniform1f(null,1) is fine.

image

Maybe you need to file an issue at https://github.com/KhronosGroup/WebGL/issues first.

yume-chan avatar Mar 01 '22 02:03 yume-chan