Skip to content

Commit d4271ed

Browse files
committed
Updated promise API
1 parent b3baa76 commit d4271ed

4 files changed

Lines changed: 98 additions & 116 deletions

File tree

src/backend/GPUCore.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,52 @@ var GPUCore = (function() {
4242

4343
return copy(texture);
4444
};
45+
46+
///
47+
/// Get and returns the Synchronous executor, of a class and kernel
48+
/// Which returns the result directly after passing the arguments.
49+
///
50+
function getSynchronousModeExecutor() {
51+
var kernel = this._kernelFunction;
52+
var paramObj = this._kernelParamObj;
53+
paramObj.dimensions = paramObj.dimensions || [];
54+
55+
var mode = this.computeMode;
56+
57+
//
58+
// CPU mode
59+
//
60+
if ( mode == "cpu" ) {
61+
return this._mode_cpu(kernel, paramObj);
62+
}
4563

64+
//
65+
// Attempts to do the glsl conversion
66+
//
67+
try {
68+
return this._mode_gpu(kernel, paramObj);
69+
} catch (e) {
70+
if ( mode != "gpu") {
71+
console.warning("Falling back to CPU!");
72+
this.computeMode = mode = "cpu";
73+
return this._mode_cpu(kernel, paramObj);
74+
} else {
75+
throw e;
76+
}
77+
}
78+
}
79+
GPUCore.prototype.getSynchronousModeExecutor = getSynchronousModeExecutor;
80+
81+
///
82+
/// Get and returns the ASYNCRONUS executor, of a class and kernel
83+
/// This returns a Promise object from an argument set.
84+
///
85+
/// Note that there is no current implmentation.
86+
///
87+
function getPromiseModeExecutor() {
88+
return null;
89+
}
90+
GPUCore.prototype.getPromiseModeExecutor = getPromiseModeExecutor;
91+
4692
return GPUCore;
4793
})();

src/gpu.js

Lines changed: 26 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -43,83 +43,39 @@ var GPU = (function() {
4343
if( paramObj === undefined ) {
4444
paramObj = {};
4545
}
46-
46+
47+
//
48+
// Replace the kernel function and param object
49+
//
50+
this._kernelFunction = kernel;
51+
this._kernelParamObj = paramObj || this._kernelParamObj || {};
52+
4753
//
4854
// Get the config, fallbacks to default value if not set
4955
//
50-
paramObj.dimensions = paramObj.dimensions || [];
5156
var mode = paramObj.mode && paramObj.mode.toLowerCase();
5257
this.computeMode = mode || "auto";
5358

54-
if ( mode == "cpu" ) {
55-
return this._mode_cpu(kernel, paramObj);
56-
}
57-
5859
//
59-
// Attempts to do the glsl conversion
60+
// Get the Synchronous executor
6061
//
61-
try {
62-
return this._mode_gpu(kernel, paramObj);
63-
} catch (e) {
64-
if ( mode != "gpu") {
65-
console.warning("Falling back to CPU!");
66-
this.computeMode = mode = "cpu";
67-
return this._mode_cpu(kernel, paramObj);
68-
} else {
69-
throw e;
70-
}
71-
}
62+
var ret = this.getSynchronousModeExecutor();
63+
// Allow class refence from function
64+
ret.gpujs = this;
65+
// Execute callback
66+
ret.exec = ret.execute = GPUUtils.functionBinder( this.execute, this );
67+
68+
// The Synchronous kernel
69+
this._kernelSynchronousExecutor = ret; //For exec to reference
70+
71+
return ret;
7272
};
7373
GPU.prototype.createKernel = createKernel;
7474

75-
///
76-
/// Function: setKernel
77-
///
78-
/// Set the kernel function and its configuration settings
79-
///
80-
/// This is the ASYNC alternative to createKernel, when used in conjuncture to executeKernel
81-
///
82-
/// |---------------|---------------|---------------------------------------------------------------------------|
83-
/// | Name | Default value | Description |
84-
/// |---------------|---------------|---------------------------------------------------------------------------|
85-
/// | dimensions | [1024] | Thread dimension array |
86-
/// | mode | null | CPU / GPU configuration mode, "auto" / null. Has the following modes. |
87-
/// | | | + null / "auto" : Attempts to build GPU mode, else fallbacks |
88-
/// | | | + "gpu" : Attempts to build GPU mode, else fallbacks |
89-
/// | | | + "cpu" : Forces JS fallback mode only |
90-
/// |---------------|---------------|---------------------------------------------------------------------------|
91-
///
92-
/// Parameters:
93-
/// inputFunction {JS Function} The calling input function to perform the conversion
94-
/// paramObj {Object} [Optional] The parameter configuration object (see above),
95-
/// uses previously set param object or blank object if not specified
96-
///
97-
/// Returns:
98-
/// {GPU} returns itself
99-
///
100-
function setKernel(kernel, paramObj) {
101-
//
102-
// basic parameters safety checks
103-
//
104-
if( kernel === undefined ) {
105-
throw "Missing kernel parameter";
106-
}
107-
if( !GPUUtils.isFunction(kernel) ) {
108-
throw "kernel parameter not a function";
109-
}
110-
111-
//
112-
// Replace the kernel function and param object
113-
//
114-
this._kernelFunction = kernel;
115-
this._kernelParamObj = paramObj || this._kernelParamObj || {};
116-
}
117-
GPU.prototype.setKernel = setKernel;
118-
11975
///
12076
/// Function: getKernelFunction
12177
///
122-
/// Get and returns the kernel function previously set by `setKernel`
78+
/// Get and returns the kernel function previously set by `createKernel`
12379
///
12480
/// Returns:
12581
/// {JS Function} The calling input function
@@ -132,7 +88,7 @@ var GPU = (function() {
13288
///
13389
/// Function: getKernelParamObj
13490
///
135-
/// Get and returns the kernel parameter object previously set by `setKernel`
91+
/// Get and returns the kernel parameter object previously set by `createKernel`
13692
///
13793
/// Returns:
13894
/// {JS Function} The calling input function
@@ -153,64 +109,19 @@ var GPU = (function() {
153109
/// Returns:
154110
/// {Promise} returns the promise object for the result / failure
155111
///
156-
function executeKernel() {
112+
function execute() {
157113
//
158-
// Get the arguments
159-
//
160-
var args = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments));
161-
162-
//
163114
// Prepare the required objects
164115
//
165-
var kernel = this._kernelFunction;
166-
var paramObj = this._kernelParamObj;
116+
var args = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments));
167117
var self = this;
168118

169119
//
170-
// Get the config, fallbacks to default value if not set
171-
//
172-
paramObj.dimensions = paramObj.dimensions || [];
173-
var mode = paramObj.mode && paramObj.mode.toLowerCase();
174-
self.computeMode = mode = mode || "auto";
175-
176-
//
177-
// Setup and return the promise, and execute the function
120+
// Setup and return the promise, and execute the function, in synchronous mode
178121
//
179122
return GPUUtils.newPromise(function(accept,reject) {
180123
try {
181-
//
182-
// Does computation in CPU mode
183-
//
184-
if ( mode == "cpu" ) {
185-
self.computeMode = "cpu";
186-
accept( self._mode_cpu(kernel, paramObj).apply(self,args) );
187-
return;
188-
}
189-
190-
//
191-
// Attempts to do computation in GPU mode
192-
//
193-
try {
194-
self.computeMode = "gpu";
195-
accept( self._mode_gpu(kernel, paramObj).apply(self,args) );
196-
return;
197-
} catch (e) {
198-
if ( mode != "gpu") {
199-
//
200-
// CPU fallback after GPU failure
201-
//
202-
console.warn("Falling back to CPU!");
203-
self.computeMode = "cpu";
204-
accept( self._mode_cpu(kernel, paramObj).apply(self,args) );
205-
return;
206-
} else {
207-
//
208-
// Error : throw rejection
209-
//
210-
reject(e);
211-
return;
212-
}
213-
}
124+
accept( self._kernelSynchronousExecutor.apply(self, args) );
214125
} catch (e) {
215126
//
216127
// Error : throw rejection
@@ -220,7 +131,8 @@ var GPU = (function() {
220131
}
221132
});
222133
}
223-
GPU.prototype.executeKernel = executeKernel;
134+
GPU.prototype.execute = execute;
135+
GPU.prototype.exec = execute;
224136

225137
///
226138
/// Function: addFunction

src/utils.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,30 @@ var GPUUtils = (function() {
186186
}
187187
GPUUtils.newPromise = newPromise;
188188

189+
///
190+
/// Function: functionBinder
191+
///
192+
/// Limited implmentation of Function.bind, with fallback
193+
///
194+
/// Parameters:
195+
/// inFunc - {JS Function} to setup bind on
196+
/// thisObj - {Object} The this parameter to assume inside the binded function
197+
///
198+
/// Returns:
199+
/// {JS Function} The binded function
200+
///
201+
function functionBinder( inFunc, thisObj ) {
202+
if( inFunc.bind ) {
203+
return inFunc.bind(thisObj);
204+
}
205+
206+
return function() {
207+
var args = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments));
208+
return inFunc.apply( thisObj, args );
209+
}
210+
}
211+
GPUUtils.functionBinder = functionBinder;
212+
189213
//-----------------------------------------------------------------------------
190214
//
191215
// Canvas validation and support

test/src/features/promiseApi.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ function promiseApi_function_return( assert, mode ) {
1515
var promiseObj = null;
1616

1717
// Setup kernel
18-
gpu.setKernel(kernel, paramObj);
18+
gpu.createKernel(kernel, paramObj);
1919

2020
// Get promise objet
21-
promiseObj = gpu.executeKernel();
21+
promiseObj = gpu.execute();
2222
assert.ok( promiseObj !== null, "Promise object generated test");
2323
promiseObj.then(function(res) {
2424
assert.equal( res, 42.0 );

0 commit comments

Comments
 (0)