+ pthread_mutex_lock(&lock);
+ texture_freelist.push_front(texture_num);
+ assert(texture_formats.count(texture_num) != 0);
+ texture_freelist_bytes += estimate_texture_size(texture_formats[texture_num]);
+
+ while (texture_freelist_bytes > texture_freelist_max_bytes) {
+ GLuint free_texture_num = texture_freelist.front();
+ texture_freelist.pop_front();
+ assert(texture_formats.count(free_texture_num) != 0);
+ texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
+ texture_formats.erase(free_texture_num);
+ glDeleteTextures(1, &free_texture_num);
+ check_error();
+
+ // Delete any FBO related to this texture.
+ for (list<GLuint>::iterator fbo_freelist_it = fbo_freelist.begin();
+ fbo_freelist_it != fbo_freelist.end(); ) {
+ GLuint fbo_num = *fbo_freelist_it;
+ map<GLuint, FBO>::const_iterator format_it = fbo_formats.find(fbo_num);
+ assert(format_it != fbo_formats.end());
+ if (format_it->second.texture_num == free_texture_num) {
+ fbo_formats.erase(fbo_num);
+ // TODO: We currently leak due to FBO sharability issues.
+ // glDeleteFramebuffers(1, &fbo_num);
+ fbo_freelist.erase(fbo_freelist_it++);
+ } else {
+ ++fbo_freelist_it;
+ }
+ }
+ }
+ pthread_mutex_unlock(&lock);
+}
+
+GLuint ResourcePool::create_fbo(void *context, GLuint texture_num)
+{
+ pthread_mutex_lock(&lock);
+ // See if there's an FBO on the freelist we can use.
+ for (list<GLuint>::iterator freelist_it = fbo_freelist.begin();
+ freelist_it != fbo_freelist.end();
+ ++freelist_it) {
+ GLuint fbo_num = *freelist_it;
+ map<GLuint, FBO>::const_iterator format_it = fbo_formats.find(fbo_num);
+ assert(format_it != fbo_formats.end());
+ if (format_it->second.context == context &&
+ format_it->second.texture_num == texture_num) {
+ fbo_freelist.erase(freelist_it);
+ pthread_mutex_unlock(&lock);
+ return fbo_num;
+ }
+ }
+
+ // Create a new one.
+ GLuint fbo_num;
+ glGenFramebuffers(1, &fbo_num);
+ check_error();
+ glBindFramebuffer(GL_FRAMEBUFFER, fbo_num);
+ check_error();
+ glFramebufferTexture2D(
+ GL_FRAMEBUFFER,
+ GL_COLOR_ATTACHMENT0,
+ GL_TEXTURE_2D,
+ texture_num,
+ 0);
+ check_error();
+ GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
+ assert(status == GL_FRAMEBUFFER_COMPLETE);
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);