12 #include "bundled_shaders.h"
17 #if defined(__APPLE__)
18 #include <OpenGL/OpenGL.h>
20 #include <epoxy/wgl.h>
22 #include <epoxy/glx.h>
23 #include <epoxy/egl.h>
30 extern string *movit_data_directory;
32 void hsv2rgb(float h, float s, float v, float *r, float *g, float *b)
38 float hp = (h * 180.0 / M_PI) / 60.0;
39 float x = c * (1 - fabs(fmod(hp, 2.0f) - 1.0f));
41 if (hp >= 0 && hp < 1) {
45 } else if (hp >= 1 && hp < 2) {
49 } else if (hp >= 2 && hp < 3) {
53 } else if (hp >= 3 && hp < 4) {
57 } else if (hp >= 4 && hp < 5) {
73 void hsv2rgb_normalized(float h, float s, float v, float *r, float *g, float *b)
75 float ref_r, ref_g, ref_b;
76 hsv2rgb(h, s, v, r, g, b);
77 hsv2rgb(h, 0.0f, v, &ref_r, &ref_g, &ref_b);
78 float lum = 0.2126 * *r + 0.7152 * *g + 0.0722 * *b;
79 float ref_lum = 0.2126 * ref_r + 0.7152 * ref_g + 0.0722 * ref_b;
81 float fac = ref_lum / lum;
88 string read_file_from_bundle(const string &filename)
90 for (const BundledShader *shader = bundled_shaders; shader->filename != nullptr; ++shader) {
91 if (shader->filename == filename) {
92 return string(shader_bundle + shader->offset, shader->length);
95 return ""; // Not found.
98 string read_file(const string &filename)
100 string contents_from_bundle = read_file_from_bundle(filename);
102 // If no data directory has been given, we read only from the bundle.
103 if (*movit_data_directory == "") {
104 if (contents_from_bundle.empty()) {
105 fprintf(stderr, "%s: Shader not compiled in, and no data directory has been given.\n", filename.c_str());
108 return contents_from_bundle;
112 // If we're given a data directory, we still support reading from the bundle,
113 // but a successful read from the file system takes priority.
114 const string full_pathname = *movit_data_directory + "/" + filename;
116 FILE *fp = fopen(full_pathname.c_str(), "r");
118 if (!contents_from_bundle.empty()) {
119 return contents_from_bundle;
121 perror(full_pathname.c_str());
125 int ret = fseek(fp, 0, SEEK_END);
127 if (!contents_from_bundle.empty()) {
129 return contents_from_bundle;
131 perror("fseek(SEEK_END)");
135 int size = ftell(fp);
137 ret = fseek(fp, 0, SEEK_SET);
139 if (!contents_from_bundle.empty()) {
141 return contents_from_bundle;
143 perror("fseek(SEEK_SET)");
149 ret = fread(&str[0], size, 1, fp);
151 if (!contents_from_bundle.empty()) {
153 return contents_from_bundle;
159 if (!contents_from_bundle.empty()) {
161 return contents_from_bundle;
163 fprintf(stderr, "Short read when trying to read %d bytes from %s\n",
164 size, full_pathname.c_str());
172 string read_version_dependent_file(const string &base, const string &extension)
174 if (movit_shader_model == MOVIT_GLSL_130) {
175 return read_file(base + ".130." + extension);
176 } else if (movit_shader_model == MOVIT_GLSL_150) {
177 return read_file(base + ".150." + extension);
178 } else if (movit_shader_model == MOVIT_ESSL_300) {
179 return read_file(base + ".300es." + extension);
185 GLuint compile_shader(const string &shader_src, GLenum type)
187 GLuint obj = glCreateShader(type);
188 const GLchar* source[] = { shader_src.data() };
189 const GLint length[] = { (GLint)shader_src.size() };
190 glShaderSource(obj, 1, source, length);
191 glCompileShader(obj);
193 GLchar info_log[4096];
194 GLsizei log_length = sizeof(info_log) - 1;
195 glGetShaderInfoLog(obj, log_length, &log_length, info_log);
196 info_log[log_length] = 0;
197 if (strlen(info_log) > 0) {
198 fprintf(stderr, "Shader compile log: %s\n", info_log);
202 glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
203 if (status == GL_FALSE) {
204 // Add some line numbers to easier identify compile errors.
205 string src_with_lines = "/* 1 */ ";
207 for (char ch : shader_src) {
208 src_with_lines.push_back(ch);
211 snprintf(buf, sizeof(buf), "/* %3zu */ ", ++lineno);
212 src_with_lines += buf;
216 fprintf(stderr, "Failed to compile shader:\n%s\n", src_with_lines.c_str());
223 void print_3x3_matrix(const Eigen::Matrix3d& m)
225 printf("%6.4f %6.4f %6.4f\n", m(0,0), m(0,1), m(0,2));
226 printf("%6.4f %6.4f %6.4f\n", m(1,0), m(1,1), m(1,2));
227 printf("%6.4f %6.4f %6.4f\n", m(2,0), m(2,1), m(2,2));
231 string output_glsl_mat3(const string &name, const Eigen::Matrix3d &m)
233 // Use stringstream to be independent of the current locale in a thread-safe manner.
235 ss.imbue(locale("C"));
238 ss << "const mat3 " << name << " = mat3(\n";
239 ss << " " << m(0,0) << ", " << m(1,0) << ", " << m(2,0) << ",\n";
240 ss << " " << m(0,1) << ", " << m(1,1) << ", " << m(2,1) << ",\n";
241 ss << " " << m(0,2) << ", " << m(1,2) << ", " << m(2,2) << ");\n\n";
245 string output_glsl_float(const string &name, float x)
247 // Use stringstream to be independent of the current locale in a thread-safe manner.
249 ss.imbue(locale("C"));
252 ss << "const float " << name << " = " << x << ";\n";
256 string output_glsl_vec2(const string &name, float x, float y)
258 // Use stringstream to be independent of the current locale in a thread-safe manner.
260 ss.imbue(locale("C"));
263 ss << "const vec2 " << name << " = vec2(" << x << ", " << y << ");\n";
267 string output_glsl_vec3(const string &name, float x, float y, float z)
269 // Use stringstream to be independent of the current locale in a thread-safe manner.
271 ss.imbue(locale("C"));
274 ss << "const vec3 " << name << " = vec3(" << x << ", " << y << ", " << z << ");\n";
278 GLuint generate_vbo(GLint size, GLenum type, GLsizeiptr data_size, const GLvoid *data)
281 glGenBuffers(1, &vbo);
283 glBindBuffer(GL_ARRAY_BUFFER, vbo);
285 glBufferData(GL_ARRAY_BUFFER, data_size, data, GL_STATIC_DRAW);
287 glBindBuffer(GL_ARRAY_BUFFER, 0);
293 GLuint fill_vertex_attribute(GLuint glsl_program_num, const string &attribute_name, GLint size, GLenum type, GLsizeiptr data_size, const GLvoid *data)
295 int attrib = glGetAttribLocation(glsl_program_num, attribute_name.c_str());
300 GLuint vbo = generate_vbo(size, type, data_size, data);
302 glBindBuffer(GL_ARRAY_BUFFER, vbo);
304 glEnableVertexAttribArray(attrib);
306 glVertexAttribPointer(attrib, size, type, GL_FALSE, 0, BUFFER_OFFSET(0));
308 glBindBuffer(GL_ARRAY_BUFFER, 0);
314 void cleanup_vertex_attribute(GLuint glsl_program_num, const string &attribute_name, GLuint vbo)
316 int attrib = glGetAttribLocation(glsl_program_num, attribute_name.c_str());
321 glDisableVertexAttribArray(attrib);
323 glDeleteBuffers(1, &vbo);
327 unsigned div_round_up(unsigned a, unsigned b)
329 return (a + b - 1) / b;
332 // Algorithm from http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2.
333 unsigned next_power_of_two(unsigned v)
345 void *get_gl_context_identifier()
347 #if defined(__APPLE__)
348 return (void *)CGLGetCurrentContext();
350 return (void *)wglGetCurrentContext();
352 void *ret = (void *)eglGetCurrentContext();
353 if (ret != nullptr) {
356 return (void *)glXGetCurrentContext();
360 void abort_gl_error(GLenum err, const char *filename, int line)
362 const char *err_text = "unknown";
364 // All errors listed in the glGetError(3G) man page.
367 err_text = "GL_NO_ERROR"; // Should not happen.
369 case GL_INVALID_ENUM:
370 err_text = "GL_INVALID_ENUM";
372 case GL_INVALID_VALUE:
373 err_text = "GL_INVALID_VALUE";
375 case GL_INVALID_OPERATION:
376 err_text = "GL_INVALID_OPERATION";
378 case GL_INVALID_FRAMEBUFFER_OPERATION:
379 err_text = "GL_INVALID_FRAMEBUFFER_OPERATION";
381 case GL_OUT_OF_MEMORY:
382 err_text = "GL_OUT_OF_MEMORY";
384 case GL_STACK_UNDERFLOW:
385 err_text = "GL_STACK_UNDERFLOW";
387 case GL_STACK_OVERFLOW:
388 err_text = "GL_STACK_OVERFLOW";
391 fprintf(stderr, "GL error 0x%x (%s) at %s:%d\n", err, err_text, filename, line);