forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinject-native.js
More file actions
82 lines (65 loc) · 1.7 KB
/
inject-native.js
File metadata and controls
82 lines (65 loc) · 1.7 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
const { assert, skip, test, module: describe, only } = require('qunit');
const { GPU } = require('../../src');
describe('features: inject native');
function gpuAddAB(mode) {
const gpu = new GPU({mode});
gpu
.injectNative(`
int customAdder(int a, int b) {
return a + b;
}
`)
.addNativeFunction('customAdderLink', `int customAdderLink(int a, int b) {
return customAdder(a, b);
}`);
const kernel = gpu.createKernel(function (a, b) {
return customAdderLink(a[this.thread.x], b[this.thread.x]);
}, {
output: [6],
returnType: 'Integer'
});
const a = [1, 2, 3, 5, 6, 7];
const b = [4, 5, 6, 1, 2, 3];
const result = kernel(a, b);
const expected = [5, 7, 9, 6, 8, 10];
assert.deepEqual(Array.from(result), expected);
gpu.destroy();
}
test('addAB auto', () => {
gpuAddAB(null);
});
test('addAB gpu', () => {
gpuAddAB('gpu');
});
(GPU.isWebGLSupported ? test : skip)('addAB webgl', () => {
gpuAddAB('webgl');
});
(GPU.isWebGL2Supported ? test : skip)('addAB webgl2', () => {
gpuAddAB('webgl2');
});
(GPU.isHeadlessGLSupported ? test : skip)('addAB headlessgl', () => {
gpuAddAB('headlessgl');
});
function cpuAddAB(mode) {
function customAdder(a, b) {
return a + b;
}
const gpu = new GPU({mode});
gpu
.injectNative(customAdder.toString());
const kernel = gpu.createKernel(function (a, b) {
return customAdder(a[this.thread.x], b[this.thread.x]);
}, {
output: [6],
returnType: 'Integer'
});
const a = [1, 2, 3, 5, 6, 7];
const b = [4, 5, 6, 1, 2, 3];
const result = kernel(a, b);
const expected = [5, 7, 9, 6, 8, 10];
assert.deepEqual(Array.from(result), expected);
gpu.destroy();
}
test('addAB cpu', () => {
cpuAddAB('cpu');
});