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