export function initWebGL()

in src/plugin/utils/wechat_platform.ts [123:163]


export function initWebGL(
  // tslint:disable-next-line:no-any
  tf: typeof tfjs, webgl: typeof webgl_backend, canvas: any,
  backendName = WECHAT_WEBGL_BACKEND_NAME, debug = false): void {
  if (tf.findBackend(backendName) == null) {
    const WEBGL_ATTRIBUTES = {
      alpha: false,
      antialias: false,
      premultipliedAlpha: false,
      preserveDrawingBuffer: false,
      depth: false,
      stencil: false,
      failIfMajorPerformanceCaveat: true
    };
    const gl = canvas.getContext('webgl', WEBGL_ATTRIBUTES);
    if (debug) {
      console.log('start backend registration');
    }
    webgl.setWebGLContext(1, gl);
    tf.ENV.set('WEBGL_VERSION', 1);
    try {
      tf.registerBackend(backendName, () => {
        const context = new webgl.GPGPUContext(gl);
        return new webgl.MathBackendWebGL(context);
      }, BACKEND_PRIORITY);

      // Register all the webgl kernels on the rn-webgl backend
      const kernels = tf.getKernelsForBackend('webgl');
      kernels.forEach(kernelConfig => {
        const newKernelConfig = Object.assign({}, kernelConfig, { backendName });
        tf.registerKernel(newKernelConfig);
      });
    } catch (e) {
      throw (new Error(`Failed to register Webgl backend: ${e.message}`));
    }
  }
  tf.setBackend(backendName);
  if (debug) {
    console.log('current backend = ', tf.getBackend());
  }
}