16 #if defined(__APPLE__)
17 #include <OpenGL/OpenGL.h>
19 #include <epoxy/wgl.h>
21 #include <epoxy/glx.h>
28 extern string *movit_data_directory;
30 void hsv2rgb(float h, float s, float v, float *r, float *g, float *b)
36 float hp = (h * 180.0 / M_PI) / 60.0;
37 float x = c * (1 - fabs(fmod(hp, 2.0f) - 1.0f));
39 if (hp >= 0 && hp < 1) {
43 } else if (hp >= 1 && hp < 2) {
47 } else if (hp >= 2 && hp < 3) {
51 } else if (hp >= 3 && hp < 4) {
55 } else if (hp >= 4 && hp < 5) {
71 void hsv2rgb_normalized(float h, float s, float v, float *r, float *g, float *b)
73 float ref_r, ref_g, ref_b;
74 hsv2rgb(h, s, v, r, g, b);
75 hsv2rgb(h, 0.0f, v, &ref_r, &ref_g, &ref_b);
76 float lum = 0.2126 * *r + 0.7152 * *g + 0.0722 * *b;
77 float ref_lum = 0.2126 * ref_r + 0.7152 * ref_g + 0.0722 * ref_b;
79 float fac = ref_lum / lum;
86 string read_file(const string &filename)
88 const string full_pathname = *movit_data_directory + "/" + filename;
90 FILE *fp = fopen(full_pathname.c_str(), "r");
92 perror(full_pathname.c_str());
96 int ret = fseek(fp, 0, SEEK_END);
98 perror("fseek(SEEK_END)");
102 int size = ftell(fp);
104 ret = fseek(fp, 0, SEEK_SET);
106 perror("fseek(SEEK_SET)");
112 ret = fread(&str[0], size, 1, fp);
118 fprintf(stderr, "Short read when trying to read %d bytes from %s\n",
119 size, full_pathname.c_str());
127 string read_version_dependent_file(const string &base, const string &extension)
129 if (movit_shader_model == MOVIT_GLSL_110) {
130 return read_file(base + "." + extension);
131 } else if (movit_shader_model == MOVIT_GLSL_130) {
132 return read_file(base + ".130." + extension);
133 } else if (movit_shader_model == MOVIT_ESSL_300) {
134 return read_file(base + ".300es." + extension);
140 GLuint compile_shader(const string &shader_src, GLenum type)
142 GLuint obj = glCreateShader(type);
143 const GLchar* source[] = { shader_src.data() };
144 const GLint length[] = { (GLint)shader_src.size() };
145 glShaderSource(obj, 1, source, length);
146 glCompileShader(obj);
148 GLchar info_log[4096];
149 GLsizei log_length = sizeof(info_log) - 1;
150 glGetShaderInfoLog(obj, log_length, &log_length, info_log);
151 info_log[log_length] = 0;
152 if (strlen(info_log) > 0) {
153 fprintf(stderr, "Shader compile log: %s\n", info_log);
157 glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
158 if (status == GL_FALSE) {
159 fprintf(stderr, "Failed to compile shader: %s\n", shader_src.c_str());
166 void print_3x3_matrix(const Eigen::Matrix3d& m)
168 printf("%6.4f %6.4f %6.4f\n", m(0,0), m(0,1), m(0,2));
169 printf("%6.4f %6.4f %6.4f\n", m(1,0), m(1,1), m(1,2));
170 printf("%6.4f %6.4f %6.4f\n", m(2,0), m(2,1), m(2,2));
174 string output_glsl_mat3(const string &name, const Eigen::Matrix3d &m)
176 // Use stringstream to be independent of the current locale in a thread-safe manner.
178 ss.imbue(locale("C"));
180 ss << "const mat3 " << name << " = mat3(\n";
181 ss << " " << m(0,0) << ", " << m(1,0) << ", " << m(2,0) << ",\n";
182 ss << " " << m(0,1) << ", " << m(1,1) << ", " << m(2,1) << ",\n";
183 ss << " " << m(0,2) << ", " << m(1,2) << ", " << m(2,2) << ");\n\n";
187 string output_glsl_float(const string &name, float x)
189 // Use stringstream to be independent of the current locale in a thread-safe manner.
191 ss.imbue(locale("C"));
193 ss << "const float " << name << " = " << x << ";\n";
197 string output_glsl_vec2(const string &name, float x, float y)
199 // Use stringstream to be independent of the current locale in a thread-safe manner.
201 ss.imbue(locale("C"));
203 ss << "const vec2 " << name << " = vec2(" << x << ", " << y << ");\n";
207 string output_glsl_vec3(const string &name, float x, float y, float z)
209 // Use stringstream to be independent of the current locale in a thread-safe manner.
211 ss.imbue(locale("C"));
213 ss << "const vec3 " << name << " = vec3(" << x << ", " << y << ", " << z << ");\n";
217 template<class DestFloat>
218 void combine_two_samples(float w1, float w2, float pos1, float pos2, unsigned size,
219 DestFloat *offset, DestFloat *total_weight, float *sum_sq_error)
221 assert(movit_initialized);
222 assert(w1 * w2 >= 0.0f); // Should not have differing signs.
223 float z; // Normalized 0..1 between pos1 and pos2.
224 if (fabs(w1 + w2) < 1e-6) {
230 // Round to the desired precision. Note that this might take z outside the 0..1 range.
231 *offset = from_fp64<DestFloat>(pos1 + z * (pos2 - pos1));
232 z = (to_fp64(*offset) - pos1) / (pos2 - pos1);
234 // Round to the minimum number of bits we have measured earlier.
235 // The card will do this for us anyway, but if we know what the real z
236 // is, we can pick a better total_weight below.
237 z *= size; // Move to pixel coordinates,
238 z = lrintf(z / movit_texel_subpixel_precision) * movit_texel_subpixel_precision; // Round.
239 z /= size; // Move back to normalized coordinates.
241 // Choose total weight w so that we minimize total squared error
242 // for the effective weights:
244 // e = (w(1-z) - a)² + (wz - b)²
246 // Differentiating by w and setting equal to zero:
248 // 2(w(1-z) - a)(1-z) + 2(wz - b)z = 0
249 // w(1-z)² - a(1-z) + wz² - bz = 0
250 // w((1-z)² + z²) = a(1-z) + bz
251 // w = (a(1-z) + bz) / ((1-z)² + z²)
253 // If z had infinite precision, this would simply reduce to w = w1 + w2.
254 *total_weight = (w1 * (1 - z) + w2 * z) / (z * z + (1 - z) * (1 - z));
256 if (sum_sq_error != NULL) {
257 float err1 = *total_weight * (1 - z) - w1;
258 float err2 = *total_weight * z - w2;
259 *sum_sq_error = err1 * err1 + err2 * err2;
263 // Explicit instantiations.
265 void combine_two_samples<float>(float w1, float w2, float pos1, float pos2, unsigned size,
266 float *offset, float *total_weight, float *sum_sq_error);
269 void combine_two_samples<fp16_int_t>(float w1, float w2, float pos1, float pos2, unsigned size,
270 fp16_int_t *offset, fp16_int_t *total_weight, float *sum_sq_error);
272 GLuint fill_vertex_attribute(GLuint glsl_program_num, const string &attribute_name, GLint size, GLenum type, GLsizeiptr data_size, const GLvoid *data)
274 int attrib = glGetAttribLocation(glsl_program_num, attribute_name.c_str());
280 glGenBuffers(1, &vbo);
282 glBindBuffer(GL_ARRAY_BUFFER, vbo);
284 glBufferData(GL_ARRAY_BUFFER, data_size, data, GL_STATIC_DRAW);
286 glEnableVertexAttribArray(attrib);
288 glVertexAttribPointer(attrib, size, type, GL_FALSE, 0, BUFFER_OFFSET(0));
290 glBindBuffer(GL_ARRAY_BUFFER, 0);
296 void cleanup_vertex_attribute(GLuint glsl_program_num, const string &attribute_name, GLuint vbo)
298 int attrib = glGetAttribLocation(glsl_program_num, attribute_name.c_str());
303 glDisableVertexAttribArray(attrib);
305 glDeleteBuffers(1, &vbo);
309 unsigned div_round_up(unsigned a, unsigned b)
311 return (a + b - 1) / b;
314 // Algorithm from http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2.
315 unsigned next_power_of_two(unsigned v)
327 void *get_gl_context_identifier()
329 #if defined(__APPLE__)
330 return (void *)CGLGetCurrentContext();
332 return (void *)wglGetCurrentContext();
334 return (void *)glXGetCurrentContext();