]> git.sesse.net Git - movit/blob - resource_pool.cpp
Add support for 10-bit RGB framebuffers.
[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_RGB10_A2:
240         case GL_SRGB8_ALPHA8:
241                 format = GL_RGBA;
242                 break;
243         case GL_RGB32F:
244         case GL_RGB16F:
245         case GL_R11F_G11F_B10F:
246         case GL_RGB8:
247         case GL_RGB10:
248         case GL_SRGB8:
249         case GL_RGB565:
250         case GL_RGB9_E5:
251                 format = GL_RGB;
252                 break;
253         case GL_RG32F:
254         case GL_RG16F:
255         case GL_RG8:
256                 format = GL_RG;
257                 break;
258         case GL_R32F:
259         case GL_R16F:
260         case GL_R8:
261                 format = GL_RED;
262                 break;
263         default:
264                 // TODO: Add more here as needed.
265                 assert(false);
266         }
267
268         // Same with type; GLES is stricter than desktop OpenGL here.
269         GLenum type;
270         switch (internal_format) {
271         case GL_RGBA32F_ARB:
272         case GL_RGBA16F_ARB:
273         case GL_RGB32F:
274         case GL_RGB16F:
275         case GL_R11F_G11F_B10F:
276         case GL_RGB9_E5:
277         case GL_RG32F:
278         case GL_RG16F:
279         case GL_R32F:
280         case GL_R16F:
281                 type = GL_FLOAT;
282                 break;
283         case GL_SRGB8_ALPHA8:
284         case GL_SRGB8:
285         case GL_RGBA8:
286         case GL_RGB8:
287         case GL_RGB10_A2:
288         case GL_RGB10:
289         case GL_RG8:
290         case GL_R8:
291                 type = GL_UNSIGNED_BYTE;
292                 break;
293         case GL_RGB565:
294                 type = GL_UNSIGNED_SHORT_5_6_5;
295                 break;
296         default:
297                 // TODO: Add more here as needed.
298                 assert(false);
299         }
300
301
302         GLuint texture_num;
303         glGenTextures(1, &texture_num);
304         check_error();
305         glBindTexture(GL_TEXTURE_2D, texture_num);
306         check_error();
307         glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, NULL);
308         check_error();
309         glBindTexture(GL_TEXTURE_2D, 0);
310         check_error();
311
312         Texture2D texture_format;
313         texture_format.internal_format = internal_format;
314         texture_format.width = width;
315         texture_format.height = height;
316         assert(texture_formats.count(texture_num) == 0);
317         texture_formats.insert(make_pair(texture_num, texture_format));
318
319         pthread_mutex_unlock(&lock);
320         return texture_num;
321 }
322
323 void ResourcePool::release_2d_texture(GLuint texture_num)
324 {
325         pthread_mutex_lock(&lock);
326         texture_freelist.push_front(texture_num);
327         assert(texture_formats.count(texture_num) != 0);
328         texture_freelist_bytes += estimate_texture_size(texture_formats[texture_num]);
329
330         while (texture_freelist_bytes > texture_freelist_max_bytes) {
331                 GLuint free_texture_num = texture_freelist.back();
332                 texture_freelist.pop_back();
333                 assert(texture_formats.count(free_texture_num) != 0);
334                 texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
335                 texture_formats.erase(free_texture_num);
336                 glDeleteTextures(1, &free_texture_num);
337                 check_error();
338
339                 // Unlink any lingering FBO related to this texture. We might
340                 // not be in the right context, so don't delete it right away;
341                 // the cleanup in release_fbo() (which calls cleanup_unlinked_fbos())
342                 // will take care of actually doing that later.
343                 for (map<pair<void *, GLuint>, FBO>::iterator format_it = fbo_formats.begin();
344                      format_it != fbo_formats.end();
345                      ++format_it) {
346                         for (unsigned i = 0; i < num_fbo_attachments; ++i) {
347                                 if (format_it->second.texture_num[i] == free_texture_num) {
348                                         format_it->second.texture_num[i] = GL_INVALID_INDEX;
349                                 }
350                         }
351                 }
352         }
353         pthread_mutex_unlock(&lock);
354 }
355
356 GLuint ResourcePool::create_fbo(GLuint texture0_num, GLuint texture1_num, GLuint texture2_num, GLuint texture3_num)
357 {
358         void *context = get_gl_context_identifier();
359
360         // Make sure we are filled from the bottom.
361         assert(texture0_num != 0);
362         if (texture1_num == 0) {
363                 assert(texture2_num == 0);
364         }
365         if (texture2_num == 0) {
366                 assert(texture3_num == 0);
367         }
368
369         pthread_mutex_lock(&lock);
370         if (fbo_freelist.count(context) != 0) {
371                 // See if there's an FBO on the freelist we can use.
372                 list<FBOFormatIterator>::iterator end = fbo_freelist[context].end();
373                 for (list<FBOFormatIterator>::iterator freelist_it = fbo_freelist[context].begin();
374                      freelist_it != end; ++freelist_it) {
375                         FBOFormatIterator fbo_it = *freelist_it;
376                         if (fbo_it->second.texture_num[0] == texture0_num &&
377                             fbo_it->second.texture_num[1] == texture1_num &&
378                             fbo_it->second.texture_num[2] == texture2_num &&
379                             fbo_it->second.texture_num[3] == texture3_num) {
380                                 fbo_freelist[context].erase(freelist_it);
381                                 pthread_mutex_unlock(&lock);
382                                 return fbo_it->second.fbo_num;
383                         }
384                 }
385         }
386
387         // Create a new one.
388         FBO fbo_format;
389         fbo_format.texture_num[0] = texture0_num;
390         fbo_format.texture_num[1] = texture1_num;
391         fbo_format.texture_num[2] = texture2_num;
392         fbo_format.texture_num[3] = texture3_num;
393
394         glGenFramebuffers(1, &fbo_format.fbo_num);
395         check_error();
396         glBindFramebuffer(GL_FRAMEBUFFER, fbo_format.fbo_num);
397         check_error();
398
399         GLenum bufs[num_fbo_attachments];
400         unsigned num_active_attachments = 0;
401         for (unsigned i = 0; i < num_fbo_attachments; ++i, ++num_active_attachments) {
402                 if (fbo_format.texture_num[i] == 0) {
403                         break;
404                 }
405                 glFramebufferTexture2D(
406                         GL_FRAMEBUFFER,
407                         GL_COLOR_ATTACHMENT0 + i,
408                         GL_TEXTURE_2D,
409                         fbo_format.texture_num[i],
410                         0);
411                 check_error();
412                 bufs[i] = GL_COLOR_ATTACHMENT0 + i;
413         }
414
415         glDrawBuffers(num_active_attachments, bufs);
416         check_error();
417
418         GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
419         assert(status == GL_FRAMEBUFFER_COMPLETE);
420         glBindFramebuffer(GL_FRAMEBUFFER, 0);
421         check_error();
422
423         pair<void *, GLuint> key(context, fbo_format.fbo_num);
424         assert(fbo_formats.count(key) == 0);
425         fbo_formats.insert(make_pair(key, fbo_format));
426
427         pthread_mutex_unlock(&lock);
428         return fbo_format.fbo_num;
429 }
430
431 void ResourcePool::release_fbo(GLuint fbo_num)
432 {
433         void *context = get_gl_context_identifier();
434
435         pthread_mutex_lock(&lock);
436         FBOFormatIterator fbo_it = fbo_formats.find(make_pair(context, fbo_num));
437         assert(fbo_it != fbo_formats.end());
438         fbo_freelist[context].push_front(fbo_it);
439
440         // Now that we're in this context, free up any FBOs that are connected
441         // to deleted textures (in release_2d_texture).
442         cleanup_unlinked_fbos(context);
443
444         shrink_fbo_freelist(context, fbo_freelist_max_length);
445         pthread_mutex_unlock(&lock);
446 }
447
448 void ResourcePool::clean_context()
449 {
450         void *context = get_gl_context_identifier();
451
452         // Currently, we only need to worry about FBOs, as they are the only
453         // non-shareable resource we hold.
454         shrink_fbo_freelist(context, 0);
455         fbo_freelist.erase(context);
456 }
457
458 void ResourcePool::cleanup_unlinked_fbos(void *context)
459 {
460         list<FBOFormatIterator>::iterator end = fbo_freelist[context].end();
461         for (list<FBOFormatIterator>::iterator freelist_it = fbo_freelist[context].begin(); freelist_it != end; ) {
462                 FBOFormatIterator fbo_it = *freelist_it;
463
464                 bool all_unlinked = true;
465                 for (unsigned i = 0; i < num_fbo_attachments; ++i) {
466                         if (fbo_it->second.texture_num[i] != 0 &&
467                             fbo_it->second.texture_num[i] != GL_INVALID_INDEX) {
468                                 all_unlinked = false;
469                                 break;
470                         }
471                 }
472                 if (all_unlinked) {
473                         glDeleteFramebuffers(1, &fbo_it->second.fbo_num);
474                         check_error();
475                         fbo_formats.erase(fbo_it);
476                         fbo_freelist[context].erase(freelist_it++);
477                 } else {
478                         freelist_it++;
479                 }
480         }
481 }
482
483 void ResourcePool::shrink_fbo_freelist(void *context, size_t max_length)
484 {
485         list<FBOFormatIterator> &freelist = fbo_freelist[context];
486         while (freelist.size() > max_length) {
487                 FBOFormatIterator free_fbo_it = freelist.back();
488                 glDeleteFramebuffers(1, &free_fbo_it->second.fbo_num);
489                 check_error();
490                 fbo_formats.erase(free_fbo_it);
491                 freelist.pop_back();
492         }
493 }
494
495 size_t ResourcePool::estimate_texture_size(const Texture2D &texture_format)
496 {
497         size_t bytes_per_pixel;
498
499         switch (texture_format.internal_format) {
500         case GL_RGBA32F_ARB:
501                 bytes_per_pixel = 16;
502                 break;
503         case GL_RGBA16F_ARB:
504                 bytes_per_pixel = 8;
505                 break;
506         case GL_RGB32F_ARB:
507                 bytes_per_pixel = 12;
508                 break;
509         case GL_RGB16F_ARB:
510                 bytes_per_pixel = 6;
511                 break;
512         case GL_R11F_G11F_B10F:
513                 bytes_per_pixel = 4;
514                 break;
515         case GL_RGB9_E5:
516                 bytes_per_pixel = 4;
517                 break;
518         case GL_RGBA8:
519         case GL_SRGB8_ALPHA8:
520         case GL_RGB10_A2:
521         case GL_RGB10:
522                 bytes_per_pixel = 4;
523                 break;
524         case GL_RGB8:
525         case GL_SRGB8:
526                 bytes_per_pixel = 3;
527                 break;
528         case GL_RG32F:
529                 bytes_per_pixel = 8;
530                 break;
531         case GL_RG16F:
532                 bytes_per_pixel = 4;
533                 break;
534         case GL_R32F:
535                 bytes_per_pixel = 4;
536                 break;
537         case GL_R16F:
538                 bytes_per_pixel = 2;
539                 break;
540         case GL_RG8:
541                 bytes_per_pixel = 2;
542                 break;
543         case GL_R8:
544                 bytes_per_pixel = 1;
545                 break;
546         case GL_RGB565:
547                 bytes_per_pixel = 2;
548                 break;
549         default:
550                 // TODO: Add more here as needed.
551                 assert(false);
552         }
553
554         return texture_format.width * texture_format.height * bytes_per_pixel;
555 }
556
557 }  // namespace movit