| 41 | * ``` |
| 42 | */ |
| 43 | export async function checkWebGPUSupport(): Promise<WebGPUSupportResult> { |
| 44 | // Return cached result if available |
| 45 | if (cachedSupportCheck) { |
| 46 | return cachedSupportCheck; |
| 47 | } |
| 48 | |
| 49 | // Create and cache the promise |
| 50 | cachedSupportCheck = (async (): Promise<WebGPUSupportResult> => { |
| 51 | // SSR-safe checks: ensure we're in a browser environment |
| 52 | if (typeof window === 'undefined') { |
| 53 | return { |
| 54 | supported: false, |
| 55 | reason: 'Not running in a browser environment (window is undefined).', |
| 56 | }; |
| 57 | } |
| 58 | |
| 59 | if (typeof navigator === 'undefined') { |
| 60 | return { |
| 61 | supported: false, |
| 62 | reason: 'Navigator is not available in this environment.', |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | // Check for navigator.gpu API |
| 67 | if (!navigator.gpu) { |
| 68 | return { |
| 69 | supported: false, |
| 70 | reason: 'WebGPU API (navigator.gpu) is not available. Your browser does not support WebGPU.', |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | // Attempt to request an adapter to verify actual support |
| 75 | try { |
| 76 | // First attempt: high-performance adapter (aligns with GPUContext behavior) |
| 77 | let adapter = await navigator.gpu.requestAdapter({ |
| 78 | powerPreference: 'high-performance', |
| 79 | }); |
| 80 | |
| 81 | // Second attempt: default adapter if high-performance is unavailable |
| 82 | if (!adapter) { |
| 83 | adapter = await navigator.gpu.requestAdapter(); |
| 84 | } |
| 85 | |
| 86 | // If both attempts fail, WebGPU is not usable |
| 87 | if (!adapter) { |
| 88 | return { |
| 89 | supported: false, |
| 90 | reason: 'No compatible WebGPU adapter found. This may occur if: (1) no GPU is available, (2) GPU drivers are outdated or incompatible, (3) running in a VM or headless environment, or (4) WebGPU is disabled in browser settings.', |
| 91 | }; |
| 92 | } |
| 93 | |
| 94 | // Success: WebGPU is supported and an adapter is available |
| 95 | return { supported: true }; |
| 96 | } catch (error) { |
| 97 | // Adapter request threw an error |
| 98 | let reason = 'Failed to request WebGPU adapter.'; |
| 99 | |
| 100 | // Try to extract useful error information |