]> git.sesse.net Git - movit/blob - util.cpp
Merge branch 'master' into epoxy
[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 <Eigen/Core>
8
9 #include "init.h"
10 #include "util.h"
11
12 #if defined(__DARWIN__)
13 #include <OpenGL/OpenGL.h>
14 #elif defined(WIN32)
15 #include <epoxy/wgl.h>
16 #else
17 #include <epoxy/glx.h>
18 #endif
19
20 using namespace std;
21
22 namespace movit {
23
24 extern string *movit_data_directory;
25
26 void hsv2rgb(float h, float s, float v, float *r, float *g, float *b)
27 {
28         if (h < 0.0f) {
29                 h += 2.0f * M_PI;
30         }
31         float c = v * s;
32         float hp = (h * 180.0 / M_PI) / 60.0;
33         float x = c * (1 - fabs(fmod(hp, 2.0f) - 1.0f));
34
35         if (hp >= 0 && hp < 1) {
36                 *r = c;
37                 *g = x;
38                 *b = 0.0f;
39         } else if (hp >= 1 && hp < 2) {
40                 *r = x;
41                 *g = c;
42                 *b = 0.0f;
43         } else if (hp >= 2 && hp < 3) {
44                 *r = 0.0f;
45                 *g = c;
46                 *b = x;
47         } else if (hp >= 3 && hp < 4) {
48                 *r = 0.0f;
49                 *g = x;
50                 *b = c;
51         } else if (hp >= 4 && hp < 5) {
52                 *r = x;
53                 *g = 0.0f;
54                 *b = c;
55         } else {
56                 *r = c;
57                 *g = 0.0f;
58                 *b = x;
59         }
60
61         float m = v - c;
62         *r += m;
63         *g += m;
64         *b += m;
65 }
66
67 void hsv2rgb_normalized(float h, float s, float v, float *r, float *g, float *b)
68 {
69         float ref_r, ref_g, ref_b;
70         hsv2rgb(h, s, v, r, g, b);
71         hsv2rgb(h, 0.0f, v, &ref_r, &ref_g, &ref_b);
72         float lum = 0.2126 * *r + 0.7152 * *g + 0.0722 * *b;
73         float ref_lum = 0.2126 * ref_r + 0.7152 * ref_g + 0.0722 * ref_b;
74         if (lum > 1e-3) {
75                 float fac = ref_lum / lum;
76                 *r *= fac;
77                 *g *= fac;
78                 *b *= fac;
79         }
80 }
81
82 string read_file(const string &filename)
83 {
84         const string full_pathname = *movit_data_directory + "/" + filename;
85
86         static char buf[131072];
87         FILE *fp = fopen(full_pathname.c_str(), "r");
88         if (fp == NULL) {
89                 perror(full_pathname.c_str());
90                 exit(1);
91         }
92
93         int len = fread(buf, 1, sizeof(buf), fp);
94         fclose(fp);
95
96         return string(buf, len);
97 }
98
99 string read_version_dependent_file(const string &base, const string &extension)
100 {
101         if (movit_shader_model == MOVIT_GLSL_110) {
102                 return read_file(base + "." + extension);
103         } else if (movit_shader_model == MOVIT_GLSL_130) {
104                 return read_file(base + ".130." + extension);
105         } else if (movit_shader_model == MOVIT_ESSL_300) {
106                 return read_file(base + ".300es." + extension);
107         } else {
108                 assert(false);
109         }
110 }
111
112 GLuint compile_shader(const string &shader_src, GLenum type)
113 {
114         GLuint obj = glCreateShader(type);
115         const GLchar* source[] = { shader_src.data() };
116         const GLint length[] = { (GLint)shader_src.size() };
117         glShaderSource(obj, 1, source, length);
118         glCompileShader(obj);
119
120         GLchar info_log[4096];
121         GLsizei log_length = sizeof(info_log) - 1;
122         glGetShaderInfoLog(obj, log_length, &log_length, info_log);
123         info_log[log_length] = 0; 
124         if (strlen(info_log) > 0) {
125                 fprintf(stderr, "Shader compile log: %s\n", info_log);
126         }
127
128         GLint status;
129         glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
130         if (status == GL_FALSE) {
131                 exit(1);
132         }
133
134         return obj;
135 }
136
137 void print_3x3_matrix(const Eigen::Matrix3d& m)
138 {
139         printf("%6.4f %6.4f %6.4f\n", m(0,0), m(0,1), m(0,2));
140         printf("%6.4f %6.4f %6.4f\n", m(1,0), m(1,1), m(1,2));
141         printf("%6.4f %6.4f %6.4f\n", m(2,0), m(2,1), m(2,2));
142         printf("\n");
143 }
144
145 string output_glsl_mat3(const string &name, const Eigen::Matrix3d &m)
146 {
147         char buf[1024];
148         sprintf(buf,
149                 "const mat3 %s = mat3(\n"
150                 "    %.8f, %.8f, %.8f,\n"
151                 "    %.8f, %.8f, %.8f,\n"
152                 "    %.8f, %.8f, %.8f);\n\n",
153                 name.c_str(),
154                 m(0,0), m(1,0), m(2,0),
155                 m(0,1), m(1,1), m(2,1),
156                 m(0,2), m(1,2), m(2,2));
157         return buf;
158 }
159
160 void combine_two_samples(float w1, float w2, float *offset, float *total_weight, float *sum_sq_error)
161 {
162         assert(movit_initialized);
163         assert(w1 * w2 >= 0.0f);  // Should not have differing signs.
164         float z;  // Just a shorter name for offset.
165         if (fabs(w1 + w2) < 1e-6) {
166                 z = 0.5f;
167         } else {
168                 z = w2 / (w1 + w2);
169         }
170
171         // Round to the minimum number of bits we have measured earlier.
172         // The card will do this for us anyway, but if we know what the real z
173         // is, we can pick a better total_weight below.
174         z = lrintf(z / movit_texel_subpixel_precision) * movit_texel_subpixel_precision;
175         
176         // Choose total weight w so that we minimize total squared error
177         // for the effective weights:
178         //
179         //   e = (w(1-z) - a)² + (wz - b)²
180         //
181         // Differentiating by w and setting equal to zero:
182         //
183         //   2(w(1-z) - a)(1-z) + 2(wz - b)z = 0
184         //   w(1-z)² - a(1-z) + wz² - bz = 0
185         //   w((1-z)² + z²) = a(1-z) + bz
186         //   w = (a(1-z) + bz) / ((1-z)² + z²)
187         //
188         // If z had infinite precision, this would simply reduce to w = w1 + w2.
189         *total_weight = (w1 * (1 - z) + w2 * z) / (z * z + (1 - z) * (1 - z));
190         *offset = z;
191
192         if (sum_sq_error != NULL) {
193                 float err1 = *total_weight * (1 - z) - w1;
194                 float err2 = *total_weight * z - w2;
195                 *sum_sq_error = err1 * err1 + err2 * err2;
196         }
197
198         assert(*offset >= 0.0f);
199         assert(*offset <= 1.0f);
200 }
201
202 GLuint fill_vertex_attribute(GLuint glsl_program_num, const string &attribute_name, GLint size, GLenum type, GLsizeiptr data_size, const GLvoid *data)
203 {
204         int attrib = glGetAttribLocation(glsl_program_num, attribute_name.c_str());
205         if (attrib == -1) {
206                 return -1;
207         }
208
209         GLuint vbo;
210         glGenBuffers(1, &vbo);
211         check_error();
212         glBindBuffer(GL_ARRAY_BUFFER, vbo);
213         check_error();
214         glBufferData(GL_ARRAY_BUFFER, data_size, data, GL_STATIC_DRAW);
215         check_error();
216         glEnableVertexAttribArray(attrib);
217         check_error();
218         glVertexAttribPointer(attrib, size, type, GL_FALSE, 0, BUFFER_OFFSET(0));
219         check_error();
220         glBindBuffer(GL_ARRAY_BUFFER, 0);
221         check_error();
222
223         return vbo;
224 }
225
226 void cleanup_vertex_attribute(GLuint glsl_program_num, const string &attribute_name, GLuint vbo)
227 {
228         int attrib = glGetAttribLocation(glsl_program_num, attribute_name.c_str());
229         if (attrib == -1) {
230                 return;
231         }
232
233         glDisableVertexAttribArray(attrib);
234         check_error();
235         glDeleteBuffers(1, &vbo);
236         check_error();
237 }
238
239 unsigned div_round_up(unsigned a, unsigned b)
240 {
241         return (a + b - 1) / b;
242 }
243
244 // Algorithm from http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2.
245 unsigned next_power_of_two(unsigned v)
246 {
247         v--;
248         v |= v >> 1;
249         v |= v >> 2;
250         v |= v >> 4;
251         v |= v >> 8;
252         v |= v >> 16;
253         v++;
254         return v;
255 }
256
257 void *get_gl_context_identifier()
258 {
259 #if defined(__DARWIN__)
260         return (void *)CGLGetCurrentContext();
261 #elif defined(WIN32)
262         return (void *)wglGetCurrentContext();
263 #else
264         return (void *)glXGetCurrentContext();
265 #endif
266 }
267
268 }  // namespace movit