]> zoso.dev Git - nano-pow.git/commitdiff
Check for API support manually before re-exporting them in barrel. Only initialize...
authorChris Duncan <chris@zoso.dev>
Sun, 23 Mar 2025 06:42:18 +0000 (23:42 -0700)
committerChris Duncan <chris@zoso.dev>
Sun, 23 Mar 2025 06:42:18 +0000 (23:42 -0700)
src/lib/gl/index.ts
src/lib/gpu/index.ts
src/lib/index.ts

index c03954cb71f3a92fec9b42542c67ccb09eb508b4..ad736e18b015fcc7b836dfe600f708ea753c1f25 100644 (file)
@@ -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') {
index ba4cb7a79e3ac4d1c88ed0b6a09ff8934f6cad16..08192831c1794e683a59f95e8a09c73a7ed4c995 100644 (file)
@@ -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') {
index b674851450c55060c65dc5376bf2b7536aad60b4..a6edffb2f60f5127266b8ea602948c87f1ec319f 100644 (file)
@@ -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