|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 6 | + <title>GPU.JS : visual rendering probe</title> |
| 7 | + <style> |
| 8 | + body { font: 13px -apple-system, Segoe UI, Roboto, sans-serif; margin: 0; padding: 8px; } |
| 9 | + canvas { border: 1px solid #ccc; image-rendering: pixelated; } |
| 10 | + .scene { display: inline-block; margin: 4px; } |
| 11 | + </style> |
| 12 | + <script src="../../dist/gpu-browser.js"></script> |
| 13 | +</head> |
| 14 | +<body> |
| 15 | +<div id="scenes"></div> |
| 16 | +<script> |
| 17 | +(function () { |
| 18 | + 'use strict'; |
| 19 | + |
| 20 | + const results = window.__gpujsVisual = { done: false, error: null, env: {}, scenes: [] }; |
| 21 | + |
| 22 | + // FNV-1a over the raw pixel bytes. Exact-match across devices is the strict |
| 23 | + // bar; the per-channel stats below are what a tolerant comparison would use. |
| 24 | + function hash(bytes) { |
| 25 | + let h = 0x811c9dc5; |
| 26 | + for (let i = 0; i < bytes.length; i++) { |
| 27 | + h ^= bytes[i]; |
| 28 | + h = (h * 0x01000193) >>> 0; |
| 29 | + } |
| 30 | + return h.toString(16).padStart(8, '0'); |
| 31 | + } |
| 32 | + |
| 33 | + function summarise(pixels) { |
| 34 | + let sum = 0; |
| 35 | + let nonZero = 0; |
| 36 | + for (let i = 0; i < pixels.length; i++) { |
| 37 | + sum += pixels[i]; |
| 38 | + if (pixels[i] !== 0) nonZero++; |
| 39 | + } |
| 40 | + return { mean: +(sum / pixels.length).toFixed(4), nonZero }; |
| 41 | + } |
| 42 | + |
| 43 | + const SIZE = 64; |
| 44 | + |
| 45 | + const scenes = { |
| 46 | + // flat fill: any device disagreeing here has a colour-management problem |
| 47 | + flat: function () { |
| 48 | + return function () { |
| 49 | + this.color(0.2, 0.6, 0.9, 1); |
| 50 | + }; |
| 51 | + }, |
| 52 | + // linear gradient: exercises thread indexing and float->byte quantisation |
| 53 | + gradient: function () { |
| 54 | + return function () { |
| 55 | + this.color(this.thread.x / 64, this.thread.y / 64, 0.5, 1); |
| 56 | + }; |
| 57 | + }, |
| 58 | + // iterative float math: the scene most likely to diverge between GPUs |
| 59 | + mandelbrot: function () { |
| 60 | + return function () { |
| 61 | + const cx = (this.thread.x / 64) * 3.0 - 2.0; |
| 62 | + const cy = (this.thread.y / 64) * 2.4 - 1.2; |
| 63 | + let zx = 0.0; |
| 64 | + let zy = 0.0; |
| 65 | + let iter = 0.0; |
| 66 | + for (let i = 0; i < 48; i++) { |
| 67 | + if (zx * zx + zy * zy < 4.0) { |
| 68 | + const t = zx * zx - zy * zy + cx; |
| 69 | + zy = 2.0 * zx * zy + cy; |
| 70 | + zx = t; |
| 71 | + iter = iter + 1.0; |
| 72 | + } |
| 73 | + } |
| 74 | + const shade = iter / 48.0; |
| 75 | + this.color(shade, shade * shade, 1.0 - shade, 1); |
| 76 | + }; |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + try { |
| 81 | + results.env = { |
| 82 | + userAgent: navigator.userAgent, |
| 83 | + devicePixelRatio: window.devicePixelRatio, |
| 84 | + isWebGLSupported: GPU.isWebGLSupported, |
| 85 | + isWebGL2Supported: GPU.isWebGL2Supported |
| 86 | + }; |
| 87 | + |
| 88 | + const modes = []; |
| 89 | + if (GPU.isWebGLSupported) modes.push('webgl'); |
| 90 | + if (GPU.isWebGL2Supported) modes.push('webgl2'); |
| 91 | + modes.push('cpu'); |
| 92 | + |
| 93 | + modes.forEach(function (mode) { |
| 94 | + Object.keys(scenes).forEach(function (name) { |
| 95 | + const entry = { mode: mode, scene: name }; |
| 96 | + let gpu = null; |
| 97 | + try { |
| 98 | + gpu = new GPU({ mode: mode }); |
| 99 | + const kernel = gpu.createKernel(scenes[name](), { |
| 100 | + output: [SIZE, SIZE], |
| 101 | + graphical: true |
| 102 | + }); |
| 103 | + kernel(); |
| 104 | + |
| 105 | + const pixels = kernel.getPixels(); |
| 106 | + entry.pixelHash = hash(pixels); |
| 107 | + entry.stats = summarise(pixels); |
| 108 | + entry.corners = [ |
| 109 | + Array.from(pixels.slice(0, 4)), |
| 110 | + Array.from(pixels.slice((SIZE - 1) * 4, SIZE * 4)), |
| 111 | + Array.from(pixels.slice(pixels.length - 4)) |
| 112 | + ]; |
| 113 | + |
| 114 | + // does the canvas itself read back, independently of getPixels? |
| 115 | + const canvas = kernel.canvas; |
| 116 | + if (canvas) { |
| 117 | + const wrap = document.createElement('div'); |
| 118 | + wrap.className = 'scene'; |
| 119 | + wrap.appendChild(canvas); |
| 120 | + document.getElementById('scenes').appendChild(wrap); |
| 121 | + try { |
| 122 | + const url = canvas.toDataURL('image/png'); |
| 123 | + entry.dataUrlLength = url.length; |
| 124 | + entry.dataUrlHash = hash(url.slice(22)); |
| 125 | + } catch (e) { |
| 126 | + entry.dataUrlError = e.message; |
| 127 | + } |
| 128 | + } |
| 129 | + } catch (e) { |
| 130 | + entry.error = (e && e.message) || String(e); |
| 131 | + } finally { |
| 132 | + if (gpu) { try { gpu.destroy(); } catch (e) {} } |
| 133 | + } |
| 134 | + results.scenes.push(entry); |
| 135 | + }); |
| 136 | + }); |
| 137 | + } catch (e) { |
| 138 | + results.error = (e && e.stack) || String(e); |
| 139 | + } |
| 140 | + |
| 141 | + results.done = true; |
| 142 | +})(); |
| 143 | +</script> |
| 144 | +</body> |
| 145 | +</html> |
0 commit comments