Skip to content

Commit 7e48127

Browse files
committed
Basic custom function test case
1 parent 5a3bb50 commit 7e48127

3 files changed

Lines changed: 102 additions & 9 deletions

File tree

src/gpu.js

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
///
2+
/// Class: GPU
3+
///
4+
/// GPU.JS core class =D
5+
///
16
GPU = (function() {
27
var gl, canvas;
38

@@ -37,6 +42,7 @@ GPU = (function() {
3742
this.endianness = endianness();
3843

3944
this.functionBuilder = new functionBuilder();
45+
this.functionBuilder.polyfillStandardFunctions();
4046
}
4147

4248
GPU.prototype.getGl = function() {
@@ -47,26 +53,31 @@ GPU = (function() {
4753
return this.canvas;
4854
};
4955

56+
///
57+
/// Function: createKernel
58+
///
5059
/// The core GPU.js function
5160
///
5261
/// The parameter object contains the following sub parameters
5362
///
54-
/// +---------------+---------------+---------------------------------------------------------------------------+
63+
/// |---------------|---------------|---------------------------------------------------------------------------|
5564
/// | Name | Default value | Description |
56-
/// +---------------+---------------+---------------------------------------------------------------------------+
65+
/// |---------------|---------------|---------------------------------------------------------------------------|
5766
/// | dimensions | [1024] | Thread dimension array |
5867
/// | mode | null | CPU / GPU configuration mode, "auto" / null. Has the following modes. |
5968
/// | | | + null / "auto" : Attempts to build GPU mode, else fallbacks |
6069
/// | | | + "gpu" : Attempts to build GPU mode, else fallbacks |
6170
/// | | | + "cpu" : Forces JS fallback mode only |
62-
/// +---------------+---------------+---------------------------------------------------------------------------+
71+
/// |---------------|---------------|---------------------------------------------------------------------------|
6372
///
64-
/// @param inputFunction The calling to perform the conversion
65-
/// @param paramObj The parameter configuration object
73+
/// Parameters:
74+
/// inputFunction {JS Function} The calling to perform the conversion
75+
/// paramObj {Object} The parameter configuration object
6676
///
67-
/// @returns callable function to run
68-
GPU.prototype.createKernel = function(kernel, paramObj) {
69-
77+
/// Returns:
78+
/// callable function to run
79+
///
80+
function createKernel(kernel, paramObj) {
7081
//
7182
// basic parameters safety checks
7283
//
@@ -104,6 +115,26 @@ GPU = (function() {
104115
}
105116
}
106117
};
107-
118+
GPU.prototype.createKernel = createKernel;
119+
120+
///
121+
/// Function: addFunction
122+
///
123+
/// Adds additional functions, that the kernel may call.
124+
///
125+
/// Parameters:
126+
/// jsFunction - {JS Function} JS Function to do conversion
127+
/// paramTypeArray - {[String,...]} Parameter type array, assumes all parameters are "float" if null
128+
/// returnType - {String} The return type, assumes "float" if null
129+
///
130+
/// Retuns:
131+
/// {GPU} returns itself
132+
///
133+
function addFunction( jsFunction, paramTypeArray, returnType ) {
134+
this.functionBuilder.addFunction( null, jsFunction, paramTypeArray, returnType );
135+
return this;
136+
}
137+
GPU.prototype.addFunction = addFunction;
138+
108139
return GPU;
109140
})();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>GPU.JS : add(Custom)Function</title>
6+
<link rel="stylesheet" href="../lib/qunit-1.20.0.css">
7+
8+
<!-- gpu.js scripts -->
9+
<script src="../../src/parser.js"></script>
10+
<script src="../../src/gpu.js"></script>
11+
<script src="../../src/backend/functionNode_webgl.js"></script>
12+
<script src="../../src/backend/functionNode.js"></script>
13+
<script src="../../src/backend/functionBuilder.js"></script>
14+
<script src="../../src/backend/glsl.js"></script>
15+
<script src="../../src/backend/fallback.js"></script>
16+
</head>
17+
<body>
18+
<div id="qunit"></div>
19+
<div id="qunit-fixture"></div>
20+
<script src="../lib/qunit-1.20.0.js"></script>
21+
<script src="../src/basic_addCustomFunction.js"></script>
22+
</body>
23+
</html>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function basic_sum_AB_test( assert, mode ) {
2+
var gpu = new GPU();
3+
4+
function custom_adder(a,b) {
5+
return a+b;
6+
}
7+
gpu.addFunction(custom_adder);
8+
9+
var f = gpu.createKernel(function(a, b) {
10+
return custom_adder(a[this.thread.x], b[this.thread.x]);
11+
}, {
12+
dimensions : [6],
13+
mode : mode
14+
});
15+
16+
assert.ok( f !== null, "function generated test");
17+
18+
var a = [1, 2, 3, 5, 6, 7];
19+
var b = [4, 5, 6, 1, 2, 3];
20+
21+
var res = f(a,b);
22+
var exp = [5, 7, 9, 6, 8, 10];
23+
24+
for(var i = 0; i < exp.length; ++i) {
25+
QUnit.close(res[i], exp[i], 0.1, "Result arr idx: "+i);
26+
}
27+
}
28+
29+
QUnit.test( "basic_sum_AB (auto)", function( assert ) {
30+
basic_sum_AB_test(assert, null);
31+
});
32+
33+
QUnit.test( "basic_sum_AB (GPU)", function( assert ) {
34+
basic_sum_AB_test(assert, "gpu");
35+
});
36+
37+
QUnit.test( "basic_sum_AB (CPU)", function( assert ) {
38+
basic_sum_AB_test(assert, "cpu");
39+
});

0 commit comments

Comments
 (0)