From: Chris Duncan Date: Sun, 23 Mar 2025 06:42:18 +0000 (-0700) Subject: Check for API support manually before re-exporting them in barrel. Only initialize... X-Git-Tag: v4.0.6~3 X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=574f547b265ad4de49afe98a2c838a42f6a3e62b;p=nano-pow.git Check for API support manually before re-exporting them in barrel. Only initialize each API once methods are called. --- diff --git a/src/lib/gl/index.ts b/src/lib/gl/index.ts index c03954c..ad736e1 100644 --- a/src/lib/gl/index.ts +++ b/src/lib/gl/index.ts @@ -14,6 +14,7 @@ export class NanoPowGl { static #SEND: bigint = 0xfffffff800000000n static #RECEIVE: bigint = 0xfffffe0000000000n + static #isInitialized: boolean = false static #busy: boolean = false static #debug: boolean = false static #raf: number = 0 @@ -177,12 +178,13 @@ export class NanoPowGl { /** Finalize configuration */ this.#query = this.#gl.createQuery() this.#pixels = new Uint32Array(this.size * 4) - console.log(`NanoPow WebGL initialized at ${this.#gl.drawingBufferWidth}x${this.#gl.drawingBufferHeight}. Maximum nonces checked per frame: ${this.size}`) } catch (err) { throw new Error('WebGL initialization failed.', { cause: err }) } finally { this.#busy = false } + this.#isInitialized = true + console.log(`NanoPow WebGL initialized at ${this.#gl.drawingBufferWidth}x${this.#gl.drawingBufferHeight}. Maximum nonces checked per frame: ${this.size}`) } /** @@ -366,6 +368,7 @@ export class NanoPowGl { }, 100) }) } + if (this.#isInitialized === false) this.init() this.#busy = true if (typeof options?.threshold === 'string') { @@ -465,6 +468,7 @@ export class NanoPowGl { }, 100) }) } + if (this.#isInitialized === false) this.init() this.#busy = true if (typeof options?.threshold === 'string') { diff --git a/src/lib/gpu/index.ts b/src/lib/gpu/index.ts index ba4cb7a..0819283 100644 --- a/src/lib/gpu/index.ts +++ b/src/lib/gpu/index.ts @@ -13,6 +13,7 @@ export class NanoPowGpu { static #RECEIVE: bigint = 0xfffffe0000000000n // Initialize WebGPU + static #isInitialized: boolean = false static #busy: boolean = false static #debug: boolean = false static #device: GPUDevice | null = null @@ -44,6 +45,7 @@ export class NanoPowGpu { } finally { this.#busy = false } + this.#isInitialized = true } static setup (): void { @@ -230,6 +232,7 @@ export class NanoPowGpu { }, 100) }) } + if (this.#isInitialized === false) this.init() this.#busy = true if (typeof options?.threshold === 'string') { @@ -308,6 +311,7 @@ export class NanoPowGpu { }, 100) }) } + if (this.#isInitialized === false) this.init() this.#busy = true if (typeof options?.threshold === 'string') { diff --git a/src/lib/index.ts b/src/lib/index.ts index b674851..a6edffb 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -6,15 +6,15 @@ import { NanoPowGpu } from "./gpu" let isGlSupported, isGpuSupported = false try { - await NanoPowGpu.init() - isGpuSupported = true + const adapter = await navigator?.gpu?.requestAdapter?.() + isGpuSupported = (adapter instanceof GPUAdapter) } catch (err) { console.warn('WebGPU is not supported in this environment.\n', err) isGpuSupported = false } try { - await NanoPowGl.init() - isGlSupported = true + const gl = new OffscreenCanvas(0, 0)?.getContext?.('webgl2') + isGlSupported = (gl instanceof WebGL2RenderingContext) } catch (err) { console.warn('WebGL is not supported in this environment.\n', err) isGlSupported = false