@@ -163,7 +163,7 @@ class WebGLKernel extends GLKernel {
163163 this . threadDim = null ;
164164 this . framebuffer = null ;
165165 this . buffer = null ;
166- this . textureCache = { } ;
166+ this . textureCache = [ ] ;
167167 this . programUniformLocationCache = { } ;
168168 this . uniform1fCache = { } ;
169169 this . uniform1iCache = { } ;
@@ -428,7 +428,7 @@ class WebGLKernel extends GLKernel {
428428 kernel : this ,
429429 strictIntegers : this . strictIntegers ,
430430 onRequestTexture : ( ) => {
431- return this . context . createTexture ( ) ;
431+ return this . createTexture ( ) ;
432432 } ,
433433 onRequestIndex : ( ) => {
434434 return textureIndexes ++ ;
@@ -446,6 +446,12 @@ class WebGLKernel extends GLKernel {
446446 }
447447 }
448448
449+ createTexture ( ) {
450+ const texture = this . context . createTexture ( ) ;
451+ this . textureCache . push ( texture ) ;
452+ return texture ;
453+ }
454+
449455 setupConstants ( args ) {
450456 const { context : gl } = this ;
451457 this . kernelConstants = [ ] ;
@@ -479,7 +485,7 @@ class WebGLKernel extends GLKernel {
479485 kernel : this ,
480486 strictIntegers : this . strictIntegers ,
481487 onRequestTexture : ( ) => {
482- return this . context . createTexture ( ) ;
488+ return this . createTexture ( ) ;
483489 } ,
484490 onRequestIndex : ( ) => {
485491 return textureIndexes ++ ;
@@ -711,7 +717,7 @@ class WebGLKernel extends GLKernel {
711717 _setupOutputTexture ( ) {
712718 const gl = this . context ;
713719 const texSize = this . texSize ;
714- const texture = this . outputTexture = this . context . createTexture ( ) ;
720+ const texture = this . outputTexture = this . createTexture ( ) ;
715721 gl . activeTexture ( gl . TEXTURE0 + this . constantTextureCount + this . argumentTextureCount ) ;
716722 gl . bindTexture ( gl . TEXTURE_2D , texture ) ;
717723 gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_WRAP_S , gl . CLAMP_TO_EDGE ) ;
@@ -768,7 +774,7 @@ class WebGLKernel extends GLKernel {
768774 this . drawBuffersMap = [ gl . COLOR_ATTACHMENT0 ] ;
769775 this . subKernelOutputTextures = [ ] ;
770776 for ( let i = 0 ; i < this . subKernels . length ; i ++ ) {
771- const texture = this . context . createTexture ( ) ;
777+ const texture = this . createTexture ( ) ;
772778 this . subKernelOutputTextures . push ( texture ) ;
773779 this . drawBuffersMap . push ( gl . COLOR_ATTACHMENT0 + i + 1 ) ;
774780 gl . activeTexture ( gl . TEXTURE0 + this . constantTextureCount + this . argumentTextureCount + i ) ;
@@ -1465,9 +1471,6 @@ class WebGLKernel extends GLKernel {
14651471 }
14661472
14671473 destroy ( removeCanvasReferences ) {
1468- if ( this . outputTexture ) {
1469- this . context . deleteTexture ( this . outputTexture ) ;
1470- }
14711474 if ( this . buffer ) {
14721475 this . context . deleteBuffer ( this . buffer ) ;
14731476 }
@@ -1483,18 +1486,9 @@ class WebGLKernel extends GLKernel {
14831486 if ( this . program ) {
14841487 this . context . deleteProgram ( this . program ) ;
14851488 }
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 ) ;
14981492 }
14991493 if ( removeCanvasReferences ) {
15001494 const idx = canvases . indexOf ( this . canvas ) ;
@@ -1535,4 +1529,4 @@ class WebGLKernel extends GLKernel {
15351529
15361530module . exports = {
15371531 WebGLKernel
1538- } ;
1532+ } ;
0 commit comments