Skip to content

Commit 16bed15

Browse files
fix: If already an IGPUFunction don't convert to one
1 parent 7f1c96e commit 16bed15

File tree

9 files changed

+78
-57
lines changed

9 files changed

+78
-57
lines changed

dist/gpu-browser-core.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* GPU Accelerated JavaScript
66
*
7-
* @version 2.6.0
8-
* @date Sat Jan 18 2020 08:34:13 GMT-0500 (Eastern Standard Time)
7+
* @version 2.6.2
8+
* @date Sun Jan 19 2020 07:44:58 GMT-0500 (Eastern Standard Time)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -6110,7 +6110,9 @@ class Kernel {
61106110
}
61116111

61126112
addFunction(source, settings = {}) {
6113-
if ('settings' in source && 'source' in source) {
6113+
if (source.name && source.source && source.argumentTypes && 'returnType' in source) {
6114+
this.functions.push(source);
6115+
} else if ('settings' in source && 'source' in source) {
61146116
this.functions.push(this.functionToIGPUFunction(source.source, source.settings));
61156117
} else if (typeof source === 'string' || typeof source === 'function') {
61166118
this.functions.push(this.functionToIGPUFunction(source, settings));

dist/gpu-browser-core.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/gpu-browser.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* GPU Accelerated JavaScript
66
*
7-
* @version 2.6.0
8-
* @date Sat Jan 18 2020 08:34:13 GMT-0500 (Eastern Standard Time)
7+
* @version 2.6.2
8+
* @date Sun Jan 19 2020 07:44:57 GMT-0500 (Eastern Standard Time)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -10544,7 +10544,9 @@ class Kernel {
1054410544
}
1054510545

1054610546
addFunction(source, settings = {}) {
10547-
if ('settings' in source && 'source' in source) {
10547+
if (source.name && source.source && source.argumentTypes && 'returnType' in source) {
10548+
this.functions.push(source);
10549+
} else if ('settings' in source && 'source' in source) {
1054810550
this.functions.push(this.functionToIGPUFunction(source.source, source.settings));
1054910551
} else if (typeof source === 'string' || typeof source === 'function') {
1055010552
this.functions.push(this.functionToIGPUFunction(source, settings));

dist/gpu-browser.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gpu.js",
3-
"version": "2.6.1",
3+
"version": "2.6.2",
44
"description": "GPU Accelerated JavaScript",
55
"engines": {
66
"node": ">=8.0.0"

src/backend/kernel.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,12 +320,14 @@ class Kernel {
320320

321321
/**
322322
*
323-
* @param {KernelFunction|string} source
323+
* @param {KernelFunction|string|IGPUFunction} source
324324
* @param {IFunctionSettings} [settings]
325325
* @return {Kernel}
326326
*/
327327
addFunction(source, settings = {}) {
328-
if ('settings' in source && 'source' in source) {
328+
if (source.name && source.source && source.argumentTypes && 'returnType' in source) {
329+
this.functions.push(source);
330+
} else if ('settings' in source && 'source' in source) {
329331
this.functions.push(this.functionToIGPUFunction(source.source, source.settings));
330332
} else if (typeof source === 'string' || typeof source === 'function') {
331333
this.functions.push(this.functionToIGPUFunction(source, settings));

src/index.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class GPU {
1313
nativeFunctions: IGPUNativeFunction[];
1414
setFunctions(flag: IFunction[]|KernelFunction[]): this;
1515
setNativeFunctions(flag: IGPUNativeFunction[]): this;
16-
addFunction(kernel: KernelFunction, settings?: IGPUFunctionSettings): this;
16+
addFunction(kernel: KernelFunction|IGPUFunction|string, settings?: IGPUFunctionSettings): this;
1717
addNativeFunction(name: string, source: string, settings?: IGPUFunctionSettings): this;
1818
combineKernels(...kernels: KernelFunction[]): IKernelRunShortcut;
1919
combineKernels<KF extends KernelFunction>(...kernels: KF[]):
@@ -213,8 +213,8 @@ export class Kernel {
213213
setImmutable(flag: boolean): this;
214214
setCanvas(flag: any): this;
215215
setContext(flag: any): this;
216-
addFunction(flag: KernelFunction|string, settings?: IFunctionSettings): this;
217-
setFunctions(flag: IFunction[]|KernelFunction[]): this;
216+
addFunction(flag: KernelFunction|IGPUFunction|string, settings?: IFunctionSettings): this;
217+
setFunctions(flag: IFunction[]|KernelFunction[]|IGPUFunction[]|string[]): this;
218218
setNativeFunctions(flag: IGPUNativeFunction[]): this;
219219
setStrictIntegers(flag: boolean): this;
220220
setTactic(flag: Tactic): this;

test/features/add-custom-function.js

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,11 @@ test('cpu', () => {
401401

402402
describe('features: setFunctions from array on kernel');
403403

404-
function testSetFunctionsFromArrayOnGPU(nativeFunction, mode) {
404+
function testSetFunctionsFromArrayOnGPU(mode) {
405405
const gpu = new GPU({ mode });
406-
assert.equal(gpu.setNativeFunctions([nativeFunction]), gpu);
406+
assert.equal(gpu.setFunctions([function custom() {
407+
return 1;
408+
}]), gpu);
407409
const kernel = gpu.createKernel(function() {
408410
return custom();
409411
}, { output: [1] });
@@ -412,56 +414,69 @@ function testSetFunctionsFromArrayOnGPU(nativeFunction, mode) {
412414
}
413415

414416
test('auto', () => {
415-
testSetFunctionsFromArrayOnGPU({
416-
name: 'custom',
417-
source: `float custom() {
418-
return 1.0;
419-
}`,
420-
});
417+
testSetFunctionsFromArrayOnGPU();
421418
});
422419

423420
test('gpu', () => {
424-
testSetFunctionsFromArrayOnGPU({
425-
name: 'custom',
426-
source: `float custom() {
427-
return 1.0;
428-
}`,
429-
}, 'gpu');
421+
testSetFunctionsFromArrayOnGPU('gpu');
430422
});
431423

432424
(GPU.isWebGLSupported ? test : skip)('webgl', () => {
433-
testSetFunctionsFromArrayOnGPU({
434-
name: 'custom',
435-
source: `float custom() {
436-
return 1.0;
437-
}`,
438-
}, 'webgl');
425+
testSetFunctionsFromArrayOnGPU('webgl');
439426
});
440427

441428
(GPU.isWebGL2Supported ? test : skip)('webgl2', () => {
442-
testSetFunctionsFromArrayOnGPU({
443-
name: 'custom',
444-
source: `float custom() {
445-
return 1.0;
446-
}`,
447-
}, 'webgl2');
429+
testSetFunctionsFromArrayOnGPU('webgl2');
448430
});
449431

450432
(GPU.isHeadlessGLSupported ? test : skip)('headlessgl', () => {
451-
testSetFunctionsFromArrayOnGPU({
452-
name: 'custom',
453-
source: `float custom() {
454-
return 1.0;
455-
}`,
456-
}, 'headlessgl');
433+
testSetFunctionsFromArrayOnGPU('headlessgl');
457434
});
458435

459436
test('cpu', () => {
460-
testSetFunctionsFromArrayOnGPU({
461-
name: 'custom',
462-
source: `function custom() {
463-
return 1.0;
437+
testSetFunctionsFromArrayOnGPU('cpu');
438+
});
439+
440+
describe('features: setFunctions from array on kernel');
441+
442+
function testAddIGPUFunction(mode) {
443+
const gpu = new GPU({ mode });
444+
const kernel = gpu.createKernel(function(value) {
445+
return custom(value);
446+
})
447+
.setOutput([1])
448+
.addFunction({
449+
name: 'custom',
450+
argumentTypes: { value: 'Number' },
451+
source: `function custom(value) {
452+
return value + 1.0;
464453
}`,
465-
returnType: 'Number'
466-
}, 'cpu');
454+
returnType: 'Number',
455+
});
456+
assert.equal(kernel(1)[0], 2);
457+
gpu.destroy();
458+
}
459+
460+
test('auto', () => {
461+
testAddIGPUFunction();
462+
});
463+
464+
test('gpu', () => {
465+
testAddIGPUFunction('gpu');
466+
});
467+
468+
(GPU.isWebGLSupported ? test : skip)('webgl', () => {
469+
testAddIGPUFunction('webgl');
470+
});
471+
472+
(GPU.isWebGL2Supported ? test : skip)('webgl2', () => {
473+
testAddIGPUFunction('webgl2');
474+
});
475+
476+
(GPU.isHeadlessGLSupported ? test : skip)('headlessgl', () => {
477+
testAddIGPUFunction('headlessgl');
478+
});
479+
480+
test('cpu', () => {
481+
testAddIGPUFunction('cpu');
467482
});

0 commit comments

Comments
 (0)