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