diff --git a/gulpfile.js b/gulpfile.js index 11a1fffc..486efe29 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -10,6 +10,7 @@ gulp.task('build', function() { 'src/parser.js', 'src/texture.js', 'src/backend/gpu_utils.js', + 'src/backend/gpu_core.js', 'src/gpu.js', 'src/backend/functionNode_webgl.js', 'src/backend/functionNode.js', diff --git a/src/backend/gpu_core.js b/src/backend/gpu_core.js new file mode 100644 index 00000000..c70401f6 --- /dev/null +++ b/src/backend/gpu_core.js @@ -0,0 +1,150 @@ +/// +/// Class: gpu_core +/// +/// *gpu_core.js* internal functions namespace +/// *gpu.js* PUBLIC function namespace +/// +/// I know @private makes more sense, but since the documentation engine state is undetirmined. +/// See https://github.com/gpujs/gpu.js/issues/19 regarding documentation engine issue +/// +var gpu_core = (function() { + + function gpu_core(ctx) { + var gl, canvas, canvasCpu; + + canvas = undefined; + gl = ctx; + if (gl === undefined) { + canvasCpu = document.createElement('canvas'); + canvas = document.createElement('canvas'); + canvas.width = 2; + canvas.height = 2; + var glOpt = { + depth: false, + antialias: false + }; + + gl = canvas.getContext("experimental-webgl", glOpt) || canvas.getContext("webgl", glOpt); + } + + gl.getExtension('OES_texture_float'); + gl.getExtension('OES_texture_float_linear'); + gl.getExtension('OES_element_index_uint'); + + this.gl = gl; + this.canvas = canvas; + this.canvasCpu = canvasCpu; + this.programCache = {}; + this.endianness = gpu_utils.system_endianness(); + + this.functionBuilder = new functionBuilder(this); + this.functionBuilder.polyfillStandardFunctions(); + } + + gpu_core.prototype.getGl = function() { + return this.gl; + }; + + gpu_core.prototype.getCanvas = function(mode) { + if (mode == "cpu") { + return this.canvasCpu; + } + + return this.canvas; + }; + + /// + /// Function: createKernel + /// + /// The core GPU.js function + /// + /// The parameter object contains the following sub parameters + /// + /// |---------------|---------------|---------------------------------------------------------------------------| + /// | Name | Default value | Description | + /// |---------------|---------------|---------------------------------------------------------------------------| + /// | dimensions | [1024] | Thread dimension array | + /// | mode | null | CPU / GPU configuration mode, "auto" / null. Has the following modes. | + /// | | | + null / "auto" : Attempts to build GPU mode, else fallbacks | + /// | | | + "gpu" : Attempts to build GPU mode, else fallbacks | + /// | | | + "cpu" : Forces JS fallback mode only | + /// |---------------|---------------|---------------------------------------------------------------------------| + /// + /// Parameters: + /// inputFunction {JS Function} The calling to perform the conversion + /// paramObj {Object} The parameter configuration object + /// + /// Returns: + /// callable function to run + /// + function createKernel(kernel, paramObj) { + // + // basic parameters safety checks + // + if( kernel === undefined ) { + throw "Missing kernel parameter"; + } + if( !(kernel instanceof Function) ) { + throw "kernel parameter not a function"; + } + if( paramObj === undefined ) { + paramObj = {}; + } + + // + // Get theconfig, fallbacks to default value if not set + // + paramObj.dimensions = paramObj.dimensions || []; + var mode = paramObj.mode && paramObj.mode.toLowerCase(); + + if ( mode == "cpu" ) { + return this._mode_cpu(kernel, paramObj); + } + + // + // Attempts to do the glsl conversion + // + try { + return this._mode_gpu(kernel, paramObj); + } catch (e) { + if ( mode != "gpu") { + console.warning("Falling back to CPU!"); + return this._mode_cpu(kernel, paramObj); + } else { + throw e; + } + } + }; + gpu_core.prototype.createKernel = createKernel; + + /// + /// Function: addFunction + /// + /// Adds additional functions, that the kernel may call. + /// + /// Parameters: + /// jsFunction - {JS Function} JS Function to do conversion + /// paramTypeArray - {[String,...]} Parameter type array, assumes all parameters are "float" if null + /// returnType - {String} The return type, assumes "float" if null + /// + /// Retuns: + /// {GPU} returns itself + /// + function addFunction( jsFunction, paramTypeArray, returnType ) { + this.functionBuilder.addFunction( null, jsFunction, paramTypeArray, returnType ); + return this; + } + gpu_core.prototype.addFunction = addFunction; + + + + gpu_core.prototype.textureToArray = function(texture) { + var copy = this.createKernel(function(x) { + return x[this.thread.z][this.thread.y][this.thread.x]; + }); + + return copy(texture); + }; + + return gpu_core; +})(); diff --git a/src/gpu.js b/src/gpu.js index a9203257..4f8f6dd0 100644 --- a/src/gpu.js +++ b/src/gpu.js @@ -1,146 +1,8 @@ /// /// Class: GPU /// -/// GPU.JS core class =D +/// Initialises the GPU.js library class which manages the WebGL context for the created functions. /// var GPU = (function() { - - function GPU(ctx) { - var gl, canvas, canvasCpu; - - canvas = undefined; - gl = ctx; - if (gl === undefined) { - canvasCpu = document.createElement('canvas'); - canvas = document.createElement('canvas'); - canvas.width = 2; - canvas.height = 2; - var glOpt = { - depth: false, - antialias: false - }; - - gl = canvas.getContext("experimental-webgl", glOpt) || canvas.getContext("webgl", glOpt); - } - - gl.getExtension('OES_texture_float'); - gl.getExtension('OES_texture_float_linear'); - gl.getExtension('OES_element_index_uint'); - - this.gl = gl; - this.canvas = canvas; - this.canvasCpu = canvasCpu; - this.programCache = {}; - this.endianness = gpu_utils.system_endianness(); - - this.functionBuilder = new functionBuilder(this); - this.functionBuilder.polyfillStandardFunctions(); - } - - GPU.prototype.getGl = function() { - return this.gl; - }; - - GPU.prototype.getCanvas = function(mode) { - if (mode == "cpu") { - return this.canvasCpu; - } - - return this.canvas; - }; - - /// - /// Function: createKernel - /// - /// The core GPU.js function - /// - /// The parameter object contains the following sub parameters - /// - /// |---------------|---------------|---------------------------------------------------------------------------| - /// | Name | Default value | Description | - /// |---------------|---------------|---------------------------------------------------------------------------| - /// | dimensions | [1024] | Thread dimension array | - /// | mode | null | CPU / GPU configuration mode, "auto" / null. Has the following modes. | - /// | | | + null / "auto" : Attempts to build GPU mode, else fallbacks | - /// | | | + "gpu" : Attempts to build GPU mode, else fallbacks | - /// | | | + "cpu" : Forces JS fallback mode only | - /// |---------------|---------------|---------------------------------------------------------------------------| - /// - /// Parameters: - /// inputFunction {JS Function} The calling to perform the conversion - /// paramObj {Object} The parameter configuration object - /// - /// Returns: - /// callable function to run - /// - function createKernel(kernel, paramObj) { - // - // basic parameters safety checks - // - if( kernel === undefined ) { - throw "Missing kernel parameter"; - } - if( !(kernel instanceof Function) ) { - throw "kernel parameter not a function"; - } - if( paramObj === undefined ) { - paramObj = {}; - } - - // - // Get theconfig, fallbacks to default value if not set - // - paramObj.dimensions = paramObj.dimensions || []; - var mode = paramObj.mode && paramObj.mode.toLowerCase(); - - if ( mode == "cpu" ) { - return this._mode_cpu(kernel, paramObj); - } - - // - // Attempts to do the glsl conversion - // - try { - return this._mode_gpu(kernel, paramObj); - } catch (e) { - if ( mode != "gpu") { - console.warning("Falling back to CPU!"); - return this._mode_cpu(kernel, paramObj); - } else { - throw e; - } - } - }; - GPU.prototype.createKernel = createKernel; - - /// - /// Function: addFunction - /// - /// Adds additional functions, that the kernel may call. - /// - /// Parameters: - /// jsFunction - {JS Function} JS Function to do conversion - /// paramTypeArray - {[String,...]} Parameter type array, assumes all parameters are "float" if null - /// returnType - {String} The return type, assumes "float" if null - /// - /// Retuns: - /// {GPU} returns itself - /// - function addFunction( jsFunction, paramTypeArray, returnType ) { - this.functionBuilder.addFunction( null, jsFunction, paramTypeArray, returnType ); - return this; - } - GPU.prototype.addFunction = addFunction; - - - - GPU.prototype.textureToArray = function(texture) { - var copy = this.createKernel(function(x) { - return x[this.thread.z][this.thread.y][this.thread.x]; - }); - - return copy(texture); - }; - - return GPU; + return gpu_core; })(); diff --git a/test/html/features/addCustomFunction.html b/test/html/features/addCustomFunction.html index 9e3d3f17..40b88233 100644 --- a/test/html/features/addCustomFunction.html +++ b/test/html/features/addCustomFunction.html @@ -7,6 +7,7 @@ + diff --git a/test/html/features/for_loop.html b/test/html/features/for_loop.html index e6ac3f09..c36a3ee0 100644 --- a/test/html/features/for_loop.html +++ b/test/html/features/for_loop.html @@ -7,6 +7,7 @@ + diff --git a/test/html/features/function_return.html b/test/html/features/function_return.html index 1166ae73..f9c7bd1f 100644 --- a/test/html/features/function_return.html +++ b/test/html/features/function_return.html @@ -7,6 +7,7 @@ + diff --git a/test/html/features/if_else.html b/test/html/features/if_else.html index f0923a92..44c603c7 100644 --- a/test/html/features/if_else.html +++ b/test/html/features/if_else.html @@ -7,6 +7,7 @@ + diff --git a/test/html/features/mult_AB.html b/test/html/features/mult_AB.html index 35985e15..57212271 100644 --- a/test/html/features/mult_AB.html +++ b/test/html/features/mult_AB.html @@ -7,6 +7,7 @@ + diff --git a/test/html/features/nestedFunction.html b/test/html/features/nestedFunction.html index cda99d83..dfe10dab 100644 --- a/test/html/features/nestedFunction.html +++ b/test/html/features/nestedFunction.html @@ -7,6 +7,7 @@ + diff --git a/test/html/features/sum_AB.html b/test/html/features/sum_AB.html index 4c361aeb..7d58259b 100644 --- a/test/html/features/sum_AB.html +++ b/test/html/features/sum_AB.html @@ -7,6 +7,7 @@ + diff --git a/test/html/test-all.html b/test/html/test-all.html index 19e87856..4126dd84 100644 --- a/test/html/test-all.html +++ b/test/html/test-all.html @@ -7,6 +7,7 @@ +