|
| 1 | +const { assert, skip, test, module: describe } = require('qunit'); |
| 2 | +const { GPU } = require('../../src'); |
| 3 | + |
| 4 | +describe('feature: modulo comparison'); |
| 5 | + |
| 6 | +// `%` lowers to a float-returning GLSL helper, but getType() used to report the |
| 7 | +// left operand's type when fixIntegerDivisionAccuracy was off, so `i % 2 === 0` |
| 8 | +// compiled to a float/int comparison and the shader was rejected outright: |
| 9 | +// '==' : no operation '==' exists that takes a left-hand operand of type |
| 10 | +// 'lowp float' and a right operand of type 'const int' |
| 11 | +// The flag is derived from GPU features, so the break only showed on hardware |
| 12 | +// where integer division is already accurate — desktop Chrome/Firefox on |
| 13 | +// Windows, but not headless-gl or Apple GPUs. Both states are pinned here. |
| 14 | +function testModuloEquality(mode, fixIntegerDivisionAccuracy) { |
| 15 | + const gpu = new GPU({ mode }); |
| 16 | + const kernel = gpu.createKernel(function () { |
| 17 | + let total = 0; |
| 18 | + for (let i = 0; i < 10; i++) { |
| 19 | + if (i % 2 === 0) { |
| 20 | + total += i; |
| 21 | + } |
| 22 | + } |
| 23 | + return total; |
| 24 | + }, { output: [1], fixIntegerDivisionAccuracy }); |
| 25 | + // 0 + 2 + 4 + 6 + 8 |
| 26 | + assert.equal(kernel()[0], 20); |
| 27 | + gpu.destroy(); |
| 28 | +} |
| 29 | + |
| 30 | +function testModuloInequality(mode, fixIntegerDivisionAccuracy) { |
| 31 | + const gpu = new GPU({ mode }); |
| 32 | + const kernel = gpu.createKernel(function () { |
| 33 | + let total = 0; |
| 34 | + for (let i = 0; i < 6; i++) { |
| 35 | + if (i % 3 !== 0) { |
| 36 | + total += 1; |
| 37 | + } |
| 38 | + } |
| 39 | + return total; |
| 40 | + }, { output: [1], fixIntegerDivisionAccuracy }); |
| 41 | + // 1, 2, 4, 5 |
| 42 | + assert.equal(kernel()[0], 4); |
| 43 | + gpu.destroy(); |
| 44 | +} |
| 45 | + |
| 46 | +function testModuloAsIndex(mode, fixIntegerDivisionAccuracy) { |
| 47 | + const gpu = new GPU({ mode }); |
| 48 | + const kernel = gpu.createKernel(function (a) { |
| 49 | + return a[this.thread.x % 3]; |
| 50 | + }, { output: [4], fixIntegerDivisionAccuracy }); |
| 51 | + assert.deepEqual(Array.from(kernel([10, 20, 30, 40])), [10, 20, 30, 10]); |
| 52 | + gpu.destroy(); |
| 53 | +} |
| 54 | + |
| 55 | +function testModuloKeepsFraction(mode, fixIntegerDivisionAccuracy) { |
| 56 | + const gpu = new GPU({ mode }); |
| 57 | + const kernel = gpu.createKernel(function (a) { |
| 58 | + return a[this.thread.x] % 2.5; |
| 59 | + }, { output: [4], fixIntegerDivisionAccuracy }); |
| 60 | + const result = Array.from(kernel([1, 3, 6, 9])); |
| 61 | + [1, 0.5, 1, 1.5].forEach((expected, i) => { |
| 62 | + assert.ok(Math.abs(result[i] - expected) < 0.01, `index ${i}: expected ${expected}, got ${result[i]}`); |
| 63 | + }); |
| 64 | + gpu.destroy(); |
| 65 | +} |
| 66 | + |
| 67 | +const scenarios = { |
| 68 | + 'modulo equality': testModuloEquality, |
| 69 | + 'modulo inequality': testModuloInequality, |
| 70 | + 'modulo as index': testModuloAsIndex, |
| 71 | + 'modulo keeps fraction': testModuloKeepsFraction, |
| 72 | +}; |
| 73 | + |
| 74 | +const modes = { |
| 75 | + auto: undefined, |
| 76 | + gpu: 'gpu', |
| 77 | + webgl: 'webgl', |
| 78 | + webgl2: 'webgl2', |
| 79 | + headlessgl: 'headlessgl', |
| 80 | + cpu: 'cpu', |
| 81 | +}; |
| 82 | + |
| 83 | +const supported = { |
| 84 | + auto: GPU.isGPUSupported, |
| 85 | + gpu: GPU.isGPUSupported, |
| 86 | + webgl: GPU.isWebGLSupported, |
| 87 | + webgl2: GPU.isWebGL2Supported, |
| 88 | + headlessgl: GPU.isHeadlessGLSupported, |
| 89 | + cpu: true, |
| 90 | +}; |
| 91 | + |
| 92 | +Object.keys(scenarios).forEach(scenario => { |
| 93 | + Object.keys(modes).forEach(modeName => { |
| 94 | + [false, true].forEach(fix => { |
| 95 | + const run = supported[modeName] ? test : skip; |
| 96 | + run(`${scenario} ${modeName} fixIntegerDivisionAccuracy=${fix}`, () => { |
| 97 | + scenarios[scenario](modes[modeName], fix); |
| 98 | + }); |
| 99 | + }); |
| 100 | + }); |
| 101 | +}); |
0 commit comments