forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathas-file.js
More file actions
37 lines (31 loc) · 1.16 KB
/
as-file.js
File metadata and controls
37 lines (31 loc) · 1.16 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
const { assert, skip, test, module: describe, only } = require('qunit');
const { GPU } = require('../../../src');
describe('features: to-string as file');
function toStringAsFileTest(mode) {
const path = __dirname + `/to-string-as-file-${mode}.js`;
const fs = require('fs');
if (fs.existsSync(path)) {
fs.unlinkSync(path);
}
const gpu = new GPU({ mode });
const kernel = gpu.createKernel(function(v) {
return v[this.thread.y][this.thread.x] + 1;
}, { output: [1, 1] });
const a = [[1]];
const expected = kernel(a);
assert.deepEqual(expected, [new Float32Array([2])]);
const kernelAsString = kernel.toString(a);
fs.writeFileSync(path, `module.exports = ${kernelAsString};`);
const toStringAsFile = require(path);
const restoredKernel = toStringAsFile({ context: kernel.context });
const result = restoredKernel(a);
assert.deepEqual(result, expected);
fs.unlinkSync(path);
gpu.destroy();
}
(GPU.isHeadlessGLSupported ? test : skip)('can save and restore function headlessgl', () => {
toStringAsFileTest('headlessgl');
});
(GPU.isHeadlessGLSupported ? test : skip)('can save and restore function cpu', () => {
toStringAsFileTest('cpu');
});