12 #include "resource_pool.h"
19 ResourcePool::ResourcePool(size_t program_freelist_max_length,
20 size_t texture_freelist_max_bytes,
21 size_t fbo_freelist_max_length,
22 size_t vao_freelist_max_length)
23 : program_freelist_max_length(program_freelist_max_length),
24 texture_freelist_max_bytes(texture_freelist_max_bytes),
25 fbo_freelist_max_length(fbo_freelist_max_length),
26 vao_freelist_max_length(vao_freelist_max_length),
27 texture_freelist_bytes(0)
29 pthread_mutex_init(&lock, nullptr);
32 ResourcePool::~ResourcePool()
34 assert(program_refcount.empty());
36 for (list<GLuint>::const_iterator freelist_it = program_freelist.begin();
37 freelist_it != program_freelist.end();
39 delete_program(*freelist_it);
41 assert(programs.empty());
42 assert(program_shaders.empty());
44 for (list<GLuint>::const_iterator freelist_it = texture_freelist.begin();
45 freelist_it != texture_freelist.end();
47 GLuint free_texture_num = *freelist_it;
48 assert(texture_formats.count(free_texture_num) != 0);
49 texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
50 texture_formats.erase(free_texture_num);
51 glDeleteTextures(1, &free_texture_num);
54 assert(texture_formats.empty());
55 assert(texture_freelist_bytes == 0);
57 void *context = get_gl_context_identifier();
58 cleanup_unlinked_fbos(context);
60 for (map<void *, std::list<FBOFormatIterator>>::iterator context_it = fbo_freelist.begin();
61 context_it != fbo_freelist.end();
63 if (context_it->first != context) {
64 // If this does not hold, the client should have called clean_context() earlier.
65 assert(context_it->second.empty());
68 for (list<FBOFormatIterator>::const_iterator freelist_it = context_it->second.begin();
69 freelist_it != context_it->second.end();
71 FBOFormatIterator fbo_it = *freelist_it;
72 glDeleteFramebuffers(1, &fbo_it->second.fbo_num);
74 fbo_formats.erase(fbo_it);
78 assert(fbo_formats.empty());
81 void ResourcePool::delete_program(GLuint glsl_program_num)
83 bool found_program = false;
84 for (map<pair<string, string>, GLuint>::iterator program_it = programs.begin();
85 program_it != programs.end();
87 if (program_it->second == glsl_program_num) {
88 programs.erase(program_it);
93 for (map<string, GLuint>::iterator program_it = compute_programs.begin();
94 program_it != compute_programs.end();
96 if (program_it->second == glsl_program_num) {
97 compute_programs.erase(program_it);
102 assert(found_program);
104 map<GLuint, stack<GLuint>>::iterator instance_list_it = program_instances.find(glsl_program_num);
105 assert(instance_list_it != program_instances.end());
107 while (!instance_list_it->second.empty()) {
108 GLuint instance_program_num = instance_list_it->second.top();
109 instance_list_it->second.pop();
110 glDeleteProgram(instance_program_num);
111 program_masters.erase(instance_program_num);
113 program_instances.erase(instance_list_it);
115 map<GLuint, ShaderSpec>::iterator shader_it =
116 program_shaders.find(glsl_program_num);
117 if (shader_it == program_shaders.end()) {
118 // Should be a compute shader.
119 map<GLuint, ComputeShaderSpec>::iterator compute_shader_it =
120 compute_program_shaders.find(glsl_program_num);
121 assert(compute_shader_it != compute_program_shaders.end());
123 glDeleteShader(compute_shader_it->second.cs_obj);
124 compute_program_shaders.erase(compute_shader_it);
126 glDeleteShader(shader_it->second.vs_obj);
127 glDeleteShader(shader_it->second.fs_obj);
128 program_shaders.erase(shader_it);
132 GLuint ResourcePool::compile_glsl_program(const string& vertex_shader,
133 const string& fragment_shader,
134 const vector<string>& fragment_shader_outputs)
136 GLuint glsl_program_num;
137 pthread_mutex_lock(&lock);
139 // Augment the fragment shader program text with the outputs, so that they become
140 // part of the key. Also potentially useful for debugging.
141 string fragment_shader_processed = fragment_shader;
142 for (unsigned output_index = 0; output_index < fragment_shader_outputs.size(); ++output_index) {
144 snprintf(buf, sizeof(buf), "// Bound output: %s\n", fragment_shader_outputs[output_index].c_str());
145 fragment_shader_processed += buf;
148 const pair<string, string> key(vertex_shader, fragment_shader_processed);
149 if (programs.count(key)) {
150 // Already in the cache.
151 glsl_program_num = programs[key];
152 increment_program_refcount(glsl_program_num);
154 // Not in the cache. Compile the shaders.
155 GLuint vs_obj = compile_shader(vertex_shader, GL_VERTEX_SHADER);
157 GLuint fs_obj = compile_shader(fragment_shader_processed, GL_FRAGMENT_SHADER);
159 glsl_program_num = link_program(vs_obj, fs_obj, fragment_shader_outputs);
161 output_debug_shader(fragment_shader_processed, "frag");
163 programs.insert(make_pair(key, glsl_program_num));
164 add_master_program(glsl_program_num);
167 spec.vs_obj = vs_obj;
168 spec.fs_obj = fs_obj;
169 spec.fragment_shader_outputs = fragment_shader_outputs;
170 program_shaders.insert(make_pair(glsl_program_num, spec));
172 pthread_mutex_unlock(&lock);
173 return glsl_program_num;
176 GLuint ResourcePool::link_program(GLuint vs_obj,
178 const vector<string>& fragment_shader_outputs)
180 GLuint glsl_program_num = glCreateProgram();
182 glAttachShader(glsl_program_num, vs_obj);
184 glAttachShader(glsl_program_num, fs_obj);
187 // Bind the outputs, if we have multiple ones.
188 if (fragment_shader_outputs.size() > 1) {
189 for (unsigned output_index = 0; output_index < fragment_shader_outputs.size(); ++output_index) {
190 glBindFragDataLocation(glsl_program_num, output_index,
191 fragment_shader_outputs[output_index].c_str());
195 glLinkProgram(glsl_program_num);
199 glGetProgramiv(glsl_program_num, GL_LINK_STATUS, &success);
200 if (success == GL_FALSE) {
201 GLchar error_log[1024] = {0};
202 glGetProgramInfoLog(glsl_program_num, 1024, nullptr, error_log);
203 fprintf(stderr, "Error linking program: %s\n", error_log);
207 return glsl_program_num;
210 void ResourcePool::release_glsl_program(GLuint glsl_program_num)
212 pthread_mutex_lock(&lock);
213 map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
214 assert(refcount_it != program_refcount.end());
216 if (--refcount_it->second == 0) {
217 program_refcount.erase(refcount_it);
218 assert(find(program_freelist.begin(), program_freelist.end(), glsl_program_num)
219 == program_freelist.end());
220 program_freelist.push_front(glsl_program_num);
221 if (program_freelist.size() > program_freelist_max_length) {
222 delete_program(program_freelist.back());
223 program_freelist.pop_back();
227 pthread_mutex_unlock(&lock);
230 GLuint ResourcePool::compile_glsl_compute_program(const string& compute_shader)
232 GLuint glsl_program_num;
233 pthread_mutex_lock(&lock);
235 const string &key = compute_shader;
236 if (compute_programs.count(key)) {
237 // Already in the cache.
238 glsl_program_num = compute_programs[key];
239 increment_program_refcount(glsl_program_num);
241 // Not in the cache. Compile the shader.
242 GLuint cs_obj = compile_shader(compute_shader, GL_COMPUTE_SHADER);
244 glsl_program_num = link_compute_program(cs_obj);
246 output_debug_shader(compute_shader, "compute");
248 compute_programs.insert(make_pair(key, glsl_program_num));
249 add_master_program(glsl_program_num);
251 ComputeShaderSpec spec;
252 spec.cs_obj = cs_obj;
253 compute_program_shaders.insert(make_pair(glsl_program_num, spec));
255 pthread_mutex_unlock(&lock);
256 return glsl_program_num;
259 GLuint ResourcePool::link_compute_program(GLuint cs_obj)
261 GLuint glsl_program_num = glCreateProgram();
263 glAttachShader(glsl_program_num, cs_obj);
265 glLinkProgram(glsl_program_num);
269 glGetProgramiv(glsl_program_num, GL_LINK_STATUS, &success);
270 if (success == GL_FALSE) {
271 GLchar error_log[1024] = {0};
272 glGetProgramInfoLog(glsl_program_num, 1024, nullptr, error_log);
273 fprintf(stderr, "Error linking program: %s\n", error_log);
277 return glsl_program_num;
280 GLuint ResourcePool::use_glsl_program(GLuint glsl_program_num)
282 pthread_mutex_lock(&lock);
283 assert(program_instances.count(glsl_program_num));
284 stack<GLuint> &instances = program_instances[glsl_program_num];
286 GLuint instance_program_num;
287 if (!instances.empty()) {
288 // There's an unused instance of this program; just return it.
289 instance_program_num = instances.top();
292 // We need to clone this program. (unuse_glsl_program()
293 // will later put it onto the list.)
294 map<GLuint, ShaderSpec>::iterator shader_it =
295 program_shaders.find(glsl_program_num);
296 if (shader_it == program_shaders.end()) {
297 // Should be a compute shader.
298 map<GLuint, ComputeShaderSpec>::iterator compute_shader_it =
299 compute_program_shaders.find(glsl_program_num);
300 instance_program_num = link_compute_program(
301 compute_shader_it->second.cs_obj);
303 // A regular fragment shader.
304 instance_program_num = link_program(
305 shader_it->second.vs_obj,
306 shader_it->second.fs_obj,
307 shader_it->second.fragment_shader_outputs);
309 program_masters.insert(make_pair(instance_program_num, glsl_program_num));
311 pthread_mutex_unlock(&lock);
313 glUseProgram(instance_program_num);
314 return instance_program_num;
317 void ResourcePool::unuse_glsl_program(GLuint instance_program_num)
319 pthread_mutex_lock(&lock);
321 map<GLuint, GLuint>::const_iterator master_it = program_masters.find(instance_program_num);
322 assert(master_it != program_masters.end());
324 assert(program_instances.count(master_it->second));
325 stack<GLuint> &instances = program_instances[master_it->second];
327 instances.push(instance_program_num);
329 pthread_mutex_unlock(&lock);
332 GLuint ResourcePool::create_2d_texture(GLint internal_format, GLsizei width, GLsizei height)
337 pthread_mutex_lock(&lock);
338 // See if there's a texture on the freelist we can use.
339 for (list<GLuint>::iterator freelist_it = texture_freelist.begin();
340 freelist_it != texture_freelist.end();
342 GLuint texture_num = *freelist_it;
343 map<GLuint, Texture2D>::const_iterator format_it = texture_formats.find(texture_num);
344 assert(format_it != texture_formats.end());
345 if (format_it->second.internal_format == internal_format &&
346 format_it->second.width == width &&
347 format_it->second.height == height) {
348 texture_freelist_bytes -= estimate_texture_size(format_it->second);
349 texture_freelist.erase(freelist_it);
350 pthread_mutex_unlock(&lock);
355 // Find any reasonable format given the internal format; OpenGL validates it
356 // even though we give nullptr as pointer.
358 switch (internal_format) {
364 case GL_SRGB8_ALPHA8:
370 case GL_R11F_G11F_B10F:
391 // TODO: Add more here as needed.
395 // Same with type; GLES is stricter than desktop OpenGL here.
397 switch (internal_format) {
402 case GL_R11F_G11F_B10F:
414 type = GL_UNSIGNED_SHORT;
416 case GL_SRGB8_ALPHA8:
424 type = GL_UNSIGNED_BYTE;
427 type = GL_UNSIGNED_SHORT_5_6_5;
430 // TODO: Add more here as needed.
436 glGenTextures(1, &texture_num);
438 glBindTexture(GL_TEXTURE_2D, texture_num);
440 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, nullptr);
442 glBindTexture(GL_TEXTURE_2D, 0);
445 Texture2D texture_format;
446 texture_format.internal_format = internal_format;
447 texture_format.width = width;
448 texture_format.height = height;
449 assert(texture_formats.count(texture_num) == 0);
450 texture_formats.insert(make_pair(texture_num, texture_format));
452 pthread_mutex_unlock(&lock);
456 void ResourcePool::release_2d_texture(GLuint texture_num)
458 pthread_mutex_lock(&lock);
459 texture_freelist.push_front(texture_num);
460 assert(texture_formats.count(texture_num) != 0);
461 texture_freelist_bytes += estimate_texture_size(texture_formats[texture_num]);
463 while (texture_freelist_bytes > texture_freelist_max_bytes) {
464 GLuint free_texture_num = texture_freelist.back();
465 texture_freelist.pop_back();
466 assert(texture_formats.count(free_texture_num) != 0);
467 texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
468 texture_formats.erase(free_texture_num);
469 glDeleteTextures(1, &free_texture_num);
472 // Unlink any lingering FBO related to this texture. We might
473 // not be in the right context, so don't delete it right away;
474 // the cleanup in release_fbo() (which calls cleanup_unlinked_fbos())
475 // will take care of actually doing that later.
476 for (map<pair<void *, GLuint>, FBO>::iterator format_it = fbo_formats.begin();
477 format_it != fbo_formats.end();
479 for (unsigned i = 0; i < num_fbo_attachments; ++i) {
480 if (format_it->second.texture_num[i] == free_texture_num) {
481 format_it->second.texture_num[i] = GL_INVALID_INDEX;
486 pthread_mutex_unlock(&lock);
489 GLuint ResourcePool::create_fbo(GLuint texture0_num, GLuint texture1_num, GLuint texture2_num, GLuint texture3_num)
491 void *context = get_gl_context_identifier();
493 // Make sure we are filled from the bottom.
494 assert(texture0_num != 0);
495 if (texture1_num == 0) {
496 assert(texture2_num == 0);
498 if (texture2_num == 0) {
499 assert(texture3_num == 0);
502 pthread_mutex_lock(&lock);
503 if (fbo_freelist.count(context) != 0) {
504 // See if there's an FBO on the freelist we can use.
505 list<FBOFormatIterator>::iterator end = fbo_freelist[context].end();
506 for (list<FBOFormatIterator>::iterator freelist_it = fbo_freelist[context].begin();
507 freelist_it != end; ++freelist_it) {
508 FBOFormatIterator fbo_it = *freelist_it;
509 if (fbo_it->second.texture_num[0] == texture0_num &&
510 fbo_it->second.texture_num[1] == texture1_num &&
511 fbo_it->second.texture_num[2] == texture2_num &&
512 fbo_it->second.texture_num[3] == texture3_num) {
513 fbo_freelist[context].erase(freelist_it);
514 pthread_mutex_unlock(&lock);
515 return fbo_it->second.fbo_num;
522 fbo_format.texture_num[0] = texture0_num;
523 fbo_format.texture_num[1] = texture1_num;
524 fbo_format.texture_num[2] = texture2_num;
525 fbo_format.texture_num[3] = texture3_num;
527 glGenFramebuffers(1, &fbo_format.fbo_num);
529 glBindFramebuffer(GL_FRAMEBUFFER, fbo_format.fbo_num);
532 GLenum bufs[num_fbo_attachments];
533 unsigned num_active_attachments = 0;
534 for (unsigned i = 0; i < num_fbo_attachments; ++i, ++num_active_attachments) {
535 if (fbo_format.texture_num[i] == 0) {
538 glFramebufferTexture2D(
540 GL_COLOR_ATTACHMENT0 + i,
542 fbo_format.texture_num[i],
545 bufs[i] = GL_COLOR_ATTACHMENT0 + i;
548 glDrawBuffers(num_active_attachments, bufs);
551 GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
552 assert(status == GL_FRAMEBUFFER_COMPLETE);
553 glBindFramebuffer(GL_FRAMEBUFFER, 0);
556 pair<void *, GLuint> key(context, fbo_format.fbo_num);
557 assert(fbo_formats.count(key) == 0);
558 fbo_formats.insert(make_pair(key, fbo_format));
560 pthread_mutex_unlock(&lock);
561 return fbo_format.fbo_num;
564 void ResourcePool::release_fbo(GLuint fbo_num)
566 void *context = get_gl_context_identifier();
568 pthread_mutex_lock(&lock);
569 FBOFormatIterator fbo_it = fbo_formats.find(make_pair(context, fbo_num));
570 assert(fbo_it != fbo_formats.end());
571 fbo_freelist[context].push_front(fbo_it);
573 // Now that we're in this context, free up any FBOs that are connected
574 // to deleted textures (in release_2d_texture).
575 cleanup_unlinked_fbos(context);
577 shrink_fbo_freelist(context, fbo_freelist_max_length);
578 pthread_mutex_unlock(&lock);
581 GLuint ResourcePool::create_vec2_vao(const set<GLint> &attribute_indices, GLuint vbo_num)
583 void *context = get_gl_context_identifier();
585 pthread_mutex_lock(&lock);
586 if (vao_freelist.count(context) != 0) {
587 // See if there's a VAO the freelist we can use.
588 list<VAOFormatIterator>::iterator end = vao_freelist[context].end();
589 for (list<VAOFormatIterator>::iterator freelist_it = vao_freelist[context].begin();
590 freelist_it != end; ++freelist_it) {
591 VAOFormatIterator vao_it = *freelist_it;
592 if (vao_it->second.vbo_num == vbo_num &&
593 vao_it->second.attribute_indices == attribute_indices) {
594 vao_freelist[context].erase(freelist_it);
595 pthread_mutex_unlock(&lock);
596 return vao_it->second.vao_num;
603 vao_format.attribute_indices = attribute_indices;
604 vao_format.vbo_num = vbo_num;
606 glGenVertexArrays(1, &vao_format.vao_num);
608 glBindVertexArray(vao_format.vao_num);
610 glBindBuffer(GL_ARRAY_BUFFER, vbo_num);
613 for (set<GLint>::const_iterator attr_it = attribute_indices.begin(); attr_it != attribute_indices.end(); ++attr_it) {
614 glEnableVertexAttribArray(*attr_it);
616 glVertexAttribPointer(*attr_it, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
620 glBindVertexArray(0);
622 glBindBuffer(GL_ARRAY_BUFFER, 0);
625 pair<void *, GLuint> key(context, vao_format.vao_num);
626 assert(vao_formats.count(key) == 0);
627 vao_formats.insert(make_pair(key, vao_format));
629 pthread_mutex_unlock(&lock);
630 return vao_format.vao_num;
633 void ResourcePool::release_vec2_vao(GLuint vao_num)
635 void *context = get_gl_context_identifier();
637 pthread_mutex_lock(&lock);
638 VAOFormatIterator vao_it = vao_formats.find(make_pair(context, vao_num));
639 assert(vao_it != vao_formats.end());
640 vao_freelist[context].push_front(vao_it);
642 shrink_vao_freelist(context, vao_freelist_max_length);
643 pthread_mutex_unlock(&lock);
646 void ResourcePool::clean_context()
648 void *context = get_gl_context_identifier();
650 // Currently, we only need to worry about FBOs and VAOs, as they are the only
651 // non-shareable resources we hold.
652 shrink_fbo_freelist(context, 0);
653 fbo_freelist.erase(context);
655 shrink_vao_freelist(context, 0);
656 vao_freelist.erase(context);
659 void ResourcePool::cleanup_unlinked_fbos(void *context)
661 list<FBOFormatIterator>::iterator end = fbo_freelist[context].end();
662 for (list<FBOFormatIterator>::iterator freelist_it = fbo_freelist[context].begin(); freelist_it != end; ) {
663 FBOFormatIterator fbo_it = *freelist_it;
665 bool all_unlinked = true;
666 for (unsigned i = 0; i < num_fbo_attachments; ++i) {
667 if (fbo_it->second.texture_num[i] != 0 &&
668 fbo_it->second.texture_num[i] != GL_INVALID_INDEX) {
669 all_unlinked = false;
674 glDeleteFramebuffers(1, &fbo_it->second.fbo_num);
676 fbo_formats.erase(fbo_it);
677 fbo_freelist[context].erase(freelist_it++);
684 void ResourcePool::shrink_fbo_freelist(void *context, size_t max_length)
686 list<FBOFormatIterator> &freelist = fbo_freelist[context];
687 while (freelist.size() > max_length) {
688 FBOFormatIterator free_fbo_it = freelist.back();
689 glDeleteFramebuffers(1, &free_fbo_it->second.fbo_num);
691 fbo_formats.erase(free_fbo_it);
696 void ResourcePool::shrink_vao_freelist(void *context, size_t max_length)
698 list<VAOFormatIterator> &freelist = vao_freelist[context];
699 while (freelist.size() > max_length) {
700 VAOFormatIterator free_vao_it = freelist.back();
701 glDeleteVertexArrays(1, &free_vao_it->second.vao_num);
703 vao_formats.erase(free_vao_it);
708 void ResourcePool::increment_program_refcount(GLuint program_num)
710 map<GLuint, int>::iterator refcount_it = program_refcount.find(program_num);
711 if (refcount_it != program_refcount.end()) {
712 ++refcount_it->second;
714 list<GLuint>::iterator freelist_it =
715 find(program_freelist.begin(), program_freelist.end(), program_num);
716 assert(freelist_it != program_freelist.end());
717 program_freelist.erase(freelist_it);
718 program_refcount.insert(make_pair(program_num, 1));
722 void ResourcePool::output_debug_shader(const string &shader_src, const string &suffix)
724 if (movit_debug_level == MOVIT_DEBUG_ON) {
725 // Output shader to a temporary file, for easier debugging.
726 static int compiled_shader_num = 0;
728 sprintf(filename, "chain-%03d.%s", compiled_shader_num++, suffix.c_str());
729 FILE *fp = fopen(filename, "w");
734 fprintf(fp, "%s\n", shader_src.c_str());
739 void ResourcePool::add_master_program(GLuint program_num)
741 program_refcount.insert(make_pair(program_num, 1));
742 stack<GLuint> instances;
743 instances.push(program_num);
744 program_instances.insert(make_pair(program_num, instances));
745 program_masters.insert(make_pair(program_num, program_num));
748 size_t ResourcePool::estimate_texture_size(const Texture2D &texture_format)
750 size_t bytes_per_pixel;
752 switch (texture_format.internal_format) {
754 bytes_per_pixel = 16;
760 bytes_per_pixel = 12;
765 case GL_R11F_G11F_B10F:
772 case GL_SRGB8_ALPHA8:
815 // TODO: Add more here as needed.
819 return texture_format.width * texture_format.height * bytes_per_pixel;