const mygl = this.canvas.getContext('webgl2');
setWebGLContext(2, mygl as any);
await tf.setBackend('webgl');
This (Hackish one) ensures the restored state after I am done with my own work in the canvas (would be great as a restore helper):
const gpgpu = (tf.backend() as any).gpgpu;
const gl = this.engine._gl;
if (gpgpu.vertexAttrsAreBound) {
this.engine.enableScissor();
gl.useProgram(gpgpu.program);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, gpgpu.indexBuffer);
gpgpu_util.bindVertexProgramAttributeStreams(gl, false, gpgpu.program, gpgpu.vertexBuffer);
}
And this helps me sharing output internal tensor textures with my app (would also be great as a simpler helper):
const output = resize(inputTensor);
const backend = tf.backend() as unknown as tfgl.MathBackendWebGL;
const dataId = (output as any).dataId
const textureData = backend.texData.get(dataId);
const texture = textureData.texture;
The only missing part would be able to share inputs data by having a kind of fromTexture equivalent to fromPixels.