Skip to content

Instantly share code, notes, and snippets.

@smokris
Last active December 29, 2015 18:39
Show Gist options
  • Save smokris/7711889 to your computer and use it in GitHub Desktop.
Save smokris/7711889 to your computer and use it in GitHub Desktop.
Pooling GL_ELEMENT_ARRAY_BUFFERs
static vector<GLuint> elementArrayBufferPool;
static dispatch_semaphore_t elementArrayBufferPoolSemaphore;
static void __attribute__((constructor)) elementArrayBufferPoolInit(void)
{
elementArrayBufferPoolSemaphore = dispatch_semaphore_create(1);
}
GLuint getUnusedElementArrayBufferFromPool(void)
{
GLuint name = 0;
dispatch_semaphore_wait(elementArrayBufferPoolSemaphore, DISPATCH_TIME_FOREVER);
{
if (elementArrayBufferPool.size())
{
name = elementArrayBufferPool.back();
elementArrayBufferPool.pop_back();
}
else
{
CGLContextObj cgl_ctx = (CGLContextObj)getUnusedGLContext();
glGenBuffers(1, &name);
throwUnusedGLContextBackInPool(cgl_ctx);
}
}
dispatch_semaphore_signal(elementArrayBufferPoolSemaphore);
return name;
}
void throwElementArrayBufferBackInPool(GLuint name)
{
dispatch_semaphore_wait(elementArrayBufferPoolSemaphore, DISPATCH_TIME_FOREVER);
elementArrayBufferPool .push_back(name);
dispatch_semaphore_signal(elementArrayBufferPoolSemaphore);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment