12 #include "resource_pool.h"
19 ResourcePool::ResourcePool(size_t program_freelist_max_length,
20 size_t texture_freelist_max_bytes)
21 : program_freelist_max_length(program_freelist_max_length),
22 texture_freelist_max_bytes(texture_freelist_max_bytes),
23 texture_freelist_bytes(0) {
24 pthread_mutex_init(&lock, NULL);
27 ResourcePool::~ResourcePool()
29 assert(program_refcount.empty());
31 for (list<GLuint>::const_iterator freelist_it = program_freelist.begin();
32 freelist_it != program_freelist.end();
34 delete_program(*freelist_it);
36 assert(programs.empty());
37 assert(program_shaders.empty());
39 for (list<GLuint>::const_iterator freelist_it = texture_freelist.begin();
40 freelist_it != texture_freelist.end();
42 GLuint free_texture_num = *freelist_it;
43 assert(texture_formats.count(free_texture_num) != 0);
44 texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
45 texture_formats.erase(free_texture_num);
46 glDeleteTextures(1, &free_texture_num);
49 assert(texture_formats.empty());
50 assert(texture_freelist_bytes == 0);
53 void ResourcePool::delete_program(GLuint glsl_program_num)
55 bool found_program = false;
56 for (map<pair<string, string>, GLuint>::iterator program_it = programs.begin();
57 program_it != programs.end();
59 if (program_it->second == glsl_program_num) {
60 programs.erase(program_it);
65 assert(found_program);
66 glDeleteProgram(glsl_program_num);
68 map<GLuint, pair<GLuint, GLuint> >::iterator shader_it =
69 program_shaders.find(glsl_program_num);
70 assert(shader_it != program_shaders.end());
72 glDeleteShader(shader_it->second.first);
73 glDeleteShader(shader_it->second.second);
74 program_shaders.erase(shader_it);
77 GLuint ResourcePool::compile_glsl_program(const string& vertex_shader, const string& fragment_shader)
79 GLuint glsl_program_num;
80 pthread_mutex_lock(&lock);
81 const pair<string, string> key(vertex_shader, fragment_shader);
82 if (programs.count(key)) {
83 // Already in the cache. Increment the refcount, or take it off the freelist
85 glsl_program_num = programs[key];
86 map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
87 if (refcount_it != program_refcount.end()) {
88 ++refcount_it->second;
90 list<GLuint>::iterator freelist_it =
91 find(program_freelist.begin(), program_freelist.end(), glsl_program_num);
92 assert(freelist_it != program_freelist.end());
93 program_freelist.erase(freelist_it);
94 program_refcount.insert(make_pair(glsl_program_num, 1));
97 // Not in the cache. Compile the shaders.
98 glsl_program_num = glCreateProgram();
99 GLuint vs_obj = compile_shader(vertex_shader, GL_VERTEX_SHADER);
100 GLuint fs_obj = compile_shader(fragment_shader, GL_FRAGMENT_SHADER);
101 glAttachShader(glsl_program_num, vs_obj);
103 glAttachShader(glsl_program_num, fs_obj);
105 glLinkProgram(glsl_program_num);
109 glGetProgramiv(glsl_program_num, GL_LINK_STATUS, &success);
110 if (success == GL_FALSE) {
111 GLchar error_log[1024] = {0};
112 glGetProgramInfoLog(glsl_program_num, 1024, NULL, error_log);
113 fprintf(stderr, "Error linking program: %s\n", error_log);
117 if (movit_debug_level == MOVIT_DEBUG_ON) {
118 // Output shader to a temporary file, for easier debugging.
119 static int compiled_shader_num = 0;
121 sprintf(filename, "chain-%03d.frag", compiled_shader_num++);
122 FILE *fp = fopen(filename, "w");
127 fprintf(fp, "%s\n", fragment_shader.c_str());
131 programs.insert(make_pair(key, glsl_program_num));
132 program_refcount.insert(make_pair(glsl_program_num, 1));
133 program_shaders.insert(make_pair(glsl_program_num, make_pair(vs_obj, fs_obj)));
135 pthread_mutex_unlock(&lock);
136 return glsl_program_num;
139 void ResourcePool::release_glsl_program(GLuint glsl_program_num)
141 pthread_mutex_lock(&lock);
142 map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
143 assert(refcount_it != program_refcount.end());
145 if (--refcount_it->second == 0) {
146 program_refcount.erase(refcount_it);
147 assert(find(program_freelist.begin(), program_freelist.end(), glsl_program_num)
148 == program_freelist.end());
149 program_freelist.push_front(glsl_program_num);
150 if (program_freelist.size() > program_freelist_max_length) {
151 delete_program(program_freelist.back());
152 program_freelist.pop_back();
156 pthread_mutex_unlock(&lock);
159 GLuint ResourcePool::create_2d_texture(GLint internal_format, GLsizei width, GLsizei height)
161 pthread_mutex_lock(&lock);
162 // See if there's a texture on the freelist we can use.
163 for (list<GLuint>::iterator freelist_it = texture_freelist.begin();
164 freelist_it != texture_freelist.end();
166 GLuint texture_num = *freelist_it;
167 map<GLuint, Texture2D>::const_iterator format_it = texture_formats.find(texture_num);
168 assert(format_it != texture_formats.end());
169 if (format_it->second.internal_format == internal_format &&
170 format_it->second.width == width &&
171 format_it->second.height == height) {
172 texture_freelist_bytes -= estimate_texture_size(format_it->second);
173 texture_freelist.erase(freelist_it);
174 pthread_mutex_unlock(&lock);
179 // Find any reasonable format given the internal format; OpenGL validates it
180 // even though we give NULL as pointer.
182 switch (internal_format) {
186 case GL_SRGB8_ALPHA8:
197 // TODO: Add more here as needed.
202 glGenTextures(1, &texture_num);
204 glBindTexture(GL_TEXTURE_2D, texture_num);
206 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
208 glBindTexture(GL_TEXTURE_2D, 0);
211 Texture2D texture_format;
212 texture_format.internal_format = internal_format;
213 texture_format.width = width;
214 texture_format.height = height;
215 assert(texture_formats.count(texture_num) == 0);
216 texture_formats.insert(make_pair(texture_num, texture_format));
218 pthread_mutex_unlock(&lock);
222 void ResourcePool::release_2d_texture(GLuint texture_num)
224 pthread_mutex_lock(&lock);
225 texture_freelist.push_front(texture_num);
226 assert(texture_formats.count(texture_num) != 0);
227 texture_freelist_bytes += estimate_texture_size(texture_formats[texture_num]);
229 while (texture_freelist_bytes > texture_freelist_max_bytes) {
230 GLuint free_texture_num = texture_freelist.front();
231 texture_freelist.pop_front();
232 assert(texture_formats.count(free_texture_num) != 0);
233 texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
234 texture_formats.erase(free_texture_num);
235 glDeleteTextures(1, &free_texture_num);
238 pthread_mutex_unlock(&lock);
241 size_t ResourcePool::estimate_texture_size(const Texture2D &texture_format)
243 size_t bytes_per_pixel;
245 switch (texture_format.internal_format) {
247 bytes_per_pixel = 16;
253 case GL_SRGB8_ALPHA8:
266 // TODO: Add more here as needed.
270 return texture_format.width * texture_format.height * bytes_per_pixel;