OpenGl ES 1.0 Android Unbind纹理释放内存

时间:2018-01-27 15:26:58

标签: android opengl-es

我目前正在阅读这本书#34;开始Android 4游戏开发"作者:Mario Zechner(libGDX开发人员)。

我偶然发现了一段代码片段,描述了在解除和删除OpenGL ES 1.0中的纹理时如何在gpu中释放内存:

enter image description here

然后有些页面后来他构建了一个Texture类来处理整个工作:

    public class Texture {

GLGraphics glGraphics;
FileIO fileIO;
String fileName;
int textureId;
int minFilter;
int magFilter;

public Texture(GLGame glGame, String fileName) {
    this.glGraphics = glGame.getGLGraphics();
    this.fileIO = glGame.getFileIO();
    this.fileName = fileName;
    load();
}
private void load() {
    GL10 gl = glGraphics.getGL(); int[] textureIds = new int[1];
    gl.glGenTextures(1, textureIds, 0);
    textureId = textureIds[0];
    InputStream in = null; try {
        in = fileIO.readAsset(fileName);
        Bitmap bitmap = BitmapFactory.decodeStream(in);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
        setFilters(GL10.GL_NEAREST, GL10.GL_NEAREST);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
    } catch(IOException e) {
        throw new RuntimeException("Couldn't load texture '" + fileName +"'", e);
    } finally {
        if(in != null)
            try { in.close(); } catch (IOException e) { }
    }
}

public void reload() { 
    load();
    bind();
    setFilters(minFilter, magFilter); glGraphics.getGL().glBindTexture(GL10.GL_TEXTURE_2D, 0);
}
public void setFilters(int minFilter, int magFilter) {
    this.minFilter = minFilter;
    this.magFilter = magFilter;
    GL10 gl = glGraphics.getGL();
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, minFilter); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, magFilter);
}
public void bind() {
    GL10 gl = glGraphics.getGL();
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
}
public void dispose() {
    GL10 gl = glGraphics.getGL();
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
    int[] textureIds = { textureId };
    gl.glDeleteTextures(1, textureIds, 0);
}
}

真正令我困惑的是他使用:

gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId );

在dispose()方法中

     public void dispose() {
      GL10 gl = glGraphics.getGL();
      gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
      int[] textureIds = { textureId };
      gl.glDeleteTextures(1, textureIds, 0);
     }

如上所述,它应该是0而不是textureId, 确保要删除的当前纹理未绑定,并使用0覆盖默认值。 当他通过textureID时,他不是这样做的吗? 据我所知,这肯定会使它成为当前的绑定纹理。所以它可能被删除但仍被引用?

我用两个版本测试了代码,用0修改了textureId, 并且它总是相同的输出,它不再绘制纹理,这很好,但是在视频RAM中删除的纹理? 即使我注释掉整条线也行。但我很困惑 确保在视频RAM中也释放纹理是正确的。

在本主题中: OpenGL Unbind texture 最后的答案谈到了差异和一些FBO。这是什么?

提前谢谢大家!

0 个答案:

没有答案