]> git.sesse.net Git - movit/blob - util.h
Fix a bug where having two DeconvolutionSharpenEffects in one chain would cause shade...
[movit] / util.h
1 #ifndef _MOVIT_UTIL_H
2 #define _MOVIT_UTIL_H 1
3
4 // Various utilities.
5
6 #include <epoxy/gl.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <Eigen/Core>
10 #include <string>
11
12 #define BUFFER_OFFSET(i) ((char *)NULL + (i))
13
14 namespace movit {
15
16 // Converts a HSV color to RGB. Assumes h in [0, 2pi> or [-pi, pi>
17 void hsv2rgb(float h, float s, float v, float *r, float *g, float *b);
18
19 // Converts a HSV color to RGB, but keeps luminance constant
20 // (ie. color luminance is as if S=0).
21 void hsv2rgb_normalized(float h, float s, float v, float *r, float *g, float *b);
22
23 // Read a file from disk and return its contents.
24 // Dies if the file does not exist.
25 std::string read_file(const std::string &filename);
26
27 // Reads <base>.<extension>, <base>.130.<extension> or <base>.300es.<extension> and
28 // returns its contents, depending on <movit_shader_level>.
29 std::string read_version_dependent_file(const std::string &base, const std::string &extension);
30
31 // Compile the given GLSL shader (typically a vertex or fragment shader)
32 // and return the object number.
33 GLuint compile_shader(const std::string &shader_src, GLenum type);
34
35 // Print a 3x3 matrix to standard output. Useful for debugging.
36 void print_3x3_matrix(const Eigen::Matrix3d &m);
37
38 // Output a GLSL 3x3 matrix declaration.
39 std::string output_glsl_mat3(const std::string &name, const Eigen::Matrix3d &m);
40
41 // Calculate a / b, rounding up. Does not handle overflow correctly.
42 unsigned div_round_up(unsigned a, unsigned b);
43
44 // Calculate where to sample, and with what weight, if one wants to use
45 // the GPU's bilinear hardware to sample w1 * x[0] + w2 * x[1].
46 //
47 // Note that since the GPU might have limited precision in its linear
48 // interpolation, the effective weights might be different from the ones you
49 // asked for. sum_sq_error, if not NULL, will contain the sum of the
50 // (estimated) squared errors of the two weights.
51 void combine_two_samples(float w1, float w2, float *offset, float *total_weight, float *sum_sq_error);
52
53 // Create a VBO with the given data, and bind it to the vertex attribute
54 // with name <attribute_name>. Returns the VBO number.
55 GLuint fill_vertex_attribute(GLuint glsl_program_num, const std::string &attribute_name, GLint size, GLenum type, GLsizeiptr data_size, const GLvoid *data);
56
57 // Clean up after fill_vertex_attribute().
58 void cleanup_vertex_attribute(GLuint glsl_program_num, const std::string &attribute_name, GLuint vbo);
59
60 // If v is not already a power of two, return the first higher power of two.
61 unsigned next_power_of_two(unsigned v);
62
63 // Get a pointer that represents the current OpenGL context, in a cross-platform way.
64 // This is not intended for anything but identification (ie., so you can associate
65 // different FBOs with different contexts); you should probably not try to cast it
66 // back into anything you intend to pass into OpenGL.
67 void *get_gl_context_identifier();
68
69 }  // namespace movit
70
71 #ifdef NDEBUG
72 #define check_error()
73 #else
74 #define check_error() { int err = glGetError(); if (err != GL_NO_ERROR) { printf("GL error 0x%x at %s:%d\n", err, __FILE__, __LINE__); abort(); } }
75 #endif
76
77 // CHECK() is like assert(), but retains any side effects no matter the compilation mode.
78 #ifdef NDEBUG
79 #define CHECK(x) (void)(x)
80 #else
81 #define CHECK(x) do { bool ok = x; if (!ok) { fprintf(stderr, "%s:%d: %s: Assertion `%s' failed.\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #x); abort(); } } while (false)
82 #endif
83
84 #endif // !defined(_MOVIT_UTIL_H)