Add support for some of the more esoteric minifloat formats to ResourcePool.
[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 #include <epoxy/gl.h>
10
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         void *context = get_gl_context_identifier();
56         cleanup_unlinked_fbos(context);
57
58         for (map<void *, std::list<FBOFormatIterator> >::iterator context_it = fbo_freelist.begin();
59              context_it != fbo_freelist.end();
60              ++context_it) {
61                 if (context_it->first != context) {
62                         // If this does not hold, the client should have called clean_context() earlier.
63                         assert(context_it->second.empty());
64                         continue;
65                 }
66                 for (list<FBOFormatIterator>::const_iterator freelist_it = context_it->second.begin();
67                      freelist_it != context_it->second.end();
68                      ++freelist_it) {
69                         FBOFormatIterator fbo_it = *freelist_it;
70                         glDeleteFramebuffers(1, &fbo_it->second.fbo_num);
71                         check_error();
72                         fbo_formats.erase(fbo_it);
73                 }
74         }
75
76         assert(fbo_formats.empty());
77 }
78
79 void ResourcePool::delete_program(GLuint glsl_program_num)
80 {
81         bool found_program = false;
82         for (map<pair<string, string>, GLuint>::iterator program_it = programs.begin();
83              program_it != programs.end();
84              ++program_it) {
85                 if (program_it->second == glsl_program_num) {
86                         programs.erase(program_it);
87                         found_program = true;
88                         break;
89                 }
90         }
91         assert(found_program);
92         glDeleteProgram(glsl_program_num);
93
94         map<GLuint, pair<GLuint, GLuint> >::iterator shader_it =
95                 program_shaders.find(glsl_program_num);
96         assert(shader_it != program_shaders.end());
97
98         glDeleteShader(shader_it->second.first);
99         glDeleteShader(shader_it->second.second);
100         program_shaders.erase(shader_it);
101 }
102
103 GLuint ResourcePool::compile_glsl_program(const string& vertex_shader,
104                                           const string& fragment_shader,
105                                           const vector<string>& fragment_shader_outputs)
106 {
107         GLuint glsl_program_num;
108         pthread_mutex_lock(&lock);
109
110         // Augment the fragment shader program text with the outputs, so that they become
111         // part of the key. Also potentially useful for debugging.
112         string fragment_shader_processed = fragment_shader;
113         for (unsigned output_index = 0; output_index < fragment_shader_outputs.size(); ++output_index) {
114                 char buf[256];
115                 snprintf(buf, sizeof(buf), "// Bound output: %s\n", fragment_shader_outputs[output_index].c_str());
116                 fragment_shader_processed += buf;
117         }
118
119         const pair<string, string> key(vertex_shader, fragment_shader_processed);
120         if (programs.count(key)) {
121                 // Already in the cache. Increment the refcount, or take it off the freelist
122                 // if it's zero.
123                 glsl_program_num = programs[key];
124                 map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
125                 if (refcount_it != program_refcount.end()) {
126                         ++refcount_it->second;
127                 } else {
128                         list<GLuint>::iterator freelist_it =
129                                 find(program_freelist.begin(), program_freelist.end(), glsl_program_num);
130                         assert(freelist_it != program_freelist.end());
131                         program_freelist.erase(freelist_it);
132                         program_refcount.insert(make_pair(glsl_program_num, 1));
133                 }
134         } else {
135                 // Not in the cache. Compile the shaders.
136                 glsl_program_num = glCreateProgram();
137                 check_error();
138                 GLuint vs_obj = compile_shader(vertex_shader, GL_VERTEX_SHADER);
139                 check_error();
140                 GLuint fs_obj = compile_shader(fragment_shader_processed, GL_FRAGMENT_SHADER);
141                 check_error();
142                 glAttachShader(glsl_program_num, vs_obj);
143                 check_error();
144                 glAttachShader(glsl_program_num, fs_obj);
145                 check_error();
146
147                 // Bind the outputs, if we have multiple ones.
148                 if (fragment_shader_outputs.size() > 1) {
149                         for (unsigned output_index = 0; output_index < fragment_shader_outputs.size(); ++output_index) {
150                                 glBindFragDataLocation(glsl_program_num, output_index,
151                                                        fragment_shader_outputs[output_index].c_str());
152                         }
153                 }
154
155                 glLinkProgram(glsl_program_num);
156                 check_error();
157
158                 GLint success;
159                 glGetProgramiv(glsl_program_num, GL_LINK_STATUS, &success);
160                 if (success == GL_FALSE) {
161                         GLchar error_log[1024] = {0};
162                         glGetProgramInfoLog(glsl_program_num, 1024, NULL, error_log);
163                         fprintf(stderr, "Error linking program: %s\n", error_log);
164                         exit(1);
165                 }
166
167                 if (movit_debug_level == MOVIT_DEBUG_ON) {
168                         // Output shader to a temporary file, for easier debugging.
169                         static int compiled_shader_num = 0;
170                         char filename[256];
171                         sprintf(filename, "chain-%03d.frag", compiled_shader_num++);
172                         FILE *fp = fopen(filename, "w");
173                         if (fp == NULL) {
174                                 perror(filename);
175                                 exit(1);
176                         }
177                         fprintf(fp, "%s\n", fragment_shader_processed.c_str());
178                         fclose(fp);
179                 }
180
181                 programs.insert(make_pair(key, glsl_program_num));
182                 program_refcount.insert(make_pair(glsl_program_num, 1));
183                 program_shaders.insert(make_pair(glsl_program_num, make_pair(vs_obj, fs_obj)));
184         }
185         pthread_mutex_unlock(&lock);
186         return glsl_program_num;
187 }
188
189 void ResourcePool::release_glsl_program(GLuint glsl_program_num)
190 {
191         pthread_mutex_lock(&lock);
192         map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
193         assert(refcount_it != program_refcount.end());
194
195         if (--refcount_it->second == 0) {
196                 program_refcount.erase(refcount_it);
197                 assert(find(program_freelist.begin(), program_freelist.end(), glsl_program_num)
198                         == program_freelist.end());
199                 program_freelist.push_front(glsl_program_num);
200                 if (program_freelist.size() > program_freelist_max_length) {
201                         delete_program(program_freelist.back());
202                         program_freelist.pop_back();
203                 }
204         }
205
206         pthread_mutex_unlock(&lock);
207 }
208
209 GLuint ResourcePool::create_2d_texture(GLint internal_format, GLsizei width, GLsizei height)
210 {
211         assert(width > 0);
212         assert(height > 0);
213
214         pthread_mutex_lock(&lock);
215         // See if there's a texture on the freelist we can use.
216         for (list<GLuint>::iterator freelist_it = texture_freelist.begin();
217              freelist_it != texture_freelist.end();
218              ++freelist_it) {
219                 GLuint texture_num = *freelist_it;
220                 map<GLuint, Texture2D>::const_iterator format_it = texture_formats.find(texture_num);
221                 assert(format_it != texture_formats.end());
222                 if (format_it->second.internal_format == internal_format &&
223                     format_it->second.width == width &&
224                     format_it->second.height == height) {
225                         texture_freelist_bytes -= estimate_texture_size(format_it->second);
226                         texture_freelist.erase(freelist_it);
227                         pthread_mutex_unlock(&lock);
228                         return texture_num;
229                 }
230         }
231
232         // Find any reasonable format given the internal format; OpenGL validates it
233         // even though we give NULL as pointer.
234         GLenum format;
235         switch (internal_format) {
236         case GL_RGBA32F_ARB:
237         case GL_RGBA16F_ARB:
238         case GL_RGBA8:
239         case GL_SRGB8_ALPHA8:
240                 format = GL_RGBA;
241                 break;
242         case GL_RGB32F:
243         case GL_RGB16F:
244         case GL_R11F_G11F_B10F:
245         case GL_RGB8:
246         case GL_SRGB8:
247         case GL_RGB565:
248         case GL_RGB9_E5:
249                 format = GL_RGB;
250                 break;
251         case GL_RG32F:
252         case GL_RG16F:
253         case GL_RG8:
254                 format = GL_RG;
255                 break;
256         case GL_R32F:
257         case GL_R16F:
258         case GL_R8:
259                 format = GL_RED;
260                 break;
261         default:
262                 // TODO: Add more here as needed.
263                 assert(false);
264         }
265
266         // Same with type; GLES is stricter than desktop OpenGL here.
267         GLenum type;
268         switch (internal_format) {
269         case GL_RGBA32F_ARB:
270         case GL_RGBA16F_ARB:
271         case GL_RGB32F:
272         case GL_RGB16F:
273         case GL_R11F_G11F_B10F:
274         case GL_RGB9_E5:
275         case GL_RG32F:
276         case GL_RG16F:
277         case GL_R32F:
278         case GL_R16F:
279                 type = GL_FLOAT;
280                 break;
281         case GL_SRGB8_ALPHA8:
282         case GL_SRGB8:
283         case GL_RGBA8:
284         case GL_RGB8:
285         case GL_RG8:
286         case GL_R8:
287                 type = GL_UNSIGNED_BYTE;
288                 break;
289         case GL_RGB565:
290                 type = GL_UNSIGNED_SHORT_5_6_5;
291                 break;
292         default:
293                 // TODO: Add more here as needed.
294                 assert(false);
295         }
296
297
298         GLuint texture_num;
299         glGenTextures(1, &texture_num);
300         check_error();
301         glBindTexture(GL_TEXTURE_2D, texture_num);
302         check_error();
303         glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, NULL);
304         check_error();
305         glBindTexture(GL_TEXTURE_2D, 0);
306         check_error();
307
308         Texture2D texture_format;
309         texture_format.internal_format = internal_format;
310         texture_format.width = width;
311         texture_format.height = height;
312         assert(texture_formats.count(texture_num) == 0);
313         texture_formats.insert(make_pair(texture_num, texture_format));
314
315         pthread_mutex_unlock(&lock);
316         return texture_num;
317 }
318
319 void ResourcePool::release_2d_texture(GLuint texture_num)
320 {
321         pthread_mutex_lock(&lock);
322         texture_freelist.push_front(texture_num);
323         assert(texture_formats.count(texture_num) != 0);
324         texture_freelist_bytes += estimate_texture_size(texture_formats[texture_num]);
325
326         while (texture_freelist_bytes > texture_freelist_max_bytes) {
327                 GLuint free_texture_num = texture_freelist.back();
328                 texture_freelist.pop_back();
329                 assert(texture_formats.count(free_texture_num) != 0);
330                 texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
331                 texture_formats.erase(free_texture_num);
332                 glDeleteTextures(1, &free_texture_num);
333                 check_error();
334
335                 // Unlink any lingering FBO related to this texture. We might
336                 // not be in the right context, so don't delete it right away;
337                 // the cleanup in release_fbo() (which calls cleanup_unlinked_fbos())
338                 // will take care of actually doing that later.
339                 for (map<pair<void *, GLuint>, FBO>::iterator format_it = fbo_formats.begin();
340                      format_it != fbo_formats.end();
341                      ++format_it) {
342                         for (unsigned i = 0; i < num_fbo_attachments; ++i) {
343                                 if (format_it->second.texture_num[i] == free_texture_num) {
344                                         format_it->second.texture_num[i] = GL_INVALID_INDEX;
345                                 }
346                         }
347                 }
348         }
349         pthread_mutex_unlock(&lock);
350 }
351
352 GLuint ResourcePool::create_fbo(GLuint texture0_num, GLuint texture1_num, GLuint texture2_num, GLuint texture3_num)
353 {
354         void *context = get_gl_context_identifier();
355
356         // Make sure we are filled from the bottom.
357         assert(texture0_num != 0);
358         if (texture1_num == 0) {
359                 assert(texture2_num == 0);
360         }
361         if (texture2_num == 0) {
362                 assert(texture3_num == 0);
363         }
364
365         pthread_mutex_lock(&lock);
366         if (fbo_freelist.count(context) != 0) {
367                 // See if there's an FBO on the freelist we can use.
368                 list<FBOFormatIterator>::iterator end = fbo_freelist[context].end();
369                 for (list<FBOFormatIterator>::iterator freelist_it = fbo_freelist[context].begin();
370                      freelist_it != end; ++freelist_it) {
371                         FBOFormatIterator fbo_it = *freelist_it;
372                         if (fbo_it->second.texture_num[0] == texture0_num &&
373                             fbo_it->second.texture_num[1] == texture1_num &&
374                             fbo_it->second.texture_num[2] == texture2_num &&
375                             fbo_it->second.texture_num[3] == texture3_num) {
376                                 fbo_freelist[context].erase(freelist_it);
377                                 pthread_mutex_unlock(&lock);
378                                 return fbo_it->second.fbo_num;
379                         }
380                 }
381         }
382
383         // Create a new one.
384         FBO fbo_format;
385         fbo_format.texture_num[0] = texture0_num;
386         fbo_format.texture_num[1] = texture1_num;
387         fbo_format.texture_num[2] = texture2_num;
388         fbo_format.texture_num[3] = texture3_num;
389
390         glGenFramebuffers(1, &fbo_format.fbo_num);
391         check_error();
392         glBindFramebuffer(GL_FRAMEBUFFER, fbo_format.fbo_num);
393         check_error();
394
395         GLenum bufs[num_fbo_attachments];
396         unsigned num_active_attachments = 0;
397         for (unsigned i = 0; i < num_fbo_attachments; ++i, ++num_active_attachments) {
398                 if (fbo_format.texture_num[i] == 0) {
399                         break;
400                 }
401                 glFramebufferTexture2D(
402                         GL_FRAMEBUFFER,
403                         GL_COLOR_ATTACHMENT0 + i,
404                         GL_TEXTURE_2D,
405                         fbo_format.texture_num[i],
406                         0);
407                 check_error();
408                 bufs[i] = GL_COLOR_ATTACHMENT0 + i;
409         }
410
411         glDrawBuffers(num_active_attachments, bufs);
412         check_error();
413
414         GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
415         assert(status == GL_FRAMEBUFFER_COMPLETE);
416         glBindFramebuffer(GL_FRAMEBUFFER, 0);
417         check_error();
418
419         pair<void *, GLuint> key(context, fbo_format.fbo_num);
420         assert(fbo_formats.count(key) == 0);
421         fbo_formats.insert(make_pair(key, fbo_format));
422
423         pthread_mutex_unlock(&lock);
424         return fbo_format.fbo_num;
425 }
426
427 void ResourcePool::release_fbo(GLuint fbo_num)
428 {
429         void *context = get_gl_context_identifier();
430
431         pthread_mutex_lock(&lock);
432         FBOFormatIterator fbo_it = fbo_formats.find(make_pair(context, fbo_num));
433         assert(fbo_it != fbo_formats.end());
434         fbo_freelist[context].push_front(fbo_it);
435
436         // Now that we're in this context, free up any FBOs that are connected
437         // to deleted textures (in release_2d_texture).
438         cleanup_unlinked_fbos(context);
439
440         shrink_fbo_freelist(context, fbo_freelist_max_length);
441         pthread_mutex_unlock(&lock);
442 }
443
444 void ResourcePool::clean_context()
445 {
446         void *context = get_gl_context_identifier();
447
448         // Currently, we only need to worry about FBOs, as they are the only
449         // non-shareable resource we hold.
450         shrink_fbo_freelist(context, 0);
451         fbo_freelist.erase(context);
452 }
453
454 void ResourcePool::cleanup_unlinked_fbos(void *context)
455 {
456         list<FBOFormatIterator>::iterator end = fbo_freelist[context].end();
457         for (list<FBOFormatIterator>::iterator freelist_it = fbo_freelist[context].begin(); freelist_it != end; ) {
458                 FBOFormatIterator fbo_it = *freelist_it;
459
460                 bool all_unlinked = true;
461                 for (unsigned i = 0; i < num_fbo_attachments; ++i) {
462                         if (fbo_it->second.texture_num[i] != 0 &&
463                             fbo_it->second.texture_num[i] != GL_INVALID_INDEX) {
464                                 all_unlinked = false;
465                                 break;
466                         }
467                 }
468                 if (all_unlinked) {
469                         glDeleteFramebuffers(1, &fbo_it->second.fbo_num);
470                         check_error();
471                         fbo_formats.erase(fbo_it);
472                         fbo_freelist[context].erase(freelist_it++);
473                 } else {
474                         freelist_it++;
475                 }
476         }
477 }
478
479 void ResourcePool::shrink_fbo_freelist(void *context, size_t max_length)
480 {
481         list<FBOFormatIterator> &freelist = fbo_freelist[context];
482         while (freelist.size() > max_length) {
483                 FBOFormatIterator free_fbo_it = freelist.back();
484                 glDeleteFramebuffers(1, &free_fbo_it->second.fbo_num);
485                 check_error();
486                 fbo_formats.erase(free_fbo_it);
487                 freelist.pop_back();
488         }
489 }
490
491 size_t ResourcePool::estimate_texture_size(const Texture2D &texture_format)
492 {
493         size_t bytes_per_pixel;
494
495         switch (texture_format.internal_format) {
496         case GL_RGBA32F_ARB:
497                 bytes_per_pixel = 16;
498                 break;
499         case GL_RGBA16F_ARB:
500                 bytes_per_pixel = 8;
501                 break;
502         case GL_RGB32F_ARB:
503                 bytes_per_pixel = 12;
504                 break;
505         case GL_RGB16F_ARB:
506                 bytes_per_pixel = 6;
507                 break;
508         case GL_R11F_G11F_B10F:
509                 bytes_per_pixel = 4;
510                 break;
511         case GL_RGB9_E5:
512                 bytes_per_pixel = 4;
513                 break;
514         case GL_RGBA8:
515         case GL_SRGB8_ALPHA8:
516                 bytes_per_pixel = 4;
517                 break;
518         case GL_RGB8:
519         case GL_SRGB8:
520                 bytes_per_pixel = 3;
521                 break;
522         case GL_RG32F:
523                 bytes_per_pixel = 8;
524                 break;
525         case GL_RG16F:
526                 bytes_per_pixel = 4;
527                 break;
528         case GL_R32F:
529                 bytes_per_pixel = 4;
530                 break;
531         case GL_R16F:
532                 bytes_per_pixel = 2;
533                 break;
534         case GL_RG8:
535                 bytes_per_pixel = 2;
536                 break;
537         case GL_R8:
538                 bytes_per_pixel = 1;
539                 break;
540         case GL_RGB565:
541                 bytes_per_pixel = 2;
542                 break;
543         default:
544                 // TODO: Add more here as needed.
545                 assert(false);
546         }
547
548         return texture_format.width * texture_format.height * bytes_per_pixel;
549 }
550
551 }  // namespace movit