0

我已經使用Apple的GLImageProcessing示例 - 我將各種濾鏡應用於圖像。我遵循GLImageProcessing Multiple Filters?使它能夠使用兩個過濾器。OpenGL ES:FBO /紋理泄漏內存

蘋果公司的原始示例非常好用,不會泄漏內存。

使用上面提到的問題中的代碼會使應用程序在10-20秒內使用所有的iPhone內存給我一個內存警告並最終導致應用程序崩潰。

我知道這是關係到創建aditional的紋理和幀緩衝對象(FBO的),但我不知道如何正確地管理他們(和它們的內存)

代碼:

void drawGL(int wide, int high, float contrastVal,float brightnessVal) 
{ 
    //INIT 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glOrthof(0, wide, 0, high, -1, 1); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    glScalef(wide, high, 1); 


    glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, (GLint *)&SystemFBO); 


    // Create the texture and the FBO the will hold the result of applying the first filter 
    glGenTextures(1, &ResultTexture); 
    glBindTexture(GL_TEXTURE_2D, ResultTexture); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, wide, high, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); 
    glGenFramebuffersOES(1, &ResultTextureFBO); 
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, ResultTextureFBO); 
    glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, ResultTexture, 0); 


    glBindFramebufferOES(GL_FRAMEBUFFER_OES, ResultTextureFBO); 
    glBindTexture(GL_TEXTURE_2D, Input.texID); 

    glViewport(0, 0, wide, high); 
    brightness(flipquad, contrastVal); 
    glCheckError();; 


    glBindFramebufferOES(GL_FRAMEBUFFER_OES, SystemFBO); 
    glBindTexture(GL_TEXTURE_2D, ResultTexture); 

    glViewport(0, 0, wide, high); 
    contrast(fullquad,brightnessVal); 
    glCheckError(); 
} 

有一個更好的方法嗎?

回答

1

你在做什麼看起來很好,但你永遠不會刪除紋理或FBO。你需要撥打:

glDeleteTextures (1, ResultTexture); 
glDeleteBuffers (1, ResultTextureFBO); 

在某些時候釋放他們使用的內存。否則,他們會永遠在一起。

1

我有完全相同的問題。內存警告似乎是每次調用方法時生成紋理的結果。

因此,儘量以下行移動到您的示例代碼initGL方法:

glGenTextures(1, &ResultTexture); 
glGenFramebuffersOES(1, &ResultTextureFBO);