简体   繁体   中英

Get WebGL texture/buffer/shader count

Is there a way to get the count of the currently existing textures, buffers or shaders of a WebGL context? Like the numbers you can get in Firefox if you look at about:memory .

在此处输入图片说明

I'd like to check if all these are deleted successfully when my application closes.

There is no way to get that info directly from the WebGLRenderingContext but you could easily augment the context yourself something like

var numTextures = 0;
var originalCreateTextureFn = gl.createTexture;
var originalDeleteTextureFn = gl.deleteTexture;
gl.createTexture = function() {
   ++numTextures;
   return originalCreateTextureFn.call(gl);
};
gl.deleteTexture = function(texture) {
   --numTextures;
   originalDeleteTextureFn.call(gl, texture);
};

You can write similar functions for other resources.

Of course if you want to be perfect you'd probably need to add a flag to each object just incase you try to delete something twice and also check the object passed in is actually the right kind. Something like

var numTextures = 0;
var originalCreateTextureFn = gl.createTexture;
var originalDeleteTextureFn = gl.deleteTexture;
gl.createTexture = function() {
   ++numTextures;
   var texture = originalCreateTextureFn.call(gl);
   texture.__deleted__ = false;
};
gl.deleteTexture = function(texture) {
   if (texture && texture instanceof WebGLTexture && !texture.__deleted__) {
     texture.__deleted__ = true;
     --numTextures;
   }
   originalDeleteTextureFn.call(gl, texture);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM