@@ -29,11 +29,12 @@ interface Dimensions {
29
29
}
30
30
31
31
// Shared cached frameBuffer object from external context
32
- let fbo : WebGLFramebuffer ;
32
+ const fboCache = new WeakMap < WebGL2RenderingContext , WebGLFramebuffer > ( ) ;
33
33
34
34
// 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 } > ( ) ;
37
38
38
39
interface ProgramObjects {
39
40
program : WebGLProgram ;
@@ -43,7 +44,9 @@ interface ProgramObjects {
43
44
}
44
45
45
46
// 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 ( ) ;
47
50
48
51
/**
49
52
* Download data from an texture.
@@ -58,9 +61,10 @@ export function downloadTextureData(
58
61
const { width, height, depth} = dims ;
59
62
const pixels = new Uint8Array ( width * height * depth ) ;
60
63
61
- if ( fbo == null ) {
62
- fbo = createFrameBuffer ( gl ) ;
64
+ if ( ! fboCache . has ( gl ) ) {
65
+ fboCache . set ( gl , createFrameBuffer ( gl ) ) ;
63
66
}
67
+ const fbo = fboCache . get ( gl ) ;
64
68
65
69
const debugMode = getDebugMode ( ) ;
66
70
@@ -197,9 +201,11 @@ export function runResizeProgram(
197
201
//
198
202
// Set up output texture.
199
203
//
200
- if ( resizeTexture == null ) {
201
- resizeTexture = gl . createTexture ( ) ;
204
+ if ( ! resizeTextureCache . has ( gl ) ) {
205
+ resizeTextureCache . set ( gl , gl . createTexture ( ) ) ;
202
206
}
207
+ const resizeTexture = resizeTextureCache . get ( gl ) ;
208
+
203
209
const targetTexture = resizeTexture ;
204
210
const targetTextureWidth = outputDims . width ;
205
211
const targetTextureHeight = outputDims . height ;
@@ -213,6 +219,11 @@ export function runResizeProgram(
213
219
gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_WRAP_T , gl . CLAMP_TO_EDGE ) ;
214
220
215
221
// 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
+
216
227
if ( resizeTextureDims == null ||
217
228
resizeTextureDims . width !== targetTextureWidth ||
218
229
resizeTextureDims . height !== targetTextureHeight ) {
@@ -228,18 +239,17 @@ export function runResizeProgram(
228
239
targetTextureHeight , border , format , type , null ) ;
229
240
} ) ;
230
241
231
- resizeTextureDims = {
232
- width : targetTextureWidth ,
233
- height : targetTextureHeight
234
- } ;
242
+ resizeTextureDimsCache . set (
243
+ gl , { width : targetTextureWidth , height : targetTextureHeight } ) ;
235
244
}
236
245
237
246
//
238
247
// Render to output texture
239
248
//
240
- if ( fbo == null ) {
241
- fbo = createFrameBuffer ( gl ) ;
249
+ if ( ! fboCache . has ( gl ) ) {
250
+ fboCache . set ( gl , createFrameBuffer ( gl ) ) ;
242
251
}
252
+ const fbo = fboCache . get ( gl ) ;
243
253
244
254
gl . viewport ( 0 , 0 , targetTextureWidth , targetTextureHeight ) ;
245
255
gl . bindFramebuffer ( gl . FRAMEBUFFER , fbo ) ;
@@ -248,7 +258,6 @@ export function runResizeProgram(
248
258
249
259
const fboComplete = gl . checkFramebufferStatus ( gl . FRAMEBUFFER ) ;
250
260
if ( fboComplete !== gl . FRAMEBUFFER_COMPLETE ) {
251
- console . log ( 'checkFramebufferStatus is not complete' , fboComplete ) ;
252
261
switch ( fboComplete ) {
253
262
case gl . FRAMEBUFFER_INCOMPLETE_ATTACHMENT :
254
263
throw new Error (
@@ -294,6 +303,11 @@ function createFrameBuffer(gl: WebGL2RenderingContext): WebGLFramebuffer {
294
303
295
304
function drawTextureProgram (
296
305
gl : WebGL2RenderingContext , flipHorizontal : boolean ) : ProgramObjects {
306
+ if ( ! programCacheByContext . has ( gl ) ) {
307
+ programCacheByContext . set ( gl , new Map ( ) ) ;
308
+ }
309
+ const programCache = programCacheByContext . get ( gl ) ;
310
+
297
311
const cacheKey = `drawTexture_${ flipHorizontal } ` ;
298
312
if ( ! programCache . has ( cacheKey ) ) {
299
313
const vertSource =
@@ -315,6 +329,11 @@ function resizeProgram(
315
329
gl : WebGL2RenderingContext , sourceDims : Dimensions , targetDims : Dimensions ,
316
330
alignCorners : boolean ,
317
331
interpolation : 'nearest_neighbor' | 'bilinear' ) : ProgramObjects {
332
+ if ( ! programCacheByContext . has ( gl ) ) {
333
+ programCacheByContext . set ( gl , new Map ( ) ) ;
334
+ }
335
+ const programCache = programCacheByContext . get ( gl ) ;
336
+
318
337
const cacheKey = `resize_${ sourceDims . width } _${ sourceDims . height } _${
319
338
sourceDims . depth } _${ targetDims . width } _${ targetDims . height } _${
320
339
targetDims . depth } _${ alignCorners } _${ interpolation } `;
0 commit comments