4
4
*
5
5
* GPU Accelerated JavaScript
6
6
*
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)
9
9
*
10
10
* @license MIT
11
11
* The MIT License
@@ -292,7 +292,7 @@ function glExtensionWiretap(extension, options) {
292
292
}
293
293
294
294
function argumentsToString ( args , options ) {
295
- const { variables } = options ;
295
+ const { variables, onUnrecognizedArgumentLookup } = options ;
296
296
return ( Array . from ( args ) . map ( ( arg ) => {
297
297
const variableName = getVariableName ( arg ) ;
298
298
if ( variableName ) {
@@ -304,11 +304,15 @@ function argumentsToString(args, options) {
304
304
function getVariableName ( value ) {
305
305
if ( variables ) {
306
306
for ( const name in variables ) {
307
+ if ( ! variables . hasOwnProperty ( name ) ) continue ;
307
308
if ( variables [ name ] === value ) {
308
309
return name ;
309
310
}
310
311
}
311
312
}
313
+ if ( onUnrecognizedArgumentLookup ) {
314
+ return onUnrecognizedArgumentLookup ( value ) ;
315
+ }
312
316
return null ;
313
317
}
314
318
}
@@ -342,7 +346,7 @@ function argumentToString(arg, options) {
342
346
case 'Number' : return getEntity ( arg ) ;
343
347
case 'Boolean' : return getEntity ( arg ) ;
344
348
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 ( ',' ) } ] )` ) ;
346
350
case 'Float32Array' :
347
351
case 'Uint8Array' :
348
352
case 'Uint16Array' :
@@ -863,7 +867,7 @@ class CPUFunctionNode extends FunctionNode {
863
867
astAssignmentExpression ( assNode , retArr ) {
864
868
const declaration = this . getDeclaration ( assNode . left ) ;
865
869
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 ) ;
867
871
}
868
872
this . astGeneric ( assNode . left , retArr ) ;
869
873
retArr . push ( assNode . operator ) ;
@@ -3908,6 +3912,17 @@ function toStringWithoutUtils(fn) {
3908
3912
}
3909
3913
3910
3914
function 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 = [ ] ;
3911
3926
const postResult = [ ] ;
3912
3927
const context = glWiretap ( originKernel . context , {
3913
3928
useTrackablePrimitives : true ,
@@ -3932,19 +3947,15 @@ function glKernelString(Kernel, args, originKernel, setupContextString, destroyC
3932
3947
}
3933
3948
} ,
3934
3949
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 ;
3947
3957
}
3958
+ return null ;
3948
3959
}
3949
3960
} ) ;
3950
3961
let subKernelsResultVariableSetup = false ;
@@ -3993,14 +4004,7 @@ function glKernelString(Kernel, args, originKernel, setupContextString, destroyC
3993
4004
kernel . build . apply ( kernel , args ) ;
3994
4005
result . push ( context . toString ( ) ) ;
3995
4006
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
+
4004
4008
kernel . kernelArguments . forEach ( ( kernelArgument , i ) => {
4005
4009
switch ( kernelArgument . type ) {
4006
4010
case 'Integer' :
@@ -4013,7 +4017,7 @@ function glKernelString(Kernel, args, originKernel, setupContextString, destroyC
4013
4017
case 'Array(4)' :
4014
4018
case 'HTMLImage' :
4015
4019
case 'HTMLVideo' :
4016
- context . insertVariable ( `uploadValue_${ kernelArgument . name } ` , upgradedArguments [ i ] ) ;
4020
+ context . insertVariable ( `uploadValue_${ kernelArgument . name } ` , kernelArgument . uploadValue ) ;
4017
4021
break ;
4018
4022
case 'HTMLImageArray' :
4019
4023
for ( let imageIndex = 0 ; imageIndex < args [ i ] . length ; imageIndex ++ ) {
@@ -4039,7 +4043,7 @@ function glKernelString(Kernel, args, originKernel, setupContextString, destroyC
4039
4043
case 'ArrayTexture(2)' :
4040
4044
case 'ArrayTexture(3)' :
4041
4045
case 'ArrayTexture(4)' :
4042
- context . insertVariable ( `uploadValue_${ kernelArgument . name } ` , upgradedArguments [ i ] . texture ) ;
4046
+ context . insertVariable ( `uploadValue_${ kernelArgument . name } ` , args [ i ] . texture ) ;
4043
4047
break ;
4044
4048
default :
4045
4049
throw new Error ( `unhandled kernelArgumentType insertion for glWiretap of type ${ kernelArgument . type } ` ) ;
@@ -4049,6 +4053,7 @@ function glKernelString(Kernel, args, originKernel, setupContextString, destroyC
4049
4053
result . push ( `function ${ toStringWithoutUtils ( utils . flattenTo ) } ` ) ;
4050
4054
result . push ( `function ${ toStringWithoutUtils ( utils . flatten2dArrayTo ) } ` ) ;
4051
4055
result . push ( `function ${ toStringWithoutUtils ( utils . flatten3dArrayTo ) } ` ) ;
4056
+ result . push ( `function ${ toStringWithoutUtils ( utils . flatten4dArrayTo ) } ` ) ;
4052
4057
result . push ( `function ${ toStringWithoutUtils ( utils . isArray ) } ` ) ;
4053
4058
if ( kernel . renderOutput !== kernel . renderTexture && kernel . formatValues ) {
4054
4059
result . push (
@@ -4058,17 +4063,17 @@ function glKernelString(Kernel, args, originKernel, setupContextString, destroyC
4058
4063
result . push ( '/** end of injected functions **/' ) ;
4059
4064
result . push ( ` const innerKernel = function (${ kernel . kernelArguments . map ( kernelArgument => kernelArgument . varName ) . join ( ', ' ) } ) {` ) ;
4060
4065
context . setIndent ( 4 ) ;
4061
- kernel . run . apply ( kernel , upgradedArguments ) ;
4066
+ kernel . run . apply ( kernel , args ) ;
4062
4067
if ( kernel . renderKernels ) {
4063
4068
kernel . renderKernels ( ) ;
4064
4069
} else if ( kernel . renderOutput ) {
4065
4070
kernel . renderOutput ( ) ;
4066
4071
}
4067
- result . push ( '/** start setup uploads for kernel values **/' ) ;
4072
+ result . push ( ' /** start setup uploads for kernel values **/' ) ;
4068
4073
kernel . kernelArguments . forEach ( kernelArgument => {
4069
- result . push ( kernelArgument . getStringValueHandler ( ) ) ;
4074
+ result . push ( ' ' + kernelArgument . getStringValueHandler ( ) . split ( '\n' ) . join ( '\n ' ) ) ;
4070
4075
} ) ;
4071
- result . push ( '/** end setup uploads for kernel values **/' ) ;
4076
+ result . push ( ' /** end setup uploads for kernel values **/' ) ;
4072
4077
result . push ( context . toString ( ) ) ;
4073
4078
if ( kernel . renderOutput === kernel . renderTexture ) {
4074
4079
context . reset ( ) ;
@@ -4100,7 +4105,7 @@ function glKernelString(Kernel, args, originKernel, setupContextString, destroyC
4100
4105
result . push ( ' };' ) ;
4101
4106
if ( kernel . graphical ) {
4102
4107
result . push ( getGetPixelsString ( kernel ) ) ;
4103
- result . push ( `innerKernel.getPixels = getPixels;` ) ;
4108
+ result . push ( ` innerKernel.getPixels = getPixels;` ) ;
4104
4109
}
4105
4110
result . push ( ' return innerKernel;' ) ;
4106
4111
@@ -4178,6 +4183,41 @@ function getToArrayString(kernelResult, textureName) {
4178
4183
return toArray();
4179
4184
}` ;
4180
4185
}
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
+
4181
4221
module . exports = {
4182
4222
glKernelString
4183
4223
} ;
@@ -5565,7 +5605,7 @@ class HeadlessGLKernel extends WebGLKernel {
5565
5605
5566
5606
toString ( ) {
5567
5607
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` ;
5569
5609
return glKernelString ( this . constructor , arguments , this , setupContextString , destroyContextString ) ;
5570
5610
}
5571
5611
@@ -5642,7 +5682,6 @@ class KernelValue {
5642
5682
module . exports = {
5643
5683
KernelValue
5644
5684
} ;
5645
-
5646
5685
} , { } ] , 34 :[ function ( require , module , exports ) {
5647
5686
const { utils } = require ( '../utils' ) ;
5648
5687
const { Input } = require ( '../input' ) ;
@@ -5711,7 +5750,7 @@ class Kernel {
5711
5750
this . constantTypes = null ;
5712
5751
this . constantBitRatios = null ;
5713
5752
this . dynamicArguments = false ;
5714
- this . dynamicOutput = true ;
5753
+ this . dynamicOutput = false ;
5715
5754
5716
5755
this . canvas = null ;
5717
5756
@@ -7158,7 +7197,7 @@ class WebGLFunctionNode extends FunctionNode {
7158
7197
astAssignmentExpression ( assNode , retArr ) {
7159
7198
const declaration = this . getDeclaration ( assNode . left ) ;
7160
7199
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 ) ;
7162
7201
}
7163
7202
if ( assNode . operator === '%=' ) {
7164
7203
this . astGeneric ( assNode . left , retArr ) ;
@@ -9766,7 +9805,7 @@ class WebGLKernel extends GLKernel {
9766
9805
gl . scissor ( 0 , 0 , texSize [ 0 ] , texSize [ 1 ] ) ;
9767
9806
9768
9807
if ( this . dynamicOutput ) {
9769
- this . setUniform3iv ( 'uOutputDim' , this . threadDim ) ;
9808
+ this . setUniform3iv ( 'uOutputDim' , new Int32Array ( this . threadDim ) ) ;
9770
9809
this . setUniform2iv ( 'uTexSize' , texSize ) ;
9771
9810
}
9772
9811
0 commit comments