Render Texture need some fixes

Hi every body,

i’m using render texture on some of my games, and have some issues in android version (like other people as i see on this forum).

so i try some “fix” to improve result and i give you what i done to have something work “not so bad”.

first : most of time, most of time i set end(bool) method parameters to false to increase performance (due to image caching), when i need to save the texture to keep it result in case of interrupt app (call, etc), i set end(bool) parameter to true only 1/20 frame, to keep a “recent version” but not saving all version.

second : i add in the render texture destructor a volatile texture removing, i’m not sure but i think without that it create memory leak and “reload all texture” unusefull waste of time. but it’s not 100% sure…

CCRenderTexture::~CCRenderTexture()
{
#if CC_ENABLE_CACHE_TEXTTURE_DATA
    if (m_pTexture)
    {
        VolatileTexture::removeTexture(m_pTexture);
    }
#endif

    removeAllChildrenWithCleanup(true);
    ccglDeleteFramebuffers(1, &m_uFBO);

    CC_SAFE_DELETE(m_pUITextureImage);
}

third : to prevent crash or other issue when returning after a call or other interrupt, i add a restoreFB() methods, which delete frame buffers and recreate it. and i call it when i detect interrupt.

bool CCRenderTexture::restoreFB()
{
    ccglDeleteFramebuffers(1, &m_uFBO);

    glGetIntegerv(CC_GL_FRAMEBUFFER_BINDING, &m_nOldFBO);

    // generate FBO
    ccglGenFramebuffers(1, &m_uFBO);
    ccglBindFramebuffer(CC_GL_FRAMEBUFFER, m_uFBO);

    // associate texture with FBO
    ccglFramebufferTexture2D(CC_GL_FRAMEBUFFER, CC_GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_pTexture->getName(), 0);

    // check if it worked (probably worth doing :) )
    GLuint status = ccglCheckFramebufferStatus(CC_GL_FRAMEBUFFER);
    if (status != CC_GL_FRAMEBUFFER_COMPLETE)
    {
        CCAssert(0, "Render Texture : Could not attach texture to framebuffer");
        CC_SAFE_DELETE(m_pTexture);
        return false;
    }

    ccglBindFramebuffer(CC_GL_FRAMEBUFFER, m_nOldFBO);
    return true;
}

with all that, i have no memory issue, no performance issue and no crash due to render texture (since now :wink: )

i will try in near future, to add a “restoreFB” call to the “reload all texture” or somewhere near to make restore FB easier.

i hope that help someone :smiley:

Thanks, does that fix the problem of render textures that appear white when the application is restoring from a stand by ?
Can you provide an example of how to detect interrupt in a scene to call restoreFB ?

it dont fix the problem of white render textures after stand by (if you mean restoring the old content of the FB), but prevent crash when you will try to write it after stand by (because of the FB become invalide).

to detect interrupt, i setup a flag that will launch at next update the restoreFB. that flag is set in 2 case :

  • in AppDelegate::applicationWillEnterForeground
  • in scene update, if the deltaTime > X sec (in my case 1.5sec)

if the is a “more clean” check for interrupt, that’s interrest me :slight_smile:

Interesting, thanks a lot