After seeing one of our projects' memory usage continously increasing from ~90MB up to 300MB in a couple of minutes ( 100-300KB/s ) I've run down to the cause of it. I don't know if this is an iOS 9 issue or not since we recently upgraded to iOS 9 on an iPad Air 2, but I've narrowed it down to using Pixel Buffer Objects for texture uploads.
So, every frame I do this :
static bool VersionCheck = false;
static bool UsePBOs = false;/
if (!VersionCheck)
{
VersionCheck = true;
const char *Version = (const char*)glGetString( GL_VERSION );
float fVer = 2.0f;
if (Version && sscanf( Version, "OpenGL ES %f", &fVer ))
{
if ( fVer >= 3.0f )
UsePBOs = true;
}
}
/
if ( UsePBOs )
{
int TotalData = Width * Height *4;
static unsigned int PBO = 0;
if ( PBO == 0)
{
glGenBuffers( 1, &PBO );
CheckGLError();
glBindBuffer( GL_PIXEL_UNPACK_BUFFER, PBO );
CheckGLError();
glBufferData( GL_PIXEL_UNPACK_BUFFER, TotalData, NULL, GL_STREAM_COPY);
CheckGLError();
}
glBindBuffer( GL_PIXEL_UNPACK_BUFFER, PBO );
CheckGLError();
GLubyte* ptr = (GLubyte*)glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, TotalData, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT );
CheckGLError();
if ( ptr )
{
pthread_mutex_lock( &CameraPixelsMutex );
if ( Data )
memcpy( ptr, Data, TotalData );
pthread_mutex_unlock( &CameraPixelsMutex );
GLboolean ret = glUnmapBuffer( GL_PIXEL_UNPACK_BUFFER );
CheckGLError();
if ( ret != 1 )
{
NSLog(@"glUnmapBuffer( GL_PIXEL_UNPACK_BUFFER ) returned %d", ret );
}
}
else
NSLog(@"glMapBufferRange returned nullptr %p", ptr );
glBindTexture( GL_TEXTURE_2D, TextureID );
glTexSubImage2D( GL_TEXTURE_2D, /
0, /
0,
0, /
Width, /
Height, /
GL_BGRA, /
GL_UNSIGNED_BYTE, /
0
);
CheckGLError();
glBindBuffer( GL_PIXEL_UNPACK_BUFFER, 0 );
CheckGLError();
}
else
{
pthread_mutex_lock( &CameraPixelsMutex );
glBindTexture( GL_TEXTURE_2D, TextureID );
CheckGLError();
if ( Data )
{
glTexSubImage2D( GL_TEXTURE_2D,
0,
0,
0,
Width,
Height,
GL_BGRA,
GL_UNSIGNED_BYTE,
Data
);
}
CheckGLError();
pthread_mutex_unlock( &CameraPixelsMutex );
}
The thing, the GLES3/PBO branch is causing this 100K/s leak while if I only use the GLES2 route (where perf goes down by 50%) the memory usage stays constant ( i waited around 5 minutes and it doesn't go up at all ).
This feels like a bug in the GL driver or something may be wrong with me code, please advise.