12 #include "resource_pool.h"
17 ResourcePool::ResourcePool(size_t program_freelist_max_length,
18 size_t texture_freelist_max_bytes)
19 : program_freelist_max_length(program_freelist_max_length),
20 texture_freelist_max_bytes(texture_freelist_max_bytes),
21 texture_freelist_bytes(0) {
22 pthread_mutex_init(&lock, NULL);
25 ResourcePool::~ResourcePool()
27 assert(program_refcount.empty());
29 for (list<GLuint>::const_iterator freelist_it = program_freelist.begin();
30 freelist_it != program_freelist.end();
32 delete_program(*freelist_it);
34 assert(programs.empty());
35 assert(program_shaders.empty());
37 for (list<GLuint>::const_iterator freelist_it = texture_freelist.begin();
38 freelist_it != texture_freelist.end();
40 GLuint free_texture_num = *freelist_it;
41 assert(texture_formats.count(free_texture_num) != 0);
42 texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
43 texture_formats.erase(free_texture_num);
44 glDeleteTextures(1, &free_texture_num);
47 assert(texture_formats.empty());
48 assert(texture_freelist_bytes == 0);
51 void ResourcePool::delete_program(GLuint glsl_program_num)
53 bool found_program = false;
54 for (map<pair<string, string>, GLuint>::iterator program_it = programs.begin();
55 program_it != programs.end();
57 if (program_it->second == glsl_program_num) {
58 programs.erase(program_it);
63 assert(found_program);
64 glDeleteProgram(glsl_program_num);
66 map<GLuint, pair<GLuint, GLuint> >::iterator shader_it =
67 program_shaders.find(glsl_program_num);
68 assert(shader_it != program_shaders.end());
70 glDeleteShader(shader_it->second.first);
71 glDeleteShader(shader_it->second.second);
72 program_shaders.erase(shader_it);
75 GLuint ResourcePool::compile_glsl_program(const string& vertex_shader, const string& fragment_shader)
77 GLuint glsl_program_num;
78 pthread_mutex_lock(&lock);
79 const pair<string, string> key(vertex_shader, fragment_shader);
80 if (programs.count(key)) {
81 // Already in the cache. Increment the refcount, or take it off the freelist
83 glsl_program_num = programs[key];
84 map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
85 if (refcount_it != program_refcount.end()) {
86 ++refcount_it->second;
88 list<GLuint>::iterator freelist_it =
89 find(program_freelist.begin(), program_freelist.end(), glsl_program_num);
90 assert(freelist_it != program_freelist.end());
91 program_freelist.erase(freelist_it);
92 program_refcount.insert(make_pair(glsl_program_num, 1));
95 // Not in the cache. Compile the shaders.
96 glsl_program_num = glCreateProgram();
97 GLuint vs_obj = compile_shader(vertex_shader, GL_VERTEX_SHADER);
98 GLuint fs_obj = compile_shader(fragment_shader, GL_FRAGMENT_SHADER);
99 glAttachShader(glsl_program_num, vs_obj);
101 glAttachShader(glsl_program_num, fs_obj);
103 glLinkProgram(glsl_program_num);
106 if (movit_debug_level == MOVIT_DEBUG_ON) {
107 // Output shader to a temporary file, for easier debugging.
108 static int compiled_shader_num = 0;
110 sprintf(filename, "chain-%03d.frag", compiled_shader_num++);
111 FILE *fp = fopen(filename, "w");
116 fprintf(fp, "%s\n", fragment_shader.c_str());
120 programs.insert(make_pair(key, glsl_program_num));
121 program_refcount.insert(make_pair(glsl_program_num, 1));
122 program_shaders.insert(make_pair(glsl_program_num, make_pair(vs_obj, fs_obj)));
124 pthread_mutex_unlock(&lock);
125 return glsl_program_num;
128 void ResourcePool::release_glsl_program(GLuint glsl_program_num)
130 pthread_mutex_lock(&lock);
131 map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
132 assert(refcount_it != program_refcount.end());
134 if (--refcount_it->second == 0) {
135 program_refcount.erase(refcount_it);
136 assert(find(program_freelist.begin(), program_freelist.end(), glsl_program_num)
137 == program_freelist.end());
138 program_freelist.push_front(glsl_program_num);
139 if (program_freelist.size() > program_freelist_max_length) {
140 delete_program(program_freelist.back());
141 program_freelist.pop_back();
145 pthread_mutex_unlock(&lock);
148 GLuint ResourcePool::create_2d_texture(GLint internal_format, GLsizei width, GLsizei height)
150 pthread_mutex_lock(&lock);
151 // See if there's a texture on the freelist we can use.
152 for (list<GLuint>::iterator freelist_it = texture_freelist.begin();
153 freelist_it != texture_freelist.end();
155 GLuint texture_num = *freelist_it;
156 map<GLuint, Texture2D>::const_iterator format_it = texture_formats.find(texture_num);
157 assert(format_it != texture_formats.end());
158 if (format_it->second.internal_format == internal_format &&
159 format_it->second.width == width &&
160 format_it->second.height == height) {
161 texture_freelist_bytes -= estimate_texture_size(format_it->second);
162 texture_freelist.erase(freelist_it);
163 pthread_mutex_unlock(&lock);
168 // Find any reasonable format given the internal format; OpenGL validates it
169 // even though we give NULL as pointer.
171 switch (internal_format) {
175 case GL_SRGB8_ALPHA8:
183 format = GL_LUMINANCE;
186 // TODO: Add more here as needed.
191 glGenTextures(1, &texture_num);
193 glBindTexture(GL_TEXTURE_2D, texture_num);
195 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
197 glBindTexture(GL_TEXTURE_2D, 0);
200 Texture2D texture_format;
201 texture_format.internal_format = internal_format;
202 texture_format.width = width;
203 texture_format.height = height;
204 assert(texture_formats.count(texture_num) == 0);
205 texture_formats.insert(make_pair(texture_num, texture_format));
207 pthread_mutex_unlock(&lock);
211 void ResourcePool::release_2d_texture(GLuint texture_num)
213 pthread_mutex_lock(&lock);
214 texture_freelist.push_front(texture_num);
215 assert(texture_formats.count(texture_num) != 0);
216 texture_freelist_bytes += estimate_texture_size(texture_formats[texture_num]);
218 while (texture_freelist_bytes > texture_freelist_max_bytes) {
219 GLuint free_texture_num = texture_freelist.front();
220 texture_freelist.pop_front();
221 assert(texture_formats.count(free_texture_num) != 0);
222 texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
223 texture_formats.erase(free_texture_num);
224 glDeleteTextures(1, &free_texture_num);
227 pthread_mutex_unlock(&lock);
230 size_t ResourcePool::estimate_texture_size(const Texture2D &texture_format)
232 size_t bytes_per_pixel;
234 switch (texture_format.internal_format) {
236 bytes_per_pixel = 16;
242 case GL_SRGB8_ALPHA8:
255 // TODO: Add more here as needed.
259 return texture_format.width * texture_format.height * bytes_per_pixel;