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 glDeleteFramebuffers(1, &free_fbo_num);
64 assert(fbo_formats.empty());
67 void ResourcePool::delete_program(GLuint glsl_program_num)
69 bool found_program = false;
70 for (map<pair<string, string>, GLuint>::iterator program_it = programs.begin();
71 program_it != programs.end();
73 if (program_it->second == glsl_program_num) {
74 programs.erase(program_it);
79 assert(found_program);
80 glDeleteProgram(glsl_program_num);
82 map<GLuint, pair<GLuint, GLuint> >::iterator shader_it =
83 program_shaders.find(glsl_program_num);
84 assert(shader_it != program_shaders.end());
86 glDeleteShader(shader_it->second.first);
87 glDeleteShader(shader_it->second.second);
88 program_shaders.erase(shader_it);
91 GLuint ResourcePool::compile_glsl_program(const string& vertex_shader, const string& fragment_shader)
93 GLuint glsl_program_num;
94 pthread_mutex_lock(&lock);
95 const pair<string, string> key(vertex_shader, fragment_shader);
96 if (programs.count(key)) {
97 // Already in the cache. Increment the refcount, or take it off the freelist
99 glsl_program_num = programs[key];
100 map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
101 if (refcount_it != program_refcount.end()) {
102 ++refcount_it->second;
104 list<GLuint>::iterator freelist_it =
105 find(program_freelist.begin(), program_freelist.end(), glsl_program_num);
106 assert(freelist_it != program_freelist.end());
107 program_freelist.erase(freelist_it);
108 program_refcount.insert(make_pair(glsl_program_num, 1));
111 // Not in the cache. Compile the shaders.
112 glsl_program_num = glCreateProgram();
113 GLuint vs_obj = compile_shader(vertex_shader, GL_VERTEX_SHADER);
114 GLuint fs_obj = compile_shader(fragment_shader, GL_FRAGMENT_SHADER);
115 glAttachShader(glsl_program_num, vs_obj);
117 glAttachShader(glsl_program_num, fs_obj);
119 glLinkProgram(glsl_program_num);
123 glGetProgramiv(glsl_program_num, GL_LINK_STATUS, &success);
124 if (success == GL_FALSE) {
125 GLchar error_log[1024] = {0};
126 glGetProgramInfoLog(glsl_program_num, 1024, NULL, error_log);
127 fprintf(stderr, "Error linking program: %s\n", error_log);
131 if (movit_debug_level == MOVIT_DEBUG_ON) {
132 // Output shader to a temporary file, for easier debugging.
133 static int compiled_shader_num = 0;
135 sprintf(filename, "chain-%03d.frag", compiled_shader_num++);
136 FILE *fp = fopen(filename, "w");
141 fprintf(fp, "%s\n", fragment_shader.c_str());
145 programs.insert(make_pair(key, glsl_program_num));
146 program_refcount.insert(make_pair(glsl_program_num, 1));
147 program_shaders.insert(make_pair(glsl_program_num, make_pair(vs_obj, fs_obj)));
149 pthread_mutex_unlock(&lock);
150 return glsl_program_num;
153 void ResourcePool::release_glsl_program(GLuint glsl_program_num)
155 pthread_mutex_lock(&lock);
156 map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
157 assert(refcount_it != program_refcount.end());
159 if (--refcount_it->second == 0) {
160 program_refcount.erase(refcount_it);
161 assert(find(program_freelist.begin(), program_freelist.end(), glsl_program_num)
162 == program_freelist.end());
163 program_freelist.push_front(glsl_program_num);
164 if (program_freelist.size() > program_freelist_max_length) {
165 delete_program(program_freelist.back());
166 program_freelist.pop_back();
170 pthread_mutex_unlock(&lock);
173 GLuint ResourcePool::create_2d_texture(GLint internal_format, GLsizei width, GLsizei height)
175 pthread_mutex_lock(&lock);
176 // See if there's a texture on the freelist we can use.
177 for (list<GLuint>::iterator freelist_it = texture_freelist.begin();
178 freelist_it != texture_freelist.end();
180 GLuint texture_num = *freelist_it;
181 map<GLuint, Texture2D>::const_iterator format_it = texture_formats.find(texture_num);
182 assert(format_it != texture_formats.end());
183 if (format_it->second.internal_format == internal_format &&
184 format_it->second.width == width &&
185 format_it->second.height == height) {
186 texture_freelist_bytes -= estimate_texture_size(format_it->second);
187 texture_freelist.erase(freelist_it);
188 pthread_mutex_unlock(&lock);
193 // Find any reasonable format given the internal format; OpenGL validates it
194 // even though we give NULL as pointer.
196 switch (internal_format) {
200 case GL_SRGB8_ALPHA8:
217 // TODO: Add more here as needed.
222 glGenTextures(1, &texture_num);
224 glBindTexture(GL_TEXTURE_2D, texture_num);
226 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
228 glBindTexture(GL_TEXTURE_2D, 0);
231 Texture2D texture_format;
232 texture_format.internal_format = internal_format;
233 texture_format.width = width;
234 texture_format.height = height;
235 assert(texture_formats.count(texture_num) == 0);
236 texture_formats.insert(make_pair(texture_num, texture_format));
238 pthread_mutex_unlock(&lock);
242 void ResourcePool::release_2d_texture(GLuint texture_num)
244 pthread_mutex_lock(&lock);
245 texture_freelist.push_front(texture_num);
246 assert(texture_formats.count(texture_num) != 0);
247 texture_freelist_bytes += estimate_texture_size(texture_formats[texture_num]);
249 while (texture_freelist_bytes > texture_freelist_max_bytes) {
250 GLuint free_texture_num = texture_freelist.front();
251 texture_freelist.pop_front();
252 assert(texture_formats.count(free_texture_num) != 0);
253 texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
254 texture_formats.erase(free_texture_num);
255 glDeleteTextures(1, &free_texture_num);
258 // Delete any FBO related to this texture.
259 for (list<GLuint>::iterator fbo_freelist_it = fbo_freelist.begin();
260 fbo_freelist_it != fbo_freelist.end(); ) {
261 GLuint fbo_num = *fbo_freelist_it;
262 map<GLuint, FBO>::const_iterator format_it = fbo_formats.find(fbo_num);
263 assert(format_it != fbo_formats.end());
264 if (format_it->second.texture_num == free_texture_num) {
265 fbo_formats.erase(fbo_num);
266 glDeleteFramebuffers(1, &fbo_num);
267 fbo_freelist.erase(fbo_freelist_it++);
273 pthread_mutex_unlock(&lock);
276 GLuint ResourcePool::create_fbo(void *context, GLuint texture_num)
278 pthread_mutex_lock(&lock);
279 // See if there's an FBO on the freelist we can use.
280 for (list<GLuint>::iterator freelist_it = fbo_freelist.begin();
281 freelist_it != fbo_freelist.end();
283 GLuint fbo_num = *freelist_it;
284 map<GLuint, FBO>::const_iterator format_it = fbo_formats.find(fbo_num);
285 assert(format_it != fbo_formats.end());
286 if (format_it->second.context == context &&
287 format_it->second.texture_num == texture_num) {
288 fbo_freelist.erase(freelist_it);
289 pthread_mutex_unlock(&lock);
296 glGenFramebuffers(1, &fbo_num);
298 glBindFramebuffer(GL_FRAMEBUFFER, fbo_num);
300 glFramebufferTexture2D(
302 GL_COLOR_ATTACHMENT0,
307 GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
308 assert(status == GL_FRAMEBUFFER_COMPLETE);
309 glBindFramebuffer(GL_FRAMEBUFFER, 0);
313 fbo_format.context = context;
314 fbo_format.texture_num = texture_num;
315 assert(fbo_formats.count(fbo_num) == 0);
316 fbo_formats.insert(make_pair(fbo_num, fbo_format));
318 pthread_mutex_unlock(&lock);
322 void ResourcePool::release_fbo(GLuint fbo_num)
324 pthread_mutex_lock(&lock);
325 fbo_freelist.push_front(fbo_num);
326 assert(fbo_formats.count(fbo_num) != 0);
328 while (fbo_freelist.size() > fbo_freelist_max_length) {
329 GLuint free_fbo_num = fbo_freelist.front();
330 fbo_freelist.pop_front();
331 assert(fbo_formats.count(free_fbo_num) != 0);
332 fbo_formats.erase(free_fbo_num);
333 glDeleteFramebuffers(1, &free_fbo_num);
336 pthread_mutex_unlock(&lock);
339 size_t ResourcePool::estimate_texture_size(const Texture2D &texture_format)
341 size_t bytes_per_pixel;
343 switch (texture_format.internal_format) {
345 bytes_per_pixel = 16;
351 case GL_SRGB8_ALPHA8:
355 bytes_per_pixel = 12;
374 // TODO: Add more here as needed.
378 return texture_format.width * texture_format.height * bytes_per_pixel;