( canvas?: SupportedCanvas, options?: GPUContextOptions )
| 84 | * @returns A new GPUContextState instance |
| 85 | */ |
| 86 | export function createGPUContext( |
| 87 | canvas?: SupportedCanvas, |
| 88 | options?: GPUContextOptions |
| 89 | ): GPUContextState { |
| 90 | // Auto-detect DPR on main thread, default to 1.0 in workers |
| 91 | const dprRaw = |
| 92 | options?.devicePixelRatio ?? (typeof window !== 'undefined' ? window.devicePixelRatio : 1.0); |
| 93 | // Be resilient: callers may pass 0/NaN/Infinity. Fall back to 1 instead of throwing. |
| 94 | const dpr = Number.isFinite(dprRaw) && dprRaw > 0 ? dprRaw : 1.0; |
| 95 | const alphaMode = options?.alphaMode ?? 'opaque'; |
| 96 | const powerPreference = options?.powerPreference ?? 'high-performance'; |
| 97 | |
| 98 | return { |
| 99 | adapter: null, |
| 100 | device: null, |
| 101 | initialized: false, |
| 102 | canvas: canvas || null, |
| 103 | canvasContext: null, |
| 104 | preferredFormat: null, |
| 105 | devicePixelRatio: dpr, |
| 106 | alphaMode, |
| 107 | powerPreference, |
| 108 | }; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Initializes the WebGPU context by requesting an adapter and device. |
no outgoing calls
no test coverage detected