2 #define _MOVIT_UTIL_H 1
13 #define BUFFER_OFFSET(i) ((char *)NULL + (i))
17 // Converts a HSV color to RGB. Assumes h in [0, 2pi> or [-pi, pi>
18 void hsv2rgb(float h, float s, float v, float *r, float *g, float *b);
20 // Converts a HSV color to RGB, but keeps luminance constant
21 // (ie. color luminance is as if S=0).
22 void hsv2rgb_normalized(float h, float s, float v, float *r, float *g, float *b);
24 // Read a file from disk and return its contents.
25 // Dies if the file does not exist.
26 std::string read_file(const std::string &filename);
28 // Reads <base>.<extension>, <base>.130.<extension> or <base>.300es.<extension> and
29 // returns its contents, depending on <movit_shader_level>.
30 std::string read_version_dependent_file(const std::string &base, const std::string &extension);
32 // Compile the given GLSL shader (typically a vertex or fragment shader)
33 // and return the object number.
34 GLuint compile_shader(const std::string &shader_src, GLenum type);
36 // Print a 3x3 matrix to standard output. Useful for debugging.
37 void print_3x3_matrix(const Eigen::Matrix3d &m);
39 // Output a GLSL 3x3 matrix declaration.
40 std::string output_glsl_mat3(const std::string &name, const Eigen::Matrix3d &m);
42 // Output GLSL scalar, 2-length and 3-length vector declarations.
43 std::string output_glsl_float(const std::string &name, float x);
44 std::string output_glsl_vec2(const std::string &name, float x, float y);
45 std::string output_glsl_vec3(const std::string &name, float x, float y, float z);
47 // Calculate a / b, rounding up. Does not handle overflow correctly.
48 unsigned div_round_up(unsigned a, unsigned b);
50 enum CombineRoundingBehavior {
51 COMBINE_DO_NOT_ROUND = 0,
52 COMBINE_ROUND_TO_FP16 = 1,
55 // Calculate where to sample, and with what weight, if one wants to use
56 // the GPU's bilinear hardware to sample w1 * x[pos1] + w2 * x[pos2],
57 // where pos1 and pos2 must be normalized coordinates describing neighboring
58 // texels in the mipmap level at which you sample. <num_subtexels> is the
59 // number of distinct accessible subtexels in the given mipmap level,
60 // calculated by num_texels / movit_texel_subpixel_precision. It is a float
61 // for performance reasons, even though it is expected to be a whole number.
62 // <inv_num_subtexels> is simply its inverse (1/x).
64 // Note that since the GPU might have limited precision in its linear
65 // interpolation, the effective weights might be different from the ones you
66 // asked for. sum_sq_error, if not NULL, will contain the sum of the
67 // (estimated) squared errors of the two weights.
69 // The answer, in "offset", comes as a normalized coordinate,
70 // so if e.g. w2 = 0, you have simply offset = pos1. If <rounding_behavior>
71 // is COMBINE_ROUND_TO_FP16, the coordinate is assumed to be stored as a
72 // rounded fp16 value. This enables more precise calculation of total_weight
74 template<class DestFloat>
75 void combine_two_samples(float w1, float w2, float pos1, float pos2, float num_subtexels, float inv_num_subtexels,
76 DestFloat *offset, DestFloat *total_weight, float *sum_sq_error);
78 // Create a VBO with the given data. Returns the VBO number.
79 GLuint generate_vbo(GLint size, GLenum type, GLsizeiptr data_size, const GLvoid *data);
81 // Create a VBO with the given data, and bind it to the vertex attribute
82 // with name <attribute_name>. Returns the VBO number.
83 GLuint fill_vertex_attribute(GLuint glsl_program_num, const std::string &attribute_name, GLint size, GLenum type, GLsizeiptr data_size, const GLvoid *data);
85 // Clean up after fill_vertex_attribute().
86 void cleanup_vertex_attribute(GLuint glsl_program_num, const std::string &attribute_name, GLuint vbo);
88 // If v is not already a power of two, return the first higher power of two.
89 unsigned next_power_of_two(unsigned v);
91 // Get a pointer that represents the current OpenGL context, in a cross-platform way.
92 // This is not intended for anything but identification (ie., so you can associate
93 // different FBOs with different contexts); you should probably not try to cast it
94 // back into anything you intend to pass into OpenGL.
95 void *get_gl_context_identifier();
97 // Used in the check_error() macro, below.
98 void abort_gl_error(GLenum err, const char *filename, int line) DOES_NOT_RETURN;
103 #define check_error()
105 #define check_error() { GLenum err = glGetError(); if (err != GL_NO_ERROR) { movit::abort_gl_error(err, __FILE__, __LINE__); } }
108 // CHECK() is like assert(), but retains any side effects no matter the compilation mode.
110 #define CHECK(x) (void)(x)
112 #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)
115 #endif // !defined(_MOVIT_UTIL_H)