Migrated gpu, to gpu core

This commit is contained in:
Eugene Cheah 2016-02-28 18:01:39 +08:00
parent a329ec95a4
commit c3ca03d4c2
11 changed files with 161 additions and 140 deletions

1
gulpfile.js vendored
View File

@ -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',

150
src/backend/gpu_core.js Normal file
View File

@ -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;
})();

142
src/gpu.js vendored
View File

@ -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;
})();

View File

@ -7,6 +7,7 @@
<!-- gpu.js scripts -->
<script src="../../../src/parser.js"></script>
<script src="../../../src/backend/gpu_core.js"></script>
<script src="../../../src/gpu.js"></script>
<script src="../../../src/backend/gpu_utils.js"></script>
<script src="../../../src/backend/functionNode_webgl.js"></script>

View File

@ -7,6 +7,7 @@
<!-- gpu.js scripts -->
<script src="../../../src/parser.js"></script>
<script src="../../../src/backend/gpu_core.js"></script>
<script src="../../../src/gpu.js"></script>
<script src="../../../src/backend/gpu_utils.js"></script>
<script src="../../../src/backend/functionNode_webgl.js"></script>

View File

@ -7,6 +7,7 @@
<!-- gpu.js scripts -->
<script src="../../../src/parser.js"></script>
<script src="../../../src/backend/gpu_core.js"></script>
<script src="../../../src/gpu.js"></script>
<script src="../../../src/backend/gpu_utils.js"></script>
<script src="../../../src/backend/functionNode_webgl.js"></script>

View File

@ -7,6 +7,7 @@
<!-- gpu.js scripts -->
<script src="../../../src/parser.js"></script>
<script src="../../../src/backend/gpu_core.js"></script>
<script src="../../../src/gpu.js"></script>
<script src="../../../src/backend/gpu_utils.js"></script>
<script src="../../../src/backend/functionNode_webgl.js"></script>

View File

@ -7,6 +7,7 @@
<!-- gpu.js scripts -->
<script src="../../../src/parser.js"></script>
<script src="../../../src/backend/gpu_core.js"></script>
<script src="../../../src/gpu.js"></script>
<script src="../../../src/backend/gpu_utils.js"></script>
<script src="../../../src/backend/functionNode_webgl.js"></script>

View File

@ -7,6 +7,7 @@
<!-- gpu.js scripts -->
<script src="../../../src/parser.js"></script>
<script src="../../../src/backend/gpu_core.js"></script>
<script src="../../../src/gpu.js"></script>
<script src="../../../src/backend/gpu_utils.js"></script>
<script src="../../../src/backend/functionNode_webgl.js"></script>

View File

@ -7,6 +7,7 @@
<!-- gpu.js scripts -->
<script src="../../../src/parser.js"></script>
<script src="../../../src/backend/gpu_core.js"></script>
<script src="../../../src/gpu.js"></script>
<script src="../../../src/backend/gpu_utils.js"></script>
<script src="../../../src/backend/functionNode_webgl.js"></script>

View File

@ -7,6 +7,7 @@
<!-- gpu.js scripts -->
<script src="../../src/parser.js"></script>
<script src="../../src/backend/gpu_core.js"></script>
<script src="../../src/gpu.js"></script>
<script src="../../src/backend/gpu_utils.js"></script>
<script src="../../src/backend/functionNode_webgl.js"></script>