Releasing loaded texture on WebGL

The Textured Box demo at http://www.khronos.org/webgl/wiki/Demo_Repository contains the following code fragments:

function loadImageTexture(ctx, url)
{
    var texture = ctx.createTexture(); // allocate texture
    texture.image = new Image();
    texture.image.onload = function() 
                              { doLoadImageTexture(ctx, texture.image, texture) }
    texture.image.src = url;
    return texture;
}

function doLoadImageTexture(ctx, image, texture)
{
    ctx.bindTexture(ctx.TEXTURE_2D, texture);
    ctx.texImage2D(ctx.TEXTURE_2D, 0, ctx.RGBA, ctx.RGBA, ctx.UNSIGNED_BYTE, image);  // loaded the image
...
}
...

var spiritTexture = loadImageTexture(gl, "resources/spirit.jpg");
...

How do you free selected / loaded texture to avoid memory leak (graphics)?

Will the following code contain both the loaded / selected texture and image?

spiritTexture  = null;

Thanks in advance for your help.

NOTE. Cross the post http://www.khronos.org/message_boards/viewtopic.php?f=43&t=3367 December 23, 2010, but there is no answer yet.

+3
source share
1 answer
ctx.deleteTexture(spiritTexture);

This should free the texture on gpu.

, loadImageTexture, image texture. image loadImageTexture, , doLoadImageTexture.

. - :

function loadImageTexture(ctx, url)
{
    var texture = ctx.createTexture(); // allocate texture
    var image = new Image();
    image.onload = function() { doLoadImageTexture(ctx, image, texture) }
    image.src = url;
    return texture;
}
+3

Source: https://habr.com/ru/post/1787341/


All Articles