]> git.sesse.net Git - movit/blob - resource_pool.cpp
b074247e8d772bc6f4b00bbb817f1363e15baaf0
[movit] / resource_pool.cpp
1 #include "resource_pool.h"
2
3 #include <stdio.h>
4 #include <pthread.h>
5
6 #include <algorithm>
7 #include <map>
8 #include <string>
9 #include <utility>
10
11 #include "init.h"
12 #include "util.h"
13
14 using namespace std;
15
16 ResourcePool::ResourcePool(size_t program_freelist_max_length,
17                            size_t texture_freelist_max_bytes)
18         : program_freelist_max_length(program_freelist_max_length),
19           texture_freelist_max_bytes(texture_freelist_max_bytes),
20           texture_freelist_bytes(0) {
21         pthread_mutex_init(&lock, NULL);
22 }
23
24 ResourcePool::~ResourcePool()
25 {
26         assert(program_refcount.empty());
27
28         for (list<GLuint>::const_iterator freelist_it = program_freelist.begin();
29              freelist_it != program_freelist.end();
30              ++freelist_it) {
31                 delete_program(*freelist_it);
32         }
33         assert(programs.empty());
34         assert(program_shaders.empty());
35
36         for (list<GLuint>::const_iterator freelist_it = texture_freelist.begin();
37              freelist_it != texture_freelist.end();
38              ++freelist_it) {
39                 GLuint free_texture_num = *freelist_it;
40                 assert(texture_formats.count(free_texture_num) != 0);
41                 texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
42                 texture_formats.erase(free_texture_num);
43                 glDeleteTextures(1, &free_texture_num);
44                 check_error();
45         }
46         assert(texture_formats.empty());
47         assert(texture_freelist_bytes == 0);
48 }
49
50 void ResourcePool::delete_program(GLuint glsl_program_num)
51 {
52         bool found_program = false;
53         for (map<pair<string, string>, GLuint>::iterator program_it = programs.begin();
54              program_it != programs.end();
55              ++program_it) {
56                 if (program_it->second == glsl_program_num) {
57                         programs.erase(program_it);
58                         found_program = true;
59                         break;
60                 }
61         }
62         assert(found_program);
63         glDeleteProgram(glsl_program_num);
64
65         map<GLuint, pair<GLuint, GLuint> >::iterator shader_it =
66                 program_shaders.find(glsl_program_num);
67         assert(shader_it != program_shaders.end());
68
69         glDeleteShader(shader_it->second.first);
70         glDeleteShader(shader_it->second.second);
71         program_shaders.erase(shader_it);
72 }
73
74 GLuint ResourcePool::compile_glsl_program(const string& vertex_shader, const string& fragment_shader)
75 {
76         GLuint glsl_program_num;
77         pthread_mutex_lock(&lock);
78         const pair<string, string> key(vertex_shader, fragment_shader);
79         if (programs.count(key)) {
80                 // Already in the cache. Increment the refcount, or take it off the freelist
81                 // if it's zero.
82                 glsl_program_num = programs[key];
83                 map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
84                 if (refcount_it != program_refcount.end()) {
85                         ++refcount_it->second;
86                 } else {
87                         list<GLuint>::iterator freelist_it =
88                                 find(program_freelist.begin(), program_freelist.end(), glsl_program_num);
89                         assert(freelist_it != program_freelist.end());
90                         program_freelist.erase(freelist_it);
91                         program_refcount.insert(make_pair(glsl_program_num, 1));
92                 }
93         } else {
94                 // Not in the cache. Compile the shaders.
95                 glsl_program_num = glCreateProgram();
96                 GLuint vs_obj = compile_shader(vertex_shader, GL_VERTEX_SHADER);
97                 GLuint fs_obj = compile_shader(fragment_shader, GL_FRAGMENT_SHADER);
98                 glAttachShader(glsl_program_num, vs_obj);
99                 check_error();
100                 glAttachShader(glsl_program_num, fs_obj);
101                 check_error();
102                 glLinkProgram(glsl_program_num);
103                 check_error();
104
105                 if (movit_debug_level == MOVIT_DEBUG_ON) {
106                         // Output shader to a temporary file, for easier debugging.
107                         static int compiled_shader_num = 0;
108                         char filename[256];
109                         sprintf(filename, "chain-%03d.frag", compiled_shader_num++);
110                         FILE *fp = fopen(filename, "w");
111                         if (fp == NULL) {
112                                 perror(filename);
113                                 exit(1);
114                         }
115                         fprintf(fp, "%s\n", fragment_shader.c_str());
116                         fclose(fp);
117                 }
118
119                 programs.insert(make_pair(key, glsl_program_num));
120                 program_refcount.insert(make_pair(glsl_program_num, 1));
121                 program_shaders.insert(make_pair(glsl_program_num, make_pair(vs_obj, fs_obj)));
122         }
123         pthread_mutex_unlock(&lock);
124         return glsl_program_num;
125 }
126
127 void ResourcePool::release_glsl_program(GLuint glsl_program_num)
128 {
129         pthread_mutex_lock(&lock);
130         map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
131         assert(refcount_it != program_refcount.end());
132
133         if (--refcount_it->second == 0) {
134                 program_refcount.erase(refcount_it);
135                 assert(find(program_freelist.begin(), program_freelist.end(), glsl_program_num)
136                         == program_freelist.end());
137                 program_freelist.push_front(glsl_program_num);
138                 if (program_freelist.size() > program_freelist_max_length) {
139                         delete_program(program_freelist.back());
140                         program_freelist.pop_back();
141                 }
142         }
143
144         pthread_mutex_unlock(&lock);
145 }
146
147 GLuint ResourcePool::create_2d_texture(GLint internal_format, GLsizei width, GLsizei height)
148 {
149         pthread_mutex_lock(&lock);
150         // See if there's a texture on the freelist we can use.
151         for (list<GLuint>::iterator freelist_it = texture_freelist.begin();
152              freelist_it != texture_freelist.end();
153              ++freelist_it) {
154                 GLuint texture_num = *freelist_it;
155                 map<GLuint, Texture2D>::const_iterator format_it = texture_formats.find(texture_num);
156                 assert(format_it != texture_formats.end());
157                 if (format_it->second.internal_format == internal_format &&
158                     format_it->second.width == width &&
159                     format_it->second.height == height) {
160                         texture_freelist_bytes -= estimate_texture_size(format_it->second);
161                         texture_freelist.erase(freelist_it);
162                         pthread_mutex_unlock(&lock);
163                         return texture_num;
164                 }
165         }
166
167         // Find any reasonable format given the internal format; OpenGL validates it
168         // even though we give NULL as pointer.
169         GLenum format;
170         switch (internal_format) {
171         case GL_RGBA32F_ARB:
172         case GL_RGBA16F_ARB:
173         case GL_RGBA8:
174         case GL_SRGB8_ALPHA8:
175                 format = GL_RGBA;
176                 break;
177         case GL_RG32F:
178         case GL_RG16F:
179                 format = GL_RG;
180                 break;
181         case GL_LUMINANCE8:
182                 format = GL_LUMINANCE;
183                 break;
184         default:
185                 // TODO: Add more here as needed.
186                 assert(false);
187         }
188
189         GLuint texture_num;
190         glGenTextures(1, &texture_num);
191         check_error();
192         glBindTexture(GL_TEXTURE_2D, texture_num);
193         check_error();
194         glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
195         check_error();
196         glBindTexture(GL_TEXTURE_2D, 0);
197         check_error();
198
199         Texture2D texture_format;
200         texture_format.internal_format = internal_format;
201         texture_format.width = width;
202         texture_format.height = height;
203         assert(texture_formats.count(texture_num) == 0);
204         texture_formats.insert(make_pair(texture_num, texture_format));
205
206         pthread_mutex_unlock(&lock);
207         return texture_num;
208 }
209
210 void ResourcePool::release_2d_texture(GLuint texture_num)
211 {
212         pthread_mutex_lock(&lock);
213         texture_freelist.push_front(texture_num);
214         assert(texture_formats.count(texture_num) != 0);
215         texture_freelist_bytes += estimate_texture_size(texture_formats[texture_num]);
216
217         while (texture_freelist_bytes > texture_freelist_max_bytes) {
218                 GLuint free_texture_num = texture_freelist.front();
219                 texture_freelist.pop_front();
220                 assert(texture_formats.count(free_texture_num) != 0);
221                 texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
222                 texture_formats.erase(free_texture_num);
223                 glDeleteTextures(1, &free_texture_num);
224                 check_error();
225         }
226         pthread_mutex_unlock(&lock);
227 }
228
229 size_t ResourcePool::estimate_texture_size(const Texture2D &texture_format)
230 {
231         size_t bytes_per_pixel;
232
233         switch (texture_format.internal_format) {
234         case GL_RGBA32F_ARB:
235                 bytes_per_pixel = 16;
236                 break;
237         case GL_RGBA16F_ARB:
238                 bytes_per_pixel = 8;
239                 break;
240         case GL_RGBA8:
241         case GL_SRGB8_ALPHA8:
242                 bytes_per_pixel = 4;
243                 break;
244         case GL_RG32F:
245                 bytes_per_pixel = 8;
246                 break;
247         case GL_RG16F:
248                 bytes_per_pixel = 4;
249                 break;
250         case GL_LUMINANCE8:
251                 bytes_per_pixel = 1;
252                 break;
253         default:
254                 // TODO: Add more here as needed.
255                 assert(false);
256         }
257
258         return texture_format.width * texture_format.height * bytes_per_pixel;
259 }