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 : program_freelist_max_length(program_freelist_max_length),
23 texture_freelist_max_bytes(texture_freelist_max_bytes),
24 fbo_freelist_max_length(fbo_freelist_max_length),
25 texture_freelist_bytes(0)
27 pthread_mutex_init(&lock, NULL);
30 ResourcePool::~ResourcePool()
32 assert(program_refcount.empty());
34 for (list<GLuint>::const_iterator freelist_it = program_freelist.begin();
35 freelist_it != program_freelist.end();
37 delete_program(*freelist_it);
39 assert(programs.empty());
40 assert(program_shaders.empty());
42 for (list<GLuint>::const_iterator freelist_it = texture_freelist.begin();
43 freelist_it != texture_freelist.end();
45 GLuint free_texture_num = *freelist_it;
46 assert(texture_formats.count(free_texture_num) != 0);
47 texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
48 texture_formats.erase(free_texture_num);
49 glDeleteTextures(1, &free_texture_num);
52 assert(texture_formats.empty());
53 assert(texture_freelist_bytes == 0);
55 void *context = get_gl_context_identifier();
56 cleanup_unlinked_fbos(context);
58 for (map<void *, std::list<FBOFormatIterator> >::iterator context_it = fbo_freelist.begin();
59 context_it != fbo_freelist.end();
61 if (context_it->first != context) {
62 // If this does not hold, the client should have called clean_context() earlier.
63 assert(context_it->second.empty());
66 for (list<FBOFormatIterator>::const_iterator freelist_it = context_it->second.begin();
67 freelist_it != context_it->second.end();
69 FBOFormatIterator fbo_it = *freelist_it;
70 glDeleteFramebuffers(1, &fbo_it->second.fbo_num);
72 fbo_formats.erase(fbo_it);
76 assert(fbo_formats.empty());
79 void ResourcePool::delete_program(GLuint glsl_program_num)
81 bool found_program = false;
82 for (map<pair<string, string>, GLuint>::iterator program_it = programs.begin();
83 program_it != programs.end();
85 if (program_it->second == glsl_program_num) {
86 programs.erase(program_it);
91 assert(found_program);
93 map<GLuint, stack<GLuint> >::iterator instance_list_it = program_instances.find(glsl_program_num);
94 assert(instance_list_it != program_instances.end());
96 while (!instance_list_it->second.empty()) {
97 GLuint instance_program_num = instance_list_it->second.top();
98 instance_list_it->second.pop();
99 glDeleteProgram(instance_program_num);
100 program_masters.erase(instance_program_num);
102 program_instances.erase(instance_list_it);
104 map<GLuint, ShaderSpec>::iterator shader_it =
105 program_shaders.find(glsl_program_num);
106 assert(shader_it != program_shaders.end());
108 glDeleteShader(shader_it->second.vs_obj);
109 glDeleteShader(shader_it->second.fs_obj);
110 program_shaders.erase(shader_it);
113 GLuint ResourcePool::compile_glsl_program(const string& vertex_shader,
114 const string& fragment_shader,
115 const vector<string>& fragment_shader_outputs)
117 GLuint glsl_program_num;
118 pthread_mutex_lock(&lock);
120 // Augment the fragment shader program text with the outputs, so that they become
121 // part of the key. Also potentially useful for debugging.
122 string fragment_shader_processed = fragment_shader;
123 for (unsigned output_index = 0; output_index < fragment_shader_outputs.size(); ++output_index) {
125 snprintf(buf, sizeof(buf), "// Bound output: %s\n", fragment_shader_outputs[output_index].c_str());
126 fragment_shader_processed += buf;
129 const pair<string, string> key(vertex_shader, fragment_shader_processed);
130 if (programs.count(key)) {
131 // Already in the cache. Increment the refcount, or take it off the freelist
133 glsl_program_num = programs[key];
134 map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
135 if (refcount_it != program_refcount.end()) {
136 ++refcount_it->second;
138 list<GLuint>::iterator freelist_it =
139 find(program_freelist.begin(), program_freelist.end(), glsl_program_num);
140 assert(freelist_it != program_freelist.end());
141 program_freelist.erase(freelist_it);
142 program_refcount.insert(make_pair(glsl_program_num, 1));
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 if (movit_debug_level == MOVIT_DEBUG_ON) {
153 // Output shader to a temporary file, for easier debugging.
154 static int compiled_shader_num = 0;
156 sprintf(filename, "chain-%03d.frag", compiled_shader_num++);
157 FILE *fp = fopen(filename, "w");
162 fprintf(fp, "%s\n", fragment_shader_processed.c_str());
166 programs.insert(make_pair(key, glsl_program_num));
167 program_refcount.insert(make_pair(glsl_program_num, 1));
169 spec.vs_obj = vs_obj;
170 spec.fs_obj = fs_obj;
171 spec.fragment_shader_outputs = fragment_shader_outputs;
172 program_shaders.insert(make_pair(glsl_program_num, spec));
173 stack<GLuint> instances;
174 instances.push(glsl_program_num);
175 program_instances.insert(make_pair(glsl_program_num, instances));
176 program_masters.insert(make_pair(glsl_program_num, glsl_program_num));
178 pthread_mutex_unlock(&lock);
179 return glsl_program_num;
182 GLuint ResourcePool::link_program(GLuint vs_obj,
184 const vector<string>& fragment_shader_outputs)
186 GLuint glsl_program_num = glCreateProgram();
188 glAttachShader(glsl_program_num, vs_obj);
190 glAttachShader(glsl_program_num, fs_obj);
193 // Bind the outputs, if we have multiple ones.
194 if (fragment_shader_outputs.size() > 1) {
195 for (unsigned output_index = 0; output_index < fragment_shader_outputs.size(); ++output_index) {
196 glBindFragDataLocation(glsl_program_num, output_index,
197 fragment_shader_outputs[output_index].c_str());
201 glLinkProgram(glsl_program_num);
205 glGetProgramiv(glsl_program_num, GL_LINK_STATUS, &success);
206 if (success == GL_FALSE) {
207 GLchar error_log[1024] = {0};
208 glGetProgramInfoLog(glsl_program_num, 1024, NULL, error_log);
209 fprintf(stderr, "Error linking program: %s\n", error_log);
213 return glsl_program_num;
216 void ResourcePool::release_glsl_program(GLuint glsl_program_num)
218 pthread_mutex_lock(&lock);
219 map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
220 assert(refcount_it != program_refcount.end());
222 if (--refcount_it->second == 0) {
223 program_refcount.erase(refcount_it);
224 assert(find(program_freelist.begin(), program_freelist.end(), glsl_program_num)
225 == program_freelist.end());
226 program_freelist.push_front(glsl_program_num);
227 if (program_freelist.size() > program_freelist_max_length) {
228 delete_program(program_freelist.back());
229 program_freelist.pop_back();
233 pthread_mutex_unlock(&lock);
236 GLuint ResourcePool::use_glsl_program(GLuint glsl_program_num)
238 pthread_mutex_lock(&lock);
239 assert(program_instances.count(glsl_program_num));
240 stack<GLuint> &instances = program_instances[glsl_program_num];
242 GLuint instance_program_num;
243 if (!instances.empty()) {
244 // There's an unused instance of this program; just return it.
245 instance_program_num = instances.top();
248 // We need to clone this program. (unuse_glsl_program()
249 // will later put it onto the list.)
250 map<GLuint, ShaderSpec>::iterator shader_it =
251 program_shaders.find(glsl_program_num);
252 assert(shader_it != program_shaders.end());
254 instance_program_num = link_program(
255 shader_it->second.vs_obj,
256 shader_it->second.fs_obj,
257 shader_it->second.fragment_shader_outputs);
258 program_masters.insert(make_pair(instance_program_num, glsl_program_num));
260 pthread_mutex_unlock(&lock);
262 glUseProgram(instance_program_num);
263 return instance_program_num;
266 void ResourcePool::unuse_glsl_program(GLuint instance_program_num)
268 pthread_mutex_lock(&lock);
270 map<GLuint, GLuint>::const_iterator master_it = program_masters.find(instance_program_num);
271 assert(master_it != program_masters.end());
273 assert(program_instances.count(master_it->second));
274 stack<GLuint> &instances = program_instances[master_it->second];
276 instances.push(instance_program_num);
278 pthread_mutex_unlock(&lock);
281 GLuint ResourcePool::create_2d_texture(GLint internal_format, GLsizei width, GLsizei height)
286 pthread_mutex_lock(&lock);
287 // See if there's a texture on the freelist we can use.
288 for (list<GLuint>::iterator freelist_it = texture_freelist.begin();
289 freelist_it != texture_freelist.end();
291 GLuint texture_num = *freelist_it;
292 map<GLuint, Texture2D>::const_iterator format_it = texture_formats.find(texture_num);
293 assert(format_it != texture_formats.end());
294 if (format_it->second.internal_format == internal_format &&
295 format_it->second.width == width &&
296 format_it->second.height == height) {
297 texture_freelist_bytes -= estimate_texture_size(format_it->second);
298 texture_freelist.erase(freelist_it);
299 pthread_mutex_unlock(&lock);
304 // Find any reasonable format given the internal format; OpenGL validates it
305 // even though we give NULL as pointer.
307 switch (internal_format) {
313 case GL_SRGB8_ALPHA8:
319 case GL_R11F_G11F_B10F:
340 // TODO: Add more here as needed.
344 // Same with type; GLES is stricter than desktop OpenGL here.
346 switch (internal_format) {
351 case GL_R11F_G11F_B10F:
363 type = GL_UNSIGNED_SHORT;
365 case GL_SRGB8_ALPHA8:
373 type = GL_UNSIGNED_BYTE;
376 type = GL_UNSIGNED_SHORT_5_6_5;
379 // TODO: Add more here as needed.
385 glGenTextures(1, &texture_num);
387 glBindTexture(GL_TEXTURE_2D, texture_num);
389 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, NULL);
391 glBindTexture(GL_TEXTURE_2D, 0);
394 Texture2D texture_format;
395 texture_format.internal_format = internal_format;
396 texture_format.width = width;
397 texture_format.height = height;
398 assert(texture_formats.count(texture_num) == 0);
399 texture_formats.insert(make_pair(texture_num, texture_format));
401 pthread_mutex_unlock(&lock);
405 void ResourcePool::release_2d_texture(GLuint texture_num)
407 pthread_mutex_lock(&lock);
408 texture_freelist.push_front(texture_num);
409 assert(texture_formats.count(texture_num) != 0);
410 texture_freelist_bytes += estimate_texture_size(texture_formats[texture_num]);
412 while (texture_freelist_bytes > texture_freelist_max_bytes) {
413 GLuint free_texture_num = texture_freelist.back();
414 texture_freelist.pop_back();
415 assert(texture_formats.count(free_texture_num) != 0);
416 texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
417 texture_formats.erase(free_texture_num);
418 glDeleteTextures(1, &free_texture_num);
421 // Unlink any lingering FBO related to this texture. We might
422 // not be in the right context, so don't delete it right away;
423 // the cleanup in release_fbo() (which calls cleanup_unlinked_fbos())
424 // will take care of actually doing that later.
425 for (map<pair<void *, GLuint>, FBO>::iterator format_it = fbo_formats.begin();
426 format_it != fbo_formats.end();
428 for (unsigned i = 0; i < num_fbo_attachments; ++i) {
429 if (format_it->second.texture_num[i] == free_texture_num) {
430 format_it->second.texture_num[i] = GL_INVALID_INDEX;
435 pthread_mutex_unlock(&lock);
438 GLuint ResourcePool::create_fbo(GLuint texture0_num, GLuint texture1_num, GLuint texture2_num, GLuint texture3_num)
440 void *context = get_gl_context_identifier();
442 // Make sure we are filled from the bottom.
443 assert(texture0_num != 0);
444 if (texture1_num == 0) {
445 assert(texture2_num == 0);
447 if (texture2_num == 0) {
448 assert(texture3_num == 0);
451 pthread_mutex_lock(&lock);
452 if (fbo_freelist.count(context) != 0) {
453 // See if there's an FBO on the freelist we can use.
454 list<FBOFormatIterator>::iterator end = fbo_freelist[context].end();
455 for (list<FBOFormatIterator>::iterator freelist_it = fbo_freelist[context].begin();
456 freelist_it != end; ++freelist_it) {
457 FBOFormatIterator fbo_it = *freelist_it;
458 if (fbo_it->second.texture_num[0] == texture0_num &&
459 fbo_it->second.texture_num[1] == texture1_num &&
460 fbo_it->second.texture_num[2] == texture2_num &&
461 fbo_it->second.texture_num[3] == texture3_num) {
462 fbo_freelist[context].erase(freelist_it);
463 pthread_mutex_unlock(&lock);
464 return fbo_it->second.fbo_num;
471 fbo_format.texture_num[0] = texture0_num;
472 fbo_format.texture_num[1] = texture1_num;
473 fbo_format.texture_num[2] = texture2_num;
474 fbo_format.texture_num[3] = texture3_num;
476 glGenFramebuffers(1, &fbo_format.fbo_num);
478 glBindFramebuffer(GL_FRAMEBUFFER, fbo_format.fbo_num);
481 GLenum bufs[num_fbo_attachments];
482 unsigned num_active_attachments = 0;
483 for (unsigned i = 0; i < num_fbo_attachments; ++i, ++num_active_attachments) {
484 if (fbo_format.texture_num[i] == 0) {
487 glFramebufferTexture2D(
489 GL_COLOR_ATTACHMENT0 + i,
491 fbo_format.texture_num[i],
494 bufs[i] = GL_COLOR_ATTACHMENT0 + i;
497 glDrawBuffers(num_active_attachments, bufs);
500 GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
501 assert(status == GL_FRAMEBUFFER_COMPLETE);
502 glBindFramebuffer(GL_FRAMEBUFFER, 0);
505 pair<void *, GLuint> key(context, fbo_format.fbo_num);
506 assert(fbo_formats.count(key) == 0);
507 fbo_formats.insert(make_pair(key, fbo_format));
509 pthread_mutex_unlock(&lock);
510 return fbo_format.fbo_num;
513 void ResourcePool::release_fbo(GLuint fbo_num)
515 void *context = get_gl_context_identifier();
517 pthread_mutex_lock(&lock);
518 FBOFormatIterator fbo_it = fbo_formats.find(make_pair(context, fbo_num));
519 assert(fbo_it != fbo_formats.end());
520 fbo_freelist[context].push_front(fbo_it);
522 // Now that we're in this context, free up any FBOs that are connected
523 // to deleted textures (in release_2d_texture).
524 cleanup_unlinked_fbos(context);
526 shrink_fbo_freelist(context, fbo_freelist_max_length);
527 pthread_mutex_unlock(&lock);
530 void ResourcePool::clean_context()
532 void *context = get_gl_context_identifier();
534 // Currently, we only need to worry about FBOs, as they are the only
535 // non-shareable resource we hold.
536 shrink_fbo_freelist(context, 0);
537 fbo_freelist.erase(context);
540 void ResourcePool::cleanup_unlinked_fbos(void *context)
542 list<FBOFormatIterator>::iterator end = fbo_freelist[context].end();
543 for (list<FBOFormatIterator>::iterator freelist_it = fbo_freelist[context].begin(); freelist_it != end; ) {
544 FBOFormatIterator fbo_it = *freelist_it;
546 bool all_unlinked = true;
547 for (unsigned i = 0; i < num_fbo_attachments; ++i) {
548 if (fbo_it->second.texture_num[i] != 0 &&
549 fbo_it->second.texture_num[i] != GL_INVALID_INDEX) {
550 all_unlinked = false;
555 glDeleteFramebuffers(1, &fbo_it->second.fbo_num);
557 fbo_formats.erase(fbo_it);
558 fbo_freelist[context].erase(freelist_it++);
565 void ResourcePool::shrink_fbo_freelist(void *context, size_t max_length)
567 list<FBOFormatIterator> &freelist = fbo_freelist[context];
568 while (freelist.size() > max_length) {
569 FBOFormatIterator free_fbo_it = freelist.back();
570 glDeleteFramebuffers(1, &free_fbo_it->second.fbo_num);
572 fbo_formats.erase(free_fbo_it);
577 size_t ResourcePool::estimate_texture_size(const Texture2D &texture_format)
579 size_t bytes_per_pixel;
581 switch (texture_format.internal_format) {
583 bytes_per_pixel = 16;
589 bytes_per_pixel = 12;
594 case GL_R11F_G11F_B10F:
601 case GL_SRGB8_ALPHA8:
644 // TODO: Add more here as needed.
648 return texture_format.width * texture_format.height * bytes_per_pixel;