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, NULL);
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 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 assert(shader_it != program_shaders.end());
110 glDeleteShader(shader_it->second.vs_obj);
111 glDeleteShader(shader_it->second.fs_obj);
112 program_shaders.erase(shader_it);
115 GLuint ResourcePool::compile_glsl_program(const string& vertex_shader,
116 const string& fragment_shader,
117 const vector<string>& fragment_shader_outputs)
119 GLuint glsl_program_num;
120 pthread_mutex_lock(&lock);
122 // Augment the fragment shader program text with the outputs, so that they become
123 // part of the key. Also potentially useful for debugging.
124 string fragment_shader_processed = fragment_shader;
125 for (unsigned output_index = 0; output_index < fragment_shader_outputs.size(); ++output_index) {
127 snprintf(buf, sizeof(buf), "// Bound output: %s\n", fragment_shader_outputs[output_index].c_str());
128 fragment_shader_processed += buf;
131 const pair<string, string> key(vertex_shader, fragment_shader_processed);
132 if (programs.count(key)) {
133 // Already in the cache.
134 glsl_program_num = programs[key];
135 increment_program_refcount(glsl_program_num);
137 // Not in the cache. Compile the shaders.
138 GLuint vs_obj = compile_shader(vertex_shader, GL_VERTEX_SHADER);
140 GLuint fs_obj = compile_shader(fragment_shader_processed, GL_FRAGMENT_SHADER);
142 glsl_program_num = link_program(vs_obj, fs_obj, fragment_shader_outputs);
144 output_debug_shader(fragment_shader_processed, "frag");
146 programs.insert(make_pair(key, glsl_program_num));
147 add_master_program(glsl_program_num);
150 spec.vs_obj = vs_obj;
151 spec.fs_obj = fs_obj;
152 spec.fragment_shader_outputs = fragment_shader_outputs;
153 program_shaders.insert(make_pair(glsl_program_num, spec));
155 pthread_mutex_unlock(&lock);
156 return glsl_program_num;
159 GLuint ResourcePool::link_program(GLuint vs_obj,
161 const vector<string>& fragment_shader_outputs)
163 GLuint glsl_program_num = glCreateProgram();
165 glAttachShader(glsl_program_num, vs_obj);
167 glAttachShader(glsl_program_num, fs_obj);
170 // Bind the outputs, if we have multiple ones.
171 if (fragment_shader_outputs.size() > 1) {
172 for (unsigned output_index = 0; output_index < fragment_shader_outputs.size(); ++output_index) {
173 glBindFragDataLocation(glsl_program_num, output_index,
174 fragment_shader_outputs[output_index].c_str());
178 glLinkProgram(glsl_program_num);
182 glGetProgramiv(glsl_program_num, GL_LINK_STATUS, &success);
183 if (success == GL_FALSE) {
184 GLchar error_log[1024] = {0};
185 glGetProgramInfoLog(glsl_program_num, 1024, NULL, error_log);
186 fprintf(stderr, "Error linking program: %s\n", error_log);
190 return glsl_program_num;
193 void ResourcePool::release_glsl_program(GLuint glsl_program_num)
195 pthread_mutex_lock(&lock);
196 map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
197 assert(refcount_it != program_refcount.end());
199 if (--refcount_it->second == 0) {
200 program_refcount.erase(refcount_it);
201 assert(find(program_freelist.begin(), program_freelist.end(), glsl_program_num)
202 == program_freelist.end());
203 program_freelist.push_front(glsl_program_num);
204 if (program_freelist.size() > program_freelist_max_length) {
205 delete_program(program_freelist.back());
206 program_freelist.pop_back();
210 pthread_mutex_unlock(&lock);
213 GLuint ResourcePool::use_glsl_program(GLuint glsl_program_num)
215 pthread_mutex_lock(&lock);
216 assert(program_instances.count(glsl_program_num));
217 stack<GLuint> &instances = program_instances[glsl_program_num];
219 GLuint instance_program_num;
220 if (!instances.empty()) {
221 // There's an unused instance of this program; just return it.
222 instance_program_num = instances.top();
225 // We need to clone this program. (unuse_glsl_program()
226 // will later put it onto the list.)
227 map<GLuint, ShaderSpec>::iterator shader_it =
228 program_shaders.find(glsl_program_num);
229 assert(shader_it != program_shaders.end());
231 instance_program_num = link_program(
232 shader_it->second.vs_obj,
233 shader_it->second.fs_obj,
234 shader_it->second.fragment_shader_outputs);
235 program_masters.insert(make_pair(instance_program_num, glsl_program_num));
237 pthread_mutex_unlock(&lock);
239 glUseProgram(instance_program_num);
240 return instance_program_num;
243 void ResourcePool::unuse_glsl_program(GLuint instance_program_num)
245 pthread_mutex_lock(&lock);
247 map<GLuint, GLuint>::const_iterator master_it = program_masters.find(instance_program_num);
248 assert(master_it != program_masters.end());
250 assert(program_instances.count(master_it->second));
251 stack<GLuint> &instances = program_instances[master_it->second];
253 instances.push(instance_program_num);
255 pthread_mutex_unlock(&lock);
258 GLuint ResourcePool::create_2d_texture(GLint internal_format, GLsizei width, GLsizei height)
263 pthread_mutex_lock(&lock);
264 // See if there's a texture on the freelist we can use.
265 for (list<GLuint>::iterator freelist_it = texture_freelist.begin();
266 freelist_it != texture_freelist.end();
268 GLuint texture_num = *freelist_it;
269 map<GLuint, Texture2D>::const_iterator format_it = texture_formats.find(texture_num);
270 assert(format_it != texture_formats.end());
271 if (format_it->second.internal_format == internal_format &&
272 format_it->second.width == width &&
273 format_it->second.height == height) {
274 texture_freelist_bytes -= estimate_texture_size(format_it->second);
275 texture_freelist.erase(freelist_it);
276 pthread_mutex_unlock(&lock);
281 // Find any reasonable format given the internal format; OpenGL validates it
282 // even though we give NULL as pointer.
284 switch (internal_format) {
290 case GL_SRGB8_ALPHA8:
296 case GL_R11F_G11F_B10F:
317 // TODO: Add more here as needed.
321 // Same with type; GLES is stricter than desktop OpenGL here.
323 switch (internal_format) {
328 case GL_R11F_G11F_B10F:
340 type = GL_UNSIGNED_SHORT;
342 case GL_SRGB8_ALPHA8:
350 type = GL_UNSIGNED_BYTE;
353 type = GL_UNSIGNED_SHORT_5_6_5;
356 // TODO: Add more here as needed.
362 glGenTextures(1, &texture_num);
364 glBindTexture(GL_TEXTURE_2D, texture_num);
366 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, NULL);
368 glBindTexture(GL_TEXTURE_2D, 0);
371 Texture2D texture_format;
372 texture_format.internal_format = internal_format;
373 texture_format.width = width;
374 texture_format.height = height;
375 assert(texture_formats.count(texture_num) == 0);
376 texture_formats.insert(make_pair(texture_num, texture_format));
378 pthread_mutex_unlock(&lock);
382 void ResourcePool::release_2d_texture(GLuint texture_num)
384 pthread_mutex_lock(&lock);
385 texture_freelist.push_front(texture_num);
386 assert(texture_formats.count(texture_num) != 0);
387 texture_freelist_bytes += estimate_texture_size(texture_formats[texture_num]);
389 while (texture_freelist_bytes > texture_freelist_max_bytes) {
390 GLuint free_texture_num = texture_freelist.back();
391 texture_freelist.pop_back();
392 assert(texture_formats.count(free_texture_num) != 0);
393 texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
394 texture_formats.erase(free_texture_num);
395 glDeleteTextures(1, &free_texture_num);
398 // Unlink any lingering FBO related to this texture. We might
399 // not be in the right context, so don't delete it right away;
400 // the cleanup in release_fbo() (which calls cleanup_unlinked_fbos())
401 // will take care of actually doing that later.
402 for (map<pair<void *, GLuint>, FBO>::iterator format_it = fbo_formats.begin();
403 format_it != fbo_formats.end();
405 for (unsigned i = 0; i < num_fbo_attachments; ++i) {
406 if (format_it->second.texture_num[i] == free_texture_num) {
407 format_it->second.texture_num[i] = GL_INVALID_INDEX;
412 pthread_mutex_unlock(&lock);
415 GLuint ResourcePool::create_fbo(GLuint texture0_num, GLuint texture1_num, GLuint texture2_num, GLuint texture3_num)
417 void *context = get_gl_context_identifier();
419 // Make sure we are filled from the bottom.
420 assert(texture0_num != 0);
421 if (texture1_num == 0) {
422 assert(texture2_num == 0);
424 if (texture2_num == 0) {
425 assert(texture3_num == 0);
428 pthread_mutex_lock(&lock);
429 if (fbo_freelist.count(context) != 0) {
430 // See if there's an FBO on the freelist we can use.
431 list<FBOFormatIterator>::iterator end = fbo_freelist[context].end();
432 for (list<FBOFormatIterator>::iterator freelist_it = fbo_freelist[context].begin();
433 freelist_it != end; ++freelist_it) {
434 FBOFormatIterator fbo_it = *freelist_it;
435 if (fbo_it->second.texture_num[0] == texture0_num &&
436 fbo_it->second.texture_num[1] == texture1_num &&
437 fbo_it->second.texture_num[2] == texture2_num &&
438 fbo_it->second.texture_num[3] == texture3_num) {
439 fbo_freelist[context].erase(freelist_it);
440 pthread_mutex_unlock(&lock);
441 return fbo_it->second.fbo_num;
448 fbo_format.texture_num[0] = texture0_num;
449 fbo_format.texture_num[1] = texture1_num;
450 fbo_format.texture_num[2] = texture2_num;
451 fbo_format.texture_num[3] = texture3_num;
453 glGenFramebuffers(1, &fbo_format.fbo_num);
455 glBindFramebuffer(GL_FRAMEBUFFER, fbo_format.fbo_num);
458 GLenum bufs[num_fbo_attachments];
459 unsigned num_active_attachments = 0;
460 for (unsigned i = 0; i < num_fbo_attachments; ++i, ++num_active_attachments) {
461 if (fbo_format.texture_num[i] == 0) {
464 glFramebufferTexture2D(
466 GL_COLOR_ATTACHMENT0 + i,
468 fbo_format.texture_num[i],
471 bufs[i] = GL_COLOR_ATTACHMENT0 + i;
474 glDrawBuffers(num_active_attachments, bufs);
477 GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
478 assert(status == GL_FRAMEBUFFER_COMPLETE);
479 glBindFramebuffer(GL_FRAMEBUFFER, 0);
482 pair<void *, GLuint> key(context, fbo_format.fbo_num);
483 assert(fbo_formats.count(key) == 0);
484 fbo_formats.insert(make_pair(key, fbo_format));
486 pthread_mutex_unlock(&lock);
487 return fbo_format.fbo_num;
490 void ResourcePool::release_fbo(GLuint fbo_num)
492 void *context = get_gl_context_identifier();
494 pthread_mutex_lock(&lock);
495 FBOFormatIterator fbo_it = fbo_formats.find(make_pair(context, fbo_num));
496 assert(fbo_it != fbo_formats.end());
497 fbo_freelist[context].push_front(fbo_it);
499 // Now that we're in this context, free up any FBOs that are connected
500 // to deleted textures (in release_2d_texture).
501 cleanup_unlinked_fbos(context);
503 shrink_fbo_freelist(context, fbo_freelist_max_length);
504 pthread_mutex_unlock(&lock);
507 GLuint ResourcePool::create_vec2_vao(const set<GLint> &attribute_indices, GLuint vbo_num)
509 void *context = get_gl_context_identifier();
511 pthread_mutex_lock(&lock);
512 if (vao_freelist.count(context) != 0) {
513 // See if there's a VAO the freelist we can use.
514 list<VAOFormatIterator>::iterator end = vao_freelist[context].end();
515 for (list<VAOFormatIterator>::iterator freelist_it = vao_freelist[context].begin();
516 freelist_it != end; ++freelist_it) {
517 VAOFormatIterator vao_it = *freelist_it;
518 if (vao_it->second.vbo_num == vbo_num &&
519 vao_it->second.attribute_indices == attribute_indices) {
520 vao_freelist[context].erase(freelist_it);
521 pthread_mutex_unlock(&lock);
522 return vao_it->second.vao_num;
529 vao_format.attribute_indices = attribute_indices;
530 vao_format.vbo_num = vbo_num;
532 glGenVertexArrays(1, &vao_format.vao_num);
534 glBindVertexArray(vao_format.vao_num);
536 glBindBuffer(GL_ARRAY_BUFFER, vbo_num);
539 for (set<GLint>::const_iterator attr_it = attribute_indices.begin(); attr_it != attribute_indices.end(); ++attr_it) {
540 glEnableVertexAttribArray(*attr_it);
542 glVertexAttribPointer(*attr_it, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
546 glBindVertexArray(0);
548 glBindBuffer(GL_ARRAY_BUFFER, 0);
551 pair<void *, GLuint> key(context, vao_format.vao_num);
552 assert(vao_formats.count(key) == 0);
553 vao_formats.insert(make_pair(key, vao_format));
555 pthread_mutex_unlock(&lock);
556 return vao_format.vao_num;
559 void ResourcePool::release_vec2_vao(GLuint vao_num)
561 void *context = get_gl_context_identifier();
563 pthread_mutex_lock(&lock);
564 VAOFormatIterator vao_it = vao_formats.find(make_pair(context, vao_num));
565 assert(vao_it != vao_formats.end());
566 vao_freelist[context].push_front(vao_it);
568 shrink_vao_freelist(context, vao_freelist_max_length);
569 pthread_mutex_unlock(&lock);
572 void ResourcePool::clean_context()
574 void *context = get_gl_context_identifier();
576 // Currently, we only need to worry about FBOs and VAOs, as they are the only
577 // non-shareable resources we hold.
578 shrink_fbo_freelist(context, 0);
579 fbo_freelist.erase(context);
581 shrink_vao_freelist(context, 0);
582 vao_freelist.erase(context);
585 void ResourcePool::cleanup_unlinked_fbos(void *context)
587 list<FBOFormatIterator>::iterator end = fbo_freelist[context].end();
588 for (list<FBOFormatIterator>::iterator freelist_it = fbo_freelist[context].begin(); freelist_it != end; ) {
589 FBOFormatIterator fbo_it = *freelist_it;
591 bool all_unlinked = true;
592 for (unsigned i = 0; i < num_fbo_attachments; ++i) {
593 if (fbo_it->second.texture_num[i] != 0 &&
594 fbo_it->second.texture_num[i] != GL_INVALID_INDEX) {
595 all_unlinked = false;
600 glDeleteFramebuffers(1, &fbo_it->second.fbo_num);
602 fbo_formats.erase(fbo_it);
603 fbo_freelist[context].erase(freelist_it++);
610 void ResourcePool::shrink_fbo_freelist(void *context, size_t max_length)
612 list<FBOFormatIterator> &freelist = fbo_freelist[context];
613 while (freelist.size() > max_length) {
614 FBOFormatIterator free_fbo_it = freelist.back();
615 glDeleteFramebuffers(1, &free_fbo_it->second.fbo_num);
617 fbo_formats.erase(free_fbo_it);
622 void ResourcePool::shrink_vao_freelist(void *context, size_t max_length)
624 list<VAOFormatIterator> &freelist = vao_freelist[context];
625 while (freelist.size() > max_length) {
626 VAOFormatIterator free_vao_it = freelist.back();
627 glDeleteVertexArrays(1, &free_vao_it->second.vao_num);
629 vao_formats.erase(free_vao_it);
634 void ResourcePool::increment_program_refcount(GLuint program_num)
636 map<GLuint, int>::iterator refcount_it = program_refcount.find(program_num);
637 if (refcount_it != program_refcount.end()) {
638 ++refcount_it->second;
640 list<GLuint>::iterator freelist_it =
641 find(program_freelist.begin(), program_freelist.end(), program_num);
642 assert(freelist_it != program_freelist.end());
643 program_freelist.erase(freelist_it);
644 program_refcount.insert(make_pair(program_num, 1));
648 void ResourcePool::output_debug_shader(const string &shader_src, const string &suffix)
650 if (movit_debug_level == MOVIT_DEBUG_ON) {
651 // Output shader to a temporary file, for easier debugging.
652 static int compiled_shader_num = 0;
654 sprintf(filename, "chain-%03d.%s", compiled_shader_num++, suffix.c_str());
655 FILE *fp = fopen(filename, "w");
660 fprintf(fp, "%s\n", shader_src.c_str());
665 void ResourcePool::add_master_program(GLuint program_num)
667 program_refcount.insert(make_pair(program_num, 1));
668 stack<GLuint> instances;
669 instances.push(program_num);
670 program_instances.insert(make_pair(program_num, instances));
671 program_masters.insert(make_pair(program_num, program_num));
674 size_t ResourcePool::estimate_texture_size(const Texture2D &texture_format)
676 size_t bytes_per_pixel;
678 switch (texture_format.internal_format) {
680 bytes_per_pixel = 16;
686 bytes_per_pixel = 12;
691 case GL_R11F_G11F_B10F:
698 case GL_SRGB8_ALPHA8:
741 // TODO: Add more here as needed.
745 return texture_format.width * texture_format.height * bytes_per_pixel;