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