Skip to content

Commit 62d4ea7

Browse files
fix: #396 by changing documentation and adding unit test
1 parent aca2741 commit 62d4ea7

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,15 +505,15 @@ Sometimes you want to do multiple math operations on the gpu without the round t
505505
_**Note:**_ Kernels can have different output sizes.
506506
```js
507507
const add = gpu.createKernel(function(a, b) {
508-
return a + b;
508+
return a[this.thread.x] + b[this.thread.x];
509509
}).setOutput([20]);
510510
511511
const multiply = gpu.createKernel(function(a, b) {
512-
return a * b;
512+
return a[this.thread.x] * b[this.thread.x];
513513
}).setOutput([20]);
514514
515515
const superKernel = gpu.combineKernels(add, multiply, function(a, b, c) {
516-
return multiply(add(a[this.thread.x], b[this.thread.x]), c[this.thread.x]);
516+
return multiply(add(a, b), c);
517517
});
518518
519519
superKernel(a, b, c);

test/all.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
<script type="module" src="issues/359-addfunction-params-wrong.js"></script>
154154
<script type="module" src="issues/382-bad-constant.js"></script>
155155
<script type="module" src="issues/390-thread-assignment.js"></script>
156+
<script type="module" src="issues/396-combine-kernels-example.js"></script>
156157
<script type="module" src="issues/399-double-definition.js"></script>
157158
<script type="module" src="issues/401-cpu-canvas-check.js"></script>
158159
<script type="module" src="issues/422-warnings.js"></script>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const { assert, skip, test, module: describe } = require('qunit');
2+
const { GPU } = require('../../src');
3+
4+
describe('issues #396 - combine kernels example');
5+
6+
function combineKernelsExample(mode) {
7+
const gpu = new GPU({ mode });
8+
const add = gpu.createKernel(function(a, b) {
9+
return a[this.thread.x] + b[this.thread.x];
10+
}).setOutput([5]);
11+
12+
const multiply = gpu.createKernel(function(a, b) {
13+
return a[this.thread.x] * b[this.thread.x];
14+
}).setOutput([5]);
15+
16+
const superKernel = gpu.combineKernels(add, multiply, function(a, b, c) {
17+
return multiply(add(a, b), c);
18+
});
19+
20+
const result = superKernel([1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]);
21+
assert.deepEqual(Array.from(result), [2,
22+
8,
23+
18,
24+
32,
25+
50
26+
]);
27+
gpu.destroy();
28+
}
29+
30+
test('auto', () => {
31+
combineKernelsExample();
32+
});
33+
34+
test('gpu', () => {
35+
combineKernelsExample('gpu');
36+
});
37+
38+
(GPU.isWebGLSupported ? test : skip)('webgl', () => {
39+
combineKernelsExample('webgl');
40+
});
41+
42+
(GPU.isWebGL2Supported ? test : skip)('webgl2', () => {
43+
combineKernelsExample('webgl2');
44+
});
45+
46+
(GPU.isHeadlessGLSupported ? test : skip)('headlessgl', () => {
47+
combineKernelsExample('headlessgl');
48+
});
49+
50+
test('cpu', () => {
51+
combineKernelsExample('cpu');
52+
});

0 commit comments

Comments
 (0)