]> git.sesse.net Git - movit/blob - util.cpp
Release Movit 1.6.0.
[movit] / util.cpp
1 #include <epoxy/gl.h>
2 #include <assert.h>
3 #include <math.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <locale>
8 #include <sstream>
9 #include <string>
10 #include <Eigen/Core>
11
12 #include "fp16.h"
13 #include "init.h"
14 #include "util.h"
15
16 #if defined(__APPLE__)
17 #include <OpenGL/OpenGL.h>
18 #elif defined(WIN32)
19 #include <epoxy/wgl.h>
20 #else
21 #include <epoxy/glx.h>
22 #include <epoxy/egl.h>
23 #endif
24
25 using namespace std;
26
27 namespace movit {
28
29 extern string *movit_data_directory;
30
31 void hsv2rgb(float h, float s, float v, float *r, float *g, float *b)
32 {
33         if (h < 0.0f) {
34                 h += 2.0f * M_PI;
35         }
36         float c = v * s;
37         float hp = (h * 180.0 / M_PI) / 60.0;
38         float x = c * (1 - fabs(fmod(hp, 2.0f) - 1.0f));
39
40         if (hp >= 0 && hp < 1) {
41                 *r = c;
42                 *g = x;
43                 *b = 0.0f;
44         } else if (hp >= 1 && hp < 2) {
45                 *r = x;
46                 *g = c;
47                 *b = 0.0f;
48         } else if (hp >= 2 && hp < 3) {
49                 *r = 0.0f;
50                 *g = c;
51                 *b = x;
52         } else if (hp >= 3 && hp < 4) {
53                 *r = 0.0f;
54                 *g = x;
55                 *b = c;
56         } else if (hp >= 4 && hp < 5) {
57                 *r = x;
58                 *g = 0.0f;
59                 *b = c;
60         } else {
61                 *r = c;
62                 *g = 0.0f;
63                 *b = x;
64         }
65
66         float m = v - c;
67         *r += m;
68         *g += m;
69         *b += m;
70 }
71
72 void hsv2rgb_normalized(float h, float s, float v, float *r, float *g, float *b)
73 {
74         float ref_r, ref_g, ref_b;
75         hsv2rgb(h, s, v, r, g, b);
76         hsv2rgb(h, 0.0f, v, &ref_r, &ref_g, &ref_b);
77         float lum = 0.2126 * *r + 0.7152 * *g + 0.0722 * *b;
78         float ref_lum = 0.2126 * ref_r + 0.7152 * ref_g + 0.0722 * ref_b;
79         if (lum > 1e-3) {
80                 float fac = ref_lum / lum;
81                 *r *= fac;
82                 *g *= fac;
83                 *b *= fac;
84         }
85 }
86
87 string read_file(const string &filename)
88 {
89         const string full_pathname = *movit_data_directory + "/" + filename;
90
91         FILE *fp = fopen(full_pathname.c_str(), "r");
92         if (fp == nullptr) {
93                 perror(full_pathname.c_str());
94                 exit(1);
95         }
96
97         int ret = fseek(fp, 0, SEEK_END);
98         if (ret == -1) {
99                 perror("fseek(SEEK_END)");
100                 exit(1);
101         }
102
103         int size = ftell(fp);
104
105         ret = fseek(fp, 0, SEEK_SET);
106         if (ret == -1) {
107                 perror("fseek(SEEK_SET)");
108                 exit(1);
109         }
110
111         string str;
112         str.resize(size);
113         ret = fread(&str[0], size, 1, fp);
114         if (ret == -1) {
115                 perror("fread");
116                 exit(1);
117         }
118         if (ret == 0) {
119                 fprintf(stderr, "Short read when trying to read %d bytes from %s\n",
120                         size, full_pathname.c_str());
121                 exit(1);
122         }
123         fclose(fp);
124
125         return str;
126 }
127
128 string read_version_dependent_file(const string &base, const string &extension)
129 {
130         if (movit_shader_model == MOVIT_GLSL_130) {
131                 return read_file(base + ".130." + extension);
132         } else if (movit_shader_model == MOVIT_GLSL_150) {
133                 return read_file(base + ".150." + extension);
134         } else if (movit_shader_model == MOVIT_ESSL_300) {
135                 return read_file(base + ".300es." + extension);
136         } else {
137                 assert(false);
138         }
139 }
140
141 GLuint compile_shader(const string &shader_src, GLenum type)
142 {
143         GLuint obj = glCreateShader(type);
144         const GLchar* source[] = { shader_src.data() };
145         const GLint length[] = { (GLint)shader_src.size() };
146         glShaderSource(obj, 1, source, length);
147         glCompileShader(obj);
148
149         GLchar info_log[4096];
150         GLsizei log_length = sizeof(info_log) - 1;
151         glGetShaderInfoLog(obj, log_length, &log_length, info_log);
152         info_log[log_length] = 0; 
153         if (strlen(info_log) > 0) {
154                 fprintf(stderr, "Shader compile log: %s\n", info_log);
155         }
156
157         GLint status;
158         glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
159         if (status == GL_FALSE) {
160                 // Add some line numbers to easier identify compile errors.
161                 string src_with_lines = "/*   1 */ ";
162                 size_t lineno = 1;
163                 for (char ch : shader_src) {
164                         src_with_lines.push_back(ch);
165                         if (ch == '\n') {
166                                 char buf[32];
167                                 snprintf(buf, sizeof(buf), "/* %3zu */ ", ++lineno);
168                                 src_with_lines += buf;
169                         }
170                 }
171
172                 fprintf(stderr, "Failed to compile shader:\n%s\n", src_with_lines.c_str());
173                 exit(1);
174         }
175
176         return obj;
177 }
178
179 void print_3x3_matrix(const Eigen::Matrix3d& m)
180 {
181         printf("%6.4f %6.4f %6.4f\n", m(0,0), m(0,1), m(0,2));
182         printf("%6.4f %6.4f %6.4f\n", m(1,0), m(1,1), m(1,2));
183         printf("%6.4f %6.4f %6.4f\n", m(2,0), m(2,1), m(2,2));
184         printf("\n");
185 }
186
187 string output_glsl_mat3(const string &name, const Eigen::Matrix3d &m)
188 {
189         // Use stringstream to be independent of the current locale in a thread-safe manner.
190         stringstream ss;
191         ss.imbue(locale("C"));
192         ss.precision(8);
193         ss << scientific;
194         ss << "const mat3 " << name << " = mat3(\n";
195         ss << "    " << m(0,0) << ", " << m(1,0) << ", " << m(2,0) << ",\n";
196         ss << "    " << m(0,1) << ", " << m(1,1) << ", " << m(2,1) << ",\n";
197         ss << "    " << m(0,2) << ", " << m(1,2) << ", " << m(2,2) << ");\n\n";
198         return ss.str();
199 }
200
201 string output_glsl_float(const string &name, float x)
202 {
203         // Use stringstream to be independent of the current locale in a thread-safe manner.
204         stringstream ss;
205         ss.imbue(locale("C"));
206         ss.precision(8);
207         ss << scientific;
208         ss << "const float " << name << " = " << x << ";\n";
209         return ss.str();
210 }
211
212 string output_glsl_vec2(const string &name, float x, float y)
213 {
214         // Use stringstream to be independent of the current locale in a thread-safe manner.
215         stringstream ss;
216         ss.imbue(locale("C"));
217         ss.precision(8);
218         ss << scientific;
219         ss << "const vec2 " << name << " = vec2(" << x << ", " << y << ");\n";
220         return ss.str();
221 }
222
223 string output_glsl_vec3(const string &name, float x, float y, float z)
224 {
225         // Use stringstream to be independent of the current locale in a thread-safe manner.
226         stringstream ss;
227         ss.imbue(locale("C"));
228         ss.precision(8);
229         ss << scientific;
230         ss << "const vec3 " << name << " = vec3(" << x << ", " << y << ", " << z << ");\n";
231         return ss.str();
232 }
233
234 GLuint generate_vbo(GLint size, GLenum type, GLsizeiptr data_size, const GLvoid *data)
235 {
236         GLuint vbo;
237         glGenBuffers(1, &vbo);
238         check_error();
239         glBindBuffer(GL_ARRAY_BUFFER, vbo);
240         check_error();
241         glBufferData(GL_ARRAY_BUFFER, data_size, data, GL_STATIC_DRAW);
242         check_error();
243         glBindBuffer(GL_ARRAY_BUFFER, 0);
244         check_error();
245
246         return vbo;
247 }
248
249 GLuint fill_vertex_attribute(GLuint glsl_program_num, const string &attribute_name, GLint size, GLenum type, GLsizeiptr data_size, const GLvoid *data)
250 {
251         int attrib = glGetAttribLocation(glsl_program_num, attribute_name.c_str());
252         if (attrib == -1) {
253                 return -1;
254         }
255
256         GLuint vbo = generate_vbo(size, type, data_size, data);
257
258         glBindBuffer(GL_ARRAY_BUFFER, vbo);
259         check_error();
260         glEnableVertexAttribArray(attrib);
261         check_error();
262         glVertexAttribPointer(attrib, size, type, GL_FALSE, 0, BUFFER_OFFSET(0));
263         check_error();
264         glBindBuffer(GL_ARRAY_BUFFER, 0);
265         check_error();
266
267         return vbo;
268 }
269
270 void cleanup_vertex_attribute(GLuint glsl_program_num, const string &attribute_name, GLuint vbo)
271 {
272         int attrib = glGetAttribLocation(glsl_program_num, attribute_name.c_str());
273         if (attrib == -1) {
274                 return;
275         }
276
277         glDisableVertexAttribArray(attrib);
278         check_error();
279         glDeleteBuffers(1, &vbo);
280         check_error();
281 }
282
283 unsigned div_round_up(unsigned a, unsigned b)
284 {
285         return (a + b - 1) / b;
286 }
287
288 // Algorithm from http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2.
289 unsigned next_power_of_two(unsigned v)
290 {
291         v--;
292         v |= v >> 1;
293         v |= v >> 2;
294         v |= v >> 4;
295         v |= v >> 8;
296         v |= v >> 16;
297         v++;
298         return v;
299 }
300
301 void *get_gl_context_identifier()
302 {
303 #if defined(__APPLE__)
304         return (void *)CGLGetCurrentContext();
305 #elif defined(WIN32)
306         return (void *)wglGetCurrentContext();
307 #else
308         void *ret = (void *)eglGetCurrentContext();
309         if (ret != nullptr) {
310                 return ret;
311         }
312         return (void *)glXGetCurrentContext();
313 #endif
314 }
315
316 void abort_gl_error(GLenum err, const char *filename, int line)
317 {
318         const char *err_text = "unknown";
319
320         // All errors listed in the glGetError(3G) man page.
321         switch (err) {
322         case GL_NO_ERROR:
323                 err_text = "GL_NO_ERROR";  // Should not happen.
324                 break;
325         case GL_INVALID_ENUM:
326                 err_text = "GL_INVALID_ENUM";
327                 break;
328         case GL_INVALID_VALUE:
329                 err_text = "GL_INVALID_VALUE";
330                 break;
331         case GL_INVALID_OPERATION:
332                 err_text = "GL_INVALID_OPERATION";
333                 break;
334         case GL_INVALID_FRAMEBUFFER_OPERATION:
335                 err_text = "GL_INVALID_FRAMEBUFFER_OPERATION";
336                 break;
337         case GL_OUT_OF_MEMORY:
338                 err_text = "GL_OUT_OF_MEMORY";
339                 break;
340         case GL_STACK_UNDERFLOW:
341                 err_text = "GL_STACK_UNDERFLOW";
342                 break;
343         case GL_STACK_OVERFLOW:
344                 err_text = "GL_STACK_OVERFLOW";
345                 break;
346         }
347         fprintf(stderr, "GL error 0x%x (%s) at %s:%d\n", err, err_text, filename, line);
348         abort();
349 }
350
351 }  // namespace movit