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