+ return vao_format.vao_num;
+}
+
+void ResourcePool::release_vec2_vao(GLuint vao_num)
+{
+ void *context = get_gl_context_identifier();
+
+ pthread_mutex_lock(&lock);
+ VAOFormatIterator vao_it = vao_formats.find(make_pair(context, vao_num));
+ assert(vao_it != vao_formats.end());
+ vao_freelist[context].push_front(vao_it);
+
+ shrink_vao_freelist(context, vao_freelist_max_length);
+ pthread_mutex_unlock(&lock);
+}
+
+void ResourcePool::clean_context()
+{
+ void *context = get_gl_context_identifier();
+
+ // Currently, we only need to worry about FBOs and VAOs, as they are the only
+ // non-shareable resources we hold.
+ shrink_fbo_freelist(context, 0);
+ fbo_freelist.erase(context);
+
+ shrink_vao_freelist(context, 0);
+ vao_freelist.erase(context);
+}
+
+void ResourcePool::cleanup_unlinked_fbos(void *context)
+{
+ auto end = fbo_freelist[context].end();
+ for (auto freelist_it = fbo_freelist[context].begin(); freelist_it != end; ) {
+ FBOFormatIterator fbo_it = *freelist_it;
+
+ bool all_unlinked = true;
+ for (unsigned i = 0; i < num_fbo_attachments; ++i) {
+ if (fbo_it->second.texture_num[i] != 0 &&
+ fbo_it->second.texture_num[i] != GL_INVALID_INDEX) {
+ all_unlinked = false;
+ break;
+ }
+ }
+ if (all_unlinked) {
+ glDeleteFramebuffers(1, &fbo_it->second.fbo_num);
+ check_error();
+ fbo_formats.erase(fbo_it);
+ fbo_freelist[context].erase(freelist_it++);
+ } else {
+ freelist_it++;
+ }
+ }
+}
+
+void ResourcePool::shrink_fbo_freelist(void *context, size_t max_length)
+{
+ list<FBOFormatIterator> &freelist = fbo_freelist[context];
+ while (freelist.size() > max_length) {
+ FBOFormatIterator free_fbo_it = freelist.back();
+ glDeleteFramebuffers(1, &free_fbo_it->second.fbo_num);
+ check_error();
+ fbo_formats.erase(free_fbo_it);
+ freelist.pop_back();
+ }
+}
+
+void ResourcePool::shrink_vao_freelist(void *context, size_t max_length)
+{
+ list<VAOFormatIterator> &freelist = vao_freelist[context];
+ while (freelist.size() > max_length) {
+ VAOFormatIterator free_vao_it = freelist.back();
+ glDeleteVertexArrays(1, &free_vao_it->second.vao_num);
+ check_error();
+ vao_formats.erase(free_vao_it);
+ freelist.pop_back();
+ }
+}
+
+void ResourcePool::increment_program_refcount(GLuint program_num)
+{
+ map<GLuint, int>::iterator refcount_it = program_refcount.find(program_num);
+ if (refcount_it != program_refcount.end()) {
+ ++refcount_it->second;
+ } else {
+ list<GLuint>::iterator freelist_it =
+ find(program_freelist.begin(), program_freelist.end(), program_num);
+ assert(freelist_it != program_freelist.end());
+ program_freelist.erase(freelist_it);
+ program_refcount.insert(make_pair(program_num, 1));
+ }
+}
+
+void ResourcePool::output_debug_shader(const string &shader_src, const string &suffix)
+{
+ if (movit_debug_level == MOVIT_DEBUG_ON) {
+ // Output shader to a temporary file, for easier debugging.
+ static int compiled_shader_num = 0;
+ char filename[256];
+ sprintf(filename, "chain-%03d.%s", compiled_shader_num++, suffix.c_str());
+ FILE *fp = fopen(filename, "w");
+ if (fp == nullptr) {
+ perror(filename);
+ exit(1);
+ }
+ fprintf(fp, "%s\n", shader_src.c_str());
+ fclose(fp);
+ }
+}
+
+void ResourcePool::add_master_program(GLuint program_num)
+{
+ program_refcount.insert(make_pair(program_num, 1));
+ stack<GLuint> instances;
+ instances.push(program_num);
+ program_instances.insert(make_pair(program_num, instances));
+ program_masters.insert(make_pair(program_num, program_num));