13 #if defined(__APPLE__)
14 #include <OpenGL/OpenGL.h>
16 #include <epoxy/wgl.h>
18 #include <epoxy/glx.h>
25 extern string *movit_data_directory;
27 void hsv2rgb(float h, float s, float v, float *r, float *g, float *b)
33 float hp = (h * 180.0 / M_PI) / 60.0;
34 float x = c * (1 - fabs(fmod(hp, 2.0f) - 1.0f));
36 if (hp >= 0 && hp < 1) {
40 } else if (hp >= 1 && hp < 2) {
44 } else if (hp >= 2 && hp < 3) {
48 } else if (hp >= 3 && hp < 4) {
52 } else if (hp >= 4 && hp < 5) {
68 void hsv2rgb_normalized(float h, float s, float v, float *r, float *g, float *b)
70 float ref_r, ref_g, ref_b;
71 hsv2rgb(h, s, v, r, g, b);
72 hsv2rgb(h, 0.0f, v, &ref_r, &ref_g, &ref_b);
73 float lum = 0.2126 * *r + 0.7152 * *g + 0.0722 * *b;
74 float ref_lum = 0.2126 * ref_r + 0.7152 * ref_g + 0.0722 * ref_b;
76 float fac = ref_lum / lum;
83 string read_file(const string &filename)
85 const string full_pathname = *movit_data_directory + "/" + filename;
87 static char buf[131072];
88 FILE *fp = fopen(full_pathname.c_str(), "r");
90 perror(full_pathname.c_str());
94 int len = fread(buf, 1, sizeof(buf), fp);
97 return string(buf, len);
100 string read_version_dependent_file(const string &base, const string &extension)
102 if (movit_shader_model == MOVIT_GLSL_110) {
103 return read_file(base + "." + extension);
104 } else if (movit_shader_model == MOVIT_GLSL_130) {
105 return read_file(base + ".130." + extension);
106 } else if (movit_shader_model == MOVIT_ESSL_300) {
107 return read_file(base + ".300es." + extension);
113 GLuint compile_shader(const string &shader_src, GLenum type)
115 GLuint obj = glCreateShader(type);
116 const GLchar* source[] = { shader_src.data() };
117 const GLint length[] = { (GLint)shader_src.size() };
118 glShaderSource(obj, 1, source, length);
119 glCompileShader(obj);
121 GLchar info_log[4096];
122 GLsizei log_length = sizeof(info_log) - 1;
123 glGetShaderInfoLog(obj, log_length, &log_length, info_log);
124 info_log[log_length] = 0;
125 if (strlen(info_log) > 0) {
126 fprintf(stderr, "Shader compile log: %s\n", info_log);
130 glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
131 if (status == GL_FALSE) {
138 void print_3x3_matrix(const Eigen::Matrix3d& m)
140 printf("%6.4f %6.4f %6.4f\n", m(0,0), m(0,1), m(0,2));
141 printf("%6.4f %6.4f %6.4f\n", m(1,0), m(1,1), m(1,2));
142 printf("%6.4f %6.4f %6.4f\n", m(2,0), m(2,1), m(2,2));
146 string output_glsl_mat3(const string &name, const Eigen::Matrix3d &m)
150 "const mat3 %s = mat3(\n"
151 " %.8f, %.8f, %.8f,\n"
152 " %.8f, %.8f, %.8f,\n"
153 " %.8f, %.8f, %.8f);\n\n",
155 m(0,0), m(1,0), m(2,0),
156 m(0,1), m(1,1), m(2,1),
157 m(0,2), m(1,2), m(2,2));
161 void combine_two_samples(float w1, float w2, float pos1, float pos2, unsigned size, CombineRoundingBehavior rounding_behavior,
162 float *offset, float *total_weight, float *sum_sq_error)
164 assert(movit_initialized);
165 assert(w1 * w2 >= 0.0f); // Should not have differing signs.
166 float z; // Normalized 0..1 between pos1 and pos2.
167 if (fabs(w1 + w2) < 1e-6) {
173 *offset = pos1 + z * (pos2 - pos1);
174 if (rounding_behavior == COMBINE_ROUND_TO_FP16) {
175 // Round to fp16. Note that this might take z outside the 0..1 range.
176 *offset = fp16_to_fp64(fp64_to_fp16(*offset));
177 z = (z - pos1) / (pos2 - pos1);
179 assert(rounding_behavior == COMBINE_DO_NOT_ROUND);
182 // Round to the minimum number of bits we have measured earlier.
183 // The card will do this for us anyway, but if we know what the real z
184 // is, we can pick a better total_weight below.
185 z *= size; // Move to pixel coordinates,
186 z = lrintf(z / movit_texel_subpixel_precision) * movit_texel_subpixel_precision; // Round.
187 z /= size; // Move back to normalized coordinates.
189 // Choose total weight w so that we minimize total squared error
190 // for the effective weights:
192 // e = (w(1-z) - a)² + (wz - b)²
194 // Differentiating by w and setting equal to zero:
196 // 2(w(1-z) - a)(1-z) + 2(wz - b)z = 0
197 // w(1-z)² - a(1-z) + wz² - bz = 0
198 // w((1-z)² + z²) = a(1-z) + bz
199 // w = (a(1-z) + bz) / ((1-z)² + z²)
201 // If z had infinite precision, this would simply reduce to w = w1 + w2.
202 *total_weight = (w1 * (1 - z) + w2 * z) / (z * z + (1 - z) * (1 - z));
204 if (sum_sq_error != NULL) {
205 float err1 = *total_weight * (1 - z) - w1;
206 float err2 = *total_weight * z - w2;
207 *sum_sq_error = err1 * err1 + err2 * err2;
211 GLuint fill_vertex_attribute(GLuint glsl_program_num, const string &attribute_name, GLint size, GLenum type, GLsizeiptr data_size, const GLvoid *data)
213 int attrib = glGetAttribLocation(glsl_program_num, attribute_name.c_str());
219 glGenBuffers(1, &vbo);
221 glBindBuffer(GL_ARRAY_BUFFER, vbo);
223 glBufferData(GL_ARRAY_BUFFER, data_size, data, GL_STATIC_DRAW);
225 glEnableVertexAttribArray(attrib);
227 glVertexAttribPointer(attrib, size, type, GL_FALSE, 0, BUFFER_OFFSET(0));
229 glBindBuffer(GL_ARRAY_BUFFER, 0);
235 void cleanup_vertex_attribute(GLuint glsl_program_num, const string &attribute_name, GLuint vbo)
237 int attrib = glGetAttribLocation(glsl_program_num, attribute_name.c_str());
242 glDisableVertexAttribArray(attrib);
244 glDeleteBuffers(1, &vbo);
248 unsigned div_round_up(unsigned a, unsigned b)
250 return (a + b - 1) / b;
253 // Algorithm from http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2.
254 unsigned next_power_of_two(unsigned v)
266 void *get_gl_context_identifier()
268 #if defined(__APPLE__)
269 return (void *)CGLGetCurrentContext();
271 return (void *)wglGetCurrentContext();
273 return (void *)glXGetCurrentContext();