Skip to content

Commit a0a9258

Browse files
fix: #378 unit test, showing the issue resolved
fix: Random demo, when webgl or webgl2 not available, use try to keep demo running
1 parent 05dd174 commit a0a9258

File tree

3 files changed

+63
-15
lines changed

3 files changed

+63
-15
lines changed

examples/random.html

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,24 @@ <h2>WebGL2 Random</h2>
1414
</body>
1515
<script src="../bin/gpu-browser.js"></script>
1616
<script>
17-
const cpu = new GPU({
17+
let cpu, webGL, webGL2;
18+
19+
cpu = new GPU({
1820
mode: 'cpu',
1921
canvas: document.getElementById('cpu-random-output')
2022
});
21-
const webGL = new GPU({
22-
mode: 'webgl',
23-
canvas: document.getElementById('web-gl-random-output')
24-
});
25-
const webGL2 = new GPU({
26-
mode: 'webgl2',
27-
canvas: document.getElementById('web-gl2-random-output')
28-
});
23+
try {
24+
webGL = new GPU({
25+
mode: 'webgl',
26+
canvas: document.getElementById('web-gl-random-output')
27+
});
28+
} catch (e) {}
29+
try {
30+
webGL2 = new GPU({
31+
mode: 'webgl2',
32+
canvas: document.getElementById('web-gl2-random-output')
33+
});
34+
} catch (e) {}
2935

3036
function drawRandomFunction() {
3137
this.color(Math.random(), Math.random(), Math.random());
@@ -35,13 +41,17 @@ <h2>WebGL2 Random</h2>
3541
.setGraphical(true)
3642
.setOutput([100, 100]);
3743

38-
const webGLDrawRandom = webGL.createKernel(drawRandomFunction)
39-
.setGraphical(true)
40-
.setOutput([100, 100]);
44+
const webGLDrawRandom = webGL
45+
? webGL.createKernel(drawRandomFunction)
46+
.setGraphical(true)
47+
.setOutput([100, 100])
48+
: () => {};
4149

42-
const webGL2DrawRandom = webGL2.createKernel(drawRandomFunction)
43-
.setGraphical(true)
44-
.setOutput([100, 100]);
50+
const webGL2DrawRandom = webGL2
51+
? webGL2.createKernel(drawRandomFunction)
52+
.setGraphical(true)
53+
.setOutput([100, 100])
54+
: () => {};
4555

4656
function draw() {
4757
cpuDrawRandom();

test/all.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
<script type="module" src="issues/349-division-by-factors-of-3.js"></script>
152152
<script type="module" src="issues/357-modulus-issue.js"></script>
153153
<script type="module" src="issues/359-addfunction-params-wrong.js"></script>
154+
<script type="module" src="issues/378-only-first-iteration.js"></script>
154155
<script type="module" src="issues/382-bad-constant.js"></script>
155156
<script type="module" src="issues/390-thread-assignment.js"></script>
156157
<script type="module" src="issues/396-combine-kernels-example.js"></script>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const { assert, skip, test, module: describe, only } = require('qunit');
2+
const { GPU } = require('../../src');
3+
4+
describe('issue #378');
5+
6+
function testOnlyFirstIterationSafari(mode) {
7+
const gpu = new GPU({ mode: mode });
8+
const conflictingName = 0.4;
9+
const kernel = gpu.createKernel(function(iter) {
10+
let sum = 0;
11+
for(let i=2; i<iter; i++) {
12+
sum = sum + i;
13+
}
14+
return 2*sum ; //+ this.thread.x;
15+
})
16+
.setOutput([10])
17+
.setConstants({
18+
conflictingName: conflictingName
19+
});
20+
21+
const result = kernel(5);
22+
23+
assert.deepEqual(Array.from(result), [18,18,18,18,18,18,18,18,18,18]);
24+
gpu.destroy();
25+
}
26+
27+
(GPU.isWebGLSupported ? test : skip)('Issue #378 - only first iteration safari webgl', () => {
28+
testOnlyFirstIterationSafari('webgl');
29+
});
30+
31+
(GPU.isWebGL2Supported ? test : skip)('Issue #378 - only first iteration safari webgl2', () => {
32+
testOnlyFirstIterationSafari('webgl2');
33+
});
34+
35+
(GPU.isHeadlessGLSupported ? test : skip)('Issue #378 - only first iteration safari headlessgl', () => {
36+
testOnlyFirstIterationSafari('headlessgl');
37+
});

0 commit comments

Comments
 (0)