import { useEffect, useState } from "react"; import { describeAdapter } from "@plate-dyn/core"; export type WebGpuSupport = | { status: "checking" } | { status: "available"; adapter: string } | { status: "unavailable"; reason: string }; /** * Whether this browser exposes WebGPU, and on what. Asked once on the main * thread so the controls can say what a GPU run would use; the worker makes * its own device request when a run asks for it. */ export function useWebGpuSupport(): WebGpuSupport { const [support, setSupport] = useState({ status: "checking" }); useEffect(() => { let alive = true; const set = (s: WebGpuSupport) => { if (alive) setSupport(s); }; (async () => { try { const gpu = (navigator as { gpu?: GPU }).gpu; if (!gpu) return set({ status: "unavailable", reason: "this browser does not expose WebGPU" }); const adapter = await gpu.requestAdapter(); if (!adapter) return set({ status: "unavailable", reason: "no WebGPU adapter was offered" }); set({ status: "available", adapter: describeAdapter(adapter) }); } catch (e) { set({ status: "unavailable", reason: e instanceof Error ? e.message : String(e) }); } })(); return () => { alive = false; }; }, []); return support; }