From cfd3c777d9d8481c5d12eba0d9b28c2a070236cb Mon Sep 17 00:00:00 2001 From: Robert Plummer Date: Thu, 7 Jun 2018 21:44:25 -0400 Subject: [PATCH] Move usage of `Number.isInteger` to utilities and cleanup some number checking Tests pass same as before --- src/backend/web-gl/kernel.js | 36 ++++++++++++++++------------------- src/backend/web-gl2/kernel.js | 9 +++++---- src/core/utils.js | 7 +++++-- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/backend/web-gl/kernel.js b/src/backend/web-gl/kernel.js index 371cf1d6..af6ae3fa 100644 --- a/src/backend/web-gl/kernel.js +++ b/src/backend/web-gl/kernel.js @@ -683,7 +683,8 @@ module.exports = class WebGLKernel extends KernelBase { this.setUniform1i(`user_${name}`, this.argumentsLength); break; } - case 'Number': + case 'Integer': + case 'Float': { this.setUniform1f(`user_${name}`, value); break; @@ -998,9 +999,9 @@ module.exports = class WebGLKernel extends KernelBase { `highp vec2 user_${ paramName }Size = vec2(${ paramSize[0] }.0, ${ paramSize[1] }.0)`, `highp vec3 user_${ paramName }Dim = vec3(${ paramDim[0] }.0, ${ paramDim[1]}.0, ${ paramDim[2] }.0)` ); - } else if (paramType === 'Number' && Number.isInteger(param)) { + } else if (paramType === 'Integer') { result.push(`highp float user_${ paramName } = ${ param }.0`); - } else if (paramType === 'Number') { + } else if (paramType === 'Float') { result.push(`highp float user_${ paramName } = ${ param }`); } } else { @@ -1010,7 +1011,7 @@ module.exports = class WebGLKernel extends KernelBase { `uniform highp vec2 user_${ paramName }Size`, `uniform highp vec3 user_${ paramName }Dim` ); - } else if (paramType === 'Number') { + } else if (paramType === 'Integer' || paramType === 'Float') { result.push(`uniform highp float user_${ paramName }`); } else { throw new Error(`Param type ${paramType} not supported in WebGL, only WebGL2`); @@ -1031,12 +1032,17 @@ module.exports = class WebGLKernel extends KernelBase { if (this.constants) { for (let name in this.constants) { if (!this.constants.hasOwnProperty(name)) continue; - let value = parseFloat(this.constants[name]); - - if (Number.isInteger(value)) { - result.push('const float constants_' + name + ' = ' + parseInt(value) + '.0'); - } else { - result.push('const float constants_' + name + ' = ' + parseFloat(value)); + let value = this.constants[name]; + let type = utils.getArgumentType(value); + switch (type) { + case 'Integer': + result.push('const float constants_' + name + ' = ' + parseInt(value) + '.0'); + break; + case 'Float': + result.push('const float constants_' + name + ' = ' + parseFloat(value)); + break; + default: + throw new Error(`Unsupported constant ${ name } type ${ type }`); } } } @@ -1063,16 +1069,6 @@ module.exports = class WebGLKernel extends KernelBase { `highp float ${ names[i] } = 0.0` ); } - - /* this is v2 prep - result.push('highp float kernelResult = 0.0'); - result.push('layout(location = 0) out highp float fradData0 = 0.0'); - for (let i = 0; i < names.length; i++) { - result.push( - `highp float ${ names[i] } = 0.0`, - `layout(location = ${ i + 1 }) out highp float fragData${ i + 1 } = 0.0` - ); - }*/ } else { result.push('highp float kernelResult = 0.0'); } diff --git a/src/backend/web-gl2/kernel.js b/src/backend/web-gl2/kernel.js index d81f3a24..49a62513 100644 --- a/src/backend/web-gl2/kernel.js +++ b/src/backend/web-gl2/kernel.js @@ -291,7 +291,8 @@ module.exports = class WebGL2Kernel extends WebGLKernel { this.setUniform1i(`user_${name}`, this.argumentsLength); break; } - case 'Number': + case 'Integer': + case 'Float': { this.setUniform1f(`user_${name}`, value); break; @@ -506,9 +507,9 @@ module.exports = class WebGL2Kernel extends WebGLKernel { `highp vec2 user_${ paramName }Size = vec2(${ paramSize[0] }.0, ${ paramSize[1] }.0)`, `highp vec3 user_${ paramName }Dim = vec3(${ paramDim[0] }.0, ${ paramDim[1]}.0, ${ paramDim[2] }.0)` ); - } else if (paramType === 'Number' && Number.isInteger(param)) { + } else if (paramType === 'Integer') { result.push(`highp float user_${ paramName } = ${ param }.0`); - } else if (paramType === 'Number') { + } else if (paramType === 'Float') { result.push(`highp float user_${ paramName } = ${ param }`); } } else { @@ -524,7 +525,7 @@ module.exports = class WebGL2Kernel extends WebGLKernel { `uniform highp vec2 user_${ paramName }Size`, `uniform highp vec3 user_${ paramName }Dim` ); - } else if (paramType === 'Number') { + } else if (paramType === 'Integer' || paramType === 'Float') { result.push(`uniform highp float user_${ paramName }`); } } diff --git a/src/core/utils.js b/src/core/utils.js index 3990826a..1682191c 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -271,7 +271,7 @@ class Utils extends UtilsCore { * * @param {Object} arg - The argument object to evaluate type * - * @returns {String} Argument type Array/Number/Texture/Unknown + * @returns {String} Argument type Array/Number/Float/Texture/Unknown * */ static getArgumentType(arg) { @@ -281,7 +281,10 @@ class Utils extends UtilsCore { } return 'Array'; } else if (typeof arg === 'number') { - return 'Number'; + if (Number.isInteger(arg)) { + return 'Integer'; + } + return 'Float'; } else if (arg instanceof Texture) { return 'Texture'; } else if (arg instanceof Input) {