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