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 for (list<GLuint>::const_iterator freelist_it = fbo_freelist.begin();
56 freelist_it != fbo_freelist.end();
58 GLuint free_fbo_num = *freelist_it;
59 assert(fbo_formats.count(free_fbo_num) != 0);
60 fbo_formats.erase(free_fbo_num);
61 // TODO: We currently leak due to FBO sharability issues.
62 // glDeleteFramebuffers(1, &free_fbo_num);
65 assert(fbo_formats.empty());
68 void ResourcePool::delete_program(GLuint glsl_program_num)
70 bool found_program = false;
71 for (map<pair<string, string>, GLuint>::iterator program_it = programs.begin();
72 program_it != programs.end();
74 if (program_it->second == glsl_program_num) {
75 programs.erase(program_it);
80 assert(found_program);
81 glDeleteProgram(glsl_program_num);
83 map<GLuint, pair<GLuint, GLuint> >::iterator shader_it =
84 program_shaders.find(glsl_program_num);
85 assert(shader_it != program_shaders.end());
87 glDeleteShader(shader_it->second.first);
88 glDeleteShader(shader_it->second.second);
89 program_shaders.erase(shader_it);
92 GLuint ResourcePool::compile_glsl_program(const string& vertex_shader, const string& fragment_shader)
94 GLuint glsl_program_num;
95 pthread_mutex_lock(&lock);
96 const pair<string, string> key(vertex_shader, fragment_shader);
97 if (programs.count(key)) {
98 // Already in the cache. Increment the refcount, or take it off the freelist
100 glsl_program_num = programs[key];
101 map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
102 if (refcount_it != program_refcount.end()) {
103 ++refcount_it->second;
105 list<GLuint>::iterator freelist_it =
106 find(program_freelist.begin(), program_freelist.end(), glsl_program_num);
107 assert(freelist_it != program_freelist.end());
108 program_freelist.erase(freelist_it);
109 program_refcount.insert(make_pair(glsl_program_num, 1));
112 // Not in the cache. Compile the shaders.
113 glsl_program_num = glCreateProgram();
114 GLuint vs_obj = compile_shader(vertex_shader, GL_VERTEX_SHADER);
115 GLuint fs_obj = compile_shader(fragment_shader, GL_FRAGMENT_SHADER);
116 glAttachShader(glsl_program_num, vs_obj);
118 glAttachShader(glsl_program_num, fs_obj);
120 glLinkProgram(glsl_program_num);
124 glGetProgramiv(glsl_program_num, GL_LINK_STATUS, &success);
125 if (success == GL_FALSE) {
126 GLchar error_log[1024] = {0};
127 glGetProgramInfoLog(glsl_program_num, 1024, NULL, error_log);
128 fprintf(stderr, "Error linking program: %s\n", error_log);
132 if (movit_debug_level == MOVIT_DEBUG_ON) {
133 // Output shader to a temporary file, for easier debugging.
134 static int compiled_shader_num = 0;
136 sprintf(filename, "chain-%03d.frag", compiled_shader_num++);
137 FILE *fp = fopen(filename, "w");
142 fprintf(fp, "%s\n", fragment_shader.c_str());
146 programs.insert(make_pair(key, glsl_program_num));
147 program_refcount.insert(make_pair(glsl_program_num, 1));
148 program_shaders.insert(make_pair(glsl_program_num, make_pair(vs_obj, fs_obj)));
150 pthread_mutex_unlock(&lock);
151 return glsl_program_num;
154 void ResourcePool::release_glsl_program(GLuint glsl_program_num)
156 pthread_mutex_lock(&lock);
157 map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
158 assert(refcount_it != program_refcount.end());
160 if (--refcount_it->second == 0) {
161 program_refcount.erase(refcount_it);
162 assert(find(program_freelist.begin(), program_freelist.end(), glsl_program_num)
163 == program_freelist.end());
164 program_freelist.push_front(glsl_program_num);
165 if (program_freelist.size() > program_freelist_max_length) {
166 delete_program(program_freelist.back());
167 program_freelist.pop_back();
171 pthread_mutex_unlock(&lock);
174 GLuint ResourcePool::create_2d_texture(GLint internal_format, GLsizei width, GLsizei height)
176 pthread_mutex_lock(&lock);
177 // See if there's a texture on the freelist we can use.
178 for (list<GLuint>::iterator freelist_it = texture_freelist.begin();
179 freelist_it != texture_freelist.end();
181 GLuint texture_num = *freelist_it;
182 map<GLuint, Texture2D>::const_iterator format_it = texture_formats.find(texture_num);
183 assert(format_it != texture_formats.end());
184 if (format_it->second.internal_format == internal_format &&
185 format_it->second.width == width &&
186 format_it->second.height == height) {
187 texture_freelist_bytes -= estimate_texture_size(format_it->second);
188 texture_freelist.erase(freelist_it);
189 pthread_mutex_unlock(&lock);
194 // Find any reasonable format given the internal format; OpenGL validates it
195 // even though we give NULL as pointer.
197 switch (internal_format) {
201 case GL_SRGB8_ALPHA8:
218 // TODO: Add more here as needed.
223 glGenTextures(1, &texture_num);
225 glBindTexture(GL_TEXTURE_2D, texture_num);
227 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
229 glBindTexture(GL_TEXTURE_2D, 0);
232 Texture2D texture_format;
233 texture_format.internal_format = internal_format;
234 texture_format.width = width;
235 texture_format.height = height;
236 assert(texture_formats.count(texture_num) == 0);
237 texture_formats.insert(make_pair(texture_num, texture_format));
239 pthread_mutex_unlock(&lock);
243 void ResourcePool::release_2d_texture(GLuint texture_num)
245 pthread_mutex_lock(&lock);
246 texture_freelist.push_front(texture_num);
247 assert(texture_formats.count(texture_num) != 0);
248 texture_freelist_bytes += estimate_texture_size(texture_formats[texture_num]);
250 while (texture_freelist_bytes > texture_freelist_max_bytes) {
251 GLuint free_texture_num = texture_freelist.front();
252 texture_freelist.pop_front();
253 assert(texture_formats.count(free_texture_num) != 0);
254 texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
255 texture_formats.erase(free_texture_num);
256 glDeleteTextures(1, &free_texture_num);
259 // Delete any FBO related to this texture.
260 for (list<GLuint>::iterator fbo_freelist_it = fbo_freelist.begin();
261 fbo_freelist_it != fbo_freelist.end(); ) {
262 GLuint fbo_num = *fbo_freelist_it;
263 map<GLuint, FBO>::const_iterator format_it = fbo_formats.find(fbo_num);
264 assert(format_it != fbo_formats.end());
265 if (format_it->second.texture_num == free_texture_num) {
266 fbo_formats.erase(fbo_num);
267 // TODO: We currently leak due to FBO sharability issues.
268 // glDeleteFramebuffers(1, &fbo_num);
269 fbo_freelist.erase(fbo_freelist_it++);
275 pthread_mutex_unlock(&lock);
278 GLuint ResourcePool::create_fbo(void *context, GLuint texture_num)
280 pthread_mutex_lock(&lock);
281 // See if there's an FBO on the freelist we can use.
282 for (list<GLuint>::iterator freelist_it = fbo_freelist.begin();
283 freelist_it != fbo_freelist.end();
285 GLuint fbo_num = *freelist_it;
286 map<GLuint, FBO>::const_iterator format_it = fbo_formats.find(fbo_num);
287 assert(format_it != fbo_formats.end());
288 if (format_it->second.context == context &&
289 format_it->second.texture_num == texture_num) {
290 fbo_freelist.erase(freelist_it);
291 pthread_mutex_unlock(&lock);
298 glGenFramebuffers(1, &fbo_num);
300 glBindFramebuffer(GL_FRAMEBUFFER, fbo_num);
302 glFramebufferTexture2D(
304 GL_COLOR_ATTACHMENT0,
309 GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
310 assert(status == GL_FRAMEBUFFER_COMPLETE);
311 glBindFramebuffer(GL_FRAMEBUFFER, 0);
315 fbo_format.context = context;
316 fbo_format.texture_num = texture_num;
317 assert(fbo_formats.count(fbo_num) == 0);
318 fbo_formats.insert(make_pair(fbo_num, fbo_format));
320 pthread_mutex_unlock(&lock);
324 void ResourcePool::release_fbo(GLuint fbo_num)
326 pthread_mutex_lock(&lock);
327 fbo_freelist.push_front(fbo_num);
328 assert(fbo_formats.count(fbo_num) != 0);
330 while (fbo_freelist.size() > fbo_freelist_max_length) {
331 GLuint free_fbo_num = fbo_freelist.front();
332 fbo_freelist.pop_front();
333 assert(fbo_formats.count(free_fbo_num) != 0);
334 fbo_formats.erase(free_fbo_num);
335 // TODO: We currently leak due to FBO sharability issues.
336 // glDeleteFramebuffers(1, &free_fbo_num);
339 pthread_mutex_unlock(&lock);
342 size_t ResourcePool::estimate_texture_size(const Texture2D &texture_format)
344 size_t bytes_per_pixel;
346 switch (texture_format.internal_format) {
348 bytes_per_pixel = 16;
354 case GL_SRGB8_ALPHA8:
358 bytes_per_pixel = 12;
377 // TODO: Add more here as needed.
381 return texture_format.width * texture_format.height * bytes_per_pixel;