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. Increment the refcount, or take it off the freelist
135 glsl_program_num = programs[key];
136 map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
137 if (refcount_it != program_refcount.end()) {
138 ++refcount_it->second;
140 list<GLuint>::iterator freelist_it =
141 find(program_freelist.begin(), program_freelist.end(), glsl_program_num);
142 assert(freelist_it != program_freelist.end());
143 program_freelist.erase(freelist_it);
144 program_refcount.insert(make_pair(glsl_program_num, 1));
147 // Not in the cache. Compile the shaders.
148 GLuint vs_obj = compile_shader(vertex_shader, GL_VERTEX_SHADER);
150 GLuint fs_obj = compile_shader(fragment_shader_processed, GL_FRAGMENT_SHADER);
152 glsl_program_num = link_program(vs_obj, fs_obj, fragment_shader_outputs);
154 if (movit_debug_level == MOVIT_DEBUG_ON) {
155 // Output shader to a temporary file, for easier debugging.
156 static int compiled_shader_num = 0;
158 sprintf(filename, "chain-%03d.frag", compiled_shader_num++);
159 FILE *fp = fopen(filename, "w");
164 fprintf(fp, "%s\n", fragment_shader_processed.c_str());
168 programs.insert(make_pair(key, glsl_program_num));
169 program_refcount.insert(make_pair(glsl_program_num, 1));
171 spec.vs_obj = vs_obj;
172 spec.fs_obj = fs_obj;
173 spec.fragment_shader_outputs = fragment_shader_outputs;
174 program_shaders.insert(make_pair(glsl_program_num, spec));
175 stack<GLuint> instances;
176 instances.push(glsl_program_num);
177 program_instances.insert(make_pair(glsl_program_num, instances));
178 program_masters.insert(make_pair(glsl_program_num, glsl_program_num));
180 pthread_mutex_unlock(&lock);
181 return glsl_program_num;
184 GLuint ResourcePool::link_program(GLuint vs_obj,
186 const vector<string>& fragment_shader_outputs)
188 GLuint glsl_program_num = glCreateProgram();
190 glAttachShader(glsl_program_num, vs_obj);
192 glAttachShader(glsl_program_num, fs_obj);
195 // Bind the outputs, if we have multiple ones.
196 if (fragment_shader_outputs.size() > 1) {
197 for (unsigned output_index = 0; output_index < fragment_shader_outputs.size(); ++output_index) {
198 glBindFragDataLocation(glsl_program_num, output_index,
199 fragment_shader_outputs[output_index].c_str());
203 glLinkProgram(glsl_program_num);
207 glGetProgramiv(glsl_program_num, GL_LINK_STATUS, &success);
208 if (success == GL_FALSE) {
209 GLchar error_log[1024] = {0};
210 glGetProgramInfoLog(glsl_program_num, 1024, NULL, error_log);
211 fprintf(stderr, "Error linking program: %s\n", error_log);
215 return glsl_program_num;
218 void ResourcePool::release_glsl_program(GLuint glsl_program_num)
220 pthread_mutex_lock(&lock);
221 map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
222 assert(refcount_it != program_refcount.end());
224 if (--refcount_it->second == 0) {
225 program_refcount.erase(refcount_it);
226 assert(find(program_freelist.begin(), program_freelist.end(), glsl_program_num)
227 == program_freelist.end());
228 program_freelist.push_front(glsl_program_num);
229 if (program_freelist.size() > program_freelist_max_length) {
230 delete_program(program_freelist.back());
231 program_freelist.pop_back();
235 pthread_mutex_unlock(&lock);
238 GLuint ResourcePool::use_glsl_program(GLuint glsl_program_num)
240 pthread_mutex_lock(&lock);
241 assert(program_instances.count(glsl_program_num));
242 stack<GLuint> &instances = program_instances[glsl_program_num];
244 GLuint instance_program_num;
245 if (!instances.empty()) {
246 // There's an unused instance of this program; just return it.
247 instance_program_num = instances.top();
250 // We need to clone this program. (unuse_glsl_program()
251 // will later put it onto the list.)
252 map<GLuint, ShaderSpec>::iterator shader_it =
253 program_shaders.find(glsl_program_num);
254 assert(shader_it != program_shaders.end());
256 instance_program_num = link_program(
257 shader_it->second.vs_obj,
258 shader_it->second.fs_obj,
259 shader_it->second.fragment_shader_outputs);
260 program_masters.insert(make_pair(instance_program_num, glsl_program_num));
262 pthread_mutex_unlock(&lock);
264 glUseProgram(instance_program_num);
265 return instance_program_num;
268 void ResourcePool::unuse_glsl_program(GLuint instance_program_num)
270 pthread_mutex_lock(&lock);
272 map<GLuint, GLuint>::const_iterator master_it = program_masters.find(instance_program_num);
273 assert(master_it != program_masters.end());
275 assert(program_instances.count(master_it->second));
276 stack<GLuint> &instances = program_instances[master_it->second];
278 instances.push(instance_program_num);
280 pthread_mutex_unlock(&lock);
283 GLuint ResourcePool::create_2d_texture(GLint internal_format, GLsizei width, GLsizei height)
288 pthread_mutex_lock(&lock);
289 // See if there's a texture on the freelist we can use.
290 for (list<GLuint>::iterator freelist_it = texture_freelist.begin();
291 freelist_it != texture_freelist.end();
293 GLuint texture_num = *freelist_it;
294 map<GLuint, Texture2D>::const_iterator format_it = texture_formats.find(texture_num);
295 assert(format_it != texture_formats.end());
296 if (format_it->second.internal_format == internal_format &&
297 format_it->second.width == width &&
298 format_it->second.height == height) {
299 texture_freelist_bytes -= estimate_texture_size(format_it->second);
300 texture_freelist.erase(freelist_it);
301 pthread_mutex_unlock(&lock);
306 // Find any reasonable format given the internal format; OpenGL validates it
307 // even though we give NULL as pointer.
309 switch (internal_format) {
315 case GL_SRGB8_ALPHA8:
321 case GL_R11F_G11F_B10F:
342 // TODO: Add more here as needed.
346 // Same with type; GLES is stricter than desktop OpenGL here.
348 switch (internal_format) {
353 case GL_R11F_G11F_B10F:
365 type = GL_UNSIGNED_SHORT;
367 case GL_SRGB8_ALPHA8:
375 type = GL_UNSIGNED_BYTE;
378 type = GL_UNSIGNED_SHORT_5_6_5;
381 // TODO: Add more here as needed.
387 glGenTextures(1, &texture_num);
389 glBindTexture(GL_TEXTURE_2D, texture_num);
391 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, NULL);
393 glBindTexture(GL_TEXTURE_2D, 0);
396 Texture2D texture_format;
397 texture_format.internal_format = internal_format;
398 texture_format.width = width;
399 texture_format.height = height;
400 assert(texture_formats.count(texture_num) == 0);
401 texture_formats.insert(make_pair(texture_num, texture_format));
403 pthread_mutex_unlock(&lock);
407 void ResourcePool::release_2d_texture(GLuint texture_num)
409 pthread_mutex_lock(&lock);
410 texture_freelist.push_front(texture_num);
411 assert(texture_formats.count(texture_num) != 0);
412 texture_freelist_bytes += estimate_texture_size(texture_formats[texture_num]);
414 while (texture_freelist_bytes > texture_freelist_max_bytes) {
415 GLuint free_texture_num = texture_freelist.back();
416 texture_freelist.pop_back();
417 assert(texture_formats.count(free_texture_num) != 0);
418 texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
419 texture_formats.erase(free_texture_num);
420 glDeleteTextures(1, &free_texture_num);
423 // Unlink any lingering FBO related to this texture. We might
424 // not be in the right context, so don't delete it right away;
425 // the cleanup in release_fbo() (which calls cleanup_unlinked_fbos())
426 // will take care of actually doing that later.
427 for (map<pair<void *, GLuint>, FBO>::iterator format_it = fbo_formats.begin();
428 format_it != fbo_formats.end();
430 for (unsigned i = 0; i < num_fbo_attachments; ++i) {
431 if (format_it->second.texture_num[i] == free_texture_num) {
432 format_it->second.texture_num[i] = GL_INVALID_INDEX;
437 pthread_mutex_unlock(&lock);
440 GLuint ResourcePool::create_fbo(GLuint texture0_num, GLuint texture1_num, GLuint texture2_num, GLuint texture3_num)
442 void *context = get_gl_context_identifier();
444 // Make sure we are filled from the bottom.
445 assert(texture0_num != 0);
446 if (texture1_num == 0) {
447 assert(texture2_num == 0);
449 if (texture2_num == 0) {
450 assert(texture3_num == 0);
453 pthread_mutex_lock(&lock);
454 if (fbo_freelist.count(context) != 0) {
455 // See if there's an FBO on the freelist we can use.
456 list<FBOFormatIterator>::iterator end = fbo_freelist[context].end();
457 for (list<FBOFormatIterator>::iterator freelist_it = fbo_freelist[context].begin();
458 freelist_it != end; ++freelist_it) {
459 FBOFormatIterator fbo_it = *freelist_it;
460 if (fbo_it->second.texture_num[0] == texture0_num &&
461 fbo_it->second.texture_num[1] == texture1_num &&
462 fbo_it->second.texture_num[2] == texture2_num &&
463 fbo_it->second.texture_num[3] == texture3_num) {
464 fbo_freelist[context].erase(freelist_it);
465 pthread_mutex_unlock(&lock);
466 return fbo_it->second.fbo_num;
473 fbo_format.texture_num[0] = texture0_num;
474 fbo_format.texture_num[1] = texture1_num;
475 fbo_format.texture_num[2] = texture2_num;
476 fbo_format.texture_num[3] = texture3_num;
478 glGenFramebuffers(1, &fbo_format.fbo_num);
480 glBindFramebuffer(GL_FRAMEBUFFER, fbo_format.fbo_num);
483 GLenum bufs[num_fbo_attachments];
484 unsigned num_active_attachments = 0;
485 for (unsigned i = 0; i < num_fbo_attachments; ++i, ++num_active_attachments) {
486 if (fbo_format.texture_num[i] == 0) {
489 glFramebufferTexture2D(
491 GL_COLOR_ATTACHMENT0 + i,
493 fbo_format.texture_num[i],
496 bufs[i] = GL_COLOR_ATTACHMENT0 + i;
499 glDrawBuffers(num_active_attachments, bufs);
502 GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
503 assert(status == GL_FRAMEBUFFER_COMPLETE);
504 glBindFramebuffer(GL_FRAMEBUFFER, 0);
507 pair<void *, GLuint> key(context, fbo_format.fbo_num);
508 assert(fbo_formats.count(key) == 0);
509 fbo_formats.insert(make_pair(key, fbo_format));
511 pthread_mutex_unlock(&lock);
512 return fbo_format.fbo_num;
515 void ResourcePool::release_fbo(GLuint fbo_num)
517 void *context = get_gl_context_identifier();
519 pthread_mutex_lock(&lock);
520 FBOFormatIterator fbo_it = fbo_formats.find(make_pair(context, fbo_num));
521 assert(fbo_it != fbo_formats.end());
522 fbo_freelist[context].push_front(fbo_it);
524 // Now that we're in this context, free up any FBOs that are connected
525 // to deleted textures (in release_2d_texture).
526 cleanup_unlinked_fbos(context);
528 shrink_fbo_freelist(context, fbo_freelist_max_length);
529 pthread_mutex_unlock(&lock);
532 GLuint ResourcePool::create_vec2_vao(const set<GLint> &attribute_indices, GLuint vbo_num)
534 void *context = get_gl_context_identifier();
536 pthread_mutex_lock(&lock);
537 if (vao_freelist.count(context) != 0) {
538 // See if there's a VAO the freelist we can use.
539 list<VAOFormatIterator>::iterator end = vao_freelist[context].end();
540 for (list<VAOFormatIterator>::iterator freelist_it = vao_freelist[context].begin();
541 freelist_it != end; ++freelist_it) {
542 VAOFormatIterator vao_it = *freelist_it;
543 if (vao_it->second.vbo_num == vbo_num &&
544 vao_it->second.attribute_indices == attribute_indices) {
545 vao_freelist[context].erase(freelist_it);
546 pthread_mutex_unlock(&lock);
547 return vao_it->second.vao_num;
554 vao_format.attribute_indices = attribute_indices;
555 vao_format.vbo_num = vbo_num;
557 glGenVertexArrays(1, &vao_format.vao_num);
559 glBindVertexArray(vao_format.vao_num);
561 glBindBuffer(GL_ARRAY_BUFFER, vbo_num);
564 for (set<GLint>::const_iterator attr_it = attribute_indices.begin(); attr_it != attribute_indices.end(); ++attr_it) {
565 glEnableVertexAttribArray(*attr_it);
567 glVertexAttribPointer(*attr_it, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
571 glBindVertexArray(0);
573 glBindBuffer(GL_ARRAY_BUFFER, 0);
576 pair<void *, GLuint> key(context, vao_format.vao_num);
577 assert(vao_formats.count(key) == 0);
578 vao_formats.insert(make_pair(key, vao_format));
580 pthread_mutex_unlock(&lock);
581 return vao_format.vao_num;
584 void ResourcePool::release_vec2_vao(GLuint vao_num)
586 void *context = get_gl_context_identifier();
588 pthread_mutex_lock(&lock);
589 VAOFormatIterator vao_it = vao_formats.find(make_pair(context, vao_num));
590 assert(vao_it != vao_formats.end());
591 vao_freelist[context].push_front(vao_it);
593 shrink_vao_freelist(context, vao_freelist_max_length);
594 pthread_mutex_unlock(&lock);
597 void ResourcePool::clean_context()
599 void *context = get_gl_context_identifier();
601 // Currently, we only need to worry about FBOs and VAOs, as they are the only
602 // non-shareable resources we hold.
603 shrink_fbo_freelist(context, 0);
604 fbo_freelist.erase(context);
606 shrink_vao_freelist(context, 0);
607 vao_freelist.erase(context);
610 void ResourcePool::cleanup_unlinked_fbos(void *context)
612 list<FBOFormatIterator>::iterator end = fbo_freelist[context].end();
613 for (list<FBOFormatIterator>::iterator freelist_it = fbo_freelist[context].begin(); freelist_it != end; ) {
614 FBOFormatIterator fbo_it = *freelist_it;
616 bool all_unlinked = true;
617 for (unsigned i = 0; i < num_fbo_attachments; ++i) {
618 if (fbo_it->second.texture_num[i] != 0 &&
619 fbo_it->second.texture_num[i] != GL_INVALID_INDEX) {
620 all_unlinked = false;
625 glDeleteFramebuffers(1, &fbo_it->second.fbo_num);
627 fbo_formats.erase(fbo_it);
628 fbo_freelist[context].erase(freelist_it++);
635 void ResourcePool::shrink_fbo_freelist(void *context, size_t max_length)
637 list<FBOFormatIterator> &freelist = fbo_freelist[context];
638 while (freelist.size() > max_length) {
639 FBOFormatIterator free_fbo_it = freelist.back();
640 glDeleteFramebuffers(1, &free_fbo_it->second.fbo_num);
642 fbo_formats.erase(free_fbo_it);
647 void ResourcePool::shrink_vao_freelist(void *context, size_t max_length)
649 list<VAOFormatIterator> &freelist = vao_freelist[context];
650 while (freelist.size() > max_length) {
651 VAOFormatIterator free_vao_it = freelist.back();
652 glDeleteVertexArrays(1, &free_vao_it->second.vao_num);
654 vao_formats.erase(free_vao_it);
659 size_t ResourcePool::estimate_texture_size(const Texture2D &texture_format)
661 size_t bytes_per_pixel;
663 switch (texture_format.internal_format) {
665 bytes_per_pixel = 16;
671 bytes_per_pixel = 12;
676 case GL_R11F_G11F_B10F:
683 case GL_SRGB8_ALPHA8:
726 // TODO: Add more here as needed.
730 return texture_format.width * texture_format.height * bytes_per_pixel;