4
4
*
5
5
* GPU Accelerated JavaScript
6
6
*
7
- * @version 2.3.1
8
- * @date Fri Dec 20 2019 12:27:34 GMT-0500 (Eastern Standard Time)
7
+ * @version 2.4.0
8
+ * @date Sun Dec 22 2019 17:36:21 GMT-0500 (Eastern Standard Time)
9
9
*
10
10
* @license MIT
11
11
* The MIT License
@@ -5144,10 +5144,16 @@ class GLKernel extends Kernel {
5144
5144
gl . viewport ( 0 , 0 , this . maxTexSize [ 0 ] , this . maxTexSize [ 1 ] ) ;
5145
5145
this . canvas . width = this . maxTexSize [ 0 ] ;
5146
5146
this . canvas . height = this . maxTexSize [ 1 ] ;
5147
- this . _setupOutputTexture ( ) ;
5148
- if ( this . subKernels && this . subKernels . length > 0 ) {
5149
- this . _setupSubOutputTextures ( ) ;
5147
+ if ( this . texture ) {
5148
+ this . texture . delete ( ) ;
5150
5149
}
5150
+ this . texture = null ;
5151
+ if ( this . mappedTextures ) {
5152
+ for ( let i = 0 ; i < this . mappedTextures . length ; i ++ ) {
5153
+ this . mappedTextures [ i ] . delete ( ) ;
5154
+ }
5155
+ }
5156
+ this . mappedTextures = null ;
5151
5157
} else {
5152
5158
this . output = newOutput ;
5153
5159
}
@@ -5195,6 +5201,39 @@ class GLKernel extends Kernel {
5195
5201
throw new Error ( `Unknown tactic "${ tactic } " use "speed", "balanced", "precision", or empty for auto` ) ;
5196
5202
}
5197
5203
}
5204
+
5205
+ updateTextureArgumentRefs ( arg ) {
5206
+ if ( this . texture . texture === arg . texture ) {
5207
+ const { prevInput } = this ;
5208
+ if ( prevInput ) {
5209
+ if ( prevInput . texture . refs === 1 ) {
5210
+ this . texture . delete ( ) ;
5211
+ this . texture = prevInput . clone ( ) ;
5212
+ }
5213
+ prevInput . delete ( ) ;
5214
+ }
5215
+ this . prevInput = arg . clone ( ) ;
5216
+ } else if ( this . mappedTextures && this . mappedTextures . length > 0 ) {
5217
+ const { mappedTextures, prevMappedInputs } = this ;
5218
+ for ( let i = 0 ; i < mappedTextures . length ; i ++ ) {
5219
+ const mappedTexture = mappedTextures [ i ] ;
5220
+ if ( mappedTexture . texture === arg . texture ) {
5221
+ const prevMappedInput = prevMappedInputs [ i ] ;
5222
+ if ( prevMappedInput ) {
5223
+ if ( prevMappedInput . texture . refs === 1 ) {
5224
+ if ( mappedTexture ) {
5225
+ mappedTexture . delete ( ) ;
5226
+ mappedTextures [ i ] = prevMappedInput . clone ( ) ;
5227
+ }
5228
+ }
5229
+ prevMappedInput . delete ( ) ;
5230
+ }
5231
+ prevMappedInputs [ i ] = arg . clone ( ) ;
5232
+ break ;
5233
+ }
5234
+ }
5235
+ }
5236
+ }
5198
5237
}
5199
5238
5200
5239
const renderStrategy = Object . freeze ( {
@@ -5534,7 +5573,7 @@ class GLTexture extends Texture {
5534
5573
}
5535
5574
5536
5575
function selectTexture ( gl , texture ) {
5537
- gl . activeTexture ( gl . TEXTURE0 ) ;
5576
+ gl . activeTexture ( gl . TEXTURE31 ) ;
5538
5577
gl . bindTexture ( gl . TEXTURE_2D , texture ) ;
5539
5578
gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_WRAP_S , gl . CLAMP_TO_EDGE ) ;
5540
5579
gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_WRAP_T , gl . CLAMP_TO_EDGE ) ;
@@ -6187,7 +6226,7 @@ class Kernel {
6187
6226
}
6188
6227
6189
6228
setImmutable ( flag ) {
6190
- this . immutable = flag ;
6229
+ utils . warnDeprecated ( 'method' , 'setImmutable' ) ;
6191
6230
return this ;
6192
6231
}
6193
6232
@@ -8400,8 +8439,8 @@ class WebGLKernelValueDynamicMemoryOptimizedNumberTexture extends WebGLKernelVal
8400
8439
}
8401
8440
8402
8441
updateValue ( inputTexture ) {
8403
- this . checkSize ( inputTexture . size [ 0 ] , inputTexture . size [ 1 ] ) ;
8404
8442
this . dimensions = inputTexture . dimensions ;
8443
+ this . checkSize ( inputTexture . size [ 0 ] , inputTexture . size [ 1 ] ) ;
8405
8444
this . textureSize = inputTexture . size ;
8406
8445
this . kernel . setUniform3iv ( this . dimensionsId , this . dimensions ) ;
8407
8446
this . kernel . setUniform2iv ( this . sizeId , this . textureSize ) ;
@@ -8906,12 +8945,12 @@ class WebGLKernelValueMemoryOptimizedNumberTexture extends WebGLKernelValue {
8906
8945
if ( this . checkContext && inputTexture . context !== this . context ) {
8907
8946
throw new Error ( `Value ${ this . name } (${ this . type } ) must be from same context` ) ;
8908
8947
}
8909
- const { context : gl } = this ;
8910
- if ( inputTexture . texture === this . kernel . outputTexture ) {
8911
- inputTexture = inputTexture . clone ( ) ;
8912
- gl . useProgram ( this . kernel . program ) ;
8913
- this . kernel . textureGarbage . push ( inputTexture ) ;
8948
+
8949
+ const { context : gl , kernel } = this ;
8950
+ if ( kernel . pipeline ) {
8951
+ kernel . updateTextureArgumentRefs ( inputTexture ) ;
8914
8952
}
8953
+
8915
8954
gl . activeTexture ( this . contextHandle ) ;
8916
8955
gl . bindTexture ( gl . TEXTURE_2D , this . uploadValue = inputTexture . texture ) ;
8917
8956
this . kernel . setUniform1i ( this . id , this . index ) ;
@@ -8952,7 +8991,6 @@ class WebGLKernelValueNumberTexture extends WebGLKernelValue {
8952
8991
}
8953
8992
8954
8993
updateValue ( inputTexture ) {
8955
- const { kernel, context : gl } = this ;
8956
8994
if ( inputTexture . constructor !== this . initialValueConstructor ) {
8957
8995
this . onUpdateValueMismatch ( inputTexture . constructor ) ;
8958
8996
return ;
@@ -8961,40 +8999,9 @@ class WebGLKernelValueNumberTexture extends WebGLKernelValue {
8961
8999
throw new Error ( `Value ${ this . name } (${ this . type } ) must be from same context` ) ;
8962
9000
}
8963
9001
9002
+ const { kernel, context : gl } = this ;
8964
9003
if ( kernel . pipeline ) {
8965
- if ( kernel . texture . texture === inputTexture . texture ) {
8966
- const { prevInput } = kernel ;
8967
- if ( prevInput ) {
8968
- if ( prevInput . texture . refs === 1 ) {
8969
- if ( kernel . texture ) {
8970
- kernel . texture . delete ( ) ;
8971
- kernel . texture = prevInput . clone ( ) ;
8972
- }
8973
- }
8974
- prevInput . delete ( ) ;
8975
- }
8976
- kernel . prevInput = inputTexture . clone ( ) ;
8977
- } else if ( kernel . mappedTextures && kernel . mappedTextures . length > 0 ) {
8978
- const { mappedTextures, prevMappedInputs } = kernel ;
8979
- for ( let i = 0 ; i < mappedTextures . length ; i ++ ) {
8980
- const mappedTexture = mappedTextures [ i ] ;
8981
- if ( mappedTexture . texture === inputTexture . texture ) {
8982
- const prevMappedInput = prevMappedInputs [ i ] ;
8983
- if ( prevMappedInput ) {
8984
- if ( prevMappedInput . texture . refs === 1 ) {
8985
- if ( mappedTexture ) {
8986
- mappedTexture . delete ( ) ;
8987
- mappedTextures [ i ] = prevMappedInput . clone ( ) ;
8988
- }
8989
- }
8990
- prevMappedInput . delete ( ) ;
8991
- }
8992
- debugger ;
8993
- prevMappedInputs [ i ] = inputTexture . clone ( ) ;
8994
- break ;
8995
- }
8996
- }
8997
- }
9004
+ kernel . updateTextureArgumentRefs ( inputTexture ) ;
8998
9005
}
8999
9006
9000
9007
gl . activeTexture ( this . contextHandle ) ;
@@ -9605,7 +9612,7 @@ class WebGLKernel extends GLKernel {
9605
9612
this . framebuffer = null ;
9606
9613
this . buffer = null ;
9607
9614
this . texture = null ;
9608
- this . mappedTextures = [ ] ;
9615
+ this . mappedTextures = null ;
9609
9616
this . textureCache = [ ] ;
9610
9617
this . programUniformLocationCache = { } ;
9611
9618
this . uniform1fCache = { } ;
@@ -9878,7 +9885,7 @@ class WebGLKernel extends GLKernel {
9878
9885
kernel : this ,
9879
9886
strictIntegers : this . strictIntegers ,
9880
9887
onRequestTexture : ( ) => {
9881
- this . createTexture ( ) ;
9888
+ return this . createTexture ( ) ;
9882
9889
} ,
9883
9890
onRequestIndex : ( ) => {
9884
9891
return textureIndexes ++ ;
@@ -10083,9 +10090,6 @@ class WebGLKernel extends GLKernel {
10083
10090
}
10084
10091
10085
10092
gl . drawArrays ( gl . TRIANGLE_STRIP , 0 , 4 ) ;
10086
- if ( gl . getError ( ) > 0 ) {
10087
- debugger ;
10088
- }
10089
10093
}
10090
10094
10091
10095
drawBuffers ( ) {
@@ -10140,7 +10144,7 @@ class WebGLKernel extends GLKernel {
10140
10144
10141
10145
_setupSubOutputTextures ( ) {
10142
10146
const { context : gl } = this ;
10143
- if ( this . mappedTextures . length > 0 ) {
10147
+ if ( this . mappedTextures && this . mappedTextures . length > 0 ) {
10144
10148
for ( let i = 0 ; i < this . mappedTextures . length ; i ++ ) {
10145
10149
const mappedTexture = this . mappedTextures [ i ] ;
10146
10150
mappedTexture . beforeMutate ( ) ;
@@ -10800,7 +10804,7 @@ class WebGLKernel extends GLKernel {
10800
10804
if ( textureCacheIndex > - 1 ) {
10801
10805
this . textureCache . splice ( textureCacheIndex , 1 ) ;
10802
10806
}
10803
- delete this . texture ;
10807
+ this . texture = null ;
10804
10808
}
10805
10809
if ( this . mappedTextures && this . mappedTextures . length ) {
10806
10810
for ( let i = 0 ; i < this . mappedTextures . length ; i ++ ) {
@@ -10811,7 +10815,7 @@ class WebGLKernel extends GLKernel {
10811
10815
this . textureCache . splice ( textureCacheIndex , 1 ) ;
10812
10816
}
10813
10817
}
10814
- delete this . mappedTextures ;
10818
+ this . mappedTextures = null ;
10815
10819
}
10816
10820
while ( this . textureCache . length > 0 ) {
10817
10821
const texture = this . textureCache . pop ( ) ;
@@ -12377,9 +12381,14 @@ class WebGL2Kernel extends WebGLKernel {
12377
12381
}
12378
12382
12379
12383
_setupOutputTexture ( ) {
12380
- const { texSize } = this ;
12381
- const gl = this . context ;
12384
+ const { context : gl } = this ;
12385
+ if ( this . texture ) {
12386
+ this . texture . beforeMutate ( ) ;
12387
+ gl . framebufferTexture2D ( gl . FRAMEBUFFER , gl . COLOR_ATTACHMENT0 , gl . TEXTURE_2D , this . texture . texture , 0 ) ;
12388
+ return ;
12389
+ }
12382
12390
const texture = this . outputTexture = gl . createTexture ( ) ;
12391
+ const { texSize } = this ;
12383
12392
gl . activeTexture ( gl . TEXTURE0 + this . constantTextureCount + this . argumentTextureCount ) ;
12384
12393
gl . bindTexture ( gl . TEXTURE_2D , texture ) ;
12385
12394
gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_WRAP_S , gl . REPEAT ) ;
@@ -12393,12 +12402,32 @@ class WebGL2Kernel extends WebGLKernel {
12393
12402
gl . texImage2D ( gl . TEXTURE_2D , 0 , format , texSize [ 0 ] , texSize [ 1 ] , 0 , format , gl . UNSIGNED_BYTE , null ) ;
12394
12403
}
12395
12404
gl . framebufferTexture2D ( gl . FRAMEBUFFER , gl . COLOR_ATTACHMENT0 , gl . TEXTURE_2D , texture , 0 ) ;
12405
+ this . texture = new this . TextureConstructor ( {
12406
+ texture,
12407
+ size : texSize ,
12408
+ dimensions : this . threadDim ,
12409
+ output : this . output ,
12410
+ context : this . context ,
12411
+ internalFormat : this . getInternalFormat ( ) ,
12412
+ textureFormat : this . getTextureFormat ( ) ,
12413
+ kernel : this ,
12414
+ } ) ;
12396
12415
}
12397
12416
12398
12417
_setupSubOutputTextures ( ) {
12418
+ const { context : gl } = this ;
12419
+ if ( this . mappedTextures && this . mappedTextures . length > 0 ) {
12420
+ for ( let i = 0 ; i < this . mappedTextures . length ; i ++ ) {
12421
+ const mappedTexture = this . mappedTextures [ i ] ;
12422
+ mappedTexture . beforeMutate ( ) ;
12423
+ gl . framebufferTexture2D ( gl . FRAMEBUFFER , gl . COLOR_ATTACHMENT0 + i + 1 , gl . TEXTURE_2D , mappedTexture . texture , 0 ) ;
12424
+ }
12425
+ return ;
12426
+ }
12399
12427
const { texSize } = this ;
12400
- const gl = this . context ;
12401
12428
this . drawBuffersMap = [ gl . COLOR_ATTACHMENT0 ] ;
12429
+ this . mappedTextures = [ ] ;
12430
+ this . prevMappedInputs = { } ;
12402
12431
for ( let i = 0 ; i < this . subKernels . length ; i ++ ) {
12403
12432
const texture = this . createTexture ( ) ;
12404
12433
this . drawBuffersMap . push ( gl . COLOR_ATTACHMENT0 + i + 1 ) ;
@@ -12408,12 +12437,24 @@ class WebGL2Kernel extends WebGLKernel {
12408
12437
gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_WRAP_T , gl . CLAMP_TO_EDGE ) ;
12409
12438
gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_MIN_FILTER , gl . NEAREST ) ;
12410
12439
gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_MAG_FILTER , gl . NEAREST ) ;
12440
+ const format = this . getInternalFormat ( ) ;
12411
12441
if ( this . precision === 'single' ) {
12412
- gl . texImage2D ( gl . TEXTURE_2D , 0 , gl . RGBA32F , texSize [ 0 ] , texSize [ 1 ] , 0 , gl . RGBA , gl . FLOAT , null ) ;
12442
+ gl . texStorage2D ( gl . TEXTURE_2D , 1 , format , texSize [ 0 ] , texSize [ 1 ] ) ;
12413
12443
} else {
12414
12444
gl . texImage2D ( gl . TEXTURE_2D , 0 , gl . RGBA , texSize [ 0 ] , texSize [ 1 ] , 0 , gl . RGBA , gl . UNSIGNED_BYTE , null ) ;
12415
12445
}
12416
12446
gl . framebufferTexture2D ( gl . FRAMEBUFFER , gl . COLOR_ATTACHMENT0 + i + 1 , gl . TEXTURE_2D , texture , 0 ) ;
12447
+
12448
+ this . mappedTextures . push ( new this . TextureConstructor ( {
12449
+ texture,
12450
+ size : texSize ,
12451
+ dimensions : this . threadDim ,
12452
+ output : this . output ,
12453
+ context : this . context ,
12454
+ internalFormat : this . getInternalFormat ( ) ,
12455
+ textureFormat : this . getTextureFormat ( ) ,
12456
+ kernel : this ,
12457
+ } ) ) ;
12417
12458
}
12418
12459
}
12419
12460
0 commit comments