+GLuint ResourcePool::use_glsl_program(GLuint glsl_program_num)
+{
+ pthread_mutex_lock(&lock);
+ assert(program_instances.count(glsl_program_num));
+ stack<GLuint> &instances = program_instances[glsl_program_num];
+
+ GLuint instance_program_num;
+ if (!instances.empty()) {
+ // There's an unused instance of this program; just return it.
+ instance_program_num = instances.top();
+ instances.pop();
+ } else {
+ // We need to clone this program. (unuse_glsl_program()
+ // will later put it onto the list.)
+ map<GLuint, ShaderSpec>::iterator shader_it =
+ program_shaders.find(glsl_program_num);
+ assert(shader_it != program_shaders.end());
+
+ instance_program_num = link_program(
+ shader_it->second.vs_obj,
+ shader_it->second.fs_obj,
+ shader_it->second.fragment_shader_outputs);
+ program_masters.insert(make_pair(instance_program_num, glsl_program_num));
+ }
+ pthread_mutex_unlock(&lock);
+
+ glUseProgram(instance_program_num);
+ return instance_program_num;
+}
+
+void ResourcePool::unuse_glsl_program(GLuint instance_program_num)
+{
+ pthread_mutex_lock(&lock);
+
+ map<GLuint, GLuint>::const_iterator master_it = program_masters.find(instance_program_num);
+ assert(master_it != program_masters.end());
+
+ assert(program_instances.count(master_it->second));
+ stack<GLuint> &instances = program_instances[master_it->second];
+
+ instances.push(instance_program_num);
+
+ pthread_mutex_unlock(&lock);
+}
+