Skip to content

Commit 7fd9e62

Browse files
committed
GPU class restructuring of sorts
1 parent 854aee0 commit 7fd9e62

2 files changed

Lines changed: 108 additions & 100 deletions

File tree

src/backend/GPUCore.js

Lines changed: 5 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
///
22
/// Class: GPUCore
33
///
4+
/// Represents the "private/protected" namespace of the GPU class
5+
///
46
/// *GPUCore.js* internal functions namespace
57
/// *gpu.js* PUBLIC function namespace
68
///
@@ -27,104 +29,12 @@ var GPUCore = (function() {
2729
this.functionBuilder = new functionBuilder(this);
2830
this.functionBuilder.polyfillStandardFunctions();
2931
}
30-
31-
GPUCore.prototype.getWebgl = function() {
32+
33+
// Legacy method to get webgl : Preseved for backwards compatibility
34+
GPUCore.prototype.getGl = function() {
3235
return this.webgl;
3336
};
3437

35-
GPUCore.prototype.getCanvas = function(mode) {
36-
if (mode == "cpu") {
37-
return null;
38-
}
39-
40-
return this.canvas;
41-
};
42-
43-
///
44-
/// Function: createKernel
45-
///
46-
/// The core GPU.js function
47-
///
48-
/// The parameter object contains the following sub parameters
49-
///
50-
/// |---------------|---------------|---------------------------------------------------------------------------|
51-
/// | Name | Default value | Description |
52-
/// |---------------|---------------|---------------------------------------------------------------------------|
53-
/// | dimensions | [1024] | Thread dimension array |
54-
/// | mode | null | CPU / GPU configuration mode, "auto" / null. Has the following modes. |
55-
/// | | | + null / "auto" : Attempts to build GPU mode, else fallbacks |
56-
/// | | | + "gpu" : Attempts to build GPU mode, else fallbacks |
57-
/// | | | + "cpu" : Forces JS fallback mode only |
58-
/// |---------------|---------------|---------------------------------------------------------------------------|
59-
///
60-
/// Parameters:
61-
/// inputFunction {JS Function} The calling to perform the conversion
62-
/// paramObj {Object} The parameter configuration object
63-
///
64-
/// Returns:
65-
/// callable function to run
66-
///
67-
function createKernel(kernel, paramObj) {
68-
//
69-
// basic parameters safety checks
70-
//
71-
if( kernel === undefined ) {
72-
throw "Missing kernel parameter";
73-
}
74-
if( !(kernel instanceof Function) ) {
75-
throw "kernel parameter not a function";
76-
}
77-
if( paramObj === undefined ) {
78-
paramObj = {};
79-
}
80-
81-
//
82-
// Get theconfig, fallbacks to default value if not set
83-
//
84-
paramObj.dimensions = paramObj.dimensions || [];
85-
var mode = paramObj.mode && paramObj.mode.toLowerCase();
86-
87-
if ( mode == "cpu" ) {
88-
return this._mode_cpu(kernel, paramObj);
89-
}
90-
91-
//
92-
// Attempts to do the glsl conversion
93-
//
94-
try {
95-
return this._mode_gpu(kernel, paramObj);
96-
} catch (e) {
97-
if ( mode != "gpu") {
98-
console.warning("Falling back to CPU!");
99-
return this._mode_cpu(kernel, paramObj);
100-
} else {
101-
throw e;
102-
}
103-
}
104-
};
105-
GPUCore.prototype.createKernel = createKernel;
106-
107-
///
108-
/// Function: addFunction
109-
///
110-
/// Adds additional functions, that the kernel may call.
111-
///
112-
/// Parameters:
113-
/// jsFunction - {JS Function} JS Function to do conversion
114-
/// paramTypeArray - {[String,...]} Parameter type array, assumes all parameters are "float" if null
115-
/// returnType - {String} The return type, assumes "float" if null
116-
///
117-
/// Retuns:
118-
/// {GPU} returns itself
119-
///
120-
function addFunction( jsFunction, paramTypeArray, returnType ) {
121-
this.functionBuilder.addFunction( null, jsFunction, paramTypeArray, returnType );
122-
return this;
123-
}
124-
GPUCore.prototype.addFunction = addFunction;
125-
126-
127-
12838
GPUCore.prototype.textureToArray = function(texture) {
12939
var copy = this.createKernel(function(x) {
13040
return x[this.thread.z][this.thread.y][this.thread.x];

src/gpu.js

Lines changed: 103 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,116 @@
66
var GPU = (function() {
77
var GPU = GPUCore;
88

9-
GPU.prototype.getGl = function() {
10-
return this.gl;
9+
///
10+
/// Function: createKernel
11+
///
12+
/// The core GPU.js function
13+
///
14+
/// The parameter object contains the following sub parameters
15+
///
16+
/// |---------------|---------------|---------------------------------------------------------------------------|
17+
/// | Name | Default value | Description |
18+
/// |---------------|---------------|---------------------------------------------------------------------------|
19+
/// | dimensions | [1024] | Thread dimension array |
20+
/// | mode | null | CPU / GPU configuration mode, "auto" / null. Has the following modes. |
21+
/// | | | + null / "auto" : Attempts to build GPU mode, else fallbacks |
22+
/// | | | + "gpu" : Attempts to build GPU mode, else fallbacks |
23+
/// | | | + "cpu" : Forces JS fallback mode only |
24+
/// |---------------|---------------|---------------------------------------------------------------------------|
25+
///
26+
/// Parameters:
27+
/// inputFunction {JS Function} The calling to perform the conversion
28+
/// paramObj {Object} The parameter configuration object
29+
///
30+
/// Returns:
31+
/// callable function to run
32+
///
33+
function createKernel(kernel, paramObj) {
34+
//
35+
// basic parameters safety checks
36+
//
37+
if( kernel === undefined ) {
38+
throw "Missing kernel parameter";
39+
}
40+
if( !(kernel instanceof Function) ) {
41+
throw "kernel parameter not a function";
42+
}
43+
if( paramObj === undefined ) {
44+
paramObj = {};
45+
}
46+
47+
//
48+
// Get theconfig, fallbacks to default value if not set
49+
//
50+
paramObj.dimensions = paramObj.dimensions || [];
51+
var mode = paramObj.mode && paramObj.mode.toLowerCase();
52+
53+
if ( mode == "cpu" ) {
54+
return this._mode_cpu(kernel, paramObj);
55+
}
56+
57+
//
58+
// Attempts to do the glsl conversion
59+
//
60+
try {
61+
return this._mode_gpu(kernel, paramObj);
62+
} catch (e) {
63+
if ( mode != "gpu") {
64+
console.warning("Falling back to CPU!");
65+
return this._mode_cpu(kernel, paramObj);
66+
} else {
67+
throw e;
68+
}
69+
}
1170
};
71+
GPU.prototype.createKernel = createKernel;
1272

73+
///
74+
/// Function: addFunction
75+
///
76+
/// Adds additional functions, that the kernel may call.
77+
///
78+
/// Parameters:
79+
/// jsFunction - {JS Function} JS Function to do conversion
80+
/// paramTypeArray - {[String,...]} Parameter type array, assumes all parameters are "float" if null
81+
/// returnType - {String} The return type, assumes "float" if null
82+
///
83+
/// Retuns:
84+
/// {GPU} returns itself
85+
///
86+
function addFunction( jsFunction, paramTypeArray, returnType ) {
87+
this.functionBuilder.addFunction( null, jsFunction, paramTypeArray, returnType );
88+
return this;
89+
}
90+
GPU.prototype.addFunction = addFunction;
91+
92+
///
93+
/// Function: getWebgl
94+
///
95+
/// Returns the internal gpu webgl instance only if it has been initiated
96+
///
97+
/// Retuns:
98+
/// {WebGL object} that the instance use
99+
///
100+
GPU.prototype.getWebgl = function() {
101+
return this.webgl;
102+
};
103+
104+
///
105+
/// Function: getCanvas
106+
///
107+
/// Returns the internal canvas instance only if it has been initiated
108+
///
109+
/// Retuns:
110+
/// {Canvas object} that the instance use
111+
///
13112
GPU.prototype.getCanvas = function(mode) {
14113
if (mode == "cpu") {
15-
return this.canvasCpu;
114+
return null;
16115
}
17-
18116
return this.canvas;
19117
};
20-
118+
21119
///
22120
/// Function: support_webgl
23121
///

0 commit comments

Comments
 (0)