4
4
*
5
5
* GPU Accelerated JavaScript
6
6
*
7
- * @version 2.0.0-rc.3
8
- * @date Wed Mar 13 2019 12:42:17 GMT-0400 (Eastern Daylight Time)
7
+ * @version 2.0.0-rc.4
8
+ * @date Sun Mar 17 2019 22:11:56 GMT-0400 (Eastern Daylight Time)
9
9
*
10
10
* @license MIT
11
11
* The MIT License
@@ -1622,7 +1622,7 @@ class FunctionNode {
1622
1622
return type ;
1623
1623
}
1624
1624
}
1625
- return null ;
1625
+ throw new Error ( `Type for constant " ${ constantName } " not declared` ) ;
1626
1626
}
1627
1627
1628
1628
getUserArgumentName ( name ) {
@@ -2842,6 +2842,8 @@ class Kernel {
2842
2842
2843
2843
this . context = null ;
2844
2844
2845
+ this . gpu = null ;
2846
+
2845
2847
this . functions = null ;
2846
2848
2847
2849
this . nativeFunctions = null ;
@@ -3472,6 +3474,18 @@ class WebGLFunctionNode extends FunctionNode {
3472
3474
3473
3475
case 'Integer & Float' :
3474
3476
case 'Integer & Number' :
3477
+ if ( ast . operator === '>' || ast . operator === '<' && ast . right . type === 'Literal' ) {
3478
+ if ( ! Number . isInteger ( ast . right . value ) ) {
3479
+ this . pushState ( 'casting-to-float' ) ;
3480
+ retArr . push ( 'float(' ) ;
3481
+ this . astGeneric ( ast . left , retArr ) ;
3482
+ retArr . push ( ')' ) ;
3483
+ this . popState ( 'casting-to-float' ) ;
3484
+ retArr . push ( operatorMap [ ast . operator ] || ast . operator ) ;
3485
+ this . astGeneric ( ast . right , retArr ) ;
3486
+ break ;
3487
+ }
3488
+ }
3475
3489
this . astGeneric ( ast . left , retArr ) ;
3476
3490
retArr . push ( operatorMap [ ast . operator ] || ast . operator ) ;
3477
3491
this . pushState ( 'casting-to-integer' ) ;
@@ -3972,8 +3986,9 @@ class WebGLFunctionNode extends FunctionNode {
3972
3986
}
3973
3987
3974
3988
let funcName = this . astMemberExpressionUnroll ( ast . callee ) ;
3989
+ const isMathFunction = funcName . indexOf ( jsMathPrefix ) === 0 ;
3975
3990
3976
- if ( funcName . indexOf ( jsMathPrefix ) === 0 ) {
3991
+ if ( isMathFunction ) {
3977
3992
funcName = funcName . slice ( jsMathPrefix . length ) ;
3978
3993
}
3979
3994
@@ -3995,15 +4010,15 @@ class WebGLFunctionNode extends FunctionNode {
3995
4010
const functionArguments = [ ] ;
3996
4011
this . calledFunctionsArguments [ funcName ] . push ( functionArguments ) ;
3997
4012
3998
- if ( funcName === 'random' && this . plugins ) {
4013
+ if ( funcName === 'random' && this . plugins && this . plugins . length > 0 ) {
3999
4014
for ( let i = 0 ; i < this . plugins . length ; i ++ ) {
4000
4015
const plugin = this . plugins [ i ] ;
4001
4016
if ( plugin . functionMatch === 'Math.random()' && plugin . functionReplace ) {
4002
4017
functionArguments . push ( plugin . functionReturnType ) ;
4003
4018
retArr . push ( plugin . functionReplace ) ;
4019
+ return retArr ;
4004
4020
}
4005
4021
}
4006
- return retArr ;
4007
4022
}
4008
4023
4009
4024
retArr . push ( funcName ) ;
@@ -4069,6 +4084,29 @@ class WebGLFunctionNode extends FunctionNode {
4069
4084
}
4070
4085
throw new Error ( `Unhandled argument combination of ${ argumentType } and ${ targetType } ` ) ;
4071
4086
}
4087
+ } else if ( isMathFunction ) {
4088
+ for ( let i = 0 ; i < ast . arguments . length ; ++ i ) {
4089
+ const argument = ast . arguments [ i ] ;
4090
+ const argumentType = this . getType ( argument ) ;
4091
+
4092
+ if ( i > 0 ) {
4093
+ retArr . push ( ', ' ) ;
4094
+ }
4095
+
4096
+ switch ( argumentType ) {
4097
+ case 'Integer' :
4098
+ this . pushState ( 'casting-to-float' ) ;
4099
+ retArr . push ( 'float(' ) ;
4100
+ this . astGeneric ( argument , retArr ) ;
4101
+ retArr . push ( ')' ) ;
4102
+ this . popState ( 'casting-to-float' ) ;
4103
+ break ;
4104
+ case 'LiteralInteger' :
4105
+ default :
4106
+ this . astGeneric ( argument , retArr ) ;
4107
+ break ;
4108
+ }
4109
+ }
4072
4110
} else {
4073
4111
for ( let i = 0 ; i < ast . arguments . length ; ++ i ) {
4074
4112
const argument = ast . arguments [ i ] ;
@@ -4726,7 +4764,15 @@ class WebGLKernel extends GLKernel {
4726
4764
this . _setupOutputTexture ( ) ;
4727
4765
}
4728
4766
gl . drawArrays ( gl . TRIANGLE_STRIP , 0 , 4 ) ;
4729
- return new Texture ( this . outputTexture , texSize , this . threadDim , this . output , this . context , 'ArrayTexture(4)' ) ;
4767
+ return new Texture ( {
4768
+ texture : this . outputTexture ,
4769
+ size : texSize ,
4770
+ dimensions : this . threadDim ,
4771
+ output : this . output ,
4772
+ context : this . context ,
4773
+ gpu : this . gpu ,
4774
+ type : 'ArrayTexture(4)'
4775
+ } ) ;
4730
4776
}
4731
4777
gl . bindRenderbuffer ( gl . RENDERBUFFER , null ) ;
4732
4778
gl . bindFramebuffer ( gl . FRAMEBUFFER , null ) ;
@@ -4756,7 +4802,14 @@ class WebGLKernel extends GLKernel {
4756
4802
result : this . renderOutput ( outputTexture ) ,
4757
4803
} ;
4758
4804
for ( let i = 0 ; i < this . subKernels . length ; i ++ ) {
4759
- output [ this . subKernels [ i ] . property ] = new Texture ( this . subKernelOutputTextures [ i ] , texSize , this . threadDim , this . output , this . context ) ;
4805
+ output [ this . subKernels [ i ] . property ] = new Texture ( {
4806
+ texture : this . subKernelOutputTextures [ i ] ,
4807
+ size : texSize ,
4808
+ dimensions : this . threadDim ,
4809
+ output : this . output ,
4810
+ context : this . context ,
4811
+ gpu : this . gpu ,
4812
+ } ) ;
4760
4813
}
4761
4814
return output ;
4762
4815
}
@@ -4771,7 +4824,14 @@ class WebGLKernel extends GLKernel {
4771
4824
const threadDim = this . threadDim ;
4772
4825
const output = this . output ;
4773
4826
if ( this . pipeline ) {
4774
- return new Texture ( outputTexture , texSize , this . threadDim , output , this . context ) ;
4827
+ return new Texture ( {
4828
+ texture : outputTexture ,
4829
+ size : texSize ,
4830
+ dimensions : this . threadDim ,
4831
+ output,
4832
+ context : this . context ,
4833
+ gpu : this . gpu ,
4834
+ } ) ;
4775
4835
} else {
4776
4836
let result ;
4777
4837
if ( this . floatOutput ) {
@@ -6100,7 +6160,15 @@ class WebGL2Kernel extends WebGLKernel {
6100
6160
this . _setupOutputTexture ( ) ;
6101
6161
}
6102
6162
gl . drawArrays ( gl . TRIANGLE_STRIP , 0 , 4 ) ;
6103
- return new Texture ( this . outputTexture , texSize , this . threadDim , this . output , this . context , 'ArrayTexture(4)' ) ;
6163
+ return new Texture ( {
6164
+ texture : this . outputTexture ,
6165
+ size : texSize ,
6166
+ dimensions : this . threadDim ,
6167
+ output : this . output ,
6168
+ context : this . context ,
6169
+ gpu : this . gpu ,
6170
+ type : 'ArrayTexture(4)'
6171
+ } ) ;
6104
6172
}
6105
6173
gl . bindRenderbuffer ( gl . RENDERBUFFER , null ) ;
6106
6174
gl . bindFramebuffer ( gl . FRAMEBUFFER , null ) ;
@@ -6130,7 +6198,14 @@ class WebGL2Kernel extends WebGLKernel {
6130
6198
result : this . renderOutput ( outputTexture )
6131
6199
} ;
6132
6200
for ( let i = 0 ; i < this . subKernels . length ; i ++ ) {
6133
- output [ this . subKernels [ i ] . property ] = new Texture ( this . subKernelOutputTextures [ i ] , texSize , this . threadDim , this . output , this . context ) ;
6201
+ output [ this . subKernels [ i ] . property ] = new Texture ( {
6202
+ texture : this . subKernelOutputTextures [ i ] ,
6203
+ size : texSize ,
6204
+ dimensions : this . threadDim ,
6205
+ output : this . output ,
6206
+ context : this . context ,
6207
+ gpu : this . gpu ,
6208
+ } ) ;
6134
6209
}
6135
6210
return output ;
6136
6211
}
@@ -6993,7 +7068,8 @@ class GPU {
6993
7068
context : this . context ,
6994
7069
canvas : this . canvas ,
6995
7070
functions : this . functions ,
6996
- nativeFunctions : this . nativeFunctions
7071
+ nativeFunctions : this . nativeFunctions ,
7072
+ gpu : this ,
6997
7073
} , settings || { } ) ;
6998
7074
6999
7075
const kernel = kernelRunShortcut ( new this . Kernel ( source , mergedSettings ) ) ;
@@ -7350,20 +7426,32 @@ module.exports = {
7350
7426
} ;
7351
7427
} , { } ] , 27 :[ function ( require , module , exports ) {
7352
7428
class Texture {
7353
- constructor ( texture , size , dimensions , output , context , type = 'NumberTexture' ) {
7429
+ constructor ( settings ) {
7430
+ const {
7431
+ texture,
7432
+ size,
7433
+ dimensions,
7434
+ output,
7435
+ context,
7436
+ gpu,
7437
+ type = 'NumberTexture'
7438
+ } = settings ;
7439
+ if ( ! output ) throw new Error ( 'settings property "output" required.' ) ;
7440
+ if ( ! context ) throw new Error ( 'settings property "context" required.' ) ;
7354
7441
this . texture = texture ;
7355
7442
this . size = size ;
7356
7443
this . dimensions = dimensions ;
7357
7444
this . output = output ;
7358
7445
this . context = context ;
7446
+ this . gpu = gpu ;
7359
7447
this . kernel = null ;
7360
7448
this . type = type ;
7361
7449
}
7362
7450
7363
7451
toArray ( gpu ) {
7364
- if ( ! gpu ) throw new Error ( 'You need to pass the GPU object for toArray to work.' ) ;
7365
7452
if ( this . kernel ) return this . kernel ( this ) ;
7366
-
7453
+ gpu = gpu || this . gpu ;
7454
+ if ( ! gpu ) throw new Error ( 'settings property "gpu" or argument required.' ) ;
7367
7455
this . kernel = gpu . createKernel ( function ( x ) {
7368
7456
return x [ this . thread . z ] [ this . thread . y ] [ this . thread . x ] ;
7369
7457
} ) . setOutput ( this . output ) ;
@@ -7379,6 +7467,7 @@ class Texture {
7379
7467
module . exports = {
7380
7468
Texture
7381
7469
} ;
7470
+
7382
7471
} , { } ] , 28 :[ function ( require , module , exports ) {
7383
7472
const {
7384
7473
Input
0 commit comments