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 (GLuint program : program_freelist) {
37 delete_program(program);
39 assert(programs.empty());
40 assert(program_shaders.empty());
42 for (GLuint free_texture_num : texture_freelist) {
43 assert(texture_formats.count(free_texture_num) != 0);
44 texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
45 glDeleteSync(texture_formats[free_texture_num].no_reuse_before);
46 texture_formats.erase(free_texture_num);
47 glDeleteTextures(1, &free_texture_num);
50 assert(texture_formats.empty());
51 assert(texture_freelist_bytes == 0);
53 void *context = get_gl_context_identifier();
54 cleanup_unlinked_fbos(context);
56 for (const auto &context_and_fbos : fbo_freelist) {
57 if (context_and_fbos.first != context) {
58 // If this does not hold, the client should have called clean_context() earlier.
59 assert(context_and_fbos.second.empty());
62 for (FBOFormatIterator fbo_it : context_and_fbos.second) {
63 glDeleteFramebuffers(1, &fbo_it->second.fbo_num);
65 fbo_formats.erase(fbo_it);
69 assert(fbo_formats.empty());
72 void ResourcePool::delete_program(GLuint glsl_program_num)
74 bool found_program = false;
75 for (map<pair<string, string>, GLuint>::iterator program_it = programs.begin();
76 program_it != programs.end();
78 if (program_it->second == glsl_program_num) {
79 programs.erase(program_it);
84 for (map<string, GLuint>::iterator program_it = compute_programs.begin();
85 program_it != compute_programs.end();
87 if (program_it->second == glsl_program_num) {
88 compute_programs.erase(program_it);
93 assert(found_program);
95 map<GLuint, stack<GLuint>>::iterator instance_list_it = program_instances.find(glsl_program_num);
96 assert(instance_list_it != program_instances.end());
98 while (!instance_list_it->second.empty()) {
99 GLuint instance_program_num = instance_list_it->second.top();
100 instance_list_it->second.pop();
101 glDeleteProgram(instance_program_num);
102 program_masters.erase(instance_program_num);
104 program_instances.erase(instance_list_it);
106 map<GLuint, ShaderSpec>::iterator shader_it =
107 program_shaders.find(glsl_program_num);
108 if (shader_it == program_shaders.end()) {
109 // Should be a compute shader.
110 map<GLuint, ComputeShaderSpec>::iterator compute_shader_it =
111 compute_program_shaders.find(glsl_program_num);
112 assert(compute_shader_it != compute_program_shaders.end());
114 glDeleteShader(compute_shader_it->second.cs_obj);
115 compute_program_shaders.erase(compute_shader_it);
117 glDeleteShader(shader_it->second.vs_obj);
118 glDeleteShader(shader_it->second.fs_obj);
119 program_shaders.erase(shader_it);
123 GLuint ResourcePool::compile_glsl_program(const string& vertex_shader,
124 const string& fragment_shader,
125 const vector<string>& fragment_shader_outputs)
127 GLuint glsl_program_num;
128 pthread_mutex_lock(&lock);
130 // Augment the fragment shader program text with the outputs, so that they become
131 // part of the key. Also potentially useful for debugging.
132 string fragment_shader_processed = fragment_shader;
133 for (unsigned output_index = 0; output_index < fragment_shader_outputs.size(); ++output_index) {
135 snprintf(buf, sizeof(buf), "// Bound output: %s\n", fragment_shader_outputs[output_index].c_str());
136 fragment_shader_processed += buf;
139 const pair<string, string> key(vertex_shader, fragment_shader_processed);
140 if (programs.count(key)) {
141 // Already in the cache.
142 glsl_program_num = programs[key];
143 increment_program_refcount(glsl_program_num);
145 // Not in the cache. Compile the shaders.
146 GLuint vs_obj = compile_shader(vertex_shader, GL_VERTEX_SHADER);
148 GLuint fs_obj = compile_shader(fragment_shader_processed, GL_FRAGMENT_SHADER);
150 glsl_program_num = link_program(vs_obj, fs_obj, fragment_shader_outputs);
152 output_debug_shader(fragment_shader_processed, "frag");
154 programs.insert(make_pair(key, glsl_program_num));
155 add_master_program(glsl_program_num);
158 spec.vs_obj = vs_obj;
159 spec.fs_obj = fs_obj;
160 spec.fragment_shader_outputs = fragment_shader_outputs;
161 program_shaders.insert(make_pair(glsl_program_num, spec));
163 pthread_mutex_unlock(&lock);
164 return glsl_program_num;
167 GLuint ResourcePool::link_program(GLuint vs_obj,
169 const vector<string>& fragment_shader_outputs)
171 GLuint glsl_program_num = glCreateProgram();
173 glAttachShader(glsl_program_num, vs_obj);
175 glAttachShader(glsl_program_num, fs_obj);
178 // Bind the outputs, if we have multiple ones.
179 if (fragment_shader_outputs.size() > 1) {
180 for (unsigned output_index = 0; output_index < fragment_shader_outputs.size(); ++output_index) {
181 glBindFragDataLocation(glsl_program_num, output_index,
182 fragment_shader_outputs[output_index].c_str());
186 glLinkProgram(glsl_program_num);
190 glGetProgramiv(glsl_program_num, GL_LINK_STATUS, &success);
191 if (success == GL_FALSE) {
192 GLchar error_log[1024] = {0};
193 glGetProgramInfoLog(glsl_program_num, 1024, nullptr, error_log);
194 fprintf(stderr, "Error linking program: %s\n", error_log);
198 return glsl_program_num;
201 void ResourcePool::release_glsl_program(GLuint glsl_program_num)
203 pthread_mutex_lock(&lock);
204 map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
205 assert(refcount_it != program_refcount.end());
207 if (--refcount_it->second == 0) {
208 program_refcount.erase(refcount_it);
209 assert(find(program_freelist.begin(), program_freelist.end(), glsl_program_num)
210 == program_freelist.end());
211 program_freelist.push_front(glsl_program_num);
212 if (program_freelist.size() > program_freelist_max_length) {
213 delete_program(program_freelist.back());
214 program_freelist.pop_back();
218 pthread_mutex_unlock(&lock);
221 GLuint ResourcePool::compile_glsl_compute_program(const string& compute_shader)
223 GLuint glsl_program_num;
224 pthread_mutex_lock(&lock);
226 const string &key = compute_shader;
227 if (compute_programs.count(key)) {
228 // Already in the cache.
229 glsl_program_num = compute_programs[key];
230 increment_program_refcount(glsl_program_num);
232 // Not in the cache. Compile the shader.
233 GLuint cs_obj = compile_shader(compute_shader, GL_COMPUTE_SHADER);
235 glsl_program_num = link_compute_program(cs_obj);
237 output_debug_shader(compute_shader, "comp");
239 compute_programs.insert(make_pair(key, glsl_program_num));
240 add_master_program(glsl_program_num);
242 ComputeShaderSpec spec;
243 spec.cs_obj = cs_obj;
244 compute_program_shaders.insert(make_pair(glsl_program_num, spec));
246 pthread_mutex_unlock(&lock);
247 return glsl_program_num;
250 GLuint ResourcePool::link_compute_program(GLuint cs_obj)
252 GLuint glsl_program_num = glCreateProgram();
254 glAttachShader(glsl_program_num, cs_obj);
256 glLinkProgram(glsl_program_num);
260 glGetProgramiv(glsl_program_num, GL_LINK_STATUS, &success);
261 if (success == GL_FALSE) {
262 GLchar error_log[1024] = {0};
263 glGetProgramInfoLog(glsl_program_num, 1024, nullptr, error_log);
264 fprintf(stderr, "Error linking program: %s\n", error_log);
268 return glsl_program_num;
271 GLuint ResourcePool::use_glsl_program(GLuint glsl_program_num)
273 pthread_mutex_lock(&lock);
274 assert(program_instances.count(glsl_program_num));
275 stack<GLuint> &instances = program_instances[glsl_program_num];
277 GLuint instance_program_num;
278 if (!instances.empty()) {
279 // There's an unused instance of this program; just return it.
280 instance_program_num = instances.top();
283 // We need to clone this program. (unuse_glsl_program()
284 // will later put it onto the list.)
285 map<GLuint, ShaderSpec>::iterator shader_it =
286 program_shaders.find(glsl_program_num);
287 if (shader_it == program_shaders.end()) {
288 // Should be a compute shader.
289 map<GLuint, ComputeShaderSpec>::iterator compute_shader_it =
290 compute_program_shaders.find(glsl_program_num);
291 instance_program_num = link_compute_program(
292 compute_shader_it->second.cs_obj);
294 // A regular fragment shader.
295 instance_program_num = link_program(
296 shader_it->second.vs_obj,
297 shader_it->second.fs_obj,
298 shader_it->second.fragment_shader_outputs);
300 program_masters.insert(make_pair(instance_program_num, glsl_program_num));
302 pthread_mutex_unlock(&lock);
304 glUseProgram(instance_program_num);
305 return instance_program_num;
308 void ResourcePool::unuse_glsl_program(GLuint instance_program_num)
310 pthread_mutex_lock(&lock);
312 auto master_it = program_masters.find(instance_program_num);
313 assert(master_it != program_masters.end());
315 assert(program_instances.count(master_it->second));
316 stack<GLuint> &instances = program_instances[master_it->second];
318 instances.push(instance_program_num);
320 pthread_mutex_unlock(&lock);
323 GLuint ResourcePool::create_2d_texture(GLint internal_format, GLsizei width, GLsizei height)
328 pthread_mutex_lock(&lock);
329 // See if there's a texture on the freelist we can use.
330 for (auto freelist_it = texture_freelist.begin();
331 freelist_it != texture_freelist.end();
333 GLuint texture_num = *freelist_it;
334 map<GLuint, Texture2D>::const_iterator format_it = texture_formats.find(texture_num);
335 assert(format_it != texture_formats.end());
336 if (format_it->second.internal_format == internal_format &&
337 format_it->second.width == width &&
338 format_it->second.height == height) {
339 texture_freelist_bytes -= estimate_texture_size(format_it->second);
340 texture_freelist.erase(freelist_it);
341 GLsync sync = format_it->second.no_reuse_before;
342 pthread_mutex_unlock(&lock);
343 glWaitSync(sync, 0, GL_TIMEOUT_IGNORED);
349 // Find any reasonable format given the internal format; OpenGL validates it
350 // even though we give nullptr as pointer.
352 switch (internal_format) {
358 case GL_SRGB8_ALPHA8:
364 case GL_R11F_G11F_B10F:
385 // TODO: Add more here as needed.
389 // Same with type; GLES is stricter than desktop OpenGL here.
391 switch (internal_format) {
396 case GL_R11F_G11F_B10F:
408 type = GL_UNSIGNED_SHORT;
410 case GL_SRGB8_ALPHA8:
418 type = GL_UNSIGNED_BYTE;
421 type = GL_UNSIGNED_SHORT_5_6_5;
424 // TODO: Add more here as needed.
430 glGenTextures(1, &texture_num);
432 glBindTexture(GL_TEXTURE_2D, texture_num);
434 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, nullptr);
436 glBindTexture(GL_TEXTURE_2D, 0);
439 Texture2D texture_format;
440 texture_format.internal_format = internal_format;
441 texture_format.width = width;
442 texture_format.height = height;
443 assert(texture_formats.count(texture_num) == 0);
444 texture_formats.insert(make_pair(texture_num, texture_format));
446 pthread_mutex_unlock(&lock);
450 void ResourcePool::release_2d_texture(GLuint texture_num)
452 pthread_mutex_lock(&lock);
453 texture_freelist.push_front(texture_num);
454 assert(texture_formats.count(texture_num) != 0);
455 texture_freelist_bytes += estimate_texture_size(texture_formats[texture_num]);
456 texture_formats[texture_num].no_reuse_before = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
458 while (texture_freelist_bytes > texture_freelist_max_bytes) {
459 GLuint free_texture_num = texture_freelist.back();
460 texture_freelist.pop_back();
461 assert(texture_formats.count(free_texture_num) != 0);
462 texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
463 glDeleteSync(texture_formats[free_texture_num].no_reuse_before);
464 texture_formats.erase(free_texture_num);
465 glDeleteTextures(1, &free_texture_num);
468 // Unlink any lingering FBO related to this texture. We might
469 // not be in the right context, so don't delete it right away;
470 // the cleanup in release_fbo() (which calls cleanup_unlinked_fbos())
471 // will take care of actually doing that later.
472 for (auto &key_and_fbo : fbo_formats) {
473 for (unsigned i = 0; i < num_fbo_attachments; ++i) {
474 if (key_and_fbo.second.texture_num[i] == free_texture_num) {
475 key_and_fbo.second.texture_num[i] = GL_INVALID_INDEX;
480 pthread_mutex_unlock(&lock);
483 GLuint ResourcePool::create_fbo(GLuint texture0_num, GLuint texture1_num, GLuint texture2_num, GLuint texture3_num)
485 void *context = get_gl_context_identifier();
487 // Make sure we are filled from the bottom.
488 assert(texture0_num != 0);
489 if (texture1_num == 0) {
490 assert(texture2_num == 0);
492 if (texture2_num == 0) {
493 assert(texture3_num == 0);
496 pthread_mutex_lock(&lock);
497 if (fbo_freelist.count(context) != 0) {
498 // See if there's an FBO on the freelist we can use.
499 auto end = fbo_freelist[context].end();
500 for (auto freelist_it = fbo_freelist[context].begin(); freelist_it != end; ++freelist_it) {
501 FBOFormatIterator fbo_it = *freelist_it;
502 if (fbo_it->second.texture_num[0] == texture0_num &&
503 fbo_it->second.texture_num[1] == texture1_num &&
504 fbo_it->second.texture_num[2] == texture2_num &&
505 fbo_it->second.texture_num[3] == texture3_num) {
506 fbo_freelist[context].erase(freelist_it);
507 pthread_mutex_unlock(&lock);
508 return fbo_it->second.fbo_num;
515 fbo_format.texture_num[0] = texture0_num;
516 fbo_format.texture_num[1] = texture1_num;
517 fbo_format.texture_num[2] = texture2_num;
518 fbo_format.texture_num[3] = texture3_num;
520 glGenFramebuffers(1, &fbo_format.fbo_num);
522 glBindFramebuffer(GL_FRAMEBUFFER, fbo_format.fbo_num);
525 GLenum bufs[num_fbo_attachments];
526 unsigned num_active_attachments = 0;
527 for (unsigned i = 0; i < num_fbo_attachments; ++i, ++num_active_attachments) {
528 if (fbo_format.texture_num[i] == 0) {
531 glFramebufferTexture2D(
533 GL_COLOR_ATTACHMENT0 + i,
535 fbo_format.texture_num[i],
538 bufs[i] = GL_COLOR_ATTACHMENT0 + i;
541 glDrawBuffers(num_active_attachments, bufs);
544 GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
545 assert(status == GL_FRAMEBUFFER_COMPLETE);
546 glBindFramebuffer(GL_FRAMEBUFFER, 0);
549 pair<void *, GLuint> key(context, fbo_format.fbo_num);
550 assert(fbo_formats.count(key) == 0);
551 fbo_formats.insert(make_pair(key, fbo_format));
553 pthread_mutex_unlock(&lock);
554 return fbo_format.fbo_num;
557 void ResourcePool::release_fbo(GLuint fbo_num)
559 void *context = get_gl_context_identifier();
561 pthread_mutex_lock(&lock);
562 FBOFormatIterator fbo_it = fbo_formats.find(make_pair(context, fbo_num));
563 assert(fbo_it != fbo_formats.end());
564 fbo_freelist[context].push_front(fbo_it);
566 // Now that we're in this context, free up any FBOs that are connected
567 // to deleted textures (in release_2d_texture).
568 cleanup_unlinked_fbos(context);
570 shrink_fbo_freelist(context, fbo_freelist_max_length);
571 pthread_mutex_unlock(&lock);
574 GLuint ResourcePool::create_vec2_vao(const set<GLint> &attribute_indices, GLuint vbo_num)
576 void *context = get_gl_context_identifier();
578 pthread_mutex_lock(&lock);
579 if (vao_freelist.count(context) != 0) {
580 // See if there's a VAO the freelist we can use.
581 auto end = vao_freelist[context].end();
582 for (auto freelist_it = vao_freelist[context].begin(); freelist_it != end; ++freelist_it) {
583 VAOFormatIterator vao_it = *freelist_it;
584 if (vao_it->second.vbo_num == vbo_num &&
585 vao_it->second.attribute_indices == attribute_indices) {
586 vao_freelist[context].erase(freelist_it);
587 pthread_mutex_unlock(&lock);
588 return vao_it->second.vao_num;
595 vao_format.attribute_indices = attribute_indices;
596 vao_format.vbo_num = vbo_num;
598 glGenVertexArrays(1, &vao_format.vao_num);
600 glBindVertexArray(vao_format.vao_num);
602 glBindBuffer(GL_ARRAY_BUFFER, vbo_num);
605 for (GLint attr : attribute_indices) {
606 glEnableVertexAttribArray(attr);
608 glVertexAttribPointer(attr, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
612 glBindVertexArray(0);
614 glBindBuffer(GL_ARRAY_BUFFER, 0);
617 pair<void *, GLuint> key(context, vao_format.vao_num);
618 assert(vao_formats.count(key) == 0);
619 vao_formats.insert(make_pair(key, vao_format));
621 pthread_mutex_unlock(&lock);
622 return vao_format.vao_num;
625 void ResourcePool::release_vec2_vao(GLuint vao_num)
627 void *context = get_gl_context_identifier();
629 pthread_mutex_lock(&lock);
630 VAOFormatIterator vao_it = vao_formats.find(make_pair(context, vao_num));
631 assert(vao_it != vao_formats.end());
632 vao_freelist[context].push_front(vao_it);
634 shrink_vao_freelist(context, vao_freelist_max_length);
635 pthread_mutex_unlock(&lock);
638 void ResourcePool::clean_context()
640 void *context = get_gl_context_identifier();
642 // Currently, we only need to worry about FBOs and VAOs, as they are the only
643 // non-shareable resources we hold.
644 shrink_fbo_freelist(context, 0);
645 fbo_freelist.erase(context);
647 shrink_vao_freelist(context, 0);
648 vao_freelist.erase(context);
651 void ResourcePool::cleanup_unlinked_fbos(void *context)
653 auto end = fbo_freelist[context].end();
654 for (auto freelist_it = fbo_freelist[context].begin(); freelist_it != end; ) {
655 FBOFormatIterator fbo_it = *freelist_it;
657 bool all_unlinked = true;
658 for (unsigned i = 0; i < num_fbo_attachments; ++i) {
659 if (fbo_it->second.texture_num[i] != 0 &&
660 fbo_it->second.texture_num[i] != GL_INVALID_INDEX) {
661 all_unlinked = false;
666 glDeleteFramebuffers(1, &fbo_it->second.fbo_num);
668 fbo_formats.erase(fbo_it);
669 fbo_freelist[context].erase(freelist_it++);
676 void ResourcePool::shrink_fbo_freelist(void *context, size_t max_length)
678 list<FBOFormatIterator> &freelist = fbo_freelist[context];
679 while (freelist.size() > max_length) {
680 FBOFormatIterator free_fbo_it = freelist.back();
681 glDeleteFramebuffers(1, &free_fbo_it->second.fbo_num);
683 fbo_formats.erase(free_fbo_it);
688 void ResourcePool::shrink_vao_freelist(void *context, size_t max_length)
690 list<VAOFormatIterator> &freelist = vao_freelist[context];
691 while (freelist.size() > max_length) {
692 VAOFormatIterator free_vao_it = freelist.back();
693 glDeleteVertexArrays(1, &free_vao_it->second.vao_num);
695 vao_formats.erase(free_vao_it);
700 void ResourcePool::increment_program_refcount(GLuint program_num)
702 map<GLuint, int>::iterator refcount_it = program_refcount.find(program_num);
703 if (refcount_it != program_refcount.end()) {
704 ++refcount_it->second;
706 list<GLuint>::iterator freelist_it =
707 find(program_freelist.begin(), program_freelist.end(), program_num);
708 assert(freelist_it != program_freelist.end());
709 program_freelist.erase(freelist_it);
710 program_refcount.insert(make_pair(program_num, 1));
714 void ResourcePool::output_debug_shader(const string &shader_src, const string &suffix)
716 if (movit_debug_level == MOVIT_DEBUG_ON) {
717 // Output shader to a temporary file, for easier debugging.
718 static int compiled_shader_num = 0;
720 sprintf(filename, "chain-%03d.%s", compiled_shader_num++, suffix.c_str());
721 FILE *fp = fopen(filename, "w");
726 fprintf(fp, "%s\n", shader_src.c_str());
731 void ResourcePool::add_master_program(GLuint program_num)
733 program_refcount.insert(make_pair(program_num, 1));
734 stack<GLuint> instances;
735 instances.push(program_num);
736 program_instances.insert(make_pair(program_num, instances));
737 program_masters.insert(make_pair(program_num, program_num));
740 size_t ResourcePool::estimate_texture_size(const Texture2D &texture_format)
742 size_t bytes_per_pixel;
744 switch (texture_format.internal_format) {
746 bytes_per_pixel = 16;
752 bytes_per_pixel = 12;
757 case GL_R11F_G11F_B10F:
764 case GL_SRGB8_ALPHA8:
807 // TODO: Add more here as needed.
811 return texture_format.width * texture_format.height * bytes_per_pixel;