]> git.sesse.net Git - movit/blob - resource_pool.cpp
Hack around FBO/VAO sharability issues.
[movit] / resource_pool.cpp
1 #include <assert.h>
2 #include <pthread.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <algorithm>
6 #include <map>
7 #include <string>
8 #include <utility>
9
10 #include "glew.h"
11 #include "init.h"
12 #include "resource_pool.h"
13 #include "util.h"
14
15 using namespace std;
16
17 namespace movit {
18
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)
26 {
27         pthread_mutex_init(&lock, NULL);
28 }
29
30 ResourcePool::~ResourcePool()
31 {
32         assert(program_refcount.empty());
33
34         for (list<GLuint>::const_iterator freelist_it = program_freelist.begin();
35              freelist_it != program_freelist.end();
36              ++freelist_it) {
37                 delete_program(*freelist_it);
38         }
39         assert(programs.empty());
40         assert(program_shaders.empty());
41
42         for (list<GLuint>::const_iterator freelist_it = texture_freelist.begin();
43              freelist_it != texture_freelist.end();
44              ++freelist_it) {
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);
50                 check_error();
51         }
52         assert(texture_formats.empty());
53         assert(texture_freelist_bytes == 0);
54
55         for (list<GLuint>::const_iterator freelist_it = fbo_freelist.begin();
56              freelist_it != fbo_freelist.end();
57              ++freelist_it) {
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);
63                 // check_error();
64         }
65         assert(fbo_formats.empty());
66 }
67
68 void ResourcePool::delete_program(GLuint glsl_program_num)
69 {
70         bool found_program = false;
71         for (map<pair<string, string>, GLuint>::iterator program_it = programs.begin();
72              program_it != programs.end();
73              ++program_it) {
74                 if (program_it->second == glsl_program_num) {
75                         programs.erase(program_it);
76                         found_program = true;
77                         break;
78                 }
79         }
80         assert(found_program);
81         glDeleteProgram(glsl_program_num);
82
83         map<GLuint, pair<GLuint, GLuint> >::iterator shader_it =
84                 program_shaders.find(glsl_program_num);
85         assert(shader_it != program_shaders.end());
86
87         glDeleteShader(shader_it->second.first);
88         glDeleteShader(shader_it->second.second);
89         program_shaders.erase(shader_it);
90 }
91
92 GLuint ResourcePool::compile_glsl_program(const string& vertex_shader, const string& fragment_shader)
93 {
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
99                 // if it's zero.
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;
104                 } else {
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));
110                 }
111         } else {
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);
117                 check_error();
118                 glAttachShader(glsl_program_num, fs_obj);
119                 check_error();
120                 glLinkProgram(glsl_program_num);
121                 check_error();
122
123                 GLint success;
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);
129                         exit(1);
130                 }
131
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;
135                         char filename[256];
136                         sprintf(filename, "chain-%03d.frag", compiled_shader_num++);
137                         FILE *fp = fopen(filename, "w");
138                         if (fp == NULL) {
139                                 perror(filename);
140                                 exit(1);
141                         }
142                         fprintf(fp, "%s\n", fragment_shader.c_str());
143                         fclose(fp);
144                 }
145
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)));
149         }
150         pthread_mutex_unlock(&lock);
151         return glsl_program_num;
152 }
153
154 void ResourcePool::release_glsl_program(GLuint glsl_program_num)
155 {
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());
159
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();
168                 }
169         }
170
171         pthread_mutex_unlock(&lock);
172 }
173
174 GLuint ResourcePool::create_2d_texture(GLint internal_format, GLsizei width, GLsizei height)
175 {
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();
180              ++freelist_it) {
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);
190                         return texture_num;
191                 }
192         }
193
194         // Find any reasonable format given the internal format; OpenGL validates it
195         // even though we give NULL as pointer.
196         GLenum format;
197         switch (internal_format) {
198         case GL_RGBA32F_ARB:
199         case GL_RGBA16F_ARB:
200         case GL_RGBA8:
201         case GL_SRGB8_ALPHA8:
202                 format = GL_RGBA;
203                 break;
204         case GL_RGB32F_ARB:
205         case GL_RGB16F_ARB:
206         case GL_RGB8:
207         case GL_SRGB8:
208                 format = GL_RGB;
209                 break;
210         case GL_RG32F:
211         case GL_RG16F:
212                 format = GL_RG;
213                 break;
214         case GL_R8:
215                 format = GL_RED;
216                 break;
217         default:
218                 // TODO: Add more here as needed.
219                 assert(false);
220         }
221
222         GLuint texture_num;
223         glGenTextures(1, &texture_num);
224         check_error();
225         glBindTexture(GL_TEXTURE_2D, texture_num);
226         check_error();
227         glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
228         check_error();
229         glBindTexture(GL_TEXTURE_2D, 0);
230         check_error();
231
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));
238
239         pthread_mutex_unlock(&lock);
240         return texture_num;
241 }
242
243 void ResourcePool::release_2d_texture(GLuint texture_num)
244 {
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]);
249
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);
257                 check_error();
258
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++);
270                         } else {
271                                 ++fbo_freelist_it;
272                         }
273                 }
274         }
275         pthread_mutex_unlock(&lock);
276 }
277
278 GLuint ResourcePool::create_fbo(void *context, GLuint texture_num)
279 {
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();
284              ++freelist_it) {
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);
292                         return fbo_num;
293                 }
294         }
295
296         // Create a new one.
297         GLuint fbo_num;
298         glGenFramebuffers(1, &fbo_num);
299         check_error();
300         glBindFramebuffer(GL_FRAMEBUFFER, fbo_num);
301         check_error();
302         glFramebufferTexture2D(
303                 GL_FRAMEBUFFER,
304                 GL_COLOR_ATTACHMENT0,
305                 GL_TEXTURE_2D,
306                 texture_num,
307                 0);
308         check_error();
309         GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
310         assert(status == GL_FRAMEBUFFER_COMPLETE);
311         glBindFramebuffer(GL_FRAMEBUFFER, 0);
312         check_error();
313
314         FBO fbo_format;
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));
319
320         pthread_mutex_unlock(&lock);
321         return fbo_num;
322 }
323
324 void ResourcePool::release_fbo(GLuint fbo_num)
325 {
326         pthread_mutex_lock(&lock);
327         fbo_freelist.push_front(fbo_num);
328         assert(fbo_formats.count(fbo_num) != 0);
329
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);
337                 // check_error();
338         }
339         pthread_mutex_unlock(&lock);
340 }
341
342 size_t ResourcePool::estimate_texture_size(const Texture2D &texture_format)
343 {
344         size_t bytes_per_pixel;
345
346         switch (texture_format.internal_format) {
347         case GL_RGBA32F_ARB:
348                 bytes_per_pixel = 16;
349                 break;
350         case GL_RGBA16F_ARB:
351                 bytes_per_pixel = 8;
352                 break;
353         case GL_RGBA8:
354         case GL_SRGB8_ALPHA8:
355                 bytes_per_pixel = 4;
356                 break;
357         case GL_RGB32F_ARB:
358                 bytes_per_pixel = 12;
359                 break;
360         case GL_RGB16F_ARB:
361                 bytes_per_pixel = 6;
362                 break;
363         case GL_RGB8:
364         case GL_SRGB8:
365                 bytes_per_pixel = 3;
366                 break;
367         case GL_RG32F:
368                 bytes_per_pixel = 8;
369                 break;
370         case GL_RG16F:
371                 bytes_per_pixel = 4;
372                 break;
373         case GL_R8:
374                 bytes_per_pixel = 1;
375                 break;
376         default:
377                 // TODO: Add more here as needed.
378                 assert(false);
379         }
380
381         return texture_format.width * texture_format.height * bytes_per_pixel;
382 }
383
384 }  // namespace movit