]> git.codecow.com Git - nano-pow.git/commitdiff
Fix max viewport dimensions.
authorChris Duncan <chris@zoso.dev>
Thu, 2 Apr 2026 07:23:05 +0000 (00:23 -0700)
committerChris Duncan <chris@zoso.dev>
Thu, 2 Apr 2026 07:23:05 +0000 (00:23 -0700)
Use MDN-recommended fallback max viewport dimensions if they return null, currently due to iOS Safari regression. Wrap entire test page script in try-catch to log additional errors.

src/lib/generate/webgl/index.ts
test/index.html

index 9dffe9251990c55fb49e2f46190a6601847531ee..2340749dba69eb57865fbc860052bc15e472b29d 100644 (file)
@@ -91,9 +91,10 @@ function createCanvas (size: number): void {
        if (context == null) throw new Error('WebGL 2 is required')
        gl = context
 
+       const MAX_VIEWPORT_DIMS = gl.getParameter(gl.MAX_VIEWPORT_DIMS) ?? [0x1000, 0x1000]
        LOG: logger.log('NanoPow WebGL createCanvas', 'requested size', size)
-       LOG: logger.log('NanoPow WebGL createCanvas', 'MAX_VIEWPORT_DIMS', ...gl.getParameter(gl.MAX_VIEWPORT_DIMS))
-       size = Math.min(size, ...gl.getParameter(gl.MAX_VIEWPORT_DIMS))
+       LOG: logger.log('NanoPow WebGL createCanvas', 'MAX_VIEWPORT_DIMS', MAX_VIEWPORT_DIMS)
+       size = Math.min(size, ...MAX_VIEWPORT_DIMS)
        size = Math.floor(size / 0x100) * 0x100
        canvas.height = canvas.width = size
        LOG: logger.log('NanoPow WebGL createCanvas', 'canvas size', canvas.height, canvas.width)
index 3540d2bf34cf2ccc6a60ef2f761fd9e3082d7e41..98d48c01d8b589d089a8264305f896f828946520 100644 (file)
@@ -8,6 +8,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
 <head>
        <link rel="icon" href="data:,">
        <script type="module">
+       try {
                let NanoPow, stats
                try {
                        ({ NanoPow, stats } = await import('../dist/main.min.js'))
@@ -27,8 +28,9 @@ SPDX-License-Identifier: GPL-3.0-or-later
 
                const glSize = (canvas => {
                        const gl = canvas.getContext('webgl2')
-                       canvas.height = gl.getParameter(gl.MAX_VIEWPORT_DIMS)[0]
-                       canvas.width = gl.getParameter(gl.MAX_VIEWPORT_DIMS)[1]
+                       const MAX_VIEWPORT_DIMS = gl.getParameter(gl.MAX_VIEWPORT_DIMS)
+                       canvas.height = MAX_VIEWPORT_DIMS[0]
+                       canvas.width = MAX_VIEWPORT_DIMS[1]
                        return Math.min(gl.drawingBufferHeight, gl.drawingBufferWidth)
                })(new OffscreenCanvas(0, 0))
 
@@ -52,7 +54,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
                        }
                }
 
-               export async function run (size, difficulty, effort, api, isOutputShown, isDebug, isSelfCheck) {
+               async function run (size, difficulty, effort, api, isOutputShown, isDebug, isSelfCheck) {
                        // Generate once on load to compile shaders and initialize buffers
                        await NanoPow.work_generate(random(), { api, difficulty: 0 })
                        const type = api
@@ -187,7 +189,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
                        console.log('%cTESTING COMPLETE', 'color:orange;font-weight:bold')
                }
 
-               export async function score (runs, size, difficulty, effort, api) {
+               async function score (runs, size, difficulty, effort, api) {
                        console.log(`%cNanoPow ${api}`, 'color:green', `Calculate truncated harmonic mean of the truncated arithmetic rate across ${runs} runs of ${size} samples.`)
                        const rates = []
                        for (let i = 0; i < runs; i++) {
@@ -281,9 +283,14 @@ SPDX-License-Identifier: GPL-3.0-or-later
                                .then(() => event.target.disabled = false)
                }
 
+               document.getElementById('api_webgpu').disabled = navigator?.gpu == null
+               document.getElementById('api_webgl').selected = navigator?.gpu == null
                document.getElementById('btnStartTest').addEventListener('click', startTest)
                document.getElementById('btnStartScore').addEventListener('click', startScore)
                document.getElementById('effort').value = Math.max(1, Math.floor(navigator.hardwareConcurrency) / 2)
+       } catch (err) {
+               console.error(err)
+       }
        </script>
        <style>
                body{background:black;color:white;}a{color:darkcyan;}input[type=number]{width:5em;}input[type=checkbox]{margin-right:2em;}
@@ -319,8 +326,8 @@ SPDX-License-Identifier: GPL-3.0-or-later
        <span>
                <label for="api">API</label>
                <select id="api">
-                       <option>WebGPU</option>
-                       <option>WebGL</option>
+                       <option id="api_webgpu">WebGPU</option>
+                       <option id="api_webgl">WebGL</option>
                        <option>WASM</option>
                        <option>CPU</option>
                </select>