mgpuImportExternalTexture function

JSObject? mgpuImportExternalTexture(
  1. JSAny videoFrame
)

Import a VideoFrame (or HTMLVideoElement) as a GPUExternalTexture.

🔴 THE DEVICE IS OURS, NOT THE HOST PAGE'S. This used to read ONLY the JS global globalThis.gpuDevice and return null when it was unset — and nothing in this repo sets it, so on a page where the wasm side never ran its EM_ASM setter every import failed silently. Observed live as a camera that delivered 121 VideoFrames of which NONE landed, with no error anywhere: a null here is indistinguishable from "the browser rejected the frame".

minigpu already knows its own device — mgpuGetWGPUDeviceHandle() is the Emscripten handle and getWebGpuJsObject resolves it — so ask for it directly and treat the global as a legacy override only. It must be the SAME device that will sample the texture, which is exactly what this returns.

Implementation

JSObject? mgpuImportExternalTexture(JSAny videoFrame) {
  final dev = _importDevice();
  if (dev == null) {
    _sayImportFailed('no GPUDevice — minigpu is not initialised on this page '
        '(mgpuGetWGPUDeviceHandle returned 0 and globalThis.gpuDevice is '
        'unset)');
    return null;
  }
  final desc = {'source': videoFrame}.toJSDeep as JSObject;
  try {
    return dev.importExternalTexture(desc);
  } catch (e) {
    // Real rejections land here: a closed VideoFrame, an unsupported colour
    // space, a source this browser will not wrap. Distinguishable from the
    // missing-device case above, which is the whole point of splitting them.
    _sayImportFailed('importExternalTexture threw: $e');
    return null;
  }
}