mirror of
https://github.com/gpujs/gpu.js.git
synced 2026-01-25 16:08:02 +00:00
To be referenced in same way. Simplify FunctionNode. Update documentation to be more straightforward. Remove code no longer needed from WebGL2 FunctionNode.
132 lines
4.7 KiB
JavaScript
132 lines
4.7 KiB
JavaScript
'use strict';
|
|
|
|
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
|
|
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
|
|
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
|
|
|
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
|
|
|
var WebGLFunctionNode = require('../web-gl/function-node');
|
|
var DECODE32_ENCODE32 = /decode32\(\s+encode32\(/g;
|
|
var ENCODE32_DECODE32 = /encode32\(\s+decode32\(/g;
|
|
|
|
/**
|
|
* @class WebGL2FunctionNode
|
|
*
|
|
* @desc [INTERNAL] Takes in a function node, and does all the AST voodoo required to generate its respective webGL code.
|
|
*
|
|
* @extends WebGLFunctionNode
|
|
*
|
|
* @returns the converted webGL function string
|
|
*
|
|
*/
|
|
module.exports = function (_WebGLFunctionNode) {
|
|
_inherits(WebGL2FunctionNode, _WebGLFunctionNode);
|
|
|
|
function WebGL2FunctionNode() {
|
|
_classCallCheck(this, WebGL2FunctionNode);
|
|
|
|
return _possibleConstructorReturn(this, (WebGL2FunctionNode.__proto__ || Object.getPrototypeOf(WebGL2FunctionNode)).apply(this, arguments));
|
|
}
|
|
|
|
_createClass(WebGL2FunctionNode, [{
|
|
key: 'generate',
|
|
value: function generate() {
|
|
if (this.debug) {
|
|
console.log(this);
|
|
}
|
|
if (this.prototypeOnly) {
|
|
return this.astFunctionPrototype(this.getJsAST(), []).join('').trim();
|
|
} else {
|
|
this.functionStringArray = this.astGeneric(this.getJsAST(), []);
|
|
}
|
|
this.functionString = webGlRegexOptimize(this.functionStringArray.join('').trim());
|
|
return this.functionString;
|
|
}
|
|
|
|
/**
|
|
* @memberOf WebGL2FunctionNode#
|
|
* @function
|
|
* @name astIdentifierExpression
|
|
*
|
|
* @desc Parses the abstract syntax tree for *identifier* expression
|
|
*
|
|
* @param {Object} idtNode - An ast Node
|
|
* @param {Array} retArr - return array string
|
|
*
|
|
* @returns {Array} the append retArr
|
|
*/
|
|
|
|
}, {
|
|
key: 'astIdentifierExpression',
|
|
value: function astIdentifierExpression(idtNode, retArr) {
|
|
if (idtNode.type !== 'Identifier') {
|
|
throw this.astErrorOutput('IdentifierExpression - not an Identifier', idtNode);
|
|
}
|
|
|
|
// do we need to cast addressing vales to float?
|
|
var castFloat = !this.isState('in-get-call-parameters');
|
|
|
|
switch (idtNode.name) {
|
|
case 'gpu_threadX':
|
|
castFloat && retArr.push('float(');
|
|
retArr.push('threadId.x');
|
|
castFloat && retArr.push(')');
|
|
break;
|
|
case 'gpu_threadY':
|
|
castFloat && retArr.push('float(');
|
|
retArr.push('threadId.y');
|
|
castFloat && retArr.push(')');
|
|
break;
|
|
case 'gpu_threadZ':
|
|
castFloat && retArr.push('float(');
|
|
retArr.push('threadId.z');
|
|
castFloat && retArr.push(')');
|
|
break;
|
|
case 'gpu_outputX':
|
|
retArr.push('uOutputDim.x');
|
|
break;
|
|
case 'gpu_outputY':
|
|
retArr.push('uOutputDim.y');
|
|
break;
|
|
case 'gpu_outputZ':
|
|
retArr.push('uOutputDim.z');
|
|
break;
|
|
case 'Infinity':
|
|
retArr.push('intBitsToFloat(2139095039)');
|
|
break;
|
|
default:
|
|
var userParamName = this.getUserParamName(idtNode.name);
|
|
if (userParamName !== null) {
|
|
this.pushParameter(retArr, 'user_' + userParamName);
|
|
} else {
|
|
this.pushParameter(retArr, 'user_' + idtNode.name);
|
|
}
|
|
}
|
|
|
|
return retArr;
|
|
}
|
|
}]);
|
|
|
|
return WebGL2FunctionNode;
|
|
}(WebGLFunctionNode);
|
|
|
|
/**
|
|
* @ignore
|
|
* @function
|
|
* @name webgl_regex_optimize
|
|
*
|
|
* @desc [INTERNAL] Takes the near final webgl function string, and do regex search and replacments.
|
|
* For voodoo optimize out the following:
|
|
*
|
|
* - decode32(encode32( <br>
|
|
* - encode32(decode32( <br>
|
|
*
|
|
* @param {String} inStr - The webGl function String
|
|
*
|
|
*/
|
|
function webGlRegexOptimize(inStr) {
|
|
return inStr.replace(DECODE32_ENCODE32, '((').replace(ENCODE32_DECODE32, '((');
|
|
} |