5
5
* GPU Accelerated JavaScript
6
6
*
7
7
* @version 1.0.1
8
- * @date Tue Feb 27 2018 13:04:40 GMT-0500 (EST)
8
+ * @date Wed Feb 28 2018 15:01:51 GMT-0500 (EST)
9
9
*
10
10
* @license MIT
11
11
* The MIT License
@@ -2787,7 +2787,7 @@ function removeNoise(str) {
2787
2787
}
2788
2788
2789
2789
module . exports = function ( gpuKernel , name ) {
2790
- return '() => {\n ' + kernelRunShortcut . toString ( ) + ';\n const utils = {\n allPropertiesOf: ' + removeNoise ( utils . allPropertiesOf . toString ( ) ) + ',\n clone: ' + removeNoise ( utils . clone . toString ( ) ) + ',\n splitArray: ' + removeNoise ( utils . splitArray . toString ( ) ) + ',\n getArgumentType: ' + removeNoise ( utils . getArgumentType . toString ( ) ) + ',\n getDimensions: ' + removeNoise ( utils . getDimensions . toString ( ) ) + ',\n dimToTexSize: ' + removeNoise ( utils . dimToTexSize . toString ( ) ) + ',\n flattenTo: ' + removeNoise ( utils . flattenTo . toString ( ) ) + ',\n flatten2dArrayTo: ' + removeNoise ( utils . flatten2dArrayTo . toString ( ) ) + ',\n flatten3dArrayTo: ' + removeNoise ( utils . flatten3dArrayTo . toString ( ) ) + ',\n systemEndianness: \'' + removeNoise ( utils . systemEndianness ( ) ) + '\',\n initWebGl: ' + removeNoise ( utils . initWebGl . toString ( ) ) + ',\n isArray: ' + removeNoise ( utils . isArray . toString ( ) ) + '\n };\n const Utils = utils;\n const canvases = [];\n const maxTexSizes = {};\n class ' + ( name || 'Kernel' ) + ' {\n constructor() {\n this.maxTexSize = null;\n this.argumentsLength = 0;\n this._canvas = null;\n this._webGl = null;\n this.built = false;\n this.program = null;\n this.paramNames = ' + JSON . stringify ( gpuKernel . paramNames ) + ';\n this.paramTypes = ' + JSON . stringify ( gpuKernel . paramTypes ) + ';\n this.texSize = ' + JSON . stringify ( gpuKernel . texSize ) + ';\n this.output = ' + JSON . stringify ( gpuKernel . output ) + ';\n this.compiledFragShaderString = `' + gpuKernel . compiledFragShaderString + '`;\n\t\t this.compiledVertShaderString = `' + gpuKernel . compiledVertShaderString + '`;\n\t\t this.programUniformLocationCache = {};\n\t\t this.textureCache = {};\n\t\t this.subKernelOutputTextures = null;\n }\n ' + removeFnNoise ( gpuKernel . _getFragShaderString . toString ( ) ) + '\n ' + removeFnNoise ( gpuKernel . _getVertShaderString . toString ( ) ) + '\n validateOptions() {}\n setupParams() {}\n setCanvas(canvas) { this._canvas = canvas; return this; }\n setWebGl(webGl) { this._webGl = webGl; return this; }\n ' + removeFnNoise ( gpuKernel . getUniformLocation . toString ( ) ) + '\n ' + removeFnNoise ( gpuKernel . setupParams . toString ( ) ) + '\n ' + removeFnNoise ( gpuKernel . build . toString ( ) ) + '\n\t\t ' + removeFnNoise ( gpuKernel . run . toString ( ) ) + '\n\t\t ' + removeFnNoise ( gpuKernel . _addArgument . toString ( ) ) + '\n\t\t ' + removeFnNoise ( gpuKernel . getArgumentTexture . toString ( ) ) + '\n\t\t ' + removeFnNoise ( gpuKernel . getTextureCache . toString ( ) ) + '\n\t\t ' + removeFnNoise ( gpuKernel . getOutputTexture . toString ( ) ) + '\n\t\t ' + removeFnNoise ( gpuKernel . renderOutput . toString ( ) ) + '\n\t\t ' + removeFnNoise ( gpuKernel . updateMaxTexSize . toString ( ) ) + '\n\t\t ' + removeFnNoise ( gpuKernel . setupOutputTexture . toString ( ) ) + '\n\t\t ' + removeFnNoise ( gpuKernel . detachOutputTexture . toString ( ) ) + '\n\t\t ' + removeFnNoise ( gpuKernel . detachTextureCache . toString ( ) ) + '\n };\n return kernelRunShortcut(new Kernel());\n };' ;
2790
+ return '() => {\n ' + kernelRunShortcut . toString ( ) + ';\n const utils = {\n allPropertiesOf: ' + removeNoise ( utils . allPropertiesOf . toString ( ) ) + ',\n clone: ' + removeNoise ( utils . clone . toString ( ) ) + ',\n splitArray: ' + removeNoise ( utils . splitArray . toString ( ) ) + ',\n getArgumentType: ' + removeNoise ( utils . getArgumentType . toString ( ) ) + ',\n getDimensions: ' + removeNoise ( utils . getDimensions . toString ( ) ) + ',\n dimToTexSize: ' + removeNoise ( utils . dimToTexSize . toString ( ) ) + ',\n flattenTo: ' + removeNoise ( utils . flattenTo . toString ( ) ) + ',\n flatten2dArrayTo: ' + removeNoise ( utils . flatten2dArrayTo . toString ( ) ) + ',\n flatten3dArrayTo: ' + removeNoise ( utils . flatten3dArrayTo . toString ( ) ) + ',\n systemEndianness: \'' + removeNoise ( utils . systemEndianness ( ) ) + '\',\n initWebGl: ' + removeNoise ( utils . initWebGl . toString ( ) ) + ',\n isArray: ' + removeNoise ( utils . isArray . toString ( ) ) + '\n };\n const Utils = utils;\n const canvases = [];\n const maxTexSizes = {};\n class ' + ( name || 'Kernel' ) + ' {\n constructor() {\n this.maxTexSize = null;\n this.argumentsLength = 0;\n this._canvas = null;\n this._webGl = null;\n this.built = false;\n this.program = null;\n this.paramNames = ' + JSON . stringify ( gpuKernel . paramNames ) + ';\n this.paramTypes = ' + JSON . stringify ( gpuKernel . paramTypes ) + ';\n this.texSize = ' + JSON . stringify ( gpuKernel . texSize ) + ';\n this.output = ' + JSON . stringify ( gpuKernel . output ) + ';\n this.compiledFragShaderString = `' + gpuKernel . compiledFragShaderString + '`;\n\t\t this.compiledVertShaderString = `' + gpuKernel . compiledVertShaderString + '`;\n\t\t this.programUniformLocationCache = {};\n\t\t this.textureCache = {};\n\t\t this.subKernelOutputTextures = null;\n\t\t this.subKernelOutputVariableNames = null;\n }\n ' + removeFnNoise ( gpuKernel . _getFragShaderString . toString ( ) ) + '\n ' + removeFnNoise ( gpuKernel . _getVertShaderString . toString ( ) ) + '\n validateOptions() {}\n setupParams() {}\n setCanvas(canvas) { this._canvas = canvas; return this; }\n setWebGl(webGl) { this._webGl = webGl; return this; }\n ' + removeFnNoise ( gpuKernel . getUniformLocation . toString ( ) ) + '\n ' + removeFnNoise ( gpuKernel . setupParams . toString ( ) ) + '\n ' + removeFnNoise ( gpuKernel . build . toString ( ) ) + '\n\t\t ' + removeFnNoise ( gpuKernel . run . toString ( ) ) + '\n\t\t ' + removeFnNoise ( gpuKernel . _addArgument . toString ( ) ) + '\n\t\t ' + removeFnNoise ( gpuKernel . getArgumentTexture . toString ( ) ) + '\n\t\t ' + removeFnNoise ( gpuKernel . getTextureCache . toString ( ) ) + '\n\t\t ' + removeFnNoise ( gpuKernel . getOutputTexture . toString ( ) ) + '\n\t\t ' + removeFnNoise ( gpuKernel . renderOutput . toString ( ) ) + '\n\t\t ' + removeFnNoise ( gpuKernel . updateMaxTexSize . toString ( ) ) + '\n\t\t ' + removeFnNoise ( gpuKernel . setupOutputTexture . toString ( ) ) + '\n\t\t ' + removeFnNoise ( gpuKernel . detachOutputTexture . toString ( ) ) + '\n\t\t ' + removeFnNoise ( gpuKernel . detachTextureCache . toString ( ) ) + '\n };\n return kernelRunShortcut(new Kernel());\n };' ;
2791
2791
} ;
2792
2792
} , { "../../core/utils" :25 , "../kernel-run-shortcut" :9 } ] , 14 :[ function ( require , module , exports ) {
2793
2793
'use strict' ;
@@ -2992,25 +2992,12 @@ module.exports = function (_KernelBase) {
2992
2992
gl . enableVertexAttribArray ( aTexCoordLoc ) ;
2993
2993
gl . vertexAttribPointer ( aTexCoordLoc , 2 , gl . FLOAT , gl . FALSE , 0 , texCoordOffset ) ;
2994
2994
2995
- this . setupOutputTexture ( ) ;
2995
+ if ( ! this . outputImmutable ) {
2996
+ this . setupOutputTexture ( ) ;
2997
+ }
2996
2998
2997
- if ( this . subKernelOutputTextures !== null ) {
2998
- var extDrawBuffersMap = this . extDrawBuffersMap = [ gl . COLOR_ATTACHMENT0 ] ;
2999
- for ( var i = 0 ; i < this . subKernelOutputTextures . length ; i ++ ) {
3000
- var subKernelOutputTexture = this . subKernelOutputTextures [ i ] ;
3001
- extDrawBuffersMap . push ( gl . COLOR_ATTACHMENT0 + i + 1 ) ;
3002
- gl . activeTexture ( gl . TEXTURE0 + arguments . length + i ) ;
3003
- gl . bindTexture ( gl . TEXTURE_2D , subKernelOutputTexture ) ;
3004
- gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_WRAP_S , gl . CLAMP_TO_EDGE ) ;
3005
- gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_WRAP_T , gl . CLAMP_TO_EDGE ) ;
3006
- gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_MIN_FILTER , gl . NEAREST ) ;
3007
- gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_MAG_FILTER , gl . NEAREST ) ;
3008
- if ( this . floatOutput ) {
3009
- gl . texImage2D ( gl . TEXTURE_2D , 0 , gl . RGBA , texSize [ 0 ] , texSize [ 1 ] , 0 , gl . RGBA , gl . FLOAT , null ) ;
3010
- } else {
3011
- gl . texImage2D ( gl . TEXTURE_2D , 0 , gl . RGBA , texSize [ 0 ] , texSize [ 1 ] , 0 , gl . RGBA , gl . UNSIGNED_BYTE , null ) ;
3012
- }
3013
- }
2999
+ if ( this . subKernelOutputVariableNames !== null && this . subKernelOutputVariableNames . length > 0 && ! this . outputImmutable ) {
3000
+ this . setupSubOutputTextures ( this . subKernelOutputVariableNames . length ) ;
3014
3001
}
3015
3002
}
3016
3003
@@ -3039,12 +3026,11 @@ module.exports = function (_KernelBase) {
3039
3026
var ratioLoc = this . getUniformLocation ( 'ratio' ) ;
3040
3027
gl . uniform2f ( ratioLoc , texSize [ 0 ] / this . maxTexSize [ 0 ] , texSize [ 1 ] / this . maxTexSize [ 1 ] ) ;
3041
3028
3042
- this . argumentsLength = 0 ;
3043
- for ( var texIndex = 0 ; texIndex < paramNames . length ; texIndex ++ ) {
3044
- this . _addArgument ( arguments [ texIndex ] , paramTypes [ texIndex ] , paramNames [ texIndex ] ) ;
3045
- }
3046
-
3047
3029
if ( this . graphical ) {
3030
+ this . argumentsLength = 0 ;
3031
+ for ( var texIndex = 0 ; texIndex < paramNames . length ; texIndex ++ ) {
3032
+ this . _addArgument ( arguments [ texIndex ] , paramTypes [ texIndex ] , paramNames [ texIndex ] ) ;
3033
+ }
3048
3034
gl . bindRenderbuffer ( gl . RENDERBUFFER , null ) ;
3049
3035
gl . bindFramebuffer ( gl . FRAMEBUFFER , null ) ;
3050
3036
gl . drawArrays ( gl . TRIANGLE_STRIP , 0 , 4 ) ;
@@ -3058,14 +3044,23 @@ module.exports = function (_KernelBase) {
3058
3044
var outputTexture = this . outputTexture ;
3059
3045
gl . framebufferTexture2D ( gl . FRAMEBUFFER , gl . COLOR_ATTACHMENT0 , gl . TEXTURE_2D , outputTexture , 0 ) ;
3060
3046
3061
- if ( this . subKernelOutputTextures !== null ) {
3047
+ if ( this . subKernelOutputVariableNames !== null ) {
3048
+ if ( this . outputImmutable ) {
3049
+ this . subKernelOutputTextures = [ ] ;
3050
+ this . setupSubOutputTextures ( this . subKernelOutputVariableNames . length ) ;
3051
+ }
3062
3052
for ( var i = 0 ; i < this . subKernelOutputTextures . length ; i ++ ) {
3063
3053
var subKernelOutputTexture = this . subKernelOutputTextures [ i ] ;
3064
3054
gl . framebufferTexture2D ( gl . FRAMEBUFFER , gl . COLOR_ATTACHMENT0 + i + 1 , gl . TEXTURE_2D , subKernelOutputTexture , 0 ) ;
3065
3055
}
3066
3056
this . ext . drawBuffersWEBGL ( this . extDrawBuffersMap ) ;
3067
3057
}
3068
3058
3059
+ this . argumentsLength = 0 ;
3060
+ for ( var _texIndex = 0 ; _texIndex < paramNames . length ; _texIndex ++ ) {
3061
+ this . _addArgument ( arguments [ _texIndex ] , paramTypes [ _texIndex ] , paramNames [ _texIndex ] ) ;
3062
+ }
3063
+
3069
3064
gl . drawArrays ( gl . TRIANGLE_STRIP , 0 , 4 ) ;
3070
3065
3071
3066
if ( this . subKernelOutputTextures !== null ) {
@@ -3133,14 +3128,14 @@ module.exports = function (_KernelBase) {
3133
3128
} , {
3134
3129
key : 'getOutputTexture' ,
3135
3130
value : function getOutputTexture ( ) {
3136
- return this . getTextureCache ( 'OUTPUT' ) ;
3131
+ return this . outputTexture ;
3137
3132
}
3138
3133
3139
3134
3140
3135
} , {
3141
3136
key : 'detachOutputTexture' ,
3142
3137
value : function detachOutputTexture ( ) {
3143
- this . detachTextureCache ( 'OUTPUT' ) ;
3138
+ delete this . outputTexture ;
3144
3139
}
3145
3140
3146
3141
@@ -3150,7 +3145,7 @@ module.exports = function (_KernelBase) {
3150
3145
var gl = this . _webGl ;
3151
3146
var texSize = this . texSize ;
3152
3147
this . detachOutputTexture ( ) ;
3153
- this . outputTexture = this . getOutputTexture ( ) ;
3148
+ this . outputTexture = this . _webGl . createTexture ( ) ;
3154
3149
gl . activeTexture ( gl . TEXTURE0 + this . paramNames . length ) ;
3155
3150
gl . bindTexture ( gl . TEXTURE_2D , this . outputTexture ) ;
3156
3151
gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_WRAP_S , gl . CLAMP_TO_EDGE ) ;
@@ -3163,6 +3158,30 @@ module.exports = function (_KernelBase) {
3163
3158
gl . texImage2D ( gl . TEXTURE_2D , 0 , gl . RGBA , texSize [ 0 ] , texSize [ 1 ] , 0 , gl . RGBA , gl . UNSIGNED_BYTE , null ) ;
3164
3159
}
3165
3160
}
3161
+ } , {
3162
+ key : 'setupSubOutputTextures' ,
3163
+ value : function setupSubOutputTextures ( length ) {
3164
+ var gl = this . _webGl ;
3165
+ var texSize = this . texSize ;
3166
+ var extDrawBuffersMap = this . extDrawBuffersMap = [ gl . COLOR_ATTACHMENT0 ] ;
3167
+ var textures = this . subKernelOutputTextures = [ ] ;
3168
+ for ( var i = 0 ; i < length ; i ++ ) {
3169
+ var texture = this . _webGl . createTexture ( ) ;
3170
+ textures . push ( texture ) ;
3171
+ extDrawBuffersMap . push ( gl . COLOR_ATTACHMENT0 + i + 1 ) ;
3172
+ gl . activeTexture ( gl . TEXTURE0 + arguments . length + i ) ;
3173
+ gl . bindTexture ( gl . TEXTURE_2D , texture ) ;
3174
+ gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_WRAP_S , gl . CLAMP_TO_EDGE ) ;
3175
+ gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_WRAP_T , gl . CLAMP_TO_EDGE ) ;
3176
+ gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_MIN_FILTER , gl . NEAREST ) ;
3177
+ gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_MAG_FILTER , gl . NEAREST ) ;
3178
+ if ( this . floatOutput ) {
3179
+ gl . texImage2D ( gl . TEXTURE_2D , 0 , gl . RGBA , texSize [ 0 ] , texSize [ 1 ] , 0 , gl . RGBA , gl . FLOAT , null ) ;
3180
+ } else {
3181
+ gl . texImage2D ( gl . TEXTURE_2D , 0 , gl . RGBA , texSize [ 0 ] , texSize [ 1 ] , 0 , gl . RGBA , gl . UNSIGNED_BYTE , null ) ;
3182
+ }
3183
+ }
3184
+ }
3166
3185
3167
3186
3168
3187
} , {
@@ -3338,10 +3357,6 @@ module.exports = function (_KernelBase) {
3338
3357
3339
3358
var _size2 = inputTexture . size ;
3340
3359
3341
- if ( ! this . outputImmutable && inputTexture . texture === this . outputTexture ) {
3342
- this . setupOutputTexture ( ) ;
3343
- }
3344
-
3345
3360
gl . activeTexture ( gl . TEXTURE0 + this . argumentsLength ) ;
3346
3361
gl . bindTexture ( gl . TEXTURE_2D , inputTexture . texture ) ;
3347
3362
@@ -3598,6 +3613,8 @@ module.exports = function (_KernelBase) {
3598
3613
} , {
3599
3614
key : '_addKernels' ,
3600
3615
value : function _addKernels ( ) {
3616
+ var _this2 = this ;
3617
+
3601
3618
var builder = this . functionBuilder ;
3602
3619
var gl = this . _webGl ;
3603
3620
@@ -3618,42 +3635,31 @@ module.exports = function (_KernelBase) {
3618
3635
if ( this . subKernels !== null ) {
3619
3636
var ext = this . ext = gl . getExtension ( 'WEBGL_draw_buffers' ) ;
3620
3637
if ( ! ext ) throw new Error ( 'could not instantiate draw buffers extension' ) ;
3621
- this . subKernelOutputTextures = [ ] ;
3622
3638
this . subKernelOutputVariableNames = [ ] ;
3623
- for ( var i = 0 ; i < this . subKernels . length ; i ++ ) {
3624
- var subKernel = this . subKernels [ i ] ;
3625
- builder . addSubKernel ( subKernel , {
3626
- prototypeOnly : false ,
3627
- constants : this . constants ,
3628
- output : this . output ,
3629
- debug : this . debug ,
3630
- loopMaxIterations : this . loopMaxIterations
3631
- } ) ;
3632
- this . subKernelOutputTextures . push ( this . getSubKernelTexture ( i ) ) ;
3633
- this . subKernelOutputVariableNames . push ( subKernel . name + 'Result' ) ;
3634
- }
3639
+ this . subKernels . forEach ( function ( subKernel ) {
3640
+ return _this2 . _addSubKernel ( subKernel ) ;
3641
+ } ) ;
3635
3642
} else if ( this . subKernelProperties !== null ) {
3636
3643
var _ext = this . ext = gl . getExtension ( 'WEBGL_draw_buffers' ) ;
3637
3644
if ( ! _ext ) throw new Error ( 'could not instantiate draw buffers extension' ) ;
3638
- this . subKernelOutputTextures = [ ] ;
3639
3645
this . subKernelOutputVariableNames = [ ] ;
3640
- var _i4 = 0 ;
3641
- for ( var p in this . subKernelProperties ) {
3642
- if ( ! this . subKernelProperties . hasOwnProperty ( p ) ) continue ;
3643
- var _subKernel = this . subKernelProperties [ p ] ;
3644
- builder . addSubKernel ( _subKernel , {
3645
- prototypeOnly : false ,
3646
- constants : this . constants ,
3647
- output : this . output ,
3648
- debug : this . debug ,
3649
- loopMaxIterations : this . loopMaxIterations
3650
- } ) ;
3651
- this . subKernelOutputTextures . push ( this . getSubKernelTexture ( p ) ) ;
3652
- this . subKernelOutputVariableNames . push ( _subKernel . name + 'Result' ) ;
3653
- _i4 ++ ;
3654
- }
3646
+ Object . keys ( this . subKernelProperties ) . forEach ( function ( property ) {
3647
+ return _this2 . _addSubKernel ( _this2 . subKernelProperties [ property ] ) ;
3648
+ } ) ;
3655
3649
}
3656
3650
}
3651
+ } , {
3652
+ key : '_addSubKernel' ,
3653
+ value : function _addSubKernel ( subKernel ) {
3654
+ this . functionBuilder . addSubKernel ( subKernel , {
3655
+ prototypeOnly : false ,
3656
+ constants : this . constants ,
3657
+ output : this . output ,
3658
+ debug : this . debug ,
3659
+ loopMaxIterations : this . loopMaxIterations
3660
+ } ) ;
3661
+ this . subKernelOutputVariableNames . push ( subKernel . name + 'Result' ) ;
3662
+ }
3657
3663
3658
3664
3659
3665
} , {
0 commit comments