To transfer data efficiently between regl and TensorFlow.js (tfjs) on WebGL2 without touching the CPU, you must enforce context sharing. First, initialize tfjs using your canvas element so it generates a custom WebGL2 backend. Next, extract that exact native WebGL2 context reference via backend.getGPGPUContext().gl and pass it directly into the createREGL({ gl }) constructor. To convert a regl texture into a tfjs tensor, call gl.flush() to ensure all rendering work is completed, extract the internal WebGL texture pointer using reglTexture._texture.texture, and pass it to tf.engine().makeTensorFromDataHook() for a zero-copy data link. To convert a tfjs tensor back into a regl texture, call gl.flush(), extract the active texture reference from the backend metadata with tf.backend().getTextureData(tensor.dataId).texture, and instantly wrap it back into a new regl.texture({ copy: nativeTexture }) instance. Always run your regl draw calls inside explicit command blocks to protect the WebGL state from tfjs mutations, and manually call .dispose() and .destroy() on your resources to prevent massive GPU memory leaks.
To transfer data efficiently between regl and TensorFlow.js (tfjs) on WebGL2 without touching the CPU, you must enforce context sharing. First, initialize tfjs using your canvas element so it generates a custom WebGL2 backend. Next, extract that exact native WebGL2 context reference via backend.getGPGPUContext().gl and pass it directly into the createREGL({ gl }) constructor. To convert a regl texture into a tfjs tensor, call gl.flush() to ensure all rendering work is completed, extract the internal WebGL texture pointer using reglTexture._texture.texture, and pass it to tf.engine().makeTensorFromDataHook() for a zero-copy data link. To convert a tfjs tensor back into a regl texture, call gl.flush(), extract the active texture reference from the backend metadata with tf.backend().getTextureData(tensor.dataId).texture, and instantly wrap it back into a new regl.texture({ copy: nativeTexture }) instance. Always run your regl draw calls inside explicit command blocks to protect the WebGL state from tfjs mutations, and manually call .dispose() and .destroy() on your resources to prevent massive GPU memory leaks.