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