forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.random.js
More file actions
112 lines (93 loc) · 3.01 KB
/
math.random.js
File metadata and controls
112 lines (93 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const { assert, skip, test, module: describe, only } = require('qunit');
const sinon = require('sinon');
const { GPU, plugins: { mathRandom } } = require('../../src');
describe('Math.random() unique');
function mathRandomUnique(mode) {
const gpu = new GPU({ mode });
const checkCount = 20;
let seed1 = Math.random();
let seed2 = Math.random();
let stub = sinon.stub(mathRandom, 'onBeforeRun').callsFake((kernel) => {
kernel.setUniform1f('randomSeed1', seed1);
kernel.setUniform1f('randomSeed2', seed2);
});
try {
gpu.addNativeFunction('getSeed', `highp float getSeed() {
return randomSeedShift;
}`);
const kernel = gpu.createKernel(function () {
const v = Math.random();
return getSeed();
}, {output: [1]});
const results = [];
for (let i = 0; i < checkCount; i++) {
const result = kernel();
assert.ok(results.indexOf(result[0]) === -1, `duplication at index ${results.indexOf(result[0])} from new value ${result[0]}. Values ${JSON.stringify(results)}`);
results.push(result[0]);
seed2 = result[0];
assert.ok(stub.called);
stub.restore();
stub.callsFake((kernel) => {
kernel.setUniform1f('randomSeed1', seed1);
kernel.setUniform1f('randomSeed2', seed2);
});
}
} finally {
stub.restore();
gpu.destroy();
}
}
test('unique every time auto', () => {
mathRandomUnique();
});
test('unique every time gpu', () => {
mathRandomUnique('gpu');
});
(GPU.isWebGLSupported ? test : skip)('unique every time webgl', () => {
mathRandomUnique('webgl');
});
(GPU.isWebGL2Supported ? test : skip)('unique every time webgl2', () => {
mathRandomUnique('webgl2');
});
(GPU.isHeadlessGLSupported ? test : skip)('unique every time headlessgl', () => {
mathRandomUnique('headlessgl');
});
describe('never above 1');
function mathRandomNeverAboveOne(mode) {
const gpu = new GPU({ mode });
const checkCount = 20;
const checkSource = [];
for (let i = 0; i < checkCount; i++) {
checkSource.push(`const check${ i } = Math.random();`);
}
for (let i = 0; i < checkCount; i++) {
for (let j = 0; j < checkCount; j++) {
if (i === j) continue;
checkSource.push(`if (check${i} >= 1) return 1;`);
}
}
const kernel = gpu.createKernel(`function() {
${checkSource.join('\n')}
return 0;
}`, { output: [1] });
const result = kernel();
assert.ok(result.every(value => value === 0));
}
test('never above 1 every time auto', () => {
mathRandomNeverAboveOne();
});
test('never above 1 every time gpu', () => {
mathRandomNeverAboveOne('gpu');
});
(GPU.isWebGLSupported ? test : skip)('never above 1 every time webgl', () => {
mathRandomNeverAboveOne('webgl');
});
(GPU.isWebGL2Supported ? test : skip)('never above 1 every time webgl2', () => {
mathRandomNeverAboveOne('webgl2');
});
(GPU.isHeadlessGLSupported ? test : skip)('never above 1 every time headlessgl', () => {
mathRandomNeverAboveOne('headlessgl');
});
test('never above 1 every time cpu', () => {
mathRandomNeverAboveOne('cpu');
});