@@ -29,11 +29,12 @@ interface Dimensions {
2929}
3030
3131// Shared cached frameBuffer object from external context
32- let fbo : WebGLFramebuffer ;
32+ const fboCache = new WeakMap < WebGL2RenderingContext , WebGLFramebuffer > ( ) ;
3333
3434// Internal target texture used for resizing camera texture input
35- let resizeTexture : WebGLTexture ;
36- let resizeTextureDims : { width : number , height : number } ;
35+ const resizeTextureCache = new WeakMap < WebGL2RenderingContext , WebGLTexture > ( ) ;
36+ const resizeTextureDimsCache =
37+ new WeakMap < WebGL2RenderingContext , { width : number , height : number } > ( ) ;
3738
3839interface ProgramObjects {
3940 program : WebGLProgram ;
@@ -43,7 +44,9 @@ interface ProgramObjects {
4344}
4445
4546// Cache for shader programs and associated vertex array buffers.
46- const programCache : Map < string , ProgramObjects > = new Map ( ) ;
47+ const programCacheByContext :
48+ WeakMap < WebGL2RenderingContext , Map < string , ProgramObjects > > =
49+ new WeakMap ( ) ;
4750
4851/**
4952 * Download data from an texture.
@@ -58,9 +61,10 @@ export function downloadTextureData(
5861 const { width, height, depth} = dims ;
5962 const pixels = new Uint8Array ( width * height * depth ) ;
6063
61- if ( fbo == null ) {
62- fbo = createFrameBuffer ( gl ) ;
64+ if ( ! fboCache . has ( gl ) ) {
65+ fboCache . set ( gl , createFrameBuffer ( gl ) ) ;
6366 }
67+ const fbo = fboCache . get ( gl ) ;
6468
6569 const debugMode = getDebugMode ( ) ;
6670
@@ -197,9 +201,11 @@ export function runResizeProgram(
197201 //
198202 // Set up output texture.
199203 //
200- if ( resizeTexture == null ) {
201- resizeTexture = gl . createTexture ( ) ;
204+ if ( ! resizeTextureCache . has ( gl ) ) {
205+ resizeTextureCache . set ( gl , gl . createTexture ( ) ) ;
202206 }
207+ const resizeTexture = resizeTextureCache . get ( gl ) ;
208+
203209 const targetTexture = resizeTexture ;
204210 const targetTextureWidth = outputDims . width ;
205211 const targetTextureHeight = outputDims . height ;
@@ -213,6 +219,11 @@ export function runResizeProgram(
213219 gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_WRAP_T , gl . CLAMP_TO_EDGE ) ;
214220
215221 // Reallocate texture storage if target size has changed.
222+ if ( ! resizeTextureDimsCache . has ( gl ) ) {
223+ resizeTextureDimsCache . set ( gl , { width : - 1 , height : - 1 } ) ;
224+ }
225+ const resizeTextureDims = resizeTextureDimsCache . get ( gl ) ;
226+
216227 if ( resizeTextureDims == null ||
217228 resizeTextureDims . width !== targetTextureWidth ||
218229 resizeTextureDims . height !== targetTextureHeight ) {
@@ -228,18 +239,17 @@ export function runResizeProgram(
228239 targetTextureHeight , border , format , type , null ) ;
229240 } ) ;
230241
231- resizeTextureDims = {
232- width : targetTextureWidth ,
233- height : targetTextureHeight
234- } ;
242+ resizeTextureDimsCache . set (
243+ gl , { width : targetTextureWidth , height : targetTextureHeight } ) ;
235244 }
236245
237246 //
238247 // Render to output texture
239248 //
240- if ( fbo == null ) {
241- fbo = createFrameBuffer ( gl ) ;
249+ if ( ! fboCache . has ( gl ) ) {
250+ fboCache . set ( gl , createFrameBuffer ( gl ) ) ;
242251 }
252+ const fbo = fboCache . get ( gl ) ;
243253
244254 gl . viewport ( 0 , 0 , targetTextureWidth , targetTextureHeight ) ;
245255 gl . bindFramebuffer ( gl . FRAMEBUFFER , fbo ) ;
@@ -248,7 +258,6 @@ export function runResizeProgram(
248258
249259 const fboComplete = gl . checkFramebufferStatus ( gl . FRAMEBUFFER ) ;
250260 if ( fboComplete !== gl . FRAMEBUFFER_COMPLETE ) {
251- console . log ( 'checkFramebufferStatus is not complete' , fboComplete ) ;
252261 switch ( fboComplete ) {
253262 case gl . FRAMEBUFFER_INCOMPLETE_ATTACHMENT :
254263 throw new Error (
@@ -294,6 +303,11 @@ function createFrameBuffer(gl: WebGL2RenderingContext): WebGLFramebuffer {
294303
295304function drawTextureProgram (
296305 gl : WebGL2RenderingContext , flipHorizontal : boolean ) : ProgramObjects {
306+ if ( ! programCacheByContext . has ( gl ) ) {
307+ programCacheByContext . set ( gl , new Map ( ) ) ;
308+ }
309+ const programCache = programCacheByContext . get ( gl ) ;
310+
297311 const cacheKey = `drawTexture_${ flipHorizontal } ` ;
298312 if ( ! programCache . has ( cacheKey ) ) {
299313 const vertSource =
@@ -315,6 +329,11 @@ function resizeProgram(
315329 gl : WebGL2RenderingContext , sourceDims : Dimensions , targetDims : Dimensions ,
316330 alignCorners : boolean ,
317331 interpolation : 'nearest_neighbor' | 'bilinear' ) : ProgramObjects {
332+ if ( ! programCacheByContext . has ( gl ) ) {
333+ programCacheByContext . set ( gl , new Map ( ) ) ;
334+ }
335+ const programCache = programCacheByContext . get ( gl ) ;
336+
318337 const cacheKey = `resize_${ sourceDims . width } _${ sourceDims . height } _${
319338 sourceDims . depth } _${ targetDims . width } _${ targetDims . height } _${
320339 targetDims . depth } _${ alignCorners } _${ interpolation } `;
0 commit comments