]> git.sesse.net Git - movit/blob - util.h
There's no need to #undef PREFIX, since we do the token pasting ourselves.
[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 #include "defs.h"
12 #include "fp16.h"
13
14 #define BUFFER_OFFSET(i) ((char *)NULL + (i))
15
16 namespace movit {
17
18 // Converts a HSV color to RGB. Assumes h in [0, 2pi> or [-pi, pi>
19 void hsv2rgb(float h, float s, float v, float *r, float *g, float *b);
20
21 // Converts a HSV color to RGB, but keeps luminance constant
22 // (ie. color luminance is as if S=0).
23 void hsv2rgb_normalized(float h, float s, float v, float *r, float *g, float *b);
24
25 // Read a file from disk and return its contents.
26 // Dies if the file does not exist.
27 std::string read_file(const std::string &filename);
28
29 // Reads <base>.<extension>, <base>.130.<extension> or <base>.300es.<extension> and
30 // returns its contents, depending on <movit_shader_level>.
31 std::string read_version_dependent_file(const std::string &base, const std::string &extension);
32
33 // Compile the given GLSL shader (typically a vertex or fragment shader)
34 // and return the object number.
35 GLuint compile_shader(const std::string &shader_src, GLenum type);
36
37 // Print a 3x3 matrix to standard output. Useful for debugging.
38 void print_3x3_matrix(const Eigen::Matrix3d &m);
39
40 // Output a GLSL 3x3 matrix declaration.
41 std::string output_glsl_mat3(const std::string &name, const Eigen::Matrix3d &m);
42
43 // Output GLSL scalar, 2-length and 3-length vector declarations.
44 std::string output_glsl_float(const std::string &name, float x);
45 std::string output_glsl_vec2(const std::string &name, float x, float y);
46 std::string output_glsl_vec3(const std::string &name, float x, float y, float z);
47
48 // Calculate a / b, rounding up. Does not handle overflow correctly.
49 unsigned div_round_up(unsigned a, unsigned b);
50
51 enum CombineRoundingBehavior {
52         COMBINE_DO_NOT_ROUND = 0,
53         COMBINE_ROUND_TO_FP16 = 1,
54 };
55
56 // Calculate where to sample, and with what weight, if one wants to use
57 // the GPU's bilinear hardware to sample w1 * x[pos1] + w2 * x[pos2],
58 // where pos1 and pos2 must be normalized coordinates describing neighboring
59 // texels in the mipmap level at which you sample. <num_subtexels> is the
60 // number of distinct accessible subtexels in the given mipmap level,
61 // calculated by num_texels / movit_texel_subpixel_precision. It is a float
62 // for performance reasons, even though it is expected to be a whole number.
63 // <inv_num_subtexels> is simply its inverse (1/x). <pos1_pos2_diff> is
64 // (pos2-pos1) and <inv_pos1_pos2_diff> is 1/(pos2-pos1).
65 //
66 // Note that since the GPU might have limited precision in its linear
67 // interpolation, the effective weights might be different from the ones you
68 // asked for. sum_sq_error, if not NULL, will contain the sum of the
69 // (estimated) squared errors of the two weights.
70 //
71 // The answer, in "offset", comes as a normalized coordinate,
72 // so if e.g. w2 = 0, you have simply offset = pos1. If <rounding_behavior>
73 // is COMBINE_ROUND_TO_FP16, the coordinate is assumed to be stored as a
74 // rounded fp16 value. This enables more precise calculation of total_weight
75 // and sum_sq_error.
76 template<class DestFloat>
77 void combine_two_samples(float w1, float w2, float pos1, float pos1_pos2_diff, float inv_pos1_pos2_diff, float num_subtexels, float inv_num_subtexels,
78                          DestFloat *offset, DestFloat *total_weight, float *sum_sq_error)
79 {
80         assert(w1 * w2 >= 0.0f);  // Should not have differing signs.
81         float z;  // Normalized 0..1 between pos1 and pos2.
82         if (fabs(w1 + w2) < 1e-6) {
83                 z = 0.5f;
84         } else {
85                 z = w2 / (w1 + w2);
86         }
87
88         // Round to the desired precision. Note that this might take z outside the 0..1 range.
89         *offset = from_fp32<DestFloat>(pos1 + z * pos1_pos2_diff);
90         z = (to_fp32(*offset) - pos1) * inv_pos1_pos2_diff;
91
92         // Round to the minimum number of bits we have measured earlier.
93         // The card will do this for us anyway, but if we know what the real z
94         // is, we can pick a better total_weight below.
95         z = lrintf(z * num_subtexels) * inv_num_subtexels;
96
97         // Choose total weight w so that we minimize total squared error
98         // for the effective weights:
99         //
100         //   e = (w(1-z) - a)² + (wz - b)²
101         //
102         // Differentiating by w and setting equal to zero:
103         //
104         //   2(w(1-z) - a)(1-z) + 2(wz - b)z = 0
105         //   w(1-z)² - a(1-z) + wz² - bz = 0
106         //   w((1-z)² + z²) = a(1-z) + bz
107         //   w = (a(1-z) + bz) / ((1-z)² + z²)
108         //
109         // If z had infinite precision, this would simply reduce to w = w1 + w2.
110         *total_weight = from_fp32<DestFloat>((w1 + z * (w2 - w1)) / (z * z + (1 - z) * (1 - z)));
111
112         if (sum_sq_error != NULL) {
113                 float err1 = to_fp32(*total_weight) * (1 - z) - w1;
114                 float err2 = to_fp32(*total_weight) * z - w2;
115                 *sum_sq_error = err1 * err1 + err2 * err2;
116         }
117 }
118
119 // Create a VBO with the given data. Returns the VBO number.
120 GLuint generate_vbo(GLint size, GLenum type, GLsizeiptr data_size, const GLvoid *data);
121
122 // Create a VBO with the given data, and bind it to the vertex attribute
123 // with name <attribute_name>. Returns the VBO number.
124 GLuint fill_vertex_attribute(GLuint glsl_program_num, const std::string &attribute_name, GLint size, GLenum type, GLsizeiptr data_size, const GLvoid *data);
125
126 // Clean up after fill_vertex_attribute().
127 void cleanup_vertex_attribute(GLuint glsl_program_num, const std::string &attribute_name, GLuint vbo);
128
129 // If v is not already a power of two, return the first higher power of two.
130 unsigned next_power_of_two(unsigned v);
131
132 // Get a pointer that represents the current OpenGL context, in a cross-platform way.
133 // This is not intended for anything but identification (ie., so you can associate
134 // different FBOs with different contexts); you should probably not try to cast it
135 // back into anything you intend to pass into OpenGL.
136 void *get_gl_context_identifier();
137
138 // Used in the check_error() macro, below.
139 void abort_gl_error(GLenum err, const char *filename, int line) DOES_NOT_RETURN;
140
141 }  // namespace movit
142
143 #ifdef NDEBUG
144 #define check_error()
145 #else
146 #define check_error() { GLenum err = glGetError(); if (err != GL_NO_ERROR) { movit::abort_gl_error(err, __FILE__, __LINE__); } }
147 #endif
148
149 // CHECK() is like assert(), but retains any side effects no matter the compilation mode.
150 #ifdef NDEBUG
151 #define CHECK(x) (void)(x)
152 #else
153 #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)
154 #endif
155
156 #endif // !defined(_MOVIT_UTIL_H)