]> git.sesse.net Git - movit/blob - util.cpp
Fix non-float framebuffers in EffectChainTester.
[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 GLuint compile_shader(const string &shader_src, GLenum type)
100 {
101         GLuint obj = glCreateShader(type);
102         const GLchar* source[] = { shader_src.data() };
103         const GLint length[] = { (GLint)shader_src.size() };
104         glShaderSource(obj, 1, source, length);
105         glCompileShader(obj);
106
107         GLchar info_log[4096];
108         GLsizei log_length = sizeof(info_log) - 1;
109         glGetShaderInfoLog(obj, log_length, &log_length, info_log);
110         info_log[log_length] = 0; 
111         if (strlen(info_log) > 0) {
112                 fprintf(stderr, "Shader compile log: %s\n", info_log);
113         }
114
115         GLint status;
116         glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
117         if (status == GL_FALSE) {
118                 exit(1);
119         }
120
121         return obj;
122 }
123
124 void print_3x3_matrix(const Eigen::Matrix3d& m)
125 {
126         printf("%6.4f %6.4f %6.4f\n", m(0,0), m(0,1), m(0,2));
127         printf("%6.4f %6.4f %6.4f\n", m(1,0), m(1,1), m(1,2));
128         printf("%6.4f %6.4f %6.4f\n", m(2,0), m(2,1), m(2,2));
129         printf("\n");
130 }
131
132 string output_glsl_mat3(const string &name, const Eigen::Matrix3d &m)
133 {
134         char buf[1024];
135         sprintf(buf,
136                 "const mat3 %s = mat3(\n"
137                 "    %.8f, %.8f, %.8f,\n"
138                 "    %.8f, %.8f, %.8f,\n"
139                 "    %.8f, %.8f, %.8f);\n\n",
140                 name.c_str(),
141                 m(0,0), m(1,0), m(2,0),
142                 m(0,1), m(1,1), m(2,1),
143                 m(0,2), m(1,2), m(2,2));
144         return buf;
145 }
146
147 void combine_two_samples(float w1, float w2, float *offset, float *total_weight, float *sum_sq_error)
148 {
149         assert(movit_initialized);
150         assert(w1 * w2 >= 0.0f);  // Should not have differing signs.
151         float z;  // Just a shorter name for offset.
152         if (fabs(w1 + w2) < 1e-6) {
153                 z = 0.5f;
154         } else {
155                 z = w2 / (w1 + w2);
156         }
157
158         // Round to the minimum number of bits we have measured earlier.
159         // The card will do this for us anyway, but if we know what the real z
160         // is, we can pick a better total_weight below.
161         z = lrintf(z / movit_texel_subpixel_precision) * movit_texel_subpixel_precision;
162         
163         // Choose total weight w so that we minimize total squared error
164         // for the effective weights:
165         //
166         //   e = (w(1-z) - a)² + (wz - b)²
167         //
168         // Differentiating by w and setting equal to zero:
169         //
170         //   2(w(1-z) - a)(1-z) + 2(wz - b)z = 0
171         //   w(1-z)² - a(1-z) + wz² - bz = 0
172         //   w((1-z)² + z²) = a(1-z) + bz
173         //   w = (a(1-z) + bz) / ((1-z)² + z²)
174         //
175         // If z had infinite precision, this would simply reduce to w = w1 + w2.
176         *total_weight = (w1 * (1 - z) + w2 * z) / (z * z + (1 - z) * (1 - z));
177         *offset = z;
178
179         if (sum_sq_error != NULL) {
180                 float err1 = *total_weight * (1 - z) - w1;
181                 float err2 = *total_weight * z - w2;
182                 *sum_sq_error = err1 * err1 + err2 * err2;
183         }
184
185         assert(*offset >= 0.0f);
186         assert(*offset <= 1.0f);
187 }
188
189 GLuint fill_vertex_attribute(GLuint glsl_program_num, const string &attribute_name, GLint size, GLenum type, GLsizeiptr data_size, const GLvoid *data)
190 {
191         int attrib = glGetAttribLocation(glsl_program_num, attribute_name.c_str());
192         if (attrib == -1) {
193                 return -1;
194         }
195
196         GLuint vbo;
197         glGenBuffers(1, &vbo);
198         check_error();
199         glBindBuffer(GL_ARRAY_BUFFER, vbo);
200         check_error();
201         glBufferData(GL_ARRAY_BUFFER, data_size, data, GL_STATIC_DRAW);
202         check_error();
203         glEnableVertexAttribArray(attrib);
204         check_error();
205         glVertexAttribPointer(attrib, size, type, GL_FALSE, 0, BUFFER_OFFSET(0));
206         check_error();
207         glBindBuffer(GL_ARRAY_BUFFER, 0);
208         check_error();
209
210         return vbo;
211 }
212
213 void cleanup_vertex_attribute(GLuint glsl_program_num, const string &attribute_name, GLuint vbo)
214 {
215         int attrib = glGetAttribLocation(glsl_program_num, attribute_name.c_str());
216         if (attrib == -1) {
217                 return;
218         }
219
220         glDisableVertexAttribArray(attrib);
221         check_error();
222         glDeleteBuffers(1, &vbo);
223         check_error();
224 }
225
226 unsigned div_round_up(unsigned a, unsigned b)
227 {
228         return (a + b - 1) / b;
229 }
230
231 // Algorithm from http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2.
232 unsigned next_power_of_two(unsigned v)
233 {
234         v--;
235         v |= v >> 1;
236         v |= v >> 2;
237         v |= v >> 4;
238         v |= v >> 8;
239         v |= v >> 16;
240         v++;
241         return v;
242 }
243
244 void *get_gl_context_identifier()
245 {
246 #if defined(__DARWIN__)
247         return (void *)CGLGetCurrentContext();
248 #elif defined(WIN32)
249         return (void *)wglGetCurrentContext();
250 #else
251         return (void *)glXGetCurrentContext();
252 #endif
253 }
254
255 }  // namespace movit