44 *
55 * GPU Accelerated JavaScript
66 *
7- * @version 2.0.2
8- * @date Wed Sep 25 2019 10:14:32 GMT-0400 (Eastern Daylight Time)
7+ * @version 2.0.3
8+ * @date Wed Oct 02 2019 21:31:03 GMT-0400 (Eastern Daylight Time)
99 *
1010 * @license MIT
1111 * The MIT License
@@ -292,7 +292,7 @@ function glExtensionWiretap(extension, options) {
292292}
293293
294294function argumentsToString ( args , options ) {
295- const { variables } = options ;
295+ const { variables, onUnrecognizedArgumentLookup } = options ;
296296 return ( Array . from ( args ) . map ( ( arg ) => {
297297 const variableName = getVariableName ( arg ) ;
298298 if ( variableName ) {
@@ -304,11 +304,15 @@ function argumentsToString(args, options) {
304304 function getVariableName ( value ) {
305305 if ( variables ) {
306306 for ( const name in variables ) {
307+ if ( ! variables . hasOwnProperty ( name ) ) continue ;
307308 if ( variables [ name ] === value ) {
308309 return name ;
309310 }
310311 }
311312 }
313+ if ( onUnrecognizedArgumentLookup ) {
314+ return onUnrecognizedArgumentLookup ( value ) ;
315+ }
312316 return null ;
313317 }
314318}
@@ -342,7 +346,7 @@ function argumentToString(arg, options) {
342346 case 'Number' : return getEntity ( arg ) ;
343347 case 'Boolean' : return getEntity ( arg ) ;
344348 case 'Array' :
345- return addVariable ( arg , `new ${ arg . constructor . name } (${ Array . from ( arg ) . join ( ',' ) } )` ) ;
349+ return addVariable ( arg , `new ${ arg . constructor . name } ([ ${ Array . from ( arg ) . join ( ',' ) } ] )` ) ;
346350 case 'Float32Array' :
347351 case 'Uint8Array' :
348352 case 'Uint16Array' :
@@ -863,7 +867,7 @@ class CPUFunctionNode extends FunctionNode {
863867 astAssignmentExpression ( assNode , retArr ) {
864868 const declaration = this . getDeclaration ( assNode . left ) ;
865869 if ( declaration && ! declaration . assignable ) {
866- throw new this . astErrorOutput ( `Variable ${ assNode . left . name } is not assignable here` , assNode ) ;
870+ throw this . astErrorOutput ( `Variable ${ assNode . left . name } is not assignable here` , assNode ) ;
867871 }
868872 this . astGeneric ( assNode . left , retArr ) ;
869873 retArr . push ( assNode . operator ) ;
@@ -3908,6 +3912,17 @@ function toStringWithoutUtils(fn) {
39083912}
39093913
39103914function glKernelString ( Kernel , args , originKernel , setupContextString , destroyContextString ) {
3915+ args = args ? Array . from ( args ) . map ( arg => {
3916+ switch ( typeof arg ) {
3917+ case 'boolean' :
3918+ return new Boolean ( arg ) ;
3919+ case 'number' :
3920+ return new Number ( arg ) ;
3921+ default :
3922+ return arg ;
3923+ }
3924+ } ) : null ;
3925+ const uploadedValues = [ ] ;
39113926 const postResult = [ ] ;
39123927 const context = glWiretap ( originKernel . context , {
39133928 useTrackablePrimitives : true ,
@@ -3932,19 +3947,15 @@ function glKernelString(Kernel, args, originKernel, setupContextString, destroyC
39323947 }
39333948 } ,
39343949 onUnrecognizedArgumentLookup : ( argument ) => {
3935- for ( let i = 0 ; i < kernel . kernelConstants . length ; i ++ ) {
3936- const value = kernel . kernelConstants [ i ] ;
3937- if ( value . type === 'HTMLImageArray' ) {
3938- const constant = kernel . constants [ value . name ] ;
3939- const variable = `uploadValue_${ value . name } [${ constant . indexOf ( value . uploadValue ) } ]` ;
3940- context . insertVariable ( variable , kernel . constants ) ;
3941- return variable ;
3942- } else if ( value . uploadValue === argument ) {
3943- const variable = `uploadValue_${ value . name } ` ;
3944- context . insertVariable ( variable , value ) ;
3945- return variable ;
3946- }
3950+ const argumentName = findKernelValue ( argument , kernel . kernelArguments , [ ] , context , uploadedValues ) ;
3951+ if ( argumentName ) {
3952+ return argumentName ;
3953+ }
3954+ const constantName = findKernelValue ( argument , kernel . kernelConstants , constants ? Object . keys ( constants ) . map ( key => constants [ key ] ) : [ ] , context , uploadedValues ) ;
3955+ if ( constantName ) {
3956+ return constantName ;
39473957 }
3958+ return null ;
39483959 }
39493960 } ) ;
39503961 let subKernelsResultVariableSetup = false ;
@@ -3993,14 +4004,7 @@ function glKernelString(Kernel, args, originKernel, setupContextString, destroyC
39934004 kernel . build . apply ( kernel , args ) ;
39944005 result . push ( context . toString ( ) ) ;
39954006 context . reset ( ) ;
3996- const upgradedArguments = Array . from ( args ) . map ( arg => {
3997- switch ( typeof arg ) {
3998- case 'number' :
3999- case 'boolean' :
4000- return new arg . constructor ( arg ) ;
4001- }
4002- return arg ;
4003- } ) ;
4007+
40044008 kernel . kernelArguments . forEach ( ( kernelArgument , i ) => {
40054009 switch ( kernelArgument . type ) {
40064010 case 'Integer' :
@@ -4013,7 +4017,7 @@ function glKernelString(Kernel, args, originKernel, setupContextString, destroyC
40134017 case 'Array(4)' :
40144018 case 'HTMLImage' :
40154019 case 'HTMLVideo' :
4016- context . insertVariable ( `uploadValue_${ kernelArgument . name } ` , upgradedArguments [ i ] ) ;
4020+ context . insertVariable ( `uploadValue_${ kernelArgument . name } ` , kernelArgument . uploadValue ) ;
40174021 break ;
40184022 case 'HTMLImageArray' :
40194023 for ( let imageIndex = 0 ; imageIndex < args [ i ] . length ; imageIndex ++ ) {
@@ -4039,7 +4043,7 @@ function glKernelString(Kernel, args, originKernel, setupContextString, destroyC
40394043 case 'ArrayTexture(2)' :
40404044 case 'ArrayTexture(3)' :
40414045 case 'ArrayTexture(4)' :
4042- context . insertVariable ( `uploadValue_${ kernelArgument . name } ` , upgradedArguments [ i ] . texture ) ;
4046+ context . insertVariable ( `uploadValue_${ kernelArgument . name } ` , args [ i ] . texture ) ;
40434047 break ;
40444048 default :
40454049 throw new Error ( `unhandled kernelArgumentType insertion for glWiretap of type ${ kernelArgument . type } ` ) ;
@@ -4049,6 +4053,7 @@ function glKernelString(Kernel, args, originKernel, setupContextString, destroyC
40494053 result . push ( `function ${ toStringWithoutUtils ( utils . flattenTo ) } ` ) ;
40504054 result . push ( `function ${ toStringWithoutUtils ( utils . flatten2dArrayTo ) } ` ) ;
40514055 result . push ( `function ${ toStringWithoutUtils ( utils . flatten3dArrayTo ) } ` ) ;
4056+ result . push ( `function ${ toStringWithoutUtils ( utils . flatten4dArrayTo ) } ` ) ;
40524057 result . push ( `function ${ toStringWithoutUtils ( utils . isArray ) } ` ) ;
40534058 if ( kernel . renderOutput !== kernel . renderTexture && kernel . formatValues ) {
40544059 result . push (
@@ -4058,17 +4063,17 @@ function glKernelString(Kernel, args, originKernel, setupContextString, destroyC
40584063 result . push ( '/** end of injected functions **/' ) ;
40594064 result . push ( ` const innerKernel = function (${ kernel . kernelArguments . map ( kernelArgument => kernelArgument . varName ) . join ( ', ' ) } ) {` ) ;
40604065 context . setIndent ( 4 ) ;
4061- kernel . run . apply ( kernel , upgradedArguments ) ;
4066+ kernel . run . apply ( kernel , args ) ;
40624067 if ( kernel . renderKernels ) {
40634068 kernel . renderKernels ( ) ;
40644069 } else if ( kernel . renderOutput ) {
40654070 kernel . renderOutput ( ) ;
40664071 }
4067- result . push ( '/** start setup uploads for kernel values **/' ) ;
4072+ result . push ( ' /** start setup uploads for kernel values **/' ) ;
40684073 kernel . kernelArguments . forEach ( kernelArgument => {
4069- result . push ( kernelArgument . getStringValueHandler ( ) ) ;
4074+ result . push ( ' ' + kernelArgument . getStringValueHandler ( ) . split ( '\n' ) . join ( '\n ' ) ) ;
40704075 } ) ;
4071- result . push ( '/** end setup uploads for kernel values **/' ) ;
4076+ result . push ( ' /** end setup uploads for kernel values **/' ) ;
40724077 result . push ( context . toString ( ) ) ;
40734078 if ( kernel . renderOutput === kernel . renderTexture ) {
40744079 context . reset ( ) ;
@@ -4100,7 +4105,7 @@ function glKernelString(Kernel, args, originKernel, setupContextString, destroyC
41004105 result . push ( ' };' ) ;
41014106 if ( kernel . graphical ) {
41024107 result . push ( getGetPixelsString ( kernel ) ) ;
4103- result . push ( `innerKernel.getPixels = getPixels;` ) ;
4108+ result . push ( ` innerKernel.getPixels = getPixels;` ) ;
41044109 }
41054110 result . push ( ' return innerKernel;' ) ;
41064111
@@ -4178,6 +4183,41 @@ function getToArrayString(kernelResult, textureName) {
41784183 return toArray();
41794184 }` ;
41804185}
4186+
4187+ function findKernelValue ( argument , kernelValues , values , context , uploadedValues ) {
4188+ if ( argument === null ) return null ;
4189+ switch ( typeof argument ) {
4190+ case 'boolean' :
4191+ case 'number' :
4192+ return null ;
4193+ }
4194+ if (
4195+ typeof HTMLImageElement !== 'undefined' &&
4196+ argument instanceof HTMLImageElement
4197+ ) {
4198+ for ( let i = 0 ; i < kernelValues . length ; i ++ ) {
4199+ const kernelValue = kernelValues [ i ] ;
4200+ if ( kernelValue . type !== 'HTMLImageArray' ) continue ;
4201+ if ( kernelValue . uploadValue !== argument ) continue ;
4202+ const variableIndex = values [ i ] . indexOf ( argument ) ;
4203+ if ( variableIndex === - 1 ) continue ;
4204+ const variableName = `uploadValue_${ kernelValue . name } [${ variableIndex } ]` ;
4205+ context . insertVariable ( variableName , argument ) ;
4206+ return variableName ;
4207+ }
4208+ return null ;
4209+ }
4210+
4211+ for ( let i = 0 ; i < kernelValues . length ; i ++ ) {
4212+ const kernelValue = kernelValues [ i ] ;
4213+ if ( argument !== kernelValue . uploadValue ) continue ;
4214+ const variable = `uploadValue_${ kernelValue . name } ` ;
4215+ context . insertVariable ( variable , kernelValue ) ;
4216+ return variable ;
4217+ }
4218+ return null ;
4219+ }
4220+
41814221module . exports = {
41824222 glKernelString
41834223} ;
@@ -5565,7 +5605,7 @@ class HeadlessGLKernel extends WebGLKernel {
55655605
55665606 toString ( ) {
55675607 const setupContextString = `const gl = context || require('gl')(1, 1);\n` ;
5568- const destroyContextString = `if (!context) { gl.getExtension('STACKGL_destroy_context').destroy(); }\n` ;
5608+ const destroyContextString = ` if (!context) { gl.getExtension('STACKGL_destroy_context').destroy(); }\n` ;
55695609 return glKernelString ( this . constructor , arguments , this , setupContextString , destroyContextString ) ;
55705610 }
55715611
@@ -5642,7 +5682,6 @@ class KernelValue {
56425682module . exports = {
56435683 KernelValue
56445684} ;
5645-
56465685} , { } ] , 34 :[ function ( require , module , exports ) {
56475686const { utils } = require ( '../utils' ) ;
56485687const { Input } = require ( '../input' ) ;
@@ -5711,7 +5750,7 @@ class Kernel {
57115750 this . constantTypes = null ;
57125751 this . constantBitRatios = null ;
57135752 this . dynamicArguments = false ;
5714- this . dynamicOutput = true ;
5753+ this . dynamicOutput = false ;
57155754
57165755 this . canvas = null ;
57175756
@@ -7158,7 +7197,7 @@ class WebGLFunctionNode extends FunctionNode {
71587197 astAssignmentExpression ( assNode , retArr ) {
71597198 const declaration = this . getDeclaration ( assNode . left ) ;
71607199 if ( declaration && ! declaration . assignable ) {
7161- throw new this . astErrorOutput ( `Variable ${ assNode . left . name } is not assignable here` , assNode ) ;
7200+ throw this . astErrorOutput ( `Variable ${ assNode . left . name } is not assignable here` , assNode ) ;
71627201 }
71637202 if ( assNode . operator === '%=' ) {
71647203 this . astGeneric ( assNode . left , retArr ) ;
@@ -9766,7 +9805,7 @@ class WebGLKernel extends GLKernel {
97669805 gl . scissor ( 0 , 0 , texSize [ 0 ] , texSize [ 1 ] ) ;
97679806
97689807 if ( this . dynamicOutput ) {
9769- this . setUniform3iv ( 'uOutputDim' , this . threadDim ) ;
9808+ this . setUniform3iv ( 'uOutputDim' , new Int32Array ( this . threadDim ) ) ;
97709809 this . setUniform2iv ( 'uTexSize' , texSize ) ;
97719810 }
97729811
0 commit comments