@@ -68,30 +68,33 @@ import {
68
68
* @param {string } nameOfClass (eg, webgl, webgl2, OES_texture_float)
69
69
*/
70
70
export function augmentAPI ( ctx , nameOfClass , options = { } ) {
71
+ // Only augment this object once
72
+ if ( ctx . __webgl_memory_augmented ) {
73
+ return ctx ;
74
+ }
75
+ ctx . __webgl_memory_augmented = true ;
76
+
71
77
const origGLErrorFn = options . origGLErrorFn || ctx . getError ;
72
78
73
79
function createSharedState ( ctx ) {
74
80
const drawingBufferInfo = getDrawingbufferInfo ( ctx ) ;
75
81
const sharedState = {
76
82
baseContext : ctx ,
77
83
config : options ,
78
- apis : {
79
- // custom extension
84
+ customExtensions : {
80
85
gman_webgl_memory : {
81
- ctx : {
82
- getMemoryInfo ( ) {
83
- const drawingbuffer = computeDrawingbufferSize ( ctx , drawingBufferInfo ) ;
84
- return {
85
- memory : {
86
- ...memory ,
87
- drawingbuffer,
88
- total : drawingbuffer + memory . buffer + memory . texture + memory . renderbuffer ,
89
- } ,
90
- resources : {
91
- ...resources ,
92
- }
93
- } ;
94
- } ,
86
+ getMemoryInfo ( ) {
87
+ const drawingbuffer = computeDrawingbufferSize ( ctx , drawingBufferInfo ) ;
88
+ return {
89
+ memory : {
90
+ ...memory ,
91
+ drawingbuffer,
92
+ total : drawingbuffer + memory . buffer + memory . texture + memory . renderbuffer ,
93
+ } ,
94
+ resources : {
95
+ ...resources ,
96
+ } ,
97
+ } ;
95
98
} ,
96
99
} ,
97
100
} ,
@@ -105,26 +108,38 @@ export function augmentAPI(ctx, nameOfClass, options = {}) {
105
108
defaultVertexArray : { } ,
106
109
webglObjectToMemory : new Map ( ) ,
107
110
} ;
108
- sharedState . webglObjectToMemory . set ( sharedState . defaultVertexArray , { } ) ;
109
- sharedState . currentVertexArray = sharedState . defaultVertexArray ;
111
+
112
+ function resetSharedState ( ) {
113
+ sharedState . bindings . clear ( ) ;
114
+ sharedState . webglObjectToMemory . clear ( ) ;
115
+ sharedState . webglObjectToMemory . set ( sharedState . defaultVertexArray , { } ) ;
116
+ sharedState . currentVertexArray = sharedState . defaultVertexArray ;
117
+ [ sharedState . resources , sharedState . memory ] . forEach ( function ( obj ) {
118
+ for ( let prop in obj ) {
119
+ obj [ prop ] = 0 ;
120
+ }
121
+ } ) ;
122
+ }
123
+
124
+ if ( ctx . canvas ) {
125
+ ctx . canvas . addEventListener ( 'webglcontextlost' , resetSharedState ) ;
126
+ }
127
+
128
+ resetSharedState ( ) ;
110
129
return sharedState ;
111
130
}
112
131
113
132
const sharedState = options . sharedState || createSharedState ( ctx ) ;
114
133
options . sharedState = sharedState ;
115
134
116
135
const {
117
- apis,
118
- baseContext,
119
136
bindings,
120
- config,
121
137
memory,
122
138
resources,
123
139
webglObjectToMemory,
140
+ customExtensions,
124
141
} = sharedState ;
125
142
126
- const origFuncs = { } ;
127
-
128
143
function noop ( ) {
129
144
}
130
145
@@ -230,14 +245,14 @@ export function augmentAPI(ctx, nameOfClass, options = {}) {
230
245
info . mips [ level ] = info . mips [ level ] || [ ] ;
231
246
const mipFaceInfo = info . mips [ level ] [ faceNdx ] || { }
232
247
info . size -= mipFaceInfo . size || 0 ;
233
-
248
+
234
249
mipFaceInfo . size = newMipSize ;
235
250
mipFaceInfo . internalFormat = internalFormat ;
236
251
mipFaceInfo . type = type ;
237
252
mipFaceInfo . width = width ;
238
253
mipFaceInfo . height = height ;
239
254
mipFaceInfo . depth = depth ;
240
-
255
+
241
256
info . mips [ level ] [ faceNdx ] = mipFaceInfo ;
242
257
info . size += newMipSize ;
243
258
@@ -546,15 +561,14 @@ export function augmentAPI(ctx, nameOfClass, options = {}) {
546
561
const origFn = ctx [ propertyName ] ;
547
562
ctx [ propertyName ] = function ( ...args ) {
548
563
const extensionName = args [ 0 ] . toLowerCase ( ) ;
549
- const api = apis [ extensionName ] ;
550
- if ( api ) {
551
- return api . ctx ;
552
- }
553
- const ext = origFn . call ( ctx , ...args ) ;
554
- if ( ext ) {
555
- augmentAPI ( ext , extensionName , { ...options , origGLErrorFn} ) ;
564
+ let ext = customExtensions [ extensionName ] ;
565
+ if ( ! ext ) {
566
+ ext = origFn . call ( ctx , ...args ) ;
567
+ if ( ext ) {
568
+ augmentAPI ( ext , extensionName , { ...options , origGLErrorFn } ) ;
569
+ }
556
570
}
557
- return ext ;
571
+ return ext || null ;
558
572
} ;
559
573
} ,
560
574
} ;
@@ -570,7 +584,6 @@ export function augmentAPI(ctx, nameOfClass, options = {}) {
570
584
ctx [ funcName ] = function ( ...args ) {
571
585
preCheck ( ctx , funcName , args ) ;
572
586
const result = origFn . call ( ctx , ...args ) ;
573
- const gl = baseContext ;
574
587
postCheck ( ctx , funcName , args , result ) ;
575
588
return result ;
576
589
} ;
@@ -583,11 +596,8 @@ export function augmentAPI(ctx, nameOfClass, options = {}) {
583
596
// Wrap each function
584
597
for ( const propertyName in ctx ) {
585
598
if ( typeof ctx [ propertyName ] === 'function' ) {
586
- origFuncs [ propertyName ] = ctx [ propertyName ] ;
587
599
makeErrorWrapper ( ctx , propertyName ) ;
588
600
}
589
601
}
590
-
591
- apis [ nameOfClass . toLowerCase ( ) ] = { ctx, origFuncs } ;
592
602
}
593
603
0 commit comments