@@ -163,7 +163,7 @@ class WebGLKernel extends GLKernel {
163
163
this . threadDim = null ;
164
164
this . framebuffer = null ;
165
165
this . buffer = null ;
166
- this . textureCache = { } ;
166
+ this . textureCache = [ ] ;
167
167
this . programUniformLocationCache = { } ;
168
168
this . uniform1fCache = { } ;
169
169
this . uniform1iCache = { } ;
@@ -428,7 +428,7 @@ class WebGLKernel extends GLKernel {
428
428
kernel : this ,
429
429
strictIntegers : this . strictIntegers ,
430
430
onRequestTexture : ( ) => {
431
- return this . context . createTexture ( ) ;
431
+ return this . createTexture ( ) ;
432
432
} ,
433
433
onRequestIndex : ( ) => {
434
434
return textureIndexes ++ ;
@@ -446,6 +446,12 @@ class WebGLKernel extends GLKernel {
446
446
}
447
447
}
448
448
449
+ createTexture ( ) {
450
+ const texture = this . context . createTexture ( ) ;
451
+ this . textureCache . push ( texture ) ;
452
+ return texture ;
453
+ }
454
+
449
455
setupConstants ( args ) {
450
456
const { context : gl } = this ;
451
457
this . kernelConstants = [ ] ;
@@ -479,7 +485,7 @@ class WebGLKernel extends GLKernel {
479
485
kernel : this ,
480
486
strictIntegers : this . strictIntegers ,
481
487
onRequestTexture : ( ) => {
482
- return this . context . createTexture ( ) ;
488
+ return this . createTexture ( ) ;
483
489
} ,
484
490
onRequestIndex : ( ) => {
485
491
return textureIndexes ++ ;
@@ -711,7 +717,7 @@ class WebGLKernel extends GLKernel {
711
717
_setupOutputTexture ( ) {
712
718
const gl = this . context ;
713
719
const texSize = this . texSize ;
714
- const texture = this . outputTexture = this . context . createTexture ( ) ;
720
+ const texture = this . outputTexture = this . createTexture ( ) ;
715
721
gl . activeTexture ( gl . TEXTURE0 + this . constantTextureCount + this . argumentTextureCount ) ;
716
722
gl . bindTexture ( gl . TEXTURE_2D , texture ) ;
717
723
gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_WRAP_S , gl . CLAMP_TO_EDGE ) ;
@@ -768,7 +774,7 @@ class WebGLKernel extends GLKernel {
768
774
this . drawBuffersMap = [ gl . COLOR_ATTACHMENT0 ] ;
769
775
this . subKernelOutputTextures = [ ] ;
770
776
for ( let i = 0 ; i < this . subKernels . length ; i ++ ) {
771
- const texture = this . context . createTexture ( ) ;
777
+ const texture = this . createTexture ( ) ;
772
778
this . subKernelOutputTextures . push ( texture ) ;
773
779
this . drawBuffersMap . push ( gl . COLOR_ATTACHMENT0 + i + 1 ) ;
774
780
gl . activeTexture ( gl . TEXTURE0 + this . constantTextureCount + this . argumentTextureCount + i ) ;
@@ -1465,9 +1471,6 @@ class WebGLKernel extends GLKernel {
1465
1471
}
1466
1472
1467
1473
destroy ( removeCanvasReferences ) {
1468
- if ( this . outputTexture ) {
1469
- this . context . deleteTexture ( this . outputTexture ) ;
1470
- }
1471
1474
if ( this . buffer ) {
1472
1475
this . context . deleteBuffer ( this . buffer ) ;
1473
1476
}
@@ -1483,18 +1486,9 @@ class WebGLKernel extends GLKernel {
1483
1486
if ( this . program ) {
1484
1487
this . context . deleteProgram ( this . program ) ;
1485
1488
}
1486
-
1487
- const keys = Object . keys ( this . textureCache ) ;
1488
-
1489
- for ( let i = 0 ; i < keys . length ; i ++ ) {
1490
- const name = keys [ i ] ;
1491
- this . context . deleteTexture ( this . textureCache [ name ] ) ;
1492
- }
1493
-
1494
- if ( this . subKernelOutputTextures ) {
1495
- for ( let i = 0 ; i < this . subKernelOutputTextures . length ; i ++ ) {
1496
- this . context . deleteTexture ( this . subKernelOutputTextures [ i ] ) ;
1497
- }
1489
+ while ( this . textureCache . length > 0 ) {
1490
+ const texture = this . textureCache . pop ( ) ;
1491
+ this . context . deleteTexture ( texture ) ;
1498
1492
}
1499
1493
if ( removeCanvasReferences ) {
1500
1494
const idx = canvases . indexOf ( this . canvas ) ;
@@ -1535,4 +1529,4 @@ class WebGLKernel extends GLKernel {
1535
1529
1536
1530
module . exports = {
1537
1531
WebGLKernel
1538
- } ;
1532
+ } ;
0 commit comments