]> git.sesse.net Git - movit/blob - resource_pool.cpp
Make Phase take other Phases as inputs, not Nodes.
[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                 GLint success;
109                 glGetProgramiv(glsl_program_num, GL_LINK_STATUS, &success);
110                 if (success == GL_FALSE) {
111                         GLchar error_log[1024] = {0};
112                         glGetProgramInfoLog(glsl_program_num, 1024, NULL, error_log);
113                         fprintf(stderr, "Error linking program: %s\n", error_log);
114                         exit(1);
115                 }
116
117                 if (movit_debug_level == MOVIT_DEBUG_ON) {
118                         // Output shader to a temporary file, for easier debugging.
119                         static int compiled_shader_num = 0;
120                         char filename[256];
121                         sprintf(filename, "chain-%03d.frag", compiled_shader_num++);
122                         FILE *fp = fopen(filename, "w");
123                         if (fp == NULL) {
124                                 perror(filename);
125                                 exit(1);
126                         }
127                         fprintf(fp, "%s\n", fragment_shader.c_str());
128                         fclose(fp);
129                 }
130
131                 programs.insert(make_pair(key, glsl_program_num));
132                 program_refcount.insert(make_pair(glsl_program_num, 1));
133                 program_shaders.insert(make_pair(glsl_program_num, make_pair(vs_obj, fs_obj)));
134         }
135         pthread_mutex_unlock(&lock);
136         return glsl_program_num;
137 }
138
139 void ResourcePool::release_glsl_program(GLuint glsl_program_num)
140 {
141         pthread_mutex_lock(&lock);
142         map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
143         assert(refcount_it != program_refcount.end());
144
145         if (--refcount_it->second == 0) {
146                 program_refcount.erase(refcount_it);
147                 assert(find(program_freelist.begin(), program_freelist.end(), glsl_program_num)
148                         == program_freelist.end());
149                 program_freelist.push_front(glsl_program_num);
150                 if (program_freelist.size() > program_freelist_max_length) {
151                         delete_program(program_freelist.back());
152                         program_freelist.pop_back();
153                 }
154         }
155
156         pthread_mutex_unlock(&lock);
157 }
158
159 GLuint ResourcePool::create_2d_texture(GLint internal_format, GLsizei width, GLsizei height)
160 {
161         pthread_mutex_lock(&lock);
162         // See if there's a texture on the freelist we can use.
163         for (list<GLuint>::iterator freelist_it = texture_freelist.begin();
164              freelist_it != texture_freelist.end();
165              ++freelist_it) {
166                 GLuint texture_num = *freelist_it;
167                 map<GLuint, Texture2D>::const_iterator format_it = texture_formats.find(texture_num);
168                 assert(format_it != texture_formats.end());
169                 if (format_it->second.internal_format == internal_format &&
170                     format_it->second.width == width &&
171                     format_it->second.height == height) {
172                         texture_freelist_bytes -= estimate_texture_size(format_it->second);
173                         texture_freelist.erase(freelist_it);
174                         pthread_mutex_unlock(&lock);
175                         return texture_num;
176                 }
177         }
178
179         // Find any reasonable format given the internal format; OpenGL validates it
180         // even though we give NULL as pointer.
181         GLenum format;
182         switch (internal_format) {
183         case GL_RGBA32F_ARB:
184         case GL_RGBA16F_ARB:
185         case GL_RGBA8:
186         case GL_SRGB8_ALPHA8:
187                 format = GL_RGBA;
188                 break;
189         case GL_RG32F:
190         case GL_RG16F:
191                 format = GL_RG;
192                 break;
193         case GL_R8:
194                 format = GL_RED;
195                 break;
196         default:
197                 // TODO: Add more here as needed.
198                 assert(false);
199         }
200
201         GLuint texture_num;
202         glGenTextures(1, &texture_num);
203         check_error();
204         glBindTexture(GL_TEXTURE_2D, texture_num);
205         check_error();
206         glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
207         check_error();
208         glBindTexture(GL_TEXTURE_2D, 0);
209         check_error();
210
211         Texture2D texture_format;
212         texture_format.internal_format = internal_format;
213         texture_format.width = width;
214         texture_format.height = height;
215         assert(texture_formats.count(texture_num) == 0);
216         texture_formats.insert(make_pair(texture_num, texture_format));
217
218         pthread_mutex_unlock(&lock);
219         return texture_num;
220 }
221
222 void ResourcePool::release_2d_texture(GLuint texture_num)
223 {
224         pthread_mutex_lock(&lock);
225         texture_freelist.push_front(texture_num);
226         assert(texture_formats.count(texture_num) != 0);
227         texture_freelist_bytes += estimate_texture_size(texture_formats[texture_num]);
228
229         while (texture_freelist_bytes > texture_freelist_max_bytes) {
230                 GLuint free_texture_num = texture_freelist.front();
231                 texture_freelist.pop_front();
232                 assert(texture_formats.count(free_texture_num) != 0);
233                 texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
234                 texture_formats.erase(free_texture_num);
235                 glDeleteTextures(1, &free_texture_num);
236                 check_error();
237         }
238         pthread_mutex_unlock(&lock);
239 }
240
241 size_t ResourcePool::estimate_texture_size(const Texture2D &texture_format)
242 {
243         size_t bytes_per_pixel;
244
245         switch (texture_format.internal_format) {
246         case GL_RGBA32F_ARB:
247                 bytes_per_pixel = 16;
248                 break;
249         case GL_RGBA16F_ARB:
250                 bytes_per_pixel = 8;
251                 break;
252         case GL_RGBA8:
253         case GL_SRGB8_ALPHA8:
254                 bytes_per_pixel = 4;
255                 break;
256         case GL_RG32F:
257                 bytes_per_pixel = 8;
258                 break;
259         case GL_RG16F:
260                 bytes_per_pixel = 4;
261                 break;
262         case GL_R8:
263                 bytes_per_pixel = 1;
264                 break;
265         default:
266                 // TODO: Add more here as needed.
267                 assert(false);
268         }
269
270         return texture_format.width * texture_format.height * bytes_per_pixel;
271 }
272
273 }  // namespace movit